@jsenv/server 16.0.2 → 16.0.4

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/js/ws.js CHANGED
@@ -1,186 +1,18 @@
1
- import require$$0 from "node:stream";
2
- import require$$0$1 from "node:zlib";
3
- import require$$1 from "node:crypto";
4
- import require$$0$2 from "node:events";
1
+ import require$$0$3 from "node:events";
5
2
  import require$$1$1 from "node:https";
6
3
  import http from "node:http";
7
4
  import net from "node:net";
8
5
  import require$$4 from "node:tls";
6
+ import require$$1 from "node:crypto";
7
+ import require$$0$2 from "node:stream";
9
8
  import require$$7 from "node:url";
9
+ import require$$0 from "node:zlib";
10
+ import require$$0$1 from "node:buffer";
10
11
 
11
12
  function getDefaultExportFromCjs (x) {
12
13
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
13
14
  }
14
15
 
15
- var stream$1;
16
- var hasRequiredStream;
17
-
18
- function requireStream () {
19
- if (hasRequiredStream) return stream$1;
20
- hasRequiredStream = 1;
21
-
22
- const { Duplex } = require$$0;
23
-
24
- /**
25
- * Emits the `'close'` event on a stream.
26
- *
27
- * @param {Duplex} stream The stream.
28
- * @private
29
- */
30
- function emitClose(stream) {
31
- stream.emit('close');
32
- }
33
-
34
- /**
35
- * The listener of the `'end'` event.
36
- *
37
- * @private
38
- */
39
- function duplexOnEnd() {
40
- if (!this.destroyed && this._writableState.finished) {
41
- this.destroy();
42
- }
43
- }
44
-
45
- /**
46
- * The listener of the `'error'` event.
47
- *
48
- * @param {Error} err The error
49
- * @private
50
- */
51
- function duplexOnError(err) {
52
- this.removeListener('error', duplexOnError);
53
- this.destroy();
54
- if (this.listenerCount('error') === 0) {
55
- // Do not suppress the throwing behavior.
56
- this.emit('error', err);
57
- }
58
- }
59
-
60
- /**
61
- * Wraps a `WebSocket` in a duplex stream.
62
- *
63
- * @param {WebSocket} ws The `WebSocket` to wrap
64
- * @param {Object} [options] The options for the `Duplex` constructor
65
- * @return {Duplex} The duplex stream
66
- * @public
67
- */
68
- function createWebSocketStream(ws, options) {
69
- let terminateOnDestroy = true;
70
-
71
- const duplex = new Duplex({
72
- ...options,
73
- autoDestroy: false,
74
- emitClose: false,
75
- objectMode: false,
76
- writableObjectMode: false
77
- });
78
-
79
- ws.on('message', function message(msg, isBinary) {
80
- const data =
81
- !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
82
-
83
- if (!duplex.push(data)) ws.pause();
84
- });
85
-
86
- ws.once('error', function error(err) {
87
- if (duplex.destroyed) return;
88
-
89
- // Prevent `ws.terminate()` from being called by `duplex._destroy()`.
90
- //
91
- // - If the `'error'` event is emitted before the `'open'` event, then
92
- // `ws.terminate()` is a noop as no socket is assigned.
93
- // - Otherwise, the error is re-emitted by the listener of the `'error'`
94
- // event of the `Receiver` object. The listener already closes the
95
- // connection by calling `ws.close()`. This allows a close frame to be
96
- // sent to the other peer. If `ws.terminate()` is called right after this,
97
- // then the close frame might not be sent.
98
- terminateOnDestroy = false;
99
- duplex.destroy(err);
100
- });
101
-
102
- ws.once('close', function close() {
103
- if (duplex.destroyed) return;
104
-
105
- duplex.push(null);
106
- });
107
-
108
- duplex._destroy = function (err, callback) {
109
- if (ws.readyState === ws.CLOSED) {
110
- callback(err);
111
- process.nextTick(emitClose, duplex);
112
- return;
113
- }
114
-
115
- let called = false;
116
-
117
- ws.once('error', function error(err) {
118
- called = true;
119
- callback(err);
120
- });
121
-
122
- ws.once('close', function close() {
123
- if (!called) callback(err);
124
- process.nextTick(emitClose, duplex);
125
- });
126
-
127
- if (terminateOnDestroy) ws.terminate();
128
- };
129
-
130
- duplex._final = function (callback) {
131
- if (ws.readyState === ws.CONNECTING) {
132
- ws.once('open', function open() {
133
- duplex._final(callback);
134
- });
135
- return;
136
- }
137
-
138
- // If the value of the `_socket` property is `null` it means that `ws` is a
139
- // client websocket and the handshake failed. In fact, when this happens, a
140
- // socket is never assigned to the websocket. Wait for the `'error'` event
141
- // that will be emitted by the websocket.
142
- if (ws._socket === null) return;
143
-
144
- if (ws._socket._writableState.finished) {
145
- callback();
146
- if (duplex._readableState.endEmitted) duplex.destroy();
147
- } else {
148
- ws._socket.once('finish', function finish() {
149
- // `duplex` is not destroyed here because the `'end'` event will be
150
- // emitted on `duplex` after this `'finish'` event. The EOF signaling
151
- // `null` chunk is, in fact, pushed when the websocket emits `'close'`.
152
- callback();
153
- });
154
- ws.close();
155
- }
156
- };
157
-
158
- duplex._read = function () {
159
- if (ws.isPaused) ws.resume();
160
- };
161
-
162
- duplex._write = function (chunk, encoding, callback) {
163
- if (ws.readyState === ws.CONNECTING) {
164
- ws.once('open', function open() {
165
- duplex._write(chunk, encoding, callback);
166
- });
167
- return;
168
- }
169
-
170
- ws.send(chunk, callback);
171
- };
172
-
173
- duplex.on('end', duplexOnEnd);
174
- duplex.on('error', duplexOnError);
175
- return duplex;
176
- }
177
-
178
- stream$1 = createWebSocketStream;
179
- return stream$1;
180
- }
181
-
182
- requireStream();
183
-
184
16
  var bufferUtil = {exports: {}};
185
17
 
186
18
  var constants;
@@ -417,7 +249,7 @@ function requirePermessageDeflate () {
417
249
  if (hasRequiredPermessageDeflate) return permessageDeflate;
418
250
  hasRequiredPermessageDeflate = 1;
419
251
 
420
- const zlib = require$$0$1;
252
+ const zlib = require$$0;
421
253
 
422
254
  const bufferUtil = requireBufferUtil();
423
255
  const Limiter = requireLimiter();
@@ -934,2095 +766,13 @@ function requirePermessageDeflate () {
934
766
 
935
767
  var validation = {exports: {}};
936
768
 
937
- var buffer = {};
938
-
939
- var base64Js = {};
940
-
941
- var hasRequiredBase64Js;
942
-
943
- function requireBase64Js () {
944
- if (hasRequiredBase64Js) return base64Js;
945
- hasRequiredBase64Js = 1;
946
-
947
- base64Js.byteLength = byteLength;
948
- base64Js.toByteArray = toByteArray;
949
- base64Js.fromByteArray = fromByteArray;
950
-
951
- var lookup = [];
952
- var revLookup = [];
953
- var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
954
-
955
- var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
956
- for (var i = 0, len = code.length; i < len; ++i) {
957
- lookup[i] = code[i];
958
- revLookup[code.charCodeAt(i)] = i;
959
- }
960
-
961
- // Support decoding URL-safe base64 strings, as Node.js does.
962
- // See: https://en.wikipedia.org/wiki/Base64#URL_applications
963
- revLookup['-'.charCodeAt(0)] = 62;
964
- revLookup['_'.charCodeAt(0)] = 63;
965
-
966
- function getLens (b64) {
967
- var len = b64.length;
968
-
969
- if (len % 4 > 0) {
970
- throw new Error('Invalid string. Length must be a multiple of 4')
971
- }
972
-
973
- // Trim off extra bytes after placeholder bytes are found
974
- // See: https://github.com/beatgammit/base64-js/issues/42
975
- var validLen = b64.indexOf('=');
976
- if (validLen === -1) validLen = len;
977
-
978
- var placeHoldersLen = validLen === len
979
- ? 0
980
- : 4 - (validLen % 4);
981
-
982
- return [validLen, placeHoldersLen]
983
- }
984
-
985
- // base64 is 4/3 + up to two characters of the original data
986
- function byteLength (b64) {
987
- var lens = getLens(b64);
988
- var validLen = lens[0];
989
- var placeHoldersLen = lens[1];
990
- return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
991
- }
992
-
993
- function _byteLength (b64, validLen, placeHoldersLen) {
994
- return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
995
- }
996
-
997
- function toByteArray (b64) {
998
- var tmp;
999
- var lens = getLens(b64);
1000
- var validLen = lens[0];
1001
- var placeHoldersLen = lens[1];
1002
-
1003
- var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen));
1004
-
1005
- var curByte = 0;
1006
-
1007
- // if there are placeholders, only get up to the last complete 4 chars
1008
- var len = placeHoldersLen > 0
1009
- ? validLen - 4
1010
- : validLen;
1011
-
1012
- var i;
1013
- for (i = 0; i < len; i += 4) {
1014
- tmp =
1015
- (revLookup[b64.charCodeAt(i)] << 18) |
1016
- (revLookup[b64.charCodeAt(i + 1)] << 12) |
1017
- (revLookup[b64.charCodeAt(i + 2)] << 6) |
1018
- revLookup[b64.charCodeAt(i + 3)];
1019
- arr[curByte++] = (tmp >> 16) & 0xFF;
1020
- arr[curByte++] = (tmp >> 8) & 0xFF;
1021
- arr[curByte++] = tmp & 0xFF;
1022
- }
1023
-
1024
- if (placeHoldersLen === 2) {
1025
- tmp =
1026
- (revLookup[b64.charCodeAt(i)] << 2) |
1027
- (revLookup[b64.charCodeAt(i + 1)] >> 4);
1028
- arr[curByte++] = tmp & 0xFF;
1029
- }
1030
-
1031
- if (placeHoldersLen === 1) {
1032
- tmp =
1033
- (revLookup[b64.charCodeAt(i)] << 10) |
1034
- (revLookup[b64.charCodeAt(i + 1)] << 4) |
1035
- (revLookup[b64.charCodeAt(i + 2)] >> 2);
1036
- arr[curByte++] = (tmp >> 8) & 0xFF;
1037
- arr[curByte++] = tmp & 0xFF;
1038
- }
1039
-
1040
- return arr
1041
- }
1042
-
1043
- function tripletToBase64 (num) {
1044
- return lookup[num >> 18 & 0x3F] +
1045
- lookup[num >> 12 & 0x3F] +
1046
- lookup[num >> 6 & 0x3F] +
1047
- lookup[num & 0x3F]
1048
- }
1049
-
1050
- function encodeChunk (uint8, start, end) {
1051
- var tmp;
1052
- var output = [];
1053
- for (var i = start; i < end; i += 3) {
1054
- tmp =
1055
- ((uint8[i] << 16) & 0xFF0000) +
1056
- ((uint8[i + 1] << 8) & 0xFF00) +
1057
- (uint8[i + 2] & 0xFF);
1058
- output.push(tripletToBase64(tmp));
1059
- }
1060
- return output.join('')
1061
- }
1062
-
1063
- function fromByteArray (uint8) {
1064
- var tmp;
1065
- var len = uint8.length;
1066
- var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
1067
- var parts = [];
1068
- var maxChunkLength = 16383; // must be multiple of 3
1069
-
1070
- // go through the array every three bytes, we'll deal with trailing stuff later
1071
- for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
1072
- parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)));
1073
- }
1074
-
1075
- // pad the end with zeros, but make sure to not forget the extra bytes
1076
- if (extraBytes === 1) {
1077
- tmp = uint8[len - 1];
1078
- parts.push(
1079
- lookup[tmp >> 2] +
1080
- lookup[(tmp << 4) & 0x3F] +
1081
- '=='
1082
- );
1083
- } else if (extraBytes === 2) {
1084
- tmp = (uint8[len - 2] << 8) + uint8[len - 1];
1085
- parts.push(
1086
- lookup[tmp >> 10] +
1087
- lookup[(tmp >> 4) & 0x3F] +
1088
- lookup[(tmp << 2) & 0x3F] +
1089
- '='
1090
- );
1091
- }
1092
-
1093
- return parts.join('')
1094
- }
1095
- return base64Js;
1096
- }
1097
-
1098
- var ieee754 = {};
1099
-
1100
- /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
1101
-
1102
- var hasRequiredIeee754;
1103
-
1104
- function requireIeee754 () {
1105
- if (hasRequiredIeee754) return ieee754;
1106
- hasRequiredIeee754 = 1;
1107
- ieee754.read = function (buffer, offset, isLE, mLen, nBytes) {
1108
- var e, m;
1109
- var eLen = (nBytes * 8) - mLen - 1;
1110
- var eMax = (1 << eLen) - 1;
1111
- var eBias = eMax >> 1;
1112
- var nBits = -7;
1113
- var i = isLE ? (nBytes - 1) : 0;
1114
- var d = isLE ? -1 : 1;
1115
- var s = buffer[offset + i];
1116
-
1117
- i += d;
1118
-
1119
- e = s & ((1 << (-nBits)) - 1);
1120
- s >>= (-nBits);
1121
- nBits += eLen;
1122
- for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
1123
-
1124
- m = e & ((1 << (-nBits)) - 1);
1125
- e >>= (-nBits);
1126
- nBits += mLen;
1127
- for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
1128
-
1129
- if (e === 0) {
1130
- e = 1 - eBias;
1131
- } else if (e === eMax) {
1132
- return m ? NaN : ((s ? -1 : 1) * Infinity)
1133
- } else {
1134
- m = m + Math.pow(2, mLen);
1135
- e = e - eBias;
1136
- }
1137
- return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
1138
- };
1139
-
1140
- ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes) {
1141
- var e, m, c;
1142
- var eLen = (nBytes * 8) - mLen - 1;
1143
- var eMax = (1 << eLen) - 1;
1144
- var eBias = eMax >> 1;
1145
- var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
1146
- var i = isLE ? 0 : (nBytes - 1);
1147
- var d = isLE ? 1 : -1;
1148
- var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
1149
-
1150
- value = Math.abs(value);
1151
-
1152
- if (isNaN(value) || value === Infinity) {
1153
- m = isNaN(value) ? 1 : 0;
1154
- e = eMax;
1155
- } else {
1156
- e = Math.floor(Math.log(value) / Math.LN2);
1157
- if (value * (c = Math.pow(2, -e)) < 1) {
1158
- e--;
1159
- c *= 2;
1160
- }
1161
- if (e + eBias >= 1) {
1162
- value += rt / c;
1163
- } else {
1164
- value += rt * Math.pow(2, 1 - eBias);
1165
- }
1166
- if (value * c >= 2) {
1167
- e++;
1168
- c /= 2;
1169
- }
1170
-
1171
- if (e + eBias >= eMax) {
1172
- m = 0;
1173
- e = eMax;
1174
- } else if (e + eBias >= 1) {
1175
- m = ((value * c) - 1) * Math.pow(2, mLen);
1176
- e = e + eBias;
1177
- } else {
1178
- m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
1179
- e = 0;
1180
- }
1181
- }
1182
-
1183
- for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
1184
-
1185
- e = (e << mLen) | m;
1186
- eLen += mLen;
1187
- for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
1188
-
1189
- buffer[offset + i - d] |= s * 128;
1190
- };
1191
- return ieee754;
1192
- }
1193
-
1194
- /*!
1195
- * The buffer module from node.js, for the browser.
1196
- *
1197
- * @author Feross Aboukhadijeh <https://feross.org>
1198
- * @license MIT
1199
- */
1200
-
1201
- var hasRequiredBuffer;
1202
-
1203
- function requireBuffer () {
1204
- if (hasRequiredBuffer) return buffer;
1205
- hasRequiredBuffer = 1;
1206
- (function (exports) {
1207
-
1208
- var base64 = requireBase64Js();
1209
- var ieee754 = requireIeee754();
1210
- var customInspectSymbol =
1211
- (typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
1212
- ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
1213
- : null;
1214
-
1215
- exports.Buffer = Buffer;
1216
- exports.SlowBuffer = SlowBuffer;
1217
- exports.INSPECT_MAX_BYTES = 50;
1218
-
1219
- var K_MAX_LENGTH = 0x7fffffff;
1220
- exports.kMaxLength = K_MAX_LENGTH;
1221
-
1222
- /**
1223
- * If `Buffer.TYPED_ARRAY_SUPPORT`:
1224
- * === true Use Uint8Array implementation (fastest)
1225
- * === false Print warning and recommend using `buffer` v4.x which has an Object
1226
- * implementation (most compatible, even IE6)
1227
- *
1228
- * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
1229
- * Opera 11.6+, iOS 4.2+.
1230
- *
1231
- * We report that the browser does not support typed arrays if the are not subclassable
1232
- * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
1233
- * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
1234
- * for __proto__ and has a buggy typed array implementation.
1235
- */
1236
- Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport();
1237
-
1238
- if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
1239
- typeof console.error === 'function') {
1240
- console.error(
1241
- 'This browser lacks typed array (Uint8Array) support which is required by ' +
1242
- '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
1243
- );
1244
- }
1245
-
1246
- function typedArraySupport () {
1247
- // Can typed array instances can be augmented?
1248
- try {
1249
- var arr = new Uint8Array(1);
1250
- var proto = { foo: function () { return 42 } };
1251
- Object.setPrototypeOf(proto, Uint8Array.prototype);
1252
- Object.setPrototypeOf(arr, proto);
1253
- return arr.foo() === 42
1254
- } catch (e) {
1255
- return false
1256
- }
1257
- }
1258
-
1259
- Object.defineProperty(Buffer.prototype, 'parent', {
1260
- enumerable: true,
1261
- get: function () {
1262
- if (!Buffer.isBuffer(this)) return undefined
1263
- return this.buffer
1264
- }
1265
- });
1266
-
1267
- Object.defineProperty(Buffer.prototype, 'offset', {
1268
- enumerable: true,
1269
- get: function () {
1270
- if (!Buffer.isBuffer(this)) return undefined
1271
- return this.byteOffset
1272
- }
1273
- });
1274
-
1275
- function createBuffer (length) {
1276
- if (length > K_MAX_LENGTH) {
1277
- throw new RangeError('The value "' + length + '" is invalid for option "size"')
1278
- }
1279
- // Return an augmented `Uint8Array` instance
1280
- var buf = new Uint8Array(length);
1281
- Object.setPrototypeOf(buf, Buffer.prototype);
1282
- return buf
1283
- }
1284
-
1285
- /**
1286
- * The Buffer constructor returns instances of `Uint8Array` that have their
1287
- * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
1288
- * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
1289
- * and the `Uint8Array` methods. Square bracket notation works as expected -- it
1290
- * returns a single octet.
1291
- *
1292
- * The `Uint8Array` prototype remains unmodified.
1293
- */
1294
-
1295
- function Buffer (arg, encodingOrOffset, length) {
1296
- // Common case.
1297
- if (typeof arg === 'number') {
1298
- if (typeof encodingOrOffset === 'string') {
1299
- throw new TypeError(
1300
- 'The "string" argument must be of type string. Received type number'
1301
- )
1302
- }
1303
- return allocUnsafe(arg)
1304
- }
1305
- return from(arg, encodingOrOffset, length)
1306
- }
1307
-
1308
- Buffer.poolSize = 8192; // not used by this implementation
1309
-
1310
- function from (value, encodingOrOffset, length) {
1311
- if (typeof value === 'string') {
1312
- return fromString(value, encodingOrOffset)
1313
- }
1314
-
1315
- if (ArrayBuffer.isView(value)) {
1316
- return fromArrayView(value)
1317
- }
1318
-
1319
- if (value == null) {
1320
- throw new TypeError(
1321
- 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
1322
- 'or Array-like Object. Received type ' + (typeof value)
1323
- )
1324
- }
1325
-
1326
- if (isInstance(value, ArrayBuffer) ||
1327
- (value && isInstance(value.buffer, ArrayBuffer))) {
1328
- return fromArrayBuffer(value, encodingOrOffset, length)
1329
- }
1330
-
1331
- if (typeof SharedArrayBuffer !== 'undefined' &&
1332
- (isInstance(value, SharedArrayBuffer) ||
1333
- (value && isInstance(value.buffer, SharedArrayBuffer)))) {
1334
- return fromArrayBuffer(value, encodingOrOffset, length)
1335
- }
1336
-
1337
- if (typeof value === 'number') {
1338
- throw new TypeError(
1339
- 'The "value" argument must not be of type number. Received type number'
1340
- )
1341
- }
1342
-
1343
- var valueOf = value.valueOf && value.valueOf();
1344
- if (valueOf != null && valueOf !== value) {
1345
- return Buffer.from(valueOf, encodingOrOffset, length)
1346
- }
1347
-
1348
- var b = fromObject(value);
1349
- if (b) return b
1350
-
1351
- if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
1352
- typeof value[Symbol.toPrimitive] === 'function') {
1353
- return Buffer.from(
1354
- value[Symbol.toPrimitive]('string'), encodingOrOffset, length
1355
- )
1356
- }
1357
-
1358
- throw new TypeError(
1359
- 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
1360
- 'or Array-like Object. Received type ' + (typeof value)
1361
- )
1362
- }
1363
-
1364
- /**
1365
- * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
1366
- * if value is a number.
1367
- * Buffer.from(str[, encoding])
1368
- * Buffer.from(array)
1369
- * Buffer.from(buffer)
1370
- * Buffer.from(arrayBuffer[, byteOffset[, length]])
1371
- **/
1372
- Buffer.from = function (value, encodingOrOffset, length) {
1373
- return from(value, encodingOrOffset, length)
1374
- };
1375
-
1376
- // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
1377
- // https://github.com/feross/buffer/pull/148
1378
- Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
1379
- Object.setPrototypeOf(Buffer, Uint8Array);
1380
-
1381
- function assertSize (size) {
1382
- if (typeof size !== 'number') {
1383
- throw new TypeError('"size" argument must be of type number')
1384
- } else if (size < 0) {
1385
- throw new RangeError('The value "' + size + '" is invalid for option "size"')
1386
- }
1387
- }
1388
-
1389
- function alloc (size, fill, encoding) {
1390
- assertSize(size);
1391
- if (size <= 0) {
1392
- return createBuffer(size)
1393
- }
1394
- if (fill !== undefined) {
1395
- // Only pay attention to encoding if it's a string. This
1396
- // prevents accidentally sending in a number that would
1397
- // be interpreted as a start offset.
1398
- return typeof encoding === 'string'
1399
- ? createBuffer(size).fill(fill, encoding)
1400
- : createBuffer(size).fill(fill)
1401
- }
1402
- return createBuffer(size)
1403
- }
1404
-
1405
- /**
1406
- * Creates a new filled Buffer instance.
1407
- * alloc(size[, fill[, encoding]])
1408
- **/
1409
- Buffer.alloc = function (size, fill, encoding) {
1410
- return alloc(size, fill, encoding)
1411
- };
1412
-
1413
- function allocUnsafe (size) {
1414
- assertSize(size);
1415
- return createBuffer(size < 0 ? 0 : checked(size) | 0)
1416
- }
1417
-
1418
- /**
1419
- * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
1420
- * */
1421
- Buffer.allocUnsafe = function (size) {
1422
- return allocUnsafe(size)
1423
- };
1424
- /**
1425
- * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
1426
- */
1427
- Buffer.allocUnsafeSlow = function (size) {
1428
- return allocUnsafe(size)
1429
- };
1430
-
1431
- function fromString (string, encoding) {
1432
- if (typeof encoding !== 'string' || encoding === '') {
1433
- encoding = 'utf8';
1434
- }
1435
-
1436
- if (!Buffer.isEncoding(encoding)) {
1437
- throw new TypeError('Unknown encoding: ' + encoding)
1438
- }
1439
-
1440
- var length = byteLength(string, encoding) | 0;
1441
- var buf = createBuffer(length);
1442
-
1443
- var actual = buf.write(string, encoding);
1444
-
1445
- if (actual !== length) {
1446
- // Writing a hex string, for example, that contains invalid characters will
1447
- // cause everything after the first invalid character to be ignored. (e.g.
1448
- // 'abxxcd' will be treated as 'ab')
1449
- buf = buf.slice(0, actual);
1450
- }
1451
-
1452
- return buf
1453
- }
1454
-
1455
- function fromArrayLike (array) {
1456
- var length = array.length < 0 ? 0 : checked(array.length) | 0;
1457
- var buf = createBuffer(length);
1458
- for (var i = 0; i < length; i += 1) {
1459
- buf[i] = array[i] & 255;
1460
- }
1461
- return buf
1462
- }
1463
-
1464
- function fromArrayView (arrayView) {
1465
- if (isInstance(arrayView, Uint8Array)) {
1466
- var copy = new Uint8Array(arrayView);
1467
- return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
1468
- }
1469
- return fromArrayLike(arrayView)
1470
- }
1471
-
1472
- function fromArrayBuffer (array, byteOffset, length) {
1473
- if (byteOffset < 0 || array.byteLength < byteOffset) {
1474
- throw new RangeError('"offset" is outside of buffer bounds')
1475
- }
1476
-
1477
- if (array.byteLength < byteOffset + (length || 0)) {
1478
- throw new RangeError('"length" is outside of buffer bounds')
1479
- }
1480
-
1481
- var buf;
1482
- if (byteOffset === undefined && length === undefined) {
1483
- buf = new Uint8Array(array);
1484
- } else if (length === undefined) {
1485
- buf = new Uint8Array(array, byteOffset);
1486
- } else {
1487
- buf = new Uint8Array(array, byteOffset, length);
1488
- }
1489
-
1490
- // Return an augmented `Uint8Array` instance
1491
- Object.setPrototypeOf(buf, Buffer.prototype);
1492
-
1493
- return buf
1494
- }
1495
-
1496
- function fromObject (obj) {
1497
- if (Buffer.isBuffer(obj)) {
1498
- var len = checked(obj.length) | 0;
1499
- var buf = createBuffer(len);
1500
-
1501
- if (buf.length === 0) {
1502
- return buf
1503
- }
1504
-
1505
- obj.copy(buf, 0, 0, len);
1506
- return buf
1507
- }
1508
-
1509
- if (obj.length !== undefined) {
1510
- if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
1511
- return createBuffer(0)
1512
- }
1513
- return fromArrayLike(obj)
1514
- }
1515
-
1516
- if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
1517
- return fromArrayLike(obj.data)
1518
- }
1519
- }
1520
-
1521
- function checked (length) {
1522
- // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
1523
- // length is NaN (which is otherwise coerced to zero.)
1524
- if (length >= K_MAX_LENGTH) {
1525
- throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
1526
- 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
1527
- }
1528
- return length | 0
1529
- }
1530
-
1531
- function SlowBuffer (length) {
1532
- if (+length != length) { // eslint-disable-line eqeqeq
1533
- length = 0;
1534
- }
1535
- return Buffer.alloc(+length)
1536
- }
1537
-
1538
- Buffer.isBuffer = function isBuffer (b) {
1539
- return b != null && b._isBuffer === true &&
1540
- b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
1541
- };
1542
-
1543
- Buffer.compare = function compare (a, b) {
1544
- if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength);
1545
- if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength);
1546
- if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
1547
- throw new TypeError(
1548
- 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
1549
- )
1550
- }
1551
-
1552
- if (a === b) return 0
1553
-
1554
- var x = a.length;
1555
- var y = b.length;
1556
-
1557
- for (var i = 0, len = Math.min(x, y); i < len; ++i) {
1558
- if (a[i] !== b[i]) {
1559
- x = a[i];
1560
- y = b[i];
1561
- break
1562
- }
1563
- }
1564
-
1565
- if (x < y) return -1
1566
- if (y < x) return 1
1567
- return 0
1568
- };
1569
-
1570
- Buffer.isEncoding = function isEncoding (encoding) {
1571
- switch (String(encoding).toLowerCase()) {
1572
- case 'hex':
1573
- case 'utf8':
1574
- case 'utf-8':
1575
- case 'ascii':
1576
- case 'latin1':
1577
- case 'binary':
1578
- case 'base64':
1579
- case 'ucs2':
1580
- case 'ucs-2':
1581
- case 'utf16le':
1582
- case 'utf-16le':
1583
- return true
1584
- default:
1585
- return false
1586
- }
1587
- };
1588
-
1589
- Buffer.concat = function concat (list, length) {
1590
- if (!Array.isArray(list)) {
1591
- throw new TypeError('"list" argument must be an Array of Buffers')
1592
- }
1593
-
1594
- if (list.length === 0) {
1595
- return Buffer.alloc(0)
1596
- }
1597
-
1598
- var i;
1599
- if (length === undefined) {
1600
- length = 0;
1601
- for (i = 0; i < list.length; ++i) {
1602
- length += list[i].length;
1603
- }
1604
- }
1605
-
1606
- var buffer = Buffer.allocUnsafe(length);
1607
- var pos = 0;
1608
- for (i = 0; i < list.length; ++i) {
1609
- var buf = list[i];
1610
- if (isInstance(buf, Uint8Array)) {
1611
- if (pos + buf.length > buffer.length) {
1612
- Buffer.from(buf).copy(buffer, pos);
1613
- } else {
1614
- Uint8Array.prototype.set.call(
1615
- buffer,
1616
- buf,
1617
- pos
1618
- );
1619
- }
1620
- } else if (!Buffer.isBuffer(buf)) {
1621
- throw new TypeError('"list" argument must be an Array of Buffers')
1622
- } else {
1623
- buf.copy(buffer, pos);
1624
- }
1625
- pos += buf.length;
1626
- }
1627
- return buffer
1628
- };
1629
-
1630
- function byteLength (string, encoding) {
1631
- if (Buffer.isBuffer(string)) {
1632
- return string.length
1633
- }
1634
- if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
1635
- return string.byteLength
1636
- }
1637
- if (typeof string !== 'string') {
1638
- throw new TypeError(
1639
- 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
1640
- 'Received type ' + typeof string
1641
- )
1642
- }
1643
-
1644
- var len = string.length;
1645
- var mustMatch = (arguments.length > 2 && arguments[2] === true);
1646
- if (!mustMatch && len === 0) return 0
1647
-
1648
- // Use a for loop to avoid recursion
1649
- var loweredCase = false;
1650
- for (;;) {
1651
- switch (encoding) {
1652
- case 'ascii':
1653
- case 'latin1':
1654
- case 'binary':
1655
- return len
1656
- case 'utf8':
1657
- case 'utf-8':
1658
- return utf8ToBytes(string).length
1659
- case 'ucs2':
1660
- case 'ucs-2':
1661
- case 'utf16le':
1662
- case 'utf-16le':
1663
- return len * 2
1664
- case 'hex':
1665
- return len >>> 1
1666
- case 'base64':
1667
- return base64ToBytes(string).length
1668
- default:
1669
- if (loweredCase) {
1670
- return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
1671
- }
1672
- encoding = ('' + encoding).toLowerCase();
1673
- loweredCase = true;
1674
- }
1675
- }
1676
- }
1677
- Buffer.byteLength = byteLength;
1678
-
1679
- function slowToString (encoding, start, end) {
1680
- var loweredCase = false;
1681
-
1682
- // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
1683
- // property of a typed array.
1684
-
1685
- // This behaves neither like String nor Uint8Array in that we set start/end
1686
- // to their upper/lower bounds if the value passed is out of range.
1687
- // undefined is handled specially as per ECMA-262 6th Edition,
1688
- // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
1689
- if (start === undefined || start < 0) {
1690
- start = 0;
1691
- }
1692
- // Return early if start > this.length. Done here to prevent potential uint32
1693
- // coercion fail below.
1694
- if (start > this.length) {
1695
- return ''
1696
- }
1697
-
1698
- if (end === undefined || end > this.length) {
1699
- end = this.length;
1700
- }
1701
-
1702
- if (end <= 0) {
1703
- return ''
1704
- }
1705
-
1706
- // Force coercion to uint32. This will also coerce falsey/NaN values to 0.
1707
- end >>>= 0;
1708
- start >>>= 0;
1709
-
1710
- if (end <= start) {
1711
- return ''
1712
- }
1713
-
1714
- if (!encoding) encoding = 'utf8';
1715
-
1716
- while (true) {
1717
- switch (encoding) {
1718
- case 'hex':
1719
- return hexSlice(this, start, end)
1720
-
1721
- case 'utf8':
1722
- case 'utf-8':
1723
- return utf8Slice(this, start, end)
1724
-
1725
- case 'ascii':
1726
- return asciiSlice(this, start, end)
1727
-
1728
- case 'latin1':
1729
- case 'binary':
1730
- return latin1Slice(this, start, end)
1731
-
1732
- case 'base64':
1733
- return base64Slice(this, start, end)
1734
-
1735
- case 'ucs2':
1736
- case 'ucs-2':
1737
- case 'utf16le':
1738
- case 'utf-16le':
1739
- return utf16leSlice(this, start, end)
1740
-
1741
- default:
1742
- if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
1743
- encoding = (encoding + '').toLowerCase();
1744
- loweredCase = true;
1745
- }
1746
- }
1747
- }
1748
-
1749
- // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
1750
- // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
1751
- // reliably in a browserify context because there could be multiple different
1752
- // copies of the 'buffer' package in use. This method works even for Buffer
1753
- // instances that were created from another copy of the `buffer` package.
1754
- // See: https://github.com/feross/buffer/issues/154
1755
- Buffer.prototype._isBuffer = true;
1756
-
1757
- function swap (b, n, m) {
1758
- var i = b[n];
1759
- b[n] = b[m];
1760
- b[m] = i;
1761
- }
1762
-
1763
- Buffer.prototype.swap16 = function swap16 () {
1764
- var len = this.length;
1765
- if (len % 2 !== 0) {
1766
- throw new RangeError('Buffer size must be a multiple of 16-bits')
1767
- }
1768
- for (var i = 0; i < len; i += 2) {
1769
- swap(this, i, i + 1);
1770
- }
1771
- return this
1772
- };
1773
-
1774
- Buffer.prototype.swap32 = function swap32 () {
1775
- var len = this.length;
1776
- if (len % 4 !== 0) {
1777
- throw new RangeError('Buffer size must be a multiple of 32-bits')
1778
- }
1779
- for (var i = 0; i < len; i += 4) {
1780
- swap(this, i, i + 3);
1781
- swap(this, i + 1, i + 2);
1782
- }
1783
- return this
1784
- };
1785
-
1786
- Buffer.prototype.swap64 = function swap64 () {
1787
- var len = this.length;
1788
- if (len % 8 !== 0) {
1789
- throw new RangeError('Buffer size must be a multiple of 64-bits')
1790
- }
1791
- for (var i = 0; i < len; i += 8) {
1792
- swap(this, i, i + 7);
1793
- swap(this, i + 1, i + 6);
1794
- swap(this, i + 2, i + 5);
1795
- swap(this, i + 3, i + 4);
1796
- }
1797
- return this
1798
- };
1799
-
1800
- Buffer.prototype.toString = function toString () {
1801
- var length = this.length;
1802
- if (length === 0) return ''
1803
- if (arguments.length === 0) return utf8Slice(this, 0, length)
1804
- return slowToString.apply(this, arguments)
1805
- };
1806
-
1807
- Buffer.prototype.toLocaleString = Buffer.prototype.toString;
1808
-
1809
- Buffer.prototype.equals = function equals (b) {
1810
- if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
1811
- if (this === b) return true
1812
- return Buffer.compare(this, b) === 0
1813
- };
1814
-
1815
- Buffer.prototype.inspect = function inspect () {
1816
- var str = '';
1817
- var max = exports.INSPECT_MAX_BYTES;
1818
- str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
1819
- if (this.length > max) str += ' ... ';
1820
- return '<Buffer ' + str + '>'
1821
- };
1822
- if (customInspectSymbol) {
1823
- Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;
1824
- }
1825
-
1826
- Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
1827
- if (isInstance(target, Uint8Array)) {
1828
- target = Buffer.from(target, target.offset, target.byteLength);
1829
- }
1830
- if (!Buffer.isBuffer(target)) {
1831
- throw new TypeError(
1832
- 'The "target" argument must be one of type Buffer or Uint8Array. ' +
1833
- 'Received type ' + (typeof target)
1834
- )
1835
- }
1836
-
1837
- if (start === undefined) {
1838
- start = 0;
1839
- }
1840
- if (end === undefined) {
1841
- end = target ? target.length : 0;
1842
- }
1843
- if (thisStart === undefined) {
1844
- thisStart = 0;
1845
- }
1846
- if (thisEnd === undefined) {
1847
- thisEnd = this.length;
1848
- }
1849
-
1850
- if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
1851
- throw new RangeError('out of range index')
1852
- }
1853
-
1854
- if (thisStart >= thisEnd && start >= end) {
1855
- return 0
1856
- }
1857
- if (thisStart >= thisEnd) {
1858
- return -1
1859
- }
1860
- if (start >= end) {
1861
- return 1
1862
- }
1863
-
1864
- start >>>= 0;
1865
- end >>>= 0;
1866
- thisStart >>>= 0;
1867
- thisEnd >>>= 0;
1868
-
1869
- if (this === target) return 0
1870
-
1871
- var x = thisEnd - thisStart;
1872
- var y = end - start;
1873
- var len = Math.min(x, y);
1874
-
1875
- var thisCopy = this.slice(thisStart, thisEnd);
1876
- var targetCopy = target.slice(start, end);
1877
-
1878
- for (var i = 0; i < len; ++i) {
1879
- if (thisCopy[i] !== targetCopy[i]) {
1880
- x = thisCopy[i];
1881
- y = targetCopy[i];
1882
- break
1883
- }
1884
- }
1885
-
1886
- if (x < y) return -1
1887
- if (y < x) return 1
1888
- return 0
1889
- };
1890
-
1891
- // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
1892
- // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
1893
- //
1894
- // Arguments:
1895
- // - buffer - a Buffer to search
1896
- // - val - a string, Buffer, or number
1897
- // - byteOffset - an index into `buffer`; will be clamped to an int32
1898
- // - encoding - an optional encoding, relevant is val is a string
1899
- // - dir - true for indexOf, false for lastIndexOf
1900
- function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
1901
- // Empty buffer means no match
1902
- if (buffer.length === 0) return -1
1903
-
1904
- // Normalize byteOffset
1905
- if (typeof byteOffset === 'string') {
1906
- encoding = byteOffset;
1907
- byteOffset = 0;
1908
- } else if (byteOffset > 0x7fffffff) {
1909
- byteOffset = 0x7fffffff;
1910
- } else if (byteOffset < -2147483648) {
1911
- byteOffset = -2147483648;
1912
- }
1913
- byteOffset = +byteOffset; // Coerce to Number.
1914
- if (numberIsNaN(byteOffset)) {
1915
- // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
1916
- byteOffset = dir ? 0 : (buffer.length - 1);
1917
- }
1918
-
1919
- // Normalize byteOffset: negative offsets start from the end of the buffer
1920
- if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
1921
- if (byteOffset >= buffer.length) {
1922
- if (dir) return -1
1923
- else byteOffset = buffer.length - 1;
1924
- } else if (byteOffset < 0) {
1925
- if (dir) byteOffset = 0;
1926
- else return -1
1927
- }
1928
-
1929
- // Normalize val
1930
- if (typeof val === 'string') {
1931
- val = Buffer.from(val, encoding);
1932
- }
1933
-
1934
- // Finally, search either indexOf (if dir is true) or lastIndexOf
1935
- if (Buffer.isBuffer(val)) {
1936
- // Special case: looking for empty string/buffer always fails
1937
- if (val.length === 0) {
1938
- return -1
1939
- }
1940
- return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
1941
- } else if (typeof val === 'number') {
1942
- val = val & 0xFF; // Search for a byte value [0-255]
1943
- if (typeof Uint8Array.prototype.indexOf === 'function') {
1944
- if (dir) {
1945
- return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
1946
- } else {
1947
- return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
1948
- }
1949
- }
1950
- return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
1951
- }
1952
-
1953
- throw new TypeError('val must be string, number or Buffer')
1954
- }
1955
-
1956
- function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
1957
- var indexSize = 1;
1958
- var arrLength = arr.length;
1959
- var valLength = val.length;
1960
-
1961
- if (encoding !== undefined) {
1962
- encoding = String(encoding).toLowerCase();
1963
- if (encoding === 'ucs2' || encoding === 'ucs-2' ||
1964
- encoding === 'utf16le' || encoding === 'utf-16le') {
1965
- if (arr.length < 2 || val.length < 2) {
1966
- return -1
1967
- }
1968
- indexSize = 2;
1969
- arrLength /= 2;
1970
- valLength /= 2;
1971
- byteOffset /= 2;
1972
- }
1973
- }
1974
-
1975
- function read (buf, i) {
1976
- if (indexSize === 1) {
1977
- return buf[i]
1978
- } else {
1979
- return buf.readUInt16BE(i * indexSize)
1980
- }
1981
- }
1982
-
1983
- var i;
1984
- if (dir) {
1985
- var foundIndex = -1;
1986
- for (i = byteOffset; i < arrLength; i++) {
1987
- if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
1988
- if (foundIndex === -1) foundIndex = i;
1989
- if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
1990
- } else {
1991
- if (foundIndex !== -1) i -= i - foundIndex;
1992
- foundIndex = -1;
1993
- }
1994
- }
1995
- } else {
1996
- if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
1997
- for (i = byteOffset; i >= 0; i--) {
1998
- var found = true;
1999
- for (var j = 0; j < valLength; j++) {
2000
- if (read(arr, i + j) !== read(val, j)) {
2001
- found = false;
2002
- break
2003
- }
2004
- }
2005
- if (found) return i
2006
- }
2007
- }
2008
-
2009
- return -1
2010
- }
2011
-
2012
- Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
2013
- return this.indexOf(val, byteOffset, encoding) !== -1
2014
- };
2015
-
2016
- Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
2017
- return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
2018
- };
2019
-
2020
- Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
2021
- return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
2022
- };
2023
-
2024
- function hexWrite (buf, string, offset, length) {
2025
- offset = Number(offset) || 0;
2026
- var remaining = buf.length - offset;
2027
- if (!length) {
2028
- length = remaining;
2029
- } else {
2030
- length = Number(length);
2031
- if (length > remaining) {
2032
- length = remaining;
2033
- }
2034
- }
2035
-
2036
- var strLen = string.length;
2037
-
2038
- if (length > strLen / 2) {
2039
- length = strLen / 2;
2040
- }
2041
- for (var i = 0; i < length; ++i) {
2042
- var parsed = parseInt(string.substr(i * 2, 2), 16);
2043
- if (numberIsNaN(parsed)) return i
2044
- buf[offset + i] = parsed;
2045
- }
2046
- return i
2047
- }
2048
-
2049
- function utf8Write (buf, string, offset, length) {
2050
- return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
2051
- }
2052
-
2053
- function asciiWrite (buf, string, offset, length) {
2054
- return blitBuffer(asciiToBytes(string), buf, offset, length)
2055
- }
2056
-
2057
- function base64Write (buf, string, offset, length) {
2058
- return blitBuffer(base64ToBytes(string), buf, offset, length)
2059
- }
2060
-
2061
- function ucs2Write (buf, string, offset, length) {
2062
- return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
2063
- }
2064
-
2065
- Buffer.prototype.write = function write (string, offset, length, encoding) {
2066
- // Buffer#write(string)
2067
- if (offset === undefined) {
2068
- encoding = 'utf8';
2069
- length = this.length;
2070
- offset = 0;
2071
- // Buffer#write(string, encoding)
2072
- } else if (length === undefined && typeof offset === 'string') {
2073
- encoding = offset;
2074
- length = this.length;
2075
- offset = 0;
2076
- // Buffer#write(string, offset[, length][, encoding])
2077
- } else if (isFinite(offset)) {
2078
- offset = offset >>> 0;
2079
- if (isFinite(length)) {
2080
- length = length >>> 0;
2081
- if (encoding === undefined) encoding = 'utf8';
2082
- } else {
2083
- encoding = length;
2084
- length = undefined;
2085
- }
2086
- } else {
2087
- throw new Error(
2088
- 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
2089
- )
2090
- }
2091
-
2092
- var remaining = this.length - offset;
2093
- if (length === undefined || length > remaining) length = remaining;
2094
-
2095
- if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
2096
- throw new RangeError('Attempt to write outside buffer bounds')
2097
- }
2098
-
2099
- if (!encoding) encoding = 'utf8';
2100
-
2101
- var loweredCase = false;
2102
- for (;;) {
2103
- switch (encoding) {
2104
- case 'hex':
2105
- return hexWrite(this, string, offset, length)
2106
-
2107
- case 'utf8':
2108
- case 'utf-8':
2109
- return utf8Write(this, string, offset, length)
2110
-
2111
- case 'ascii':
2112
- case 'latin1':
2113
- case 'binary':
2114
- return asciiWrite(this, string, offset, length)
2115
-
2116
- case 'base64':
2117
- // Warning: maxLength not taken into account in base64Write
2118
- return base64Write(this, string, offset, length)
2119
-
2120
- case 'ucs2':
2121
- case 'ucs-2':
2122
- case 'utf16le':
2123
- case 'utf-16le':
2124
- return ucs2Write(this, string, offset, length)
2125
-
2126
- default:
2127
- if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
2128
- encoding = ('' + encoding).toLowerCase();
2129
- loweredCase = true;
2130
- }
2131
- }
2132
- };
2133
-
2134
- Buffer.prototype.toJSON = function toJSON () {
2135
- return {
2136
- type: 'Buffer',
2137
- data: Array.prototype.slice.call(this._arr || this, 0)
2138
- }
2139
- };
2140
-
2141
- function base64Slice (buf, start, end) {
2142
- if (start === 0 && end === buf.length) {
2143
- return base64.fromByteArray(buf)
2144
- } else {
2145
- return base64.fromByteArray(buf.slice(start, end))
2146
- }
2147
- }
2148
-
2149
- function utf8Slice (buf, start, end) {
2150
- end = Math.min(buf.length, end);
2151
- var res = [];
2152
-
2153
- var i = start;
2154
- while (i < end) {
2155
- var firstByte = buf[i];
2156
- var codePoint = null;
2157
- var bytesPerSequence = (firstByte > 0xEF)
2158
- ? 4
2159
- : (firstByte > 0xDF)
2160
- ? 3
2161
- : (firstByte > 0xBF)
2162
- ? 2
2163
- : 1;
2164
-
2165
- if (i + bytesPerSequence <= end) {
2166
- var secondByte, thirdByte, fourthByte, tempCodePoint;
2167
-
2168
- switch (bytesPerSequence) {
2169
- case 1:
2170
- if (firstByte < 0x80) {
2171
- codePoint = firstByte;
2172
- }
2173
- break
2174
- case 2:
2175
- secondByte = buf[i + 1];
2176
- if ((secondByte & 0xC0) === 0x80) {
2177
- tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
2178
- if (tempCodePoint > 0x7F) {
2179
- codePoint = tempCodePoint;
2180
- }
2181
- }
2182
- break
2183
- case 3:
2184
- secondByte = buf[i + 1];
2185
- thirdByte = buf[i + 2];
2186
- if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
2187
- tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
2188
- if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
2189
- codePoint = tempCodePoint;
2190
- }
2191
- }
2192
- break
2193
- case 4:
2194
- secondByte = buf[i + 1];
2195
- thirdByte = buf[i + 2];
2196
- fourthByte = buf[i + 3];
2197
- if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
2198
- tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
2199
- if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
2200
- codePoint = tempCodePoint;
2201
- }
2202
- }
2203
- }
2204
- }
2205
-
2206
- if (codePoint === null) {
2207
- // we did not generate a valid codePoint so insert a
2208
- // replacement char (U+FFFD) and advance only 1 byte
2209
- codePoint = 0xFFFD;
2210
- bytesPerSequence = 1;
2211
- } else if (codePoint > 0xFFFF) {
2212
- // encode to utf16 (surrogate pair dance)
2213
- codePoint -= 0x10000;
2214
- res.push(codePoint >>> 10 & 0x3FF | 0xD800);
2215
- codePoint = 0xDC00 | codePoint & 0x3FF;
2216
- }
2217
-
2218
- res.push(codePoint);
2219
- i += bytesPerSequence;
2220
- }
2221
-
2222
- return decodeCodePointsArray(res)
2223
- }
2224
-
2225
- // Based on http://stackoverflow.com/a/22747272/680742, the browser with
2226
- // the lowest limit is Chrome, with 0x10000 args.
2227
- // We go 1 magnitude less, for safety
2228
- var MAX_ARGUMENTS_LENGTH = 0x1000;
2229
-
2230
- function decodeCodePointsArray (codePoints) {
2231
- var len = codePoints.length;
2232
- if (len <= MAX_ARGUMENTS_LENGTH) {
2233
- return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
2234
- }
2235
-
2236
- // Decode in chunks to avoid "call stack size exceeded".
2237
- var res = '';
2238
- var i = 0;
2239
- while (i < len) {
2240
- res += String.fromCharCode.apply(
2241
- String,
2242
- codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
2243
- );
2244
- }
2245
- return res
2246
- }
2247
-
2248
- function asciiSlice (buf, start, end) {
2249
- var ret = '';
2250
- end = Math.min(buf.length, end);
2251
-
2252
- for (var i = start; i < end; ++i) {
2253
- ret += String.fromCharCode(buf[i] & 0x7F);
2254
- }
2255
- return ret
2256
- }
2257
-
2258
- function latin1Slice (buf, start, end) {
2259
- var ret = '';
2260
- end = Math.min(buf.length, end);
2261
-
2262
- for (var i = start; i < end; ++i) {
2263
- ret += String.fromCharCode(buf[i]);
2264
- }
2265
- return ret
2266
- }
2267
-
2268
- function hexSlice (buf, start, end) {
2269
- var len = buf.length;
2270
-
2271
- if (!start || start < 0) start = 0;
2272
- if (!end || end < 0 || end > len) end = len;
2273
-
2274
- var out = '';
2275
- for (var i = start; i < end; ++i) {
2276
- out += hexSliceLookupTable[buf[i]];
2277
- }
2278
- return out
2279
- }
2280
-
2281
- function utf16leSlice (buf, start, end) {
2282
- var bytes = buf.slice(start, end);
2283
- var res = '';
2284
- // If bytes.length is odd, the last 8 bits must be ignored (same as node.js)
2285
- for (var i = 0; i < bytes.length - 1; i += 2) {
2286
- res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256));
2287
- }
2288
- return res
2289
- }
2290
-
2291
- Buffer.prototype.slice = function slice (start, end) {
2292
- var len = this.length;
2293
- start = ~~start;
2294
- end = end === undefined ? len : ~~end;
2295
-
2296
- if (start < 0) {
2297
- start += len;
2298
- if (start < 0) start = 0;
2299
- } else if (start > len) {
2300
- start = len;
2301
- }
2302
-
2303
- if (end < 0) {
2304
- end += len;
2305
- if (end < 0) end = 0;
2306
- } else if (end > len) {
2307
- end = len;
2308
- }
2309
-
2310
- if (end < start) end = start;
2311
-
2312
- var newBuf = this.subarray(start, end);
2313
- // Return an augmented `Uint8Array` instance
2314
- Object.setPrototypeOf(newBuf, Buffer.prototype);
2315
-
2316
- return newBuf
2317
- };
2318
-
2319
- /*
2320
- * Need to make sure that buffer isn't trying to write out of bounds.
2321
- */
2322
- function checkOffset (offset, ext, length) {
2323
- if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
2324
- if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
2325
- }
2326
-
2327
- Buffer.prototype.readUintLE =
2328
- Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
2329
- offset = offset >>> 0;
2330
- byteLength = byteLength >>> 0;
2331
- if (!noAssert) checkOffset(offset, byteLength, this.length);
2332
-
2333
- var val = this[offset];
2334
- var mul = 1;
2335
- var i = 0;
2336
- while (++i < byteLength && (mul *= 0x100)) {
2337
- val += this[offset + i] * mul;
2338
- }
2339
-
2340
- return val
2341
- };
2342
-
2343
- Buffer.prototype.readUintBE =
2344
- Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
2345
- offset = offset >>> 0;
2346
- byteLength = byteLength >>> 0;
2347
- if (!noAssert) {
2348
- checkOffset(offset, byteLength, this.length);
2349
- }
2350
-
2351
- var val = this[offset + --byteLength];
2352
- var mul = 1;
2353
- while (byteLength > 0 && (mul *= 0x100)) {
2354
- val += this[offset + --byteLength] * mul;
2355
- }
2356
-
2357
- return val
2358
- };
2359
-
2360
- Buffer.prototype.readUint8 =
2361
- Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
2362
- offset = offset >>> 0;
2363
- if (!noAssert) checkOffset(offset, 1, this.length);
2364
- return this[offset]
2365
- };
2366
-
2367
- Buffer.prototype.readUint16LE =
2368
- Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
2369
- offset = offset >>> 0;
2370
- if (!noAssert) checkOffset(offset, 2, this.length);
2371
- return this[offset] | (this[offset + 1] << 8)
2372
- };
2373
-
2374
- Buffer.prototype.readUint16BE =
2375
- Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
2376
- offset = offset >>> 0;
2377
- if (!noAssert) checkOffset(offset, 2, this.length);
2378
- return (this[offset] << 8) | this[offset + 1]
2379
- };
2380
-
2381
- Buffer.prototype.readUint32LE =
2382
- Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
2383
- offset = offset >>> 0;
2384
- if (!noAssert) checkOffset(offset, 4, this.length);
2385
-
2386
- return ((this[offset]) |
2387
- (this[offset + 1] << 8) |
2388
- (this[offset + 2] << 16)) +
2389
- (this[offset + 3] * 0x1000000)
2390
- };
2391
-
2392
- Buffer.prototype.readUint32BE =
2393
- Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
2394
- offset = offset >>> 0;
2395
- if (!noAssert) checkOffset(offset, 4, this.length);
2396
-
2397
- return (this[offset] * 0x1000000) +
2398
- ((this[offset + 1] << 16) |
2399
- (this[offset + 2] << 8) |
2400
- this[offset + 3])
2401
- };
2402
-
2403
- Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
2404
- offset = offset >>> 0;
2405
- byteLength = byteLength >>> 0;
2406
- if (!noAssert) checkOffset(offset, byteLength, this.length);
2407
-
2408
- var val = this[offset];
2409
- var mul = 1;
2410
- var i = 0;
2411
- while (++i < byteLength && (mul *= 0x100)) {
2412
- val += this[offset + i] * mul;
2413
- }
2414
- mul *= 0x80;
2415
-
2416
- if (val >= mul) val -= Math.pow(2, 8 * byteLength);
2417
-
2418
- return val
2419
- };
2420
-
2421
- Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
2422
- offset = offset >>> 0;
2423
- byteLength = byteLength >>> 0;
2424
- if (!noAssert) checkOffset(offset, byteLength, this.length);
2425
-
2426
- var i = byteLength;
2427
- var mul = 1;
2428
- var val = this[offset + --i];
2429
- while (i > 0 && (mul *= 0x100)) {
2430
- val += this[offset + --i] * mul;
2431
- }
2432
- mul *= 0x80;
2433
-
2434
- if (val >= mul) val -= Math.pow(2, 8 * byteLength);
2435
-
2436
- return val
2437
- };
2438
-
2439
- Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
2440
- offset = offset >>> 0;
2441
- if (!noAssert) checkOffset(offset, 1, this.length);
2442
- if (!(this[offset] & 0x80)) return (this[offset])
2443
- return ((0xff - this[offset] + 1) * -1)
2444
- };
2445
-
2446
- Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
2447
- offset = offset >>> 0;
2448
- if (!noAssert) checkOffset(offset, 2, this.length);
2449
- var val = this[offset] | (this[offset + 1] << 8);
2450
- return (val & 0x8000) ? val | 0xFFFF0000 : val
2451
- };
2452
-
2453
- Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
2454
- offset = offset >>> 0;
2455
- if (!noAssert) checkOffset(offset, 2, this.length);
2456
- var val = this[offset + 1] | (this[offset] << 8);
2457
- return (val & 0x8000) ? val | 0xFFFF0000 : val
2458
- };
2459
-
2460
- Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
2461
- offset = offset >>> 0;
2462
- if (!noAssert) checkOffset(offset, 4, this.length);
2463
-
2464
- return (this[offset]) |
2465
- (this[offset + 1] << 8) |
2466
- (this[offset + 2] << 16) |
2467
- (this[offset + 3] << 24)
2468
- };
2469
-
2470
- Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
2471
- offset = offset >>> 0;
2472
- if (!noAssert) checkOffset(offset, 4, this.length);
2473
-
2474
- return (this[offset] << 24) |
2475
- (this[offset + 1] << 16) |
2476
- (this[offset + 2] << 8) |
2477
- (this[offset + 3])
2478
- };
2479
-
2480
- Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
2481
- offset = offset >>> 0;
2482
- if (!noAssert) checkOffset(offset, 4, this.length);
2483
- return ieee754.read(this, offset, true, 23, 4)
2484
- };
2485
-
2486
- Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
2487
- offset = offset >>> 0;
2488
- if (!noAssert) checkOffset(offset, 4, this.length);
2489
- return ieee754.read(this, offset, false, 23, 4)
2490
- };
2491
-
2492
- Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
2493
- offset = offset >>> 0;
2494
- if (!noAssert) checkOffset(offset, 8, this.length);
2495
- return ieee754.read(this, offset, true, 52, 8)
2496
- };
2497
-
2498
- Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
2499
- offset = offset >>> 0;
2500
- if (!noAssert) checkOffset(offset, 8, this.length);
2501
- return ieee754.read(this, offset, false, 52, 8)
2502
- };
2503
-
2504
- function checkInt (buf, value, offset, ext, max, min) {
2505
- if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
2506
- if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
2507
- if (offset + ext > buf.length) throw new RangeError('Index out of range')
2508
- }
2509
-
2510
- Buffer.prototype.writeUintLE =
2511
- Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
2512
- value = +value;
2513
- offset = offset >>> 0;
2514
- byteLength = byteLength >>> 0;
2515
- if (!noAssert) {
2516
- var maxBytes = Math.pow(2, 8 * byteLength) - 1;
2517
- checkInt(this, value, offset, byteLength, maxBytes, 0);
2518
- }
2519
-
2520
- var mul = 1;
2521
- var i = 0;
2522
- this[offset] = value & 0xFF;
2523
- while (++i < byteLength && (mul *= 0x100)) {
2524
- this[offset + i] = (value / mul) & 0xFF;
2525
- }
2526
-
2527
- return offset + byteLength
2528
- };
2529
-
2530
- Buffer.prototype.writeUintBE =
2531
- Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
2532
- value = +value;
2533
- offset = offset >>> 0;
2534
- byteLength = byteLength >>> 0;
2535
- if (!noAssert) {
2536
- var maxBytes = Math.pow(2, 8 * byteLength) - 1;
2537
- checkInt(this, value, offset, byteLength, maxBytes, 0);
2538
- }
2539
-
2540
- var i = byteLength - 1;
2541
- var mul = 1;
2542
- this[offset + i] = value & 0xFF;
2543
- while (--i >= 0 && (mul *= 0x100)) {
2544
- this[offset + i] = (value / mul) & 0xFF;
2545
- }
2546
-
2547
- return offset + byteLength
2548
- };
2549
-
2550
- Buffer.prototype.writeUint8 =
2551
- Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
2552
- value = +value;
2553
- offset = offset >>> 0;
2554
- if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
2555
- this[offset] = (value & 0xff);
2556
- return offset + 1
2557
- };
2558
-
2559
- Buffer.prototype.writeUint16LE =
2560
- Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
2561
- value = +value;
2562
- offset = offset >>> 0;
2563
- if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
2564
- this[offset] = (value & 0xff);
2565
- this[offset + 1] = (value >>> 8);
2566
- return offset + 2
2567
- };
2568
-
2569
- Buffer.prototype.writeUint16BE =
2570
- Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
2571
- value = +value;
2572
- offset = offset >>> 0;
2573
- if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
2574
- this[offset] = (value >>> 8);
2575
- this[offset + 1] = (value & 0xff);
2576
- return offset + 2
2577
- };
2578
-
2579
- Buffer.prototype.writeUint32LE =
2580
- Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
2581
- value = +value;
2582
- offset = offset >>> 0;
2583
- if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
2584
- this[offset + 3] = (value >>> 24);
2585
- this[offset + 2] = (value >>> 16);
2586
- this[offset + 1] = (value >>> 8);
2587
- this[offset] = (value & 0xff);
2588
- return offset + 4
2589
- };
2590
-
2591
- Buffer.prototype.writeUint32BE =
2592
- Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
2593
- value = +value;
2594
- offset = offset >>> 0;
2595
- if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
2596
- this[offset] = (value >>> 24);
2597
- this[offset + 1] = (value >>> 16);
2598
- this[offset + 2] = (value >>> 8);
2599
- this[offset + 3] = (value & 0xff);
2600
- return offset + 4
2601
- };
2602
-
2603
- Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
2604
- value = +value;
2605
- offset = offset >>> 0;
2606
- if (!noAssert) {
2607
- var limit = Math.pow(2, (8 * byteLength) - 1);
2608
-
2609
- checkInt(this, value, offset, byteLength, limit - 1, -limit);
2610
- }
2611
-
2612
- var i = 0;
2613
- var mul = 1;
2614
- var sub = 0;
2615
- this[offset] = value & 0xFF;
2616
- while (++i < byteLength && (mul *= 0x100)) {
2617
- if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
2618
- sub = 1;
2619
- }
2620
- this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
2621
- }
2622
-
2623
- return offset + byteLength
2624
- };
2625
-
2626
- Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
2627
- value = +value;
2628
- offset = offset >>> 0;
2629
- if (!noAssert) {
2630
- var limit = Math.pow(2, (8 * byteLength) - 1);
2631
-
2632
- checkInt(this, value, offset, byteLength, limit - 1, -limit);
2633
- }
2634
-
2635
- var i = byteLength - 1;
2636
- var mul = 1;
2637
- var sub = 0;
2638
- this[offset + i] = value & 0xFF;
2639
- while (--i >= 0 && (mul *= 0x100)) {
2640
- if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
2641
- sub = 1;
2642
- }
2643
- this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
2644
- }
2645
-
2646
- return offset + byteLength
2647
- };
2648
-
2649
- Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
2650
- value = +value;
2651
- offset = offset >>> 0;
2652
- if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -128);
2653
- if (value < 0) value = 0xff + value + 1;
2654
- this[offset] = (value & 0xff);
2655
- return offset + 1
2656
- };
2657
-
2658
- Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
2659
- value = +value;
2660
- offset = offset >>> 0;
2661
- if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
2662
- this[offset] = (value & 0xff);
2663
- this[offset + 1] = (value >>> 8);
2664
- return offset + 2
2665
- };
2666
-
2667
- Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
2668
- value = +value;
2669
- offset = offset >>> 0;
2670
- if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
2671
- this[offset] = (value >>> 8);
2672
- this[offset + 1] = (value & 0xff);
2673
- return offset + 2
2674
- };
2675
-
2676
- Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
2677
- value = +value;
2678
- offset = offset >>> 0;
2679
- if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
2680
- this[offset] = (value & 0xff);
2681
- this[offset + 1] = (value >>> 8);
2682
- this[offset + 2] = (value >>> 16);
2683
- this[offset + 3] = (value >>> 24);
2684
- return offset + 4
2685
- };
2686
-
2687
- Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
2688
- value = +value;
2689
- offset = offset >>> 0;
2690
- if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
2691
- if (value < 0) value = 0xffffffff + value + 1;
2692
- this[offset] = (value >>> 24);
2693
- this[offset + 1] = (value >>> 16);
2694
- this[offset + 2] = (value >>> 8);
2695
- this[offset + 3] = (value & 0xff);
2696
- return offset + 4
2697
- };
2698
-
2699
- function checkIEEE754 (buf, value, offset, ext, max, min) {
2700
- if (offset + ext > buf.length) throw new RangeError('Index out of range')
2701
- if (offset < 0) throw new RangeError('Index out of range')
2702
- }
2703
-
2704
- function writeFloat (buf, value, offset, littleEndian, noAssert) {
2705
- value = +value;
2706
- offset = offset >>> 0;
2707
- if (!noAssert) {
2708
- checkIEEE754(buf, value, offset, 4);
2709
- }
2710
- ieee754.write(buf, value, offset, littleEndian, 23, 4);
2711
- return offset + 4
2712
- }
2713
-
2714
- Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
2715
- return writeFloat(this, value, offset, true, noAssert)
2716
- };
2717
-
2718
- Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
2719
- return writeFloat(this, value, offset, false, noAssert)
2720
- };
2721
-
2722
- function writeDouble (buf, value, offset, littleEndian, noAssert) {
2723
- value = +value;
2724
- offset = offset >>> 0;
2725
- if (!noAssert) {
2726
- checkIEEE754(buf, value, offset, 8);
2727
- }
2728
- ieee754.write(buf, value, offset, littleEndian, 52, 8);
2729
- return offset + 8
2730
- }
2731
-
2732
- Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
2733
- return writeDouble(this, value, offset, true, noAssert)
2734
- };
2735
-
2736
- Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
2737
- return writeDouble(this, value, offset, false, noAssert)
2738
- };
2739
-
2740
- // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
2741
- Buffer.prototype.copy = function copy (target, targetStart, start, end) {
2742
- if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
2743
- if (!start) start = 0;
2744
- if (!end && end !== 0) end = this.length;
2745
- if (targetStart >= target.length) targetStart = target.length;
2746
- if (!targetStart) targetStart = 0;
2747
- if (end > 0 && end < start) end = start;
2748
-
2749
- // Copy 0 bytes; we're done
2750
- if (end === start) return 0
2751
- if (target.length === 0 || this.length === 0) return 0
2752
-
2753
- // Fatal error conditions
2754
- if (targetStart < 0) {
2755
- throw new RangeError('targetStart out of bounds')
2756
- }
2757
- if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
2758
- if (end < 0) throw new RangeError('sourceEnd out of bounds')
2759
-
2760
- // Are we oob?
2761
- if (end > this.length) end = this.length;
2762
- if (target.length - targetStart < end - start) {
2763
- end = target.length - targetStart + start;
2764
- }
2765
-
2766
- var len = end - start;
2767
-
2768
- if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
2769
- // Use built-in when available, missing from IE11
2770
- this.copyWithin(targetStart, start, end);
2771
- } else {
2772
- Uint8Array.prototype.set.call(
2773
- target,
2774
- this.subarray(start, end),
2775
- targetStart
2776
- );
2777
- }
2778
-
2779
- return len
2780
- };
2781
-
2782
- // Usage:
2783
- // buffer.fill(number[, offset[, end]])
2784
- // buffer.fill(buffer[, offset[, end]])
2785
- // buffer.fill(string[, offset[, end]][, encoding])
2786
- Buffer.prototype.fill = function fill (val, start, end, encoding) {
2787
- // Handle string cases:
2788
- if (typeof val === 'string') {
2789
- if (typeof start === 'string') {
2790
- encoding = start;
2791
- start = 0;
2792
- end = this.length;
2793
- } else if (typeof end === 'string') {
2794
- encoding = end;
2795
- end = this.length;
2796
- }
2797
- if (encoding !== undefined && typeof encoding !== 'string') {
2798
- throw new TypeError('encoding must be a string')
2799
- }
2800
- if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
2801
- throw new TypeError('Unknown encoding: ' + encoding)
2802
- }
2803
- if (val.length === 1) {
2804
- var code = val.charCodeAt(0);
2805
- if ((encoding === 'utf8' && code < 128) ||
2806
- encoding === 'latin1') {
2807
- // Fast path: If `val` fits into a single byte, use that numeric value.
2808
- val = code;
2809
- }
2810
- }
2811
- } else if (typeof val === 'number') {
2812
- val = val & 255;
2813
- } else if (typeof val === 'boolean') {
2814
- val = Number(val);
2815
- }
2816
-
2817
- // Invalid ranges are not set to a default, so can range check early.
2818
- if (start < 0 || this.length < start || this.length < end) {
2819
- throw new RangeError('Out of range index')
2820
- }
2821
-
2822
- if (end <= start) {
2823
- return this
2824
- }
2825
-
2826
- start = start >>> 0;
2827
- end = end === undefined ? this.length : end >>> 0;
2828
-
2829
- if (!val) val = 0;
2830
-
2831
- var i;
2832
- if (typeof val === 'number') {
2833
- for (i = start; i < end; ++i) {
2834
- this[i] = val;
2835
- }
2836
- } else {
2837
- var bytes = Buffer.isBuffer(val)
2838
- ? val
2839
- : Buffer.from(val, encoding);
2840
- var len = bytes.length;
2841
- if (len === 0) {
2842
- throw new TypeError('The value "' + val +
2843
- '" is invalid for argument "value"')
2844
- }
2845
- for (i = 0; i < end - start; ++i) {
2846
- this[i + start] = bytes[i % len];
2847
- }
2848
- }
2849
-
2850
- return this
2851
- };
2852
-
2853
- // HELPER FUNCTIONS
2854
- // ================
2855
-
2856
- var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
2857
-
2858
- function base64clean (str) {
2859
- // Node takes equal signs as end of the Base64 encoding
2860
- str = str.split('=')[0];
2861
- // Node strips out invalid characters like \n and \t from the string, base64-js does not
2862
- str = str.trim().replace(INVALID_BASE64_RE, '');
2863
- // Node converts strings with length < 2 to ''
2864
- if (str.length < 2) return ''
2865
- // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
2866
- while (str.length % 4 !== 0) {
2867
- str = str + '=';
2868
- }
2869
- return str
2870
- }
2871
-
2872
- function utf8ToBytes (string, units) {
2873
- units = units || Infinity;
2874
- var codePoint;
2875
- var length = string.length;
2876
- var leadSurrogate = null;
2877
- var bytes = [];
2878
-
2879
- for (var i = 0; i < length; ++i) {
2880
- codePoint = string.charCodeAt(i);
2881
-
2882
- // is surrogate component
2883
- if (codePoint > 0xD7FF && codePoint < 0xE000) {
2884
- // last char was a lead
2885
- if (!leadSurrogate) {
2886
- // no lead yet
2887
- if (codePoint > 0xDBFF) {
2888
- // unexpected trail
2889
- if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
2890
- continue
2891
- } else if (i + 1 === length) {
2892
- // unpaired lead
2893
- if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
2894
- continue
2895
- }
2896
-
2897
- // valid lead
2898
- leadSurrogate = codePoint;
2899
-
2900
- continue
2901
- }
2902
-
2903
- // 2 leads in a row
2904
- if (codePoint < 0xDC00) {
2905
- if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
2906
- leadSurrogate = codePoint;
2907
- continue
2908
- }
2909
-
2910
- // valid surrogate pair
2911
- codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
2912
- } else if (leadSurrogate) {
2913
- // valid bmp char, but last char was a lead
2914
- if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
2915
- }
2916
-
2917
- leadSurrogate = null;
2918
-
2919
- // encode utf8
2920
- if (codePoint < 0x80) {
2921
- if ((units -= 1) < 0) break
2922
- bytes.push(codePoint);
2923
- } else if (codePoint < 0x800) {
2924
- if ((units -= 2) < 0) break
2925
- bytes.push(
2926
- codePoint >> 0x6 | 0xC0,
2927
- codePoint & 0x3F | 0x80
2928
- );
2929
- } else if (codePoint < 0x10000) {
2930
- if ((units -= 3) < 0) break
2931
- bytes.push(
2932
- codePoint >> 0xC | 0xE0,
2933
- codePoint >> 0x6 & 0x3F | 0x80,
2934
- codePoint & 0x3F | 0x80
2935
- );
2936
- } else if (codePoint < 0x110000) {
2937
- if ((units -= 4) < 0) break
2938
- bytes.push(
2939
- codePoint >> 0x12 | 0xF0,
2940
- codePoint >> 0xC & 0x3F | 0x80,
2941
- codePoint >> 0x6 & 0x3F | 0x80,
2942
- codePoint & 0x3F | 0x80
2943
- );
2944
- } else {
2945
- throw new Error('Invalid code point')
2946
- }
2947
- }
2948
-
2949
- return bytes
2950
- }
2951
-
2952
- function asciiToBytes (str) {
2953
- var byteArray = [];
2954
- for (var i = 0; i < str.length; ++i) {
2955
- // Node's code seems to be doing this and not & 0x7F..
2956
- byteArray.push(str.charCodeAt(i) & 0xFF);
2957
- }
2958
- return byteArray
2959
- }
2960
-
2961
- function utf16leToBytes (str, units) {
2962
- var c, hi, lo;
2963
- var byteArray = [];
2964
- for (var i = 0; i < str.length; ++i) {
2965
- if ((units -= 2) < 0) break
2966
-
2967
- c = str.charCodeAt(i);
2968
- hi = c >> 8;
2969
- lo = c % 256;
2970
- byteArray.push(lo);
2971
- byteArray.push(hi);
2972
- }
2973
-
2974
- return byteArray
2975
- }
2976
-
2977
- function base64ToBytes (str) {
2978
- return base64.toByteArray(base64clean(str))
2979
- }
2980
-
2981
- function blitBuffer (src, dst, offset, length) {
2982
- for (var i = 0; i < length; ++i) {
2983
- if ((i + offset >= dst.length) || (i >= src.length)) break
2984
- dst[i + offset] = src[i];
2985
- }
2986
- return i
2987
- }
2988
-
2989
- // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
2990
- // the `instanceof` check but they should be treated as of that type.
2991
- // See: https://github.com/feross/buffer/issues/166
2992
- function isInstance (obj, type) {
2993
- return obj instanceof type ||
2994
- (obj != null && obj.constructor != null && obj.constructor.name != null &&
2995
- obj.constructor.name === type.name)
2996
- }
2997
- function numberIsNaN (obj) {
2998
- // For IE11 support
2999
- return obj !== obj // eslint-disable-line no-self-compare
3000
- }
3001
-
3002
- // Create lookup table for `toString('hex')`
3003
- // See: https://github.com/feross/buffer/issues/219
3004
- var hexSliceLookupTable = (function () {
3005
- var alphabet = '0123456789abcdef';
3006
- var table = new Array(256);
3007
- for (var i = 0; i < 16; ++i) {
3008
- var i16 = i * 16;
3009
- for (var j = 0; j < 16; ++j) {
3010
- table[i16 + j] = alphabet[i] + alphabet[j];
3011
- }
3012
- }
3013
- return table
3014
- })();
3015
- } (buffer));
3016
- return buffer;
3017
- }
3018
-
3019
769
  var hasRequiredValidation;
3020
770
 
3021
771
  function requireValidation () {
3022
772
  if (hasRequiredValidation) return validation.exports;
3023
773
  hasRequiredValidation = 1;
3024
774
 
3025
- const { isUtf8 } = requireBuffer();
775
+ const { isUtf8 } = require$$0$1;
3026
776
 
3027
777
  const { hasBlob } = requireConstants();
3028
778
 
@@ -3182,7 +932,7 @@ function requireReceiver () {
3182
932
  if (hasRequiredReceiver) return receiver$1;
3183
933
  hasRequiredReceiver = 1;
3184
934
 
3185
- const { Writable } = require$$0;
935
+ const { Writable } = require$$0$2;
3186
936
 
3187
937
  const PerMessageDeflate = requirePermessageDeflate();
3188
938
  const {
@@ -3889,8 +1639,6 @@ function requireReceiver () {
3889
1639
  return receiver$1;
3890
1640
  }
3891
1641
 
3892
- requireReceiver();
3893
-
3894
1642
  /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex" }] */
3895
1643
 
3896
1644
  var sender$1;
@@ -3900,7 +1648,7 @@ function requireSender () {
3900
1648
  if (hasRequiredSender) return sender$1;
3901
1649
  hasRequiredSender = 1;
3902
1650
 
3903
- const { Duplex } = require$$0;
1651
+ const { Duplex } = require$$0$2;
3904
1652
  const { randomFillSync } = require$$1;
3905
1653
 
3906
1654
  const PerMessageDeflate = requirePermessageDeflate();
@@ -4449,7 +2197,7 @@ function requireSender () {
4449
2197
  /**
4450
2198
  * Sends a frame.
4451
2199
  *
4452
- * @param {Buffer[]} list The frame to send
2200
+ * @param {(Buffer | String)[]} list The frame to send
4453
2201
  * @param {Function} [cb] Callback
4454
2202
  * @private
4455
2203
  */
@@ -4501,8 +2249,6 @@ function requireSender () {
4501
2249
  return sender$1;
4502
2250
  }
4503
2251
 
4504
- requireSender();
4505
-
4506
2252
  var eventTarget;
4507
2253
  var hasRequiredEventTarget;
4508
2254
 
@@ -5023,13 +2769,13 @@ function requireWebsocket () {
5023
2769
  if (hasRequiredWebsocket) return websocket;
5024
2770
  hasRequiredWebsocket = 1;
5025
2771
 
5026
- const EventEmitter = require$$0$2;
2772
+ const EventEmitter = require$$0$3;
5027
2773
  const https = require$$1$1;
5028
2774
  const http$1 = http;
5029
2775
  const net$1 = net;
5030
2776
  const tls = require$$4;
5031
2777
  const { randomBytes, createHash } = require$$1;
5032
- const { Duplex, Readable } = require$$0;
2778
+ const { Duplex, Readable } = require$$0$2;
5033
2779
  const { URL } = require$$7;
5034
2780
 
5035
2781
  const PerMessageDeflate = requirePermessageDeflate();
@@ -6410,6 +4156,182 @@ function requireWebsocket () {
6410
4156
  return websocket;
6411
4157
  }
6412
4158
 
4159
+ /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^WebSocket$" }] */
4160
+
4161
+ var stream$1;
4162
+ var hasRequiredStream;
4163
+
4164
+ function requireStream () {
4165
+ if (hasRequiredStream) return stream$1;
4166
+ hasRequiredStream = 1;
4167
+
4168
+ requireWebsocket();
4169
+ const { Duplex } = require$$0$2;
4170
+
4171
+ /**
4172
+ * Emits the `'close'` event on a stream.
4173
+ *
4174
+ * @param {Duplex} stream The stream.
4175
+ * @private
4176
+ */
4177
+ function emitClose(stream) {
4178
+ stream.emit('close');
4179
+ }
4180
+
4181
+ /**
4182
+ * The listener of the `'end'` event.
4183
+ *
4184
+ * @private
4185
+ */
4186
+ function duplexOnEnd() {
4187
+ if (!this.destroyed && this._writableState.finished) {
4188
+ this.destroy();
4189
+ }
4190
+ }
4191
+
4192
+ /**
4193
+ * The listener of the `'error'` event.
4194
+ *
4195
+ * @param {Error} err The error
4196
+ * @private
4197
+ */
4198
+ function duplexOnError(err) {
4199
+ this.removeListener('error', duplexOnError);
4200
+ this.destroy();
4201
+ if (this.listenerCount('error') === 0) {
4202
+ // Do not suppress the throwing behavior.
4203
+ this.emit('error', err);
4204
+ }
4205
+ }
4206
+
4207
+ /**
4208
+ * Wraps a `WebSocket` in a duplex stream.
4209
+ *
4210
+ * @param {WebSocket} ws The `WebSocket` to wrap
4211
+ * @param {Object} [options] The options for the `Duplex` constructor
4212
+ * @return {Duplex} The duplex stream
4213
+ * @public
4214
+ */
4215
+ function createWebSocketStream(ws, options) {
4216
+ let terminateOnDestroy = true;
4217
+
4218
+ const duplex = new Duplex({
4219
+ ...options,
4220
+ autoDestroy: false,
4221
+ emitClose: false,
4222
+ objectMode: false,
4223
+ writableObjectMode: false
4224
+ });
4225
+
4226
+ ws.on('message', function message(msg, isBinary) {
4227
+ const data =
4228
+ !isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
4229
+
4230
+ if (!duplex.push(data)) ws.pause();
4231
+ });
4232
+
4233
+ ws.once('error', function error(err) {
4234
+ if (duplex.destroyed) return;
4235
+
4236
+ // Prevent `ws.terminate()` from being called by `duplex._destroy()`.
4237
+ //
4238
+ // - If the `'error'` event is emitted before the `'open'` event, then
4239
+ // `ws.terminate()` is a noop as no socket is assigned.
4240
+ // - Otherwise, the error is re-emitted by the listener of the `'error'`
4241
+ // event of the `Receiver` object. The listener already closes the
4242
+ // connection by calling `ws.close()`. This allows a close frame to be
4243
+ // sent to the other peer. If `ws.terminate()` is called right after this,
4244
+ // then the close frame might not be sent.
4245
+ terminateOnDestroy = false;
4246
+ duplex.destroy(err);
4247
+ });
4248
+
4249
+ ws.once('close', function close() {
4250
+ if (duplex.destroyed) return;
4251
+
4252
+ duplex.push(null);
4253
+ });
4254
+
4255
+ duplex._destroy = function (err, callback) {
4256
+ if (ws.readyState === ws.CLOSED) {
4257
+ callback(err);
4258
+ process.nextTick(emitClose, duplex);
4259
+ return;
4260
+ }
4261
+
4262
+ let called = false;
4263
+
4264
+ ws.once('error', function error(err) {
4265
+ called = true;
4266
+ callback(err);
4267
+ });
4268
+
4269
+ ws.once('close', function close() {
4270
+ if (!called) callback(err);
4271
+ process.nextTick(emitClose, duplex);
4272
+ });
4273
+
4274
+ if (terminateOnDestroy) ws.terminate();
4275
+ };
4276
+
4277
+ duplex._final = function (callback) {
4278
+ if (ws.readyState === ws.CONNECTING) {
4279
+ ws.once('open', function open() {
4280
+ duplex._final(callback);
4281
+ });
4282
+ return;
4283
+ }
4284
+
4285
+ // If the value of the `_socket` property is `null` it means that `ws` is a
4286
+ // client websocket and the handshake failed. In fact, when this happens, a
4287
+ // socket is never assigned to the websocket. Wait for the `'error'` event
4288
+ // that will be emitted by the websocket.
4289
+ if (ws._socket === null) return;
4290
+
4291
+ if (ws._socket._writableState.finished) {
4292
+ callback();
4293
+ if (duplex._readableState.endEmitted) duplex.destroy();
4294
+ } else {
4295
+ ws._socket.once('finish', function finish() {
4296
+ // `duplex` is not destroyed here because the `'end'` event will be
4297
+ // emitted on `duplex` after this `'finish'` event. The EOF signaling
4298
+ // `null` chunk is, in fact, pushed when the websocket emits `'close'`.
4299
+ callback();
4300
+ });
4301
+ ws.close();
4302
+ }
4303
+ };
4304
+
4305
+ duplex._read = function () {
4306
+ if (ws.isPaused) ws.resume();
4307
+ };
4308
+
4309
+ duplex._write = function (chunk, encoding, callback) {
4310
+ if (ws.readyState === ws.CONNECTING) {
4311
+ ws.once('open', function open() {
4312
+ duplex._write(chunk, encoding, callback);
4313
+ });
4314
+ return;
4315
+ }
4316
+
4317
+ ws.send(chunk, callback);
4318
+ };
4319
+
4320
+ duplex.on('end', duplexOnEnd);
4321
+ duplex.on('error', duplexOnError);
4322
+ return duplex;
4323
+ }
4324
+
4325
+ stream$1 = createWebSocketStream;
4326
+ return stream$1;
4327
+ }
4328
+
4329
+ requireStream();
4330
+
4331
+ requireReceiver();
4332
+
4333
+ requireSender();
4334
+
6413
4335
  requireWebsocket();
6414
4336
 
6415
4337
  var subprotocol;
@@ -6491,9 +4413,9 @@ function requireWebsocketServer () {
6491
4413
  if (hasRequiredWebsocketServer) return websocketServer$1;
6492
4414
  hasRequiredWebsocketServer = 1;
6493
4415
 
6494
- const EventEmitter = require$$0$2;
4416
+ const EventEmitter = require$$0$3;
6495
4417
  const http$1 = http;
6496
- const { Duplex } = require$$0;
4418
+ const { Duplex } = require$$0$2;
6497
4419
  const { createHash } = require$$1;
6498
4420
 
6499
4421
  const extension = requireExtension();