@leofcoin/codec-format-interface 1.2.8 → 1.3.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/index.js +10 -5
- package/dist/module.js +4836 -0
- package/package.json +2 -3
- package/rollup.config.js +12 -0
- package/src/codec-format-interface.js +10 -4
package/dist/module.js
ADDED
|
@@ -0,0 +1,4836 @@
|
|
|
1
|
+
import stream$1 from 'stream';
|
|
2
|
+
import events from 'events';
|
|
3
|
+
import buffer from 'buffer';
|
|
4
|
+
import util from 'util';
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
import os from 'os';
|
|
8
|
+
|
|
9
|
+
// base-x encoding / decoding
|
|
10
|
+
// Copyright (c) 2018 base-x contributors
|
|
11
|
+
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
|
12
|
+
// Distributed under the MIT software license, see the accompanying
|
|
13
|
+
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
14
|
+
const base = ALPHABET => {
|
|
15
|
+
if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
|
|
16
|
+
const BASE_MAP = new Uint8Array(256);
|
|
17
|
+
for (let j = 0; j < BASE_MAP.length; j++) {
|
|
18
|
+
BASE_MAP[j] = 255;
|
|
19
|
+
}
|
|
20
|
+
for (let i = 0; i < ALPHABET.length; i++) {
|
|
21
|
+
const x = ALPHABET.charAt(i);
|
|
22
|
+
const xc = x.charCodeAt(0);
|
|
23
|
+
if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
|
|
24
|
+
BASE_MAP[xc] = i;
|
|
25
|
+
}
|
|
26
|
+
const BASE = ALPHABET.length;
|
|
27
|
+
const LEADER = ALPHABET.charAt(0);
|
|
28
|
+
const FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
|
|
29
|
+
const iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
|
|
30
|
+
|
|
31
|
+
const encode = source => {
|
|
32
|
+
if (source instanceof Uint8Array) ; else if (ArrayBuffer.isView(source)) {
|
|
33
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
34
|
+
} else if (Array.isArray(source)) {
|
|
35
|
+
source = Uint8Array.from(source);
|
|
36
|
+
}
|
|
37
|
+
if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }
|
|
38
|
+
if (source.length === 0) { return '' }
|
|
39
|
+
// Skip & count leading zeroes.
|
|
40
|
+
let zeroes = 0;
|
|
41
|
+
let length = 0;
|
|
42
|
+
let pbegin = 0;
|
|
43
|
+
const pend = source.length;
|
|
44
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
|
45
|
+
pbegin++;
|
|
46
|
+
zeroes++;
|
|
47
|
+
}
|
|
48
|
+
// Allocate enough space in big-endian base58 representation.
|
|
49
|
+
const size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
|
|
50
|
+
const b58 = new Uint8Array(size);
|
|
51
|
+
// Process the bytes.
|
|
52
|
+
while (pbegin !== pend) {
|
|
53
|
+
let carry = source[pbegin];
|
|
54
|
+
// Apply "b58 = b58 * 256 + ch".
|
|
55
|
+
let i = 0;
|
|
56
|
+
for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
|
|
57
|
+
carry += (256 * b58[it1]) >>> 0;
|
|
58
|
+
b58[it1] = (carry % BASE) >>> 0;
|
|
59
|
+
carry = (carry / BASE) >>> 0;
|
|
60
|
+
}
|
|
61
|
+
if (carry !== 0) { throw new Error('Non-zero carry') }
|
|
62
|
+
length = i;
|
|
63
|
+
pbegin++;
|
|
64
|
+
}
|
|
65
|
+
// Skip leading zeroes in base58 result.
|
|
66
|
+
let it2 = size - length;
|
|
67
|
+
while (it2 !== size && b58[it2] === 0) {
|
|
68
|
+
it2++;
|
|
69
|
+
}
|
|
70
|
+
// Translate the result into a string.
|
|
71
|
+
let str = LEADER.repeat(zeroes);
|
|
72
|
+
for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]); }
|
|
73
|
+
return str
|
|
74
|
+
};
|
|
75
|
+
const decodeUnsafe = source => {
|
|
76
|
+
if (typeof source !== 'string') { throw new TypeError('Expected String') }
|
|
77
|
+
if (source.length === 0) { return new Uint8Array() }
|
|
78
|
+
let psz = 0;
|
|
79
|
+
// Skip and count leading '1's.
|
|
80
|
+
let zeroes = 0;
|
|
81
|
+
let length = 0;
|
|
82
|
+
while (source[psz] === LEADER) {
|
|
83
|
+
zeroes++;
|
|
84
|
+
psz++;
|
|
85
|
+
}
|
|
86
|
+
// Allocate enough space in big-endian base256 representation.
|
|
87
|
+
const size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
|
|
88
|
+
let b256 = new Uint8Array(size);
|
|
89
|
+
// Process the characters.
|
|
90
|
+
while (source[psz]) {
|
|
91
|
+
// Decode character
|
|
92
|
+
let carry = BASE_MAP[source.charCodeAt(psz)];
|
|
93
|
+
// Invalid character
|
|
94
|
+
if (carry === 255) { return }
|
|
95
|
+
let i = 0;
|
|
96
|
+
for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
|
|
97
|
+
carry += (BASE * b256[it3]) >>> 0;
|
|
98
|
+
b256[it3] = (carry % 256) >>> 0;
|
|
99
|
+
carry = (carry / 256) >>> 0;
|
|
100
|
+
}
|
|
101
|
+
if (carry !== 0) { throw new Error('Non-zero carry') }
|
|
102
|
+
length = i;
|
|
103
|
+
psz++;
|
|
104
|
+
}
|
|
105
|
+
// Skip leading zeroes in b256.
|
|
106
|
+
let it4 = size - length;
|
|
107
|
+
while (it4 !== size && b256[it4] === 0) {
|
|
108
|
+
it4++;
|
|
109
|
+
}
|
|
110
|
+
let vch = new Uint8Array(zeroes + (size - it4));
|
|
111
|
+
let j = zeroes;
|
|
112
|
+
while (it4 !== size) {
|
|
113
|
+
vch[j++] = b256[it4++];
|
|
114
|
+
}
|
|
115
|
+
return vch
|
|
116
|
+
};
|
|
117
|
+
const decode = string => {
|
|
118
|
+
const buffer = decodeUnsafe(string);
|
|
119
|
+
if (buffer) { return buffer }
|
|
120
|
+
throw new Error('Non-base' + BASE + ' character')
|
|
121
|
+
};
|
|
122
|
+
return {
|
|
123
|
+
encode: encode,
|
|
124
|
+
decodeUnsafe: decodeUnsafe,
|
|
125
|
+
decode: decode
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const base32 = 'abcdefghijklmnopqrstuvwxyz234567';
|
|
130
|
+
const base32Hex = '0123456789abcdefghijklmnopqrstuv';
|
|
131
|
+
|
|
132
|
+
const decode$2 = (uint8Array, hex = false) => {
|
|
133
|
+
const decoder = hex ? base(base32Hex) : base(base32);
|
|
134
|
+
return decoder.decode(uint8Array)
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
var bs32 = {
|
|
138
|
+
encode: (uint8Array, hex = false) => {
|
|
139
|
+
const encoder = hex ? base(base32Hex) : base(base32);
|
|
140
|
+
return encoder.encode(uint8Array)
|
|
141
|
+
},
|
|
142
|
+
decode: decode$2,
|
|
143
|
+
isBase32: (uint8Array, hex = false) => {
|
|
144
|
+
try {
|
|
145
|
+
decode$2(uint8Array, hex);
|
|
146
|
+
return true;
|
|
147
|
+
} catch (e) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
154
|
+
|
|
155
|
+
const decode$1 = uint8Array => base(base58).decode(uint8Array);
|
|
156
|
+
|
|
157
|
+
var bs58 = {
|
|
158
|
+
encode: uint8Array => base(base58).encode(uint8Array),
|
|
159
|
+
decode: decode$1,
|
|
160
|
+
isBase58: uint8Array => {
|
|
161
|
+
try {
|
|
162
|
+
decode$1(uint8Array);
|
|
163
|
+
return true
|
|
164
|
+
} catch (e) {
|
|
165
|
+
return false
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
var isHex = string => /^[A-F0-9]+$/i.test(string);
|
|
171
|
+
|
|
172
|
+
class BasicInterface$1 {
|
|
173
|
+
handleDecode() {
|
|
174
|
+
if (!this.decode) throw new Error('bad implementation: needs decode func')
|
|
175
|
+
return this.decode()
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
handleEncode() {
|
|
179
|
+
if (!this.encode) throw new Error('bad implementation: needs encode func')
|
|
180
|
+
return this.encode()
|
|
181
|
+
}
|
|
182
|
+
isHex(string) {
|
|
183
|
+
return isHex(string)
|
|
184
|
+
}
|
|
185
|
+
isBase32(string) {
|
|
186
|
+
return bs32.isBase32(string)
|
|
187
|
+
}
|
|
188
|
+
isBase58(string) {
|
|
189
|
+
return bs58.isBase58(string)
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* @param {String} encoded
|
|
193
|
+
*/
|
|
194
|
+
fromBs32(encoded) {
|
|
195
|
+
this.encoded = bs32.decode(encoded);
|
|
196
|
+
return this.handleDecode()
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* @param {String} encoded
|
|
201
|
+
*/
|
|
202
|
+
fromBs58(encoded) {
|
|
203
|
+
this.encoded = bs58.decode(encoded);
|
|
204
|
+
return this.handleDecode()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async toArray() {
|
|
208
|
+
const array = [];
|
|
209
|
+
for await (const value of this.encoded.values()) {
|
|
210
|
+
array.push(value);
|
|
211
|
+
}
|
|
212
|
+
return array
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
fromString(string) {
|
|
216
|
+
this.encoded = new Uint8Array(string.split(','));
|
|
217
|
+
return this.handleDecode()
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
fromArray(array) {
|
|
221
|
+
this.encoded = new Uint8Array([...array]);
|
|
222
|
+
return this.handleDecode()
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {Buffer} encoded
|
|
227
|
+
*/
|
|
228
|
+
fromEncoded(encoded) {
|
|
229
|
+
this.encoded = encoded;
|
|
230
|
+
return this.handleDecode()
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* @param {String} encoded
|
|
235
|
+
*/
|
|
236
|
+
fromHex(encoded) {
|
|
237
|
+
this.encoded = Buffer.from(encoded, 'hex');
|
|
238
|
+
return this.handleDecode()
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async toString(encoding = 'utf8') {
|
|
242
|
+
if (!this.encoded) await this.handleEncode();
|
|
243
|
+
return this.encoded.toString(encoding)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @return {String} encoded
|
|
248
|
+
*/
|
|
249
|
+
toHex() {
|
|
250
|
+
return this.toString('hex')
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* @return {String} encoded
|
|
255
|
+
*/
|
|
256
|
+
async toBs32() {
|
|
257
|
+
if (!this.encoded) await this.handleEncode();
|
|
258
|
+
return bs32.encode(this.encoded)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* @return {String} encoded
|
|
263
|
+
*/
|
|
264
|
+
async toBs58() {
|
|
265
|
+
if (!this.encoded) await this.handleEncode();
|
|
266
|
+
return bs58.encode(this.encoded)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @param {Object} data
|
|
271
|
+
*/
|
|
272
|
+
create(data) {
|
|
273
|
+
const decoded = {};
|
|
274
|
+
if (this.keys?.length > 0) {
|
|
275
|
+
for (const key of this.keys) {
|
|
276
|
+
Object.defineProperties(decoded, {
|
|
277
|
+
[key]: {
|
|
278
|
+
enumerable: true,
|
|
279
|
+
configurable: true,
|
|
280
|
+
set: (val) => value = data[key],
|
|
281
|
+
get: () => data[key]
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
this.decoded = decoded;
|
|
287
|
+
return this.encode()
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
var encode_1 = encode;
|
|
293
|
+
|
|
294
|
+
var MSB$1 = 0x80
|
|
295
|
+
, REST$1 = 0x7F
|
|
296
|
+
, MSBALL = ~REST$1
|
|
297
|
+
, INT = Math.pow(2, 31);
|
|
298
|
+
|
|
299
|
+
function encode(num, out, offset) {
|
|
300
|
+
if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
|
|
301
|
+
encode.bytes = 0;
|
|
302
|
+
throw new RangeError('Could not encode varint')
|
|
303
|
+
}
|
|
304
|
+
out = out || [];
|
|
305
|
+
offset = offset || 0;
|
|
306
|
+
var oldOffset = offset;
|
|
307
|
+
|
|
308
|
+
while(num >= INT) {
|
|
309
|
+
out[offset++] = (num & 0xFF) | MSB$1;
|
|
310
|
+
num /= 128;
|
|
311
|
+
}
|
|
312
|
+
while(num & MSBALL) {
|
|
313
|
+
out[offset++] = (num & 0xFF) | MSB$1;
|
|
314
|
+
num >>>= 7;
|
|
315
|
+
}
|
|
316
|
+
out[offset] = num | 0;
|
|
317
|
+
|
|
318
|
+
encode.bytes = offset - oldOffset + 1;
|
|
319
|
+
|
|
320
|
+
return out
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
var decode = read;
|
|
324
|
+
|
|
325
|
+
var MSB = 0x80
|
|
326
|
+
, REST = 0x7F;
|
|
327
|
+
|
|
328
|
+
function read(buf, offset) {
|
|
329
|
+
var res = 0
|
|
330
|
+
, offset = offset || 0
|
|
331
|
+
, shift = 0
|
|
332
|
+
, counter = offset
|
|
333
|
+
, b
|
|
334
|
+
, l = buf.length;
|
|
335
|
+
|
|
336
|
+
do {
|
|
337
|
+
if (counter >= l || shift > 49) {
|
|
338
|
+
read.bytes = 0;
|
|
339
|
+
throw new RangeError('Could not decode varint')
|
|
340
|
+
}
|
|
341
|
+
b = buf[counter++];
|
|
342
|
+
res += shift < 28
|
|
343
|
+
? (b & REST) << shift
|
|
344
|
+
: (b & REST) * Math.pow(2, shift);
|
|
345
|
+
shift += 7;
|
|
346
|
+
} while (b >= MSB)
|
|
347
|
+
|
|
348
|
+
read.bytes = counter - offset;
|
|
349
|
+
|
|
350
|
+
return res
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
var N1 = Math.pow(2, 7);
|
|
354
|
+
var N2 = Math.pow(2, 14);
|
|
355
|
+
var N3 = Math.pow(2, 21);
|
|
356
|
+
var N4 = Math.pow(2, 28);
|
|
357
|
+
var N5 = Math.pow(2, 35);
|
|
358
|
+
var N6 = Math.pow(2, 42);
|
|
359
|
+
var N7 = Math.pow(2, 49);
|
|
360
|
+
var N8 = Math.pow(2, 56);
|
|
361
|
+
var N9 = Math.pow(2, 63);
|
|
362
|
+
|
|
363
|
+
var length = function (value) {
|
|
364
|
+
return (
|
|
365
|
+
value < N1 ? 1
|
|
366
|
+
: value < N2 ? 2
|
|
367
|
+
: value < N3 ? 3
|
|
368
|
+
: value < N4 ? 4
|
|
369
|
+
: value < N5 ? 5
|
|
370
|
+
: value < N6 ? 6
|
|
371
|
+
: value < N7 ? 7
|
|
372
|
+
: value < N8 ? 8
|
|
373
|
+
: value < N9 ? 9
|
|
374
|
+
: 10
|
|
375
|
+
)
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
var varint = {
|
|
379
|
+
encode: encode_1
|
|
380
|
+
, decode: decode
|
|
381
|
+
, encodingLength: length
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
var codecs = {
|
|
385
|
+
// just a hash
|
|
386
|
+
'disco-hash': {
|
|
387
|
+
codec: parseInt('30', 16),
|
|
388
|
+
hashAlg: 'dbl-keccak-256', // ,
|
|
389
|
+
// testnet: 'olivia'
|
|
390
|
+
},
|
|
391
|
+
'peernet-peer-response': {
|
|
392
|
+
codec: parseInt('707072', 16),
|
|
393
|
+
hashAlg: 'keccak-256',
|
|
394
|
+
},
|
|
395
|
+
'peernet-peer': {
|
|
396
|
+
codec: parseInt('7070', 16),
|
|
397
|
+
hashAlg: 'keccak-256',
|
|
398
|
+
},
|
|
399
|
+
'peernet-dht': {
|
|
400
|
+
codec: parseInt('706468', 16),
|
|
401
|
+
hashAlg: 'keccak-256',
|
|
402
|
+
},
|
|
403
|
+
'peernet-dht-response': {
|
|
404
|
+
codec: parseInt('706472', 16),
|
|
405
|
+
hashAlg: 'keccak-256',
|
|
406
|
+
},
|
|
407
|
+
// data
|
|
408
|
+
'peernet-data': {
|
|
409
|
+
codec: parseInt('706461', 16),
|
|
410
|
+
hashAlg: 'keccak-256',
|
|
411
|
+
},
|
|
412
|
+
'peernet-data-response': {
|
|
413
|
+
codec: parseInt('70646172', 16),
|
|
414
|
+
hashAlg: 'keccak-256',
|
|
415
|
+
},
|
|
416
|
+
// message
|
|
417
|
+
'peernet-message': {
|
|
418
|
+
codec: parseInt('706d65', 16),
|
|
419
|
+
hashAlg: 'keccak-256',
|
|
420
|
+
},
|
|
421
|
+
// pubsub
|
|
422
|
+
'peernet-ps': {
|
|
423
|
+
codec: parseInt('707073', 16),
|
|
424
|
+
hashAlg: 'keccak-256',
|
|
425
|
+
},
|
|
426
|
+
'peernet-response': {
|
|
427
|
+
codec: parseInt('7072', 16),
|
|
428
|
+
hashAlg: 'keccak-256',
|
|
429
|
+
},
|
|
430
|
+
'peernet-request': {
|
|
431
|
+
codec: parseInt('707271', 16),
|
|
432
|
+
hashAlg: 'keccak-256',
|
|
433
|
+
},
|
|
434
|
+
// normal block
|
|
435
|
+
'leofcoin-block': {
|
|
436
|
+
codec: parseInt('6c62', 16),
|
|
437
|
+
hashAlg: 'dbl-keccak-512', // ,
|
|
438
|
+
// testnet: 'olivia'
|
|
439
|
+
},
|
|
440
|
+
'leofcoin-tx': {
|
|
441
|
+
codec: parseInt('6c74', 16),
|
|
442
|
+
hashAlg: 'dbl-keccak-512', // ,
|
|
443
|
+
// testnet: 'olivia'
|
|
444
|
+
},
|
|
445
|
+
// itx
|
|
446
|
+
'leofcoin-itx': {
|
|
447
|
+
codec: parseInt('6c69', 16),
|
|
448
|
+
hashAlg: 'keccak-512', // ,
|
|
449
|
+
// testnet: 'olivia'
|
|
450
|
+
},
|
|
451
|
+
// peer reputation
|
|
452
|
+
'leofcoin-pr': {
|
|
453
|
+
codec: parseInt('6c70', 16),
|
|
454
|
+
hashAlg: 'keccak-256', // ,
|
|
455
|
+
// testnet: 'olivia'
|
|
456
|
+
},
|
|
457
|
+
// chat message
|
|
458
|
+
'chat-message': {
|
|
459
|
+
codec: parseInt('636d', 16),
|
|
460
|
+
hashAlg: 'dbl-keccak-256',
|
|
461
|
+
},
|
|
462
|
+
'peernet-file' : {
|
|
463
|
+
codec: parseInt('7066', 16),
|
|
464
|
+
hashAlg: 'keccak-256',
|
|
465
|
+
},
|
|
466
|
+
'peernet-file-response' : {
|
|
467
|
+
codec: parseInt('706672', 16),
|
|
468
|
+
hashAlg: 'keccak-256',
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
class PeernetCodec extends BasicInterface$1 {
|
|
473
|
+
get codecs() {
|
|
474
|
+
return {...globalThis.peernet.codecs, ...codecs}
|
|
475
|
+
}
|
|
476
|
+
constructor(buffer) {
|
|
477
|
+
super();
|
|
478
|
+
if (buffer) {
|
|
479
|
+
if (buffer instanceof Uint8Array) {
|
|
480
|
+
const codec = varint.decode(buffer);
|
|
481
|
+
const name = this.getCodecName(codec);
|
|
482
|
+
if (name) {
|
|
483
|
+
this.name = name;
|
|
484
|
+
this.encoded = buffer;
|
|
485
|
+
this.decode(buffer);
|
|
486
|
+
} else {
|
|
487
|
+
this.encode(buffer);
|
|
488
|
+
}
|
|
489
|
+
} else if (buffer instanceof ArrayBuffer) {
|
|
490
|
+
const encoded = new Uint8Array(buffer.byteLength);
|
|
491
|
+
|
|
492
|
+
for (let i = 0; i < buffer.byteLength; i++) {
|
|
493
|
+
encoded[i] = buffer[i];
|
|
494
|
+
}
|
|
495
|
+
this.encoded = encoded;
|
|
496
|
+
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
497
|
+
this.decode(buffer);
|
|
498
|
+
return
|
|
499
|
+
}
|
|
500
|
+
if (typeof buffer === 'string') {
|
|
501
|
+
if (this.codecs[buffer]) this.fromName(buffer);
|
|
502
|
+
else if (this.isHex(buffer)) this.fromHex(buffer);
|
|
503
|
+
else if (this.isBase32(buffer)) this.fromBs32(buffer);
|
|
504
|
+
else if (this.isBase58(buffer)) this.fromBs58(buffer);
|
|
505
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
506
|
+
}
|
|
507
|
+
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
fromEncoded(encoded) {
|
|
512
|
+
const codec = varint.decode(encoded);
|
|
513
|
+
const name = this.getCodecName(codec);
|
|
514
|
+
this.name = name;
|
|
515
|
+
this.encoded = encoded;
|
|
516
|
+
this.decode(encoded);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
getCodec(name) {
|
|
520
|
+
return this.codecs[name].codec
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
getCodecName(codec) {
|
|
524
|
+
return Object.keys(this.codecs).reduce((p, c) => {
|
|
525
|
+
const item = this.codecs[c];
|
|
526
|
+
if (item.codec === codec) return c;
|
|
527
|
+
else return p;
|
|
528
|
+
}, undefined)
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
getHashAlg(name) {
|
|
532
|
+
return this.codecs[name].hashAlg
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
fromCodec(codec) {
|
|
536
|
+
this.name = this.getCodecName(codec);
|
|
537
|
+
this.hashAlg = this.getHashAlg(this.name);
|
|
538
|
+
|
|
539
|
+
this.codec = this.getCodec(this.name);
|
|
540
|
+
this.codecBuffer = varint.encode(codec);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
fromName(name) {
|
|
544
|
+
const codec = this.getCodec(name);
|
|
545
|
+
this.name = name;
|
|
546
|
+
this.codec = codec;
|
|
547
|
+
this.hashAlg = this.getHashAlg(name);
|
|
548
|
+
this.codecBuffer = varint.encode(codec);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
decode() {
|
|
552
|
+
const codec = varint.decode(this.encoded);
|
|
553
|
+
this.fromCodec(codec);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
encode() {
|
|
557
|
+
const codec = varint.encode(this.decoded);
|
|
558
|
+
this.encoded = codec;
|
|
559
|
+
return this.encoded
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
564
|
+
|
|
565
|
+
function commonjsRequire () {
|
|
566
|
+
throw new Error('Dynamic requires are not currently supported by rollup-plugin-commonjs');
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function createCommonjsModule(fn, module) {
|
|
570
|
+
return module = { exports: {} }, fn(module, module.exports), module.exports;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
var stream = stream$1;
|
|
574
|
+
|
|
575
|
+
function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
576
|
+
|
|
577
|
+
function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$1(Object(source), true).forEach(function (key) { _defineProperty$2(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
578
|
+
|
|
579
|
+
function _defineProperty$2(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
580
|
+
|
|
581
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
582
|
+
|
|
583
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
584
|
+
|
|
585
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
586
|
+
|
|
587
|
+
var Buffer$4 = buffer.Buffer;
|
|
588
|
+
|
|
589
|
+
var inspect = util.inspect;
|
|
590
|
+
|
|
591
|
+
var custom = inspect && inspect.custom || 'inspect';
|
|
592
|
+
|
|
593
|
+
function copyBuffer(src, target, offset) {
|
|
594
|
+
Buffer$4.prototype.copy.call(src, target, offset);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
var buffer_list =
|
|
598
|
+
/*#__PURE__*/
|
|
599
|
+
function () {
|
|
600
|
+
function BufferList() {
|
|
601
|
+
_classCallCheck(this, BufferList);
|
|
602
|
+
|
|
603
|
+
this.head = null;
|
|
604
|
+
this.tail = null;
|
|
605
|
+
this.length = 0;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
_createClass(BufferList, [{
|
|
609
|
+
key: "push",
|
|
610
|
+
value: function push(v) {
|
|
611
|
+
var entry = {
|
|
612
|
+
data: v,
|
|
613
|
+
next: null
|
|
614
|
+
};
|
|
615
|
+
if (this.length > 0) this.tail.next = entry;else this.head = entry;
|
|
616
|
+
this.tail = entry;
|
|
617
|
+
++this.length;
|
|
618
|
+
}
|
|
619
|
+
}, {
|
|
620
|
+
key: "unshift",
|
|
621
|
+
value: function unshift(v) {
|
|
622
|
+
var entry = {
|
|
623
|
+
data: v,
|
|
624
|
+
next: this.head
|
|
625
|
+
};
|
|
626
|
+
if (this.length === 0) this.tail = entry;
|
|
627
|
+
this.head = entry;
|
|
628
|
+
++this.length;
|
|
629
|
+
}
|
|
630
|
+
}, {
|
|
631
|
+
key: "shift",
|
|
632
|
+
value: function shift() {
|
|
633
|
+
if (this.length === 0) return;
|
|
634
|
+
var ret = this.head.data;
|
|
635
|
+
if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
|
|
636
|
+
--this.length;
|
|
637
|
+
return ret;
|
|
638
|
+
}
|
|
639
|
+
}, {
|
|
640
|
+
key: "clear",
|
|
641
|
+
value: function clear() {
|
|
642
|
+
this.head = this.tail = null;
|
|
643
|
+
this.length = 0;
|
|
644
|
+
}
|
|
645
|
+
}, {
|
|
646
|
+
key: "join",
|
|
647
|
+
value: function join(s) {
|
|
648
|
+
if (this.length === 0) return '';
|
|
649
|
+
var p = this.head;
|
|
650
|
+
var ret = '' + p.data;
|
|
651
|
+
|
|
652
|
+
while (p = p.next) {
|
|
653
|
+
ret += s + p.data;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
return ret;
|
|
657
|
+
}
|
|
658
|
+
}, {
|
|
659
|
+
key: "concat",
|
|
660
|
+
value: function concat(n) {
|
|
661
|
+
if (this.length === 0) return Buffer$4.alloc(0);
|
|
662
|
+
var ret = Buffer$4.allocUnsafe(n >>> 0);
|
|
663
|
+
var p = this.head;
|
|
664
|
+
var i = 0;
|
|
665
|
+
|
|
666
|
+
while (p) {
|
|
667
|
+
copyBuffer(p.data, ret, i);
|
|
668
|
+
i += p.data.length;
|
|
669
|
+
p = p.next;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
return ret;
|
|
673
|
+
} // Consumes a specified amount of bytes or characters from the buffered data.
|
|
674
|
+
|
|
675
|
+
}, {
|
|
676
|
+
key: "consume",
|
|
677
|
+
value: function consume(n, hasStrings) {
|
|
678
|
+
var ret;
|
|
679
|
+
|
|
680
|
+
if (n < this.head.data.length) {
|
|
681
|
+
// `slice` is the same for buffers and strings.
|
|
682
|
+
ret = this.head.data.slice(0, n);
|
|
683
|
+
this.head.data = this.head.data.slice(n);
|
|
684
|
+
} else if (n === this.head.data.length) {
|
|
685
|
+
// First chunk is a perfect match.
|
|
686
|
+
ret = this.shift();
|
|
687
|
+
} else {
|
|
688
|
+
// Result spans more than one buffer.
|
|
689
|
+
ret = hasStrings ? this._getString(n) : this._getBuffer(n);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
return ret;
|
|
693
|
+
}
|
|
694
|
+
}, {
|
|
695
|
+
key: "first",
|
|
696
|
+
value: function first() {
|
|
697
|
+
return this.head.data;
|
|
698
|
+
} // Consumes a specified amount of characters from the buffered data.
|
|
699
|
+
|
|
700
|
+
}, {
|
|
701
|
+
key: "_getString",
|
|
702
|
+
value: function _getString(n) {
|
|
703
|
+
var p = this.head;
|
|
704
|
+
var c = 1;
|
|
705
|
+
var ret = p.data;
|
|
706
|
+
n -= ret.length;
|
|
707
|
+
|
|
708
|
+
while (p = p.next) {
|
|
709
|
+
var str = p.data;
|
|
710
|
+
var nb = n > str.length ? str.length : n;
|
|
711
|
+
if (nb === str.length) ret += str;else ret += str.slice(0, n);
|
|
712
|
+
n -= nb;
|
|
713
|
+
|
|
714
|
+
if (n === 0) {
|
|
715
|
+
if (nb === str.length) {
|
|
716
|
+
++c;
|
|
717
|
+
if (p.next) this.head = p.next;else this.head = this.tail = null;
|
|
718
|
+
} else {
|
|
719
|
+
this.head = p;
|
|
720
|
+
p.data = str.slice(nb);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
++c;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
this.length -= c;
|
|
730
|
+
return ret;
|
|
731
|
+
} // Consumes a specified amount of bytes from the buffered data.
|
|
732
|
+
|
|
733
|
+
}, {
|
|
734
|
+
key: "_getBuffer",
|
|
735
|
+
value: function _getBuffer(n) {
|
|
736
|
+
var ret = Buffer$4.allocUnsafe(n);
|
|
737
|
+
var p = this.head;
|
|
738
|
+
var c = 1;
|
|
739
|
+
p.data.copy(ret);
|
|
740
|
+
n -= p.data.length;
|
|
741
|
+
|
|
742
|
+
while (p = p.next) {
|
|
743
|
+
var buf = p.data;
|
|
744
|
+
var nb = n > buf.length ? buf.length : n;
|
|
745
|
+
buf.copy(ret, ret.length - n, 0, nb);
|
|
746
|
+
n -= nb;
|
|
747
|
+
|
|
748
|
+
if (n === 0) {
|
|
749
|
+
if (nb === buf.length) {
|
|
750
|
+
++c;
|
|
751
|
+
if (p.next) this.head = p.next;else this.head = this.tail = null;
|
|
752
|
+
} else {
|
|
753
|
+
this.head = p;
|
|
754
|
+
p.data = buf.slice(nb);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
break;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
++c;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
this.length -= c;
|
|
764
|
+
return ret;
|
|
765
|
+
} // Make sure the linked list only shows the minimal necessary information.
|
|
766
|
+
|
|
767
|
+
}, {
|
|
768
|
+
key: custom,
|
|
769
|
+
value: function value(_, options) {
|
|
770
|
+
return inspect(this, _objectSpread$1({}, options, {
|
|
771
|
+
// Only inspect one level.
|
|
772
|
+
depth: 0,
|
|
773
|
+
// It should not recurse.
|
|
774
|
+
customInspect: false
|
|
775
|
+
}));
|
|
776
|
+
}
|
|
777
|
+
}]);
|
|
778
|
+
|
|
779
|
+
return BufferList;
|
|
780
|
+
}();
|
|
781
|
+
|
|
782
|
+
function destroy(err, cb) {
|
|
783
|
+
var _this = this;
|
|
784
|
+
|
|
785
|
+
var readableDestroyed = this._readableState && this._readableState.destroyed;
|
|
786
|
+
var writableDestroyed = this._writableState && this._writableState.destroyed;
|
|
787
|
+
|
|
788
|
+
if (readableDestroyed || writableDestroyed) {
|
|
789
|
+
if (cb) {
|
|
790
|
+
cb(err);
|
|
791
|
+
} else if (err) {
|
|
792
|
+
if (!this._writableState) {
|
|
793
|
+
process.nextTick(emitErrorNT, this, err);
|
|
794
|
+
} else if (!this._writableState.errorEmitted) {
|
|
795
|
+
this._writableState.errorEmitted = true;
|
|
796
|
+
process.nextTick(emitErrorNT, this, err);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
return this;
|
|
801
|
+
} // we set destroyed to true before firing error callbacks in order
|
|
802
|
+
// to make it re-entrance safe in case destroy() is called within callbacks
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
if (this._readableState) {
|
|
806
|
+
this._readableState.destroyed = true;
|
|
807
|
+
} // if this is a duplex stream mark the writable part as destroyed as well
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
if (this._writableState) {
|
|
811
|
+
this._writableState.destroyed = true;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
this._destroy(err || null, function (err) {
|
|
815
|
+
if (!cb && err) {
|
|
816
|
+
if (!_this._writableState) {
|
|
817
|
+
process.nextTick(emitErrorAndCloseNT, _this, err);
|
|
818
|
+
} else if (!_this._writableState.errorEmitted) {
|
|
819
|
+
_this._writableState.errorEmitted = true;
|
|
820
|
+
process.nextTick(emitErrorAndCloseNT, _this, err);
|
|
821
|
+
} else {
|
|
822
|
+
process.nextTick(emitCloseNT, _this);
|
|
823
|
+
}
|
|
824
|
+
} else if (cb) {
|
|
825
|
+
process.nextTick(emitCloseNT, _this);
|
|
826
|
+
cb(err);
|
|
827
|
+
} else {
|
|
828
|
+
process.nextTick(emitCloseNT, _this);
|
|
829
|
+
}
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
return this;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function emitErrorAndCloseNT(self, err) {
|
|
836
|
+
emitErrorNT(self, err);
|
|
837
|
+
emitCloseNT(self);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
function emitCloseNT(self) {
|
|
841
|
+
if (self._writableState && !self._writableState.emitClose) return;
|
|
842
|
+
if (self._readableState && !self._readableState.emitClose) return;
|
|
843
|
+
self.emit('close');
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
function undestroy() {
|
|
847
|
+
if (this._readableState) {
|
|
848
|
+
this._readableState.destroyed = false;
|
|
849
|
+
this._readableState.reading = false;
|
|
850
|
+
this._readableState.ended = false;
|
|
851
|
+
this._readableState.endEmitted = false;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
if (this._writableState) {
|
|
855
|
+
this._writableState.destroyed = false;
|
|
856
|
+
this._writableState.ended = false;
|
|
857
|
+
this._writableState.ending = false;
|
|
858
|
+
this._writableState.finalCalled = false;
|
|
859
|
+
this._writableState.prefinished = false;
|
|
860
|
+
this._writableState.finished = false;
|
|
861
|
+
this._writableState.errorEmitted = false;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function emitErrorNT(self, err) {
|
|
866
|
+
self.emit('error', err);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
function errorOrDestroy$2(stream, err) {
|
|
870
|
+
// We have tests that rely on errors being emitted
|
|
871
|
+
// in the same tick, so changing this is semver major.
|
|
872
|
+
// For now when you opt-in to autoDestroy we allow
|
|
873
|
+
// the error to be emitted nextTick. In a future
|
|
874
|
+
// semver major update we should change the default to this.
|
|
875
|
+
var rState = stream._readableState;
|
|
876
|
+
var wState = stream._writableState;
|
|
877
|
+
if (rState && rState.autoDestroy || wState && wState.autoDestroy) stream.destroy(err);else stream.emit('error', err);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
var destroy_1 = {
|
|
881
|
+
destroy: destroy,
|
|
882
|
+
undestroy: undestroy,
|
|
883
|
+
errorOrDestroy: errorOrDestroy$2
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
const codes = {};
|
|
887
|
+
|
|
888
|
+
function createErrorType(code, message, Base) {
|
|
889
|
+
if (!Base) {
|
|
890
|
+
Base = Error;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
function getMessage (arg1, arg2, arg3) {
|
|
894
|
+
if (typeof message === 'string') {
|
|
895
|
+
return message
|
|
896
|
+
} else {
|
|
897
|
+
return message(arg1, arg2, arg3)
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
class NodeError extends Base {
|
|
902
|
+
constructor (arg1, arg2, arg3) {
|
|
903
|
+
super(getMessage(arg1, arg2, arg3));
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
NodeError.prototype.name = Base.name;
|
|
908
|
+
NodeError.prototype.code = code;
|
|
909
|
+
|
|
910
|
+
codes[code] = NodeError;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
// https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js
|
|
914
|
+
function oneOf(expected, thing) {
|
|
915
|
+
if (Array.isArray(expected)) {
|
|
916
|
+
const len = expected.length;
|
|
917
|
+
expected = expected.map((i) => String(i));
|
|
918
|
+
if (len > 2) {
|
|
919
|
+
return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` +
|
|
920
|
+
expected[len - 1];
|
|
921
|
+
} else if (len === 2) {
|
|
922
|
+
return `one of ${thing} ${expected[0]} or ${expected[1]}`;
|
|
923
|
+
} else {
|
|
924
|
+
return `of ${thing} ${expected[0]}`;
|
|
925
|
+
}
|
|
926
|
+
} else {
|
|
927
|
+
return `of ${thing} ${String(expected)}`;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
|
|
932
|
+
function startsWith(str, search, pos) {
|
|
933
|
+
return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
|
|
937
|
+
function endsWith(str, search, this_len) {
|
|
938
|
+
if (this_len === undefined || this_len > str.length) {
|
|
939
|
+
this_len = str.length;
|
|
940
|
+
}
|
|
941
|
+
return str.substring(this_len - search.length, this_len) === search;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
|
|
945
|
+
function includes(str, search, start) {
|
|
946
|
+
if (typeof start !== 'number') {
|
|
947
|
+
start = 0;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
if (start + search.length > str.length) {
|
|
951
|
+
return false;
|
|
952
|
+
} else {
|
|
953
|
+
return str.indexOf(search, start) !== -1;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) {
|
|
958
|
+
return 'The value "' + value + '" is invalid for option "' + name + '"'
|
|
959
|
+
}, TypeError);
|
|
960
|
+
createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {
|
|
961
|
+
// determiner: 'must be' or 'must not be'
|
|
962
|
+
let determiner;
|
|
963
|
+
if (typeof expected === 'string' && startsWith(expected, 'not ')) {
|
|
964
|
+
determiner = 'must not be';
|
|
965
|
+
expected = expected.replace(/^not /, '');
|
|
966
|
+
} else {
|
|
967
|
+
determiner = 'must be';
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
let msg;
|
|
971
|
+
if (endsWith(name, ' argument')) {
|
|
972
|
+
// For cases like 'first argument'
|
|
973
|
+
msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
|
|
974
|
+
} else {
|
|
975
|
+
const type = includes(name, '.') ? 'property' : 'argument';
|
|
976
|
+
msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
msg += `. Received type ${typeof actual}`;
|
|
980
|
+
return msg;
|
|
981
|
+
}, TypeError);
|
|
982
|
+
createErrorType('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');
|
|
983
|
+
createErrorType('ERR_METHOD_NOT_IMPLEMENTED', function (name) {
|
|
984
|
+
return 'The ' + name + ' method is not implemented'
|
|
985
|
+
});
|
|
986
|
+
createErrorType('ERR_STREAM_PREMATURE_CLOSE', 'Premature close');
|
|
987
|
+
createErrorType('ERR_STREAM_DESTROYED', function (name) {
|
|
988
|
+
return 'Cannot call ' + name + ' after a stream was destroyed';
|
|
989
|
+
});
|
|
990
|
+
createErrorType('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
|
|
991
|
+
createErrorType('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable');
|
|
992
|
+
createErrorType('ERR_STREAM_WRITE_AFTER_END', 'write after end');
|
|
993
|
+
createErrorType('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
|
|
994
|
+
createErrorType('ERR_UNKNOWN_ENCODING', function (arg) {
|
|
995
|
+
return 'Unknown encoding: ' + arg
|
|
996
|
+
}, TypeError);
|
|
997
|
+
createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
|
|
998
|
+
|
|
999
|
+
var codes_1 = codes;
|
|
1000
|
+
|
|
1001
|
+
var errors = {
|
|
1002
|
+
codes: codes_1
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
var ERR_INVALID_OPT_VALUE = errors.codes.ERR_INVALID_OPT_VALUE;
|
|
1006
|
+
|
|
1007
|
+
function highWaterMarkFrom(options, isDuplex, duplexKey) {
|
|
1008
|
+
return options.highWaterMark != null ? options.highWaterMark : isDuplex ? options[duplexKey] : null;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
function getHighWaterMark$2(state, options, duplexKey, isDuplex) {
|
|
1012
|
+
var hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
|
|
1013
|
+
|
|
1014
|
+
if (hwm != null) {
|
|
1015
|
+
if (!(isFinite(hwm) && Math.floor(hwm) === hwm) || hwm < 0) {
|
|
1016
|
+
var name = isDuplex ? duplexKey : 'highWaterMark';
|
|
1017
|
+
throw new ERR_INVALID_OPT_VALUE(name, hwm);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
return Math.floor(hwm);
|
|
1021
|
+
} // Default value
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
return state.objectMode ? 16 : 16 * 1024;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
var state = {
|
|
1028
|
+
getHighWaterMark: getHighWaterMark$2
|
|
1029
|
+
};
|
|
1030
|
+
|
|
1031
|
+
var inherits_browser = createCommonjsModule(function (module) {
|
|
1032
|
+
if (typeof Object.create === 'function') {
|
|
1033
|
+
// implementation from standard node.js 'util' module
|
|
1034
|
+
module.exports = function inherits(ctor, superCtor) {
|
|
1035
|
+
if (superCtor) {
|
|
1036
|
+
ctor.super_ = superCtor;
|
|
1037
|
+
ctor.prototype = Object.create(superCtor.prototype, {
|
|
1038
|
+
constructor: {
|
|
1039
|
+
value: ctor,
|
|
1040
|
+
enumerable: false,
|
|
1041
|
+
writable: true,
|
|
1042
|
+
configurable: true
|
|
1043
|
+
}
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
};
|
|
1047
|
+
} else {
|
|
1048
|
+
// old school shim for old browsers
|
|
1049
|
+
module.exports = function inherits(ctor, superCtor) {
|
|
1050
|
+
if (superCtor) {
|
|
1051
|
+
ctor.super_ = superCtor;
|
|
1052
|
+
var TempCtor = function () {};
|
|
1053
|
+
TempCtor.prototype = superCtor.prototype;
|
|
1054
|
+
ctor.prototype = new TempCtor();
|
|
1055
|
+
ctor.prototype.constructor = ctor;
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
}
|
|
1059
|
+
});
|
|
1060
|
+
|
|
1061
|
+
var inherits = createCommonjsModule(function (module) {
|
|
1062
|
+
try {
|
|
1063
|
+
var util$1 = util;
|
|
1064
|
+
/* istanbul ignore next */
|
|
1065
|
+
if (typeof util$1.inherits !== 'function') throw '';
|
|
1066
|
+
module.exports = util$1.inherits;
|
|
1067
|
+
} catch (e) {
|
|
1068
|
+
/* istanbul ignore next */
|
|
1069
|
+
module.exports = inherits_browser;
|
|
1070
|
+
}
|
|
1071
|
+
});
|
|
1072
|
+
|
|
1073
|
+
/**
|
|
1074
|
+
* For Node.js, simply re-export the core `util.deprecate` function.
|
|
1075
|
+
*/
|
|
1076
|
+
|
|
1077
|
+
var node = util.deprecate;
|
|
1078
|
+
|
|
1079
|
+
var require$$2 = _stream_duplex;
|
|
1080
|
+
|
|
1081
|
+
var _stream_writable = Writable;
|
|
1082
|
+
// there will be only 2 of these for each stream
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
function CorkedRequest(state) {
|
|
1086
|
+
var _this = this;
|
|
1087
|
+
|
|
1088
|
+
this.next = null;
|
|
1089
|
+
this.entry = null;
|
|
1090
|
+
|
|
1091
|
+
this.finish = function () {
|
|
1092
|
+
onCorkedFinish(_this, state);
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
/* </replacement> */
|
|
1096
|
+
|
|
1097
|
+
/*<replacement>*/
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
var Duplex$2;
|
|
1101
|
+
/*</replacement>*/
|
|
1102
|
+
|
|
1103
|
+
Writable.WritableState = WritableState;
|
|
1104
|
+
/*<replacement>*/
|
|
1105
|
+
|
|
1106
|
+
var internalUtil = {
|
|
1107
|
+
deprecate: node
|
|
1108
|
+
};
|
|
1109
|
+
/*</replacement>*/
|
|
1110
|
+
|
|
1111
|
+
/*<replacement>*/
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
/*</replacement>*/
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
var Buffer$3 = buffer.Buffer;
|
|
1118
|
+
|
|
1119
|
+
var OurUint8Array$1 = commonjsGlobal.Uint8Array || function () {};
|
|
1120
|
+
|
|
1121
|
+
function _uint8ArrayToBuffer$1(chunk) {
|
|
1122
|
+
return Buffer$3.from(chunk);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
function _isUint8Array$1(obj) {
|
|
1126
|
+
return Buffer$3.isBuffer(obj) || obj instanceof OurUint8Array$1;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
var getHighWaterMark$1 = state.getHighWaterMark;
|
|
1132
|
+
|
|
1133
|
+
var _require$codes$3 = errors.codes,
|
|
1134
|
+
ERR_INVALID_ARG_TYPE$2 = _require$codes$3.ERR_INVALID_ARG_TYPE,
|
|
1135
|
+
ERR_METHOD_NOT_IMPLEMENTED$2 = _require$codes$3.ERR_METHOD_NOT_IMPLEMENTED,
|
|
1136
|
+
ERR_MULTIPLE_CALLBACK$1 = _require$codes$3.ERR_MULTIPLE_CALLBACK,
|
|
1137
|
+
ERR_STREAM_CANNOT_PIPE = _require$codes$3.ERR_STREAM_CANNOT_PIPE,
|
|
1138
|
+
ERR_STREAM_DESTROYED$1 = _require$codes$3.ERR_STREAM_DESTROYED,
|
|
1139
|
+
ERR_STREAM_NULL_VALUES = _require$codes$3.ERR_STREAM_NULL_VALUES,
|
|
1140
|
+
ERR_STREAM_WRITE_AFTER_END = _require$codes$3.ERR_STREAM_WRITE_AFTER_END,
|
|
1141
|
+
ERR_UNKNOWN_ENCODING = _require$codes$3.ERR_UNKNOWN_ENCODING;
|
|
1142
|
+
|
|
1143
|
+
var errorOrDestroy$1 = destroy_1.errorOrDestroy;
|
|
1144
|
+
|
|
1145
|
+
inherits(Writable, stream);
|
|
1146
|
+
|
|
1147
|
+
function nop() {}
|
|
1148
|
+
|
|
1149
|
+
function WritableState(options, stream, isDuplex) {
|
|
1150
|
+
Duplex$2 = Duplex$2 || require$$2;
|
|
1151
|
+
options = options || {}; // Duplex streams are both readable and writable, but share
|
|
1152
|
+
// the same options object.
|
|
1153
|
+
// However, some cases require setting options to different
|
|
1154
|
+
// values for the readable and the writable sides of the duplex stream,
|
|
1155
|
+
// e.g. options.readableObjectMode vs. options.writableObjectMode, etc.
|
|
1156
|
+
|
|
1157
|
+
if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex$2; // object stream flag to indicate whether or not this stream
|
|
1158
|
+
// contains buffers or objects.
|
|
1159
|
+
|
|
1160
|
+
this.objectMode = !!options.objectMode;
|
|
1161
|
+
if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; // the point at which write() starts returning false
|
|
1162
|
+
// Note: 0 is a valid value, means that we always return false if
|
|
1163
|
+
// the entire buffer is not flushed immediately on write()
|
|
1164
|
+
|
|
1165
|
+
this.highWaterMark = getHighWaterMark$1(this, options, 'writableHighWaterMark', isDuplex); // if _final has been called
|
|
1166
|
+
|
|
1167
|
+
this.finalCalled = false; // drain event flag.
|
|
1168
|
+
|
|
1169
|
+
this.needDrain = false; // at the start of calling end()
|
|
1170
|
+
|
|
1171
|
+
this.ending = false; // when end() has been called, and returned
|
|
1172
|
+
|
|
1173
|
+
this.ended = false; // when 'finish' is emitted
|
|
1174
|
+
|
|
1175
|
+
this.finished = false; // has it been destroyed
|
|
1176
|
+
|
|
1177
|
+
this.destroyed = false; // should we decode strings into buffers before passing to _write?
|
|
1178
|
+
// this is here so that some node-core streams can optimize string
|
|
1179
|
+
// handling at a lower level.
|
|
1180
|
+
|
|
1181
|
+
var noDecode = options.decodeStrings === false;
|
|
1182
|
+
this.decodeStrings = !noDecode; // Crypto is kind of old and crusty. Historically, its default string
|
|
1183
|
+
// encoding is 'binary' so we have to make this configurable.
|
|
1184
|
+
// Everything else in the universe uses 'utf8', though.
|
|
1185
|
+
|
|
1186
|
+
this.defaultEncoding = options.defaultEncoding || 'utf8'; // not an actual buffer we keep track of, but a measurement
|
|
1187
|
+
// of how much we're waiting to get pushed to some underlying
|
|
1188
|
+
// socket or file.
|
|
1189
|
+
|
|
1190
|
+
this.length = 0; // a flag to see when we're in the middle of a write.
|
|
1191
|
+
|
|
1192
|
+
this.writing = false; // when true all writes will be buffered until .uncork() call
|
|
1193
|
+
|
|
1194
|
+
this.corked = 0; // a flag to be able to tell if the onwrite cb is called immediately,
|
|
1195
|
+
// or on a later tick. We set this to true at first, because any
|
|
1196
|
+
// actions that shouldn't happen until "later" should generally also
|
|
1197
|
+
// not happen before the first write call.
|
|
1198
|
+
|
|
1199
|
+
this.sync = true; // a flag to know if we're processing previously buffered items, which
|
|
1200
|
+
// may call the _write() callback in the same tick, so that we don't
|
|
1201
|
+
// end up in an overlapped onwrite situation.
|
|
1202
|
+
|
|
1203
|
+
this.bufferProcessing = false; // the callback that's passed to _write(chunk,cb)
|
|
1204
|
+
|
|
1205
|
+
this.onwrite = function (er) {
|
|
1206
|
+
onwrite(stream, er);
|
|
1207
|
+
}; // the callback that the user supplies to write(chunk,encoding,cb)
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
this.writecb = null; // the amount that is being written when _write is called.
|
|
1211
|
+
|
|
1212
|
+
this.writelen = 0;
|
|
1213
|
+
this.bufferedRequest = null;
|
|
1214
|
+
this.lastBufferedRequest = null; // number of pending user-supplied write callbacks
|
|
1215
|
+
// this must be 0 before 'finish' can be emitted
|
|
1216
|
+
|
|
1217
|
+
this.pendingcb = 0; // emit prefinish if the only thing we're waiting for is _write cbs
|
|
1218
|
+
// This is relevant for synchronous Transform streams
|
|
1219
|
+
|
|
1220
|
+
this.prefinished = false; // True if the error was already emitted and should not be thrown again
|
|
1221
|
+
|
|
1222
|
+
this.errorEmitted = false; // Should close be emitted on destroy. Defaults to true.
|
|
1223
|
+
|
|
1224
|
+
this.emitClose = options.emitClose !== false; // Should .destroy() be called after 'finish' (and potentially 'end')
|
|
1225
|
+
|
|
1226
|
+
this.autoDestroy = !!options.autoDestroy; // count buffered requests
|
|
1227
|
+
|
|
1228
|
+
this.bufferedRequestCount = 0; // allocate the first CorkedRequest, there is always
|
|
1229
|
+
// one allocated and free to use, and we maintain at most two
|
|
1230
|
+
|
|
1231
|
+
this.corkedRequestsFree = new CorkedRequest(this);
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
WritableState.prototype.getBuffer = function getBuffer() {
|
|
1235
|
+
var current = this.bufferedRequest;
|
|
1236
|
+
var out = [];
|
|
1237
|
+
|
|
1238
|
+
while (current) {
|
|
1239
|
+
out.push(current);
|
|
1240
|
+
current = current.next;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
return out;
|
|
1244
|
+
};
|
|
1245
|
+
|
|
1246
|
+
(function () {
|
|
1247
|
+
try {
|
|
1248
|
+
Object.defineProperty(WritableState.prototype, 'buffer', {
|
|
1249
|
+
get: internalUtil.deprecate(function writableStateBufferGetter() {
|
|
1250
|
+
return this.getBuffer();
|
|
1251
|
+
}, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003')
|
|
1252
|
+
});
|
|
1253
|
+
} catch (_) {}
|
|
1254
|
+
})(); // Test _writableState for inheritance to account for Duplex streams,
|
|
1255
|
+
// whose prototype chain only points to Readable.
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
var realHasInstance;
|
|
1259
|
+
|
|
1260
|
+
if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') {
|
|
1261
|
+
realHasInstance = Function.prototype[Symbol.hasInstance];
|
|
1262
|
+
Object.defineProperty(Writable, Symbol.hasInstance, {
|
|
1263
|
+
value: function value(object) {
|
|
1264
|
+
if (realHasInstance.call(this, object)) return true;
|
|
1265
|
+
if (this !== Writable) return false;
|
|
1266
|
+
return object && object._writableState instanceof WritableState;
|
|
1267
|
+
}
|
|
1268
|
+
});
|
|
1269
|
+
} else {
|
|
1270
|
+
realHasInstance = function realHasInstance(object) {
|
|
1271
|
+
return object instanceof this;
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
function Writable(options) {
|
|
1276
|
+
Duplex$2 = Duplex$2 || require$$2; // Writable ctor is applied to Duplexes, too.
|
|
1277
|
+
// `realHasInstance` is necessary because using plain `instanceof`
|
|
1278
|
+
// would return false, as no `_writableState` property is attached.
|
|
1279
|
+
// Trying to use the custom `instanceof` for Writable here will also break the
|
|
1280
|
+
// Node.js LazyTransform implementation, which has a non-trivial getter for
|
|
1281
|
+
// `_writableState` that would lead to infinite recursion.
|
|
1282
|
+
// Checking for a Stream.Duplex instance is faster here instead of inside
|
|
1283
|
+
// the WritableState constructor, at least with V8 6.5
|
|
1284
|
+
|
|
1285
|
+
var isDuplex = this instanceof Duplex$2;
|
|
1286
|
+
if (!isDuplex && !realHasInstance.call(Writable, this)) return new Writable(options);
|
|
1287
|
+
this._writableState = new WritableState(options, this, isDuplex); // legacy.
|
|
1288
|
+
|
|
1289
|
+
this.writable = true;
|
|
1290
|
+
|
|
1291
|
+
if (options) {
|
|
1292
|
+
if (typeof options.write === 'function') this._write = options.write;
|
|
1293
|
+
if (typeof options.writev === 'function') this._writev = options.writev;
|
|
1294
|
+
if (typeof options.destroy === 'function') this._destroy = options.destroy;
|
|
1295
|
+
if (typeof options.final === 'function') this._final = options.final;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
stream.call(this);
|
|
1299
|
+
} // Otherwise people can pipe Writable streams, which is just wrong.
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
Writable.prototype.pipe = function () {
|
|
1303
|
+
errorOrDestroy$1(this, new ERR_STREAM_CANNOT_PIPE());
|
|
1304
|
+
};
|
|
1305
|
+
|
|
1306
|
+
function writeAfterEnd(stream, cb) {
|
|
1307
|
+
var er = new ERR_STREAM_WRITE_AFTER_END(); // TODO: defer error events consistently everywhere, not just the cb
|
|
1308
|
+
|
|
1309
|
+
errorOrDestroy$1(stream, er);
|
|
1310
|
+
process.nextTick(cb, er);
|
|
1311
|
+
} // Checks that a user-supplied chunk is valid, especially for the particular
|
|
1312
|
+
// mode the stream is in. Currently this means that `null` is never accepted
|
|
1313
|
+
// and undefined/non-string values are only allowed in object mode.
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
function validChunk(stream, state, chunk, cb) {
|
|
1317
|
+
var er;
|
|
1318
|
+
|
|
1319
|
+
if (chunk === null) {
|
|
1320
|
+
er = new ERR_STREAM_NULL_VALUES();
|
|
1321
|
+
} else if (typeof chunk !== 'string' && !state.objectMode) {
|
|
1322
|
+
er = new ERR_INVALID_ARG_TYPE$2('chunk', ['string', 'Buffer'], chunk);
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
if (er) {
|
|
1326
|
+
errorOrDestroy$1(stream, er);
|
|
1327
|
+
process.nextTick(cb, er);
|
|
1328
|
+
return false;
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
return true;
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
Writable.prototype.write = function (chunk, encoding, cb) {
|
|
1335
|
+
var state = this._writableState;
|
|
1336
|
+
var ret = false;
|
|
1337
|
+
|
|
1338
|
+
var isBuf = !state.objectMode && _isUint8Array$1(chunk);
|
|
1339
|
+
|
|
1340
|
+
if (isBuf && !Buffer$3.isBuffer(chunk)) {
|
|
1341
|
+
chunk = _uint8ArrayToBuffer$1(chunk);
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
if (typeof encoding === 'function') {
|
|
1345
|
+
cb = encoding;
|
|
1346
|
+
encoding = null;
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding;
|
|
1350
|
+
if (typeof cb !== 'function') cb = nop;
|
|
1351
|
+
if (state.ending) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) {
|
|
1352
|
+
state.pendingcb++;
|
|
1353
|
+
ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb);
|
|
1354
|
+
}
|
|
1355
|
+
return ret;
|
|
1356
|
+
};
|
|
1357
|
+
|
|
1358
|
+
Writable.prototype.cork = function () {
|
|
1359
|
+
this._writableState.corked++;
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
Writable.prototype.uncork = function () {
|
|
1363
|
+
var state = this._writableState;
|
|
1364
|
+
|
|
1365
|
+
if (state.corked) {
|
|
1366
|
+
state.corked--;
|
|
1367
|
+
if (!state.writing && !state.corked && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state);
|
|
1368
|
+
}
|
|
1369
|
+
};
|
|
1370
|
+
|
|
1371
|
+
Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
|
|
1372
|
+
// node::ParseEncoding() requires lower case.
|
|
1373
|
+
if (typeof encoding === 'string') encoding = encoding.toLowerCase();
|
|
1374
|
+
if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new ERR_UNKNOWN_ENCODING(encoding);
|
|
1375
|
+
this._writableState.defaultEncoding = encoding;
|
|
1376
|
+
return this;
|
|
1377
|
+
};
|
|
1378
|
+
|
|
1379
|
+
Object.defineProperty(Writable.prototype, 'writableBuffer', {
|
|
1380
|
+
// making it explicit this property is not enumerable
|
|
1381
|
+
// because otherwise some prototype manipulation in
|
|
1382
|
+
// userland will fail
|
|
1383
|
+
enumerable: false,
|
|
1384
|
+
get: function get() {
|
|
1385
|
+
return this._writableState && this._writableState.getBuffer();
|
|
1386
|
+
}
|
|
1387
|
+
});
|
|
1388
|
+
|
|
1389
|
+
function decodeChunk(state, chunk, encoding) {
|
|
1390
|
+
if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') {
|
|
1391
|
+
chunk = Buffer$3.from(chunk, encoding);
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
return chunk;
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
|
|
1398
|
+
// making it explicit this property is not enumerable
|
|
1399
|
+
// because otherwise some prototype manipulation in
|
|
1400
|
+
// userland will fail
|
|
1401
|
+
enumerable: false,
|
|
1402
|
+
get: function get() {
|
|
1403
|
+
return this._writableState.highWaterMark;
|
|
1404
|
+
}
|
|
1405
|
+
}); // if we're already writing something, then just put this
|
|
1406
|
+
// in the queue, and wait our turn. Otherwise, call _write
|
|
1407
|
+
// If we return false, then we need a drain event, so set that flag.
|
|
1408
|
+
|
|
1409
|
+
function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
|
|
1410
|
+
if (!isBuf) {
|
|
1411
|
+
var newChunk = decodeChunk(state, chunk, encoding);
|
|
1412
|
+
|
|
1413
|
+
if (chunk !== newChunk) {
|
|
1414
|
+
isBuf = true;
|
|
1415
|
+
encoding = 'buffer';
|
|
1416
|
+
chunk = newChunk;
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
var len = state.objectMode ? 1 : chunk.length;
|
|
1421
|
+
state.length += len;
|
|
1422
|
+
var ret = state.length < state.highWaterMark; // we must ensure that previous needDrain will not be reset to false.
|
|
1423
|
+
|
|
1424
|
+
if (!ret) state.needDrain = true;
|
|
1425
|
+
|
|
1426
|
+
if (state.writing || state.corked) {
|
|
1427
|
+
var last = state.lastBufferedRequest;
|
|
1428
|
+
state.lastBufferedRequest = {
|
|
1429
|
+
chunk: chunk,
|
|
1430
|
+
encoding: encoding,
|
|
1431
|
+
isBuf: isBuf,
|
|
1432
|
+
callback: cb,
|
|
1433
|
+
next: null
|
|
1434
|
+
};
|
|
1435
|
+
|
|
1436
|
+
if (last) {
|
|
1437
|
+
last.next = state.lastBufferedRequest;
|
|
1438
|
+
} else {
|
|
1439
|
+
state.bufferedRequest = state.lastBufferedRequest;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
state.bufferedRequestCount += 1;
|
|
1443
|
+
} else {
|
|
1444
|
+
doWrite(stream, state, false, len, chunk, encoding, cb);
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
return ret;
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
function doWrite(stream, state, writev, len, chunk, encoding, cb) {
|
|
1451
|
+
state.writelen = len;
|
|
1452
|
+
state.writecb = cb;
|
|
1453
|
+
state.writing = true;
|
|
1454
|
+
state.sync = true;
|
|
1455
|
+
if (state.destroyed) state.onwrite(new ERR_STREAM_DESTROYED$1('write'));else if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite);
|
|
1456
|
+
state.sync = false;
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
function onwriteError(stream, state, sync, er, cb) {
|
|
1460
|
+
--state.pendingcb;
|
|
1461
|
+
|
|
1462
|
+
if (sync) {
|
|
1463
|
+
// defer the callback if we are being called synchronously
|
|
1464
|
+
// to avoid piling up things on the stack
|
|
1465
|
+
process.nextTick(cb, er); // this can emit finish, and it will always happen
|
|
1466
|
+
// after error
|
|
1467
|
+
|
|
1468
|
+
process.nextTick(finishMaybe, stream, state);
|
|
1469
|
+
stream._writableState.errorEmitted = true;
|
|
1470
|
+
errorOrDestroy$1(stream, er);
|
|
1471
|
+
} else {
|
|
1472
|
+
// the caller expect this to happen before if
|
|
1473
|
+
// it is async
|
|
1474
|
+
cb(er);
|
|
1475
|
+
stream._writableState.errorEmitted = true;
|
|
1476
|
+
errorOrDestroy$1(stream, er); // this can emit finish, but finish must
|
|
1477
|
+
// always follow error
|
|
1478
|
+
|
|
1479
|
+
finishMaybe(stream, state);
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
function onwriteStateUpdate(state) {
|
|
1484
|
+
state.writing = false;
|
|
1485
|
+
state.writecb = null;
|
|
1486
|
+
state.length -= state.writelen;
|
|
1487
|
+
state.writelen = 0;
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
function onwrite(stream, er) {
|
|
1491
|
+
var state = stream._writableState;
|
|
1492
|
+
var sync = state.sync;
|
|
1493
|
+
var cb = state.writecb;
|
|
1494
|
+
if (typeof cb !== 'function') throw new ERR_MULTIPLE_CALLBACK$1();
|
|
1495
|
+
onwriteStateUpdate(state);
|
|
1496
|
+
if (er) onwriteError(stream, state, sync, er, cb);else {
|
|
1497
|
+
// Check if we're actually ready to finish, but don't emit yet
|
|
1498
|
+
var finished = needFinish(state) || stream.destroyed;
|
|
1499
|
+
|
|
1500
|
+
if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) {
|
|
1501
|
+
clearBuffer(stream, state);
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
if (sync) {
|
|
1505
|
+
process.nextTick(afterWrite, stream, state, finished, cb);
|
|
1506
|
+
} else {
|
|
1507
|
+
afterWrite(stream, state, finished, cb);
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
function afterWrite(stream, state, finished, cb) {
|
|
1513
|
+
if (!finished) onwriteDrain(stream, state);
|
|
1514
|
+
state.pendingcb--;
|
|
1515
|
+
cb();
|
|
1516
|
+
finishMaybe(stream, state);
|
|
1517
|
+
} // Must force callback to be called on nextTick, so that we don't
|
|
1518
|
+
// emit 'drain' before the write() consumer gets the 'false' return
|
|
1519
|
+
// value, and has a chance to attach a 'drain' listener.
|
|
1520
|
+
|
|
1521
|
+
|
|
1522
|
+
function onwriteDrain(stream, state) {
|
|
1523
|
+
if (state.length === 0 && state.needDrain) {
|
|
1524
|
+
state.needDrain = false;
|
|
1525
|
+
stream.emit('drain');
|
|
1526
|
+
}
|
|
1527
|
+
} // if there's something in the buffer waiting, then process it
|
|
1528
|
+
|
|
1529
|
+
|
|
1530
|
+
function clearBuffer(stream, state) {
|
|
1531
|
+
state.bufferProcessing = true;
|
|
1532
|
+
var entry = state.bufferedRequest;
|
|
1533
|
+
|
|
1534
|
+
if (stream._writev && entry && entry.next) {
|
|
1535
|
+
// Fast case, write everything using _writev()
|
|
1536
|
+
var l = state.bufferedRequestCount;
|
|
1537
|
+
var buffer = new Array(l);
|
|
1538
|
+
var holder = state.corkedRequestsFree;
|
|
1539
|
+
holder.entry = entry;
|
|
1540
|
+
var count = 0;
|
|
1541
|
+
var allBuffers = true;
|
|
1542
|
+
|
|
1543
|
+
while (entry) {
|
|
1544
|
+
buffer[count] = entry;
|
|
1545
|
+
if (!entry.isBuf) allBuffers = false;
|
|
1546
|
+
entry = entry.next;
|
|
1547
|
+
count += 1;
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
buffer.allBuffers = allBuffers;
|
|
1551
|
+
doWrite(stream, state, true, state.length, buffer, '', holder.finish); // doWrite is almost always async, defer these to save a bit of time
|
|
1552
|
+
// as the hot path ends with doWrite
|
|
1553
|
+
|
|
1554
|
+
state.pendingcb++;
|
|
1555
|
+
state.lastBufferedRequest = null;
|
|
1556
|
+
|
|
1557
|
+
if (holder.next) {
|
|
1558
|
+
state.corkedRequestsFree = holder.next;
|
|
1559
|
+
holder.next = null;
|
|
1560
|
+
} else {
|
|
1561
|
+
state.corkedRequestsFree = new CorkedRequest(state);
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
state.bufferedRequestCount = 0;
|
|
1565
|
+
} else {
|
|
1566
|
+
// Slow case, write chunks one-by-one
|
|
1567
|
+
while (entry) {
|
|
1568
|
+
var chunk = entry.chunk;
|
|
1569
|
+
var encoding = entry.encoding;
|
|
1570
|
+
var cb = entry.callback;
|
|
1571
|
+
var len = state.objectMode ? 1 : chunk.length;
|
|
1572
|
+
doWrite(stream, state, false, len, chunk, encoding, cb);
|
|
1573
|
+
entry = entry.next;
|
|
1574
|
+
state.bufferedRequestCount--; // if we didn't call the onwrite immediately, then
|
|
1575
|
+
// it means that we need to wait until it does.
|
|
1576
|
+
// also, that means that the chunk and cb are currently
|
|
1577
|
+
// being processed, so move the buffer counter past them.
|
|
1578
|
+
|
|
1579
|
+
if (state.writing) {
|
|
1580
|
+
break;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
if (entry === null) state.lastBufferedRequest = null;
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
state.bufferedRequest = entry;
|
|
1588
|
+
state.bufferProcessing = false;
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
Writable.prototype._write = function (chunk, encoding, cb) {
|
|
1592
|
+
cb(new ERR_METHOD_NOT_IMPLEMENTED$2('_write()'));
|
|
1593
|
+
};
|
|
1594
|
+
|
|
1595
|
+
Writable.prototype._writev = null;
|
|
1596
|
+
|
|
1597
|
+
Writable.prototype.end = function (chunk, encoding, cb) {
|
|
1598
|
+
var state = this._writableState;
|
|
1599
|
+
|
|
1600
|
+
if (typeof chunk === 'function') {
|
|
1601
|
+
cb = chunk;
|
|
1602
|
+
chunk = null;
|
|
1603
|
+
encoding = null;
|
|
1604
|
+
} else if (typeof encoding === 'function') {
|
|
1605
|
+
cb = encoding;
|
|
1606
|
+
encoding = null;
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); // .end() fully uncorks
|
|
1610
|
+
|
|
1611
|
+
if (state.corked) {
|
|
1612
|
+
state.corked = 1;
|
|
1613
|
+
this.uncork();
|
|
1614
|
+
} // ignore unnecessary end() calls.
|
|
1615
|
+
|
|
1616
|
+
|
|
1617
|
+
if (!state.ending) endWritable(this, state, cb);
|
|
1618
|
+
return this;
|
|
1619
|
+
};
|
|
1620
|
+
|
|
1621
|
+
Object.defineProperty(Writable.prototype, 'writableLength', {
|
|
1622
|
+
// making it explicit this property is not enumerable
|
|
1623
|
+
// because otherwise some prototype manipulation in
|
|
1624
|
+
// userland will fail
|
|
1625
|
+
enumerable: false,
|
|
1626
|
+
get: function get() {
|
|
1627
|
+
return this._writableState.length;
|
|
1628
|
+
}
|
|
1629
|
+
});
|
|
1630
|
+
|
|
1631
|
+
function needFinish(state) {
|
|
1632
|
+
return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing;
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
function callFinal(stream, state) {
|
|
1636
|
+
stream._final(function (err) {
|
|
1637
|
+
state.pendingcb--;
|
|
1638
|
+
|
|
1639
|
+
if (err) {
|
|
1640
|
+
errorOrDestroy$1(stream, err);
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
state.prefinished = true;
|
|
1644
|
+
stream.emit('prefinish');
|
|
1645
|
+
finishMaybe(stream, state);
|
|
1646
|
+
});
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
function prefinish$1(stream, state) {
|
|
1650
|
+
if (!state.prefinished && !state.finalCalled) {
|
|
1651
|
+
if (typeof stream._final === 'function' && !state.destroyed) {
|
|
1652
|
+
state.pendingcb++;
|
|
1653
|
+
state.finalCalled = true;
|
|
1654
|
+
process.nextTick(callFinal, stream, state);
|
|
1655
|
+
} else {
|
|
1656
|
+
state.prefinished = true;
|
|
1657
|
+
stream.emit('prefinish');
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
function finishMaybe(stream, state) {
|
|
1663
|
+
var need = needFinish(state);
|
|
1664
|
+
|
|
1665
|
+
if (need) {
|
|
1666
|
+
prefinish$1(stream, state);
|
|
1667
|
+
|
|
1668
|
+
if (state.pendingcb === 0) {
|
|
1669
|
+
state.finished = true;
|
|
1670
|
+
stream.emit('finish');
|
|
1671
|
+
|
|
1672
|
+
if (state.autoDestroy) {
|
|
1673
|
+
// In case of duplex streams we need a way to detect
|
|
1674
|
+
// if the readable side is ready for autoDestroy as well
|
|
1675
|
+
var rState = stream._readableState;
|
|
1676
|
+
|
|
1677
|
+
if (!rState || rState.autoDestroy && rState.endEmitted) {
|
|
1678
|
+
stream.destroy();
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1684
|
+
return need;
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
function endWritable(stream, state, cb) {
|
|
1688
|
+
state.ending = true;
|
|
1689
|
+
finishMaybe(stream, state);
|
|
1690
|
+
|
|
1691
|
+
if (cb) {
|
|
1692
|
+
if (state.finished) process.nextTick(cb);else stream.once('finish', cb);
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
state.ended = true;
|
|
1696
|
+
stream.writable = false;
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
function onCorkedFinish(corkReq, state, err) {
|
|
1700
|
+
var entry = corkReq.entry;
|
|
1701
|
+
corkReq.entry = null;
|
|
1702
|
+
|
|
1703
|
+
while (entry) {
|
|
1704
|
+
var cb = entry.callback;
|
|
1705
|
+
state.pendingcb--;
|
|
1706
|
+
cb(err);
|
|
1707
|
+
entry = entry.next;
|
|
1708
|
+
} // reuse the free corkReq.
|
|
1709
|
+
|
|
1710
|
+
|
|
1711
|
+
state.corkedRequestsFree.next = corkReq;
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
Object.defineProperty(Writable.prototype, 'destroyed', {
|
|
1715
|
+
// making it explicit this property is not enumerable
|
|
1716
|
+
// because otherwise some prototype manipulation in
|
|
1717
|
+
// userland will fail
|
|
1718
|
+
enumerable: false,
|
|
1719
|
+
get: function get() {
|
|
1720
|
+
if (this._writableState === undefined) {
|
|
1721
|
+
return false;
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
return this._writableState.destroyed;
|
|
1725
|
+
},
|
|
1726
|
+
set: function set(value) {
|
|
1727
|
+
// we ignore the value if the stream
|
|
1728
|
+
// has not been initialized yet
|
|
1729
|
+
if (!this._writableState) {
|
|
1730
|
+
return;
|
|
1731
|
+
} // backward compatibility, the user is explicitly
|
|
1732
|
+
// managing destroyed
|
|
1733
|
+
|
|
1734
|
+
|
|
1735
|
+
this._writableState.destroyed = value;
|
|
1736
|
+
}
|
|
1737
|
+
});
|
|
1738
|
+
Writable.prototype.destroy = destroy_1.destroy;
|
|
1739
|
+
Writable.prototype._undestroy = destroy_1.undestroy;
|
|
1740
|
+
|
|
1741
|
+
Writable.prototype._destroy = function (err, cb) {
|
|
1742
|
+
cb(err);
|
|
1743
|
+
};
|
|
1744
|
+
|
|
1745
|
+
var require$$0 = _stream_readable;
|
|
1746
|
+
|
|
1747
|
+
/*<replacement>*/
|
|
1748
|
+
|
|
1749
|
+
var objectKeys = Object.keys || function (obj) {
|
|
1750
|
+
var keys = [];
|
|
1751
|
+
|
|
1752
|
+
for (var key in obj) {
|
|
1753
|
+
keys.push(key);
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
return keys;
|
|
1757
|
+
};
|
|
1758
|
+
/*</replacement>*/
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
var _stream_duplex = Duplex$1;
|
|
1762
|
+
|
|
1763
|
+
|
|
1764
|
+
|
|
1765
|
+
|
|
1766
|
+
|
|
1767
|
+
inherits(Duplex$1, require$$0);
|
|
1768
|
+
|
|
1769
|
+
{
|
|
1770
|
+
// Allow the keys array to be GC'ed.
|
|
1771
|
+
var keys = objectKeys(_stream_writable.prototype);
|
|
1772
|
+
|
|
1773
|
+
for (var v = 0; v < keys.length; v++) {
|
|
1774
|
+
var method = keys[v];
|
|
1775
|
+
if (!Duplex$1.prototype[method]) Duplex$1.prototype[method] = _stream_writable.prototype[method];
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
function Duplex$1(options) {
|
|
1780
|
+
if (!(this instanceof Duplex$1)) return new Duplex$1(options);
|
|
1781
|
+
require$$0.call(this, options);
|
|
1782
|
+
_stream_writable.call(this, options);
|
|
1783
|
+
this.allowHalfOpen = true;
|
|
1784
|
+
|
|
1785
|
+
if (options) {
|
|
1786
|
+
if (options.readable === false) this.readable = false;
|
|
1787
|
+
if (options.writable === false) this.writable = false;
|
|
1788
|
+
|
|
1789
|
+
if (options.allowHalfOpen === false) {
|
|
1790
|
+
this.allowHalfOpen = false;
|
|
1791
|
+
this.once('end', onend);
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
Object.defineProperty(Duplex$1.prototype, 'writableHighWaterMark', {
|
|
1797
|
+
// making it explicit this property is not enumerable
|
|
1798
|
+
// because otherwise some prototype manipulation in
|
|
1799
|
+
// userland will fail
|
|
1800
|
+
enumerable: false,
|
|
1801
|
+
get: function get() {
|
|
1802
|
+
return this._writableState.highWaterMark;
|
|
1803
|
+
}
|
|
1804
|
+
});
|
|
1805
|
+
Object.defineProperty(Duplex$1.prototype, 'writableBuffer', {
|
|
1806
|
+
// making it explicit this property is not enumerable
|
|
1807
|
+
// because otherwise some prototype manipulation in
|
|
1808
|
+
// userland will fail
|
|
1809
|
+
enumerable: false,
|
|
1810
|
+
get: function get() {
|
|
1811
|
+
return this._writableState && this._writableState.getBuffer();
|
|
1812
|
+
}
|
|
1813
|
+
});
|
|
1814
|
+
Object.defineProperty(Duplex$1.prototype, 'writableLength', {
|
|
1815
|
+
// making it explicit this property is not enumerable
|
|
1816
|
+
// because otherwise some prototype manipulation in
|
|
1817
|
+
// userland will fail
|
|
1818
|
+
enumerable: false,
|
|
1819
|
+
get: function get() {
|
|
1820
|
+
return this._writableState.length;
|
|
1821
|
+
}
|
|
1822
|
+
}); // the no-half-open enforcer
|
|
1823
|
+
|
|
1824
|
+
function onend() {
|
|
1825
|
+
// If the writable side ended, then we're ok.
|
|
1826
|
+
if (this._writableState.ended) return; // no more data can be written.
|
|
1827
|
+
// But allow more writes to happen in this tick.
|
|
1828
|
+
|
|
1829
|
+
process.nextTick(onEndNT, this);
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
function onEndNT(self) {
|
|
1833
|
+
self.end();
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
Object.defineProperty(Duplex$1.prototype, 'destroyed', {
|
|
1837
|
+
// making it explicit this property is not enumerable
|
|
1838
|
+
// because otherwise some prototype manipulation in
|
|
1839
|
+
// userland will fail
|
|
1840
|
+
enumerable: false,
|
|
1841
|
+
get: function get() {
|
|
1842
|
+
if (this._readableState === undefined || this._writableState === undefined) {
|
|
1843
|
+
return false;
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
return this._readableState.destroyed && this._writableState.destroyed;
|
|
1847
|
+
},
|
|
1848
|
+
set: function set(value) {
|
|
1849
|
+
// we ignore the value if the stream
|
|
1850
|
+
// has not been initialized yet
|
|
1851
|
+
if (this._readableState === undefined || this._writableState === undefined) {
|
|
1852
|
+
return;
|
|
1853
|
+
} // backward compatibility, the user is explicitly
|
|
1854
|
+
// managing destroyed
|
|
1855
|
+
|
|
1856
|
+
|
|
1857
|
+
this._readableState.destroyed = value;
|
|
1858
|
+
this._writableState.destroyed = value;
|
|
1859
|
+
}
|
|
1860
|
+
});
|
|
1861
|
+
|
|
1862
|
+
var safeBuffer = createCommonjsModule(function (module, exports) {
|
|
1863
|
+
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
1864
|
+
/* eslint-disable node/no-deprecated-api */
|
|
1865
|
+
|
|
1866
|
+
var Buffer = buffer.Buffer;
|
|
1867
|
+
|
|
1868
|
+
// alternative to using Object.keys for old browsers
|
|
1869
|
+
function copyProps (src, dst) {
|
|
1870
|
+
for (var key in src) {
|
|
1871
|
+
dst[key] = src[key];
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
|
|
1875
|
+
module.exports = buffer;
|
|
1876
|
+
} else {
|
|
1877
|
+
// Copy properties from require('buffer')
|
|
1878
|
+
copyProps(buffer, exports);
|
|
1879
|
+
exports.Buffer = SafeBuffer;
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
function SafeBuffer (arg, encodingOrOffset, length) {
|
|
1883
|
+
return Buffer(arg, encodingOrOffset, length)
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
SafeBuffer.prototype = Object.create(Buffer.prototype);
|
|
1887
|
+
|
|
1888
|
+
// Copy static methods from Buffer
|
|
1889
|
+
copyProps(Buffer, SafeBuffer);
|
|
1890
|
+
|
|
1891
|
+
SafeBuffer.from = function (arg, encodingOrOffset, length) {
|
|
1892
|
+
if (typeof arg === 'number') {
|
|
1893
|
+
throw new TypeError('Argument must not be a number')
|
|
1894
|
+
}
|
|
1895
|
+
return Buffer(arg, encodingOrOffset, length)
|
|
1896
|
+
};
|
|
1897
|
+
|
|
1898
|
+
SafeBuffer.alloc = function (size, fill, encoding) {
|
|
1899
|
+
if (typeof size !== 'number') {
|
|
1900
|
+
throw new TypeError('Argument must be a number')
|
|
1901
|
+
}
|
|
1902
|
+
var buf = Buffer(size);
|
|
1903
|
+
if (fill !== undefined) {
|
|
1904
|
+
if (typeof encoding === 'string') {
|
|
1905
|
+
buf.fill(fill, encoding);
|
|
1906
|
+
} else {
|
|
1907
|
+
buf.fill(fill);
|
|
1908
|
+
}
|
|
1909
|
+
} else {
|
|
1910
|
+
buf.fill(0);
|
|
1911
|
+
}
|
|
1912
|
+
return buf
|
|
1913
|
+
};
|
|
1914
|
+
|
|
1915
|
+
SafeBuffer.allocUnsafe = function (size) {
|
|
1916
|
+
if (typeof size !== 'number') {
|
|
1917
|
+
throw new TypeError('Argument must be a number')
|
|
1918
|
+
}
|
|
1919
|
+
return Buffer(size)
|
|
1920
|
+
};
|
|
1921
|
+
|
|
1922
|
+
SafeBuffer.allocUnsafeSlow = function (size) {
|
|
1923
|
+
if (typeof size !== 'number') {
|
|
1924
|
+
throw new TypeError('Argument must be a number')
|
|
1925
|
+
}
|
|
1926
|
+
return buffer.SlowBuffer(size)
|
|
1927
|
+
};
|
|
1928
|
+
});
|
|
1929
|
+
safeBuffer.Buffer;
|
|
1930
|
+
|
|
1931
|
+
/*<replacement>*/
|
|
1932
|
+
|
|
1933
|
+
var Buffer$2 = safeBuffer.Buffer;
|
|
1934
|
+
/*</replacement>*/
|
|
1935
|
+
|
|
1936
|
+
var isEncoding = Buffer$2.isEncoding || function (encoding) {
|
|
1937
|
+
encoding = '' + encoding;
|
|
1938
|
+
switch (encoding && encoding.toLowerCase()) {
|
|
1939
|
+
case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw':
|
|
1940
|
+
return true;
|
|
1941
|
+
default:
|
|
1942
|
+
return false;
|
|
1943
|
+
}
|
|
1944
|
+
};
|
|
1945
|
+
|
|
1946
|
+
function _normalizeEncoding(enc) {
|
|
1947
|
+
if (!enc) return 'utf8';
|
|
1948
|
+
var retried;
|
|
1949
|
+
while (true) {
|
|
1950
|
+
switch (enc) {
|
|
1951
|
+
case 'utf8':
|
|
1952
|
+
case 'utf-8':
|
|
1953
|
+
return 'utf8';
|
|
1954
|
+
case 'ucs2':
|
|
1955
|
+
case 'ucs-2':
|
|
1956
|
+
case 'utf16le':
|
|
1957
|
+
case 'utf-16le':
|
|
1958
|
+
return 'utf16le';
|
|
1959
|
+
case 'latin1':
|
|
1960
|
+
case 'binary':
|
|
1961
|
+
return 'latin1';
|
|
1962
|
+
case 'base64':
|
|
1963
|
+
case 'ascii':
|
|
1964
|
+
case 'hex':
|
|
1965
|
+
return enc;
|
|
1966
|
+
default:
|
|
1967
|
+
if (retried) return; // undefined
|
|
1968
|
+
enc = ('' + enc).toLowerCase();
|
|
1969
|
+
retried = true;
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
// Do not cache `Buffer.isEncoding` when checking encoding names as some
|
|
1974
|
+
// modules monkey-patch it to support additional encodings
|
|
1975
|
+
function normalizeEncoding(enc) {
|
|
1976
|
+
var nenc = _normalizeEncoding(enc);
|
|
1977
|
+
if (typeof nenc !== 'string' && (Buffer$2.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc);
|
|
1978
|
+
return nenc || enc;
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
// StringDecoder provides an interface for efficiently splitting a series of
|
|
1982
|
+
// buffers into a series of JS strings without breaking apart multi-byte
|
|
1983
|
+
// characters.
|
|
1984
|
+
var StringDecoder_1 = StringDecoder$1;
|
|
1985
|
+
function StringDecoder$1(encoding) {
|
|
1986
|
+
this.encoding = normalizeEncoding(encoding);
|
|
1987
|
+
var nb;
|
|
1988
|
+
switch (this.encoding) {
|
|
1989
|
+
case 'utf16le':
|
|
1990
|
+
this.text = utf16Text;
|
|
1991
|
+
this.end = utf16End;
|
|
1992
|
+
nb = 4;
|
|
1993
|
+
break;
|
|
1994
|
+
case 'utf8':
|
|
1995
|
+
this.fillLast = utf8FillLast;
|
|
1996
|
+
nb = 4;
|
|
1997
|
+
break;
|
|
1998
|
+
case 'base64':
|
|
1999
|
+
this.text = base64Text;
|
|
2000
|
+
this.end = base64End;
|
|
2001
|
+
nb = 3;
|
|
2002
|
+
break;
|
|
2003
|
+
default:
|
|
2004
|
+
this.write = simpleWrite;
|
|
2005
|
+
this.end = simpleEnd;
|
|
2006
|
+
return;
|
|
2007
|
+
}
|
|
2008
|
+
this.lastNeed = 0;
|
|
2009
|
+
this.lastTotal = 0;
|
|
2010
|
+
this.lastChar = Buffer$2.allocUnsafe(nb);
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
StringDecoder$1.prototype.write = function (buf) {
|
|
2014
|
+
if (buf.length === 0) return '';
|
|
2015
|
+
var r;
|
|
2016
|
+
var i;
|
|
2017
|
+
if (this.lastNeed) {
|
|
2018
|
+
r = this.fillLast(buf);
|
|
2019
|
+
if (r === undefined) return '';
|
|
2020
|
+
i = this.lastNeed;
|
|
2021
|
+
this.lastNeed = 0;
|
|
2022
|
+
} else {
|
|
2023
|
+
i = 0;
|
|
2024
|
+
}
|
|
2025
|
+
if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i);
|
|
2026
|
+
return r || '';
|
|
2027
|
+
};
|
|
2028
|
+
|
|
2029
|
+
StringDecoder$1.prototype.end = utf8End;
|
|
2030
|
+
|
|
2031
|
+
// Returns only complete characters in a Buffer
|
|
2032
|
+
StringDecoder$1.prototype.text = utf8Text;
|
|
2033
|
+
|
|
2034
|
+
// Attempts to complete a partial non-UTF-8 character using bytes from a Buffer
|
|
2035
|
+
StringDecoder$1.prototype.fillLast = function (buf) {
|
|
2036
|
+
if (this.lastNeed <= buf.length) {
|
|
2037
|
+
buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed);
|
|
2038
|
+
return this.lastChar.toString(this.encoding, 0, this.lastTotal);
|
|
2039
|
+
}
|
|
2040
|
+
buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length);
|
|
2041
|
+
this.lastNeed -= buf.length;
|
|
2042
|
+
};
|
|
2043
|
+
|
|
2044
|
+
// Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a
|
|
2045
|
+
// continuation byte. If an invalid byte is detected, -2 is returned.
|
|
2046
|
+
function utf8CheckByte(byte) {
|
|
2047
|
+
if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4;
|
|
2048
|
+
return byte >> 6 === 0x02 ? -1 : -2;
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
// Checks at most 3 bytes at the end of a Buffer in order to detect an
|
|
2052
|
+
// incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4)
|
|
2053
|
+
// needed to complete the UTF-8 character (if applicable) are returned.
|
|
2054
|
+
function utf8CheckIncomplete(self, buf, i) {
|
|
2055
|
+
var j = buf.length - 1;
|
|
2056
|
+
if (j < i) return 0;
|
|
2057
|
+
var nb = utf8CheckByte(buf[j]);
|
|
2058
|
+
if (nb >= 0) {
|
|
2059
|
+
if (nb > 0) self.lastNeed = nb - 1;
|
|
2060
|
+
return nb;
|
|
2061
|
+
}
|
|
2062
|
+
if (--j < i || nb === -2) return 0;
|
|
2063
|
+
nb = utf8CheckByte(buf[j]);
|
|
2064
|
+
if (nb >= 0) {
|
|
2065
|
+
if (nb > 0) self.lastNeed = nb - 2;
|
|
2066
|
+
return nb;
|
|
2067
|
+
}
|
|
2068
|
+
if (--j < i || nb === -2) return 0;
|
|
2069
|
+
nb = utf8CheckByte(buf[j]);
|
|
2070
|
+
if (nb >= 0) {
|
|
2071
|
+
if (nb > 0) {
|
|
2072
|
+
if (nb === 2) nb = 0;else self.lastNeed = nb - 3;
|
|
2073
|
+
}
|
|
2074
|
+
return nb;
|
|
2075
|
+
}
|
|
2076
|
+
return 0;
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
// Validates as many continuation bytes for a multi-byte UTF-8 character as
|
|
2080
|
+
// needed or are available. If we see a non-continuation byte where we expect
|
|
2081
|
+
// one, we "replace" the validated continuation bytes we've seen so far with
|
|
2082
|
+
// a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding
|
|
2083
|
+
// behavior. The continuation byte check is included three times in the case
|
|
2084
|
+
// where all of the continuation bytes for a character exist in the same buffer.
|
|
2085
|
+
// It is also done this way as a slight performance increase instead of using a
|
|
2086
|
+
// loop.
|
|
2087
|
+
function utf8CheckExtraBytes(self, buf, p) {
|
|
2088
|
+
if ((buf[0] & 0xC0) !== 0x80) {
|
|
2089
|
+
self.lastNeed = 0;
|
|
2090
|
+
return '\ufffd';
|
|
2091
|
+
}
|
|
2092
|
+
if (self.lastNeed > 1 && buf.length > 1) {
|
|
2093
|
+
if ((buf[1] & 0xC0) !== 0x80) {
|
|
2094
|
+
self.lastNeed = 1;
|
|
2095
|
+
return '\ufffd';
|
|
2096
|
+
}
|
|
2097
|
+
if (self.lastNeed > 2 && buf.length > 2) {
|
|
2098
|
+
if ((buf[2] & 0xC0) !== 0x80) {
|
|
2099
|
+
self.lastNeed = 2;
|
|
2100
|
+
return '\ufffd';
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
// Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer.
|
|
2107
|
+
function utf8FillLast(buf) {
|
|
2108
|
+
var p = this.lastTotal - this.lastNeed;
|
|
2109
|
+
var r = utf8CheckExtraBytes(this, buf);
|
|
2110
|
+
if (r !== undefined) return r;
|
|
2111
|
+
if (this.lastNeed <= buf.length) {
|
|
2112
|
+
buf.copy(this.lastChar, p, 0, this.lastNeed);
|
|
2113
|
+
return this.lastChar.toString(this.encoding, 0, this.lastTotal);
|
|
2114
|
+
}
|
|
2115
|
+
buf.copy(this.lastChar, p, 0, buf.length);
|
|
2116
|
+
this.lastNeed -= buf.length;
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
// Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a
|
|
2120
|
+
// partial character, the character's bytes are buffered until the required
|
|
2121
|
+
// number of bytes are available.
|
|
2122
|
+
function utf8Text(buf, i) {
|
|
2123
|
+
var total = utf8CheckIncomplete(this, buf, i);
|
|
2124
|
+
if (!this.lastNeed) return buf.toString('utf8', i);
|
|
2125
|
+
this.lastTotal = total;
|
|
2126
|
+
var end = buf.length - (total - this.lastNeed);
|
|
2127
|
+
buf.copy(this.lastChar, 0, end);
|
|
2128
|
+
return buf.toString('utf8', i, end);
|
|
2129
|
+
}
|
|
2130
|
+
|
|
2131
|
+
// For UTF-8, a replacement character is added when ending on a partial
|
|
2132
|
+
// character.
|
|
2133
|
+
function utf8End(buf) {
|
|
2134
|
+
var r = buf && buf.length ? this.write(buf) : '';
|
|
2135
|
+
if (this.lastNeed) return r + '\ufffd';
|
|
2136
|
+
return r;
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
// UTF-16LE typically needs two bytes per character, but even if we have an even
|
|
2140
|
+
// number of bytes available, we need to check if we end on a leading/high
|
|
2141
|
+
// surrogate. In that case, we need to wait for the next two bytes in order to
|
|
2142
|
+
// decode the last character properly.
|
|
2143
|
+
function utf16Text(buf, i) {
|
|
2144
|
+
if ((buf.length - i) % 2 === 0) {
|
|
2145
|
+
var r = buf.toString('utf16le', i);
|
|
2146
|
+
if (r) {
|
|
2147
|
+
var c = r.charCodeAt(r.length - 1);
|
|
2148
|
+
if (c >= 0xD800 && c <= 0xDBFF) {
|
|
2149
|
+
this.lastNeed = 2;
|
|
2150
|
+
this.lastTotal = 4;
|
|
2151
|
+
this.lastChar[0] = buf[buf.length - 2];
|
|
2152
|
+
this.lastChar[1] = buf[buf.length - 1];
|
|
2153
|
+
return r.slice(0, -1);
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
return r;
|
|
2157
|
+
}
|
|
2158
|
+
this.lastNeed = 1;
|
|
2159
|
+
this.lastTotal = 2;
|
|
2160
|
+
this.lastChar[0] = buf[buf.length - 1];
|
|
2161
|
+
return buf.toString('utf16le', i, buf.length - 1);
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
// For UTF-16LE we do not explicitly append special replacement characters if we
|
|
2165
|
+
// end on a partial character, we simply let v8 handle that.
|
|
2166
|
+
function utf16End(buf) {
|
|
2167
|
+
var r = buf && buf.length ? this.write(buf) : '';
|
|
2168
|
+
if (this.lastNeed) {
|
|
2169
|
+
var end = this.lastTotal - this.lastNeed;
|
|
2170
|
+
return r + this.lastChar.toString('utf16le', 0, end);
|
|
2171
|
+
}
|
|
2172
|
+
return r;
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
function base64Text(buf, i) {
|
|
2176
|
+
var n = (buf.length - i) % 3;
|
|
2177
|
+
if (n === 0) return buf.toString('base64', i);
|
|
2178
|
+
this.lastNeed = 3 - n;
|
|
2179
|
+
this.lastTotal = 3;
|
|
2180
|
+
if (n === 1) {
|
|
2181
|
+
this.lastChar[0] = buf[buf.length - 1];
|
|
2182
|
+
} else {
|
|
2183
|
+
this.lastChar[0] = buf[buf.length - 2];
|
|
2184
|
+
this.lastChar[1] = buf[buf.length - 1];
|
|
2185
|
+
}
|
|
2186
|
+
return buf.toString('base64', i, buf.length - n);
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2189
|
+
function base64End(buf) {
|
|
2190
|
+
var r = buf && buf.length ? this.write(buf) : '';
|
|
2191
|
+
if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
|
|
2192
|
+
return r;
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
// Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex)
|
|
2196
|
+
function simpleWrite(buf) {
|
|
2197
|
+
return buf.toString(this.encoding);
|
|
2198
|
+
}
|
|
2199
|
+
|
|
2200
|
+
function simpleEnd(buf) {
|
|
2201
|
+
return buf && buf.length ? this.write(buf) : '';
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
var string_decoder = {
|
|
2205
|
+
StringDecoder: StringDecoder_1
|
|
2206
|
+
};
|
|
2207
|
+
|
|
2208
|
+
var ERR_STREAM_PREMATURE_CLOSE = errors.codes.ERR_STREAM_PREMATURE_CLOSE;
|
|
2209
|
+
|
|
2210
|
+
function once$1(callback) {
|
|
2211
|
+
var called = false;
|
|
2212
|
+
return function () {
|
|
2213
|
+
if (called) return;
|
|
2214
|
+
called = true;
|
|
2215
|
+
|
|
2216
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2217
|
+
args[_key] = arguments[_key];
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
callback.apply(this, args);
|
|
2221
|
+
};
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2224
|
+
function noop$1() {}
|
|
2225
|
+
|
|
2226
|
+
function isRequest$1(stream) {
|
|
2227
|
+
return stream.setHeader && typeof stream.abort === 'function';
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
function eos$1(stream, opts, callback) {
|
|
2231
|
+
if (typeof opts === 'function') return eos$1(stream, null, opts);
|
|
2232
|
+
if (!opts) opts = {};
|
|
2233
|
+
callback = once$1(callback || noop$1);
|
|
2234
|
+
var readable = opts.readable || opts.readable !== false && stream.readable;
|
|
2235
|
+
var writable = opts.writable || opts.writable !== false && stream.writable;
|
|
2236
|
+
|
|
2237
|
+
var onlegacyfinish = function onlegacyfinish() {
|
|
2238
|
+
if (!stream.writable) onfinish();
|
|
2239
|
+
};
|
|
2240
|
+
|
|
2241
|
+
var writableEnded = stream._writableState && stream._writableState.finished;
|
|
2242
|
+
|
|
2243
|
+
var onfinish = function onfinish() {
|
|
2244
|
+
writable = false;
|
|
2245
|
+
writableEnded = true;
|
|
2246
|
+
if (!readable) callback.call(stream);
|
|
2247
|
+
};
|
|
2248
|
+
|
|
2249
|
+
var readableEnded = stream._readableState && stream._readableState.endEmitted;
|
|
2250
|
+
|
|
2251
|
+
var onend = function onend() {
|
|
2252
|
+
readable = false;
|
|
2253
|
+
readableEnded = true;
|
|
2254
|
+
if (!writable) callback.call(stream);
|
|
2255
|
+
};
|
|
2256
|
+
|
|
2257
|
+
var onerror = function onerror(err) {
|
|
2258
|
+
callback.call(stream, err);
|
|
2259
|
+
};
|
|
2260
|
+
|
|
2261
|
+
var onclose = function onclose() {
|
|
2262
|
+
var err;
|
|
2263
|
+
|
|
2264
|
+
if (readable && !readableEnded) {
|
|
2265
|
+
if (!stream._readableState || !stream._readableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE();
|
|
2266
|
+
return callback.call(stream, err);
|
|
2267
|
+
}
|
|
2268
|
+
|
|
2269
|
+
if (writable && !writableEnded) {
|
|
2270
|
+
if (!stream._writableState || !stream._writableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE();
|
|
2271
|
+
return callback.call(stream, err);
|
|
2272
|
+
}
|
|
2273
|
+
};
|
|
2274
|
+
|
|
2275
|
+
var onrequest = function onrequest() {
|
|
2276
|
+
stream.req.on('finish', onfinish);
|
|
2277
|
+
};
|
|
2278
|
+
|
|
2279
|
+
if (isRequest$1(stream)) {
|
|
2280
|
+
stream.on('complete', onfinish);
|
|
2281
|
+
stream.on('abort', onclose);
|
|
2282
|
+
if (stream.req) onrequest();else stream.on('request', onrequest);
|
|
2283
|
+
} else if (writable && !stream._writableState) {
|
|
2284
|
+
// legacy streams
|
|
2285
|
+
stream.on('end', onlegacyfinish);
|
|
2286
|
+
stream.on('close', onlegacyfinish);
|
|
2287
|
+
}
|
|
2288
|
+
|
|
2289
|
+
stream.on('end', onend);
|
|
2290
|
+
stream.on('finish', onfinish);
|
|
2291
|
+
if (opts.error !== false) stream.on('error', onerror);
|
|
2292
|
+
stream.on('close', onclose);
|
|
2293
|
+
return function () {
|
|
2294
|
+
stream.removeListener('complete', onfinish);
|
|
2295
|
+
stream.removeListener('abort', onclose);
|
|
2296
|
+
stream.removeListener('request', onrequest);
|
|
2297
|
+
if (stream.req) stream.req.removeListener('finish', onfinish);
|
|
2298
|
+
stream.removeListener('end', onlegacyfinish);
|
|
2299
|
+
stream.removeListener('close', onlegacyfinish);
|
|
2300
|
+
stream.removeListener('finish', onfinish);
|
|
2301
|
+
stream.removeListener('end', onend);
|
|
2302
|
+
stream.removeListener('error', onerror);
|
|
2303
|
+
stream.removeListener('close', onclose);
|
|
2304
|
+
};
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
var endOfStream = eos$1;
|
|
2308
|
+
|
|
2309
|
+
var _Object$setPrototypeO;
|
|
2310
|
+
|
|
2311
|
+
function _defineProperty$1(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2312
|
+
|
|
2313
|
+
|
|
2314
|
+
|
|
2315
|
+
var kLastResolve = Symbol('lastResolve');
|
|
2316
|
+
var kLastReject = Symbol('lastReject');
|
|
2317
|
+
var kError = Symbol('error');
|
|
2318
|
+
var kEnded = Symbol('ended');
|
|
2319
|
+
var kLastPromise = Symbol('lastPromise');
|
|
2320
|
+
var kHandlePromise = Symbol('handlePromise');
|
|
2321
|
+
var kStream = Symbol('stream');
|
|
2322
|
+
|
|
2323
|
+
function createIterResult(value, done) {
|
|
2324
|
+
return {
|
|
2325
|
+
value: value,
|
|
2326
|
+
done: done
|
|
2327
|
+
};
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
function readAndResolve(iter) {
|
|
2331
|
+
var resolve = iter[kLastResolve];
|
|
2332
|
+
|
|
2333
|
+
if (resolve !== null) {
|
|
2334
|
+
var data = iter[kStream].read(); // we defer if data is null
|
|
2335
|
+
// we can be expecting either 'end' or
|
|
2336
|
+
// 'error'
|
|
2337
|
+
|
|
2338
|
+
if (data !== null) {
|
|
2339
|
+
iter[kLastPromise] = null;
|
|
2340
|
+
iter[kLastResolve] = null;
|
|
2341
|
+
iter[kLastReject] = null;
|
|
2342
|
+
resolve(createIterResult(data, false));
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
function onReadable(iter) {
|
|
2348
|
+
// we wait for the next tick, because it might
|
|
2349
|
+
// emit an error with process.nextTick
|
|
2350
|
+
process.nextTick(readAndResolve, iter);
|
|
2351
|
+
}
|
|
2352
|
+
|
|
2353
|
+
function wrapForNext(lastPromise, iter) {
|
|
2354
|
+
return function (resolve, reject) {
|
|
2355
|
+
lastPromise.then(function () {
|
|
2356
|
+
if (iter[kEnded]) {
|
|
2357
|
+
resolve(createIterResult(undefined, true));
|
|
2358
|
+
return;
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
iter[kHandlePromise](resolve, reject);
|
|
2362
|
+
}, reject);
|
|
2363
|
+
};
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
var AsyncIteratorPrototype = Object.getPrototypeOf(function () {});
|
|
2367
|
+
var ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf((_Object$setPrototypeO = {
|
|
2368
|
+
get stream() {
|
|
2369
|
+
return this[kStream];
|
|
2370
|
+
},
|
|
2371
|
+
|
|
2372
|
+
next: function next() {
|
|
2373
|
+
var _this = this;
|
|
2374
|
+
|
|
2375
|
+
// if we have detected an error in the meanwhile
|
|
2376
|
+
// reject straight away
|
|
2377
|
+
var error = this[kError];
|
|
2378
|
+
|
|
2379
|
+
if (error !== null) {
|
|
2380
|
+
return Promise.reject(error);
|
|
2381
|
+
}
|
|
2382
|
+
|
|
2383
|
+
if (this[kEnded]) {
|
|
2384
|
+
return Promise.resolve(createIterResult(undefined, true));
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
if (this[kStream].destroyed) {
|
|
2388
|
+
// We need to defer via nextTick because if .destroy(err) is
|
|
2389
|
+
// called, the error will be emitted via nextTick, and
|
|
2390
|
+
// we cannot guarantee that there is no error lingering around
|
|
2391
|
+
// waiting to be emitted.
|
|
2392
|
+
return new Promise(function (resolve, reject) {
|
|
2393
|
+
process.nextTick(function () {
|
|
2394
|
+
if (_this[kError]) {
|
|
2395
|
+
reject(_this[kError]);
|
|
2396
|
+
} else {
|
|
2397
|
+
resolve(createIterResult(undefined, true));
|
|
2398
|
+
}
|
|
2399
|
+
});
|
|
2400
|
+
});
|
|
2401
|
+
} // if we have multiple next() calls
|
|
2402
|
+
// we will wait for the previous Promise to finish
|
|
2403
|
+
// this logic is optimized to support for await loops,
|
|
2404
|
+
// where next() is only called once at a time
|
|
2405
|
+
|
|
2406
|
+
|
|
2407
|
+
var lastPromise = this[kLastPromise];
|
|
2408
|
+
var promise;
|
|
2409
|
+
|
|
2410
|
+
if (lastPromise) {
|
|
2411
|
+
promise = new Promise(wrapForNext(lastPromise, this));
|
|
2412
|
+
} else {
|
|
2413
|
+
// fast path needed to support multiple this.push()
|
|
2414
|
+
// without triggering the next() queue
|
|
2415
|
+
var data = this[kStream].read();
|
|
2416
|
+
|
|
2417
|
+
if (data !== null) {
|
|
2418
|
+
return Promise.resolve(createIterResult(data, false));
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
promise = new Promise(this[kHandlePromise]);
|
|
2422
|
+
}
|
|
2423
|
+
|
|
2424
|
+
this[kLastPromise] = promise;
|
|
2425
|
+
return promise;
|
|
2426
|
+
}
|
|
2427
|
+
}, _defineProperty$1(_Object$setPrototypeO, Symbol.asyncIterator, function () {
|
|
2428
|
+
return this;
|
|
2429
|
+
}), _defineProperty$1(_Object$setPrototypeO, "return", function _return() {
|
|
2430
|
+
var _this2 = this;
|
|
2431
|
+
|
|
2432
|
+
// destroy(err, cb) is a private API
|
|
2433
|
+
// we can guarantee we have that here, because we control the
|
|
2434
|
+
// Readable class this is attached to
|
|
2435
|
+
return new Promise(function (resolve, reject) {
|
|
2436
|
+
_this2[kStream].destroy(null, function (err) {
|
|
2437
|
+
if (err) {
|
|
2438
|
+
reject(err);
|
|
2439
|
+
return;
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2442
|
+
resolve(createIterResult(undefined, true));
|
|
2443
|
+
});
|
|
2444
|
+
});
|
|
2445
|
+
}), _Object$setPrototypeO), AsyncIteratorPrototype);
|
|
2446
|
+
|
|
2447
|
+
var createReadableStreamAsyncIterator$1 = function createReadableStreamAsyncIterator(stream) {
|
|
2448
|
+
var _Object$create;
|
|
2449
|
+
|
|
2450
|
+
var iterator = Object.create(ReadableStreamAsyncIteratorPrototype, (_Object$create = {}, _defineProperty$1(_Object$create, kStream, {
|
|
2451
|
+
value: stream,
|
|
2452
|
+
writable: true
|
|
2453
|
+
}), _defineProperty$1(_Object$create, kLastResolve, {
|
|
2454
|
+
value: null,
|
|
2455
|
+
writable: true
|
|
2456
|
+
}), _defineProperty$1(_Object$create, kLastReject, {
|
|
2457
|
+
value: null,
|
|
2458
|
+
writable: true
|
|
2459
|
+
}), _defineProperty$1(_Object$create, kError, {
|
|
2460
|
+
value: null,
|
|
2461
|
+
writable: true
|
|
2462
|
+
}), _defineProperty$1(_Object$create, kEnded, {
|
|
2463
|
+
value: stream._readableState.endEmitted,
|
|
2464
|
+
writable: true
|
|
2465
|
+
}), _defineProperty$1(_Object$create, kHandlePromise, {
|
|
2466
|
+
value: function value(resolve, reject) {
|
|
2467
|
+
var data = iterator[kStream].read();
|
|
2468
|
+
|
|
2469
|
+
if (data) {
|
|
2470
|
+
iterator[kLastPromise] = null;
|
|
2471
|
+
iterator[kLastResolve] = null;
|
|
2472
|
+
iterator[kLastReject] = null;
|
|
2473
|
+
resolve(createIterResult(data, false));
|
|
2474
|
+
} else {
|
|
2475
|
+
iterator[kLastResolve] = resolve;
|
|
2476
|
+
iterator[kLastReject] = reject;
|
|
2477
|
+
}
|
|
2478
|
+
},
|
|
2479
|
+
writable: true
|
|
2480
|
+
}), _Object$create));
|
|
2481
|
+
iterator[kLastPromise] = null;
|
|
2482
|
+
endOfStream(stream, function (err) {
|
|
2483
|
+
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
|
|
2484
|
+
var reject = iterator[kLastReject]; // reject if we are waiting for data in the Promise
|
|
2485
|
+
// returned by next() and store the error
|
|
2486
|
+
|
|
2487
|
+
if (reject !== null) {
|
|
2488
|
+
iterator[kLastPromise] = null;
|
|
2489
|
+
iterator[kLastResolve] = null;
|
|
2490
|
+
iterator[kLastReject] = null;
|
|
2491
|
+
reject(err);
|
|
2492
|
+
}
|
|
2493
|
+
|
|
2494
|
+
iterator[kError] = err;
|
|
2495
|
+
return;
|
|
2496
|
+
}
|
|
2497
|
+
|
|
2498
|
+
var resolve = iterator[kLastResolve];
|
|
2499
|
+
|
|
2500
|
+
if (resolve !== null) {
|
|
2501
|
+
iterator[kLastPromise] = null;
|
|
2502
|
+
iterator[kLastResolve] = null;
|
|
2503
|
+
iterator[kLastReject] = null;
|
|
2504
|
+
resolve(createIterResult(undefined, true));
|
|
2505
|
+
}
|
|
2506
|
+
|
|
2507
|
+
iterator[kEnded] = true;
|
|
2508
|
+
});
|
|
2509
|
+
stream.on('readable', onReadable.bind(null, iterator));
|
|
2510
|
+
return iterator;
|
|
2511
|
+
};
|
|
2512
|
+
|
|
2513
|
+
var async_iterator = createReadableStreamAsyncIterator$1;
|
|
2514
|
+
|
|
2515
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
2516
|
+
|
|
2517
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
2518
|
+
|
|
2519
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
2520
|
+
|
|
2521
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
2522
|
+
|
|
2523
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2524
|
+
|
|
2525
|
+
var ERR_INVALID_ARG_TYPE$1 = errors.codes.ERR_INVALID_ARG_TYPE;
|
|
2526
|
+
|
|
2527
|
+
function from$1(Readable, iterable, opts) {
|
|
2528
|
+
var iterator;
|
|
2529
|
+
|
|
2530
|
+
if (iterable && typeof iterable.next === 'function') {
|
|
2531
|
+
iterator = iterable;
|
|
2532
|
+
} else if (iterable && iterable[Symbol.asyncIterator]) iterator = iterable[Symbol.asyncIterator]();else if (iterable && iterable[Symbol.iterator]) iterator = iterable[Symbol.iterator]();else throw new ERR_INVALID_ARG_TYPE$1('iterable', ['Iterable'], iterable);
|
|
2533
|
+
|
|
2534
|
+
var readable = new Readable(_objectSpread({
|
|
2535
|
+
objectMode: true
|
|
2536
|
+
}, opts)); // Reading boolean to protect against _read
|
|
2537
|
+
// being called before last iteration completion.
|
|
2538
|
+
|
|
2539
|
+
var reading = false;
|
|
2540
|
+
|
|
2541
|
+
readable._read = function () {
|
|
2542
|
+
if (!reading) {
|
|
2543
|
+
reading = true;
|
|
2544
|
+
next();
|
|
2545
|
+
}
|
|
2546
|
+
};
|
|
2547
|
+
|
|
2548
|
+
function next() {
|
|
2549
|
+
return _next2.apply(this, arguments);
|
|
2550
|
+
}
|
|
2551
|
+
|
|
2552
|
+
function _next2() {
|
|
2553
|
+
_next2 = _asyncToGenerator(function* () {
|
|
2554
|
+
try {
|
|
2555
|
+
var _ref = yield iterator.next(),
|
|
2556
|
+
value = _ref.value,
|
|
2557
|
+
done = _ref.done;
|
|
2558
|
+
|
|
2559
|
+
if (done) {
|
|
2560
|
+
readable.push(null);
|
|
2561
|
+
} else if (readable.push((yield value))) {
|
|
2562
|
+
next();
|
|
2563
|
+
} else {
|
|
2564
|
+
reading = false;
|
|
2565
|
+
}
|
|
2566
|
+
} catch (err) {
|
|
2567
|
+
readable.destroy(err);
|
|
2568
|
+
}
|
|
2569
|
+
});
|
|
2570
|
+
return _next2.apply(this, arguments);
|
|
2571
|
+
}
|
|
2572
|
+
|
|
2573
|
+
return readable;
|
|
2574
|
+
}
|
|
2575
|
+
|
|
2576
|
+
var from_1 = from$1;
|
|
2577
|
+
|
|
2578
|
+
var _stream_readable = Readable;
|
|
2579
|
+
/*<replacement>*/
|
|
2580
|
+
|
|
2581
|
+
var Duplex;
|
|
2582
|
+
/*</replacement>*/
|
|
2583
|
+
|
|
2584
|
+
Readable.ReadableState = ReadableState;
|
|
2585
|
+
/*<replacement>*/
|
|
2586
|
+
|
|
2587
|
+
events.EventEmitter;
|
|
2588
|
+
|
|
2589
|
+
var EElistenerCount = function EElistenerCount(emitter, type) {
|
|
2590
|
+
return emitter.listeners(type).length;
|
|
2591
|
+
};
|
|
2592
|
+
/*</replacement>*/
|
|
2593
|
+
|
|
2594
|
+
/*<replacement>*/
|
|
2595
|
+
|
|
2596
|
+
|
|
2597
|
+
|
|
2598
|
+
/*</replacement>*/
|
|
2599
|
+
|
|
2600
|
+
|
|
2601
|
+
var Buffer$1 = buffer.Buffer;
|
|
2602
|
+
|
|
2603
|
+
var OurUint8Array = commonjsGlobal.Uint8Array || function () {};
|
|
2604
|
+
|
|
2605
|
+
function _uint8ArrayToBuffer(chunk) {
|
|
2606
|
+
return Buffer$1.from(chunk);
|
|
2607
|
+
}
|
|
2608
|
+
|
|
2609
|
+
function _isUint8Array(obj) {
|
|
2610
|
+
return Buffer$1.isBuffer(obj) || obj instanceof OurUint8Array;
|
|
2611
|
+
}
|
|
2612
|
+
/*<replacement>*/
|
|
2613
|
+
|
|
2614
|
+
|
|
2615
|
+
|
|
2616
|
+
|
|
2617
|
+
var debug;
|
|
2618
|
+
|
|
2619
|
+
if (util && util.debuglog) {
|
|
2620
|
+
debug = util.debuglog('stream');
|
|
2621
|
+
} else {
|
|
2622
|
+
debug = function debug() {};
|
|
2623
|
+
}
|
|
2624
|
+
/*</replacement>*/
|
|
2625
|
+
|
|
2626
|
+
|
|
2627
|
+
|
|
2628
|
+
|
|
2629
|
+
|
|
2630
|
+
|
|
2631
|
+
var getHighWaterMark = state.getHighWaterMark;
|
|
2632
|
+
|
|
2633
|
+
var _require$codes$2 = errors.codes,
|
|
2634
|
+
ERR_INVALID_ARG_TYPE = _require$codes$2.ERR_INVALID_ARG_TYPE,
|
|
2635
|
+
ERR_STREAM_PUSH_AFTER_EOF = _require$codes$2.ERR_STREAM_PUSH_AFTER_EOF,
|
|
2636
|
+
ERR_METHOD_NOT_IMPLEMENTED$1 = _require$codes$2.ERR_METHOD_NOT_IMPLEMENTED,
|
|
2637
|
+
ERR_STREAM_UNSHIFT_AFTER_END_EVENT = _require$codes$2.ERR_STREAM_UNSHIFT_AFTER_END_EVENT; // Lazy loaded to improve the startup performance.
|
|
2638
|
+
|
|
2639
|
+
|
|
2640
|
+
var StringDecoder;
|
|
2641
|
+
var createReadableStreamAsyncIterator;
|
|
2642
|
+
var from;
|
|
2643
|
+
|
|
2644
|
+
inherits(Readable, stream);
|
|
2645
|
+
|
|
2646
|
+
var errorOrDestroy = destroy_1.errorOrDestroy;
|
|
2647
|
+
var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];
|
|
2648
|
+
|
|
2649
|
+
function prependListener(emitter, event, fn) {
|
|
2650
|
+
// Sadly this is not cacheable as some libraries bundle their own
|
|
2651
|
+
// event emitter implementation with them.
|
|
2652
|
+
if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn); // This is a hack to make sure that our error handler is attached before any
|
|
2653
|
+
// userland ones. NEVER DO THIS. This is here only because this code needs
|
|
2654
|
+
// to continue to work with older versions of Node.js that do not include
|
|
2655
|
+
// the prependListener() method. The goal is to eventually remove this hack.
|
|
2656
|
+
|
|
2657
|
+
if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (Array.isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
|
|
2658
|
+
}
|
|
2659
|
+
|
|
2660
|
+
function ReadableState(options, stream, isDuplex) {
|
|
2661
|
+
Duplex = Duplex || require$$2;
|
|
2662
|
+
options = options || {}; // Duplex streams are both readable and writable, but share
|
|
2663
|
+
// the same options object.
|
|
2664
|
+
// However, some cases require setting options to different
|
|
2665
|
+
// values for the readable and the writable sides of the duplex stream.
|
|
2666
|
+
// These options can be provided separately as readableXXX and writableXXX.
|
|
2667
|
+
|
|
2668
|
+
if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; // object stream flag. Used to make read(n) ignore n and to
|
|
2669
|
+
// make all the buffer merging and length checks go away
|
|
2670
|
+
|
|
2671
|
+
this.objectMode = !!options.objectMode;
|
|
2672
|
+
if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode; // the point at which it stops calling _read() to fill the buffer
|
|
2673
|
+
// Note: 0 is a valid value, means "don't call _read preemptively ever"
|
|
2674
|
+
|
|
2675
|
+
this.highWaterMark = getHighWaterMark(this, options, 'readableHighWaterMark', isDuplex); // A linked list is used to store data chunks instead of an array because the
|
|
2676
|
+
// linked list can remove elements from the beginning faster than
|
|
2677
|
+
// array.shift()
|
|
2678
|
+
|
|
2679
|
+
this.buffer = new buffer_list();
|
|
2680
|
+
this.length = 0;
|
|
2681
|
+
this.pipes = null;
|
|
2682
|
+
this.pipesCount = 0;
|
|
2683
|
+
this.flowing = null;
|
|
2684
|
+
this.ended = false;
|
|
2685
|
+
this.endEmitted = false;
|
|
2686
|
+
this.reading = false; // a flag to be able to tell if the event 'readable'/'data' is emitted
|
|
2687
|
+
// immediately, or on a later tick. We set this to true at first, because
|
|
2688
|
+
// any actions that shouldn't happen until "later" should generally also
|
|
2689
|
+
// not happen before the first read call.
|
|
2690
|
+
|
|
2691
|
+
this.sync = true; // whenever we return null, then we set a flag to say
|
|
2692
|
+
// that we're awaiting a 'readable' event emission.
|
|
2693
|
+
|
|
2694
|
+
this.needReadable = false;
|
|
2695
|
+
this.emittedReadable = false;
|
|
2696
|
+
this.readableListening = false;
|
|
2697
|
+
this.resumeScheduled = false;
|
|
2698
|
+
this.paused = true; // Should close be emitted on destroy. Defaults to true.
|
|
2699
|
+
|
|
2700
|
+
this.emitClose = options.emitClose !== false; // Should .destroy() be called after 'end' (and potentially 'finish')
|
|
2701
|
+
|
|
2702
|
+
this.autoDestroy = !!options.autoDestroy; // has it been destroyed
|
|
2703
|
+
|
|
2704
|
+
this.destroyed = false; // Crypto is kind of old and crusty. Historically, its default string
|
|
2705
|
+
// encoding is 'binary' so we have to make this configurable.
|
|
2706
|
+
// Everything else in the universe uses 'utf8', though.
|
|
2707
|
+
|
|
2708
|
+
this.defaultEncoding = options.defaultEncoding || 'utf8'; // the number of writers that are awaiting a drain event in .pipe()s
|
|
2709
|
+
|
|
2710
|
+
this.awaitDrain = 0; // if true, a maybeReadMore has been scheduled
|
|
2711
|
+
|
|
2712
|
+
this.readingMore = false;
|
|
2713
|
+
this.decoder = null;
|
|
2714
|
+
this.encoding = null;
|
|
2715
|
+
|
|
2716
|
+
if (options.encoding) {
|
|
2717
|
+
if (!StringDecoder) StringDecoder = string_decoder.StringDecoder;
|
|
2718
|
+
this.decoder = new StringDecoder(options.encoding);
|
|
2719
|
+
this.encoding = options.encoding;
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
function Readable(options) {
|
|
2724
|
+
Duplex = Duplex || require$$2;
|
|
2725
|
+
if (!(this instanceof Readable)) return new Readable(options); // Checking for a Stream.Duplex instance is faster here instead of inside
|
|
2726
|
+
// the ReadableState constructor, at least with V8 6.5
|
|
2727
|
+
|
|
2728
|
+
var isDuplex = this instanceof Duplex;
|
|
2729
|
+
this._readableState = new ReadableState(options, this, isDuplex); // legacy
|
|
2730
|
+
|
|
2731
|
+
this.readable = true;
|
|
2732
|
+
|
|
2733
|
+
if (options) {
|
|
2734
|
+
if (typeof options.read === 'function') this._read = options.read;
|
|
2735
|
+
if (typeof options.destroy === 'function') this._destroy = options.destroy;
|
|
2736
|
+
}
|
|
2737
|
+
|
|
2738
|
+
stream.call(this);
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
Object.defineProperty(Readable.prototype, 'destroyed', {
|
|
2742
|
+
// making it explicit this property is not enumerable
|
|
2743
|
+
// because otherwise some prototype manipulation in
|
|
2744
|
+
// userland will fail
|
|
2745
|
+
enumerable: false,
|
|
2746
|
+
get: function get() {
|
|
2747
|
+
if (this._readableState === undefined) {
|
|
2748
|
+
return false;
|
|
2749
|
+
}
|
|
2750
|
+
|
|
2751
|
+
return this._readableState.destroyed;
|
|
2752
|
+
},
|
|
2753
|
+
set: function set(value) {
|
|
2754
|
+
// we ignore the value if the stream
|
|
2755
|
+
// has not been initialized yet
|
|
2756
|
+
if (!this._readableState) {
|
|
2757
|
+
return;
|
|
2758
|
+
} // backward compatibility, the user is explicitly
|
|
2759
|
+
// managing destroyed
|
|
2760
|
+
|
|
2761
|
+
|
|
2762
|
+
this._readableState.destroyed = value;
|
|
2763
|
+
}
|
|
2764
|
+
});
|
|
2765
|
+
Readable.prototype.destroy = destroy_1.destroy;
|
|
2766
|
+
Readable.prototype._undestroy = destroy_1.undestroy;
|
|
2767
|
+
|
|
2768
|
+
Readable.prototype._destroy = function (err, cb) {
|
|
2769
|
+
cb(err);
|
|
2770
|
+
}; // Manually shove something into the read() buffer.
|
|
2771
|
+
// This returns true if the highWaterMark has not been hit yet,
|
|
2772
|
+
// similar to how Writable.write() returns true if you should
|
|
2773
|
+
// write() some more.
|
|
2774
|
+
|
|
2775
|
+
|
|
2776
|
+
Readable.prototype.push = function (chunk, encoding) {
|
|
2777
|
+
var state = this._readableState;
|
|
2778
|
+
var skipChunkCheck;
|
|
2779
|
+
|
|
2780
|
+
if (!state.objectMode) {
|
|
2781
|
+
if (typeof chunk === 'string') {
|
|
2782
|
+
encoding = encoding || state.defaultEncoding;
|
|
2783
|
+
|
|
2784
|
+
if (encoding !== state.encoding) {
|
|
2785
|
+
chunk = Buffer$1.from(chunk, encoding);
|
|
2786
|
+
encoding = '';
|
|
2787
|
+
}
|
|
2788
|
+
|
|
2789
|
+
skipChunkCheck = true;
|
|
2790
|
+
}
|
|
2791
|
+
} else {
|
|
2792
|
+
skipChunkCheck = true;
|
|
2793
|
+
}
|
|
2794
|
+
|
|
2795
|
+
return readableAddChunk(this, chunk, encoding, false, skipChunkCheck);
|
|
2796
|
+
}; // Unshift should *always* be something directly out of read()
|
|
2797
|
+
|
|
2798
|
+
|
|
2799
|
+
Readable.prototype.unshift = function (chunk) {
|
|
2800
|
+
return readableAddChunk(this, chunk, null, true, false);
|
|
2801
|
+
};
|
|
2802
|
+
|
|
2803
|
+
function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
|
|
2804
|
+
debug('readableAddChunk', chunk);
|
|
2805
|
+
var state = stream._readableState;
|
|
2806
|
+
|
|
2807
|
+
if (chunk === null) {
|
|
2808
|
+
state.reading = false;
|
|
2809
|
+
onEofChunk(stream, state);
|
|
2810
|
+
} else {
|
|
2811
|
+
var er;
|
|
2812
|
+
if (!skipChunkCheck) er = chunkInvalid(state, chunk);
|
|
2813
|
+
|
|
2814
|
+
if (er) {
|
|
2815
|
+
errorOrDestroy(stream, er);
|
|
2816
|
+
} else if (state.objectMode || chunk && chunk.length > 0) {
|
|
2817
|
+
if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer$1.prototype) {
|
|
2818
|
+
chunk = _uint8ArrayToBuffer(chunk);
|
|
2819
|
+
}
|
|
2820
|
+
|
|
2821
|
+
if (addToFront) {
|
|
2822
|
+
if (state.endEmitted) errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());else addChunk(stream, state, chunk, true);
|
|
2823
|
+
} else if (state.ended) {
|
|
2824
|
+
errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
|
|
2825
|
+
} else if (state.destroyed) {
|
|
2826
|
+
return false;
|
|
2827
|
+
} else {
|
|
2828
|
+
state.reading = false;
|
|
2829
|
+
|
|
2830
|
+
if (state.decoder && !encoding) {
|
|
2831
|
+
chunk = state.decoder.write(chunk);
|
|
2832
|
+
if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state);
|
|
2833
|
+
} else {
|
|
2834
|
+
addChunk(stream, state, chunk, false);
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
} else if (!addToFront) {
|
|
2838
|
+
state.reading = false;
|
|
2839
|
+
maybeReadMore(stream, state);
|
|
2840
|
+
}
|
|
2841
|
+
} // We can push more data if we are below the highWaterMark.
|
|
2842
|
+
// Also, if we have no data yet, we can stand some more bytes.
|
|
2843
|
+
// This is to work around cases where hwm=0, such as the repl.
|
|
2844
|
+
|
|
2845
|
+
|
|
2846
|
+
return !state.ended && (state.length < state.highWaterMark || state.length === 0);
|
|
2847
|
+
}
|
|
2848
|
+
|
|
2849
|
+
function addChunk(stream, state, chunk, addToFront) {
|
|
2850
|
+
if (state.flowing && state.length === 0 && !state.sync) {
|
|
2851
|
+
state.awaitDrain = 0;
|
|
2852
|
+
stream.emit('data', chunk);
|
|
2853
|
+
} else {
|
|
2854
|
+
// update the buffer info.
|
|
2855
|
+
state.length += state.objectMode ? 1 : chunk.length;
|
|
2856
|
+
if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
|
|
2857
|
+
if (state.needReadable) emitReadable(stream);
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
maybeReadMore(stream, state);
|
|
2861
|
+
}
|
|
2862
|
+
|
|
2863
|
+
function chunkInvalid(state, chunk) {
|
|
2864
|
+
var er;
|
|
2865
|
+
|
|
2866
|
+
if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
|
|
2867
|
+
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
|
|
2868
|
+
}
|
|
2869
|
+
|
|
2870
|
+
return er;
|
|
2871
|
+
}
|
|
2872
|
+
|
|
2873
|
+
Readable.prototype.isPaused = function () {
|
|
2874
|
+
return this._readableState.flowing === false;
|
|
2875
|
+
}; // backwards compatibility.
|
|
2876
|
+
|
|
2877
|
+
|
|
2878
|
+
Readable.prototype.setEncoding = function (enc) {
|
|
2879
|
+
if (!StringDecoder) StringDecoder = string_decoder.StringDecoder;
|
|
2880
|
+
var decoder = new StringDecoder(enc);
|
|
2881
|
+
this._readableState.decoder = decoder; // If setEncoding(null), decoder.encoding equals utf8
|
|
2882
|
+
|
|
2883
|
+
this._readableState.encoding = this._readableState.decoder.encoding; // Iterate over current buffer to convert already stored Buffers:
|
|
2884
|
+
|
|
2885
|
+
var p = this._readableState.buffer.head;
|
|
2886
|
+
var content = '';
|
|
2887
|
+
|
|
2888
|
+
while (p !== null) {
|
|
2889
|
+
content += decoder.write(p.data);
|
|
2890
|
+
p = p.next;
|
|
2891
|
+
}
|
|
2892
|
+
|
|
2893
|
+
this._readableState.buffer.clear();
|
|
2894
|
+
|
|
2895
|
+
if (content !== '') this._readableState.buffer.push(content);
|
|
2896
|
+
this._readableState.length = content.length;
|
|
2897
|
+
return this;
|
|
2898
|
+
}; // Don't raise the hwm > 1GB
|
|
2899
|
+
|
|
2900
|
+
|
|
2901
|
+
var MAX_HWM = 0x40000000;
|
|
2902
|
+
|
|
2903
|
+
function computeNewHighWaterMark(n) {
|
|
2904
|
+
if (n >= MAX_HWM) {
|
|
2905
|
+
// TODO(ronag): Throw ERR_VALUE_OUT_OF_RANGE.
|
|
2906
|
+
n = MAX_HWM;
|
|
2907
|
+
} else {
|
|
2908
|
+
// Get the next highest power of 2 to prevent increasing hwm excessively in
|
|
2909
|
+
// tiny amounts
|
|
2910
|
+
n--;
|
|
2911
|
+
n |= n >>> 1;
|
|
2912
|
+
n |= n >>> 2;
|
|
2913
|
+
n |= n >>> 4;
|
|
2914
|
+
n |= n >>> 8;
|
|
2915
|
+
n |= n >>> 16;
|
|
2916
|
+
n++;
|
|
2917
|
+
}
|
|
2918
|
+
|
|
2919
|
+
return n;
|
|
2920
|
+
} // This function is designed to be inlinable, so please take care when making
|
|
2921
|
+
// changes to the function body.
|
|
2922
|
+
|
|
2923
|
+
|
|
2924
|
+
function howMuchToRead(n, state) {
|
|
2925
|
+
if (n <= 0 || state.length === 0 && state.ended) return 0;
|
|
2926
|
+
if (state.objectMode) return 1;
|
|
2927
|
+
|
|
2928
|
+
if (n !== n) {
|
|
2929
|
+
// Only flow one buffer at a time
|
|
2930
|
+
if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
|
|
2931
|
+
} // If we're asking for more than the current hwm, then raise the hwm.
|
|
2932
|
+
|
|
2933
|
+
|
|
2934
|
+
if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
|
|
2935
|
+
if (n <= state.length) return n; // Don't have enough
|
|
2936
|
+
|
|
2937
|
+
if (!state.ended) {
|
|
2938
|
+
state.needReadable = true;
|
|
2939
|
+
return 0;
|
|
2940
|
+
}
|
|
2941
|
+
|
|
2942
|
+
return state.length;
|
|
2943
|
+
} // you can override either this method, or the async _read(n) below.
|
|
2944
|
+
|
|
2945
|
+
|
|
2946
|
+
Readable.prototype.read = function (n) {
|
|
2947
|
+
debug('read', n);
|
|
2948
|
+
n = parseInt(n, 10);
|
|
2949
|
+
var state = this._readableState;
|
|
2950
|
+
var nOrig = n;
|
|
2951
|
+
if (n !== 0) state.emittedReadable = false; // if we're doing read(0) to trigger a readable event, but we
|
|
2952
|
+
// already have a bunch of data in the buffer, then just trigger
|
|
2953
|
+
// the 'readable' event and move on.
|
|
2954
|
+
|
|
2955
|
+
if (n === 0 && state.needReadable && ((state.highWaterMark !== 0 ? state.length >= state.highWaterMark : state.length > 0) || state.ended)) {
|
|
2956
|
+
debug('read: emitReadable', state.length, state.ended);
|
|
2957
|
+
if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
|
|
2958
|
+
return null;
|
|
2959
|
+
}
|
|
2960
|
+
|
|
2961
|
+
n = howMuchToRead(n, state); // if we've ended, and we're now clear, then finish it up.
|
|
2962
|
+
|
|
2963
|
+
if (n === 0 && state.ended) {
|
|
2964
|
+
if (state.length === 0) endReadable(this);
|
|
2965
|
+
return null;
|
|
2966
|
+
} // All the actual chunk generation logic needs to be
|
|
2967
|
+
// *below* the call to _read. The reason is that in certain
|
|
2968
|
+
// synthetic stream cases, such as passthrough streams, _read
|
|
2969
|
+
// may be a completely synchronous operation which may change
|
|
2970
|
+
// the state of the read buffer, providing enough data when
|
|
2971
|
+
// before there was *not* enough.
|
|
2972
|
+
//
|
|
2973
|
+
// So, the steps are:
|
|
2974
|
+
// 1. Figure out what the state of things will be after we do
|
|
2975
|
+
// a read from the buffer.
|
|
2976
|
+
//
|
|
2977
|
+
// 2. If that resulting state will trigger a _read, then call _read.
|
|
2978
|
+
// Note that this may be asynchronous, or synchronous. Yes, it is
|
|
2979
|
+
// deeply ugly to write APIs this way, but that still doesn't mean
|
|
2980
|
+
// that the Readable class should behave improperly, as streams are
|
|
2981
|
+
// designed to be sync/async agnostic.
|
|
2982
|
+
// Take note if the _read call is sync or async (ie, if the read call
|
|
2983
|
+
// has returned yet), so that we know whether or not it's safe to emit
|
|
2984
|
+
// 'readable' etc.
|
|
2985
|
+
//
|
|
2986
|
+
// 3. Actually pull the requested chunks out of the buffer and return.
|
|
2987
|
+
// if we need a readable event, then we need to do some reading.
|
|
2988
|
+
|
|
2989
|
+
|
|
2990
|
+
var doRead = state.needReadable;
|
|
2991
|
+
debug('need readable', doRead); // if we currently have less than the highWaterMark, then also read some
|
|
2992
|
+
|
|
2993
|
+
if (state.length === 0 || state.length - n < state.highWaterMark) {
|
|
2994
|
+
doRead = true;
|
|
2995
|
+
debug('length less than watermark', doRead);
|
|
2996
|
+
} // however, if we've ended, then there's no point, and if we're already
|
|
2997
|
+
// reading, then it's unnecessary.
|
|
2998
|
+
|
|
2999
|
+
|
|
3000
|
+
if (state.ended || state.reading) {
|
|
3001
|
+
doRead = false;
|
|
3002
|
+
debug('reading or ended', doRead);
|
|
3003
|
+
} else if (doRead) {
|
|
3004
|
+
debug('do read');
|
|
3005
|
+
state.reading = true;
|
|
3006
|
+
state.sync = true; // if the length is currently zero, then we *need* a readable event.
|
|
3007
|
+
|
|
3008
|
+
if (state.length === 0) state.needReadable = true; // call internal read method
|
|
3009
|
+
|
|
3010
|
+
this._read(state.highWaterMark);
|
|
3011
|
+
|
|
3012
|
+
state.sync = false; // If _read pushed data synchronously, then `reading` will be false,
|
|
3013
|
+
// and we need to re-evaluate how much data we can return to the user.
|
|
3014
|
+
|
|
3015
|
+
if (!state.reading) n = howMuchToRead(nOrig, state);
|
|
3016
|
+
}
|
|
3017
|
+
|
|
3018
|
+
var ret;
|
|
3019
|
+
if (n > 0) ret = fromList(n, state);else ret = null;
|
|
3020
|
+
|
|
3021
|
+
if (ret === null) {
|
|
3022
|
+
state.needReadable = state.length <= state.highWaterMark;
|
|
3023
|
+
n = 0;
|
|
3024
|
+
} else {
|
|
3025
|
+
state.length -= n;
|
|
3026
|
+
state.awaitDrain = 0;
|
|
3027
|
+
}
|
|
3028
|
+
|
|
3029
|
+
if (state.length === 0) {
|
|
3030
|
+
// If we have nothing in the buffer, then we want to know
|
|
3031
|
+
// as soon as we *do* get something into the buffer.
|
|
3032
|
+
if (!state.ended) state.needReadable = true; // If we tried to read() past the EOF, then emit end on the next tick.
|
|
3033
|
+
|
|
3034
|
+
if (nOrig !== n && state.ended) endReadable(this);
|
|
3035
|
+
}
|
|
3036
|
+
|
|
3037
|
+
if (ret !== null) this.emit('data', ret);
|
|
3038
|
+
return ret;
|
|
3039
|
+
};
|
|
3040
|
+
|
|
3041
|
+
function onEofChunk(stream, state) {
|
|
3042
|
+
debug('onEofChunk');
|
|
3043
|
+
if (state.ended) return;
|
|
3044
|
+
|
|
3045
|
+
if (state.decoder) {
|
|
3046
|
+
var chunk = state.decoder.end();
|
|
3047
|
+
|
|
3048
|
+
if (chunk && chunk.length) {
|
|
3049
|
+
state.buffer.push(chunk);
|
|
3050
|
+
state.length += state.objectMode ? 1 : chunk.length;
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
3053
|
+
|
|
3054
|
+
state.ended = true;
|
|
3055
|
+
|
|
3056
|
+
if (state.sync) {
|
|
3057
|
+
// if we are sync, wait until next tick to emit the data.
|
|
3058
|
+
// Otherwise we risk emitting data in the flow()
|
|
3059
|
+
// the readable code triggers during a read() call
|
|
3060
|
+
emitReadable(stream);
|
|
3061
|
+
} else {
|
|
3062
|
+
// emit 'readable' now to make sure it gets picked up.
|
|
3063
|
+
state.needReadable = false;
|
|
3064
|
+
|
|
3065
|
+
if (!state.emittedReadable) {
|
|
3066
|
+
state.emittedReadable = true;
|
|
3067
|
+
emitReadable_(stream);
|
|
3068
|
+
}
|
|
3069
|
+
}
|
|
3070
|
+
} // Don't emit readable right away in sync mode, because this can trigger
|
|
3071
|
+
// another read() call => stack overflow. This way, it might trigger
|
|
3072
|
+
// a nextTick recursion warning, but that's not so bad.
|
|
3073
|
+
|
|
3074
|
+
|
|
3075
|
+
function emitReadable(stream) {
|
|
3076
|
+
var state = stream._readableState;
|
|
3077
|
+
debug('emitReadable', state.needReadable, state.emittedReadable);
|
|
3078
|
+
state.needReadable = false;
|
|
3079
|
+
|
|
3080
|
+
if (!state.emittedReadable) {
|
|
3081
|
+
debug('emitReadable', state.flowing);
|
|
3082
|
+
state.emittedReadable = true;
|
|
3083
|
+
process.nextTick(emitReadable_, stream);
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
|
|
3087
|
+
function emitReadable_(stream) {
|
|
3088
|
+
var state = stream._readableState;
|
|
3089
|
+
debug('emitReadable_', state.destroyed, state.length, state.ended);
|
|
3090
|
+
|
|
3091
|
+
if (!state.destroyed && (state.length || state.ended)) {
|
|
3092
|
+
stream.emit('readable');
|
|
3093
|
+
state.emittedReadable = false;
|
|
3094
|
+
} // The stream needs another readable event if
|
|
3095
|
+
// 1. It is not flowing, as the flow mechanism will take
|
|
3096
|
+
// care of it.
|
|
3097
|
+
// 2. It is not ended.
|
|
3098
|
+
// 3. It is below the highWaterMark, so we can schedule
|
|
3099
|
+
// another readable later.
|
|
3100
|
+
|
|
3101
|
+
|
|
3102
|
+
state.needReadable = !state.flowing && !state.ended && state.length <= state.highWaterMark;
|
|
3103
|
+
flow(stream);
|
|
3104
|
+
} // at this point, the user has presumably seen the 'readable' event,
|
|
3105
|
+
// and called read() to consume some data. that may have triggered
|
|
3106
|
+
// in turn another _read(n) call, in which case reading = true if
|
|
3107
|
+
// it's in progress.
|
|
3108
|
+
// However, if we're not ended, or reading, and the length < hwm,
|
|
3109
|
+
// then go ahead and try to read some more preemptively.
|
|
3110
|
+
|
|
3111
|
+
|
|
3112
|
+
function maybeReadMore(stream, state) {
|
|
3113
|
+
if (!state.readingMore) {
|
|
3114
|
+
state.readingMore = true;
|
|
3115
|
+
process.nextTick(maybeReadMore_, stream, state);
|
|
3116
|
+
}
|
|
3117
|
+
}
|
|
3118
|
+
|
|
3119
|
+
function maybeReadMore_(stream, state) {
|
|
3120
|
+
// Attempt to read more data if we should.
|
|
3121
|
+
//
|
|
3122
|
+
// The conditions for reading more data are (one of):
|
|
3123
|
+
// - Not enough data buffered (state.length < state.highWaterMark). The loop
|
|
3124
|
+
// is responsible for filling the buffer with enough data if such data
|
|
3125
|
+
// is available. If highWaterMark is 0 and we are not in the flowing mode
|
|
3126
|
+
// we should _not_ attempt to buffer any extra data. We'll get more data
|
|
3127
|
+
// when the stream consumer calls read() instead.
|
|
3128
|
+
// - No data in the buffer, and the stream is in flowing mode. In this mode
|
|
3129
|
+
// the loop below is responsible for ensuring read() is called. Failing to
|
|
3130
|
+
// call read here would abort the flow and there's no other mechanism for
|
|
3131
|
+
// continuing the flow if the stream consumer has just subscribed to the
|
|
3132
|
+
// 'data' event.
|
|
3133
|
+
//
|
|
3134
|
+
// In addition to the above conditions to keep reading data, the following
|
|
3135
|
+
// conditions prevent the data from being read:
|
|
3136
|
+
// - The stream has ended (state.ended).
|
|
3137
|
+
// - There is already a pending 'read' operation (state.reading). This is a
|
|
3138
|
+
// case where the the stream has called the implementation defined _read()
|
|
3139
|
+
// method, but they are processing the call asynchronously and have _not_
|
|
3140
|
+
// called push() with new data. In this case we skip performing more
|
|
3141
|
+
// read()s. The execution ends in this method again after the _read() ends
|
|
3142
|
+
// up calling push() with more data.
|
|
3143
|
+
while (!state.reading && !state.ended && (state.length < state.highWaterMark || state.flowing && state.length === 0)) {
|
|
3144
|
+
var len = state.length;
|
|
3145
|
+
debug('maybeReadMore read 0');
|
|
3146
|
+
stream.read(0);
|
|
3147
|
+
if (len === state.length) // didn't get any data, stop spinning.
|
|
3148
|
+
break;
|
|
3149
|
+
}
|
|
3150
|
+
|
|
3151
|
+
state.readingMore = false;
|
|
3152
|
+
} // abstract method. to be overridden in specific implementation classes.
|
|
3153
|
+
// call cb(er, data) where data is <= n in length.
|
|
3154
|
+
// for virtual (non-string, non-buffer) streams, "length" is somewhat
|
|
3155
|
+
// arbitrary, and perhaps not very meaningful.
|
|
3156
|
+
|
|
3157
|
+
|
|
3158
|
+
Readable.prototype._read = function (n) {
|
|
3159
|
+
errorOrDestroy(this, new ERR_METHOD_NOT_IMPLEMENTED$1('_read()'));
|
|
3160
|
+
};
|
|
3161
|
+
|
|
3162
|
+
Readable.prototype.pipe = function (dest, pipeOpts) {
|
|
3163
|
+
var src = this;
|
|
3164
|
+
var state = this._readableState;
|
|
3165
|
+
|
|
3166
|
+
switch (state.pipesCount) {
|
|
3167
|
+
case 0:
|
|
3168
|
+
state.pipes = dest;
|
|
3169
|
+
break;
|
|
3170
|
+
|
|
3171
|
+
case 1:
|
|
3172
|
+
state.pipes = [state.pipes, dest];
|
|
3173
|
+
break;
|
|
3174
|
+
|
|
3175
|
+
default:
|
|
3176
|
+
state.pipes.push(dest);
|
|
3177
|
+
break;
|
|
3178
|
+
}
|
|
3179
|
+
|
|
3180
|
+
state.pipesCount += 1;
|
|
3181
|
+
debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
|
|
3182
|
+
var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
|
|
3183
|
+
var endFn = doEnd ? onend : unpipe;
|
|
3184
|
+
if (state.endEmitted) process.nextTick(endFn);else src.once('end', endFn);
|
|
3185
|
+
dest.on('unpipe', onunpipe);
|
|
3186
|
+
|
|
3187
|
+
function onunpipe(readable, unpipeInfo) {
|
|
3188
|
+
debug('onunpipe');
|
|
3189
|
+
|
|
3190
|
+
if (readable === src) {
|
|
3191
|
+
if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
|
|
3192
|
+
unpipeInfo.hasUnpiped = true;
|
|
3193
|
+
cleanup();
|
|
3194
|
+
}
|
|
3195
|
+
}
|
|
3196
|
+
}
|
|
3197
|
+
|
|
3198
|
+
function onend() {
|
|
3199
|
+
debug('onend');
|
|
3200
|
+
dest.end();
|
|
3201
|
+
} // when the dest drains, it reduces the awaitDrain counter
|
|
3202
|
+
// on the source. This would be more elegant with a .once()
|
|
3203
|
+
// handler in flow(), but adding and removing repeatedly is
|
|
3204
|
+
// too slow.
|
|
3205
|
+
|
|
3206
|
+
|
|
3207
|
+
var ondrain = pipeOnDrain(src);
|
|
3208
|
+
dest.on('drain', ondrain);
|
|
3209
|
+
var cleanedUp = false;
|
|
3210
|
+
|
|
3211
|
+
function cleanup() {
|
|
3212
|
+
debug('cleanup'); // cleanup event handlers once the pipe is broken
|
|
3213
|
+
|
|
3214
|
+
dest.removeListener('close', onclose);
|
|
3215
|
+
dest.removeListener('finish', onfinish);
|
|
3216
|
+
dest.removeListener('drain', ondrain);
|
|
3217
|
+
dest.removeListener('error', onerror);
|
|
3218
|
+
dest.removeListener('unpipe', onunpipe);
|
|
3219
|
+
src.removeListener('end', onend);
|
|
3220
|
+
src.removeListener('end', unpipe);
|
|
3221
|
+
src.removeListener('data', ondata);
|
|
3222
|
+
cleanedUp = true; // if the reader is waiting for a drain event from this
|
|
3223
|
+
// specific writer, then it would cause it to never start
|
|
3224
|
+
// flowing again.
|
|
3225
|
+
// So, if this is awaiting a drain, then we just call it now.
|
|
3226
|
+
// If we don't know, then assume that we are waiting for one.
|
|
3227
|
+
|
|
3228
|
+
if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
|
|
3229
|
+
}
|
|
3230
|
+
|
|
3231
|
+
src.on('data', ondata);
|
|
3232
|
+
|
|
3233
|
+
function ondata(chunk) {
|
|
3234
|
+
debug('ondata');
|
|
3235
|
+
var ret = dest.write(chunk);
|
|
3236
|
+
debug('dest.write', ret);
|
|
3237
|
+
|
|
3238
|
+
if (ret === false) {
|
|
3239
|
+
// If the user unpiped during `dest.write()`, it is possible
|
|
3240
|
+
// to get stuck in a permanently paused state if that write
|
|
3241
|
+
// also returned false.
|
|
3242
|
+
// => Check whether `dest` is still a piping destination.
|
|
3243
|
+
if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
|
|
3244
|
+
debug('false write response, pause', state.awaitDrain);
|
|
3245
|
+
state.awaitDrain++;
|
|
3246
|
+
}
|
|
3247
|
+
|
|
3248
|
+
src.pause();
|
|
3249
|
+
}
|
|
3250
|
+
} // if the dest has an error, then stop piping into it.
|
|
3251
|
+
// however, don't suppress the throwing behavior for this.
|
|
3252
|
+
|
|
3253
|
+
|
|
3254
|
+
function onerror(er) {
|
|
3255
|
+
debug('onerror', er);
|
|
3256
|
+
unpipe();
|
|
3257
|
+
dest.removeListener('error', onerror);
|
|
3258
|
+
if (EElistenerCount(dest, 'error') === 0) errorOrDestroy(dest, er);
|
|
3259
|
+
} // Make sure our error handler is attached before userland ones.
|
|
3260
|
+
|
|
3261
|
+
|
|
3262
|
+
prependListener(dest, 'error', onerror); // Both close and finish should trigger unpipe, but only once.
|
|
3263
|
+
|
|
3264
|
+
function onclose() {
|
|
3265
|
+
dest.removeListener('finish', onfinish);
|
|
3266
|
+
unpipe();
|
|
3267
|
+
}
|
|
3268
|
+
|
|
3269
|
+
dest.once('close', onclose);
|
|
3270
|
+
|
|
3271
|
+
function onfinish() {
|
|
3272
|
+
debug('onfinish');
|
|
3273
|
+
dest.removeListener('close', onclose);
|
|
3274
|
+
unpipe();
|
|
3275
|
+
}
|
|
3276
|
+
|
|
3277
|
+
dest.once('finish', onfinish);
|
|
3278
|
+
|
|
3279
|
+
function unpipe() {
|
|
3280
|
+
debug('unpipe');
|
|
3281
|
+
src.unpipe(dest);
|
|
3282
|
+
} // tell the dest that it's being piped to
|
|
3283
|
+
|
|
3284
|
+
|
|
3285
|
+
dest.emit('pipe', src); // start the flow if it hasn't been started already.
|
|
3286
|
+
|
|
3287
|
+
if (!state.flowing) {
|
|
3288
|
+
debug('pipe resume');
|
|
3289
|
+
src.resume();
|
|
3290
|
+
}
|
|
3291
|
+
|
|
3292
|
+
return dest;
|
|
3293
|
+
};
|
|
3294
|
+
|
|
3295
|
+
function pipeOnDrain(src) {
|
|
3296
|
+
return function pipeOnDrainFunctionResult() {
|
|
3297
|
+
var state = src._readableState;
|
|
3298
|
+
debug('pipeOnDrain', state.awaitDrain);
|
|
3299
|
+
if (state.awaitDrain) state.awaitDrain--;
|
|
3300
|
+
|
|
3301
|
+
if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
|
|
3302
|
+
state.flowing = true;
|
|
3303
|
+
flow(src);
|
|
3304
|
+
}
|
|
3305
|
+
};
|
|
3306
|
+
}
|
|
3307
|
+
|
|
3308
|
+
Readable.prototype.unpipe = function (dest) {
|
|
3309
|
+
var state = this._readableState;
|
|
3310
|
+
var unpipeInfo = {
|
|
3311
|
+
hasUnpiped: false
|
|
3312
|
+
}; // if we're not piping anywhere, then do nothing.
|
|
3313
|
+
|
|
3314
|
+
if (state.pipesCount === 0) return this; // just one destination. most common case.
|
|
3315
|
+
|
|
3316
|
+
if (state.pipesCount === 1) {
|
|
3317
|
+
// passed in one, but it's not the right one.
|
|
3318
|
+
if (dest && dest !== state.pipes) return this;
|
|
3319
|
+
if (!dest) dest = state.pipes; // got a match.
|
|
3320
|
+
|
|
3321
|
+
state.pipes = null;
|
|
3322
|
+
state.pipesCount = 0;
|
|
3323
|
+
state.flowing = false;
|
|
3324
|
+
if (dest) dest.emit('unpipe', this, unpipeInfo);
|
|
3325
|
+
return this;
|
|
3326
|
+
} // slow case. multiple pipe destinations.
|
|
3327
|
+
|
|
3328
|
+
|
|
3329
|
+
if (!dest) {
|
|
3330
|
+
// remove all.
|
|
3331
|
+
var dests = state.pipes;
|
|
3332
|
+
var len = state.pipesCount;
|
|
3333
|
+
state.pipes = null;
|
|
3334
|
+
state.pipesCount = 0;
|
|
3335
|
+
state.flowing = false;
|
|
3336
|
+
|
|
3337
|
+
for (var i = 0; i < len; i++) {
|
|
3338
|
+
dests[i].emit('unpipe', this, {
|
|
3339
|
+
hasUnpiped: false
|
|
3340
|
+
});
|
|
3341
|
+
}
|
|
3342
|
+
|
|
3343
|
+
return this;
|
|
3344
|
+
} // try to find the right one.
|
|
3345
|
+
|
|
3346
|
+
|
|
3347
|
+
var index = indexOf(state.pipes, dest);
|
|
3348
|
+
if (index === -1) return this;
|
|
3349
|
+
state.pipes.splice(index, 1);
|
|
3350
|
+
state.pipesCount -= 1;
|
|
3351
|
+
if (state.pipesCount === 1) state.pipes = state.pipes[0];
|
|
3352
|
+
dest.emit('unpipe', this, unpipeInfo);
|
|
3353
|
+
return this;
|
|
3354
|
+
}; // set up data events if they are asked for
|
|
3355
|
+
// Ensure readable listeners eventually get something
|
|
3356
|
+
|
|
3357
|
+
|
|
3358
|
+
Readable.prototype.on = function (ev, fn) {
|
|
3359
|
+
var res = stream.prototype.on.call(this, ev, fn);
|
|
3360
|
+
var state = this._readableState;
|
|
3361
|
+
|
|
3362
|
+
if (ev === 'data') {
|
|
3363
|
+
// update readableListening so that resume() may be a no-op
|
|
3364
|
+
// a few lines down. This is needed to support once('readable').
|
|
3365
|
+
state.readableListening = this.listenerCount('readable') > 0; // Try start flowing on next tick if stream isn't explicitly paused
|
|
3366
|
+
|
|
3367
|
+
if (state.flowing !== false) this.resume();
|
|
3368
|
+
} else if (ev === 'readable') {
|
|
3369
|
+
if (!state.endEmitted && !state.readableListening) {
|
|
3370
|
+
state.readableListening = state.needReadable = true;
|
|
3371
|
+
state.flowing = false;
|
|
3372
|
+
state.emittedReadable = false;
|
|
3373
|
+
debug('on readable', state.length, state.reading);
|
|
3374
|
+
|
|
3375
|
+
if (state.length) {
|
|
3376
|
+
emitReadable(this);
|
|
3377
|
+
} else if (!state.reading) {
|
|
3378
|
+
process.nextTick(nReadingNextTick, this);
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3381
|
+
}
|
|
3382
|
+
|
|
3383
|
+
return res;
|
|
3384
|
+
};
|
|
3385
|
+
|
|
3386
|
+
Readable.prototype.addListener = Readable.prototype.on;
|
|
3387
|
+
|
|
3388
|
+
Readable.prototype.removeListener = function (ev, fn) {
|
|
3389
|
+
var res = stream.prototype.removeListener.call(this, ev, fn);
|
|
3390
|
+
|
|
3391
|
+
if (ev === 'readable') {
|
|
3392
|
+
// We need to check if there is someone still listening to
|
|
3393
|
+
// readable and reset the state. However this needs to happen
|
|
3394
|
+
// after readable has been emitted but before I/O (nextTick) to
|
|
3395
|
+
// support once('readable', fn) cycles. This means that calling
|
|
3396
|
+
// resume within the same tick will have no
|
|
3397
|
+
// effect.
|
|
3398
|
+
process.nextTick(updateReadableListening, this);
|
|
3399
|
+
}
|
|
3400
|
+
|
|
3401
|
+
return res;
|
|
3402
|
+
};
|
|
3403
|
+
|
|
3404
|
+
Readable.prototype.removeAllListeners = function (ev) {
|
|
3405
|
+
var res = stream.prototype.removeAllListeners.apply(this, arguments);
|
|
3406
|
+
|
|
3407
|
+
if (ev === 'readable' || ev === undefined) {
|
|
3408
|
+
// We need to check if there is someone still listening to
|
|
3409
|
+
// readable and reset the state. However this needs to happen
|
|
3410
|
+
// after readable has been emitted but before I/O (nextTick) to
|
|
3411
|
+
// support once('readable', fn) cycles. This means that calling
|
|
3412
|
+
// resume within the same tick will have no
|
|
3413
|
+
// effect.
|
|
3414
|
+
process.nextTick(updateReadableListening, this);
|
|
3415
|
+
}
|
|
3416
|
+
|
|
3417
|
+
return res;
|
|
3418
|
+
};
|
|
3419
|
+
|
|
3420
|
+
function updateReadableListening(self) {
|
|
3421
|
+
var state = self._readableState;
|
|
3422
|
+
state.readableListening = self.listenerCount('readable') > 0;
|
|
3423
|
+
|
|
3424
|
+
if (state.resumeScheduled && !state.paused) {
|
|
3425
|
+
// flowing needs to be set to true now, otherwise
|
|
3426
|
+
// the upcoming resume will not flow.
|
|
3427
|
+
state.flowing = true; // crude way to check if we should resume
|
|
3428
|
+
} else if (self.listenerCount('data') > 0) {
|
|
3429
|
+
self.resume();
|
|
3430
|
+
}
|
|
3431
|
+
}
|
|
3432
|
+
|
|
3433
|
+
function nReadingNextTick(self) {
|
|
3434
|
+
debug('readable nexttick read 0');
|
|
3435
|
+
self.read(0);
|
|
3436
|
+
} // pause() and resume() are remnants of the legacy readable stream API
|
|
3437
|
+
// If the user uses them, then switch into old mode.
|
|
3438
|
+
|
|
3439
|
+
|
|
3440
|
+
Readable.prototype.resume = function () {
|
|
3441
|
+
var state = this._readableState;
|
|
3442
|
+
|
|
3443
|
+
if (!state.flowing) {
|
|
3444
|
+
debug('resume'); // we flow only if there is no one listening
|
|
3445
|
+
// for readable, but we still have to call
|
|
3446
|
+
// resume()
|
|
3447
|
+
|
|
3448
|
+
state.flowing = !state.readableListening;
|
|
3449
|
+
resume(this, state);
|
|
3450
|
+
}
|
|
3451
|
+
|
|
3452
|
+
state.paused = false;
|
|
3453
|
+
return this;
|
|
3454
|
+
};
|
|
3455
|
+
|
|
3456
|
+
function resume(stream, state) {
|
|
3457
|
+
if (!state.resumeScheduled) {
|
|
3458
|
+
state.resumeScheduled = true;
|
|
3459
|
+
process.nextTick(resume_, stream, state);
|
|
3460
|
+
}
|
|
3461
|
+
}
|
|
3462
|
+
|
|
3463
|
+
function resume_(stream, state) {
|
|
3464
|
+
debug('resume', state.reading);
|
|
3465
|
+
|
|
3466
|
+
if (!state.reading) {
|
|
3467
|
+
stream.read(0);
|
|
3468
|
+
}
|
|
3469
|
+
|
|
3470
|
+
state.resumeScheduled = false;
|
|
3471
|
+
stream.emit('resume');
|
|
3472
|
+
flow(stream);
|
|
3473
|
+
if (state.flowing && !state.reading) stream.read(0);
|
|
3474
|
+
}
|
|
3475
|
+
|
|
3476
|
+
Readable.prototype.pause = function () {
|
|
3477
|
+
debug('call pause flowing=%j', this._readableState.flowing);
|
|
3478
|
+
|
|
3479
|
+
if (this._readableState.flowing !== false) {
|
|
3480
|
+
debug('pause');
|
|
3481
|
+
this._readableState.flowing = false;
|
|
3482
|
+
this.emit('pause');
|
|
3483
|
+
}
|
|
3484
|
+
|
|
3485
|
+
this._readableState.paused = true;
|
|
3486
|
+
return this;
|
|
3487
|
+
};
|
|
3488
|
+
|
|
3489
|
+
function flow(stream) {
|
|
3490
|
+
var state = stream._readableState;
|
|
3491
|
+
debug('flow', state.flowing);
|
|
3492
|
+
|
|
3493
|
+
while (state.flowing && stream.read() !== null) {
|
|
3494
|
+
}
|
|
3495
|
+
} // wrap an old-style stream as the async data source.
|
|
3496
|
+
// This is *not* part of the readable stream interface.
|
|
3497
|
+
// It is an ugly unfortunate mess of history.
|
|
3498
|
+
|
|
3499
|
+
|
|
3500
|
+
Readable.prototype.wrap = function (stream) {
|
|
3501
|
+
var _this = this;
|
|
3502
|
+
|
|
3503
|
+
var state = this._readableState;
|
|
3504
|
+
var paused = false;
|
|
3505
|
+
stream.on('end', function () {
|
|
3506
|
+
debug('wrapped end');
|
|
3507
|
+
|
|
3508
|
+
if (state.decoder && !state.ended) {
|
|
3509
|
+
var chunk = state.decoder.end();
|
|
3510
|
+
if (chunk && chunk.length) _this.push(chunk);
|
|
3511
|
+
}
|
|
3512
|
+
|
|
3513
|
+
_this.push(null);
|
|
3514
|
+
});
|
|
3515
|
+
stream.on('data', function (chunk) {
|
|
3516
|
+
debug('wrapped data');
|
|
3517
|
+
if (state.decoder) chunk = state.decoder.write(chunk); // don't skip over falsy values in objectMode
|
|
3518
|
+
|
|
3519
|
+
if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
|
|
3520
|
+
|
|
3521
|
+
var ret = _this.push(chunk);
|
|
3522
|
+
|
|
3523
|
+
if (!ret) {
|
|
3524
|
+
paused = true;
|
|
3525
|
+
stream.pause();
|
|
3526
|
+
}
|
|
3527
|
+
}); // proxy all the other methods.
|
|
3528
|
+
// important when wrapping filters and duplexes.
|
|
3529
|
+
|
|
3530
|
+
for (var i in stream) {
|
|
3531
|
+
if (this[i] === undefined && typeof stream[i] === 'function') {
|
|
3532
|
+
this[i] = function methodWrap(method) {
|
|
3533
|
+
return function methodWrapReturnFunction() {
|
|
3534
|
+
return stream[method].apply(stream, arguments);
|
|
3535
|
+
};
|
|
3536
|
+
}(i);
|
|
3537
|
+
}
|
|
3538
|
+
} // proxy certain important events.
|
|
3539
|
+
|
|
3540
|
+
|
|
3541
|
+
for (var n = 0; n < kProxyEvents.length; n++) {
|
|
3542
|
+
stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n]));
|
|
3543
|
+
} // when we try to consume some more bytes, simply unpause the
|
|
3544
|
+
// underlying stream.
|
|
3545
|
+
|
|
3546
|
+
|
|
3547
|
+
this._read = function (n) {
|
|
3548
|
+
debug('wrapped _read', n);
|
|
3549
|
+
|
|
3550
|
+
if (paused) {
|
|
3551
|
+
paused = false;
|
|
3552
|
+
stream.resume();
|
|
3553
|
+
}
|
|
3554
|
+
};
|
|
3555
|
+
|
|
3556
|
+
return this;
|
|
3557
|
+
};
|
|
3558
|
+
|
|
3559
|
+
if (typeof Symbol === 'function') {
|
|
3560
|
+
Readable.prototype[Symbol.asyncIterator] = function () {
|
|
3561
|
+
if (createReadableStreamAsyncIterator === undefined) {
|
|
3562
|
+
createReadableStreamAsyncIterator = async_iterator;
|
|
3563
|
+
}
|
|
3564
|
+
|
|
3565
|
+
return createReadableStreamAsyncIterator(this);
|
|
3566
|
+
};
|
|
3567
|
+
}
|
|
3568
|
+
|
|
3569
|
+
Object.defineProperty(Readable.prototype, 'readableHighWaterMark', {
|
|
3570
|
+
// making it explicit this property is not enumerable
|
|
3571
|
+
// because otherwise some prototype manipulation in
|
|
3572
|
+
// userland will fail
|
|
3573
|
+
enumerable: false,
|
|
3574
|
+
get: function get() {
|
|
3575
|
+
return this._readableState.highWaterMark;
|
|
3576
|
+
}
|
|
3577
|
+
});
|
|
3578
|
+
Object.defineProperty(Readable.prototype, 'readableBuffer', {
|
|
3579
|
+
// making it explicit this property is not enumerable
|
|
3580
|
+
// because otherwise some prototype manipulation in
|
|
3581
|
+
// userland will fail
|
|
3582
|
+
enumerable: false,
|
|
3583
|
+
get: function get() {
|
|
3584
|
+
return this._readableState && this._readableState.buffer;
|
|
3585
|
+
}
|
|
3586
|
+
});
|
|
3587
|
+
Object.defineProperty(Readable.prototype, 'readableFlowing', {
|
|
3588
|
+
// making it explicit this property is not enumerable
|
|
3589
|
+
// because otherwise some prototype manipulation in
|
|
3590
|
+
// userland will fail
|
|
3591
|
+
enumerable: false,
|
|
3592
|
+
get: function get() {
|
|
3593
|
+
return this._readableState.flowing;
|
|
3594
|
+
},
|
|
3595
|
+
set: function set(state) {
|
|
3596
|
+
if (this._readableState) {
|
|
3597
|
+
this._readableState.flowing = state;
|
|
3598
|
+
}
|
|
3599
|
+
}
|
|
3600
|
+
}); // exposed for testing purposes only.
|
|
3601
|
+
|
|
3602
|
+
Readable._fromList = fromList;
|
|
3603
|
+
Object.defineProperty(Readable.prototype, 'readableLength', {
|
|
3604
|
+
// making it explicit this property is not enumerable
|
|
3605
|
+
// because otherwise some prototype manipulation in
|
|
3606
|
+
// userland will fail
|
|
3607
|
+
enumerable: false,
|
|
3608
|
+
get: function get() {
|
|
3609
|
+
return this._readableState.length;
|
|
3610
|
+
}
|
|
3611
|
+
}); // Pluck off n bytes from an array of buffers.
|
|
3612
|
+
// Length is the combined lengths of all the buffers in the list.
|
|
3613
|
+
// This function is designed to be inlinable, so please take care when making
|
|
3614
|
+
// changes to the function body.
|
|
3615
|
+
|
|
3616
|
+
function fromList(n, state) {
|
|
3617
|
+
// nothing buffered
|
|
3618
|
+
if (state.length === 0) return null;
|
|
3619
|
+
var ret;
|
|
3620
|
+
if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
|
|
3621
|
+
// read it all, truncate the list
|
|
3622
|
+
if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.first();else ret = state.buffer.concat(state.length);
|
|
3623
|
+
state.buffer.clear();
|
|
3624
|
+
} else {
|
|
3625
|
+
// read part of list
|
|
3626
|
+
ret = state.buffer.consume(n, state.decoder);
|
|
3627
|
+
}
|
|
3628
|
+
return ret;
|
|
3629
|
+
}
|
|
3630
|
+
|
|
3631
|
+
function endReadable(stream) {
|
|
3632
|
+
var state = stream._readableState;
|
|
3633
|
+
debug('endReadable', state.endEmitted);
|
|
3634
|
+
|
|
3635
|
+
if (!state.endEmitted) {
|
|
3636
|
+
state.ended = true;
|
|
3637
|
+
process.nextTick(endReadableNT, state, stream);
|
|
3638
|
+
}
|
|
3639
|
+
}
|
|
3640
|
+
|
|
3641
|
+
function endReadableNT(state, stream) {
|
|
3642
|
+
debug('endReadableNT', state.endEmitted, state.length); // Check that we didn't get one last unshift.
|
|
3643
|
+
|
|
3644
|
+
if (!state.endEmitted && state.length === 0) {
|
|
3645
|
+
state.endEmitted = true;
|
|
3646
|
+
stream.readable = false;
|
|
3647
|
+
stream.emit('end');
|
|
3648
|
+
|
|
3649
|
+
if (state.autoDestroy) {
|
|
3650
|
+
// In case of duplex streams we need a way to detect
|
|
3651
|
+
// if the writable side is ready for autoDestroy as well
|
|
3652
|
+
var wState = stream._writableState;
|
|
3653
|
+
|
|
3654
|
+
if (!wState || wState.autoDestroy && wState.finished) {
|
|
3655
|
+
stream.destroy();
|
|
3656
|
+
}
|
|
3657
|
+
}
|
|
3658
|
+
}
|
|
3659
|
+
}
|
|
3660
|
+
|
|
3661
|
+
if (typeof Symbol === 'function') {
|
|
3662
|
+
Readable.from = function (iterable, opts) {
|
|
3663
|
+
if (from === undefined) {
|
|
3664
|
+
from = from_1;
|
|
3665
|
+
}
|
|
3666
|
+
|
|
3667
|
+
return from(Readable, iterable, opts);
|
|
3668
|
+
};
|
|
3669
|
+
}
|
|
3670
|
+
|
|
3671
|
+
function indexOf(xs, x) {
|
|
3672
|
+
for (var i = 0, l = xs.length; i < l; i++) {
|
|
3673
|
+
if (xs[i] === x) return i;
|
|
3674
|
+
}
|
|
3675
|
+
|
|
3676
|
+
return -1;
|
|
3677
|
+
}
|
|
3678
|
+
|
|
3679
|
+
var _stream_transform = Transform$2;
|
|
3680
|
+
|
|
3681
|
+
var _require$codes$1 = errors.codes,
|
|
3682
|
+
ERR_METHOD_NOT_IMPLEMENTED = _require$codes$1.ERR_METHOD_NOT_IMPLEMENTED,
|
|
3683
|
+
ERR_MULTIPLE_CALLBACK = _require$codes$1.ERR_MULTIPLE_CALLBACK,
|
|
3684
|
+
ERR_TRANSFORM_ALREADY_TRANSFORMING = _require$codes$1.ERR_TRANSFORM_ALREADY_TRANSFORMING,
|
|
3685
|
+
ERR_TRANSFORM_WITH_LENGTH_0 = _require$codes$1.ERR_TRANSFORM_WITH_LENGTH_0;
|
|
3686
|
+
|
|
3687
|
+
|
|
3688
|
+
|
|
3689
|
+
inherits(Transform$2, require$$2);
|
|
3690
|
+
|
|
3691
|
+
function afterTransform(er, data) {
|
|
3692
|
+
var ts = this._transformState;
|
|
3693
|
+
ts.transforming = false;
|
|
3694
|
+
var cb = ts.writecb;
|
|
3695
|
+
|
|
3696
|
+
if (cb === null) {
|
|
3697
|
+
return this.emit('error', new ERR_MULTIPLE_CALLBACK());
|
|
3698
|
+
}
|
|
3699
|
+
|
|
3700
|
+
ts.writechunk = null;
|
|
3701
|
+
ts.writecb = null;
|
|
3702
|
+
if (data != null) // single equals check for both `null` and `undefined`
|
|
3703
|
+
this.push(data);
|
|
3704
|
+
cb(er);
|
|
3705
|
+
var rs = this._readableState;
|
|
3706
|
+
rs.reading = false;
|
|
3707
|
+
|
|
3708
|
+
if (rs.needReadable || rs.length < rs.highWaterMark) {
|
|
3709
|
+
this._read(rs.highWaterMark);
|
|
3710
|
+
}
|
|
3711
|
+
}
|
|
3712
|
+
|
|
3713
|
+
function Transform$2(options) {
|
|
3714
|
+
if (!(this instanceof Transform$2)) return new Transform$2(options);
|
|
3715
|
+
require$$2.call(this, options);
|
|
3716
|
+
this._transformState = {
|
|
3717
|
+
afterTransform: afterTransform.bind(this),
|
|
3718
|
+
needTransform: false,
|
|
3719
|
+
transforming: false,
|
|
3720
|
+
writecb: null,
|
|
3721
|
+
writechunk: null,
|
|
3722
|
+
writeencoding: null
|
|
3723
|
+
}; // start out asking for a readable event once data is transformed.
|
|
3724
|
+
|
|
3725
|
+
this._readableState.needReadable = true; // we have implemented the _read method, and done the other things
|
|
3726
|
+
// that Readable wants before the first _read call, so unset the
|
|
3727
|
+
// sync guard flag.
|
|
3728
|
+
|
|
3729
|
+
this._readableState.sync = false;
|
|
3730
|
+
|
|
3731
|
+
if (options) {
|
|
3732
|
+
if (typeof options.transform === 'function') this._transform = options.transform;
|
|
3733
|
+
if (typeof options.flush === 'function') this._flush = options.flush;
|
|
3734
|
+
} // When the writable side finishes, then flush out anything remaining.
|
|
3735
|
+
|
|
3736
|
+
|
|
3737
|
+
this.on('prefinish', prefinish);
|
|
3738
|
+
}
|
|
3739
|
+
|
|
3740
|
+
function prefinish() {
|
|
3741
|
+
var _this = this;
|
|
3742
|
+
|
|
3743
|
+
if (typeof this._flush === 'function' && !this._readableState.destroyed) {
|
|
3744
|
+
this._flush(function (er, data) {
|
|
3745
|
+
done(_this, er, data);
|
|
3746
|
+
});
|
|
3747
|
+
} else {
|
|
3748
|
+
done(this, null, null);
|
|
3749
|
+
}
|
|
3750
|
+
}
|
|
3751
|
+
|
|
3752
|
+
Transform$2.prototype.push = function (chunk, encoding) {
|
|
3753
|
+
this._transformState.needTransform = false;
|
|
3754
|
+
return require$$2.prototype.push.call(this, chunk, encoding);
|
|
3755
|
+
}; // This is the part where you do stuff!
|
|
3756
|
+
// override this function in implementation classes.
|
|
3757
|
+
// 'chunk' is an input chunk.
|
|
3758
|
+
//
|
|
3759
|
+
// Call `push(newChunk)` to pass along transformed output
|
|
3760
|
+
// to the readable side. You may call 'push' zero or more times.
|
|
3761
|
+
//
|
|
3762
|
+
// Call `cb(err)` when you are done with this chunk. If you pass
|
|
3763
|
+
// an error, then that'll put the hurt on the whole operation. If you
|
|
3764
|
+
// never call cb(), then you'll never get another chunk.
|
|
3765
|
+
|
|
3766
|
+
|
|
3767
|
+
Transform$2.prototype._transform = function (chunk, encoding, cb) {
|
|
3768
|
+
cb(new ERR_METHOD_NOT_IMPLEMENTED('_transform()'));
|
|
3769
|
+
};
|
|
3770
|
+
|
|
3771
|
+
Transform$2.prototype._write = function (chunk, encoding, cb) {
|
|
3772
|
+
var ts = this._transformState;
|
|
3773
|
+
ts.writecb = cb;
|
|
3774
|
+
ts.writechunk = chunk;
|
|
3775
|
+
ts.writeencoding = encoding;
|
|
3776
|
+
|
|
3777
|
+
if (!ts.transforming) {
|
|
3778
|
+
var rs = this._readableState;
|
|
3779
|
+
if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
|
|
3780
|
+
}
|
|
3781
|
+
}; // Doesn't matter what the args are here.
|
|
3782
|
+
// _transform does all the work.
|
|
3783
|
+
// That we got here means that the readable side wants more data.
|
|
3784
|
+
|
|
3785
|
+
|
|
3786
|
+
Transform$2.prototype._read = function (n) {
|
|
3787
|
+
var ts = this._transformState;
|
|
3788
|
+
|
|
3789
|
+
if (ts.writechunk !== null && !ts.transforming) {
|
|
3790
|
+
ts.transforming = true;
|
|
3791
|
+
|
|
3792
|
+
this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
|
|
3793
|
+
} else {
|
|
3794
|
+
// mark that we need a transform, so that any data that comes in
|
|
3795
|
+
// will get processed, now that we've asked for it.
|
|
3796
|
+
ts.needTransform = true;
|
|
3797
|
+
}
|
|
3798
|
+
};
|
|
3799
|
+
|
|
3800
|
+
Transform$2.prototype._destroy = function (err, cb) {
|
|
3801
|
+
require$$2.prototype._destroy.call(this, err, function (err2) {
|
|
3802
|
+
cb(err2);
|
|
3803
|
+
});
|
|
3804
|
+
};
|
|
3805
|
+
|
|
3806
|
+
function done(stream, er, data) {
|
|
3807
|
+
if (er) return stream.emit('error', er);
|
|
3808
|
+
if (data != null) // single equals check for both `null` and `undefined`
|
|
3809
|
+
stream.push(data); // TODO(BridgeAR): Write a test for these two error cases
|
|
3810
|
+
// if there's nothing in the write buffer, then that means
|
|
3811
|
+
// that nothing more will ever be provided
|
|
3812
|
+
|
|
3813
|
+
if (stream._writableState.length) throw new ERR_TRANSFORM_WITH_LENGTH_0();
|
|
3814
|
+
if (stream._transformState.transforming) throw new ERR_TRANSFORM_ALREADY_TRANSFORMING();
|
|
3815
|
+
return stream.push(null);
|
|
3816
|
+
}
|
|
3817
|
+
|
|
3818
|
+
var _stream_passthrough = PassThrough;
|
|
3819
|
+
|
|
3820
|
+
|
|
3821
|
+
|
|
3822
|
+
inherits(PassThrough, _stream_transform);
|
|
3823
|
+
|
|
3824
|
+
function PassThrough(options) {
|
|
3825
|
+
if (!(this instanceof PassThrough)) return new PassThrough(options);
|
|
3826
|
+
_stream_transform.call(this, options);
|
|
3827
|
+
}
|
|
3828
|
+
|
|
3829
|
+
PassThrough.prototype._transform = function (chunk, encoding, cb) {
|
|
3830
|
+
cb(null, chunk);
|
|
3831
|
+
};
|
|
3832
|
+
|
|
3833
|
+
var eos;
|
|
3834
|
+
|
|
3835
|
+
function once(callback) {
|
|
3836
|
+
var called = false;
|
|
3837
|
+
return function () {
|
|
3838
|
+
if (called) return;
|
|
3839
|
+
called = true;
|
|
3840
|
+
callback.apply(void 0, arguments);
|
|
3841
|
+
};
|
|
3842
|
+
}
|
|
3843
|
+
|
|
3844
|
+
var _require$codes = errors.codes,
|
|
3845
|
+
ERR_MISSING_ARGS = _require$codes.ERR_MISSING_ARGS,
|
|
3846
|
+
ERR_STREAM_DESTROYED = _require$codes.ERR_STREAM_DESTROYED;
|
|
3847
|
+
|
|
3848
|
+
function noop(err) {
|
|
3849
|
+
// Rethrow the error if it exists to avoid swallowing it
|
|
3850
|
+
if (err) throw err;
|
|
3851
|
+
}
|
|
3852
|
+
|
|
3853
|
+
function isRequest(stream) {
|
|
3854
|
+
return stream.setHeader && typeof stream.abort === 'function';
|
|
3855
|
+
}
|
|
3856
|
+
|
|
3857
|
+
function destroyer(stream, reading, writing, callback) {
|
|
3858
|
+
callback = once(callback);
|
|
3859
|
+
var closed = false;
|
|
3860
|
+
stream.on('close', function () {
|
|
3861
|
+
closed = true;
|
|
3862
|
+
});
|
|
3863
|
+
if (eos === undefined) eos = endOfStream;
|
|
3864
|
+
eos(stream, {
|
|
3865
|
+
readable: reading,
|
|
3866
|
+
writable: writing
|
|
3867
|
+
}, function (err) {
|
|
3868
|
+
if (err) return callback(err);
|
|
3869
|
+
closed = true;
|
|
3870
|
+
callback();
|
|
3871
|
+
});
|
|
3872
|
+
var destroyed = false;
|
|
3873
|
+
return function (err) {
|
|
3874
|
+
if (closed) return;
|
|
3875
|
+
if (destroyed) return;
|
|
3876
|
+
destroyed = true; // request.destroy just do .end - .abort is what we want
|
|
3877
|
+
|
|
3878
|
+
if (isRequest(stream)) return stream.abort();
|
|
3879
|
+
if (typeof stream.destroy === 'function') return stream.destroy();
|
|
3880
|
+
callback(err || new ERR_STREAM_DESTROYED('pipe'));
|
|
3881
|
+
};
|
|
3882
|
+
}
|
|
3883
|
+
|
|
3884
|
+
function call(fn) {
|
|
3885
|
+
fn();
|
|
3886
|
+
}
|
|
3887
|
+
|
|
3888
|
+
function pipe(from, to) {
|
|
3889
|
+
return from.pipe(to);
|
|
3890
|
+
}
|
|
3891
|
+
|
|
3892
|
+
function popCallback(streams) {
|
|
3893
|
+
if (!streams.length) return noop;
|
|
3894
|
+
if (typeof streams[streams.length - 1] !== 'function') return noop;
|
|
3895
|
+
return streams.pop();
|
|
3896
|
+
}
|
|
3897
|
+
|
|
3898
|
+
function pipeline() {
|
|
3899
|
+
for (var _len = arguments.length, streams = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
3900
|
+
streams[_key] = arguments[_key];
|
|
3901
|
+
}
|
|
3902
|
+
|
|
3903
|
+
var callback = popCallback(streams);
|
|
3904
|
+
if (Array.isArray(streams[0])) streams = streams[0];
|
|
3905
|
+
|
|
3906
|
+
if (streams.length < 2) {
|
|
3907
|
+
throw new ERR_MISSING_ARGS('streams');
|
|
3908
|
+
}
|
|
3909
|
+
|
|
3910
|
+
var error;
|
|
3911
|
+
var destroys = streams.map(function (stream, i) {
|
|
3912
|
+
var reading = i < streams.length - 1;
|
|
3913
|
+
var writing = i > 0;
|
|
3914
|
+
return destroyer(stream, reading, writing, function (err) {
|
|
3915
|
+
if (!error) error = err;
|
|
3916
|
+
if (err) destroys.forEach(call);
|
|
3917
|
+
if (reading) return;
|
|
3918
|
+
destroys.forEach(call);
|
|
3919
|
+
callback(error);
|
|
3920
|
+
});
|
|
3921
|
+
});
|
|
3922
|
+
return streams.reduce(pipe);
|
|
3923
|
+
}
|
|
3924
|
+
|
|
3925
|
+
var pipeline_1 = pipeline;
|
|
3926
|
+
|
|
3927
|
+
var readable = createCommonjsModule(function (module, exports) {
|
|
3928
|
+
if (process.env.READABLE_STREAM === 'disable' && stream$1) {
|
|
3929
|
+
module.exports = stream$1.Readable;
|
|
3930
|
+
Object.assign(module.exports, stream$1);
|
|
3931
|
+
module.exports.Stream = stream$1;
|
|
3932
|
+
} else {
|
|
3933
|
+
exports = module.exports = require$$0;
|
|
3934
|
+
exports.Stream = stream$1 || exports;
|
|
3935
|
+
exports.Readable = exports;
|
|
3936
|
+
exports.Writable = _stream_writable;
|
|
3937
|
+
exports.Duplex = require$$2;
|
|
3938
|
+
exports.Transform = _stream_transform;
|
|
3939
|
+
exports.PassThrough = _stream_passthrough;
|
|
3940
|
+
exports.finished = endOfStream;
|
|
3941
|
+
exports.pipeline = pipeline_1;
|
|
3942
|
+
}
|
|
3943
|
+
});
|
|
3944
|
+
readable.Stream;
|
|
3945
|
+
readable.Readable;
|
|
3946
|
+
readable.Writable;
|
|
3947
|
+
readable.Duplex;
|
|
3948
|
+
readable.Transform;
|
|
3949
|
+
readable.PassThrough;
|
|
3950
|
+
readable.finished;
|
|
3951
|
+
readable.pipeline;
|
|
3952
|
+
|
|
3953
|
+
const { Transform: Transform$1 } = readable;
|
|
3954
|
+
|
|
3955
|
+
var keccak$2 = (KeccakState) => class Keccak extends Transform$1 {
|
|
3956
|
+
constructor (rate, capacity, delimitedSuffix, hashBitLength, options) {
|
|
3957
|
+
super(options);
|
|
3958
|
+
|
|
3959
|
+
this._rate = rate;
|
|
3960
|
+
this._capacity = capacity;
|
|
3961
|
+
this._delimitedSuffix = delimitedSuffix;
|
|
3962
|
+
this._hashBitLength = hashBitLength;
|
|
3963
|
+
this._options = options;
|
|
3964
|
+
|
|
3965
|
+
this._state = new KeccakState();
|
|
3966
|
+
this._state.initialize(rate, capacity);
|
|
3967
|
+
this._finalized = false;
|
|
3968
|
+
}
|
|
3969
|
+
|
|
3970
|
+
_transform (chunk, encoding, callback) {
|
|
3971
|
+
let error = null;
|
|
3972
|
+
try {
|
|
3973
|
+
this.update(chunk, encoding);
|
|
3974
|
+
} catch (err) {
|
|
3975
|
+
error = err;
|
|
3976
|
+
}
|
|
3977
|
+
|
|
3978
|
+
callback(error);
|
|
3979
|
+
}
|
|
3980
|
+
|
|
3981
|
+
_flush (callback) {
|
|
3982
|
+
let error = null;
|
|
3983
|
+
try {
|
|
3984
|
+
this.push(this.digest());
|
|
3985
|
+
} catch (err) {
|
|
3986
|
+
error = err;
|
|
3987
|
+
}
|
|
3988
|
+
|
|
3989
|
+
callback(error);
|
|
3990
|
+
}
|
|
3991
|
+
|
|
3992
|
+
update (data, encoding) {
|
|
3993
|
+
if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
|
|
3994
|
+
if (this._finalized) throw new Error('Digest already called')
|
|
3995
|
+
if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding);
|
|
3996
|
+
|
|
3997
|
+
this._state.absorb(data);
|
|
3998
|
+
|
|
3999
|
+
return this
|
|
4000
|
+
}
|
|
4001
|
+
|
|
4002
|
+
digest (encoding) {
|
|
4003
|
+
if (this._finalized) throw new Error('Digest already called')
|
|
4004
|
+
this._finalized = true;
|
|
4005
|
+
|
|
4006
|
+
if (this._delimitedSuffix) this._state.absorbLastFewBits(this._delimitedSuffix);
|
|
4007
|
+
let digest = this._state.squeeze(this._hashBitLength / 8);
|
|
4008
|
+
if (encoding !== undefined) digest = digest.toString(encoding);
|
|
4009
|
+
|
|
4010
|
+
this._resetState();
|
|
4011
|
+
|
|
4012
|
+
return digest
|
|
4013
|
+
}
|
|
4014
|
+
|
|
4015
|
+
// remove result from memory
|
|
4016
|
+
_resetState () {
|
|
4017
|
+
this._state.initialize(this._rate, this._capacity);
|
|
4018
|
+
return this
|
|
4019
|
+
}
|
|
4020
|
+
|
|
4021
|
+
// because sometimes we need hash right now and little later
|
|
4022
|
+
_clone () {
|
|
4023
|
+
const clone = new Keccak(this._rate, this._capacity, this._delimitedSuffix, this._hashBitLength, this._options);
|
|
4024
|
+
this._state.copy(clone._state);
|
|
4025
|
+
clone._finalized = this._finalized;
|
|
4026
|
+
|
|
4027
|
+
return clone
|
|
4028
|
+
}
|
|
4029
|
+
};
|
|
4030
|
+
|
|
4031
|
+
const { Transform } = readable;
|
|
4032
|
+
|
|
4033
|
+
var shake = (KeccakState) => class Shake extends Transform {
|
|
4034
|
+
constructor (rate, capacity, delimitedSuffix, options) {
|
|
4035
|
+
super(options);
|
|
4036
|
+
|
|
4037
|
+
this._rate = rate;
|
|
4038
|
+
this._capacity = capacity;
|
|
4039
|
+
this._delimitedSuffix = delimitedSuffix;
|
|
4040
|
+
this._options = options;
|
|
4041
|
+
|
|
4042
|
+
this._state = new KeccakState();
|
|
4043
|
+
this._state.initialize(rate, capacity);
|
|
4044
|
+
this._finalized = false;
|
|
4045
|
+
}
|
|
4046
|
+
|
|
4047
|
+
_transform (chunk, encoding, callback) {
|
|
4048
|
+
let error = null;
|
|
4049
|
+
try {
|
|
4050
|
+
this.update(chunk, encoding);
|
|
4051
|
+
} catch (err) {
|
|
4052
|
+
error = err;
|
|
4053
|
+
}
|
|
4054
|
+
|
|
4055
|
+
callback(error);
|
|
4056
|
+
}
|
|
4057
|
+
|
|
4058
|
+
_flush () {}
|
|
4059
|
+
|
|
4060
|
+
_read (size) {
|
|
4061
|
+
this.push(this.squeeze(size));
|
|
4062
|
+
}
|
|
4063
|
+
|
|
4064
|
+
update (data, encoding) {
|
|
4065
|
+
if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
|
|
4066
|
+
if (this._finalized) throw new Error('Squeeze already called')
|
|
4067
|
+
if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding);
|
|
4068
|
+
|
|
4069
|
+
this._state.absorb(data);
|
|
4070
|
+
|
|
4071
|
+
return this
|
|
4072
|
+
}
|
|
4073
|
+
|
|
4074
|
+
squeeze (dataByteLength, encoding) {
|
|
4075
|
+
if (!this._finalized) {
|
|
4076
|
+
this._finalized = true;
|
|
4077
|
+
this._state.absorbLastFewBits(this._delimitedSuffix);
|
|
4078
|
+
}
|
|
4079
|
+
|
|
4080
|
+
let data = this._state.squeeze(dataByteLength);
|
|
4081
|
+
if (encoding !== undefined) data = data.toString(encoding);
|
|
4082
|
+
|
|
4083
|
+
return data
|
|
4084
|
+
}
|
|
4085
|
+
|
|
4086
|
+
_resetState () {
|
|
4087
|
+
this._state.initialize(this._rate, this._capacity);
|
|
4088
|
+
return this
|
|
4089
|
+
}
|
|
4090
|
+
|
|
4091
|
+
_clone () {
|
|
4092
|
+
const clone = new Shake(this._rate, this._capacity, this._delimitedSuffix, this._options);
|
|
4093
|
+
this._state.copy(clone._state);
|
|
4094
|
+
clone._finalized = this._finalized;
|
|
4095
|
+
|
|
4096
|
+
return clone
|
|
4097
|
+
}
|
|
4098
|
+
};
|
|
4099
|
+
|
|
4100
|
+
var api = function (KeccakState) {
|
|
4101
|
+
const Keccak = keccak$2(KeccakState);
|
|
4102
|
+
const Shake = shake(KeccakState);
|
|
4103
|
+
|
|
4104
|
+
return function (algorithm, options) {
|
|
4105
|
+
const hash = typeof algorithm === 'string' ? algorithm.toLowerCase() : algorithm;
|
|
4106
|
+
switch (hash) {
|
|
4107
|
+
case 'keccak224': return new Keccak(1152, 448, null, 224, options)
|
|
4108
|
+
case 'keccak256': return new Keccak(1088, 512, null, 256, options)
|
|
4109
|
+
case 'keccak384': return new Keccak(832, 768, null, 384, options)
|
|
4110
|
+
case 'keccak512': return new Keccak(576, 1024, null, 512, options)
|
|
4111
|
+
|
|
4112
|
+
case 'sha3-224': return new Keccak(1152, 448, 0x06, 224, options)
|
|
4113
|
+
case 'sha3-256': return new Keccak(1088, 512, 0x06, 256, options)
|
|
4114
|
+
case 'sha3-384': return new Keccak(832, 768, 0x06, 384, options)
|
|
4115
|
+
case 'sha3-512': return new Keccak(576, 1024, 0x06, 512, options)
|
|
4116
|
+
|
|
4117
|
+
case 'shake128': return new Shake(1344, 256, 0x1f, options)
|
|
4118
|
+
case 'shake256': return new Shake(1088, 512, 0x1f, options)
|
|
4119
|
+
|
|
4120
|
+
default: throw new Error('Invald algorithm: ' + algorithm)
|
|
4121
|
+
}
|
|
4122
|
+
}
|
|
4123
|
+
};
|
|
4124
|
+
|
|
4125
|
+
// Workaround to fix webpack's build warnings: 'the request of a dependency is an expression'
|
|
4126
|
+
var runtimeRequire = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : commonjsRequire; // eslint-disable-line
|
|
4127
|
+
|
|
4128
|
+
var vars = (process.config && process.config.variables) || {};
|
|
4129
|
+
var prebuildsOnly = !!process.env.PREBUILDS_ONLY;
|
|
4130
|
+
var abi = process.versions.modules; // TODO: support old node where this is undef
|
|
4131
|
+
var runtime = isElectron() ? 'electron' : (isNwjs() ? 'node-webkit' : 'node');
|
|
4132
|
+
|
|
4133
|
+
var arch = os.arch();
|
|
4134
|
+
var platform = os.platform();
|
|
4135
|
+
var libc = process.env.LIBC || (isAlpine(platform) ? 'musl' : 'glibc');
|
|
4136
|
+
var armv = process.env.ARM_VERSION || (arch === 'arm64' ? '8' : vars.arm_version) || '';
|
|
4137
|
+
var uv = (process.versions.uv || '').split('.')[0];
|
|
4138
|
+
|
|
4139
|
+
var nodeGypBuild = load;
|
|
4140
|
+
|
|
4141
|
+
function load (dir) {
|
|
4142
|
+
return runtimeRequire(load.path(dir))
|
|
4143
|
+
}
|
|
4144
|
+
|
|
4145
|
+
load.path = function (dir) {
|
|
4146
|
+
dir = path.resolve(dir || '.');
|
|
4147
|
+
|
|
4148
|
+
try {
|
|
4149
|
+
var name = runtimeRequire(path.join(dir, 'package.json')).name.toUpperCase().replace(/-/g, '_');
|
|
4150
|
+
if (process.env[name + '_PREBUILD']) dir = process.env[name + '_PREBUILD'];
|
|
4151
|
+
} catch (err) {}
|
|
4152
|
+
|
|
4153
|
+
if (!prebuildsOnly) {
|
|
4154
|
+
var release = getFirst(path.join(dir, 'build/Release'), matchBuild);
|
|
4155
|
+
if (release) return release
|
|
4156
|
+
|
|
4157
|
+
var debug = getFirst(path.join(dir, 'build/Debug'), matchBuild);
|
|
4158
|
+
if (debug) return debug
|
|
4159
|
+
}
|
|
4160
|
+
|
|
4161
|
+
var prebuild = resolve(dir);
|
|
4162
|
+
if (prebuild) return prebuild
|
|
4163
|
+
|
|
4164
|
+
var nearby = resolve(path.dirname(process.execPath));
|
|
4165
|
+
if (nearby) return nearby
|
|
4166
|
+
|
|
4167
|
+
var target = [
|
|
4168
|
+
'platform=' + platform,
|
|
4169
|
+
'arch=' + arch,
|
|
4170
|
+
'runtime=' + runtime,
|
|
4171
|
+
'abi=' + abi,
|
|
4172
|
+
'uv=' + uv,
|
|
4173
|
+
armv ? 'armv=' + armv : '',
|
|
4174
|
+
'libc=' + libc,
|
|
4175
|
+
'node=' + process.versions.node,
|
|
4176
|
+
process.versions.electron ? 'electron=' + process.versions.electron : '',
|
|
4177
|
+
typeof __webpack_require__ === 'function' ? 'webpack=true' : '' // eslint-disable-line
|
|
4178
|
+
].filter(Boolean).join(' ');
|
|
4179
|
+
|
|
4180
|
+
throw new Error('No native build was found for ' + target + '\n loaded from: ' + dir + '\n')
|
|
4181
|
+
|
|
4182
|
+
function resolve (dir) {
|
|
4183
|
+
// Find matching "prebuilds/<platform>-<arch>" directory
|
|
4184
|
+
var tuples = readdirSync(path.join(dir, 'prebuilds')).map(parseTuple);
|
|
4185
|
+
var tuple = tuples.filter(matchTuple(platform, arch)).sort(compareTuples)[0];
|
|
4186
|
+
if (!tuple) return
|
|
4187
|
+
|
|
4188
|
+
// Find most specific flavor first
|
|
4189
|
+
var prebuilds = path.join(dir, 'prebuilds', tuple.name);
|
|
4190
|
+
var parsed = readdirSync(prebuilds).map(parseTags);
|
|
4191
|
+
var candidates = parsed.filter(matchTags(runtime, abi));
|
|
4192
|
+
var winner = candidates.sort(compareTags(runtime))[0];
|
|
4193
|
+
if (winner) return path.join(prebuilds, winner.file)
|
|
4194
|
+
}
|
|
4195
|
+
};
|
|
4196
|
+
|
|
4197
|
+
function readdirSync (dir) {
|
|
4198
|
+
try {
|
|
4199
|
+
return fs.readdirSync(dir)
|
|
4200
|
+
} catch (err) {
|
|
4201
|
+
return []
|
|
4202
|
+
}
|
|
4203
|
+
}
|
|
4204
|
+
|
|
4205
|
+
function getFirst (dir, filter) {
|
|
4206
|
+
var files = readdirSync(dir).filter(filter);
|
|
4207
|
+
return files[0] && path.join(dir, files[0])
|
|
4208
|
+
}
|
|
4209
|
+
|
|
4210
|
+
function matchBuild (name) {
|
|
4211
|
+
return /\.node$/.test(name)
|
|
4212
|
+
}
|
|
4213
|
+
|
|
4214
|
+
function parseTuple (name) {
|
|
4215
|
+
// Example: darwin-x64+arm64
|
|
4216
|
+
var arr = name.split('-');
|
|
4217
|
+
if (arr.length !== 2) return
|
|
4218
|
+
|
|
4219
|
+
var platform = arr[0];
|
|
4220
|
+
var architectures = arr[1].split('+');
|
|
4221
|
+
|
|
4222
|
+
if (!platform) return
|
|
4223
|
+
if (!architectures.length) return
|
|
4224
|
+
if (!architectures.every(Boolean)) return
|
|
4225
|
+
|
|
4226
|
+
return { name, platform, architectures }
|
|
4227
|
+
}
|
|
4228
|
+
|
|
4229
|
+
function matchTuple (platform, arch) {
|
|
4230
|
+
return function (tuple) {
|
|
4231
|
+
if (tuple == null) return false
|
|
4232
|
+
if (tuple.platform !== platform) return false
|
|
4233
|
+
return tuple.architectures.includes(arch)
|
|
4234
|
+
}
|
|
4235
|
+
}
|
|
4236
|
+
|
|
4237
|
+
function compareTuples (a, b) {
|
|
4238
|
+
// Prefer single-arch prebuilds over multi-arch
|
|
4239
|
+
return a.architectures.length - b.architectures.length
|
|
4240
|
+
}
|
|
4241
|
+
|
|
4242
|
+
function parseTags (file) {
|
|
4243
|
+
var arr = file.split('.');
|
|
4244
|
+
var extension = arr.pop();
|
|
4245
|
+
var tags = { file: file, specificity: 0 };
|
|
4246
|
+
|
|
4247
|
+
if (extension !== 'node') return
|
|
4248
|
+
|
|
4249
|
+
for (var i = 0; i < arr.length; i++) {
|
|
4250
|
+
var tag = arr[i];
|
|
4251
|
+
|
|
4252
|
+
if (tag === 'node' || tag === 'electron' || tag === 'node-webkit') {
|
|
4253
|
+
tags.runtime = tag;
|
|
4254
|
+
} else if (tag === 'napi') {
|
|
4255
|
+
tags.napi = true;
|
|
4256
|
+
} else if (tag.slice(0, 3) === 'abi') {
|
|
4257
|
+
tags.abi = tag.slice(3);
|
|
4258
|
+
} else if (tag.slice(0, 2) === 'uv') {
|
|
4259
|
+
tags.uv = tag.slice(2);
|
|
4260
|
+
} else if (tag.slice(0, 4) === 'armv') {
|
|
4261
|
+
tags.armv = tag.slice(4);
|
|
4262
|
+
} else if (tag === 'glibc' || tag === 'musl') {
|
|
4263
|
+
tags.libc = tag;
|
|
4264
|
+
} else {
|
|
4265
|
+
continue
|
|
4266
|
+
}
|
|
4267
|
+
|
|
4268
|
+
tags.specificity++;
|
|
4269
|
+
}
|
|
4270
|
+
|
|
4271
|
+
return tags
|
|
4272
|
+
}
|
|
4273
|
+
|
|
4274
|
+
function matchTags (runtime, abi) {
|
|
4275
|
+
return function (tags) {
|
|
4276
|
+
if (tags == null) return false
|
|
4277
|
+
if (tags.runtime !== runtime && !runtimeAgnostic(tags)) return false
|
|
4278
|
+
if (tags.abi !== abi && !tags.napi) return false
|
|
4279
|
+
if (tags.uv && tags.uv !== uv) return false
|
|
4280
|
+
if (tags.armv && tags.armv !== armv) return false
|
|
4281
|
+
if (tags.libc && tags.libc !== libc) return false
|
|
4282
|
+
|
|
4283
|
+
return true
|
|
4284
|
+
}
|
|
4285
|
+
}
|
|
4286
|
+
|
|
4287
|
+
function runtimeAgnostic (tags) {
|
|
4288
|
+
return tags.runtime === 'node' && tags.napi
|
|
4289
|
+
}
|
|
4290
|
+
|
|
4291
|
+
function compareTags (runtime) {
|
|
4292
|
+
// Precedence: non-agnostic runtime, abi over napi, then by specificity.
|
|
4293
|
+
return function (a, b) {
|
|
4294
|
+
if (a.runtime !== b.runtime) {
|
|
4295
|
+
return a.runtime === runtime ? -1 : 1
|
|
4296
|
+
} else if (a.abi !== b.abi) {
|
|
4297
|
+
return a.abi ? -1 : 1
|
|
4298
|
+
} else if (a.specificity !== b.specificity) {
|
|
4299
|
+
return a.specificity > b.specificity ? -1 : 1
|
|
4300
|
+
} else {
|
|
4301
|
+
return 0
|
|
4302
|
+
}
|
|
4303
|
+
}
|
|
4304
|
+
}
|
|
4305
|
+
|
|
4306
|
+
function isNwjs () {
|
|
4307
|
+
return !!(process.versions && process.versions.nw)
|
|
4308
|
+
}
|
|
4309
|
+
|
|
4310
|
+
function isElectron () {
|
|
4311
|
+
if (process.versions && process.versions.electron) return true
|
|
4312
|
+
if (process.env.ELECTRON_RUN_AS_NODE) return true
|
|
4313
|
+
return typeof window !== 'undefined' && window.process && window.process.type === 'renderer'
|
|
4314
|
+
}
|
|
4315
|
+
|
|
4316
|
+
function isAlpine (platform) {
|
|
4317
|
+
return platform === 'linux' && fs.existsSync('/etc/alpine-release')
|
|
4318
|
+
}
|
|
4319
|
+
|
|
4320
|
+
// Exposed for unit tests
|
|
4321
|
+
// TODO: move to lib
|
|
4322
|
+
load.parseTags = parseTags;
|
|
4323
|
+
load.matchTags = matchTags;
|
|
4324
|
+
load.compareTags = compareTags;
|
|
4325
|
+
load.parseTuple = parseTuple;
|
|
4326
|
+
load.matchTuple = matchTuple;
|
|
4327
|
+
load.compareTuples = compareTuples;
|
|
4328
|
+
|
|
4329
|
+
var bindings = api(nodeGypBuild(__dirname));
|
|
4330
|
+
|
|
4331
|
+
const P1600_ROUND_CONSTANTS = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649, 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648, 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648];
|
|
4332
|
+
|
|
4333
|
+
var p1600 = function (s) {
|
|
4334
|
+
for (let round = 0; round < 24; ++round) {
|
|
4335
|
+
// theta
|
|
4336
|
+
const lo0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40];
|
|
4337
|
+
const hi0 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41];
|
|
4338
|
+
const lo1 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42];
|
|
4339
|
+
const hi1 = s[3] ^ s[13] ^ s[23] ^ s[33] ^ s[43];
|
|
4340
|
+
const lo2 = s[4] ^ s[14] ^ s[24] ^ s[34] ^ s[44];
|
|
4341
|
+
const hi2 = s[5] ^ s[15] ^ s[25] ^ s[35] ^ s[45];
|
|
4342
|
+
const lo3 = s[6] ^ s[16] ^ s[26] ^ s[36] ^ s[46];
|
|
4343
|
+
const hi3 = s[7] ^ s[17] ^ s[27] ^ s[37] ^ s[47];
|
|
4344
|
+
const lo4 = s[8] ^ s[18] ^ s[28] ^ s[38] ^ s[48];
|
|
4345
|
+
const hi4 = s[9] ^ s[19] ^ s[29] ^ s[39] ^ s[49];
|
|
4346
|
+
|
|
4347
|
+
let lo = lo4 ^ (lo1 << 1 | hi1 >>> 31);
|
|
4348
|
+
let hi = hi4 ^ (hi1 << 1 | lo1 >>> 31);
|
|
4349
|
+
const t1slo0 = s[0] ^ lo;
|
|
4350
|
+
const t1shi0 = s[1] ^ hi;
|
|
4351
|
+
const t1slo5 = s[10] ^ lo;
|
|
4352
|
+
const t1shi5 = s[11] ^ hi;
|
|
4353
|
+
const t1slo10 = s[20] ^ lo;
|
|
4354
|
+
const t1shi10 = s[21] ^ hi;
|
|
4355
|
+
const t1slo15 = s[30] ^ lo;
|
|
4356
|
+
const t1shi15 = s[31] ^ hi;
|
|
4357
|
+
const t1slo20 = s[40] ^ lo;
|
|
4358
|
+
const t1shi20 = s[41] ^ hi;
|
|
4359
|
+
lo = lo0 ^ (lo2 << 1 | hi2 >>> 31);
|
|
4360
|
+
hi = hi0 ^ (hi2 << 1 | lo2 >>> 31);
|
|
4361
|
+
const t1slo1 = s[2] ^ lo;
|
|
4362
|
+
const t1shi1 = s[3] ^ hi;
|
|
4363
|
+
const t1slo6 = s[12] ^ lo;
|
|
4364
|
+
const t1shi6 = s[13] ^ hi;
|
|
4365
|
+
const t1slo11 = s[22] ^ lo;
|
|
4366
|
+
const t1shi11 = s[23] ^ hi;
|
|
4367
|
+
const t1slo16 = s[32] ^ lo;
|
|
4368
|
+
const t1shi16 = s[33] ^ hi;
|
|
4369
|
+
const t1slo21 = s[42] ^ lo;
|
|
4370
|
+
const t1shi21 = s[43] ^ hi;
|
|
4371
|
+
lo = lo1 ^ (lo3 << 1 | hi3 >>> 31);
|
|
4372
|
+
hi = hi1 ^ (hi3 << 1 | lo3 >>> 31);
|
|
4373
|
+
const t1slo2 = s[4] ^ lo;
|
|
4374
|
+
const t1shi2 = s[5] ^ hi;
|
|
4375
|
+
const t1slo7 = s[14] ^ lo;
|
|
4376
|
+
const t1shi7 = s[15] ^ hi;
|
|
4377
|
+
const t1slo12 = s[24] ^ lo;
|
|
4378
|
+
const t1shi12 = s[25] ^ hi;
|
|
4379
|
+
const t1slo17 = s[34] ^ lo;
|
|
4380
|
+
const t1shi17 = s[35] ^ hi;
|
|
4381
|
+
const t1slo22 = s[44] ^ lo;
|
|
4382
|
+
const t1shi22 = s[45] ^ hi;
|
|
4383
|
+
lo = lo2 ^ (lo4 << 1 | hi4 >>> 31);
|
|
4384
|
+
hi = hi2 ^ (hi4 << 1 | lo4 >>> 31);
|
|
4385
|
+
const t1slo3 = s[6] ^ lo;
|
|
4386
|
+
const t1shi3 = s[7] ^ hi;
|
|
4387
|
+
const t1slo8 = s[16] ^ lo;
|
|
4388
|
+
const t1shi8 = s[17] ^ hi;
|
|
4389
|
+
const t1slo13 = s[26] ^ lo;
|
|
4390
|
+
const t1shi13 = s[27] ^ hi;
|
|
4391
|
+
const t1slo18 = s[36] ^ lo;
|
|
4392
|
+
const t1shi18 = s[37] ^ hi;
|
|
4393
|
+
const t1slo23 = s[46] ^ lo;
|
|
4394
|
+
const t1shi23 = s[47] ^ hi;
|
|
4395
|
+
lo = lo3 ^ (lo0 << 1 | hi0 >>> 31);
|
|
4396
|
+
hi = hi3 ^ (hi0 << 1 | lo0 >>> 31);
|
|
4397
|
+
const t1slo4 = s[8] ^ lo;
|
|
4398
|
+
const t1shi4 = s[9] ^ hi;
|
|
4399
|
+
const t1slo9 = s[18] ^ lo;
|
|
4400
|
+
const t1shi9 = s[19] ^ hi;
|
|
4401
|
+
const t1slo14 = s[28] ^ lo;
|
|
4402
|
+
const t1shi14 = s[29] ^ hi;
|
|
4403
|
+
const t1slo19 = s[38] ^ lo;
|
|
4404
|
+
const t1shi19 = s[39] ^ hi;
|
|
4405
|
+
const t1slo24 = s[48] ^ lo;
|
|
4406
|
+
const t1shi24 = s[49] ^ hi;
|
|
4407
|
+
|
|
4408
|
+
// rho & pi
|
|
4409
|
+
const t2slo0 = t1slo0;
|
|
4410
|
+
const t2shi0 = t1shi0;
|
|
4411
|
+
const t2slo16 = (t1shi5 << 4 | t1slo5 >>> 28);
|
|
4412
|
+
const t2shi16 = (t1slo5 << 4 | t1shi5 >>> 28);
|
|
4413
|
+
const t2slo7 = (t1slo10 << 3 | t1shi10 >>> 29);
|
|
4414
|
+
const t2shi7 = (t1shi10 << 3 | t1slo10 >>> 29);
|
|
4415
|
+
const t2slo23 = (t1shi15 << 9 | t1slo15 >>> 23);
|
|
4416
|
+
const t2shi23 = (t1slo15 << 9 | t1shi15 >>> 23);
|
|
4417
|
+
const t2slo14 = (t1slo20 << 18 | t1shi20 >>> 14);
|
|
4418
|
+
const t2shi14 = (t1shi20 << 18 | t1slo20 >>> 14);
|
|
4419
|
+
const t2slo10 = (t1slo1 << 1 | t1shi1 >>> 31);
|
|
4420
|
+
const t2shi10 = (t1shi1 << 1 | t1slo1 >>> 31);
|
|
4421
|
+
const t2slo1 = (t1shi6 << 12 | t1slo6 >>> 20);
|
|
4422
|
+
const t2shi1 = (t1slo6 << 12 | t1shi6 >>> 20);
|
|
4423
|
+
const t2slo17 = (t1slo11 << 10 | t1shi11 >>> 22);
|
|
4424
|
+
const t2shi17 = (t1shi11 << 10 | t1slo11 >>> 22);
|
|
4425
|
+
const t2slo8 = (t1shi16 << 13 | t1slo16 >>> 19);
|
|
4426
|
+
const t2shi8 = (t1slo16 << 13 | t1shi16 >>> 19);
|
|
4427
|
+
const t2slo24 = (t1slo21 << 2 | t1shi21 >>> 30);
|
|
4428
|
+
const t2shi24 = (t1shi21 << 2 | t1slo21 >>> 30);
|
|
4429
|
+
const t2slo20 = (t1shi2 << 30 | t1slo2 >>> 2);
|
|
4430
|
+
const t2shi20 = (t1slo2 << 30 | t1shi2 >>> 2);
|
|
4431
|
+
const t2slo11 = (t1slo7 << 6 | t1shi7 >>> 26);
|
|
4432
|
+
const t2shi11 = (t1shi7 << 6 | t1slo7 >>> 26);
|
|
4433
|
+
const t2slo2 = (t1shi12 << 11 | t1slo12 >>> 21);
|
|
4434
|
+
const t2shi2 = (t1slo12 << 11 | t1shi12 >>> 21);
|
|
4435
|
+
const t2slo18 = (t1slo17 << 15 | t1shi17 >>> 17);
|
|
4436
|
+
const t2shi18 = (t1shi17 << 15 | t1slo17 >>> 17);
|
|
4437
|
+
const t2slo9 = (t1shi22 << 29 | t1slo22 >>> 3);
|
|
4438
|
+
const t2shi9 = (t1slo22 << 29 | t1shi22 >>> 3);
|
|
4439
|
+
const t2slo5 = (t1slo3 << 28 | t1shi3 >>> 4);
|
|
4440
|
+
const t2shi5 = (t1shi3 << 28 | t1slo3 >>> 4);
|
|
4441
|
+
const t2slo21 = (t1shi8 << 23 | t1slo8 >>> 9);
|
|
4442
|
+
const t2shi21 = (t1slo8 << 23 | t1shi8 >>> 9);
|
|
4443
|
+
const t2slo12 = (t1slo13 << 25 | t1shi13 >>> 7);
|
|
4444
|
+
const t2shi12 = (t1shi13 << 25 | t1slo13 >>> 7);
|
|
4445
|
+
const t2slo3 = (t1slo18 << 21 | t1shi18 >>> 11);
|
|
4446
|
+
const t2shi3 = (t1shi18 << 21 | t1slo18 >>> 11);
|
|
4447
|
+
const t2slo19 = (t1shi23 << 24 | t1slo23 >>> 8);
|
|
4448
|
+
const t2shi19 = (t1slo23 << 24 | t1shi23 >>> 8);
|
|
4449
|
+
const t2slo15 = (t1slo4 << 27 | t1shi4 >>> 5);
|
|
4450
|
+
const t2shi15 = (t1shi4 << 27 | t1slo4 >>> 5);
|
|
4451
|
+
const t2slo6 = (t1slo9 << 20 | t1shi9 >>> 12);
|
|
4452
|
+
const t2shi6 = (t1shi9 << 20 | t1slo9 >>> 12);
|
|
4453
|
+
const t2slo22 = (t1shi14 << 7 | t1slo14 >>> 25);
|
|
4454
|
+
const t2shi22 = (t1slo14 << 7 | t1shi14 >>> 25);
|
|
4455
|
+
const t2slo13 = (t1slo19 << 8 | t1shi19 >>> 24);
|
|
4456
|
+
const t2shi13 = (t1shi19 << 8 | t1slo19 >>> 24);
|
|
4457
|
+
const t2slo4 = (t1slo24 << 14 | t1shi24 >>> 18);
|
|
4458
|
+
const t2shi4 = (t1shi24 << 14 | t1slo24 >>> 18);
|
|
4459
|
+
|
|
4460
|
+
// chi
|
|
4461
|
+
s[0] = t2slo0 ^ (~t2slo1 & t2slo2);
|
|
4462
|
+
s[1] = t2shi0 ^ (~t2shi1 & t2shi2);
|
|
4463
|
+
s[10] = t2slo5 ^ (~t2slo6 & t2slo7);
|
|
4464
|
+
s[11] = t2shi5 ^ (~t2shi6 & t2shi7);
|
|
4465
|
+
s[20] = t2slo10 ^ (~t2slo11 & t2slo12);
|
|
4466
|
+
s[21] = t2shi10 ^ (~t2shi11 & t2shi12);
|
|
4467
|
+
s[30] = t2slo15 ^ (~t2slo16 & t2slo17);
|
|
4468
|
+
s[31] = t2shi15 ^ (~t2shi16 & t2shi17);
|
|
4469
|
+
s[40] = t2slo20 ^ (~t2slo21 & t2slo22);
|
|
4470
|
+
s[41] = t2shi20 ^ (~t2shi21 & t2shi22);
|
|
4471
|
+
s[2] = t2slo1 ^ (~t2slo2 & t2slo3);
|
|
4472
|
+
s[3] = t2shi1 ^ (~t2shi2 & t2shi3);
|
|
4473
|
+
s[12] = t2slo6 ^ (~t2slo7 & t2slo8);
|
|
4474
|
+
s[13] = t2shi6 ^ (~t2shi7 & t2shi8);
|
|
4475
|
+
s[22] = t2slo11 ^ (~t2slo12 & t2slo13);
|
|
4476
|
+
s[23] = t2shi11 ^ (~t2shi12 & t2shi13);
|
|
4477
|
+
s[32] = t2slo16 ^ (~t2slo17 & t2slo18);
|
|
4478
|
+
s[33] = t2shi16 ^ (~t2shi17 & t2shi18);
|
|
4479
|
+
s[42] = t2slo21 ^ (~t2slo22 & t2slo23);
|
|
4480
|
+
s[43] = t2shi21 ^ (~t2shi22 & t2shi23);
|
|
4481
|
+
s[4] = t2slo2 ^ (~t2slo3 & t2slo4);
|
|
4482
|
+
s[5] = t2shi2 ^ (~t2shi3 & t2shi4);
|
|
4483
|
+
s[14] = t2slo7 ^ (~t2slo8 & t2slo9);
|
|
4484
|
+
s[15] = t2shi7 ^ (~t2shi8 & t2shi9);
|
|
4485
|
+
s[24] = t2slo12 ^ (~t2slo13 & t2slo14);
|
|
4486
|
+
s[25] = t2shi12 ^ (~t2shi13 & t2shi14);
|
|
4487
|
+
s[34] = t2slo17 ^ (~t2slo18 & t2slo19);
|
|
4488
|
+
s[35] = t2shi17 ^ (~t2shi18 & t2shi19);
|
|
4489
|
+
s[44] = t2slo22 ^ (~t2slo23 & t2slo24);
|
|
4490
|
+
s[45] = t2shi22 ^ (~t2shi23 & t2shi24);
|
|
4491
|
+
s[6] = t2slo3 ^ (~t2slo4 & t2slo0);
|
|
4492
|
+
s[7] = t2shi3 ^ (~t2shi4 & t2shi0);
|
|
4493
|
+
s[16] = t2slo8 ^ (~t2slo9 & t2slo5);
|
|
4494
|
+
s[17] = t2shi8 ^ (~t2shi9 & t2shi5);
|
|
4495
|
+
s[26] = t2slo13 ^ (~t2slo14 & t2slo10);
|
|
4496
|
+
s[27] = t2shi13 ^ (~t2shi14 & t2shi10);
|
|
4497
|
+
s[36] = t2slo18 ^ (~t2slo19 & t2slo15);
|
|
4498
|
+
s[37] = t2shi18 ^ (~t2shi19 & t2shi15);
|
|
4499
|
+
s[46] = t2slo23 ^ (~t2slo24 & t2slo20);
|
|
4500
|
+
s[47] = t2shi23 ^ (~t2shi24 & t2shi20);
|
|
4501
|
+
s[8] = t2slo4 ^ (~t2slo0 & t2slo1);
|
|
4502
|
+
s[9] = t2shi4 ^ (~t2shi0 & t2shi1);
|
|
4503
|
+
s[18] = t2slo9 ^ (~t2slo5 & t2slo6);
|
|
4504
|
+
s[19] = t2shi9 ^ (~t2shi5 & t2shi6);
|
|
4505
|
+
s[28] = t2slo14 ^ (~t2slo10 & t2slo11);
|
|
4506
|
+
s[29] = t2shi14 ^ (~t2shi10 & t2shi11);
|
|
4507
|
+
s[38] = t2slo19 ^ (~t2slo15 & t2slo16);
|
|
4508
|
+
s[39] = t2shi19 ^ (~t2shi15 & t2shi16);
|
|
4509
|
+
s[48] = t2slo24 ^ (~t2slo20 & t2slo21);
|
|
4510
|
+
s[49] = t2shi24 ^ (~t2shi20 & t2shi21);
|
|
4511
|
+
|
|
4512
|
+
// iota
|
|
4513
|
+
s[0] ^= P1600_ROUND_CONSTANTS[round * 2];
|
|
4514
|
+
s[1] ^= P1600_ROUND_CONSTANTS[round * 2 + 1];
|
|
4515
|
+
}
|
|
4516
|
+
};
|
|
4517
|
+
|
|
4518
|
+
var keccakStateUnroll = {
|
|
4519
|
+
p1600: p1600
|
|
4520
|
+
};
|
|
4521
|
+
|
|
4522
|
+
function Keccak () {
|
|
4523
|
+
// much faster than `new Array(50)`
|
|
4524
|
+
this.state = [
|
|
4525
|
+
0, 0, 0, 0, 0,
|
|
4526
|
+
0, 0, 0, 0, 0,
|
|
4527
|
+
0, 0, 0, 0, 0,
|
|
4528
|
+
0, 0, 0, 0, 0,
|
|
4529
|
+
0, 0, 0, 0, 0
|
|
4530
|
+
];
|
|
4531
|
+
|
|
4532
|
+
this.blockSize = null;
|
|
4533
|
+
this.count = 0;
|
|
4534
|
+
this.squeezing = false;
|
|
4535
|
+
}
|
|
4536
|
+
|
|
4537
|
+
Keccak.prototype.initialize = function (rate, capacity) {
|
|
4538
|
+
for (let i = 0; i < 50; ++i) this.state[i] = 0;
|
|
4539
|
+
this.blockSize = rate / 8;
|
|
4540
|
+
this.count = 0;
|
|
4541
|
+
this.squeezing = false;
|
|
4542
|
+
};
|
|
4543
|
+
|
|
4544
|
+
Keccak.prototype.absorb = function (data) {
|
|
4545
|
+
for (let i = 0; i < data.length; ++i) {
|
|
4546
|
+
this.state[~~(this.count / 4)] ^= data[i] << (8 * (this.count % 4));
|
|
4547
|
+
this.count += 1;
|
|
4548
|
+
if (this.count === this.blockSize) {
|
|
4549
|
+
keccakStateUnroll.p1600(this.state);
|
|
4550
|
+
this.count = 0;
|
|
4551
|
+
}
|
|
4552
|
+
}
|
|
4553
|
+
};
|
|
4554
|
+
|
|
4555
|
+
Keccak.prototype.absorbLastFewBits = function (bits) {
|
|
4556
|
+
this.state[~~(this.count / 4)] ^= bits << (8 * (this.count % 4));
|
|
4557
|
+
if ((bits & 0x80) !== 0 && this.count === (this.blockSize - 1)) keccakStateUnroll.p1600(this.state);
|
|
4558
|
+
this.state[~~((this.blockSize - 1) / 4)] ^= 0x80 << (8 * ((this.blockSize - 1) % 4));
|
|
4559
|
+
keccakStateUnroll.p1600(this.state);
|
|
4560
|
+
this.count = 0;
|
|
4561
|
+
this.squeezing = true;
|
|
4562
|
+
};
|
|
4563
|
+
|
|
4564
|
+
Keccak.prototype.squeeze = function (length) {
|
|
4565
|
+
if (!this.squeezing) this.absorbLastFewBits(0x01);
|
|
4566
|
+
|
|
4567
|
+
const output = Buffer.alloc(length);
|
|
4568
|
+
for (let i = 0; i < length; ++i) {
|
|
4569
|
+
output[i] = (this.state[~~(this.count / 4)] >>> (8 * (this.count % 4))) & 0xff;
|
|
4570
|
+
this.count += 1;
|
|
4571
|
+
if (this.count === this.blockSize) {
|
|
4572
|
+
keccakStateUnroll.p1600(this.state);
|
|
4573
|
+
this.count = 0;
|
|
4574
|
+
}
|
|
4575
|
+
}
|
|
4576
|
+
|
|
4577
|
+
return output
|
|
4578
|
+
};
|
|
4579
|
+
|
|
4580
|
+
Keccak.prototype.copy = function (dest) {
|
|
4581
|
+
for (let i = 0; i < 50; ++i) dest.state[i] = this.state[i];
|
|
4582
|
+
dest.blockSize = this.blockSize;
|
|
4583
|
+
dest.count = this.count;
|
|
4584
|
+
dest.squeezing = this.squeezing;
|
|
4585
|
+
};
|
|
4586
|
+
|
|
4587
|
+
var keccak$1 = Keccak;
|
|
4588
|
+
|
|
4589
|
+
var js = api(keccak$1);
|
|
4590
|
+
|
|
4591
|
+
var keccak = createCommonjsModule(function (module) {
|
|
4592
|
+
try {
|
|
4593
|
+
module.exports = bindings;
|
|
4594
|
+
} catch (err) {
|
|
4595
|
+
module.exports = js;
|
|
4596
|
+
}
|
|
4597
|
+
});
|
|
4598
|
+
|
|
4599
|
+
class CodecHash$1 extends BasicInterface$1 {
|
|
4600
|
+
constructor(buffer, options = {}) {
|
|
4601
|
+
super();
|
|
4602
|
+
if (options.name) this.name = options.name;
|
|
4603
|
+
else this.name = 'disco-hash';
|
|
4604
|
+
if (options.codecs) this.codecs = options.codecs;
|
|
4605
|
+
if (buffer) {
|
|
4606
|
+
if (buffer instanceof Uint8Array) {
|
|
4607
|
+
this.discoCodec = new PeernetCodec(buffer, this.codecs);
|
|
4608
|
+
const name = this.discoCodec.name;
|
|
4609
|
+
|
|
4610
|
+
if (name) {
|
|
4611
|
+
this.name = name;
|
|
4612
|
+
this.decode(buffer);
|
|
4613
|
+
} else {
|
|
4614
|
+
this.encode(buffer);
|
|
4615
|
+
}
|
|
4616
|
+
}
|
|
4617
|
+
|
|
4618
|
+
if (typeof buffer === 'string') {
|
|
4619
|
+
if (this.isHex(buffer)) this.fromHex(buffer);
|
|
4620
|
+
if (this.isBase32(buffer)) this.fromBs32(buffer);
|
|
4621
|
+
else if (this.isBase58(buffer)) this.fromBs58(buffer);
|
|
4622
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
4623
|
+
} else if (typeof buffer === 'object') this.fromJSON(buffer);
|
|
4624
|
+
}
|
|
4625
|
+
}
|
|
4626
|
+
|
|
4627
|
+
get prefix() {
|
|
4628
|
+
const length = this.length;
|
|
4629
|
+
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
4630
|
+
uint8Array.set(length);
|
|
4631
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
4632
|
+
|
|
4633
|
+
return uint8Array
|
|
4634
|
+
}
|
|
4635
|
+
|
|
4636
|
+
get length() {
|
|
4637
|
+
return varint.encode(this.size)
|
|
4638
|
+
}
|
|
4639
|
+
|
|
4640
|
+
get buffer() {
|
|
4641
|
+
return this.encoded
|
|
4642
|
+
}
|
|
4643
|
+
|
|
4644
|
+
get hash() {
|
|
4645
|
+
return this.encoded
|
|
4646
|
+
}
|
|
4647
|
+
|
|
4648
|
+
fromJSON(json) {
|
|
4649
|
+
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
4650
|
+
}
|
|
4651
|
+
|
|
4652
|
+
encode(buffer, name) {
|
|
4653
|
+
if (!this.name && name) this.name = name;
|
|
4654
|
+
if (!buffer) buffer = this.buffer;
|
|
4655
|
+
this.discoCodec = new PeernetCodec(this.name, this.codecs);
|
|
4656
|
+
this.discoCodec.fromName(this.name);
|
|
4657
|
+
let hashAlg = this.discoCodec.hashAlg;
|
|
4658
|
+
if (hashAlg.includes('dbl')) {
|
|
4659
|
+
hashAlg = hashAlg.replace('dbl-', '');
|
|
4660
|
+
buffer = keccak(hashAlg.replace('-', '')).update(buffer).digest();
|
|
4661
|
+
}
|
|
4662
|
+
this.digest = keccak(hashAlg.replace('-', '')).update(buffer).digest();
|
|
4663
|
+
this.size = this.digest.length;
|
|
4664
|
+
|
|
4665
|
+
this.codec = this.discoCodec.encode();
|
|
4666
|
+
this.codec = this.discoCodec.codecBuffer;
|
|
4667
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
4668
|
+
uint8Array.set(this.prefix);
|
|
4669
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
4670
|
+
|
|
4671
|
+
this.encoded = uint8Array;
|
|
4672
|
+
|
|
4673
|
+
return this.encoded
|
|
4674
|
+
}
|
|
4675
|
+
|
|
4676
|
+
validate(buffer) {
|
|
4677
|
+
if (Buffer.isBuffer(buffer)) {
|
|
4678
|
+
const codec = varint.decode(buffer);
|
|
4679
|
+
if (this.codecs[codec]) {
|
|
4680
|
+
this.decode(buffer);
|
|
4681
|
+
} else {
|
|
4682
|
+
this.encode(buffer);
|
|
4683
|
+
}
|
|
4684
|
+
}
|
|
4685
|
+
if (typeof buffer === 'string') {
|
|
4686
|
+
if (this.isHex(buffer)) this.fromHex(buffer);
|
|
4687
|
+
if (this.isBase32(buffer)) this.fromBs32(buffer);
|
|
4688
|
+
}
|
|
4689
|
+
if (typeof buffer === 'object') this.fromJSON(buffer);
|
|
4690
|
+
}
|
|
4691
|
+
|
|
4692
|
+
decode(buffer) {
|
|
4693
|
+
this.encoded = buffer;
|
|
4694
|
+
const codec = varint.decode(buffer);
|
|
4695
|
+
|
|
4696
|
+
this.discoCodec = new PeernetCodec(codec, this.codecs);
|
|
4697
|
+
// TODO: validate codec
|
|
4698
|
+
buffer = buffer.slice(varint.decode.bytes);
|
|
4699
|
+
this.size = varint.decode(buffer);
|
|
4700
|
+
this.digest = buffer.slice(varint.decode.bytes);
|
|
4701
|
+
if (this.digest.length !== this.size) {
|
|
4702
|
+
throw new Error(`hash length inconsistent: 0x${this.encoded.toString('hex')}`)
|
|
4703
|
+
}
|
|
4704
|
+
|
|
4705
|
+
// const discoCodec = new Codec(codec, this.codecs)
|
|
4706
|
+
|
|
4707
|
+
this.name = this.discoCodec.name;
|
|
4708
|
+
|
|
4709
|
+
|
|
4710
|
+
this.size = this.digest.length;
|
|
4711
|
+
|
|
4712
|
+
return {
|
|
4713
|
+
codec: this.codec,
|
|
4714
|
+
name: this.name,
|
|
4715
|
+
size: this.size,
|
|
4716
|
+
length: this.length,
|
|
4717
|
+
digest: this.digest,
|
|
4718
|
+
}
|
|
4719
|
+
}
|
|
4720
|
+
}
|
|
4721
|
+
|
|
4722
|
+
class FormatInterface$1 extends BasicInterface$1 {
|
|
4723
|
+
|
|
4724
|
+
async protoEncode(data) {
|
|
4725
|
+
// check schema
|
|
4726
|
+
return new TextEncoder().encode(data)
|
|
4727
|
+
}
|
|
4728
|
+
|
|
4729
|
+
async protoDecode(data) {
|
|
4730
|
+
// check schema
|
|
4731
|
+
return new TextDecoder().decode(data)
|
|
4732
|
+
}
|
|
4733
|
+
|
|
4734
|
+
async init(buffer) {
|
|
4735
|
+
if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer);
|
|
4736
|
+
else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer);
|
|
4737
|
+
else if (buffer?.name === this.name) return buffer
|
|
4738
|
+
else if (buffer instanceof String) {
|
|
4739
|
+
if (this.isHex(buffer)) await this.fromHex(buffer);
|
|
4740
|
+
else if (this.isBase32(buffer)) await this.fromBs32(buffer);
|
|
4741
|
+
else if (this.isBase58(buffer)) await this.fromBs58(buffer);
|
|
4742
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
4743
|
+
} else {
|
|
4744
|
+
await this.create(buffer);
|
|
4745
|
+
}
|
|
4746
|
+
return this
|
|
4747
|
+
}
|
|
4748
|
+
|
|
4749
|
+
/**
|
|
4750
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
4751
|
+
* @param {Object} proto - {encode, decode}
|
|
4752
|
+
* @param {Object} options - {hashFormat, name}
|
|
4753
|
+
*/
|
|
4754
|
+
constructor(buffer, proto, options = {}) {
|
|
4755
|
+
super();
|
|
4756
|
+
this.proto = proto;
|
|
4757
|
+
this.hashFormat = options.hashFormat || 'bs32';
|
|
4758
|
+
if (options.name) this.name = options.name;
|
|
4759
|
+
return this.init(buffer)
|
|
4760
|
+
}
|
|
4761
|
+
|
|
4762
|
+
/**
|
|
4763
|
+
* @return {PeernetHash}
|
|
4764
|
+
*/
|
|
4765
|
+
get peernetHash() {
|
|
4766
|
+
return new CodecHash$1(this.decoded, {name: this.name})
|
|
4767
|
+
}
|
|
4768
|
+
|
|
4769
|
+
/**
|
|
4770
|
+
* @return {peernetHash}
|
|
4771
|
+
*/
|
|
4772
|
+
get hash() {
|
|
4773
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
4774
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
4775
|
+
return this.peernetHash[`to${format}`]()
|
|
4776
|
+
}
|
|
4777
|
+
|
|
4778
|
+
/**
|
|
4779
|
+
* @return {Object}
|
|
4780
|
+
*/
|
|
4781
|
+
async decode() {
|
|
4782
|
+
let encoded = this.encoded;
|
|
4783
|
+
const discoCodec = new PeernetCodec(this.encoded);
|
|
4784
|
+
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
4785
|
+
this.name = discoCodec.name;
|
|
4786
|
+
this.decoded = await this.protoDecode(encoded);
|
|
4787
|
+
try {
|
|
4788
|
+
this.decoded = JSON.parse(this.decoded);
|
|
4789
|
+
} catch {
|
|
4790
|
+
|
|
4791
|
+
}
|
|
4792
|
+
return this.decoded
|
|
4793
|
+
}
|
|
4794
|
+
|
|
4795
|
+
/**
|
|
4796
|
+
* @return {Buffer}
|
|
4797
|
+
*/
|
|
4798
|
+
async encode(decoded) {
|
|
4799
|
+
if (!decoded) decoded = this.decoded;
|
|
4800
|
+
const codec = new PeernetCodec(this.name);
|
|
4801
|
+
const encoded = await this.protoEncode(typeof decoded === 'object' ? JSON.stringify(decoded) : decoded);
|
|
4802
|
+
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
4803
|
+
uint8Array.set(codec.codecBuffer);
|
|
4804
|
+
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
4805
|
+
this.encoded = uint8Array;
|
|
4806
|
+
return this.encoded
|
|
4807
|
+
}
|
|
4808
|
+
|
|
4809
|
+
hasCodec() {
|
|
4810
|
+
if (!this.encoded) return false
|
|
4811
|
+
const codec = new PeernetCodec(this.encoded);
|
|
4812
|
+
if (codec.name) return true
|
|
4813
|
+
}
|
|
4814
|
+
|
|
4815
|
+
fromUint8Array(buffer) {
|
|
4816
|
+
this.encoded = buffer;
|
|
4817
|
+
return this.hasCodec() ? this.decode() : this.create(
|
|
4818
|
+
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
4819
|
+
)
|
|
4820
|
+
}
|
|
4821
|
+
|
|
4822
|
+
fromArrayBuffer(buffer) {
|
|
4823
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
4824
|
+
return this.hasCodec() ? this.decode() : this.create(
|
|
4825
|
+
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
4826
|
+
)
|
|
4827
|
+
}
|
|
4828
|
+
}
|
|
4829
|
+
|
|
4830
|
+
const BasicInterface = BasicInterface$1;
|
|
4831
|
+
const FormatInterface = FormatInterface$1;
|
|
4832
|
+
const CodecHash = CodecHash$1;
|
|
4833
|
+
const Codec = PeernetCodec;
|
|
4834
|
+
const Codecs = codecs;
|
|
4835
|
+
|
|
4836
|
+
export { BasicInterface, Codec, CodecHash, Codecs, FormatInterface };
|