hypercore-fetch 8.3.0 → 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 +25 -2
- package/package.json +3 -3
- package/test.js +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')
|
|
@@ -66,8 +68,13 @@ module.exports = function makeHyperFetch (opts = {}) {
|
|
|
66
68
|
const allPeers = archive.peers
|
|
67
69
|
return allPeers.filter((peer) => {
|
|
68
70
|
const { remoteExtensions } = peer
|
|
71
|
+
|
|
72
|
+
if (!remoteExtensions) return false
|
|
73
|
+
|
|
69
74
|
const { names } = remoteExtensions
|
|
70
75
|
|
|
76
|
+
if (!names) return false
|
|
77
|
+
|
|
71
78
|
return names.includes(name)
|
|
72
79
|
})
|
|
73
80
|
}
|
|
@@ -270,11 +277,27 @@ module.exports = function makeHyperFetch (opts = {}) {
|
|
|
270
277
|
function onMessage (name, content, peer) {
|
|
271
278
|
const id = peer.remotePublicKey.toString('hex')
|
|
272
279
|
// TODO: Fancy verification on the `name`?
|
|
273
|
-
|
|
280
|
+
// Send each line of content separately on a `data` line
|
|
281
|
+
const data = content.split('\n').map((line) => `data:${line}\n`).join('')
|
|
282
|
+
push(`id:${id}\nevent:${name}\n${data}\n`)
|
|
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`)
|
|
274
293
|
}
|
|
275
294
|
archive.on(EXTENSION_EVENT, onMessage)
|
|
295
|
+
archive.on(PEER_OPEN, onPeerOpen)
|
|
296
|
+
archive.on(PEER_REMOVE, onPeerRemove)
|
|
276
297
|
return () => {
|
|
277
|
-
archive.removeListener(
|
|
298
|
+
archive.removeListener(EXTENSION_EVENT, onMessage)
|
|
299
|
+
archive.removeListener(PEER_OPEN, onPeerOpen)
|
|
300
|
+
archive.removeListener(PEER_REMOVE, onPeerRemove)
|
|
278
301
|
}
|
|
279
302
|
})
|
|
280
303
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hypercore-fetch",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.4.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"
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"event-iterator": "^2.0.0",
|
|
28
28
|
"fetch-headers": "^2.0.0",
|
|
29
|
-
"hyper-dns": "^0.
|
|
30
|
-
"hyper-sdk": "^3.0.
|
|
29
|
+
"hyper-dns": "^0.12.0",
|
|
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",
|
package/test.js
CHANGED
|
@@ -248,7 +248,7 @@ async function runTests () {
|
|
|
248
248
|
|
|
249
249
|
t.ok(data.value, 'Got eventsource data after writing')
|
|
250
250
|
t.ok(data.value.includes('event:example\n'), 'EventSource data represents an example event')
|
|
251
|
-
t.ok(data.value.includes('
|
|
251
|
+
t.ok(data.value.includes('data:Hello World\n'), 'EventSource data contains expected body')
|
|
252
252
|
t.ok(data.value.includes('id:'), 'EventSource data contains an ID')
|
|
253
253
|
t.ok(data.value.endsWith('\n\n'), 'Ends with two newlines')
|
|
254
254
|
|