@leofcoin/peernet 0.11.0 → 0.11.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/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/codec-format-interface.js.html +637 -0
- package/coverage/lcov-report/dht-response.js.html +193 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +199 -0
- package/dist/browser/peernet.js +1288 -1385
- package/dist/commonjs/{client-bd0caeb7.js → client-1a1f75e6.js} +41 -41
- package/dist/commonjs/{codec-4a768e5e.js → codec-8c8c652f.js} +118 -118
- package/dist/commonjs/codec-format-interface.js +167 -167
- package/dist/commonjs/codec.js +2 -2
- package/dist/commonjs/dht-response.js +11 -11
- package/dist/commonjs/dht.js +2 -2
- package/dist/commonjs/hash.js +149 -149
- package/dist/commonjs/{http-2b0735ef.js → http-7bbac90a.js} +1 -1
- package/dist/commonjs/peernet-message.js +2 -2
- package/dist/commonjs/peernet.js +843 -934
- package/dist/commonjs/request.js +2 -2
- package/dist/commonjs/response.js +2 -2
- package/dist/module/peernet.js +1357 -1448
- package/package.json +2 -18
- package/src/client.js +75 -75
- package/src/codec/codec-format-interface.js +172 -172
- package/src/codec/codec.js +124 -124
- package/src/dht/dht.js +121 -121
- package/src/discovery/peer-discovery.js +75 -75
- package/src/hash/hash.js +155 -155
- package/src/http/client/http-client.js +44 -44
- package/src/messages/dht-response.js +14 -14
- package/src/peer.js +67 -67
- package/src/peernet.js +612 -612
- package/src/proto/chat-message.proto.js +7 -7
- package/src/utils/utils.js +78 -78
|
@@ -229,47 +229,47 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
|
|
|
229
229
|
});
|
|
230
230
|
};
|
|
231
231
|
|
|
232
|
-
class HttpClientApi$1 {
|
|
233
|
-
constructor(config = {}) {
|
|
234
|
-
if (!config.apiPath) config.apiPath = 'api';
|
|
235
|
-
|
|
236
|
-
const address = `ws://${config.host}:${config.port}`;
|
|
237
|
-
|
|
238
|
-
this.apiUrl = (url) => `${address}/${url}`;
|
|
239
|
-
return (async () => {
|
|
240
|
-
this.client = await socketRequestClient(address, config.protocol, {pubsub: config.pubsub, retry: 3000});
|
|
241
|
-
return this
|
|
242
|
-
})()
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
async get(url, obj) {
|
|
246
|
-
const headers = {};
|
|
247
|
-
let body = null;
|
|
248
|
-
let method = 'GET';
|
|
249
|
-
if (obj) {
|
|
250
|
-
method = 'POST';
|
|
251
|
-
headers['Content-Type'] = 'application/json';
|
|
252
|
-
body = JSON.stringify(obj);
|
|
253
|
-
}
|
|
254
|
-
let response = await this.client.request(url, {headers, body, method});
|
|
255
|
-
const type = response.headers.get('content-type').split(';')[0];
|
|
256
|
-
if (type==='application/json') response = await response.json();
|
|
257
|
-
return response
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
async put(url, obj) {
|
|
261
|
-
const headers = {};
|
|
262
|
-
let body = {};
|
|
263
|
-
if (obj) {
|
|
264
|
-
headers['Content-Type'] = 'application/json';
|
|
265
|
-
body = JSON.stringify(obj);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body});
|
|
269
|
-
const type = response.headers.get('content-type').split(';')[0];
|
|
270
|
-
if (type==='application/json') response = await response.json();
|
|
271
|
-
return response
|
|
272
|
-
}
|
|
232
|
+
class HttpClientApi$1 {
|
|
233
|
+
constructor(config = {}) {
|
|
234
|
+
if (!config.apiPath) config.apiPath = 'api';
|
|
235
|
+
|
|
236
|
+
const address = `ws://${config.host}:${config.port}`;
|
|
237
|
+
|
|
238
|
+
this.apiUrl = (url) => `${address}/${url}`;
|
|
239
|
+
return (async () => {
|
|
240
|
+
this.client = await socketRequestClient(address, config.protocol, {pubsub: config.pubsub, retry: 3000});
|
|
241
|
+
return this
|
|
242
|
+
})()
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async get(url, obj) {
|
|
246
|
+
const headers = {};
|
|
247
|
+
let body = null;
|
|
248
|
+
let method = 'GET';
|
|
249
|
+
if (obj) {
|
|
250
|
+
method = 'POST';
|
|
251
|
+
headers['Content-Type'] = 'application/json';
|
|
252
|
+
body = JSON.stringify(obj);
|
|
253
|
+
}
|
|
254
|
+
let response = await this.client.request(url, {headers, body, method});
|
|
255
|
+
const type = response.headers.get('content-type').split(';')[0];
|
|
256
|
+
if (type==='application/json') response = await response.json();
|
|
257
|
+
return response
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async put(url, obj) {
|
|
261
|
+
const headers = {};
|
|
262
|
+
let body = {};
|
|
263
|
+
if (obj) {
|
|
264
|
+
headers['Content-Type'] = 'application/json';
|
|
265
|
+
body = JSON.stringify(obj);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body});
|
|
269
|
+
const type = response.headers.get('content-type').split(';')[0];
|
|
270
|
+
if (type==='application/json') response = await response.json();
|
|
271
|
+
return response
|
|
272
|
+
}
|
|
273
273
|
}
|
|
274
274
|
|
|
275
275
|
class HttpClientApi extends HttpClientApi$1 {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
var varint = require('varint');
|
|
4
4
|
var bs32 = require('@vandeurenglenn/base32');
|
|
5
5
|
var bs58 = require('@vandeurenglenn/base58');
|
|
6
|
-
var isHex = require('is-hex');
|
|
6
|
+
var isHex = require('@vandeurenglenn/is-hex');
|
|
7
7
|
|
|
8
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
9
|
|
|
@@ -92,123 +92,123 @@ var codecs = {
|
|
|
92
92
|
},
|
|
93
93
|
};
|
|
94
94
|
|
|
95
|
-
class PeernetCodec {
|
|
96
|
-
get codecs() {
|
|
97
|
-
return {...globalThis.peernet.codecs, ...codecs}
|
|
98
|
-
}
|
|
99
|
-
constructor(buffer) {
|
|
100
|
-
if (buffer) {
|
|
101
|
-
if (buffer instanceof Uint8Array) {
|
|
102
|
-
const codec = varint__default["default"].decode(buffer);
|
|
103
|
-
const name = this.getCodecName(codec);
|
|
104
|
-
if (name) {
|
|
105
|
-
this.name = name;
|
|
106
|
-
this.encoded = buffer;
|
|
107
|
-
this.decode(buffer);
|
|
108
|
-
} else {
|
|
109
|
-
this.encode(buffer);
|
|
110
|
-
}
|
|
111
|
-
} else if (buffer instanceof ArrayBuffer) {
|
|
112
|
-
const encoded = new Uint8Array(buffer.byteLength);
|
|
113
|
-
|
|
114
|
-
for (let i = 0; i < buffer.byteLength; i++) {
|
|
115
|
-
encoded[i] = buffer[i];
|
|
116
|
-
}
|
|
117
|
-
this.encoded = encoded;
|
|
118
|
-
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
119
|
-
this.decode(buffer);
|
|
120
|
-
return
|
|
121
|
-
}
|
|
122
|
-
if (typeof buffer === 'string') {
|
|
123
|
-
if (this.codecs[buffer]) this.fromName(buffer);
|
|
124
|
-
else if (isHex__default["default"](buffer)) this.fromHex(buffer);
|
|
125
|
-
else if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
|
|
126
|
-
else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
|
|
127
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
128
|
-
}
|
|
129
|
-
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
fromEncoded(encoded) {
|
|
134
|
-
const codec = varint__default["default"].decode(encoded);
|
|
135
|
-
const name = this.getCodecName(codec);
|
|
136
|
-
this.name = name;
|
|
137
|
-
this.encoded = encoded;
|
|
138
|
-
this.decode(encoded);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
fromHex(hex) {
|
|
142
|
-
this.encoded = Buffer.from(hex, 'hex');
|
|
143
|
-
this.decode();
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
fromBs32(input) {
|
|
147
|
-
this.encoded = bs32__default["default"].decode(input);
|
|
148
|
-
this.decode();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
fromBs58(input) {
|
|
152
|
-
this.encoded = bs58__default["default"].decode(input);
|
|
153
|
-
this.decode();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
getCodec(name) {
|
|
157
|
-
return this.codecs[name].codec
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
getCodecName(codec) {
|
|
161
|
-
return Object.keys(this.codecs).reduce((p, c) => {
|
|
162
|
-
const item = this.codecs[c];
|
|
163
|
-
if (item.codec === codec) return c;
|
|
164
|
-
else return p;
|
|
165
|
-
}, undefined)
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
getHashAlg(name) {
|
|
169
|
-
return this.codecs[name].hashAlg
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
fromCodec(codec) {
|
|
173
|
-
this.name = this.getCodecName(codec);
|
|
174
|
-
this.hashAlg = this.getHashAlg(this.name);
|
|
175
|
-
|
|
176
|
-
this.codec = this.getCodec(this.name);
|
|
177
|
-
this.codecBuffer = varint__default["default"].encode(codec);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
fromName(name) {
|
|
181
|
-
const codec = this.getCodec(name);
|
|
182
|
-
this.name = name;
|
|
183
|
-
this.codec = codec;
|
|
184
|
-
this.hashAlg = this.getHashAlg(name);
|
|
185
|
-
this.codecBuffer = varint__default["default"].encode(codec);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
toBs32() {
|
|
189
|
-
this.encode();
|
|
190
|
-
return bs32__default["default"].encode(this.encoded)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
toBs58() {
|
|
194
|
-
this.encode();
|
|
195
|
-
return bs58__default["default"].encode(this.encoded)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
toHex() {
|
|
199
|
-
return this.encoded.toString('hex')
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
decode() {
|
|
203
|
-
const codec = varint__default["default"].decode(this.encoded);
|
|
204
|
-
this.fromCodec(codec);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
encode() {
|
|
208
|
-
const codec = varint__default["default"].encode(this.decoded);
|
|
209
|
-
this.encoded = codec;
|
|
210
|
-
return this.encoded
|
|
211
|
-
}
|
|
95
|
+
class PeernetCodec {
|
|
96
|
+
get codecs() {
|
|
97
|
+
return {...globalThis.peernet.codecs, ...codecs}
|
|
98
|
+
}
|
|
99
|
+
constructor(buffer) {
|
|
100
|
+
if (buffer) {
|
|
101
|
+
if (buffer instanceof Uint8Array) {
|
|
102
|
+
const codec = varint__default["default"].decode(buffer);
|
|
103
|
+
const name = this.getCodecName(codec);
|
|
104
|
+
if (name) {
|
|
105
|
+
this.name = name;
|
|
106
|
+
this.encoded = buffer;
|
|
107
|
+
this.decode(buffer);
|
|
108
|
+
} else {
|
|
109
|
+
this.encode(buffer);
|
|
110
|
+
}
|
|
111
|
+
} else if (buffer instanceof ArrayBuffer) {
|
|
112
|
+
const encoded = new Uint8Array(buffer.byteLength);
|
|
113
|
+
|
|
114
|
+
for (let i = 0; i < buffer.byteLength; i++) {
|
|
115
|
+
encoded[i] = buffer[i];
|
|
116
|
+
}
|
|
117
|
+
this.encoded = encoded;
|
|
118
|
+
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
119
|
+
this.decode(buffer);
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
if (typeof buffer === 'string') {
|
|
123
|
+
if (this.codecs[buffer]) this.fromName(buffer);
|
|
124
|
+
else if (isHex__default["default"](buffer)) this.fromHex(buffer);
|
|
125
|
+
else if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
|
|
126
|
+
else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
|
|
127
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
128
|
+
}
|
|
129
|
+
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fromEncoded(encoded) {
|
|
134
|
+
const codec = varint__default["default"].decode(encoded);
|
|
135
|
+
const name = this.getCodecName(codec);
|
|
136
|
+
this.name = name;
|
|
137
|
+
this.encoded = encoded;
|
|
138
|
+
this.decode(encoded);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
fromHex(hex) {
|
|
142
|
+
this.encoded = Buffer.from(hex, 'hex');
|
|
143
|
+
this.decode();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
fromBs32(input) {
|
|
147
|
+
this.encoded = bs32__default["default"].decode(input);
|
|
148
|
+
this.decode();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
fromBs58(input) {
|
|
152
|
+
this.encoded = bs58__default["default"].decode(input);
|
|
153
|
+
this.decode();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
getCodec(name) {
|
|
157
|
+
return this.codecs[name].codec
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
getCodecName(codec) {
|
|
161
|
+
return Object.keys(this.codecs).reduce((p, c) => {
|
|
162
|
+
const item = this.codecs[c];
|
|
163
|
+
if (item.codec === codec) return c;
|
|
164
|
+
else return p;
|
|
165
|
+
}, undefined)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
getHashAlg(name) {
|
|
169
|
+
return this.codecs[name].hashAlg
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
fromCodec(codec) {
|
|
173
|
+
this.name = this.getCodecName(codec);
|
|
174
|
+
this.hashAlg = this.getHashAlg(this.name);
|
|
175
|
+
|
|
176
|
+
this.codec = this.getCodec(this.name);
|
|
177
|
+
this.codecBuffer = varint__default["default"].encode(codec);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
fromName(name) {
|
|
181
|
+
const codec = this.getCodec(name);
|
|
182
|
+
this.name = name;
|
|
183
|
+
this.codec = codec;
|
|
184
|
+
this.hashAlg = this.getHashAlg(name);
|
|
185
|
+
this.codecBuffer = varint__default["default"].encode(codec);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
toBs32() {
|
|
189
|
+
this.encode();
|
|
190
|
+
return bs32__default["default"].encode(this.encoded)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
toBs58() {
|
|
194
|
+
this.encode();
|
|
195
|
+
return bs58__default["default"].encode(this.encoded)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
toHex() {
|
|
199
|
+
return this.encoded.toString('hex')
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
decode() {
|
|
203
|
+
const codec = varint__default["default"].decode(this.encoded);
|
|
204
|
+
this.fromCodec(codec);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
encode() {
|
|
208
|
+
const codec = varint__default["default"].encode(this.decoded);
|
|
209
|
+
this.encoded = codec;
|
|
210
|
+
return this.encoded
|
|
211
|
+
}
|
|
212
212
|
}
|
|
213
213
|
|
|
214
214
|
exports.PeernetCodec = PeernetCodec;
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var bs32 = require('@vandeurenglenn/base32');
|
|
4
4
|
var bs58 = require('@vandeurenglenn/base58');
|
|
5
|
-
var isHex = require('is-hex');
|
|
6
|
-
var codec = require('./codec-
|
|
5
|
+
var isHex = require('@vandeurenglenn/is-hex');
|
|
6
|
+
var codec = require('./codec-8c8c652f.js');
|
|
7
7
|
var hash = require('./hash.js');
|
|
8
8
|
require('varint');
|
|
9
9
|
require('keccak');
|
|
@@ -14,171 +14,171 @@ var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
|
|
|
14
14
|
var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
|
|
15
15
|
var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
|
|
16
16
|
|
|
17
|
-
class FormatInterface {
|
|
18
|
-
/**
|
|
19
|
-
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
20
|
-
* @param {Object} proto - {encode, decode}
|
|
21
|
-
* @param {Object} options - {hashFormat, name}
|
|
22
|
-
*/
|
|
23
|
-
constructor(buffer, proto, options = {}) {
|
|
24
|
-
this.protoEncode = proto.encode;
|
|
25
|
-
this.protoDecode = proto.decode;
|
|
26
|
-
this.hashFormat = options.hashFormat || 'bs32';
|
|
27
|
-
if (options.name) this.name = options.name;
|
|
28
|
-
if (buffer instanceof Uint8Array) return this.fromUint8Array(buffer)
|
|
29
|
-
else if (buffer instanceof ArrayBuffer) return this.fromArrayBuffer(buffer)
|
|
30
|
-
else if (buffer.name === options.name) return buffer
|
|
31
|
-
else if (typeof buffer === 'string') {
|
|
32
|
-
if (isHex__default["default"](buffer)) this.fromHex(buffer);
|
|
33
|
-
else if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
|
|
34
|
-
else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
|
|
35
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
36
|
-
} else {
|
|
37
|
-
this.create(buffer);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @return {PeernetHash}
|
|
43
|
-
*/
|
|
44
|
-
get peernetHash() {
|
|
45
|
-
return new hash(this.decoded, {name: this.name})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @return {peernetHash}
|
|
50
|
-
*/
|
|
51
|
-
get hash() {
|
|
52
|
-
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
53
|
-
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
54
|
-
return this.peernetHash[`to${format}`]()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* @return {Object}
|
|
59
|
-
*/
|
|
60
|
-
decode() {
|
|
61
|
-
let encoded = this.encoded;
|
|
62
|
-
const discoCodec = new codec.PeernetCodec(this.encoded);
|
|
63
|
-
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
64
|
-
this.name = discoCodec.name;
|
|
65
|
-
this.decoded = this.protoDecode(encoded);
|
|
66
|
-
return this.decoded
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @return {Buffer}
|
|
71
|
-
*/
|
|
72
|
-
encode(decoded) {
|
|
73
|
-
if (!decoded) decoded = this.decoded;
|
|
74
|
-
const codec$1 = new codec.PeernetCodec(this.name);
|
|
75
|
-
const encoded = this.protoEncode(decoded);
|
|
76
|
-
const uint8Array = new Uint8Array(encoded.length + codec$1.codecBuffer.length);
|
|
77
|
-
uint8Array.set(codec$1.codecBuffer);
|
|
78
|
-
uint8Array.set(encoded, codec$1.codecBuffer.length);
|
|
79
|
-
this.encoded = uint8Array;
|
|
80
|
-
return this.encoded
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
hasCodec() {
|
|
84
|
-
if (!this.encoded) return false
|
|
85
|
-
const codec$1 = new codec.PeernetCodec(this.encoded);
|
|
86
|
-
if (codec$1.name) return true
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
fromUint8Array(buffer) {
|
|
90
|
-
this.encoded = buffer;
|
|
91
|
-
if (!this.hasCodec()) this.create(
|
|
92
|
-
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
93
|
-
);
|
|
94
|
-
else this.decode();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
fromArrayBuffer(buffer) {
|
|
98
|
-
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
99
|
-
if (!this.hasCodec()) this.create(
|
|
100
|
-
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
101
|
-
);
|
|
102
|
-
else this.decode();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* @param {Buffer} encoded
|
|
107
|
-
*/
|
|
108
|
-
fromEncoded(encoded) {
|
|
109
|
-
this.encoded = encoded;
|
|
110
|
-
this.decode();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* @param {String} encoded
|
|
115
|
-
*/
|
|
116
|
-
fromHex(encoded) {
|
|
117
|
-
this.encoded = Buffer.from(encoded, 'hex');
|
|
118
|
-
this.decode();
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* @param {String} encoded
|
|
123
|
-
*/
|
|
124
|
-
fromBs32(encoded) {
|
|
125
|
-
this.encoded = bs32__default["default"].decode(encoded);
|
|
126
|
-
this.decode();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* @param {String} encoded
|
|
131
|
-
*/
|
|
132
|
-
fromBs58(encoded) {
|
|
133
|
-
this.encoded = bs58__default["default"].decode(encoded);
|
|
134
|
-
this.decode();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* @return {String} encoded
|
|
139
|
-
*/
|
|
140
|
-
toHex() {
|
|
141
|
-
if (!this.encoded) this.encode();
|
|
142
|
-
return this.encoded.toString('hex')
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* @return {String} encoded
|
|
147
|
-
*/
|
|
148
|
-
toBs32() {
|
|
149
|
-
if (!this.encoded) this.encode();
|
|
150
|
-
return bs32__default["default"].encode(this.encoded)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* @return {String} encoded
|
|
155
|
-
*/
|
|
156
|
-
toBs58() {
|
|
157
|
-
if (!this.encoded) this.encode();
|
|
158
|
-
return bs58__default["default"].encode(this.encoded)
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* @param {Object} data
|
|
163
|
-
*/
|
|
164
|
-
create(data) {
|
|
165
|
-
const decoded = {};
|
|
166
|
-
if (this.keys?.length > 0) {
|
|
167
|
-
for (const key of this.keys) {
|
|
168
|
-
Object.defineProperties(decoded, {
|
|
169
|
-
[key]: {
|
|
170
|
-
enumerable: true,
|
|
171
|
-
configurable: true,
|
|
172
|
-
set: (val) => value = data[key],
|
|
173
|
-
get: () => data[key]
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
this.decoded = decoded;
|
|
179
|
-
this.encode();
|
|
180
|
-
}
|
|
181
|
-
}
|
|
17
|
+
class FormatInterface {
|
|
18
|
+
/**
|
|
19
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
20
|
+
* @param {Object} proto - {encode, decode}
|
|
21
|
+
* @param {Object} options - {hashFormat, name}
|
|
22
|
+
*/
|
|
23
|
+
constructor(buffer, proto, options = {}) {
|
|
24
|
+
this.protoEncode = proto.encode;
|
|
25
|
+
this.protoDecode = proto.decode;
|
|
26
|
+
this.hashFormat = options.hashFormat || 'bs32';
|
|
27
|
+
if (options.name) this.name = options.name;
|
|
28
|
+
if (buffer instanceof Uint8Array) return this.fromUint8Array(buffer)
|
|
29
|
+
else if (buffer instanceof ArrayBuffer) return this.fromArrayBuffer(buffer)
|
|
30
|
+
else if (buffer.name === options.name) return buffer
|
|
31
|
+
else if (typeof buffer === 'string') {
|
|
32
|
+
if (isHex__default["default"](buffer)) this.fromHex(buffer);
|
|
33
|
+
else if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
|
|
34
|
+
else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
|
|
35
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
36
|
+
} else {
|
|
37
|
+
this.create(buffer);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @return {PeernetHash}
|
|
43
|
+
*/
|
|
44
|
+
get peernetHash() {
|
|
45
|
+
return new hash(this.decoded, {name: this.name})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @return {peernetHash}
|
|
50
|
+
*/
|
|
51
|
+
get hash() {
|
|
52
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
53
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
54
|
+
return this.peernetHash[`to${format}`]()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @return {Object}
|
|
59
|
+
*/
|
|
60
|
+
decode() {
|
|
61
|
+
let encoded = this.encoded;
|
|
62
|
+
const discoCodec = new codec.PeernetCodec(this.encoded);
|
|
63
|
+
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
64
|
+
this.name = discoCodec.name;
|
|
65
|
+
this.decoded = this.protoDecode(encoded);
|
|
66
|
+
return this.decoded
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @return {Buffer}
|
|
71
|
+
*/
|
|
72
|
+
encode(decoded) {
|
|
73
|
+
if (!decoded) decoded = this.decoded;
|
|
74
|
+
const codec$1 = new codec.PeernetCodec(this.name);
|
|
75
|
+
const encoded = this.protoEncode(decoded);
|
|
76
|
+
const uint8Array = new Uint8Array(encoded.length + codec$1.codecBuffer.length);
|
|
77
|
+
uint8Array.set(codec$1.codecBuffer);
|
|
78
|
+
uint8Array.set(encoded, codec$1.codecBuffer.length);
|
|
79
|
+
this.encoded = uint8Array;
|
|
80
|
+
return this.encoded
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
hasCodec() {
|
|
84
|
+
if (!this.encoded) return false
|
|
85
|
+
const codec$1 = new codec.PeernetCodec(this.encoded);
|
|
86
|
+
if (codec$1.name) return true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fromUint8Array(buffer) {
|
|
90
|
+
this.encoded = buffer;
|
|
91
|
+
if (!this.hasCodec()) this.create(
|
|
92
|
+
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
93
|
+
);
|
|
94
|
+
else this.decode();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fromArrayBuffer(buffer) {
|
|
98
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
99
|
+
if (!this.hasCodec()) this.create(
|
|
100
|
+
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
101
|
+
);
|
|
102
|
+
else this.decode();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {Buffer} encoded
|
|
107
|
+
*/
|
|
108
|
+
fromEncoded(encoded) {
|
|
109
|
+
this.encoded = encoded;
|
|
110
|
+
this.decode();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param {String} encoded
|
|
115
|
+
*/
|
|
116
|
+
fromHex(encoded) {
|
|
117
|
+
this.encoded = Buffer.from(encoded, 'hex');
|
|
118
|
+
this.decode();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {String} encoded
|
|
123
|
+
*/
|
|
124
|
+
fromBs32(encoded) {
|
|
125
|
+
this.encoded = bs32__default["default"].decode(encoded);
|
|
126
|
+
this.decode();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {String} encoded
|
|
131
|
+
*/
|
|
132
|
+
fromBs58(encoded) {
|
|
133
|
+
this.encoded = bs58__default["default"].decode(encoded);
|
|
134
|
+
this.decode();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @return {String} encoded
|
|
139
|
+
*/
|
|
140
|
+
toHex() {
|
|
141
|
+
if (!this.encoded) this.encode();
|
|
142
|
+
return this.encoded.toString('hex')
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @return {String} encoded
|
|
147
|
+
*/
|
|
148
|
+
toBs32() {
|
|
149
|
+
if (!this.encoded) this.encode();
|
|
150
|
+
return bs32__default["default"].encode(this.encoded)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @return {String} encoded
|
|
155
|
+
*/
|
|
156
|
+
toBs58() {
|
|
157
|
+
if (!this.encoded) this.encode();
|
|
158
|
+
return bs58__default["default"].encode(this.encoded)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @param {Object} data
|
|
163
|
+
*/
|
|
164
|
+
create(data) {
|
|
165
|
+
const decoded = {};
|
|
166
|
+
if (this.keys?.length > 0) {
|
|
167
|
+
for (const key of this.keys) {
|
|
168
|
+
Object.defineProperties(decoded, {
|
|
169
|
+
[key]: {
|
|
170
|
+
enumerable: true,
|
|
171
|
+
configurable: true,
|
|
172
|
+
set: (val) => value = data[key],
|
|
173
|
+
get: () => data[key]
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
this.decoded = decoded;
|
|
179
|
+
this.encode();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
module.exports = FormatInterface;
|