@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
|
@@ -8,14 +8,21 @@ import {
|
|
|
8
8
|
trace,
|
|
9
9
|
type Span as ApiSpan,
|
|
10
10
|
} from '@opentelemetry/api';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
ATTR_LEN,
|
|
13
|
+
EVENT_LEN,
|
|
14
|
+
HEADER_LEN,
|
|
15
|
+
ITEM_LEN,
|
|
16
|
+
LINK_LEN,
|
|
17
|
+
TYPE_I64,
|
|
18
|
+
TYPE_STR,
|
|
19
|
+
} from './encode';
|
|
20
|
+
import {Field} from '../../reqctx/mod';
|
|
12
21
|
import {
|
|
13
22
|
activeCtxBytes,
|
|
14
23
|
Span,
|
|
15
24
|
ContextManager,
|
|
16
|
-
getRequestFromContext,
|
|
17
25
|
TracerProvider,
|
|
18
|
-
withRequestContext,
|
|
19
26
|
type Backend,
|
|
20
27
|
} from './index';
|
|
21
28
|
|
|
@@ -92,6 +99,19 @@ function str(buf: Buffer, off: number): string {
|
|
|
92
99
|
return buf.subarray(start, start + buf.readUInt32LE(off + 4)).toString();
|
|
93
100
|
}
|
|
94
101
|
|
|
102
|
+
/** A packed span's string and integer attributes. */
|
|
103
|
+
function decodeAttributes(buf: Buffer): Record<string, string | number> {
|
|
104
|
+
const out: Record<string, string | number> = {};
|
|
105
|
+
for (let i = 0; i < buf.readUInt32LE(4); i++) {
|
|
106
|
+
const off = HEADER_LEN + i * ATTR_LEN;
|
|
107
|
+
if (buf[off + 8] === TYPE_STR) out[str(buf, off)] = str(buf, off + 16);
|
|
108
|
+
if (buf[off + 8] === TYPE_I64) {
|
|
109
|
+
out[str(buf, off)] = Number(buf.readBigInt64LE(off + 16));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
95
115
|
beforeEach(() => {
|
|
96
116
|
backend.n = 0;
|
|
97
117
|
backend.rootFlags = 1;
|
|
@@ -328,25 +348,33 @@ describe('non-recording spans', () => {
|
|
|
328
348
|
});
|
|
329
349
|
});
|
|
330
350
|
|
|
331
|
-
describe('
|
|
332
|
-
test('
|
|
333
|
-
const
|
|
351
|
+
describe('Request Context', () => {
|
|
352
|
+
test('fields declared with trace are attributes of spans started with them', () => {
|
|
353
|
+
const tenant = new Field<string>('test-tenant', {trace: true});
|
|
354
|
+
const user = new Field<{id: string}>('test-user');
|
|
355
|
+
const ctx = tenant.context('acme', user.context({id: 'u1'}, ROOT_CONTEXT));
|
|
356
|
+
tracer.startSpan('s', {attributes: {own: 1}}, ctx);
|
|
357
|
+
tracer.startSpan(
|
|
358
|
+
'wins',
|
|
359
|
+
{attributes: {'encore.ctx.test-tenant': 'x'}},
|
|
360
|
+
ctx,
|
|
361
|
+
);
|
|
362
|
+
tracer.startSpan('none', {}, ROOT_CONTEXT);
|
|
363
|
+
const attrs = (i: number) => decodeAttributes(backend.starts[i]!.packed);
|
|
364
|
+
expect(attrs(0)).toEqual({'encore.ctx.test-tenant': '"acme"', own: 1});
|
|
365
|
+
expect(attrs(1)).toEqual({'encore.ctx.test-tenant': 'x'});
|
|
366
|
+
expect(attrs(2)).toEqual({});
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
test('a span parents on the span context set in its context', () => {
|
|
334
370
|
const sc = {
|
|
335
371
|
traceId: '0102030405060708090a0b0c0d0e0f10',
|
|
336
372
|
spanId: '1112131415161718',
|
|
337
373
|
traceFlags: 1,
|
|
338
374
|
};
|
|
339
|
-
const ctx =
|
|
340
|
-
expect(getRequestFromContext(ctx)).toBe(req);
|
|
341
|
-
expect(trace.getSpanContext(ctx)).toEqual(sc);
|
|
375
|
+
const ctx = trace.setSpanContext(ROOT_CONTEXT, sc);
|
|
342
376
|
const span = context.with(ctx, () => tracer.startSpan('user'));
|
|
343
377
|
expect(backend.starts[0]!.parent!.toString('hex', 16, 24)).toBe(sc.spanId);
|
|
344
378
|
expect(span.spanContext().traceId).toBe(sc.traceId);
|
|
345
|
-
expect(getRequestFromContext(withRequestContext(ROOT_CONTEXT, req))).toBe(
|
|
346
|
-
req,
|
|
347
|
-
);
|
|
348
|
-
expect(
|
|
349
|
-
trace.getSpanContext(withRequestContext(ROOT_CONTEXT, req)),
|
|
350
|
-
).toBeUndefined();
|
|
351
379
|
});
|
|
352
380
|
});
|
package/internal/otel/tracer.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type {Backend} from './backend';
|
|
|
11
11
|
import {activeContext, withContext} from './context';
|
|
12
12
|
import {encodeStart, timeToUnixNanos, type Scope} from './encode';
|
|
13
13
|
import {Span, spanCtxBytes} from './span';
|
|
14
|
+
import {spanAttributes} from '../reqfields';
|
|
14
15
|
|
|
15
16
|
/** Every span records under the tracer's scope; the runtime assigns ids
|
|
16
17
|
* and clocks. Spans are parented on, and made active in, the context
|
|
@@ -22,9 +23,15 @@ export class Tracer implements ApiTracer {
|
|
|
22
23
|
) {}
|
|
23
24
|
|
|
24
25
|
startSpan(name: string, options: SpanOptions = {}, ctx?: Context): ApiSpan {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const parentCtx = ctx ?? activeContext();
|
|
27
|
+
// The Request Context fields declared with `trace` are attributes of
|
|
28
|
+
// every span started in their context; the span's own win.
|
|
29
|
+
const recorded = spanAttributes(parentCtx);
|
|
30
|
+
const attributes =
|
|
31
|
+
Object.keys(recorded).length === 0
|
|
32
|
+
? options.attributes
|
|
33
|
+
: {...recorded, ...options.attributes};
|
|
34
|
+
const parentSpan = options.root ? undefined : trace.getSpan(parentCtx);
|
|
28
35
|
const parent =
|
|
29
36
|
parentSpan && isSpanContextValid(parentSpan.spanContext())
|
|
30
37
|
? parentSpan
|
|
@@ -40,7 +47,7 @@ export class Tracer implements ApiTracer {
|
|
|
40
47
|
options.startTime === undefined
|
|
41
48
|
? undefined
|
|
42
49
|
: timeToUnixNanos(options.startTime),
|
|
43
|
-
encodeStart(this.scope,
|
|
50
|
+
encodeStart(this.scope, attributes, options.links),
|
|
44
51
|
);
|
|
45
52
|
|
|
46
53
|
return new Span({
|
|
@@ -53,7 +60,7 @@ export class Tracer implements ApiTracer {
|
|
|
53
60
|
parentSpanId: parentBytes?.subarray(16, 24),
|
|
54
61
|
startUnixNanos: res.startUnixNanos,
|
|
55
62
|
startMonoNanos: res.startMonoNanos,
|
|
56
|
-
attributes
|
|
63
|
+
attributes,
|
|
57
64
|
links: options.links,
|
|
58
65
|
});
|
|
59
66
|
}
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request Context in its wire form, as the OpenTelemetry context holds it.
|
|
3
|
+
*
|
|
4
|
+
* A request's fields (ADR 0014) travel with the context of the code handling
|
|
5
|
+
* it: each field's name, and its value as it travels, base64url (without
|
|
6
|
+
* padding) of its JSON. They are kept in that form, so moving them costs
|
|
7
|
+
* nothing; only reading one decodes it. Every field a request carries is kept
|
|
8
|
+
* and sent on, those this process declares nothing about included, so a
|
|
9
|
+
* service passes on the fields of the services it sits between.
|
|
10
|
+
*
|
|
11
|
+
* `@encore.dev/sdk/reqctx` reads and sets fields and takes them from and puts
|
|
12
|
+
* them on requests; `pubsub` hands the runtime every field a message is
|
|
13
|
+
* published with, and the runtime carries those declared with `pubsub`.
|
|
14
|
+
*
|
|
15
|
+
* How a field travels is its declaration's, wherever it is: the runtime
|
|
16
|
+
* config lists every field the app declares ({@link configOf}).
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
createContextKey,
|
|
20
|
+
type Context,
|
|
21
|
+
type TextMapGetter,
|
|
22
|
+
type TextMapPropagator,
|
|
23
|
+
} from '@opentelemetry/api';
|
|
24
|
+
import {activeContext} from './otel/context';
|
|
25
|
+
|
|
26
|
+
/** The header a field travels in on a request, before its name. */
|
|
27
|
+
export const HEADER_PREFIX = 'x-encore-meta-ctx-';
|
|
28
|
+
/** The attribute a field travels in on a Pub/Sub message, before its name. */
|
|
29
|
+
export const ATTRIBUTE_PREFIX = 'encore-ctx-';
|
|
30
|
+
/** The span attribute a field declared with `trace` is, before its name. */
|
|
31
|
+
export const SPAN_ATTRIBUTE_PREFIX = 'encore.ctx.';
|
|
32
|
+
|
|
33
|
+
/** The most one field encodes to, in bytes. */
|
|
34
|
+
export const MAX_VALUE_SIZE = 4096;
|
|
35
|
+
/** The most a field declared with `pubsub` encodes to, in bytes: GCP
|
|
36
|
+
* Pub/Sub's limit on an attribute's value. */
|
|
37
|
+
export const MAX_PUBSUB_VALUE_SIZE = 1024;
|
|
38
|
+
/** The most all of a request's fields encode to, in bytes. */
|
|
39
|
+
export const MAX_SIZE = 8192;
|
|
40
|
+
|
|
41
|
+
/** A context's fields: name to wire form. Never changed once in a context,
|
|
42
|
+
* but those of a gateway hook's ({@link withLiveFields}). */
|
|
43
|
+
export type Fields = Readonly<Record<string, string>>;
|
|
44
|
+
|
|
45
|
+
const KEY = createContextKey('encore-request-context');
|
|
46
|
+
|
|
47
|
+
const NAME = /^[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
48
|
+
const BASE64URL = /^[A-Za-z0-9_-]*$/;
|
|
49
|
+
|
|
50
|
+
/** How a field travels, as its declaration says. */
|
|
51
|
+
export interface Config {
|
|
52
|
+
/** Carried on the Pub/Sub messages published with it set. */
|
|
53
|
+
pubsub: boolean;
|
|
54
|
+
/** Put on the spans started with it set. */
|
|
55
|
+
trace: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const NONE: Config = {pubsub: false, trace: false};
|
|
59
|
+
|
|
60
|
+
/** The configs of the fields this process declares. */
|
|
61
|
+
const configs = new Map<string, Config>();
|
|
62
|
+
|
|
63
|
+
/** The configs the runtime config lists, memoised by name. */
|
|
64
|
+
const declared = new Map<string, Config | null>();
|
|
65
|
+
let lookup: ((name: string) => Config | null) | undefined;
|
|
66
|
+
|
|
67
|
+
/** Whether `name` can be a field's: what the parser lets a declaration
|
|
68
|
+
* have, kebab-case, 1 to 63 lowercase letters, digits and hyphens, starting
|
|
69
|
+
* with a letter and ending with a letter or digit. The parser checks the
|
|
70
|
+
* declarations; this only keeps what a request or message carries to names a
|
|
71
|
+
* field could have. */
|
|
72
|
+
export function validName(name: unknown): name is string {
|
|
73
|
+
return typeof name === 'string' && NAME.test(name);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Records the config of the field `name`, declared in this process. */
|
|
77
|
+
export function declare(name: string, config: Config): void {
|
|
78
|
+
configs.set(name, config);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Where {@link configOf} reads the runtime config's fields: set by the
|
|
82
|
+
* runtime as it loads, so this module needs no native code. */
|
|
83
|
+
export function setDeclaredLookup(fn: (name: string) => Config | null): void {
|
|
84
|
+
lookup = fn;
|
|
85
|
+
declared.clear();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The config of the field `name`, as the app declares it.
|
|
90
|
+
*
|
|
91
|
+
* The runtime config lists every field the app declares, so a field this
|
|
92
|
+
* process only binds by name travels as declared. A field it does not list,
|
|
93
|
+
* as without Encore's tooling, has the config this process declares it with,
|
|
94
|
+
* or none.
|
|
95
|
+
*/
|
|
96
|
+
export function configOf(name: string): Config {
|
|
97
|
+
if (lookup !== undefined) {
|
|
98
|
+
let config = declared.get(name);
|
|
99
|
+
if (config === undefined) {
|
|
100
|
+
config = lookup(name);
|
|
101
|
+
declared.set(name, config);
|
|
102
|
+
}
|
|
103
|
+
if (config !== null) return config;
|
|
104
|
+
}
|
|
105
|
+
return configs.get(name) ?? NONE;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The wire form of a JSON value. Throws a `TypeError` when it has none. */
|
|
109
|
+
export function encode(value: unknown): string {
|
|
110
|
+
const json = JSON.stringify(value);
|
|
111
|
+
if (json === undefined) {
|
|
112
|
+
throw new TypeError(`a ${typeOf(value)} has no JSON form`);
|
|
113
|
+
}
|
|
114
|
+
return Buffer.from(json, 'utf8').toString('base64url');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function typeOf(value: unknown): string {
|
|
118
|
+
return value === null ? 'null' : typeof value;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The JSON value of a field's wire form. Throws when it is not base64url
|
|
123
|
+
* without padding, or what that holds is not JSON. Says where it is wrong,
|
|
124
|
+
* never what it holds: a field may hold who a request is made for.
|
|
125
|
+
*/
|
|
126
|
+
export function decode(value: string): unknown {
|
|
127
|
+
if (!BASE64URL.test(value) || value.length % 4 === 1) {
|
|
128
|
+
throw new Error('it is not base64url without padding');
|
|
129
|
+
}
|
|
130
|
+
let text: string;
|
|
131
|
+
try {
|
|
132
|
+
text = new TextDecoder('utf-8', {fatal: true}).decode(
|
|
133
|
+
Buffer.from(value, 'base64url'),
|
|
134
|
+
);
|
|
135
|
+
} catch {
|
|
136
|
+
throw new Error('it is not UTF-8');
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
return JSON.parse(text);
|
|
140
|
+
} catch {
|
|
141
|
+
throw new Error('it is not valid JSON');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The fields of `ctx`, or of the active context: name to wire form. */
|
|
146
|
+
export function current(ctx: Context = activeContext()): Fields {
|
|
147
|
+
const fields = ctx.getValue(KEY);
|
|
148
|
+
return isFields(fields) ? fields : {};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function isFields(value: unknown): value is Fields {
|
|
152
|
+
return typeof value === 'object' && value !== null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A copy of `ctx` holding exactly `fields`. */
|
|
156
|
+
export function withFields(
|
|
157
|
+
fields: Record<string, string>,
|
|
158
|
+
ctx: Context = activeContext(),
|
|
159
|
+
): Context {
|
|
160
|
+
return ctx.setValue(KEY, Object.freeze({...fields}));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* A copy of `ctx` holding `fields` itself, not a copy of them: a field set
|
|
165
|
+
* in `fields` later shows in the context, and in every context made from
|
|
166
|
+
* it, until one of those sets a field of its own. A gateway Middleware's
|
|
167
|
+
* hook runs in one, so its calls carry what it has set on its request so
|
|
168
|
+
* far.
|
|
169
|
+
*/
|
|
170
|
+
export function withLiveFields(
|
|
171
|
+
fields: Record<string, string>,
|
|
172
|
+
ctx: Context = activeContext(),
|
|
173
|
+
): Context {
|
|
174
|
+
return ctx.setValue(KEY, fields);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* A copy of `ctx` with the field `name` set to `value`, a wire form.
|
|
179
|
+
*
|
|
180
|
+
* Throws a `RangeError`, saying what the value does, when it is bigger than
|
|
181
|
+
* the field may be, or makes the fields bigger than a request's may be.
|
|
182
|
+
*/
|
|
183
|
+
export function withValue(
|
|
184
|
+
name: string,
|
|
185
|
+
value: string,
|
|
186
|
+
ctx: Context = activeContext(),
|
|
187
|
+
): Context {
|
|
188
|
+
if (value.length > MAX_VALUE_SIZE) {
|
|
189
|
+
throw new RangeError(
|
|
190
|
+
`encodes to ${value.length} bytes, more than the ${MAX_VALUE_SIZE} a field may`,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
if (configOf(name).pubsub && value.length > MAX_PUBSUB_VALUE_SIZE) {
|
|
194
|
+
throw new RangeError(
|
|
195
|
+
`encodes to ${value.length} bytes, more than the ${MAX_PUBSUB_VALUE_SIZE} ` +
|
|
196
|
+
'a field carried on Pub/Sub messages may',
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const fields = {...current(ctx), [name]: value};
|
|
200
|
+
const size = Object.values(fields).reduce((n, v) => n + v.length, 0);
|
|
201
|
+
if (size > MAX_SIZE) {
|
|
202
|
+
throw new RangeError(
|
|
203
|
+
`makes the request's fields encode to ${size} bytes, more than the ${MAX_SIZE} they may`,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return withFields(fields, ctx);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** A copy of `ctx` without the field `name`. */
|
|
210
|
+
export function without(name: string, ctx: Context = activeContext()): Context {
|
|
211
|
+
const fields = current(ctx);
|
|
212
|
+
if (!Object.hasOwn(fields, name)) return ctx;
|
|
213
|
+
const rest: Record<string, string> = {...fields};
|
|
214
|
+
delete rest[name];
|
|
215
|
+
return withFields(rest, ctx);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The field the header `name` carries, if it carries one. */
|
|
219
|
+
export function headerField(name: string): string | undefined {
|
|
220
|
+
const lowered = name.toLowerCase();
|
|
221
|
+
if (!lowered.startsWith(HEADER_PREFIX)) return undefined;
|
|
222
|
+
const field = lowered.slice(HEADER_PREFIX.length);
|
|
223
|
+
return validName(field) ? field : undefined;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** A fetch `Headers`, or anything with its methods. */
|
|
227
|
+
export interface HeadersLike {
|
|
228
|
+
get(name: string): string | null;
|
|
229
|
+
set(name: string, value: string): void;
|
|
230
|
+
delete(name: string): void;
|
|
231
|
+
forEach(fn: (value: string, name: string) => void): void;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** A request's headers: a fetch `Headers`, Node's `IncomingHttpHeaders`, or
|
|
235
|
+
* a plain record. */
|
|
236
|
+
export type HeaderSource =
|
|
237
|
+
| HeadersLike
|
|
238
|
+
| Readonly<Record<string, string | readonly string[] | undefined>>;
|
|
239
|
+
|
|
240
|
+
/** An outbound request's headers: a fetch `Headers`, or a plain record. */
|
|
241
|
+
export type HeaderSink = HeadersLike | Record<string, string | string[]>;
|
|
242
|
+
|
|
243
|
+
function isHeadersLike(headers: object): headers is HeadersLike {
|
|
244
|
+
return (
|
|
245
|
+
typeof (headers as HeadersLike).forEach === 'function' &&
|
|
246
|
+
typeof (headers as HeadersLike).get === 'function'
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The fields a request's headers carry: the first value of each, under a
|
|
251
|
+
* name in any case. */
|
|
252
|
+
export function fromHeaders(headers: HeaderSource): Record<string, string> {
|
|
253
|
+
const fields: Record<string, string> = {};
|
|
254
|
+
const take = (
|
|
255
|
+
name: string,
|
|
256
|
+
value: string | readonly string[] | undefined,
|
|
257
|
+
) => {
|
|
258
|
+
const field = headerField(name);
|
|
259
|
+
if (field === undefined || Object.hasOwn(fields, field)) return;
|
|
260
|
+
// Fetch joins a repeated header's values with ", ".
|
|
261
|
+
const first = typeof value === 'string' ? value.split(',')[0] : value?.[0];
|
|
262
|
+
if (first !== undefined) fields[field] = first.trim();
|
|
263
|
+
};
|
|
264
|
+
if (isHeadersLike(headers)) {
|
|
265
|
+
headers.forEach((value, name) => take(name, value));
|
|
266
|
+
} else {
|
|
267
|
+
for (const [name, value] of Object.entries(headers)) take(name, value);
|
|
268
|
+
}
|
|
269
|
+
return fields;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Sets the fields of `ctx` on a request's headers, in place of any it
|
|
273
|
+
* carried. */
|
|
274
|
+
export function toHeaders(headers: HeaderSink, ctx: Context): void {
|
|
275
|
+
const fields = current(ctx);
|
|
276
|
+
if (isHeadersLike(headers)) {
|
|
277
|
+
const stale: string[] = [];
|
|
278
|
+
headers.forEach((_, name) => {
|
|
279
|
+
if (name.toLowerCase().startsWith(HEADER_PREFIX)) stale.push(name);
|
|
280
|
+
});
|
|
281
|
+
for (const name of stale) headers.delete(name);
|
|
282
|
+
for (const [name, value] of Object.entries(fields)) {
|
|
283
|
+
headers.set(HEADER_PREFIX + name, value);
|
|
284
|
+
}
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
for (const name of Object.keys(headers)) {
|
|
288
|
+
if (name.toLowerCase().startsWith(HEADER_PREFIX)) delete headers[name];
|
|
289
|
+
}
|
|
290
|
+
for (const [name, value] of Object.entries(fields)) {
|
|
291
|
+
headers[HEADER_PREFIX + name] = value;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Whether the message attribute `key` carries a field. */
|
|
296
|
+
export function isAttribute(key: string): boolean {
|
|
297
|
+
return key.startsWith(ATTRIBUTE_PREFIX);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* `attributes`, with the fields of `ctx` in place of any field attributes
|
|
302
|
+
* they had: every field `ctx` holds. The runtime publishes the message with
|
|
303
|
+
* those the app declares with `pubsub`, and leaves the rest off it.
|
|
304
|
+
*/
|
|
305
|
+
export function messageAttributes(
|
|
306
|
+
attributes: Record<string, string> | undefined,
|
|
307
|
+
ctx: Context = activeContext(),
|
|
308
|
+
): Record<string, string> | undefined {
|
|
309
|
+
const fields = current(ctx);
|
|
310
|
+
const names = Object.keys(fields);
|
|
311
|
+
if (names.length === 0 && attributes === undefined) return undefined;
|
|
312
|
+
const out: Record<string, string> = {};
|
|
313
|
+
for (const [key, value] of Object.entries(attributes ?? {})) {
|
|
314
|
+
if (!isAttribute(key)) out[key] = value;
|
|
315
|
+
}
|
|
316
|
+
for (const name of names) out[ATTRIBUTE_PREFIX + name] = fields[name]!;
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* The fields a delivered message carries, and its other attributes.
|
|
322
|
+
*
|
|
323
|
+
* A message's attributes are its publisher's to write, and nothing signs
|
|
324
|
+
* them: the runtime hands a handler only those of fields the app declares
|
|
325
|
+
* with `pubsub`, and of those only ones under a valid name and within their
|
|
326
|
+
* limit are taken, so a forged attribute can at most claim a field the app
|
|
327
|
+
* chose to carry on messages.
|
|
328
|
+
*/
|
|
329
|
+
export function fromMessageAttributes(attributes: Record<string, string>): {
|
|
330
|
+
fields: Record<string, string>;
|
|
331
|
+
rest: Record<string, string>;
|
|
332
|
+
} {
|
|
333
|
+
const fields: Record<string, string> = {};
|
|
334
|
+
const rest: Record<string, string> = {};
|
|
335
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
336
|
+
if (!isAttribute(key)) {
|
|
337
|
+
rest[key] = value;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const name = key.slice(ATTRIBUTE_PREFIX.length);
|
|
341
|
+
if (validName(name) && value.length <= MAX_PUBSUB_VALUE_SIZE) {
|
|
342
|
+
fields[name] = value;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return {fields, rest};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** The span attributes of the fields of `ctx` declared with `trace`: each
|
|
349
|
+
* the value's JSON, as a string. */
|
|
350
|
+
export function spanAttributes(ctx: Context): Record<string, string> {
|
|
351
|
+
const out: Record<string, string> = {};
|
|
352
|
+
for (const [name, value] of Object.entries(current(ctx))) {
|
|
353
|
+
if (!configOf(name).trace || !BASE64URL.test(value)) continue;
|
|
354
|
+
out[SPAN_ATTRIBUTE_PREFIX + name] = Buffer.from(
|
|
355
|
+
value,
|
|
356
|
+
'base64url',
|
|
357
|
+
).toString('utf8');
|
|
358
|
+
}
|
|
359
|
+
return out;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Extracts the fields a request carries into its context; injects none.
|
|
364
|
+
*
|
|
365
|
+
* An instrumented client sends whatever the global propagator injects to
|
|
366
|
+
* whatever it calls, the app's own services or not, so fields are sent only
|
|
367
|
+
* where the app asks, to its services.
|
|
368
|
+
*/
|
|
369
|
+
export class Propagator implements TextMapPropagator {
|
|
370
|
+
/** `ctx` with the fields `carrier` carries, in place of its own. */
|
|
371
|
+
extract(ctx: Context, carrier: unknown, getter: TextMapGetter): Context {
|
|
372
|
+
const fields: Record<string, string> = {};
|
|
373
|
+
for (const key of getter.keys(carrier)) {
|
|
374
|
+
const name = headerField(key);
|
|
375
|
+
if (name === undefined || Object.hasOwn(fields, name)) continue;
|
|
376
|
+
const value = getter.get(carrier, key);
|
|
377
|
+
const first = Array.isArray(value) ? value[0] : value;
|
|
378
|
+
if (typeof first === 'string') fields[name] = first;
|
|
379
|
+
}
|
|
380
|
+
return Object.keys(fields).length === 0 ? ctx : withFields(fields, ctx);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** Injects nothing: see the class's documentation. */
|
|
384
|
+
inject(): void {}
|
|
385
|
+
|
|
386
|
+
/** None: {@link inject} sets nothing. */
|
|
387
|
+
fields(): string[] {
|
|
388
|
+
return [];
|
|
389
|
+
}
|
|
390
|
+
}
|
package/internal/runtime/mod.ts
CHANGED
|
@@ -2,28 +2,66 @@ import {Decimal} from '../../types/mod';
|
|
|
2
2
|
import {printRuntimeLine} from '../../log/runtime';
|
|
3
3
|
import {setShutdownRuntime, watchShutdownSignals} from './shutdown';
|
|
4
4
|
import {Runtime, setRuntimeLogHandler} from './napi/napi.cjs';
|
|
5
|
+
import {setDeclaredLookup} from '../reqfields';
|
|
5
6
|
|
|
6
7
|
export * from './napi/napi.cjs';
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
/** `ENCORE_TEST=0` (or `false`, `off`): the process is not run as a test. */
|
|
10
|
+
const optedOut = ['0', 'false', 'off'].includes(
|
|
11
|
+
process.env.ENCORE_TEST?.trim() ?? '',
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const testMode = process.env.NODE_ENV === 'test' && !optedOut;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Whether a test runner runs the process: vitest, jest and bun set
|
|
18
|
+
* `NODE_ENV=test`, vitest `VITEST` too, and `node --test` starts each file's
|
|
19
|
+
* process with `NODE_TEST_CONTEXT`. A runner adapter of
|
|
20
|
+
* `@encore.dev/sdk/testing` says so for a runner that sets none of them. The
|
|
21
|
+
* core then runs the process as a test, unless its environment says
|
|
22
|
+
* otherwise (`ENCORE_TEST=0`, a runtime config).
|
|
23
|
+
*/
|
|
24
|
+
const testRunner =
|
|
25
|
+
testMode ||
|
|
26
|
+
!!process.env.VITEST ||
|
|
27
|
+
!!process.env.NODE_TEST_CONTEXT ||
|
|
28
|
+
(globalThis as Record<symbol, unknown>)[Symbol.for('encore.testRunner')] ===
|
|
29
|
+
true;
|
|
9
30
|
|
|
10
31
|
setRuntimeLogHandler(printRuntimeLine);
|
|
11
32
|
|
|
12
33
|
export const RT = new Runtime({
|
|
13
34
|
testMode,
|
|
35
|
+
testRunner,
|
|
14
36
|
typeConstructors: {
|
|
15
37
|
decimal: (val: string | number | bigint) => new Decimal(val),
|
|
16
38
|
},
|
|
17
39
|
});
|
|
18
40
|
|
|
41
|
+
/** Each test's infrastructure: null outside test mode. */
|
|
42
|
+
export const TEST_SCOPES = RT.testScopes();
|
|
43
|
+
|
|
44
|
+
if (TEST_SCOPES !== null) {
|
|
45
|
+
// What the app's .env files set, which `encore test` would have started
|
|
46
|
+
// the process with: the variables it lacks.
|
|
47
|
+
for (const [name, value] of Object.entries(RT.testDotenv())) {
|
|
48
|
+
if (process.env[name] === undefined) process.env[name] = value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
19
52
|
setShutdownRuntime(RT);
|
|
20
|
-
|
|
53
|
+
// How each Request Context field travels, as the app declares it.
|
|
54
|
+
setDeclaredLookup(name => RT.reqctxField(name));
|
|
55
|
+
if (!testMode && TEST_SCOPES === null) watchShutdownSignals();
|
|
21
56
|
|
|
22
57
|
export {runUntilShutdown} from './shutdown';
|
|
23
58
|
|
|
24
59
|
export interface RuntimeConfig {
|
|
25
60
|
/** The service hosted by this process, if any. */
|
|
26
61
|
currentService?: string;
|
|
62
|
+
/** The Worker of that service this process runs, if any: none for the
|
|
63
|
+
* service's main process. */
|
|
64
|
+
currentWorker?: string;
|
|
27
65
|
/** Base URLs of the services this process can reach, keyed by service
|
|
28
66
|
* name. */
|
|
29
67
|
services: Record<string, string>;
|
|
@@ -35,6 +73,7 @@ export function runtimeConfig(): RuntimeConfig {
|
|
|
35
73
|
const cfg = RT.runtimeConfig();
|
|
36
74
|
cached = {
|
|
37
75
|
currentService: cfg.currentService,
|
|
76
|
+
currentWorker: cfg.currentWorker ?? undefined,
|
|
38
77
|
services: cfg.services ?? {},
|
|
39
78
|
};
|
|
40
79
|
}
|