@leofcoin/peernet 2.0.0 → 3.1.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 (124) hide show
  1. package/README.md +45 -0
  2. package/docs/automatic-transport.md +37 -0
  3. package/exports/browser/{client-CAC7RVMf.js → client-DKKzPSlb.js} +252 -76
  4. package/exports/browser/{messages-Dw86z0zq.js → messages-DCePyqKC.js} +1 -1
  5. package/exports/browser/{peernet-B2jRhqGI.js → peernet-DjddqiXc.js} +460 -18
  6. package/exports/browser/peernet.js +1 -1
  7. package/exports/peernet.js +445 -3
  8. package/exports/types/archive.d.ts +69 -0
  9. package/exports/types/peernet.d.ts +7 -1
  10. package/package.json +9 -5
  11. package/.esdoc.json +0 -10
  12. package/.gitattributes +0 -2
  13. package/.github/workflows/release.yml +0 -34
  14. package/.prettierrc +0 -7
  15. package/.travis.yml +0 -27
  16. package/BREAKING_CHANGES.md +0 -35
  17. package/deploy.js +0 -8
  18. package/exports/browser/browser-D-r0O9Qn.js +0 -53
  19. package/exports/browser/browser-DvU1xNFS.js +0 -147
  20. package/exports/browser/browser-_hiyXwPp.js +0 -147
  21. package/exports/browser/client-86Tnax66.js +0 -2224
  22. package/exports/browser/client-BFxJts6j.js +0 -2224
  23. package/exports/browser/client-BVhUamQG.js +0 -2200
  24. package/exports/browser/client-BzvWzzLv.js +0 -2224
  25. package/exports/browser/client-C9GBz2Tf.js +0 -2224
  26. package/exports/browser/client-CQzuu6At.js +0 -2224
  27. package/exports/browser/client-CVkFejdx.js +0 -2224
  28. package/exports/browser/client-Cj_UUO8W.js +0 -2224
  29. package/exports/browser/client-CrGaeBjj.js +0 -2224
  30. package/exports/browser/client-DDC8C510.js +0 -2224
  31. package/exports/browser/client-ERocgKET.js +0 -2224
  32. package/exports/browser/client-a8rws3cu.js +0 -2224
  33. package/exports/browser/identity-CYVPDmkf.js +0 -8402
  34. package/exports/browser/identity-ChyyZSve.js +0 -8397
  35. package/exports/browser/index-BD0Anx7_.js +0 -37
  36. package/exports/browser/index-Bxr5Iztg.js +0 -37
  37. package/exports/browser/messages-BFCmg6Vb.js +0 -212
  38. package/exports/browser/messages-BHIC2mQu.js +0 -212
  39. package/exports/browser/messages-CimzJ4wr.js +0 -210
  40. package/exports/browser/messages-Cjoa_LGO.js +0 -210
  41. package/exports/browser/messages-D7JxkkDW.js +0 -212
  42. package/exports/browser/messages-DH0tgNM9.js +0 -207
  43. package/exports/browser/messages-DVHcNvs_.js +0 -212
  44. package/exports/browser/messages-DqL_g2qw.js +0 -210
  45. package/exports/browser/messages-UKnuelZ7.js +0 -207
  46. package/exports/browser/messages-XgZZUMma.js +0 -212
  47. package/exports/browser/messages-kfFyctPL.js +0 -210
  48. package/exports/browser/messages-psROnfRi.js +0 -212
  49. package/exports/browser/peernet-B9VL4Xwe.js +0 -9522
  50. package/exports/browser/peernet-C-HNdrcd.js +0 -9528
  51. package/exports/browser/peernet-C4rOvyyU.js +0 -9661
  52. package/exports/browser/peernet-Ca1iM2tN.js +0 -9336
  53. package/exports/browser/peernet-CbI0-jFn.js +0 -9651
  54. package/exports/browser/peernet-CtHScDyh.js +0 -9524
  55. package/exports/browser/peernet-DfuxUK3u.js +0 -9525
  56. package/exports/browser/peernet-DiEcNDLD.js +0 -9655
  57. package/exports/browser/peernet-DtuRXOzP.js +0 -9653
  58. package/exports/browser/peernet-r_9IyzTe.js +0 -9336
  59. package/exports/browser/peernet-rcD5XL1y.js +0 -9590
  60. package/exports/browser/peernet-rw8HJGOz.js +0 -9597
  61. package/exports/messages-CHKVVTVE.js +0 -204
  62. package/exports/messages-DaUtFfBh.js +0 -207
  63. package/index.html +0 -19
  64. package/rollup.config.js +0 -143
  65. package/src/dht/dht.ts +0 -142
  66. package/src/discovery/peer-discovery.js +0 -75
  67. package/src/errors/errors.js +0 -12
  68. package/src/file-transfer.ts +0 -194
  69. package/src/handlers/data.js +0 -15
  70. package/src/handlers/message.js +0 -34
  71. package/src/identity.ts +0 -172
  72. package/src/messages/chat.ts +0 -13
  73. package/src/messages/data-response.ts +0 -13
  74. package/src/messages/data.ts +0 -17
  75. package/src/messages/dht-response.ts +0 -13
  76. package/src/messages/dht.ts +0 -21
  77. package/src/messages/file-link.ts +0 -17
  78. package/src/messages/file.ts +0 -17
  79. package/src/messages/peer-response.ts +0 -13
  80. package/src/messages/peer.ts +0 -13
  81. package/src/messages/peernet.ts +0 -13
  82. package/src/messages/ps.ts +0 -13
  83. package/src/messages/request.ts +0 -13
  84. package/src/messages/response.ts +0 -13
  85. package/src/messages.js +0 -12
  86. package/src/peer-info.js +0 -9
  87. package/src/peernet.ts +0 -1113
  88. package/src/prompts/password/browser.js +0 -1
  89. package/src/prompts/password/node.js +0 -6
  90. package/src/prompts/password.js +0 -1
  91. package/src/proto/chat-message.proto.js +0 -6
  92. package/src/proto/data-response.proto.js +0 -4
  93. package/src/proto/data.proto.js +0 -4
  94. package/src/proto/dht-response.proto.js +0 -4
  95. package/src/proto/dht.proto.js +0 -4
  96. package/src/proto/file-link.proto.js +0 -5
  97. package/src/proto/file.proto.js +0 -10
  98. package/src/proto/peer-response.proto.js +0 -3
  99. package/src/proto/peer.proto.js +0 -3
  100. package/src/proto/peernet.proto.js +0 -7
  101. package/src/proto/ps.proto.js +0 -4
  102. package/src/proto/request.proto.js +0 -4
  103. package/src/proto/response.proto.js +0 -3
  104. package/src/types.ts +0 -25
  105. package/src/utils/utils.js +0 -77
  106. package/test/all.js +0 -5
  107. package/test/client.js +0 -15
  108. package/test/codec.js +0 -56
  109. package/test/file-transfer.test.js +0 -81
  110. package/test/fresh-empty-store.js +0 -15
  111. package/test/hash.js +0 -14
  112. package/test/index.js +0 -6
  113. package/test/lastBlock.js +0 -7
  114. package/test/messages.js +0 -26
  115. package/test/peer-data-exchange.test.js +0 -52
  116. package/test/peer-file-exchange.test.js +0 -78
  117. package/test/peernet.js +0 -17
  118. package/test/peernet.test.js +0 -477
  119. package/test-output.log +0 -84
  120. package/test.js +0 -63
  121. package/test2.js +0 -13
  122. package/test3.js +0 -16
  123. package/test4.js +0 -7
  124. package/tsconfig.json +0 -13
@@ -1 +0,0 @@
1
- export default async () => prompt('enter password')
@@ -1,6 +0,0 @@
1
- import inquirer from 'inquirer'
2
-
3
- export default async () => {
4
- const answers = await inquirer.prompt({ type: 'password', name: 'password', message: 'Enter password' })
5
- return answers.password
6
- }
@@ -1 +0,0 @@
1
- export { default } from './password/browser.js'
@@ -1,6 +0,0 @@
1
- export default {
2
- value: new Uint8Array(),
3
- author: new Uint8Array(),
4
- timestamp: Number(),
5
- files: [new Uint8Array()]
6
- }
@@ -1,4 +0,0 @@
1
- export default {
2
- hash: String(),
3
- data: new Uint8Array()
4
- }
@@ -1,4 +0,0 @@
1
- export default {
2
- hash: String(),
3
- 'store?': String()
4
- }
@@ -1,4 +0,0 @@
1
- export default {
2
- hash: String(),
3
- has: Boolean()
4
- }
@@ -1,4 +0,0 @@
1
- export default {
2
- hash: String(),
3
- 'store?': String()
4
- }
@@ -1,5 +0,0 @@
1
- export default {
2
- hash: String(),
3
- path: String(),
4
- 'size?': Number()
5
- }
@@ -1,10 +0,0 @@
1
- export default {
2
- path: String(),
3
- 'content?': new Uint8Array(),
4
- 'links?': [],
5
- 'size?': Number(),
6
- 'chunkSize?': Number(),
7
- 'chunked?': Boolean(),
8
- 'kind?': String(),
9
- 'blockHash?': String()
10
- }
@@ -1,3 +0,0 @@
1
- export default {
2
- id: String()
3
- }
@@ -1,3 +0,0 @@
1
- export default {
2
- id: String()
3
- }
@@ -1,7 +0,0 @@
1
- export default {
2
- data: new Uint8Array(),
3
- signature: new Uint8Array(),
4
- 'from?': String(),
5
- 'to?': String(),
6
- 'id?': String()
7
- }
@@ -1,4 +0,0 @@
1
- export default {
2
- data: new Uint8Array(),
3
- topic: new Uint8Array()
4
- }
@@ -1,4 +0,0 @@
1
- export default {
2
- request: String(),
3
- 'requested?': new Uint8Array()
4
- }
@@ -1,3 +0,0 @@
1
- export default {
2
- response: new Uint8Array()
3
- }
package/src/types.ts DELETED
@@ -1,25 +0,0 @@
1
- import '@vandeurenglenn/debug'
2
- import PubSub from '@vandeurenglenn/little-pubsub'
3
- import Peernet from './peernet.js'
4
- import PeerDiscovery from './discovery/peer-discovery.js'
5
- import DHT from './dht/dht.js'
6
- import { BufferToUint8Array, protoFor, target } from './utils/utils.js'
7
- import MessageHandler from './handlers/message.js'
8
- import dataHandler from './handlers/data.js'
9
- import { encapsulatedError, dhtError, nothingFoundError } from './errors/errors.js'
10
-
11
- import { Storage as LeofcoinStorageClass } from '@leofcoin/storage'
12
- import { utils as codecUtils } from '@leofcoin/codecs'
13
- import Identity from './identity.js'
14
-
15
- declare global {
16
- var globalSub: PubSub
17
- var pubsub: PubSub
18
- var peernet: Peernet
19
- var LeofcoinStorage: typeof LeofcoinStorageClass
20
- var LeofcoinStorageClient
21
- var messageStore: LeofcoinStorageClass
22
- var dataStore: LeofcoinStorageClass
23
- var transactionStore: LeofcoinStorageClass
24
- var blockStore: LeofcoinStorageClass
25
- }
@@ -1,77 +0,0 @@
1
- import { Codec } from '@leofcoin/codec-format-interface'
2
-
3
- export const BufferToUint8Array = (data) => {
4
- if (data.type === 'Buffer') {
5
- data = new Uint8Array(data.data)
6
- }
7
- return data
8
- }
9
-
10
- export const expected = (expected, actual) => {
11
- const rule = (entry) => {
12
- return !entry ? `: undefined - ${entry} ` : `: ${typeof entry} - `
13
- }
14
-
15
- const entries = Object.entries(actual).map((entry) => entry.join(rule(entry[1])))
16
-
17
- return `\nExpected:
18
- ${expected.join('\n\t')}
19
-
20
- actual:
21
- ${entries.join('\n\t')}`
22
- }
23
-
24
- export const protoFor = (message) => {
25
- const codec = new Codec(message)
26
- if (!codec.name) throw new Error(`proto not found ${message}`)
27
- const Proto = globalThis.peernet.protos[codec.name]
28
- if (!Proto) throw new Error(`No proto defined for ${codec.name}`)
29
- return new Proto(message)
30
- }
31
-
32
- /**
33
- * wether or not a peernet daemon is active
34
- * @return {Boolean}
35
- */
36
- export const hasDaemon = async () => {
37
- try {
38
- let response = await fetch('http://127.0.0.1:1000/api/version')
39
- response = await response.json()
40
- return Boolean(response.client === '@peernet/api/http')
41
- } catch (e) {
42
- return false
43
- }
44
- }
45
-
46
- export const https = () => {
47
- if (!globalThis.location) return false
48
- return Boolean(globalThis.location.protocol === 'https:')
49
- }
50
-
51
- /**
52
- * Get current environment
53
- * @return {String} current environment [node, electron, browser]
54
- */
55
- export const environment = () => {
56
- const _navigator = globalThis.navigator
57
- if (!_navigator) {
58
- return 'node'
59
- } else if (_navigator && /electron/i.test(_navigator.userAgent)) {
60
- return 'electron'
61
- } else {
62
- return 'browser'
63
- }
64
- }
65
-
66
- /**
67
- * * Get current environment
68
- * @return {Object} result
69
- * @property {Boolean} reult.daemon whether or not daemon is running
70
- * @property {Boolean} reult.environment Current environment
71
- */
72
- export const target = async () => {
73
- let daemon = false
74
- if (!https()) daemon = await hasDaemon()
75
-
76
- return { daemon, environment: environment() }
77
- }
package/test/all.js DELETED
@@ -1,5 +0,0 @@
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/client.js DELETED
@@ -1,15 +0,0 @@
1
- const Client = require('./../client.js')
2
-
3
- const client = new Client()
4
-
5
- console.log(pubsub.subscribers);
6
- pubsub.subscribe('peer:connected', (data) => {
7
- console.log(data);
8
- })
9
- pubsub.subscribe('peer:data', (data) => {
10
- console.log(data);
11
- })
12
-
13
- // pubsub.subscribe('peer:connected', async peer => setInterval(async () => {
14
- // // console.log(await peer.request({type: 'block', index: 0}));
15
- // }, 5000))
package/test/codec.js DELETED
@@ -1,56 +0,0 @@
1
- import test from 'tape'
2
- import Codec from './../src/commonjs/codec.js'
3
-
4
- const block = '4lmacqas6kpnmvehxpmlvsndgydw5i46mf2pgnaub72eug63dozkw4oqxhk3fazbhkguvlhslcaeqm5jtwghxdk5nvmuakrs7yi2gl6xqmmmw';
5
- const itx = '5hmacqb6ao252jmbhkhu5isr4yua3jnkhgb5ejjbiomrlzglwarlb777wq7aw5nfz5m4pubx5uffaxp2cqc4ckrh4xirss4nkujvcjbnmomtm';
6
- const dht = 'b4wiyebqbigjfvm5mizik7ke3v6lyhoahecqiwergnl3cytarf5bd2qvqase'
7
- const message = '4xnmcay'
8
- const data = '4hemcay'
9
- globalThis.peernet = {};
10
- console.log(parseInt('706468', 16));
11
-
12
- test('peernet dht codec', tape => {
13
- tape.plan(2)
14
-
15
- const codec = new Codec(dht)
16
- const codec2 = new Codec(codec.encoded)
17
- tape.equal(codec.name, 'peernet-dht-response', `name from encoded is valid ${codec.name}`)
18
- tape.equal(codec.name, codec2.name, `decoded name is valid ${codec.name}`)
19
- })
20
-
21
- test('peernet message codec', tape => {
22
- tape.plan(2)
23
-
24
- const codec = new Codec(message)
25
- const codec2 = new Codec(codec.encoded)
26
- tape.equal(codec.name, 'peernet-message', `name from encoded is valid ${codec.name}`)
27
- tape.equal(codec.name, codec2.name, `decoded name is valid ${codec.name}`)
28
- })
29
-
30
- test('peernet data codec', tape => {
31
- tape.plan(2)
32
-
33
- const codec = new Codec(data)
34
- const codec2 = new Codec(codec.encoded)
35
- tape.equal(codec.name, 'peernet-data', `name from encoded is valid ${codec.name}`)
36
- tape.equal(codec.name, codec2.name, `decoded name is valid ${codec.name}`)
37
- })
38
-
39
- test('leofcoin block codec', tape => {
40
- tape.plan(2)
41
-
42
- const codec = new Codec(block)
43
- const codec2 = new Codec(codec.encoded)
44
- tape.equal(codec.name, 'leofcoin-block', `name from encoded is valid ${codec.name}`)
45
- tape.equal(codec.name, codec2.name, `decoded name is valid ${codec.name}`)
46
- })
47
-
48
- test('leofcoin itx codec', tape => {
49
- tape.plan(2)
50
-
51
- const codec = new Codec(itx)
52
-
53
- const codec2 = new Codec(codec.encoded)
54
- tape.equal(codec.name, 'leofcoin-itx', `name from encoded is valid ${codec.name}`)
55
- tape.equal(codec.name, codec2.name, `decoded name is valid ${codec.name}`)
56
- })
@@ -1,81 +0,0 @@
1
- import test from 'node:test'
2
- import assert from 'node:assert/strict'
3
- import { FileTransfer } from '../exports/peernet.js'
4
-
5
- const bytes = (value) => new TextEncoder().encode(JSON.stringify(value))
6
- const decode = async (value) => JSON.parse(new TextDecoder().decode(value))
7
-
8
- test('chunk transfer pauses, resumes, reassembles, and reports progress', async () => {
9
- const objects = new Map([
10
- ['manifest', bytes({ chunked: true, size: 6, links: [{ hash: 'a', size: 2 }, { hash: 'b', size: 2 }, { hash: 'c', size: 2 }] })],
11
- ['a', bytes({ content: [1, 2] })],
12
- ['b', bytes({ content: [3, 4] })],
13
- ['c', bytes({ content: [5, 6] })]
14
- ])
15
- const fetched = []
16
- const transfer = new FileTransfer({
17
- hash: 'manifest',
18
- fetch: async (hash) => {
19
- fetched.push(hash)
20
- return objects.get(hash)
21
- },
22
- decode: async (encoded) => {
23
- const value = await decode(encoded)
24
- if (value.content) value.content = new Uint8Array(value.content)
25
- return value
26
- },
27
- verify: async (_encoded, hash) => objects.has(hash)
28
- })
29
-
30
- transfer.pause()
31
- transfer.start()
32
- transfer.pause()
33
- await new Promise((resolve) => setTimeout(resolve, 10))
34
- assert.deepEqual(fetched, ['manifest'])
35
- assert.equal(transfer.state, 'paused')
36
-
37
- transfer.resume()
38
- assert.deepEqual(await transfer.result, new Uint8Array([1, 2, 3, 4, 5, 6]))
39
- assert.equal(transfer.state, 'completed')
40
- assert.equal(transfer.progress.completedChunks, 3)
41
- assert.equal(transfer.progress.transferredBytes, 6)
42
- })
43
-
44
- test('chunk transfer rejects failed integrity checks', async () => {
45
- const transfer = new FileTransfer({
46
- hash: 'bad',
47
- fetch: async () => bytes({ content: [1] }),
48
- decode,
49
- verify: async () => false
50
- }).start()
51
-
52
- await assert.rejects(transfer.result, /Manifest hash mismatch/)
53
- assert.equal(transfer.state, 'failed')
54
- })
55
-
56
- test('chunk transfer pipelines up to the configured concurrency', async () => {
57
- const links = Array.from({ length: 8 }, (_, index) => ({ hash: `chunk-${index}`, size: 1 }))
58
- let active = 0
59
- let maximumActive = 0
60
- const transfer = new FileTransfer({
61
- hash: 'manifest',
62
- concurrency: 4,
63
- fetch: async (hash) => {
64
- if (hash === 'manifest') return bytes({ chunked: true, size: 8, links })
65
- active += 1
66
- maximumActive = Math.max(maximumActive, active)
67
- await new Promise((resolve) => setTimeout(resolve, 5))
68
- active -= 1
69
- return bytes({ content: [Number(hash.split('-')[1])] })
70
- },
71
- decode: async (encoded) => {
72
- const value = await decode(encoded)
73
- if (value.content) value.content = new Uint8Array(value.content)
74
- return value
75
- },
76
- verify: async () => true
77
- }).start()
78
-
79
- assert.deepEqual(await transfer.result, new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]))
80
- assert.equal(maximumActive, 4)
81
- })
@@ -1,15 +0,0 @@
1
- import Peernet from '../exports/peernet.js'
2
-
3
- const peernet = await new Peernet(
4
- {
5
- network: 'leofcoin:peach',
6
- stars: [],
7
- root: process.argv[2],
8
- autoStart: false,
9
- freshIdentity: true
10
- },
11
- 'fresh-empty-store-test'
12
- )
13
-
14
- if (!peernet.id || !peernet.selectedAccount) throw new Error('fresh identity was not initialized')
15
- console.log('FRESH_IDENTITY_READY')
package/test/hash.js DELETED
@@ -1,14 +0,0 @@
1
- const test = require('tape')
2
- const Node = require('./../dist/hash/hash.js')
3
-
4
- const message = '4xnmcay'
5
-
6
- test('peernet message dht-response', tape => {
7
- tape.plan(2)
8
-
9
- const node = new Node(response)
10
- const node2 = new Node(node.encoded)
11
- console.log(node2.decoded.has);
12
- tape.equal(node.name, 'peernet-dht-response', `name from encoded is valid ${node.name}`)
13
- tape.equal(node.name, node2.name, `decoded name is valid ${node.name}`)
14
- })
package/test/index.js DELETED
@@ -1,6 +0,0 @@
1
- import './peernet.test.js'
2
- import './peer-data-exchange.test.js'
3
- import './peer-file-exchange.test.js'
4
- // import './messages.js'
5
- // require('./peernet')
6
- // require('./hash')
package/test/lastBlock.js DELETED
@@ -1,7 +0,0 @@
1
- peernet.addRequestHandler('lastBlock', () => {
2
- let response;
3
- const height = await chainStore.get('localIndex')
4
- const hash = await chainStore.get('localBlock')
5
- response = JSON.stringify({ height: height.toString(), hash: hash.toString() })
6
- return new peernet.protos['peernet-response']({ response })
7
- })
package/test/messages.js DELETED
@@ -1,26 +0,0 @@
1
- import test from 'tape'
2
- import Node from './../dist/commonjs/dht-response.js'
3
-
4
- const responseHash = 'cqbigjfvm5mizik7ke3v6lyhoahecqiwergnl3cytarf5bd2qvqase'
5
-
6
- const block = '4lmacqas6kpnmvehxpmlvsndgydw5i46mf2pgnaub72eug63dozkw4oqxhk3fazbhkguvlhslcaeqm5jtwghxdk5nvmuakrs7yi2gl6xqmmmw';
7
- const itx = '5hmacqb6ao252jmbhkhu5isr4yua3jnkhgb5ejjbiomrlzglwarlb777wq7aw5nfz5m4pubx5uffaxp2cqc4ckrh4xirss4nkujvcjbnmomtm';
8
- const dht = '5demcay'
9
-
10
- const message = '4xnmcay'
11
- const data = '4hemcay'
12
- globalThis.peernet = {
13
- codecs: {}
14
- }
15
- test('peernet message dht-response', tape => {
16
- tape.plan(4)
17
- const n = new Node({ hash: 'hello', has: true })
18
- console.log(n.hash);
19
- const node = new Node(n.encoded)
20
- const node2 = new Node(node.encoded)
21
-
22
- tape.equal(node.name, 'peernet-dht-response', `name from encoded is valid ${node.name}`)
23
- tape.equal(node.name, node2.name, `decoded name is valid ${node.name}`)
24
- tape.equal(node.hash, responseHash, `hash from encoded is valid ${node.hash}`)
25
- tape.equal(node.hash, node2.hash, `decoded hash is valid ${node.hash}`)
26
- })
@@ -1,52 +0,0 @@
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
- })
@@ -1,78 +0,0 @@
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
- })
package/test/peernet.js DELETED
@@ -1,17 +0,0 @@
1
- const Client = require('./../dist/commonjs/peernet.js')
2
-
3
- globalThis.DEBUG = true;
4
- (async () => {
5
- const peernet = await new Client({root: '.peernet/test3'})
6
-
7
- pubsub.subscribe('peer:connected', async peer => setInterval(async () => {
8
- // const dhtMessage = new DHTMessage({hash: 'hello'})
9
- // console.log(dhtMessage.encoded);
10
- // const message = new PeernetMessage({ from: client.id, to: peer.id, data: dhtMessage.encoded, signature: Buffer.from('pnsig') })
11
- // console.log(message.encoded);
12
- // console.log(await peer.request(message.encoded));
13
- const hello = await peernet.get('hello')
14
- console.log({hello: hello.toString()});
15
- process.exit()
16
- }, 5000))
17
- })()