@leofcoin/peernet 0.12.2 → 0.13.1

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/test.js CHANGED
@@ -1,35 +1,47 @@
1
- const Client = require('./dist/commonjs/peernet.js')
1
+ const Client = require('./dist/commonjs/peernet.js');
2
2
 
3
- const client = new Client({root: '.peernet/test'})
4
3
 
5
- pubsub.subscribe('peer:connected', async peer => {
6
- chainStore.put('localBlock', '00000')
7
- const request = new globalThis.peernet.protos['peernet-request']({
8
- request:'lastBlock'
9
- })
10
- const to = peer.id
11
- await peernet.data.put('hello', 'hi')
12
- console.log(request);
13
- const node = await peernet.prepareMessage(to, request.encoded)
14
- console.log({node});
15
- let response = await peer.request(node.encoded)
16
- console.log({response});
17
- const keys = Object.keys(response)
18
- const uint8Array = new Uint8Array(keys.length)
19
- for (const key of keys) {
20
- uint8Array[Number(key)] = response[key]
21
- }
22
- const proto = new globalThis.peernet.protos['peernet-message'](uint8Array)
23
- console.log(proto.decoded.data);
24
- response = new globalThis.peernet.protos['peernet-response'](proto.decoded.data)
25
- console.log({response});
4
+ (async () => {
5
+ const client = await new Client({root: '.peernet/test'})
26
6
 
27
- const block = new TextDecoder().decode(response.decoded.response)
28
- console.log(block);
29
- const task = () => setTimeout(() => {
30
- console.log(peernet.connections[0]?.connected);
31
- console.log(pubsub.subscribers);
7
+ peernet.addFolder([{
8
+ path: 'assets/asset.png',
9
+ content: 'png'
10
+ }, {
11
+ path: 'index.html',
12
+ content: 'html'
13
+ }]).then(hash => peernet.ls(hash).then(paths => peernet.cat(paths[0].hash).then(content => console.log(content))))
14
+
15
+ pubsub.subscribe('peer:connected', async peer => {
16
+ chainStore.put('localBlock', '00000')
17
+ const request = await new globalThis.peernet.protos['peernet-request']({
18
+ request:'lastBlock'
19
+ })
20
+ const to = peer.id
21
+ await peernet.data.put('hello', 'hi')
22
+ console.log(request);
23
+ const node = await peernet.prepareMessage(to, request.encoded)
24
+ console.log({node});
25
+ let response = await peer.request(node.encoded)
26
+ console.log({response});
27
+ const keys = Object.keys(response)
28
+ const uint8Array = new Uint8Array(keys.length)
29
+ for (const key of keys) {
30
+ uint8Array[Number(key)] = response[key]
31
+ }
32
+ const proto = await new globalThis.peernet.protos['peernet-message'](uint8Array)
33
+ console.log(proto.decoded.data);
34
+ response = await new globalThis.peernet.protos['peernet-response'](proto.decoded.data)
35
+ console.log({response});
36
+
37
+ const block = new TextDecoder().decode(response.decoded.response)
38
+ console.log(block);
39
+ const task = () => setTimeout(() => {
40
+ console.log(peernet.connections[0]?.connected);
41
+ console.log(pubsub.subscribers);
42
+ task()
43
+ }, 5000);
32
44
  task()
33
- }, 5000);
34
- task()
35
- })
45
+ })
46
+
47
+ })()
@@ -1,45 +0,0 @@
1
- 'use strict';
2
-
3
- var codecFormatInterface = require('@leofcoin/codec-format-interface');
4
-
5
- var proto = `
6
- // PeernetDHTMessage
7
- message PeernetDHTMessage {
8
- required string hash = 1;
9
- optional string store = 2;
10
- }
11
- `;
12
-
13
- /**
14
- * @example `
15
- new DHTMessage(hash, store)
16
- // store = optional if not set, peernet checks every store
17
- let message = new DHTMessage('hashmvbs124xcfd...', 'transaction')
18
- message = new DHTMessage('hashmvbs124xcfd...', 'block')
19
- `
20
- */
21
- class DHTMessage extends codecFormatInterface.FormatInterface {
22
- /**
23
- *
24
- */
25
- get keys() {
26
- return ['hash', 'store']
27
- }
28
-
29
- get messageName() {
30
- return 'PeernetDHTMessage'
31
- }
32
-
33
- constructor(data) {
34
- const name = 'peernet-dht';
35
- super(data, proto, {name});
36
- }
37
- }
38
-
39
- var dht = /*#__PURE__*/Object.freeze({
40
- __proto__: null,
41
- 'default': DHTMessage
42
- });
43
-
44
- exports.DHTMessage = DHTMessage;
45
- exports.dht = dht;
@@ -1,34 +0,0 @@
1
- 'use strict';
2
-
3
- var codecFormatInterface = require('@leofcoin/codec-format-interface');
4
-
5
- var proto = `
6
- // PeernetDHTMessageResponse
7
- message PeernetDHTMessageResponse {
8
- required string hash = 1;
9
- required bool has = 2;
10
- }
11
- `;
12
-
13
- class DHTMessageResponse extends codecFormatInterface.FormatInterface {
14
- get keys() {
15
- return ['hash', 'has']
16
- }
17
-
18
- get messageName() {
19
- return 'PeernetDHTMessageResponse'
20
- }
21
-
22
- constructor(data) {
23
- const name = 'peernet-dht-response';
24
- super(data, proto, {name});
25
- }
26
- }
27
-
28
- var dhtResponse = /*#__PURE__*/Object.freeze({
29
- __proto__: null,
30
- 'default': DHTMessageResponse
31
- });
32
-
33
- exports.DHTMessageResponse = DHTMessageResponse;
34
- exports.dhtResponse = dhtResponse;
@@ -1,36 +0,0 @@
1
- 'use strict';
2
-
3
- var codecFormatInterface = require('@leofcoin/codec-format-interface');
4
-
5
- var proto = `
6
- // PeernetMessage
7
- message PeernetMessage {
8
- required bytes data = 1;
9
- required bytes signature = 2;
10
- optional string from = 3;
11
- optional string to = 4;
12
- optional string id = 5;
13
- }`;
14
-
15
- class PeernetMessage extends codecFormatInterface.FormatInterface {
16
- get keys() {
17
- return ['data', 'signature', 'from', 'to', 'id']
18
- }
19
-
20
- get messageName() {
21
- return 'PeernetMessage'
22
- }
23
-
24
- constructor(buffer) {
25
- const name = 'peernet-message';
26
- super(buffer, proto, {name});
27
- }
28
- }
29
-
30
- var peernet = /*#__PURE__*/Object.freeze({
31
- __proto__: null,
32
- 'default': PeernetMessage
33
- });
34
-
35
- exports.PeernetMessage = PeernetMessage;
36
- exports.peernet = peernet;
@@ -1,33 +0,0 @@
1
- 'use strict';
2
-
3
- var codecFormatInterface = require('@leofcoin/codec-format-interface');
4
-
5
- var proto = `
6
- // PeernetRequestMessage
7
- message PeernetRequestMessage {
8
- required string request = 1;
9
- }
10
- `;
11
-
12
- class RequestMessage extends codecFormatInterface.FormatInterface {
13
- get keys() {
14
- return ['request']
15
- }
16
-
17
- get messageName() {
18
- return 'PeernetRequestMessage'
19
- }
20
-
21
- constructor(data) {
22
- const name = 'peernet-request';
23
- super(data, proto, {name});
24
- }
25
- }
26
-
27
- var request = /*#__PURE__*/Object.freeze({
28
- __proto__: null,
29
- 'default': RequestMessage
30
- });
31
-
32
- exports.RequestMessage = RequestMessage;
33
- exports.request = request;
@@ -1,33 +0,0 @@
1
- 'use strict';
2
-
3
- var codecFormatInterface = require('@leofcoin/codec-format-interface');
4
-
5
- var proto = `
6
- // PeernetResponseMessage
7
- message PeernetResponseMessage {
8
- required bytes response = 1;
9
- }
10
- `;
11
-
12
- class ResponseMessage extends codecFormatInterface.FormatInterface {
13
- get keys() {
14
- return ['response']
15
- }
16
-
17
- get messageName() {
18
- return 'PeernetResponseMessage'
19
- }
20
-
21
- constructor(data) {
22
- const name = 'peernet-response';
23
- super(data, proto, {name});
24
- }
25
- }
26
-
27
- var response = /*#__PURE__*/Object.freeze({
28
- __proto__: null,
29
- 'default': ResponseMessage
30
- });
31
-
32
- exports.ResponseMessage = ResponseMessage;
33
- exports.response = response;