@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/topic.test.ts
CHANGED
|
@@ -20,6 +20,15 @@ const outcomes = (() => {
|
|
|
20
20
|
unkeyed: await outcome(Topic.named("changes").publish({ id: "1" })),
|
|
21
21
|
// An unordered topic refuses one.
|
|
22
22
|
unordered: await outcome(OrderedTopic.named("events").publish({ id: "1" }, "user-1")),
|
|
23
|
+
// Each message of a batch answers for itself, in order.
|
|
24
|
+
batch: await Topic.named("changes")
|
|
25
|
+
.publishBatch([{ data: { id: "1" } }, { data: { id: "2" } }])
|
|
26
|
+
.then(() => [], (e) => e.results.map((r) => r.error.message)),
|
|
27
|
+
emptyBatch: await Topic.named("events").publishBatch([]),
|
|
28
|
+
// More than 1000 is refused whole.
|
|
29
|
+
overLimit: await outcome(
|
|
30
|
+
Topic.named("events").publishBatch(Array.from({ length: 1001 }, () => ({ data: 1 }))),
|
|
31
|
+
),
|
|
23
32
|
}));
|
|
24
33
|
`;
|
|
25
34
|
const proc = Bun.spawnSync({
|
|
@@ -56,6 +65,23 @@ describe('ordered topics', () => {
|
|
|
56
65
|
});
|
|
57
66
|
});
|
|
58
67
|
|
|
68
|
+
describe('batches', () => {
|
|
69
|
+
test('each message is held to its topic, and answered for in order', () => {
|
|
70
|
+
expect(outcomes.batch).toHaveLength(2);
|
|
71
|
+
for (const error of outcomes.batch) {
|
|
72
|
+
expect(error).toContain('needs an ordering key');
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('an empty batch publishes nothing', () => {
|
|
77
|
+
expect(outcomes.emptyBatch).toEqual([]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('more than 1000 messages are refused whole', () => {
|
|
81
|
+
expect(outcomes.overLimit).toContain('at most 1000');
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
59
85
|
/**
|
|
60
86
|
* A serialized `encore.runtime.v1.RuntimeConfig` with an ordered topic,
|
|
61
87
|
* base64-encoded as the runtime expects it. Hand-encoded, as in
|
package/pubsub/topic.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {activeCtxBytes} from '../internal/otel/index';
|
|
2
|
+
import {messageAttributes} from '../internal/reqfields';
|
|
2
3
|
import * as runtime from '../internal/runtime/mod';
|
|
3
|
-
import {
|
|
4
|
+
import {currentScope, testMode, type Scope} from '../internal/testing/scope';
|
|
5
|
+
import {encodePayload, rawPayloads} from './encoding';
|
|
4
6
|
import {
|
|
5
7
|
OrderedPublisher,
|
|
6
8
|
OrderedTopicPerms,
|
|
@@ -8,6 +10,45 @@ import {
|
|
|
8
10
|
TopicPerms,
|
|
9
11
|
} from './refs';
|
|
10
12
|
|
|
13
|
+
/** The most messages one `publishBatch` call takes. */
|
|
14
|
+
export const MaxBatchSize = 1000;
|
|
15
|
+
|
|
16
|
+
/** A message to publish with `Topic.publishBatch`. */
|
|
17
|
+
export interface BatchMessage<Msg> {
|
|
18
|
+
/** The message, encoded as `publish` encodes it. */
|
|
19
|
+
data: Msg;
|
|
20
|
+
/** Attributes delivered alongside the payload. */
|
|
21
|
+
attributes?: Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A message to publish with `OrderedTopic.publishBatch`. */
|
|
25
|
+
export interface OrderedBatchMessage<Msg> extends BatchMessage<Msg> {
|
|
26
|
+
/** The message's ordering key, which cannot be empty. */
|
|
27
|
+
orderingKey: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** What became of one message of a batch: its id, or why it was not published. */
|
|
31
|
+
export type PublishBatchResult = {id: string} | {error: Error};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Some messages of a batch were not published. `results` says, in the order
|
|
35
|
+
* the messages were given, each one's id or why it was not published.
|
|
36
|
+
*/
|
|
37
|
+
export class PublishBatchError extends Error {
|
|
38
|
+
readonly results: ReadonlyArray<PublishBatchResult>;
|
|
39
|
+
|
|
40
|
+
constructor(results: ReadonlyArray<PublishBatchResult>) {
|
|
41
|
+
const failed = results.filter(r => 'error' in r);
|
|
42
|
+
const first = failed[0] as {error: Error} | undefined;
|
|
43
|
+
super(
|
|
44
|
+
`${failed.length} of the ${results.length} messages were not published` +
|
|
45
|
+
(first ? `: ${first.error.message}` : ''),
|
|
46
|
+
);
|
|
47
|
+
this.name = 'PublishBatchError';
|
|
48
|
+
this.results = results;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
11
52
|
/**
|
|
12
53
|
* A topic is a resource to which you can publish messages
|
|
13
54
|
* to be delivered to subscribers of that topic.
|
|
@@ -19,13 +60,13 @@ export class Topic<Msg> extends TopicPerms implements Publisher<Msg> {
|
|
|
19
60
|
* with `Topic.named`, which is configured where it is declared.
|
|
20
61
|
*/
|
|
21
62
|
readonly cfg?: TopicConfig<Msg>;
|
|
22
|
-
private
|
|
63
|
+
private readonly sender: Sender;
|
|
23
64
|
|
|
24
65
|
constructor(name: string, cfg?: TopicConfig<Msg>) {
|
|
25
66
|
super();
|
|
26
67
|
this.name = name;
|
|
27
68
|
this.cfg = cfg;
|
|
28
|
-
this.
|
|
69
|
+
this.sender = new Sender(name);
|
|
29
70
|
}
|
|
30
71
|
|
|
31
72
|
/**
|
|
@@ -49,7 +90,10 @@ export class Topic<Msg> extends TopicPerms implements Publisher<Msg> {
|
|
|
49
90
|
*
|
|
50
91
|
* JSON values are serialized as the payload; Buffer and Uint8Array payloads
|
|
51
92
|
* are sent as raw bytes. Messages are delivered to subscribers verbatim.
|
|
52
|
-
* `attributes` are delivered alongside the payload.
|
|
93
|
+
* `attributes` are delivered alongside the payload. The message also
|
|
94
|
+
* carries the Request Context fields of the active context declared with
|
|
95
|
+
* `pubsub`, into its handler's context; `encore-ctx-*` attributes are
|
|
96
|
+
* Encore's, and replaced by them.
|
|
53
97
|
*/
|
|
54
98
|
async publish(
|
|
55
99
|
msg: Msg,
|
|
@@ -57,7 +101,32 @@ export class Topic<Msg> extends TopicPerms implements Publisher<Msg> {
|
|
|
57
101
|
): Promise<string> {
|
|
58
102
|
const source = activeCtxBytes();
|
|
59
103
|
const payload = encodePayload(msg);
|
|
60
|
-
return this.
|
|
104
|
+
return this.sender
|
|
105
|
+
.impl()
|
|
106
|
+
.publish(payload, messageAttributes(attributes), source);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Publish up to {@link MaxBatchSize} messages to the topic, returning their
|
|
111
|
+
* ids in the order given. Each message is encoded as `publish` encodes it,
|
|
112
|
+
* and carries the Request Context and trace context as a published one does.
|
|
113
|
+
*
|
|
114
|
+
* It is not atomic: if some messages are not published, the rest still
|
|
115
|
+
* are, and it rejects with a {@link PublishBatchError} saying which. A
|
|
116
|
+
* batch of more than {@link MaxBatchSize} messages is refused whole, and
|
|
117
|
+
* none of it published. An empty batch publishes nothing.
|
|
118
|
+
*/
|
|
119
|
+
async publishBatch(
|
|
120
|
+
messages: readonly BatchMessage<Msg>[],
|
|
121
|
+
): Promise<string[]> {
|
|
122
|
+
checkBatchSize(messages.length);
|
|
123
|
+
const source = activeCtxBytes();
|
|
124
|
+
const entries = messages.map(m => ({
|
|
125
|
+
data: encodePayload(m.data),
|
|
126
|
+
// Each message carries the Request Context fields, as one sent alone.
|
|
127
|
+
attributes: messageAttributes(m.attributes),
|
|
128
|
+
}));
|
|
129
|
+
return publishBatch(this.sender.impl(), entries, source);
|
|
61
130
|
}
|
|
62
131
|
|
|
63
132
|
ref<P extends TopicPerms>(): P {
|
|
@@ -106,13 +175,13 @@ export class OrderedTopic<Msg>
|
|
|
106
175
|
* with `OrderedTopic.named`, which is configured where it is declared.
|
|
107
176
|
*/
|
|
108
177
|
readonly cfg?: OrderedTopicConfig<Msg>;
|
|
109
|
-
private
|
|
178
|
+
private readonly sender: Sender;
|
|
110
179
|
|
|
111
180
|
constructor(name: string, cfg?: OrderedTopicConfig<Msg>) {
|
|
112
181
|
super();
|
|
113
182
|
this.name = name;
|
|
114
183
|
this.cfg = cfg;
|
|
115
|
-
this.
|
|
184
|
+
this.sender = new Sender(name);
|
|
116
185
|
}
|
|
117
186
|
|
|
118
187
|
/**
|
|
@@ -132,7 +201,10 @@ export class OrderedTopic<Msg>
|
|
|
132
201
|
*
|
|
133
202
|
* JSON values are serialized as the payload; Buffer and Uint8Array payloads
|
|
134
203
|
* are sent as raw bytes. Messages are delivered to subscribers verbatim.
|
|
135
|
-
* `attributes` are delivered alongside the payload.
|
|
204
|
+
* `attributes` are delivered alongside the payload. The message also
|
|
205
|
+
* carries the Request Context fields of the active context declared with
|
|
206
|
+
* `pubsub`, into its handler's context; `encore-ctx-*` attributes are
|
|
207
|
+
* Encore's, and replaced by them.
|
|
136
208
|
*/
|
|
137
209
|
async publish(
|
|
138
210
|
msg: Msg,
|
|
@@ -144,7 +216,43 @@ export class OrderedTopic<Msg>
|
|
|
144
216
|
}
|
|
145
217
|
const source = activeCtxBytes();
|
|
146
218
|
const payload = encodePayload(msg);
|
|
147
|
-
return this.
|
|
219
|
+
return this.sender
|
|
220
|
+
.impl()
|
|
221
|
+
.publish(payload, messageAttributes(attributes), source, orderingKey);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Publish up to {@link MaxBatchSize} messages to the topic, returning their
|
|
226
|
+
* ids in the order given. Each message is encoded as `publish` encodes it,
|
|
227
|
+
* and carries the Request Context and trace context as a published one does.
|
|
228
|
+
*
|
|
229
|
+
* The messages sharing an ordering key are published in the order given,
|
|
230
|
+
* and once one of them is not published, neither are the later ones with
|
|
231
|
+
* its key. It is not atomic: if some messages are not published, the rest
|
|
232
|
+
* still are, and it rejects with a {@link PublishBatchError} saying which.
|
|
233
|
+
* A batch of more than {@link MaxBatchSize} messages, or with a message
|
|
234
|
+
* whose `orderingKey` is not a non-empty string, is refused whole, and none
|
|
235
|
+
* of it published. An empty batch publishes nothing.
|
|
236
|
+
*/
|
|
237
|
+
async publishBatch(
|
|
238
|
+
messages: readonly OrderedBatchMessage<Msg>[],
|
|
239
|
+
): Promise<string[]> {
|
|
240
|
+
checkBatchSize(messages.length);
|
|
241
|
+
messages.forEach((m, i) => {
|
|
242
|
+
if (typeof m.orderingKey !== 'string' || m.orderingKey.length === 0) {
|
|
243
|
+
throw new TypeError(
|
|
244
|
+
`message ${i}: orderingKey must be a non-empty string`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
const source = activeCtxBytes();
|
|
249
|
+
const entries = messages.map(m => ({
|
|
250
|
+
data: encodePayload(m.data),
|
|
251
|
+
// Each message carries the Request Context fields, as one sent alone.
|
|
252
|
+
attributes: messageAttributes(m.attributes),
|
|
253
|
+
orderingKey: m.orderingKey,
|
|
254
|
+
}));
|
|
255
|
+
return publishBatch(this.sender.impl(), entries, source);
|
|
148
256
|
}
|
|
149
257
|
|
|
150
258
|
ref<P extends OrderedTopicPerms>(): P {
|
|
@@ -152,39 +260,117 @@ export class OrderedTopic<Msg>
|
|
|
152
260
|
}
|
|
153
261
|
}
|
|
154
262
|
|
|
263
|
+
/** What a topic publishes with: the runtime's topic, or the test's record. */
|
|
264
|
+
type Publish = Pick<runtime.PubSubTopic, 'publish' | 'publishBatch'>;
|
|
265
|
+
|
|
155
266
|
/**
|
|
156
|
-
*
|
|
267
|
+
* Where a topic's messages go: to the runtime's topic, or, in a test, to the
|
|
268
|
+
* test's record of what it published, sent nowhere, for it to read back with
|
|
269
|
+
* `published` or hand to a subscription with `deliver`.
|
|
157
270
|
*/
|
|
158
|
-
|
|
271
|
+
class Sender {
|
|
272
|
+
/** The runtime's topic, made on first use in test mode. */
|
|
273
|
+
private topic?: runtime.PubSubTopic;
|
|
274
|
+
|
|
275
|
+
constructor(private readonly name: string) {
|
|
276
|
+
if (!testMode) this.topic = runtime.RT.pubsubTopic(name);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
impl(): Publish {
|
|
280
|
+
const scope = currentScope();
|
|
281
|
+
if (scope !== null) return recorder(scope, this.name);
|
|
282
|
+
return (this.topic ??= runtime.RT.pubsubTopic(this.name));
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Publishes to the record of what the test `scope` published to `topic`. */
|
|
287
|
+
function recorder(scope: Scope, topic: string): Publish {
|
|
288
|
+
const record = (
|
|
289
|
+
data: Buffer,
|
|
290
|
+
attributes: Record<string, string> | undefined | null,
|
|
291
|
+
orderingKey: string | undefined | null,
|
|
292
|
+
): string => {
|
|
293
|
+
const id = scope.native.recordPublished(
|
|
294
|
+
topic,
|
|
295
|
+
data,
|
|
296
|
+
attributes ?? undefined,
|
|
297
|
+
orderingKey ?? undefined,
|
|
298
|
+
);
|
|
299
|
+
if (rawPayloads.has(data)) scope.raw.add(id);
|
|
300
|
+
return id;
|
|
301
|
+
};
|
|
302
|
+
return {
|
|
303
|
+
publish: async (data, attributes, _source, orderingKey) =>
|
|
304
|
+
record(data, attributes, orderingKey),
|
|
305
|
+
publishBatch: async entries =>
|
|
306
|
+
entries.map(e => ({
|
|
307
|
+
id: record(e.data, e.attributes, e.orderingKey),
|
|
308
|
+
})),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
159
311
|
|
|
160
312
|
/**
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
* On AWS and GCP there is no limit to the throughput for a topic.
|
|
313
|
+
* Refuses a batch of more than {@link MaxBatchSize} messages before any of
|
|
314
|
+
* it is encoded, as the runtime would refuse it once it was.
|
|
165
315
|
*/
|
|
166
|
-
|
|
316
|
+
function checkBatchSize(count: number): void {
|
|
317
|
+
if (count > MaxBatchSize) {
|
|
318
|
+
throw new Error(
|
|
319
|
+
`failed to publish: a batch has at most ${MaxBatchSize} messages, and this one has ${count}`,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
167
323
|
|
|
168
324
|
/**
|
|
169
|
-
*
|
|
170
|
-
* a
|
|
325
|
+
* Publishes the encoded messages, answering with their ids, or rejecting
|
|
326
|
+
* with a {@link PublishBatchError} if any was not published.
|
|
327
|
+
*/
|
|
328
|
+
async function publishBatch(
|
|
329
|
+
impl: runtime.PubSubTopic,
|
|
330
|
+
entries: runtime.PubSubBatchEntry[],
|
|
331
|
+
source: Buffer | undefined,
|
|
332
|
+
): Promise<string[]> {
|
|
333
|
+
if (entries.length === 0) return [];
|
|
334
|
+
const outcomes = await impl.publishBatch(entries, source);
|
|
335
|
+
if (outcomes.every(o => o.id !== undefined && o.id !== null)) {
|
|
336
|
+
return outcomes.map(o => o.id!);
|
|
337
|
+
}
|
|
338
|
+
throw new PublishBatchError(
|
|
339
|
+
outcomes.map(o =>
|
|
340
|
+
o.id !== undefined && o.id !== null
|
|
341
|
+
? {id: o.id}
|
|
342
|
+
: {error: new Error(o.error ?? 'the message was not published')},
|
|
343
|
+
),
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* DeliveryGuarantee is used to configure the delivery contract for a topic,
|
|
349
|
+
* written out as one of the two strings: Encore reads it from the source.
|
|
350
|
+
*
|
|
351
|
+
* `"at-least-once"` guarantees that a message for a subscription is
|
|
352
|
+
* delivered to a consumer at least once. On AWS and GCP there is no limit to
|
|
353
|
+
* the throughput for a topic.
|
|
354
|
+
*
|
|
355
|
+
* `"exactly-once"` guarantees that a message for a subscription is delivered
|
|
356
|
+
* to a consumer exactly once, to the best of the system's ability.
|
|
171
357
|
*
|
|
172
358
|
* However, there are edge cases when a message might be redelivered.
|
|
173
359
|
* For example, if a networking issue causes the acknowledgement of success
|
|
174
360
|
* processing the message to be lost before the cloud provider receives it.
|
|
175
361
|
*
|
|
176
|
-
* It is also important to note that the
|
|
362
|
+
* It is also important to note that the exactly-once delivery guarantee only
|
|
177
363
|
* applies to the delivery of the message to the consumer, and not to the
|
|
178
364
|
* original publishing of the message, such that if a message is published twice,
|
|
179
365
|
* such as due to an retry within the application logic, it will be delivered twice.
|
|
180
|
-
* (i.e.
|
|
366
|
+
* (i.e. exactly-once delivery does not imply message deduplication on publish)
|
|
181
367
|
*
|
|
182
368
|
* As such it's recommended that the subscription handler function is idempotent
|
|
183
369
|
* and is able to handle duplicate messages.
|
|
184
370
|
*
|
|
185
|
-
* Subscriptions attached to
|
|
371
|
+
* Subscriptions attached to exactly-once topics have higher message delivery latency compared to at-least-once.
|
|
186
372
|
*
|
|
187
|
-
* By using
|
|
373
|
+
* By using exactly-once semantics on a topic, the throughput will be limited depending on the cloud provider:
|
|
188
374
|
* - AWS: 300 messages per second for the topic (see [AWS SQS Quotas]).
|
|
189
375
|
* - GCP: At least 3,000 messages per second across all topics in the region
|
|
190
376
|
* (can be higher on the region see [GCP PubSub Quotas]).
|
|
@@ -192,17 +378,26 @@ export const atLeastOnce: DeliveryGuarantee = 'at-least-once';
|
|
|
192
378
|
* [AWS SQS Quotas]: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/quotas-messages.html
|
|
193
379
|
* [GCP PubSub Quotas]: https://cloud.google.com/pubsub/quotas#quotas
|
|
194
380
|
*/
|
|
195
|
-
export
|
|
381
|
+
export type DeliveryGuarantee = 'at-least-once' | 'exactly-once';
|
|
196
382
|
|
|
197
383
|
/**
|
|
198
384
|
* TopicConfig is used when creating a Topic
|
|
199
385
|
*/
|
|
200
386
|
export interface TopicConfig<_Msg> {
|
|
201
387
|
/**
|
|
202
|
-
* DeliveryGuarantee is used to configure the delivery guarantee of a Topic
|
|
203
|
-
*
|
|
388
|
+
* DeliveryGuarantee is used to configure the delivery guarantee of a Topic:
|
|
389
|
+
* `"at-least-once"` or `"exactly-once"`, written out. Defaults to
|
|
390
|
+
* `"at-least-once"`.
|
|
204
391
|
*/
|
|
205
392
|
deliveryGuarantee?: DeliveryGuarantee;
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Tags that classify the topic, for policies to read: written out as
|
|
396
|
+
* string literals, as in `{data: "pci"}`. Keys and values are lowercase
|
|
397
|
+
* letters, digits, `_` and `-`, at most 63 characters; a key starts with a
|
|
398
|
+
* letter, and not with `encore`, which is reserved. At most 32 tags.
|
|
399
|
+
*/
|
|
400
|
+
tags?: Record<string, string>;
|
|
206
401
|
}
|
|
207
402
|
|
|
208
403
|
/**
|
package/reqctx/mod.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request Context: typed fields that travel with a request to every service
|
|
3
|
+
* it reaches, such as the user it is made for.
|
|
4
|
+
*
|
|
5
|
+
* A field is declared once in the app, as a module-level `const`, with a name
|
|
6
|
+
* and the type of its value. Encore's parser reads the declaration, so the
|
|
7
|
+
* name must be a string literal:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {Field} from '@encore.dev/sdk/reqctx';
|
|
11
|
+
*
|
|
12
|
+
* export interface User {
|
|
13
|
+
* id: string;
|
|
14
|
+
* email: string;
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* export const currentUser = new Field<User>('user', {pubsub: true});
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Gateway Middleware sets fields on the requests it lets through, with
|
|
21
|
+
* `req.set(currentUser, user)` (`@encore.dev/sdk/gateway`), and a service
|
|
22
|
+
* reads them while handling one:
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* const user = currentUser.get();
|
|
26
|
+
* if (user === undefined) throw new Error('not signed in');
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* A service may set any field, or clear one, for the code it runs and the
|
|
30
|
+
* calls it makes:
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* await currentUser.with({id: 'system', email: ''}, () => fetchInvoices());
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* Another service, possibly in another language, binds a field by name, with
|
|
37
|
+
* the type of its value: `Field.named<User>('user')`.
|
|
38
|
+
*
|
|
39
|
+
* ## Where fields are
|
|
40
|
+
*
|
|
41
|
+
* The fields are in the OpenTelemetry context, with the trace, so they follow
|
|
42
|
+
* the active context across `await`, timers and callbacks. Encore tracks it
|
|
43
|
+
* without a global context manager too.
|
|
44
|
+
*
|
|
45
|
+
* ## Trust
|
|
46
|
+
*
|
|
47
|
+
* No client can set a field: Encore strips the headers fields travel in
|
|
48
|
+
* wherever a request enters the app, and signs them between containers. So a
|
|
49
|
+
* service can trust what it reads, as far as it trusts the app's other
|
|
50
|
+
* services.
|
|
51
|
+
*
|
|
52
|
+
* ## Receiving and sending fields
|
|
53
|
+
*
|
|
54
|
+
* {@link extract} takes the fields a request a service receives carries into
|
|
55
|
+
* a context, and {@link Propagator}, composed into the app's global
|
|
56
|
+
* propagator, does it for a server instrumented for tracing. {@link inject}
|
|
57
|
+
* sets the fields of a context on a request for another of the app's
|
|
58
|
+
* services, and only on those: anything else would be sent who the request is
|
|
59
|
+
* made for. A Pub/Sub message carries the fields declared with `pubsub` that
|
|
60
|
+
* are set where it is published, into its handler's context.
|
|
61
|
+
*
|
|
62
|
+
* ## Limits
|
|
63
|
+
*
|
|
64
|
+
* A field travels as the base64url of its value's JSON, in a header of its
|
|
65
|
+
* own, so it must be small: its encoding may be at most 4 KiB, and those of
|
|
66
|
+
* all of a request's fields at most 8 KiB; a field carried on Pub/Sub
|
|
67
|
+
* messages, at most 1 KiB. Setting a value beyond that throws, at the code
|
|
68
|
+
* that set it, rather than the request failing some hops later.
|
|
69
|
+
*
|
|
70
|
+
* @module
|
|
71
|
+
*/
|
|
72
|
+
import type {Context} from '@opentelemetry/api';
|
|
73
|
+
import log from '../log/index';
|
|
74
|
+
import {activeContext, withContext} from '../internal/otel/context';
|
|
75
|
+
import * as reqfields from '../internal/reqfields';
|
|
76
|
+
|
|
77
|
+
export type {
|
|
78
|
+
HeaderSink,
|
|
79
|
+
HeaderSource,
|
|
80
|
+
HeadersLike,
|
|
81
|
+
} from '../internal/reqfields';
|
|
82
|
+
|
|
83
|
+
/** How a field travels. Read by Encore's parser, so each is written out as
|
|
84
|
+
* `true` or `false`. */
|
|
85
|
+
export interface FieldConfig {
|
|
86
|
+
/**
|
|
87
|
+
* Also carry the field on the Pub/Sub messages published with it set, into
|
|
88
|
+
* the context of the handlers that receive them. Its value may then encode
|
|
89
|
+
* to at most 1 KiB, GCP Pub/Sub's limit on an attribute's value.
|
|
90
|
+
*/
|
|
91
|
+
pubsub?: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Put the value on the spans started with it set, as the attribute
|
|
94
|
+
* `encore.ctx.<name>`, holding its JSON: for identifiers worth finding
|
|
95
|
+
* traces by, such as a tenant's. Never for secrets.
|
|
96
|
+
*/
|
|
97
|
+
trace?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Tags that classify the field, for policies to read: written out as
|
|
100
|
+
* string literals, as in `{data: "pci"}`. Keys and values are lowercase
|
|
101
|
+
* letters, digits, `_` and `-`, at most 63 characters; a key starts with a
|
|
102
|
+
* letter, and not with `encore`, which is reserved. At most 32 tags.
|
|
103
|
+
*/
|
|
104
|
+
tags?: Record<string, string>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A Request Context field holding a JSON value of type `T`.
|
|
109
|
+
*
|
|
110
|
+
* Declare it as a module-level `const`: `new Field<User>("user")`. The type
|
|
111
|
+
* argument is the value's type, which Encore describes, as a config's, and
|
|
112
|
+
* checks the bindings of the field by name against. TypeScript has no decoder:
|
|
113
|
+
* a value reads as its JSON, which the type describes.
|
|
114
|
+
*/
|
|
115
|
+
export class Field<T> {
|
|
116
|
+
/** The name the field was declared with. */
|
|
117
|
+
readonly name: string;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Declare a field.
|
|
121
|
+
*
|
|
122
|
+
* @param name - The field's name, unique within the app, in kebab-case:
|
|
123
|
+
* lowercase letters, digits and hyphens, starting with a letter and ending
|
|
124
|
+
* with a letter or digit. A string literal, which Encore's parser checks.
|
|
125
|
+
* @param cfg - How it travels.
|
|
126
|
+
*/
|
|
127
|
+
constructor(name: string, cfg?: FieldConfig) {
|
|
128
|
+
this.name = name;
|
|
129
|
+
reqfields.declare(name, {
|
|
130
|
+
pubsub: cfg?.pubsub === true,
|
|
131
|
+
trace: cfg?.trace === true,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Reference a field declared elsewhere in the app, possibly in another
|
|
137
|
+
* language. Give the value's type, which Encore checks against the
|
|
138
|
+
* declaration's: `Field.named<User>("user")`. The field travels as its
|
|
139
|
+
* declaration says: carried on Pub/Sub messages, and put on spans, as
|
|
140
|
+
* declared.
|
|
141
|
+
*/
|
|
142
|
+
static named<T>(name: string): Field<T> {
|
|
143
|
+
const field = Object.create(Field.prototype) as Field<T>;
|
|
144
|
+
Object.defineProperty(field, 'name', {
|
|
145
|
+
value: name,
|
|
146
|
+
enumerable: true,
|
|
147
|
+
});
|
|
148
|
+
return field;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The field's value in `ctx`, or in the active context; `undefined` when it
|
|
153
|
+
* is not set.
|
|
154
|
+
*
|
|
155
|
+
* Each call decodes the value anew, so the caller may change what it
|
|
156
|
+
* returns. A value that is not JSON reads as not set; Encore logs it, once.
|
|
157
|
+
*/
|
|
158
|
+
get(ctx?: Context): T | undefined {
|
|
159
|
+
const value = reqfields.current(ctx ?? activeContext())[this.name];
|
|
160
|
+
if (value === undefined) return undefined;
|
|
161
|
+
try {
|
|
162
|
+
return reqfields.decode(value) as T;
|
|
163
|
+
} catch (err) {
|
|
164
|
+
logUndecodable(this.name, err);
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Run `fn` with the field set to `value`, for the code it runs and the
|
|
171
|
+
* calls it makes. Returns what `fn` returns: a promise for an async `fn`.
|
|
172
|
+
*
|
|
173
|
+
* @throws TypeError - The value has no JSON form: a `bigint`, a cycle, or
|
|
174
|
+
* `undefined`.
|
|
175
|
+
* @throws RangeError - The value encodes to more than the field may: 4 KiB,
|
|
176
|
+
* or 1 KiB for a field declared with `pubsub`, or makes the request's fields
|
|
177
|
+
* encode to more than 8 KiB.
|
|
178
|
+
*/
|
|
179
|
+
with<R>(value: T, fn: () => R): R {
|
|
180
|
+
return withContext(this.context(value), fn);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Run `fn` with the field cleared, for the code it runs and the calls it
|
|
184
|
+
* makes. */
|
|
185
|
+
without<R>(fn: () => R): R {
|
|
186
|
+
return withContext(this.contextWithout(), fn);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* A copy of `ctx`, or of the active context, with the field set to
|
|
191
|
+
* `value`: for code that passes contexts explicitly. Throws as
|
|
192
|
+
* {@link Field.with} does.
|
|
193
|
+
*/
|
|
194
|
+
context(value: T, ctx?: Context): Context {
|
|
195
|
+
let encoded: string;
|
|
196
|
+
try {
|
|
197
|
+
encoded = reqfields.encode(value);
|
|
198
|
+
} catch (err) {
|
|
199
|
+
throw new TypeError(
|
|
200
|
+
`reqctx: field ${JSON.stringify(this.name)}: ${messageOf(err)}`,
|
|
201
|
+
{cause: err},
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
return reqfields.withValue(this.name, encoded, ctx ?? activeContext());
|
|
206
|
+
} catch (err) {
|
|
207
|
+
throw new RangeError(
|
|
208
|
+
`reqctx: field ${JSON.stringify(this.name)}: the value ${messageOf(err)}`,
|
|
209
|
+
{cause: err},
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** A copy of `ctx`, or of the active context, without the field. */
|
|
215
|
+
contextWithout(ctx?: Context): Context {
|
|
216
|
+
return reqfields.without(this.name, ctx ?? activeContext());
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** `Field("name")`. */
|
|
220
|
+
toString(): string {
|
|
221
|
+
return `Field(${JSON.stringify(this.name)})`;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* A copy of `ctx`, or of the active context, holding the fields a request's
|
|
227
|
+
* headers carry, in place of those it held: the first value of each header
|
|
228
|
+
* named for a field, in any case. When they carry none, `ctx` itself.
|
|
229
|
+
*/
|
|
230
|
+
export function extract(
|
|
231
|
+
headers: reqfields.HeaderSource,
|
|
232
|
+
ctx?: Context,
|
|
233
|
+
): Context {
|
|
234
|
+
const base = ctx ?? activeContext();
|
|
235
|
+
const fields = reqfields.fromHeaders(headers);
|
|
236
|
+
return Object.keys(fields).length === 0
|
|
237
|
+
? base
|
|
238
|
+
: reqfields.withFields(fields, base);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Set the fields of `ctx`, or of the active context, on a request's headers,
|
|
243
|
+
* in place of any they carried.
|
|
244
|
+
*
|
|
245
|
+
* Use it only for requests to the app's own services: anything else is sent
|
|
246
|
+
* who the request is made for.
|
|
247
|
+
*/
|
|
248
|
+
export function inject(headers: reqfields.HeaderSink, ctx?: Context): void {
|
|
249
|
+
reqfields.toHeaders(headers, ctx ?? activeContext());
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* An OpenTelemetry propagator that extracts the fields a request carries into
|
|
254
|
+
* its context, and injects none. Compose it into the app's global propagator,
|
|
255
|
+
* so a server instrumented for tracing has them:
|
|
256
|
+
*
|
|
257
|
+
* ```ts
|
|
258
|
+
* propagation.setGlobalPropagator(
|
|
259
|
+
* new CompositePropagator({
|
|
260
|
+
* propagators: [new W3CTraceContextPropagator(), new reqctx.Propagator()],
|
|
261
|
+
* }),
|
|
262
|
+
* );
|
|
263
|
+
* ```
|
|
264
|
+
*
|
|
265
|
+
* It never injects, so an instrumented client never sends fields to a third
|
|
266
|
+
* party: send them to the app's services with {@link inject}.
|
|
267
|
+
*/
|
|
268
|
+
export const Propagator = reqfields.Propagator;
|
|
269
|
+
export type Propagator = reqfields.Propagator;
|
|
270
|
+
|
|
271
|
+
function messageOf(err: unknown): string {
|
|
272
|
+
return err instanceof Error ? err.message : String(err);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const logged = new Set<string>();
|
|
276
|
+
|
|
277
|
+
function logUndecodable(name: string, err: unknown): void {
|
|
278
|
+
if (logged.has(name)) return;
|
|
279
|
+
logged.add(name);
|
|
280
|
+
log.warn(
|
|
281
|
+
`encore: cannot decode Request Context field ${JSON.stringify(name)}, so it reads as not set: ${messageOf(err)}`,
|
|
282
|
+
{field: name},
|
|
283
|
+
);
|
|
284
|
+
}
|