0.12.0 • Published 3 months ago

hyper-connect v0.12.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 months ago

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:secret@cloud.hyper.io/: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

ServiceActionDescription
dataaddcreates a json document in the hyper data store
datalistlists the documents given a start,stop,limit range
datagetretrieves a document by id
dataupdateupdates a given document by id
dataremoveremoves a document from the store
dataqueryqueries the store for a set of documents based on selector criteria
dataindexcreates an index for the data store
databulkinserts, updates, and removed document via a batch of documents

cache

ServiceActionDescription
cacheaddcreates a json document in the hyper cache store with a key
cachegetretrieves a document by key
cachesetsets a given document by key
cacheremoveremoves a document from the cache
cachequeryqueries the cache for a set of documents based on a pattern matcher

search

ServiceActionDescription
searchaddindexes a json document in the hyper search index
searchgetretrieves a document from index
searchremoveremoves a document from the index
searchquerysearches index by text
searchloadloads a batch of documents

storage

ServiceActionDescription
storageuploadadds object/file to hyper storage bucket
storagedownloadretrieves a object/file from bucket
storageremoveremoves a object/file from the bucket

queue

ServiceActionDescription
queueenqueueposts object to queue
queueerrorsgets list of errors occured with queue
queuequeuedgets 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

0.12.0

3 months ago

0.11.0

5 months ago

0.10.0

6 months ago

0.9.0

7 months ago

0.8.1

8 months ago

0.7.2

12 months ago

0.8.0

11 months ago

0.7.1

1 year ago

0.7.0

1 year ago

0.6.2

1 year ago

0.6.1

1 year ago

0.5.3

1 year ago

0.5.2

2 years ago

0.6.0

1 year ago

0.3.0

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.5.1

2 years ago

0.2.1

2 years ago

0.2.2

2 years ago

0.1.20

2 years ago

0.2.0

2 years ago

0.1.15

2 years ago

0.1.16

2 years ago

0.1.17

2 years ago

0.1.18

2 years ago

0.1.19

2 years ago

0.1.10

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

0.1.13

2 years ago

0.1.14

2 years ago

0.1.9

2 years ago

0.1.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.8

2 years ago

0.0.15

3 years ago

0.1.7

2 years ago

0.0.16

3 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.1

3 years ago