@volter/tabnode 0.2.14-volter.88

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 (429) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +78 -0
  3. package/THIRD-PARTY-NOTICES.md +12 -0
  4. package/dist/__sw__.js +526 -0
  5. package/dist/assets/runtime-worker-4MKaHFVb.js +75221 -0
  6. package/dist/assets/runtime-worker-4MKaHFVb.js.map +1 -0
  7. package/dist/code-transforms.d.ts +21 -0
  8. package/dist/code-transforms.d.ts.map +1 -0
  9. package/dist/config/cdn.d.ts +12 -0
  10. package/dist/config/cdn.d.ts.map +1 -0
  11. package/dist/create-runtime.d.ts +22 -0
  12. package/dist/create-runtime.d.ts.map +1 -0
  13. package/dist/host-globals.d.ts +62 -0
  14. package/dist/host-globals.d.ts.map +1 -0
  15. package/dist/index.cjs +36021 -0
  16. package/dist/index.cjs.map +1 -0
  17. package/dist/index.d.ts +127 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.mjs +36020 -0
  20. package/dist/index.mjs.map +1 -0
  21. package/dist/node-internals.d.ts +68 -0
  22. package/dist/node-internals.d.ts.map +1 -0
  23. package/dist/node-lib/binding/blob.d.ts +12 -0
  24. package/dist/node-lib/binding/blob.d.ts.map +1 -0
  25. package/dist/node-lib/binding/buffer.d.ts +144 -0
  26. package/dist/node-lib/binding/buffer.d.ts.map +1 -0
  27. package/dist/node-lib/binding/cares_wrap.d.ts +14 -0
  28. package/dist/node-lib/binding/cares_wrap.d.ts.map +1 -0
  29. package/dist/node-lib/binding/fds.d.ts +30 -0
  30. package/dist/node-lib/binding/fds.d.ts.map +1 -0
  31. package/dist/node-lib/binding/fs.d.ts +157 -0
  32. package/dist/node-lib/binding/fs.d.ts.map +1 -0
  33. package/dist/node-lib/binding/handles.d.ts +81 -0
  34. package/dist/node-lib/binding/handles.d.ts.map +1 -0
  35. package/dist/node-lib/binding/http_parser.d.ts +122 -0
  36. package/dist/node-lib/binding/http_parser.d.ts.map +1 -0
  37. package/dist/node-lib/binding/index.d.ts +3 -0
  38. package/dist/node-lib/binding/index.d.ts.map +1 -0
  39. package/dist/node-lib/binding/llhttp-wasm.d.ts +22 -0
  40. package/dist/node-lib/binding/llhttp-wasm.d.ts.map +1 -0
  41. package/dist/node-lib/binding/misc.d.ts +123 -0
  42. package/dist/node-lib/binding/misc.d.ts.map +1 -0
  43. package/dist/node-lib/binding/pipe_wrap.d.ts +84 -0
  44. package/dist/node-lib/binding/pipe_wrap.d.ts.map +1 -0
  45. package/dist/node-lib/binding/process_wrap.d.ts +173 -0
  46. package/dist/node-lib/binding/process_wrap.d.ts.map +1 -0
  47. package/dist/node-lib/binding/spawn_sync.d.ts +30 -0
  48. package/dist/node-lib/binding/spawn_sync.d.ts.map +1 -0
  49. package/dist/node-lib/binding/stream_wrap.d.ts +191 -0
  50. package/dist/node-lib/binding/stream_wrap.d.ts.map +1 -0
  51. package/dist/node-lib/binding/stubs.d.ts +23 -0
  52. package/dist/node-lib/binding/stubs.d.ts.map +1 -0
  53. package/dist/node-lib/binding/tcp_wrap.d.ts +98 -0
  54. package/dist/node-lib/binding/tcp_wrap.d.ts.map +1 -0
  55. package/dist/node-lib/binding/tty_wrap.d.ts +36 -0
  56. package/dist/node-lib/binding/tty_wrap.d.ts.map +1 -0
  57. package/dist/node-lib/binding/util.d.ts +126 -0
  58. package/dist/node-lib/binding/util.d.ts.map +1 -0
  59. package/dist/node-lib/binding/uv.d.ts +32 -0
  60. package/dist/node-lib/binding/uv.d.ts.map +1 -0
  61. package/dist/node-lib/binding/zlib-pako.d.ts +22 -0
  62. package/dist/node-lib/binding/zlib-pako.d.ts.map +1 -0
  63. package/dist/node-lib/binding/zlib.d.ts +38 -0
  64. package/dist/node-lib/binding/zlib.d.ts.map +1 -0
  65. package/dist/node-lib/buffer-module.d.ts +51 -0
  66. package/dist/node-lib/buffer-module.d.ts.map +1 -0
  67. package/dist/node-lib/child-process-module.d.ts +26 -0
  68. package/dist/node-lib/child-process-module.d.ts.map +1 -0
  69. package/dist/node-lib/events-module.d.ts +58 -0
  70. package/dist/node-lib/events-module.d.ts.map +1 -0
  71. package/dist/node-lib/fs-module.d.ts +5 -0
  72. package/dist/node-lib/fs-module.d.ts.map +1 -0
  73. package/dist/node-lib/http-bridge.d.ts +43 -0
  74. package/dist/node-lib/http-bridge.d.ts.map +1 -0
  75. package/dist/node-lib/http-module.d.ts +35 -0
  76. package/dist/node-lib/http-module.d.ts.map +1 -0
  77. package/dist/node-lib/internals/addresses.d.ts +40 -0
  78. package/dist/node-lib/internals/addresses.d.ts.map +1 -0
  79. package/dist/node-lib/internals/buffer-and-streams.d.ts +96 -0
  80. package/dist/node-lib/internals/buffer-and-streams.d.ts.map +1 -0
  81. package/dist/node-lib/internals/child-process.d.ts +27 -0
  82. package/dist/node-lib/internals/child-process.d.ts.map +1 -0
  83. package/dist/node-lib/internals/events-util.d.ts +83 -0
  84. package/dist/node-lib/internals/events-util.d.ts.map +1 -0
  85. package/dist/node-lib/internals/index.d.ts +2 -0
  86. package/dist/node-lib/internals/index.d.ts.map +1 -0
  87. package/dist/node-lib/internals/modules.d.ts +67 -0
  88. package/dist/node-lib/internals/modules.d.ts.map +1 -0
  89. package/dist/node-lib/internals/readline-access.d.ts +2 -0
  90. package/dist/node-lib/internals/readline-access.d.ts.map +1 -0
  91. package/dist/node-lib/internals/runtime.d.ts +150 -0
  92. package/dist/node-lib/internals/runtime.d.ts.map +1 -0
  93. package/dist/node-lib/internals/util.d.ts +33 -0
  94. package/dist/node-lib/internals/util.d.ts.map +1 -0
  95. package/dist/node-lib/lazy.d.ts +31 -0
  96. package/dist/node-lib/lazy.d.ts.map +1 -0
  97. package/dist/node-lib/load.d.ts +27 -0
  98. package/dist/node-lib/load.d.ts.map +1 -0
  99. package/dist/node-lib/module-hooks.d.ts +89 -0
  100. package/dist/node-lib/module-hooks.d.ts.map +1 -0
  101. package/dist/node-lib/net-module.d.ts +181 -0
  102. package/dist/node-lib/net-module.d.ts.map +1 -0
  103. package/dist/node-lib/node-versions.d.ts +27 -0
  104. package/dist/node-lib/node-versions.d.ts.map +1 -0
  105. package/dist/node-lib/public-modules.d.ts +2 -0
  106. package/dist/node-lib/public-modules.d.ts.map +1 -0
  107. package/dist/node-lib/require-hook.d.ts +15 -0
  108. package/dist/node-lib/require-hook.d.ts.map +1 -0
  109. package/dist/node-lib/small-modules.d.ts +16 -0
  110. package/dist/node-lib/small-modules.d.ts.map +1 -0
  111. package/dist/node-lib/sources.d.ts +4 -0
  112. package/dist/node-lib/sources.d.ts.map +1 -0
  113. package/dist/node-lib/stream-module.d.ts +77 -0
  114. package/dist/node-lib/stream-module.d.ts.map +1 -0
  115. package/dist/node-lib/timers.d.ts +19 -0
  116. package/dist/node-lib/timers.d.ts.map +1 -0
  117. package/dist/node-lib/util-module.d.ts +76 -0
  118. package/dist/node-lib/util-module.d.ts.map +1 -0
  119. package/dist/node-resolution.d.ts +50 -0
  120. package/dist/node-resolution.d.ts.map +1 -0
  121. package/dist/node-resolver.d.ts +7 -0
  122. package/dist/node-resolver.d.ts.map +1 -0
  123. package/dist/npm/index.d.ts +73 -0
  124. package/dist/npm/index.d.ts.map +1 -0
  125. package/dist/npm/registry.d.ts +111 -0
  126. package/dist/npm/registry.d.ts.map +1 -0
  127. package/dist/npm/resolver.d.ts +79 -0
  128. package/dist/npm/resolver.d.ts.map +1 -0
  129. package/dist/npm/tarball.d.ts +28 -0
  130. package/dist/npm/tarball.d.ts.map +1 -0
  131. package/dist/process-tokens.d.ts +77 -0
  132. package/dist/process-tokens.d.ts.map +1 -0
  133. package/dist/punycode-source.d.ts +3 -0
  134. package/dist/punycode-source.d.ts.map +1 -0
  135. package/dist/runtime-interface.d.ts +74 -0
  136. package/dist/runtime-interface.d.ts.map +1 -0
  137. package/dist/runtime.d.ts +144 -0
  138. package/dist/runtime.d.ts.map +1 -0
  139. package/dist/server-bridge.d.ts +147 -0
  140. package/dist/server-bridge.d.ts.map +1 -0
  141. package/dist/shims/async_hooks.d.ts +50 -0
  142. package/dist/shims/async_hooks.d.ts.map +1 -0
  143. package/dist/shims/child_process.d.ts +104 -0
  144. package/dist/shims/child_process.d.ts.map +1 -0
  145. package/dist/shims/cluster.d.ts +52 -0
  146. package/dist/shims/cluster.d.ts.map +1 -0
  147. package/dist/shims/command-line.d.ts +43 -0
  148. package/dist/shims/command-line.d.ts.map +1 -0
  149. package/dist/shims/constants.d.ts +222 -0
  150. package/dist/shims/constants.d.ts.map +1 -0
  151. package/dist/shims/crypto-constants.d.ts +12 -0
  152. package/dist/shims/crypto-constants.d.ts.map +1 -0
  153. package/dist/shims/crypto.d.ts +144 -0
  154. package/dist/shims/crypto.d.ts.map +1 -0
  155. package/dist/shims/dgram.d.ts +34 -0
  156. package/dist/shims/dgram.d.ts.map +1 -0
  157. package/dist/shims/dns.d.ts +91 -0
  158. package/dist/shims/dns.d.ts.map +1 -0
  159. package/dist/shims/domain.d.ts +25 -0
  160. package/dist/shims/domain.d.ts.map +1 -0
  161. package/dist/shims/esbuild.d.ts +205 -0
  162. package/dist/shims/esbuild.d.ts.map +1 -0
  163. package/dist/shims/fs.d.ts +25 -0
  164. package/dist/shims/fs.d.ts.map +1 -0
  165. package/dist/shims/http2.d.ts +81 -0
  166. package/dist/shims/http2.d.ts.map +1 -0
  167. package/dist/shims/inspector.d.ts +25 -0
  168. package/dist/shims/inspector.d.ts.map +1 -0
  169. package/dist/shims/module.d.ts +22 -0
  170. package/dist/shims/module.d.ts.map +1 -0
  171. package/dist/shims/os.d.ts +159 -0
  172. package/dist/shims/os.d.ts.map +1 -0
  173. package/dist/shims/path.d.ts +52 -0
  174. package/dist/shims/path.d.ts.map +1 -0
  175. package/dist/shims/perf_hooks.d.ts +50 -0
  176. package/dist/shims/perf_hooks.d.ts.map +1 -0
  177. package/dist/shims/process.d.ts +226 -0
  178. package/dist/shims/process.d.ts.map +1 -0
  179. package/dist/shims/rollup.d.ts +71 -0
  180. package/dist/shims/rollup.d.ts.map +1 -0
  181. package/dist/shims/string_decoder.d.ts +41 -0
  182. package/dist/shims/string_decoder.d.ts.map +1 -0
  183. package/dist/shims/sync-child.d.ts +64 -0
  184. package/dist/shims/sync-child.d.ts.map +1 -0
  185. package/dist/shims/tls.d.ts +53 -0
  186. package/dist/shims/tls.d.ts.map +1 -0
  187. package/dist/shims/url.d.ts +84 -0
  188. package/dist/shims/url.d.ts.map +1 -0
  189. package/dist/shims/v8.d.ts +73 -0
  190. package/dist/shims/v8.d.ts.map +1 -0
  191. package/dist/shims/vfs-adapter.d.ts +126 -0
  192. package/dist/shims/vfs-adapter.d.ts.map +1 -0
  193. package/dist/shims/vm.d.ts +52 -0
  194. package/dist/shims/vm.d.ts.map +1 -0
  195. package/dist/shims/wasi.d.ts +78 -0
  196. package/dist/shims/wasi.d.ts.map +1 -0
  197. package/dist/shims/worker_threads.d.ts +66 -0
  198. package/dist/shims/worker_threads.d.ts.map +1 -0
  199. package/dist/shims/zlib-constants.d.ts +66 -0
  200. package/dist/shims/zlib-constants.d.ts.map +1 -0
  201. package/dist/shims/zlib.d.ts +102 -0
  202. package/dist/shims/zlib.d.ts.map +1 -0
  203. package/dist/transform.d.ts +24 -0
  204. package/dist/transform.d.ts.map +1 -0
  205. package/dist/types/package-json.d.ts +18 -0
  206. package/dist/types/package-json.d.ts.map +1 -0
  207. package/dist/utils/binary-encoding.d.ts +13 -0
  208. package/dist/utils/binary-encoding.d.ts.map +1 -0
  209. package/dist/utils/glob.d.ts +19 -0
  210. package/dist/utils/glob.d.ts.map +1 -0
  211. package/dist/utils/hash.d.ts +6 -0
  212. package/dist/utils/hash.d.ts.map +1 -0
  213. package/dist/virtual-fs.d.ts +259 -0
  214. package/dist/virtual-fs.d.ts.map +1 -0
  215. package/dist/vite-plugin.cjs +50 -0
  216. package/dist/vite-plugin.cjs.map +1 -0
  217. package/dist/vite-plugin.d.ts +36 -0
  218. package/dist/vite-plugin.d.ts.map +1 -0
  219. package/dist/vite-plugin.mjs +31 -0
  220. package/dist/vite-plugin.mjs.map +1 -0
  221. package/dist/worker/runtime-worker.d.ts +8 -0
  222. package/dist/worker/runtime-worker.d.ts.map +1 -0
  223. package/dist/worker-runtime.d.ts +50 -0
  224. package/dist/worker-runtime.d.ts.map +1 -0
  225. package/package.json +88 -0
  226. package/src/code-transforms.ts +560 -0
  227. package/src/config/cdn.ts +13 -0
  228. package/src/create-runtime.ts +89 -0
  229. package/src/host-globals.ts +121 -0
  230. package/src/index.ts +235 -0
  231. package/src/node-internals.ts +283 -0
  232. package/src/node-lib/LICENSE +47 -0
  233. package/src/node-lib/_http_agent.js +547 -0
  234. package/src/node-lib/_http_client.js +996 -0
  235. package/src/node-lib/_http_common.js +269 -0
  236. package/src/node-lib/_http_incoming.js +453 -0
  237. package/src/node-lib/_http_outgoing.js +1262 -0
  238. package/src/node-lib/_http_server.js +1230 -0
  239. package/src/node-lib/assert/strict.js +3 -0
  240. package/src/node-lib/assert.js +844 -0
  241. package/src/node-lib/binding/blob.ts +35 -0
  242. package/src/node-lib/binding/buffer.ts +465 -0
  243. package/src/node-lib/binding/cares_wrap.ts +57 -0
  244. package/src/node-lib/binding/fds.ts +80 -0
  245. package/src/node-lib/binding/fs.ts +857 -0
  246. package/src/node-lib/binding/handles.ts +187 -0
  247. package/src/node-lib/binding/http_parser.ts +536 -0
  248. package/src/node-lib/binding/index.ts +77 -0
  249. package/src/node-lib/binding/llhttp-wasm.ts +670 -0
  250. package/src/node-lib/binding/misc.ts +252 -0
  251. package/src/node-lib/binding/pipe_wrap.ts +166 -0
  252. package/src/node-lib/binding/process_wrap.ts +371 -0
  253. package/src/node-lib/binding/spawn_sync.ts +141 -0
  254. package/src/node-lib/binding/stream_wrap.ts +413 -0
  255. package/src/node-lib/binding/stubs.ts +19 -0
  256. package/src/node-lib/binding/tcp_wrap.ts +254 -0
  257. package/src/node-lib/binding/tty_wrap.ts +56 -0
  258. package/src/node-lib/binding/util.ts +274 -0
  259. package/src/node-lib/binding/uv.ts +102 -0
  260. package/src/node-lib/binding/zlib-pako.ts +50 -0
  261. package/src/node-lib/binding/zlib.ts +259 -0
  262. package/src/node-lib/buffer-module.ts +70 -0
  263. package/src/node-lib/buffer.js +1373 -0
  264. package/src/node-lib/child-process-module.ts +58 -0
  265. package/src/node-lib/child_process.js +1046 -0
  266. package/src/node-lib/constants.js +41 -0
  267. package/src/node-lib/diagnostics_channel.js +438 -0
  268. package/src/node-lib/events-module.ts +80 -0
  269. package/src/node-lib/events.js +1223 -0
  270. package/src/node-lib/fs-module.ts +43 -0
  271. package/src/node-lib/fs.js +3377 -0
  272. package/src/node-lib/http-bridge.ts +299 -0
  273. package/src/node-lib/http-module.ts +53 -0
  274. package/src/node-lib/http.js +198 -0
  275. package/src/node-lib/https.js +429 -0
  276. package/src/node-lib/internal/assert/assertion_error.js +417 -0
  277. package/src/node-lib/internal/assert/calltracker.js +153 -0
  278. package/src/node-lib/internal/assert/myers_diff.js +177 -0
  279. package/src/node-lib/internal/assert/utils.js +278 -0
  280. package/src/node-lib/internal/buffer.js +1120 -0
  281. package/src/node-lib/internal/child_process/serialization.js +169 -0
  282. package/src/node-lib/internal/child_process.js +1135 -0
  283. package/src/node-lib/internal/constants.js +56 -0
  284. package/src/node-lib/internal/deps/minimatch/index.js +1628 -0
  285. package/src/node-lib/internal/errors.js +1912 -0
  286. package/src/node-lib/internal/events/abort_listener.js +54 -0
  287. package/src/node-lib/internal/events/symbols.js +11 -0
  288. package/src/node-lib/internal/fixed_queue.js +119 -0
  289. package/src/node-lib/internal/freelist.js +30 -0
  290. package/src/node-lib/internal/fs/cp/cp-sync.js +242 -0
  291. package/src/node-lib/internal/fs/cp/cp.js +382 -0
  292. package/src/node-lib/internal/fs/dir.js +369 -0
  293. package/src/node-lib/internal/fs/glob.js +763 -0
  294. package/src/node-lib/internal/fs/promises.js +1312 -0
  295. package/src/node-lib/internal/fs/read/context.js +129 -0
  296. package/src/node-lib/internal/fs/recursive_watch.js +275 -0
  297. package/src/node-lib/internal/fs/rimraf.js +311 -0
  298. package/src/node-lib/internal/fs/streams.js +571 -0
  299. package/src/node-lib/internal/fs/sync_write_stream.js +49 -0
  300. package/src/node-lib/internal/fs/utils.js +987 -0
  301. package/src/node-lib/internal/fs/watchers.js +418 -0
  302. package/src/node-lib/internal/http.js +62 -0
  303. package/src/node-lib/internal/mime.js +389 -0
  304. package/src/node-lib/internal/modules/customization_hooks.js +406 -0
  305. package/src/node-lib/internal/net.js +95 -0
  306. package/src/node-lib/internal/querystring.js +119 -0
  307. package/src/node-lib/internal/readline/callbacks.js +132 -0
  308. package/src/node-lib/internal/readline/emitKeypressEvents.js +99 -0
  309. package/src/node-lib/internal/readline/interface.js +1418 -0
  310. package/src/node-lib/internal/readline/promises.js +144 -0
  311. package/src/node-lib/internal/readline/utils.js +405 -0
  312. package/src/node-lib/internal/socket_list.js +108 -0
  313. package/src/node-lib/internal/stream_base_commons.js +280 -0
  314. package/src/node-lib/internal/streams/add-abort-signal.js +60 -0
  315. package/src/node-lib/internal/streams/compose.js +257 -0
  316. package/src/node-lib/internal/streams/destroy.js +361 -0
  317. package/src/node-lib/internal/streams/duplex.js +203 -0
  318. package/src/node-lib/internal/streams/duplexify.js +379 -0
  319. package/src/node-lib/internal/streams/duplexpair.js +62 -0
  320. package/src/node-lib/internal/streams/end-of-stream.js +331 -0
  321. package/src/node-lib/internal/streams/from.js +208 -0
  322. package/src/node-lib/internal/streams/lazy_transform.js +57 -0
  323. package/src/node-lib/internal/streams/legacy.js +125 -0
  324. package/src/node-lib/internal/streams/operators.js +436 -0
  325. package/src/node-lib/internal/streams/passthrough.js +47 -0
  326. package/src/node-lib/internal/streams/pipeline.js +465 -0
  327. package/src/node-lib/internal/streams/readable.js +1762 -0
  328. package/src/node-lib/internal/streams/state.js +51 -0
  329. package/src/node-lib/internal/streams/transform.js +204 -0
  330. package/src/node-lib/internal/streams/utils.js +363 -0
  331. package/src/node-lib/internal/streams/writable.js +1163 -0
  332. package/src/node-lib/internal/tty.js +260 -0
  333. package/src/node-lib/internal/util/colors.js +40 -0
  334. package/src/node-lib/internal/util/comparisons.js +1028 -0
  335. package/src/node-lib/internal/util/debuglog.js +524 -0
  336. package/src/node-lib/internal/util/inspect.js +2535 -0
  337. package/src/node-lib/internal/util/parse_args/parse_args.js +412 -0
  338. package/src/node-lib/internal/util/parse_args/utils.js +197 -0
  339. package/src/node-lib/internal/util.js +1003 -0
  340. package/src/node-lib/internal/validators.js +654 -0
  341. package/src/node-lib/internals/addresses.ts +159 -0
  342. package/src/node-lib/internals/buffer-and-streams.ts +224 -0
  343. package/src/node-lib/internals/child-process.ts +64 -0
  344. package/src/node-lib/internals/events-util.ts +105 -0
  345. package/src/node-lib/internals/index.ts +66 -0
  346. package/src/node-lib/internals/modules.ts +117 -0
  347. package/src/node-lib/internals/readline-access.ts +16 -0
  348. package/src/node-lib/internals/runtime.ts +277 -0
  349. package/src/node-lib/internals/util.ts +98 -0
  350. package/src/node-lib/lazy.ts +158 -0
  351. package/src/node-lib/load.ts +390 -0
  352. package/src/node-lib/module-hooks.ts +281 -0
  353. package/src/node-lib/net-module.ts +248 -0
  354. package/src/node-lib/net.js +2540 -0
  355. package/src/node-lib/node-versions.ts +34 -0
  356. package/src/node-lib/os.js +356 -0
  357. package/src/node-lib/path.js +1694 -0
  358. package/src/node-lib/primordials.js +743 -0
  359. package/src/node-lib/public-modules.ts +109 -0
  360. package/src/node-lib/punycode.js +455 -0
  361. package/src/node-lib/querystring.js +484 -0
  362. package/src/node-lib/readline/promises.js +73 -0
  363. package/src/node-lib/readline.js +523 -0
  364. package/src/node-lib/require-hook.ts +27 -0
  365. package/src/node-lib/small-modules.ts +35 -0
  366. package/src/node-lib/sources.ts +200 -0
  367. package/src/node-lib/stream/promises.js +44 -0
  368. package/src/node-lib/stream-module.ts +101 -0
  369. package/src/node-lib/stream.js +152 -0
  370. package/src/node-lib/timers.ts +33 -0
  371. package/src/node-lib/tty.js +168 -0
  372. package/src/node-lib/util-module.ts +104 -0
  373. package/src/node-lib/util.js +689 -0
  374. package/src/node-lib/wasi.js +179 -0
  375. package/src/node-lib/zlib.js +1036 -0
  376. package/src/node-resolution.ts +241 -0
  377. package/src/node-resolver.ts +33 -0
  378. package/src/npm/index.ts +425 -0
  379. package/src/npm/registry.ts +235 -0
  380. package/src/npm/resolver.ts +589 -0
  381. package/src/npm/tarball.ts +246 -0
  382. package/src/process-tokens.ts +156 -0
  383. package/src/punycode-source.ts +6 -0
  384. package/src/runtime-interface.ts +85 -0
  385. package/src/runtime.ts +3073 -0
  386. package/src/server-bridge.ts +971 -0
  387. package/src/shims/async_hooks.ts +181 -0
  388. package/src/shims/child_process.ts +1591 -0
  389. package/src/shims/cluster.ts +79 -0
  390. package/src/shims/command-line.ts +79 -0
  391. package/src/shims/constants.ts +68 -0
  392. package/src/shims/crypto-constants.ts +11 -0
  393. package/src/shims/crypto.ts +1061 -0
  394. package/src/shims/dgram.ts +47 -0
  395. package/src/shims/dns.ts +185 -0
  396. package/src/shims/domain.ts +58 -0
  397. package/src/shims/esbuild.ts +1638 -0
  398. package/src/shims/fs.ts +82 -0
  399. package/src/shims/http2.ts +96 -0
  400. package/src/shims/inspector.ts +30 -0
  401. package/src/shims/module.ts +86 -0
  402. package/src/shims/os.ts +195 -0
  403. package/src/shims/path.ts +246 -0
  404. package/src/shims/perf_hooks.ts +92 -0
  405. package/src/shims/process.ts +804 -0
  406. package/src/shims/rollup.ts +149 -0
  407. package/src/shims/string_decoder.ts +396 -0
  408. package/src/shims/sync-child.ts +469 -0
  409. package/src/shims/tls.ts +94 -0
  410. package/src/shims/url.ts +217 -0
  411. package/src/shims/v8.ts +113 -0
  412. package/src/shims/vfs-adapter.ts +408 -0
  413. package/src/shims/vm.ts +132 -0
  414. package/src/shims/wasi.ts +1440 -0
  415. package/src/shims/worker_threads.ts +114 -0
  416. package/src/shims/zlib-constants.ts +65 -0
  417. package/src/shims/zlib.ts +228 -0
  418. package/src/transform.ts +324 -0
  419. package/src/types/external.d.ts +55 -0
  420. package/src/types/package-json.ts +17 -0
  421. package/src/types/pako-zlib.d.ts +30 -0
  422. package/src/types/sha.js.d.ts +14 -0
  423. package/src/utils/binary-encoding.ts +43 -0
  424. package/src/utils/glob.ts +54 -0
  425. package/src/utils/hash.ts +12 -0
  426. package/src/virtual-fs.ts +1048 -0
  427. package/src/vite-plugin.ts +76 -0
  428. package/src/worker/runtime-worker.ts +128 -0
  429. package/src/worker-runtime.ts +145 -0
@@ -0,0 +1,1373 @@
1
+ // Copyright Joyent, Inc. and other Node contributors.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a
4
+ // copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ // persons to whom the Software is furnished to do so, subject to the
9
+ // following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included
12
+ // in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ 'use strict';
23
+
24
+ const {
25
+ Array,
26
+ ArrayBufferIsView,
27
+ ArrayIsArray,
28
+ ArrayPrototypeForEach,
29
+ MathFloor,
30
+ MathMin,
31
+ MathTrunc,
32
+ NumberIsInteger,
33
+ NumberIsNaN,
34
+ NumberMAX_SAFE_INTEGER,
35
+ NumberMIN_SAFE_INTEGER,
36
+ ObjectDefineProperties,
37
+ ObjectDefineProperty,
38
+ ObjectPrototypeHasOwnProperty,
39
+ ObjectSetPrototypeOf,
40
+ RegExpPrototypeSymbolReplace,
41
+ StringPrototypeCharCodeAt,
42
+ StringPrototypeSlice,
43
+ StringPrototypeToLowerCase,
44
+ StringPrototypeTrim,
45
+ SymbolSpecies,
46
+ SymbolToPrimitive,
47
+ TypedArrayPrototypeFill,
48
+ TypedArrayPrototypeGetBuffer,
49
+ TypedArrayPrototypeGetByteLength,
50
+ TypedArrayPrototypeGetByteOffset,
51
+ TypedArrayPrototypeGetLength,
52
+ TypedArrayPrototypeSet,
53
+ TypedArrayPrototypeSlice,
54
+ Uint8Array,
55
+ Uint8ArrayPrototype,
56
+ } = primordials;
57
+
58
+ const {
59
+ byteLengthUtf8,
60
+ compare: _compare,
61
+ compareOffset,
62
+ copy: _copy,
63
+ fill: bindingFill,
64
+ isAscii: bindingIsAscii,
65
+ isUtf8: bindingIsUtf8,
66
+ indexOfBuffer,
67
+ indexOfNumber,
68
+ indexOfString,
69
+ swap16: _swap16,
70
+ swap32: _swap32,
71
+ swap64: _swap64,
72
+ kMaxLength,
73
+ kStringMaxLength,
74
+ atob: _atob,
75
+ btoa: _btoa,
76
+ } = internalBinding('buffer');
77
+ const {
78
+ constants: {
79
+ ALL_PROPERTIES,
80
+ ONLY_ENUMERABLE,
81
+ },
82
+ getOwnNonIndexProperties,
83
+ isInsideNodeModules,
84
+ } = internalBinding('util');
85
+ const {
86
+ customInspectSymbol,
87
+ lazyDOMException,
88
+ normalizeEncoding,
89
+ kIsEncodingSymbol,
90
+ defineLazyProperties,
91
+ encodingsMap,
92
+ } = require('internal/util');
93
+ const {
94
+ isAnyArrayBuffer,
95
+ isArrayBufferView,
96
+ isUint8Array,
97
+ isTypedArray,
98
+ } = require('internal/util/types');
99
+ const {
100
+ inspect: utilInspect,
101
+ } = require('internal/util/inspect');
102
+
103
+ const {
104
+ codes: {
105
+ ERR_BUFFER_OUT_OF_BOUNDS,
106
+ ERR_INVALID_ARG_TYPE,
107
+ ERR_INVALID_ARG_VALUE,
108
+ ERR_INVALID_BUFFER_SIZE,
109
+ ERR_MISSING_ARGS,
110
+ ERR_OUT_OF_RANGE,
111
+ ERR_UNKNOWN_ENCODING,
112
+ },
113
+ genericNodeError,
114
+ } = require('internal/errors');
115
+ const {
116
+ validateArray,
117
+ validateBuffer,
118
+ validateInteger,
119
+ validateNumber,
120
+ validateString,
121
+ } = require('internal/validators');
122
+ // Provide validateInteger() but with kMaxLength as the default maximum value.
123
+ const validateOffset = (value, name, min = 0, max = kMaxLength) =>
124
+ validateInteger(value, name, min, max);
125
+
126
+ const {
127
+ FastBuffer,
128
+ markAsUntransferable,
129
+ addBufferPrototypeMethods,
130
+ createUnsafeBuffer,
131
+ } = require('internal/buffer');
132
+
133
+ FastBuffer.prototype.constructor = Buffer;
134
+ Buffer.prototype = FastBuffer.prototype;
135
+ addBufferPrototypeMethods(Buffer.prototype);
136
+
137
+ const constants = ObjectDefineProperties({}, {
138
+ MAX_LENGTH: {
139
+ __proto__: null,
140
+ value: kMaxLength,
141
+ writable: false,
142
+ enumerable: true,
143
+ },
144
+ MAX_STRING_LENGTH: {
145
+ __proto__: null,
146
+ value: kStringMaxLength,
147
+ writable: false,
148
+ enumerable: true,
149
+ },
150
+ });
151
+
152
+ Buffer.poolSize = 8 * 1024;
153
+ let poolSize, poolOffset, allocPool, allocBuffer;
154
+
155
+ function createPool() {
156
+ poolSize = Buffer.poolSize;
157
+ allocBuffer = createUnsafeBuffer(poolSize);
158
+ allocPool = allocBuffer.buffer;
159
+ markAsUntransferable(allocPool);
160
+ poolOffset = 0;
161
+ }
162
+ createPool();
163
+
164
+ function alignPool() {
165
+ // Ensure aligned slices
166
+ if (poolOffset & 0x7) {
167
+ poolOffset |= 0x7;
168
+ poolOffset++;
169
+ }
170
+ }
171
+
172
+ let bufferWarningAlreadyEmitted = false;
173
+ let nodeModulesCheckCounter = 0;
174
+ const bufferWarning = 'Buffer() is deprecated due to security and usability ' +
175
+ 'issues. Please use the Buffer.alloc(), ' +
176
+ 'Buffer.allocUnsafe(), or Buffer.from() methods instead.';
177
+
178
+ function showFlaggedDeprecation() {
179
+ if (bufferWarningAlreadyEmitted ||
180
+ ++nodeModulesCheckCounter > 10000 ||
181
+ (!require('internal/options').getOptionValue('--pending-deprecation') &&
182
+ isInsideNodeModules(100, true))) {
183
+ // We don't emit a warning, because we either:
184
+ // - Already did so, or
185
+ // - Already checked too many times whether a call is coming
186
+ // from node_modules and want to stop slowing down things, or
187
+ // - We aren't running with `--pending-deprecation` enabled,
188
+ // and the code is inside `node_modules`.
189
+ // - We found node_modules in up to the topmost 100 frames, or
190
+ // there are more than 100 frames and we don't want to search anymore.
191
+ return;
192
+ }
193
+
194
+ process.emitWarning(bufferWarning, 'DeprecationWarning', 'DEP0005');
195
+ bufferWarningAlreadyEmitted = true;
196
+ }
197
+
198
+ function toInteger(n, defaultVal) {
199
+ n = +n;
200
+ if (!NumberIsNaN(n) &&
201
+ n >= NumberMIN_SAFE_INTEGER &&
202
+ n <= NumberMAX_SAFE_INTEGER) {
203
+ return ((n % 1) === 0 ? n : MathFloor(n));
204
+ }
205
+ return defaultVal;
206
+ }
207
+
208
+ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
209
+ if (!ArrayBufferIsView(source))
210
+ throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
211
+ if (!ArrayBufferIsView(target))
212
+ throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
213
+
214
+ if (targetStart === undefined) {
215
+ targetStart = 0;
216
+ } else {
217
+ targetStart = NumberIsInteger(targetStart) ? targetStart : toInteger(targetStart, 0);
218
+ if (targetStart < 0)
219
+ throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart);
220
+ }
221
+
222
+ if (sourceStart === undefined) {
223
+ sourceStart = 0;
224
+ } else {
225
+ sourceStart = NumberIsInteger(sourceStart) ? sourceStart : toInteger(sourceStart, 0);
226
+ if (sourceStart < 0 || sourceStart > source.byteLength)
227
+ throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.byteLength}`, sourceStart);
228
+ }
229
+
230
+ if (sourceEnd === undefined) {
231
+ sourceEnd = source.byteLength;
232
+ } else {
233
+ sourceEnd = NumberIsInteger(sourceEnd) ? sourceEnd : toInteger(sourceEnd, 0);
234
+ if (sourceEnd < 0)
235
+ throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd);
236
+ }
237
+
238
+ if (targetStart >= target.byteLength || sourceStart >= sourceEnd)
239
+ return 0;
240
+
241
+ return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
242
+ }
243
+
244
+ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
245
+ if (sourceEnd - sourceStart > target.byteLength - targetStart)
246
+ sourceEnd = sourceStart + target.byteLength - targetStart;
247
+
248
+ let nb = sourceEnd - sourceStart;
249
+ const sourceLen = source.byteLength - sourceStart;
250
+ if (nb > sourceLen)
251
+ nb = sourceLen;
252
+
253
+ if (nb <= 0)
254
+ return 0;
255
+
256
+ _copy(source, target, targetStart, sourceStart, nb);
257
+
258
+ return nb;
259
+ }
260
+
261
+ /**
262
+ * The Buffer() constructor is deprecated in documentation and should not be
263
+ * used moving forward. Rather, developers should use one of the three new
264
+ * factory APIs: Buffer.from(), Buffer.allocUnsafe() or Buffer.alloc() based on
265
+ * their specific needs. There is no runtime deprecation because of the extent
266
+ * to which the Buffer constructor is used in the ecosystem currently -- a
267
+ * runtime deprecation would introduce too much breakage at this time. It's not
268
+ * likely that the Buffer constructors would ever actually be removed.
269
+ * Deprecation Code: DEP0005
270
+ */
271
+ function Buffer(arg, encodingOrOffset, length) {
272
+ showFlaggedDeprecation();
273
+ // Common case.
274
+ if (typeof arg === 'number') {
275
+ if (typeof encodingOrOffset === 'string') {
276
+ throw new ERR_INVALID_ARG_TYPE('string', 'string', arg);
277
+ }
278
+ return Buffer.alloc(arg);
279
+ }
280
+ return Buffer.from(arg, encodingOrOffset, length);
281
+ }
282
+
283
+ ObjectDefineProperty(Buffer, SymbolSpecies, {
284
+ __proto__: null,
285
+ enumerable: false,
286
+ configurable: true,
287
+ get() { return FastBuffer; },
288
+ });
289
+
290
+ /**
291
+ * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
292
+ * if value is a number.
293
+ * Buffer.from(str[, encoding])
294
+ * Buffer.from(array)
295
+ * Buffer.from(buffer)
296
+ * Buffer.from(arrayBuffer[, byteOffset[, length]])
297
+ */
298
+ Buffer.from = function from(value, encodingOrOffset, length) {
299
+ if (typeof value === 'string')
300
+ return fromString(value, encodingOrOffset);
301
+
302
+ if (typeof value === 'object' && value !== null) {
303
+ if (isAnyArrayBuffer(value))
304
+ return fromArrayBuffer(value, encodingOrOffset, length);
305
+
306
+ const valueOf = value.valueOf && value.valueOf();
307
+ if (valueOf != null &&
308
+ valueOf !== value &&
309
+ (typeof valueOf === 'string' || typeof valueOf === 'object')) {
310
+ return from(valueOf, encodingOrOffset, length);
311
+ }
312
+
313
+ const b = fromObject(value);
314
+ if (b)
315
+ return b;
316
+
317
+ if (typeof value[SymbolToPrimitive] === 'function') {
318
+ const primitive = value[SymbolToPrimitive]('string');
319
+ if (typeof primitive === 'string') {
320
+ return fromString(primitive, encodingOrOffset);
321
+ }
322
+ }
323
+ }
324
+
325
+ throw new ERR_INVALID_ARG_TYPE(
326
+ 'first argument',
327
+ ['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
328
+ value,
329
+ );
330
+ };
331
+
332
+ /**
333
+ * Creates the Buffer as a copy of the underlying ArrayBuffer of the view
334
+ * rather than the contents of the view.
335
+ * @param {TypedArray} view
336
+ * @param {number} [offset]
337
+ * @param {number} [length]
338
+ * @returns {Buffer}
339
+ */
340
+ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
341
+ if (!isTypedArray(view)) {
342
+ throw new ERR_INVALID_ARG_TYPE('view', [ 'TypedArray' ], view);
343
+ }
344
+
345
+ const viewLength = TypedArrayPrototypeGetLength(view);
346
+ if (viewLength === 0) {
347
+ return Buffer.alloc(0);
348
+ }
349
+
350
+ if (offset !== undefined || length !== undefined) {
351
+ if (offset !== undefined) {
352
+ validateInteger(offset, 'offset', 0);
353
+ if (offset >= viewLength) return Buffer.alloc(0);
354
+ } else {
355
+ offset = 0;
356
+ }
357
+ let end;
358
+ if (length !== undefined) {
359
+ validateInteger(length, 'length', 0);
360
+ end = offset + length;
361
+ } else {
362
+ end = viewLength;
363
+ }
364
+
365
+ view = TypedArrayPrototypeSlice(view, offset, end);
366
+ }
367
+
368
+ return fromArrayLike(new Uint8Array(
369
+ TypedArrayPrototypeGetBuffer(view),
370
+ TypedArrayPrototypeGetByteOffset(view),
371
+ TypedArrayPrototypeGetByteLength(view)));
372
+ };
373
+
374
+ // Identical to the built-in %TypedArray%.of(), but avoids using the deprecated
375
+ // Buffer() constructor. Must use arrow function syntax to avoid automatically
376
+ // adding a `prototype` property and making the function a constructor.
377
+ //
378
+ // Refs: https://tc39.github.io/ecma262/#sec-%typedarray%.of
379
+ // Refs: https://esdiscuss.org/topic/isconstructor#content-11
380
+ const of = (...items) => {
381
+ const newObj = createUnsafeBuffer(items.length);
382
+ for (let k = 0; k < items.length; k++)
383
+ newObj[k] = items[k];
384
+ return newObj;
385
+ };
386
+ Buffer.of = of;
387
+
388
+ ObjectSetPrototypeOf(Buffer, Uint8Array);
389
+
390
+ /**
391
+ * Creates a new filled Buffer instance.
392
+ * alloc(size[, fill[, encoding]])
393
+ */
394
+ Buffer.alloc = function alloc(size, fill, encoding) {
395
+ validateNumber(size, 'size', 0, kMaxLength);
396
+ if (fill !== undefined && fill !== 0 && size > 0) {
397
+ const buf = createUnsafeBuffer(size);
398
+ return _fill(buf, fill, 0, buf.length, encoding);
399
+ }
400
+ return new FastBuffer(size);
401
+ };
402
+
403
+ /**
404
+ * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer
405
+ * instance. If `--zero-fill-buffers` is set, will zero-fill the buffer.
406
+ */
407
+ Buffer.allocUnsafe = function allocUnsafe(size) {
408
+ validateNumber(size, 'size', 0, kMaxLength);
409
+ return allocate(size);
410
+ };
411
+
412
+ /**
413
+ * Equivalent to SlowBuffer(num), by default creates a non-zero-filled
414
+ * Buffer instance that is not allocated off the pre-initialized pool.
415
+ * If `--zero-fill-buffers` is set, will zero-fill the buffer.
416
+ */
417
+ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
418
+ validateNumber(size, 'size', 0, kMaxLength);
419
+ return createUnsafeBuffer(size);
420
+ };
421
+
422
+ // If --zero-fill-buffers command line argument is set, a zero-filled
423
+ // buffer is returned.
424
+ function SlowBuffer(size) {
425
+ validateNumber(size, 'size', 0, kMaxLength);
426
+ return createUnsafeBuffer(size);
427
+ }
428
+
429
+ ObjectSetPrototypeOf(SlowBuffer.prototype, Uint8ArrayPrototype);
430
+ ObjectSetPrototypeOf(SlowBuffer, Uint8Array);
431
+
432
+ function allocate(size) {
433
+ if (size <= 0) {
434
+ return new FastBuffer();
435
+ }
436
+ if (size < (Buffer.poolSize >>> 1)) {
437
+ if (size > (poolSize - poolOffset))
438
+ createPool();
439
+ const b = new FastBuffer(allocPool, poolOffset, size);
440
+ poolOffset += size;
441
+ alignPool();
442
+ return b;
443
+ }
444
+ return createUnsafeBuffer(size);
445
+ }
446
+
447
+ function fromStringFast(string, ops) {
448
+ const maxLength = Buffer.poolSize >>> 1;
449
+
450
+ let length = string.length; // Min length
451
+
452
+ if (length >= maxLength)
453
+ return createFromString(string, ops);
454
+
455
+ length *= 4; // Max length (4 bytes per character)
456
+
457
+ if (length >= maxLength)
458
+ length = ops.byteLength(string); // Actual length
459
+
460
+ if (length >= maxLength)
461
+ return createFromString(string, ops, length);
462
+
463
+ if (length > (poolSize - poolOffset))
464
+ createPool();
465
+
466
+ const actual = ops.write(allocBuffer, string, poolOffset, length);
467
+ const b = new FastBuffer(allocPool, poolOffset, actual);
468
+
469
+ poolOffset += actual;
470
+ alignPool();
471
+ return b;
472
+ }
473
+
474
+ function createFromString(string, ops, length = ops.byteLength(string)) {
475
+ const buf = Buffer.allocUnsafeSlow(length);
476
+ const actual = ops.write(buf, string, 0, length);
477
+ return actual < length ? new FastBuffer(buf.buffer, 0, actual) : buf;
478
+ }
479
+
480
+ function fromString(string, encoding) {
481
+ let ops;
482
+ if (!encoding || encoding === 'utf8' || typeof encoding !== 'string') {
483
+ ops = encodingOps.utf8;
484
+ } else {
485
+ ops = getEncodingOps(encoding);
486
+ if (ops === undefined)
487
+ throw new ERR_UNKNOWN_ENCODING(encoding);
488
+ }
489
+
490
+ return string.length === 0 ? new FastBuffer() : fromStringFast(string, ops);
491
+ }
492
+
493
+ function fromArrayBuffer(obj, byteOffset, length) {
494
+ // Convert byteOffset to integer
495
+ if (byteOffset === undefined) {
496
+ byteOffset = 0;
497
+ } else {
498
+ byteOffset = +byteOffset;
499
+ if (NumberIsNaN(byteOffset))
500
+ byteOffset = 0;
501
+ }
502
+
503
+ const maxLength = obj.byteLength - byteOffset;
504
+
505
+ if (maxLength < 0)
506
+ throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
507
+
508
+ if (length !== undefined) {
509
+ // Convert length to non-negative integer.
510
+ length = +length;
511
+ if (length > 0) {
512
+ if (length > maxLength)
513
+ throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
514
+ } else {
515
+ length = 0;
516
+ }
517
+ }
518
+
519
+ return new FastBuffer(obj, byteOffset, length);
520
+ }
521
+
522
+ function fromArrayLike(obj) {
523
+ if (obj.length <= 0)
524
+ return new FastBuffer();
525
+ if (obj.length < (Buffer.poolSize >>> 1)) {
526
+ if (obj.length > (poolSize - poolOffset))
527
+ createPool();
528
+ const b = new FastBuffer(allocPool, poolOffset, obj.length);
529
+ TypedArrayPrototypeSet(b, obj, 0);
530
+ poolOffset += obj.length;
531
+ alignPool();
532
+ return b;
533
+ }
534
+ return new FastBuffer(obj);
535
+ }
536
+
537
+ function fromObject(obj) {
538
+ if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) {
539
+ if (typeof obj.length !== 'number') {
540
+ return new FastBuffer();
541
+ }
542
+ return fromArrayLike(obj);
543
+ }
544
+
545
+ if (obj.type === 'Buffer' && ArrayIsArray(obj.data)) {
546
+ return fromArrayLike(obj.data);
547
+ }
548
+ }
549
+
550
+ // Static methods
551
+
552
+ Buffer.isBuffer = function isBuffer(b) {
553
+ return b instanceof Buffer;
554
+ };
555
+
556
+ Buffer.compare = function compare(buf1, buf2) {
557
+ if (!isUint8Array(buf1)) {
558
+ throw new ERR_INVALID_ARG_TYPE('buf1', ['Buffer', 'Uint8Array'], buf1);
559
+ }
560
+
561
+ if (!isUint8Array(buf2)) {
562
+ throw new ERR_INVALID_ARG_TYPE('buf2', ['Buffer', 'Uint8Array'], buf2);
563
+ }
564
+
565
+ if (buf1 === buf2) {
566
+ return 0;
567
+ }
568
+
569
+ return _compare(buf1, buf2);
570
+ };
571
+
572
+ Buffer.isEncoding = function isEncoding(encoding) {
573
+ return typeof encoding === 'string' && encoding.length !== 0 &&
574
+ normalizeEncoding(encoding) !== undefined;
575
+ };
576
+ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
577
+
578
+ Buffer.concat = function concat(list, length) {
579
+ validateArray(list, 'list');
580
+
581
+ if (list.length === 0)
582
+ return new FastBuffer();
583
+
584
+ if (length === undefined) {
585
+ length = 0;
586
+ for (let i = 0; i < list.length; i++) {
587
+ if (list[i].length) {
588
+ length += list[i].length;
589
+ }
590
+ }
591
+ } else {
592
+ validateOffset(length, 'length');
593
+ }
594
+
595
+ const buffer = Buffer.allocUnsafe(length);
596
+ let pos = 0;
597
+ for (let i = 0; i < list.length; i++) {
598
+ const buf = list[i];
599
+ if (!isUint8Array(buf)) {
600
+ // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
601
+ // Instead, find the proper error code for this.
602
+ throw new ERR_INVALID_ARG_TYPE(
603
+ `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
604
+ }
605
+ pos += _copyActual(buf, buffer, pos, 0, buf.length);
606
+ }
607
+
608
+ // Note: `length` is always equal to `buffer.length` at this point
609
+ if (pos < length) {
610
+ // Zero-fill the remaining bytes if the specified `length` was more than
611
+ // the actual total length, i.e. if we have some remaining allocated bytes
612
+ // there were not initialized.
613
+ TypedArrayPrototypeFill(buffer, 0, pos, length);
614
+ }
615
+
616
+ return buffer;
617
+ };
618
+
619
+ function base64ByteLength(str, bytes) {
620
+ // Handle padding
621
+ if (StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
622
+ bytes--;
623
+ if (bytes > 1 && StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
624
+ bytes--;
625
+
626
+ // Base64 ratio: 3/4
627
+ return (bytes * 3) >>> 2;
628
+ }
629
+
630
+ const encodingOps = {
631
+ utf8: {
632
+ encoding: 'utf8',
633
+ encodingVal: encodingsMap.utf8,
634
+ byteLength: byteLengthUtf8,
635
+ write: (buf, string, offset, len) => buf.utf8Write(string, offset, len),
636
+ slice: (buf, start, end) => buf.utf8Slice(start, end),
637
+ indexOf: (buf, val, byteOffset, dir) =>
638
+ indexOfString(buf, val, byteOffset, encodingsMap.utf8, dir),
639
+ },
640
+ ucs2: {
641
+ encoding: 'ucs2',
642
+ encodingVal: encodingsMap.utf16le,
643
+ byteLength: (string) => string.length * 2,
644
+ write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len),
645
+ slice: (buf, start, end) => buf.ucs2Slice(start, end),
646
+ indexOf: (buf, val, byteOffset, dir) =>
647
+ indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir),
648
+ },
649
+ utf16le: {
650
+ encoding: 'utf16le',
651
+ encodingVal: encodingsMap.utf16le,
652
+ byteLength: (string) => string.length * 2,
653
+ write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len),
654
+ slice: (buf, start, end) => buf.ucs2Slice(start, end),
655
+ indexOf: (buf, val, byteOffset, dir) =>
656
+ indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir),
657
+ },
658
+ latin1: {
659
+ encoding: 'latin1',
660
+ encodingVal: encodingsMap.latin1,
661
+ byteLength: (string) => string.length,
662
+ write: (buf, string, offset, len) => buf.latin1Write(string, offset, len),
663
+ slice: (buf, start, end) => buf.latin1Slice(start, end),
664
+ indexOf: (buf, val, byteOffset, dir) =>
665
+ indexOfString(buf, val, byteOffset, encodingsMap.latin1, dir),
666
+ },
667
+ ascii: {
668
+ encoding: 'ascii',
669
+ encodingVal: encodingsMap.ascii,
670
+ byteLength: (string) => string.length,
671
+ write: (buf, string, offset, len) => buf.asciiWrite(string, offset, len),
672
+ slice: (buf, start, end) => buf.asciiSlice(start, end),
673
+ indexOf: (buf, val, byteOffset, dir) =>
674
+ indexOfBuffer(buf,
675
+ fromStringFast(val, encodingOps.ascii),
676
+ byteOffset,
677
+ encodingsMap.ascii,
678
+ dir),
679
+ },
680
+ base64: {
681
+ encoding: 'base64',
682
+ encodingVal: encodingsMap.base64,
683
+ byteLength: (string) => base64ByteLength(string, string.length),
684
+ write: (buf, string, offset, len) => buf.base64Write(string, offset, len),
685
+ slice: (buf, start, end) => buf.base64Slice(start, end),
686
+ indexOf: (buf, val, byteOffset, dir) =>
687
+ indexOfBuffer(buf,
688
+ fromStringFast(val, encodingOps.base64),
689
+ byteOffset,
690
+ encodingsMap.base64,
691
+ dir),
692
+ },
693
+ base64url: {
694
+ encoding: 'base64url',
695
+ encodingVal: encodingsMap.base64url,
696
+ byteLength: (string) => base64ByteLength(string, string.length),
697
+ write: (buf, string, offset, len) =>
698
+ buf.base64urlWrite(string, offset, len),
699
+ slice: (buf, start, end) => buf.base64urlSlice(start, end),
700
+ indexOf: (buf, val, byteOffset, dir) =>
701
+ indexOfBuffer(buf,
702
+ fromStringFast(val, encodingOps.base64url),
703
+ byteOffset,
704
+ encodingsMap.base64url,
705
+ dir),
706
+ },
707
+ hex: {
708
+ encoding: 'hex',
709
+ encodingVal: encodingsMap.hex,
710
+ byteLength: (string) => string.length >>> 1,
711
+ write: (buf, string, offset, len) => buf.hexWrite(string, offset, len),
712
+ slice: (buf, start, end) => buf.hexSlice(start, end),
713
+ indexOf: (buf, val, byteOffset, dir) =>
714
+ indexOfBuffer(buf,
715
+ fromStringFast(val, encodingOps.hex),
716
+ byteOffset,
717
+ encodingsMap.hex,
718
+ dir),
719
+ },
720
+ };
721
+ function getEncodingOps(encoding) {
722
+ encoding += '';
723
+ switch (encoding.length) {
724
+ case 4:
725
+ if (encoding === 'utf8') return encodingOps.utf8;
726
+ if (encoding === 'ucs2') return encodingOps.ucs2;
727
+ encoding = StringPrototypeToLowerCase(encoding);
728
+ if (encoding === 'utf8') return encodingOps.utf8;
729
+ if (encoding === 'ucs2') return encodingOps.ucs2;
730
+ break;
731
+ case 5:
732
+ if (encoding === 'utf-8') return encodingOps.utf8;
733
+ if (encoding === 'ascii') return encodingOps.ascii;
734
+ if (encoding === 'ucs-2') return encodingOps.ucs2;
735
+ encoding = StringPrototypeToLowerCase(encoding);
736
+ if (encoding === 'utf-8') return encodingOps.utf8;
737
+ if (encoding === 'ascii') return encodingOps.ascii;
738
+ if (encoding === 'ucs-2') return encodingOps.ucs2;
739
+ break;
740
+ case 7:
741
+ if (encoding === 'utf16le' ||
742
+ StringPrototypeToLowerCase(encoding) === 'utf16le')
743
+ return encodingOps.utf16le;
744
+ break;
745
+ case 8:
746
+ if (encoding === 'utf-16le' ||
747
+ StringPrototypeToLowerCase(encoding) === 'utf-16le')
748
+ return encodingOps.utf16le;
749
+ break;
750
+ case 6:
751
+ if (encoding === 'latin1' || encoding === 'binary')
752
+ return encodingOps.latin1;
753
+ if (encoding === 'base64') return encodingOps.base64;
754
+ encoding = StringPrototypeToLowerCase(encoding);
755
+ if (encoding === 'latin1' || encoding === 'binary')
756
+ return encodingOps.latin1;
757
+ if (encoding === 'base64') return encodingOps.base64;
758
+ break;
759
+ case 3:
760
+ if (encoding === 'hex' || StringPrototypeToLowerCase(encoding) === 'hex')
761
+ return encodingOps.hex;
762
+ break;
763
+ case 9:
764
+ if (encoding === 'base64url' ||
765
+ StringPrototypeToLowerCase(encoding) === 'base64url')
766
+ return encodingOps.base64url;
767
+ break;
768
+ }
769
+ }
770
+
771
+ function byteLength(string, encoding) {
772
+ if (typeof string !== 'string') {
773
+ if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
774
+ return string.byteLength;
775
+ }
776
+
777
+ throw new ERR_INVALID_ARG_TYPE(
778
+ 'string', ['string', 'Buffer', 'ArrayBuffer'], string,
779
+ );
780
+ }
781
+
782
+ const len = string.length;
783
+ if (len === 0)
784
+ return 0;
785
+
786
+ if (!encoding || encoding === 'utf8') {
787
+ return byteLengthUtf8(string);
788
+ }
789
+
790
+ if (encoding === 'ascii') {
791
+ return len;
792
+ }
793
+
794
+ const ops = getEncodingOps(encoding);
795
+ if (ops === undefined) {
796
+ // TODO (ronag): Makes more sense to throw here.
797
+ // throw new ERR_UNKNOWN_ENCODING(encoding);
798
+ return byteLengthUtf8(string);
799
+ }
800
+
801
+ return ops.byteLength(string);
802
+ }
803
+
804
+ Buffer.byteLength = byteLength;
805
+
806
+ // For backwards compatibility.
807
+ ObjectDefineProperty(Buffer.prototype, 'parent', {
808
+ __proto__: null,
809
+ enumerable: true,
810
+ get() {
811
+ if (!(this instanceof Buffer))
812
+ return undefined;
813
+ return this.buffer;
814
+ },
815
+ });
816
+ ObjectDefineProperty(Buffer.prototype, 'offset', {
817
+ __proto__: null,
818
+ enumerable: true,
819
+ get() {
820
+ if (!(this instanceof Buffer))
821
+ return undefined;
822
+ return this.byteOffset;
823
+ },
824
+ });
825
+
826
+ Buffer.prototype.copy =
827
+ function copy(target, targetStart, sourceStart, sourceEnd) {
828
+ return copyImpl(this, target, targetStart, sourceStart, sourceEnd);
829
+ };
830
+
831
+ // No need to verify that "buf.length <= MAX_UINT32" since it's a read-only
832
+ // property of a typed array.
833
+ // This behaves neither like String nor Uint8Array in that we set start/end
834
+ // to their upper/lower bounds if the value passed is out of range.
835
+ Buffer.prototype.toString = function toString(encoding, start, end) {
836
+ if (arguments.length === 0) {
837
+ return this.utf8Slice(0, this.length);
838
+ }
839
+
840
+ const len = this.length;
841
+
842
+ if (start <= 0)
843
+ start = 0;
844
+ else if (start >= len)
845
+ return '';
846
+ else
847
+ start = MathTrunc(start) || 0;
848
+
849
+ if (end === undefined || end > len)
850
+ end = len;
851
+ else
852
+ end = MathTrunc(end) || 0;
853
+
854
+ if (end <= start)
855
+ return '';
856
+
857
+ if (encoding === undefined)
858
+ return this.utf8Slice(start, end);
859
+
860
+ const ops = getEncodingOps(encoding);
861
+ if (ops === undefined)
862
+ throw new ERR_UNKNOWN_ENCODING(encoding);
863
+
864
+ return ops.slice(this, start, end);
865
+ };
866
+
867
+ Buffer.prototype.equals = function equals(otherBuffer) {
868
+ if (!isUint8Array(otherBuffer)) {
869
+ throw new ERR_INVALID_ARG_TYPE(
870
+ 'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
871
+ }
872
+
873
+ if (this === otherBuffer)
874
+ return true;
875
+ const len = TypedArrayPrototypeGetByteLength(this);
876
+ if (len !== TypedArrayPrototypeGetByteLength(otherBuffer))
877
+ return false;
878
+
879
+ return len === 0 || _compare(this, otherBuffer) === 0;
880
+ };
881
+
882
+ let INSPECT_MAX_BYTES = 50;
883
+ // Override how buffers are presented by util.inspect().
884
+ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
885
+ const max = INSPECT_MAX_BYTES;
886
+ const actualMax = MathMin(max, this.length);
887
+ const remaining = this.length - max;
888
+ let str = StringPrototypeTrim(RegExpPrototypeSymbolReplace(
889
+ /(.{2})/g, this.hexSlice(0, actualMax), '$1 '));
890
+ if (remaining > 0)
891
+ str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
892
+ // Inspect special properties as well, if possible.
893
+ if (ctx) {
894
+ let extras = false;
895
+ const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
896
+ const obj = { __proto__: null };
897
+ ArrayPrototypeForEach(getOwnNonIndexProperties(this, filter),
898
+ (key) => {
899
+ extras = true;
900
+ obj[key] = this[key];
901
+ });
902
+ if (extras) {
903
+ if (this.length !== 0)
904
+ str += ', ';
905
+ // '[Object: null prototype] {'.length === 26
906
+ // This is guarded with a test.
907
+ str += StringPrototypeSlice(utilInspect(obj, {
908
+ ...ctx,
909
+ breakLength: Infinity,
910
+ compact: true,
911
+ }), 27, -2);
912
+ }
913
+ }
914
+ let constructorName = 'Buffer';
915
+ try {
916
+ const { constructor } = this;
917
+ if (typeof constructor === 'function' && ObjectPrototypeHasOwnProperty(constructor, 'name')) {
918
+ constructorName = constructor.name;
919
+ }
920
+ } catch { /* Ignore error and use default name */ }
921
+ return `<${constructorName} ${str}>`;
922
+ };
923
+ Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];
924
+
925
+ Buffer.prototype.compare = function compare(target,
926
+ targetStart,
927
+ targetEnd,
928
+ sourceStart,
929
+ sourceEnd) {
930
+ if (!isUint8Array(target)) {
931
+ throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
932
+ }
933
+ if (arguments.length === 1)
934
+ return _compare(this, target);
935
+
936
+ if (targetStart === undefined)
937
+ targetStart = 0;
938
+ else
939
+ validateOffset(targetStart, 'targetStart');
940
+
941
+ if (targetEnd === undefined)
942
+ targetEnd = target.length;
943
+ else
944
+ validateOffset(targetEnd, 'targetEnd', 0, target.length);
945
+
946
+ if (sourceStart === undefined)
947
+ sourceStart = 0;
948
+ else
949
+ validateOffset(sourceStart, 'sourceStart');
950
+
951
+ if (sourceEnd === undefined)
952
+ sourceEnd = this.length;
953
+ else
954
+ validateOffset(sourceEnd, 'sourceEnd', 0, this.length);
955
+
956
+ if (sourceStart >= sourceEnd)
957
+ return (targetStart >= targetEnd ? 0 : -1);
958
+ if (targetStart >= targetEnd)
959
+ return 1;
960
+
961
+ return compareOffset(this, target, targetStart, sourceStart, targetEnd,
962
+ sourceEnd);
963
+ };
964
+
965
+ // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
966
+ // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
967
+ //
968
+ // Arguments:
969
+ // - buffer - a Buffer to search
970
+ // - val - a string, Buffer, or number
971
+ // - byteOffset - an index into `buffer`; will be clamped to an int32
972
+ // - encoding - an optional encoding, relevant if val is a string
973
+ // - dir - true for indexOf, false for lastIndexOf
974
+ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
975
+ validateBuffer(buffer);
976
+
977
+ if (typeof byteOffset === 'string') {
978
+ encoding = byteOffset;
979
+ byteOffset = undefined;
980
+ } else if (byteOffset > 0x7fffffff) {
981
+ byteOffset = 0x7fffffff;
982
+ } else if (byteOffset < -0x80000000) {
983
+ byteOffset = -0x80000000;
984
+ }
985
+ // Coerce to Number. Values like null and [] become 0.
986
+ byteOffset = +byteOffset;
987
+ // If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
988
+ if (NumberIsNaN(byteOffset)) {
989
+ byteOffset = dir ? 0 : (buffer.length || buffer.byteLength);
990
+ }
991
+ dir = !!dir; // Cast to bool.
992
+
993
+ if (typeof val === 'number')
994
+ return indexOfNumber(buffer, val >>> 0, byteOffset, dir);
995
+
996
+ let ops;
997
+ if (encoding === undefined)
998
+ ops = encodingOps.utf8;
999
+ else
1000
+ ops = getEncodingOps(encoding);
1001
+
1002
+ if (typeof val === 'string') {
1003
+ if (ops === undefined)
1004
+ throw new ERR_UNKNOWN_ENCODING(encoding);
1005
+ return ops.indexOf(buffer, val, byteOffset, dir);
1006
+ }
1007
+
1008
+ if (isUint8Array(val)) {
1009
+ const encodingVal =
1010
+ (ops === undefined ? encodingsMap.utf8 : ops.encodingVal);
1011
+ return indexOfBuffer(buffer, val, byteOffset, encodingVal, dir);
1012
+ }
1013
+
1014
+ throw new ERR_INVALID_ARG_TYPE(
1015
+ 'value', ['number', 'string', 'Buffer', 'Uint8Array'], val,
1016
+ );
1017
+ }
1018
+
1019
+ Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
1020
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, true);
1021
+ };
1022
+
1023
+ Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) {
1024
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, false);
1025
+ };
1026
+
1027
+ Buffer.prototype.includes = function includes(val, byteOffset, encoding) {
1028
+ return this.indexOf(val, byteOffset, encoding) !== -1;
1029
+ };
1030
+
1031
+ // Usage:
1032
+ // buffer.fill(number[, offset[, end]])
1033
+ // buffer.fill(buffer[, offset[, end]])
1034
+ // buffer.fill(string[, offset[, end]][, encoding])
1035
+ Buffer.prototype.fill = function fill(value, offset, end, encoding) {
1036
+ return _fill(this, value, offset, end, encoding);
1037
+ };
1038
+
1039
+ function _fill(buf, value, offset, end, encoding) {
1040
+ if (typeof value === 'string') {
1041
+ if (offset === undefined || typeof offset === 'string') {
1042
+ encoding = offset;
1043
+ offset = 0;
1044
+ end = buf.length;
1045
+ } else if (typeof end === 'string') {
1046
+ encoding = end;
1047
+ end = buf.length;
1048
+ }
1049
+
1050
+ const normalizedEncoding = normalizeEncoding(encoding);
1051
+ if (normalizedEncoding === undefined) {
1052
+ validateString(encoding, 'encoding');
1053
+ throw new ERR_UNKNOWN_ENCODING(encoding);
1054
+ }
1055
+
1056
+ if (value.length === 0) {
1057
+ // If value === '' default to zero.
1058
+ value = 0;
1059
+ } else if (value.length === 1) {
1060
+ // Fast path: If `value` fits into a single byte, use that numeric value.
1061
+ if (normalizedEncoding === 'utf8') {
1062
+ const code = StringPrototypeCharCodeAt(value, 0);
1063
+ if (code < 128) {
1064
+ value = code;
1065
+ }
1066
+ } else if (normalizedEncoding === 'latin1') {
1067
+ value = StringPrototypeCharCodeAt(value, 0);
1068
+ }
1069
+ }
1070
+ } else {
1071
+ encoding = undefined;
1072
+ }
1073
+
1074
+ if (offset === undefined) {
1075
+ offset = 0;
1076
+ end = buf.length;
1077
+ } else {
1078
+ validateOffset(offset, 'offset');
1079
+ // Invalid ranges are not set to a default, so can range check early.
1080
+ if (end === undefined) {
1081
+ end = buf.length;
1082
+ } else {
1083
+ validateOffset(end, 'end', 0, buf.length);
1084
+ }
1085
+ if (offset >= end)
1086
+ return buf;
1087
+ }
1088
+
1089
+
1090
+ if (typeof value === 'number') {
1091
+ // OOB check
1092
+ const byteLen = TypedArrayPrototypeGetByteLength(buf);
1093
+ const fillLength = end - offset;
1094
+ if (offset > end || fillLength + offset > byteLen)
1095
+ throw new ERR_BUFFER_OUT_OF_BOUNDS();
1096
+
1097
+ TypedArrayPrototypeFill(buf, value, offset, end);
1098
+ } else {
1099
+ const res = bindingFill(buf, value, offset, end, encoding);
1100
+ if (res < 0) {
1101
+ if (res === -1)
1102
+ throw new ERR_INVALID_ARG_VALUE('value', value);
1103
+ throw new ERR_BUFFER_OUT_OF_BOUNDS();
1104
+ }
1105
+ }
1106
+
1107
+ return buf;
1108
+ }
1109
+
1110
+ Buffer.prototype.write = function write(string, offset, length, encoding) {
1111
+ // Buffer#write(string);
1112
+ if (offset === undefined) {
1113
+ return this.utf8Write(string, 0, this.length);
1114
+ }
1115
+ // Buffer#write(string, encoding)
1116
+ if (length === undefined && typeof offset === 'string') {
1117
+ encoding = offset;
1118
+ length = this.length;
1119
+ offset = 0;
1120
+
1121
+ // Buffer#write(string, offset[, length][, encoding])
1122
+ } else {
1123
+ validateOffset(offset, 'offset', 0, this.length);
1124
+
1125
+ const remaining = this.length - offset;
1126
+
1127
+ if (length === undefined) {
1128
+ length = remaining;
1129
+ } else if (typeof length === 'string') {
1130
+ encoding = length;
1131
+ length = remaining;
1132
+ } else {
1133
+ validateOffset(length, 'length', 0, this.length);
1134
+ if (length > remaining)
1135
+ length = remaining;
1136
+ }
1137
+ }
1138
+
1139
+ if (!encoding || encoding === 'utf8')
1140
+ return this.utf8Write(string, offset, length);
1141
+ if (encoding === 'ascii')
1142
+ return this.asciiWrite(string, offset, length);
1143
+
1144
+ const ops = getEncodingOps(encoding);
1145
+ if (ops === undefined)
1146
+ throw new ERR_UNKNOWN_ENCODING(encoding);
1147
+ return ops.write(this, string, offset, length);
1148
+ };
1149
+
1150
+ Buffer.prototype.toJSON = function toJSON() {
1151
+ if (this.length > 0) {
1152
+ const data = new Array(this.length);
1153
+ for (let i = 0; i < this.length; ++i)
1154
+ data[i] = this[i];
1155
+ return { type: 'Buffer', data };
1156
+ }
1157
+ return { type: 'Buffer', data: [] };
1158
+ };
1159
+
1160
+ function adjustOffset(offset, length) {
1161
+ // Use Math.trunc() to convert offset to an integer value that can be larger
1162
+ // than an Int32. Hence, don't use offset | 0 or similar techniques.
1163
+ offset = MathTrunc(offset);
1164
+ if (offset === 0) {
1165
+ return 0;
1166
+ }
1167
+ if (offset < 0) {
1168
+ offset += length;
1169
+ return offset > 0 ? offset : 0;
1170
+ }
1171
+ if (offset < length) {
1172
+ return offset;
1173
+ }
1174
+ return NumberIsNaN(offset) ? 0 : length;
1175
+ }
1176
+
1177
+ Buffer.prototype.subarray = function subarray(start, end) {
1178
+ const srcLength = this.length;
1179
+ start = adjustOffset(start, srcLength);
1180
+ end = end !== undefined ? adjustOffset(end, srcLength) : srcLength;
1181
+ const newLength = end > start ? end - start : 0;
1182
+ return new FastBuffer(this.buffer, this.byteOffset + start, newLength);
1183
+ };
1184
+
1185
+ Buffer.prototype.slice = function slice(start, end) {
1186
+ return this.subarray(start, end);
1187
+ };
1188
+
1189
+ function swap(b, n, m) {
1190
+ const i = b[n];
1191
+ b[n] = b[m];
1192
+ b[m] = i;
1193
+ }
1194
+
1195
+ Buffer.prototype.swap16 = function swap16() {
1196
+ // For Buffer.length < 128, it's generally faster to
1197
+ // do the swap in javascript. For larger buffers,
1198
+ // dropping down to the native code is faster.
1199
+ const len = this.length;
1200
+ if (len % 2 !== 0)
1201
+ throw new ERR_INVALID_BUFFER_SIZE('16-bits');
1202
+ if (len < 128) {
1203
+ for (let i = 0; i < len; i += 2)
1204
+ swap(this, i, i + 1);
1205
+ return this;
1206
+ }
1207
+ return _swap16(this);
1208
+ };
1209
+
1210
+ Buffer.prototype.swap32 = function swap32() {
1211
+ // For Buffer.length < 192, it's generally faster to
1212
+ // do the swap in javascript. For larger buffers,
1213
+ // dropping down to the native code is faster.
1214
+ const len = this.length;
1215
+ if (len % 4 !== 0)
1216
+ throw new ERR_INVALID_BUFFER_SIZE('32-bits');
1217
+ if (len < 192) {
1218
+ for (let i = 0; i < len; i += 4) {
1219
+ swap(this, i, i + 3);
1220
+ swap(this, i + 1, i + 2);
1221
+ }
1222
+ return this;
1223
+ }
1224
+ return _swap32(this);
1225
+ };
1226
+
1227
+ Buffer.prototype.swap64 = function swap64() {
1228
+ // For Buffer.length < 192, it's generally faster to
1229
+ // do the swap in javascript. For larger buffers,
1230
+ // dropping down to the native code is faster.
1231
+ const len = this.length;
1232
+ if (len % 8 !== 0)
1233
+ throw new ERR_INVALID_BUFFER_SIZE('64-bits');
1234
+ if (len < 192) {
1235
+ for (let i = 0; i < len; i += 8) {
1236
+ swap(this, i, i + 7);
1237
+ swap(this, i + 1, i + 6);
1238
+ swap(this, i + 2, i + 5);
1239
+ swap(this, i + 3, i + 4);
1240
+ }
1241
+ return this;
1242
+ }
1243
+ return _swap64(this);
1244
+ };
1245
+
1246
+ Buffer.prototype.toLocaleString = Buffer.prototype.toString;
1247
+
1248
+ let transcode;
1249
+ if (internalBinding('config').hasIntl) {
1250
+ const {
1251
+ icuErrName,
1252
+ transcode: _transcode,
1253
+ } = internalBinding('icu');
1254
+
1255
+ // Transcodes the Buffer from one encoding to another, returning a new
1256
+ // Buffer instance.
1257
+ transcode = function transcode(source, fromEncoding, toEncoding) {
1258
+ if (!isUint8Array(source)) {
1259
+ throw new ERR_INVALID_ARG_TYPE('source',
1260
+ ['Buffer', 'Uint8Array'], source);
1261
+ }
1262
+ if (source.length === 0) return Buffer.alloc(0);
1263
+
1264
+ fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding;
1265
+ toEncoding = normalizeEncoding(toEncoding) || toEncoding;
1266
+ const result = _transcode(source, fromEncoding, toEncoding);
1267
+ if (typeof result !== 'number')
1268
+ return result;
1269
+
1270
+ const code = icuErrName(result);
1271
+ const err = genericNodeError(
1272
+ `Unable to transcode Buffer [${code}]`,
1273
+ { code: code, errno: result },
1274
+ );
1275
+ throw err;
1276
+ };
1277
+ }
1278
+
1279
+ function btoa(input) {
1280
+ // The implementation here has not been performance optimized in any way and
1281
+ // should not be.
1282
+ // Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932
1283
+ if (arguments.length === 0) {
1284
+ throw new ERR_MISSING_ARGS('input');
1285
+ }
1286
+ const result = _btoa(`${input}`);
1287
+ if (result === -1) {
1288
+ throw lazyDOMException('Invalid character', 'InvalidCharacterError');
1289
+ }
1290
+ return result;
1291
+ }
1292
+
1293
+ function atob(input) {
1294
+ if (arguments.length === 0) {
1295
+ throw new ERR_MISSING_ARGS('input');
1296
+ }
1297
+
1298
+ const result = _atob(`${input}`);
1299
+
1300
+ switch (result) {
1301
+ case -2: // Invalid character
1302
+ throw lazyDOMException('Invalid character', 'InvalidCharacterError');
1303
+ case -1: // Single character remained
1304
+ throw lazyDOMException(
1305
+ 'The string to be decoded is not correctly encoded.',
1306
+ 'InvalidCharacterError');
1307
+ case -3: // Possible overflow
1308
+ // TODO(@anonrig): Throw correct error in here.
1309
+ throw lazyDOMException('The input causes overflow.', 'InvalidCharacterError');
1310
+ default:
1311
+ return result;
1312
+ }
1313
+ }
1314
+
1315
+ function isUtf8(input) {
1316
+ if (isTypedArray(input) || isAnyArrayBuffer(input)) {
1317
+ return bindingIsUtf8(input);
1318
+ }
1319
+
1320
+ throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
1321
+ }
1322
+
1323
+ function isAscii(input) {
1324
+ if (isTypedArray(input) || isAnyArrayBuffer(input)) {
1325
+ return bindingIsAscii(input);
1326
+ }
1327
+
1328
+ throw new ERR_INVALID_ARG_TYPE('input', ['ArrayBuffer', 'Buffer', 'TypedArray'], input);
1329
+ }
1330
+
1331
+ module.exports = {
1332
+ Buffer,
1333
+ SlowBuffer,
1334
+ transcode,
1335
+ isUtf8,
1336
+ isAscii,
1337
+
1338
+ // Legacy
1339
+ kMaxLength,
1340
+ kStringMaxLength,
1341
+ btoa,
1342
+ atob,
1343
+ };
1344
+
1345
+ ObjectDefineProperties(module.exports, {
1346
+ constants: {
1347
+ __proto__: null,
1348
+ configurable: false,
1349
+ enumerable: true,
1350
+ value: constants,
1351
+ },
1352
+ INSPECT_MAX_BYTES: {
1353
+ __proto__: null,
1354
+ configurable: true,
1355
+ enumerable: true,
1356
+ get() { return INSPECT_MAX_BYTES; },
1357
+ set(val) {
1358
+ validateNumber(val, 'INSPECT_MAX_BYTES', 0);
1359
+ INSPECT_MAX_BYTES = val;
1360
+ },
1361
+ },
1362
+ });
1363
+
1364
+ defineLazyProperties(
1365
+ module.exports,
1366
+ 'internal/blob',
1367
+ ['Blob', 'resolveObjectURL'],
1368
+ );
1369
+ defineLazyProperties(
1370
+ module.exports,
1371
+ 'internal/file',
1372
+ ['File'],
1373
+ );