{"id":15497181,"url":"https://github.com/jwerle/hypersource","last_synced_at":"2025-08-13T21:32:50.079Z","repository":{"id":57270246,"uuid":"183091484","full_name":"jwerle/hypersource","owner":"jwerle","description":"Build WebSocket APIs that leverage the HyperCore Protocol","archived":false,"fork":false,"pushed_at":"2019-04-26T18:27:04.000Z","size":26,"stargazers_count":20,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-14T02:50:06.133Z","etag":null,"topics":["api","hypercore","protocol","service","web","websocket"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwerle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-23T20:31:13.000Z","updated_at":"2023-10-23T08:11:20.000Z","dependencies_parsed_at":"2022-08-25T03:41:09.679Z","dependency_job_id":null,"html_url":"https://github.com/jwerle/hypersource","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fhypersource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fhypersource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fhypersource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fhypersource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwerle","download_url":"https://codeload.github.com/jwerle/hypersource/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250328601,"owners_count":21412649,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","hypercore","protocol","service","web","websocket"],"created_at":"2024-10-02T08:31:28.029Z","updated_at":"2025-04-22T21:32:32.544Z","avatar_url":"https://github.com/jwerle.png","language":"JavaScript","readme":"hypersource\n===========\n\nBuild WebSocket APIs that leverage the HyperCore Protocol\n\n## Installation\n\n```sh\n$ npm install hypersource\n```\n\n## Example\n\nBelow creates a web socket server that echos the first value as a\nresponse.\n\n```js\n// can use hyperdrive, hypertrie, or hyperdb too\nconst hypercore = require('hypercore')\nconst ram = require('random-access-memory')\nconst hs = require('hypersource')\n\nconst server = hs.createServer((req, res) =\u003e {\n  console.log('request from:', req.url)\n\n  // 'req.key' points to the public key for this feed\n  const reader = hypercore(ram, req)\n\n  // 'res.key' and 'res.secretKey' contain the ephemeral key pair\n  // used for writing a response. The public key is stored in the\n  // 'userData' of the handshake\n  const writer = hypercore(ram, res)\n\n  // replicate from request\n  reader.replicate(req)\n  // replicate to response\n  writer.replicate(res)\n\n  // echo first value to writer\n  reader.get(0, (err, buf) =\u003e writer.append(buf))\n\n  // close response when writer uploads\n  writer.once('upload', () =\u003e res.destroy())\n})\n\nserver.listen(3000, () =\u003e {\n  const { protocol, address, port } = server.address()\n  console.log('Listening on %s//%s:%s', protocol, address, port)\n})\n```\n\nUsing [simple-websocket](https://github.com/feross/simple-websocket) we\ncan connect to this host and initiate a request.\n\n```js\nconst WebSocket = require('simple-websocket')\nconst hypercore = require('hypercore')\nconst pump = require('pump')\nconst ram = require('random-access-memory')\n\nconst request = hypercore(ram)\nrequest.ready(() =\u003e {\n  const key = request.key.toString('hex')\n  const stream = request.replicate({ live: true })\n  const socket = new WebSocket(`ws://localhost:3000/${key}`)\n\n  pump(stream, socket, stream).once('handshake', () =\u003e {\n    const remotePublicKey = stream.remoteUserData\n    const response = hypercore(ram, remotePublicKey)\n\n    request.append('hello world')\n    response.replicate({ stream, live: true })\n    response.get(0, (err, res) =\u003e {\n      console.log('response', res.toString()); // 'hello world'\n    })\n  })\n})\n\n```\n\n## API\n\n### `server = hs.createServer([opts[, onrequest]])`\n\nCreate a new hypersource web socket server where `onrequest` is called\nwhen the `'request'` event is emitted and `opts` is an optional object\nthat is passed directly to\n[simple-websocket `Server`](https://github.com/feross/simple-websocket#server).\n\n#### `server.listen(port[, host[, callback]])`\n\nListen on `port` on an optional `host` calling `callback` when the\n`'listening'` event is fired.\n\n#### `addrinfo = server.address()`\n\nReturns the address info for the server.\n\n```js\nconst addrinfo = server.address() // { protocol: 'ws:', address: '::', family: 'IPv6', port: 3000 }\n```\n\n#### `server.close()`\n\nClose the server.\n\n#### `server.on('error', error)`\n\nEmitted when an error occurs.\n\n#### `server.on('connection', socket, httpRequest)`\n\nEmitted when a connection has been established where `socket` is a\n`Duplex` stream that wraps the underlying web socket and `httpRequest` is a\n`http.IncomingMessage` containing HTTP request information.\n\n#### `server.on('request', request, response, discoveryKey)`\n\nEmitted when a request has been established where `request` and\n`response` both wrap\n[hypercore-protocol](https://github.com/mafintosh/hypercore-protocol)\nstreams and `discoveryKey` is the discovery key for the request.\n\nThe `request` and `response` object contains useful properties for\ncreating hypercore instances and replicating their feeds.\n\n##### `request.url`\n\nThe URL associated with the request.\n\n##### `request.method`\n\nThe HTTP method associated with the request.\n\n##### `request.headers`\n\nThe HTTP headers associated with the request.\n\n##### `request.key`\n\nThe public key associated with the request.\n\n##### `request.publicKey`\n\nAn alias to `request.key`.\n\n##### `request.discoveryKey`\n\nThe discovery key associated with the request.\n\n##### `request.stream`\n\nThe [hypercore-protocol](https://github.com/mafintosh/hypercore-protocol)\nstream that is associated with the request.\n\n##### `response.key`\n\nThe public key associated with the response.\n\n##### `response.publicKey`\n\nAn alias to `response.key`.\n\n##### `response.secretKey`\n\nThe secret key associated with the response.\n\n##### `response.discoveryKey`\n\nThe discovery key associated with the response.\n\n##### `request.stream`\n\nThe [hypercore-protocol](https://github.com/mafintosh/hypercore-protocol)\nstream that is associated with the request.\n\n## License\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwerle%2Fhypersource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwerle%2Fhypersource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwerle%2Fhypersource/lists"}