hypercore-fetch 8.3.3 → 8.4.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.
- package/README.md +4 -0
- package/index.js +17 -1
- package/package.json +1 -1
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(
|
|
298
|
+
archive.removeListener(EXTENSION_EVENT, onMessage)
|
|
299
|
+
archive.removeListener(PEER_OPEN, onPeerOpen)
|
|
300
|
+
archive.removeListener(PEER_REMOVE, onPeerRemove)
|
|
285
301
|
}
|
|
286
302
|
})
|
|
287
303
|
|