@leofcoin/peernet 0.12.3 → 0.13.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/dist/browser/messages.js +43 -1
- package/dist/browser/peernet.js +631 -10
- package/dist/browser/protons.js +54 -11
- package/dist/browser/storage.js +1 -1
- package/dist/commonjs/{messages-d7de0e2e.js → messages-f46a3ca1.js} +47 -12
- package/dist/commonjs/peernet.js +75 -2
- package/dist/module/{messages-1168ade4.js → messages-bce1b91d.js} +57 -23
- package/dist/module/peernet.js +75 -2
- package/package.json +2 -2
- package/src/messages/file.js +21 -0
- package/src/messages.js +2 -0
- package/src/peernet.js +75 -1
- package/src/proto/file.proto.js +14 -0
- package/test.js +43 -31
|
@@ -7,7 +7,7 @@ var codecFormatInterface = require('@leofcoin/codec-format-interface');
|
|
|
7
7
|
var request = require('./request.js');
|
|
8
8
|
var response = require('./response.js');
|
|
9
9
|
|
|
10
|
-
var proto$
|
|
10
|
+
var proto$6 = `
|
|
11
11
|
// PeernetDataMessage
|
|
12
12
|
message PeernetDataMessage {
|
|
13
13
|
required string hash = 1;
|
|
@@ -30,11 +30,11 @@ class DataMessage extends codecFormatInterface.FormatInterface {
|
|
|
30
30
|
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
31
31
|
*/
|
|
32
32
|
constructor(data) {
|
|
33
|
-
super(data, proto$
|
|
33
|
+
super(data, proto$6, {name: 'peernet-data'});
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
var proto$
|
|
37
|
+
var proto$5 = `
|
|
38
38
|
// PsMessage
|
|
39
39
|
message PsMessage {
|
|
40
40
|
required bytes data = 1;
|
|
@@ -52,11 +52,11 @@ class PsMessage extends codecFormatInterface.FormatInterface {
|
|
|
52
52
|
|
|
53
53
|
constructor(buffer) {
|
|
54
54
|
const name = 'peernet-ps';
|
|
55
|
-
super(buffer, proto$
|
|
55
|
+
super(buffer, proto$5, {name});
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
var proto$
|
|
59
|
+
var proto$4 = `
|
|
60
60
|
// PeernetPeerMessage
|
|
61
61
|
message PeernetPeerMessage {
|
|
62
62
|
required string id = 1;
|
|
@@ -74,11 +74,11 @@ class PeerMessage extends codecFormatInterface.FormatInterface {
|
|
|
74
74
|
|
|
75
75
|
constructor(data) {
|
|
76
76
|
const name = 'peernet-peer';
|
|
77
|
-
super(data, proto$
|
|
77
|
+
super(data, proto$4, {name});
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
var proto$
|
|
81
|
+
var proto$3 = `
|
|
82
82
|
// PeernetPeerMessageResponse
|
|
83
83
|
message PeernetPeerMessageResponse {
|
|
84
84
|
required string id = 1;
|
|
@@ -96,11 +96,11 @@ class PeerMessageResponse extends codecFormatInterface.FormatInterface {
|
|
|
96
96
|
|
|
97
97
|
constructor(data) {
|
|
98
98
|
const name = 'peernet-peer-response';
|
|
99
|
-
super(data, proto$
|
|
99
|
+
super(data, proto$3, {name});
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
var proto$
|
|
103
|
+
var proto$2 = `
|
|
104
104
|
// PeernetDataMessageResponse
|
|
105
105
|
message PeernetDataMessageResponse {
|
|
106
106
|
required string hash = 1;
|
|
@@ -119,11 +119,11 @@ class DataMessageResponse extends codecFormatInterface.FormatInterface {
|
|
|
119
119
|
|
|
120
120
|
constructor(data) {
|
|
121
121
|
const name = 'peernet-data-response';
|
|
122
|
-
super(data, proto$
|
|
122
|
+
super(data, proto$2, {name});
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
var proto = `
|
|
126
|
+
var proto$1 = `
|
|
127
127
|
message ChatMessage {
|
|
128
128
|
required string value = 1;
|
|
129
129
|
required string author = 2;
|
|
@@ -142,7 +142,41 @@ class ChatMessage extends codecFormatInterface.FormatInterface {
|
|
|
142
142
|
|
|
143
143
|
constructor(buffer) {
|
|
144
144
|
const name = 'chat-message';
|
|
145
|
-
super(buffer, proto, {name});
|
|
145
|
+
super(buffer, proto$1, {name});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
var proto = `
|
|
150
|
+
// PeernetFolder
|
|
151
|
+
message PeernetFileLink {
|
|
152
|
+
required string hash = 1;
|
|
153
|
+
required string path = 2;
|
|
154
|
+
optional string size = 3;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
message PeernetFile {
|
|
158
|
+
required string path = 1;
|
|
159
|
+
optional string content = 2;
|
|
160
|
+
repeated PeernetFileLink links = 3;
|
|
161
|
+
}
|
|
162
|
+
`;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* @extends {CodecFormat}
|
|
166
|
+
*/
|
|
167
|
+
class PeernetFile extends codecFormatInterface.FormatInterface {
|
|
168
|
+
get keys() {
|
|
169
|
+
return ['path', 'content', 'links']
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
get messageName() {
|
|
173
|
+
return 'PeernetFile'
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
177
|
+
*/
|
|
178
|
+
constructor(data) {
|
|
179
|
+
super(data, proto, {name: 'peernet-file'});
|
|
146
180
|
}
|
|
147
181
|
}
|
|
148
182
|
|
|
@@ -156,4 +190,5 @@ exports.DataMessage = DataMessage;
|
|
|
156
190
|
exports.DataMessageResponse = DataMessageResponse;
|
|
157
191
|
exports.PeerMessage = PeerMessage;
|
|
158
192
|
exports.PeerMessageResponse = PeerMessageResponse;
|
|
193
|
+
exports.PeernetFile = PeernetFile;
|
|
159
194
|
exports.PsMessage = PsMessage;
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -5,6 +5,7 @@ var PubSub = require('@vandeurenglenn/little-pubsub');
|
|
|
5
5
|
var fetch = require('node-fetch');
|
|
6
6
|
var codecFormatInterface = require('@leofcoin/codec-format-interface');
|
|
7
7
|
var MultiWallet = require('@leofcoin/multi-wallet');
|
|
8
|
+
require('path');
|
|
8
9
|
|
|
9
10
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
11
|
|
|
@@ -501,8 +502,10 @@ class Peernet {
|
|
|
501
502
|
DataMessage,
|
|
502
503
|
DataMessageResponse,
|
|
503
504
|
PsMessage,
|
|
504
|
-
ChatMessage
|
|
505
|
-
|
|
505
|
+
ChatMessage,
|
|
506
|
+
PeernetFile
|
|
507
|
+
// FolderMessageResponse
|
|
508
|
+
} = await Promise.resolve().then(function () { return require(/* webpackChunkName: "messages" */ './messages-f46a3ca1.js'); });
|
|
506
509
|
|
|
507
510
|
/**
|
|
508
511
|
* proto Object containing protos
|
|
@@ -526,6 +529,7 @@ class Peernet {
|
|
|
526
529
|
'peernet-data-response': DataMessageResponse,
|
|
527
530
|
'peernet-ps': PsMessage,
|
|
528
531
|
'chat-message': ChatMessage,
|
|
532
|
+
'peernet-file': PeernetFile
|
|
529
533
|
};
|
|
530
534
|
|
|
531
535
|
this._messageHandler = new MessageHandler(this.network);
|
|
@@ -862,6 +866,75 @@ class Peernet {
|
|
|
862
866
|
}
|
|
863
867
|
}
|
|
864
868
|
|
|
869
|
+
get folder() {
|
|
870
|
+
return {
|
|
871
|
+
/**
|
|
872
|
+
* Get content for given data hash
|
|
873
|
+
*
|
|
874
|
+
* @param {String} hash
|
|
875
|
+
*/
|
|
876
|
+
get: async (hash) => {
|
|
877
|
+
debug(`get data ${hash}`);
|
|
878
|
+
const data = await dataStore.has(hash);
|
|
879
|
+
if (data) return await dataStore.get(hash)
|
|
880
|
+
return this.requestData(hash, 'data')
|
|
881
|
+
},
|
|
882
|
+
/**
|
|
883
|
+
* put data content
|
|
884
|
+
*
|
|
885
|
+
* @param {String} hash
|
|
886
|
+
* @param {Buffer} data
|
|
887
|
+
*/
|
|
888
|
+
put: async (hash, data) => await dataStore.put(hash, data),
|
|
889
|
+
/**
|
|
890
|
+
* @param {String} hash
|
|
891
|
+
* @return {Boolean}
|
|
892
|
+
*/
|
|
893
|
+
has: async (hash) => await dataStore.has(hash),
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
async addFolder(files) {
|
|
898
|
+
const links = [];
|
|
899
|
+
for (const file of files) {
|
|
900
|
+
const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
|
|
901
|
+
const hash = await fileNode.hash;
|
|
902
|
+
await dataStore.put(hash, fileNode.encoded);
|
|
903
|
+
links.push({hash, path: file.path});
|
|
904
|
+
}
|
|
905
|
+
const node = await new globalThis.peernet.protos['peernet-file']({path: '/', links});
|
|
906
|
+
const hash = await node.hash;
|
|
907
|
+
await dataStore.put(hash, node.encoded);
|
|
908
|
+
|
|
909
|
+
return hash
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
async ls(hash) {
|
|
913
|
+
let data;
|
|
914
|
+
const has = await dataStore.has(hash);
|
|
915
|
+
if (has) data = await dataStore.get(hash);
|
|
916
|
+
else data = await this.requestData(hash, 'data');
|
|
917
|
+
|
|
918
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
919
|
+
const paths = [];
|
|
920
|
+
if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
|
|
921
|
+
for (const {path, hash} of node.decoded.links) {
|
|
922
|
+
paths.push({path, hash});
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
return paths
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
async cat(hash) {
|
|
929
|
+
let data;
|
|
930
|
+
const has = await dataStore.has(hash);
|
|
931
|
+
if (has) data = await dataStore.get(hash);
|
|
932
|
+
else data = await this.requestData(hash, 'data');
|
|
933
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
934
|
+
if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
|
|
935
|
+
return node.decoded.content
|
|
936
|
+
}
|
|
937
|
+
|
|
865
938
|
/**
|
|
866
939
|
* goes trough given stores and tries to find data for given hash
|
|
867
940
|
* @param {Array} stores
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
2
|
|
|
3
|
-
var proto$
|
|
3
|
+
var proto$b = `
|
|
4
4
|
// PeernetMessage
|
|
5
5
|
message PeernetMessage {
|
|
6
6
|
required bytes data = 1;
|
|
@@ -21,11 +21,11 @@ class PeernetMessage extends FormatInterface {
|
|
|
21
21
|
|
|
22
22
|
constructor(buffer) {
|
|
23
23
|
const name = 'peernet-message';
|
|
24
|
-
super(buffer, proto$
|
|
24
|
+
super(buffer, proto$b, {name});
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
var proto$
|
|
28
|
+
var proto$a = `
|
|
29
29
|
// PeernetDHTMessage
|
|
30
30
|
message PeernetDHTMessage {
|
|
31
31
|
required string hash = 1;
|
|
@@ -55,11 +55,11 @@ class DHTMessage extends FormatInterface {
|
|
|
55
55
|
|
|
56
56
|
constructor(data) {
|
|
57
57
|
const name = 'peernet-dht';
|
|
58
|
-
super(data, proto$
|
|
58
|
+
super(data, proto$a, {name});
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
var proto$
|
|
62
|
+
var proto$9 = `
|
|
63
63
|
// PeernetDHTMessageResponse
|
|
64
64
|
message PeernetDHTMessageResponse {
|
|
65
65
|
required string hash = 1;
|
|
@@ -78,11 +78,11 @@ class DHTMessageResponse extends FormatInterface {
|
|
|
78
78
|
|
|
79
79
|
constructor(data) {
|
|
80
80
|
const name = 'peernet-dht-response';
|
|
81
|
-
super(data, proto$
|
|
81
|
+
super(data, proto$9, {name});
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
var proto$
|
|
85
|
+
var proto$8 = `
|
|
86
86
|
// PeernetDataMessage
|
|
87
87
|
message PeernetDataMessage {
|
|
88
88
|
required string hash = 1;
|
|
@@ -105,11 +105,11 @@ class DataMessage extends FormatInterface {
|
|
|
105
105
|
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
106
106
|
*/
|
|
107
107
|
constructor(data) {
|
|
108
|
-
super(data, proto$
|
|
108
|
+
super(data, proto$8, {name: 'peernet-data'});
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
var proto$
|
|
112
|
+
var proto$7 = `
|
|
113
113
|
// PsMessage
|
|
114
114
|
message PsMessage {
|
|
115
115
|
required bytes data = 1;
|
|
@@ -127,11 +127,11 @@ class PsMessage extends FormatInterface {
|
|
|
127
127
|
|
|
128
128
|
constructor(buffer) {
|
|
129
129
|
const name = 'peernet-ps';
|
|
130
|
-
super(buffer, proto$
|
|
130
|
+
super(buffer, proto$7, {name});
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
var proto$
|
|
134
|
+
var proto$6 = `
|
|
135
135
|
// PeernetPeerMessage
|
|
136
136
|
message PeernetPeerMessage {
|
|
137
137
|
required string id = 1;
|
|
@@ -149,11 +149,11 @@ class PeerMessage extends FormatInterface {
|
|
|
149
149
|
|
|
150
150
|
constructor(data) {
|
|
151
151
|
const name = 'peernet-peer';
|
|
152
|
-
super(data, proto$
|
|
152
|
+
super(data, proto$6, {name});
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
var proto$
|
|
156
|
+
var proto$5 = `
|
|
157
157
|
// PeernetRequestMessage
|
|
158
158
|
message PeernetRequestMessage {
|
|
159
159
|
required string request = 1;
|
|
@@ -171,11 +171,11 @@ class RequestMessage extends FormatInterface {
|
|
|
171
171
|
|
|
172
172
|
constructor(data) {
|
|
173
173
|
const name = 'peernet-request';
|
|
174
|
-
super(data, proto$
|
|
174
|
+
super(data, proto$5, {name});
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
var proto$
|
|
178
|
+
var proto$4 = `
|
|
179
179
|
// PeernetResponseMessage
|
|
180
180
|
message PeernetResponseMessage {
|
|
181
181
|
required bytes response = 1;
|
|
@@ -193,11 +193,11 @@ class ResponseMessage extends FormatInterface {
|
|
|
193
193
|
|
|
194
194
|
constructor(data) {
|
|
195
195
|
const name = 'peernet-response';
|
|
196
|
-
super(data, proto$
|
|
196
|
+
super(data, proto$4, {name});
|
|
197
197
|
}
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
-
var proto$
|
|
200
|
+
var proto$3 = `
|
|
201
201
|
// PeernetPeerMessageResponse
|
|
202
202
|
message PeernetPeerMessageResponse {
|
|
203
203
|
required string id = 1;
|
|
@@ -215,11 +215,11 @@ class PeerMessageResponse extends FormatInterface {
|
|
|
215
215
|
|
|
216
216
|
constructor(data) {
|
|
217
217
|
const name = 'peernet-peer-response';
|
|
218
|
-
super(data, proto$
|
|
218
|
+
super(data, proto$3, {name});
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
|
|
222
|
-
var proto$
|
|
222
|
+
var proto$2 = `
|
|
223
223
|
// PeernetDataMessageResponse
|
|
224
224
|
message PeernetDataMessageResponse {
|
|
225
225
|
required string hash = 1;
|
|
@@ -238,11 +238,11 @@ class DataMessageResponse extends FormatInterface {
|
|
|
238
238
|
|
|
239
239
|
constructor(data) {
|
|
240
240
|
const name = 'peernet-data-response';
|
|
241
|
-
super(data, proto$
|
|
241
|
+
super(data, proto$2, {name});
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
244
|
|
|
245
|
-
var proto = `
|
|
245
|
+
var proto$1 = `
|
|
246
246
|
message ChatMessage {
|
|
247
247
|
required string value = 1;
|
|
248
248
|
required string author = 2;
|
|
@@ -261,8 +261,42 @@ class ChatMessage extends FormatInterface {
|
|
|
261
261
|
|
|
262
262
|
constructor(buffer) {
|
|
263
263
|
const name = 'chat-message';
|
|
264
|
-
super(buffer, proto, {name});
|
|
264
|
+
super(buffer, proto$1, {name});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
var proto = `
|
|
269
|
+
// PeernetFolder
|
|
270
|
+
message PeernetFileLink {
|
|
271
|
+
required string hash = 1;
|
|
272
|
+
required string path = 2;
|
|
273
|
+
optional string size = 3;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
message PeernetFile {
|
|
277
|
+
required string path = 1;
|
|
278
|
+
optional string content = 2;
|
|
279
|
+
repeated PeernetFileLink links = 3;
|
|
280
|
+
}
|
|
281
|
+
`;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @extends {CodecFormat}
|
|
285
|
+
*/
|
|
286
|
+
class PeernetFile extends FormatInterface {
|
|
287
|
+
get keys() {
|
|
288
|
+
return ['path', 'content', 'links']
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
get messageName() {
|
|
292
|
+
return 'PeernetFile'
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
296
|
+
*/
|
|
297
|
+
constructor(data) {
|
|
298
|
+
super(data, proto, {name: 'peernet-file'});
|
|
265
299
|
}
|
|
266
300
|
}
|
|
267
301
|
|
|
268
|
-
export { ChatMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PeerMessage, PeerMessageResponse, PeernetMessage, PsMessage, RequestMessage, ResponseMessage };
|
|
302
|
+
export { ChatMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PeerMessage, PeerMessageResponse, PeernetFile, PeernetMessage, PsMessage, RequestMessage, ResponseMessage };
|
package/dist/module/peernet.js
CHANGED
|
@@ -2,6 +2,7 @@ import '@vandeurenglenn/debug';
|
|
|
2
2
|
import PubSub from '@vandeurenglenn/little-pubsub';
|
|
3
3
|
import { Codec, CodecHash, codecs } from '@leofcoin/codec-format-interface';
|
|
4
4
|
import MultiWallet from '@leofcoin/multi-wallet';
|
|
5
|
+
import 'path';
|
|
5
6
|
|
|
6
7
|
const protoFor = (data) => {
|
|
7
8
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
@@ -474,8 +475,10 @@ class Peernet {
|
|
|
474
475
|
DataMessage,
|
|
475
476
|
DataMessageResponse,
|
|
476
477
|
PsMessage,
|
|
477
|
-
ChatMessage
|
|
478
|
-
|
|
478
|
+
ChatMessage,
|
|
479
|
+
PeernetFile
|
|
480
|
+
// FolderMessageResponse
|
|
481
|
+
} = await import(/* webpackChunkName: "messages" */ './messages-bce1b91d.js');
|
|
479
482
|
|
|
480
483
|
/**
|
|
481
484
|
* proto Object containing protos
|
|
@@ -499,6 +502,7 @@ class Peernet {
|
|
|
499
502
|
'peernet-data-response': DataMessageResponse,
|
|
500
503
|
'peernet-ps': PsMessage,
|
|
501
504
|
'chat-message': ChatMessage,
|
|
505
|
+
'peernet-file': PeernetFile
|
|
502
506
|
};
|
|
503
507
|
|
|
504
508
|
this._messageHandler = new MessageHandler(this.network);
|
|
@@ -835,6 +839,75 @@ class Peernet {
|
|
|
835
839
|
}
|
|
836
840
|
}
|
|
837
841
|
|
|
842
|
+
get folder() {
|
|
843
|
+
return {
|
|
844
|
+
/**
|
|
845
|
+
* Get content for given data hash
|
|
846
|
+
*
|
|
847
|
+
* @param {String} hash
|
|
848
|
+
*/
|
|
849
|
+
get: async (hash) => {
|
|
850
|
+
debug(`get data ${hash}`);
|
|
851
|
+
const data = await dataStore.has(hash);
|
|
852
|
+
if (data) return await dataStore.get(hash)
|
|
853
|
+
return this.requestData(hash, 'data')
|
|
854
|
+
},
|
|
855
|
+
/**
|
|
856
|
+
* put data content
|
|
857
|
+
*
|
|
858
|
+
* @param {String} hash
|
|
859
|
+
* @param {Buffer} data
|
|
860
|
+
*/
|
|
861
|
+
put: async (hash, data) => await dataStore.put(hash, data),
|
|
862
|
+
/**
|
|
863
|
+
* @param {String} hash
|
|
864
|
+
* @return {Boolean}
|
|
865
|
+
*/
|
|
866
|
+
has: async (hash) => await dataStore.has(hash),
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
async addFolder(files) {
|
|
871
|
+
const links = [];
|
|
872
|
+
for (const file of files) {
|
|
873
|
+
const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
|
|
874
|
+
const hash = await fileNode.hash;
|
|
875
|
+
await dataStore.put(hash, fileNode.encoded);
|
|
876
|
+
links.push({hash, path: file.path});
|
|
877
|
+
}
|
|
878
|
+
const node = await new globalThis.peernet.protos['peernet-file']({path: '/', links});
|
|
879
|
+
const hash = await node.hash;
|
|
880
|
+
await dataStore.put(hash, node.encoded);
|
|
881
|
+
|
|
882
|
+
return hash
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
async ls(hash) {
|
|
886
|
+
let data;
|
|
887
|
+
const has = await dataStore.has(hash);
|
|
888
|
+
if (has) data = await dataStore.get(hash);
|
|
889
|
+
else data = await this.requestData(hash, 'data');
|
|
890
|
+
|
|
891
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
892
|
+
const paths = [];
|
|
893
|
+
if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
|
|
894
|
+
for (const {path, hash} of node.decoded.links) {
|
|
895
|
+
paths.push({path, hash});
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
return paths
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
async cat(hash) {
|
|
902
|
+
let data;
|
|
903
|
+
const has = await dataStore.has(hash);
|
|
904
|
+
if (has) data = await dataStore.get(hash);
|
|
905
|
+
else data = await this.requestData(hash, 'data');
|
|
906
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
907
|
+
if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
|
|
908
|
+
return node.decoded.content
|
|
909
|
+
}
|
|
910
|
+
|
|
838
911
|
/**
|
|
839
912
|
* goes trough given stores and tries to find data for given hash
|
|
840
913
|
* @param {Array} stores
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "src/module/peernet.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@leofcoin/codec-format-interface": "^1.2.
|
|
25
|
+
"@leofcoin/codec-format-interface": "^1.2.5",
|
|
26
26
|
"@leofcoin/generate-account": "^1.0.4",
|
|
27
27
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
28
|
"@leofcoin/peernet-swarm": "^0.3.3",
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import proto from './../proto/file.proto.js'
|
|
2
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @extends {CodecFormat}
|
|
6
|
+
*/
|
|
7
|
+
export default class PeernetFile extends FormatInterface {
|
|
8
|
+
get keys() {
|
|
9
|
+
return ['path', 'content', 'links']
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get messageName() {
|
|
13
|
+
return 'PeernetFile'
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
17
|
+
*/
|
|
18
|
+
constructor(data) {
|
|
19
|
+
super(data, proto, {name: 'peernet-file'})
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/messages.js
CHANGED
|
@@ -9,3 +9,5 @@ export { default as ResponseMessage } from './messages/response.js'
|
|
|
9
9
|
export { default as PeerMessageResponse } from './messages/peer-response.js'
|
|
10
10
|
export { default as DataMessageResponse } from './messages/data-response.js'
|
|
11
11
|
export { default as ChatMessage } from './messages/chat.js'
|
|
12
|
+
export { default as PeernetFile } from './messages/file.js'
|
|
13
|
+
// export { default as FolderMessageResponse } from './messages/folder-response.js'
|
package/src/peernet.js
CHANGED
|
@@ -9,6 +9,7 @@ import dataHandler from './handlers/data.js'
|
|
|
9
9
|
import { encapsulatedError, dhtError,
|
|
10
10
|
nothingFoundError } from './errors/errors.js'
|
|
11
11
|
|
|
12
|
+
import {parse} from 'path'
|
|
12
13
|
globalThis.leofcoin = globalThis.leofcoin || {}
|
|
13
14
|
globalThis.pubsub = globalThis.pubsub || new PubSub()
|
|
14
15
|
globalThis.globalSub = globalThis.globalSub || new PubSub({verbose: true})
|
|
@@ -157,7 +158,9 @@ export default class Peernet {
|
|
|
157
158
|
DataMessage,
|
|
158
159
|
DataMessageResponse,
|
|
159
160
|
PsMessage,
|
|
160
|
-
ChatMessage
|
|
161
|
+
ChatMessage,
|
|
162
|
+
PeernetFile
|
|
163
|
+
// FolderMessageResponse
|
|
161
164
|
} = await import(/* webpackChunkName: "messages" */ './messages.js')
|
|
162
165
|
|
|
163
166
|
/**
|
|
@@ -182,6 +185,7 @@ export default class Peernet {
|
|
|
182
185
|
'peernet-data-response': DataMessageResponse,
|
|
183
186
|
'peernet-ps': PsMessage,
|
|
184
187
|
'chat-message': ChatMessage,
|
|
188
|
+
'peernet-file': PeernetFile
|
|
185
189
|
}
|
|
186
190
|
|
|
187
191
|
this._messageHandler = new MessageHandler(this.network)
|
|
@@ -523,6 +527,76 @@ export default class Peernet {
|
|
|
523
527
|
}
|
|
524
528
|
}
|
|
525
529
|
|
|
530
|
+
get folder() {
|
|
531
|
+
return {
|
|
532
|
+
/**
|
|
533
|
+
* Get content for given data hash
|
|
534
|
+
*
|
|
535
|
+
* @param {String} hash
|
|
536
|
+
*/
|
|
537
|
+
get: async (hash) => {
|
|
538
|
+
debug(`get data ${hash}`)
|
|
539
|
+
const data = await dataStore.has(hash)
|
|
540
|
+
if (data) return await dataStore.get(hash)
|
|
541
|
+
return this.requestData(hash, 'data')
|
|
542
|
+
},
|
|
543
|
+
/**
|
|
544
|
+
* put data content
|
|
545
|
+
*
|
|
546
|
+
* @param {String} hash
|
|
547
|
+
* @param {Buffer} data
|
|
548
|
+
*/
|
|
549
|
+
put: async (hash, data) => await dataStore.put(hash, data),
|
|
550
|
+
/**
|
|
551
|
+
* @param {String} hash
|
|
552
|
+
* @return {Boolean}
|
|
553
|
+
*/
|
|
554
|
+
has: async (hash) => await dataStore.has(hash),
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
async addFolder(files) {
|
|
559
|
+
const links = []
|
|
560
|
+
for (const file of files) {
|
|
561
|
+
const fileNode = await new globalThis.peernet.protos['peernet-file'](file)
|
|
562
|
+
const hash = await fileNode.hash
|
|
563
|
+
await dataStore.put(hash, fileNode.encoded)
|
|
564
|
+
links.push({hash, path: file.path})
|
|
565
|
+
}
|
|
566
|
+
const node = await new globalThis.peernet.protos['peernet-file']({path: '/', links})
|
|
567
|
+
const hash = await node.hash
|
|
568
|
+
await dataStore.put(hash, node.encoded)
|
|
569
|
+
|
|
570
|
+
return hash
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
async ls(hash) {
|
|
574
|
+
let data
|
|
575
|
+
const has = await dataStore.has(hash)
|
|
576
|
+
if (has) data = await dataStore.get(hash)
|
|
577
|
+
else data = await this.requestData(hash, 'data')
|
|
578
|
+
|
|
579
|
+
const node = await new peernet.protos['peernet-file'](data)
|
|
580
|
+
const paths = []
|
|
581
|
+
if (node.decoded?.links.length === 0) throw new Error(`${hash} is a file`)
|
|
582
|
+
for (const {path, hash} of node.decoded.links) {
|
|
583
|
+
paths.push({path, hash})
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return paths
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
async cat(hash) {
|
|
590
|
+
let data
|
|
591
|
+
const has = await dataStore.has(hash)
|
|
592
|
+
if (has) data = await dataStore.get(hash)
|
|
593
|
+
else data = await this.requestData(hash, 'data')
|
|
594
|
+
const node = await new peernet.protos['peernet-file'](data)
|
|
595
|
+
const paths = []
|
|
596
|
+
if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
|
|
597
|
+
return node.decoded.content
|
|
598
|
+
}
|
|
599
|
+
|
|
526
600
|
/**
|
|
527
601
|
* goes trough given stores and tries to find data for given hash
|
|
528
602
|
* @param {Array} stores
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default `
|
|
2
|
+
// PeernetFolder
|
|
3
|
+
message PeernetFileLink {
|
|
4
|
+
required string hash = 1;
|
|
5
|
+
required string path = 2;
|
|
6
|
+
optional string size = 3;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
message PeernetFile {
|
|
10
|
+
required string path = 1;
|
|
11
|
+
optional string content = 2;
|
|
12
|
+
repeated PeernetFileLink links = 3;
|
|
13
|
+
}
|
|
14
|
+
`
|