@leofcoin/peernet 1.2.12 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.2.12",
3
+ "version": "1.2.13",
4
4
  "description": "",
5
5
  "browser": "./exports/browser/peernet.js",
6
6
  "exports": {
package/src/peernet.ts CHANGED
@@ -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
@@ -0,0 +1,5 @@
1
+ import './codec.js'
2
+ import './messages.js'
3
+ import './peernet.test.js'
4
+ import './peer-data-exchange.test.js'
5
+ import './peer-file-exchange.test.js'
package/test/index.js CHANGED
@@ -1,3 +1,6 @@
1
- import './messages.js'
1
+ import './peernet.test.js'
2
+ import './peer-data-exchange.test.js'
3
+ import './peer-file-exchange.test.js'
4
+ // import './messages.js'
2
5
  // require('./peernet')
3
6
  // require('./hash')
@@ -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
+ })