@ahoo-wang/wow-client 9.9.0 → 10.0.0-alpha.4
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/README.md +85 -230
- package/README.zh-CN.md +74 -209
- package/dist/commands.cjs +2 -0
- package/dist/commands.cjs.map +1 -0
- package/dist/commands.d.cts +123 -0
- package/dist/commands.d.ts +123 -0
- package/dist/commands.es.js +96 -0
- package/dist/commands.es.js.map +1 -0
- package/dist/dsl/aggregation/types.cjs.map +1 -1
- package/dist/dsl/aggregation/types.d.cts +1 -1
- package/dist/dsl/aggregation/types.d.ts +1 -1
- package/dist/dsl/aggregation/types.es.js.map +1 -1
- package/dist/dsl/cursorQuery.cjs.map +1 -1
- package/dist/dsl/cursorQuery.d.cts +7 -8
- package/dist/dsl/cursorQuery.d.ts +7 -8
- package/dist/dsl/cursorQuery.es.js.map +1 -1
- package/dist/dsl/descriptor.cjs.map +1 -1
- package/dist/dsl/descriptor.d.cts +8 -1
- package/dist/dsl/descriptor.d.ts +8 -1
- package/dist/dsl/descriptor.es.js.map +1 -1
- package/dist/dsl/documents.d.cts +1 -1
- package/dist/dsl/documents.d.ts +1 -1
- package/dist/dsl/filter/types.d.cts +2 -1
- package/dist/dsl/filter/types.d.ts +2 -1
- package/dist/dsl/projection.cjs.map +1 -1
- package/dist/dsl/projection.d.cts +22 -0
- package/dist/dsl/projection.d.ts +22 -0
- package/dist/dsl/projection.es.js.map +1 -1
- package/dist/dsl/queryErrorCodes.cjs +2 -0
- package/dist/dsl/queryErrorCodes.cjs.map +1 -0
- package/dist/{error → dsl}/queryErrorCodes.d.cts +25 -24
- package/dist/{error → dsl}/queryErrorCodes.d.ts +25 -24
- package/dist/{error → dsl}/queryErrorCodes.es.js +2 -1
- package/dist/{error/queryErrorCodes.cjs.map → dsl/queryErrorCodes.es.js.map} +1 -1
- package/dist/dsl.cjs +1 -1
- package/dist/dsl.d.cts +1 -2
- package/dist/dsl.d.ts +1 -2
- package/dist/dsl.es.js +2 -3
- package/dist/exactJson.cjs +3 -0
- package/dist/exactJson.cjs.map +1 -0
- package/dist/exactJson.d.cts +54 -0
- package/dist/exactJson.d.ts +54 -0
- package/dist/exactJson.es.js +213 -0
- package/dist/exactJson.es.js.map +1 -0
- package/dist/http.cjs +2 -0
- package/dist/http.cjs.map +1 -0
- package/dist/http.d.cts +96 -0
- package/dist/http.d.ts +96 -0
- package/dist/http.es.js +77 -0
- package/dist/http.es.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +19 -17
- package/dist/index.d.ts +19 -17
- package/dist/index.es.js +10 -36
- package/dist/manifest.cjs +2 -0
- package/dist/manifest.cjs.map +1 -0
- package/dist/manifest.d.cts +109 -0
- package/dist/manifest.d.ts +109 -0
- package/dist/manifest.es.js +14 -0
- package/dist/manifest.es.js.map +1 -0
- package/dist/problem.cjs +2 -0
- package/dist/problem.cjs.map +1 -0
- package/dist/problem.d.cts +211 -0
- package/dist/problem.d.ts +211 -0
- package/dist/problem.es.js +138 -0
- package/dist/problem.es.js.map +1 -0
- package/dist/queries.cjs +2 -0
- package/dist/queries.cjs.map +1 -0
- package/dist/queries.d.cts +182 -0
- package/dist/queries.d.ts +182 -0
- package/dist/queries.es.js +112 -0
- package/dist/queries.es.js.map +1 -0
- package/dist/state.cjs +2 -0
- package/dist/state.cjs.map +1 -0
- package/dist/state.d.cts +75 -0
- package/dist/state.d.ts +75 -0
- package/dist/state.es.js +70 -0
- package/dist/state.es.js.map +1 -0
- package/dist/streams.cjs +2 -0
- package/dist/streams.cjs.map +1 -0
- package/dist/streams.d.cts +74 -0
- package/dist/streams.d.ts +74 -0
- package/dist/streams.es.js +103 -0
- package/dist/streams.es.js.map +1 -0
- package/dist/wait.cjs +2 -0
- package/dist/wait.cjs.map +1 -0
- package/dist/wait.d.cts +61 -0
- package/dist/wait.d.ts +61 -0
- package/dist/wait.es.js +44 -0
- package/dist/wait.es.js.map +1 -0
- package/dist/wire.cjs +2 -0
- package/dist/wire.cjs.map +1 -0
- package/dist/wire.d.cts +326 -0
- package/dist/wire.d.ts +326 -0
- package/dist/wire.es.js +10 -0
- package/dist/wire.es.js.map +1 -0
- package/package.json +4 -16
- package/dist/_virtual/_@oxc-project_runtime@0.153.0/helpers/esm/decorate.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.153.0/helpers/esm/decorate.es.js +0 -9
- package/dist/_virtual/_@oxc-project_runtime@0.153.0/helpers/esm/decorateMetadata.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.153.0/helpers/esm/decorateMetadata.es.js +0 -6
- package/dist/_virtual/_@oxc-project_runtime@0.153.0/helpers/esm/decorateParam.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.153.0/helpers/esm/decorateParam.es.js +0 -8
- package/dist/client/bindMethods.cjs +0 -2
- package/dist/client/bindMethods.cjs.map +0 -1
- package/dist/client/bindMethods.d.cts +0 -13
- package/dist/client/bindMethods.d.ts +0 -13
- package/dist/client/bindMethods.es.js +0 -19
- package/dist/client/bindMethods.es.js.map +0 -1
- package/dist/client/command/commandClient.cjs +0 -2
- package/dist/client/command/commandClient.cjs.map +0 -1
- package/dist/client/command/commandClient.d.cts +0 -67
- package/dist/client/command/commandClient.d.ts +0 -67
- package/dist/client/command/commandClient.es.js +0 -38
- package/dist/client/command/commandClient.es.js.map +0 -1
- package/dist/client/command/commandHeaders.cjs +0 -2
- package/dist/client/command/commandHeaders.cjs.map +0 -1
- package/dist/client/command/commandHeaders.d.cts +0 -75
- package/dist/client/command/commandHeaders.d.ts +0 -75
- package/dist/client/command/commandHeaders.es.js +0 -31
- package/dist/client/command/commandHeaders.es.js.map +0 -1
- package/dist/client/command/commandRequest.cjs +0 -2
- package/dist/client/command/commandRequest.cjs.map +0 -1
- package/dist/client/command/commandRequest.d.cts +0 -180
- package/dist/client/command/commandRequest.d.ts +0 -180
- package/dist/client/command/commandRequest.es.js +0 -31
- package/dist/client/command/commandRequest.es.js.map +0 -1
- package/dist/client/command/commandResult.d.cts +0 -15
- package/dist/client/command/commandResult.d.ts +0 -15
- package/dist/client/command/index.d.cts +0 -5
- package/dist/client/command/index.d.ts +0 -5
- package/dist/client/command/types.d.cts +0 -71
- package/dist/client/command/types.d.ts +0 -71
- package/dist/client/metadata/index.d.cts +0 -2
- package/dist/client/metadata/index.d.ts +0 -2
- package/dist/client/metadata/wowMetadata.d.cts +0 -48
- package/dist/client/metadata/wowMetadata.d.ts +0 -48
- package/dist/client/metadata/wowMetadataClient.cjs +0 -2
- package/dist/client/metadata/wowMetadataClient.cjs.map +0 -1
- package/dist/client/metadata/wowMetadataClient.d.cts +0 -23
- package/dist/client/metadata/wowMetadataClient.d.ts +0 -23
- package/dist/client/metadata/wowMetadataClient.es.js +0 -25
- package/dist/client/metadata/wowMetadataClient.es.js.map +0 -1
- package/dist/client/query/dataset/datasetDescriptorEndpoint.cjs +0 -2
- package/dist/client/query/dataset/datasetDescriptorEndpoint.cjs.map +0 -1
- package/dist/client/query/dataset/datasetDescriptorEndpoint.d.cts +0 -8
- package/dist/client/query/dataset/datasetDescriptorEndpoint.d.ts +0 -8
- package/dist/client/query/dataset/datasetDescriptorEndpoint.es.js +0 -34
- package/dist/client/query/dataset/datasetDescriptorEndpoint.es.js.map +0 -1
- package/dist/client/query/dataset/datasetDirectoryClient.cjs +0 -2
- package/dist/client/query/dataset/datasetDirectoryClient.cjs.map +0 -1
- package/dist/client/query/dataset/datasetDirectoryClient.d.cts +0 -29
- package/dist/client/query/dataset/datasetDirectoryClient.d.ts +0 -29
- package/dist/client/query/dataset/datasetDirectoryClient.es.js +0 -25
- package/dist/client/query/dataset/datasetDirectoryClient.es.js.map +0 -1
- package/dist/client/query/dataset/datasetQueryApi.d.cts +0 -40
- package/dist/client/query/dataset/datasetQueryApi.d.ts +0 -40
- package/dist/client/query/dataset/datasetQueryClient.cjs +0 -2
- package/dist/client/query/dataset/datasetQueryClient.cjs.map +0 -1
- package/dist/client/query/dataset/datasetQueryClient.d.cts +0 -57
- package/dist/client/query/dataset/datasetQueryClient.d.ts +0 -57
- package/dist/client/query/dataset/datasetQueryClient.es.js +0 -154
- package/dist/client/query/dataset/datasetQueryClient.es.js.map +0 -1
- package/dist/client/query/dataset/endpointPaths.cjs +0 -2
- package/dist/client/query/dataset/endpointPaths.cjs.map +0 -1
- package/dist/client/query/dataset/endpointPaths.d.cts +0 -9
- package/dist/client/query/dataset/endpointPaths.d.ts +0 -9
- package/dist/client/query/dataset/endpointPaths.es.js +0 -14
- package/dist/client/query/dataset/endpointPaths.es.js.map +0 -1
- package/dist/client/query/dataset/index.d.cts +0 -3
- package/dist/client/query/dataset/index.d.ts +0 -3
- package/dist/client/query/descriptor/descriptorEndpoints.cjs +0 -2
- package/dist/client/query/descriptor/descriptorEndpoints.cjs.map +0 -1
- package/dist/client/query/descriptor/descriptorEndpoints.d.cts +0 -29
- package/dist/client/query/descriptor/descriptorEndpoints.d.ts +0 -29
- package/dist/client/query/descriptor/descriptorEndpoints.es.js +0 -69
- package/dist/client/query/descriptor/descriptorEndpoints.es.js.map +0 -1
- package/dist/client/query/descriptor/endpointPaths.cjs +0 -2
- package/dist/client/query/descriptor/endpointPaths.cjs.map +0 -1
- package/dist/client/query/descriptor/endpointPaths.d.cts +0 -4
- package/dist/client/query/descriptor/endpointPaths.d.ts +0 -4
- package/dist/client/query/descriptor/endpointPaths.es.js +0 -9
- package/dist/client/query/descriptor/endpointPaths.es.js.map +0 -1
- package/dist/client/query/descriptor/index.d.cts +0 -2
- package/dist/client/query/descriptor/index.d.ts +0 -2
- package/dist/client/query/descriptor/queryDescriptorApi.d.cts +0 -58
- package/dist/client/query/descriptor/queryDescriptorApi.d.ts +0 -58
- package/dist/client/query/descriptor/queryDescriptorClient.cjs +0 -2
- package/dist/client/query/descriptor/queryDescriptorClient.cjs.map +0 -1
- package/dist/client/query/descriptor/queryDescriptorClient.d.cts +0 -61
- package/dist/client/query/descriptor/queryDescriptorClient.d.ts +0 -61
- package/dist/client/query/descriptor/queryDescriptorClient.es.js +0 -20
- package/dist/client/query/descriptor/queryDescriptorClient.es.js.map +0 -1
- package/dist/client/query/event/endpointPaths.cjs +0 -2
- package/dist/client/query/event/endpointPaths.cjs.map +0 -1
- package/dist/client/query/event/endpointPaths.d.cts +0 -8
- package/dist/client/query/event/endpointPaths.d.ts +0 -8
- package/dist/client/query/event/endpointPaths.es.js +0 -13
- package/dist/client/query/event/endpointPaths.es.js.map +0 -1
- package/dist/client/query/event/eventStreamQueryApi.d.cts +0 -29
- package/dist/client/query/event/eventStreamQueryApi.d.ts +0 -29
- package/dist/client/query/event/eventStreamQueryClient.cjs +0 -2
- package/dist/client/query/event/eventStreamQueryClient.cjs.map +0 -1
- package/dist/client/query/event/eventStreamQueryClient.d.cts +0 -193
- package/dist/client/query/event/eventStreamQueryClient.d.ts +0 -193
- package/dist/client/query/event/eventStreamQueryClient.es.js +0 -160
- package/dist/client/query/event/eventStreamQueryClient.es.js.map +0 -1
- package/dist/client/query/event/index.d.cts +0 -2
- package/dist/client/query/event/index.d.ts +0 -2
- package/dist/client/query/factory.cjs +0 -2
- package/dist/client/query/factory.cjs.map +0 -1
- package/dist/client/query/factory.d.cts +0 -165
- package/dist/client/query/factory.d.ts +0 -165
- package/dist/client/query/factory.es.js +0 -45
- package/dist/client/query/factory.es.js.map +0 -1
- package/dist/client/query/index.d.cts +0 -7
- package/dist/client/query/index.d.ts +0 -7
- package/dist/client/query/queryApi.d.cts +0 -68
- package/dist/client/query/queryApi.d.ts +0 -68
- package/dist/client/query/requests.d.cts +0 -24
- package/dist/client/query/requests.d.ts +0 -24
- package/dist/client/query/snapshot/endpointPaths.cjs +0 -2
- package/dist/client/query/snapshot/endpointPaths.cjs.map +0 -1
- package/dist/client/query/snapshot/endpointPaths.d.cts +0 -12
- package/dist/client/query/snapshot/endpointPaths.d.ts +0 -12
- package/dist/client/query/snapshot/endpointPaths.es.js +0 -17
- package/dist/client/query/snapshot/endpointPaths.es.js.map +0 -1
- package/dist/client/query/snapshot/index.d.cts +0 -2
- package/dist/client/query/snapshot/index.d.ts +0 -2
- package/dist/client/query/snapshot/snapshotQueryApi.d.cts +0 -101
- package/dist/client/query/snapshot/snapshotQueryApi.d.ts +0 -101
- package/dist/client/query/snapshot/snapshotQueryClient.cjs +0 -2
- package/dist/client/query/snapshot/snapshotQueryClient.cjs.map +0 -1
- package/dist/client/query/snapshot/snapshotQueryClient.d.cts +0 -372
- package/dist/client/query/snapshot/snapshotQueryClient.d.ts +0 -372
- package/dist/client/query/snapshot/snapshotQueryClient.es.js +0 -239
- package/dist/client/query/snapshot/snapshotQueryClient.es.js.map +0 -1
- package/dist/client/query/state/endpointPaths.cjs +0 -2
- package/dist/client/query/state/endpointPaths.cjs.map +0 -1
- package/dist/client/query/state/endpointPaths.d.cts +0 -10
- package/dist/client/query/state/endpointPaths.d.ts +0 -10
- package/dist/client/query/state/endpointPaths.es.js +0 -14
- package/dist/client/query/state/endpointPaths.es.js.map +0 -1
- package/dist/client/query/state/index.d.cts +0 -4
- package/dist/client/query/state/index.d.ts +0 -4
- package/dist/client/query/state/loadOwnerStateAggregateApi.d.cts +0 -36
- package/dist/client/query/state/loadOwnerStateAggregateApi.d.ts +0 -36
- package/dist/client/query/state/loadOwnerStateAggregateClient.cjs +0 -2
- package/dist/client/query/state/loadOwnerStateAggregateClient.cjs.map +0 -1
- package/dist/client/query/state/loadOwnerStateAggregateClient.d.cts +0 -58
- package/dist/client/query/state/loadOwnerStateAggregateClient.d.ts +0 -58
- package/dist/client/query/state/loadOwnerStateAggregateClient.es.js +0 -56
- package/dist/client/query/state/loadOwnerStateAggregateClient.es.js.map +0 -1
- package/dist/client/query/state/loadStateAggregateApi.d.cts +0 -37
- package/dist/client/query/state/loadStateAggregateApi.d.ts +0 -37
- package/dist/client/query/state/loadStateAggregateClient.cjs +0 -2
- package/dist/client/query/state/loadStateAggregateClient.cjs.map +0 -1
- package/dist/client/query/state/loadStateAggregateClient.d.cts +0 -62
- package/dist/client/query/state/loadStateAggregateClient.d.ts +0 -62
- package/dist/client/query/state/loadStateAggregateClient.es.js +0 -65
- package/dist/client/query/state/loadStateAggregateClient.es.js.map +0 -1
- package/dist/client/routing.cjs +0 -2
- package/dist/client/routing.cjs.map +0 -1
- package/dist/client/routing.d.cts +0 -47
- package/dist/client/routing.d.ts +0 -47
- package/dist/client/routing.es.js +0 -8
- package/dist/client/routing.es.js.map +0 -1
- package/dist/error/errorInfo.cjs +0 -2
- package/dist/error/errorInfo.cjs.map +0 -1
- package/dist/error/errorInfo.d.cts +0 -139
- package/dist/error/errorInfo.d.ts +0 -139
- package/dist/error/errorInfo.es.js +0 -32
- package/dist/error/errorInfo.es.js.map +0 -1
- package/dist/error/headers.cjs +0 -2
- package/dist/error/headers.cjs.map +0 -1
- package/dist/error/headers.d.cts +0 -39
- package/dist/error/headers.d.ts +0 -39
- package/dist/error/headers.es.js +0 -10
- package/dist/error/headers.es.js.map +0 -1
- package/dist/error/index.d.cts +0 -21
- package/dist/error/index.d.ts +0 -21
- package/dist/error/queryErrorCodes.cjs +0 -2
- package/dist/error/queryErrorCodes.es.js.map +0 -1
- package/dist/error/wowError.cjs +0 -2
- package/dist/error/wowError.cjs.map +0 -1
- package/dist/error/wowError.d.cts +0 -99
- package/dist/error/wowError.d.ts +0 -99
- package/dist/error/wowError.es.js +0 -53
- package/dist/error/wowError.es.js.map +0 -1
- package/dist/legacy/condition.cjs +0 -2
- package/dist/legacy/condition.cjs.map +0 -1
- package/dist/legacy/condition.d.cts +0 -517
- package/dist/legacy/condition.d.ts +0 -517
- package/dist/legacy/condition.es.js +0 -338
- package/dist/legacy/condition.es.js.map +0 -1
- package/dist/legacy/index.d.cts +0 -16
- package/dist/legacy/index.d.ts +0 -16
- package/dist/legacy/locale/en_US.cjs +0 -2
- package/dist/legacy/locale/en_US.cjs.map +0 -1
- package/dist/legacy/locale/en_US.d.cts +0 -7
- package/dist/legacy/locale/en_US.d.ts +0 -7
- package/dist/legacy/locale/en_US.es.js +0 -50
- package/dist/legacy/locale/en_US.es.js.map +0 -1
- package/dist/legacy/locale/operatorLocale.d.cts +0 -10
- package/dist/legacy/locale/operatorLocale.d.ts +0 -10
- package/dist/legacy/locale/zh_CN.cjs +0 -2
- package/dist/legacy/locale/zh_CN.cjs.map +0 -1
- package/dist/legacy/locale/zh_CN.d.cts +0 -7
- package/dist/legacy/locale/zh_CN.d.ts +0 -7
- package/dist/legacy/locale/zh_CN.es.js +0 -50
- package/dist/legacy/locale/zh_CN.es.js.map +0 -1
- package/dist/legacy/operator.cjs +0 -2
- package/dist/legacy/operator.cjs.map +0 -1
- package/dist/legacy/operator.d.cts +0 -218
- package/dist/legacy/operator.d.ts +0 -218
- package/dist/legacy/operator.es.js +0 -25
- package/dist/legacy/operator.es.js.map +0 -1
- package/dist/legacy/queryable.cjs +0 -2
- package/dist/legacy/queryable.cjs.map +0 -1
- package/dist/legacy/queryable.d.cts +0 -61
- package/dist/legacy/queryable.d.ts +0 -61
- package/dist/legacy/queryable.es.js +0 -34
- package/dist/legacy/queryable.es.js.map +0 -1
- package/dist/legacy.cjs +0 -1
- package/dist/legacy.es.js +0 -6
- package/dist/model/abac.cjs +0 -2
- package/dist/model/abac.cjs.map +0 -1
- package/dist/model/abac.d.cts +0 -33
- package/dist/model/abac.d.ts +0 -33
- package/dist/model/abac.es.js +0 -6
- package/dist/model/abac.es.js.map +0 -1
- package/dist/model/bi.cjs +0 -2
- package/dist/model/bi.cjs.map +0 -1
- package/dist/model/bi.d.cts +0 -12
- package/dist/model/bi.d.ts +0 -12
- package/dist/model/bi.es.js +0 -8
- package/dist/model/bi.es.js.map +0 -1
- package/dist/model/command.cjs +0 -2
- package/dist/model/command.cjs.map +0 -1
- package/dist/model/command.d.cts +0 -137
- package/dist/model/command.d.ts +0 -137
- package/dist/model/command.es.js +0 -8
- package/dist/model/command.es.js.map +0 -1
- package/dist/model/common.d.cts +0 -19
- package/dist/model/common.d.ts +0 -19
- package/dist/model/domainEventStream.cjs +0 -2
- package/dist/model/domainEventStream.cjs.map +0 -1
- package/dist/model/domainEventStream.d.cts +0 -109
- package/dist/model/domainEventStream.d.ts +0 -109
- package/dist/model/domainEventStream.es.js +0 -23
- package/dist/model/domainEventStream.es.js.map +0 -1
- package/dist/model/function.cjs +0 -2
- package/dist/model/function.cjs.map +0 -1
- package/dist/model/function.d.cts +0 -41
- package/dist/model/function.d.ts +0 -41
- package/dist/model/function.es.js +0 -8
- package/dist/model/function.es.js.map +0 -1
- package/dist/model/index.d.cts +0 -24
- package/dist/model/index.d.ts +0 -24
- package/dist/model/messaging.d.cts +0 -5
- package/dist/model/messaging.d.ts +0 -5
- package/dist/model/modeling.d.cts +0 -145
- package/dist/model/modeling.d.ts +0 -145
- package/dist/model/naming.d.cts +0 -27
- package/dist/model/naming.d.ts +0 -27
- package/dist/model/snapshot.cjs +0 -2
- package/dist/model/snapshot.cjs.map +0 -1
- package/dist/model/snapshot.d.cts +0 -53
- package/dist/model/snapshot.d.ts +0 -53
- package/dist/model/snapshot.es.js +0 -20
- package/dist/model/snapshot.es.js.map +0 -1
- package/dist/transport/endpoints.cjs +0 -2
- package/dist/transport/endpoints.cjs.map +0 -1
- package/dist/transport/endpoints.d.cts +0 -79
- package/dist/transport/endpoints.d.ts +0 -79
- package/dist/transport/endpoints.es.js +0 -14
- package/dist/transport/endpoints.es.js.map +0 -1
- package/dist/transport/eventStreams.cjs +0 -2
- package/dist/transport/eventStreams.cjs.map +0 -1
- package/dist/transport/eventStreams.d.cts +0 -25
- package/dist/transport/eventStreams.d.ts +0 -25
- package/dist/transport/eventStreams.es.js +0 -21
- package/dist/transport/eventStreams.es.js.map +0 -1
- package/dist/transport/index.d.cts +0 -1
- package/dist/transport/index.d.ts +0 -1
package/dist/state.d.cts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { RequestOptions, WowClientOptions } from './http.cjs';
|
|
2
|
+
import { AggregateType, EventView, StateTrace, StateView } from './wire.cjs';
|
|
3
|
+
/** Which state a read asks for: the latest, at a version or as of a time. */
|
|
4
|
+
export interface StateReadOptions extends RequestOptions {
|
|
5
|
+
/** The committed state at this version. */
|
|
6
|
+
readonly version?: number;
|
|
7
|
+
/** The committed state as of this instant (ISO-8601, or a `Date`). */
|
|
8
|
+
readonly at?: string | Date;
|
|
9
|
+
/**
|
|
10
|
+
* The latest state at this version or later (review R4-Q, Q-13: read your
|
|
11
|
+
* writes), such as the `version` of a command's result: a lagging snapshot is
|
|
12
|
+
* caught up with the log. Not with `version` or `at`. One not reached within
|
|
13
|
+
* the server's wait rejects with 409 `wow.query.min-version-not-reached`,
|
|
14
|
+
* whose `version` is the current one.
|
|
15
|
+
*/
|
|
16
|
+
readonly minVersion?: number;
|
|
17
|
+
/**
|
|
18
|
+
* The entity tag of the state already held, as a read answered it
|
|
19
|
+
* (`etag`): a 304 then answers `notModified`. The tag is the version and a
|
|
20
|
+
* digest of the body (`3-<digest>`), so it changes whenever what is read
|
|
21
|
+
* does, not only with the version.
|
|
22
|
+
*/
|
|
23
|
+
readonly ifNoneMatch?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A state read: the view and its entity tag, or `notModified` when the state
|
|
27
|
+
* held is current, with its tag and the version the tag names.
|
|
28
|
+
*/
|
|
29
|
+
export type StateReadResult<S> = {
|
|
30
|
+
readonly notModified: false;
|
|
31
|
+
readonly view: StateView<S>;
|
|
32
|
+
readonly etag?: string;
|
|
33
|
+
} | {
|
|
34
|
+
readonly notModified: true;
|
|
35
|
+
readonly version: number;
|
|
36
|
+
readonly etag: string;
|
|
37
|
+
};
|
|
38
|
+
/** Which commits a trace reads (decision D40). */
|
|
39
|
+
export interface StateTraceOptions extends RequestOptions {
|
|
40
|
+
/** The first version; 1 when absent. */
|
|
41
|
+
readonly from?: number;
|
|
42
|
+
/** The last version; the latest when absent. */
|
|
43
|
+
readonly to?: number;
|
|
44
|
+
/** The most commits, at most 1000; 100 when absent. */
|
|
45
|
+
readonly limit?: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Reads an aggregate type's states (decisions D40, D112):
|
|
49
|
+
* `GET /{context}/{aggregate}/{id}/state` with `?version=`, `?at=` or `?minVersion=`, and
|
|
50
|
+
* its trace, `…/state/trace`. A state that does not exist rejects with 404
|
|
51
|
+
* `wow.aggregate.not-found`.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam S - The state's type.
|
|
54
|
+
* @typeParam E - The events' type of a trace.
|
|
55
|
+
*/
|
|
56
|
+
export declare class StateClient<S = unknown, E extends EventView = EventView> {
|
|
57
|
+
readonly type: AggregateType;
|
|
58
|
+
readonly client: WowClientOptions;
|
|
59
|
+
/**
|
|
60
|
+
* @param type - The aggregate type
|
|
61
|
+
* @param client - How the service is reached
|
|
62
|
+
*/
|
|
63
|
+
constructor(type: AggregateType, client?: WowClientOptions);
|
|
64
|
+
/** The state the options ask for. */
|
|
65
|
+
read(id: string, options?: StateReadOptions): Promise<StateReadResult<S>>;
|
|
66
|
+
/** The latest state, or the one the options ask for, without a cache. */
|
|
67
|
+
get(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<StateView<S>>;
|
|
68
|
+
/** The commits from `from` to `to`, each with its events and the state after it. */
|
|
69
|
+
trace(id: string, options?: StateTraceOptions): Promise<StateTrace<S, E>>;
|
|
70
|
+
/** The exact numbers of a state view: its state's. */
|
|
71
|
+
private get stateNumbers();
|
|
72
|
+
/** The exact numbers of a trace: each commit's state and events. */
|
|
73
|
+
private get traceNumbers();
|
|
74
|
+
private statePath;
|
|
75
|
+
}
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { RequestOptions, WowClientOptions } from './http.js';
|
|
2
|
+
import { AggregateType, EventView, StateTrace, StateView } from './wire.js';
|
|
3
|
+
/** Which state a read asks for: the latest, at a version or as of a time. */
|
|
4
|
+
export interface StateReadOptions extends RequestOptions {
|
|
5
|
+
/** The committed state at this version. */
|
|
6
|
+
readonly version?: number;
|
|
7
|
+
/** The committed state as of this instant (ISO-8601, or a `Date`). */
|
|
8
|
+
readonly at?: string | Date;
|
|
9
|
+
/**
|
|
10
|
+
* The latest state at this version or later (review R4-Q, Q-13: read your
|
|
11
|
+
* writes), such as the `version` of a command's result: a lagging snapshot is
|
|
12
|
+
* caught up with the log. Not with `version` or `at`. One not reached within
|
|
13
|
+
* the server's wait rejects with 409 `wow.query.min-version-not-reached`,
|
|
14
|
+
* whose `version` is the current one.
|
|
15
|
+
*/
|
|
16
|
+
readonly minVersion?: number;
|
|
17
|
+
/**
|
|
18
|
+
* The entity tag of the state already held, as a read answered it
|
|
19
|
+
* (`etag`): a 304 then answers `notModified`. The tag is the version and a
|
|
20
|
+
* digest of the body (`3-<digest>`), so it changes whenever what is read
|
|
21
|
+
* does, not only with the version.
|
|
22
|
+
*/
|
|
23
|
+
readonly ifNoneMatch?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A state read: the view and its entity tag, or `notModified` when the state
|
|
27
|
+
* held is current, with its tag and the version the tag names.
|
|
28
|
+
*/
|
|
29
|
+
export type StateReadResult<S> = {
|
|
30
|
+
readonly notModified: false;
|
|
31
|
+
readonly view: StateView<S>;
|
|
32
|
+
readonly etag?: string;
|
|
33
|
+
} | {
|
|
34
|
+
readonly notModified: true;
|
|
35
|
+
readonly version: number;
|
|
36
|
+
readonly etag: string;
|
|
37
|
+
};
|
|
38
|
+
/** Which commits a trace reads (decision D40). */
|
|
39
|
+
export interface StateTraceOptions extends RequestOptions {
|
|
40
|
+
/** The first version; 1 when absent. */
|
|
41
|
+
readonly from?: number;
|
|
42
|
+
/** The last version; the latest when absent. */
|
|
43
|
+
readonly to?: number;
|
|
44
|
+
/** The most commits, at most 1000; 100 when absent. */
|
|
45
|
+
readonly limit?: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Reads an aggregate type's states (decisions D40, D112):
|
|
49
|
+
* `GET /{context}/{aggregate}/{id}/state` with `?version=`, `?at=` or `?minVersion=`, and
|
|
50
|
+
* its trace, `…/state/trace`. A state that does not exist rejects with 404
|
|
51
|
+
* `wow.aggregate.not-found`.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam S - The state's type.
|
|
54
|
+
* @typeParam E - The events' type of a trace.
|
|
55
|
+
*/
|
|
56
|
+
export declare class StateClient<S = unknown, E extends EventView = EventView> {
|
|
57
|
+
readonly type: AggregateType;
|
|
58
|
+
readonly client: WowClientOptions;
|
|
59
|
+
/**
|
|
60
|
+
* @param type - The aggregate type
|
|
61
|
+
* @param client - How the service is reached
|
|
62
|
+
*/
|
|
63
|
+
constructor(type: AggregateType, client?: WowClientOptions);
|
|
64
|
+
/** The state the options ask for. */
|
|
65
|
+
read(id: string, options?: StateReadOptions): Promise<StateReadResult<S>>;
|
|
66
|
+
/** The latest state, or the one the options ask for, without a cache. */
|
|
67
|
+
get(id: string, options?: Omit<StateReadOptions, 'ifNoneMatch'>): Promise<StateView<S>>;
|
|
68
|
+
/** The commits from `from` to `to`, each with its events and the state after it. */
|
|
69
|
+
trace(id: string, options?: StateTraceOptions): Promise<StateTrace<S, E>>;
|
|
70
|
+
/** The exact numbers of a state view: its state's. */
|
|
71
|
+
private get stateNumbers();
|
|
72
|
+
/** The exact numbers of a trace: each commit's state and events. */
|
|
73
|
+
private get traceNumbers();
|
|
74
|
+
private statePath;
|
|
75
|
+
}
|
package/dist/state.es.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { eventNumbers as e } from "./exactJson.es.js";
|
|
2
|
+
import { WowHeaders as t, entityTag as n, json as r, send as i } from "./http.es.js";
|
|
3
|
+
//#region src/state.ts
|
|
4
|
+
var a = class {
|
|
5
|
+
constructor(e, t = {}) {
|
|
6
|
+
this.type = e, this.client = t;
|
|
7
|
+
}
|
|
8
|
+
async read(e, a = {}) {
|
|
9
|
+
let o = a.at instanceof Date ? a.at.toISOString() : a.at, s = await i(this.client, {
|
|
10
|
+
method: "GET",
|
|
11
|
+
path: this.statePath(e),
|
|
12
|
+
query: {
|
|
13
|
+
version: a.version,
|
|
14
|
+
at: o,
|
|
15
|
+
minVersion: a.minVersion
|
|
16
|
+
},
|
|
17
|
+
headers: { [t.IF_NONE_MATCH]: a.ifNoneMatch === void 0 ? void 0 : `"${a.ifNoneMatch}"` },
|
|
18
|
+
options: a,
|
|
19
|
+
notModified: !0
|
|
20
|
+
});
|
|
21
|
+
if (s.status === 304) {
|
|
22
|
+
let e = n(s) ?? a.ifNoneMatch ?? "";
|
|
23
|
+
return {
|
|
24
|
+
notModified: !0,
|
|
25
|
+
version: Number.parseInt(e, 10),
|
|
26
|
+
etag: e
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
notModified: !1,
|
|
31
|
+
view: await r(s, this.stateNumbers),
|
|
32
|
+
etag: n(s)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
async get(e, t = {}) {
|
|
36
|
+
let n = await this.read(e, t);
|
|
37
|
+
if (n.notModified) throw Error("A read without If-None-Match was answered 304.");
|
|
38
|
+
return n.view;
|
|
39
|
+
}
|
|
40
|
+
async trace(e, t = {}) {
|
|
41
|
+
return r(await i(this.client, {
|
|
42
|
+
method: "GET",
|
|
43
|
+
path: `${this.statePath(e)}/trace`,
|
|
44
|
+
query: {
|
|
45
|
+
from: t.from,
|
|
46
|
+
to: t.to,
|
|
47
|
+
limit: t.limit
|
|
48
|
+
},
|
|
49
|
+
options: t
|
|
50
|
+
}), this.traceNumbers);
|
|
51
|
+
}
|
|
52
|
+
get stateNumbers() {
|
|
53
|
+
let e = this.type.textNumbers?.state;
|
|
54
|
+
return e === void 0 ? void 0 : { state: e };
|
|
55
|
+
}
|
|
56
|
+
get traceNumbers() {
|
|
57
|
+
let t = this.type.textNumbers?.state, n = e(this.type);
|
|
58
|
+
if (t !== void 0 || n !== void 0) return { commits: { "*": {
|
|
59
|
+
...t === void 0 ? {} : { state: t },
|
|
60
|
+
...n === void 0 ? {} : { events: { "*": n } }
|
|
61
|
+
} } };
|
|
62
|
+
}
|
|
63
|
+
statePath(e) {
|
|
64
|
+
return `/${this.type.context}/${this.type.name}/${encodeURIComponent(e)}/state`;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
//#endregion
|
|
68
|
+
export { a as StateClient };
|
|
69
|
+
|
|
70
|
+
//# sourceMappingURL=state.es.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.es.js","names":["StateClient","type","client","at","options","response","send","id","WowHeaders","etag","entityTag","json","result","state","events","eventNumbers"],"sources":["../src/state.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n entityTag,\n json,\n type RequestOptions,\n send,\n type WowClientOptions,\n WowHeaders,\n} from './http.js';\nimport { eventNumbers, type TextNumbers } from './exactJson.js';\nimport type {\n AggregateType,\n EventView,\n StateTrace,\n StateView,\n} from './wire.js';\n\n/** Which state a read asks for: the latest, at a version or as of a time. */\nexport interface StateReadOptions extends RequestOptions {\n /** The committed state at this version. */\n readonly version?: number;\n /** The committed state as of this instant (ISO-8601, or a `Date`). */\n readonly at?: string | Date;\n /**\n * The latest state at this version or later (review R4-Q, Q-13: read your\n * writes), such as the `version` of a command's result: a lagging snapshot is\n * caught up with the log. Not with `version` or `at`. One not reached within\n * the server's wait rejects with 409 `wow.query.min-version-not-reached`,\n * whose `version` is the current one.\n */\n readonly minVersion?: number;\n /**\n * The entity tag of the state already held, as a read answered it\n * (`etag`): a 304 then answers `notModified`. The tag is the version and a\n * digest of the body (`3-<digest>`), so it changes whenever what is read\n * does, not only with the version.\n */\n readonly ifNoneMatch?: string;\n}\n\n/**\n * A state read: the view and its entity tag, or `notModified` when the state\n * held is current, with its tag and the version the tag names.\n */\nexport type StateReadResult<S> =\n | {\n readonly notModified: false;\n readonly view: StateView<S>;\n readonly etag?: string;\n }\n | {\n readonly notModified: true;\n readonly version: number;\n readonly etag: string;\n };\n\n/** Which commits a trace reads (decision D40). */\nexport interface StateTraceOptions extends RequestOptions {\n /** The first version; 1 when absent. */\n readonly from?: number;\n /** The last version; the latest when absent. */\n readonly to?: number;\n /** The most commits, at most 1000; 100 when absent. */\n readonly limit?: number;\n}\n\n/**\n * Reads an aggregate type's states (decisions D40, D112):\n * `GET /{context}/{aggregate}/{id}/state` with `?version=`, `?at=` or `?minVersion=`, and\n * its trace, `…/state/trace`. A state that does not exist rejects with 404\n * `wow.aggregate.not-found`.\n *\n * @typeParam S - The state's type.\n * @typeParam E - The events' type of a trace.\n */\nexport class StateClient<S = unknown, E extends EventView = EventView> {\n /**\n * @param type - The aggregate type\n * @param client - How the service is reached\n */\n constructor(\n readonly type: AggregateType,\n readonly client: WowClientOptions = {},\n ) {}\n\n /** The state the options ask for. */\n async read(\n id: string,\n options: StateReadOptions = {},\n ): Promise<StateReadResult<S>> {\n const at =\n options.at instanceof Date ? options.at.toISOString() : options.at;\n const response = await send(this.client, {\n method: 'GET',\n path: this.statePath(id),\n query: { version: options.version, at, minVersion: options.minVersion },\n headers: {\n [WowHeaders.IF_NONE_MATCH]:\n options.ifNoneMatch === undefined\n ? undefined\n : `\"${options.ifNoneMatch}\"`,\n },\n options,\n notModified: true,\n });\n if (response.status === 304) {\n const etag = entityTag(response) ?? options.ifNoneMatch ?? '';\n return {\n notModified: true,\n version: Number.parseInt(etag, 10),\n etag,\n };\n }\n return {\n notModified: false,\n view: await json<StateView<S>>(response, this.stateNumbers),\n etag: entityTag(response),\n };\n }\n\n /** The latest state, or the one the options ask for, without a cache. */\n async get(\n id: string,\n options: Omit<StateReadOptions, 'ifNoneMatch'> = {},\n ): Promise<StateView<S>> {\n const result = await this.read(id, options);\n if (result.notModified) {\n throw new Error('A read without If-None-Match was answered 304.');\n }\n return result.view;\n }\n\n /** The commits from `from` to `to`, each with its events and the state after it. */\n async trace(\n id: string,\n options: StateTraceOptions = {},\n ): Promise<StateTrace<S, E>> {\n return json<StateTrace<S, E>>(\n await send(this.client, {\n method: 'GET',\n path: `${this.statePath(id)}/trace`,\n query: { from: options.from, to: options.to, limit: options.limit },\n options,\n }),\n this.traceNumbers,\n );\n }\n\n /** The exact numbers of a state view: its state's. */\n private get stateNumbers(): TextNumbers | undefined {\n const state = this.type.textNumbers?.state;\n return state === undefined ? undefined : { state };\n }\n\n /** The exact numbers of a trace: each commit's state and events. */\n private get traceNumbers(): TextNumbers | undefined {\n const state = this.type.textNumbers?.state;\n const events = eventNumbers(this.type);\n if (state === undefined && events === undefined) return undefined;\n return {\n commits: {\n '*': {\n ...(state === undefined ? {} : { state }),\n ...(events === undefined ? {} : { events: { '*': events } }),\n },\n },\n };\n }\n\n private statePath(id: string): string {\n return `/${this.type.context}/${this.type.name}/${encodeURIComponent(id)}/state`;\n }\n}\n"],"mappings":";;;AAuFA,IAAaA,IAAb,MAAuE;CAKrE,YACE,GACA,IAAoC,CAAC,GACrC;EADS,AADA,KAAA,OAAAC,GACA,KAAA,SAAAC;CACR;CAGH,MAAM,KACJ,GACA,IAA4B,CAAC,GACA;EAC7B,IAAMC,IACJC,EAAQ,cAAc,OAAOA,EAAQ,GAAG,YAAY,IAAIA,EAAQ,IAC5DC,IAAW,MAAMC,EAAK,KAAK,QAAQ;GACvC,QAAQ;GACR,MAAM,KAAK,UAAUC,CAAE;GACvB,OAAO;IAAE,SAASH,EAAQ;IAAS;IAAI,YAAYA,EAAQ;GAAW;GACtE,SAAS,GACNI,EAAW,gBACVJ,EAAQ,gBAAgB,KAAA,IACpB,KAAA,IACA,IAAIA,EAAQ,YAAY,GAChC;GACA;GACA,aAAa;EACf,CAAC;E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,CAAE,EAAE;CAC3E;AACF"}
|
package/dist/streams.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=require("./exactJson.cjs"),t=require("./problem.cjs");let n=require("@ahoo-wang/fetcher-eventstream");var r=`row`,i=`error`,a=`end`,o=`message`;function s(e){return(0,n.toJsonServerSentEventStream)((0,n.toServerSentEventStream)(e)).pipeThrough(new TransformStream({transform(e,t){t.enqueue(e.data)}}))}var c=`hold-expired`,l=`complete`;function u(e,t){return new Promise((n,r)=>{let i=()=>{clearTimeout(a),r(t?.reason)},a=setTimeout(()=>{t?.removeEventListener(`abort`,i),n()},e);t?.addEventListener(`abort`,i)})}function d(e){return new t.WowProblemError(t.isProblem(e)?e:{type:`about:blank`,title:`Query failed`,status:500})}function f(s,f,p={}){let{maxRetries:m=5,initialDelayMs:h=200,maxDelayMs:g=1e4,signal:_}=p,v=p.numbers,y=t=>(e.marksNothing(v)?(0,n.toJsonServerSentEventStream)((0,n.toServerSentEventStream)(t)):(0,n.toServerSentEventStream)(t).pipeThrough(new TransformStream({transform(t,n){let i=t.event===r||t.event===o;n.enqueue({...t,data:e.parseJson(t.data,i?v:void 0)})}}))).getReader(),b=y(s),x=p.lastEventId,S=0,C=0,w=!1;async function T(e,n){for(;;){if(_?.aborted)throw _.reason;if(C++>=m)throw e;let r=n??Math.min(g,h*2**(C-1));r>0&&await u(r,_);try{let e=await f(x,S);return e&&(b=y(e)),e!==void 0}catch(r){if(_?.aborted||r instanceof t.WowProblemError&&!r.retry.retryable)throw r;n=r.retry?.afterMs,e=r}}}return new ReadableStream({async pull(e){for(;;){let n,s;try{let t=await b.read();if(t.done)n=TypeError(`The query stream was cut off.`);else{let{event:u,id:f,data:p}=t.value;if(u===r||u===o){f&&(x=f),S++,C=0,e.enqueue(f?{record:p,position:f}:{record:p});return}if(u===a){let t=p;if(t.reason===l){e.close();return}if(n=TypeError(`The query stream ended: ${t.reason}.`),!t.resumable)throw n;t.reason===c&&(s=0)}else if(u===i){let e=d(p);if(!e.retry.retryable)throw e;n=e,s=e.retry.afterMs}else continue}}catch(e){if(e===n||e instanceof t.WowProblemError)throw e;n=e}if(w)return;if(_?.aborted)throw _.reason;if(x===void 0&&S>0)throw n;if(!await T(n,s)){e.close();return}}},cancel(e){return w=!0,b.cancel(e)}})}exports.commandResults=s,exports.resumableQueryRows=f;
|
|
2
|
+
//# sourceMappingURL=streams.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streams.cjs","names":["toJsonServerSentEventStream","toServerSentEventStream","WowProblemError","isProblem","marksNothing","parseJson","require_exactJson","require_problem","_ahoo_wang_fetcher_eventstream","ROW_EVENT","ERROR_EVENT","END_EVENT","UNNAMED_EVENT","commandResults","response","controller","event","HOLD_EXPIRED","COMPLETE","sleep","abort","timer","reject","signal","resolve","ms","problemOf","data","resumableQueryRows","options","numbers","events","from","row","reader","lastEventId","received","failures","cancelled","resume","maxRetries","cause","wait","delayMs","maxDelayMs","initialDelayMs","next","reopen","error","stop","id","end","reason"],"sources":["../src/streams.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n type JsonServerSentEvent,\n type ServerSentEvent,\n toJsonServerSentEventStream,\n toServerSentEventStream,\n} from '@ahoo-wang/fetcher-eventstream';\nimport { marksNothing, parseJson, type TextNumbers } from './exactJson.js';\nimport type { CommandResult } from './wire.js';\nimport { isProblem, WowProblemError } from './problem.js';\n\n/** The event of each record of a streamed query list. */\nconst ROW_EVENT = 'row';\n\n/** The event that ends a streamed query list that failed: the problem. */\nconst ERROR_EVENT = 'error';\n\n/** The event that ends a resumable streamed query list (decision D253). */\nconst END_EVENT = 'end';\n\n/** The name an event without one is read under. */\nconst UNNAMED_EVENT = 'message';\n\n/**\n * The results of a command's event stream (`Accept: text/event-stream`), one\n * per hop, each event named by its stage. A rejection or a failure after\n * admission is the stream's last result, not an error.\n */\nexport type CommandResultStream<R = Record<string, unknown>> = ReadableStream<\n CommandResult<R>\n>;\n\n/** The data of each event of `response`'s server-sent event stream. */\nexport function commandResults<R>(response: Response): CommandResultStream<R> {\n return toJsonServerSentEventStream<CommandResult<R>>(\n toServerSentEventStream(response),\n ).pipeThrough(\n new TransformStream<\n JsonServerSentEvent<CommandResult<R>>,\n CommandResult<R>\n >({\n transform(event, controller) {\n controller.enqueue(event.data);\n },\n }),\n );\n}\n\n/**\n * Why a resumable streamed list ended (decision D253): `complete` once every\n * record was sent; the others are the server's, and resumable: the stream ran\n * for its hold time, its store had no query permit for its next chunk, or\n * the node is stopping.\n */\nexport type StreamEndReason =\n 'complete' | 'hold-expired' | 'permit-reclaimed' | 'node-stopping';\n\n/** The data of the `end` event that ends a resumable streamed list. */\nexport interface StreamEnd {\n /** Why it ended. */\n readonly reason: StreamEndReason | (string & {});\n /** Whether asking again with `Last-Event-ID` goes on where it ended. */\n readonly resumable: boolean;\n}\n\n/** One record of a streamed list, with its position. */\nexport interface StreamedRow<T> {\n /** The record. */\n readonly record: T;\n /**\n * The record's position, its event's `id`: an opaque cursor token to\n * resume after it ({@link StreamResumeOptions.lastEventId}). Absent from a\n * list whose sort cannot order a cursor, which cannot be resumed.\n */\n readonly position?: string;\n}\n\n/**\n * How a streamed list resumes (decision D253). A stream the server ends with\n * a resumable `end`, one cut off before its `end`, and one that fails with a\n * retryable problem (a 429 after its `Retry-After`) are asked for again with\n * `Last-Event-ID`, the position of the last record read, and go on strictly\n * after it.\n */\nexport interface StreamResumeOptions {\n /** Starts after this position: a {@link StreamedRow.position} read before. */\n readonly lastEventId?: string;\n /**\n * How many times in a row the stream may be resumed without a record in\n * between before it fails with what stopped it; 5 by default, 0 never\n * resumes.\n */\n readonly maxRetries?: number;\n /** The wait before the first such retry, doubled for each next; 200 ms by default. */\n readonly initialDelayMs?: number;\n /** The longest wait between retries; 10 s by default. */\n readonly maxDelayMs?: number;\n}\n\nconst HOLD_EXPIRED: StreamEndReason = 'hold-expired';\nconst COMPLETE: StreamEndReason = 'complete';\n\n/** Waits `ms`, or rejects once `signal` aborts. */\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n const abort = () => {\n clearTimeout(timer);\n reject(signal?.reason);\n };\n const timer = setTimeout(() => {\n signal?.removeEventListener('abort', abort);\n resolve();\n }, ms);\n signal?.addEventListener('abort', abort);\n });\n}\n\nfunction problemOf(data: unknown): WowProblemError {\n return new WowProblemError(\n isProblem(data)\n ? data\n : { type: 'about:blank', title: 'Query failed', status: 500 },\n );\n}\n\n/**\n * The records of a resumable streamed list (decision D253), read from\n * `response` and, each time the stream is to be resumed, from what `reopen`\n * answers for the position of the last record read (none before the first),\n * with how many records were read so far: `undefined` when no more are\n * wanted. Each record comes with its position. An `error` event that is not\n * retryable, a failure `reopen` cannot get past, and an abort of `signal`\n * error the stream.\n *\n * @param response - The first response\n * @param reopen - Asks for the stream again after a position\n * @param options - How the stream resumes, and what cancels it\n */\nexport function resumableQueryRows<T>(\n response: Response,\n reopen: (\n lastEventId: string | undefined,\n received: number,\n ) => Promise<Response | undefined>,\n options: StreamResumeOptions & {\n readonly signal?: AbortSignal;\n /** The numbers of each record read as their exact text (review R4-Q, Q-9). */\n readonly numbers?: TextNumbers;\n } = {},\n): ReadableStream<StreamedRow<T>> {\n const {\n maxRetries = 5,\n initialDelayMs = 200,\n maxDelayMs = 10_000,\n signal,\n } = options;\n const numbers = options.numbers;\n const events = (from: Response) =>\n (marksNothing(numbers)\n ? toJsonServerSentEventStream<unknown>(toServerSentEventStream(from))\n : toServerSentEventStream(from).pipeThrough(\n new TransformStream<ServerSentEvent, JsonServerSentEvent<unknown>>({\n transform(event, controller) {\n const row =\n event.event === ROW_EVENT || event.event === UNNAMED_EVENT;\n controller.enqueue({\n ...event,\n data: parseJson(event.data, row ? numbers : undefined),\n });\n },\n }),\n )\n ).getReader();\n let reader = events(response);\n let lastEventId = options.lastEventId;\n let received = 0;\n let failures = 0;\n // Set once the reader cancels: a read in flight then ends, and must not be\n // taken for a cut-off stream to ask for again.\n let cancelled = false;\n\n /** Asks for the stream again, or throws `cause` once out of retries. */\n async function resume(cause: unknown, delayMs?: number): Promise<boolean> {\n for (;;) {\n if (signal?.aborted) throw signal.reason;\n if (failures++ >= maxRetries) throw cause;\n const wait =\n delayMs ?? Math.min(maxDelayMs, initialDelayMs * 2 ** (failures - 1));\n if (wait > 0) await sleep(wait, signal);\n try {\n const next = await reopen(lastEventId, received);\n if (next) reader = events(next);\n return next !== undefined;\n } catch (error) {\n if (\n signal?.aborted ||\n (error instanceof WowProblemError && !error.retry.retryable)\n ) {\n throw error;\n }\n delayMs = (error as WowProblemError).retry?.afterMs;\n cause = error;\n }\n }\n }\n\n return new ReadableStream<StreamedRow<T>>({\n async pull(controller) {\n for (;;) {\n let stop: unknown;\n let delayMs: number | undefined;\n try {\n const next = await reader.read();\n if (next.done) {\n // Every stream ends with `end`: one without was cut off.\n stop = new TypeError('The query stream was cut off.');\n } else {\n const { event, id, data } = next.value;\n if (event === ROW_EVENT || event === UNNAMED_EVENT) {\n if (id) lastEventId = id;\n received++;\n failures = 0;\n controller.enqueue(\n id\n ? { record: data as T, position: id }\n : { record: data as T },\n );\n return;\n }\n if (event === END_EVENT) {\n const end = data as StreamEnd;\n if (end.reason === COMPLETE) {\n controller.close();\n return;\n }\n stop = new TypeError(`The query stream ended: ${end.reason}.`);\n if (!end.resumable) throw stop;\n if (end.reason === HOLD_EXPIRED) delayMs = 0;\n } else if (event === ERROR_EVENT) {\n const error = problemOf(data);\n if (!error.retry.retryable) throw error;\n stop = error;\n delayMs = error.retry.afterMs;\n } else {\n continue;\n }\n }\n } catch (error) {\n if (error === stop || error instanceof WowProblemError) throw error;\n stop = error;\n }\n if (cancelled) return;\n // Only a stream read from the start, or whose records have positions, can be asked for again.\n if (signal?.aborted) throw signal.reason;\n if (lastEventId === undefined && received > 0) throw stop;\n if (!(await resume(stop, delayMs))) {\n controller.close();\n return;\n }\n }\n },\n cancel(reason) {\n cancelled = true;\n return reader.cancel(reason);\n },\n });\n}\n"],"mappings":"8GAwBA,IAAMS,EAAY,MAGZC,EAAc,QAGdC,EAAY,MAGZC,EAAgB,UAYtB,SAAgBC,EAAkB,EAA4C,CAC5E,OAAA,EAAOb,EAAAA,4BAAAA,EAAAA,EACLC,EAAAA,wBAAAA,CAAwBa,CAAQ,CAClC,CAAC,CAAC,YACA,IAAI,gBAGF,CACA,UAAU,EAAO,EAAY,CAC3BC,EAAW,QAAQC,EAAM,IAAI,CAC/B,CACF,CAAC,CACH,CACF,CAqDA,IAAMC,EAAgC,eAChCC,EAA4B,WAGlC,SAASC,EAAM,EAAY,EAAqC,CAC9D,OAAO,IAAI,SAAS,EAAS,IAAW,CACtC,IAAMC,MAAc,CAClB,aAAaC,CAAK,EAClBC,EAAOC,GAAQ,MAAM,CACvB,EACMF,EAAQ,eAAiB,CAC7BE,GAAQ,oBAAoB,QAASH,CAAK,EAC1CI,EAAQ,CACV,EAAGC,CAAE,EACLF,GAAQ,iBAAiB,QAASH,CAAK,CACzC,CAAC,CACH,CAEA,SAASM,EAAU,EAAgC,CACjD,OAAO,IAAIxB,EAAAA,gBACTC,EAAAA,UAAUwB,CAAI,EACVA,EACA,CAAE,KAAM,cAAe,MAAO,eAAgB,OAAQ,GAAI,CAChE,CACF,CAeA,SAAgBC,EACd,EACA,EAIA,EAII,CAAC,EAC2B,CAChC,GAAM,CACJ,aAAa,EACb,iBAAiB,IACjB,aAAa,IACb,UACEC,EACEC,EAAUD,EAAQ,QAClBE,EAAU,IACb3B,EAAAA,aAAa0B,CAAO,GAAA,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B,IAAUP,GAAaO,IAAUJ,EAAe,CAC9CsC,IAAI,EAAcA,GACtB,IACA,EAAW,EACXnC,EAAW,QACTmC,EACI,CAAE,OAAQvB,EAAW,SAAUuB,CAAG,EAClC,CAAE,OAAQvB,CAAU,CAC1B,EACA,MACF,CACA,GAAIX,IAAUL,EAAW,CACvB,IAAMwC,EAAMxB,EACZ,GAAIwB,EAAI,SAAWjC,EAAU,CAC3BH,EAAW,MAAM,EACjB,MACF,CAEA,GADA,EAAW,UAAU,2BAA2BoC,EAAI,OAAO,EAAE,EACzD,CAACA,EAAI,UAAW,MAAMF,EACtBE,EAAI,SAAWlC,IAAc,EAAU,EAC7C,MAAO,GAAID,IAAUN,EAAa,CAChC,IAAMsC,EAAQtB,EAAUC,CAAI,EAC5B,GAAI,CAACqB,EAAM,MAAM,UAAW,MAAMA,EAClC,EAAOA,EACP,EAAUA,EAAM,MAAM,OACxB,MACE,QAEJ,CACF,OAASA,EAAO,CACd,GAAIA,IAAUC,GAAQD,aAAiB9C,EAAAA,gBAAiB,MAAM8C,EAC9D,EAAOA,CACT,CACA,GAAIV,EAAW,OAEf,GAAIf,GAAQ,QAAS,MAAMA,EAAO,OAClC,GAAIY,IAAgB,IAAA,IAAaC,EAAW,EAAG,MAAMa,EACrD,GAAI,CAAE,MAAMV,EAAOU,EAAMN,CAAO,EAAI,CAClC5B,EAAW,MAAM,EACjB,MACF,CACF,CACF,EACA,OAAO,EAAQ,CAEb,MADA,GAAY,GACLmB,EAAO,OAAOkB,CAAM,CAC7B,CACF,CAAC,CACH"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { TextNumbers } from './exactJson.cjs';
|
|
2
|
+
import { CommandResult } from './wire.cjs';
|
|
3
|
+
/**
|
|
4
|
+
* The results of a command's event stream (`Accept: text/event-stream`), one
|
|
5
|
+
* per hop, each event named by its stage. A rejection or a failure after
|
|
6
|
+
* admission is the stream's last result, not an error.
|
|
7
|
+
*/
|
|
8
|
+
export type CommandResultStream<R = Record<string, unknown>> = ReadableStream<CommandResult<R>>;
|
|
9
|
+
/** The data of each event of `response`'s server-sent event stream. */
|
|
10
|
+
export declare function commandResults<R>(response: Response): CommandResultStream<R>;
|
|
11
|
+
/**
|
|
12
|
+
* Why a resumable streamed list ended (decision D253): `complete` once every
|
|
13
|
+
* record was sent; the others are the server's, and resumable: the stream ran
|
|
14
|
+
* for its hold time, its store had no query permit for its next chunk, or
|
|
15
|
+
* the node is stopping.
|
|
16
|
+
*/
|
|
17
|
+
export type StreamEndReason = 'complete' | 'hold-expired' | 'permit-reclaimed' | 'node-stopping';
|
|
18
|
+
/** The data of the `end` event that ends a resumable streamed list. */
|
|
19
|
+
export interface StreamEnd {
|
|
20
|
+
/** Why it ended. */
|
|
21
|
+
readonly reason: StreamEndReason | (string & {});
|
|
22
|
+
/** Whether asking again with `Last-Event-ID` goes on where it ended. */
|
|
23
|
+
readonly resumable: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** One record of a streamed list, with its position. */
|
|
26
|
+
export interface StreamedRow<T> {
|
|
27
|
+
/** The record. */
|
|
28
|
+
readonly record: T;
|
|
29
|
+
/**
|
|
30
|
+
* The record's position, its event's `id`: an opaque cursor token to
|
|
31
|
+
* resume after it ({@link StreamResumeOptions.lastEventId}). Absent from a
|
|
32
|
+
* list whose sort cannot order a cursor, which cannot be resumed.
|
|
33
|
+
*/
|
|
34
|
+
readonly position?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* How a streamed list resumes (decision D253). A stream the server ends with
|
|
38
|
+
* a resumable `end`, one cut off before its `end`, and one that fails with a
|
|
39
|
+
* retryable problem (a 429 after its `Retry-After`) are asked for again with
|
|
40
|
+
* `Last-Event-ID`, the position of the last record read, and go on strictly
|
|
41
|
+
* after it.
|
|
42
|
+
*/
|
|
43
|
+
export interface StreamResumeOptions {
|
|
44
|
+
/** Starts after this position: a {@link StreamedRow.position} read before. */
|
|
45
|
+
readonly lastEventId?: string;
|
|
46
|
+
/**
|
|
47
|
+
* How many times in a row the stream may be resumed without a record in
|
|
48
|
+
* between before it fails with what stopped it; 5 by default, 0 never
|
|
49
|
+
* resumes.
|
|
50
|
+
*/
|
|
51
|
+
readonly maxRetries?: number;
|
|
52
|
+
/** The wait before the first such retry, doubled for each next; 200 ms by default. */
|
|
53
|
+
readonly initialDelayMs?: number;
|
|
54
|
+
/** The longest wait between retries; 10 s by default. */
|
|
55
|
+
readonly maxDelayMs?: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The records of a resumable streamed list (decision D253), read from
|
|
59
|
+
* `response` and, each time the stream is to be resumed, from what `reopen`
|
|
60
|
+
* answers for the position of the last record read (none before the first),
|
|
61
|
+
* with how many records were read so far: `undefined` when no more are
|
|
62
|
+
* wanted. Each record comes with its position. An `error` event that is not
|
|
63
|
+
* retryable, a failure `reopen` cannot get past, and an abort of `signal`
|
|
64
|
+
* error the stream.
|
|
65
|
+
*
|
|
66
|
+
* @param response - The first response
|
|
67
|
+
* @param reopen - Asks for the stream again after a position
|
|
68
|
+
* @param options - How the stream resumes, and what cancels it
|
|
69
|
+
*/
|
|
70
|
+
export declare function resumableQueryRows<T>(response: Response, reopen: (lastEventId: string | undefined, received: number) => Promise<Response | undefined>, options?: StreamResumeOptions & {
|
|
71
|
+
readonly signal?: AbortSignal;
|
|
72
|
+
/** The numbers of each record read as their exact text (review R4-Q, Q-9). */
|
|
73
|
+
readonly numbers?: TextNumbers;
|
|
74
|
+
}): ReadableStream<StreamedRow<T>>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { TextNumbers } from './exactJson.js';
|
|
2
|
+
import { CommandResult } from './wire.js';
|
|
3
|
+
/**
|
|
4
|
+
* The results of a command's event stream (`Accept: text/event-stream`), one
|
|
5
|
+
* per hop, each event named by its stage. A rejection or a failure after
|
|
6
|
+
* admission is the stream's last result, not an error.
|
|
7
|
+
*/
|
|
8
|
+
export type CommandResultStream<R = Record<string, unknown>> = ReadableStream<CommandResult<R>>;
|
|
9
|
+
/** The data of each event of `response`'s server-sent event stream. */
|
|
10
|
+
export declare function commandResults<R>(response: Response): CommandResultStream<R>;
|
|
11
|
+
/**
|
|
12
|
+
* Why a resumable streamed list ended (decision D253): `complete` once every
|
|
13
|
+
* record was sent; the others are the server's, and resumable: the stream ran
|
|
14
|
+
* for its hold time, its store had no query permit for its next chunk, or
|
|
15
|
+
* the node is stopping.
|
|
16
|
+
*/
|
|
17
|
+
export type StreamEndReason = 'complete' | 'hold-expired' | 'permit-reclaimed' | 'node-stopping';
|
|
18
|
+
/** The data of the `end` event that ends a resumable streamed list. */
|
|
19
|
+
export interface StreamEnd {
|
|
20
|
+
/** Why it ended. */
|
|
21
|
+
readonly reason: StreamEndReason | (string & {});
|
|
22
|
+
/** Whether asking again with `Last-Event-ID` goes on where it ended. */
|
|
23
|
+
readonly resumable: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** One record of a streamed list, with its position. */
|
|
26
|
+
export interface StreamedRow<T> {
|
|
27
|
+
/** The record. */
|
|
28
|
+
readonly record: T;
|
|
29
|
+
/**
|
|
30
|
+
* The record's position, its event's `id`: an opaque cursor token to
|
|
31
|
+
* resume after it ({@link StreamResumeOptions.lastEventId}). Absent from a
|
|
32
|
+
* list whose sort cannot order a cursor, which cannot be resumed.
|
|
33
|
+
*/
|
|
34
|
+
readonly position?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* How a streamed list resumes (decision D253). A stream the server ends with
|
|
38
|
+
* a resumable `end`, one cut off before its `end`, and one that fails with a
|
|
39
|
+
* retryable problem (a 429 after its `Retry-After`) are asked for again with
|
|
40
|
+
* `Last-Event-ID`, the position of the last record read, and go on strictly
|
|
41
|
+
* after it.
|
|
42
|
+
*/
|
|
43
|
+
export interface StreamResumeOptions {
|
|
44
|
+
/** Starts after this position: a {@link StreamedRow.position} read before. */
|
|
45
|
+
readonly lastEventId?: string;
|
|
46
|
+
/**
|
|
47
|
+
* How many times in a row the stream may be resumed without a record in
|
|
48
|
+
* between before it fails with what stopped it; 5 by default, 0 never
|
|
49
|
+
* resumes.
|
|
50
|
+
*/
|
|
51
|
+
readonly maxRetries?: number;
|
|
52
|
+
/** The wait before the first such retry, doubled for each next; 200 ms by default. */
|
|
53
|
+
readonly initialDelayMs?: number;
|
|
54
|
+
/** The longest wait between retries; 10 s by default. */
|
|
55
|
+
readonly maxDelayMs?: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The records of a resumable streamed list (decision D253), read from
|
|
59
|
+
* `response` and, each time the stream is to be resumed, from what `reopen`
|
|
60
|
+
* answers for the position of the last record read (none before the first),
|
|
61
|
+
* with how many records were read so far: `undefined` when no more are
|
|
62
|
+
* wanted. Each record comes with its position. An `error` event that is not
|
|
63
|
+
* retryable, a failure `reopen` cannot get past, and an abort of `signal`
|
|
64
|
+
* error the stream.
|
|
65
|
+
*
|
|
66
|
+
* @param response - The first response
|
|
67
|
+
* @param reopen - Asks for the stream again after a position
|
|
68
|
+
* @param options - How the stream resumes, and what cancels it
|
|
69
|
+
*/
|
|
70
|
+
export declare function resumableQueryRows<T>(response: Response, reopen: (lastEventId: string | undefined, received: number) => Promise<Response | undefined>, options?: StreamResumeOptions & {
|
|
71
|
+
readonly signal?: AbortSignal;
|
|
72
|
+
/** The numbers of each record read as their exact text (review R4-Q, Q-9). */
|
|
73
|
+
readonly numbers?: TextNumbers;
|
|
74
|
+
}): ReadableStream<StreamedRow<T>>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { marksNothing as e, parseJson as t } from "./exactJson.es.js";
|
|
2
|
+
import { WowProblemError as n, isProblem as r } from "./problem.es.js";
|
|
3
|
+
import { toJsonServerSentEventStream as i, toServerSentEventStream as a } from "@ahoo-wang/fetcher-eventstream";
|
|
4
|
+
//#region src/streams.ts
|
|
5
|
+
var o = "row", s = "error", c = "end", l = "message";
|
|
6
|
+
function u(e) {
|
|
7
|
+
return i(a(e)).pipeThrough(new TransformStream({ transform(e, t) {
|
|
8
|
+
t.enqueue(e.data);
|
|
9
|
+
} }));
|
|
10
|
+
}
|
|
11
|
+
var d = "hold-expired", f = "complete";
|
|
12
|
+
function p(e, t) {
|
|
13
|
+
return new Promise((n, r) => {
|
|
14
|
+
let i = () => {
|
|
15
|
+
clearTimeout(a), r(t?.reason);
|
|
16
|
+
}, a = setTimeout(() => {
|
|
17
|
+
t?.removeEventListener("abort", i), n();
|
|
18
|
+
}, e);
|
|
19
|
+
t?.addEventListener("abort", i);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function m(e) {
|
|
23
|
+
return new n(r(e) ? e : {
|
|
24
|
+
type: "about:blank",
|
|
25
|
+
title: "Query failed",
|
|
26
|
+
status: 500
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function h(r, u, h = {}) {
|
|
30
|
+
let { maxRetries: g = 5, initialDelayMs: _ = 200, maxDelayMs: v = 1e4, signal: y } = h, b = h.numbers, x = (n) => (e(b) ? i(a(n)) : a(n).pipeThrough(new TransformStream({ transform(e, n) {
|
|
31
|
+
let r = e.event === o || e.event === l;
|
|
32
|
+
n.enqueue({
|
|
33
|
+
...e,
|
|
34
|
+
data: t(e.data, r ? b : void 0)
|
|
35
|
+
});
|
|
36
|
+
} }))).getReader(), S = x(r), C = h.lastEventId, w = 0, T = 0, E = !1;
|
|
37
|
+
async function D(e, t) {
|
|
38
|
+
for (;;) {
|
|
39
|
+
if (y?.aborted) throw y.reason;
|
|
40
|
+
if (T++ >= g) throw e;
|
|
41
|
+
let r = t ?? Math.min(v, _ * 2 ** (T - 1));
|
|
42
|
+
r > 0 && await p(r, y);
|
|
43
|
+
try {
|
|
44
|
+
let e = await u(C, w);
|
|
45
|
+
return e && (S = x(e)), e !== void 0;
|
|
46
|
+
} catch (r) {
|
|
47
|
+
if (y?.aborted || r instanceof n && !r.retry.retryable) throw r;
|
|
48
|
+
t = r.retry?.afterMs, e = r;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return new ReadableStream({
|
|
53
|
+
async pull(e) {
|
|
54
|
+
for (;;) {
|
|
55
|
+
let t, r;
|
|
56
|
+
try {
|
|
57
|
+
let n = await S.read();
|
|
58
|
+
if (n.done) t = /* @__PURE__ */ TypeError("The query stream was cut off.");
|
|
59
|
+
else {
|
|
60
|
+
let { event: i, id: a, data: u } = n.value;
|
|
61
|
+
if (i === o || i === l) {
|
|
62
|
+
a && (C = a), w++, T = 0, e.enqueue(a ? {
|
|
63
|
+
record: u,
|
|
64
|
+
position: a
|
|
65
|
+
} : { record: u });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (i === c) {
|
|
69
|
+
let n = u;
|
|
70
|
+
if (n.reason === f) {
|
|
71
|
+
e.close();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (t = /* @__PURE__ */ TypeError(`The query stream ended: ${n.reason}.`), !n.resumable) throw t;
|
|
75
|
+
n.reason === d && (r = 0);
|
|
76
|
+
} else if (i === s) {
|
|
77
|
+
let e = m(u);
|
|
78
|
+
if (!e.retry.retryable) throw e;
|
|
79
|
+
t = e, r = e.retry.afterMs;
|
|
80
|
+
} else continue;
|
|
81
|
+
}
|
|
82
|
+
} catch (e) {
|
|
83
|
+
if (e === t || e instanceof n) throw e;
|
|
84
|
+
t = e;
|
|
85
|
+
}
|
|
86
|
+
if (E) return;
|
|
87
|
+
if (y?.aborted) throw y.reason;
|
|
88
|
+
if (C === void 0 && w > 0) throw t;
|
|
89
|
+
if (!await D(t, r)) {
|
|
90
|
+
e.close();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
cancel(e) {
|
|
96
|
+
return E = !0, S.cancel(e);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
export { u as commandResults, h as resumableQueryRows };
|
|
102
|
+
|
|
103
|
+
//# sourceMappingURL=streams.es.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streams.es.js","names":["ROW_EVENT","ERROR_EVENT","END_EVENT","UNNAMED_EVENT","commandResults","toJsonServerSentEventStream","toServerSentEventStream","response","event","HOLD_EXPIRED","COMPLETE","sleep","abort","timer","reject","signal","resolve","ms","problemOf","WowProblemError","isProblem","data","resumableQueryRows","options","numbers","events","marksNothing","from","row","parseJson","reader","lastEventId","received","failures","cancelled","resume","maxRetries","cause","wait","delayMs","maxDelayMs","initialDelayMs","next","reopen","error","stop","id","controller","end","reason"],"sources":["../src/streams.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n type JsonServerSentEvent,\n type ServerSentEvent,\n toJsonServerSentEventStream,\n toServerSentEventStream,\n} from '@ahoo-wang/fetcher-eventstream';\nimport { marksNothing, parseJson, type TextNumbers } from './exactJson.js';\nimport type { CommandResult } from './wire.js';\nimport { isProblem, WowProblemError } from './problem.js';\n\n/** The event of each record of a streamed query list. */\nconst ROW_EVENT = 'row';\n\n/** The event that ends a streamed query list that failed: the problem. */\nconst ERROR_EVENT = 'error';\n\n/** The event that ends a resumable streamed query list (decision D253). */\nconst END_EVENT = 'end';\n\n/** The name an event without one is read under. */\nconst UNNAMED_EVENT = 'message';\n\n/**\n * The results of a command's event stream (`Accept: text/event-stream`), one\n * per hop, each event named by its stage. A rejection or a failure after\n * admission is the stream's last result, not an error.\n */\nexport type CommandResultStream<R = Record<string, unknown>> = ReadableStream<\n CommandResult<R>\n>;\n\n/** The data of each event of `response`'s server-sent event stream. */\nexport function commandResults<R>(response: Response): CommandResultStream<R> {\n return toJsonServerSentEventStream<CommandResult<R>>(\n toServerSentEventStream(response),\n ).pipeThrough(\n new TransformStream<\n JsonServerSentEvent<CommandResult<R>>,\n CommandResult<R>\n >({\n transform(event, controller) {\n controller.enqueue(event.data);\n },\n }),\n );\n}\n\n/**\n * Why a resumable streamed list ended (decision D253): `complete` once every\n * record was sent; the others are the server's, and resumable: the stream ran\n * for its hold time, its store had no query permit for its next chunk, or\n * the node is stopping.\n */\nexport type StreamEndReason =\n 'complete' | 'hold-expired' | 'permit-reclaimed' | 'node-stopping';\n\n/** The data of the `end` event that ends a resumable streamed list. */\nexport interface StreamEnd {\n /** Why it ended. */\n readonly reason: StreamEndReason | (string & {});\n /** Whether asking again with `Last-Event-ID` goes on where it ended. */\n readonly resumable: boolean;\n}\n\n/** One record of a streamed list, with its position. */\nexport interface StreamedRow<T> {\n /** The record. */\n readonly record: T;\n /**\n * The record's position, its event's `id`: an opaque cursor token to\n * resume after it ({@link StreamResumeOptions.lastEventId}). Absent from a\n * list whose sort cannot order a cursor, which cannot be resumed.\n */\n readonly position?: string;\n}\n\n/**\n * How a streamed list resumes (decision D253). A stream the server ends with\n * a resumable `end`, one cut off before its `end`, and one that fails with a\n * retryable problem (a 429 after its `Retry-After`) are asked for again with\n * `Last-Event-ID`, the position of the last record read, and go on strictly\n * after it.\n */\nexport interface StreamResumeOptions {\n /** Starts after this position: a {@link StreamedRow.position} read before. */\n readonly lastEventId?: string;\n /**\n * How many times in a row the stream may be resumed without a record in\n * between before it fails with what stopped it; 5 by default, 0 never\n * resumes.\n */\n readonly maxRetries?: number;\n /** The wait before the first such retry, doubled for each next; 200 ms by default. */\n readonly initialDelayMs?: number;\n /** The longest wait between retries; 10 s by default. */\n readonly maxDelayMs?: number;\n}\n\nconst HOLD_EXPIRED: StreamEndReason = 'hold-expired';\nconst COMPLETE: StreamEndReason = 'complete';\n\n/** Waits `ms`, or rejects once `signal` aborts. */\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n const abort = () => {\n clearTimeout(timer);\n reject(signal?.reason);\n };\n const timer = setTimeout(() => {\n signal?.removeEventListener('abort', abort);\n resolve();\n }, ms);\n signal?.addEventListener('abort', abort);\n });\n}\n\nfunction problemOf(data: unknown): WowProblemError {\n return new WowProblemError(\n isProblem(data)\n ? data\n : { type: 'about:blank', title: 'Query failed', status: 500 },\n );\n}\n\n/**\n * The records of a resumable streamed list (decision D253), read from\n * `response` and, each time the stream is to be resumed, from what `reopen`\n * answers for the position of the last record read (none before the first),\n * with how many records were read so far: `undefined` when no more are\n * wanted. Each record comes with its position. An `error` event that is not\n * retryable, a failure `reopen` cannot get past, and an abort of `signal`\n * error the stream.\n *\n * @param response - The first response\n * @param reopen - Asks for the stream again after a position\n * @param options - How the stream resumes, and what cancels it\n */\nexport function resumableQueryRows<T>(\n response: Response,\n reopen: (\n lastEventId: string | undefined,\n received: number,\n ) => Promise<Response | undefined>,\n options: StreamResumeOptions & {\n readonly signal?: AbortSignal;\n /** The numbers of each record read as their exact text (review R4-Q, Q-9). */\n readonly numbers?: TextNumbers;\n } = {},\n): ReadableStream<StreamedRow<T>> {\n const {\n maxRetries = 5,\n initialDelayMs = 200,\n maxDelayMs = 10_000,\n signal,\n } = options;\n const numbers = options.numbers;\n const events = (from: Response) =>\n (marksNothing(numbers)\n ? toJsonServerSentEventStream<unknown>(toServerSentEventStream(from))\n : toServerSentEventStream(from).pipeThrough(\n new TransformStream<ServerSentEvent, JsonServerSentEvent<unknown>>({\n transform(event, controller) {\n const row =\n event.event === ROW_EVENT || event.event === UNNAMED_EVENT;\n controller.enqueue({\n ...event,\n data: parseJson(event.data, row ? numbers : undefined),\n });\n },\n }),\n )\n ).getReader();\n let reader = events(response);\n let lastEventId = options.lastEventId;\n let received = 0;\n let failures = 0;\n // Set once the reader cancels: a read in flight then ends, and must not be\n // taken for a cut-off stream to ask for again.\n let cancelled = false;\n\n /** Asks for the stream again, or throws `cause` once out of retries. */\n async function resume(cause: unknown, delayMs?: number): Promise<boolean> {\n for (;;) {\n if (signal?.aborted) throw signal.reason;\n if (failures++ >= maxRetries) throw cause;\n const wait =\n delayMs ?? Math.min(maxDelayMs, initialDelayMs * 2 ** (failures - 1));\n if (wait > 0) await sleep(wait, signal);\n try {\n const next = await reopen(lastEventId, received);\n if (next) reader = events(next);\n return next !== undefined;\n } catch (error) {\n if (\n signal?.aborted ||\n (error instanceof WowProblemError && !error.retry.retryable)\n ) {\n throw error;\n }\n delayMs = (error as WowProblemError).retry?.afterMs;\n cause = error;\n }\n }\n }\n\n return new ReadableStream<StreamedRow<T>>({\n async pull(controller) {\n for (;;) {\n let stop: unknown;\n let delayMs: number | undefined;\n try {\n const next = await reader.read();\n if (next.done) {\n // Every stream ends with `end`: one without was cut off.\n stop = new TypeError('The query stream was cut off.');\n } else {\n const { event, id, data } = next.value;\n if (event === ROW_EVENT || event === UNNAMED_EVENT) {\n if (id) lastEventId = id;\n received++;\n failures = 0;\n controller.enqueue(\n id\n ? { record: data as T, position: id }\n : { record: data as T },\n );\n return;\n }\n if (event === END_EVENT) {\n const end = data as StreamEnd;\n if (end.reason === COMPLETE) {\n controller.close();\n return;\n }\n stop = new TypeError(`The query stream ended: ${end.reason}.`);\n if (!end.resumable) throw stop;\n if (end.reason === HOLD_EXPIRED) delayMs = 0;\n } else if (event === ERROR_EVENT) {\n const error = problemOf(data);\n if (!error.retry.retryable) throw error;\n stop = error;\n delayMs = error.retry.afterMs;\n } else {\n continue;\n }\n }\n } catch (error) {\n if (error === stop || error instanceof WowProblemError) throw error;\n stop = error;\n }\n if (cancelled) return;\n // Only a stream read from the start, or whose records have positions, can be asked for again.\n if (signal?.aborted) throw signal.reason;\n if (lastEventId === undefined && received > 0) throw stop;\n if (!(await resume(stop, delayMs))) {\n controller.close();\n return;\n }\n }\n },\n cancel(reason) {\n cancelled = true;\n return reader.cancel(reason);\n },\n });\n}\n"],"mappings":";;;;A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IAAU;MAC7C,OAAO,IAAID,MAAUP,GAAa;OAChC,IAAM2C,IAAQ1B,EAAUG,CAAI;OAC5B,IAAI,CAACuB,EAAM,MAAM,WAAW,MAAMA;OAElC,AADA,IAAOA,GACP,IAAUA,EAAM,MAAM;MACxB,OACE;KAEJ;IACF,SAASA,GAAO;KACd,IAAIA,MAAUC,KAAQD,aAAiBzB,GAAiB,MAAMyB;KAC9D,IAAOA;IACT;IACA,IAAIV,GAAW;IAEf,IAAInB,GAAQ,SAAS,MAAMA,EAAO;IAClC,IAAIgB,MAAgB,KAAA,KAAaC,IAAW,GAAG,MAAMa;IACrD,IAAI,CAAE,MAAMV,EAAOU,GAAMN,CAAO,GAAI;KAClC,EAAW,MAAM;KACjB;IACF;GACF;EACF;EACA,OAAO,GAAQ;GAEb,OADA,IAAY,IACLT,EAAO,OAAOmB,CAAM;EAC7B;CACF,CAAC;AACH"}
|
package/dist/wait.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=require("./wire.cjs");var t=`snapshot`,n=/^[A-Za-z0-9_.-]+$/;function r(e,t){if(!n.test(t))throw TypeError(`Not a ${e} name: '${t}'.`);return t}function i(e,t,n){return{stage:e,...t===void 0?{}:{context:r(`context`,t)},...n===void 0?{}:{subscription:r(`subscription`,n)}}}var a={accepted:t=>i(e.Stage.ACCEPTED,t),committed:t=>i(e.Stage.COMMITTED,t),snapshot:n=>i(e.Stage.HANDLED,n,t),handled:(t,n)=>i(e.Stage.HANDLED,n,t)};function o(t){let n=t.context??void 0,r=t.subscription??void 0;if(t.stage===e.Stage.HANDLED){if(r===void 0)throw TypeError(`A HANDLED hop names its subscription.`);return r===`snapshot`?n===void 0?`snapshot`:`snapshot(${n})`:n===void 0?`handled(${r})`:`handled(${n}/${r})`}if(r!==void 0)throw TypeError(`Only a HANDLED hop names a subscription.`);let i=t.stage.toLowerCase();return n===void 0?i:`${i}(${n})`}function s(e){if(typeof e==`string`)return e;if(e.length===0)throw TypeError(`A wait plan has at least one hop.`);return e.map(o).join(` > `)}function c(e){if(typeof e==`string`)return e;if(!Number.isInteger(e)||e<=0)throw TypeError(`A wait timeout is a positive whole number of milliseconds: ${e}.`);return e%1e3==0?`${e/1e3}s`:`${e}ms`}exports.SNAPSHOT_SUBSCRIPTION=t,exports.formatHop=o,exports.formatWaitPlan=s,exports.formatWaitTimeout=c,exports.wait=a;
|
|
2
|
+
//# sourceMappingURL=wait.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wait.cjs","names":["Stage","require_wire","SNAPSHOT_SUBSCRIPTION","NAME","checkName","value","kind","hop","context","subscription","wait","formatHop","keyword","formatWaitPlan","plan","formatWaitTimeout","timeout"],"sources":["../src/wait.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { type Hop, Stage } from './wire.js';\n\n/** The name of the built-in subscription that writes snapshots. */\nexport const SNAPSHOT_SUBSCRIPTION = 'snapshot';\n\nconst NAME = /^[A-Za-z0-9_.-]+$/;\n\nfunction checkName(kind: string, value: string): string {\n if (!NAME.test(value)) {\n throw new TypeError(`Not a ${kind} name: '${value}'.`);\n }\n return value;\n}\n\nfunction hop(stage: Stage, context?: string, subscription?: string): Hop {\n return {\n stage,\n ...(context === undefined\n ? {}\n : { context: checkName('context', context) }),\n ...(subscription === undefined\n ? {}\n : { subscription: checkName('subscription', subscription) }),\n };\n}\n\n/**\n * Builds the hops of a wait chain (decisions D62, D99). A context left out is\n * the command's own. A saga's `handled` hop may be followed by hops of the\n * commands it derived, in their contexts:\n *\n * @example\n * ```typescript\n * const plan = [\n * wait.committed(),\n * wait.handled('BasketCheckoutSaga.onBasketCheckedOut', 'eshop-basket'),\n * wait.committed('eshop-ordering'),\n * ];\n * ```\n */\nexport const wait = {\n /** Until the command passed admission; the route answers 202. */\n accepted: (context?: string): Hop => hop(Stage.ACCEPTED, context),\n /** Until the command's events are appended to the log. */\n committed: (context?: string): Hop => hop(Stage.COMMITTED, context),\n /** Until the snapshot holds the commit: the caller reads its own write. */\n snapshot: (context?: string): Hop =>\n hop(Stage.HANDLED, context, SNAPSHOT_SUBSCRIPTION),\n /** Until the subscription named `processor.function` handled the commit. */\n handled: (subscription: string, context?: string): Hop =>\n hop(Stage.HANDLED, context, subscription),\n} as const;\n\n/**\n * A hop as `Wow-Wait` spells it: `accepted`, `committed`, `snapshot` and\n * `handled(subscription)`, each with its context when it has one:\n * `committed(ctx)`, `snapshot(ctx)`, `handled(ctx/subscription)`.\n *\n * @param value - The hop\n */\nexport function formatHop(value: Hop): string {\n const context = value.context ?? undefined;\n const subscription = value.subscription ?? undefined;\n if (value.stage === Stage.HANDLED) {\n if (subscription === undefined) {\n throw new TypeError('A HANDLED hop names its subscription.');\n }\n if (subscription === SNAPSHOT_SUBSCRIPTION) {\n return context === undefined ? 'snapshot' : `snapshot(${context})`;\n }\n return context === undefined\n ? `handled(${subscription})`\n : `handled(${context}/${subscription})`;\n }\n if (subscription !== undefined) {\n throw new TypeError(`Only a HANDLED hop names a subscription.`);\n }\n const keyword = value.stage.toLowerCase();\n return context === undefined ? keyword : `${keyword}(${context})`;\n}\n\n/**\n * A wait plan as the `Wow-Wait` header spells it: its hops, in order,\n * separated by `>`. A string is taken as already spelled.\n *\n * @param plan - The hops, or the header's value\n */\nexport function formatWaitPlan(plan: readonly Hop[] | string): string {\n if (typeof plan === 'string') return plan;\n if (plan.length === 0) {\n throw new TypeError('A wait plan has at least one hop.');\n }\n return plan.map(formatHop).join(' > ');\n}\n\n/**\n * A wait timeout as the `Wow-Wait-Timeout` header spells it: milliseconds\n * as `<n>s` when whole seconds, else `<n>ms`. A string (`10s`, `PT10S`) is\n * taken as already spelled.\n *\n * @param timeout - Milliseconds, or the header's value\n */\nexport function formatWaitTimeout(timeout: number | string): string {\n if (typeof timeout === 'string') return timeout;\n if (!Number.isInteger(timeout) || timeout <= 0) {\n throw new TypeError(\n `A wait timeout is a positive whole number of milliseconds: ${timeout}.`,\n );\n }\n return timeout % 1000 === 0 ? `${timeout / 1000}s` : `${timeout}ms`;\n}\n\n/**\n * What a command waits for (`Wow-Wait`, `Wow-Wait-Timeout`). Without either,\n * the server waits for the snapshot, or for the commit where the aggregate\n * keeps no snapshot (decision D69), within its default timeout.\n */\nexport interface WaitOptions {\n /** The hops, or the `Wow-Wait` value. */\n readonly plan?: readonly Hop[] | string;\n /** Milliseconds, or the `Wow-Wait-Timeout` value. */\n readonly timeout?: number | string;\n}\n"],"mappings":"8BAgBA,IAAaE,EAAwB,WAE/BC,EAAO,oBAEb,SAASC,EAAU,EAAc,EAAuB,CACtD,GAAI,CAACD,EAAK,KAAKE,CAAK,EAClB,MAAU,UAAU,SAASC,EAAK,UAAUD,EAAM,GAAG,EAEvD,OAAOA,CACT,CAEA,SAASE,EAAI,EAAc,EAAkB,EAA4B,CACvE,MAAO,CACL,QACA,GAAIC,IAAY,IAAA,GACZ,CAAC,EACD,CAAE,QAASJ,EAAU,UAAWI,CAAO,CAAE,EAC7C,GAAIC,IAAiB,IAAA,GACjB,CAAC,EACD,CAAE,aAAcL,EAAU,eAAgBK,CAAY,CAAE,CAC9D,CACF,CAgBA,IAAaC,EAAO,CAElB,SAAW,GAA0BH,EAAIP,EAAAA,MAAM,SAAUQ,CAAO,EAEhE,UAAY,GAA0BD,EAAIP,EAAAA,MAAM,UAAWQ,CAAO,EAElE,SAAW,GACTD,EAAIP,EAAAA,MAAM,QAASQ,EAASN,CAAqB,EAEnD,SAAU,EAAsB,IAC9BK,EAAIP,EAAAA,MAAM,QAASQ,EAASC,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