npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hyper-connect

v0.12.0

Published

A client built on fetch to consume hyper services

Downloads

876

Readme


Table of Contents


Install

NodeJS

npm install hyper-connect

hyper-connect constructs a Request Object and sends it to the hyper server using fetch. hyper-connect wraps your hyper app's REST API, generating short-lived JWTs using the provided connection string.

Getting Started

New Experimental Feature: hyper Queue worker support, see below

NodeJS (TypeScript)

import { connect } from 'hyper-connect'

const hyper = connect(process.env.HYPER as string)

await hyper.data.add({ id: 'game-1', type: 'game', name: 'Donkey Kong' })
await hyper.data.add({ id: 'game-2', type: 'game', name: 'Pac Man' })
await hyper.data.add({ id: 'game-3', type: 'game', name: 'Galaga' })

const results = await hyper.data.query({ type: 'game' })

NodeJS (ESM)

import { connect } from 'hyper-connect'

const hyper = connect(process.env.HYPER)

await hyper.data.add({ id: 'game-1', type: 'game', name: 'Donkey Kong' })
await hyper.data.add({ id: 'game-2', type: 'game', name: 'Pac Man' })
await hyper.data.add({ id: 'game-3', type: 'game', name: 'Galaga' })

const results = await hyper.data.query({ type: 'game' })

NodeJS (CJS)

const { connect } = require('hyper-connect')

const hyper = connect(process.env.HYPER)

await hyper.data.add({ id: 'game-1', type: 'game', name: 'Donkey Kong' })
await hyper.data.add({ id: 'game-2', type: 'game', name: 'Pac Man' })
await hyper.data.add({ id: 'game-3', type: 'game', name: 'Galaga' })

const results = await hyper.data.query({ type: 'game' })

A Note for NodeJS

For Node environments, starting with v0.5.0, hyper-connect's Storage service api returns a Web ReadableStream instead of a NodeJS.ReadableStream. If you'd like a NodeJS.ReadableStream, follow one of the approaches below.

If you're using node>=17, you can use Node's built in fromWeb to get a Node stream:

import { createReadStream } from 'node:fs'
import { Readable } from 'node:stream'

// Convert the ReadableStream to a NodeJS.ReadableStream
await hyper.storage.download('foo.png')
  .then((res) => {
    if (!res.ok) throw res
    return Readable.fromWeb(res.object)
  })

// Or convert to a ReadbleStream from a NodeJS.ReadableStream
await hyper.storage.upload('foo.png', Readable.toWeb(createReadStream('foo.png')))

Otherwise, you will need to use v0.4.0 or less of hyper-connect. Node 18 will be in LTS soon, and we recommend upgrading to Node 18 LTS as soon as possible, to take advantage of the new Web Standards centric features, like global fetch and WebStreams.

Node 18 and localhost

Starting with Node 17, Node has changed how it resolves localhost, when using global fetch and fetch from libraries like undici. This may cause requests to localhost not to resolve correctly and fail. To get around this, you can use 127.0.0.1 or 0.0.0.0, in lieu of localhost. For more info, See this issue

Deno

import { connect } from 'https://x.nest.land/hyper-connect@VERSION/deno/mod.ts'

const HYPER = Deno.env.get('HYPER') // connect string: cloud://key:[email protected]/:app

const hyper = connect(HYPER)()

await hyper.data.add({ id: 'game-1', type: 'game', name: 'Donkey Kong' })
await hyper.data.add({ id: 'game-2', type: 'game', name: 'Pac Man' })
await hyper.data.add({ id: 'game-3', type: 'game', name: 'Galaga' })

const results = await hyper.data.query({ type: 'game' })

With hyper-connect, you can access all of the hyper services. hyper-connect uses the fetch library to execute REST requests for you.

Examples

How to add a document to hyper data?

const doc = {
  id: 'movie-1',
  type: 'movie',
  title: 'Dune',
  year: '2021',
}

const result = await hyper.data.add(doc)
console.log(result) // {ok: true, id: "movie-1"}

How to get all the documents of type 'movie'?

const result = await hyper.data.query({ type: 'movie' })
console.log(result) // {ok: true, docs: [...]}

How to add a cache key/value pair to hyper cache?

const result = await hyper.cache.add('key', { counter: 1 })
console.log(result) // {ok: true}

Documentation

hyper is a suite of service apis, with hyper connect you can specify the api you want to connect with and the action you want to perform. hyper.[service].[action] - with each service there are a different set of actions to call. This table breaks down the service and action with description of the action.

data

| Service | Action | Description | | ------- | ------ | ------------------------------------------------------------------- | | data | add | creates a json document in the hyper data store | | data | list | lists the documents given a start,stop,limit range | | data | get | retrieves a document by id | | data | update | updates a given document by id | | data | remove | removes a document from the store | | data | query | queries the store for a set of documents based on selector criteria | | data | index | creates an index for the data store | | data | bulk | inserts, updates, and removed document via a batch of documents |

cache

| Service | Action | Description | | ------- | ------ | ------------------------------------------------------------------- | | cache | add | creates a json document in the hyper cache store with a key | | cache | get | retrieves a document by key | | cache | set | sets a given document by key | | cache | remove | removes a document from the cache | | cache | query | queries the cache for a set of documents based on a pattern matcher |

search

| Service | Action | Description | | ------- | ------ | ------------------------------------------------- | | search | add | indexes a json document in the hyper search index | | search | get | retrieves a document from index | | search | remove | removes a document from the index | | search | query | searches index by text | | search | load | loads a batch of documents |

storage

| Service | Action | Description | | ------- | -------- | ---------------------------------------- | | storage | upload | adds object/file to hyper storage bucket | | storage | download | retrieves a object/file from bucket | | storage | remove | removes a object/file from the bucket |

queue

| Service | Action | Description | | ------- | ------- | ---------------------------------------------------------- | | queue | enqueue | posts object to queue | | queue | errors | gets list of errors occured with queue | | queue | queued | gets list of objects that are queued and ready to be sent. |


Verify Signature

hyper Queue allows you to create a target web hook endpoint to receive jobs, in order to secure that endpoint to only receive jobs from hyper, you can implement a secret, this secret using sha256 to encode a nounce timestamp and a signature of the job payload. We created a function on hyper-connect to make it easier to implement your own middleware to validate these incoming jobs in a secure way.

import { createHyperVerify } from 'hyper-connect'

const signatureVerify = createHyperVerify(process.env.QUEUE_SECRET, '1m')

export const validateSignature(req, res, next) {
  const result = signatureVerify(req.headers.get('x-hyper-signature'), req.body))
  if (!result.ok) {
    return res.setStatus(result.status).send({msg: result.msg})
  }
  next()
}

Contributing

  • deno task test to run unit tests
  • deno task test:integration to run integration tests. This ensures the Deno code is properly transformed into Node code. See the Node test harness for more info.

License

Apache 2.0