webpack 5.107.2 → 5.108.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.
- package/hot/dev-server.js +2 -0
- package/lib/APIPlugin.js +8 -4
- package/lib/CacheFacade.js +7 -0
- package/lib/Chunk.js +4 -0
- package/lib/ChunkGraph.js +30 -9
- package/lib/CircularModulesPlugin.js +190 -0
- package/lib/CleanPlugin.js +2 -1
- package/lib/Compilation.js +396 -65
- package/lib/Compiler.js +25 -11
- package/lib/ConcatenationScope.js +10 -3
- package/lib/ContextExclusionPlugin.js +1 -0
- package/lib/ContextModule.js +92 -47
- package/lib/ContextReplacementPlugin.js +4 -0
- package/lib/DefinePlugin.js +89 -15
- package/lib/Dependency.js +111 -7
- package/lib/EntryOptionPlugin.js +39 -2
- package/lib/EntryPlugin.js +2 -0
- package/lib/ExportsInfo.js +314 -73
- package/lib/ExternalModule.js +82 -34
- package/lib/ExternalModuleFactoryPlugin.js +1 -0
- package/lib/ExternalsPlugin.js +1 -0
- package/lib/FileSystemInfo.js +173 -23
- package/lib/FlagAllModulesAsUsedPlugin.js +1 -5
- package/lib/FlagDependencyExportsPlugin.js +23 -0
- package/lib/FlagDependencyUsagePlugin.js +87 -5
- package/lib/FlagEntryExportAsUsedPlugin.js +2 -0
- package/lib/Generator.js +8 -0
- package/lib/HotModuleReplacementPlugin.js +65 -28
- package/lib/IgnorePlugin.js +1 -0
- package/lib/InitFragment.js +5 -0
- package/lib/JavascriptMetaInfoPlugin.js +7 -5
- package/lib/LazyBarrel.js +361 -0
- package/lib/LoaderTargetPlugin.js +1 -0
- package/lib/Module.js +21 -42
- package/lib/ModuleGraph.js +3 -2
- package/lib/ModuleProfile.js +27 -1
- package/lib/ModuleTemplate.js +2 -0
- package/lib/MultiCompiler.js +2 -0
- package/lib/MultiStats.js +1 -0
- package/lib/MultiWatching.js +2 -0
- package/lib/NodeStuffPlugin.js +22 -17
- package/lib/NormalModule.js +142 -80
- package/lib/NormalModuleReplacementPlugin.js +2 -0
- package/lib/PrefetchPlugin.js +2 -0
- package/lib/ProgressPlugin.js +8 -0
- package/lib/ProvidePlugin.js +1 -0
- package/lib/RawModule.js +20 -16
- package/lib/RecordIdsPlugin.js +1 -0
- package/lib/RuntimeGlobals.js +5 -0
- package/lib/RuntimePlugin.js +58 -26
- package/lib/RuntimeTemplate.js +278 -21
- package/lib/SelfModuleFactory.js +1 -0
- package/lib/SourceMapDevToolPlugin.js +3 -5
- package/lib/Stats.js +1 -0
- package/lib/Template.js +8 -2
- package/lib/TemplatedPathPlugin.js +473 -131
- package/lib/WarnCaseSensitiveModulesPlugin.js +1 -0
- package/lib/WarnDeprecatedOptionPlugin.js +4 -0
- package/lib/WarnNoModeSetPlugin.js +1 -0
- package/lib/WatchIgnorePlugin.js +1 -0
- package/lib/Watching.js +9 -0
- package/lib/WebpackOptionsApply.js +50 -5
- package/lib/asset/AssetBytesGenerator.js +11 -3
- package/lib/asset/AssetGenerator.js +47 -22
- package/lib/asset/AssetModule.js +47 -0
- package/lib/asset/AssetModulesPlugin.js +14 -14
- package/lib/asset/AssetParser.js +2 -2
- package/lib/asset/AssetSourceGenerator.js +8 -0
- package/lib/asset/RawDataUrlModule.js +12 -13
- package/lib/buildChunkGraph.js +64 -7
- package/lib/bun/BunTargetPlugin.js +48 -0
- package/lib/cache/AddBuildDependenciesPlugin.js +1 -0
- package/lib/cache/AddManagedPathsPlugin.js +3 -0
- package/lib/cache/IdleFileCachePlugin.js +4 -0
- package/lib/cache/MemoryWithGcCachePlugin.js +1 -0
- package/lib/cache/PackFileCacheStrategy.js +1 -0
- package/lib/cache/ResolverCachePlugin.js +2 -0
- package/lib/cache/mergeEtags.js +2 -0
- package/lib/cli.js +109 -19
- package/lib/config/browserslistTargetHandler.js +99 -0
- package/lib/config/defaults.js +147 -7
- package/lib/config/defineConfig.js +31 -0
- package/lib/config/normalization.js +5 -0
- package/lib/config/target.js +181 -2
- package/lib/container/ContainerEntryModule.js +15 -14
- package/lib/container/ContainerExposedDependency.js +2 -2
- package/lib/container/ContainerReferencePlugin.js +1 -1
- package/lib/container/FallbackDependency.js +5 -7
- package/lib/container/FallbackModule.js +10 -9
- package/lib/container/RemoteModule.js +20 -10
- package/lib/container/RemoteRuntimeModule.js +14 -12
- package/lib/css/CssGenerator.js +353 -327
- package/lib/css/CssInjectStyleRuntimeModule.js +36 -8
- package/lib/css/CssLoadingRuntimeModule.js +118 -47
- package/lib/css/CssModule.js +52 -23
- package/lib/css/CssModulesPlugin.js +107 -124
- package/lib/css/CssParser.js +2759 -2205
- package/lib/css/syntax.js +2859 -0
- package/lib/debug/ProfilingPlugin.js +2 -0
- package/lib/deno/DenoTargetPlugin.js +47 -0
- package/lib/dependencies/AMDDefineDependency.js +22 -17
- package/lib/dependencies/AMDDefineDependencyParserPlugin.js +1 -0
- package/lib/dependencies/AMDRequireArrayDependency.js +7 -11
- package/lib/dependencies/AMDRequireContextDependency.js +7 -11
- package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +1 -0
- package/lib/dependencies/AMDRequireDependency.js +24 -20
- package/lib/dependencies/CachedConstDependency.js +3 -0
- package/lib/dependencies/CommonJsDependencyHelpers.js +1 -0
- package/lib/dependencies/CommonJsExportRequireDependency.js +79 -31
- package/lib/dependencies/CommonJsExportsDependency.js +21 -16
- package/lib/dependencies/CommonJsExportsParserPlugin.js +230 -7
- package/lib/dependencies/CommonJsFullRequireDependency.js +27 -19
- package/lib/dependencies/CommonJsImportsParserPlugin.js +51 -16
- package/lib/dependencies/CommonJsPlugin.js +1 -1
- package/lib/dependencies/CommonJsRequireContextDependency.js +10 -13
- package/lib/dependencies/CommonJsRequireDependency.js +42 -11
- package/lib/dependencies/CommonJsSelfReferenceDependency.js +22 -14
- package/lib/dependencies/ConstDependency.js +16 -11
- package/lib/dependencies/ContextDependency.js +4 -0
- package/lib/dependencies/ContextDependencyTemplateAsId.js +1 -1
- package/lib/dependencies/ContextElementDependency.js +23 -14
- package/lib/dependencies/CreateRequireParserPlugin.js +1 -0
- package/lib/dependencies/CreateScriptUrlDependency.js +5 -7
- package/lib/dependencies/CriticalDependencyWarning.js +1 -0
- package/lib/dependencies/CssIcssExportDependency.js +512 -574
- package/lib/dependencies/CssIcssImportDependency.js +9 -9
- package/lib/dependencies/CssIcssSymbolDependency.js +22 -15
- package/lib/dependencies/CssImportDependency.js +25 -1
- package/lib/dependencies/CssUrlDependency.js +23 -14
- package/lib/dependencies/DllEntryDependency.js +9 -13
- package/lib/dependencies/ExportBindingInitFragment.js +164 -0
- package/lib/dependencies/ExportsInfoDependency.js +12 -12
- package/lib/dependencies/ExternalModuleDependency.js +8 -5
- package/lib/dependencies/ExternalModuleInitFragment.js +7 -5
- package/lib/dependencies/ExternalModuleInitFragmentDependency.js +13 -10
- package/lib/dependencies/HarmonyAcceptDependency.js +11 -11
- package/lib/dependencies/HarmonyAcceptImportDependency.js +1 -0
- package/lib/dependencies/HarmonyDetectionParserPlugin.js +47 -22
- package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +28 -2
- package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +46 -22
- package/lib/dependencies/HarmonyExportExpressionDependency.js +107 -30
- package/lib/dependencies/HarmonyExportHeaderDependency.js +7 -9
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +109 -31
- package/lib/dependencies/HarmonyExportInitFragment.js +18 -15
- package/lib/dependencies/HarmonyExportSpecifierDependency.js +83 -18
- package/lib/dependencies/HarmonyExports.js +4 -1
- package/lib/dependencies/HarmonyImportDependency.js +24 -0
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +110 -74
- package/lib/dependencies/HarmonyImportGuard.js +254 -0
- package/lib/dependencies/HarmonyImportSideEffectDependency.js +18 -1
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +118 -14
- package/lib/dependencies/HarmonyLinkingError.js +1 -0
- package/lib/dependencies/HarmonyModulesPlugin.js +1 -0
- package/lib/dependencies/{HtmlScriptSrcDependency.js → HtmlEntryDependency.js} +261 -109
- package/lib/dependencies/HtmlInlineHtmlDependency.js +107 -0
- package/lib/dependencies/HtmlInlineScriptDependency.js +17 -13
- package/lib/dependencies/HtmlInlineStyleDependency.js +45 -11
- package/lib/dependencies/ImportContextDependency.js +5 -9
- package/lib/dependencies/ImportDependency.js +44 -2
- package/lib/dependencies/ImportMetaHotAcceptDependency.js +1 -0
- package/lib/dependencies/ImportMetaHotDeclineDependency.js +1 -0
- package/lib/dependencies/ImportMetaPlugin.js +74 -30
- package/lib/dependencies/ImportParserPlugin.js +19 -0
- package/lib/dependencies/ImportWeakDependency.js +1 -0
- package/lib/dependencies/JsonExportsDependency.js +9 -9
- package/lib/dependencies/LoaderImportDependency.js +1 -0
- package/lib/dependencies/LocalModule.js +11 -12
- package/lib/dependencies/LocalModuleDependency.js +11 -13
- package/lib/dependencies/ModuleDecoratorDependency.js +9 -9
- package/lib/dependencies/ModuleDependency.js +7 -0
- package/lib/dependencies/ModuleHotAcceptDependency.js +1 -0
- package/lib/dependencies/ModuleHotDeclineDependency.js +1 -0
- package/lib/dependencies/ModuleInitFragmentDependency.js +15 -11
- package/lib/dependencies/ProvidedDependency.js +31 -27
- package/lib/dependencies/PureExpressionDependency.js +7 -9
- package/lib/dependencies/RequireEnsureDependency.js +12 -13
- package/lib/dependencies/RequireHeaderDependency.js +3 -4
- package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +3 -0
- package/lib/dependencies/RequireResolveContextDependency.js +7 -11
- package/lib/dependencies/RequireResolveDependency.js +1 -0
- package/lib/dependencies/RequireResolveHeaderDependency.js +3 -5
- package/lib/dependencies/RuntimeRequirementsDependency.js +6 -7
- package/lib/dependencies/StaticExportsDependency.js +10 -10
- package/lib/dependencies/SystemPlugin.js +2 -0
- package/lib/dependencies/URLContextDependency.js +5 -7
- package/lib/dependencies/URLDependency.js +14 -16
- package/lib/dependencies/UnsupportedDependency.js +8 -13
- package/lib/dependencies/WebAssemblyExportImportedDependency.js +11 -16
- package/lib/dependencies/WebAssemblyImportDependency.js +14 -16
- package/lib/dependencies/WebpackIsIncludedDependency.js +1 -0
- package/lib/dependencies/WorkerDependency.js +19 -8
- package/lib/dependencies/WorkerPlugin.js +22 -6
- package/lib/dependencies/processExportInfo.js +13 -11
- package/lib/dll/DelegatedModule.js +29 -15
- package/lib/dll/DelegatedModuleFactoryPlugin.js +1 -0
- package/lib/dll/DelegatedPlugin.js +1 -0
- package/lib/dll/DllEntryPlugin.js +3 -0
- package/lib/dll/DllModule.js +3 -2
- package/lib/dll/DllPlugin.js +16 -0
- package/lib/dll/DllReferencePlugin.js +3 -0
- package/lib/dll/LibManifestPlugin.js +1 -0
- package/lib/electron/ElectronTargetPlugin.js +22 -4
- package/lib/errors/ConcurrentCompilationError.js +1 -0
- package/lib/errors/HookWebpackError.js +11 -13
- package/lib/errors/IgnoreErrorModuleFactory.js +1 -0
- package/lib/errors/InvalidDependenciesModuleWarning.js +2 -0
- package/lib/errors/JSONParseError.js +9 -8
- package/lib/errors/ModuleBuildError.js +16 -13
- package/lib/errors/ModuleDependencyError.js +8 -1
- package/lib/errors/ModuleDependencyWarning.js +8 -1
- package/lib/errors/ModuleError.js +5 -11
- package/lib/errors/ModuleHashingError.js +4 -0
- package/lib/errors/ModuleNotFoundError.js +3 -0
- package/lib/errors/ModuleParseError.js +5 -11
- package/lib/errors/ModuleRestoreError.js +2 -0
- package/lib/errors/ModuleStoreError.js +3 -0
- package/lib/errors/ModuleWarning.js +7 -11
- package/lib/errors/NodeStuffInWebError.js +1 -0
- package/lib/errors/NonErrorEmittedError.js +2 -0
- package/lib/errors/UnhandledSchemeError.js +1 -0
- package/lib/esm/ModuleChunkLoadingPlugin.js +0 -1
- package/lib/esm/ModuleChunkLoadingRuntimeModule.js +28 -23
- package/lib/hmr/HotModuleReplacement.runtime.js +175 -30
- package/lib/hmr/HotModuleReplacementRuntimeModule.js +7 -0
- package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +173 -26
- package/lib/hmr/LazyCompilationPlugin.js +30 -6
- package/lib/html/HtmlGenerator.js +217 -23
- package/lib/html/HtmlModule.js +40 -0
- package/lib/html/HtmlModulesPlugin.js +219 -55
- package/lib/html/HtmlParser.js +1108 -1099
- package/lib/html/{walkHtmlTokens.js → syntax.js} +4701 -212
- package/lib/ids/IdHelpers.js +4 -2
- package/lib/index.js +19 -0
- package/lib/javascript/BasicEvaluatedExpression.js +1 -0
- package/lib/javascript/EnableChunkLoadingPlugin.js +1 -0
- package/lib/javascript/JavascriptGenerator.js +6 -2
- package/lib/javascript/JavascriptModule.js +54 -0
- package/lib/javascript/JavascriptModulesPlugin.js +257 -102
- package/lib/javascript/JavascriptParser.js +285 -158
- package/lib/json/JsonModule.js +40 -0
- package/lib/json/JsonModulesPlugin.js +7 -0
- package/lib/json/JsonParser.js +4 -2
- package/lib/library/AssignLibraryPlugin.js +5 -3
- package/lib/library/ExportPropertyLibraryPlugin.js +1 -0
- package/lib/library/FalseIIFEUmdWarning.js +1 -0
- package/lib/library/ModuleLibraryPlugin.js +24 -5
- package/lib/library/SystemLibraryPlugin.js +3 -3
- package/lib/node/NodeTargetPlugin.js +1 -0
- package/lib/node/NodeWatchFileSystem.js +1 -0
- package/lib/node/ReadFileChunkLoadingRuntimeModule.js +10 -9
- package/lib/node/RequireChunkLoadingRuntimeModule.js +18 -16
- package/lib/optimize/ConcatenatedModule.js +395 -77
- package/lib/optimize/ConstExportsPlugin.js +211 -0
- package/lib/optimize/InlineExports.js +178 -0
- package/lib/optimize/InnerGraph.js +372 -275
- package/lib/optimize/InnerGraphPlugin.js +196 -99
- package/lib/optimize/ModuleConcatenationPlugin.js +56 -25
- package/lib/optimize/RealContentHashPlugin.js +14 -1
- package/lib/optimize/SideEffectsFlagPlugin.js +234 -64
- package/lib/runtime/AsyncModuleRuntimeModule.js +32 -30
- package/lib/runtime/AutoPublicPathRuntimeModule.js +11 -5
- package/lib/runtime/CompatGetDefaultExportRuntimeModule.js +1 -1
- package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +6 -4
- package/lib/runtime/DefinePropertyGettersRuntimeModule.js +33 -4
- package/lib/runtime/GetChunkFilenameRuntimeModule.js +82 -3
- package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -1
- package/lib/runtime/HasOwnPropertyRuntimeModule.js +1 -1
- package/lib/runtime/LoadScriptRuntimeModule.js +18 -13
- package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +39 -26
- package/lib/runtime/MakeNamespaceObjectRuntimeModule.js +1 -1
- package/lib/runtime/OnChunksLoadedRuntimeModule.js +4 -4
- package/lib/runtime/RelativeUrlRuntimeModule.js +3 -2
- package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +1 -1
- package/lib/runtime/StartupEntrypointRuntimeModule.js +4 -3
- package/lib/runtime/WorkerRuntimeModule.js +33 -0
- package/lib/schemes/HttpUriPlugin.js +3 -2
- package/lib/serialization/AggregateErrorSerializer.js +7 -6
- package/lib/serialization/ArraySerializer.js +4 -8
- package/lib/serialization/BinaryMiddleware.js +538 -468
- package/lib/serialization/DateObjectSerializer.js +2 -2
- package/lib/serialization/ErrorObjectSerializer.js +7 -6
- package/lib/serialization/MapObjectSerializer.js +5 -9
- package/lib/serialization/NullPrototypeObjectSerializer.js +7 -9
- package/lib/serialization/ObjectMiddleware.js +50 -13
- package/lib/serialization/PlainObjectSerializer.js +9 -8
- package/lib/serialization/RegExpObjectSerializer.js +2 -2
- package/lib/serialization/Serializer.js +1 -0
- package/lib/serialization/SetObjectSerializer.js +4 -8
- package/lib/sharing/ConsumeSharedModule.js +6 -7
- package/lib/sharing/ConsumeSharedPlugin.js +1 -0
- package/lib/sharing/ConsumeSharedRuntimeModule.js +46 -41
- package/lib/sharing/ProvideSharedDependency.js +30 -15
- package/lib/sharing/ProvideSharedModule.js +30 -11
- package/lib/sharing/ProvideSharedPlugin.js +4 -2
- package/lib/sharing/SharePlugin.js +3 -0
- package/lib/sharing/ShareRuntimeModule.js +18 -16
- package/lib/sharing/resolveMatchedConfigs.js +2 -1
- package/lib/sharing/utils.js +1 -1
- package/lib/stats/DefaultStatsFactoryPlugin.js +4 -6
- package/lib/stats/DefaultStatsPrinterPlugin.js +14 -14
- package/lib/stats/StatsFactory.js +11 -9
- package/lib/stats/StatsPrinter.js +9 -2
- package/lib/url/URLParserPlugin.js +5 -2
- package/lib/util/AsyncQueue.js +10 -0
- package/lib/util/LazyBucketSortedSet.js +5 -0
- package/lib/util/LazySet.js +6 -4
- package/lib/util/LocConverter.js +11 -1
- package/lib/util/Semaphore.js +1 -0
- package/lib/util/SourceProcessor.js +106 -0
- package/lib/util/TupleSet.js +1 -0
- package/lib/util/WeakTupleMap.js +27 -1
- package/lib/util/chainedImports.js +3 -3
- package/lib/util/comparators.js +2 -2
- package/lib/util/concatenate.js +31 -7
- package/lib/util/createHash.js +0 -1
- package/lib/util/deterministicGrouping.js +19 -12
- package/lib/util/extractSourceMap.js +1 -1
- package/lib/util/findGraphRoots.js +2 -0
- package/lib/util/fs.js +10 -5
- package/lib/util/hash/DebugHash.js +1 -0
- package/lib/util/hash/hash-digest.js +24 -15
- package/lib/util/identifier.js +7 -0
- package/lib/util/internalSerializables.js +11 -2
- package/lib/util/magicComment.js +42 -0
- package/lib/util/makeSerializable.js +1 -0
- package/lib/util/nonNumericOnlyHash.js +30 -1
- package/lib/util/property.js +7 -1
- package/lib/util/publicPathPlaceholder.js +64 -0
- package/lib/util/registerExternalSerializer.js +4 -4
- package/lib/wasm-async/AsyncWasmModule.js +77 -0
- package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +1 -1
- package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +1 -96
- package/lib/wasm-async/AsyncWebAssemblyParser.js +1 -1
- package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +1 -1
- package/lib/wasm-sync/SyncWasmModule.js +39 -0
- package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +1 -0
- package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +6 -3
- package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +2 -1
- package/lib/wasm-sync/WebAssemblyGenerator.js +1 -0
- package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +2 -0
- package/lib/wasm-sync/WebAssemblyModulesPlugin.js +10 -0
- package/lib/wasm-sync/WebAssemblyParser.js +7 -3
- package/lib/web/JsonpChunkLoadingRuntimeModule.js +45 -39
- package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +11 -10
- package/package.json +35 -27
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +212 -3
- package/schemas/plugins/HtmlGeneratorOptions.check.js +1 -1
- package/schemas/plugins/HtmlParserOptions.check.d.ts +7 -0
- package/schemas/plugins/HtmlParserOptions.check.js +6 -0
- package/schemas/plugins/HtmlParserOptions.json +3 -0
- package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
- package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
- package/schemas/plugins/css/CssParserOptions.check.js +1 -1
- package/types.d.ts +1456 -290
- package/lib/css/walkCssTokens.js +0 -2020
- package/lib/util/AppendOnlyStackedSet.js +0 -93
|
@@ -84,6 +84,7 @@ const NULL3_HEADER = 0x12;
|
|
|
84
84
|
const NULLS8_HEADER = 0x13;
|
|
85
85
|
const NULLS32_HEADER = 0x14;
|
|
86
86
|
const NULL_AND_I8_HEADER = 0x15;
|
|
87
|
+
const NULL_AND_I16_HEADER = 0x19;
|
|
87
88
|
const NULL_AND_I32_HEADER = 0x16;
|
|
88
89
|
const NULL_AND_TRUE_HEADER = 0x17;
|
|
89
90
|
const NULL_AND_FALSE_HEADER = 0x18;
|
|
@@ -104,6 +105,7 @@ const SHORT_STRING_LENGTH_MASK = 0x7f; // 0b0111_1111
|
|
|
104
105
|
|
|
105
106
|
const HEADER_SIZE = 1;
|
|
106
107
|
const I8_SIZE = 1;
|
|
108
|
+
const I16_SIZE = 2;
|
|
107
109
|
const I32_SIZE = 4;
|
|
108
110
|
const F64_SIZE = 8;
|
|
109
111
|
|
|
@@ -148,6 +150,521 @@ const identifyBigInt = (n) => {
|
|
|
148
150
|
* @typedef {import("./SerializerMiddleware").LazyFunction<LazyInputValue, LazyOutputValue, BinaryMiddleware, undefined>} LazyFunction
|
|
149
151
|
*/
|
|
150
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Mutable read state of one `_deserialize` run; the module-level dispatch
|
|
155
|
+
* table operates on this instead of per-call closures.
|
|
156
|
+
*/
|
|
157
|
+
class ReadState {
|
|
158
|
+
/**
|
|
159
|
+
* @param {SerializedType} data data
|
|
160
|
+
* @param {Context} context context object
|
|
161
|
+
* @param {BinaryMiddleware} middleware binary middleware
|
|
162
|
+
*/
|
|
163
|
+
constructor(data, context, middleware) {
|
|
164
|
+
/** @type {SerializedType} */
|
|
165
|
+
this.data = data;
|
|
166
|
+
/** @type {Context} */
|
|
167
|
+
this.context = context;
|
|
168
|
+
/** @type {BinaryMiddleware} */
|
|
169
|
+
this.middleware = middleware;
|
|
170
|
+
this.retainedBuffer = context.retainedBuffer || ((x) => x);
|
|
171
|
+
/** @type {number} */
|
|
172
|
+
this.currentDataItem = 0;
|
|
173
|
+
/** @type {BufferSerializableType | null} */
|
|
174
|
+
this.currentBuffer = data[0];
|
|
175
|
+
/** @type {boolean} */
|
|
176
|
+
this.currentIsBuffer = Buffer.isBuffer(this.currentBuffer);
|
|
177
|
+
/** @type {number} */
|
|
178
|
+
this.currentPosition = 0;
|
|
179
|
+
/** @type {DeserializedType} */
|
|
180
|
+
this.result = [];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Advances to the next data item (does not reset the position). */
|
|
184
|
+
nextDataItem() {
|
|
185
|
+
this.currentDataItem++;
|
|
186
|
+
this.currentBuffer =
|
|
187
|
+
this.currentDataItem < this.data.length
|
|
188
|
+
? this.data[this.currentDataItem]
|
|
189
|
+
: null;
|
|
190
|
+
this.currentIsBuffer = Buffer.isBuffer(this.currentBuffer);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
checkOverflow() {
|
|
194
|
+
if (
|
|
195
|
+
this.currentPosition >= /** @type {Buffer} */ (this.currentBuffer).length
|
|
196
|
+
) {
|
|
197
|
+
this.currentPosition = 0;
|
|
198
|
+
this.nextDataItem();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Checks whether n bytes are available in the current buffer.
|
|
204
|
+
* @param {number} n n
|
|
205
|
+
* @returns {boolean} true when in current buffer, otherwise false
|
|
206
|
+
*/
|
|
207
|
+
isInCurrentBuffer(n) {
|
|
208
|
+
return (
|
|
209
|
+
this.currentIsBuffer &&
|
|
210
|
+
n + this.currentPosition <=
|
|
211
|
+
/** @type {Buffer} */ (this.currentBuffer).length
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
ensureBuffer() {
|
|
216
|
+
if (!this.currentIsBuffer) {
|
|
217
|
+
throw new Error(
|
|
218
|
+
this.currentBuffer === null
|
|
219
|
+
? "Unexpected end of stream"
|
|
220
|
+
: "Unexpected lazy element in stream"
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Returns buffer with bytes.
|
|
227
|
+
* @param {number} n amount of bytes to read
|
|
228
|
+
* @returns {Buffer} buffer with bytes
|
|
229
|
+
*/
|
|
230
|
+
read(n) {
|
|
231
|
+
this.ensureBuffer();
|
|
232
|
+
const rem =
|
|
233
|
+
/** @type {Buffer} */ (this.currentBuffer).length - this.currentPosition;
|
|
234
|
+
if (rem < n) {
|
|
235
|
+
const buffers = [this.read(rem)];
|
|
236
|
+
n -= rem;
|
|
237
|
+
this.ensureBuffer();
|
|
238
|
+
while (/** @type {Buffer} */ (this.currentBuffer).length < n) {
|
|
239
|
+
const b = /** @type {Buffer} */ (this.currentBuffer);
|
|
240
|
+
buffers.push(b);
|
|
241
|
+
n -= b.length;
|
|
242
|
+
this.nextDataItem();
|
|
243
|
+
this.ensureBuffer();
|
|
244
|
+
}
|
|
245
|
+
buffers.push(this.read(n));
|
|
246
|
+
return Buffer.concat(buffers);
|
|
247
|
+
}
|
|
248
|
+
const b = /** @type {Buffer} */ (this.currentBuffer);
|
|
249
|
+
const res = Buffer.from(b.buffer, b.byteOffset + this.currentPosition, n);
|
|
250
|
+
this.currentPosition += n;
|
|
251
|
+
this.checkOverflow();
|
|
252
|
+
return res;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Reads up to n bytes.
|
|
257
|
+
* @param {number} n amount of bytes to read
|
|
258
|
+
* @returns {Buffer} buffer with bytes
|
|
259
|
+
*/
|
|
260
|
+
readUpTo(n) {
|
|
261
|
+
this.ensureBuffer();
|
|
262
|
+
const rem =
|
|
263
|
+
/** @type {Buffer} */ (this.currentBuffer).length - this.currentPosition;
|
|
264
|
+
if (rem < n) {
|
|
265
|
+
n = rem;
|
|
266
|
+
}
|
|
267
|
+
const b = /** @type {Buffer} */ (this.currentBuffer);
|
|
268
|
+
const res = Buffer.from(b.buffer, b.byteOffset + this.currentPosition, n);
|
|
269
|
+
this.currentPosition += n;
|
|
270
|
+
this.checkOverflow();
|
|
271
|
+
return res;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Returns u8.
|
|
276
|
+
* @returns {number} U8
|
|
277
|
+
*/
|
|
278
|
+
readU8() {
|
|
279
|
+
this.ensureBuffer();
|
|
280
|
+
/**
|
|
281
|
+
* There is no need to check remaining buffer size here
|
|
282
|
+
* since {@link ReadState#checkOverflow} guarantees at least one byte remaining
|
|
283
|
+
*/
|
|
284
|
+
const byte =
|
|
285
|
+
/** @type {Buffer} */
|
|
286
|
+
(this.currentBuffer).readUInt8(this.currentPosition);
|
|
287
|
+
this.currentPosition += I8_SIZE;
|
|
288
|
+
this.checkOverflow();
|
|
289
|
+
return byte;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Returns u32.
|
|
294
|
+
* @returns {number} U32
|
|
295
|
+
*/
|
|
296
|
+
readU32() {
|
|
297
|
+
// fast path avoids allocating a 4-byte view per length read
|
|
298
|
+
if (this.isInCurrentBuffer(I32_SIZE)) {
|
|
299
|
+
const value =
|
|
300
|
+
/** @type {Buffer} */
|
|
301
|
+
(this.currentBuffer).readUInt32LE(this.currentPosition);
|
|
302
|
+
this.currentPosition += I32_SIZE;
|
|
303
|
+
this.checkOverflow();
|
|
304
|
+
return value;
|
|
305
|
+
}
|
|
306
|
+
return this.read(I32_SIZE).readUInt32LE(0);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Pushes the lowest n bits of data as booleans.
|
|
311
|
+
* @param {number} data data
|
|
312
|
+
* @param {number} n n
|
|
313
|
+
*/
|
|
314
|
+
readBits(data, n) {
|
|
315
|
+
let mask = 1;
|
|
316
|
+
while (n !== 0) {
|
|
317
|
+
this.result.push((data & mask) !== 0);
|
|
318
|
+
mask <<= 1;
|
|
319
|
+
n--;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Deserialize handlers indexed by header byte; built once so each
|
|
326
|
+
* `_deserialize` call doesn't rebuild 256 closures (hot on cache restore).
|
|
327
|
+
* @type {((s: ReadState) => void)[]}
|
|
328
|
+
*/
|
|
329
|
+
const dispatchTable = Array.from({ length: 256 }, (_, header) => {
|
|
330
|
+
switch (header) {
|
|
331
|
+
case LAZY_HEADER:
|
|
332
|
+
return (s) => {
|
|
333
|
+
const count = s.readU32();
|
|
334
|
+
/** @type {number[]} */
|
|
335
|
+
const lengths = [];
|
|
336
|
+
for (let i = 0; i < count; i++) lengths.push(s.readU32());
|
|
337
|
+
/** @type {(Buffer | LazyFunction<SerializedType, DeserializedType>)[]} */
|
|
338
|
+
const content = [];
|
|
339
|
+
for (let l of lengths) {
|
|
340
|
+
if (l === 0) {
|
|
341
|
+
if (typeof s.currentBuffer !== "function") {
|
|
342
|
+
throw new Error("Unexpected non-lazy element in stream");
|
|
343
|
+
}
|
|
344
|
+
content.push(s.currentBuffer);
|
|
345
|
+
s.nextDataItem();
|
|
346
|
+
} else {
|
|
347
|
+
do {
|
|
348
|
+
const buf = s.readUpTo(l);
|
|
349
|
+
l -= buf.length;
|
|
350
|
+
content.push(s.retainedBuffer(buf));
|
|
351
|
+
} while (l > 0);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
s.result.push(s.middleware._createLazyDeserialized(content, s.context));
|
|
355
|
+
};
|
|
356
|
+
case BUFFER_HEADER:
|
|
357
|
+
return (s) => {
|
|
358
|
+
const len = s.readU32();
|
|
359
|
+
s.result.push(s.retainedBuffer(s.read(len)));
|
|
360
|
+
};
|
|
361
|
+
case TRUE_HEADER:
|
|
362
|
+
return (s) => s.result.push(true);
|
|
363
|
+
case FALSE_HEADER:
|
|
364
|
+
return (s) => s.result.push(false);
|
|
365
|
+
case NULL3_HEADER:
|
|
366
|
+
return (s) => s.result.push(null, null, null);
|
|
367
|
+
case NULL2_HEADER:
|
|
368
|
+
return (s) => s.result.push(null, null);
|
|
369
|
+
case NULL_HEADER:
|
|
370
|
+
return (s) => s.result.push(null);
|
|
371
|
+
case NULL_AND_TRUE_HEADER:
|
|
372
|
+
return (s) => s.result.push(null, true);
|
|
373
|
+
case NULL_AND_FALSE_HEADER:
|
|
374
|
+
return (s) => s.result.push(null, false);
|
|
375
|
+
case NULL_AND_I8_HEADER:
|
|
376
|
+
return (s) => {
|
|
377
|
+
if (s.currentIsBuffer) {
|
|
378
|
+
s.result.push(
|
|
379
|
+
null,
|
|
380
|
+
/** @type {Buffer} */ (s.currentBuffer).readInt8(s.currentPosition)
|
|
381
|
+
);
|
|
382
|
+
s.currentPosition += I8_SIZE;
|
|
383
|
+
s.checkOverflow();
|
|
384
|
+
} else {
|
|
385
|
+
s.result.push(null, s.read(I8_SIZE).readInt8(0));
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
case NULL_AND_I16_HEADER:
|
|
389
|
+
return (s) => {
|
|
390
|
+
s.result.push(null);
|
|
391
|
+
if (s.isInCurrentBuffer(I16_SIZE)) {
|
|
392
|
+
s.result.push(
|
|
393
|
+
/** @type {Buffer} */ (s.currentBuffer).readInt16LE(
|
|
394
|
+
s.currentPosition
|
|
395
|
+
)
|
|
396
|
+
);
|
|
397
|
+
s.currentPosition += I16_SIZE;
|
|
398
|
+
s.checkOverflow();
|
|
399
|
+
} else {
|
|
400
|
+
s.result.push(s.read(I16_SIZE).readInt16LE(0));
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
case NULL_AND_I32_HEADER:
|
|
404
|
+
return (s) => {
|
|
405
|
+
s.result.push(null);
|
|
406
|
+
if (s.isInCurrentBuffer(I32_SIZE)) {
|
|
407
|
+
s.result.push(
|
|
408
|
+
/** @type {Buffer} */ (s.currentBuffer).readInt32LE(
|
|
409
|
+
s.currentPosition
|
|
410
|
+
)
|
|
411
|
+
);
|
|
412
|
+
s.currentPosition += I32_SIZE;
|
|
413
|
+
s.checkOverflow();
|
|
414
|
+
} else {
|
|
415
|
+
s.result.push(s.read(I32_SIZE).readInt32LE(0));
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
case NULLS8_HEADER:
|
|
419
|
+
return (s) => {
|
|
420
|
+
const len = s.readU8() + 4;
|
|
421
|
+
for (let i = 0; i < len; i++) {
|
|
422
|
+
s.result.push(null);
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
case NULLS32_HEADER:
|
|
426
|
+
return (s) => {
|
|
427
|
+
const len = s.readU32() + 260;
|
|
428
|
+
for (let i = 0; i < len; i++) {
|
|
429
|
+
s.result.push(null);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
case BOOLEANS_HEADER:
|
|
433
|
+
return (s) => {
|
|
434
|
+
const innerHeader = s.readU8();
|
|
435
|
+
if ((innerHeader & 0xf0) === 0) {
|
|
436
|
+
s.readBits(innerHeader, 3);
|
|
437
|
+
} else if ((innerHeader & 0xe0) === 0) {
|
|
438
|
+
s.readBits(innerHeader, 4);
|
|
439
|
+
} else if ((innerHeader & 0xc0) === 0) {
|
|
440
|
+
s.readBits(innerHeader, 5);
|
|
441
|
+
} else if ((innerHeader & 0x80) === 0) {
|
|
442
|
+
s.readBits(innerHeader, 6);
|
|
443
|
+
} else if (innerHeader !== 0xff) {
|
|
444
|
+
let count = (innerHeader & 0x7f) + 7;
|
|
445
|
+
while (count > 8) {
|
|
446
|
+
s.readBits(s.readU8(), 8);
|
|
447
|
+
count -= 8;
|
|
448
|
+
}
|
|
449
|
+
s.readBits(s.readU8(), count);
|
|
450
|
+
} else {
|
|
451
|
+
let count = s.readU32();
|
|
452
|
+
while (count > 8) {
|
|
453
|
+
s.readBits(s.readU8(), 8);
|
|
454
|
+
count -= 8;
|
|
455
|
+
}
|
|
456
|
+
s.readBits(s.readU8(), count);
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
case STRING_HEADER:
|
|
460
|
+
return (s) => {
|
|
461
|
+
const len = s.readU32();
|
|
462
|
+
if (s.isInCurrentBuffer(len) && s.currentPosition + len < 0x7fffffff) {
|
|
463
|
+
s.result.push(
|
|
464
|
+
/** @type {Buffer} */
|
|
465
|
+
(s.currentBuffer).toString(
|
|
466
|
+
undefined,
|
|
467
|
+
s.currentPosition,
|
|
468
|
+
s.currentPosition + len
|
|
469
|
+
)
|
|
470
|
+
);
|
|
471
|
+
s.currentPosition += len;
|
|
472
|
+
s.checkOverflow();
|
|
473
|
+
} else {
|
|
474
|
+
s.result.push(s.read(len).toString());
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
case SHORT_STRING_HEADER:
|
|
478
|
+
return (s) => s.result.push("");
|
|
479
|
+
case SHORT_STRING_HEADER | 1:
|
|
480
|
+
return (s) => {
|
|
481
|
+
if (s.currentIsBuffer && s.currentPosition < 0x7ffffffe) {
|
|
482
|
+
s.result.push(
|
|
483
|
+
/** @type {Buffer} */
|
|
484
|
+
(s.currentBuffer).toString(
|
|
485
|
+
"latin1",
|
|
486
|
+
s.currentPosition,
|
|
487
|
+
s.currentPosition + 1
|
|
488
|
+
)
|
|
489
|
+
);
|
|
490
|
+
s.currentPosition++;
|
|
491
|
+
s.checkOverflow();
|
|
492
|
+
} else {
|
|
493
|
+
s.result.push(s.read(1).toString("latin1"));
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
case I8_HEADER:
|
|
497
|
+
return (s) => {
|
|
498
|
+
if (s.currentIsBuffer) {
|
|
499
|
+
s.result.push(
|
|
500
|
+
/** @type {Buffer} */ (s.currentBuffer).readInt8(s.currentPosition)
|
|
501
|
+
);
|
|
502
|
+
s.currentPosition++;
|
|
503
|
+
s.checkOverflow();
|
|
504
|
+
} else {
|
|
505
|
+
s.result.push(s.read(1).readInt8(0));
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
case BIGINT_I8_HEADER: {
|
|
509
|
+
const len = 1;
|
|
510
|
+
return (s) => {
|
|
511
|
+
const need = I8_SIZE * len;
|
|
512
|
+
|
|
513
|
+
if (s.isInCurrentBuffer(need)) {
|
|
514
|
+
for (let i = 0; i < len; i++) {
|
|
515
|
+
const value =
|
|
516
|
+
/** @type {Buffer} */
|
|
517
|
+
(s.currentBuffer).readInt8(s.currentPosition);
|
|
518
|
+
s.result.push(BigInt(value));
|
|
519
|
+
s.currentPosition += I8_SIZE;
|
|
520
|
+
}
|
|
521
|
+
s.checkOverflow();
|
|
522
|
+
} else {
|
|
523
|
+
const buf = s.read(need);
|
|
524
|
+
for (let i = 0; i < len; i++) {
|
|
525
|
+
const value = buf.readInt8(i * I8_SIZE);
|
|
526
|
+
s.result.push(BigInt(value));
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
case BIGINT_I32_HEADER: {
|
|
532
|
+
const len = 1;
|
|
533
|
+
return (s) => {
|
|
534
|
+
const need = I32_SIZE * len;
|
|
535
|
+
if (s.isInCurrentBuffer(need)) {
|
|
536
|
+
for (let i = 0; i < len; i++) {
|
|
537
|
+
const value = /** @type {Buffer} */ (s.currentBuffer).readInt32LE(
|
|
538
|
+
s.currentPosition
|
|
539
|
+
);
|
|
540
|
+
s.result.push(BigInt(value));
|
|
541
|
+
s.currentPosition += I32_SIZE;
|
|
542
|
+
}
|
|
543
|
+
s.checkOverflow();
|
|
544
|
+
} else {
|
|
545
|
+
const buf = s.read(need);
|
|
546
|
+
for (let i = 0; i < len; i++) {
|
|
547
|
+
const value = buf.readInt32LE(i * I32_SIZE);
|
|
548
|
+
s.result.push(BigInt(value));
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
case BIGINT_HEADER: {
|
|
554
|
+
return (s) => {
|
|
555
|
+
const len = s.readU32();
|
|
556
|
+
if (s.isInCurrentBuffer(len) && s.currentPosition + len < 0x7fffffff) {
|
|
557
|
+
const value =
|
|
558
|
+
/** @type {Buffer} */
|
|
559
|
+
(s.currentBuffer).toString(
|
|
560
|
+
undefined,
|
|
561
|
+
s.currentPosition,
|
|
562
|
+
s.currentPosition + len
|
|
563
|
+
);
|
|
564
|
+
|
|
565
|
+
s.result.push(BigInt(value));
|
|
566
|
+
s.currentPosition += len;
|
|
567
|
+
s.checkOverflow();
|
|
568
|
+
} else {
|
|
569
|
+
const value = s.read(len).toString();
|
|
570
|
+
s.result.push(BigInt(value));
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
default:
|
|
575
|
+
if (header <= 10) {
|
|
576
|
+
return (s) => s.result.push(header);
|
|
577
|
+
} else if ((header & SHORT_STRING_HEADER) === SHORT_STRING_HEADER) {
|
|
578
|
+
const len = header & SHORT_STRING_LENGTH_MASK;
|
|
579
|
+
return (s) => {
|
|
580
|
+
if (
|
|
581
|
+
s.isInCurrentBuffer(len) &&
|
|
582
|
+
s.currentPosition + len < 0x7fffffff
|
|
583
|
+
) {
|
|
584
|
+
s.result.push(
|
|
585
|
+
/** @type {Buffer} */
|
|
586
|
+
(s.currentBuffer).toString(
|
|
587
|
+
"latin1",
|
|
588
|
+
s.currentPosition,
|
|
589
|
+
s.currentPosition + len
|
|
590
|
+
)
|
|
591
|
+
);
|
|
592
|
+
s.currentPosition += len;
|
|
593
|
+
s.checkOverflow();
|
|
594
|
+
} else {
|
|
595
|
+
s.result.push(s.read(len).toString("latin1"));
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
} else if ((header & NUMBERS_HEADER_MASK) === F64_HEADER) {
|
|
599
|
+
const len = (header & NUMBERS_COUNT_MASK) + 1;
|
|
600
|
+
return (s) => {
|
|
601
|
+
const need = F64_SIZE * len;
|
|
602
|
+
if (s.isInCurrentBuffer(need)) {
|
|
603
|
+
for (let i = 0; i < len; i++) {
|
|
604
|
+
s.result.push(
|
|
605
|
+
/** @type {Buffer} */ (s.currentBuffer).readDoubleLE(
|
|
606
|
+
s.currentPosition
|
|
607
|
+
)
|
|
608
|
+
);
|
|
609
|
+
s.currentPosition += F64_SIZE;
|
|
610
|
+
}
|
|
611
|
+
s.checkOverflow();
|
|
612
|
+
} else {
|
|
613
|
+
const buf = s.read(need);
|
|
614
|
+
for (let i = 0; i < len; i++) {
|
|
615
|
+
s.result.push(buf.readDoubleLE(i * F64_SIZE));
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
} else if ((header & NUMBERS_HEADER_MASK) === I32_HEADER) {
|
|
620
|
+
const len = (header & NUMBERS_COUNT_MASK) + 1;
|
|
621
|
+
return (s) => {
|
|
622
|
+
const need = I32_SIZE * len;
|
|
623
|
+
if (s.isInCurrentBuffer(need)) {
|
|
624
|
+
for (let i = 0; i < len; i++) {
|
|
625
|
+
s.result.push(
|
|
626
|
+
/** @type {Buffer} */ (s.currentBuffer).readInt32LE(
|
|
627
|
+
s.currentPosition
|
|
628
|
+
)
|
|
629
|
+
);
|
|
630
|
+
s.currentPosition += I32_SIZE;
|
|
631
|
+
}
|
|
632
|
+
s.checkOverflow();
|
|
633
|
+
} else {
|
|
634
|
+
const buf = s.read(need);
|
|
635
|
+
for (let i = 0; i < len; i++) {
|
|
636
|
+
s.result.push(buf.readInt32LE(i * I32_SIZE));
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
} else if ((header & NUMBERS_HEADER_MASK) === I8_HEADER) {
|
|
641
|
+
const len = (header & NUMBERS_COUNT_MASK) + 1;
|
|
642
|
+
return (s) => {
|
|
643
|
+
const need = I8_SIZE * len;
|
|
644
|
+
if (s.isInCurrentBuffer(need)) {
|
|
645
|
+
for (let i = 0; i < len; i++) {
|
|
646
|
+
s.result.push(
|
|
647
|
+
/** @type {Buffer} */ (s.currentBuffer).readInt8(
|
|
648
|
+
s.currentPosition
|
|
649
|
+
)
|
|
650
|
+
);
|
|
651
|
+
s.currentPosition += I8_SIZE;
|
|
652
|
+
}
|
|
653
|
+
s.checkOverflow();
|
|
654
|
+
} else {
|
|
655
|
+
const buf = s.read(need);
|
|
656
|
+
for (let i = 0; i < len; i++) {
|
|
657
|
+
s.result.push(buf.readInt8(i * I8_SIZE));
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
return (s) => {
|
|
663
|
+
throw new Error(`Unexpected header byte 0x${header.toString(16)}`);
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
|
|
151
668
|
/**
|
|
152
669
|
* Represents BinaryMiddleware.
|
|
153
670
|
* @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>}
|
|
@@ -582,10 +1099,19 @@ class BinaryMiddleware extends SerializerMiddleware {
|
|
|
582
1099
|
currentPosition += I8_SIZE;
|
|
583
1100
|
i++;
|
|
584
1101
|
} else if (type === 1) {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
1102
|
+
// `null` + i32 is 5 bytes; if the value also fits an
|
|
1103
|
+
// i16, fuse it as 3 bytes instead (helps back-references)
|
|
1104
|
+
if (next >= -32768 && next <= 32767) {
|
|
1105
|
+
allocate(HEADER_SIZE + I16_SIZE);
|
|
1106
|
+
writeU8(NULL_AND_I16_HEADER);
|
|
1107
|
+
currentBuffer.writeInt16LE(next, currentPosition);
|
|
1108
|
+
currentPosition += I16_SIZE;
|
|
1109
|
+
} else {
|
|
1110
|
+
allocate(HEADER_SIZE + I32_SIZE);
|
|
1111
|
+
writeU8(NULL_AND_I32_HEADER);
|
|
1112
|
+
currentBuffer.writeInt32LE(next, currentPosition);
|
|
1113
|
+
currentPosition += I32_SIZE;
|
|
1114
|
+
}
|
|
589
1115
|
i++;
|
|
590
1116
|
} else {
|
|
591
1117
|
allocate(HEADER_SIZE);
|
|
@@ -674,8 +1200,7 @@ class BinaryMiddleware extends SerializerMiddleware {
|
|
|
674
1200
|
}
|
|
675
1201
|
|
|
676
1202
|
/**
|
|
677
|
-
* Create lazy deserialized.
|
|
678
|
-
* @private
|
|
1203
|
+
* Create lazy deserialized. Internal, but called from the dispatch table.
|
|
679
1204
|
* @param {SerializedType} content content
|
|
680
1205
|
* @param {Context} context context object
|
|
681
1206
|
* @returns {LazyFunction<DeserializedType, SerializedType>} lazy function
|
|
@@ -709,471 +1234,16 @@ class BinaryMiddleware extends SerializerMiddleware {
|
|
|
709
1234
|
* @returns {DeserializedType} deserialized data
|
|
710
1235
|
*/
|
|
711
1236
|
_deserialize(data, context) {
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
const retainedBuffer = context.retainedBuffer || ((x) => x);
|
|
719
|
-
|
|
720
|
-
const checkOverflow = () => {
|
|
721
|
-
if (currentPosition >= /** @type {Buffer} */ (currentBuffer).length) {
|
|
722
|
-
currentPosition = 0;
|
|
723
|
-
currentDataItem++;
|
|
724
|
-
currentBuffer =
|
|
725
|
-
currentDataItem < data.length ? data[currentDataItem] : null;
|
|
726
|
-
currentIsBuffer = Buffer.isBuffer(currentBuffer);
|
|
727
|
-
}
|
|
728
|
-
};
|
|
729
|
-
/**
|
|
730
|
-
* Checks whether this binary middleware is in current buffer.
|
|
731
|
-
* @param {number} n n
|
|
732
|
-
* @returns {boolean} true when in current buffer, otherwise false
|
|
733
|
-
*/
|
|
734
|
-
const isInCurrentBuffer = (n) =>
|
|
735
|
-
currentIsBuffer &&
|
|
736
|
-
n + currentPosition <= /** @type {Buffer} */ (currentBuffer).length;
|
|
737
|
-
const ensureBuffer = () => {
|
|
738
|
-
if (!currentIsBuffer) {
|
|
739
|
-
throw new Error(
|
|
740
|
-
currentBuffer === null
|
|
741
|
-
? "Unexpected end of stream"
|
|
742
|
-
: "Unexpected lazy element in stream"
|
|
743
|
-
);
|
|
744
|
-
}
|
|
745
|
-
};
|
|
746
|
-
/**
|
|
747
|
-
* Returns buffer with bytes.
|
|
748
|
-
* @param {number} n amount of bytes to read
|
|
749
|
-
* @returns {Buffer} buffer with bytes
|
|
750
|
-
*/
|
|
751
|
-
const read = (n) => {
|
|
752
|
-
ensureBuffer();
|
|
753
|
-
const rem =
|
|
754
|
-
/** @type {Buffer} */ (currentBuffer).length - currentPosition;
|
|
755
|
-
if (rem < n) {
|
|
756
|
-
const buffers = [read(rem)];
|
|
757
|
-
n -= rem;
|
|
758
|
-
ensureBuffer();
|
|
759
|
-
while (/** @type {Buffer} */ (currentBuffer).length < n) {
|
|
760
|
-
const b = /** @type {Buffer} */ (currentBuffer);
|
|
761
|
-
buffers.push(b);
|
|
762
|
-
n -= b.length;
|
|
763
|
-
currentDataItem++;
|
|
764
|
-
currentBuffer =
|
|
765
|
-
currentDataItem < data.length ? data[currentDataItem] : null;
|
|
766
|
-
currentIsBuffer = Buffer.isBuffer(currentBuffer);
|
|
767
|
-
ensureBuffer();
|
|
768
|
-
}
|
|
769
|
-
buffers.push(read(n));
|
|
770
|
-
return Buffer.concat(buffers);
|
|
771
|
-
}
|
|
772
|
-
const b = /** @type {Buffer} */ (currentBuffer);
|
|
773
|
-
const res = Buffer.from(b.buffer, b.byteOffset + currentPosition, n);
|
|
774
|
-
currentPosition += n;
|
|
775
|
-
checkOverflow();
|
|
776
|
-
return res;
|
|
777
|
-
};
|
|
778
|
-
/**
|
|
779
|
-
* Reads up to n bytes
|
|
780
|
-
* @param {number} n amount of bytes to read
|
|
781
|
-
* @returns {Buffer} buffer with bytes
|
|
782
|
-
*/
|
|
783
|
-
const readUpTo = (n) => {
|
|
784
|
-
ensureBuffer();
|
|
785
|
-
const rem =
|
|
786
|
-
/** @type {Buffer} */
|
|
787
|
-
(currentBuffer).length - currentPosition;
|
|
788
|
-
if (rem < n) {
|
|
789
|
-
n = rem;
|
|
790
|
-
}
|
|
791
|
-
const b = /** @type {Buffer} */ (currentBuffer);
|
|
792
|
-
const res = Buffer.from(b.buffer, b.byteOffset + currentPosition, n);
|
|
793
|
-
currentPosition += n;
|
|
794
|
-
checkOverflow();
|
|
795
|
-
return res;
|
|
796
|
-
};
|
|
797
|
-
/**
|
|
798
|
-
* Returns u8.
|
|
799
|
-
* @returns {number} U8
|
|
800
|
-
*/
|
|
801
|
-
const readU8 = () => {
|
|
802
|
-
ensureBuffer();
|
|
803
|
-
/**
|
|
804
|
-
* There is no need to check remaining buffer size here
|
|
805
|
-
* since {@link checkOverflow} guarantees at least one byte remaining
|
|
806
|
-
*/
|
|
807
|
-
const byte =
|
|
808
|
-
/** @type {Buffer} */
|
|
809
|
-
(currentBuffer).readUInt8(currentPosition);
|
|
810
|
-
currentPosition += I8_SIZE;
|
|
811
|
-
checkOverflow();
|
|
812
|
-
return byte;
|
|
813
|
-
};
|
|
814
|
-
/**
|
|
815
|
-
* Returns u32.
|
|
816
|
-
* @returns {number} U32
|
|
817
|
-
*/
|
|
818
|
-
const readU32 = () => read(I32_SIZE).readUInt32LE(0);
|
|
819
|
-
/**
|
|
820
|
-
* Processes the provided data.
|
|
821
|
-
* @param {number} data data
|
|
822
|
-
* @param {number} n n
|
|
823
|
-
*/
|
|
824
|
-
const readBits = (data, n) => {
|
|
825
|
-
let mask = 1;
|
|
826
|
-
while (n !== 0) {
|
|
827
|
-
result.push((data & mask) !== 0);
|
|
828
|
-
mask <<= 1;
|
|
829
|
-
n--;
|
|
830
|
-
}
|
|
831
|
-
};
|
|
832
|
-
const dispatchTable = Array.from({ length: 256 }).map((_, header) => {
|
|
833
|
-
switch (header) {
|
|
834
|
-
case LAZY_HEADER:
|
|
835
|
-
return () => {
|
|
836
|
-
const count = readU32();
|
|
837
|
-
const lengths = Array.from({ length: count }).map(() => readU32());
|
|
838
|
-
/** @type {(Buffer | LazyFunction<SerializedType, DeserializedType>)[]} */
|
|
839
|
-
const content = [];
|
|
840
|
-
for (let l of lengths) {
|
|
841
|
-
if (l === 0) {
|
|
842
|
-
if (typeof currentBuffer !== "function") {
|
|
843
|
-
throw new Error("Unexpected non-lazy element in stream");
|
|
844
|
-
}
|
|
845
|
-
content.push(currentBuffer);
|
|
846
|
-
currentDataItem++;
|
|
847
|
-
currentBuffer =
|
|
848
|
-
currentDataItem < data.length ? data[currentDataItem] : null;
|
|
849
|
-
currentIsBuffer = Buffer.isBuffer(currentBuffer);
|
|
850
|
-
} else {
|
|
851
|
-
do {
|
|
852
|
-
const buf = readUpTo(l);
|
|
853
|
-
l -= buf.length;
|
|
854
|
-
content.push(retainedBuffer(buf));
|
|
855
|
-
} while (l > 0);
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
result.push(this._createLazyDeserialized(content, context));
|
|
859
|
-
};
|
|
860
|
-
case BUFFER_HEADER:
|
|
861
|
-
return () => {
|
|
862
|
-
const len = readU32();
|
|
863
|
-
result.push(retainedBuffer(read(len)));
|
|
864
|
-
};
|
|
865
|
-
case TRUE_HEADER:
|
|
866
|
-
return () => result.push(true);
|
|
867
|
-
case FALSE_HEADER:
|
|
868
|
-
return () => result.push(false);
|
|
869
|
-
case NULL3_HEADER:
|
|
870
|
-
return () => result.push(null, null, null);
|
|
871
|
-
case NULL2_HEADER:
|
|
872
|
-
return () => result.push(null, null);
|
|
873
|
-
case NULL_HEADER:
|
|
874
|
-
return () => result.push(null);
|
|
875
|
-
case NULL_AND_TRUE_HEADER:
|
|
876
|
-
return () => result.push(null, true);
|
|
877
|
-
case NULL_AND_FALSE_HEADER:
|
|
878
|
-
return () => result.push(null, false);
|
|
879
|
-
case NULL_AND_I8_HEADER:
|
|
880
|
-
return () => {
|
|
881
|
-
if (currentIsBuffer) {
|
|
882
|
-
result.push(
|
|
883
|
-
null,
|
|
884
|
-
/** @type {Buffer} */ (currentBuffer).readInt8(currentPosition)
|
|
885
|
-
);
|
|
886
|
-
currentPosition += I8_SIZE;
|
|
887
|
-
checkOverflow();
|
|
888
|
-
} else {
|
|
889
|
-
result.push(null, read(I8_SIZE).readInt8(0));
|
|
890
|
-
}
|
|
891
|
-
};
|
|
892
|
-
case NULL_AND_I32_HEADER:
|
|
893
|
-
return () => {
|
|
894
|
-
result.push(null);
|
|
895
|
-
if (isInCurrentBuffer(I32_SIZE)) {
|
|
896
|
-
result.push(
|
|
897
|
-
/** @type {Buffer} */ (currentBuffer).readInt32LE(
|
|
898
|
-
currentPosition
|
|
899
|
-
)
|
|
900
|
-
);
|
|
901
|
-
currentPosition += I32_SIZE;
|
|
902
|
-
checkOverflow();
|
|
903
|
-
} else {
|
|
904
|
-
result.push(read(I32_SIZE).readInt32LE(0));
|
|
905
|
-
}
|
|
906
|
-
};
|
|
907
|
-
case NULLS8_HEADER:
|
|
908
|
-
return () => {
|
|
909
|
-
const len = readU8() + 4;
|
|
910
|
-
for (let i = 0; i < len; i++) {
|
|
911
|
-
result.push(null);
|
|
912
|
-
}
|
|
913
|
-
};
|
|
914
|
-
case NULLS32_HEADER:
|
|
915
|
-
return () => {
|
|
916
|
-
const len = readU32() + 260;
|
|
917
|
-
for (let i = 0; i < len; i++) {
|
|
918
|
-
result.push(null);
|
|
919
|
-
}
|
|
920
|
-
};
|
|
921
|
-
case BOOLEANS_HEADER:
|
|
922
|
-
return () => {
|
|
923
|
-
const innerHeader = readU8();
|
|
924
|
-
if ((innerHeader & 0xf0) === 0) {
|
|
925
|
-
readBits(innerHeader, 3);
|
|
926
|
-
} else if ((innerHeader & 0xe0) === 0) {
|
|
927
|
-
readBits(innerHeader, 4);
|
|
928
|
-
} else if ((innerHeader & 0xc0) === 0) {
|
|
929
|
-
readBits(innerHeader, 5);
|
|
930
|
-
} else if ((innerHeader & 0x80) === 0) {
|
|
931
|
-
readBits(innerHeader, 6);
|
|
932
|
-
} else if (innerHeader !== 0xff) {
|
|
933
|
-
let count = (innerHeader & 0x7f) + 7;
|
|
934
|
-
while (count > 8) {
|
|
935
|
-
readBits(readU8(), 8);
|
|
936
|
-
count -= 8;
|
|
937
|
-
}
|
|
938
|
-
readBits(readU8(), count);
|
|
939
|
-
} else {
|
|
940
|
-
let count = readU32();
|
|
941
|
-
while (count > 8) {
|
|
942
|
-
readBits(readU8(), 8);
|
|
943
|
-
count -= 8;
|
|
944
|
-
}
|
|
945
|
-
readBits(readU8(), count);
|
|
946
|
-
}
|
|
947
|
-
};
|
|
948
|
-
case STRING_HEADER:
|
|
949
|
-
return () => {
|
|
950
|
-
const len = readU32();
|
|
951
|
-
if (isInCurrentBuffer(len) && currentPosition + len < 0x7fffffff) {
|
|
952
|
-
result.push(
|
|
953
|
-
/** @type {Buffer} */
|
|
954
|
-
(currentBuffer).toString(
|
|
955
|
-
undefined,
|
|
956
|
-
currentPosition,
|
|
957
|
-
currentPosition + len
|
|
958
|
-
)
|
|
959
|
-
);
|
|
960
|
-
currentPosition += len;
|
|
961
|
-
checkOverflow();
|
|
962
|
-
} else {
|
|
963
|
-
result.push(read(len).toString());
|
|
964
|
-
}
|
|
965
|
-
};
|
|
966
|
-
case SHORT_STRING_HEADER:
|
|
967
|
-
return () => result.push("");
|
|
968
|
-
case SHORT_STRING_HEADER | 1:
|
|
969
|
-
return () => {
|
|
970
|
-
if (currentIsBuffer && currentPosition < 0x7ffffffe) {
|
|
971
|
-
result.push(
|
|
972
|
-
/** @type {Buffer} */
|
|
973
|
-
(currentBuffer).toString(
|
|
974
|
-
"latin1",
|
|
975
|
-
currentPosition,
|
|
976
|
-
currentPosition + 1
|
|
977
|
-
)
|
|
978
|
-
);
|
|
979
|
-
currentPosition++;
|
|
980
|
-
checkOverflow();
|
|
981
|
-
} else {
|
|
982
|
-
result.push(read(1).toString("latin1"));
|
|
983
|
-
}
|
|
984
|
-
};
|
|
985
|
-
case I8_HEADER:
|
|
986
|
-
return () => {
|
|
987
|
-
if (currentIsBuffer) {
|
|
988
|
-
result.push(
|
|
989
|
-
/** @type {Buffer} */ (currentBuffer).readInt8(currentPosition)
|
|
990
|
-
);
|
|
991
|
-
currentPosition++;
|
|
992
|
-
checkOverflow();
|
|
993
|
-
} else {
|
|
994
|
-
result.push(read(1).readInt8(0));
|
|
995
|
-
}
|
|
996
|
-
};
|
|
997
|
-
case BIGINT_I8_HEADER: {
|
|
998
|
-
const len = 1;
|
|
999
|
-
return () => {
|
|
1000
|
-
const need = I8_SIZE * len;
|
|
1001
|
-
|
|
1002
|
-
if (isInCurrentBuffer(need)) {
|
|
1003
|
-
for (let i = 0; i < len; i++) {
|
|
1004
|
-
const value =
|
|
1005
|
-
/** @type {Buffer} */
|
|
1006
|
-
(currentBuffer).readInt8(currentPosition);
|
|
1007
|
-
result.push(BigInt(value));
|
|
1008
|
-
currentPosition += I8_SIZE;
|
|
1009
|
-
}
|
|
1010
|
-
checkOverflow();
|
|
1011
|
-
} else {
|
|
1012
|
-
const buf = read(need);
|
|
1013
|
-
for (let i = 0; i < len; i++) {
|
|
1014
|
-
const value = buf.readInt8(i * I8_SIZE);
|
|
1015
|
-
result.push(BigInt(value));
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
};
|
|
1019
|
-
}
|
|
1020
|
-
case BIGINT_I32_HEADER: {
|
|
1021
|
-
const len = 1;
|
|
1022
|
-
return () => {
|
|
1023
|
-
const need = I32_SIZE * len;
|
|
1024
|
-
if (isInCurrentBuffer(need)) {
|
|
1025
|
-
for (let i = 0; i < len; i++) {
|
|
1026
|
-
const value = /** @type {Buffer} */ (currentBuffer).readInt32LE(
|
|
1027
|
-
currentPosition
|
|
1028
|
-
);
|
|
1029
|
-
result.push(BigInt(value));
|
|
1030
|
-
currentPosition += I32_SIZE;
|
|
1031
|
-
}
|
|
1032
|
-
checkOverflow();
|
|
1033
|
-
} else {
|
|
1034
|
-
const buf = read(need);
|
|
1035
|
-
for (let i = 0; i < len; i++) {
|
|
1036
|
-
const value = buf.readInt32LE(i * I32_SIZE);
|
|
1037
|
-
result.push(BigInt(value));
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
};
|
|
1041
|
-
}
|
|
1042
|
-
case BIGINT_HEADER: {
|
|
1043
|
-
return () => {
|
|
1044
|
-
const len = readU32();
|
|
1045
|
-
if (isInCurrentBuffer(len) && currentPosition + len < 0x7fffffff) {
|
|
1046
|
-
const value =
|
|
1047
|
-
/** @type {Buffer} */
|
|
1048
|
-
(currentBuffer).toString(
|
|
1049
|
-
undefined,
|
|
1050
|
-
currentPosition,
|
|
1051
|
-
currentPosition + len
|
|
1052
|
-
);
|
|
1053
|
-
|
|
1054
|
-
result.push(BigInt(value));
|
|
1055
|
-
currentPosition += len;
|
|
1056
|
-
checkOverflow();
|
|
1057
|
-
} else {
|
|
1058
|
-
const value = read(len).toString();
|
|
1059
|
-
result.push(BigInt(value));
|
|
1060
|
-
}
|
|
1061
|
-
};
|
|
1062
|
-
}
|
|
1063
|
-
default:
|
|
1064
|
-
if (header <= 10) {
|
|
1065
|
-
return () => result.push(header);
|
|
1066
|
-
} else if ((header & SHORT_STRING_HEADER) === SHORT_STRING_HEADER) {
|
|
1067
|
-
const len = header & SHORT_STRING_LENGTH_MASK;
|
|
1068
|
-
return () => {
|
|
1069
|
-
if (
|
|
1070
|
-
isInCurrentBuffer(len) &&
|
|
1071
|
-
currentPosition + len < 0x7fffffff
|
|
1072
|
-
) {
|
|
1073
|
-
result.push(
|
|
1074
|
-
/** @type {Buffer} */
|
|
1075
|
-
(currentBuffer).toString(
|
|
1076
|
-
"latin1",
|
|
1077
|
-
currentPosition,
|
|
1078
|
-
currentPosition + len
|
|
1079
|
-
)
|
|
1080
|
-
);
|
|
1081
|
-
currentPosition += len;
|
|
1082
|
-
checkOverflow();
|
|
1083
|
-
} else {
|
|
1084
|
-
result.push(read(len).toString("latin1"));
|
|
1085
|
-
}
|
|
1086
|
-
};
|
|
1087
|
-
} else if ((header & NUMBERS_HEADER_MASK) === F64_HEADER) {
|
|
1088
|
-
const len = (header & NUMBERS_COUNT_MASK) + 1;
|
|
1089
|
-
return () => {
|
|
1090
|
-
const need = F64_SIZE * len;
|
|
1091
|
-
if (isInCurrentBuffer(need)) {
|
|
1092
|
-
for (let i = 0; i < len; i++) {
|
|
1093
|
-
result.push(
|
|
1094
|
-
/** @type {Buffer} */ (currentBuffer).readDoubleLE(
|
|
1095
|
-
currentPosition
|
|
1096
|
-
)
|
|
1097
|
-
);
|
|
1098
|
-
currentPosition += F64_SIZE;
|
|
1099
|
-
}
|
|
1100
|
-
checkOverflow();
|
|
1101
|
-
} else {
|
|
1102
|
-
const buf = read(need);
|
|
1103
|
-
for (let i = 0; i < len; i++) {
|
|
1104
|
-
result.push(buf.readDoubleLE(i * F64_SIZE));
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
};
|
|
1108
|
-
} else if ((header & NUMBERS_HEADER_MASK) === I32_HEADER) {
|
|
1109
|
-
const len = (header & NUMBERS_COUNT_MASK) + 1;
|
|
1110
|
-
return () => {
|
|
1111
|
-
const need = I32_SIZE * len;
|
|
1112
|
-
if (isInCurrentBuffer(need)) {
|
|
1113
|
-
for (let i = 0; i < len; i++) {
|
|
1114
|
-
result.push(
|
|
1115
|
-
/** @type {Buffer} */ (currentBuffer).readInt32LE(
|
|
1116
|
-
currentPosition
|
|
1117
|
-
)
|
|
1118
|
-
);
|
|
1119
|
-
currentPosition += I32_SIZE;
|
|
1120
|
-
}
|
|
1121
|
-
checkOverflow();
|
|
1122
|
-
} else {
|
|
1123
|
-
const buf = read(need);
|
|
1124
|
-
for (let i = 0; i < len; i++) {
|
|
1125
|
-
result.push(buf.readInt32LE(i * I32_SIZE));
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
};
|
|
1129
|
-
} else if ((header & NUMBERS_HEADER_MASK) === I8_HEADER) {
|
|
1130
|
-
const len = (header & NUMBERS_COUNT_MASK) + 1;
|
|
1131
|
-
return () => {
|
|
1132
|
-
const need = I8_SIZE * len;
|
|
1133
|
-
if (isInCurrentBuffer(need)) {
|
|
1134
|
-
for (let i = 0; i < len; i++) {
|
|
1135
|
-
result.push(
|
|
1136
|
-
/** @type {Buffer} */ (currentBuffer).readInt8(
|
|
1137
|
-
currentPosition
|
|
1138
|
-
)
|
|
1139
|
-
);
|
|
1140
|
-
currentPosition += I8_SIZE;
|
|
1141
|
-
}
|
|
1142
|
-
checkOverflow();
|
|
1143
|
-
} else {
|
|
1144
|
-
const buf = read(need);
|
|
1145
|
-
for (let i = 0; i < len; i++) {
|
|
1146
|
-
result.push(buf.readInt8(i * I8_SIZE));
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
};
|
|
1150
|
-
}
|
|
1151
|
-
return () => {
|
|
1152
|
-
throw new Error(`Unexpected header byte 0x${header.toString(16)}`);
|
|
1153
|
-
};
|
|
1154
|
-
}
|
|
1155
|
-
});
|
|
1156
|
-
|
|
1157
|
-
/** @type {DeserializedType} */
|
|
1158
|
-
let result = [];
|
|
1159
|
-
while (currentBuffer !== null) {
|
|
1160
|
-
if (typeof currentBuffer === "function") {
|
|
1161
|
-
result.push(this._deserializeLazy(currentBuffer, context));
|
|
1162
|
-
currentDataItem++;
|
|
1163
|
-
currentBuffer =
|
|
1164
|
-
currentDataItem < data.length ? data[currentDataItem] : null;
|
|
1165
|
-
currentIsBuffer = Buffer.isBuffer(currentBuffer);
|
|
1237
|
+
const state = new ReadState(data, context, this);
|
|
1238
|
+
while (state.currentBuffer !== null) {
|
|
1239
|
+
if (typeof state.currentBuffer === "function") {
|
|
1240
|
+
state.result.push(this._deserializeLazy(state.currentBuffer, context));
|
|
1241
|
+
state.nextDataItem();
|
|
1166
1242
|
} else {
|
|
1167
|
-
|
|
1168
|
-
dispatchTable[header]();
|
|
1243
|
+
dispatchTable[state.readU8()](state);
|
|
1169
1244
|
}
|
|
1170
1245
|
}
|
|
1171
|
-
|
|
1172
|
-
// avoid leaking memory in context
|
|
1173
|
-
// eslint-disable-next-line prefer-const
|
|
1174
|
-
let _result = result;
|
|
1175
|
-
result = /** @type {EXPECTED_ANY} */ (undefined);
|
|
1176
|
-
return _result;
|
|
1246
|
+
return state.result;
|
|
1177
1247
|
}
|
|
1178
1248
|
}
|
|
1179
1249
|
|