deepline 0.3.293 → 0.3.295

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.
Files changed (138) hide show
  1. package/dist/bundling-sources/sdk/src/compat.ts +35 -7
  2. package/dist/bundling-sources/sdk/src/release.ts +1 -1
  3. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-pool.ts +269 -267
  4. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +3 -4
  5. package/dist/bundling-sources/shared_libs/play-runtime/tool-http-errors.ts +14 -1
  6. package/dist/cli/auth-EDTRERZP.mjs +31 -0
  7. package/dist/cli/auth-TDHKB277.js +31 -0
  8. package/dist/cli/billing-ELIDPILY.js +1749 -0
  9. package/dist/cli/billing-EXGXJVAU.mjs +1749 -0
  10. package/dist/cli/chunk-2S5Y3TZP.js +5688 -0
  11. package/dist/cli/chunk-3KC45UVF.js +150 -0
  12. package/dist/cli/chunk-3XX5TEA4.js +15 -0
  13. package/dist/cli/chunk-4MBOJQPL.mjs +0 -0
  14. package/dist/cli/chunk-4NEFYBF7.js +2240 -0
  15. package/dist/cli/chunk-5JLQPVNX.js +3056 -0
  16. package/dist/cli/chunk-5KDKFHZI.js +1269 -0
  17. package/dist/cli/chunk-6A3TJUWC.js +76 -0
  18. package/dist/cli/chunk-6O5YZOVN.mjs +890 -0
  19. package/dist/cli/chunk-7P5AA3SN.js +344 -0
  20. package/dist/cli/chunk-AYHLUHE5.js +52 -0
  21. package/dist/cli/chunk-CBVIJSHF.mjs +560 -0
  22. package/dist/cli/chunk-CFVVCN4A.js +735 -0
  23. package/dist/cli/chunk-COGTRVWW.js +30 -0
  24. package/dist/cli/chunk-D4AZUZO4.js +190 -0
  25. package/dist/cli/chunk-DLIRSHV6.mjs +28 -0
  26. package/dist/cli/chunk-EACVZI6N.mjs +1995 -0
  27. package/dist/cli/chunk-EDWJY6T4.mjs +3056 -0
  28. package/dist/cli/chunk-EXMLR72R.mjs +0 -0
  29. package/dist/cli/chunk-F35VOIQL.js +890 -0
  30. package/dist/cli/chunk-GAMFK2ZP.mjs +148 -0
  31. package/dist/cli/chunk-GHRQQPW2.mjs +2240 -0
  32. package/dist/cli/chunk-GONRLVV3.js +3440 -0
  33. package/dist/cli/chunk-GVUJJJ5G.mjs +76 -0
  34. package/dist/cli/chunk-H7HIDGZ7.mjs +150 -0
  35. package/dist/cli/chunk-HUZXWOSK.mjs +503 -0
  36. package/dist/cli/chunk-I5YZLGMG.js +1 -0
  37. package/dist/cli/chunk-ITOFSXIU.mjs +15 -0
  38. package/dist/cli/chunk-J5NA2TTX.js +503 -0
  39. package/dist/cli/chunk-JCS5TPNY.mjs +3440 -0
  40. package/dist/cli/chunk-JNNOCMQY.js +47 -0
  41. package/dist/cli/chunk-KRD6H754.mjs +30 -0
  42. package/dist/cli/chunk-LA46MMTL.js +560 -0
  43. package/dist/cli/chunk-LDWT7RJK.mjs +190 -0
  44. package/dist/cli/chunk-M2V26OR5.js +499 -0
  45. package/dist/cli/chunk-MIMASXO4.mjs +389 -0
  46. package/dist/cli/chunk-MROPCKW2.mjs +190 -0
  47. package/dist/cli/chunk-NXDRDJ4K.js +389 -0
  48. package/dist/cli/chunk-NXGCO4JB.mjs +344 -0
  49. package/dist/cli/chunk-OG64RBAC.js +6 -0
  50. package/dist/cli/chunk-OQYEC4GL.mjs +34 -0
  51. package/dist/cli/chunk-OWJZUE3Y.mjs +103 -0
  52. package/dist/cli/chunk-SDK3ZA2C.js +103 -0
  53. package/dist/cli/chunk-SFLARO4Y.mjs +47 -0
  54. package/dist/cli/chunk-SVIIIYIQ.js +923 -0
  55. package/dist/cli/chunk-TAC3JIFE.mjs +354 -0
  56. package/dist/cli/chunk-U3PI7HBM.js +34 -0
  57. package/dist/cli/chunk-UFS752TU.mjs +52 -0
  58. package/dist/cli/chunk-UI37DQE7.mjs +499 -0
  59. package/dist/cli/chunk-UNEMDKXN.js +1 -0
  60. package/dist/cli/chunk-WB6Z2Q44.mjs +1622 -0
  61. package/dist/cli/chunk-WJYPBHTE.js +148 -0
  62. package/dist/cli/chunk-WOHBA5PV.js +354 -0
  63. package/dist/cli/chunk-WQICC2B3.js +190 -0
  64. package/dist/cli/chunk-WZYKRD7T.mjs +6 -0
  65. package/dist/cli/chunk-XL2FO6CW.mjs +5688 -0
  66. package/dist/cli/chunk-XNYIZMNQ.js +28 -0
  67. package/dist/cli/chunk-XRK2TZT5.js +1995 -0
  68. package/dist/cli/chunk-YJYPL5OK.mjs +1269 -0
  69. package/dist/cli/chunk-YWNDYCXR.mjs +735 -0
  70. package/dist/cli/chunk-YWWAF7KZ.mjs +923 -0
  71. package/dist/cli/chunk-ZDO3TO3M.js +1622 -0
  72. package/dist/cli/client-4JVOYVC5.js +20 -0
  73. package/dist/cli/client-EEN2IJN2.mjs +20 -0
  74. package/dist/cli/csv-BOHBEP2I.mjs +585 -0
  75. package/dist/cli/csv-S5NYRUEX.js +585 -0
  76. package/dist/cli/db-CLODWCOB.mjs +486 -0
  77. package/dist/cli/db-F6PKFOOV.js +486 -0
  78. package/dist/cli/executions-5TQAKLJU.js +28 -0
  79. package/dist/cli/executions-MAKW6QOY.mjs +28 -0
  80. package/dist/cli/feedback-GV4QJFEN.js +107 -0
  81. package/dist/cli/feedback-XY24TW7H.mjs +107 -0
  82. package/dist/cli/getting-started.js +18 -76
  83. package/dist/cli/getting-started.mjs +10 -37
  84. package/dist/cli/git-6S2C22T5.js +23 -0
  85. package/dist/cli/git-OZLBD5HH.mjs +23 -0
  86. package/dist/cli/index.js +291 -54294
  87. package/dist/cli/index.mjs +209 -54352
  88. package/dist/cli/legacy-noops-LLCGDPQE.js +157 -0
  89. package/dist/cli/legacy-noops-TYH3TW2D.mjs +157 -0
  90. package/dist/cli/monitors-CABMB4DW.js +3251 -0
  91. package/dist/cli/monitors-TTD55HMK.mjs +3251 -0
  92. package/dist/cli/org-MSF3RG76.js +849 -0
  93. package/dist/cli/org-SFJ77MWY.mjs +849 -0
  94. package/dist/cli/play-NXHJSCKL.mjs +18439 -0
  95. package/dist/cli/play-OKSFNNO4.js +18439 -0
  96. package/dist/cli/providers-QC2ERERL.js +95 -0
  97. package/dist/cli/providers-TIIAZEUA.mjs +95 -0
  98. package/dist/cli/proxy-env-EBANZHPV.js +161 -0
  99. package/dist/cli/proxy-env-M4ZHXHRL.mjs +161 -0
  100. package/dist/cli/quickstart-ICANHLMI.js +69 -0
  101. package/dist/cli/quickstart-KWUOP3FI.mjs +69 -0
  102. package/dist/cli/search-U5H2FDOO.mjs +18 -0
  103. package/dist/cli/search-XQRQOLWS.js +18 -0
  104. package/dist/cli/secrets-3J6YTWFA.mjs +400 -0
  105. package/dist/cli/secrets-YNMNZNOM.js +400 -0
  106. package/dist/cli/sessions-CLEES4L6.mjs +15 -0
  107. package/dist/cli/sessions-J6VR47FG.js +15 -0
  108. package/dist/cli/settings-EEKZMTY4.js +846 -0
  109. package/dist/cli/settings-UVLOQZRH.mjs +846 -0
  110. package/dist/cli/setup-DLMDXPGJ.mjs +1257 -0
  111. package/dist/cli/setup-HC4Q7QM4.js +1257 -0
  112. package/dist/cli/skills-33PHFBSB.mjs +28 -0
  113. package/dist/cli/skills-LFRY3KPX.js +28 -0
  114. package/dist/cli/tools-3COEPKML.mjs +2955 -0
  115. package/dist/cli/tools-QXEVCN45.js +2955 -0
  116. package/dist/cli/update-LSAC4FF5.mjs +28 -0
  117. package/dist/cli/update-NXHSPYXA.js +28 -0
  118. package/dist/cli/workflow-7S44OECF.mjs +1936 -0
  119. package/dist/cli/workflow-QPZWYSZE.js +1936 -0
  120. package/dist/helpers.js +6 -32
  121. package/dist/helpers.mjs +2 -0
  122. package/dist/index.js +1972 -12825
  123. package/dist/index.mjs +1910 -12672
  124. package/dist/install-integrity.json +110 -0
  125. package/dist/monitor-fleet-contract.js +31 -730
  126. package/dist/monitor-fleet-contract.mjs +18 -667
  127. package/dist/monitor-input-contract.js +10 -390
  128. package/dist/monitor-input-contract.mjs +7 -348
  129. package/dist/plays/bundle-play-file.mjs +13 -5178
  130. package/dist/plays/csv-data-input.js +5 -3132
  131. package/dist/plays/csv-data-input.mjs +5 -3105
  132. package/dist/plays/run-request.js +56 -664
  133. package/dist/plays/run-request.mjs +30 -588
  134. package/dist/release.d.mts +1 -1
  135. package/dist/release.d.ts +1 -1
  136. package/dist/release.js +5 -212
  137. package/dist/release.mjs +5 -185
  138. package/package.json +1 -1
@@ -0,0 +1,3056 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
+
3
+ var _chunk7P5AA3SNjs = require('./chunk-7P5AA3SN.js');
4
+
5
+ // node_modules/csv-parse/dist/esm/sync.js
6
+ var global$1 = typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {};
7
+ var lookup = [];
8
+ var revLookup = [];
9
+ var Arr = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
10
+ var inited = false;
11
+ function init() {
12
+ inited = true;
13
+ var code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
14
+ for (var i = 0, len = code.length; i < len; ++i) {
15
+ lookup[i] = code[i];
16
+ revLookup[code.charCodeAt(i)] = i;
17
+ }
18
+ revLookup["-".charCodeAt(0)] = 62;
19
+ revLookup["_".charCodeAt(0)] = 63;
20
+ }
21
+ function toByteArray(b64) {
22
+ if (!inited) {
23
+ init();
24
+ }
25
+ var i, j, l, tmp, placeHolders, arr;
26
+ var len = b64.length;
27
+ if (len % 4 > 0) {
28
+ throw new Error("Invalid string. Length must be a multiple of 4");
29
+ }
30
+ placeHolders = b64[len - 2] === "=" ? 2 : b64[len - 1] === "=" ? 1 : 0;
31
+ arr = new Arr(len * 3 / 4 - placeHolders);
32
+ l = placeHolders > 0 ? len - 4 : len;
33
+ var L = 0;
34
+ for (i = 0, j = 0; i < l; i += 4, j += 3) {
35
+ tmp = revLookup[b64.charCodeAt(i)] << 18 | revLookup[b64.charCodeAt(i + 1)] << 12 | revLookup[b64.charCodeAt(i + 2)] << 6 | revLookup[b64.charCodeAt(i + 3)];
36
+ arr[L++] = tmp >> 16 & 255;
37
+ arr[L++] = tmp >> 8 & 255;
38
+ arr[L++] = tmp & 255;
39
+ }
40
+ if (placeHolders === 2) {
41
+ tmp = revLookup[b64.charCodeAt(i)] << 2 | revLookup[b64.charCodeAt(i + 1)] >> 4;
42
+ arr[L++] = tmp & 255;
43
+ } else if (placeHolders === 1) {
44
+ tmp = revLookup[b64.charCodeAt(i)] << 10 | revLookup[b64.charCodeAt(i + 1)] << 4 | revLookup[b64.charCodeAt(i + 2)] >> 2;
45
+ arr[L++] = tmp >> 8 & 255;
46
+ arr[L++] = tmp & 255;
47
+ }
48
+ return arr;
49
+ }
50
+ function tripletToBase64(num) {
51
+ return lookup[num >> 18 & 63] + lookup[num >> 12 & 63] + lookup[num >> 6 & 63] + lookup[num & 63];
52
+ }
53
+ function encodeChunk(uint8, start, end) {
54
+ var tmp;
55
+ var output = [];
56
+ for (var i = start; i < end; i += 3) {
57
+ tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + uint8[i + 2];
58
+ output.push(tripletToBase64(tmp));
59
+ }
60
+ return output.join("");
61
+ }
62
+ function fromByteArray(uint8) {
63
+ if (!inited) {
64
+ init();
65
+ }
66
+ var tmp;
67
+ var len = uint8.length;
68
+ var extraBytes = len % 3;
69
+ var output = "";
70
+ var parts = [];
71
+ var maxChunkLength = 16383;
72
+ for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
73
+ parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength));
74
+ }
75
+ if (extraBytes === 1) {
76
+ tmp = uint8[len - 1];
77
+ output += lookup[tmp >> 2];
78
+ output += lookup[tmp << 4 & 63];
79
+ output += "==";
80
+ } else if (extraBytes === 2) {
81
+ tmp = (uint8[len - 2] << 8) + uint8[len - 1];
82
+ output += lookup[tmp >> 10];
83
+ output += lookup[tmp >> 4 & 63];
84
+ output += lookup[tmp << 2 & 63];
85
+ output += "=";
86
+ }
87
+ parts.push(output);
88
+ return parts.join("");
89
+ }
90
+ function read(buffer, offset, isLE, mLen, nBytes) {
91
+ var e, m;
92
+ var eLen = nBytes * 8 - mLen - 1;
93
+ var eMax = (1 << eLen) - 1;
94
+ var eBias = eMax >> 1;
95
+ var nBits = -7;
96
+ var i = isLE ? nBytes - 1 : 0;
97
+ var d = isLE ? -1 : 1;
98
+ var s = buffer[offset + i];
99
+ i += d;
100
+ e = s & (1 << -nBits) - 1;
101
+ s >>= -nBits;
102
+ nBits += eLen;
103
+ for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {
104
+ }
105
+ m = e & (1 << -nBits) - 1;
106
+ e >>= -nBits;
107
+ nBits += mLen;
108
+ for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {
109
+ }
110
+ if (e === 0) {
111
+ e = 1 - eBias;
112
+ } else if (e === eMax) {
113
+ return m ? NaN : (s ? -1 : 1) * Infinity;
114
+ } else {
115
+ m = m + Math.pow(2, mLen);
116
+ e = e - eBias;
117
+ }
118
+ return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
119
+ }
120
+ function write(buffer, value, offset, isLE, mLen, nBytes) {
121
+ var e, m, c;
122
+ var eLen = nBytes * 8 - mLen - 1;
123
+ var eMax = (1 << eLen) - 1;
124
+ var eBias = eMax >> 1;
125
+ var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
126
+ var i = isLE ? 0 : nBytes - 1;
127
+ var d = isLE ? 1 : -1;
128
+ var s = value < 0 || value === 0 && 1 / value < 0 ? 1 : 0;
129
+ value = Math.abs(value);
130
+ if (isNaN(value) || value === Infinity) {
131
+ m = isNaN(value) ? 1 : 0;
132
+ e = eMax;
133
+ } else {
134
+ e = Math.floor(Math.log(value) / Math.LN2);
135
+ if (value * (c = Math.pow(2, -e)) < 1) {
136
+ e--;
137
+ c *= 2;
138
+ }
139
+ if (e + eBias >= 1) {
140
+ value += rt / c;
141
+ } else {
142
+ value += rt * Math.pow(2, 1 - eBias);
143
+ }
144
+ if (value * c >= 2) {
145
+ e++;
146
+ c /= 2;
147
+ }
148
+ if (e + eBias >= eMax) {
149
+ m = 0;
150
+ e = eMax;
151
+ } else if (e + eBias >= 1) {
152
+ m = (value * c - 1) * Math.pow(2, mLen);
153
+ e = e + eBias;
154
+ } else {
155
+ m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
156
+ e = 0;
157
+ }
158
+ }
159
+ for (; mLen >= 8; buffer[offset + i] = m & 255, i += d, m /= 256, mLen -= 8) {
160
+ }
161
+ e = e << mLen | m;
162
+ eLen += mLen;
163
+ for (; eLen > 0; buffer[offset + i] = e & 255, i += d, e /= 256, eLen -= 8) {
164
+ }
165
+ buffer[offset + i - d] |= s * 128;
166
+ }
167
+ var toString = {}.toString;
168
+ var isArray = Array.isArray || function(arr) {
169
+ return toString.call(arr) == "[object Array]";
170
+ };
171
+ var INSPECT_MAX_BYTES = 50;
172
+ Buffer.TYPED_ARRAY_SUPPORT = global$1.TYPED_ARRAY_SUPPORT !== void 0 ? global$1.TYPED_ARRAY_SUPPORT : true;
173
+ kMaxLength();
174
+ function kMaxLength() {
175
+ return Buffer.TYPED_ARRAY_SUPPORT ? 2147483647 : 1073741823;
176
+ }
177
+ function createBuffer(that, length) {
178
+ if (kMaxLength() < length) {
179
+ throw new RangeError("Invalid typed array length");
180
+ }
181
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
182
+ that = new Uint8Array(length);
183
+ that.__proto__ = Buffer.prototype;
184
+ } else {
185
+ if (that === null) {
186
+ that = new Buffer(length);
187
+ }
188
+ that.length = length;
189
+ }
190
+ return that;
191
+ }
192
+ function Buffer(arg, encodingOrOffset, length) {
193
+ if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
194
+ return new Buffer(arg, encodingOrOffset, length);
195
+ }
196
+ if (typeof arg === "number") {
197
+ if (typeof encodingOrOffset === "string") {
198
+ throw new Error(
199
+ "If encoding is specified then the first argument must be a string"
200
+ );
201
+ }
202
+ return allocUnsafe(this, arg);
203
+ }
204
+ return from(this, arg, encodingOrOffset, length);
205
+ }
206
+ Buffer.poolSize = 8192;
207
+ Buffer._augment = function(arr) {
208
+ arr.__proto__ = Buffer.prototype;
209
+ return arr;
210
+ };
211
+ function from(that, value, encodingOrOffset, length) {
212
+ if (typeof value === "number") {
213
+ throw new TypeError('"value" argument must not be a number');
214
+ }
215
+ if (typeof ArrayBuffer !== "undefined" && value instanceof ArrayBuffer) {
216
+ return fromArrayBuffer(that, value, encodingOrOffset, length);
217
+ }
218
+ if (typeof value === "string") {
219
+ return fromString(that, value, encodingOrOffset);
220
+ }
221
+ return fromObject(that, value);
222
+ }
223
+ Buffer.from = function(value, encodingOrOffset, length) {
224
+ return from(null, value, encodingOrOffset, length);
225
+ };
226
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
227
+ Buffer.prototype.__proto__ = Uint8Array.prototype;
228
+ Buffer.__proto__ = Uint8Array;
229
+ if (typeof Symbol !== "undefined" && Symbol.species && Buffer[Symbol.species] === Buffer) ;
230
+ }
231
+ function assertSize(size) {
232
+ if (typeof size !== "number") {
233
+ throw new TypeError('"size" argument must be a number');
234
+ } else if (size < 0) {
235
+ throw new RangeError('"size" argument must not be negative');
236
+ }
237
+ }
238
+ function alloc(that, size, fill2, encoding) {
239
+ assertSize(size);
240
+ if (size <= 0) {
241
+ return createBuffer(that, size);
242
+ }
243
+ if (fill2 !== void 0) {
244
+ return typeof encoding === "string" ? createBuffer(that, size).fill(fill2, encoding) : createBuffer(that, size).fill(fill2);
245
+ }
246
+ return createBuffer(that, size);
247
+ }
248
+ Buffer.alloc = function(size, fill2, encoding) {
249
+ return alloc(null, size, fill2, encoding);
250
+ };
251
+ function allocUnsafe(that, size) {
252
+ assertSize(size);
253
+ that = createBuffer(that, size < 0 ? 0 : checked(size) | 0);
254
+ if (!Buffer.TYPED_ARRAY_SUPPORT) {
255
+ for (var i = 0; i < size; ++i) {
256
+ that[i] = 0;
257
+ }
258
+ }
259
+ return that;
260
+ }
261
+ Buffer.allocUnsafe = function(size) {
262
+ return allocUnsafe(null, size);
263
+ };
264
+ Buffer.allocUnsafeSlow = function(size) {
265
+ return allocUnsafe(null, size);
266
+ };
267
+ function fromString(that, string, encoding) {
268
+ if (typeof encoding !== "string" || encoding === "") {
269
+ encoding = "utf8";
270
+ }
271
+ if (!Buffer.isEncoding(encoding)) {
272
+ throw new TypeError('"encoding" must be a valid string encoding');
273
+ }
274
+ var length = byteLength(string, encoding) | 0;
275
+ that = createBuffer(that, length);
276
+ var actual = that.write(string, encoding);
277
+ if (actual !== length) {
278
+ that = that.slice(0, actual);
279
+ }
280
+ return that;
281
+ }
282
+ function fromArrayLike(that, array) {
283
+ var length = array.length < 0 ? 0 : checked(array.length) | 0;
284
+ that = createBuffer(that, length);
285
+ for (var i = 0; i < length; i += 1) {
286
+ that[i] = array[i] & 255;
287
+ }
288
+ return that;
289
+ }
290
+ function fromArrayBuffer(that, array, byteOffset, length) {
291
+ array.byteLength;
292
+ if (byteOffset < 0 || array.byteLength < byteOffset) {
293
+ throw new RangeError("'offset' is out of bounds");
294
+ }
295
+ if (array.byteLength < byteOffset + (length || 0)) {
296
+ throw new RangeError("'length' is out of bounds");
297
+ }
298
+ if (byteOffset === void 0 && length === void 0) {
299
+ array = new Uint8Array(array);
300
+ } else if (length === void 0) {
301
+ array = new Uint8Array(array, byteOffset);
302
+ } else {
303
+ array = new Uint8Array(array, byteOffset, length);
304
+ }
305
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
306
+ that = array;
307
+ that.__proto__ = Buffer.prototype;
308
+ } else {
309
+ that = fromArrayLike(that, array);
310
+ }
311
+ return that;
312
+ }
313
+ function fromObject(that, obj) {
314
+ if (internalIsBuffer(obj)) {
315
+ var len = checked(obj.length) | 0;
316
+ that = createBuffer(that, len);
317
+ if (that.length === 0) {
318
+ return that;
319
+ }
320
+ obj.copy(that, 0, 0, len);
321
+ return that;
322
+ }
323
+ if (obj) {
324
+ if (typeof ArrayBuffer !== "undefined" && obj.buffer instanceof ArrayBuffer || "length" in obj) {
325
+ if (typeof obj.length !== "number" || isnan(obj.length)) {
326
+ return createBuffer(that, 0);
327
+ }
328
+ return fromArrayLike(that, obj);
329
+ }
330
+ if (obj.type === "Buffer" && isArray(obj.data)) {
331
+ return fromArrayLike(that, obj.data);
332
+ }
333
+ }
334
+ throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.");
335
+ }
336
+ function checked(length) {
337
+ if (length >= kMaxLength()) {
338
+ throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x" + kMaxLength().toString(16) + " bytes");
339
+ }
340
+ return length | 0;
341
+ }
342
+ Buffer.isBuffer = isBuffer;
343
+ function internalIsBuffer(b) {
344
+ return !!(b != null && b._isBuffer);
345
+ }
346
+ Buffer.compare = function compare(a, b) {
347
+ if (!internalIsBuffer(a) || !internalIsBuffer(b)) {
348
+ throw new TypeError("Arguments must be Buffers");
349
+ }
350
+ if (a === b) return 0;
351
+ var x = a.length;
352
+ var y = b.length;
353
+ for (var i = 0, len = Math.min(x, y); i < len; ++i) {
354
+ if (a[i] !== b[i]) {
355
+ x = a[i];
356
+ y = b[i];
357
+ break;
358
+ }
359
+ }
360
+ if (x < y) return -1;
361
+ if (y < x) return 1;
362
+ return 0;
363
+ };
364
+ Buffer.isEncoding = function isEncoding(encoding) {
365
+ switch (String(encoding).toLowerCase()) {
366
+ case "hex":
367
+ case "utf8":
368
+ case "utf-8":
369
+ case "ascii":
370
+ case "latin1":
371
+ case "binary":
372
+ case "base64":
373
+ case "ucs2":
374
+ case "ucs-2":
375
+ case "utf16le":
376
+ case "utf-16le":
377
+ return true;
378
+ default:
379
+ return false;
380
+ }
381
+ };
382
+ Buffer.concat = function concat(list, length) {
383
+ if (!isArray(list)) {
384
+ throw new TypeError('"list" argument must be an Array of Buffers');
385
+ }
386
+ if (list.length === 0) {
387
+ return Buffer.alloc(0);
388
+ }
389
+ var i;
390
+ if (length === void 0) {
391
+ length = 0;
392
+ for (i = 0; i < list.length; ++i) {
393
+ length += list[i].length;
394
+ }
395
+ }
396
+ var buffer = Buffer.allocUnsafe(length);
397
+ var pos = 0;
398
+ for (i = 0; i < list.length; ++i) {
399
+ var buf = list[i];
400
+ if (!internalIsBuffer(buf)) {
401
+ throw new TypeError('"list" argument must be an Array of Buffers');
402
+ }
403
+ buf.copy(buffer, pos);
404
+ pos += buf.length;
405
+ }
406
+ return buffer;
407
+ };
408
+ function byteLength(string, encoding) {
409
+ if (internalIsBuffer(string)) {
410
+ return string.length;
411
+ }
412
+ if (typeof ArrayBuffer !== "undefined" && typeof ArrayBuffer.isView === "function" && (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
413
+ return string.byteLength;
414
+ }
415
+ if (typeof string !== "string") {
416
+ string = "" + string;
417
+ }
418
+ var len = string.length;
419
+ if (len === 0) return 0;
420
+ var loweredCase = false;
421
+ for (; ; ) {
422
+ switch (encoding) {
423
+ case "ascii":
424
+ case "latin1":
425
+ case "binary":
426
+ return len;
427
+ case "utf8":
428
+ case "utf-8":
429
+ case void 0:
430
+ return utf8ToBytes(string).length;
431
+ case "ucs2":
432
+ case "ucs-2":
433
+ case "utf16le":
434
+ case "utf-16le":
435
+ return len * 2;
436
+ case "hex":
437
+ return len >>> 1;
438
+ case "base64":
439
+ return base64ToBytes(string).length;
440
+ default:
441
+ if (loweredCase) return utf8ToBytes(string).length;
442
+ encoding = ("" + encoding).toLowerCase();
443
+ loweredCase = true;
444
+ }
445
+ }
446
+ }
447
+ Buffer.byteLength = byteLength;
448
+ function slowToString(encoding, start, end) {
449
+ var loweredCase = false;
450
+ if (start === void 0 || start < 0) {
451
+ start = 0;
452
+ }
453
+ if (start > this.length) {
454
+ return "";
455
+ }
456
+ if (end === void 0 || end > this.length) {
457
+ end = this.length;
458
+ }
459
+ if (end <= 0) {
460
+ return "";
461
+ }
462
+ end >>>= 0;
463
+ start >>>= 0;
464
+ if (end <= start) {
465
+ return "";
466
+ }
467
+ if (!encoding) encoding = "utf8";
468
+ while (true) {
469
+ switch (encoding) {
470
+ case "hex":
471
+ return hexSlice(this, start, end);
472
+ case "utf8":
473
+ case "utf-8":
474
+ return utf8Slice(this, start, end);
475
+ case "ascii":
476
+ return asciiSlice(this, start, end);
477
+ case "latin1":
478
+ case "binary":
479
+ return latin1Slice(this, start, end);
480
+ case "base64":
481
+ return base64Slice(this, start, end);
482
+ case "ucs2":
483
+ case "ucs-2":
484
+ case "utf16le":
485
+ case "utf-16le":
486
+ return utf16leSlice(this, start, end);
487
+ default:
488
+ if (loweredCase) throw new TypeError("Unknown encoding: " + encoding);
489
+ encoding = (encoding + "").toLowerCase();
490
+ loweredCase = true;
491
+ }
492
+ }
493
+ }
494
+ Buffer.prototype._isBuffer = true;
495
+ function swap(b, n, m) {
496
+ var i = b[n];
497
+ b[n] = b[m];
498
+ b[m] = i;
499
+ }
500
+ Buffer.prototype.swap16 = function swap16() {
501
+ var len = this.length;
502
+ if (len % 2 !== 0) {
503
+ throw new RangeError("Buffer size must be a multiple of 16-bits");
504
+ }
505
+ for (var i = 0; i < len; i += 2) {
506
+ swap(this, i, i + 1);
507
+ }
508
+ return this;
509
+ };
510
+ Buffer.prototype.swap32 = function swap32() {
511
+ var len = this.length;
512
+ if (len % 4 !== 0) {
513
+ throw new RangeError("Buffer size must be a multiple of 32-bits");
514
+ }
515
+ for (var i = 0; i < len; i += 4) {
516
+ swap(this, i, i + 3);
517
+ swap(this, i + 1, i + 2);
518
+ }
519
+ return this;
520
+ };
521
+ Buffer.prototype.swap64 = function swap64() {
522
+ var len = this.length;
523
+ if (len % 8 !== 0) {
524
+ throw new RangeError("Buffer size must be a multiple of 64-bits");
525
+ }
526
+ for (var i = 0; i < len; i += 8) {
527
+ swap(this, i, i + 7);
528
+ swap(this, i + 1, i + 6);
529
+ swap(this, i + 2, i + 5);
530
+ swap(this, i + 3, i + 4);
531
+ }
532
+ return this;
533
+ };
534
+ Buffer.prototype.toString = function toString2() {
535
+ var length = this.length | 0;
536
+ if (length === 0) return "";
537
+ if (arguments.length === 0) return utf8Slice(this, 0, length);
538
+ return slowToString.apply(this, arguments);
539
+ };
540
+ Buffer.prototype.equals = function equals(b) {
541
+ if (!internalIsBuffer(b)) throw new TypeError("Argument must be a Buffer");
542
+ if (this === b) return true;
543
+ return Buffer.compare(this, b) === 0;
544
+ };
545
+ Buffer.prototype.inspect = function inspect() {
546
+ var str = "";
547
+ var max = INSPECT_MAX_BYTES;
548
+ if (this.length > 0) {
549
+ str = this.toString("hex", 0, max).match(/.{2}/g).join(" ");
550
+ if (this.length > max) str += " ... ";
551
+ }
552
+ return "<Buffer " + str + ">";
553
+ };
554
+ Buffer.prototype.compare = function compare2(target, start, end, thisStart, thisEnd) {
555
+ if (!internalIsBuffer(target)) {
556
+ throw new TypeError("Argument must be a Buffer");
557
+ }
558
+ if (start === void 0) {
559
+ start = 0;
560
+ }
561
+ if (end === void 0) {
562
+ end = target ? target.length : 0;
563
+ }
564
+ if (thisStart === void 0) {
565
+ thisStart = 0;
566
+ }
567
+ if (thisEnd === void 0) {
568
+ thisEnd = this.length;
569
+ }
570
+ if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
571
+ throw new RangeError("out of range index");
572
+ }
573
+ if (thisStart >= thisEnd && start >= end) {
574
+ return 0;
575
+ }
576
+ if (thisStart >= thisEnd) {
577
+ return -1;
578
+ }
579
+ if (start >= end) {
580
+ return 1;
581
+ }
582
+ start >>>= 0;
583
+ end >>>= 0;
584
+ thisStart >>>= 0;
585
+ thisEnd >>>= 0;
586
+ if (this === target) return 0;
587
+ var x = thisEnd - thisStart;
588
+ var y = end - start;
589
+ var len = Math.min(x, y);
590
+ var thisCopy = this.slice(thisStart, thisEnd);
591
+ var targetCopy = target.slice(start, end);
592
+ for (var i = 0; i < len; ++i) {
593
+ if (thisCopy[i] !== targetCopy[i]) {
594
+ x = thisCopy[i];
595
+ y = targetCopy[i];
596
+ break;
597
+ }
598
+ }
599
+ if (x < y) return -1;
600
+ if (y < x) return 1;
601
+ return 0;
602
+ };
603
+ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
604
+ if (buffer.length === 0) return -1;
605
+ if (typeof byteOffset === "string") {
606
+ encoding = byteOffset;
607
+ byteOffset = 0;
608
+ } else if (byteOffset > 2147483647) {
609
+ byteOffset = 2147483647;
610
+ } else if (byteOffset < -2147483648) {
611
+ byteOffset = -2147483648;
612
+ }
613
+ byteOffset = +byteOffset;
614
+ if (isNaN(byteOffset)) {
615
+ byteOffset = dir ? 0 : buffer.length - 1;
616
+ }
617
+ if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
618
+ if (byteOffset >= buffer.length) {
619
+ if (dir) return -1;
620
+ else byteOffset = buffer.length - 1;
621
+ } else if (byteOffset < 0) {
622
+ if (dir) byteOffset = 0;
623
+ else return -1;
624
+ }
625
+ if (typeof val === "string") {
626
+ val = Buffer.from(val, encoding);
627
+ }
628
+ if (internalIsBuffer(val)) {
629
+ if (val.length === 0) {
630
+ return -1;
631
+ }
632
+ return arrayIndexOf(buffer, val, byteOffset, encoding, dir);
633
+ } else if (typeof val === "number") {
634
+ val = val & 255;
635
+ if (Buffer.TYPED_ARRAY_SUPPORT && typeof Uint8Array.prototype.indexOf === "function") {
636
+ if (dir) {
637
+ return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset);
638
+ } else {
639
+ return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset);
640
+ }
641
+ }
642
+ return arrayIndexOf(buffer, [val], byteOffset, encoding, dir);
643
+ }
644
+ throw new TypeError("val must be string, number or Buffer");
645
+ }
646
+ function arrayIndexOf(arr, val, byteOffset, encoding, dir) {
647
+ var indexSize = 1;
648
+ var arrLength = arr.length;
649
+ var valLength = val.length;
650
+ if (encoding !== void 0) {
651
+ encoding = String(encoding).toLowerCase();
652
+ if (encoding === "ucs2" || encoding === "ucs-2" || encoding === "utf16le" || encoding === "utf-16le") {
653
+ if (arr.length < 2 || val.length < 2) {
654
+ return -1;
655
+ }
656
+ indexSize = 2;
657
+ arrLength /= 2;
658
+ valLength /= 2;
659
+ byteOffset /= 2;
660
+ }
661
+ }
662
+ function read2(buf, i2) {
663
+ if (indexSize === 1) {
664
+ return buf[i2];
665
+ } else {
666
+ return buf.readUInt16BE(i2 * indexSize);
667
+ }
668
+ }
669
+ var i;
670
+ if (dir) {
671
+ var foundIndex = -1;
672
+ for (i = byteOffset; i < arrLength; i++) {
673
+ if (read2(arr, i) === read2(val, foundIndex === -1 ? 0 : i - foundIndex)) {
674
+ if (foundIndex === -1) foundIndex = i;
675
+ if (i - foundIndex + 1 === valLength) return foundIndex * indexSize;
676
+ } else {
677
+ if (foundIndex !== -1) i -= i - foundIndex;
678
+ foundIndex = -1;
679
+ }
680
+ }
681
+ } else {
682
+ if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
683
+ for (i = byteOffset; i >= 0; i--) {
684
+ var found = true;
685
+ for (var j = 0; j < valLength; j++) {
686
+ if (read2(arr, i + j) !== read2(val, j)) {
687
+ found = false;
688
+ break;
689
+ }
690
+ }
691
+ if (found) return i;
692
+ }
693
+ }
694
+ return -1;
695
+ }
696
+ Buffer.prototype.includes = function includes(val, byteOffset, encoding) {
697
+ return this.indexOf(val, byteOffset, encoding) !== -1;
698
+ };
699
+ Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
700
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, true);
701
+ };
702
+ Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) {
703
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, false);
704
+ };
705
+ function hexWrite(buf, string, offset, length) {
706
+ offset = Number(offset) || 0;
707
+ var remaining = buf.length - offset;
708
+ if (!length) {
709
+ length = remaining;
710
+ } else {
711
+ length = Number(length);
712
+ if (length > remaining) {
713
+ length = remaining;
714
+ }
715
+ }
716
+ var strLen = string.length;
717
+ if (strLen % 2 !== 0) throw new TypeError("Invalid hex string");
718
+ if (length > strLen / 2) {
719
+ length = strLen / 2;
720
+ }
721
+ for (var i = 0; i < length; ++i) {
722
+ var parsed = parseInt(string.substr(i * 2, 2), 16);
723
+ if (isNaN(parsed)) return i;
724
+ buf[offset + i] = parsed;
725
+ }
726
+ return i;
727
+ }
728
+ function utf8Write(buf, string, offset, length) {
729
+ return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length);
730
+ }
731
+ function asciiWrite(buf, string, offset, length) {
732
+ return blitBuffer(asciiToBytes(string), buf, offset, length);
733
+ }
734
+ function latin1Write(buf, string, offset, length) {
735
+ return asciiWrite(buf, string, offset, length);
736
+ }
737
+ function base64Write(buf, string, offset, length) {
738
+ return blitBuffer(base64ToBytes(string), buf, offset, length);
739
+ }
740
+ function ucs2Write(buf, string, offset, length) {
741
+ return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length);
742
+ }
743
+ Buffer.prototype.write = function write2(string, offset, length, encoding) {
744
+ if (offset === void 0) {
745
+ encoding = "utf8";
746
+ length = this.length;
747
+ offset = 0;
748
+ } else if (length === void 0 && typeof offset === "string") {
749
+ encoding = offset;
750
+ length = this.length;
751
+ offset = 0;
752
+ } else if (isFinite(offset)) {
753
+ offset = offset | 0;
754
+ if (isFinite(length)) {
755
+ length = length | 0;
756
+ if (encoding === void 0) encoding = "utf8";
757
+ } else {
758
+ encoding = length;
759
+ length = void 0;
760
+ }
761
+ } else {
762
+ throw new Error(
763
+ "Buffer.write(string, encoding, offset[, length]) is no longer supported"
764
+ );
765
+ }
766
+ var remaining = this.length - offset;
767
+ if (length === void 0 || length > remaining) length = remaining;
768
+ if (string.length > 0 && (length < 0 || offset < 0) || offset > this.length) {
769
+ throw new RangeError("Attempt to write outside buffer bounds");
770
+ }
771
+ if (!encoding) encoding = "utf8";
772
+ var loweredCase = false;
773
+ for (; ; ) {
774
+ switch (encoding) {
775
+ case "hex":
776
+ return hexWrite(this, string, offset, length);
777
+ case "utf8":
778
+ case "utf-8":
779
+ return utf8Write(this, string, offset, length);
780
+ case "ascii":
781
+ return asciiWrite(this, string, offset, length);
782
+ case "latin1":
783
+ case "binary":
784
+ return latin1Write(this, string, offset, length);
785
+ case "base64":
786
+ return base64Write(this, string, offset, length);
787
+ case "ucs2":
788
+ case "ucs-2":
789
+ case "utf16le":
790
+ case "utf-16le":
791
+ return ucs2Write(this, string, offset, length);
792
+ default:
793
+ if (loweredCase) throw new TypeError("Unknown encoding: " + encoding);
794
+ encoding = ("" + encoding).toLowerCase();
795
+ loweredCase = true;
796
+ }
797
+ }
798
+ };
799
+ Buffer.prototype.toJSON = function toJSON() {
800
+ return {
801
+ type: "Buffer",
802
+ data: Array.prototype.slice.call(this._arr || this, 0)
803
+ };
804
+ };
805
+ function base64Slice(buf, start, end) {
806
+ if (start === 0 && end === buf.length) {
807
+ return fromByteArray(buf);
808
+ } else {
809
+ return fromByteArray(buf.slice(start, end));
810
+ }
811
+ }
812
+ function utf8Slice(buf, start, end) {
813
+ end = Math.min(buf.length, end);
814
+ var res = [];
815
+ var i = start;
816
+ while (i < end) {
817
+ var firstByte = buf[i];
818
+ var codePoint = null;
819
+ var bytesPerSequence = firstByte > 239 ? 4 : firstByte > 223 ? 3 : firstByte > 191 ? 2 : 1;
820
+ if (i + bytesPerSequence <= end) {
821
+ var secondByte, thirdByte, fourthByte, tempCodePoint;
822
+ switch (bytesPerSequence) {
823
+ case 1:
824
+ if (firstByte < 128) {
825
+ codePoint = firstByte;
826
+ }
827
+ break;
828
+ case 2:
829
+ secondByte = buf[i + 1];
830
+ if ((secondByte & 192) === 128) {
831
+ tempCodePoint = (firstByte & 31) << 6 | secondByte & 63;
832
+ if (tempCodePoint > 127) {
833
+ codePoint = tempCodePoint;
834
+ }
835
+ }
836
+ break;
837
+ case 3:
838
+ secondByte = buf[i + 1];
839
+ thirdByte = buf[i + 2];
840
+ if ((secondByte & 192) === 128 && (thirdByte & 192) === 128) {
841
+ tempCodePoint = (firstByte & 15) << 12 | (secondByte & 63) << 6 | thirdByte & 63;
842
+ if (tempCodePoint > 2047 && (tempCodePoint < 55296 || tempCodePoint > 57343)) {
843
+ codePoint = tempCodePoint;
844
+ }
845
+ }
846
+ break;
847
+ case 4:
848
+ secondByte = buf[i + 1];
849
+ thirdByte = buf[i + 2];
850
+ fourthByte = buf[i + 3];
851
+ if ((secondByte & 192) === 128 && (thirdByte & 192) === 128 && (fourthByte & 192) === 128) {
852
+ tempCodePoint = (firstByte & 15) << 18 | (secondByte & 63) << 12 | (thirdByte & 63) << 6 | fourthByte & 63;
853
+ if (tempCodePoint > 65535 && tempCodePoint < 1114112) {
854
+ codePoint = tempCodePoint;
855
+ }
856
+ }
857
+ }
858
+ }
859
+ if (codePoint === null) {
860
+ codePoint = 65533;
861
+ bytesPerSequence = 1;
862
+ } else if (codePoint > 65535) {
863
+ codePoint -= 65536;
864
+ res.push(codePoint >>> 10 & 1023 | 55296);
865
+ codePoint = 56320 | codePoint & 1023;
866
+ }
867
+ res.push(codePoint);
868
+ i += bytesPerSequence;
869
+ }
870
+ return decodeCodePointsArray(res);
871
+ }
872
+ var MAX_ARGUMENTS_LENGTH = 4096;
873
+ function decodeCodePointsArray(codePoints) {
874
+ var len = codePoints.length;
875
+ if (len <= MAX_ARGUMENTS_LENGTH) {
876
+ return String.fromCharCode.apply(String, codePoints);
877
+ }
878
+ var res = "";
879
+ var i = 0;
880
+ while (i < len) {
881
+ res += String.fromCharCode.apply(
882
+ String,
883
+ codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
884
+ );
885
+ }
886
+ return res;
887
+ }
888
+ function asciiSlice(buf, start, end) {
889
+ var ret = "";
890
+ end = Math.min(buf.length, end);
891
+ for (var i = start; i < end; ++i) {
892
+ ret += String.fromCharCode(buf[i] & 127);
893
+ }
894
+ return ret;
895
+ }
896
+ function latin1Slice(buf, start, end) {
897
+ var ret = "";
898
+ end = Math.min(buf.length, end);
899
+ for (var i = start; i < end; ++i) {
900
+ ret += String.fromCharCode(buf[i]);
901
+ }
902
+ return ret;
903
+ }
904
+ function hexSlice(buf, start, end) {
905
+ var len = buf.length;
906
+ if (!start || start < 0) start = 0;
907
+ if (!end || end < 0 || end > len) end = len;
908
+ var out = "";
909
+ for (var i = start; i < end; ++i) {
910
+ out += toHex(buf[i]);
911
+ }
912
+ return out;
913
+ }
914
+ function utf16leSlice(buf, start, end) {
915
+ var bytes = buf.slice(start, end);
916
+ var res = "";
917
+ for (var i = 0; i < bytes.length; i += 2) {
918
+ res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256);
919
+ }
920
+ return res;
921
+ }
922
+ Buffer.prototype.slice = function slice(start, end) {
923
+ var len = this.length;
924
+ start = ~~start;
925
+ end = end === void 0 ? len : ~~end;
926
+ if (start < 0) {
927
+ start += len;
928
+ if (start < 0) start = 0;
929
+ } else if (start > len) {
930
+ start = len;
931
+ }
932
+ if (end < 0) {
933
+ end += len;
934
+ if (end < 0) end = 0;
935
+ } else if (end > len) {
936
+ end = len;
937
+ }
938
+ if (end < start) end = start;
939
+ var newBuf;
940
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
941
+ newBuf = this.subarray(start, end);
942
+ newBuf.__proto__ = Buffer.prototype;
943
+ } else {
944
+ var sliceLen = end - start;
945
+ newBuf = new Buffer(sliceLen, void 0);
946
+ for (var i = 0; i < sliceLen; ++i) {
947
+ newBuf[i] = this[i + start];
948
+ }
949
+ }
950
+ return newBuf;
951
+ };
952
+ function checkOffset(offset, ext, length) {
953
+ if (offset % 1 !== 0 || offset < 0) throw new RangeError("offset is not uint");
954
+ if (offset + ext > length) throw new RangeError("Trying to access beyond buffer length");
955
+ }
956
+ Buffer.prototype.readUIntLE = function readUIntLE(offset, byteLength2, noAssert) {
957
+ offset = offset | 0;
958
+ byteLength2 = byteLength2 | 0;
959
+ if (!noAssert) checkOffset(offset, byteLength2, this.length);
960
+ var val = this[offset];
961
+ var mul = 1;
962
+ var i = 0;
963
+ while (++i < byteLength2 && (mul *= 256)) {
964
+ val += this[offset + i] * mul;
965
+ }
966
+ return val;
967
+ };
968
+ Buffer.prototype.readUIntBE = function readUIntBE(offset, byteLength2, noAssert) {
969
+ offset = offset | 0;
970
+ byteLength2 = byteLength2 | 0;
971
+ if (!noAssert) {
972
+ checkOffset(offset, byteLength2, this.length);
973
+ }
974
+ var val = this[offset + --byteLength2];
975
+ var mul = 1;
976
+ while (byteLength2 > 0 && (mul *= 256)) {
977
+ val += this[offset + --byteLength2] * mul;
978
+ }
979
+ return val;
980
+ };
981
+ Buffer.prototype.readUInt8 = function readUInt8(offset, noAssert) {
982
+ if (!noAssert) checkOffset(offset, 1, this.length);
983
+ return this[offset];
984
+ };
985
+ Buffer.prototype.readUInt16LE = function readUInt16LE(offset, noAssert) {
986
+ if (!noAssert) checkOffset(offset, 2, this.length);
987
+ return this[offset] | this[offset + 1] << 8;
988
+ };
989
+ Buffer.prototype.readUInt16BE = function readUInt16BE(offset, noAssert) {
990
+ if (!noAssert) checkOffset(offset, 2, this.length);
991
+ return this[offset] << 8 | this[offset + 1];
992
+ };
993
+ Buffer.prototype.readUInt32LE = function readUInt32LE(offset, noAssert) {
994
+ if (!noAssert) checkOffset(offset, 4, this.length);
995
+ return (this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16) + this[offset + 3] * 16777216;
996
+ };
997
+ Buffer.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) {
998
+ if (!noAssert) checkOffset(offset, 4, this.length);
999
+ return this[offset] * 16777216 + (this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3]);
1000
+ };
1001
+ Buffer.prototype.readIntLE = function readIntLE(offset, byteLength2, noAssert) {
1002
+ offset = offset | 0;
1003
+ byteLength2 = byteLength2 | 0;
1004
+ if (!noAssert) checkOffset(offset, byteLength2, this.length);
1005
+ var val = this[offset];
1006
+ var mul = 1;
1007
+ var i = 0;
1008
+ while (++i < byteLength2 && (mul *= 256)) {
1009
+ val += this[offset + i] * mul;
1010
+ }
1011
+ mul *= 128;
1012
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength2);
1013
+ return val;
1014
+ };
1015
+ Buffer.prototype.readIntBE = function readIntBE(offset, byteLength2, noAssert) {
1016
+ offset = offset | 0;
1017
+ byteLength2 = byteLength2 | 0;
1018
+ if (!noAssert) checkOffset(offset, byteLength2, this.length);
1019
+ var i = byteLength2;
1020
+ var mul = 1;
1021
+ var val = this[offset + --i];
1022
+ while (i > 0 && (mul *= 256)) {
1023
+ val += this[offset + --i] * mul;
1024
+ }
1025
+ mul *= 128;
1026
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength2);
1027
+ return val;
1028
+ };
1029
+ Buffer.prototype.readInt8 = function readInt8(offset, noAssert) {
1030
+ if (!noAssert) checkOffset(offset, 1, this.length);
1031
+ if (!(this[offset] & 128)) return this[offset];
1032
+ return (255 - this[offset] + 1) * -1;
1033
+ };
1034
+ Buffer.prototype.readInt16LE = function readInt16LE(offset, noAssert) {
1035
+ if (!noAssert) checkOffset(offset, 2, this.length);
1036
+ var val = this[offset] | this[offset + 1] << 8;
1037
+ return val & 32768 ? val | 4294901760 : val;
1038
+ };
1039
+ Buffer.prototype.readInt16BE = function readInt16BE(offset, noAssert) {
1040
+ if (!noAssert) checkOffset(offset, 2, this.length);
1041
+ var val = this[offset + 1] | this[offset] << 8;
1042
+ return val & 32768 ? val | 4294901760 : val;
1043
+ };
1044
+ Buffer.prototype.readInt32LE = function readInt32LE(offset, noAssert) {
1045
+ if (!noAssert) checkOffset(offset, 4, this.length);
1046
+ return this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16 | this[offset + 3] << 24;
1047
+ };
1048
+ Buffer.prototype.readInt32BE = function readInt32BE(offset, noAssert) {
1049
+ if (!noAssert) checkOffset(offset, 4, this.length);
1050
+ return this[offset] << 24 | this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3];
1051
+ };
1052
+ Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) {
1053
+ if (!noAssert) checkOffset(offset, 4, this.length);
1054
+ return read(this, offset, true, 23, 4);
1055
+ };
1056
+ Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) {
1057
+ if (!noAssert) checkOffset(offset, 4, this.length);
1058
+ return read(this, offset, false, 23, 4);
1059
+ };
1060
+ Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {
1061
+ if (!noAssert) checkOffset(offset, 8, this.length);
1062
+ return read(this, offset, true, 52, 8);
1063
+ };
1064
+ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
1065
+ if (!noAssert) checkOffset(offset, 8, this.length);
1066
+ return read(this, offset, false, 52, 8);
1067
+ };
1068
+ function checkInt(buf, value, offset, ext, max, min) {
1069
+ if (!internalIsBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance');
1070
+ if (value > max || value < min) throw new RangeError('"value" argument is out of bounds');
1071
+ if (offset + ext > buf.length) throw new RangeError("Index out of range");
1072
+ }
1073
+ Buffer.prototype.writeUIntLE = function writeUIntLE(value, offset, byteLength2, noAssert) {
1074
+ value = +value;
1075
+ offset = offset | 0;
1076
+ byteLength2 = byteLength2 | 0;
1077
+ if (!noAssert) {
1078
+ var maxBytes = Math.pow(2, 8 * byteLength2) - 1;
1079
+ checkInt(this, value, offset, byteLength2, maxBytes, 0);
1080
+ }
1081
+ var mul = 1;
1082
+ var i = 0;
1083
+ this[offset] = value & 255;
1084
+ while (++i < byteLength2 && (mul *= 256)) {
1085
+ this[offset + i] = value / mul & 255;
1086
+ }
1087
+ return offset + byteLength2;
1088
+ };
1089
+ Buffer.prototype.writeUIntBE = function writeUIntBE(value, offset, byteLength2, noAssert) {
1090
+ value = +value;
1091
+ offset = offset | 0;
1092
+ byteLength2 = byteLength2 | 0;
1093
+ if (!noAssert) {
1094
+ var maxBytes = Math.pow(2, 8 * byteLength2) - 1;
1095
+ checkInt(this, value, offset, byteLength2, maxBytes, 0);
1096
+ }
1097
+ var i = byteLength2 - 1;
1098
+ var mul = 1;
1099
+ this[offset + i] = value & 255;
1100
+ while (--i >= 0 && (mul *= 256)) {
1101
+ this[offset + i] = value / mul & 255;
1102
+ }
1103
+ return offset + byteLength2;
1104
+ };
1105
+ Buffer.prototype.writeUInt8 = function writeUInt8(value, offset, noAssert) {
1106
+ value = +value;
1107
+ offset = offset | 0;
1108
+ if (!noAssert) checkInt(this, value, offset, 1, 255, 0);
1109
+ if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
1110
+ this[offset] = value & 255;
1111
+ return offset + 1;
1112
+ };
1113
+ function objectWriteUInt16(buf, value, offset, littleEndian) {
1114
+ if (value < 0) value = 65535 + value + 1;
1115
+ for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) {
1116
+ buf[offset + i] = (value & 255 << 8 * (littleEndian ? i : 1 - i)) >>> (littleEndian ? i : 1 - i) * 8;
1117
+ }
1118
+ }
1119
+ Buffer.prototype.writeUInt16LE = function writeUInt16LE(value, offset, noAssert) {
1120
+ value = +value;
1121
+ offset = offset | 0;
1122
+ if (!noAssert) checkInt(this, value, offset, 2, 65535, 0);
1123
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1124
+ this[offset] = value & 255;
1125
+ this[offset + 1] = value >>> 8;
1126
+ } else {
1127
+ objectWriteUInt16(this, value, offset, true);
1128
+ }
1129
+ return offset + 2;
1130
+ };
1131
+ Buffer.prototype.writeUInt16BE = function writeUInt16BE(value, offset, noAssert) {
1132
+ value = +value;
1133
+ offset = offset | 0;
1134
+ if (!noAssert) checkInt(this, value, offset, 2, 65535, 0);
1135
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1136
+ this[offset] = value >>> 8;
1137
+ this[offset + 1] = value & 255;
1138
+ } else {
1139
+ objectWriteUInt16(this, value, offset, false);
1140
+ }
1141
+ return offset + 2;
1142
+ };
1143
+ function objectWriteUInt32(buf, value, offset, littleEndian) {
1144
+ if (value < 0) value = 4294967295 + value + 1;
1145
+ for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) {
1146
+ buf[offset + i] = value >>> (littleEndian ? i : 3 - i) * 8 & 255;
1147
+ }
1148
+ }
1149
+ Buffer.prototype.writeUInt32LE = function writeUInt32LE(value, offset, noAssert) {
1150
+ value = +value;
1151
+ offset = offset | 0;
1152
+ if (!noAssert) checkInt(this, value, offset, 4, 4294967295, 0);
1153
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1154
+ this[offset + 3] = value >>> 24;
1155
+ this[offset + 2] = value >>> 16;
1156
+ this[offset + 1] = value >>> 8;
1157
+ this[offset] = value & 255;
1158
+ } else {
1159
+ objectWriteUInt32(this, value, offset, true);
1160
+ }
1161
+ return offset + 4;
1162
+ };
1163
+ Buffer.prototype.writeUInt32BE = function writeUInt32BE(value, offset, noAssert) {
1164
+ value = +value;
1165
+ offset = offset | 0;
1166
+ if (!noAssert) checkInt(this, value, offset, 4, 4294967295, 0);
1167
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1168
+ this[offset] = value >>> 24;
1169
+ this[offset + 1] = value >>> 16;
1170
+ this[offset + 2] = value >>> 8;
1171
+ this[offset + 3] = value & 255;
1172
+ } else {
1173
+ objectWriteUInt32(this, value, offset, false);
1174
+ }
1175
+ return offset + 4;
1176
+ };
1177
+ Buffer.prototype.writeIntLE = function writeIntLE(value, offset, byteLength2, noAssert) {
1178
+ value = +value;
1179
+ offset = offset | 0;
1180
+ if (!noAssert) {
1181
+ var limit = Math.pow(2, 8 * byteLength2 - 1);
1182
+ checkInt(this, value, offset, byteLength2, limit - 1, -limit);
1183
+ }
1184
+ var i = 0;
1185
+ var mul = 1;
1186
+ var sub = 0;
1187
+ this[offset] = value & 255;
1188
+ while (++i < byteLength2 && (mul *= 256)) {
1189
+ if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
1190
+ sub = 1;
1191
+ }
1192
+ this[offset + i] = (value / mul >> 0) - sub & 255;
1193
+ }
1194
+ return offset + byteLength2;
1195
+ };
1196
+ Buffer.prototype.writeIntBE = function writeIntBE(value, offset, byteLength2, noAssert) {
1197
+ value = +value;
1198
+ offset = offset | 0;
1199
+ if (!noAssert) {
1200
+ var limit = Math.pow(2, 8 * byteLength2 - 1);
1201
+ checkInt(this, value, offset, byteLength2, limit - 1, -limit);
1202
+ }
1203
+ var i = byteLength2 - 1;
1204
+ var mul = 1;
1205
+ var sub = 0;
1206
+ this[offset + i] = value & 255;
1207
+ while (--i >= 0 && (mul *= 256)) {
1208
+ if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
1209
+ sub = 1;
1210
+ }
1211
+ this[offset + i] = (value / mul >> 0) - sub & 255;
1212
+ }
1213
+ return offset + byteLength2;
1214
+ };
1215
+ Buffer.prototype.writeInt8 = function writeInt8(value, offset, noAssert) {
1216
+ value = +value;
1217
+ offset = offset | 0;
1218
+ if (!noAssert) checkInt(this, value, offset, 1, 127, -128);
1219
+ if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
1220
+ if (value < 0) value = 255 + value + 1;
1221
+ this[offset] = value & 255;
1222
+ return offset + 1;
1223
+ };
1224
+ Buffer.prototype.writeInt16LE = function writeInt16LE(value, offset, noAssert) {
1225
+ value = +value;
1226
+ offset = offset | 0;
1227
+ if (!noAssert) checkInt(this, value, offset, 2, 32767, -32768);
1228
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1229
+ this[offset] = value & 255;
1230
+ this[offset + 1] = value >>> 8;
1231
+ } else {
1232
+ objectWriteUInt16(this, value, offset, true);
1233
+ }
1234
+ return offset + 2;
1235
+ };
1236
+ Buffer.prototype.writeInt16BE = function writeInt16BE(value, offset, noAssert) {
1237
+ value = +value;
1238
+ offset = offset | 0;
1239
+ if (!noAssert) checkInt(this, value, offset, 2, 32767, -32768);
1240
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1241
+ this[offset] = value >>> 8;
1242
+ this[offset + 1] = value & 255;
1243
+ } else {
1244
+ objectWriteUInt16(this, value, offset, false);
1245
+ }
1246
+ return offset + 2;
1247
+ };
1248
+ Buffer.prototype.writeInt32LE = function writeInt32LE(value, offset, noAssert) {
1249
+ value = +value;
1250
+ offset = offset | 0;
1251
+ if (!noAssert) checkInt(this, value, offset, 4, 2147483647, -2147483648);
1252
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1253
+ this[offset] = value & 255;
1254
+ this[offset + 1] = value >>> 8;
1255
+ this[offset + 2] = value >>> 16;
1256
+ this[offset + 3] = value >>> 24;
1257
+ } else {
1258
+ objectWriteUInt32(this, value, offset, true);
1259
+ }
1260
+ return offset + 4;
1261
+ };
1262
+ Buffer.prototype.writeInt32BE = function writeInt32BE(value, offset, noAssert) {
1263
+ value = +value;
1264
+ offset = offset | 0;
1265
+ if (!noAssert) checkInt(this, value, offset, 4, 2147483647, -2147483648);
1266
+ if (value < 0) value = 4294967295 + value + 1;
1267
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
1268
+ this[offset] = value >>> 24;
1269
+ this[offset + 1] = value >>> 16;
1270
+ this[offset + 2] = value >>> 8;
1271
+ this[offset + 3] = value & 255;
1272
+ } else {
1273
+ objectWriteUInt32(this, value, offset, false);
1274
+ }
1275
+ return offset + 4;
1276
+ };
1277
+ function checkIEEE754(buf, value, offset, ext, max, min) {
1278
+ if (offset + ext > buf.length) throw new RangeError("Index out of range");
1279
+ if (offset < 0) throw new RangeError("Index out of range");
1280
+ }
1281
+ function writeFloat(buf, value, offset, littleEndian, noAssert) {
1282
+ if (!noAssert) {
1283
+ checkIEEE754(buf, value, offset, 4);
1284
+ }
1285
+ write(buf, value, offset, littleEndian, 23, 4);
1286
+ return offset + 4;
1287
+ }
1288
+ Buffer.prototype.writeFloatLE = function writeFloatLE(value, offset, noAssert) {
1289
+ return writeFloat(this, value, offset, true, noAssert);
1290
+ };
1291
+ Buffer.prototype.writeFloatBE = function writeFloatBE(value, offset, noAssert) {
1292
+ return writeFloat(this, value, offset, false, noAssert);
1293
+ };
1294
+ function writeDouble(buf, value, offset, littleEndian, noAssert) {
1295
+ if (!noAssert) {
1296
+ checkIEEE754(buf, value, offset, 8);
1297
+ }
1298
+ write(buf, value, offset, littleEndian, 52, 8);
1299
+ return offset + 8;
1300
+ }
1301
+ Buffer.prototype.writeDoubleLE = function writeDoubleLE(value, offset, noAssert) {
1302
+ return writeDouble(this, value, offset, true, noAssert);
1303
+ };
1304
+ Buffer.prototype.writeDoubleBE = function writeDoubleBE(value, offset, noAssert) {
1305
+ return writeDouble(this, value, offset, false, noAssert);
1306
+ };
1307
+ Buffer.prototype.copy = function copy(target, targetStart, start, end) {
1308
+ if (!start) start = 0;
1309
+ if (!end && end !== 0) end = this.length;
1310
+ if (targetStart >= target.length) targetStart = target.length;
1311
+ if (!targetStart) targetStart = 0;
1312
+ if (end > 0 && end < start) end = start;
1313
+ if (end === start) return 0;
1314
+ if (target.length === 0 || this.length === 0) return 0;
1315
+ if (targetStart < 0) {
1316
+ throw new RangeError("targetStart out of bounds");
1317
+ }
1318
+ if (start < 0 || start >= this.length) throw new RangeError("sourceStart out of bounds");
1319
+ if (end < 0) throw new RangeError("sourceEnd out of bounds");
1320
+ if (end > this.length) end = this.length;
1321
+ if (target.length - targetStart < end - start) {
1322
+ end = target.length - targetStart + start;
1323
+ }
1324
+ var len = end - start;
1325
+ var i;
1326
+ if (this === target && start < targetStart && targetStart < end) {
1327
+ for (i = len - 1; i >= 0; --i) {
1328
+ target[i + targetStart] = this[i + start];
1329
+ }
1330
+ } else if (len < 1e3 || !Buffer.TYPED_ARRAY_SUPPORT) {
1331
+ for (i = 0; i < len; ++i) {
1332
+ target[i + targetStart] = this[i + start];
1333
+ }
1334
+ } else {
1335
+ Uint8Array.prototype.set.call(
1336
+ target,
1337
+ this.subarray(start, start + len),
1338
+ targetStart
1339
+ );
1340
+ }
1341
+ return len;
1342
+ };
1343
+ Buffer.prototype.fill = function fill(val, start, end, encoding) {
1344
+ if (typeof val === "string") {
1345
+ if (typeof start === "string") {
1346
+ encoding = start;
1347
+ start = 0;
1348
+ end = this.length;
1349
+ } else if (typeof end === "string") {
1350
+ encoding = end;
1351
+ end = this.length;
1352
+ }
1353
+ if (val.length === 1) {
1354
+ var code = val.charCodeAt(0);
1355
+ if (code < 256) {
1356
+ val = code;
1357
+ }
1358
+ }
1359
+ if (encoding !== void 0 && typeof encoding !== "string") {
1360
+ throw new TypeError("encoding must be a string");
1361
+ }
1362
+ if (typeof encoding === "string" && !Buffer.isEncoding(encoding)) {
1363
+ throw new TypeError("Unknown encoding: " + encoding);
1364
+ }
1365
+ } else if (typeof val === "number") {
1366
+ val = val & 255;
1367
+ }
1368
+ if (start < 0 || this.length < start || this.length < end) {
1369
+ throw new RangeError("Out of range index");
1370
+ }
1371
+ if (end <= start) {
1372
+ return this;
1373
+ }
1374
+ start = start >>> 0;
1375
+ end = end === void 0 ? this.length : end >>> 0;
1376
+ if (!val) val = 0;
1377
+ var i;
1378
+ if (typeof val === "number") {
1379
+ for (i = start; i < end; ++i) {
1380
+ this[i] = val;
1381
+ }
1382
+ } else {
1383
+ var bytes = internalIsBuffer(val) ? val : utf8ToBytes(new Buffer(val, encoding).toString());
1384
+ var len = bytes.length;
1385
+ for (i = 0; i < end - start; ++i) {
1386
+ this[i + start] = bytes[i % len];
1387
+ }
1388
+ }
1389
+ return this;
1390
+ };
1391
+ var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g;
1392
+ function base64clean(str) {
1393
+ str = stringtrim(str).replace(INVALID_BASE64_RE, "");
1394
+ if (str.length < 2) return "";
1395
+ while (str.length % 4 !== 0) {
1396
+ str = str + "=";
1397
+ }
1398
+ return str;
1399
+ }
1400
+ function stringtrim(str) {
1401
+ if (str.trim) return str.trim();
1402
+ return str.replace(/^\s+|\s+$/g, "");
1403
+ }
1404
+ function toHex(n) {
1405
+ if (n < 16) return "0" + n.toString(16);
1406
+ return n.toString(16);
1407
+ }
1408
+ function utf8ToBytes(string, units) {
1409
+ units = units || Infinity;
1410
+ var codePoint;
1411
+ var length = string.length;
1412
+ var leadSurrogate = null;
1413
+ var bytes = [];
1414
+ for (var i = 0; i < length; ++i) {
1415
+ codePoint = string.charCodeAt(i);
1416
+ if (codePoint > 55295 && codePoint < 57344) {
1417
+ if (!leadSurrogate) {
1418
+ if (codePoint > 56319) {
1419
+ if ((units -= 3) > -1) bytes.push(239, 191, 189);
1420
+ continue;
1421
+ } else if (i + 1 === length) {
1422
+ if ((units -= 3) > -1) bytes.push(239, 191, 189);
1423
+ continue;
1424
+ }
1425
+ leadSurrogate = codePoint;
1426
+ continue;
1427
+ }
1428
+ if (codePoint < 56320) {
1429
+ if ((units -= 3) > -1) bytes.push(239, 191, 189);
1430
+ leadSurrogate = codePoint;
1431
+ continue;
1432
+ }
1433
+ codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536;
1434
+ } else if (leadSurrogate) {
1435
+ if ((units -= 3) > -1) bytes.push(239, 191, 189);
1436
+ }
1437
+ leadSurrogate = null;
1438
+ if (codePoint < 128) {
1439
+ if ((units -= 1) < 0) break;
1440
+ bytes.push(codePoint);
1441
+ } else if (codePoint < 2048) {
1442
+ if ((units -= 2) < 0) break;
1443
+ bytes.push(
1444
+ codePoint >> 6 | 192,
1445
+ codePoint & 63 | 128
1446
+ );
1447
+ } else if (codePoint < 65536) {
1448
+ if ((units -= 3) < 0) break;
1449
+ bytes.push(
1450
+ codePoint >> 12 | 224,
1451
+ codePoint >> 6 & 63 | 128,
1452
+ codePoint & 63 | 128
1453
+ );
1454
+ } else if (codePoint < 1114112) {
1455
+ if ((units -= 4) < 0) break;
1456
+ bytes.push(
1457
+ codePoint >> 18 | 240,
1458
+ codePoint >> 12 & 63 | 128,
1459
+ codePoint >> 6 & 63 | 128,
1460
+ codePoint & 63 | 128
1461
+ );
1462
+ } else {
1463
+ throw new Error("Invalid code point");
1464
+ }
1465
+ }
1466
+ return bytes;
1467
+ }
1468
+ function asciiToBytes(str) {
1469
+ var byteArray = [];
1470
+ for (var i = 0; i < str.length; ++i) {
1471
+ byteArray.push(str.charCodeAt(i) & 255);
1472
+ }
1473
+ return byteArray;
1474
+ }
1475
+ function utf16leToBytes(str, units) {
1476
+ var c, hi, lo;
1477
+ var byteArray = [];
1478
+ for (var i = 0; i < str.length; ++i) {
1479
+ if ((units -= 2) < 0) break;
1480
+ c = str.charCodeAt(i);
1481
+ hi = c >> 8;
1482
+ lo = c % 256;
1483
+ byteArray.push(lo);
1484
+ byteArray.push(hi);
1485
+ }
1486
+ return byteArray;
1487
+ }
1488
+ function base64ToBytes(str) {
1489
+ return toByteArray(base64clean(str));
1490
+ }
1491
+ function blitBuffer(src, dst, offset, length) {
1492
+ for (var i = 0; i < length; ++i) {
1493
+ if (i + offset >= dst.length || i >= src.length) break;
1494
+ dst[i + offset] = src[i];
1495
+ }
1496
+ return i;
1497
+ }
1498
+ function isnan(val) {
1499
+ return val !== val;
1500
+ }
1501
+ function isBuffer(obj) {
1502
+ return obj != null && (!!obj._isBuffer || isFastBuffer(obj) || isSlowBuffer(obj));
1503
+ }
1504
+ function isFastBuffer(obj) {
1505
+ return !!obj.constructor && typeof obj.constructor.isBuffer === "function" && obj.constructor.isBuffer(obj);
1506
+ }
1507
+ function isSlowBuffer(obj) {
1508
+ return typeof obj.readFloatLE === "function" && typeof obj.slice === "function" && isFastBuffer(obj.slice(0, 0));
1509
+ }
1510
+ var CsvError = class _CsvError extends Error {
1511
+ constructor(code, message, options, ...contexts) {
1512
+ if (Array.isArray(message)) message = message.join(" ").trim();
1513
+ super(message);
1514
+ if (Error.captureStackTrace !== void 0) {
1515
+ Error.captureStackTrace(this, _CsvError);
1516
+ }
1517
+ this.code = code;
1518
+ for (const context of contexts) {
1519
+ for (const key in context) {
1520
+ const value = context[key];
1521
+ this[key] = isBuffer(value) ? value.toString(options.encoding) : value == null ? value : JSON.parse(JSON.stringify(value));
1522
+ }
1523
+ }
1524
+ }
1525
+ };
1526
+ var is_object = function(obj) {
1527
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
1528
+ };
1529
+ var normalize_columns_array = function(columns) {
1530
+ const normalizedColumns = [];
1531
+ for (let i = 0, l = columns.length; i < l; i++) {
1532
+ const column = columns[i];
1533
+ if (column === void 0 || column === null || column === false) {
1534
+ normalizedColumns[i] = { disabled: true };
1535
+ } else if (typeof column === "string") {
1536
+ normalizedColumns[i] = { name: column };
1537
+ } else if (is_object(column)) {
1538
+ if (typeof column.name !== "string") {
1539
+ throw new CsvError("CSV_OPTION_COLUMNS_MISSING_NAME", [
1540
+ "Option columns missing name:",
1541
+ `property "name" is required at position ${i}`,
1542
+ "when column is an object literal"
1543
+ ]);
1544
+ }
1545
+ normalizedColumns[i] = column;
1546
+ } else {
1547
+ throw new CsvError("CSV_INVALID_COLUMN_DEFINITION", [
1548
+ "Invalid column definition:",
1549
+ "expect a string or a literal object,",
1550
+ `got ${JSON.stringify(column)} at position ${i}`
1551
+ ]);
1552
+ }
1553
+ }
1554
+ return normalizedColumns;
1555
+ };
1556
+ var ResizeableBuffer = class {
1557
+ constructor(size = 100) {
1558
+ this.size = size;
1559
+ this.length = 0;
1560
+ this.buf = Buffer.allocUnsafe(size);
1561
+ }
1562
+ prepend(val) {
1563
+ if (isBuffer(val)) {
1564
+ const length = this.length + val.length;
1565
+ if (length >= this.size) {
1566
+ this.resize();
1567
+ if (length >= this.size) {
1568
+ throw Error("INVALID_BUFFER_STATE");
1569
+ }
1570
+ }
1571
+ const buf = this.buf;
1572
+ this.buf = Buffer.allocUnsafe(this.size);
1573
+ val.copy(this.buf, 0);
1574
+ buf.copy(this.buf, val.length);
1575
+ this.length += val.length;
1576
+ } else {
1577
+ const length = this.length++;
1578
+ if (length === this.size) {
1579
+ this.resize();
1580
+ }
1581
+ const buf = this.clone();
1582
+ this.buf[0] = val;
1583
+ buf.copy(this.buf, 1, 0, length);
1584
+ }
1585
+ }
1586
+ append(val) {
1587
+ const length = this.length++;
1588
+ if (length === this.size) {
1589
+ this.resize();
1590
+ }
1591
+ this.buf[length] = val;
1592
+ }
1593
+ clone() {
1594
+ return Buffer.from(this.buf.slice(0, this.length));
1595
+ }
1596
+ resize() {
1597
+ const length = this.length;
1598
+ this.size = this.size * 2;
1599
+ const buf = Buffer.allocUnsafe(this.size);
1600
+ this.buf.copy(buf, 0, 0, length);
1601
+ this.buf = buf;
1602
+ }
1603
+ toString(encoding) {
1604
+ if (encoding) {
1605
+ return this.buf.slice(0, this.length).toString(encoding);
1606
+ } else {
1607
+ return Uint8Array.prototype.slice.call(this.buf.slice(0, this.length));
1608
+ }
1609
+ }
1610
+ toJSON() {
1611
+ return this.toString("utf8");
1612
+ }
1613
+ reset() {
1614
+ this.length = 0;
1615
+ }
1616
+ };
1617
+ var np = 12;
1618
+ var cr$1 = 13;
1619
+ var nl$1 = 10;
1620
+ var space = 32;
1621
+ var tab = 9;
1622
+ var init_state = function(options) {
1623
+ return {
1624
+ bomSkipped: false,
1625
+ bufBytesStart: 0,
1626
+ castField: options.cast_function,
1627
+ commenting: false,
1628
+ // Current error encountered by a record
1629
+ error: void 0,
1630
+ enabled: options.from_line === 1,
1631
+ escaping: false,
1632
+ escapeIsQuote: isBuffer(options.escape) && isBuffer(options.quote) && Buffer.compare(options.escape, options.quote) === 0,
1633
+ // columns can be `false`, `true`, `Array`
1634
+ expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : void 0,
1635
+ field: new ResizeableBuffer(20),
1636
+ firstLineToHeaders: options.cast_first_line_to_header,
1637
+ needMoreDataSize: Math.max(
1638
+ // Skip if the remaining buffer smaller than comment
1639
+ options.comment !== null ? options.comment.length : 0,
1640
+ ...options.delimiter.map((delimiter) => delimiter.length),
1641
+ // Skip if the remaining buffer can be escape sequence
1642
+ options.quote !== null ? options.quote.length : 0
1643
+ ),
1644
+ previousBuf: void 0,
1645
+ quoting: false,
1646
+ stop: false,
1647
+ rawBuffer: new ResizeableBuffer(100),
1648
+ record: [],
1649
+ recordHasError: false,
1650
+ record_length: 0,
1651
+ recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 0 : Math.max(...options.record_delimiter.map((v) => v.length)),
1652
+ trimChars: [
1653
+ Buffer.from(" ", options.encoding)[0],
1654
+ Buffer.from(" ", options.encoding)[0]
1655
+ ],
1656
+ wasQuoting: false,
1657
+ wasRowDelimiter: false,
1658
+ timchars: [
1659
+ Buffer.from(Buffer.from([cr$1], "utf8").toString(), options.encoding),
1660
+ Buffer.from(Buffer.from([nl$1], "utf8").toString(), options.encoding),
1661
+ Buffer.from(Buffer.from([np], "utf8").toString(), options.encoding),
1662
+ Buffer.from(Buffer.from([space], "utf8").toString(), options.encoding),
1663
+ Buffer.from(Buffer.from([tab], "utf8").toString(), options.encoding)
1664
+ ]
1665
+ };
1666
+ };
1667
+ var underscore = function(str) {
1668
+ return str.replace(/([A-Z])/g, function(_, match) {
1669
+ return "_" + match.toLowerCase();
1670
+ });
1671
+ };
1672
+ var normalize_options = function(opts) {
1673
+ const options = {};
1674
+ for (const opt in opts) {
1675
+ options[underscore(opt)] = opts[opt];
1676
+ }
1677
+ if (options.encoding === void 0 || options.encoding === true) {
1678
+ options.encoding = "utf8";
1679
+ } else if (options.encoding === null || options.encoding === false) {
1680
+ options.encoding = null;
1681
+ } else if (typeof options.encoding !== "string" && options.encoding !== null) {
1682
+ throw new CsvError(
1683
+ "CSV_INVALID_OPTION_ENCODING",
1684
+ [
1685
+ "Invalid option encoding:",
1686
+ "encoding must be a string or null to return a buffer,",
1687
+ `got ${JSON.stringify(options.encoding)}`
1688
+ ],
1689
+ options
1690
+ );
1691
+ }
1692
+ if (options.bom === void 0 || options.bom === null || options.bom === false) {
1693
+ options.bom = false;
1694
+ } else if (options.bom !== true) {
1695
+ throw new CsvError(
1696
+ "CSV_INVALID_OPTION_BOM",
1697
+ [
1698
+ "Invalid option bom:",
1699
+ "bom must be true,",
1700
+ `got ${JSON.stringify(options.bom)}`
1701
+ ],
1702
+ options
1703
+ );
1704
+ }
1705
+ options.cast_function = null;
1706
+ if (options.cast === void 0 || options.cast === null || options.cast === false || options.cast === "") {
1707
+ options.cast = void 0;
1708
+ } else if (typeof options.cast === "function") {
1709
+ options.cast_function = options.cast;
1710
+ options.cast = true;
1711
+ } else if (options.cast !== true) {
1712
+ throw new CsvError(
1713
+ "CSV_INVALID_OPTION_CAST",
1714
+ [
1715
+ "Invalid option cast:",
1716
+ "cast must be true or a function,",
1717
+ `got ${JSON.stringify(options.cast)}`
1718
+ ],
1719
+ options
1720
+ );
1721
+ }
1722
+ if (options.cast_date === void 0 || options.cast_date === null || options.cast_date === false || options.cast_date === "") {
1723
+ options.cast_date = false;
1724
+ } else if (options.cast_date === true) {
1725
+ options.cast_date = function(value) {
1726
+ const date = Date.parse(value);
1727
+ return !isNaN(date) ? new Date(date) : value;
1728
+ };
1729
+ } else if (typeof options.cast_date !== "function") {
1730
+ throw new CsvError(
1731
+ "CSV_INVALID_OPTION_CAST_DATE",
1732
+ [
1733
+ "Invalid option cast_date:",
1734
+ "cast_date must be true or a function,",
1735
+ `got ${JSON.stringify(options.cast_date)}`
1736
+ ],
1737
+ options
1738
+ );
1739
+ }
1740
+ options.cast_first_line_to_header = null;
1741
+ if (options.columns === true) {
1742
+ options.cast_first_line_to_header = void 0;
1743
+ } else if (typeof options.columns === "function") {
1744
+ options.cast_first_line_to_header = options.columns;
1745
+ options.columns = true;
1746
+ } else if (Array.isArray(options.columns)) {
1747
+ options.columns = normalize_columns_array(options.columns);
1748
+ } else if (options.columns === void 0 || options.columns === null || options.columns === false) {
1749
+ options.columns = false;
1750
+ } else {
1751
+ throw new CsvError(
1752
+ "CSV_INVALID_OPTION_COLUMNS",
1753
+ [
1754
+ "Invalid option columns:",
1755
+ "expect an array, a function or true,",
1756
+ `got ${JSON.stringify(options.columns)}`
1757
+ ],
1758
+ options
1759
+ );
1760
+ }
1761
+ if (options.group_columns_by_name === void 0 || options.group_columns_by_name === null || options.group_columns_by_name === false) {
1762
+ options.group_columns_by_name = false;
1763
+ } else if (options.group_columns_by_name !== true) {
1764
+ throw new CsvError(
1765
+ "CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME",
1766
+ [
1767
+ "Invalid option group_columns_by_name:",
1768
+ "expect an boolean,",
1769
+ `got ${JSON.stringify(options.group_columns_by_name)}`
1770
+ ],
1771
+ options
1772
+ );
1773
+ } else if (options.columns === false) {
1774
+ throw new CsvError(
1775
+ "CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME",
1776
+ [
1777
+ "Invalid option group_columns_by_name:",
1778
+ "the `columns` mode must be activated."
1779
+ ],
1780
+ options
1781
+ );
1782
+ }
1783
+ if (options.comment === void 0 || options.comment === null || options.comment === false || options.comment === "") {
1784
+ options.comment = null;
1785
+ } else {
1786
+ if (typeof options.comment === "string") {
1787
+ options.comment = Buffer.from(options.comment, options.encoding);
1788
+ }
1789
+ if (!isBuffer(options.comment)) {
1790
+ throw new CsvError(
1791
+ "CSV_INVALID_OPTION_COMMENT",
1792
+ [
1793
+ "Invalid option comment:",
1794
+ "comment must be a buffer or a string,",
1795
+ `got ${JSON.stringify(options.comment)}`
1796
+ ],
1797
+ options
1798
+ );
1799
+ }
1800
+ }
1801
+ if (options.comment_no_infix === void 0 || options.comment_no_infix === null || options.comment_no_infix === false) {
1802
+ options.comment_no_infix = false;
1803
+ } else if (options.comment_no_infix !== true) {
1804
+ throw new CsvError(
1805
+ "CSV_INVALID_OPTION_COMMENT",
1806
+ [
1807
+ "Invalid option comment_no_infix:",
1808
+ "value must be a boolean,",
1809
+ `got ${JSON.stringify(options.comment_no_infix)}`
1810
+ ],
1811
+ options
1812
+ );
1813
+ }
1814
+ const delimiter_json = JSON.stringify(options.delimiter);
1815
+ if (!Array.isArray(options.delimiter))
1816
+ options.delimiter = [options.delimiter];
1817
+ if (options.delimiter.length === 0) {
1818
+ throw new CsvError(
1819
+ "CSV_INVALID_OPTION_DELIMITER",
1820
+ [
1821
+ "Invalid option delimiter:",
1822
+ "delimiter must be a non empty string or buffer or array of string|buffer,",
1823
+ `got ${delimiter_json}`
1824
+ ],
1825
+ options
1826
+ );
1827
+ }
1828
+ options.delimiter = options.delimiter.map(function(delimiter) {
1829
+ if (delimiter === void 0 || delimiter === null || delimiter === false) {
1830
+ return Buffer.from(",", options.encoding);
1831
+ }
1832
+ if (typeof delimiter === "string") {
1833
+ delimiter = Buffer.from(delimiter, options.encoding);
1834
+ }
1835
+ if (!isBuffer(delimiter) || delimiter.length === 0) {
1836
+ throw new CsvError(
1837
+ "CSV_INVALID_OPTION_DELIMITER",
1838
+ [
1839
+ "Invalid option delimiter:",
1840
+ "delimiter must be a non empty string or buffer or array of string|buffer,",
1841
+ `got ${delimiter_json}`
1842
+ ],
1843
+ options
1844
+ );
1845
+ }
1846
+ return delimiter;
1847
+ });
1848
+ if (options.escape === void 0 || options.escape === true) {
1849
+ options.escape = Buffer.from('"', options.encoding);
1850
+ } else if (typeof options.escape === "string") {
1851
+ options.escape = Buffer.from(options.escape, options.encoding);
1852
+ } else if (options.escape === null || options.escape === false) {
1853
+ options.escape = null;
1854
+ }
1855
+ if (options.escape !== null) {
1856
+ if (!isBuffer(options.escape)) {
1857
+ throw new Error(
1858
+ `Invalid Option: escape must be a buffer, a string or a boolean, got ${JSON.stringify(options.escape)}`
1859
+ );
1860
+ }
1861
+ }
1862
+ if (options.from === void 0 || options.from === null) {
1863
+ options.from = 1;
1864
+ } else {
1865
+ if (typeof options.from === "string" && /\d+/.test(options.from)) {
1866
+ options.from = parseInt(options.from);
1867
+ }
1868
+ if (Number.isInteger(options.from)) {
1869
+ if (options.from < 0) {
1870
+ throw new Error(
1871
+ `Invalid Option: from must be a positive integer, got ${JSON.stringify(opts.from)}`
1872
+ );
1873
+ }
1874
+ } else {
1875
+ throw new Error(
1876
+ `Invalid Option: from must be an integer, got ${JSON.stringify(options.from)}`
1877
+ );
1878
+ }
1879
+ }
1880
+ if (options.from_line === void 0 || options.from_line === null) {
1881
+ options.from_line = 1;
1882
+ } else {
1883
+ if (typeof options.from_line === "string" && /\d+/.test(options.from_line)) {
1884
+ options.from_line = parseInt(options.from_line);
1885
+ }
1886
+ if (Number.isInteger(options.from_line)) {
1887
+ if (options.from_line <= 0) {
1888
+ throw new Error(
1889
+ `Invalid Option: from_line must be a positive integer greater than 0, got ${JSON.stringify(opts.from_line)}`
1890
+ );
1891
+ }
1892
+ } else {
1893
+ throw new Error(
1894
+ `Invalid Option: from_line must be an integer, got ${JSON.stringify(opts.from_line)}`
1895
+ );
1896
+ }
1897
+ }
1898
+ if (options.ignore_last_delimiters === void 0 || options.ignore_last_delimiters === null) {
1899
+ options.ignore_last_delimiters = false;
1900
+ } else if (typeof options.ignore_last_delimiters === "number") {
1901
+ options.ignore_last_delimiters = Math.floor(options.ignore_last_delimiters);
1902
+ if (options.ignore_last_delimiters === 0) {
1903
+ options.ignore_last_delimiters = false;
1904
+ }
1905
+ } else if (typeof options.ignore_last_delimiters !== "boolean") {
1906
+ throw new CsvError(
1907
+ "CSV_INVALID_OPTION_IGNORE_LAST_DELIMITERS",
1908
+ [
1909
+ "Invalid option `ignore_last_delimiters`:",
1910
+ "the value must be a boolean value or an integer,",
1911
+ `got ${JSON.stringify(options.ignore_last_delimiters)}`
1912
+ ],
1913
+ options
1914
+ );
1915
+ }
1916
+ if (options.ignore_last_delimiters === true && options.columns === false) {
1917
+ throw new CsvError(
1918
+ "CSV_IGNORE_LAST_DELIMITERS_REQUIRES_COLUMNS",
1919
+ [
1920
+ "The option `ignore_last_delimiters`",
1921
+ "requires the activation of the `columns` option"
1922
+ ],
1923
+ options
1924
+ );
1925
+ }
1926
+ if (options.info === void 0 || options.info === null || options.info === false) {
1927
+ options.info = false;
1928
+ } else if (options.info !== true) {
1929
+ throw new Error(
1930
+ `Invalid Option: info must be true, got ${JSON.stringify(options.info)}`
1931
+ );
1932
+ }
1933
+ if (options.max_record_size === void 0 || options.max_record_size === null || options.max_record_size === false) {
1934
+ options.max_record_size = 0;
1935
+ } else if (Number.isInteger(options.max_record_size) && options.max_record_size >= 0) ;
1936
+ else if (typeof options.max_record_size === "string" && /\d+/.test(options.max_record_size)) {
1937
+ options.max_record_size = parseInt(options.max_record_size);
1938
+ } else {
1939
+ throw new Error(
1940
+ `Invalid Option: max_record_size must be a positive integer, got ${JSON.stringify(options.max_record_size)}`
1941
+ );
1942
+ }
1943
+ if (options.objname === void 0 || options.objname === null || options.objname === false) {
1944
+ options.objname = void 0;
1945
+ } else if (isBuffer(options.objname)) {
1946
+ if (options.objname.length === 0) {
1947
+ throw new Error(`Invalid Option: objname must be a non empty buffer`);
1948
+ }
1949
+ if (options.encoding === null) ;
1950
+ else {
1951
+ options.objname = options.objname.toString(options.encoding);
1952
+ }
1953
+ } else if (typeof options.objname === "string") {
1954
+ if (options.objname.length === 0) {
1955
+ throw new Error(`Invalid Option: objname must be a non empty string`);
1956
+ }
1957
+ } else if (typeof options.objname === "number") ;
1958
+ else {
1959
+ throw new Error(
1960
+ `Invalid Option: objname must be a string or a buffer, got ${options.objname}`
1961
+ );
1962
+ }
1963
+ if (options.objname !== void 0) {
1964
+ if (typeof options.objname === "number") {
1965
+ if (options.columns !== false) {
1966
+ throw Error(
1967
+ "Invalid Option: objname index cannot be combined with columns or be defined as a field"
1968
+ );
1969
+ }
1970
+ } else {
1971
+ if (options.columns === false) {
1972
+ throw Error(
1973
+ "Invalid Option: objname field must be combined with columns or be defined as an index"
1974
+ );
1975
+ }
1976
+ }
1977
+ }
1978
+ if (options.on_record === void 0 || options.on_record === null) {
1979
+ options.on_record = void 0;
1980
+ } else if (typeof options.on_record !== "function") {
1981
+ throw new CsvError(
1982
+ "CSV_INVALID_OPTION_ON_RECORD",
1983
+ [
1984
+ "Invalid option `on_record`:",
1985
+ "expect a function,",
1986
+ `got ${JSON.stringify(options.on_record)}`
1987
+ ],
1988
+ options
1989
+ );
1990
+ }
1991
+ if (options.on_skip !== void 0 && options.on_skip !== null && typeof options.on_skip !== "function") {
1992
+ throw new Error(
1993
+ `Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`
1994
+ );
1995
+ }
1996
+ if (options.quote === null || options.quote === false || options.quote === "") {
1997
+ options.quote = null;
1998
+ } else {
1999
+ if (options.quote === void 0 || options.quote === true) {
2000
+ options.quote = Buffer.from('"', options.encoding);
2001
+ } else if (typeof options.quote === "string") {
2002
+ options.quote = Buffer.from(options.quote, options.encoding);
2003
+ }
2004
+ if (!isBuffer(options.quote)) {
2005
+ throw new Error(
2006
+ `Invalid Option: quote must be a buffer or a string, got ${JSON.stringify(options.quote)}`
2007
+ );
2008
+ }
2009
+ }
2010
+ if (options.raw === void 0 || options.raw === null || options.raw === false) {
2011
+ options.raw = false;
2012
+ } else if (options.raw !== true) {
2013
+ throw new Error(
2014
+ `Invalid Option: raw must be true, got ${JSON.stringify(options.raw)}`
2015
+ );
2016
+ }
2017
+ if (options.record_delimiter === void 0) {
2018
+ options.record_delimiter = [];
2019
+ } else if (typeof options.record_delimiter === "string" || isBuffer(options.record_delimiter)) {
2020
+ if (options.record_delimiter.length === 0) {
2021
+ throw new CsvError(
2022
+ "CSV_INVALID_OPTION_RECORD_DELIMITER",
2023
+ [
2024
+ "Invalid option `record_delimiter`:",
2025
+ "value must be a non empty string or buffer,",
2026
+ `got ${JSON.stringify(options.record_delimiter)}`
2027
+ ],
2028
+ options
2029
+ );
2030
+ }
2031
+ options.record_delimiter = [options.record_delimiter];
2032
+ } else if (!Array.isArray(options.record_delimiter)) {
2033
+ throw new CsvError(
2034
+ "CSV_INVALID_OPTION_RECORD_DELIMITER",
2035
+ [
2036
+ "Invalid option `record_delimiter`:",
2037
+ "value must be a string, a buffer or array of string|buffer,",
2038
+ `got ${JSON.stringify(options.record_delimiter)}`
2039
+ ],
2040
+ options
2041
+ );
2042
+ }
2043
+ options.record_delimiter = options.record_delimiter.map(function(rd, i) {
2044
+ if (typeof rd !== "string" && !isBuffer(rd)) {
2045
+ throw new CsvError(
2046
+ "CSV_INVALID_OPTION_RECORD_DELIMITER",
2047
+ [
2048
+ "Invalid option `record_delimiter`:",
2049
+ "value must be a string, a buffer or array of string|buffer",
2050
+ `at index ${i},`,
2051
+ `got ${JSON.stringify(rd)}`
2052
+ ],
2053
+ options
2054
+ );
2055
+ } else if (rd.length === 0) {
2056
+ throw new CsvError(
2057
+ "CSV_INVALID_OPTION_RECORD_DELIMITER",
2058
+ [
2059
+ "Invalid option `record_delimiter`:",
2060
+ "value must be a non empty string or buffer",
2061
+ `at index ${i},`,
2062
+ `got ${JSON.stringify(rd)}`
2063
+ ],
2064
+ options
2065
+ );
2066
+ }
2067
+ if (typeof rd === "string") {
2068
+ rd = Buffer.from(rd, options.encoding);
2069
+ }
2070
+ return rd;
2071
+ });
2072
+ if (typeof options.relax_column_count === "boolean") ;
2073
+ else if (options.relax_column_count === void 0 || options.relax_column_count === null) {
2074
+ options.relax_column_count = false;
2075
+ } else {
2076
+ throw new Error(
2077
+ `Invalid Option: relax_column_count must be a boolean, got ${JSON.stringify(options.relax_column_count)}`
2078
+ );
2079
+ }
2080
+ if (typeof options.relax_column_count_less === "boolean") ;
2081
+ else if (options.relax_column_count_less === void 0 || options.relax_column_count_less === null) {
2082
+ options.relax_column_count_less = false;
2083
+ } else {
2084
+ throw new Error(
2085
+ `Invalid Option: relax_column_count_less must be a boolean, got ${JSON.stringify(options.relax_column_count_less)}`
2086
+ );
2087
+ }
2088
+ if (typeof options.relax_column_count_more === "boolean") ;
2089
+ else if (options.relax_column_count_more === void 0 || options.relax_column_count_more === null) {
2090
+ options.relax_column_count_more = false;
2091
+ } else {
2092
+ throw new Error(
2093
+ `Invalid Option: relax_column_count_more must be a boolean, got ${JSON.stringify(options.relax_column_count_more)}`
2094
+ );
2095
+ }
2096
+ if (typeof options.relax_quotes === "boolean") ;
2097
+ else if (options.relax_quotes === void 0 || options.relax_quotes === null) {
2098
+ options.relax_quotes = false;
2099
+ } else {
2100
+ throw new Error(
2101
+ `Invalid Option: relax_quotes must be a boolean, got ${JSON.stringify(options.relax_quotes)}`
2102
+ );
2103
+ }
2104
+ if (typeof options.skip_empty_lines === "boolean") ;
2105
+ else if (options.skip_empty_lines === void 0 || options.skip_empty_lines === null) {
2106
+ options.skip_empty_lines = false;
2107
+ } else {
2108
+ throw new Error(
2109
+ `Invalid Option: skip_empty_lines must be a boolean, got ${JSON.stringify(options.skip_empty_lines)}`
2110
+ );
2111
+ }
2112
+ if (typeof options.skip_records_with_empty_values === "boolean") ;
2113
+ else if (options.skip_records_with_empty_values === void 0 || options.skip_records_with_empty_values === null) {
2114
+ options.skip_records_with_empty_values = false;
2115
+ } else {
2116
+ throw new Error(
2117
+ `Invalid Option: skip_records_with_empty_values must be a boolean, got ${JSON.stringify(options.skip_records_with_empty_values)}`
2118
+ );
2119
+ }
2120
+ if (typeof options.skip_records_with_error === "boolean") ;
2121
+ else if (options.skip_records_with_error === void 0 || options.skip_records_with_error === null) {
2122
+ options.skip_records_with_error = false;
2123
+ } else {
2124
+ throw new Error(
2125
+ `Invalid Option: skip_records_with_error must be a boolean, got ${JSON.stringify(options.skip_records_with_error)}`
2126
+ );
2127
+ }
2128
+ if (options.rtrim === void 0 || options.rtrim === null || options.rtrim === false) {
2129
+ options.rtrim = false;
2130
+ } else if (options.rtrim !== true) {
2131
+ throw new Error(
2132
+ `Invalid Option: rtrim must be a boolean, got ${JSON.stringify(options.rtrim)}`
2133
+ );
2134
+ }
2135
+ if (options.ltrim === void 0 || options.ltrim === null || options.ltrim === false) {
2136
+ options.ltrim = false;
2137
+ } else if (options.ltrim !== true) {
2138
+ throw new Error(
2139
+ `Invalid Option: ltrim must be a boolean, got ${JSON.stringify(options.ltrim)}`
2140
+ );
2141
+ }
2142
+ if (options.trim === void 0 || options.trim === null || options.trim === false) {
2143
+ options.trim = false;
2144
+ } else if (options.trim !== true) {
2145
+ throw new Error(
2146
+ `Invalid Option: trim must be a boolean, got ${JSON.stringify(options.trim)}`
2147
+ );
2148
+ }
2149
+ if (options.trim === true && opts.ltrim !== false) {
2150
+ options.ltrim = true;
2151
+ } else if (options.ltrim !== true) {
2152
+ options.ltrim = false;
2153
+ }
2154
+ if (options.trim === true && opts.rtrim !== false) {
2155
+ options.rtrim = true;
2156
+ } else if (options.rtrim !== true) {
2157
+ options.rtrim = false;
2158
+ }
2159
+ if (options.to === void 0 || options.to === null) {
2160
+ options.to = -1;
2161
+ } else {
2162
+ if (typeof options.to === "string" && /\d+/.test(options.to)) {
2163
+ options.to = parseInt(options.to);
2164
+ }
2165
+ if (Number.isInteger(options.to)) {
2166
+ if (options.to <= 0) {
2167
+ throw new Error(
2168
+ `Invalid Option: to must be a positive integer greater than 0, got ${JSON.stringify(opts.to)}`
2169
+ );
2170
+ }
2171
+ } else {
2172
+ throw new Error(
2173
+ `Invalid Option: to must be an integer, got ${JSON.stringify(opts.to)}`
2174
+ );
2175
+ }
2176
+ }
2177
+ if (options.to_line === void 0 || options.to_line === null) {
2178
+ options.to_line = -1;
2179
+ } else {
2180
+ if (typeof options.to_line === "string" && /\d+/.test(options.to_line)) {
2181
+ options.to_line = parseInt(options.to_line);
2182
+ }
2183
+ if (Number.isInteger(options.to_line)) {
2184
+ if (options.to_line <= 0) {
2185
+ throw new Error(
2186
+ `Invalid Option: to_line must be a positive integer greater than 0, got ${JSON.stringify(opts.to_line)}`
2187
+ );
2188
+ }
2189
+ } else {
2190
+ throw new Error(
2191
+ `Invalid Option: to_line must be an integer, got ${JSON.stringify(opts.to_line)}`
2192
+ );
2193
+ }
2194
+ }
2195
+ return options;
2196
+ };
2197
+ var isRecordEmpty = function(record) {
2198
+ return record.every(
2199
+ (field) => field == null || field.toString && field.toString().trim() === ""
2200
+ );
2201
+ };
2202
+ var cr = 13;
2203
+ var nl = 10;
2204
+ var boms = {
2205
+ // Note, the following are equals:
2206
+ // Buffer.from("\ufeff")
2207
+ // Buffer.from([239, 187, 191])
2208
+ // Buffer.from('EFBBBF', 'hex')
2209
+ utf8: Buffer.from([239, 187, 191]),
2210
+ // Note, the following are equals:
2211
+ // Buffer.from "\ufeff", 'utf16le
2212
+ // Buffer.from([255, 254])
2213
+ utf16le: Buffer.from([255, 254])
2214
+ };
2215
+ var transform = function(original_options = {}) {
2216
+ const info = {
2217
+ bytes: 0,
2218
+ comment_lines: 0,
2219
+ empty_lines: 0,
2220
+ invalid_field_length: 0,
2221
+ lines: 1,
2222
+ records: 0
2223
+ };
2224
+ const options = normalize_options(original_options);
2225
+ return {
2226
+ info,
2227
+ original_options,
2228
+ options,
2229
+ state: init_state(options),
2230
+ __needMoreData: function(i, bufLen, end) {
2231
+ if (end) return false;
2232
+ const { encoding, escape, quote } = this.options;
2233
+ const { quoting, needMoreDataSize, recordDelimiterMaxLength } = this.state;
2234
+ const numOfCharLeft = bufLen - i - 1;
2235
+ const requiredLength = Math.max(
2236
+ needMoreDataSize,
2237
+ // Skip if the remaining buffer smaller than record delimiter
2238
+ // If "record_delimiter" is yet to be discovered:
2239
+ // 1. It is equals to `[]` and "recordDelimiterMaxLength" equals `0`
2240
+ // 2. We set the length to windows line ending in the current encoding
2241
+ // Note, that encoding is known from user or bom discovery at that point
2242
+ // recordDelimiterMaxLength,
2243
+ recordDelimiterMaxLength === 0 ? Buffer.from("\r\n", encoding).length : recordDelimiterMaxLength,
2244
+ // Skip if remaining buffer can be an escaped quote
2245
+ quoting ? (escape === null ? 0 : escape.length) + quote.length : 0,
2246
+ // Skip if remaining buffer can be record delimiter following the closing quote
2247
+ quoting ? quote.length + recordDelimiterMaxLength : 0
2248
+ );
2249
+ return numOfCharLeft < requiredLength;
2250
+ },
2251
+ // Central parser implementation
2252
+ parse: function(nextBuf, end, push, close) {
2253
+ const {
2254
+ bom,
2255
+ comment_no_infix,
2256
+ encoding,
2257
+ from_line,
2258
+ ltrim,
2259
+ max_record_size,
2260
+ raw,
2261
+ relax_quotes,
2262
+ rtrim,
2263
+ skip_empty_lines,
2264
+ to,
2265
+ to_line
2266
+ } = this.options;
2267
+ let { comment, escape, quote, record_delimiter } = this.options;
2268
+ const { bomSkipped, previousBuf, rawBuffer, escapeIsQuote } = this.state;
2269
+ let buf;
2270
+ if (previousBuf === void 0) {
2271
+ if (nextBuf === void 0) {
2272
+ close();
2273
+ return;
2274
+ } else {
2275
+ buf = nextBuf;
2276
+ }
2277
+ } else if (previousBuf !== void 0 && nextBuf === void 0) {
2278
+ buf = previousBuf;
2279
+ } else {
2280
+ buf = Buffer.concat([previousBuf, nextBuf]);
2281
+ }
2282
+ if (bomSkipped === false) {
2283
+ if (bom === false) {
2284
+ this.state.bomSkipped = true;
2285
+ } else if (buf.length < 3) {
2286
+ if (end === false) {
2287
+ this.state.previousBuf = buf;
2288
+ return;
2289
+ }
2290
+ } else {
2291
+ for (const encoding2 in boms) {
2292
+ if (boms[encoding2].compare(buf, 0, boms[encoding2].length) === 0) {
2293
+ const bomLength = boms[encoding2].length;
2294
+ this.state.bufBytesStart += bomLength;
2295
+ buf = buf.slice(bomLength);
2296
+ this.options = normalize_options({
2297
+ ...this.original_options,
2298
+ encoding: encoding2
2299
+ });
2300
+ ({ comment, escape, quote } = this.options);
2301
+ break;
2302
+ }
2303
+ }
2304
+ this.state.bomSkipped = true;
2305
+ }
2306
+ }
2307
+ const bufLen = buf.length;
2308
+ let pos;
2309
+ for (pos = 0; pos < bufLen; pos++) {
2310
+ if (this.__needMoreData(pos, bufLen, end)) {
2311
+ break;
2312
+ }
2313
+ if (this.state.wasRowDelimiter === true) {
2314
+ this.info.lines++;
2315
+ this.state.wasRowDelimiter = false;
2316
+ }
2317
+ if (to_line !== -1 && this.info.lines > to_line) {
2318
+ this.state.stop = true;
2319
+ close();
2320
+ return;
2321
+ }
2322
+ if (this.state.quoting === false && record_delimiter.length === 0) {
2323
+ const record_delimiterCount = this.__autoDiscoverRecordDelimiter(
2324
+ buf,
2325
+ pos
2326
+ );
2327
+ if (record_delimiterCount) {
2328
+ record_delimiter = this.options.record_delimiter;
2329
+ }
2330
+ }
2331
+ const chr = buf[pos];
2332
+ if (raw === true) {
2333
+ rawBuffer.append(chr);
2334
+ }
2335
+ if ((chr === cr || chr === nl) && this.state.wasRowDelimiter === false) {
2336
+ this.state.wasRowDelimiter = true;
2337
+ }
2338
+ if (this.state.escaping === true) {
2339
+ this.state.escaping = false;
2340
+ } else {
2341
+ if (escape !== null && this.state.quoting === true && this.__isEscape(buf, pos, chr) && pos + escape.length < bufLen) {
2342
+ if (escapeIsQuote) {
2343
+ if (this.__isQuote(buf, pos + escape.length)) {
2344
+ this.state.escaping = true;
2345
+ pos += escape.length - 1;
2346
+ continue;
2347
+ }
2348
+ } else {
2349
+ this.state.escaping = true;
2350
+ pos += escape.length - 1;
2351
+ continue;
2352
+ }
2353
+ }
2354
+ if (this.state.commenting === false && this.__isQuote(buf, pos)) {
2355
+ if (this.state.quoting === true) {
2356
+ const nextChr = buf[pos + quote.length];
2357
+ const isNextChrTrimable = rtrim && this.__isCharTrimable(buf, pos + quote.length);
2358
+ const isNextChrComment = comment !== null && this.__compareBytes(comment, buf, pos + quote.length, nextChr);
2359
+ const isNextChrDelimiter = this.__isDelimiter(
2360
+ buf,
2361
+ pos + quote.length,
2362
+ nextChr
2363
+ );
2364
+ const isNextChrRecordDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRecordDelimiter(buf, pos + quote.length) : this.__isRecordDelimiter(nextChr, buf, pos + quote.length);
2365
+ if (escape !== null && this.__isEscape(buf, pos, chr) && this.__isQuote(buf, pos + escape.length)) {
2366
+ pos += escape.length - 1;
2367
+ } else if (!nextChr || isNextChrDelimiter || isNextChrRecordDelimiter || isNextChrComment || isNextChrTrimable) {
2368
+ this.state.quoting = false;
2369
+ this.state.wasQuoting = true;
2370
+ pos += quote.length - 1;
2371
+ continue;
2372
+ } else if (relax_quotes === false) {
2373
+ const err = this.__error(
2374
+ new CsvError(
2375
+ "CSV_INVALID_CLOSING_QUOTE",
2376
+ [
2377
+ "Invalid Closing Quote:",
2378
+ `got "${String.fromCharCode(nextChr)}"`,
2379
+ `at line ${this.info.lines}`,
2380
+ "instead of delimiter, record delimiter, trimable character",
2381
+ "(if activated) or comment"
2382
+ ],
2383
+ this.options,
2384
+ this.__infoField()
2385
+ )
2386
+ );
2387
+ if (err !== void 0) return err;
2388
+ } else {
2389
+ this.state.quoting = false;
2390
+ this.state.wasQuoting = true;
2391
+ this.state.field.prepend(quote);
2392
+ pos += quote.length - 1;
2393
+ }
2394
+ } else {
2395
+ if (this.state.field.length !== 0) {
2396
+ if (relax_quotes === false) {
2397
+ const info2 = this.__infoField();
2398
+ const bom2 = Object.keys(boms).map(
2399
+ (b) => boms[b].equals(this.state.field.toString()) ? b : false
2400
+ ).filter(Boolean)[0];
2401
+ const err = this.__error(
2402
+ new CsvError(
2403
+ "INVALID_OPENING_QUOTE",
2404
+ [
2405
+ "Invalid Opening Quote:",
2406
+ `a quote is found on field ${JSON.stringify(info2.column)} at line ${info2.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
2407
+ bom2 ? `(${bom2} bom)` : void 0
2408
+ ],
2409
+ this.options,
2410
+ info2,
2411
+ {
2412
+ field: this.state.field
2413
+ }
2414
+ )
2415
+ );
2416
+ if (err !== void 0) return err;
2417
+ }
2418
+ } else {
2419
+ this.state.quoting = true;
2420
+ pos += quote.length - 1;
2421
+ continue;
2422
+ }
2423
+ }
2424
+ }
2425
+ if (this.state.quoting === false) {
2426
+ const recordDelimiterLength = this.__isRecordDelimiter(
2427
+ chr,
2428
+ buf,
2429
+ pos
2430
+ );
2431
+ if (recordDelimiterLength !== 0) {
2432
+ const skipCommentLine = this.state.commenting && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0;
2433
+ if (skipCommentLine) {
2434
+ this.info.comment_lines++;
2435
+ } else {
2436
+ if (this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1 : 0) >= from_line) {
2437
+ this.state.enabled = true;
2438
+ this.__resetField();
2439
+ this.__resetRecord();
2440
+ pos += recordDelimiterLength - 1;
2441
+ continue;
2442
+ }
2443
+ if (skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0) {
2444
+ this.info.empty_lines++;
2445
+ pos += recordDelimiterLength - 1;
2446
+ continue;
2447
+ }
2448
+ this.info.bytes = this.state.bufBytesStart + pos;
2449
+ const errField = this.__onField();
2450
+ if (errField !== void 0) return errField;
2451
+ this.info.bytes = this.state.bufBytesStart + pos + recordDelimiterLength;
2452
+ const errRecord = this.__onRecord(push);
2453
+ if (errRecord !== void 0) return errRecord;
2454
+ if (to !== -1 && this.info.records >= to) {
2455
+ this.state.stop = true;
2456
+ close();
2457
+ return;
2458
+ }
2459
+ }
2460
+ this.state.commenting = false;
2461
+ pos += recordDelimiterLength - 1;
2462
+ continue;
2463
+ }
2464
+ if (this.state.commenting) {
2465
+ continue;
2466
+ }
2467
+ if (comment !== null && (comment_no_infix === false || this.state.record.length === 0 && this.state.field.length === 0)) {
2468
+ const commentCount = this.__compareBytes(comment, buf, pos, chr);
2469
+ if (commentCount !== 0) {
2470
+ this.state.commenting = true;
2471
+ continue;
2472
+ }
2473
+ }
2474
+ const delimiterLength = this.__isDelimiter(buf, pos, chr);
2475
+ if (delimiterLength !== 0) {
2476
+ this.info.bytes = this.state.bufBytesStart + pos;
2477
+ const errField = this.__onField();
2478
+ if (errField !== void 0) return errField;
2479
+ pos += delimiterLength - 1;
2480
+ continue;
2481
+ }
2482
+ }
2483
+ }
2484
+ if (this.state.commenting === false) {
2485
+ if (max_record_size !== 0 && this.state.record_length + this.state.field.length > max_record_size) {
2486
+ return this.__error(
2487
+ new CsvError(
2488
+ "CSV_MAX_RECORD_SIZE",
2489
+ [
2490
+ "Max Record Size:",
2491
+ "record exceed the maximum number of tolerated bytes",
2492
+ `of ${max_record_size}`,
2493
+ `at line ${this.info.lines}`
2494
+ ],
2495
+ this.options,
2496
+ this.__infoField()
2497
+ )
2498
+ );
2499
+ }
2500
+ }
2501
+ const lappend = ltrim === false || this.state.quoting === true || this.state.field.length !== 0 || !this.__isCharTrimable(buf, pos);
2502
+ const rappend = rtrim === false || this.state.wasQuoting === false;
2503
+ if (lappend === true && rappend === true) {
2504
+ this.state.field.append(chr);
2505
+ } else if (rtrim === true && !this.__isCharTrimable(buf, pos)) {
2506
+ return this.__error(
2507
+ new CsvError(
2508
+ "CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE",
2509
+ [
2510
+ "Invalid Closing Quote:",
2511
+ "found non trimable byte after quote",
2512
+ `at line ${this.info.lines}`
2513
+ ],
2514
+ this.options,
2515
+ this.__infoField()
2516
+ )
2517
+ );
2518
+ } else {
2519
+ if (lappend === false) {
2520
+ pos += this.__isCharTrimable(buf, pos) - 1;
2521
+ }
2522
+ continue;
2523
+ }
2524
+ }
2525
+ if (end === true) {
2526
+ if (this.state.quoting === true) {
2527
+ const err = this.__error(
2528
+ new CsvError(
2529
+ "CSV_QUOTE_NOT_CLOSED",
2530
+ [
2531
+ "Quote Not Closed:",
2532
+ `the parsing is finished with an opening quote at line ${this.info.lines}`
2533
+ ],
2534
+ this.options,
2535
+ this.__infoField()
2536
+ )
2537
+ );
2538
+ if (err !== void 0) return err;
2539
+ } else {
2540
+ if (this.state.wasQuoting === true || this.state.record.length !== 0 || this.state.field.length !== 0) {
2541
+ this.info.bytes = this.state.bufBytesStart + pos;
2542
+ const errField = this.__onField();
2543
+ if (errField !== void 0) return errField;
2544
+ const errRecord = this.__onRecord(push);
2545
+ if (errRecord !== void 0) return errRecord;
2546
+ } else if (this.state.wasRowDelimiter === true) {
2547
+ this.info.empty_lines++;
2548
+ } else if (this.state.commenting === true) {
2549
+ this.info.comment_lines++;
2550
+ }
2551
+ }
2552
+ } else {
2553
+ this.state.bufBytesStart += pos;
2554
+ this.state.previousBuf = buf.slice(pos);
2555
+ }
2556
+ if (this.state.wasRowDelimiter === true) {
2557
+ this.info.lines++;
2558
+ this.state.wasRowDelimiter = false;
2559
+ }
2560
+ },
2561
+ __onRecord: function(push) {
2562
+ const {
2563
+ columns,
2564
+ group_columns_by_name,
2565
+ encoding,
2566
+ info: info2,
2567
+ from: from2,
2568
+ relax_column_count,
2569
+ relax_column_count_less,
2570
+ relax_column_count_more,
2571
+ raw,
2572
+ skip_records_with_empty_values
2573
+ } = this.options;
2574
+ const { enabled, record } = this.state;
2575
+ if (enabled === false) {
2576
+ return this.__resetRecord();
2577
+ }
2578
+ const recordLength = record.length;
2579
+ if (columns === true) {
2580
+ if (skip_records_with_empty_values === true && isRecordEmpty(record)) {
2581
+ this.__resetRecord();
2582
+ return;
2583
+ }
2584
+ return this.__firstLineToColumns(record);
2585
+ }
2586
+ if (columns === false && this.info.records === 0) {
2587
+ this.state.expectedRecordLength = recordLength;
2588
+ }
2589
+ if (recordLength !== this.state.expectedRecordLength) {
2590
+ const err = columns === false ? new CsvError(
2591
+ "CSV_RECORD_INCONSISTENT_FIELDS_LENGTH",
2592
+ [
2593
+ "Invalid Record Length:",
2594
+ `expect ${this.state.expectedRecordLength},`,
2595
+ `got ${recordLength} on line ${this.info.lines}`
2596
+ ],
2597
+ this.options,
2598
+ this.__infoField(),
2599
+ {
2600
+ record
2601
+ }
2602
+ ) : new CsvError(
2603
+ "CSV_RECORD_INCONSISTENT_COLUMNS",
2604
+ [
2605
+ "Invalid Record Length:",
2606
+ `columns length is ${columns.length},`,
2607
+ // rename columns
2608
+ `got ${recordLength} on line ${this.info.lines}`
2609
+ ],
2610
+ this.options,
2611
+ this.__infoField(),
2612
+ {
2613
+ record
2614
+ }
2615
+ );
2616
+ if (relax_column_count === true || relax_column_count_less === true && recordLength < this.state.expectedRecordLength || relax_column_count_more === true && recordLength > this.state.expectedRecordLength) {
2617
+ this.info.invalid_field_length++;
2618
+ this.state.error = err;
2619
+ } else {
2620
+ const finalErr = this.__error(err);
2621
+ if (finalErr) return finalErr;
2622
+ }
2623
+ }
2624
+ if (skip_records_with_empty_values === true && isRecordEmpty(record)) {
2625
+ this.__resetRecord();
2626
+ return;
2627
+ }
2628
+ if (this.state.recordHasError === true) {
2629
+ this.__resetRecord();
2630
+ this.state.recordHasError = false;
2631
+ return;
2632
+ }
2633
+ this.info.records++;
2634
+ if (from2 === 1 || this.info.records >= from2) {
2635
+ const { objname } = this.options;
2636
+ if (columns !== false) {
2637
+ const obj = {};
2638
+ for (let i = 0, l = record.length; i < l; i++) {
2639
+ if (columns[i] === void 0 || columns[i].disabled) continue;
2640
+ if (group_columns_by_name === true && obj[columns[i].name] !== void 0) {
2641
+ if (Array.isArray(obj[columns[i].name])) {
2642
+ obj[columns[i].name] = obj[columns[i].name].concat(record[i]);
2643
+ } else {
2644
+ obj[columns[i].name] = [obj[columns[i].name], record[i]];
2645
+ }
2646
+ } else {
2647
+ obj[columns[i].name] = record[i];
2648
+ }
2649
+ }
2650
+ if (raw === true || info2 === true) {
2651
+ const extRecord = Object.assign(
2652
+ { record: obj },
2653
+ raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {},
2654
+ info2 === true ? { info: this.__infoRecord() } : {}
2655
+ );
2656
+ const err = this.__push(
2657
+ objname === void 0 ? extRecord : [obj[objname], extRecord],
2658
+ push
2659
+ );
2660
+ if (err) {
2661
+ return err;
2662
+ }
2663
+ } else {
2664
+ const err = this.__push(
2665
+ objname === void 0 ? obj : [obj[objname], obj],
2666
+ push
2667
+ );
2668
+ if (err) {
2669
+ return err;
2670
+ }
2671
+ }
2672
+ } else {
2673
+ if (raw === true || info2 === true) {
2674
+ const extRecord = Object.assign(
2675
+ { record },
2676
+ raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {},
2677
+ info2 === true ? { info: this.__infoRecord() } : {}
2678
+ );
2679
+ const err = this.__push(
2680
+ objname === void 0 ? extRecord : [record[objname], extRecord],
2681
+ push
2682
+ );
2683
+ if (err) {
2684
+ return err;
2685
+ }
2686
+ } else {
2687
+ const err = this.__push(
2688
+ objname === void 0 ? record : [record[objname], record],
2689
+ push
2690
+ );
2691
+ if (err) {
2692
+ return err;
2693
+ }
2694
+ }
2695
+ }
2696
+ }
2697
+ this.__resetRecord();
2698
+ },
2699
+ __firstLineToColumns: function(record) {
2700
+ const { firstLineToHeaders } = this.state;
2701
+ try {
2702
+ const headers = firstLineToHeaders === void 0 ? record : firstLineToHeaders.call(null, record);
2703
+ if (!Array.isArray(headers)) {
2704
+ return this.__error(
2705
+ new CsvError(
2706
+ "CSV_INVALID_COLUMN_MAPPING",
2707
+ [
2708
+ "Invalid Column Mapping:",
2709
+ "expect an array from column function,",
2710
+ `got ${JSON.stringify(headers)}`
2711
+ ],
2712
+ this.options,
2713
+ this.__infoField(),
2714
+ {
2715
+ headers
2716
+ }
2717
+ )
2718
+ );
2719
+ }
2720
+ const normalizedHeaders = normalize_columns_array(headers);
2721
+ this.state.expectedRecordLength = normalizedHeaders.length;
2722
+ this.options.columns = normalizedHeaders;
2723
+ this.__resetRecord();
2724
+ return;
2725
+ } catch (err) {
2726
+ return err;
2727
+ }
2728
+ },
2729
+ __resetRecord: function() {
2730
+ if (this.options.raw === true) {
2731
+ this.state.rawBuffer.reset();
2732
+ }
2733
+ this.state.error = void 0;
2734
+ this.state.record = [];
2735
+ this.state.record_length = 0;
2736
+ },
2737
+ __onField: function() {
2738
+ const { cast, encoding, rtrim, max_record_size } = this.options;
2739
+ const { enabled, wasQuoting } = this.state;
2740
+ if (enabled === false) {
2741
+ return this.__resetField();
2742
+ }
2743
+ let field = this.state.field.toString(encoding);
2744
+ if (rtrim === true && wasQuoting === false) {
2745
+ field = field.trimRight();
2746
+ }
2747
+ if (cast === true) {
2748
+ const [err, f] = this.__cast(field);
2749
+ if (err !== void 0) return err;
2750
+ field = f;
2751
+ }
2752
+ this.state.record.push(field);
2753
+ if (max_record_size !== 0 && typeof field === "string") {
2754
+ this.state.record_length += field.length;
2755
+ }
2756
+ this.__resetField();
2757
+ },
2758
+ __resetField: function() {
2759
+ this.state.field.reset();
2760
+ this.state.wasQuoting = false;
2761
+ },
2762
+ __push: function(record, push) {
2763
+ const { on_record } = this.options;
2764
+ if (on_record !== void 0) {
2765
+ const info2 = this.__infoRecord();
2766
+ try {
2767
+ record = on_record.call(null, record, info2);
2768
+ } catch (err) {
2769
+ return err;
2770
+ }
2771
+ if (record === void 0 || record === null) {
2772
+ return;
2773
+ }
2774
+ }
2775
+ push(record);
2776
+ },
2777
+ // Return a tuple with the error and the casted value
2778
+ __cast: function(field) {
2779
+ const { columns, relax_column_count } = this.options;
2780
+ const isColumns = Array.isArray(columns);
2781
+ if (isColumns === true && relax_column_count && this.options.columns.length <= this.state.record.length) {
2782
+ return [void 0, void 0];
2783
+ }
2784
+ if (this.state.castField !== null) {
2785
+ try {
2786
+ const info2 = this.__infoField();
2787
+ return [void 0, this.state.castField.call(null, field, info2)];
2788
+ } catch (err) {
2789
+ return [err];
2790
+ }
2791
+ }
2792
+ if (this.__isFloat(field)) {
2793
+ return [void 0, parseFloat(field)];
2794
+ } else if (this.options.cast_date !== false) {
2795
+ const info2 = this.__infoField();
2796
+ return [void 0, this.options.cast_date.call(null, field, info2)];
2797
+ }
2798
+ return [void 0, field];
2799
+ },
2800
+ // Helper to test if a character is a space or a line delimiter
2801
+ __isCharTrimable: function(buf, pos) {
2802
+ const isTrim = (buf2, pos2) => {
2803
+ const { timchars } = this.state;
2804
+ loop1: for (let i = 0; i < timchars.length; i++) {
2805
+ const timchar = timchars[i];
2806
+ for (let j = 0; j < timchar.length; j++) {
2807
+ if (timchar[j] !== buf2[pos2 + j]) continue loop1;
2808
+ }
2809
+ return timchar.length;
2810
+ }
2811
+ return 0;
2812
+ };
2813
+ return isTrim(buf, pos);
2814
+ },
2815
+ // Keep it in case we implement the `cast_int` option
2816
+ // __isInt(value){
2817
+ // // return Number.isInteger(parseInt(value))
2818
+ // // return !isNaN( parseInt( obj ) );
2819
+ // return /^(\-|\+)?[1-9][0-9]*$/.test(value)
2820
+ // }
2821
+ __isFloat: function(value) {
2822
+ return value - parseFloat(value) + 1 >= 0;
2823
+ },
2824
+ __compareBytes: function(sourceBuf, targetBuf, targetPos, firstByte) {
2825
+ if (sourceBuf[0] !== firstByte) return 0;
2826
+ const sourceLength = sourceBuf.length;
2827
+ for (let i = 1; i < sourceLength; i++) {
2828
+ if (sourceBuf[i] !== targetBuf[targetPos + i]) return 0;
2829
+ }
2830
+ return sourceLength;
2831
+ },
2832
+ __isDelimiter: function(buf, pos, chr) {
2833
+ const { delimiter, ignore_last_delimiters } = this.options;
2834
+ if (ignore_last_delimiters === true && this.state.record.length === this.options.columns.length - 1) {
2835
+ return 0;
2836
+ } else if (ignore_last_delimiters !== false && typeof ignore_last_delimiters === "number" && this.state.record.length === ignore_last_delimiters - 1) {
2837
+ return 0;
2838
+ }
2839
+ loop1: for (let i = 0; i < delimiter.length; i++) {
2840
+ const del = delimiter[i];
2841
+ if (del[0] === chr) {
2842
+ for (let j = 1; j < del.length; j++) {
2843
+ if (del[j] !== buf[pos + j]) continue loop1;
2844
+ }
2845
+ return del.length;
2846
+ }
2847
+ }
2848
+ return 0;
2849
+ },
2850
+ __isRecordDelimiter: function(chr, buf, pos) {
2851
+ const { record_delimiter } = this.options;
2852
+ const recordDelimiterLength = record_delimiter.length;
2853
+ loop1: for (let i = 0; i < recordDelimiterLength; i++) {
2854
+ const rd = record_delimiter[i];
2855
+ const rdLength = rd.length;
2856
+ if (rd[0] !== chr) {
2857
+ continue;
2858
+ }
2859
+ for (let j = 1; j < rdLength; j++) {
2860
+ if (rd[j] !== buf[pos + j]) {
2861
+ continue loop1;
2862
+ }
2863
+ }
2864
+ return rd.length;
2865
+ }
2866
+ return 0;
2867
+ },
2868
+ __isEscape: function(buf, pos, chr) {
2869
+ const { escape } = this.options;
2870
+ if (escape === null) return false;
2871
+ const l = escape.length;
2872
+ if (escape[0] === chr) {
2873
+ for (let i = 0; i < l; i++) {
2874
+ if (escape[i] !== buf[pos + i]) {
2875
+ return false;
2876
+ }
2877
+ }
2878
+ return true;
2879
+ }
2880
+ return false;
2881
+ },
2882
+ __isQuote: function(buf, pos) {
2883
+ const { quote } = this.options;
2884
+ if (quote === null) return false;
2885
+ const l = quote.length;
2886
+ for (let i = 0; i < l; i++) {
2887
+ if (quote[i] !== buf[pos + i]) {
2888
+ return false;
2889
+ }
2890
+ }
2891
+ return true;
2892
+ },
2893
+ __autoDiscoverRecordDelimiter: function(buf, pos) {
2894
+ const { encoding } = this.options;
2895
+ const rds = [
2896
+ // Important, the windows line ending must be before mac os 9
2897
+ Buffer.from("\r\n", encoding),
2898
+ Buffer.from("\n", encoding),
2899
+ Buffer.from("\r", encoding)
2900
+ ];
2901
+ loop: for (let i = 0; i < rds.length; i++) {
2902
+ const l = rds[i].length;
2903
+ for (let j = 0; j < l; j++) {
2904
+ if (rds[i][j] !== buf[pos + j]) {
2905
+ continue loop;
2906
+ }
2907
+ }
2908
+ this.options.record_delimiter.push(rds[i]);
2909
+ this.state.recordDelimiterMaxLength = rds[i].length;
2910
+ return rds[i].length;
2911
+ }
2912
+ return 0;
2913
+ },
2914
+ __error: function(msg) {
2915
+ const { encoding, raw, skip_records_with_error } = this.options;
2916
+ const err = typeof msg === "string" ? new Error(msg) : msg;
2917
+ if (skip_records_with_error) {
2918
+ this.state.recordHasError = true;
2919
+ if (this.options.on_skip !== void 0) {
2920
+ this.options.on_skip(
2921
+ err,
2922
+ raw ? this.state.rawBuffer.toString(encoding) : void 0
2923
+ );
2924
+ }
2925
+ return void 0;
2926
+ } else {
2927
+ return err;
2928
+ }
2929
+ },
2930
+ __infoDataSet: function() {
2931
+ return {
2932
+ ...this.info,
2933
+ columns: this.options.columns
2934
+ };
2935
+ },
2936
+ __infoRecord: function() {
2937
+ const { columns, raw, encoding } = this.options;
2938
+ return {
2939
+ ...this.__infoDataSet(),
2940
+ error: this.state.error,
2941
+ header: columns === true,
2942
+ index: this.state.record.length,
2943
+ raw: raw ? this.state.rawBuffer.toString(encoding) : void 0
2944
+ };
2945
+ },
2946
+ __infoField: function() {
2947
+ const { columns } = this.options;
2948
+ const isColumns = Array.isArray(columns);
2949
+ return {
2950
+ ...this.__infoRecord(),
2951
+ column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
2952
+ quoting: this.state.wasQuoting
2953
+ };
2954
+ }
2955
+ };
2956
+ };
2957
+ var parse = function(data, opts = {}) {
2958
+ if (typeof data === "string") {
2959
+ data = Buffer.from(data);
2960
+ }
2961
+ const records = opts && opts.objname ? {} : [];
2962
+ const parser = transform(opts);
2963
+ const push = (record) => {
2964
+ if (parser.options.objname === void 0) records.push(record);
2965
+ else {
2966
+ records[record[0]] = record[1];
2967
+ }
2968
+ };
2969
+ const close = () => {
2970
+ };
2971
+ const err1 = parser.parse(data, false, push, close);
2972
+ if (err1 !== void 0) throw err1;
2973
+ const err2 = parser.parse(void 0, true, push, close);
2974
+ if (err2 !== void 0) throw err2;
2975
+ return records;
2976
+ };
2977
+
2978
+ // src/plays/csv-data-input.ts
2979
+ function parseCsvDataInput(source, content, options = {}) {
2980
+ const onRecord = options.onRecord;
2981
+ let recordCount = 0;
2982
+ let records;
2983
+ try {
2984
+ records = parse(content, {
2985
+ bom: true,
2986
+ columns: false,
2987
+ skip_empty_lines: true,
2988
+ relax_column_count: true,
2989
+ // STRICT RFC quoting: an unclosed quote or ragged quoting is a hard
2990
+ // error rather than being swallowed into one giant field.
2991
+ relax_quotes: false,
2992
+ trim: true,
2993
+ ...onRecord ? {
2994
+ on_record: (record) => {
2995
+ recordCount += 1;
2996
+ onRecord(recordCount);
2997
+ return record;
2998
+ }
2999
+ } : {}
3000
+ });
3001
+ } catch (error) {
3002
+ throw new (0, _chunk7P5AA3SNjs.DeeplineError)(
3003
+ `Input ${source.inputPath} could not be parsed as CSV (${source.value}): ${error instanceof Error ? error.message : String(error)}. This usually means an unclosed quote or ragged quoting. No run was created.`,
3004
+ void 0,
3005
+ "PLAY_INPUT_FILE_PREFLIGHT",
3006
+ { inputPath: source.inputPath, path: source.value }
3007
+ );
3008
+ }
3009
+ if (records.length === 0) {
3010
+ throw new (0, _chunk7P5AA3SNjs.DeeplineError)(
3011
+ `${source.value} is empty: it has no header row and no data rows. No run was created.`,
3012
+ void 0,
3013
+ "PLAY_INPUT_FILE_PREFLIGHT",
3014
+ { inputPath: source.inputPath, path: source.value }
3015
+ );
3016
+ }
3017
+ const header = records[0];
3018
+ const seen = /* @__PURE__ */ new Map();
3019
+ for (let i = 0; i < header.length; i++) {
3020
+ const raw = _nullishCoalesce(header[i], () => ( ""));
3021
+ const name = raw.trim();
3022
+ if (name.length === 0) continue;
3023
+ const prior = seen.get(name);
3024
+ if (prior) {
3025
+ throw new (0, _chunk7P5AA3SNjs.DeeplineError)(
3026
+ `Input ${source.inputPath} (${source.value}) has a duplicate CSV header "${name}" (columns ${prior.index + 1} and ${i + 1}). The second column would silently overwrite the first. Rename one of them. No run was created.`,
3027
+ void 0,
3028
+ "PLAY_INPUT_FILE_PREFLIGHT",
3029
+ {
3030
+ inputPath: source.inputPath,
3031
+ path: source.value,
3032
+ duplicateHeader: name,
3033
+ columns: [prior.index + 1, i + 1],
3034
+ rawSpellings: [prior.raw, raw]
3035
+ }
3036
+ );
3037
+ }
3038
+ seen.set(name, { index: i, raw });
3039
+ }
3040
+ if (records.length - 1 < 1) {
3041
+ throw new (0, _chunk7P5AA3SNjs.DeeplineError)(
3042
+ `${source.value} has a header row but no data rows. No run was created.`,
3043
+ void 0,
3044
+ "PLAY_INPUT_FILE_PREFLIGHT",
3045
+ { inputPath: source.inputPath, path: source.value }
3046
+ );
3047
+ }
3048
+ return {
3049
+ headers: header.map((name) => name.trim()),
3050
+ records: records.slice(1)
3051
+ };
3052
+ }
3053
+
3054
+
3055
+
3056
+ exports.parseCsvDataInput = parseCsvDataInput;