@jsenv/server 16.0.1 → 16.0.3

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,12 +1,13 @@
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;
@@ -248,7 +249,7 @@ function requirePermessageDeflate () {
248
249
  if (hasRequiredPermessageDeflate) return permessageDeflate;
249
250
  hasRequiredPermessageDeflate = 1;
250
251
 
251
- const zlib = require$$0$1;
252
+ const zlib = require$$0;
252
253
 
253
254
  const bufferUtil = requireBufferUtil();
254
255
  const Limiter = requireLimiter();
@@ -765,2095 +766,13 @@ function requirePermessageDeflate () {
765
766
 
766
767
  var validation = {exports: {}};
767
768
 
768
- var buffer = {};
769
-
770
- var base64Js = {};
771
-
772
- var hasRequiredBase64Js;
773
-
774
- function requireBase64Js () {
775
- if (hasRequiredBase64Js) return base64Js;
776
- hasRequiredBase64Js = 1;
777
-
778
- base64Js.byteLength = byteLength;
779
- base64Js.toByteArray = toByteArray;
780
- base64Js.fromByteArray = fromByteArray;
781
-
782
- var lookup = [];
783
- var revLookup = [];
784
- var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
785
-
786
- var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
787
- for (var i = 0, len = code.length; i < len; ++i) {
788
- lookup[i] = code[i];
789
- revLookup[code.charCodeAt(i)] = i;
790
- }
791
-
792
- // Support decoding URL-safe base64 strings, as Node.js does.
793
- // See: https://en.wikipedia.org/wiki/Base64#URL_applications
794
- revLookup['-'.charCodeAt(0)] = 62;
795
- revLookup['_'.charCodeAt(0)] = 63;
796
-
797
- function getLens (b64) {
798
- var len = b64.length;
799
-
800
- if (len % 4 > 0) {
801
- throw new Error('Invalid string. Length must be a multiple of 4')
802
- }
803
-
804
- // Trim off extra bytes after placeholder bytes are found
805
- // See: https://github.com/beatgammit/base64-js/issues/42
806
- var validLen = b64.indexOf('=');
807
- if (validLen === -1) validLen = len;
808
-
809
- var placeHoldersLen = validLen === len
810
- ? 0
811
- : 4 - (validLen % 4);
812
-
813
- return [validLen, placeHoldersLen]
814
- }
815
-
816
- // base64 is 4/3 + up to two characters of the original data
817
- function byteLength (b64) {
818
- var lens = getLens(b64);
819
- var validLen = lens[0];
820
- var placeHoldersLen = lens[1];
821
- return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
822
- }
823
-
824
- function _byteLength (b64, validLen, placeHoldersLen) {
825
- return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
826
- }
827
-
828
- function toByteArray (b64) {
829
- var tmp;
830
- var lens = getLens(b64);
831
- var validLen = lens[0];
832
- var placeHoldersLen = lens[1];
833
-
834
- var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen));
835
-
836
- var curByte = 0;
837
-
838
- // if there are placeholders, only get up to the last complete 4 chars
839
- var len = placeHoldersLen > 0
840
- ? validLen - 4
841
- : validLen;
842
-
843
- var i;
844
- for (i = 0; i < len; i += 4) {
845
- tmp =
846
- (revLookup[b64.charCodeAt(i)] << 18) |
847
- (revLookup[b64.charCodeAt(i + 1)] << 12) |
848
- (revLookup[b64.charCodeAt(i + 2)] << 6) |
849
- revLookup[b64.charCodeAt(i + 3)];
850
- arr[curByte++] = (tmp >> 16) & 0xFF;
851
- arr[curByte++] = (tmp >> 8) & 0xFF;
852
- arr[curByte++] = tmp & 0xFF;
853
- }
854
-
855
- if (placeHoldersLen === 2) {
856
- tmp =
857
- (revLookup[b64.charCodeAt(i)] << 2) |
858
- (revLookup[b64.charCodeAt(i + 1)] >> 4);
859
- arr[curByte++] = tmp & 0xFF;
860
- }
861
-
862
- if (placeHoldersLen === 1) {
863
- tmp =
864
- (revLookup[b64.charCodeAt(i)] << 10) |
865
- (revLookup[b64.charCodeAt(i + 1)] << 4) |
866
- (revLookup[b64.charCodeAt(i + 2)] >> 2);
867
- arr[curByte++] = (tmp >> 8) & 0xFF;
868
- arr[curByte++] = tmp & 0xFF;
869
- }
870
-
871
- return arr
872
- }
873
-
874
- function tripletToBase64 (num) {
875
- return lookup[num >> 18 & 0x3F] +
876
- lookup[num >> 12 & 0x3F] +
877
- lookup[num >> 6 & 0x3F] +
878
- lookup[num & 0x3F]
879
- }
880
-
881
- function encodeChunk (uint8, start, end) {
882
- var tmp;
883
- var output = [];
884
- for (var i = start; i < end; i += 3) {
885
- tmp =
886
- ((uint8[i] << 16) & 0xFF0000) +
887
- ((uint8[i + 1] << 8) & 0xFF00) +
888
- (uint8[i + 2] & 0xFF);
889
- output.push(tripletToBase64(tmp));
890
- }
891
- return output.join('')
892
- }
893
-
894
- function fromByteArray (uint8) {
895
- var tmp;
896
- var len = uint8.length;
897
- var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
898
- var parts = [];
899
- var maxChunkLength = 16383; // must be multiple of 3
900
-
901
- // go through the array every three bytes, we'll deal with trailing stuff later
902
- for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
903
- parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)));
904
- }
905
-
906
- // pad the end with zeros, but make sure to not forget the extra bytes
907
- if (extraBytes === 1) {
908
- tmp = uint8[len - 1];
909
- parts.push(
910
- lookup[tmp >> 2] +
911
- lookup[(tmp << 4) & 0x3F] +
912
- '=='
913
- );
914
- } else if (extraBytes === 2) {
915
- tmp = (uint8[len - 2] << 8) + uint8[len - 1];
916
- parts.push(
917
- lookup[tmp >> 10] +
918
- lookup[(tmp >> 4) & 0x3F] +
919
- lookup[(tmp << 2) & 0x3F] +
920
- '='
921
- );
922
- }
923
-
924
- return parts.join('')
925
- }
926
- return base64Js;
927
- }
928
-
929
- var ieee754 = {};
930
-
931
- /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
932
-
933
- var hasRequiredIeee754;
934
-
935
- function requireIeee754 () {
936
- if (hasRequiredIeee754) return ieee754;
937
- hasRequiredIeee754 = 1;
938
- ieee754.read = function (buffer, offset, isLE, mLen, nBytes) {
939
- var e, m;
940
- var eLen = (nBytes * 8) - mLen - 1;
941
- var eMax = (1 << eLen) - 1;
942
- var eBias = eMax >> 1;
943
- var nBits = -7;
944
- var i = isLE ? (nBytes - 1) : 0;
945
- var d = isLE ? -1 : 1;
946
- var s = buffer[offset + i];
947
-
948
- i += d;
949
-
950
- e = s & ((1 << (-nBits)) - 1);
951
- s >>= (-nBits);
952
- nBits += eLen;
953
- for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
954
-
955
- m = e & ((1 << (-nBits)) - 1);
956
- e >>= (-nBits);
957
- nBits += mLen;
958
- for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
959
-
960
- if (e === 0) {
961
- e = 1 - eBias;
962
- } else if (e === eMax) {
963
- return m ? NaN : ((s ? -1 : 1) * Infinity)
964
- } else {
965
- m = m + Math.pow(2, mLen);
966
- e = e - eBias;
967
- }
968
- return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
969
- };
970
-
971
- ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes) {
972
- var e, m, c;
973
- var eLen = (nBytes * 8) - mLen - 1;
974
- var eMax = (1 << eLen) - 1;
975
- var eBias = eMax >> 1;
976
- var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
977
- var i = isLE ? 0 : (nBytes - 1);
978
- var d = isLE ? 1 : -1;
979
- var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
980
-
981
- value = Math.abs(value);
982
-
983
- if (isNaN(value) || value === Infinity) {
984
- m = isNaN(value) ? 1 : 0;
985
- e = eMax;
986
- } else {
987
- e = Math.floor(Math.log(value) / Math.LN2);
988
- if (value * (c = Math.pow(2, -e)) < 1) {
989
- e--;
990
- c *= 2;
991
- }
992
- if (e + eBias >= 1) {
993
- value += rt / c;
994
- } else {
995
- value += rt * Math.pow(2, 1 - eBias);
996
- }
997
- if (value * c >= 2) {
998
- e++;
999
- c /= 2;
1000
- }
1001
-
1002
- if (e + eBias >= eMax) {
1003
- m = 0;
1004
- e = eMax;
1005
- } else if (e + eBias >= 1) {
1006
- m = ((value * c) - 1) * Math.pow(2, mLen);
1007
- e = e + eBias;
1008
- } else {
1009
- m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
1010
- e = 0;
1011
- }
1012
- }
1013
-
1014
- for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
1015
-
1016
- e = (e << mLen) | m;
1017
- eLen += mLen;
1018
- for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
1019
-
1020
- buffer[offset + i - d] |= s * 128;
1021
- };
1022
- return ieee754;
1023
- }
1024
-
1025
- /*!
1026
- * The buffer module from node.js, for the browser.
1027
- *
1028
- * @author Feross Aboukhadijeh <https://feross.org>
1029
- * @license MIT
1030
- */
1031
-
1032
- var hasRequiredBuffer;
1033
-
1034
- function requireBuffer () {
1035
- if (hasRequiredBuffer) return buffer;
1036
- hasRequiredBuffer = 1;
1037
- (function (exports) {
1038
-
1039
- var base64 = requireBase64Js();
1040
- var ieee754 = requireIeee754();
1041
- var customInspectSymbol =
1042
- (typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
1043
- ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
1044
- : null;
1045
-
1046
- exports.Buffer = Buffer;
1047
- exports.SlowBuffer = SlowBuffer;
1048
- exports.INSPECT_MAX_BYTES = 50;
1049
-
1050
- var K_MAX_LENGTH = 0x7fffffff;
1051
- exports.kMaxLength = K_MAX_LENGTH;
1052
-
1053
- /**
1054
- * If `Buffer.TYPED_ARRAY_SUPPORT`:
1055
- * === true Use Uint8Array implementation (fastest)
1056
- * === false Print warning and recommend using `buffer` v4.x which has an Object
1057
- * implementation (most compatible, even IE6)
1058
- *
1059
- * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
1060
- * Opera 11.6+, iOS 4.2+.
1061
- *
1062
- * We report that the browser does not support typed arrays if the are not subclassable
1063
- * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
1064
- * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
1065
- * for __proto__ and has a buggy typed array implementation.
1066
- */
1067
- Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport();
1068
-
1069
- if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
1070
- typeof console.error === 'function') {
1071
- console.error(
1072
- 'This browser lacks typed array (Uint8Array) support which is required by ' +
1073
- '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
1074
- );
1075
- }
1076
-
1077
- function typedArraySupport () {
1078
- // Can typed array instances can be augmented?
1079
- try {
1080
- var arr = new Uint8Array(1);
1081
- var proto = { foo: function () { return 42 } };
1082
- Object.setPrototypeOf(proto, Uint8Array.prototype);
1083
- Object.setPrototypeOf(arr, proto);
1084
- return arr.foo() === 42
1085
- } catch (e) {
1086
- return false
1087
- }
1088
- }
1089
-
1090
- Object.defineProperty(Buffer.prototype, 'parent', {
1091
- enumerable: true,
1092
- get: function () {
1093
- if (!Buffer.isBuffer(this)) return undefined
1094
- return this.buffer
1095
- }
1096
- });
1097
-
1098
- Object.defineProperty(Buffer.prototype, 'offset', {
1099
- enumerable: true,
1100
- get: function () {
1101
- if (!Buffer.isBuffer(this)) return undefined
1102
- return this.byteOffset
1103
- }
1104
- });
1105
-
1106
- function createBuffer (length) {
1107
- if (length > K_MAX_LENGTH) {
1108
- throw new RangeError('The value "' + length + '" is invalid for option "size"')
1109
- }
1110
- // Return an augmented `Uint8Array` instance
1111
- var buf = new Uint8Array(length);
1112
- Object.setPrototypeOf(buf, Buffer.prototype);
1113
- return buf
1114
- }
1115
-
1116
- /**
1117
- * The Buffer constructor returns instances of `Uint8Array` that have their
1118
- * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
1119
- * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
1120
- * and the `Uint8Array` methods. Square bracket notation works as expected -- it
1121
- * returns a single octet.
1122
- *
1123
- * The `Uint8Array` prototype remains unmodified.
1124
- */
1125
-
1126
- function Buffer (arg, encodingOrOffset, length) {
1127
- // Common case.
1128
- if (typeof arg === 'number') {
1129
- if (typeof encodingOrOffset === 'string') {
1130
- throw new TypeError(
1131
- 'The "string" argument must be of type string. Received type number'
1132
- )
1133
- }
1134
- return allocUnsafe(arg)
1135
- }
1136
- return from(arg, encodingOrOffset, length)
1137
- }
1138
-
1139
- Buffer.poolSize = 8192; // not used by this implementation
1140
-
1141
- function from (value, encodingOrOffset, length) {
1142
- if (typeof value === 'string') {
1143
- return fromString(value, encodingOrOffset)
1144
- }
1145
-
1146
- if (ArrayBuffer.isView(value)) {
1147
- return fromArrayView(value)
1148
- }
1149
-
1150
- if (value == null) {
1151
- throw new TypeError(
1152
- 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
1153
- 'or Array-like Object. Received type ' + (typeof value)
1154
- )
1155
- }
1156
-
1157
- if (isInstance(value, ArrayBuffer) ||
1158
- (value && isInstance(value.buffer, ArrayBuffer))) {
1159
- return fromArrayBuffer(value, encodingOrOffset, length)
1160
- }
1161
-
1162
- if (typeof SharedArrayBuffer !== 'undefined' &&
1163
- (isInstance(value, SharedArrayBuffer) ||
1164
- (value && isInstance(value.buffer, SharedArrayBuffer)))) {
1165
- return fromArrayBuffer(value, encodingOrOffset, length)
1166
- }
1167
-
1168
- if (typeof value === 'number') {
1169
- throw new TypeError(
1170
- 'The "value" argument must not be of type number. Received type number'
1171
- )
1172
- }
1173
-
1174
- var valueOf = value.valueOf && value.valueOf();
1175
- if (valueOf != null && valueOf !== value) {
1176
- return Buffer.from(valueOf, encodingOrOffset, length)
1177
- }
1178
-
1179
- var b = fromObject(value);
1180
- if (b) return b
1181
-
1182
- if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
1183
- typeof value[Symbol.toPrimitive] === 'function') {
1184
- return Buffer.from(
1185
- value[Symbol.toPrimitive]('string'), encodingOrOffset, length
1186
- )
1187
- }
1188
-
1189
- throw new TypeError(
1190
- 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
1191
- 'or Array-like Object. Received type ' + (typeof value)
1192
- )
1193
- }
1194
-
1195
- /**
1196
- * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
1197
- * if value is a number.
1198
- * Buffer.from(str[, encoding])
1199
- * Buffer.from(array)
1200
- * Buffer.from(buffer)
1201
- * Buffer.from(arrayBuffer[, byteOffset[, length]])
1202
- **/
1203
- Buffer.from = function (value, encodingOrOffset, length) {
1204
- return from(value, encodingOrOffset, length)
1205
- };
1206
-
1207
- // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
1208
- // https://github.com/feross/buffer/pull/148
1209
- Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
1210
- Object.setPrototypeOf(Buffer, Uint8Array);
1211
-
1212
- function assertSize (size) {
1213
- if (typeof size !== 'number') {
1214
- throw new TypeError('"size" argument must be of type number')
1215
- } else if (size < 0) {
1216
- throw new RangeError('The value "' + size + '" is invalid for option "size"')
1217
- }
1218
- }
1219
-
1220
- function alloc (size, fill, encoding) {
1221
- assertSize(size);
1222
- if (size <= 0) {
1223
- return createBuffer(size)
1224
- }
1225
- if (fill !== undefined) {
1226
- // Only pay attention to encoding if it's a string. This
1227
- // prevents accidentally sending in a number that would
1228
- // be interpreted as a start offset.
1229
- return typeof encoding === 'string'
1230
- ? createBuffer(size).fill(fill, encoding)
1231
- : createBuffer(size).fill(fill)
1232
- }
1233
- return createBuffer(size)
1234
- }
1235
-
1236
- /**
1237
- * Creates a new filled Buffer instance.
1238
- * alloc(size[, fill[, encoding]])
1239
- **/
1240
- Buffer.alloc = function (size, fill, encoding) {
1241
- return alloc(size, fill, encoding)
1242
- };
1243
-
1244
- function allocUnsafe (size) {
1245
- assertSize(size);
1246
- return createBuffer(size < 0 ? 0 : checked(size) | 0)
1247
- }
1248
-
1249
- /**
1250
- * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
1251
- * */
1252
- Buffer.allocUnsafe = function (size) {
1253
- return allocUnsafe(size)
1254
- };
1255
- /**
1256
- * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
1257
- */
1258
- Buffer.allocUnsafeSlow = function (size) {
1259
- return allocUnsafe(size)
1260
- };
1261
-
1262
- function fromString (string, encoding) {
1263
- if (typeof encoding !== 'string' || encoding === '') {
1264
- encoding = 'utf8';
1265
- }
1266
-
1267
- if (!Buffer.isEncoding(encoding)) {
1268
- throw new TypeError('Unknown encoding: ' + encoding)
1269
- }
1270
-
1271
- var length = byteLength(string, encoding) | 0;
1272
- var buf = createBuffer(length);
1273
-
1274
- var actual = buf.write(string, encoding);
1275
-
1276
- if (actual !== length) {
1277
- // Writing a hex string, for example, that contains invalid characters will
1278
- // cause everything after the first invalid character to be ignored. (e.g.
1279
- // 'abxxcd' will be treated as 'ab')
1280
- buf = buf.slice(0, actual);
1281
- }
1282
-
1283
- return buf
1284
- }
1285
-
1286
- function fromArrayLike (array) {
1287
- var length = array.length < 0 ? 0 : checked(array.length) | 0;
1288
- var buf = createBuffer(length);
1289
- for (var i = 0; i < length; i += 1) {
1290
- buf[i] = array[i] & 255;
1291
- }
1292
- return buf
1293
- }
1294
-
1295
- function fromArrayView (arrayView) {
1296
- if (isInstance(arrayView, Uint8Array)) {
1297
- var copy = new Uint8Array(arrayView);
1298
- return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
1299
- }
1300
- return fromArrayLike(arrayView)
1301
- }
1302
-
1303
- function fromArrayBuffer (array, byteOffset, length) {
1304
- if (byteOffset < 0 || array.byteLength < byteOffset) {
1305
- throw new RangeError('"offset" is outside of buffer bounds')
1306
- }
1307
-
1308
- if (array.byteLength < byteOffset + (length || 0)) {
1309
- throw new RangeError('"length" is outside of buffer bounds')
1310
- }
1311
-
1312
- var buf;
1313
- if (byteOffset === undefined && length === undefined) {
1314
- buf = new Uint8Array(array);
1315
- } else if (length === undefined) {
1316
- buf = new Uint8Array(array, byteOffset);
1317
- } else {
1318
- buf = new Uint8Array(array, byteOffset, length);
1319
- }
1320
-
1321
- // Return an augmented `Uint8Array` instance
1322
- Object.setPrototypeOf(buf, Buffer.prototype);
1323
-
1324
- return buf
1325
- }
1326
-
1327
- function fromObject (obj) {
1328
- if (Buffer.isBuffer(obj)) {
1329
- var len = checked(obj.length) | 0;
1330
- var buf = createBuffer(len);
1331
-
1332
- if (buf.length === 0) {
1333
- return buf
1334
- }
1335
-
1336
- obj.copy(buf, 0, 0, len);
1337
- return buf
1338
- }
1339
-
1340
- if (obj.length !== undefined) {
1341
- if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
1342
- return createBuffer(0)
1343
- }
1344
- return fromArrayLike(obj)
1345
- }
1346
-
1347
- if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
1348
- return fromArrayLike(obj.data)
1349
- }
1350
- }
1351
-
1352
- function checked (length) {
1353
- // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
1354
- // length is NaN (which is otherwise coerced to zero.)
1355
- if (length >= K_MAX_LENGTH) {
1356
- throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
1357
- 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
1358
- }
1359
- return length | 0
1360
- }
1361
-
1362
- function SlowBuffer (length) {
1363
- if (+length != length) { // eslint-disable-line eqeqeq
1364
- length = 0;
1365
- }
1366
- return Buffer.alloc(+length)
1367
- }
1368
-
1369
- Buffer.isBuffer = function isBuffer (b) {
1370
- return b != null && b._isBuffer === true &&
1371
- b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
1372
- };
1373
-
1374
- Buffer.compare = function compare (a, b) {
1375
- if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength);
1376
- if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength);
1377
- if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
1378
- throw new TypeError(
1379
- 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
1380
- )
1381
- }
1382
-
1383
- if (a === b) return 0
1384
-
1385
- var x = a.length;
1386
- var y = b.length;
1387
-
1388
- for (var i = 0, len = Math.min(x, y); i < len; ++i) {
1389
- if (a[i] !== b[i]) {
1390
- x = a[i];
1391
- y = b[i];
1392
- break
1393
- }
1394
- }
1395
-
1396
- if (x < y) return -1
1397
- if (y < x) return 1
1398
- return 0
1399
- };
1400
-
1401
- Buffer.isEncoding = function isEncoding (encoding) {
1402
- switch (String(encoding).toLowerCase()) {
1403
- case 'hex':
1404
- case 'utf8':
1405
- case 'utf-8':
1406
- case 'ascii':
1407
- case 'latin1':
1408
- case 'binary':
1409
- case 'base64':
1410
- case 'ucs2':
1411
- case 'ucs-2':
1412
- case 'utf16le':
1413
- case 'utf-16le':
1414
- return true
1415
- default:
1416
- return false
1417
- }
1418
- };
1419
-
1420
- Buffer.concat = function concat (list, length) {
1421
- if (!Array.isArray(list)) {
1422
- throw new TypeError('"list" argument must be an Array of Buffers')
1423
- }
1424
-
1425
- if (list.length === 0) {
1426
- return Buffer.alloc(0)
1427
- }
1428
-
1429
- var i;
1430
- if (length === undefined) {
1431
- length = 0;
1432
- for (i = 0; i < list.length; ++i) {
1433
- length += list[i].length;
1434
- }
1435
- }
1436
-
1437
- var buffer = Buffer.allocUnsafe(length);
1438
- var pos = 0;
1439
- for (i = 0; i < list.length; ++i) {
1440
- var buf = list[i];
1441
- if (isInstance(buf, Uint8Array)) {
1442
- if (pos + buf.length > buffer.length) {
1443
- Buffer.from(buf).copy(buffer, pos);
1444
- } else {
1445
- Uint8Array.prototype.set.call(
1446
- buffer,
1447
- buf,
1448
- pos
1449
- );
1450
- }
1451
- } else if (!Buffer.isBuffer(buf)) {
1452
- throw new TypeError('"list" argument must be an Array of Buffers')
1453
- } else {
1454
- buf.copy(buffer, pos);
1455
- }
1456
- pos += buf.length;
1457
- }
1458
- return buffer
1459
- };
1460
-
1461
- function byteLength (string, encoding) {
1462
- if (Buffer.isBuffer(string)) {
1463
- return string.length
1464
- }
1465
- if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
1466
- return string.byteLength
1467
- }
1468
- if (typeof string !== 'string') {
1469
- throw new TypeError(
1470
- 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
1471
- 'Received type ' + typeof string
1472
- )
1473
- }
1474
-
1475
- var len = string.length;
1476
- var mustMatch = (arguments.length > 2 && arguments[2] === true);
1477
- if (!mustMatch && len === 0) return 0
1478
-
1479
- // Use a for loop to avoid recursion
1480
- var loweredCase = false;
1481
- for (;;) {
1482
- switch (encoding) {
1483
- case 'ascii':
1484
- case 'latin1':
1485
- case 'binary':
1486
- return len
1487
- case 'utf8':
1488
- case 'utf-8':
1489
- return utf8ToBytes(string).length
1490
- case 'ucs2':
1491
- case 'ucs-2':
1492
- case 'utf16le':
1493
- case 'utf-16le':
1494
- return len * 2
1495
- case 'hex':
1496
- return len >>> 1
1497
- case 'base64':
1498
- return base64ToBytes(string).length
1499
- default:
1500
- if (loweredCase) {
1501
- return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
1502
- }
1503
- encoding = ('' + encoding).toLowerCase();
1504
- loweredCase = true;
1505
- }
1506
- }
1507
- }
1508
- Buffer.byteLength = byteLength;
1509
-
1510
- function slowToString (encoding, start, end) {
1511
- var loweredCase = false;
1512
-
1513
- // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
1514
- // property of a typed array.
1515
-
1516
- // This behaves neither like String nor Uint8Array in that we set start/end
1517
- // to their upper/lower bounds if the value passed is out of range.
1518
- // undefined is handled specially as per ECMA-262 6th Edition,
1519
- // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
1520
- if (start === undefined || start < 0) {
1521
- start = 0;
1522
- }
1523
- // Return early if start > this.length. Done here to prevent potential uint32
1524
- // coercion fail below.
1525
- if (start > this.length) {
1526
- return ''
1527
- }
1528
-
1529
- if (end === undefined || end > this.length) {
1530
- end = this.length;
1531
- }
1532
-
1533
- if (end <= 0) {
1534
- return ''
1535
- }
1536
-
1537
- // Force coercion to uint32. This will also coerce falsey/NaN values to 0.
1538
- end >>>= 0;
1539
- start >>>= 0;
1540
-
1541
- if (end <= start) {
1542
- return ''
1543
- }
1544
-
1545
- if (!encoding) encoding = 'utf8';
1546
-
1547
- while (true) {
1548
- switch (encoding) {
1549
- case 'hex':
1550
- return hexSlice(this, start, end)
1551
-
1552
- case 'utf8':
1553
- case 'utf-8':
1554
- return utf8Slice(this, start, end)
1555
-
1556
- case 'ascii':
1557
- return asciiSlice(this, start, end)
1558
-
1559
- case 'latin1':
1560
- case 'binary':
1561
- return latin1Slice(this, start, end)
1562
-
1563
- case 'base64':
1564
- return base64Slice(this, start, end)
1565
-
1566
- case 'ucs2':
1567
- case 'ucs-2':
1568
- case 'utf16le':
1569
- case 'utf-16le':
1570
- return utf16leSlice(this, start, end)
1571
-
1572
- default:
1573
- if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
1574
- encoding = (encoding + '').toLowerCase();
1575
- loweredCase = true;
1576
- }
1577
- }
1578
- }
1579
-
1580
- // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
1581
- // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
1582
- // reliably in a browserify context because there could be multiple different
1583
- // copies of the 'buffer' package in use. This method works even for Buffer
1584
- // instances that were created from another copy of the `buffer` package.
1585
- // See: https://github.com/feross/buffer/issues/154
1586
- Buffer.prototype._isBuffer = true;
1587
-
1588
- function swap (b, n, m) {
1589
- var i = b[n];
1590
- b[n] = b[m];
1591
- b[m] = i;
1592
- }
1593
-
1594
- Buffer.prototype.swap16 = function swap16 () {
1595
- var len = this.length;
1596
- if (len % 2 !== 0) {
1597
- throw new RangeError('Buffer size must be a multiple of 16-bits')
1598
- }
1599
- for (var i = 0; i < len; i += 2) {
1600
- swap(this, i, i + 1);
1601
- }
1602
- return this
1603
- };
1604
-
1605
- Buffer.prototype.swap32 = function swap32 () {
1606
- var len = this.length;
1607
- if (len % 4 !== 0) {
1608
- throw new RangeError('Buffer size must be a multiple of 32-bits')
1609
- }
1610
- for (var i = 0; i < len; i += 4) {
1611
- swap(this, i, i + 3);
1612
- swap(this, i + 1, i + 2);
1613
- }
1614
- return this
1615
- };
1616
-
1617
- Buffer.prototype.swap64 = function swap64 () {
1618
- var len = this.length;
1619
- if (len % 8 !== 0) {
1620
- throw new RangeError('Buffer size must be a multiple of 64-bits')
1621
- }
1622
- for (var i = 0; i < len; i += 8) {
1623
- swap(this, i, i + 7);
1624
- swap(this, i + 1, i + 6);
1625
- swap(this, i + 2, i + 5);
1626
- swap(this, i + 3, i + 4);
1627
- }
1628
- return this
1629
- };
1630
-
1631
- Buffer.prototype.toString = function toString () {
1632
- var length = this.length;
1633
- if (length === 0) return ''
1634
- if (arguments.length === 0) return utf8Slice(this, 0, length)
1635
- return slowToString.apply(this, arguments)
1636
- };
1637
-
1638
- Buffer.prototype.toLocaleString = Buffer.prototype.toString;
1639
-
1640
- Buffer.prototype.equals = function equals (b) {
1641
- if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
1642
- if (this === b) return true
1643
- return Buffer.compare(this, b) === 0
1644
- };
1645
-
1646
- Buffer.prototype.inspect = function inspect () {
1647
- var str = '';
1648
- var max = exports.INSPECT_MAX_BYTES;
1649
- str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
1650
- if (this.length > max) str += ' ... ';
1651
- return '<Buffer ' + str + '>'
1652
- };
1653
- if (customInspectSymbol) {
1654
- Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;
1655
- }
1656
-
1657
- Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
1658
- if (isInstance(target, Uint8Array)) {
1659
- target = Buffer.from(target, target.offset, target.byteLength);
1660
- }
1661
- if (!Buffer.isBuffer(target)) {
1662
- throw new TypeError(
1663
- 'The "target" argument must be one of type Buffer or Uint8Array. ' +
1664
- 'Received type ' + (typeof target)
1665
- )
1666
- }
1667
-
1668
- if (start === undefined) {
1669
- start = 0;
1670
- }
1671
- if (end === undefined) {
1672
- end = target ? target.length : 0;
1673
- }
1674
- if (thisStart === undefined) {
1675
- thisStart = 0;
1676
- }
1677
- if (thisEnd === undefined) {
1678
- thisEnd = this.length;
1679
- }
1680
-
1681
- if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
1682
- throw new RangeError('out of range index')
1683
- }
1684
-
1685
- if (thisStart >= thisEnd && start >= end) {
1686
- return 0
1687
- }
1688
- if (thisStart >= thisEnd) {
1689
- return -1
1690
- }
1691
- if (start >= end) {
1692
- return 1
1693
- }
1694
-
1695
- start >>>= 0;
1696
- end >>>= 0;
1697
- thisStart >>>= 0;
1698
- thisEnd >>>= 0;
1699
-
1700
- if (this === target) return 0
1701
-
1702
- var x = thisEnd - thisStart;
1703
- var y = end - start;
1704
- var len = Math.min(x, y);
1705
-
1706
- var thisCopy = this.slice(thisStart, thisEnd);
1707
- var targetCopy = target.slice(start, end);
1708
-
1709
- for (var i = 0; i < len; ++i) {
1710
- if (thisCopy[i] !== targetCopy[i]) {
1711
- x = thisCopy[i];
1712
- y = targetCopy[i];
1713
- break
1714
- }
1715
- }
1716
-
1717
- if (x < y) return -1
1718
- if (y < x) return 1
1719
- return 0
1720
- };
1721
-
1722
- // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
1723
- // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
1724
- //
1725
- // Arguments:
1726
- // - buffer - a Buffer to search
1727
- // - val - a string, Buffer, or number
1728
- // - byteOffset - an index into `buffer`; will be clamped to an int32
1729
- // - encoding - an optional encoding, relevant is val is a string
1730
- // - dir - true for indexOf, false for lastIndexOf
1731
- function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
1732
- // Empty buffer means no match
1733
- if (buffer.length === 0) return -1
1734
-
1735
- // Normalize byteOffset
1736
- if (typeof byteOffset === 'string') {
1737
- encoding = byteOffset;
1738
- byteOffset = 0;
1739
- } else if (byteOffset > 0x7fffffff) {
1740
- byteOffset = 0x7fffffff;
1741
- } else if (byteOffset < -2147483648) {
1742
- byteOffset = -2147483648;
1743
- }
1744
- byteOffset = +byteOffset; // Coerce to Number.
1745
- if (numberIsNaN(byteOffset)) {
1746
- // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
1747
- byteOffset = dir ? 0 : (buffer.length - 1);
1748
- }
1749
-
1750
- // Normalize byteOffset: negative offsets start from the end of the buffer
1751
- if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
1752
- if (byteOffset >= buffer.length) {
1753
- if (dir) return -1
1754
- else byteOffset = buffer.length - 1;
1755
- } else if (byteOffset < 0) {
1756
- if (dir) byteOffset = 0;
1757
- else return -1
1758
- }
1759
-
1760
- // Normalize val
1761
- if (typeof val === 'string') {
1762
- val = Buffer.from(val, encoding);
1763
- }
1764
-
1765
- // Finally, search either indexOf (if dir is true) or lastIndexOf
1766
- if (Buffer.isBuffer(val)) {
1767
- // Special case: looking for empty string/buffer always fails
1768
- if (val.length === 0) {
1769
- return -1
1770
- }
1771
- return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
1772
- } else if (typeof val === 'number') {
1773
- val = val & 0xFF; // Search for a byte value [0-255]
1774
- if (typeof Uint8Array.prototype.indexOf === 'function') {
1775
- if (dir) {
1776
- return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
1777
- } else {
1778
- return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
1779
- }
1780
- }
1781
- return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
1782
- }
1783
-
1784
- throw new TypeError('val must be string, number or Buffer')
1785
- }
1786
-
1787
- function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
1788
- var indexSize = 1;
1789
- var arrLength = arr.length;
1790
- var valLength = val.length;
1791
-
1792
- if (encoding !== undefined) {
1793
- encoding = String(encoding).toLowerCase();
1794
- if (encoding === 'ucs2' || encoding === 'ucs-2' ||
1795
- encoding === 'utf16le' || encoding === 'utf-16le') {
1796
- if (arr.length < 2 || val.length < 2) {
1797
- return -1
1798
- }
1799
- indexSize = 2;
1800
- arrLength /= 2;
1801
- valLength /= 2;
1802
- byteOffset /= 2;
1803
- }
1804
- }
1805
-
1806
- function read (buf, i) {
1807
- if (indexSize === 1) {
1808
- return buf[i]
1809
- } else {
1810
- return buf.readUInt16BE(i * indexSize)
1811
- }
1812
- }
1813
-
1814
- var i;
1815
- if (dir) {
1816
- var foundIndex = -1;
1817
- for (i = byteOffset; i < arrLength; i++) {
1818
- if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
1819
- if (foundIndex === -1) foundIndex = i;
1820
- if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
1821
- } else {
1822
- if (foundIndex !== -1) i -= i - foundIndex;
1823
- foundIndex = -1;
1824
- }
1825
- }
1826
- } else {
1827
- if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
1828
- for (i = byteOffset; i >= 0; i--) {
1829
- var found = true;
1830
- for (var j = 0; j < valLength; j++) {
1831
- if (read(arr, i + j) !== read(val, j)) {
1832
- found = false;
1833
- break
1834
- }
1835
- }
1836
- if (found) return i
1837
- }
1838
- }
1839
-
1840
- return -1
1841
- }
1842
-
1843
- Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
1844
- return this.indexOf(val, byteOffset, encoding) !== -1
1845
- };
1846
-
1847
- Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
1848
- return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
1849
- };
1850
-
1851
- Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
1852
- return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
1853
- };
1854
-
1855
- function hexWrite (buf, string, offset, length) {
1856
- offset = Number(offset) || 0;
1857
- var remaining = buf.length - offset;
1858
- if (!length) {
1859
- length = remaining;
1860
- } else {
1861
- length = Number(length);
1862
- if (length > remaining) {
1863
- length = remaining;
1864
- }
1865
- }
1866
-
1867
- var strLen = string.length;
1868
-
1869
- if (length > strLen / 2) {
1870
- length = strLen / 2;
1871
- }
1872
- for (var i = 0; i < length; ++i) {
1873
- var parsed = parseInt(string.substr(i * 2, 2), 16);
1874
- if (numberIsNaN(parsed)) return i
1875
- buf[offset + i] = parsed;
1876
- }
1877
- return i
1878
- }
1879
-
1880
- function utf8Write (buf, string, offset, length) {
1881
- return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
1882
- }
1883
-
1884
- function asciiWrite (buf, string, offset, length) {
1885
- return blitBuffer(asciiToBytes(string), buf, offset, length)
1886
- }
1887
-
1888
- function base64Write (buf, string, offset, length) {
1889
- return blitBuffer(base64ToBytes(string), buf, offset, length)
1890
- }
1891
-
1892
- function ucs2Write (buf, string, offset, length) {
1893
- return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
1894
- }
1895
-
1896
- Buffer.prototype.write = function write (string, offset, length, encoding) {
1897
- // Buffer#write(string)
1898
- if (offset === undefined) {
1899
- encoding = 'utf8';
1900
- length = this.length;
1901
- offset = 0;
1902
- // Buffer#write(string, encoding)
1903
- } else if (length === undefined && typeof offset === 'string') {
1904
- encoding = offset;
1905
- length = this.length;
1906
- offset = 0;
1907
- // Buffer#write(string, offset[, length][, encoding])
1908
- } else if (isFinite(offset)) {
1909
- offset = offset >>> 0;
1910
- if (isFinite(length)) {
1911
- length = length >>> 0;
1912
- if (encoding === undefined) encoding = 'utf8';
1913
- } else {
1914
- encoding = length;
1915
- length = undefined;
1916
- }
1917
- } else {
1918
- throw new Error(
1919
- 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
1920
- )
1921
- }
1922
-
1923
- var remaining = this.length - offset;
1924
- if (length === undefined || length > remaining) length = remaining;
1925
-
1926
- if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
1927
- throw new RangeError('Attempt to write outside buffer bounds')
1928
- }
1929
-
1930
- if (!encoding) encoding = 'utf8';
1931
-
1932
- var loweredCase = false;
1933
- for (;;) {
1934
- switch (encoding) {
1935
- case 'hex':
1936
- return hexWrite(this, string, offset, length)
1937
-
1938
- case 'utf8':
1939
- case 'utf-8':
1940
- return utf8Write(this, string, offset, length)
1941
-
1942
- case 'ascii':
1943
- case 'latin1':
1944
- case 'binary':
1945
- return asciiWrite(this, string, offset, length)
1946
-
1947
- case 'base64':
1948
- // Warning: maxLength not taken into account in base64Write
1949
- return base64Write(this, string, offset, length)
1950
-
1951
- case 'ucs2':
1952
- case 'ucs-2':
1953
- case 'utf16le':
1954
- case 'utf-16le':
1955
- return ucs2Write(this, string, offset, length)
1956
-
1957
- default:
1958
- if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
1959
- encoding = ('' + encoding).toLowerCase();
1960
- loweredCase = true;
1961
- }
1962
- }
1963
- };
1964
-
1965
- Buffer.prototype.toJSON = function toJSON () {
1966
- return {
1967
- type: 'Buffer',
1968
- data: Array.prototype.slice.call(this._arr || this, 0)
1969
- }
1970
- };
1971
-
1972
- function base64Slice (buf, start, end) {
1973
- if (start === 0 && end === buf.length) {
1974
- return base64.fromByteArray(buf)
1975
- } else {
1976
- return base64.fromByteArray(buf.slice(start, end))
1977
- }
1978
- }
1979
-
1980
- function utf8Slice (buf, start, end) {
1981
- end = Math.min(buf.length, end);
1982
- var res = [];
1983
-
1984
- var i = start;
1985
- while (i < end) {
1986
- var firstByte = buf[i];
1987
- var codePoint = null;
1988
- var bytesPerSequence = (firstByte > 0xEF)
1989
- ? 4
1990
- : (firstByte > 0xDF)
1991
- ? 3
1992
- : (firstByte > 0xBF)
1993
- ? 2
1994
- : 1;
1995
-
1996
- if (i + bytesPerSequence <= end) {
1997
- var secondByte, thirdByte, fourthByte, tempCodePoint;
1998
-
1999
- switch (bytesPerSequence) {
2000
- case 1:
2001
- if (firstByte < 0x80) {
2002
- codePoint = firstByte;
2003
- }
2004
- break
2005
- case 2:
2006
- secondByte = buf[i + 1];
2007
- if ((secondByte & 0xC0) === 0x80) {
2008
- tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
2009
- if (tempCodePoint > 0x7F) {
2010
- codePoint = tempCodePoint;
2011
- }
2012
- }
2013
- break
2014
- case 3:
2015
- secondByte = buf[i + 1];
2016
- thirdByte = buf[i + 2];
2017
- if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
2018
- tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
2019
- if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
2020
- codePoint = tempCodePoint;
2021
- }
2022
- }
2023
- break
2024
- case 4:
2025
- secondByte = buf[i + 1];
2026
- thirdByte = buf[i + 2];
2027
- fourthByte = buf[i + 3];
2028
- if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
2029
- tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
2030
- if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
2031
- codePoint = tempCodePoint;
2032
- }
2033
- }
2034
- }
2035
- }
2036
-
2037
- if (codePoint === null) {
2038
- // we did not generate a valid codePoint so insert a
2039
- // replacement char (U+FFFD) and advance only 1 byte
2040
- codePoint = 0xFFFD;
2041
- bytesPerSequence = 1;
2042
- } else if (codePoint > 0xFFFF) {
2043
- // encode to utf16 (surrogate pair dance)
2044
- codePoint -= 0x10000;
2045
- res.push(codePoint >>> 10 & 0x3FF | 0xD800);
2046
- codePoint = 0xDC00 | codePoint & 0x3FF;
2047
- }
2048
-
2049
- res.push(codePoint);
2050
- i += bytesPerSequence;
2051
- }
2052
-
2053
- return decodeCodePointsArray(res)
2054
- }
2055
-
2056
- // Based on http://stackoverflow.com/a/22747272/680742, the browser with
2057
- // the lowest limit is Chrome, with 0x10000 args.
2058
- // We go 1 magnitude less, for safety
2059
- var MAX_ARGUMENTS_LENGTH = 0x1000;
2060
-
2061
- function decodeCodePointsArray (codePoints) {
2062
- var len = codePoints.length;
2063
- if (len <= MAX_ARGUMENTS_LENGTH) {
2064
- return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
2065
- }
2066
-
2067
- // Decode in chunks to avoid "call stack size exceeded".
2068
- var res = '';
2069
- var i = 0;
2070
- while (i < len) {
2071
- res += String.fromCharCode.apply(
2072
- String,
2073
- codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
2074
- );
2075
- }
2076
- return res
2077
- }
2078
-
2079
- function asciiSlice (buf, start, end) {
2080
- var ret = '';
2081
- end = Math.min(buf.length, end);
2082
-
2083
- for (var i = start; i < end; ++i) {
2084
- ret += String.fromCharCode(buf[i] & 0x7F);
2085
- }
2086
- return ret
2087
- }
2088
-
2089
- function latin1Slice (buf, start, end) {
2090
- var ret = '';
2091
- end = Math.min(buf.length, end);
2092
-
2093
- for (var i = start; i < end; ++i) {
2094
- ret += String.fromCharCode(buf[i]);
2095
- }
2096
- return ret
2097
- }
2098
-
2099
- function hexSlice (buf, start, end) {
2100
- var len = buf.length;
2101
-
2102
- if (!start || start < 0) start = 0;
2103
- if (!end || end < 0 || end > len) end = len;
2104
-
2105
- var out = '';
2106
- for (var i = start; i < end; ++i) {
2107
- out += hexSliceLookupTable[buf[i]];
2108
- }
2109
- return out
2110
- }
2111
-
2112
- function utf16leSlice (buf, start, end) {
2113
- var bytes = buf.slice(start, end);
2114
- var res = '';
2115
- // If bytes.length is odd, the last 8 bits must be ignored (same as node.js)
2116
- for (var i = 0; i < bytes.length - 1; i += 2) {
2117
- res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256));
2118
- }
2119
- return res
2120
- }
2121
-
2122
- Buffer.prototype.slice = function slice (start, end) {
2123
- var len = this.length;
2124
- start = ~~start;
2125
- end = end === undefined ? len : ~~end;
2126
-
2127
- if (start < 0) {
2128
- start += len;
2129
- if (start < 0) start = 0;
2130
- } else if (start > len) {
2131
- start = len;
2132
- }
2133
-
2134
- if (end < 0) {
2135
- end += len;
2136
- if (end < 0) end = 0;
2137
- } else if (end > len) {
2138
- end = len;
2139
- }
2140
-
2141
- if (end < start) end = start;
2142
-
2143
- var newBuf = this.subarray(start, end);
2144
- // Return an augmented `Uint8Array` instance
2145
- Object.setPrototypeOf(newBuf, Buffer.prototype);
2146
-
2147
- return newBuf
2148
- };
2149
-
2150
- /*
2151
- * Need to make sure that buffer isn't trying to write out of bounds.
2152
- */
2153
- function checkOffset (offset, ext, length) {
2154
- if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
2155
- if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
2156
- }
2157
-
2158
- Buffer.prototype.readUintLE =
2159
- Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
2160
- offset = offset >>> 0;
2161
- byteLength = byteLength >>> 0;
2162
- if (!noAssert) checkOffset(offset, byteLength, this.length);
2163
-
2164
- var val = this[offset];
2165
- var mul = 1;
2166
- var i = 0;
2167
- while (++i < byteLength && (mul *= 0x100)) {
2168
- val += this[offset + i] * mul;
2169
- }
2170
-
2171
- return val
2172
- };
2173
-
2174
- Buffer.prototype.readUintBE =
2175
- Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
2176
- offset = offset >>> 0;
2177
- byteLength = byteLength >>> 0;
2178
- if (!noAssert) {
2179
- checkOffset(offset, byteLength, this.length);
2180
- }
2181
-
2182
- var val = this[offset + --byteLength];
2183
- var mul = 1;
2184
- while (byteLength > 0 && (mul *= 0x100)) {
2185
- val += this[offset + --byteLength] * mul;
2186
- }
2187
-
2188
- return val
2189
- };
2190
-
2191
- Buffer.prototype.readUint8 =
2192
- Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
2193
- offset = offset >>> 0;
2194
- if (!noAssert) checkOffset(offset, 1, this.length);
2195
- return this[offset]
2196
- };
2197
-
2198
- Buffer.prototype.readUint16LE =
2199
- Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
2200
- offset = offset >>> 0;
2201
- if (!noAssert) checkOffset(offset, 2, this.length);
2202
- return this[offset] | (this[offset + 1] << 8)
2203
- };
2204
-
2205
- Buffer.prototype.readUint16BE =
2206
- Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
2207
- offset = offset >>> 0;
2208
- if (!noAssert) checkOffset(offset, 2, this.length);
2209
- return (this[offset] << 8) | this[offset + 1]
2210
- };
2211
-
2212
- Buffer.prototype.readUint32LE =
2213
- Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
2214
- offset = offset >>> 0;
2215
- if (!noAssert) checkOffset(offset, 4, this.length);
2216
-
2217
- return ((this[offset]) |
2218
- (this[offset + 1] << 8) |
2219
- (this[offset + 2] << 16)) +
2220
- (this[offset + 3] * 0x1000000)
2221
- };
2222
-
2223
- Buffer.prototype.readUint32BE =
2224
- Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
2225
- offset = offset >>> 0;
2226
- if (!noAssert) checkOffset(offset, 4, this.length);
2227
-
2228
- return (this[offset] * 0x1000000) +
2229
- ((this[offset + 1] << 16) |
2230
- (this[offset + 2] << 8) |
2231
- this[offset + 3])
2232
- };
2233
-
2234
- Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
2235
- offset = offset >>> 0;
2236
- byteLength = byteLength >>> 0;
2237
- if (!noAssert) checkOffset(offset, byteLength, this.length);
2238
-
2239
- var val = this[offset];
2240
- var mul = 1;
2241
- var i = 0;
2242
- while (++i < byteLength && (mul *= 0x100)) {
2243
- val += this[offset + i] * mul;
2244
- }
2245
- mul *= 0x80;
2246
-
2247
- if (val >= mul) val -= Math.pow(2, 8 * byteLength);
2248
-
2249
- return val
2250
- };
2251
-
2252
- Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
2253
- offset = offset >>> 0;
2254
- byteLength = byteLength >>> 0;
2255
- if (!noAssert) checkOffset(offset, byteLength, this.length);
2256
-
2257
- var i = byteLength;
2258
- var mul = 1;
2259
- var val = this[offset + --i];
2260
- while (i > 0 && (mul *= 0x100)) {
2261
- val += this[offset + --i] * mul;
2262
- }
2263
- mul *= 0x80;
2264
-
2265
- if (val >= mul) val -= Math.pow(2, 8 * byteLength);
2266
-
2267
- return val
2268
- };
2269
-
2270
- Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
2271
- offset = offset >>> 0;
2272
- if (!noAssert) checkOffset(offset, 1, this.length);
2273
- if (!(this[offset] & 0x80)) return (this[offset])
2274
- return ((0xff - this[offset] + 1) * -1)
2275
- };
2276
-
2277
- Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
2278
- offset = offset >>> 0;
2279
- if (!noAssert) checkOffset(offset, 2, this.length);
2280
- var val = this[offset] | (this[offset + 1] << 8);
2281
- return (val & 0x8000) ? val | 0xFFFF0000 : val
2282
- };
2283
-
2284
- Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
2285
- offset = offset >>> 0;
2286
- if (!noAssert) checkOffset(offset, 2, this.length);
2287
- var val = this[offset + 1] | (this[offset] << 8);
2288
- return (val & 0x8000) ? val | 0xFFFF0000 : val
2289
- };
2290
-
2291
- Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
2292
- offset = offset >>> 0;
2293
- if (!noAssert) checkOffset(offset, 4, this.length);
2294
-
2295
- return (this[offset]) |
2296
- (this[offset + 1] << 8) |
2297
- (this[offset + 2] << 16) |
2298
- (this[offset + 3] << 24)
2299
- };
2300
-
2301
- Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
2302
- offset = offset >>> 0;
2303
- if (!noAssert) checkOffset(offset, 4, this.length);
2304
-
2305
- return (this[offset] << 24) |
2306
- (this[offset + 1] << 16) |
2307
- (this[offset + 2] << 8) |
2308
- (this[offset + 3])
2309
- };
2310
-
2311
- Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
2312
- offset = offset >>> 0;
2313
- if (!noAssert) checkOffset(offset, 4, this.length);
2314
- return ieee754.read(this, offset, true, 23, 4)
2315
- };
2316
-
2317
- Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
2318
- offset = offset >>> 0;
2319
- if (!noAssert) checkOffset(offset, 4, this.length);
2320
- return ieee754.read(this, offset, false, 23, 4)
2321
- };
2322
-
2323
- Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
2324
- offset = offset >>> 0;
2325
- if (!noAssert) checkOffset(offset, 8, this.length);
2326
- return ieee754.read(this, offset, true, 52, 8)
2327
- };
2328
-
2329
- Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
2330
- offset = offset >>> 0;
2331
- if (!noAssert) checkOffset(offset, 8, this.length);
2332
- return ieee754.read(this, offset, false, 52, 8)
2333
- };
2334
-
2335
- function checkInt (buf, value, offset, ext, max, min) {
2336
- if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
2337
- if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
2338
- if (offset + ext > buf.length) throw new RangeError('Index out of range')
2339
- }
2340
-
2341
- Buffer.prototype.writeUintLE =
2342
- Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
2343
- value = +value;
2344
- offset = offset >>> 0;
2345
- byteLength = byteLength >>> 0;
2346
- if (!noAssert) {
2347
- var maxBytes = Math.pow(2, 8 * byteLength) - 1;
2348
- checkInt(this, value, offset, byteLength, maxBytes, 0);
2349
- }
2350
-
2351
- var mul = 1;
2352
- var i = 0;
2353
- this[offset] = value & 0xFF;
2354
- while (++i < byteLength && (mul *= 0x100)) {
2355
- this[offset + i] = (value / mul) & 0xFF;
2356
- }
2357
-
2358
- return offset + byteLength
2359
- };
2360
-
2361
- Buffer.prototype.writeUintBE =
2362
- Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
2363
- value = +value;
2364
- offset = offset >>> 0;
2365
- byteLength = byteLength >>> 0;
2366
- if (!noAssert) {
2367
- var maxBytes = Math.pow(2, 8 * byteLength) - 1;
2368
- checkInt(this, value, offset, byteLength, maxBytes, 0);
2369
- }
2370
-
2371
- var i = byteLength - 1;
2372
- var mul = 1;
2373
- this[offset + i] = value & 0xFF;
2374
- while (--i >= 0 && (mul *= 0x100)) {
2375
- this[offset + i] = (value / mul) & 0xFF;
2376
- }
2377
-
2378
- return offset + byteLength
2379
- };
2380
-
2381
- Buffer.prototype.writeUint8 =
2382
- Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
2383
- value = +value;
2384
- offset = offset >>> 0;
2385
- if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
2386
- this[offset] = (value & 0xff);
2387
- return offset + 1
2388
- };
2389
-
2390
- Buffer.prototype.writeUint16LE =
2391
- Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
2392
- value = +value;
2393
- offset = offset >>> 0;
2394
- if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
2395
- this[offset] = (value & 0xff);
2396
- this[offset + 1] = (value >>> 8);
2397
- return offset + 2
2398
- };
2399
-
2400
- Buffer.prototype.writeUint16BE =
2401
- Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
2402
- value = +value;
2403
- offset = offset >>> 0;
2404
- if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
2405
- this[offset] = (value >>> 8);
2406
- this[offset + 1] = (value & 0xff);
2407
- return offset + 2
2408
- };
2409
-
2410
- Buffer.prototype.writeUint32LE =
2411
- Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
2412
- value = +value;
2413
- offset = offset >>> 0;
2414
- if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
2415
- this[offset + 3] = (value >>> 24);
2416
- this[offset + 2] = (value >>> 16);
2417
- this[offset + 1] = (value >>> 8);
2418
- this[offset] = (value & 0xff);
2419
- return offset + 4
2420
- };
2421
-
2422
- Buffer.prototype.writeUint32BE =
2423
- Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
2424
- value = +value;
2425
- offset = offset >>> 0;
2426
- if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
2427
- this[offset] = (value >>> 24);
2428
- this[offset + 1] = (value >>> 16);
2429
- this[offset + 2] = (value >>> 8);
2430
- this[offset + 3] = (value & 0xff);
2431
- return offset + 4
2432
- };
2433
-
2434
- Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
2435
- value = +value;
2436
- offset = offset >>> 0;
2437
- if (!noAssert) {
2438
- var limit = Math.pow(2, (8 * byteLength) - 1);
2439
-
2440
- checkInt(this, value, offset, byteLength, limit - 1, -limit);
2441
- }
2442
-
2443
- var i = 0;
2444
- var mul = 1;
2445
- var sub = 0;
2446
- this[offset] = value & 0xFF;
2447
- while (++i < byteLength && (mul *= 0x100)) {
2448
- if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
2449
- sub = 1;
2450
- }
2451
- this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
2452
- }
2453
-
2454
- return offset + byteLength
2455
- };
2456
-
2457
- Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
2458
- value = +value;
2459
- offset = offset >>> 0;
2460
- if (!noAssert) {
2461
- var limit = Math.pow(2, (8 * byteLength) - 1);
2462
-
2463
- checkInt(this, value, offset, byteLength, limit - 1, -limit);
2464
- }
2465
-
2466
- var i = byteLength - 1;
2467
- var mul = 1;
2468
- var sub = 0;
2469
- this[offset + i] = value & 0xFF;
2470
- while (--i >= 0 && (mul *= 0x100)) {
2471
- if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
2472
- sub = 1;
2473
- }
2474
- this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
2475
- }
2476
-
2477
- return offset + byteLength
2478
- };
2479
-
2480
- Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
2481
- value = +value;
2482
- offset = offset >>> 0;
2483
- if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -128);
2484
- if (value < 0) value = 0xff + value + 1;
2485
- this[offset] = (value & 0xff);
2486
- return offset + 1
2487
- };
2488
-
2489
- Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
2490
- value = +value;
2491
- offset = offset >>> 0;
2492
- if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
2493
- this[offset] = (value & 0xff);
2494
- this[offset + 1] = (value >>> 8);
2495
- return offset + 2
2496
- };
2497
-
2498
- Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
2499
- value = +value;
2500
- offset = offset >>> 0;
2501
- if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
2502
- this[offset] = (value >>> 8);
2503
- this[offset + 1] = (value & 0xff);
2504
- return offset + 2
2505
- };
2506
-
2507
- Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
2508
- value = +value;
2509
- offset = offset >>> 0;
2510
- if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
2511
- this[offset] = (value & 0xff);
2512
- this[offset + 1] = (value >>> 8);
2513
- this[offset + 2] = (value >>> 16);
2514
- this[offset + 3] = (value >>> 24);
2515
- return offset + 4
2516
- };
2517
-
2518
- Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
2519
- value = +value;
2520
- offset = offset >>> 0;
2521
- if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
2522
- if (value < 0) value = 0xffffffff + value + 1;
2523
- this[offset] = (value >>> 24);
2524
- this[offset + 1] = (value >>> 16);
2525
- this[offset + 2] = (value >>> 8);
2526
- this[offset + 3] = (value & 0xff);
2527
- return offset + 4
2528
- };
2529
-
2530
- function checkIEEE754 (buf, value, offset, ext, max, min) {
2531
- if (offset + ext > buf.length) throw new RangeError('Index out of range')
2532
- if (offset < 0) throw new RangeError('Index out of range')
2533
- }
2534
-
2535
- function writeFloat (buf, value, offset, littleEndian, noAssert) {
2536
- value = +value;
2537
- offset = offset >>> 0;
2538
- if (!noAssert) {
2539
- checkIEEE754(buf, value, offset, 4);
2540
- }
2541
- ieee754.write(buf, value, offset, littleEndian, 23, 4);
2542
- return offset + 4
2543
- }
2544
-
2545
- Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
2546
- return writeFloat(this, value, offset, true, noAssert)
2547
- };
2548
-
2549
- Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
2550
- return writeFloat(this, value, offset, false, noAssert)
2551
- };
2552
-
2553
- function writeDouble (buf, value, offset, littleEndian, noAssert) {
2554
- value = +value;
2555
- offset = offset >>> 0;
2556
- if (!noAssert) {
2557
- checkIEEE754(buf, value, offset, 8);
2558
- }
2559
- ieee754.write(buf, value, offset, littleEndian, 52, 8);
2560
- return offset + 8
2561
- }
2562
-
2563
- Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
2564
- return writeDouble(this, value, offset, true, noAssert)
2565
- };
2566
-
2567
- Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
2568
- return writeDouble(this, value, offset, false, noAssert)
2569
- };
2570
-
2571
- // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
2572
- Buffer.prototype.copy = function copy (target, targetStart, start, end) {
2573
- if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
2574
- if (!start) start = 0;
2575
- if (!end && end !== 0) end = this.length;
2576
- if (targetStart >= target.length) targetStart = target.length;
2577
- if (!targetStart) targetStart = 0;
2578
- if (end > 0 && end < start) end = start;
2579
-
2580
- // Copy 0 bytes; we're done
2581
- if (end === start) return 0
2582
- if (target.length === 0 || this.length === 0) return 0
2583
-
2584
- // Fatal error conditions
2585
- if (targetStart < 0) {
2586
- throw new RangeError('targetStart out of bounds')
2587
- }
2588
- if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
2589
- if (end < 0) throw new RangeError('sourceEnd out of bounds')
2590
-
2591
- // Are we oob?
2592
- if (end > this.length) end = this.length;
2593
- if (target.length - targetStart < end - start) {
2594
- end = target.length - targetStart + start;
2595
- }
2596
-
2597
- var len = end - start;
2598
-
2599
- if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
2600
- // Use built-in when available, missing from IE11
2601
- this.copyWithin(targetStart, start, end);
2602
- } else {
2603
- Uint8Array.prototype.set.call(
2604
- target,
2605
- this.subarray(start, end),
2606
- targetStart
2607
- );
2608
- }
2609
-
2610
- return len
2611
- };
2612
-
2613
- // Usage:
2614
- // buffer.fill(number[, offset[, end]])
2615
- // buffer.fill(buffer[, offset[, end]])
2616
- // buffer.fill(string[, offset[, end]][, encoding])
2617
- Buffer.prototype.fill = function fill (val, start, end, encoding) {
2618
- // Handle string cases:
2619
- if (typeof val === 'string') {
2620
- if (typeof start === 'string') {
2621
- encoding = start;
2622
- start = 0;
2623
- end = this.length;
2624
- } else if (typeof end === 'string') {
2625
- encoding = end;
2626
- end = this.length;
2627
- }
2628
- if (encoding !== undefined && typeof encoding !== 'string') {
2629
- throw new TypeError('encoding must be a string')
2630
- }
2631
- if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
2632
- throw new TypeError('Unknown encoding: ' + encoding)
2633
- }
2634
- if (val.length === 1) {
2635
- var code = val.charCodeAt(0);
2636
- if ((encoding === 'utf8' && code < 128) ||
2637
- encoding === 'latin1') {
2638
- // Fast path: If `val` fits into a single byte, use that numeric value.
2639
- val = code;
2640
- }
2641
- }
2642
- } else if (typeof val === 'number') {
2643
- val = val & 255;
2644
- } else if (typeof val === 'boolean') {
2645
- val = Number(val);
2646
- }
2647
-
2648
- // Invalid ranges are not set to a default, so can range check early.
2649
- if (start < 0 || this.length < start || this.length < end) {
2650
- throw new RangeError('Out of range index')
2651
- }
2652
-
2653
- if (end <= start) {
2654
- return this
2655
- }
2656
-
2657
- start = start >>> 0;
2658
- end = end === undefined ? this.length : end >>> 0;
2659
-
2660
- if (!val) val = 0;
2661
-
2662
- var i;
2663
- if (typeof val === 'number') {
2664
- for (i = start; i < end; ++i) {
2665
- this[i] = val;
2666
- }
2667
- } else {
2668
- var bytes = Buffer.isBuffer(val)
2669
- ? val
2670
- : Buffer.from(val, encoding);
2671
- var len = bytes.length;
2672
- if (len === 0) {
2673
- throw new TypeError('The value "' + val +
2674
- '" is invalid for argument "value"')
2675
- }
2676
- for (i = 0; i < end - start; ++i) {
2677
- this[i + start] = bytes[i % len];
2678
- }
2679
- }
2680
-
2681
- return this
2682
- };
2683
-
2684
- // HELPER FUNCTIONS
2685
- // ================
2686
-
2687
- var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
2688
-
2689
- function base64clean (str) {
2690
- // Node takes equal signs as end of the Base64 encoding
2691
- str = str.split('=')[0];
2692
- // Node strips out invalid characters like \n and \t from the string, base64-js does not
2693
- str = str.trim().replace(INVALID_BASE64_RE, '');
2694
- // Node converts strings with length < 2 to ''
2695
- if (str.length < 2) return ''
2696
- // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
2697
- while (str.length % 4 !== 0) {
2698
- str = str + '=';
2699
- }
2700
- return str
2701
- }
2702
-
2703
- function utf8ToBytes (string, units) {
2704
- units = units || Infinity;
2705
- var codePoint;
2706
- var length = string.length;
2707
- var leadSurrogate = null;
2708
- var bytes = [];
2709
-
2710
- for (var i = 0; i < length; ++i) {
2711
- codePoint = string.charCodeAt(i);
2712
-
2713
- // is surrogate component
2714
- if (codePoint > 0xD7FF && codePoint < 0xE000) {
2715
- // last char was a lead
2716
- if (!leadSurrogate) {
2717
- // no lead yet
2718
- if (codePoint > 0xDBFF) {
2719
- // unexpected trail
2720
- if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
2721
- continue
2722
- } else if (i + 1 === length) {
2723
- // unpaired lead
2724
- if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
2725
- continue
2726
- }
2727
-
2728
- // valid lead
2729
- leadSurrogate = codePoint;
2730
-
2731
- continue
2732
- }
2733
-
2734
- // 2 leads in a row
2735
- if (codePoint < 0xDC00) {
2736
- if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
2737
- leadSurrogate = codePoint;
2738
- continue
2739
- }
2740
-
2741
- // valid surrogate pair
2742
- codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
2743
- } else if (leadSurrogate) {
2744
- // valid bmp char, but last char was a lead
2745
- if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
2746
- }
2747
-
2748
- leadSurrogate = null;
2749
-
2750
- // encode utf8
2751
- if (codePoint < 0x80) {
2752
- if ((units -= 1) < 0) break
2753
- bytes.push(codePoint);
2754
- } else if (codePoint < 0x800) {
2755
- if ((units -= 2) < 0) break
2756
- bytes.push(
2757
- codePoint >> 0x6 | 0xC0,
2758
- codePoint & 0x3F | 0x80
2759
- );
2760
- } else if (codePoint < 0x10000) {
2761
- if ((units -= 3) < 0) break
2762
- bytes.push(
2763
- codePoint >> 0xC | 0xE0,
2764
- codePoint >> 0x6 & 0x3F | 0x80,
2765
- codePoint & 0x3F | 0x80
2766
- );
2767
- } else if (codePoint < 0x110000) {
2768
- if ((units -= 4) < 0) break
2769
- bytes.push(
2770
- codePoint >> 0x12 | 0xF0,
2771
- codePoint >> 0xC & 0x3F | 0x80,
2772
- codePoint >> 0x6 & 0x3F | 0x80,
2773
- codePoint & 0x3F | 0x80
2774
- );
2775
- } else {
2776
- throw new Error('Invalid code point')
2777
- }
2778
- }
2779
-
2780
- return bytes
2781
- }
2782
-
2783
- function asciiToBytes (str) {
2784
- var byteArray = [];
2785
- for (var i = 0; i < str.length; ++i) {
2786
- // Node's code seems to be doing this and not & 0x7F..
2787
- byteArray.push(str.charCodeAt(i) & 0xFF);
2788
- }
2789
- return byteArray
2790
- }
2791
-
2792
- function utf16leToBytes (str, units) {
2793
- var c, hi, lo;
2794
- var byteArray = [];
2795
- for (var i = 0; i < str.length; ++i) {
2796
- if ((units -= 2) < 0) break
2797
-
2798
- c = str.charCodeAt(i);
2799
- hi = c >> 8;
2800
- lo = c % 256;
2801
- byteArray.push(lo);
2802
- byteArray.push(hi);
2803
- }
2804
-
2805
- return byteArray
2806
- }
2807
-
2808
- function base64ToBytes (str) {
2809
- return base64.toByteArray(base64clean(str))
2810
- }
2811
-
2812
- function blitBuffer (src, dst, offset, length) {
2813
- for (var i = 0; i < length; ++i) {
2814
- if ((i + offset >= dst.length) || (i >= src.length)) break
2815
- dst[i + offset] = src[i];
2816
- }
2817
- return i
2818
- }
2819
-
2820
- // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
2821
- // the `instanceof` check but they should be treated as of that type.
2822
- // See: https://github.com/feross/buffer/issues/166
2823
- function isInstance (obj, type) {
2824
- return obj instanceof type ||
2825
- (obj != null && obj.constructor != null && obj.constructor.name != null &&
2826
- obj.constructor.name === type.name)
2827
- }
2828
- function numberIsNaN (obj) {
2829
- // For IE11 support
2830
- return obj !== obj // eslint-disable-line no-self-compare
2831
- }
2832
-
2833
- // Create lookup table for `toString('hex')`
2834
- // See: https://github.com/feross/buffer/issues/219
2835
- var hexSliceLookupTable = (function () {
2836
- var alphabet = '0123456789abcdef';
2837
- var table = new Array(256);
2838
- for (var i = 0; i < 16; ++i) {
2839
- var i16 = i * 16;
2840
- for (var j = 0; j < 16; ++j) {
2841
- table[i16 + j] = alphabet[i] + alphabet[j];
2842
- }
2843
- }
2844
- return table
2845
- })();
2846
- } (buffer));
2847
- return buffer;
2848
- }
2849
-
2850
769
  var hasRequiredValidation;
2851
770
 
2852
771
  function requireValidation () {
2853
772
  if (hasRequiredValidation) return validation.exports;
2854
773
  hasRequiredValidation = 1;
2855
774
 
2856
- const { isUtf8 } = requireBuffer();
775
+ const { isUtf8 } = require$$0$1;
2857
776
 
2858
777
  const { hasBlob } = requireConstants();
2859
778
 
@@ -3013,7 +932,7 @@ function requireReceiver () {
3013
932
  if (hasRequiredReceiver) return receiver$1;
3014
933
  hasRequiredReceiver = 1;
3015
934
 
3016
- const { Writable } = require$$0;
935
+ const { Writable } = require$$0$2;
3017
936
 
3018
937
  const PerMessageDeflate = requirePermessageDeflate();
3019
938
  const {
@@ -3720,8 +1639,6 @@ function requireReceiver () {
3720
1639
  return receiver$1;
3721
1640
  }
3722
1641
 
3723
- requireReceiver();
3724
-
3725
1642
  /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex" }] */
3726
1643
 
3727
1644
  var sender$1;
@@ -3730,6 +1647,8 @@ var hasRequiredSender;
3730
1647
  function requireSender () {
3731
1648
  if (hasRequiredSender) return sender$1;
3732
1649
  hasRequiredSender = 1;
1650
+
1651
+ const { Duplex } = require$$0$2;
3733
1652
  const { randomFillSync } = require$$1;
3734
1653
 
3735
1654
  const PerMessageDeflate = requirePermessageDeflate();
@@ -4278,7 +2197,7 @@ function requireSender () {
4278
2197
  /**
4279
2198
  * Sends a frame.
4280
2199
  *
4281
- * @param {Buffer[]} list The frame to send
2200
+ * @param {(Buffer | String)[]} list The frame to send
4282
2201
  * @param {Function} [cb] Callback
4283
2202
  * @private
4284
2203
  */
@@ -4330,8 +2249,6 @@ function requireSender () {
4330
2249
  return sender$1;
4331
2250
  }
4332
2251
 
4333
- requireSender();
4334
-
4335
2252
  var eventTarget;
4336
2253
  var hasRequiredEventTarget;
4337
2254
 
@@ -4852,12 +2769,13 @@ function requireWebsocket () {
4852
2769
  if (hasRequiredWebsocket) return websocket;
4853
2770
  hasRequiredWebsocket = 1;
4854
2771
 
4855
- const EventEmitter = require$$0$2;
2772
+ const EventEmitter = require$$0$3;
4856
2773
  const https = require$$1$1;
4857
2774
  const http$1 = http;
4858
2775
  const net$1 = net;
4859
2776
  const tls = require$$4;
4860
2777
  const { randomBytes, createHash } = require$$1;
2778
+ const { Duplex, Readable } = require$$0$2;
4861
2779
  const { URL } = require$$7;
4862
2780
 
4863
2781
  const PerMessageDeflate = requirePermessageDeflate();
@@ -6238,6 +4156,182 @@ function requireWebsocket () {
6238
4156
  return websocket;
6239
4157
  }
6240
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
+
6241
4335
  requireWebsocket();
6242
4336
 
6243
4337
  var subprotocol;
@@ -6319,8 +4413,9 @@ function requireWebsocketServer () {
6319
4413
  if (hasRequiredWebsocketServer) return websocketServer$1;
6320
4414
  hasRequiredWebsocketServer = 1;
6321
4415
 
6322
- const EventEmitter = require$$0$2;
4416
+ const EventEmitter = require$$0$3;
6323
4417
  const http$1 = http;
4418
+ const { Duplex } = require$$0$2;
6324
4419
  const { createHash } = require$$1;
6325
4420
 
6326
4421
  const extension = requireExtension();