@depup/webpack 5.106.1-depup.0 → 5.109.2-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (669) hide show
  1. package/README.md +9 -11
  2. package/bin/webpack.js +12 -0
  3. package/changes.json +16 -24
  4. package/hot/dev-server.js +2 -0
  5. package/lib/APIPlugin.js +60 -35
  6. package/lib/AsyncDependenciesBlock.js +3 -0
  7. package/lib/AutomaticPrefetchPlugin.js +6 -1
  8. package/lib/BannerPlugin.js +13 -5
  9. package/lib/Cache.js +27 -5
  10. package/lib/CacheFacade.js +41 -0
  11. package/lib/Chunk.js +71 -25
  12. package/lib/ChunkGraph.js +153 -19
  13. package/lib/ChunkGroup.js +115 -28
  14. package/lib/ChunkTemplate.js +9 -0
  15. package/lib/CircularModulesPlugin.js +190 -0
  16. package/lib/CleanPlugin.js +27 -28
  17. package/lib/CodeGenerationResults.js +19 -0
  18. package/lib/CompatibilityPlugin.js +85 -37
  19. package/lib/Compilation.js +656 -112
  20. package/lib/Compiler.js +109 -35
  21. package/lib/ConcatenationScope.js +34 -4
  22. package/lib/ConditionalInitFragment.js +6 -0
  23. package/lib/ConstPlugin.js +15 -12
  24. package/lib/ContextExclusionPlugin.js +3 -1
  25. package/lib/ContextModule.js +345 -55
  26. package/lib/ContextModuleFactory.js +193 -51
  27. package/lib/ContextReplacementPlugin.js +8 -1
  28. package/lib/DefinePlugin.js +537 -139
  29. package/lib/DependenciesBlock.js +6 -1
  30. package/lib/Dependency.js +159 -11
  31. package/lib/DependencyTemplate.js +7 -1
  32. package/lib/DependencyTemplates.js +4 -0
  33. package/lib/DotenvPlugin.js +3 -0
  34. package/lib/DynamicEntryPlugin.js +3 -1
  35. package/lib/EntryOptionPlugin.js +42 -2
  36. package/lib/EntryPlugin.js +4 -1
  37. package/lib/Entrypoint.js +5 -1
  38. package/lib/EnvironmentPlugin.js +3 -2
  39. package/lib/ErrorHelpers.js +7 -0
  40. package/lib/EvalDevToolModulePlugin.js +3 -1
  41. package/lib/EvalSourceMapDevToolPlugin.js +11 -11
  42. package/lib/ExportsInfo.js +421 -113
  43. package/lib/ExportsInfoApiPlugin.js +4 -3
  44. package/lib/ExternalModule.js +381 -80
  45. package/lib/ExternalModuleFactoryPlugin.js +75 -3
  46. package/lib/ExternalsPlugin.js +3 -1
  47. package/lib/FileSystemInfo.js +701 -175
  48. package/lib/FlagAllModulesAsUsedPlugin.js +3 -6
  49. package/lib/FlagDependencyExportsPlugin.js +82 -9
  50. package/lib/FlagDependencyUsagePlugin.js +115 -8
  51. package/lib/FlagEntryExportAsUsedPlugin.js +4 -1
  52. package/lib/Generator.js +24 -3
  53. package/lib/HotModuleReplacementPlugin.js +110 -44
  54. package/lib/IgnorePlugin.js +5 -2
  55. package/lib/IgnoreWarningsPlugin.js +2 -1
  56. package/lib/InitFragment.js +33 -30
  57. package/lib/JavascriptMetaInfoPlugin.js +9 -6
  58. package/lib/LazyBarrel.js +389 -0
  59. package/lib/LibraryTemplatePlugin.js +2 -1
  60. package/lib/LoaderOptionsPlugin.js +3 -1
  61. package/lib/LoaderTargetPlugin.js +3 -1
  62. package/lib/MainTemplate.js +15 -0
  63. package/lib/ManifestPlugin.js +9 -2
  64. package/lib/Module.js +121 -57
  65. package/lib/ModuleFactory.js +6 -1
  66. package/lib/ModuleFilenameHelpers.js +18 -4
  67. package/lib/ModuleGraph.js +69 -2
  68. package/lib/ModuleGraphConnection.js +9 -0
  69. package/lib/ModuleInfoHeaderPlugin.js +5 -0
  70. package/lib/ModuleNotFoundError.js +3 -83
  71. package/lib/ModuleProfile.js +27 -1
  72. package/lib/ModuleSourceTypeConstants.js +52 -19
  73. package/lib/ModuleTemplate.js +10 -0
  74. package/lib/ModuleTypeConstants.js +20 -4
  75. package/lib/MultiCompiler.js +59 -4
  76. package/lib/MultiStats.js +9 -0
  77. package/lib/MultiWatching.js +6 -0
  78. package/lib/NoEmitOnErrorsPlugin.js +1 -1
  79. package/lib/NodeStuffPlugin.js +78 -46
  80. package/lib/NormalModule.js +519 -206
  81. package/lib/NormalModuleFactory.js +156 -31
  82. package/lib/NormalModuleReplacementPlugin.js +3 -1
  83. package/lib/NullFactory.js +1 -0
  84. package/lib/OptionsApply.js +1 -0
  85. package/lib/Parser.js +3 -1
  86. package/lib/PlatformPlugin.js +2 -1
  87. package/lib/PrefetchPlugin.js +4 -1
  88. package/lib/ProgressPlugin.js +351 -161
  89. package/lib/ProvidePlugin.js +6 -3
  90. package/lib/RawModule.js +32 -16
  91. package/lib/RecordIdsPlugin.js +9 -0
  92. package/lib/RequestShortener.js +8 -0
  93. package/lib/ResolverFactory.js +5 -0
  94. package/lib/RuntimeGlobals.js +58 -5
  95. package/lib/RuntimeModule.js +22 -7
  96. package/lib/RuntimePlugin.js +123 -118
  97. package/lib/RuntimeTemplate.js +616 -37
  98. package/lib/SelfModuleFactory.js +3 -0
  99. package/lib/SourceMapDevToolModuleOptionsPlugin.js +2 -0
  100. package/lib/SourceMapDevToolPlugin.js +341 -63
  101. package/lib/Stats.js +6 -0
  102. package/lib/Template.js +52 -6
  103. package/lib/TemplatedPathPlugin.js +505 -138
  104. package/lib/UseStrictPlugin.js +3 -2
  105. package/lib/WarnCaseSensitiveModulesPlugin.js +72 -3
  106. package/lib/WarnDeprecatedOptionPlugin.js +6 -2
  107. package/lib/WarnNoModeSetPlugin.js +18 -2
  108. package/lib/WatchIgnorePlugin.js +5 -1
  109. package/lib/Watching.js +25 -3
  110. package/lib/WebpackError.js +3 -74
  111. package/lib/WebpackIsIncludedPlugin.js +4 -2
  112. package/lib/WebpackOptionsApply.js +88 -25
  113. package/lib/WebpackOptionsDefaulter.js +1 -0
  114. package/lib/asset/AssetBytesGenerator.js +28 -10
  115. package/lib/asset/AssetBytesParser.js +1 -0
  116. package/lib/asset/AssetGenerator.js +199 -60
  117. package/lib/asset/AssetModule.js +47 -0
  118. package/lib/asset/AssetModulesPlugin.js +115 -24
  119. package/lib/asset/AssetParser.js +4 -2
  120. package/lib/asset/AssetSourceGenerator.js +26 -8
  121. package/lib/asset/AssetSourceParser.js +1 -0
  122. package/lib/asset/RawDataUrlModule.js +23 -13
  123. package/lib/asset/WebManifestGenerator.js +164 -0
  124. package/lib/asset/WebManifestParser.js +130 -0
  125. package/lib/async-modules/AsyncModuleHelpers.js +1 -0
  126. package/lib/async-modules/AwaitDependenciesInitFragment.js +18 -4
  127. package/lib/async-modules/InferAsyncModulesPlugin.js +1 -1
  128. package/lib/async-modules/isGeneratorLowered.js +26 -0
  129. package/lib/buildChunkGraph.js +107 -15
  130. package/lib/bun/BunTargetPlugin.js +42 -0
  131. package/lib/cache/AddBuildDependenciesPlugin.js +3 -1
  132. package/lib/cache/AddManagedPathsPlugin.js +5 -1
  133. package/lib/cache/IdleFileCachePlugin.js +6 -1
  134. package/lib/cache/MemoryCachePlugin.js +1 -1
  135. package/lib/cache/MemoryWithGcCachePlugin.js +4 -1
  136. package/lib/cache/PackFileCacheStrategy.js +302 -12
  137. package/lib/cache/ResolverCachePlugin.js +12 -1
  138. package/lib/cache/getLazyHashedEtag.js +13 -2
  139. package/lib/cache/mergeEtags.js +4 -0
  140. package/lib/cli.js +142 -20
  141. package/lib/config/browserslistTargetHandler.js +141 -0
  142. package/lib/config/defaults.js +612 -43
  143. package/lib/config/defineConfig.js +31 -0
  144. package/lib/config/normalization.js +46 -1
  145. package/lib/config/target.js +200 -2
  146. package/lib/container/ContainerEntryDependency.js +2 -0
  147. package/lib/container/ContainerEntryModule.js +27 -14
  148. package/lib/container/ContainerEntryModuleFactory.js +1 -0
  149. package/lib/container/ContainerExposedDependency.js +6 -2
  150. package/lib/container/ContainerPlugin.js +2 -1
  151. package/lib/container/ContainerReferencePlugin.js +3 -2
  152. package/lib/container/FallbackDependency.js +9 -7
  153. package/lib/container/FallbackItemDependency.js +1 -0
  154. package/lib/container/FallbackModule.js +22 -9
  155. package/lib/container/FallbackModuleFactory.js +1 -0
  156. package/lib/container/HoistContainerReferencesPlugin.js +47 -55
  157. package/lib/container/ModuleFederationPlugin.js +21 -29
  158. package/lib/container/RemoteModule.js +33 -10
  159. package/lib/container/RemoteRuntimeModule.js +15 -12
  160. package/lib/container/RemoteToExternalDependency.js +1 -0
  161. package/lib/container/options.js +7 -0
  162. package/lib/css/CssGenerator.js +540 -272
  163. package/lib/css/CssInjectStyleRuntimeModule.js +86 -70
  164. package/lib/css/CssLoadingRuntimeModule.js +173 -81
  165. package/lib/{CssModule.js → css/CssModule.js} +72 -37
  166. package/lib/css/CssModulesPlugin.js +371 -208
  167. package/lib/css/CssParser.js +3538 -1963
  168. package/lib/css/syntax.js +4155 -0
  169. package/lib/debug/ProfilingPlugin.js +33 -2
  170. package/lib/deno/DenoTargetPlugin.js +41 -0
  171. package/lib/dependencies/AMDDefineDependency.js +29 -17
  172. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +19 -6
  173. package/lib/dependencies/AMDPlugin.js +6 -3
  174. package/lib/dependencies/AMDRequireArrayDependency.js +13 -11
  175. package/lib/dependencies/AMDRequireContextDependency.js +10 -11
  176. package/lib/dependencies/AMDRequireDependenciesBlock.js +1 -0
  177. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +28 -14
  178. package/lib/dependencies/AMDRequireDependency.js +28 -20
  179. package/lib/dependencies/AMDRequireItemDependency.js +1 -0
  180. package/lib/dependencies/AMDRuntimeModules.js +3 -0
  181. package/lib/dependencies/CachedConstDependency.js +9 -1
  182. package/lib/dependencies/CommonJsDependencyHelpers.js +104 -0
  183. package/lib/dependencies/CommonJsExportRequireDependency.js +157 -38
  184. package/lib/dependencies/CommonJsExportsDependency.js +74 -20
  185. package/lib/dependencies/CommonJsExportsParserPlugin.js +246 -15
  186. package/lib/dependencies/CommonJsFullRequireDependency.js +91 -27
  187. package/lib/dependencies/CommonJsImportsParserPlugin.js +189 -24
  188. package/lib/dependencies/CommonJsPlugin.js +7 -4
  189. package/lib/dependencies/CommonJsRequireContextDependency.js +13 -13
  190. package/lib/dependencies/CommonJsRequireDependency.js +135 -13
  191. package/lib/dependencies/CommonJsSelfReferenceDependency.js +60 -16
  192. package/lib/dependencies/ConstDependency.js +22 -12
  193. package/lib/dependencies/ContextDependency.js +13 -2
  194. package/lib/dependencies/ContextDependencyHelpers.js +7 -4
  195. package/lib/dependencies/ContextDependencyTemplateAsId.js +2 -1
  196. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +1 -0
  197. package/lib/dependencies/ContextElementDependency.js +27 -14
  198. package/lib/dependencies/CreateRequireParserPlugin.js +14 -7
  199. package/lib/dependencies/CreateScriptUrlDependency.js +9 -7
  200. package/lib/dependencies/CriticalDependencyWarning.js +3 -1
  201. package/lib/dependencies/CssIcssExportDependency.js +681 -367
  202. package/lib/dependencies/CssIcssImportDependency.js +62 -17
  203. package/lib/dependencies/CssIcssSymbolDependency.js +36 -17
  204. package/lib/dependencies/CssImportDependency.js +37 -1
  205. package/lib/dependencies/CssUrlDependency.js +87 -14
  206. package/lib/dependencies/DelegatedSourceDependency.js +1 -0
  207. package/lib/dependencies/DllEntryDependency.js +12 -13
  208. package/lib/dependencies/DynamicExports.js +5 -0
  209. package/lib/dependencies/EntryDependency.js +1 -0
  210. package/lib/dependencies/ExportBindingInitFragment.js +164 -0
  211. package/lib/dependencies/ExportsInfoDependency.js +18 -12
  212. package/lib/dependencies/ExternalModuleDependency.js +13 -5
  213. package/lib/dependencies/ExternalModuleInitFragment.js +10 -5
  214. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +19 -12
  215. package/lib/dependencies/HarmonyAcceptDependency.js +24 -26
  216. package/lib/dependencies/HarmonyAcceptImportDependency.js +2 -0
  217. package/lib/dependencies/HarmonyCompatibilityDependency.js +33 -14
  218. package/lib/dependencies/HarmonyDetectionParserPlugin.js +108 -30
  219. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +34 -3
  220. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +70 -33
  221. package/lib/dependencies/HarmonyExportExpressionDependency.js +133 -43
  222. package/lib/dependencies/HarmonyExportHeaderDependency.js +11 -9
  223. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +268 -44
  224. package/lib/dependencies/HarmonyExportInitFragment.js +28 -20
  225. package/lib/dependencies/HarmonyExportSpecifierDependency.js +88 -18
  226. package/lib/dependencies/HarmonyExports.js +6 -1
  227. package/lib/dependencies/HarmonyImportDependency.js +60 -4
  228. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +139 -146
  229. package/lib/dependencies/HarmonyImportGuard.js +427 -0
  230. package/lib/dependencies/HarmonyImportSideEffectDependency.js +22 -1
  231. package/lib/dependencies/HarmonyImportSpecifierDependency.js +146 -24
  232. package/lib/{HarmonyLinkingError.js → dependencies/HarmonyLinkingError.js} +6 -3
  233. package/lib/dependencies/HarmonyModulesPlugin.js +11 -1
  234. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +2 -1
  235. package/lib/dependencies/HtmlEntryDependency.js +1295 -0
  236. package/lib/dependencies/HtmlInlineHtmlDependency.js +107 -0
  237. package/lib/dependencies/HtmlInlineScriptDependency.js +126 -0
  238. package/lib/dependencies/HtmlInlineStyleDependency.js +152 -0
  239. package/lib/dependencies/HtmlSourceDependency.js +172 -0
  240. package/lib/dependencies/ImportContextDependency.js +9 -9
  241. package/lib/dependencies/ImportDependency.js +92 -3
  242. package/lib/dependencies/ImportEagerDependency.js +2 -0
  243. package/lib/dependencies/ImportMetaContextDependency.js +1 -0
  244. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +35 -19
  245. package/lib/dependencies/ImportMetaContextPlugin.js +40 -8
  246. package/lib/dependencies/ImportMetaGlobDependency.js +81 -0
  247. package/lib/dependencies/ImportMetaGlobDependencyParserPlugin.js +78 -0
  248. package/lib/dependencies/ImportMetaGlobHelpers.js +660 -0
  249. package/lib/dependencies/ImportMetaHotAcceptDependency.js +2 -0
  250. package/lib/dependencies/ImportMetaHotDeclineDependency.js +2 -0
  251. package/lib/dependencies/ImportMetaPlugin.js +595 -209
  252. package/lib/dependencies/ImportMetaResolveDependency.js +90 -0
  253. package/lib/dependencies/ImportParserPlugin.js +114 -27
  254. package/lib/dependencies/ImportPhase.js +17 -11
  255. package/lib/dependencies/ImportPlugin.js +2 -1
  256. package/lib/dependencies/ImportWeakDependency.js +3 -0
  257. package/lib/dependencies/JsonExportsDependency.js +15 -10
  258. package/lib/dependencies/LoaderDependency.js +2 -0
  259. package/lib/dependencies/LoaderImportDependency.js +3 -0
  260. package/lib/dependencies/LoaderPlugin.js +9 -2
  261. package/lib/dependencies/LocalModule.js +15 -12
  262. package/lib/dependencies/LocalModuleDependency.js +15 -13
  263. package/lib/dependencies/LocalModulesHelpers.js +3 -0
  264. package/lib/dependencies/ModuleDecoratorDependency.js +16 -10
  265. package/lib/dependencies/ModuleDependency.js +14 -0
  266. package/lib/dependencies/ModuleDependencyTemplateAsId.js +1 -0
  267. package/lib/dependencies/ModuleDependencyTemplateAsRequireId.js +1 -0
  268. package/lib/dependencies/ModuleHotAcceptDependency.js +2 -0
  269. package/lib/dependencies/ModuleHotDeclineDependency.js +2 -0
  270. package/lib/dependencies/ModuleInitFragmentDependency.js +19 -11
  271. package/lib/dependencies/NullDependency.js +2 -0
  272. package/lib/dependencies/PrefetchDependency.js +1 -0
  273. package/lib/dependencies/ProvidedDependency.js +36 -27
  274. package/lib/dependencies/PureExpressionDependency.js +14 -10
  275. package/lib/dependencies/RequireContextDependency.js +1 -0
  276. package/lib/dependencies/RequireContextDependencyParserPlugin.js +2 -1
  277. package/lib/dependencies/RequireContextPlugin.js +2 -1
  278. package/lib/dependencies/RequireEnsureDependenciesBlock.js +1 -0
  279. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +4 -6
  280. package/lib/dependencies/RequireEnsureDependency.js +16 -13
  281. package/lib/dependencies/RequireEnsureItemDependency.js +1 -0
  282. package/lib/dependencies/RequireEnsurePlugin.js +2 -1
  283. package/lib/dependencies/RequireHeaderDependency.js +7 -4
  284. package/lib/dependencies/RequireIncludeDependency.js +2 -0
  285. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +11 -12
  286. package/lib/dependencies/RequireIncludePlugin.js +2 -1
  287. package/lib/{RequireJsStuffPlugin.js → dependencies/RequireJsStuffPlugin.js} +9 -8
  288. package/lib/dependencies/RequireResolveContextDependency.js +10 -11
  289. package/lib/dependencies/RequireResolveDependency.js +2 -0
  290. package/lib/dependencies/RequireResolveHeaderDependency.js +8 -5
  291. package/lib/dependencies/RuntimeRequirementsDependency.js +11 -8
  292. package/lib/dependencies/StaticExportsDependency.js +13 -10
  293. package/lib/dependencies/SystemPlugin.js +9 -6
  294. package/lib/dependencies/SystemRuntimeModule.js +1 -0
  295. package/lib/dependencies/TopLevelAwaitDependency.js +93 -0
  296. package/lib/dependencies/URLContextDependency.js +8 -7
  297. package/lib/dependencies/URLDependency.js +157 -33
  298. package/lib/dependencies/URLPlugin.js +2 -0
  299. package/lib/dependencies/UnsupportedDependency.js +12 -13
  300. package/lib/dependencies/WebAssemblyExportImportedDependency.js +15 -16
  301. package/lib/dependencies/WebAssemblyImportDependency.js +19 -18
  302. package/lib/dependencies/WebpackIsIncludedDependency.js +3 -0
  303. package/lib/dependencies/{WorkerPlugin.js → WorkerAndWorkletPlugin.js} +364 -114
  304. package/lib/dependencies/WorkerDependency.js +111 -27
  305. package/lib/dependencies/WorkletDependency.js +278 -0
  306. package/lib/dependencies/getFunctionExpression.js +1 -0
  307. package/lib/dependencies/processExportInfo.js +14 -11
  308. package/lib/{DelegatedModule.js → dll/DelegatedModule.js} +71 -44
  309. package/lib/{DelegatedModuleFactoryPlugin.js → dll/DelegatedModuleFactoryPlugin.js} +27 -4
  310. package/lib/{DelegatedPlugin.js → dll/DelegatedPlugin.js} +5 -3
  311. package/lib/{DllEntryPlugin.js → dll/DllEntryPlugin.js} +9 -5
  312. package/lib/{DllModule.js → dll/DllModule.js} +36 -24
  313. package/lib/{DllModuleFactory.js → dll/DllModuleFactory.js} +5 -4
  314. package/lib/{DllPlugin.js → dll/DllPlugin.js} +24 -6
  315. package/lib/{DllReferencePlugin.js → dll/DllReferencePlugin.js} +26 -18
  316. package/lib/{LibManifestPlugin.js → dll/LibManifestPlugin.js} +14 -10
  317. package/lib/electron/ElectronTargetPlugin.js +24 -5
  318. package/lib/{AbstractMethodError.js → errors/AbstractMethodError.js} +10 -1
  319. package/lib/{AsyncDependencyToInitialChunkError.js → errors/AsyncDependencyToInitialChunkError.js} +8 -3
  320. package/lib/errors/BuildCycleError.js +1 -1
  321. package/lib/{ChunkRenderError.js → errors/ChunkRenderError.js} +1 -1
  322. package/lib/{CodeGenerationError.js → errors/CodeGenerationError.js} +1 -1
  323. package/lib/{CommentCompilationWarning.js → errors/CommentCompilationWarning.js} +9 -3
  324. package/lib/{ConcurrentCompilationError.js → errors/ConcurrentCompilationError.js} +5 -2
  325. package/lib/{EnvironmentNotSupportAsyncWarning.js → errors/EnvironmentNotSupportAsyncWarning.js} +5 -5
  326. package/lib/{HookWebpackError.js → errors/HookWebpackError.js} +20 -16
  327. package/lib/{IgnoreErrorModuleFactory.js → errors/IgnoreErrorModuleFactory.js} +7 -4
  328. package/lib/{InvalidDependenciesModuleWarning.js → errors/InvalidDependenciesModuleWarning.js} +6 -3
  329. package/lib/errors/JSONParseError.js +115 -0
  330. package/lib/errors/LoaderLoadingError.js +20 -0
  331. package/lib/{ModuleBuildError.js → errors/ModuleBuildError.js} +22 -16
  332. package/lib/{ModuleDependencyError.js → errors/ModuleDependencyError.js} +10 -3
  333. package/lib/{ModuleDependencyWarning.js → errors/ModuleDependencyWarning.js} +13 -5
  334. package/lib/{ModuleError.js → errors/ModuleError.js} +10 -14
  335. package/lib/{ModuleHashingError.js → errors/ModuleHashingError.js} +5 -1
  336. package/lib/errors/ModuleNotFoundError.js +94 -0
  337. package/lib/errors/ModuleParseError.js +141 -0
  338. package/lib/{ModuleRestoreError.js → errors/ModuleRestoreError.js} +4 -1
  339. package/lib/{ModuleStoreError.js → errors/ModuleStoreError.js} +5 -1
  340. package/lib/{ModuleWarning.js → errors/ModuleWarning.js} +13 -14
  341. package/lib/{NodeStuffInWebError.js → errors/NodeStuffInWebError.js} +7 -5
  342. package/lib/errors/NonErrorEmittedError.js +30 -0
  343. package/lib/{UnhandledSchemeError.js → errors/UnhandledSchemeError.js} +9 -2
  344. package/lib/{UnsupportedFeatureWarning.js → errors/UnsupportedFeatureWarning.js} +4 -3
  345. package/lib/errors/WebpackError.js +84 -0
  346. package/lib/esm/ExportWebpackRequireRuntimeModule.js +2 -0
  347. package/lib/esm/ModuleChunkFormatPlugin.js +52 -15
  348. package/lib/esm/ModuleChunkLoadingPlugin.js +11 -3
  349. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +80 -52
  350. package/lib/hmr/HotModuleReplacement.runtime.js +175 -30
  351. package/lib/hmr/HotModuleReplacementRuntimeModule.js +8 -0
  352. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +173 -26
  353. package/lib/hmr/JavascriptHotModuleReplacementHelper.js +1 -0
  354. package/lib/hmr/LazyCompilationPlugin.js +154 -7
  355. package/lib/hmr/lazyCompilationBackend.js +53 -12
  356. package/lib/html/HtmlGenerator.js +1490 -0
  357. package/lib/html/HtmlModule.js +41 -0
  358. package/lib/html/HtmlModulesPlugin.js +1129 -0
  359. package/lib/html/HtmlParser.js +1970 -0
  360. package/lib/html/favicon.svg +1 -0
  361. package/lib/html/syntax.js +9106 -0
  362. package/lib/ids/ChunkModuleIdRangePlugin.js +3 -1
  363. package/lib/ids/DeterministicChunkIdsPlugin.js +3 -1
  364. package/lib/ids/DeterministicModuleIdsPlugin.js +3 -1
  365. package/lib/ids/HashedModuleIdsPlugin.js +2 -1
  366. package/lib/ids/IdHelpers.js +49 -12
  367. package/lib/ids/NamedChunkIdsPlugin.js +3 -1
  368. package/lib/ids/NamedModuleIdsPlugin.js +3 -1
  369. package/lib/ids/NaturalChunkIdsPlugin.js +1 -1
  370. package/lib/ids/NaturalModuleIdsPlugin.js +1 -1
  371. package/lib/ids/OccurrenceChunkIdsPlugin.js +2 -1
  372. package/lib/ids/OccurrenceModuleIdsPlugin.js +4 -1
  373. package/lib/ids/SyncModuleIdsPlugin.js +3 -1
  374. package/lib/index.js +66 -15
  375. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +5 -6
  376. package/lib/javascript/BasicEvaluatedExpression.js +370 -49
  377. package/lib/javascript/ChunkFormatHelpers.js +2 -1
  378. package/lib/javascript/ChunkHelpers.js +1 -0
  379. package/lib/javascript/CommonJsChunkFormatPlugin.js +1 -1
  380. package/lib/javascript/EnableChunkLoadingPlugin.js +6 -1
  381. package/lib/javascript/JavascriptGenerator.js +213 -51
  382. package/lib/javascript/JavascriptModule.js +54 -0
  383. package/lib/javascript/JavascriptModulesPlugin.js +628 -198
  384. package/lib/javascript/JavascriptParser.js +1334 -471
  385. package/lib/javascript/JavascriptParserHelpers.js +10 -7
  386. package/lib/javascript/StartupHelpers.js +5 -0
  387. package/lib/javascript/syntax.js +5037 -0
  388. package/lib/json/JsonData.js +5 -0
  389. package/lib/json/JsonGenerator.js +21 -0
  390. package/lib/json/JsonModule.js +40 -0
  391. package/lib/json/JsonModulesPlugin.js +8 -1
  392. package/lib/json/JsonParser.js +18 -27
  393. package/lib/library/AbstractLibraryPlugin.js +17 -2
  394. package/lib/library/AmdLibraryPlugin.js +8 -0
  395. package/lib/library/AssignLibraryPlugin.js +21 -3
  396. package/lib/library/EnableLibraryPlugin.js +8 -2
  397. package/lib/library/ExportPropertyLibraryPlugin.js +10 -0
  398. package/lib/{FalseIIFEUmdWarning.js → library/FalseIIFEUmdWarning.js} +2 -1
  399. package/lib/library/JsonpLibraryPlugin.js +8 -0
  400. package/lib/library/ModuleLibraryPlugin.js +154 -7
  401. package/lib/library/SystemLibraryPlugin.js +11 -3
  402. package/lib/library/UmdLibraryPlugin.js +16 -0
  403. package/lib/loaders/LoaderRunner.js +714 -0
  404. package/lib/loaders/loadLoader.js +100 -0
  405. package/lib/logging/Logger.js +17 -0
  406. package/lib/logging/createConsoleLogger.js +7 -0
  407. package/lib/logging/runtime.js +2 -0
  408. package/lib/logging/truncateArgs.js +2 -0
  409. package/lib/node/CommonJsChunkLoadingPlugin.js +5 -1
  410. package/lib/node/NodeEnvironmentPlugin.js +7 -3
  411. package/lib/node/NodeSourcePlugin.js +1 -1
  412. package/lib/node/NodeTargetPlugin.js +4 -63
  413. package/lib/node/NodeTemplatePlugin.js +3 -1
  414. package/lib/node/NodeWatchFileSystem.js +40 -22
  415. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +13 -9
  416. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +33 -3
  417. package/lib/node/ReadFileCompileWasmPlugin.js +28 -3
  418. package/lib/node/RequireChunkLoadingRuntimeModule.js +21 -16
  419. package/lib/node/nodeBuiltins.js +80 -0
  420. package/lib/node/nodeConsole.js +116 -64
  421. package/lib/optimize/AggressiveMergingPlugin.js +22 -16
  422. package/lib/optimize/AggressiveSplittingPlugin.js +6 -1
  423. package/lib/optimize/ConcatenatedModule.js +603 -130
  424. package/lib/optimize/ConstExportsPlugin.js +211 -0
  425. package/lib/optimize/EnsureChunkConditionsPlugin.js +2 -1
  426. package/lib/optimize/FlagIncludedChunksPlugin.js +15 -4
  427. package/lib/optimize/InlineExports.js +178 -0
  428. package/lib/optimize/InnerGraph.js +374 -262
  429. package/lib/optimize/InnerGraphPlugin.js +246 -117
  430. package/lib/optimize/LimitChunkCountPlugin.js +9 -0
  431. package/lib/optimize/MangleExportsPlugin.js +5 -1
  432. package/lib/optimize/MergeDuplicateChunksPlugin.js +2 -0
  433. package/lib/optimize/MinChunkSizePlugin.js +2 -1
  434. package/lib/optimize/MinMaxSizeWarning.js +5 -4
  435. package/lib/optimize/ModuleConcatenationPlugin.js +229 -55
  436. package/lib/optimize/RealContentHashPlugin.js +130 -52
  437. package/lib/optimize/RemoveEmptyChunksPlugin.js +2 -1
  438. package/lib/optimize/RemoveParentModulesPlugin.js +3 -1
  439. package/lib/optimize/RuntimeChunkPlugin.js +2 -1
  440. package/lib/optimize/SideEffectsFlagPlugin.js +381 -55
  441. package/lib/optimize/SplitChunksPlugin.js +200 -50
  442. package/lib/performance/AssetsOverSizeLimitWarning.js +3 -2
  443. package/lib/performance/EntrypointsOverSizeLimitWarning.js +3 -2
  444. package/lib/performance/NoAsyncChunksWarning.js +5 -3
  445. package/lib/performance/SizeLimitsPlugin.js +8 -2
  446. package/lib/prefetch/ChunkPrefetchFunctionRuntimeModule.js +1 -0
  447. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +21 -0
  448. package/lib/prefetch/ChunkPrefetchStartupRuntimeModule.js +1 -0
  449. package/lib/prefetch/ChunkPrefetchTriggerRuntimeModule.js +5 -1
  450. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +25 -13
  451. package/lib/prefetch/ResourceHintPlugin.js +687 -0
  452. package/lib/prefetch/ResourceHintRuntimeModule.js +149 -0
  453. package/lib/prefetch/StartupAssetHintRuntimeModule.js +210 -0
  454. package/lib/prefetch/parseResourceHintOptions.js +103 -0
  455. package/lib/rules/BasicEffectRulePlugin.js +3 -0
  456. package/lib/rules/BasicMatcherRulePlugin.js +3 -0
  457. package/lib/rules/ObjectMatcherRulePlugin.js +3 -0
  458. package/lib/rules/RuleSetCompiler.js +132 -0
  459. package/lib/rules/UseEffectRulePlugin.js +10 -3
  460. package/lib/runtime/AsyncModuleGeneratorRuntimeModule.js +56 -0
  461. package/lib/runtime/AsyncModuleRuntimeModule.js +52 -33
  462. package/lib/runtime/AutoPublicPathRuntimeModule.js +22 -9
  463. package/lib/runtime/BaseUriRuntimeModule.js +1 -0
  464. package/lib/runtime/ChunkNameRuntimeModule.js +1 -0
  465. package/lib/runtime/CommonJsWrapRuntimeModule.js +37 -0
  466. package/lib/runtime/CompatGetDefaultExportRuntimeModule.js +2 -1
  467. package/lib/runtime/CompatRuntimeModule.js +2 -0
  468. package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +7 -4
  469. package/lib/runtime/CreateScriptRuntimeModule.js +1 -0
  470. package/lib/runtime/CreateScriptUrlRuntimeModule.js +1 -0
  471. package/lib/runtime/DefinePropertyGettersRuntimeModule.js +34 -4
  472. package/lib/runtime/EnsureChunkRuntimeModule.js +1 -0
  473. package/lib/runtime/GetChunkFilenameRuntimeModule.js +88 -8
  474. package/lib/runtime/GetFullHashRuntimeModule.js +1 -0
  475. package/lib/runtime/GetMainFilenameRuntimeModule.js +1 -0
  476. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +2 -1
  477. package/lib/runtime/GetWorkletBootstrapRuntimeModule.js +59 -0
  478. package/lib/runtime/GlobalRuntimeModule.js +1 -0
  479. package/lib/runtime/HasOwnPropertyRuntimeModule.js +2 -1
  480. package/lib/runtime/HelperRuntimeModule.js +5 -0
  481. package/lib/runtime/LoadScriptRuntimeModule.js +28 -37
  482. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +354 -37
  483. package/lib/runtime/MakeNamespaceObjectRuntimeModule.js +2 -1
  484. package/lib/runtime/NonceRuntimeModule.js +1 -0
  485. package/lib/runtime/OnChunksLoadedRuntimeModule.js +5 -4
  486. package/lib/runtime/PublicPathRuntimeModule.js +1 -0
  487. package/lib/runtime/RelativeUrlRuntimeModule.js +4 -2
  488. package/lib/runtime/RuntimeIdRuntimeModule.js +1 -0
  489. package/lib/runtime/SetAnonymousDefaultNameRuntimeModule.js +38 -0
  490. package/lib/runtime/StartupChunkDependenciesPlugin.js +13 -1
  491. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +2 -1
  492. package/lib/runtime/StartupEntrypointRuntimeModule.js +5 -3
  493. package/lib/runtime/SystemContextRuntimeModule.js +1 -0
  494. package/lib/runtime/ToBinaryRuntimeModule.js +1 -0
  495. package/lib/runtime/WorkerRuntimeModule.js +33 -0
  496. package/lib/schemes/DataUriPlugin.js +14 -2
  497. package/lib/schemes/FileUriPlugin.js +1 -1
  498. package/lib/schemes/HttpUriPlugin.js +86 -4
  499. package/lib/schemes/VirtualUrlPlugin.js +8 -4
  500. package/lib/serialization/AggregateErrorSerializer.js +9 -6
  501. package/lib/serialization/ArraySerializer.js +6 -8
  502. package/lib/serialization/BinaryMiddleware.js +427 -1006
  503. package/lib/serialization/DateObjectSerializer.js +4 -2
  504. package/lib/serialization/ErrorObjectSerializer.js +10 -6
  505. package/lib/serialization/FileMiddleware.js +345 -59
  506. package/lib/serialization/MapObjectSerializer.js +7 -9
  507. package/lib/serialization/NullPrototypeObjectSerializer.js +9 -9
  508. package/lib/serialization/ObjectMiddleware.js +73 -13
  509. package/lib/serialization/PlainObjectSerializer.js +16 -8
  510. package/lib/serialization/RegExpObjectSerializer.js +4 -2
  511. package/lib/serialization/Serializer.js +6 -0
  512. package/lib/serialization/SerializerMiddleware.js +14 -2
  513. package/lib/serialization/SetObjectSerializer.js +6 -8
  514. package/lib/serialization/SingleItemMiddleware.js +3 -0
  515. package/lib/sharing/ConsumeSharedFallbackDependency.js +1 -0
  516. package/lib/sharing/ConsumeSharedModule.js +21 -7
  517. package/lib/sharing/ConsumeSharedPlugin.js +11 -11
  518. package/lib/sharing/ConsumeSharedRuntimeModule.js +55 -45
  519. package/lib/sharing/ProvideForSharedDependency.js +1 -0
  520. package/lib/sharing/ProvideSharedDependency.js +34 -15
  521. package/lib/sharing/ProvideSharedModule.js +42 -12
  522. package/lib/sharing/ProvideSharedModuleFactory.js +1 -0
  523. package/lib/sharing/ProvideSharedPlugin.js +13 -8
  524. package/lib/sharing/SharePlugin.js +5 -1
  525. package/lib/sharing/ShareRuntimeModule.js +19 -16
  526. package/lib/sharing/resolveMatchedConfigs.js +6 -2
  527. package/lib/sharing/utils.js +9 -1
  528. package/lib/stats/DefaultStatsFactoryPlugin.js +289 -201
  529. package/lib/stats/DefaultStatsPresetPlugin.js +14 -2
  530. package/lib/stats/DefaultStatsPrinterPlugin.js +74 -170
  531. package/lib/stats/StatsFactory.js +70 -27
  532. package/lib/stats/StatsPrinter.js +16 -2
  533. package/lib/typescript/TypeScriptPlugin.js +210 -0
  534. package/lib/url/URLParserPlugin.js +111 -23
  535. package/lib/util/ArrayHelpers.js +1 -0
  536. package/lib/util/ArrayQueue.js +10 -5
  537. package/lib/util/AsyncQueue.js +154 -74
  538. package/lib/util/Hash.js +2 -2
  539. package/lib/util/IterableHelpers.js +3 -0
  540. package/lib/util/LazyBucketSortedSet.js +26 -0
  541. package/lib/util/LazySet.js +45 -4
  542. package/lib/util/LocConverter.js +64 -0
  543. package/lib/util/ParallelismFactorCalculator.js +1 -0
  544. package/lib/util/Queue.js +6 -3
  545. package/lib/util/Semaphore.js +15 -1
  546. package/lib/util/SetHelpers.js +4 -1
  547. package/lib/util/SortableSet.js +7 -1
  548. package/lib/util/SourceProcessor.js +119 -0
  549. package/lib/util/StackedCacheMap.js +20 -3
  550. package/lib/util/StackedMap.js +162 -43
  551. package/lib/util/StringXor.js +1 -1
  552. package/lib/util/TupleQueue.js +7 -3
  553. package/lib/util/TupleSet.js +14 -0
  554. package/lib/util/URLAbsoluteSpecifier.js +1 -0
  555. package/lib/util/WeakTupleMap.js +60 -1
  556. package/lib/util/binarySearchBounds.js +3 -2
  557. package/lib/util/chainedImports.js +3 -3
  558. package/lib/util/cleverMerge.js +19 -2
  559. package/lib/util/comparators.js +35 -4
  560. package/lib/util/compileBooleanMatcher.js +9 -0
  561. package/lib/util/concatenate.js +80 -24
  562. package/lib/util/conventions.js +46 -1
  563. package/lib/util/createHash.js +0 -1
  564. package/lib/util/createHooksRegistry.js +43 -0
  565. package/lib/util/createMappings.js +118 -0
  566. package/lib/util/dataURL.js +3 -2
  567. package/lib/util/deprecation.js +19 -0
  568. package/lib/util/deterministicGrouping.js +46 -17
  569. package/lib/util/extractSourceMap.js +2 -1
  570. package/lib/util/extractUrlAndGlobal.js +1 -0
  571. package/lib/util/findGraphRoots.js +7 -0
  572. package/lib/{formatLocation.js → util/formatLocation.js} +4 -2
  573. package/lib/{SizeFormatHelpers.js → util/formatSize.js} +10 -3
  574. package/lib/util/fs.js +87 -14
  575. package/lib/util/generateDebugId.js +1 -0
  576. package/lib/util/globUtils.js +710 -0
  577. package/lib/util/hash/BatchedHash.js +1 -0
  578. package/lib/util/hash/BulkUpdateHash.js +1 -0
  579. package/lib/util/hash/DebugHash.js +1 -0
  580. package/lib/util/hash/hash-digest.js +32 -15
  581. package/lib/util/hash/md4.js +1 -1
  582. package/lib/util/hash/wasm-hash.js +5 -0
  583. package/lib/util/hash/xxhash64.js +1 -1
  584. package/lib/util/identifier.js +118 -7
  585. package/lib/util/implicitTypeLoaderFallback.js +62 -0
  586. package/lib/util/internalSerializables.js +102 -60
  587. package/lib/util/magicComment.js +149 -7
  588. package/lib/util/makeSerializable.js +7 -0
  589. package/lib/util/memoize.js +2 -0
  590. package/lib/util/mimeTypes.js +176 -0
  591. package/lib/util/nonNumericOnlyHash.js +31 -1
  592. package/lib/util/numberHash.js +2 -2
  593. package/lib/util/parseJson.js +41 -0
  594. package/lib/util/processAsyncTree.js +8 -0
  595. package/lib/util/property.js +9 -2
  596. package/lib/util/publicPathPlaceholder.js +64 -0
  597. package/lib/util/registerExternalSerializer.js +24 -4
  598. package/lib/util/removeBOM.js +1 -0
  599. package/lib/util/runtime.js +32 -0
  600. package/lib/util/semver.js +15 -0
  601. package/lib/util/serialization.js +2 -0
  602. package/lib/util/smartGrouping.js +8 -0
  603. package/lib/util/source.js +23 -0
  604. package/lib/util/topologicalSort.js +69 -0
  605. package/lib/validateSchema.js +1 -0
  606. package/lib/wasm/EnableWasmLoadingPlugin.js +15 -1
  607. package/lib/wasm-async/AsyncWasmCompileRuntimeModule.js +25 -18
  608. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +34 -22
  609. package/lib/wasm-async/AsyncWasmModule.js +77 -0
  610. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +6 -0
  611. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +6 -1
  612. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +22 -126
  613. package/lib/wasm-async/AsyncWebAssemblyParser.js +3 -2
  614. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +13 -2
  615. package/lib/wasm-sync/SyncWasmModule.js +39 -0
  616. package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +7 -3
  617. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +86 -19
  618. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +4 -3
  619. package/lib/wasm-sync/WebAssemblyGenerator.js +27 -1
  620. package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +9 -3
  621. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +4 -0
  622. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +20 -1
  623. package/lib/wasm-sync/WebAssemblyParser.js +9 -3
  624. package/lib/wasm-sync/WebAssemblyUtils.js +2 -0
  625. package/lib/web/FetchCompileAsyncWasmPlugin.js +29 -1
  626. package/lib/web/FetchCompileWasmPlugin.js +27 -1
  627. package/lib/web/JsonpChunkLoadingPlugin.js +11 -1
  628. package/lib/web/JsonpChunkLoadingRuntimeModule.js +58 -64
  629. package/lib/web/JsonpTemplatePlugin.js +2 -1
  630. package/lib/webpack.js +78 -1
  631. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +10 -1
  632. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +15 -10
  633. package/lib/webworker/WebWorkerTemplatePlugin.js +1 -1
  634. package/module.d.ts +21 -0
  635. package/package.json +79 -77
  636. package/schemas/WebpackOptions.check.js +1 -1
  637. package/schemas/WebpackOptions.json +1173 -80
  638. package/schemas/plugins/{DllPlugin.check.d.ts → HtmlGeneratorOptions.check.d.ts} +1 -1
  639. package/schemas/plugins/HtmlGeneratorOptions.check.js +6 -0
  640. package/schemas/plugins/HtmlGeneratorOptions.json +3 -0
  641. package/schemas/plugins/{DllReferencePlugin.check.d.ts → HtmlParserOptions.check.d.ts} +1 -1
  642. package/schemas/plugins/HtmlParserOptions.check.js +6 -0
  643. package/schemas/plugins/HtmlParserOptions.json +3 -0
  644. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  645. package/schemas/plugins/ProgressPlugin.json +38 -0
  646. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  647. package/schemas/plugins/container/ContainerReferencePlugin.json +2 -0
  648. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  649. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  650. package/schemas/plugins/container/ModuleFederationPlugin.json +2 -0
  651. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.d.ts +7 -0
  652. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +6 -0
  653. package/schemas/plugins/css/CssAutoOrModuleParserOptions.json +3 -0
  654. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  655. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  656. package/schemas/plugins/dll/DllPlugin.check.d.ts +7 -0
  657. package/schemas/plugins/dll/DllReferencePlugin.check.d.ts +7 -0
  658. package/types.d.ts +11053 -3019
  659. package/lib/CaseSensitiveModulesWarning.js +0 -72
  660. package/lib/GraphHelpers.js +0 -46
  661. package/lib/ModuleParseError.js +0 -125
  662. package/lib/NoModeWarning.js +0 -23
  663. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +0 -56
  664. package/lib/css/walkCssTokens.js +0 -1843
  665. package/lib/util/AppendOnlyStackedSet.js +0 -78
  666. /package/schemas/plugins/{DllPlugin.check.js → dll/DllPlugin.check.js} +0 -0
  667. /package/schemas/plugins/{DllPlugin.json → dll/DllPlugin.json} +0 -0
  668. /package/schemas/plugins/{DllReferencePlugin.check.js → dll/DllReferencePlugin.check.js} +0 -0
  669. /package/schemas/plugins/{DllReferencePlugin.json → dll/DllReferencePlugin.json} +0 -0
@@ -4,6 +4,7 @@
4
4
 
5
5
  "use strict";
6
6
 
7
+ const { deserialize: v8Deserialize, serialize: v8Serialize } = require("v8");
7
8
  const memoize = require("../util/memoize");
8
9
  const SerializerMiddleware = require("./SerializerMiddleware");
9
10
 
@@ -15,97 +16,33 @@ Format:
15
16
 
16
17
  File -> Section*
17
18
 
18
- Section -> NullsSection |
19
- BooleansSection |
20
- F64NumbersSection |
21
- I32NumbersSection |
22
- I8NumbersSection |
23
- ShortStringSection |
24
- BigIntSection |
25
- I32BigIntSection |
26
- I8BigIntSection
27
- StringSection |
28
- BufferSection |
29
- NopSection
19
+ Section -> ValuesSection | LazySection
30
20
 
21
+ ValuesSection ->
22
+ ValuesHeaderByte u32:payloadSize u32:bufferCount (u32:valueIndex u32:bufferSize)*
23
+ payload buffer*
24
+ LazySection ->
25
+ LazyHeaderByte u32:count u32:size*
31
26
 
32
-
33
- NullsSection ->
34
- NullHeaderByte | Null2HeaderByte | Null3HeaderByte |
35
- Nulls8HeaderByte 0xnn (n:count - 4) |
36
- Nulls32HeaderByte n:ui32 (n:count - 260) |
37
- BooleansSection -> TrueHeaderByte | FalseHeaderByte | BooleansSectionHeaderByte BooleansCountAndBitsByte
38
- F64NumbersSection -> F64NumbersSectionHeaderByte f64*
39
- I32NumbersSection -> I32NumbersSectionHeaderByte i32*
40
- I8NumbersSection -> I8NumbersSectionHeaderByte i8*
41
- ShortStringSection -> ShortStringSectionHeaderByte ascii-byte*
42
- StringSection -> StringSectionHeaderByte i32:length utf8-byte*
43
- BufferSection -> BufferSectionHeaderByte i32:length byte*
44
- NopSection --> NopSectionHeaderByte
45
- BigIntSection -> BigIntSectionHeaderByte i32:length ascii-byte*
46
- I32BigIntSection -> I32BigIntSectionHeaderByte i32
47
- I8BigIntSection -> I8BigIntSectionHeaderByte i8
48
-
49
- ShortStringSectionHeaderByte -> 0b1nnn_nnnn (n:length)
50
-
51
- F64NumbersSectionHeaderByte -> 0b001n_nnnn (n:count - 1)
52
- I32NumbersSectionHeaderByte -> 0b010n_nnnn (n:count - 1)
53
- I8NumbersSectionHeaderByte -> 0b011n_nnnn (n:count - 1)
54
-
55
- NullsSectionHeaderByte -> 0b0001_nnnn (n:count - 1)
56
- BooleansCountAndBitsByte ->
57
- 0b0000_1xxx (count = 3) |
58
- 0b0001_xxxx (count = 4) |
59
- 0b001x_xxxx (count = 5) |
60
- 0b01xx_xxxx (count = 6) |
61
- 0b1nnn_nnnn (n:count - 7, 7 <= count <= 133)
62
- 0xff n:ui32 (n:count, 134 <= count < 2^32)
63
-
64
- StringSectionHeaderByte -> 0b0000_1110
65
- BufferSectionHeaderByte -> 0b0000_1111
66
- NopSectionHeaderByte -> 0b0000_1011
67
- BigIntSectionHeaderByte -> 0b0001_1010
68
- I32BigIntSectionHeaderByte -> 0b0001_1100
69
- I8BigIntSectionHeaderByte -> 0b0001_1011
70
- FalseHeaderByte -> 0b0000_1100
71
- TrueHeaderByte -> 0b0000_1101
72
-
73
- RawNumber -> n (n <= 10)
74
-
27
+ ValuesHeaderByte -> 0xf1
28
+ LazyHeaderByte -> 0xf2
75
29
  */
76
30
 
77
- const LAZY_HEADER = 0x0b;
78
- const TRUE_HEADER = 0x0c;
79
- const FALSE_HEADER = 0x0d;
80
- const BOOLEANS_HEADER = 0x0e;
81
- const NULL_HEADER = 0x10;
82
- const NULL2_HEADER = 0x11;
83
- const NULL3_HEADER = 0x12;
84
- const NULLS8_HEADER = 0x13;
85
- const NULLS32_HEADER = 0x14;
86
- const NULL_AND_I8_HEADER = 0x15;
87
- const NULL_AND_I32_HEADER = 0x16;
88
- const NULL_AND_TRUE_HEADER = 0x17;
89
- const NULL_AND_FALSE_HEADER = 0x18;
90
- const BIGINT_HEADER = 0x1a;
91
- const BIGINT_I8_HEADER = 0x1b;
92
- const BIGINT_I32_HEADER = 0x1c;
93
- const STRING_HEADER = 0x1e;
94
- const BUFFER_HEADER = 0x1f;
95
- const I8_HEADER = 0x60;
96
- const I32_HEADER = 0x40;
97
- const F64_HEADER = 0x20;
98
- const SHORT_STRING_HEADER = 0x80;
99
-
100
- /** Uplift high-order bits */
101
- const NUMBERS_HEADER_MASK = 0xe0; // 0b1010_0000
102
- const NUMBERS_COUNT_MASK = 0x1f; // 0b0001_1111
103
- const SHORT_STRING_LENGTH_MASK = 0x7f; // 0b0111_1111
31
+ const VALUES_HEADER = 0xf1;
32
+ const LAZY_HEADER = 0xf2;
104
33
 
105
34
  const HEADER_SIZE = 1;
106
- const I8_SIZE = 1;
107
35
  const I32_SIZE = 4;
108
- const F64_SIZE = 8;
36
+
37
+ /** A values section is closed once its payload is estimated to reach this size. */
38
+ const MAX_SECTION_SIZE = 16 * 1024 * 1024;
39
+ /** Assumed encoded size of a value that isn't a string, for that estimate. */
40
+ const ESTIMATED_VALUE_SIZE = 4;
41
+
42
+ const EMPTY_BUFFER = Buffer.alloc(0);
43
+
44
+ /** Highest V8 value serialization format version this Node.js can read. */
45
+ const V8_FORMAT_VERSION = v8Serialize(null)[1];
109
46
 
110
47
  const MEASURE_START_OPERATION = Symbol("MEASURE_START_OPERATION");
111
48
  const MEASURE_END_OPERATION = Symbol("MEASURE_END_OPERATION");
@@ -113,43 +50,229 @@ const MEASURE_END_OPERATION = Symbol("MEASURE_END_OPERATION");
113
50
  /** @typedef {typeof MEASURE_START_OPERATION} MEASURE_START_OPERATION_TYPE */
114
51
  /** @typedef {typeof MEASURE_END_OPERATION} MEASURE_END_OPERATION_TYPE */
115
52
 
53
+ /** @typedef {PrimitiveSerializableType[]} DeserializedType */
54
+ /** @typedef {BufferSerializableType[]} SerializedType} */
55
+ /** @typedef {{ retainedBuffer?: (x: Buffer) => Buffer }} Context} */
56
+
116
57
  /**
117
- * @param {number} n number
118
- * @returns {0 | 1 | 2} type of number for serialization
58
+ * Defines the lazy function type used by this module.
59
+ * @template LazyInputValue
60
+ * @template LazyOutputValue
61
+ * @typedef {import("./SerializerMiddleware").LazyFunction<LazyInputValue, LazyOutputValue, BinaryMiddleware, undefined>} LazyFunction
119
62
  */
120
- const identifyNumber = (n) => {
121
- if (n === (n | 0)) {
122
- if (n <= 127 && n >= -128) return 0;
123
- if (n <= 2147483647 && n >= -2147483648) return 1;
63
+
64
+ /** Mutable read state of one `_deserialize` run. */
65
+ class ReadState {
66
+ /**
67
+ * @param {SerializedType} data data
68
+ * @param {Context} context context object
69
+ */
70
+ constructor(data, context) {
71
+ /** @type {SerializedType} */
72
+ this.data = data;
73
+ this.retainedBuffer = context.retainedBuffer || ((x) => x);
74
+ /** @type {number} */
75
+ this.currentDataItem = 0;
76
+ /** @type {BufferSerializableType | null} */
77
+ this.currentBuffer = data.length > 0 ? data[0] : null;
78
+ /** @type {boolean} */
79
+ this.currentIsBuffer = Buffer.isBuffer(this.currentBuffer);
80
+ /** @type {number} */
81
+ this.currentPosition = 0;
82
+ }
83
+
84
+ /** Advances to the next data item (does not reset the position). */
85
+ nextDataItem() {
86
+ this.currentDataItem++;
87
+ this.currentBuffer =
88
+ this.currentDataItem < this.data.length
89
+ ? this.data[this.currentDataItem]
90
+ : null;
91
+ this.currentIsBuffer = Buffer.isBuffer(this.currentBuffer);
124
92
  }
125
- return 2;
126
- };
93
+
94
+ checkOverflow() {
95
+ if (
96
+ this.currentPosition >= /** @type {Buffer} */ (this.currentBuffer).length
97
+ ) {
98
+ this.currentPosition = 0;
99
+ this.nextDataItem();
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Checks whether n bytes are available in the current buffer.
105
+ * @param {number} n n
106
+ * @returns {boolean} true when in current buffer, otherwise false
107
+ */
108
+ isInCurrentBuffer(n) {
109
+ return (
110
+ this.currentIsBuffer &&
111
+ n + this.currentPosition <=
112
+ /** @type {Buffer} */ (this.currentBuffer).length
113
+ );
114
+ }
115
+
116
+ ensureBuffer() {
117
+ if (!this.currentIsBuffer) {
118
+ throw new Error(
119
+ this.currentBuffer === null
120
+ ? "Unexpected end of stream"
121
+ : "Unexpected lazy element in stream"
122
+ );
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Returns buffer with bytes.
128
+ * @param {number} n amount of bytes to read
129
+ * @returns {Buffer} buffer with bytes
130
+ */
131
+ read(n) {
132
+ if (n === 0) return EMPTY_BUFFER;
133
+ this.ensureBuffer();
134
+ const rem =
135
+ /** @type {Buffer} */ (this.currentBuffer).length - this.currentPosition;
136
+ if (rem < n) {
137
+ const buffers = [this.read(rem)];
138
+ n -= rem;
139
+ this.ensureBuffer();
140
+ while (/** @type {Buffer} */ (this.currentBuffer).length < n) {
141
+ const b = /** @type {Buffer} */ (this.currentBuffer);
142
+ buffers.push(b);
143
+ n -= b.length;
144
+ this.nextDataItem();
145
+ this.ensureBuffer();
146
+ }
147
+ buffers.push(this.read(n));
148
+ return Buffer.concat(buffers);
149
+ }
150
+ const b = /** @type {Buffer} */ (this.currentBuffer);
151
+ const res = Buffer.from(b.buffer, b.byteOffset + this.currentPosition, n);
152
+ this.currentPosition += n;
153
+ this.checkOverflow();
154
+ return res;
155
+ }
156
+
157
+ /**
158
+ * Reads up to n bytes.
159
+ * @param {number} n amount of bytes to read
160
+ * @returns {Buffer} buffer with bytes
161
+ */
162
+ readUpTo(n) {
163
+ this.ensureBuffer();
164
+ const rem =
165
+ /** @type {Buffer} */ (this.currentBuffer).length - this.currentPosition;
166
+ if (rem < n) {
167
+ n = rem;
168
+ }
169
+ const b = /** @type {Buffer} */ (this.currentBuffer);
170
+ const res = Buffer.from(b.buffer, b.byteOffset + this.currentPosition, n);
171
+ this.currentPosition += n;
172
+ this.checkOverflow();
173
+ return res;
174
+ }
175
+
176
+ /**
177
+ * Returns u8.
178
+ * @returns {number} U8
179
+ */
180
+ readU8() {
181
+ this.ensureBuffer();
182
+ /**
183
+ * There is no need to check remaining buffer size here
184
+ * since {@link ReadState#checkOverflow} guarantees at least one byte remaining
185
+ */
186
+ const byte =
187
+ /** @type {Buffer} */
188
+ (this.currentBuffer).readUInt8(this.currentPosition);
189
+ this.currentPosition += HEADER_SIZE;
190
+ this.checkOverflow();
191
+ return byte;
192
+ }
193
+
194
+ /**
195
+ * Returns u32.
196
+ * @returns {number} U32
197
+ */
198
+ readU32() {
199
+ // fast path avoids allocating a 4-byte view per length read
200
+ if (this.isInCurrentBuffer(I32_SIZE)) {
201
+ const value =
202
+ /** @type {Buffer} */
203
+ (this.currentBuffer).readUInt32LE(this.currentPosition);
204
+ this.currentPosition += I32_SIZE;
205
+ this.checkOverflow();
206
+ return value;
207
+ }
208
+ return this.read(I32_SIZE).readUInt32LE(0);
209
+ }
210
+ }
127
211
 
128
212
  /**
129
- * @param {bigint} n bigint
130
- * @returns {0 | 1 | 2} type of bigint for serialization
213
+ * Reads a section of values written by V8's value serializer.
214
+ * @param {ReadState} state read state
215
+ * @returns {DeserializedType} values of the section
131
216
  */
132
- const identifyBigInt = (n) => {
133
- if (n <= BigInt(127) && n >= BigInt(-128)) return 0;
134
- if (n <= BigInt(2147483647) && n >= BigInt(-2147483648)) return 1;
135
- return 2;
217
+ const readValuesSection = (state) => {
218
+ const payloadSize = state.readU32();
219
+ const bufferCount = state.readU32();
220
+ /** @type {number[]} */
221
+ const bufferInfo = [];
222
+ for (let i = 0; i < bufferCount * 2; i++) bufferInfo.push(state.readU32());
223
+ const payload = state.read(payloadSize);
224
+ // a V8 payload opens with 0xff and its format version, which only ever grows
225
+ if (payload[0] === 0xff && payload[1] > V8_FORMAT_VERSION) {
226
+ throw new Error(
227
+ `Data was written with V8 serialization format version ${payload[1]}, but this Node.js (${process.version}) only reads up to version ${V8_FORMAT_VERSION}`
228
+ );
229
+ }
230
+ const values = /** @type {DeserializedType} */ (v8Deserialize(payload));
231
+ for (let i = 0; i < bufferCount; i++) {
232
+ values[bufferInfo[i * 2]] = state.retainedBuffer(
233
+ state.read(bufferInfo[i * 2 + 1])
234
+ );
235
+ }
236
+ return values;
136
237
  };
137
238
 
138
- /** @typedef {PrimitiveSerializableType[]} DeserializedType */
139
- /** @typedef {BufferSerializableType[]} SerializedType} */
140
- /** @typedef {{ retainedBuffer?: (x: Buffer) => Buffer }} Context} */
141
-
142
239
  /**
143
- * @template LazyInputValue
144
- * @template LazyOutputValue
145
- * @typedef {import("./SerializerMiddleware").LazyFunction<LazyInputValue, LazyOutputValue, BinaryMiddleware, undefined>} LazyFunction
240
+ * Reads the content items of a lazy section.
241
+ * @param {ReadState} state read state
242
+ * @returns {SerializedType} content of the lazy value
146
243
  */
244
+ const readLazySection = (state) => {
245
+ const count = state.readU32();
246
+ /** @type {number[]} */
247
+ const sizes = [];
248
+ for (let i = 0; i < count; i++) sizes.push(state.readU32());
249
+ /** @type {SerializedType} */
250
+ const content = [];
251
+ for (let size of sizes) {
252
+ if (size === 0) {
253
+ if (typeof state.currentBuffer !== "function") {
254
+ throw new Error("Unexpected non-lazy element in stream");
255
+ }
256
+ content.push(state.currentBuffer);
257
+ state.nextDataItem();
258
+ } else {
259
+ do {
260
+ const buf = state.readUpTo(size);
261
+ size -= buf.length;
262
+ content.push(state.retainedBuffer(buf));
263
+ } while (size > 0);
264
+ }
265
+ }
266
+ return content;
267
+ };
147
268
 
148
269
  /**
270
+ * Represents BinaryMiddleware.
149
271
  * @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>}
150
272
  */
151
273
  class BinaryMiddleware extends SerializerMiddleware {
152
274
  /**
275
+ * Serializes this instance into the provided serializer context.
153
276
  * @param {DeserializedType} data data
154
277
  * @param {Context} context context object
155
278
  * @returns {SerializedType | Promise<SerializedType> | null} serialized data
@@ -159,6 +282,7 @@ class BinaryMiddleware extends SerializerMiddleware {
159
282
  }
160
283
 
161
284
  /**
285
+ * Returns new lazy.
162
286
  * @param {LazyFunction<DeserializedType, SerializedType>} fn lazy function
163
287
  * @param {Context} context serialize function
164
288
  * @returns {LazyFunction<SerializedType, DeserializedType>} new lazy
@@ -170,489 +294,216 @@ class BinaryMiddleware extends SerializerMiddleware {
170
294
  }
171
295
 
172
296
  /**
297
+ * Returns serialized data.
173
298
  * @param {DeserializedType} data data
174
299
  * @param {Context} context context object
175
- * @param {{ leftOverBuffer: Buffer | null, allocationSize: number, increaseCounter: number }} allocationScope allocation scope
176
300
  * @returns {SerializedType} serialized data
177
301
  */
178
- _serialize(
179
- data,
180
- context,
181
- allocationScope = {
182
- allocationSize: 1024,
183
- increaseCounter: 0,
184
- leftOverBuffer: null
185
- }
186
- ) {
187
- /** @type {Buffer | null} */
188
- let leftOverBuffer = null;
189
- /** @type {BufferSerializableType[]} */
190
- let buffers = [];
191
- /** @type {Buffer | null} */
192
- let currentBuffer = allocationScope ? allocationScope.leftOverBuffer : null;
193
- allocationScope.leftOverBuffer = null;
194
- let currentPosition = 0;
195
- if (currentBuffer === null) {
196
- currentBuffer = Buffer.allocUnsafe(allocationScope.allocationSize);
197
- }
302
+ _serialize(data, context) {
303
+ /** @type {SerializedType} */
304
+ const result = [];
305
+ /** @type {number[]} */
306
+ const measureStack = [];
307
+ let writtenBytes = 0;
308
+ /** Index of the first value of the open section. */
309
+ let sectionStart = 0;
310
+ /** Estimated payload size of the open section. */
311
+ let sectionSize = 0;
198
312
  /**
199
- * @param {number} bytesNeeded bytes needed
313
+ * Positions of the buffers of the open section, relative to `data`.
314
+ * @type {number[]}
200
315
  */
201
- const allocate = (bytesNeeded) => {
202
- if (currentBuffer !== null) {
203
- if (currentBuffer.length - currentPosition >= bytesNeeded) return;
204
- flush();
205
- }
206
- if (leftOverBuffer && leftOverBuffer.length >= bytesNeeded) {
207
- currentBuffer = leftOverBuffer;
208
- leftOverBuffer = null;
209
- } else {
210
- currentBuffer = Buffer.allocUnsafe(
211
- Math.max(bytesNeeded, allocationScope.allocationSize)
212
- );
213
- if (
214
- !(allocationScope.increaseCounter =
215
- (allocationScope.increaseCounter + 1) % 4) &&
216
- allocationScope.allocationSize < 16777216
217
- ) {
218
- allocationScope.allocationSize <<= 1;
219
- }
220
- }
221
- };
222
- const flush = () => {
223
- if (currentBuffer !== null) {
224
- if (currentPosition > 0) {
225
- buffers.push(
226
- Buffer.from(
227
- currentBuffer.buffer,
228
- currentBuffer.byteOffset,
229
- currentPosition
230
- )
231
- );
232
- }
233
- if (
234
- !leftOverBuffer ||
235
- leftOverBuffer.length < currentBuffer.length - currentPosition
236
- ) {
237
- leftOverBuffer = Buffer.from(
238
- currentBuffer.buffer,
239
- currentBuffer.byteOffset + currentPosition,
240
- currentBuffer.byteLength - currentPosition
241
- );
242
- }
243
-
244
- currentBuffer = null;
245
- currentPosition = 0;
246
- }
247
- };
316
+ let bufferIndices = [];
317
+ /** @type {Buffer[]} */
318
+ let buffers = [];
248
319
  /**
249
- * @param {number} byte byte
320
+ * Appends a buffer to the output.
321
+ * @param {Buffer} buffer buffer
250
322
  */
251
- const writeU8 = (byte) => {
252
- /** @type {Buffer} */
253
- (currentBuffer).writeUInt8(byte, currentPosition++);
323
+ const write = (buffer) => {
324
+ writtenBytes += buffer.length;
325
+ result.push(buffer);
254
326
  };
255
327
  /**
256
- * @param {number} ui32 ui32
328
+ * Writes a values section for the given values.
329
+ * @param {DeserializedType} values values without their buffers
330
+ * @param {number[]} indices position of each buffer within `values`
331
+ * @param {Buffer[]} bufferList buffers in the order of `indices`
257
332
  */
258
- const writeU32 = (ui32) => {
259
- /** @type {Buffer} */
260
- (currentBuffer).writeUInt32LE(ui32, currentPosition);
261
- currentPosition += 4;
262
- };
263
- /** @type {number[]} */
264
- const measureStack = [];
265
- const measureStart = () => {
266
- measureStack.push(buffers.length, currentPosition);
333
+ const writeValuesSection = (values, indices, bufferList) => {
334
+ const payload = v8Serialize(values);
335
+ const header = Buffer.allocUnsafe(
336
+ HEADER_SIZE + I32_SIZE * (2 + indices.length * 2)
337
+ );
338
+ header[0] = VALUES_HEADER;
339
+ header.writeUInt32LE(payload.length, HEADER_SIZE);
340
+ header.writeUInt32LE(indices.length, HEADER_SIZE + I32_SIZE);
341
+ let offset = HEADER_SIZE + I32_SIZE * 2;
342
+ for (let i = 0; i < indices.length; i++) {
343
+ header.writeUInt32LE(indices[i], offset);
344
+ header.writeUInt32LE(bufferList[i].length, offset + I32_SIZE);
345
+ offset += I32_SIZE * 2;
346
+ }
347
+ write(header);
348
+ write(payload);
349
+ for (const buffer of bufferList) {
350
+ if (buffer.length > 0) write(buffer);
351
+ }
267
352
  };
268
353
  /**
269
- * @returns {number} size
354
+ * Closes the open values section before the value at `end`.
355
+ * @param {number} end index of the first value not in the section
270
356
  */
271
- const measureEnd = () => {
272
- const oldPos = /** @type {number} */ (measureStack.pop());
273
- const buffersIndex = /** @type {number} */ (measureStack.pop());
274
- let size = currentPosition - oldPos;
275
- for (let i = buffersIndex; i < buffers.length; i++) {
276
- size += buffers[i].length;
277
- }
278
- return size;
279
- };
280
- for (let i = 0; i < data.length; i++) {
281
- const thing = data[i];
282
- switch (typeof thing) {
283
- case "function": {
284
- if (!SerializerMiddleware.isLazy(thing)) {
285
- throw new Error(`Unexpected function ${thing}`);
286
- }
287
- /** @type {SerializedType | LazyFunction<SerializedType, DeserializedType> | undefined} */
288
- let serializedData =
289
- SerializerMiddleware.getLazySerializedValue(thing);
290
- if (serializedData === undefined) {
291
- if (SerializerMiddleware.isLazy(thing, this)) {
292
- flush();
293
- allocationScope.leftOverBuffer = leftOverBuffer;
294
- const result =
295
- /** @type {PrimitiveSerializableType[]} */
296
- (thing());
297
- const data = this._serialize(result, context, allocationScope);
298
- leftOverBuffer = allocationScope.leftOverBuffer;
299
- allocationScope.leftOverBuffer = null;
300
- SerializerMiddleware.setLazySerializedValue(thing, data);
301
- serializedData = data;
302
- } else {
303
- serializedData = this._serializeLazy(thing, context);
304
- flush();
305
- buffers.push(serializedData);
306
- break;
357
+ const flush = (end) => {
358
+ if (end > sectionStart) {
359
+ if (
360
+ sectionStart === 0 &&
361
+ end === data.length &&
362
+ // a frozen input can't take the placeholders, so it needs the copy
363
+ (bufferIndices.length === 0 || !Object.isFrozen(data))
364
+ ) {
365
+ // the section covers all values: swap the buffers out and back in
366
+ // instead of copying the whole array to place the placeholders
367
+ try {
368
+ for (let i = 0; i < bufferIndices.length; i++) {
369
+ data[bufferIndices[i]] = null;
307
370
  }
308
- } else if (typeof serializedData === "function") {
309
- flush();
310
- buffers.push(serializedData);
311
- break;
312
- }
313
- /** @type {number[]} */
314
- const lengths = [];
315
- for (const item of serializedData) {
316
- /** @type {undefined | number} */
317
- let last;
318
- if (typeof item === "function") {
319
- lengths.push(0);
320
- } else if (item.length === 0) {
321
- // ignore
322
- } else if (
323
- lengths.length > 0 &&
324
- (last = lengths[lengths.length - 1]) !== 0
325
- ) {
326
- const remaining = 0xffffffff - last;
327
- if (remaining >= item.length) {
328
- lengths[lengths.length - 1] += item.length;
329
- } else {
330
- lengths.push(item.length - remaining);
331
- lengths[lengths.length - 2] = 0xffffffff;
332
- }
333
- } else {
334
- lengths.push(item.length);
371
+ writeValuesSection(data, bufferIndices, buffers);
372
+ } finally {
373
+ for (let i = 0; i < bufferIndices.length; i++) {
374
+ data[bufferIndices[i]] = buffers[i];
335
375
  }
336
376
  }
337
- allocate(5 + lengths.length * 4);
338
- writeU8(LAZY_HEADER);
339
- writeU32(lengths.length);
340
- for (const l of lengths) {
341
- writeU32(l);
377
+ } else {
378
+ const values = data.slice(sectionStart, end);
379
+ for (let i = 0; i < bufferIndices.length; i++) {
380
+ const index = bufferIndices[i] - sectionStart;
381
+ bufferIndices[i] = index;
382
+ values[index] = null;
342
383
  }
343
- flush();
344
- for (const item of serializedData) {
345
- buffers.push(item);
346
- }
347
- break;
384
+ writeValuesSection(values, bufferIndices, buffers);
348
385
  }
349
- case "string": {
350
- const len = Buffer.byteLength(thing);
351
- if (len >= 128 || len !== thing.length) {
352
- allocate(len + HEADER_SIZE + I32_SIZE);
353
- writeU8(STRING_HEADER);
354
- writeU32(len);
355
- currentBuffer.write(thing, currentPosition);
356
- currentPosition += len;
357
- } else if (len >= 70) {
358
- allocate(len + HEADER_SIZE);
359
- writeU8(SHORT_STRING_HEADER | len);
360
-
361
- currentBuffer.write(thing, currentPosition, "latin1");
362
- currentPosition += len;
363
- } else {
364
- allocate(len + HEADER_SIZE);
365
- writeU8(SHORT_STRING_HEADER | len);
366
-
367
- for (let i = 0; i < len; i++) {
368
- currentBuffer[currentPosition++] = thing.charCodeAt(i);
369
- }
370
- }
371
- break;
386
+ if (bufferIndices.length > 0) {
387
+ bufferIndices = [];
388
+ buffers = [];
372
389
  }
373
- case "bigint": {
374
- const type = identifyBigInt(thing);
375
- if (type === 0 && thing >= 0 && thing <= BigInt(10)) {
376
- // shortcut for very small bigints
377
- allocate(HEADER_SIZE + I8_SIZE);
378
- writeU8(BIGINT_I8_HEADER);
379
- writeU8(Number(thing));
380
- break;
381
- }
382
-
383
- switch (type) {
384
- case 0: {
385
- let n = 1;
386
- allocate(HEADER_SIZE + I8_SIZE * n);
387
- writeU8(BIGINT_I8_HEADER | (n - 1));
388
- while (n > 0) {
389
- currentBuffer.writeInt8(
390
- Number(/** @type {bigint} */ (data[i])),
391
- currentPosition
392
- );
393
- currentPosition += I8_SIZE;
394
- n--;
395
- i++;
396
- }
397
- i--;
398
- break;
399
- }
400
- case 1: {
401
- let n = 1;
402
- allocate(HEADER_SIZE + I32_SIZE * n);
403
- writeU8(BIGINT_I32_HEADER | (n - 1));
404
- while (n > 0) {
405
- currentBuffer.writeInt32LE(
406
- Number(/** @type {bigint} */ (data[i])),
407
- currentPosition
408
- );
409
- currentPosition += I32_SIZE;
410
- n--;
411
- i++;
412
- }
413
- i--;
414
- break;
415
- }
416
- default: {
417
- const value = thing.toString();
418
- const len = Buffer.byteLength(value);
419
- allocate(len + HEADER_SIZE + I32_SIZE);
420
- writeU8(BIGINT_HEADER);
421
- writeU32(len);
422
- currentBuffer.write(value, currentPosition);
423
- currentPosition += len;
424
- break;
425
- }
390
+ }
391
+ sectionStart = end;
392
+ sectionSize = 0;
393
+ };
394
+ for (let i = 0; i < data.length; i++) {
395
+ const thing = data[i];
396
+ const type = typeof thing;
397
+ if (type === "string") {
398
+ sectionSize += /** @type {string} */ (thing).length;
399
+ } else if (type === "object") {
400
+ // buffers are appended to the section instead of entering the payload
401
+ if (thing !== null) {
402
+ if (!Buffer.isBuffer(thing)) {
403
+ throw new Error(`Unexpected object ${thing} in binary middleware`);
426
404
  }
427
- break;
405
+ bufferIndices.push(i);
406
+ buffers.push(thing);
407
+ // count placeholder + bytes so buffer-heavy runs still split
408
+ sectionSize += ESTIMATED_VALUE_SIZE + thing.length;
409
+ } else {
410
+ sectionSize += ESTIMATED_VALUE_SIZE;
428
411
  }
429
- case "number": {
430
- const type = identifyNumber(thing);
431
- if (type === 0 && thing >= 0 && thing <= 10) {
432
- // shortcut for very small numbers
433
- allocate(I8_SIZE);
434
- writeU8(thing);
435
- break;
436
- }
437
- /**
438
- * amount of numbers to write
439
- * @type {number}
440
- */
441
- let n = 1;
442
- for (; n < 32 && i + n < data.length; n++) {
443
- const item = data[i + n];
444
- if (typeof item !== "number") break;
445
- if (identifyNumber(item) !== type) break;
446
- }
447
- switch (type) {
448
- case 0:
449
- allocate(HEADER_SIZE + I8_SIZE * n);
450
- writeU8(I8_HEADER | (n - 1));
451
- while (n > 0) {
452
- currentBuffer.writeInt8(
453
- /** @type {number} */ (data[i]),
454
- currentPosition
455
- );
456
- currentPosition += I8_SIZE;
457
- n--;
458
- i++;
459
- }
460
- break;
461
- case 1:
462
- allocate(HEADER_SIZE + I32_SIZE * n);
463
- writeU8(I32_HEADER | (n - 1));
464
- while (n > 0) {
465
- currentBuffer.writeInt32LE(
466
- /** @type {number} */ (data[i]),
467
- currentPosition
468
- );
469
- currentPosition += I32_SIZE;
470
- n--;
471
- i++;
472
- }
473
- break;
474
- case 2:
475
- allocate(HEADER_SIZE + F64_SIZE * n);
476
- writeU8(F64_HEADER | (n - 1));
477
- while (n > 0) {
478
- currentBuffer.writeDoubleLE(
479
- /** @type {number} */ (data[i]),
480
- currentPosition
481
- );
482
- currentPosition += F64_SIZE;
483
- n--;
484
- i++;
485
- }
486
- break;
487
- }
488
-
489
- i--;
490
- break;
412
+ } else if (type === "function") {
413
+ flush(i);
414
+ sectionStart = i + 1;
415
+ if (!SerializerMiddleware.isLazy(thing)) {
416
+ throw new Error(`Unexpected function ${thing}`);
491
417
  }
492
- case "boolean": {
493
- let lastByte = thing === true ? 1 : 0;
494
- /** @type {number[]} */
495
- const bytes = [];
496
- let count = 1;
497
- /** @type {undefined | number} */
498
- let n;
499
- for (n = 1; n < 0xffffffff && i + n < data.length; n++) {
500
- const item = data[i + n];
501
- if (typeof item !== "boolean") break;
502
- const pos = count & 0x7;
503
- if (pos === 0) {
504
- bytes.push(lastByte);
505
- lastByte = item === true ? 1 : 0;
506
- } else if (item === true) {
507
- lastByte |= 1 << pos;
508
- }
509
- count++;
510
- }
511
- i += count - 1;
512
- if (count === 1) {
513
- allocate(HEADER_SIZE);
514
- writeU8(lastByte === 1 ? TRUE_HEADER : FALSE_HEADER);
515
- } else if (count === 2) {
516
- allocate(HEADER_SIZE * 2);
517
- writeU8(lastByte & 1 ? TRUE_HEADER : FALSE_HEADER);
518
- writeU8(lastByte & 2 ? TRUE_HEADER : FALSE_HEADER);
519
- } else if (count <= 6) {
520
- allocate(HEADER_SIZE + I8_SIZE);
521
- writeU8(BOOLEANS_HEADER);
522
- writeU8((1 << count) | lastByte);
523
- } else if (count <= 133) {
524
- allocate(HEADER_SIZE + I8_SIZE + I8_SIZE * bytes.length + I8_SIZE);
525
- writeU8(BOOLEANS_HEADER);
526
- writeU8(0x80 | (count - 7));
527
- for (const byte of bytes) writeU8(byte);
528
- writeU8(lastByte);
418
+ /** @type {SerializedType | LazyFunction<SerializedType, DeserializedType> | undefined} */
419
+ let serializedData = SerializerMiddleware.getLazySerializedValue(thing);
420
+ if (serializedData === undefined) {
421
+ if (SerializerMiddleware.isLazy(thing, this)) {
422
+ serializedData = this._serialize(
423
+ /** @type {DeserializedType} */ (thing()),
424
+ context
425
+ );
426
+ SerializerMiddleware.setLazySerializedValue(thing, serializedData);
529
427
  } else {
530
- allocate(
531
- HEADER_SIZE +
532
- I8_SIZE +
533
- I32_SIZE +
534
- I8_SIZE * bytes.length +
535
- I8_SIZE
428
+ result.push(
429
+ this._serializeLazy(
430
+ /** @type {LazyFunction<DeserializedType, SerializedType>} */
431
+ (thing),
432
+ context
433
+ )
536
434
  );
537
- writeU8(BOOLEANS_HEADER);
538
- writeU8(0xff);
539
- writeU32(count);
540
- for (const byte of bytes) writeU8(byte);
541
- writeU8(lastByte);
435
+ continue;
542
436
  }
543
- break;
437
+ } else if (typeof serializedData === "function") {
438
+ result.push(serializedData);
439
+ continue;
544
440
  }
545
- case "object": {
546
- if (thing === null) {
547
- /** @type {number} */
548
- let n;
549
- for (n = 1; n < 0x100000104 && i + n < data.length; n++) {
550
- const item = data[i + n];
551
- if (item !== null) break;
552
- }
553
- i += n - 1;
554
- if (n === 1) {
555
- if (i + 1 < data.length) {
556
- const next = data[i + 1];
557
- if (next === true) {
558
- allocate(HEADER_SIZE);
559
- writeU8(NULL_AND_TRUE_HEADER);
560
- i++;
561
- } else if (next === false) {
562
- allocate(HEADER_SIZE);
563
- writeU8(NULL_AND_FALSE_HEADER);
564
- i++;
565
- } else if (typeof next === "number") {
566
- const type = identifyNumber(next);
567
- if (type === 0) {
568
- allocate(HEADER_SIZE + I8_SIZE);
569
- writeU8(NULL_AND_I8_HEADER);
570
- currentBuffer.writeInt8(next, currentPosition);
571
- currentPosition += I8_SIZE;
572
- i++;
573
- } else if (type === 1) {
574
- allocate(HEADER_SIZE + I32_SIZE);
575
- writeU8(NULL_AND_I32_HEADER);
576
- currentBuffer.writeInt32LE(next, currentPosition);
577
- currentPosition += I32_SIZE;
578
- i++;
579
- } else {
580
- allocate(HEADER_SIZE);
581
- writeU8(NULL_HEADER);
582
- }
583
- } else {
584
- allocate(HEADER_SIZE);
585
- writeU8(NULL_HEADER);
586
- }
587
- } else {
588
- allocate(HEADER_SIZE);
589
- writeU8(NULL_HEADER);
590
- }
591
- } else if (n === 2) {
592
- allocate(HEADER_SIZE);
593
- writeU8(NULL2_HEADER);
594
- } else if (n === 3) {
595
- allocate(HEADER_SIZE);
596
- writeU8(NULL3_HEADER);
597
- } else if (n < 260) {
598
- allocate(HEADER_SIZE + I8_SIZE);
599
- writeU8(NULLS8_HEADER);
600
- writeU8(n - 4);
601
- } else {
602
- allocate(HEADER_SIZE + I32_SIZE);
603
- writeU8(NULLS32_HEADER);
604
- writeU32(n - 260);
605
- }
606
- } else if (Buffer.isBuffer(thing)) {
607
- if (thing.length < 8192) {
608
- allocate(HEADER_SIZE + I32_SIZE + thing.length);
609
- writeU8(BUFFER_HEADER);
610
- writeU32(thing.length);
611
- thing.copy(currentBuffer, currentPosition);
612
- currentPosition += thing.length;
441
+ /** @type {number[]} */
442
+ const sizes = [];
443
+ for (const item of serializedData) {
444
+ /** @type {undefined | number} */
445
+ let last;
446
+ if (typeof item === "function") {
447
+ sizes.push(0);
448
+ } else if (item.length === 0) {
449
+ // ignore
450
+ } else if (
451
+ sizes.length > 0 &&
452
+ (last = sizes[sizes.length - 1]) !== 0
453
+ ) {
454
+ const remaining = 0xffffffff - last;
455
+ if (remaining >= item.length) {
456
+ sizes[sizes.length - 1] += item.length;
613
457
  } else {
614
- allocate(HEADER_SIZE + I32_SIZE);
615
- writeU8(BUFFER_HEADER);
616
- writeU32(thing.length);
617
- flush();
618
- buffers.push(thing);
458
+ sizes.push(item.length - remaining);
459
+ sizes[sizes.length - 2] = 0xffffffff;
619
460
  }
461
+ } else {
462
+ sizes.push(item.length);
620
463
  }
621
- break;
622
464
  }
623
- case "symbol": {
624
- if (thing === MEASURE_START_OPERATION) {
625
- measureStart();
626
- } else if (thing === MEASURE_END_OPERATION) {
627
- const size = measureEnd();
628
- allocate(HEADER_SIZE + I32_SIZE);
629
- writeU8(I32_HEADER);
630
- currentBuffer.writeInt32LE(size, currentPosition);
631
- currentPosition += I32_SIZE;
465
+ const header = Buffer.allocUnsafe(
466
+ HEADER_SIZE + I32_SIZE * (1 + sizes.length)
467
+ );
468
+ header[0] = LAZY_HEADER;
469
+ header.writeUInt32LE(sizes.length, HEADER_SIZE);
470
+ for (let j = 0; j < sizes.length; j++) {
471
+ header.writeUInt32LE(sizes[j], HEADER_SIZE + I32_SIZE * (1 + j));
472
+ }
473
+ write(header);
474
+ for (const item of serializedData) {
475
+ if (typeof item === "function") {
476
+ result.push(item);
477
+ } else {
478
+ write(item);
632
479
  }
633
- break;
634
480
  }
635
- default: {
636
- throw new Error(
637
- `Unknown typeof "${typeof thing}" in binary middleware`
638
- );
481
+ continue;
482
+ } else if (type === "symbol") {
483
+ flush(i);
484
+ sectionStart = i + 1;
485
+ const operation =
486
+ /** @type {MEASURE_START_OPERATION_TYPE | MEASURE_END_OPERATION_TYPE} */
487
+ (/** @type {unknown} */ (thing));
488
+ if (operation === MEASURE_START_OPERATION) {
489
+ measureStack.push(writtenBytes);
490
+ } else if (operation === MEASURE_END_OPERATION) {
491
+ const size =
492
+ writtenBytes - /** @type {number} */ (measureStack.pop());
493
+ writeValuesSection([size], [], []);
639
494
  }
495
+ continue;
496
+ } else {
497
+ sectionSize += ESTIMATED_VALUE_SIZE;
640
498
  }
499
+ if (sectionSize >= MAX_SECTION_SIZE) flush(i + 1);
641
500
  }
642
- flush();
643
-
644
- allocationScope.leftOverBuffer = leftOverBuffer;
645
-
646
- // avoid leaking memory
647
- currentBuffer = null;
648
- leftOverBuffer = null;
649
- allocationScope = /** @type {EXPECTED_ANY} */ (undefined);
650
- const _buffers = buffers;
651
- buffers = /** @type {EXPECTED_ANY} */ (undefined);
652
- return _buffers;
501
+ flush(data.length);
502
+ return result;
653
503
  }
654
504
 
655
505
  /**
506
+ * Restores this instance from the provided deserializer context.
656
507
  * @param {SerializedType} data data
657
508
  * @param {Context} context context object
658
509
  * @returns {DeserializedType | Promise<DeserializedType>} deserialized data
@@ -662,7 +513,7 @@ class BinaryMiddleware extends SerializerMiddleware {
662
513
  }
663
514
 
664
515
  /**
665
- * @private
516
+ * Create lazy deserialized.
666
517
  * @param {SerializedType} content content
667
518
  * @param {Context} context context object
668
519
  * @returns {LazyFunction<DeserializedType, SerializedType>} lazy function
@@ -677,6 +528,7 @@ class BinaryMiddleware extends SerializerMiddleware {
677
528
  }
678
529
 
679
530
  /**
531
+ * Returns new lazy.
680
532
  * @private
681
533
  * @param {LazyFunction<SerializedType, DeserializedType>} fn lazy function
682
534
  * @param {Context} context context object
@@ -689,472 +541,41 @@ class BinaryMiddleware extends SerializerMiddleware {
689
541
  }
690
542
 
691
543
  /**
544
+ * Returns deserialized data.
692
545
  * @param {SerializedType} data data
693
546
  * @param {Context} context context object
694
547
  * @returns {DeserializedType} deserialized data
695
548
  */
696
549
  _deserialize(data, context) {
697
- let currentDataItem = 0;
698
- /** @type {BufferSerializableType | null} */
699
- let currentBuffer = data[0];
700
- let currentIsBuffer = Buffer.isBuffer(currentBuffer);
701
- let currentPosition = 0;
702
-
703
- const retainedBuffer = context.retainedBuffer || ((x) => x);
704
-
705
- const checkOverflow = () => {
706
- if (currentPosition >= /** @type {Buffer} */ (currentBuffer).length) {
707
- currentPosition = 0;
708
- currentDataItem++;
709
- currentBuffer =
710
- currentDataItem < data.length ? data[currentDataItem] : null;
711
- currentIsBuffer = Buffer.isBuffer(currentBuffer);
712
- }
713
- };
714
- /**
715
- * @param {number} n n
716
- * @returns {boolean} true when in current buffer, otherwise false
717
- */
718
- const isInCurrentBuffer = (n) =>
719
- currentIsBuffer &&
720
- n + currentPosition <= /** @type {Buffer} */ (currentBuffer).length;
721
- const ensureBuffer = () => {
722
- if (!currentIsBuffer) {
723
- throw new Error(
724
- currentBuffer === null
725
- ? "Unexpected end of stream"
726
- : "Unexpected lazy element in stream"
727
- );
728
- }
729
- };
730
- /**
731
- * Reads n bytes
732
- * @param {number} n amount of bytes to read
733
- * @returns {Buffer} buffer with bytes
734
- */
735
- const read = (n) => {
736
- ensureBuffer();
737
- const rem =
738
- /** @type {Buffer} */ (currentBuffer).length - currentPosition;
739
- if (rem < n) {
740
- const buffers = [read(rem)];
741
- n -= rem;
742
- ensureBuffer();
743
- while (/** @type {Buffer} */ (currentBuffer).length < n) {
744
- const b = /** @type {Buffer} */ (currentBuffer);
745
- buffers.push(b);
746
- n -= b.length;
747
- currentDataItem++;
748
- currentBuffer =
749
- currentDataItem < data.length ? data[currentDataItem] : null;
750
- currentIsBuffer = Buffer.isBuffer(currentBuffer);
751
- ensureBuffer();
752
- }
753
- buffers.push(read(n));
754
- return Buffer.concat(buffers);
755
- }
756
- const b = /** @type {Buffer} */ (currentBuffer);
757
- const res = Buffer.from(b.buffer, b.byteOffset + currentPosition, n);
758
- currentPosition += n;
759
- checkOverflow();
760
- return res;
761
- };
762
- /**
763
- * Reads up to n bytes
764
- * @param {number} n amount of bytes to read
765
- * @returns {Buffer} buffer with bytes
766
- */
767
- const readUpTo = (n) => {
768
- ensureBuffer();
769
- const rem =
770
- /** @type {Buffer} */
771
- (currentBuffer).length - currentPosition;
772
- if (rem < n) {
773
- n = rem;
774
- }
775
- const b = /** @type {Buffer} */ (currentBuffer);
776
- const res = Buffer.from(b.buffer, b.byteOffset + currentPosition, n);
777
- currentPosition += n;
778
- checkOverflow();
779
- return res;
780
- };
781
- /**
782
- * @returns {number} U8
783
- */
784
- const readU8 = () => {
785
- ensureBuffer();
786
- /**
787
- * There is no need to check remaining buffer size here
788
- * since {@link checkOverflow} guarantees at least one byte remaining
789
- */
790
- const byte =
791
- /** @type {Buffer} */
792
- (currentBuffer).readUInt8(currentPosition);
793
- currentPosition += I8_SIZE;
794
- checkOverflow();
795
- return byte;
796
- };
797
- /**
798
- * @returns {number} U32
799
- */
800
- const readU32 = () => read(I32_SIZE).readUInt32LE(0);
801
- /**
802
- * @param {number} data data
803
- * @param {number} n n
804
- */
805
- const readBits = (data, n) => {
806
- let mask = 1;
807
- while (n !== 0) {
808
- result.push((data & mask) !== 0);
809
- mask <<= 1;
810
- n--;
811
- }
812
- };
813
- const dispatchTable = Array.from({ length: 256 }).map((_, header) => {
814
- switch (header) {
815
- case LAZY_HEADER:
816
- return () => {
817
- const count = readU32();
818
- const lengths = Array.from({ length: count }).map(() => readU32());
819
- /** @type {(Buffer | LazyFunction<SerializedType, DeserializedType>)[]} */
820
- const content = [];
821
- for (let l of lengths) {
822
- if (l === 0) {
823
- if (typeof currentBuffer !== "function") {
824
- throw new Error("Unexpected non-lazy element in stream");
825
- }
826
- content.push(currentBuffer);
827
- currentDataItem++;
828
- currentBuffer =
829
- currentDataItem < data.length ? data[currentDataItem] : null;
830
- currentIsBuffer = Buffer.isBuffer(currentBuffer);
831
- } else {
832
- do {
833
- const buf = readUpTo(l);
834
- l -= buf.length;
835
- content.push(retainedBuffer(buf));
836
- } while (l > 0);
837
- }
838
- }
839
- result.push(this._createLazyDeserialized(content, context));
840
- };
841
- case BUFFER_HEADER:
842
- return () => {
843
- const len = readU32();
844
- result.push(retainedBuffer(read(len)));
845
- };
846
- case TRUE_HEADER:
847
- return () => result.push(true);
848
- case FALSE_HEADER:
849
- return () => result.push(false);
850
- case NULL3_HEADER:
851
- return () => result.push(null, null, null);
852
- case NULL2_HEADER:
853
- return () => result.push(null, null);
854
- case NULL_HEADER:
855
- return () => result.push(null);
856
- case NULL_AND_TRUE_HEADER:
857
- return () => result.push(null, true);
858
- case NULL_AND_FALSE_HEADER:
859
- return () => result.push(null, false);
860
- case NULL_AND_I8_HEADER:
861
- return () => {
862
- if (currentIsBuffer) {
863
- result.push(
864
- null,
865
- /** @type {Buffer} */ (currentBuffer).readInt8(currentPosition)
866
- );
867
- currentPosition += I8_SIZE;
868
- checkOverflow();
869
- } else {
870
- result.push(null, read(I8_SIZE).readInt8(0));
871
- }
872
- };
873
- case NULL_AND_I32_HEADER:
874
- return () => {
875
- result.push(null);
876
- if (isInCurrentBuffer(I32_SIZE)) {
877
- result.push(
878
- /** @type {Buffer} */ (currentBuffer).readInt32LE(
879
- currentPosition
880
- )
881
- );
882
- currentPosition += I32_SIZE;
883
- checkOverflow();
884
- } else {
885
- result.push(read(I32_SIZE).readInt32LE(0));
886
- }
887
- };
888
- case NULLS8_HEADER:
889
- return () => {
890
- const len = readU8() + 4;
891
- for (let i = 0; i < len; i++) {
892
- result.push(null);
893
- }
894
- };
895
- case NULLS32_HEADER:
896
- return () => {
897
- const len = readU32() + 260;
898
- for (let i = 0; i < len; i++) {
899
- result.push(null);
900
- }
901
- };
902
- case BOOLEANS_HEADER:
903
- return () => {
904
- const innerHeader = readU8();
905
- if ((innerHeader & 0xf0) === 0) {
906
- readBits(innerHeader, 3);
907
- } else if ((innerHeader & 0xe0) === 0) {
908
- readBits(innerHeader, 4);
909
- } else if ((innerHeader & 0xc0) === 0) {
910
- readBits(innerHeader, 5);
911
- } else if ((innerHeader & 0x80) === 0) {
912
- readBits(innerHeader, 6);
913
- } else if (innerHeader !== 0xff) {
914
- let count = (innerHeader & 0x7f) + 7;
915
- while (count > 8) {
916
- readBits(readU8(), 8);
917
- count -= 8;
918
- }
919
- readBits(readU8(), count);
920
- } else {
921
- let count = readU32();
922
- while (count > 8) {
923
- readBits(readU8(), 8);
924
- count -= 8;
925
- }
926
- readBits(readU8(), count);
927
- }
928
- };
929
- case STRING_HEADER:
930
- return () => {
931
- const len = readU32();
932
- if (isInCurrentBuffer(len) && currentPosition + len < 0x7fffffff) {
933
- result.push(
934
- /** @type {Buffer} */
935
- (currentBuffer).toString(
936
- undefined,
937
- currentPosition,
938
- currentPosition + len
939
- )
940
- );
941
- currentPosition += len;
942
- checkOverflow();
943
- } else {
944
- result.push(read(len).toString());
945
- }
946
- };
947
- case SHORT_STRING_HEADER:
948
- return () => result.push("");
949
- case SHORT_STRING_HEADER | 1:
950
- return () => {
951
- if (currentIsBuffer && currentPosition < 0x7ffffffe) {
952
- result.push(
953
- /** @type {Buffer} */
954
- (currentBuffer).toString(
955
- "latin1",
956
- currentPosition,
957
- currentPosition + 1
958
- )
959
- );
960
- currentPosition++;
961
- checkOverflow();
962
- } else {
963
- result.push(read(1).toString("latin1"));
964
- }
965
- };
966
- case I8_HEADER:
967
- return () => {
968
- if (currentIsBuffer) {
969
- result.push(
970
- /** @type {Buffer} */ (currentBuffer).readInt8(currentPosition)
971
- );
972
- currentPosition++;
973
- checkOverflow();
974
- } else {
975
- result.push(read(1).readInt8(0));
976
- }
977
- };
978
- case BIGINT_I8_HEADER: {
979
- const len = 1;
980
- return () => {
981
- const need = I8_SIZE * len;
982
-
983
- if (isInCurrentBuffer(need)) {
984
- for (let i = 0; i < len; i++) {
985
- const value =
986
- /** @type {Buffer} */
987
- (currentBuffer).readInt8(currentPosition);
988
- result.push(BigInt(value));
989
- currentPosition += I8_SIZE;
990
- }
991
- checkOverflow();
992
- } else {
993
- const buf = read(need);
994
- for (let i = 0; i < len; i++) {
995
- const value = buf.readInt8(i * I8_SIZE);
996
- result.push(BigInt(value));
997
- }
998
- }
999
- };
1000
- }
1001
- case BIGINT_I32_HEADER: {
1002
- const len = 1;
1003
- return () => {
1004
- const need = I32_SIZE * len;
1005
- if (isInCurrentBuffer(need)) {
1006
- for (let i = 0; i < len; i++) {
1007
- const value = /** @type {Buffer} */ (currentBuffer).readInt32LE(
1008
- currentPosition
1009
- );
1010
- result.push(BigInt(value));
1011
- currentPosition += I32_SIZE;
1012
- }
1013
- checkOverflow();
1014
- } else {
1015
- const buf = read(need);
1016
- for (let i = 0; i < len; i++) {
1017
- const value = buf.readInt32LE(i * I32_SIZE);
1018
- result.push(BigInt(value));
1019
- }
1020
- }
1021
- };
1022
- }
1023
- case BIGINT_HEADER: {
1024
- return () => {
1025
- const len = readU32();
1026
- if (isInCurrentBuffer(len) && currentPosition + len < 0x7fffffff) {
1027
- const value =
1028
- /** @type {Buffer} */
1029
- (currentBuffer).toString(
1030
- undefined,
1031
- currentPosition,
1032
- currentPosition + len
1033
- );
1034
-
1035
- result.push(BigInt(value));
1036
- currentPosition += len;
1037
- checkOverflow();
1038
- } else {
1039
- const value = read(len).toString();
1040
- result.push(BigInt(value));
1041
- }
1042
- };
550
+ const state = new ReadState(data, context);
551
+ /** @type {DeserializedType | undefined} */
552
+ let result;
553
+ while (state.currentBuffer !== null) {
554
+ /** @type {DeserializedType} */
555
+ let part;
556
+ if (typeof state.currentBuffer === "function") {
557
+ part = [this._deserializeLazy(state.currentBuffer, context)];
558
+ state.nextDataItem();
559
+ } else {
560
+ const header = state.readU8();
561
+ if (header === VALUES_HEADER) {
562
+ part = readValuesSection(state);
563
+ } else if (header === LAZY_HEADER) {
564
+ part = [
565
+ this._createLazyDeserialized(readLazySection(state), context)
566
+ ];
567
+ } else {
568
+ throw new Error(`Unexpected header byte 0x${header.toString(16)}`);
1043
569
  }
1044
- default:
1045
- if (header <= 10) {
1046
- return () => result.push(header);
1047
- } else if ((header & SHORT_STRING_HEADER) === SHORT_STRING_HEADER) {
1048
- const len = header & SHORT_STRING_LENGTH_MASK;
1049
- return () => {
1050
- if (
1051
- isInCurrentBuffer(len) &&
1052
- currentPosition + len < 0x7fffffff
1053
- ) {
1054
- result.push(
1055
- /** @type {Buffer} */
1056
- (currentBuffer).toString(
1057
- "latin1",
1058
- currentPosition,
1059
- currentPosition + len
1060
- )
1061
- );
1062
- currentPosition += len;
1063
- checkOverflow();
1064
- } else {
1065
- result.push(read(len).toString("latin1"));
1066
- }
1067
- };
1068
- } else if ((header & NUMBERS_HEADER_MASK) === F64_HEADER) {
1069
- const len = (header & NUMBERS_COUNT_MASK) + 1;
1070
- return () => {
1071
- const need = F64_SIZE * len;
1072
- if (isInCurrentBuffer(need)) {
1073
- for (let i = 0; i < len; i++) {
1074
- result.push(
1075
- /** @type {Buffer} */ (currentBuffer).readDoubleLE(
1076
- currentPosition
1077
- )
1078
- );
1079
- currentPosition += F64_SIZE;
1080
- }
1081
- checkOverflow();
1082
- } else {
1083
- const buf = read(need);
1084
- for (let i = 0; i < len; i++) {
1085
- result.push(buf.readDoubleLE(i * F64_SIZE));
1086
- }
1087
- }
1088
- };
1089
- } else if ((header & NUMBERS_HEADER_MASK) === I32_HEADER) {
1090
- const len = (header & NUMBERS_COUNT_MASK) + 1;
1091
- return () => {
1092
- const need = I32_SIZE * len;
1093
- if (isInCurrentBuffer(need)) {
1094
- for (let i = 0; i < len; i++) {
1095
- result.push(
1096
- /** @type {Buffer} */ (currentBuffer).readInt32LE(
1097
- currentPosition
1098
- )
1099
- );
1100
- currentPosition += I32_SIZE;
1101
- }
1102
- checkOverflow();
1103
- } else {
1104
- const buf = read(need);
1105
- for (let i = 0; i < len; i++) {
1106
- result.push(buf.readInt32LE(i * I32_SIZE));
1107
- }
1108
- }
1109
- };
1110
- } else if ((header & NUMBERS_HEADER_MASK) === I8_HEADER) {
1111
- const len = (header & NUMBERS_COUNT_MASK) + 1;
1112
- return () => {
1113
- const need = I8_SIZE * len;
1114
- if (isInCurrentBuffer(need)) {
1115
- for (let i = 0; i < len; i++) {
1116
- result.push(
1117
- /** @type {Buffer} */ (currentBuffer).readInt8(
1118
- currentPosition
1119
- )
1120
- );
1121
- currentPosition += I8_SIZE;
1122
- }
1123
- checkOverflow();
1124
- } else {
1125
- const buf = read(need);
1126
- for (let i = 0; i < len; i++) {
1127
- result.push(buf.readInt8(i * I8_SIZE));
1128
- }
1129
- }
1130
- };
1131
- }
1132
- return () => {
1133
- throw new Error(`Unexpected header byte 0x${header.toString(16)}`);
1134
- };
1135
570
  }
1136
- });
1137
-
1138
- /** @type {DeserializedType} */
1139
- let result = [];
1140
- while (currentBuffer !== null) {
1141
- if (typeof currentBuffer === "function") {
1142
- result.push(this._deserializeLazy(currentBuffer, context));
1143
- currentDataItem++;
1144
- currentBuffer =
1145
- currentDataItem < data.length ? data[currentDataItem] : null;
1146
- currentIsBuffer = Buffer.isBuffer(currentBuffer);
571
+ // a single-section stream needs no copying: reuse its array as the result
572
+ if (result === undefined) {
573
+ result = part;
1147
574
  } else {
1148
- const header = readU8();
1149
- dispatchTable[header]();
575
+ for (let j = 0; j < part.length; j++) result.push(part[j]);
1150
576
  }
1151
577
  }
1152
-
1153
- // avoid leaking memory in context
1154
- // eslint-disable-next-line prefer-const
1155
- let _result = result;
1156
- result = /** @type {EXPECTED_ANY} */ (undefined);
1157
- return _result;
578
+ return result === undefined ? [] : result;
1158
579
  }
1159
580
  }
1160
581