@leofcoin/peernet 1.2.11 → 1.2.13
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/exports/browser/{client-CnZoUZcU.js → client-DN_Y7L6h.js} +1 -1
- package/exports/browser/{messages-BzbOJ4KS.js → messages-C-BwUsHC.js} +1 -1
- package/exports/browser/{peernet-DgNp4A5c.js → peernet-BpKdfmda.js} +3 -3
- package/exports/browser/peernet.js +1 -1
- package/exports/peernet.js +1 -1
- package/package.json +1 -1
- package/src/peernet.ts +5 -5
- package/test/all.js +5 -0
- package/test/index.js +4 -1
- package/test/peer-data-exchange.test.js +52 -0
- package/test/peer-file-exchange.test.js +78 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-
|
|
1
|
+
import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-BpKdfmda.js';
|
|
2
2
|
import './identity-CqSnKXWH.js';
|
|
3
3
|
import './value-C3vAp-wb.js';
|
|
4
4
|
|
|
@@ -8473,7 +8473,7 @@ class Peernet {
|
|
|
8473
8473
|
await getAddress();
|
|
8474
8474
|
this.storePrefix = options.storePrefix;
|
|
8475
8475
|
this.root = options.root;
|
|
8476
|
-
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-
|
|
8476
|
+
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-C-BwUsHC.js');
|
|
8477
8477
|
/**
|
|
8478
8478
|
* proto Object containing protos
|
|
8479
8479
|
* @type {Object}
|
|
@@ -8520,7 +8520,7 @@ class Peernet {
|
|
|
8520
8520
|
if (this.#starting || this.#started)
|
|
8521
8521
|
return;
|
|
8522
8522
|
this.#starting = true;
|
|
8523
|
-
const importee = await import('./client-
|
|
8523
|
+
const importee = await import('./client-DN_Y7L6h.js');
|
|
8524
8524
|
/**
|
|
8525
8525
|
* @access public
|
|
8526
8526
|
* @type {PeernetClient}
|
|
@@ -8967,7 +8967,7 @@ class Peernet {
|
|
|
8967
8967
|
const data = await shareStore.has(hash);
|
|
8968
8968
|
if (data)
|
|
8969
8969
|
return await shareStore.get(hash);
|
|
8970
|
-
return
|
|
8970
|
+
return this.requestData(hash, 'share');
|
|
8971
8971
|
},
|
|
8972
8972
|
/**
|
|
8973
8973
|
* put share content
|
package/exports/peernet.js
CHANGED
package/package.json
CHANGED
package/src/peernet.ts
CHANGED
|
@@ -730,7 +730,7 @@ export default class Peernet {
|
|
|
730
730
|
debug(`get share ${hash}`)
|
|
731
731
|
const data = await shareStore.has(hash)
|
|
732
732
|
if (data) return await shareStore.get(hash)
|
|
733
|
-
return
|
|
733
|
+
return this.requestData(hash, 'share')
|
|
734
734
|
},
|
|
735
735
|
/**
|
|
736
736
|
* put share content
|
|
@@ -754,7 +754,7 @@ export default class Peernet {
|
|
|
754
754
|
const links = []
|
|
755
755
|
for (const file of files) {
|
|
756
756
|
const fileNode = await new globalThis.peernet.protos['peernet-file'](file)
|
|
757
|
-
const hash = await fileNode.hash
|
|
757
|
+
const hash = await fileNode.hash()
|
|
758
758
|
await dataStore.put(hash, fileNode.encoded)
|
|
759
759
|
links.push({ hash, path: file.path })
|
|
760
760
|
}
|
|
@@ -762,7 +762,7 @@ export default class Peernet {
|
|
|
762
762
|
path: '/',
|
|
763
763
|
links
|
|
764
764
|
})
|
|
765
|
-
const hash = await node.hash
|
|
765
|
+
const hash = await node.hash()
|
|
766
766
|
await dataStore.put(hash, node.encoded)
|
|
767
767
|
|
|
768
768
|
return hash
|
|
@@ -783,7 +783,7 @@ export default class Peernet {
|
|
|
783
783
|
const links = []
|
|
784
784
|
for (const file of files) {
|
|
785
785
|
const fileNode = await new globalThis.peernet.protos['peernet-file'](file)
|
|
786
|
-
const hash = await fileNode.hash
|
|
786
|
+
const hash = await fileNode.hash()
|
|
787
787
|
await dataStore.put(hash, fileNode.encoded)
|
|
788
788
|
links.push({ hash, path: file.path })
|
|
789
789
|
}
|
|
@@ -791,7 +791,7 @@ export default class Peernet {
|
|
|
791
791
|
path: '/',
|
|
792
792
|
links
|
|
793
793
|
})
|
|
794
|
-
const hash = await node.hash
|
|
794
|
+
const hash = await node.hash()
|
|
795
795
|
await dataStore.put(hash, node.encoded)
|
|
796
796
|
|
|
797
797
|
return hash
|
package/test/all.js
ADDED
package/test/index.js
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import Peernet from '../exports/peernet.js'
|
|
4
|
+
import { createHash } from 'crypto'
|
|
5
|
+
|
|
6
|
+
// This test simulates two peers: one saves data, the other retrieves it
|
|
7
|
+
|
|
8
|
+
test('peer can save data and other peer can get it', async (t) => {
|
|
9
|
+
const password = 'password'
|
|
10
|
+
// Peer 1
|
|
11
|
+
const peer1 = await new Peernet(
|
|
12
|
+
{
|
|
13
|
+
network: 'leofcoin:peach',
|
|
14
|
+
stars: [],
|
|
15
|
+
root: '.testnet-peer1',
|
|
16
|
+
version: '1.0.0',
|
|
17
|
+
storePrefix: 'test-peer1',
|
|
18
|
+
autoStart: true
|
|
19
|
+
},
|
|
20
|
+
password
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
// Peer 2
|
|
24
|
+
const peer2 = await new Peernet(
|
|
25
|
+
{
|
|
26
|
+
network: 'leofcoin:peach',
|
|
27
|
+
stars: [],
|
|
28
|
+
root: '.testnet-peer2',
|
|
29
|
+
version: '1.0.0',
|
|
30
|
+
storePrefix: 'test-peer2',
|
|
31
|
+
autoStart: true
|
|
32
|
+
},
|
|
33
|
+
password
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
// Wait for both peers to be ready and connected
|
|
37
|
+
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
38
|
+
|
|
39
|
+
// Peer 1 saves data
|
|
40
|
+
const testData = new TextEncoder().encode('shared-data')
|
|
41
|
+
// Generate SHA-256 hash for the data
|
|
42
|
+
const hash = createHash('sha256').update(testData).digest('hex')
|
|
43
|
+
await peer1.put(hash, testData)
|
|
44
|
+
|
|
45
|
+
// Wait for DHT propagation
|
|
46
|
+
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
47
|
+
|
|
48
|
+
// Peer 2 tries to get the data
|
|
49
|
+
const received = await peer2.get(hash)
|
|
50
|
+
assert.ok(received, 'Peer 2 should retrieve the data by hash')
|
|
51
|
+
assert.equal(new TextDecoder().decode(received), 'shared-data')
|
|
52
|
+
})
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import Peernet from '../exports/peernet.js'
|
|
4
|
+
|
|
5
|
+
test('peer can save (put) and retrieve (get) PeernetFile messages', async (t) => {
|
|
6
|
+
const password = 'password'
|
|
7
|
+
|
|
8
|
+
// Peer 1
|
|
9
|
+
const peer1 = await new Peernet(
|
|
10
|
+
{
|
|
11
|
+
network: 'leofcoin:peach',
|
|
12
|
+
stars: [],
|
|
13
|
+
root: '.testnet-file-peer1',
|
|
14
|
+
version: '1.0.0',
|
|
15
|
+
storePrefix: 'test-file-peer1',
|
|
16
|
+
autoStart: true
|
|
17
|
+
},
|
|
18
|
+
password
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
// Peer 2
|
|
22
|
+
const peer2 = await new Peernet(
|
|
23
|
+
{
|
|
24
|
+
network: 'leofcoin:peach',
|
|
25
|
+
stars: [],
|
|
26
|
+
root: '.testnet-file-peer2',
|
|
27
|
+
version: '1.0.0',
|
|
28
|
+
storePrefix: 'test-file-peer2',
|
|
29
|
+
autoStart: true
|
|
30
|
+
},
|
|
31
|
+
password
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
// Wait for peers to connect
|
|
35
|
+
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
36
|
+
|
|
37
|
+
// Access the proto class from the global object (populated by Peernet)
|
|
38
|
+
const PeernetFile = globalThis.peernet.protos['peernet-file']
|
|
39
|
+
assert.ok(PeernetFile, 'PeernetFile proto should be available')
|
|
40
|
+
|
|
41
|
+
// Create a file message
|
|
42
|
+
const fileContent = `${Date.now()} - Hello, this is a test file content!`
|
|
43
|
+
const fileData = {
|
|
44
|
+
path: '/hello.txt',
|
|
45
|
+
content: new TextEncoder().encode(fileContent)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const fileMsg = new PeernetFile(fileData)
|
|
49
|
+
|
|
50
|
+
// Get hash and encoded data
|
|
51
|
+
// Note: hash might be a function
|
|
52
|
+
const hash = await fileMsg.hash()
|
|
53
|
+
const encoded = fileMsg.encoded
|
|
54
|
+
|
|
55
|
+
assert.ok(hash, 'File message should have a hash')
|
|
56
|
+
assert.ok(encoded, 'File message should have encoded data')
|
|
57
|
+
|
|
58
|
+
// Peer 1 puts the file message
|
|
59
|
+
await peer1.put(hash, encoded)
|
|
60
|
+
|
|
61
|
+
// Wait for propagation
|
|
62
|
+
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
63
|
+
|
|
64
|
+
// Peer 2 gets the file message
|
|
65
|
+
const retrievedEncoded = await peer2.get(hash)
|
|
66
|
+
|
|
67
|
+
assert.ok(retrievedEncoded, 'Peer 2 should be able to retrieve the file message')
|
|
68
|
+
|
|
69
|
+
// Decode and verify
|
|
70
|
+
const retrievedMsg = new PeernetFile(retrievedEncoded)
|
|
71
|
+
const decodedContent = new TextDecoder().decode(retrievedMsg.decoded.content)
|
|
72
|
+
|
|
73
|
+
assert.equal(decodedContent, fileContent, 'Retrieved content should match original')
|
|
74
|
+
assert.equal(retrievedMsg.decoded.path, fileData.path, 'Retrieved path should match original')
|
|
75
|
+
|
|
76
|
+
// Cleanup (if stop methods existed, checking if specific teardown is needed)
|
|
77
|
+
// peer1.close() // or similar if available
|
|
78
|
+
})
|