@carbonenginejs/runtime-resource 0.9.2 → 0.10.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/NOTICE +3 -2
- package/README.md +47 -6
- package/dist/CjsMotherLode.js +88 -88
- package/dist/CjsMotherLode.js.map +1 -1
- package/dist/CjsResMan.js +717 -228
- package/dist/CjsResMan.js.map +1 -1
- package/dist/CjsResManFetchProvider.js +94 -0
- package/dist/CjsResManFetchProvider.js.map +1 -0
- package/dist/{CjsResManQueue.js → CjsResManWorkQueue.js} +112 -25
- package/dist/CjsResManWorkQueue.js.map +1 -0
- package/dist/format/CjsBlueReader.js +89 -0
- package/dist/format/CjsBlueReader.js.map +1 -1
- package/dist/format/CjsFormat.js +42 -16
- package/dist/format/CjsFormat.js.map +1 -1
- package/dist/format/CjsReader.js +4 -0
- package/dist/format/CjsReader.js.map +1 -1
- package/dist/format/CjsResourceProbe.js +2 -0
- package/dist/format/CjsResourceProbe.js.map +1 -1
- package/dist/format/payloadContract.js +20 -0
- package/dist/format/payloadContract.js.map +1 -1
- package/dist/formats/black/CjsBlackFormat.js +15 -1
- package/dist/formats/black/CjsBlackFormat.js.map +1 -1
- package/dist/formats/black/core/CjsBlackBinaryReader.js +103 -0
- package/dist/formats/black/core/CjsBlackBinaryReader.js.map +1 -1
- package/dist/formats/black/core/CjsBlackPropertyReaders.js +61 -0
- package/dist/formats/black/core/CjsBlackPropertyReaders.js.map +1 -1
- package/dist/formats/black/core/CjsBlackReader.js +125 -0
- package/dist/formats/black/core/CjsBlackReader.js.map +1 -1
- package/dist/formats/black/core/CjsBlackSchemaRegistry.js +103 -0
- package/dist/formats/black/core/CjsBlackSchemaRegistry.js.map +1 -1
- package/dist/formats/black/core/helpers.js +17 -0
- package/dist/formats/black/core/helpers.js.map +1 -1
- package/dist/formats/bnk/CjsBnkFormat.js +8 -0
- package/dist/formats/bnk/CjsBnkFormat.js.map +1 -1
- package/dist/formats/bnk/core/helpers.js +20 -0
- package/dist/formats/bnk/core/helpers.js.map +1 -1
- package/dist/formats/bnk/core/musicNodes.js +40 -3
- package/dist/formats/bnk/core/musicNodes.js.map +1 -1
- package/dist/formats/bnk/core/soundbanksInfo.js +2 -0
- package/dist/formats/bnk/core/soundbanksInfo.js.map +1 -1
- package/dist/formats/cmf/core/binary.js +71 -0
- package/dist/formats/cmf/core/binary.js.map +1 -1
- package/dist/formats/cmf/core/buffers.js +4 -0
- package/dist/formats/cmf/core/buffers.js.map +1 -1
- package/dist/formats/cmf/core/helpers.js +31 -0
- package/dist/formats/cmf/core/helpers.js.map +1 -1
- package/dist/formats/cmf/core/schema.js +10 -0
- package/dist/formats/cmf/core/schema.js.map +1 -1
- package/dist/formats/cmf/core/shared.js +9 -0
- package/dist/formats/cmf/core/shared.js.map +1 -1
- package/dist/formats/cmf/core/writer.js +54 -0
- package/dist/formats/cmf/core/writer.js.map +1 -1
- package/dist/formats/dds/core/bc6h.js +10 -0
- package/dist/formats/dds/core/bc6h.js.map +1 -1
- package/dist/formats/dds/core/bc7.js +16 -0
- package/dist/formats/dds/core/bc7.js.map +1 -1
- package/dist/formats/dds/core/helpers.js +47 -0
- package/dist/formats/dds/core/helpers.js.map +1 -1
- package/dist/formats/fbx/core/helpers.js +21 -0
- package/dist/formats/fbx/core/helpers.js.map +1 -1
- package/dist/formats/flac/CjsFlacFormat.js +49 -0
- package/dist/formats/flac/CjsFlacFormat.js.map +1 -1
- package/dist/formats/flac/core/helpers.js +20 -0
- package/dist/formats/flac/core/helpers.js.map +1 -1
- package/dist/formats/gif/CjsGifFormat.js +49 -0
- package/dist/formats/gif/CjsGifFormat.js.map +1 -1
- package/dist/formats/gif/core/helpers.js +20 -0
- package/dist/formats/gif/core/helpers.js.map +1 -1
- package/dist/formats/gltf/core/helpers.js +15 -0
- package/dist/formats/gltf/core/helpers.js.map +1 -1
- package/dist/formats/gltf/core/parser.js +13 -0
- package/dist/formats/gltf/core/parser.js.map +1 -1
- package/dist/formats/gltf/core/targets.js +10 -0
- package/dist/formats/gltf/core/targets.js.map +1 -1
- package/dist/formats/gr2/core/bitknit2.js +2 -0
- package/dist/formats/gr2/core/bitknit2.js.map +1 -1
- package/dist/formats/gr2/core/helpers.js +20 -0
- package/dist/formats/gr2/core/helpers.js.map +1 -1
- package/dist/formats/gr2/core/targets.js +10 -0
- package/dist/formats/gr2/core/targets.js.map +1 -1
- package/dist/formats/jpeg/core/helpers.js +52 -0
- package/dist/formats/jpeg/core/helpers.js.map +1 -1
- package/dist/formats/jpeg/core/jpeg.js +93 -0
- package/dist/formats/jpeg/core/jpeg.js.map +1 -1
- package/dist/formats/mp3/core/helpers.js +37 -0
- package/dist/formats/mp3/core/helpers.js.map +1 -1
- package/dist/formats/mp4/core/helpers.js +35 -0
- package/dist/formats/mp4/core/helpers.js.map +1 -1
- package/dist/formats/ogg/CjsOggFormat.js +49 -0
- package/dist/formats/ogg/CjsOggFormat.js.map +1 -1
- package/dist/formats/ogg/core/helpers.js +23 -0
- package/dist/formats/ogg/core/helpers.js.map +1 -1
- package/dist/formats/ogg/core/vorbis.js +13 -0
- package/dist/formats/ogg/core/vorbis.js.map +1 -1
- package/dist/formats/png/core/helpers.js +52 -0
- package/dist/formats/png/core/helpers.js.map +1 -1
- package/dist/formats/red/CjsRedFormat.js +2 -0
- package/dist/formats/red/CjsRedFormat.js.map +1 -1
- package/dist/formats/red/core/CjsRedReader.js +52 -0
- package/dist/formats/red/core/CjsRedReader.js.map +1 -1
- package/dist/formats/red/core/helpers.js +19 -0
- package/dist/formats/red/core/helpers.js.map +1 -1
- package/dist/formats/stl/core/stl.js +8 -0
- package/dist/formats/stl/core/stl.js.map +1 -1
- package/dist/formats/stl/core/targets.js +10 -0
- package/dist/formats/stl/core/targets.js.map +1 -1
- package/dist/formats/tga/core/helpers.js +47 -0
- package/dist/formats/tga/core/helpers.js.map +1 -1
- package/dist/formats/wav/core/helpers.js +37 -0
- package/dist/formats/wav/core/helpers.js.map +1 -1
- package/dist/formats/webm/core/helpers.js +35 -0
- package/dist/formats/webm/core/helpers.js.map +1 -1
- package/dist/formats/webp/CjsWebpFormat.js +49 -0
- package/dist/formats/webp/CjsWebpFormat.js.map +1 -1
- package/dist/formats/webp/core/helpers.js +23 -0
- package/dist/formats/webp/core/helpers.js.map +1 -1
- package/dist/formats/wem/CjsWemFormat.js +8 -0
- package/dist/formats/wem/CjsWemFormat.js.map +1 -1
- package/dist/formats/wem/core/bitStream.js +2 -0
- package/dist/formats/wem/core/bitStream.js.map +1 -1
- package/dist/formats/wem/core/helpers.js +20 -0
- package/dist/formats/wem/core/helpers.js.map +1 -1
- package/dist/formats/yaml/CjsYamlFormat.js +45 -0
- package/dist/formats/yaml/CjsYamlFormat.js.map +1 -1
- package/dist/formats/yaml/core/CjsYamlReader.js +91 -0
- package/dist/formats/yaml/core/CjsYamlReader.js.map +1 -1
- package/dist/formats/yaml/core/helpers.js +10 -0
- package/dist/formats/yaml/core/helpers.js.map +1 -1
- package/dist/index.js +56 -43
- package/dist/index.js.map +1 -1
- package/dist/{CjsResource.js → resource/CjsResource.js} +232 -17
- package/dist/resource/CjsResource.js.map +1 -0
- package/dist/{resources → resource}/Tr2LightProfileRes.js +4 -3
- package/dist/resource/Tr2LightProfileRes.js.map +1 -0
- package/dist/{resources → resource/audio}/AudioGeometryResData.js +1 -1
- package/dist/resource/audio/AudioGeometryResData.js.map +1 -0
- package/dist/resource/audio/CjsAudioBufferRes.js +86 -0
- package/dist/resource/audio/CjsAudioBufferRes.js.map +1 -0
- package/dist/resource/audio/CjsAudioRes.js +213 -0
- package/dist/resource/audio/CjsAudioRes.js.map +1 -0
- package/dist/resource/audio/index.js +4 -0
- package/dist/resource/audio/index.js.map +1 -0
- package/dist/{resources → resource/geometry}/MeshDecalData.js +1 -1
- package/dist/resource/geometry/MeshDecalData.js.map +1 -0
- package/dist/{resources → resource/geometry}/MeshDecalLodData.js +1 -1
- package/dist/resource/geometry/MeshDecalLodData.js.map +1 -0
- package/dist/resource/geometry/TriGeometryRes.js +656 -0
- package/dist/resource/geometry/TriGeometryRes.js.map +1 -0
- package/dist/{resources → resource/geometry}/TriGeometryResAreaData.js +1 -1
- package/dist/resource/geometry/TriGeometryResAreaData.js.map +1 -0
- package/dist/{resources → resource/geometry}/TriGeometryResJointData.js +1 -1
- package/dist/resource/geometry/TriGeometryResJointData.js.map +1 -0
- package/dist/{resources → resource/geometry}/TriGeometryResLodData.js +1 -1
- package/dist/resource/geometry/TriGeometryResLodData.js.map +1 -0
- package/dist/{resources → resource/geometry}/TriGeometryResMeshData.js +1 -1
- package/dist/resource/geometry/TriGeometryResMeshData.js.map +1 -0
- package/dist/{resources → resource/geometry}/TriGeometryResSkeletonData.js +1 -1
- package/dist/resource/geometry/TriGeometryResSkeletonData.js.map +1 -0
- package/dist/{resources → resource/geometry}/TriJointBinding.js +1 -1
- package/dist/resource/geometry/TriJointBinding.js.map +1 -0
- package/dist/{resources → resource/geometry}/TriMorphTargetGeometryConstants.js +1 -1
- package/dist/resource/geometry/TriMorphTargetGeometryConstants.js.map +1 -0
- package/dist/{resources → resource/geometry}/TriRtGeometryConstants.js +1 -1
- package/dist/resource/geometry/TriRtGeometryConstants.js.map +1 -0
- package/dist/{resources → resource/geometry/granny}/GStateBindingCallbackData.js +1 -1
- package/dist/resource/geometry/granny/GStateBindingCallbackData.js.map +1 -0
- package/dist/{resources → resource/geometry/granny}/Tr2GrannyIntersectionResult.js +1 -1
- package/dist/resource/geometry/granny/Tr2GrannyIntersectionResult.js.map +1 -0
- package/dist/{resources → resource/geometry/granny}/Tr2GrannyStateRes.js +6 -5
- package/dist/resource/geometry/granny/Tr2GrannyStateRes.js.map +1 -0
- package/dist/{resources → resource/geometry/granny}/TriGrannyRes.js +6 -5
- package/dist/resource/geometry/granny/TriGrannyRes.js.map +1 -0
- package/dist/{resources → resource/geometry/granny}/enums.js +1 -9
- package/dist/resource/geometry/granny/enums.js.map +1 -0
- package/dist/resource/geometry/granny/index.js +6 -0
- package/dist/resource/geometry/granny/index.js.map +1 -0
- package/dist/resource/geometry/index.js +17 -0
- package/dist/resource/geometry/index.js.map +1 -0
- package/dist/resource/index.js +52 -0
- package/dist/resource/index.js.map +1 -0
- package/dist/resource/resourceBoundary.js +64 -0
- package/dist/resource/resourceBoundary.js.map +1 -0
- package/dist/resource/shader/Tr2EffectRes.js +420 -0
- package/dist/resource/shader/Tr2EffectRes.js.map +1 -0
- package/dist/{resources → resource/shader}/Tr2MaterialArea.js +9 -4
- package/dist/resource/shader/Tr2MaterialArea.js.map +1 -0
- package/dist/{resources → resource/shader}/Tr2MaterialMesh.js +8 -3
- package/dist/resource/shader/Tr2MaterialMesh.js.map +1 -0
- package/dist/{resources → resource/shader}/Tr2MaterialRes.js +10 -4
- package/dist/resource/shader/Tr2MaterialRes.js.map +1 -0
- package/dist/resource/shader/Tr2Shader.js +250 -0
- package/dist/resource/shader/Tr2Shader.js.map +1 -0
- package/dist/{resources → resource/shader}/Tr2ShaderPermutation.js +13 -7
- package/dist/resource/shader/Tr2ShaderPermutation.js.map +1 -0
- package/dist/resource/shader/index.js +17 -0
- package/dist/resource/shader/index.js.map +1 -0
- package/dist/resource/shader/portable.js +33 -0
- package/dist/resource/shader/portable.js.map +1 -0
- package/dist/resource/shader/portableValidation.js +461 -0
- package/dist/resource/shader/portableValidation.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2EffectConstant.js +100 -0
- package/dist/resource/shader/reflection/Tr2EffectConstant.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2EffectDefine.js +34 -0
- package/dist/resource/shader/reflection/Tr2EffectDefine.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2EffectDescription.js +113 -0
- package/dist/resource/shader/reflection/Tr2EffectDescription.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2EffectLibrary.js +141 -0
- package/dist/resource/shader/reflection/Tr2EffectLibrary.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2EffectParameterAnnotation.js +107 -0
- package/dist/resource/shader/reflection/Tr2EffectParameterAnnotation.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2EffectResource.js +90 -0
- package/dist/resource/shader/reflection/Tr2EffectResource.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2EffectStageInput.js +235 -0
- package/dist/resource/shader/reflection/Tr2EffectStageInput.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2EffectTechnique.js +71 -0
- package/dist/resource/shader/reflection/Tr2EffectTechnique.js.map +1 -0
- package/dist/resource/shader/reflection/Tr2Pass.js +111 -0
- package/dist/resource/shader/reflection/Tr2Pass.js.map +1 -0
- package/dist/resource/shader/sampler/Tr2SamplerSetup.js +80 -0
- package/dist/resource/shader/sampler/Tr2SamplerSetup.js.map +1 -0
- package/dist/{texture → resource/texture}/CjsTextureArrayRes.js +83 -6
- package/dist/resource/texture/CjsTextureArrayRes.js.map +1 -0
- package/dist/{texture/CjsTextureParameterProxy.js → resource/texture/CjsTextureArrayResParameterProxy.js} +56 -10
- package/dist/resource/texture/CjsTextureArrayResParameterProxy.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2ImageRes.js +6 -4
- package/dist/resource/texture/Tr2ImageRes.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TextureLodManager.js +3 -1
- package/dist/resource/texture/Tr2TextureLodManager.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TextureLodUpdateRequest.js +1 -1
- package/dist/resource/texture/Tr2TextureLodUpdateRequest.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TexturePackChannel.js +1 -1
- package/dist/resource/texture/Tr2TexturePackChannel.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TexturePipeline.js +6 -4
- package/dist/resource/texture/Tr2TexturePipeline.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TexturePipelineParams.js +1 -1
- package/dist/resource/texture/Tr2TexturePipelineParams.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TexturePipelineStepCompress.js +1 -1
- package/dist/resource/texture/Tr2TexturePipelineStepCompress.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TexturePipelineStepGenerateMips.js +1 -1
- package/dist/resource/texture/Tr2TexturePipelineStepGenerateMips.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TexturePipelineStepLimitSize.js +1 -1
- package/dist/resource/texture/Tr2TexturePipelineStepLimitSize.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TexturePipelineStepLoad.js +1 -1
- package/dist/resource/texture/Tr2TexturePipelineStepLoad.js.map +1 -0
- package/dist/{resources → resource/texture}/Tr2TexturePipelineStepPack.js +1 -1
- package/dist/resource/texture/Tr2TexturePipelineStepPack.js.map +1 -0
- package/dist/{resources → resource/texture}/TriTextureRes.js +42 -26
- package/dist/resource/texture/TriTextureRes.js.map +1 -0
- package/dist/resource/texture/index.js +15 -0
- package/dist/resource/texture/index.js.map +1 -0
- package/dist/{resources → resource/texture}/texturePipelineBehavior.js +59 -59
- package/dist/resource/texture/texturePipelineBehavior.js.map +1 -0
- package/dist/worker/CjsResManMainThreadLoader.js +87 -0
- package/dist/worker/CjsResManMainThreadLoader.js.map +1 -0
- package/dist/worker/CjsResManWorker.js +217 -0
- package/dist/worker/CjsResManWorker.js.map +1 -0
- package/dist/worker/CjsResManWorkerLoader.js +434 -0
- package/dist/worker/CjsResManWorkerLoader.js.map +1 -0
- package/dist/worker/protocol.js +12 -0
- package/dist/worker/protocol.js.map +1 -0
- package/docs/README.md +16 -3
- package/docs/architecture.md +39 -16
- package/docs/concepts/resource-lifecycle.md +2 -1
- package/docs/formats/README.md +4 -0
- package/docs/reference/classes/README.md +6 -4
- package/docs/reference/classes/audio.md +30 -0
- package/docs/reference/classes/core.md +31 -11
- package/docs/reference/classes/resources.md +151 -41
- package/docs/reference/classes/texture.md +7 -7
- package/docs/reference/motherlode-cache.md +17 -0
- package/docs/reference/queues.md +9 -1
- package/docs/reference/workers.md +135 -0
- package/docs/roadmap.md +27 -1
- package/package.json +8 -1
- package/dist/CjsResManQueue.js.map +0 -1
- package/dist/CjsResource.js.map +0 -1
- package/dist/CjsResourceSource.js +0 -68
- package/dist/CjsResourceSource.js.map +0 -1
- package/dist/resourcePath.js +0 -18
- package/dist/resourcePath.js.map +0 -1
- package/dist/resourceStates.js +0 -9
- package/dist/resourceStates.js.map +0 -1
- package/dist/resources/AudioGeometryResData.js.map +0 -1
- package/dist/resources/GStateBindingCallbackData.js.map +0 -1
- package/dist/resources/MeshDecalData.js.map +0 -1
- package/dist/resources/MeshDecalLodData.js.map +0 -1
- package/dist/resources/Tr2EffectRes.js +0 -71
- package/dist/resources/Tr2EffectRes.js.map +0 -1
- package/dist/resources/Tr2GrannyIntersectionResult.js.map +0 -1
- package/dist/resources/Tr2GrannyStateRes.js.map +0 -1
- package/dist/resources/Tr2ImageRes.js.map +0 -1
- package/dist/resources/Tr2LightProfileRes.js.map +0 -1
- package/dist/resources/Tr2MaterialArea.js.map +0 -1
- package/dist/resources/Tr2MaterialMesh.js.map +0 -1
- package/dist/resources/Tr2MaterialRes.js.map +0 -1
- package/dist/resources/Tr2ShaderPermutation.js.map +0 -1
- package/dist/resources/Tr2TextureLodManager.js.map +0 -1
- package/dist/resources/Tr2TextureLodUpdateRequest.js.map +0 -1
- package/dist/resources/Tr2TexturePackChannel.js.map +0 -1
- package/dist/resources/Tr2TexturePipeline.js.map +0 -1
- package/dist/resources/Tr2TexturePipelineParams.js.map +0 -1
- package/dist/resources/Tr2TexturePipelineStepCompress.js.map +0 -1
- package/dist/resources/Tr2TexturePipelineStepGenerateMips.js.map +0 -1
- package/dist/resources/Tr2TexturePipelineStepLimitSize.js.map +0 -1
- package/dist/resources/Tr2TexturePipelineStepLoad.js.map +0 -1
- package/dist/resources/Tr2TexturePipelineStepPack.js.map +0 -1
- package/dist/resources/TriGeometryRes.js +0 -239
- package/dist/resources/TriGeometryRes.js.map +0 -1
- package/dist/resources/TriGeometryResAreaData.js.map +0 -1
- package/dist/resources/TriGeometryResJointData.js.map +0 -1
- package/dist/resources/TriGeometryResLodData.js.map +0 -1
- package/dist/resources/TriGeometryResMeshData.js.map +0 -1
- package/dist/resources/TriGeometryResSkeletonData.js.map +0 -1
- package/dist/resources/TriGrannyRes.js.map +0 -1
- package/dist/resources/TriJointBinding.js.map +0 -1
- package/dist/resources/TriMorphTargetGeometryConstants.js.map +0 -1
- package/dist/resources/TriRtGeometryConstants.js.map +0 -1
- package/dist/resources/TriTextureRes.js.map +0 -1
- package/dist/resources/enums.js.map +0 -1
- package/dist/resources/resourceBoundary.js +0 -47
- package/dist/resources/resourceBoundary.js.map +0 -1
- package/dist/resources/texturePipelineBehavior.js.map +0 -1
- package/dist/texture/CjsTextureArrayRes.js.map +0 -1
- package/dist/texture/CjsTextureParameterProxy.js.map +0 -1
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Operation } from './worker/protocol.js';
|
|
2
|
+
|
|
3
|
+
const FETCH_OPTION_KEYS = Object.freeze(["body", "cache", "credentials", "duplex", "headers", "integrity", "keepalive", "method", "mode", "priority", "redirect", "referrer", "referrerPolicy", "window"]);
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `CjsResMan` provider that fetches an already-resolved URL on the caller
|
|
7
|
+
* thread or through the resource worker.
|
|
8
|
+
*/
|
|
9
|
+
class CjsResManFetchProvider {
|
|
10
|
+
/** Creates a CjsResManFetchProvider with caller-provided fetch policy. */
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
this.fetch = options.fetch || globalThis.fetch;
|
|
13
|
+
this.fetchOptions = options.fetchOptions || null;
|
|
14
|
+
this.workerEnabled = options.worker !== false && (options.fetch === undefined || options.worker === true);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Fetches bytes for a URL already resolved by CjsResMan.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} url Resolved source URL.
|
|
21
|
+
* @param {object} [options={}] Resource and fetch options.
|
|
22
|
+
* @returns {Promise<ArrayBuffer>} Response bytes.
|
|
23
|
+
*/
|
|
24
|
+
async Read(url, options = {}) {
|
|
25
|
+
if (typeof this.fetch !== "function") {
|
|
26
|
+
throw new TypeError("CjsResManFetchProvider requires a fetch implementation.");
|
|
27
|
+
}
|
|
28
|
+
const fetchOptions = getCloneableFetchOptions(this.fetchOptions, options.fetchOptions || options);
|
|
29
|
+
if (options.signal) fetchOptions.signal = options.signal;
|
|
30
|
+
const response = await this.fetch(url, fetchOptions);
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
const error = new Error(`Resource fetch failed: ${response.status} ${response.statusText}`);
|
|
33
|
+
error.code = "CJS_RESOURCE_FETCH_FAILED";
|
|
34
|
+
error.path = options.resourcePath || url;
|
|
35
|
+
error.url = url;
|
|
36
|
+
error.status = response.status;
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
return response.arrayBuffer();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Describe the equivalent cloneable worker fetch operation.
|
|
44
|
+
*
|
|
45
|
+
* An injected fetch implementation stays on the caller thread unless the
|
|
46
|
+
* source explicitly opts into worker fetch with `{ worker: true }`.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} url URL already resolved by CjsResMan.
|
|
49
|
+
* @param {object} [options={}] Resource and fetch options.
|
|
50
|
+
* @returns {object|null} Worker operation, or `null` when worker fetch is disabled.
|
|
51
|
+
*/
|
|
52
|
+
CreateWorkerRequest(url, options = {}) {
|
|
53
|
+
if (!this.workerEnabled) return null;
|
|
54
|
+
return {
|
|
55
|
+
operation: Operation.FETCH,
|
|
56
|
+
payload: {
|
|
57
|
+
url,
|
|
58
|
+
path: options.resourcePath || url,
|
|
59
|
+
responseType: "arraybuffer",
|
|
60
|
+
options: getCloneableFetchOptions(this.fetchOptions, options.fetchOptions || options)
|
|
61
|
+
},
|
|
62
|
+
signal: options.signal || null
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Signals that CjsResMan must resolve resource paths before provider reads. */
|
|
67
|
+
static requiresUrl = true;
|
|
68
|
+
}
|
|
69
|
+
function getCloneableFetchOptions(defaults, options) {
|
|
70
|
+
const result = {};
|
|
71
|
+
for (const source of [defaults, options]) {
|
|
72
|
+
if (!source || typeof source !== "object") continue;
|
|
73
|
+
for (const key of FETCH_OPTION_KEYS) {
|
|
74
|
+
if (source[key] === undefined) continue;
|
|
75
|
+
result[key] = key === "headers" ? normalizeFetchHeaders(source[key]) : source[key];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
function normalizeFetchHeaders(value) {
|
|
81
|
+
if (!value || typeof value !== "object") return value;
|
|
82
|
+
if (Array.isArray(value)) {
|
|
83
|
+
return value.map(entry => Array.isArray(entry) ? [...entry] : entry);
|
|
84
|
+
}
|
|
85
|
+
if (typeof value.entries === "function") {
|
|
86
|
+
return [...value.entries()];
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
...value
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { CjsResManFetchProvider };
|
|
94
|
+
//# sourceMappingURL=CjsResManFetchProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CjsResManFetchProvider.js","sources":["../../src/CjsResManFetchProvider.js"],"sourcesContent":["import { Operation } from \"./worker/protocol.js\";\n\nconst FETCH_OPTION_KEYS = Object.freeze([\n \"body\",\n \"cache\",\n \"credentials\",\n \"duplex\",\n \"headers\",\n \"integrity\",\n \"keepalive\",\n \"method\",\n \"mode\",\n \"priority\",\n \"redirect\",\n \"referrer\",\n \"referrerPolicy\",\n \"window\"\n]);\n\n/**\n * `CjsResMan` provider that fetches an already-resolved URL on the caller\n * thread or through the resource worker.\n */\nexport class CjsResManFetchProvider\n{\n /** Creates a CjsResManFetchProvider with caller-provided fetch policy. */\n constructor(options = {})\n {\n this.fetch = options.fetch || globalThis.fetch;\n this.fetchOptions = options.fetchOptions || null;\n this.workerEnabled = options.worker !== false\n && (options.fetch === undefined || options.worker === true);\n }\n\n /**\n * Fetches bytes for a URL already resolved by CjsResMan.\n *\n * @param {string} url Resolved source URL.\n * @param {object} [options={}] Resource and fetch options.\n * @returns {Promise<ArrayBuffer>} Response bytes.\n */\n async Read(url, options = {})\n {\n if (typeof this.fetch !== \"function\")\n {\n throw new TypeError(\"CjsResManFetchProvider requires a fetch implementation.\");\n }\n\n const fetchOptions = getCloneableFetchOptions(\n this.fetchOptions,\n options.fetchOptions || options\n );\n if (options.signal) fetchOptions.signal = options.signal;\n const response = await this.fetch(url, fetchOptions);\n if (!response.ok)\n {\n const error = new Error(`Resource fetch failed: ${response.status} ${response.statusText}`);\n error.code = \"CJS_RESOURCE_FETCH_FAILED\";\n error.path = options.resourcePath || url;\n error.url = url;\n error.status = response.status;\n throw error;\n }\n\n return response.arrayBuffer();\n }\n\n /**\n * Describe the equivalent cloneable worker fetch operation.\n *\n * An injected fetch implementation stays on the caller thread unless the\n * source explicitly opts into worker fetch with `{ worker: true }`.\n *\n * @param {string} url URL already resolved by CjsResMan.\n * @param {object} [options={}] Resource and fetch options.\n * @returns {object|null} Worker operation, or `null` when worker fetch is disabled.\n */\n CreateWorkerRequest(url, options = {})\n {\n if (!this.workerEnabled) return null;\n return {\n operation: Operation.FETCH,\n payload: {\n url,\n path: options.resourcePath || url,\n responseType: \"arraybuffer\",\n options: getCloneableFetchOptions(this.fetchOptions, options.fetchOptions || options)\n },\n signal: options.signal || null\n };\n }\n\n /** Signals that CjsResMan must resolve resource paths before provider reads. */\n static requiresUrl = true;\n}\n\nfunction getCloneableFetchOptions(defaults, options) {\n const result = {};\n for (const source of [ defaults, options ]) {\n if (!source || typeof source !== \"object\") continue;\n for (const key of FETCH_OPTION_KEYS) {\n if (source[key] === undefined) continue;\n result[key] = key === \"headers\"\n ? normalizeFetchHeaders(source[key])\n : source[key];\n }\n }\n return result;\n}\n\nfunction normalizeFetchHeaders(value) {\n if (!value || typeof value !== \"object\") return value;\n if (Array.isArray(value)) {\n return value.map(entry => Array.isArray(entry) ? [ ...entry ] : entry);\n }\n if (typeof value.entries === \"function\") {\n return [ ...value.entries() ];\n }\n return { ...value };\n}\n"],"names":["FETCH_OPTION_KEYS","Object","freeze","CjsResManFetchProvider","constructor","options","fetch","globalThis","fetchOptions","workerEnabled","worker","undefined","Read","url","TypeError","getCloneableFetchOptions","signal","response","ok","error","Error","status","statusText","code","path","resourcePath","arrayBuffer","CreateWorkerRequest","operation","Operation","FETCH","payload","responseType","requiresUrl","defaults","result","source","key","normalizeFetchHeaders","value","Array","isArray","map","entry","entries"],"mappings":";;AAEA,MAAMA,iBAAiB,GAAGC,MAAM,CAACC,MAAM,CAAC,CACtC,MAAM,EACN,OAAO,EACP,aAAa,EACb,QAAQ,EACR,SAAS,EACT,WAAW,EACX,WAAW,EACX,QAAQ,EACR,MAAM,EACN,UAAU,EACV,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,QAAQ,CACT,CAAC;;AAEF;AACA;AACA;AACA;AACO,MAAMC,sBAAsB,CACnC;AACE;AACAC,EAAAA,WAAWA,CAACC,OAAO,GAAG,EAAE,EACxB;IACE,IAAI,CAACC,KAAK,GAAGD,OAAO,CAACC,KAAK,IAAIC,UAAU,CAACD,KAAK;AAC9C,IAAA,IAAI,CAACE,YAAY,GAAGH,OAAO,CAACG,YAAY,IAAI,IAAI;IAChD,IAAI,CAACC,aAAa,GAAGJ,OAAO,CAACK,MAAM,KAAK,KAAK,KACvCL,OAAO,CAACC,KAAK,KAAKK,SAAS,IAAIN,OAAO,CAACK,MAAM,KAAK,IAAI,CAAC;AAC/D,EAAA;;AAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACE,MAAME,IAAIA,CAACC,GAAG,EAAER,OAAO,GAAG,EAAE,EAC5B;AACE,IAAA,IAAI,OAAO,IAAI,CAACC,KAAK,KAAK,UAAU,EACpC;AACE,MAAA,MAAM,IAAIQ,SAAS,CAAC,yDAAyD,CAAC;AAChF,IAAA;AAEA,IAAA,MAAMN,YAAY,GA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
|
5
5
|
LOAD: "background"
|
|
6
6
|
});
|
|
7
|
-
function NormalizeCjsResManQueue(value) {
|
|
8
|
-
const name = String(value ?? "").trim().toLowerCase();
|
|
9
|
-
if (name === "main" || name === "prepare") return CjsResManQueue.MAIN;
|
|
10
|
-
if (name === "background" || name === "load") return CjsResManQueue.BACKGROUND;
|
|
11
|
-
throw new TypeError(`Unknown CjsResMan queue: ${value}`);
|
|
12
|
-
}
|
|
13
|
-
function CjsResManQueueCancelledError(queue, id, reason = "") {
|
|
14
|
-
const error = new Error(`CjsResMan ${queue} queue item ${id} was cancelled.${reason ? ` ${reason}` : ""}`);
|
|
15
|
-
error.code = "CJS_RESMAN_QUEUE_CANCELLED";
|
|
16
|
-
error.queue = queue;
|
|
17
|
-
error.id = id;
|
|
18
|
-
return error;
|
|
19
|
-
}
|
|
20
7
|
|
|
21
8
|
/**
|
|
22
|
-
* Small FIFO executor used
|
|
9
|
+
* Small FIFO executor used inside `CjsResMan` that tracks item ids, pause
|
|
10
|
+
* state, concurrency, cancellation, and sync/async completion while queue
|
|
11
|
+
* policy stays in the manager.
|
|
23
12
|
*
|
|
24
|
-
*
|
|
25
|
-
* concurrency, cancellation, and sync/async completion.
|
|
13
|
+
* The queue deliberately does not choose task priority or resource policy.
|
|
26
14
|
*/
|
|
27
15
|
class CjsResManWorkQueue {
|
|
28
16
|
#active = new Map();
|
|
@@ -34,11 +22,18 @@ class CjsResManWorkQueue {
|
|
|
34
22
|
#onReady;
|
|
35
23
|
#paused = false;
|
|
36
24
|
#queued = new Map();
|
|
25
|
+
|
|
26
|
+
/** Creates a CjsResManWorkQueue with caller-provided initial state. */
|
|
37
27
|
constructor(name, options = {}) {
|
|
38
|
-
this.#name =
|
|
28
|
+
this.#name = CjsResManWorkQueue.normalizeName(name);
|
|
39
29
|
this.#onReady = typeof options.onReady === "function" ? options.onReady : null;
|
|
40
30
|
this.SetConcurrency(options.concurrency ?? 1);
|
|
41
31
|
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Updates the maximum number of tasks that may run together for the resource
|
|
35
|
+
* work queue.
|
|
36
|
+
*/
|
|
42
37
|
SetConcurrency(value) {
|
|
43
38
|
if (!Number.isInteger(value) || value < 1) {
|
|
44
39
|
throw new TypeError("CjsResMan queue concurrency must be a positive integer.");
|
|
@@ -47,24 +42,53 @@ class CjsResManWorkQueue {
|
|
|
47
42
|
this.#NotifyReady();
|
|
48
43
|
return this;
|
|
49
44
|
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns the maximum number of tasks allowed to run together for the resource
|
|
48
|
+
* work queue.
|
|
49
|
+
*/
|
|
50
50
|
GetConcurrency() {
|
|
51
51
|
return this.#concurrency;
|
|
52
52
|
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Allocates the next monotonically increasing task identifier for the resource
|
|
56
|
+
* work queue.
|
|
57
|
+
*/
|
|
53
58
|
GetNextId() {
|
|
54
59
|
return this.#nextId;
|
|
55
60
|
}
|
|
61
|
+
|
|
62
|
+
/** Returns the number of tasks waiting to start for the resource work queue. */
|
|
56
63
|
GetQueuedCount() {
|
|
57
64
|
return this.#queued.size;
|
|
58
65
|
}
|
|
66
|
+
|
|
67
|
+
/** Returns the number of tasks currently executing for the resource work queue. */
|
|
59
68
|
GetActiveCount() {
|
|
60
69
|
return this.#active.size;
|
|
61
70
|
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Returns the combined number of waiting and active tasks for the resource
|
|
74
|
+
* work queue.
|
|
75
|
+
*/
|
|
62
76
|
GetPendingCount() {
|
|
63
77
|
return this.#queued.size + this.#active.size;
|
|
64
78
|
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Reports whether the queue is prevented from starting work for the resource
|
|
82
|
+
* work queue.
|
|
83
|
+
*/
|
|
65
84
|
IsPaused() {
|
|
66
85
|
return this.#paused;
|
|
67
86
|
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Enqueues one task and returns its cancellation-aware promise for the
|
|
90
|
+
* resource work queue.
|
|
91
|
+
*/
|
|
68
92
|
Add(callback, context = null, metadata = null) {
|
|
69
93
|
if (typeof callback !== "function") {
|
|
70
94
|
throw new TypeError("CjsResMan queue items require a callback.");
|
|
@@ -92,35 +116,51 @@ class CjsResManWorkQueue {
|
|
|
92
116
|
this.#NotifyReady();
|
|
93
117
|
return item;
|
|
94
118
|
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Cancels a queued task selected by its identifier for the resource work
|
|
122
|
+
* queue.
|
|
123
|
+
*/
|
|
95
124
|
Cancel(id, reason = "") {
|
|
96
125
|
const item = this.#queued.get(id);
|
|
97
126
|
if (!item) return false;
|
|
98
127
|
this.#queued.delete(id);
|
|
99
128
|
item.state = "cancelled";
|
|
100
|
-
item.reject(
|
|
129
|
+
item.reject(CjsResManWorkQueue.createCancelledError(this.#name, id, reason));
|
|
101
130
|
this.#Compact();
|
|
102
131
|
return true;
|
|
103
132
|
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Prevents queued tasks from starting until resumed for the resource work
|
|
136
|
+
* queue.
|
|
137
|
+
*/
|
|
104
138
|
Pause() {
|
|
105
139
|
this.#paused = true;
|
|
106
140
|
return this;
|
|
107
141
|
}
|
|
142
|
+
|
|
143
|
+
/** Allows queued tasks to start after a pause for the resource work queue. */
|
|
108
144
|
Resume() {
|
|
109
145
|
if (!this.#paused) return this;
|
|
110
146
|
this.#paused = false;
|
|
111
147
|
this.#NotifyReady();
|
|
112
148
|
return this;
|
|
113
149
|
}
|
|
150
|
+
|
|
151
|
+
/** Cancels every task that has not started for the resource work queue. */
|
|
114
152
|
Clear(reason = "Queue cleared.") {
|
|
115
153
|
const ids = [...this.#queued.keys()];
|
|
116
154
|
for (const id of ids) this.Cancel(id, reason);
|
|
117
155
|
this.#Compact(true);
|
|
118
156
|
return ids.length;
|
|
119
157
|
}
|
|
158
|
+
|
|
159
|
+
/** Starts queued work while concurrency permits for the resource work queue. */
|
|
120
160
|
Pump(options = {}) {
|
|
121
|
-
const maxItems =
|
|
122
|
-
const maxTime =
|
|
123
|
-
const now = typeof options.now === "function" ? options.now :
|
|
161
|
+
const maxItems = normalizeLimit(options.maxItems);
|
|
162
|
+
const maxTime = normalizeLimit(options.maxTime);
|
|
163
|
+
const now = typeof options.now === "function" ? options.now : defaultNow;
|
|
124
164
|
const startedAt = now();
|
|
125
165
|
let processed = 0;
|
|
126
166
|
if (!this.#paused) {
|
|
@@ -141,6 +181,11 @@ class CjsResManWorkQueue {
|
|
|
141
181
|
paused: this.#paused
|
|
142
182
|
});
|
|
143
183
|
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Returns an immutable snapshot of queue counts and policy for the resource
|
|
187
|
+
* work queue.
|
|
188
|
+
*/
|
|
144
189
|
GetStats() {
|
|
145
190
|
return Object.freeze({
|
|
146
191
|
name: this.#name,
|
|
@@ -152,6 +197,8 @@ class CjsResManWorkQueue {
|
|
|
152
197
|
paused: this.#paused
|
|
153
198
|
});
|
|
154
199
|
}
|
|
200
|
+
|
|
201
|
+
/** Removes and returns the next runnable work item for the resource work queue. */
|
|
155
202
|
#TakeNext() {
|
|
156
203
|
while (this.#head < this.#items.length) {
|
|
157
204
|
const item = this.#items[this.#head++];
|
|
@@ -161,6 +208,8 @@ class CjsResManWorkQueue {
|
|
|
161
208
|
}
|
|
162
209
|
return null;
|
|
163
210
|
}
|
|
211
|
+
|
|
212
|
+
/** Starts one queued work item for the resource work queue. */
|
|
164
213
|
#Start(item) {
|
|
165
214
|
item.state = "active";
|
|
166
215
|
this.#active.set(item.id, item);
|
|
@@ -181,34 +230,72 @@ class CjsResManWorkQueue {
|
|
|
181
230
|
}
|
|
182
231
|
this.#Settle(item, true, result);
|
|
183
232
|
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Settles one active work item and advances the queue for the resource work
|
|
236
|
+
* queue.
|
|
237
|
+
*/
|
|
184
238
|
#Settle(item, didResolve, value) {
|
|
185
239
|
if (!this.#active.delete(item.id)) return;
|
|
186
240
|
item.state = didResolve ? "resolved" : "rejected";
|
|
187
241
|
if (didResolve) item.resolve(value);else item.reject(value);
|
|
188
242
|
this.#NotifyReady();
|
|
189
243
|
}
|
|
244
|
+
|
|
245
|
+
/** Removes settled work items from the queue for the resource work queue. */
|
|
190
246
|
#Compact(force = false) {
|
|
191
247
|
if (force || this.#head > 256 && this.#head > this.#items.length * 0.5) {
|
|
192
248
|
this.#items = this.#items.slice(this.#head);
|
|
193
249
|
this.#head = 0;
|
|
194
250
|
}
|
|
195
251
|
}
|
|
252
|
+
|
|
253
|
+
/** Notifies waiters when the queue becomes ready for the resource work queue. */
|
|
196
254
|
#NotifyReady() {
|
|
197
255
|
if (this.#onReady && !this.#paused && this.GetQueuedCount() > 0) {
|
|
198
256
|
this.#onReady(this);
|
|
199
257
|
}
|
|
200
258
|
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Resolves a queue name or alias to its canonical execution lane.
|
|
262
|
+
*
|
|
263
|
+
* @param {*} value
|
|
264
|
+
* @returns {string}
|
|
265
|
+
*/
|
|
266
|
+
static normalizeName(value) {
|
|
267
|
+
const name = String(value ?? "").trim().toLowerCase();
|
|
268
|
+
if (name === "main" || name === "prepare") return CjsResManQueue.MAIN;
|
|
269
|
+
if (name === "background" || name === "load") return CjsResManQueue.BACKGROUND;
|
|
270
|
+
throw new TypeError(`Unknown CjsResMan queue: ${value}`);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Creates the structured error used when queued work is cancelled.
|
|
275
|
+
*
|
|
276
|
+
* @param {string} queue
|
|
277
|
+
* @param {number} id
|
|
278
|
+
* @param {string} reason
|
|
279
|
+
* @returns {Error}
|
|
280
|
+
*/
|
|
281
|
+
static createCancelledError(queue, id, reason = "") {
|
|
282
|
+
const error = new Error(`CjsResMan ${queue} queue item ${id} was cancelled.${reason ? ` ${reason}` : ""}`);
|
|
283
|
+
error.code = "CJS_RESMAN_QUEUE_CANCELLED";
|
|
284
|
+
error.queue = queue;
|
|
285
|
+
error.id = id;
|
|
286
|
+
return error;
|
|
287
|
+
}
|
|
201
288
|
}
|
|
202
|
-
function
|
|
289
|
+
function normalizeLimit(value) {
|
|
203
290
|
if (value === undefined || value === null || value === 0) return Number.POSITIVE_INFINITY;
|
|
204
291
|
if (typeof value !== "number" || Number.isNaN(value) || value < 0) {
|
|
205
292
|
throw new TypeError("CjsResMan queue limits must be non-negative numbers.");
|
|
206
293
|
}
|
|
207
294
|
return value;
|
|
208
295
|
}
|
|
209
|
-
function
|
|
296
|
+
function defaultNow() {
|
|
210
297
|
return globalThis.performance?.now?.() ?? Date.now();
|
|
211
298
|
}
|
|
212
299
|
|
|
213
|
-
export { CjsResManQueue,
|
|
214
|
-
//# sourceMappingURL=
|
|
300
|
+
export { CjsResManQueue, CjsResManWorkQueue };
|
|
301
|
+
//# sourceMappingURL=CjsResManWorkQueue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CjsResManWorkQueue.js","sources":["../../src/CjsResManWorkQueue.js"],"sourcesContent":["export const CjsResManQueue = Object.freeze({\n MAIN: \"main\",\n BACKGROUND: \"background\",\n PREPARE: \"main\",\n LOAD: \"background\"\n});\n\n/**\n * Small FIFO executor used inside `CjsResMan` that tracks item ids, pause\n * state, concurrency, cancellation, and sync/async completion while queue\n * policy stays in the manager.\n *\n * The queue deliberately does not choose task priority or resource policy.\n */\nexport class CjsResManWorkQueue\n{\n #active = new Map();\n #concurrency = 1;\n #head = 0;\n #items = [];\n #name;\n #nextId = 1;\n #onReady;\n #paused = false;\n #queued = new Map();\n\n /** Creates a CjsResManWorkQueue with caller-provided initial state. */\n constructor(name, options = {}) {\n this.#name = CjsResManWorkQueue.normalizeName(name);\n this.#onReady = typeof options.onReady === \"function\" ? options.onReady : null;\n this.SetConcurrency(options.concurrency ?? 1);\n }\n\n /**\n * Updates the maximum number of tasks that may run together for the resource\n * work queue.\n */\n SetConcurrency(value) {\n if (!Number.isInteger(value) || value < 1) {\n throw new TypeError(\"CjsResMan queue concurrency must be a positive integer.\");\n }\n this.#concurrency = value;\n this.#NotifyReady();\n return this;\n }\n\n /**\n * Returns the maximum number of tasks allowed to run together for the resource\n * work queue.\n */\n GetConcurrency() {\n return this.#concurrency;\n }\n\n /**\n * Allocates the next monotonically increasing task identifier for the resource\n * work queue.\n */\n GetNextId() {\n return this.#nextId;\n }\n\n /** Returns the number of tasks waiting to start for the resource work queue. */\n GetQueuedCount() {\n return this.#queued.size;\n }\n\n /** Returns the number of tasks currently executing for the resource work queue. */\n GetActiveCount() {\n return this.#active.size;\n }\n\n /**\n * Returns the combined number of waiting and active tasks for the resource\n * work queue.\n */\n GetPendingCount() {\n return this.#queued.size + this.#active.size;\n }\n\n /**\n * Reports whether the queue is prevented from starting work for the resource\n * work queue.\n */\n IsPaused() {\n return this.#paused;\n }\n\n /**\n * Enqueues one task and returns its cancellation-aware promise for the\n * resource work queue.\n */\n Add(callback, context = null, metadata = null) {\n if (typeof callback !== \"function\") {\n throw new TypeError(\"CjsResMan queue items require a callback.\");\n }\n\n const id = this.#nextId++;\n let resolve;\n let reject;\n const promise = new Promise((onResolve, onReject) => {\n resolve = onResolve;\n reject = onReject;\n });\n const item = {\n id,\n queue: this.#name,\n callback,\n context,\n metadata,\n promise,\n resolve,\n reject,\n state: \"queued\"\n };\n\n this.#items.push(item);\n this.#queued.set(id, item);\n this.#NotifyReady();\n return item;\n }\n\n /**\n * Cancels a queued task selected by its identifier for the resource work\n * queue.\n */\n Cancel(id, reason = \"\") {\n const item = this.#queued.get(id);\n if (!item) return false;\n this.#queued.delete(id);\n item.state = \"cancelled\";\n item.reject(CjsResManWorkQueue.createCancelledError(this.#name, id, reason));\n this.#Compact();\n return true;\n }\n\n /**\n * Prevents queued tasks from starting until resumed for the resource work\n * queue.\n */\n Pause() {\n this.#paused = true;\n return this;\n }\n\n /** Allows queued tasks to start after a pause for the resource work queue. */\n Resume() {\n if (!this.#paused) return this;\n this.#paused = false;\n this.#NotifyReady();\n return this;\n }\n\n /** Cancels every task that has not started for the resource work queue. */\n Clear(reason = \"Queue cleared.\") {\n const ids = [ ...this.#queued.keys() ];\n for (const id of ids) this.Cancel(id, reason);\n this.#Compact(true);\n return ids.length;\n }\n\n /** Starts queued work while concurrency permits for the resource work queue. */\n Pump(options = {}) {\n const maxItems = normalizeLimit(options.maxItems);\n const maxTime = normalizeLimit(options.maxTime);\n const now = typeof options.now === \"function\" ? options.now : defaultNow;\n const startedAt = now();\n let processed = 0;\n\n if (!this.#paused) {\n while (this.#active.size < this.#concurrency && processed < maxItems) {\n const item = this.#TakeNext();\n if (!item) break;\n this.#Start(item);\n processed++;\n if (processed > 0 && now() - startedAt >= maxTime) break;\n }\n }\n\n this.#Compact();\n return Object.freeze({\n processed,\n queued: this.GetQueuedCount(),\n active: this.GetActiveCount(),\n pending: this.GetPendingCount(),\n paused: this.#paused\n });\n }\n\n /**\n * Returns an immutable snapshot of queue counts and policy for the resource\n * work queue.\n */\n GetStats() {\n return Object.freeze({\n name: this.#name,\n nextId: this.#nextId,\n concurrency: this.#concurrency,\n queued: this.GetQueuedCount(),\n active: this.GetActiveCount(),\n pending: this.GetPendingCount(),\n paused: this.#paused\n });\n }\n\n /** Removes and returns the next runnable work item for the resource work queue. */\n #TakeNext() {\n while (this.#head < this.#items.length) {\n const item = this.#items[this.#head++];\n if (item.state !== \"queued\") continue;\n this.#queued.delete(item.id);\n return item;\n }\n return null;\n }\n\n /** Starts one queued work item for the resource work queue. */\n #Start(item) {\n item.state = \"active\";\n this.#active.set(item.id, item);\n\n let result;\n try {\n result = item.callback.call(item.context, Object.freeze({\n id: item.id,\n queue: item.queue,\n metadata: item.metadata\n }));\n } catch (error) {\n this.#Settle(item, false, error);\n return;\n }\n\n if (result && typeof result.then === \"function\") {\n Promise.resolve(result).then(\n value => this.#Settle(item, true, value),\n error => this.#Settle(item, false, error)\n );\n return;\n }\n this.#Settle(item, true, result);\n }\n\n /**\n * Settles one active work item and advances the queue for the resource work\n * queue.\n */\n #Settle(item, didResolve, value) {\n if (!this.#active.delete(item.id)) return;\n item.state = didResolve ? \"resolved\" : \"rejected\";\n if (didResolve) item.resolve(value);\n else item.reject(value);\n this.#NotifyReady();\n }\n\n /** Removes settled work items from the queue for the resource work queue. */\n #Compact(force = false) {\n if (force || (this.#head > 256 && this.#head > this.#items.length * 0.5)) {\n this.#items = this.#items.slice(this.#head);\n this.#head = 0;\n }\n }\n\n /** Notifies waiters when the queue becomes ready for the resource work queue. */\n #NotifyReady() {\n if (this.#onReady && !this.#paused && this.GetQueuedCount() > 0) {\n this.#onReady(this);\n }\n }\n\n /**\n * Resolves a queue name or alias to its canonical execution lane.\n *\n * @param {*} value\n * @returns {string}\n */\n static normalizeName(value)\n {\n const name = String(value ?? \"\").trim().toLowerCase();\n if (name === \"main\" || name === \"prepare\") return CjsResManQueue.MAIN;\n if (name === \"background\" || name === \"load\") return CjsResManQueue.BACKGROUND;\n throw new TypeError(`Unknown CjsResMan queue: ${value}`);\n }\n\n /**\n * Creates the structured error used when queued work is cancelled.\n *\n * @param {string} queue\n * @param {number} id\n * @param {string} reason\n * @returns {Error}\n */\n static createCancelledError(queue, id, reason = \"\")\n {\n const error = new Error(`CjsResMan ${queue} queue item ${id} was cancelled.${reason ? ` ${reason}` : \"\"}`);\n error.code = \"CJS_RESMAN_QUEUE_CANCELLED\";\n error.queue = queue;\n error.id = id;\n return error;\n }\n}\n\nfunction normalizeLimit(value) {\n if (value === undefined || value === null || value === 0) return Number.POSITIVE_INFINITY;\n if (typeof value !== \"number\" || Number.isNaN(value) || value < 0) {\n throw new TypeError(\"CjsResMan queue limits must be non-negative numbers.\");\n }\n return value;\n}\n\nfunction defaultNow() {\n return globalThis.performance?.now?.() ?? Date.now();\n}\n"],"names":["CjsResManQueue","Object","freeze","MAIN","BACKGROUND","PREPARE","LOAD","CjsResManWorkQueue","Map","constructor","name","options","normalizeName","onReady","SetConcurrency","concurrency","value","Number","isInteger","TypeError","GetConcurrency","GetNextId","GetQueuedCount","size","GetActiveCount","GetPendingCount","IsPaused","Add","callback","context","metadata","id","resolve","reject","promise","Promise","onResolve","onReject","item","queue","state","push","set","Cancel","reason","get","delete","createCancelledError","Pause","Resume","Clear","ids","keys","length","Pump","maxItems","normalizeLimit","maxTime","now","defaultNow","startedAt","processed","queued","active","pending","paused","GetStats","nextId","#TakeNext","#Start","result","call","error","then","#Settle","didResolve","#Compact","force","slice","#NotifyReady","String","trim","toLowerCase","Error","code","undefined","POSITIVE_INFINITY","isNaN","globalThis","performance","Date"],"mappings":"MAAaA,cAAc,GAAGC,MAAM,CAACC,MAAM,CAAC;AAC1CC,EAAAA,IAAI,EAAE,MAAM;AACZC,EAAAA,UAAU,EAAE,YAAY;AACxBC,EAAAA,OAAO,EAAE,MAAM;AACfC,EAAAA,IAAI,EAAE;AACR,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,kBAAkB,CAC/B;AACE,EAAA,OAAO,GAAG,IAAIC,GAAG,EAAE;EACnB,YAAY,GAAG,CAAC;EAChB,KAAK,GAAG,CAAC;EACT,MAAM,GAAG,EAAE;AACX,EAAA,KAAK;EACL,OAAO,GAAG,CAAC;AACX,EAAA,QAAQ;EACR,OAAO,GAAG,KAAK;AACf,EAAA,OAAO,GAAG,IAAIA,GAAG,EAAE;;AAEnB;AACAC,EAAAA,WAAWA,CAACC,IAAI,EAAEC,OAAO,GAAG,EAAE,EAAE;IAC9B,IAAI,CAAC,KAAK,GAAGJ,kBAAkB,CAACK,aAAa,CAACF,IAAI,CAAC;AACnD,IAAA,IAAI,CAAC,QAAQ,GAAG,OAAOC,OAAO,CAACE,OAAO,KAAK,UAAU,GAAGF,OAAO,CAACE,OAAO,GAAG,IAAI;IAC9E,IAAI,CAACC,cAAc,CAACH,OAAO,CAACI,WAAW,IAAI,CAAC,CAAC;AAC/C,EAAA;;AAEA;AACF;AACA;AACA;EACED,cAAcA,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+B,oBAAoBA,CAACR,KAAK,EAAER,EAAE,EAAEa,MAAM,GAAG,EAAE,EAClD;AACE,IAAA,MAAM4B,KAAK,GAAG,IAAIW,KAAK,CAAC,CAAA,UAAA,EAAa5C,KAAK,CAAA,YAAA,EAAeR,EAAE,CAAA,eAAA,EAAkBa,MAAM,GAAG,CAAA,CAAA,EAAIA,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC;IAC1G4B,KAAK,CAACY,IAAI,GAAG,4BAA4B;IACzCZ,KAAK,CAACjC,KAAK,GAAGA,KAAK;IACnBiC,KAAK,CAACzC,EAAE,GAAGA,EAAE;AACb,IAAA,OAAOyC,KAAK;AACd,EAAA;AACF;AAEA,SAAShB,cAAcA,CAACxC,KAAK,EAAE;AAC7B,EAAA,IAAIA,KAAK,KAAKqE,SAAS,IAAIrE,KAAK,KAAK,IAAI,IAAIA,KAAK,KAAK,CAAC,EAAE,OAAOC,MAAM,CAACqE,iBAAiB;AACzF,EAAA,IAAI,OAAOtE,KAAK,KAAK,QAAQ,IAAIC,MAAM,CAACsE,KAAK,CAACvE,KAAK,CAAC,IAAIA,KAAK,GAAG,CAAC,EAAE;AACjE,IAAA,MAAM,IAAIG,SAAS,CAAC,sDAAsD,CAAC;AAC7E,EAAA;AACA,EAAA,OAAOH,KAAK;AACd;AAEA,SAAS2C,UAAUA,GAAG;AACpB,EAAA,OAAO6B,UAAU,CAACC,WAAW,EAAE/B,GAAG,IAAI,IAAIgC,IAAI,CAAChC,GAAG,EAAE;AACtD;;;;"}
|
|
@@ -14,6 +14,10 @@ import { CjsReader } from './CjsReader.js';
|
|
|
14
14
|
* path yet and retains its lenient named-field behavior.
|
|
15
15
|
*/
|
|
16
16
|
class CjsBlueReader extends CjsReader {
|
|
17
|
+
/**
|
|
18
|
+
* Creates a CjsBlueReader over caller-provided resource bytes and reader
|
|
19
|
+
* options.
|
|
20
|
+
*/
|
|
17
21
|
constructor(options = {}, config = {}) {
|
|
18
22
|
super({
|
|
19
23
|
...options
|
|
@@ -37,15 +41,24 @@ class CjsBlueReader extends CjsReader {
|
|
|
37
41
|
};
|
|
38
42
|
this.ResetBlueReadState();
|
|
39
43
|
}
|
|
44
|
+
|
|
45
|
+
/** Clears blue read state before reusing the current Blue graph reader. */
|
|
40
46
|
ResetBlueReadState() {
|
|
41
47
|
this.reports = [];
|
|
42
48
|
this.runtimeInstances = [];
|
|
43
49
|
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Finalizes runtime instances after the current Blue graph reader has been
|
|
53
|
+
* read.
|
|
54
|
+
*/
|
|
44
55
|
FinalizeRuntimeInstances() {
|
|
45
56
|
for (const record of this.runtimeInstances) {
|
|
46
57
|
this.adapter.finalize(record.instance, this.CreateRuntimeContext(record.kind, record.shape, false));
|
|
47
58
|
}
|
|
48
59
|
}
|
|
60
|
+
|
|
61
|
+
/** Creates runtime target for the current Blue graph reader. */
|
|
49
62
|
CreateRuntimeTarget(kind, shape = null) {
|
|
50
63
|
const built = this.adapter.construct(kind, this.CreateRuntimeContext(kind, shape, true, this.options));
|
|
51
64
|
if (built !== undefined) return built;
|
|
@@ -53,6 +66,8 @@ class CjsBlueReader extends CjsReader {
|
|
|
53
66
|
if (ClassConstructor) return new ClassConstructor();
|
|
54
67
|
return this.CreateRuntimeFallback(kind, shape);
|
|
55
68
|
}
|
|
69
|
+
|
|
70
|
+
/** Creates runtime fallback for the current Blue graph reader. */
|
|
56
71
|
CreateRuntimeFallback(kind, shape = null) {
|
|
57
72
|
const target = {
|
|
58
73
|
_sourceClassName: this.includeSourceShape ? kind : kind || null
|
|
@@ -60,6 +75,8 @@ class CjsBlueReader extends CjsReader {
|
|
|
60
75
|
if (this.includeSourceShape) target._sourceShape = shape || null;
|
|
61
76
|
return target;
|
|
62
77
|
}
|
|
78
|
+
|
|
79
|
+
/** Applies runtime values to the current Blue graph reader. */
|
|
63
80
|
ApplyRuntimeValues(target, values, kind, shape = null) {
|
|
64
81
|
this.adapter.applyValues(target, values, this.CreateRuntimeContext(kind, shape, true, this.hydrationOptions));
|
|
65
82
|
this.runtimeInstances.push({
|
|
@@ -68,6 +85,8 @@ class CjsBlueReader extends CjsReader {
|
|
|
68
85
|
shape
|
|
69
86
|
});
|
|
70
87
|
}
|
|
88
|
+
|
|
89
|
+
/** Creates runtime context for the current Blue graph reader. */
|
|
71
90
|
CreateRuntimeContext(kind, shape, includeOptions, options = null) {
|
|
72
91
|
const context = {
|
|
73
92
|
kind
|
|
@@ -76,12 +95,16 @@ class CjsBlueReader extends CjsReader {
|
|
|
76
95
|
if (includeOptions) context.options = options;
|
|
77
96
|
return context;
|
|
78
97
|
}
|
|
98
|
+
|
|
99
|
+
/** Creates payload target for the current Blue graph reader. */
|
|
79
100
|
CreatePayloadTarget(kind) {
|
|
80
101
|
const typeField = this.GetPayloadTypeField();
|
|
81
102
|
return typeField && (kind || this.includeEmptyPayloadType) ? {
|
|
82
103
|
[typeField]: kind
|
|
83
104
|
} : {};
|
|
84
105
|
}
|
|
106
|
+
|
|
107
|
+
/** Creates payload reference for the current Blue graph reader. */
|
|
85
108
|
CreatePayloadReference(targetObject, referenceId) {
|
|
86
109
|
const idField = this.GetPayloadIdField();
|
|
87
110
|
const referenceField = this.GetPayloadReferenceField();
|
|
@@ -94,18 +117,28 @@ class CjsBlueReader extends CjsReader {
|
|
|
94
117
|
[referenceField]: referenceId
|
|
95
118
|
} : targetObject;
|
|
96
119
|
}
|
|
120
|
+
|
|
121
|
+
/** Returns payload type field from the current Blue graph reader. */
|
|
97
122
|
GetPayloadTypeField() {
|
|
98
123
|
return this.GetPayloadField("payloadTypeField", "_type");
|
|
99
124
|
}
|
|
125
|
+
|
|
126
|
+
/** Returns payload ID field from the current Blue graph reader. */
|
|
100
127
|
GetPayloadIdField() {
|
|
101
128
|
return this.GetPayloadField("payloadIdField", "_id");
|
|
102
129
|
}
|
|
130
|
+
|
|
131
|
+
/** Returns payload reference field from the current Blue graph reader. */
|
|
103
132
|
GetPayloadReferenceField() {
|
|
104
133
|
return this.GetPayloadField("payloadReferenceField", "_reference");
|
|
105
134
|
}
|
|
135
|
+
|
|
136
|
+
/** Returns payload values field from the current Blue graph reader. */
|
|
106
137
|
GetPayloadValuesField() {
|
|
107
138
|
return this.GetPayloadField("payloadValuesField", "_values");
|
|
108
139
|
}
|
|
140
|
+
|
|
141
|
+
/** Returns payload field from the current Blue graph reader. */
|
|
109
142
|
GetPayloadField(name, fallback) {
|
|
110
143
|
const value = this.options[name];
|
|
111
144
|
if (!this.validatePayloadReservedFields) {
|
|
@@ -116,6 +149,11 @@ class CjsBlueReader extends CjsReader {
|
|
|
116
149
|
if (typeof value === "string" && value) return value;
|
|
117
150
|
throw this.PayloadError("PAYLOAD_MARKER_CONFIGURATION", `${name} must be a non-empty string or false`);
|
|
118
151
|
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Validates payload configuration against Blue graph reader constraints and
|
|
155
|
+
* throws on failure.
|
|
156
|
+
*/
|
|
119
157
|
ValidatePayloadConfiguration() {
|
|
120
158
|
if (!this.validatePayloadReservedFields) {
|
|
121
159
|
this.payloadReservedFields = null;
|
|
@@ -142,6 +180,11 @@ class CjsBlueReader extends CjsReader {
|
|
|
142
180
|
}
|
|
143
181
|
this.payloadReservedFields = reserved;
|
|
144
182
|
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Validates payload field available against Blue graph reader constraints
|
|
186
|
+
* and throws on failure.
|
|
187
|
+
*/
|
|
145
188
|
AssertPayloadFieldAvailable(field) {
|
|
146
189
|
if (!this.validatePayloadReservedFields) return;
|
|
147
190
|
if (!this.payloadReservedFields) this.ValidatePayloadConfiguration();
|
|
@@ -149,12 +192,22 @@ class CjsBlueReader extends CjsReader {
|
|
|
149
192
|
if (!role) return;
|
|
150
193
|
throw this.PayloadError("PAYLOAD_RESERVED_FIELD_COLLISION", `payload field "${field}" is reserved for the ${role} marker`);
|
|
151
194
|
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Assigns payload values while preserving the current Blue graph reader
|
|
198
|
+
* contract.
|
|
199
|
+
*/
|
|
152
200
|
AssignPayloadValues(targetObject, values) {
|
|
153
201
|
for (const [field, value] of Object.entries(values)) {
|
|
154
202
|
this.AssertPayloadFieldAvailable(field);
|
|
155
203
|
this.DefinePayloadProperty(targetObject, field, value);
|
|
156
204
|
}
|
|
157
205
|
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Assigns payload reference ID while preserving the current Blue graph
|
|
209
|
+
* reader contract.
|
|
210
|
+
*/
|
|
158
211
|
AssignPayloadReferenceId(targetObject, idField, referenceId) {
|
|
159
212
|
if (!Object.hasOwn(targetObject, idField)) {
|
|
160
213
|
this.DefinePayloadProperty(targetObject, idField, referenceId);
|
|
@@ -164,11 +217,18 @@ class CjsBlueReader extends CjsReader {
|
|
|
164
217
|
if (targetObject[idField] === referenceId) return;
|
|
165
218
|
throw this.PayloadError("PAYLOAD_RESERVED_FIELD_COLLISION", `payload ID field "${idField}" conflicts with reference ${referenceId}`);
|
|
166
219
|
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Creates a payload error annotated with the current graph context for the
|
|
223
|
+
* Blue graph reader.
|
|
224
|
+
*/
|
|
167
225
|
PayloadError(code, message) {
|
|
168
226
|
const error = new TypeError(`${code}: ${message}`);
|
|
169
227
|
error.code = code;
|
|
170
228
|
return error;
|
|
171
229
|
}
|
|
230
|
+
|
|
231
|
+
/** Defines payload property on the current Blue graph reader result. */
|
|
172
232
|
DefinePayloadProperty(targetObject, field, value) {
|
|
173
233
|
Object.defineProperty(targetObject, field, {
|
|
174
234
|
value,
|
|
@@ -177,6 +237,11 @@ class CjsBlueReader extends CjsReader {
|
|
|
177
237
|
writable: true
|
|
178
238
|
});
|
|
179
239
|
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Assigns runtime field value while preserving the current Blue graph reader
|
|
243
|
+
* contract.
|
|
244
|
+
*/
|
|
180
245
|
AssignRuntimeFieldValue(targetObject, target, value) {
|
|
181
246
|
if (target.unknown) {
|
|
182
247
|
targetObject[target.wireName] = value;
|
|
@@ -193,6 +258,11 @@ class CjsBlueReader extends CjsReader {
|
|
|
193
258
|
}
|
|
194
259
|
targetObject[target.field.name] = this.NormalizeRuntimeFieldValue(value, target.field);
|
|
195
260
|
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Assigns payload field value while preserving the current Blue graph reader
|
|
264
|
+
* contract.
|
|
265
|
+
*/
|
|
196
266
|
AssignPayloadFieldValue(targetObject, target, value) {
|
|
197
267
|
this.AssertPayloadFieldAvailable(target.unknown ? target.wireName : target.field.name);
|
|
198
268
|
if (target.unknown) {
|
|
@@ -222,6 +292,11 @@ class CjsBlueReader extends CjsReader {
|
|
|
222
292
|
targetObject[target.field.name] = value;
|
|
223
293
|
}
|
|
224
294
|
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Converts runtime field value into the canonical Blue graph reader
|
|
298
|
+
* representation.
|
|
299
|
+
*/
|
|
225
300
|
NormalizeRuntimeFieldValue(value, field) {
|
|
226
301
|
if (!field) return value;
|
|
227
302
|
|
|
@@ -236,6 +311,8 @@ class CjsBlueReader extends CjsReader {
|
|
|
236
311
|
if (CjsBlueReader.isShapeIncompatibleMathArray(value, descriptor)) return value;
|
|
237
312
|
return normalizeCarbonValue(value, field);
|
|
238
313
|
}
|
|
314
|
+
|
|
315
|
+
/** Resolves source shape against the current Blue graph reader. */
|
|
239
316
|
ResolveSourceShape(kind) {
|
|
240
317
|
const registry = this.options.registry || null;
|
|
241
318
|
if (registry?.GetSourceShape) return registry.GetSourceShape(kind);
|
|
@@ -249,16 +326,28 @@ class CjsBlueReader extends CjsReader {
|
|
|
249
326
|
}
|
|
250
327
|
return this.schemaShapes.get(kind) || null;
|
|
251
328
|
}
|
|
329
|
+
|
|
330
|
+
/** Resolves class against the current Blue graph reader. */
|
|
252
331
|
ResolveClass(kind) {
|
|
253
332
|
if (!kind) return null;
|
|
254
333
|
const classes = this.options.classes || {};
|
|
255
334
|
const Registry = this.options.registry || this.defaultRegistry;
|
|
256
335
|
return classes[kind] || (Registry ? Registry.GetConstructor(kind) : null);
|
|
257
336
|
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Applies the configured resource-path transform to a decoded field value
|
|
340
|
+
* for the Blue graph reader.
|
|
341
|
+
*/
|
|
258
342
|
TransformPath(value) {
|
|
259
343
|
const handler = this.options.pathHandler;
|
|
260
344
|
return typeof handler === "function" ? handler(value) : value;
|
|
261
345
|
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Reports whether the current Blue graph reader satisfies shape incompatible
|
|
349
|
+
* math array.
|
|
350
|
+
*/
|
|
262
351
|
static isShapeIncompatibleMathArray(value, descriptor) {
|
|
263
352
|
const expectedLength = descriptor?.length;
|
|
264
353
|
return Array.isArray(value) && Number.isInteger(expectedLength) && value.length !== expectedLength;
|