hypercore-fetch 8.3.2 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +4 -0
  2. package/index.js +18 -2
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -212,6 +212,10 @@ Using the `text/event-stream` content type in the `Accept` header will get back
212
212
 
213
213
  The `event` will be the name of the extension you got the data for, the `id` (accessible by `e.lastEventId` in EventSource) will be set to the ID of the peer that sent it.
214
214
 
215
+ Only extension messages that have been queried before via a `GET` to the EXTENSION_NAME will be visible in this stream.
216
+
217
+ There are also two special events: `peer-open` which gets emitted whena new peer has connected, and `peer-remove` which gets emitted when an existing peer disconnects.
218
+
215
219
  ### `fetch('hyper://NAME/$/extensions/EXTENSION_NAME', {method: 'POST', body: 'Example'})`
216
220
 
217
221
  You can broadcast an extension message to all peers that are replicating that extension type with a `POST` to the extension's URL.
package/index.js CHANGED
@@ -24,6 +24,8 @@ const TAGS_FOLDER = SPECIAL_FOLDER + TAGS_FOLDER_NAME
24
24
  const EXTENSIONS_FOLDER_NAME = 'extensions/'
25
25
  const EXTENSIONS_FOLDER = SPECIAL_FOLDER + EXTENSIONS_FOLDER_NAME
26
26
  const EXTENSION_EVENT = 'extension-message'
27
+ const PEER_OPEN = 'peer-open'
28
+ const PEER_REMOVE = 'peer-remove'
27
29
 
28
30
  // TODO: Add caching support
29
31
  const { resolveURL: DEFAULT_RESOLVE_URL } = require('hyper-dns')
@@ -279,9 +281,23 @@ module.exports = function makeHyperFetch (opts = {}) {
279
281
  const data = content.split('\n').map((line) => `data:${line}\n`).join('')
280
282
  push(`id:${id}\nevent:${name}\n${data}\n`)
281
283
  }
284
+ function onPeerOpen (peer) {
285
+ const id = peer.remotePublicKey.toString('hex')
286
+ push(`id:${id}\nevent:${PEER_OPEN}\n\n`)
287
+ }
288
+ function onPeerRemove (peer) {
289
+ // Whatever, probably an uninitialized peer
290
+ if (!peer.remotePublicKey) return
291
+ const id = peer.remotePublicKey.toString('hex')
292
+ push(`id:${id}\nevent:${PEER_REMOVE}\n\n`)
293
+ }
282
294
  archive.on(EXTENSION_EVENT, onMessage)
295
+ archive.on(PEER_OPEN, onPeerOpen)
296
+ archive.on(PEER_REMOVE, onPeerRemove)
283
297
  return () => {
284
- archive.removeListener('extension-message', onMessage)
298
+ archive.removeListener(EXTENSION_EVENT, onMessage)
299
+ archive.removeListener(PEER_OPEN, onPeerOpen)
300
+ archive.removeListener(PEER_REMOVE, onPeerRemove)
285
301
  }
286
302
  })
287
303
 
@@ -389,7 +405,7 @@ module.exports = function makeHyperFetch (opts = {}) {
389
405
  return {
390
406
  statusCode: 200,
391
407
  headers: responseHeaders,
392
- data: intoAsyncIterable('')
408
+ data: intoAsyncIterable(canonical)
393
409
  }
394
410
  } else if (method === 'DELETE') {
395
411
  if (headers.get('x-clear') === 'cache') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-fetch",
3
- "version": "8.3.2",
3
+ "version": "8.5.0",
4
4
  "description": "Implementation of Fetch that uses the Dat SDK for loading p2p content",
5
5
  "bin": {
6
6
  "hypercore-fetch": "bin.js"
@@ -27,7 +27,7 @@
27
27
  "event-iterator": "^2.0.0",
28
28
  "fetch-headers": "^2.0.0",
29
29
  "hyper-dns": "^0.12.0",
30
- "hyper-sdk": "^3.0.8",
30
+ "hyper-sdk": "^3.0.9",
31
31
  "make-dir": "^3.1.0",
32
32
  "make-fetch": "^2.2.1",
33
33
  "mime": "^2.4.4",