@encore.dev/sdk 2.0.0-beta.6 → 2.0.0-beta.8
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 +55 -0
- package/app_meta.ts +8 -0
- package/config/config.test.ts +10 -3
- package/config/config.ts +75 -21
- package/config/mod.ts +4 -3
- package/config/secrets.ts +72 -55
- package/cron/mod.ts +32 -0
- package/dist/app_meta.d.ts +7 -0
- package/dist/app_meta.d.ts.map +1 -0
- package/dist/app_meta.js +1 -0
- package/dist/app_meta.js.map +1 -1
- package/dist/config/config.d.ts +44 -18
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/config.js +43 -18
- package/dist/config/config.js.map +1 -1
- package/dist/config/mod.d.ts +5 -4
- package/dist/config/mod.d.ts.map +1 -0
- package/dist/config/mod.js +2 -2
- package/dist/config/mod.js.map +1 -1
- package/dist/config/secrets.d.ts +62 -39
- package/dist/config/secrets.d.ts.map +1 -0
- package/dist/config/secrets.js +21 -37
- package/dist/config/secrets.js.map +1 -1
- package/dist/config/value.d.ts +1 -0
- package/dist/config/value.d.ts.map +1 -0
- package/dist/cron/mod.d.ts +31 -0
- package/dist/cron/mod.d.ts.map +1 -0
- package/dist/cron/mod.js +23 -0
- package/dist/cron/mod.js.map +1 -1
- package/dist/gateway/errors.d.ts +101 -0
- package/dist/gateway/errors.d.ts.map +1 -0
- package/dist/gateway/errors.js +146 -0
- package/dist/gateway/errors.js.map +1 -0
- package/dist/gateway/match.d.ts +127 -0
- package/dist/gateway/match.d.ts.map +1 -0
- package/dist/gateway/match.js +206 -0
- package/dist/gateway/match.js.map +1 -0
- package/dist/gateway/middleware.d.ts +108 -0
- package/dist/gateway/middleware.d.ts.map +1 -0
- package/dist/gateway/middleware.js +9 -0
- package/dist/gateway/middleware.js.map +1 -0
- package/dist/gateway/mod.d.ts +129 -0
- package/dist/gateway/mod.d.ts.map +1 -0
- package/dist/gateway/mod.js +187 -0
- package/dist/gateway/mod.js.map +1 -0
- package/dist/gateway/request.d.ts +304 -0
- package/dist/gateway/request.d.ts.map +1 -0
- package/dist/gateway/request.js +403 -0
- package/dist/gateway/request.js.map +1 -0
- package/dist/gateway/serve.d.ts +114 -0
- package/dist/gateway/serve.d.ts.map +1 -0
- package/dist/gateway/serve.js +510 -0
- package/dist/gateway/serve.js.map +1 -0
- package/dist/internal/clients/call.d.ts +147 -0
- package/dist/internal/clients/call.d.ts.map +1 -0
- package/dist/internal/clients/call.js +274 -0
- package/dist/internal/clients/call.js.map +1 -0
- package/dist/internal/clients/grpc.d.ts +90 -0
- package/dist/internal/clients/grpc.d.ts.map +1 -0
- package/dist/internal/clients/grpc.js +355 -0
- package/dist/internal/clients/grpc.js.map +1 -0
- package/dist/internal/clients/mod.d.ts +81 -0
- package/dist/internal/clients/mod.d.ts.map +1 -0
- package/dist/internal/clients/mod.js +237 -0
- package/dist/internal/clients/mod.js.map +1 -0
- package/dist/internal/fetch.d.ts +54 -0
- package/dist/internal/fetch.d.ts.map +1 -0
- package/dist/internal/fetch.js +240 -0
- package/dist/internal/fetch.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +2 -0
- package/dist/internal/otel/backend.d.ts.map +1 -0
- package/dist/internal/otel/context.d.ts +1 -0
- package/dist/internal/otel/context.d.ts.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +1 -0
- package/dist/internal/otel/context_manager.d.ts.map +1 -0
- package/dist/internal/otel/encode.d.ts +2 -0
- package/dist/internal/otel/encode.d.ts.map +1 -0
- package/dist/internal/otel/index.d.ts +2 -2
- package/dist/internal/otel/index.d.ts.map +1 -0
- package/dist/internal/otel/index.js +1 -2
- package/dist/internal/otel/index.js.map +1 -1
- package/dist/internal/otel/meter.d.ts +1 -0
- package/dist/internal/otel/meter.d.ts.map +1 -0
- package/dist/internal/otel/propagator.d.ts +1 -0
- package/dist/internal/otel/propagator.d.ts.map +1 -0
- package/dist/internal/otel/provider.d.ts +1 -0
- package/dist/internal/otel/provider.d.ts.map +1 -0
- package/dist/internal/otel/span.d.ts +4 -0
- package/dist/internal/otel/span.d.ts.map +1 -0
- package/dist/internal/otel/span.js +5 -0
- package/dist/internal/otel/span.js.map +1 -1
- package/dist/internal/otel/tracer.d.ts +1 -0
- package/dist/internal/otel/tracer.d.ts.map +1 -0
- package/dist/internal/otel/tracer.js +11 -5
- package/dist/internal/otel/tracer.js.map +1 -1
- package/dist/internal/reqfields.d.ts +150 -0
- package/dist/internal/reqfields.d.ts.map +1 -0
- package/dist/internal/reqfields.js +312 -0
- package/dist/internal/reqfields.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +1 -0
- package/dist/internal/runtime/meter.d.ts.map +1 -0
- package/dist/internal/runtime/mod.d.ts +6 -0
- package/dist/internal/runtime/mod.d.ts.map +1 -0
- package/dist/internal/runtime/mod.js +32 -2
- package/dist/internal/runtime/mod.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +160 -16
- package/dist/internal/runtime/napi/napi.d.cts +474 -29
- package/dist/internal/runtime/shutdown.d.ts +5 -0
- package/dist/internal/runtime/shutdown.d.ts.map +1 -0
- package/dist/internal/runtime/shutdown.js +9 -1
- package/dist/internal/runtime/shutdown.js.map +1 -1
- package/dist/internal/testing/runner.d.ts +2 -0
- package/dist/internal/testing/runner.d.ts.map +1 -0
- package/dist/internal/testing/runner.js +10 -0
- package/dist/internal/testing/runner.js.map +1 -0
- package/dist/internal/testing/scope.d.ts +81 -0
- package/dist/internal/testing/scope.d.ts.map +1 -0
- package/dist/internal/testing/scope.js +237 -0
- package/dist/internal/testing/scope.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +1 -0
- package/dist/internal/utils/constraints.d.ts.map +1 -0
- package/dist/log/index.d.ts +1 -0
- package/dist/log/index.d.ts.map +1 -0
- package/dist/log/logger.d.ts +2 -0
- package/dist/log/logger.d.ts.map +1 -0
- package/dist/log/runtime.d.ts +2 -0
- package/dist/log/runtime.d.ts.map +1 -0
- package/dist/log/stderr.d.ts +1 -0
- package/dist/log/stderr.d.ts.map +1 -0
- package/dist/metrics/mod.d.ts +45 -118
- package/dist/metrics/mod.d.ts.map +1 -0
- package/dist/metrics/mod.js +44 -179
- package/dist/metrics/mod.js.map +1 -1
- package/dist/mod.d.ts +1 -2
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +0 -1
- package/dist/mod.js.map +1 -1
- package/dist/otel.d.ts +1 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/pubsub/encoding.d.ts +5 -0
- package/dist/pubsub/encoding.d.ts.map +1 -0
- package/dist/pubsub/encoding.js +10 -2
- package/dist/pubsub/encoding.js.map +1 -1
- package/dist/pubsub/mod.d.ts +4 -3
- package/dist/pubsub/mod.d.ts.map +1 -0
- package/dist/pubsub/mod.js +2 -2
- package/dist/pubsub/mod.js.map +1 -1
- package/dist/pubsub/refs.d.ts +8 -0
- package/dist/pubsub/refs.d.ts.map +1 -0
- package/dist/pubsub/refs.js.map +1 -1
- package/dist/pubsub/subscription.d.ts +104 -10
- package/dist/pubsub/subscription.d.ts.map +1 -0
- package/dist/pubsub/subscription.js +98 -16
- package/dist/pubsub/subscription.js.map +1 -1
- package/dist/pubsub/topic.d.ts +87 -23
- package/dist/pubsub/topic.d.ts.map +1 -0
- package/dist/pubsub/topic.js +143 -40
- package/dist/pubsub/topic.js.map +1 -1
- package/dist/reqctx/mod.d.ts +191 -0
- package/dist/reqctx/mod.d.ts.map +1 -0
- package/dist/reqctx/mod.js +160 -0
- package/dist/reqctx/mod.js.map +1 -0
- package/dist/services/index.d.ts +145 -4
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +155 -8
- package/dist/services/index.js.map +1 -1
- package/dist/shutdown/mod.d.ts +1 -0
- package/dist/shutdown/mod.d.ts.map +1 -0
- package/dist/storage/objects/bucket.d.ts +312 -7
- package/dist/storage/objects/bucket.d.ts.map +1 -0
- package/dist/storage/objects/bucket.js +305 -15
- package/dist/storage/objects/bucket.js.map +1 -1
- package/dist/storage/objects/error.d.ts +10 -0
- package/dist/storage/objects/error.d.ts.map +1 -0
- package/dist/storage/objects/error.js +25 -1
- package/dist/storage/objects/error.js.map +1 -1
- package/dist/storage/objects/mod.d.ts +4 -3
- package/dist/storage/objects/mod.d.ts.map +1 -0
- package/dist/storage/objects/mod.js +1 -1
- package/dist/storage/objects/mod.js.map +1 -1
- package/dist/storage/objects/refs.d.ts +20 -4
- package/dist/storage/objects/refs.d.ts.map +1 -0
- package/dist/storage/objects/refs.js +6 -0
- package/dist/storage/objects/refs.js.map +1 -1
- package/dist/storage/postgres/database.d.ts +81 -25
- package/dist/storage/postgres/database.d.ts.map +1 -0
- package/dist/storage/postgres/database.js +125 -37
- package/dist/storage/postgres/database.js.map +1 -1
- package/dist/storage/postgres/errors.d.ts +120 -0
- package/dist/storage/postgres/errors.d.ts.map +1 -0
- package/dist/storage/postgres/errors.js +162 -0
- package/dist/storage/postgres/errors.js.map +1 -0
- package/dist/storage/postgres/interval.d.ts +44 -0
- package/dist/storage/postgres/interval.d.ts.map +1 -0
- package/dist/storage/postgres/interval.js +153 -0
- package/dist/storage/postgres/interval.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +4 -1
- package/dist/storage/postgres/mod.d.ts.map +1 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -1
- package/dist/storage/redis/bitfield.d.ts +67 -0
- package/dist/storage/redis/bitfield.d.ts.map +1 -0
- package/dist/storage/redis/bitfield.js +114 -0
- package/dist/storage/redis/bitfield.js.map +1 -0
- package/dist/storage/redis/command.d.ts +45 -0
- package/dist/storage/redis/command.d.ts.map +1 -0
- package/dist/storage/redis/command.js +78 -0
- package/dist/storage/redis/command.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +223 -139
- package/dist/storage/redis/commands.d.ts.map +1 -0
- package/dist/storage/redis/commands.js +128 -6
- package/dist/storage/redis/commands.js.map +1 -1
- package/dist/storage/redis/database.d.ts +56 -22
- package/dist/storage/redis/database.d.ts.map +1 -0
- package/dist/storage/redis/database.js +62 -0
- package/dist/storage/redis/database.js.map +1 -1
- package/dist/storage/redis/mod.d.ts +31 -4
- package/dist/storage/redis/mod.d.ts.map +1 -0
- package/dist/storage/redis/mod.js +26 -1
- package/dist/storage/redis/mod.js.map +1 -1
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.d.ts.map +1 -0
- package/dist/storage/redis/native.js +10 -0
- package/dist/storage/redis/native.js.map +1 -1
- package/dist/storage/redis/options.d.ts +1 -0
- package/dist/storage/redis/options.d.ts.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +2 -0
- package/dist/storage/redis/pubsub.d.ts.map +1 -0
- package/dist/storage/redis/reply.d.ts +34 -0
- package/dist/storage/redis/reply.d.ts.map +1 -0
- package/dist/storage/redis/reply.js +40 -1
- package/dist/storage/redis/reply.js.map +1 -1
- package/dist/storage/redis/scan.d.ts +1 -0
- package/dist/storage/redis/scan.d.ts.map +1 -0
- package/dist/storage/redis/script.d.ts +1 -0
- package/dist/storage/redis/script.d.ts.map +1 -0
- package/dist/storage/redis/target.d.ts +5 -3
- package/dist/storage/redis/target.d.ts.map +1 -0
- package/dist/storage/redis/target.js +39 -28
- package/dist/storage/redis/target.js.map +1 -1
- package/dist/storage/redis/wire.d.ts +7 -10
- package/dist/storage/redis/wire.d.ts.map +1 -0
- package/dist/storage/redis/wire.js +8 -32
- package/dist/storage/redis/wire.js.map +1 -1
- package/dist/testing/bun.d.ts +8 -0
- package/dist/testing/bun.d.ts.map +1 -0
- package/dist/testing/bun.js +13 -0
- package/dist/testing/bun.js.map +1 -0
- package/dist/testing/jest.d.ts +8 -0
- package/dist/testing/jest.d.ts.map +1 -0
- package/dist/testing/jest.js +11 -0
- package/dist/testing/jest.js.map +1 -0
- package/dist/testing/mod.d.ts +160 -0
- package/dist/testing/mod.d.ts.map +1 -0
- package/dist/testing/mod.js +227 -0
- package/dist/testing/mod.js.map +1 -0
- package/dist/testing/node.d.ts +9 -0
- package/dist/testing/node.d.ts.map +1 -0
- package/dist/testing/node.js +12 -0
- package/dist/testing/node.js.map +1 -0
- package/dist/testing/vitest.d.ts +8 -0
- package/dist/testing/vitest.d.ts.map +1 -0
- package/dist/testing/vitest.js +13 -0
- package/dist/testing/vitest.js.map +1 -0
- package/dist/tracing/mod.d.ts +19 -0
- package/dist/tracing/mod.d.ts.map +1 -0
- package/dist/tracing/mod.js +46 -0
- package/dist/tracing/mod.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/mod.d.ts +1 -0
- package/dist/types/mod.d.ts.map +1 -0
- package/gateway/errors.ts +172 -0
- package/gateway/gateway.e2e.test.ts +297 -0
- package/gateway/gateway.test.ts +891 -0
- package/gateway/match.ts +239 -0
- package/gateway/middleware.ts +134 -0
- package/gateway/mod.ts +210 -0
- package/gateway/request.ts +578 -0
- package/gateway/serve.ts +738 -0
- package/internal/clients/call.ts +389 -0
- package/internal/clients/grpc.test.ts +368 -0
- package/internal/clients/grpc.ts +485 -0
- package/internal/clients/mod.test.ts +464 -0
- package/internal/clients/mod.ts +337 -0
- package/internal/fetch.ts +276 -0
- package/internal/otel/index.ts +1 -7
- package/internal/otel/span.ts +6 -0
- package/internal/otel/tracer.test.ts +43 -15
- package/internal/otel/tracer.ts +12 -5
- package/internal/reqfields.ts +390 -0
- package/internal/runtime/mod.ts +41 -2
- package/internal/runtime/napi/napi.cjs +160 -16
- package/internal/runtime/napi/napi.d.cts +474 -29
- package/internal/runtime/shutdown.ts +11 -1
- package/internal/testing/runner.ts +10 -0
- package/internal/testing/scope.ts +273 -0
- package/metrics/mod.test.ts +13 -16
- package/metrics/mod.ts +57 -226
- package/mod.ts +0 -8
- package/otel.test.ts +13 -6
- package/package.json +67 -15
- package/pubsub/encoding.ts +11 -1
- package/pubsub/mod.ts +10 -3
- package/pubsub/pubsub.test.ts +232 -14
- package/pubsub/refs.ts +16 -0
- package/pubsub/subscription.ts +194 -16
- package/pubsub/topic.test.ts +26 -0
- package/pubsub/topic.ts +220 -25
- package/reqctx/mod.ts +284 -0
- package/reqctx/reqctx.test.ts +193 -0
- package/scripts/postprocess-docs.test.ts +48 -0
- package/services/index.test.ts +341 -1
- package/services/index.ts +183 -8
- package/storage/objects/bucket.test.ts +573 -0
- package/storage/objects/bucket.ts +649 -20
- package/storage/objects/error.ts +28 -1
- package/storage/objects/mod.ts +15 -0
- package/storage/objects/refs.ts +51 -3
- package/storage/postgres/database.integration.test.ts +275 -0
- package/storage/postgres/database.test.ts +193 -6
- package/storage/postgres/database.ts +202 -55
- package/storage/postgres/errors.ts +186 -0
- package/storage/postgres/interval.test.ts +72 -0
- package/storage/postgres/interval.ts +170 -0
- package/storage/postgres/mod.ts +4 -0
- package/storage/redis/bitfield.ts +162 -0
- package/storage/redis/command.ts +84 -0
- package/storage/redis/commands.ts +271 -21
- package/storage/redis/database.ts +77 -23
- package/storage/redis/mod.ts +38 -2
- package/storage/redis/native.ts +11 -0
- package/storage/redis/redis.test.ts +170 -10
- package/storage/redis/reply.ts +76 -1
- package/storage/redis/runtime.test.ts +15 -0
- package/storage/redis/target.ts +47 -44
- package/storage/redis/wire.ts +8 -32
- package/testing/bun.ts +13 -0
- package/testing/jest.ts +11 -0
- package/testing/mod.ts +337 -0
- package/testing/node.ts +12 -0
- package/testing/vitest.ts +13 -0
- package/tracing/mod.ts +82 -0
- package/tracing/native.test.ts +170 -0
- package/tracing/tracing.test.ts +317 -0
- package/dist/internal/otel/request_context.d.ts +0 -11
- package/dist/internal/otel/request_context.js +0 -20
- package/dist/internal/otel/request_context.js.map +0 -1
- package/dist/internal/reqtrack/mod.d.ts +0 -6
- package/dist/internal/reqtrack/mod.js +0 -28
- package/dist/internal/reqtrack/mod.js.map +0 -1
- package/dist/req_meta.d.ts +0 -47
- package/dist/req_meta.js +0 -26
- package/dist/req_meta.js.map +0 -1
- package/internal/otel/request_context.ts +0 -32
- package/internal/reqtrack/mod.ts +0 -39
- package/req_meta.ts +0 -75
package/pubsub/mod.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
export {Topic, OrderedTopic} from './topic';
|
|
2
|
-
export type {
|
|
1
|
+
export {Topic, OrderedTopic, MaxBatchSize, PublishBatchError} from './topic';
|
|
2
|
+
export type {
|
|
3
|
+
TopicConfig,
|
|
4
|
+
OrderedTopicConfig,
|
|
5
|
+
DeliveryGuarantee,
|
|
6
|
+
BatchMessage,
|
|
7
|
+
OrderedBatchMessage,
|
|
8
|
+
PublishBatchResult,
|
|
9
|
+
} from './topic';
|
|
3
10
|
|
|
4
|
-
export {Subscription} from './subscription';
|
|
11
|
+
export {Subscription, NoRetries, InfiniteRetries} from './subscription';
|
|
5
12
|
export type {SubscriptionConfig, RetryPolicy, Message} from './subscription';
|
|
6
13
|
|
|
7
14
|
export type {
|
package/pubsub/pubsub.test.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import {beforeEach, expect, mock, test} from 'bun:test';
|
|
2
2
|
import type {Message, SubscriptionConfig} from './subscription';
|
|
3
|
+
import type {OrderedPublisher, Publisher} from './refs';
|
|
3
4
|
|
|
4
5
|
const publish = mock(async (..._args: unknown[]) => 'message-id');
|
|
6
|
+
type Outcome = {id?: string; error?: string};
|
|
7
|
+
const publishBatch = mock(
|
|
8
|
+
async (entries: unknown[], _source: unknown): Promise<Outcome[]> =>
|
|
9
|
+
entries.map((_, i) => ({id: `id-${i}`})),
|
|
10
|
+
);
|
|
5
11
|
const subscribe = mock(() => new Promise<void>(() => {}));
|
|
6
12
|
const errors = mock((..._args: unknown[]) => {});
|
|
7
|
-
let dispatch: (
|
|
13
|
+
let dispatch: (message: unknown) => Promise<void>;
|
|
8
14
|
mock.module('../internal/runtime/mod', () => ({
|
|
9
15
|
RT: {
|
|
10
|
-
pubsubTopic: () => ({publish}),
|
|
16
|
+
pubsubTopic: () => ({publish, publishBatch}),
|
|
11
17
|
pubsubSubscription: (cfg: {handler: typeof dispatch}) => {
|
|
12
18
|
dispatch = cfg.handler;
|
|
13
19
|
return {subscribe};
|
|
@@ -17,15 +23,21 @@ mock.module('../internal/runtime/mod', () => ({
|
|
|
17
23
|
mock.module('../internal/otel/index', () => ({
|
|
18
24
|
activeCtxBytes: () => undefined,
|
|
19
25
|
}));
|
|
20
|
-
mock.module('../internal/reqtrack/mod', () => ({
|
|
21
|
-
runWithRequest: (_req: unknown, fn: () => unknown) => fn(),
|
|
22
|
-
}));
|
|
23
26
|
mock.module('../log/index', () => ({default: {error: errors}}));
|
|
24
|
-
const {Topic, OrderedTopic} =
|
|
25
|
-
|
|
27
|
+
const {Topic, OrderedTopic, PublishBatchError, MaxBatchSize} =
|
|
28
|
+
await import('./topic');
|
|
29
|
+
const {Subscription, NoRetries, InfiniteRetries} =
|
|
30
|
+
await import('./subscription');
|
|
31
|
+
const {Field} = await import('../reqctx/mod');
|
|
32
|
+
const {trace} = await import('@opentelemetry/api');
|
|
33
|
+
const {activeContext} = await import('../internal/otel/context');
|
|
26
34
|
|
|
27
35
|
beforeEach(() => {
|
|
28
36
|
publish.mockClear();
|
|
37
|
+
publishBatch.mockClear();
|
|
38
|
+
publishBatch.mockImplementation(async entries =>
|
|
39
|
+
entries.map((_, i) => ({id: `id-${i}`})),
|
|
40
|
+
);
|
|
29
41
|
subscribe.mockClear();
|
|
30
42
|
subscribe.mockImplementation(() => new Promise<void>(() => {}));
|
|
31
43
|
errors.mockClear();
|
|
@@ -93,6 +105,119 @@ test('ordered topics reject empty keys before calling the native publisher', ()
|
|
|
93
105
|
expect(publish).not.toHaveBeenCalled();
|
|
94
106
|
});
|
|
95
107
|
|
|
108
|
+
test('a batch is encoded as single messages are and answers with ids in order', async () => {
|
|
109
|
+
const topic = new Topic<unknown>('events');
|
|
110
|
+
const ids = await topic.publishBatch([
|
|
111
|
+
{data: {id: 1}, attributes: {a: 'b'}},
|
|
112
|
+
{data: Buffer.from([0, 255])},
|
|
113
|
+
]);
|
|
114
|
+
expect(ids).toEqual(['id-0', 'id-1']);
|
|
115
|
+
expect(publishBatch.mock.lastCall).toEqual([
|
|
116
|
+
[
|
|
117
|
+
{data: Buffer.from('{"id":1}'), attributes: {a: 'b'}},
|
|
118
|
+
{data: Buffer.from([0, 255]), attributes: undefined},
|
|
119
|
+
],
|
|
120
|
+
undefined,
|
|
121
|
+
]);
|
|
122
|
+
|
|
123
|
+
const ordered = OrderedTopic.named<string>('changes');
|
|
124
|
+
await ordered.publishBatch([
|
|
125
|
+
{data: 'hello', orderingKey: 'user-1'},
|
|
126
|
+
{data: 'bye', orderingKey: 'user-2', attributes: {a: 'b'}},
|
|
127
|
+
]);
|
|
128
|
+
expect(publishBatch.mock.lastCall?.[0]).toEqual([
|
|
129
|
+
{
|
|
130
|
+
data: Buffer.from('"hello"'),
|
|
131
|
+
attributes: undefined,
|
|
132
|
+
orderingKey: 'user-1',
|
|
133
|
+
},
|
|
134
|
+
{data: Buffer.from('"bye"'), attributes: {a: 'b'}, orderingKey: 'user-2'},
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('a batch some of whose messages were not published rejects with each outcome in order', async () => {
|
|
139
|
+
publishBatch.mockImplementationOnce(async () => [
|
|
140
|
+
{id: 'id-0'},
|
|
141
|
+
{error: 'failed to publish: boom'},
|
|
142
|
+
{id: 'id-2'},
|
|
143
|
+
{error: 'failed to publish: not sent'},
|
|
144
|
+
]);
|
|
145
|
+
const topic = new Topic<number>('events');
|
|
146
|
+
const err = await topic
|
|
147
|
+
.publishBatch([{data: 0}, {data: 1}, {data: 2}, {data: 3}])
|
|
148
|
+
.catch((e: unknown) => e);
|
|
149
|
+
expect(err).toBeInstanceOf(PublishBatchError);
|
|
150
|
+
expect(err).toBeInstanceOf(Error);
|
|
151
|
+
const batchErr = err as InstanceType<typeof PublishBatchError>;
|
|
152
|
+
expect(batchErr.name).toBe('PublishBatchError');
|
|
153
|
+
expect(batchErr.message).toBe(
|
|
154
|
+
'2 of the 4 messages were not published: failed to publish: boom',
|
|
155
|
+
);
|
|
156
|
+
expect(
|
|
157
|
+
batchErr.results.map(r => ('id' in r ? r.id : r.error.message)),
|
|
158
|
+
).toEqual([
|
|
159
|
+
'id-0',
|
|
160
|
+
'failed to publish: boom',
|
|
161
|
+
'id-2',
|
|
162
|
+
'failed to publish: not sent',
|
|
163
|
+
]);
|
|
164
|
+
expect(batchErr.results[1]).toEqual({error: expect.any(Error)});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('a batch refused whole rejects with the runtime error', async () => {
|
|
168
|
+
publishBatch.mockImplementationOnce(() =>
|
|
169
|
+
Promise.reject(new Error('a batch has at most 1000 messages')),
|
|
170
|
+
);
|
|
171
|
+
await expect(
|
|
172
|
+
new Topic<number>('events').publishBatch([{data: 1}]),
|
|
173
|
+
).rejects.toThrow('at most 1000');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('an empty batch publishes nothing', async () => {
|
|
177
|
+
expect(await new Topic<number>('events').publishBatch([])).toEqual([]);
|
|
178
|
+
expect(await OrderedTopic.named<number>('changes').publishBatch([])).toEqual(
|
|
179
|
+
[],
|
|
180
|
+
);
|
|
181
|
+
expect(publishBatch).not.toHaveBeenCalled();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('an ordered batch with a message without a key is refused whole', async () => {
|
|
185
|
+
const topic = OrderedTopic.named<string>('changes');
|
|
186
|
+
for (const orderingKey of ['', undefined as unknown as string]) {
|
|
187
|
+
await expect(
|
|
188
|
+
topic.publishBatch([
|
|
189
|
+
{data: 'a', orderingKey: 'user-1'},
|
|
190
|
+
{data: 'b', orderingKey},
|
|
191
|
+
]),
|
|
192
|
+
).rejects.toThrow('message 1: orderingKey must be a non-empty string');
|
|
193
|
+
}
|
|
194
|
+
expect(publishBatch).not.toHaveBeenCalled();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('a batch that cannot be encoded is refused whole', async () => {
|
|
198
|
+
await expect(
|
|
199
|
+
new Topic<unknown>('events').publishBatch([{data: 1}, {data: undefined}]),
|
|
200
|
+
).rejects.toThrow('JSON or bytes');
|
|
201
|
+
expect(publishBatch).not.toHaveBeenCalled();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('the batch limit is the core one', () => {
|
|
205
|
+
expect(MaxBatchSize).toBe(1000);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('a reference publishes batches as its topic does', async () => {
|
|
209
|
+
const ref = new Topic<number>('events').ref<Publisher<number>>();
|
|
210
|
+
expect(ref.name).toBe('events');
|
|
211
|
+
expect(await ref.publishBatch([{data: 1}])).toEqual(['id-0']);
|
|
212
|
+
const ordered = new OrderedTopic<number>('changes').ref<
|
|
213
|
+
OrderedPublisher<number>
|
|
214
|
+
>();
|
|
215
|
+
expect(ordered.name).toBe('changes');
|
|
216
|
+
expect(await ordered.publishBatch([{data: 1, orderingKey: 'k'}])).toEqual([
|
|
217
|
+
'id-0',
|
|
218
|
+
]);
|
|
219
|
+
});
|
|
220
|
+
|
|
96
221
|
test('automatically starts subscriptions and delivers decoded JSON with original bytes and metadata', async () => {
|
|
97
222
|
let received!: Message<{id: number}>;
|
|
98
223
|
new Subscription(new OrderedTopic<{id: number}>('events'), 'handler', {
|
|
@@ -102,7 +227,7 @@ test('automatically starts subscriptions and delivers decoded JSON with original
|
|
|
102
227
|
});
|
|
103
228
|
expect(subscribe).toHaveBeenCalledTimes(1);
|
|
104
229
|
const rawData = Buffer.from('{"id":123}');
|
|
105
|
-
await dispatch(
|
|
230
|
+
await dispatch(native(rawData, 'customer-123'));
|
|
106
231
|
expect(received).toEqual({
|
|
107
232
|
...native(rawData),
|
|
108
233
|
data: {id: 123},
|
|
@@ -120,7 +245,7 @@ test('raw subscriptions preserve arbitrary non-JSON bytes', async () => {
|
|
|
120
245
|
},
|
|
121
246
|
});
|
|
122
247
|
const bytes = Buffer.from([0, 255, 128]);
|
|
123
|
-
await dispatch(
|
|
248
|
+
await dispatch(native(bytes));
|
|
124
249
|
expect(received.data).toBe(bytes);
|
|
125
250
|
expect(received.rawData).toBe(bytes);
|
|
126
251
|
expect(received.orderingKey).toBeNull();
|
|
@@ -142,13 +267,13 @@ test('invalid JSON and validation failures reject delivery before invoking the h
|
|
|
142
267
|
},
|
|
143
268
|
handler,
|
|
144
269
|
});
|
|
145
|
-
expect(dispatch(
|
|
146
|
-
expect(dispatch(
|
|
147
|
-
expect(dispatch(
|
|
270
|
+
expect(dispatch(native(Buffer.from('not json')))).rejects.toThrow();
|
|
271
|
+
expect(dispatch(native(Buffer.from([34, 255, 34])))).rejects.toThrow();
|
|
272
|
+
expect(dispatch(native(Buffer.from('{"id":"bad"}')))).rejects.toThrow(
|
|
148
273
|
'invalid id',
|
|
149
274
|
);
|
|
150
275
|
expect(handler).not.toHaveBeenCalled();
|
|
151
|
-
await dispatch(
|
|
276
|
+
await dispatch(native(Buffer.from('{"id":123}')));
|
|
152
277
|
expect(handler).toHaveBeenCalledTimes(1);
|
|
153
278
|
});
|
|
154
279
|
|
|
@@ -158,7 +283,7 @@ test('handler errors propagate to the runtime for nack', async () => {
|
|
|
158
283
|
throw new Error('handler failed');
|
|
159
284
|
},
|
|
160
285
|
});
|
|
161
|
-
expect(dispatch(
|
|
286
|
+
expect(dispatch(native(Buffer.from('null')))).rejects.toThrow(
|
|
162
287
|
'handler failed',
|
|
163
288
|
);
|
|
164
289
|
});
|
|
@@ -180,7 +305,100 @@ test('consumer failures are logged and normal completion does not restart', asyn
|
|
|
180
305
|
expect(subscribe).toHaveBeenCalledTimes(2);
|
|
181
306
|
});
|
|
182
307
|
|
|
308
|
+
test('a subscription and its topic have their names', () => {
|
|
309
|
+
const topic = new Topic<unknown>('events');
|
|
310
|
+
const sub = new Subscription(topic, 'audit', {handler: async () => {}});
|
|
311
|
+
expect(sub.name).toBe('audit');
|
|
312
|
+
expect(topic.name).toBe('events');
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test("the retry sentinels are the core's", () => {
|
|
316
|
+
expect(NoRetries).toBe(-2);
|
|
317
|
+
expect(InfiniteRetries).toBe(-1);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test('the package exports the batch and retry names', async () => {
|
|
321
|
+
const mod = await import('./mod');
|
|
322
|
+
expect(mod.MaxBatchSize).toBe(MaxBatchSize);
|
|
323
|
+
expect(mod.PublishBatchError).toBe(PublishBatchError);
|
|
324
|
+
expect(mod.NoRetries).toBe(NoRetries);
|
|
325
|
+
expect(mod.InfiniteRetries).toBe(InfiniteRetries);
|
|
326
|
+
});
|
|
327
|
+
|
|
183
328
|
// Byte subscriptions must explicitly opt out of JSON decoding.
|
|
184
329
|
// @ts-expect-error raw subscriptions require encoding: "raw"
|
|
185
330
|
const invalidConfig: SubscriptionConfig<Buffer> = {handler: async () => {}};
|
|
186
331
|
void invalidConfig;
|
|
332
|
+
|
|
333
|
+
test('a message carries the Request Context fields of the context it is published in', async () => {
|
|
334
|
+
const user = new Field<{id: string}>('pubsub-test-user', {pubsub: true});
|
|
335
|
+
const topic = new Topic<string>('events');
|
|
336
|
+
await user.with({id: 'u1'}, () =>
|
|
337
|
+
topic.publish('hello', {'encore-ctx-forged': 'e30', a: 'b'}),
|
|
338
|
+
);
|
|
339
|
+
expect(publish.mock.lastCall?.[1]).toEqual({
|
|
340
|
+
a: 'b',
|
|
341
|
+
'encore-ctx-pubsub-test-user':
|
|
342
|
+
Buffer.from('{"id":"u1"}').toString('base64url'),
|
|
343
|
+
});
|
|
344
|
+
// Without fields, the attributes are passed as they are.
|
|
345
|
+
await topic.publish('hello');
|
|
346
|
+
expect(publish.mock.lastCall?.[1]).toBeUndefined();
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
test('each message of a batch carries the Request Context fields', async () => {
|
|
350
|
+
const user = new Field<{id: string}>('pubsub-test-user', {pubsub: true});
|
|
351
|
+
const field = Buffer.from('{"id":"u1"}').toString('base64url');
|
|
352
|
+
await user.with({id: 'u1'}, async () => {
|
|
353
|
+
await new Topic<string>('events').publishBatch([
|
|
354
|
+
{data: 'a', attributes: {'encore-ctx-forged': 'e30', a: 'b'}},
|
|
355
|
+
{data: 'b'},
|
|
356
|
+
]);
|
|
357
|
+
const attributes = () =>
|
|
358
|
+
(
|
|
359
|
+
publishBatch.mock.lastCall?.[0] as
|
|
360
|
+
{attributes?: Record<string, string>}[] | undefined
|
|
361
|
+
)?.map(e => e.attributes);
|
|
362
|
+
expect(attributes()).toEqual([
|
|
363
|
+
{a: 'b', 'encore-ctx-pubsub-test-user': field},
|
|
364
|
+
{'encore-ctx-pubsub-test-user': field},
|
|
365
|
+
]);
|
|
366
|
+
await OrderedTopic.named<string>('changes').publishBatch([
|
|
367
|
+
{data: 'a', orderingKey: 'k'},
|
|
368
|
+
]);
|
|
369
|
+
expect(attributes()).toEqual([{'encore-ctx-pubsub-test-user': field}]);
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("a handler runs in its message's span, with the fields it carries", async () => {
|
|
374
|
+
const user = Field.named<{id: string}>('pubsub-test-user');
|
|
375
|
+
let seen: unknown;
|
|
376
|
+
new Subscription(new Topic<string>('events'), 'handler', {
|
|
377
|
+
handler: async message => {
|
|
378
|
+
await new Promise(r => setTimeout(r, 1));
|
|
379
|
+
seen = {
|
|
380
|
+
user: user.get(),
|
|
381
|
+
attributes: message.attributes,
|
|
382
|
+
traceId: trace.getSpanContext(activeContext())?.traceId,
|
|
383
|
+
};
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
const ctx = Buffer.alloc(25, 2);
|
|
387
|
+
ctx[24] = 1;
|
|
388
|
+
await dispatch({
|
|
389
|
+
...native(Buffer.from('"hi"')),
|
|
390
|
+
attributes: {
|
|
391
|
+
customer: '123',
|
|
392
|
+
'encore-ctx-pubsub-test-user':
|
|
393
|
+
Buffer.from('{"id":"u2"}').toString('base64url'),
|
|
394
|
+
'encore-ctx-Bad': 'e30',
|
|
395
|
+
},
|
|
396
|
+
spanContext: {ctx, tracestate: 'encore=rc'},
|
|
397
|
+
});
|
|
398
|
+
expect(seen).toEqual({
|
|
399
|
+
user: {id: 'u2'},
|
|
400
|
+
attributes: {customer: '123'},
|
|
401
|
+
traceId: ctx.toString('hex', 0, 16),
|
|
402
|
+
});
|
|
403
|
+
expect(user.get()).toBeUndefined();
|
|
404
|
+
});
|
package/pubsub/refs.ts
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
+
import type {BatchMessage, OrderedBatchMessage} from './topic';
|
|
2
|
+
|
|
1
3
|
export abstract class TopicPerms {
|
|
2
4
|
private topicPerms(): void {}
|
|
3
5
|
}
|
|
4
6
|
|
|
5
7
|
export abstract class Publisher<Msg> extends TopicPerms {
|
|
8
|
+
/** The topic's name. */
|
|
9
|
+
abstract readonly name: string;
|
|
10
|
+
|
|
6
11
|
abstract publish(
|
|
7
12
|
msg: Msg,
|
|
8
13
|
attributes?: Record<string, string>,
|
|
9
14
|
): Promise<string>;
|
|
15
|
+
|
|
16
|
+
abstract publishBatch(
|
|
17
|
+
messages: readonly BatchMessage<Msg>[],
|
|
18
|
+
): Promise<string[]>;
|
|
10
19
|
}
|
|
11
20
|
|
|
12
21
|
export abstract class OrderedTopicPerms {
|
|
@@ -14,9 +23,16 @@ export abstract class OrderedTopicPerms {
|
|
|
14
23
|
}
|
|
15
24
|
|
|
16
25
|
export abstract class OrderedPublisher<Msg> extends OrderedTopicPerms {
|
|
26
|
+
/** The topic's name. */
|
|
27
|
+
abstract readonly name: string;
|
|
28
|
+
|
|
17
29
|
abstract publish(
|
|
18
30
|
msg: Msg,
|
|
19
31
|
orderingKey: string,
|
|
20
32
|
attributes?: Record<string, string>,
|
|
21
33
|
): Promise<string>;
|
|
34
|
+
|
|
35
|
+
abstract publishBatch(
|
|
36
|
+
messages: readonly OrderedBatchMessage<Msg>[],
|
|
37
|
+
): Promise<string[]>;
|
|
22
38
|
}
|
package/pubsub/subscription.ts
CHANGED
|
@@ -1,29 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createTraceState,
|
|
3
|
+
ROOT_CONTEXT,
|
|
4
|
+
trace,
|
|
5
|
+
type Context,
|
|
6
|
+
} from '@opentelemetry/api';
|
|
1
7
|
import log from '../log/index';
|
|
2
|
-
import {
|
|
8
|
+
import {withContext} from '../internal/otel/context';
|
|
9
|
+
import {fromMessageAttributes, withFields} from '../internal/reqfields';
|
|
3
10
|
import {DurationString} from '../types/mod';
|
|
4
11
|
import {OrderedTopic, Topic} from './topic';
|
|
5
12
|
import * as runtime from '../internal/runtime/mod';
|
|
13
|
+
import {testMode, type Scope} from '../internal/testing/scope';
|
|
6
14
|
|
|
7
15
|
/**
|
|
8
16
|
* A delivered payload with its original bytes and provider metadata.
|
|
9
17
|
*
|
|
10
|
-
*
|
|
11
|
-
* and
|
|
18
|
+
* The handler runs in the message's context: its spans and calls continue
|
|
19
|
+
* the trace the message was published in, and the Request Context fields it
|
|
20
|
+
* carries are set (`@encore.dev/sdk/reqctx`).
|
|
12
21
|
*/
|
|
13
22
|
export interface Message<T = unknown> {
|
|
23
|
+
/** The payload, decoded as the subscription says. */
|
|
14
24
|
data: T;
|
|
25
|
+
/** The payload as it was published. */
|
|
15
26
|
rawData: Buffer;
|
|
27
|
+
/**
|
|
28
|
+
* The unique id of the message: the one `publish` returned. It stays the
|
|
29
|
+
* same across delivery attempts.
|
|
30
|
+
*/
|
|
16
31
|
id: string;
|
|
32
|
+
/**
|
|
33
|
+
* The attributes it was published with, without the `encore-ctx-*` ones
|
|
34
|
+
* carrying Request Context fields, which the handler reads from its
|
|
35
|
+
* context.
|
|
36
|
+
*/
|
|
17
37
|
attributes: Record<string, string>;
|
|
38
|
+
/** The delivery attempt, starting at 1. */
|
|
18
39
|
deliveryAttempt: number;
|
|
40
|
+
/** When it was published. */
|
|
19
41
|
publishTime: Date;
|
|
42
|
+
/** The key it was published with on an ordered topic; `null` on an
|
|
43
|
+
* unordered one. */
|
|
20
44
|
orderingKey: string | null;
|
|
21
45
|
}
|
|
22
46
|
|
|
23
47
|
export class Subscription<Msg> {
|
|
24
48
|
private readonly topic: Topic<Msg> | OrderedTopic<Msg>;
|
|
25
|
-
|
|
26
|
-
|
|
49
|
+
/** The subscription's name, unique to its topic. */
|
|
50
|
+
readonly name: string;
|
|
51
|
+
private readonly impl?: runtime.PubSubSubscription;
|
|
52
|
+
/** Runs the handler on a message, as it is delivered. */
|
|
53
|
+
private readonly dispatch: (msg: Delivered) => Promise<void>;
|
|
27
54
|
|
|
28
55
|
constructor(
|
|
29
56
|
topic: Topic<Msg> | OrderedTopic<Msg>,
|
|
@@ -33,8 +60,9 @@ export class Subscription<Msg> {
|
|
|
33
60
|
this.topic = topic;
|
|
34
61
|
this.name = name;
|
|
35
62
|
|
|
36
|
-
|
|
37
|
-
|
|
63
|
+
this.dispatch = (msg: Delivered) => {
|
|
64
|
+
const {fields, rest} = fromMessageAttributes(msg.attributes);
|
|
65
|
+
return withContext(handlerContext(msg, fields), async () => {
|
|
38
66
|
const rawData = msg.data;
|
|
39
67
|
const data =
|
|
40
68
|
cfg.encoding === 'raw'
|
|
@@ -46,30 +74,113 @@ export class Subscription<Msg> {
|
|
|
46
74
|
data: cfg.decode ? cfg.decode(data) : data,
|
|
47
75
|
rawData,
|
|
48
76
|
id: msg.id,
|
|
49
|
-
attributes:
|
|
77
|
+
attributes: rest,
|
|
50
78
|
deliveryAttempt: msg.deliveryAttempt,
|
|
51
79
|
publishTime: msg.publishTime,
|
|
52
80
|
orderingKey: msg.orderingKey ?? null,
|
|
53
81
|
});
|
|
54
82
|
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// A test hands the subscription what it published itself, with
|
|
86
|
+
// `deliver`: nothing is delivered to it otherwise.
|
|
87
|
+
if (testMode) return;
|
|
55
88
|
|
|
56
89
|
this.impl = runtime.RT.pubsubSubscription({
|
|
57
90
|
topicName: topic.name,
|
|
58
91
|
subscriptionName: name,
|
|
59
|
-
handler,
|
|
92
|
+
handler: this.dispatch,
|
|
60
93
|
});
|
|
61
94
|
|
|
62
95
|
this.startSubscribing();
|
|
63
96
|
}
|
|
64
97
|
|
|
65
98
|
private startSubscribing() {
|
|
66
|
-
void this.impl
|
|
99
|
+
void this.impl!.subscribe().catch((error: unknown) => {
|
|
67
100
|
log.error(
|
|
68
101
|
error,
|
|
69
102
|
`Encore subscription ${this.topic.name}/${this.name} failed`,
|
|
70
103
|
);
|
|
71
104
|
});
|
|
72
105
|
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Runs the handler on each message the test `scope` published to the
|
|
109
|
+
* topic that the subscription has not handled and its filter matches, in
|
|
110
|
+
* order, returning how many it handled. Stops at the first the handler
|
|
111
|
+
* fails, rejecting with its error: that message is handed over again next
|
|
112
|
+
* time, as its next attempt.
|
|
113
|
+
*
|
|
114
|
+
* @internal
|
|
115
|
+
*/
|
|
116
|
+
static async deliverIn(
|
|
117
|
+
sub: Subscription<unknown>,
|
|
118
|
+
scope: Scope,
|
|
119
|
+
maxDeliveries: number,
|
|
120
|
+
): Promise<number> {
|
|
121
|
+
const topic = sub.topic.name;
|
|
122
|
+
let handled = 0;
|
|
123
|
+
for (;;) {
|
|
124
|
+
const delivery = scope.native.nextDelivery(topic, sub.name);
|
|
125
|
+
if (delivery === null) return handled;
|
|
126
|
+
if (handled === maxDeliveries) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`subscription ${JSON.stringify(sub.name)} handled ${maxDeliveries} messages in one ` +
|
|
129
|
+
`delivery and was still handed more: does its handler publish to its own topic ` +
|
|
130
|
+
`${JSON.stringify(topic)} every time?`,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
const msg = delivery.message;
|
|
134
|
+
try {
|
|
135
|
+
await sub.dispatch({
|
|
136
|
+
id: msg.id,
|
|
137
|
+
data: msg.data,
|
|
138
|
+
attributes: msg.attributes,
|
|
139
|
+
orderingKey: msg.orderingKey,
|
|
140
|
+
deliveryAttempt: delivery.attempt,
|
|
141
|
+
publishTime: new Date(msg.publishTime),
|
|
142
|
+
});
|
|
143
|
+
} catch (err) {
|
|
144
|
+
scope.native.settleDelivery(topic, sub.name, false);
|
|
145
|
+
throw err;
|
|
146
|
+
}
|
|
147
|
+
scope.native.settleDelivery(topic, sub.name, true);
|
|
148
|
+
handled++;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** A message to hand a subscription's handler: the runtime's, or one a test
|
|
154
|
+
* published. */
|
|
155
|
+
type Delivered = Pick<
|
|
156
|
+
runtime.PubSubMessage,
|
|
157
|
+
'id' | 'data' | 'attributes' | 'deliveryAttempt' | 'publishTime'
|
|
158
|
+
> & {
|
|
159
|
+
orderingKey?: string | null;
|
|
160
|
+
spanContext?: runtime.PubSubMessage['spanContext'];
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The context a message's handler runs in: its delivery span's, so the
|
|
165
|
+
* handler's spans and calls continue the trace and carry Encore's propagation
|
|
166
|
+
* mark, with the Request Context fields the message carries.
|
|
167
|
+
*/
|
|
168
|
+
function handlerContext(
|
|
169
|
+
msg: Delivered,
|
|
170
|
+
fields: Record<string, string>,
|
|
171
|
+
): Context {
|
|
172
|
+
let ctx = ROOT_CONTEXT;
|
|
173
|
+
const sc = msg.spanContext;
|
|
174
|
+
if (sc) {
|
|
175
|
+
ctx = trace.setSpanContext(ctx, {
|
|
176
|
+
traceId: sc.ctx.toString('hex', 0, 16),
|
|
177
|
+
spanId: sc.ctx.toString('hex', 16, 24),
|
|
178
|
+
traceFlags: sc.ctx[24]!,
|
|
179
|
+
traceState: sc.tracestate ? createTraceState(sc.tracestate) : undefined,
|
|
180
|
+
isRemote: true,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return Object.keys(fields).length === 0 ? ctx : withFields(fields, ctx);
|
|
73
184
|
}
|
|
74
185
|
|
|
75
186
|
/**
|
|
@@ -125,8 +236,11 @@ export interface SubscriptionOptions<Msg> {
|
|
|
125
236
|
maxConcurrency?: number;
|
|
126
237
|
|
|
127
238
|
/**
|
|
128
|
-
* AckDeadline is
|
|
129
|
-
*
|
|
239
|
+
* AckDeadline is how long a message is kept for its handler before
|
|
240
|
+
* it's delivered again. While the handler runs, a pulled message's
|
|
241
|
+
* lease is extended for an hour, or for the ack deadline when that is
|
|
242
|
+
* longer; a message pushed to the service (a GCP push subscription)
|
|
243
|
+
* has the ack deadline.
|
|
130
244
|
*
|
|
131
245
|
* Default is 30 seconds, however the ack deadline must be at least
|
|
132
246
|
* 1 second.
|
|
@@ -146,6 +260,50 @@ export interface SubscriptionOptions<Msg> {
|
|
|
146
260
|
* the subscriber returns an error
|
|
147
261
|
*/
|
|
148
262
|
retryPolicy?: RetryPolicy;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Which of the topic's messages the subscription receives: those whose
|
|
266
|
+
* attributes this condition matches. Messages it does not match are never
|
|
267
|
+
* delivered to the subscription. By default it receives them all.
|
|
268
|
+
*
|
|
269
|
+
* It is written in a subset of the [expr language](https://expr-lang.org),
|
|
270
|
+
* which every cloud Encore deploys to can filter on:
|
|
271
|
+
*
|
|
272
|
+
* ```ts
|
|
273
|
+
* filter: `attrs.region in ["eu-west-1", "eu-north-1"] and not (attrs.kind startsWith "test-")`
|
|
274
|
+
* ```
|
|
275
|
+
*
|
|
276
|
+
* - `attrs.region == "eu"`, `!=`, `in [...]`, `not in [...]`, and
|
|
277
|
+
* `startsWith "eu-"`; `attrs["iana.org-lang"]` for a name with other
|
|
278
|
+
* characters than letters, digits and `_`.
|
|
279
|
+
* - `"region" in attrs` and `"region" not in attrs`: whether a message has
|
|
280
|
+
* the attribute. A test of a value does not hold for a message without
|
|
281
|
+
* the attribute, and a negated one does.
|
|
282
|
+
* - `and` (`&&`), `or` (`||`), `not (...)` (`!(...)`) and parentheses.
|
|
283
|
+
*
|
|
284
|
+
* Encore reads it from the source, so it is a string written out, and
|
|
285
|
+
* checks it fits the clouds' limits.
|
|
286
|
+
*/
|
|
287
|
+
filter?: string;
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* The Worker of the service that consumes the subscription: a long-running
|
|
291
|
+
* process of the service that serves no requests, declared in the `workers`
|
|
292
|
+
* section of its `encore.service.json`. Only that process takes its
|
|
293
|
+
* messages; another process of the service that creates the subscription
|
|
294
|
+
* takes none. By default the service's main process consumes it.
|
|
295
|
+
*
|
|
296
|
+
* Encore reads it from the source, so it is a string written out.
|
|
297
|
+
*/
|
|
298
|
+
worker?: string;
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Tags that classify the subscription, for policies to read: written out as
|
|
302
|
+
* string literals, as in `{data: "pci"}`. Keys and values are lowercase
|
|
303
|
+
* letters, digits, `_` and `-`, at most 63 characters; a key starts with a
|
|
304
|
+
* letter, and not with `encore`, which is reserved. At most 32 tags.
|
|
305
|
+
*/
|
|
306
|
+
tags?: Record<string, string>;
|
|
149
307
|
}
|
|
150
308
|
|
|
151
309
|
/**
|
|
@@ -171,10 +329,30 @@ export interface RetryPolicy {
|
|
|
171
329
|
maxBackoff?: DurationString;
|
|
172
330
|
|
|
173
331
|
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
332
|
+
* How many times a message is retried after its handler fails before it is
|
|
333
|
+
* forwarded to the dead-letter queue. Defaults to 100.
|
|
334
|
+
*
|
|
335
|
+
* - `n >= 0`: retried up to `n` times. `0` retries it not at all: it is
|
|
336
|
+
* dead-lettered as soon as its handler fails once.
|
|
337
|
+
* - `-1`, {@link InfiniteRetries}: retried for as long as it fails, never
|
|
338
|
+
* dead-lettered.
|
|
339
|
+
* - `-2`, {@link NoRetries}: not retried, as `0`.
|
|
340
|
+
*
|
|
341
|
+
* Anything below `-2` is rejected. Encore reads it from the source, so it
|
|
342
|
+
* is a number written out, or one of the two constants imported from
|
|
343
|
+
* `@encore.dev/sdk/pubsub`.
|
|
178
344
|
*/
|
|
179
345
|
maxRetries?: number;
|
|
180
346
|
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* The `maxRetries` that retries a message not at all: it is forwarded to the
|
|
350
|
+
* dead-letter queue as soon as its handler fails once.
|
|
351
|
+
*/
|
|
352
|
+
export const NoRetries = -2;
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* The `maxRetries` that retries a message for as long as its handler fails:
|
|
356
|
+
* it is never forwarded to the dead-letter queue.
|
|
357
|
+
*/
|
|
358
|
+
export const InfiniteRetries = -1;
|