@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
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {expect, test} from 'bun:test';
|
|
2
|
+
import {Interval} from './interval';
|
|
3
|
+
|
|
4
|
+
const HOUR = 3_600_000_000;
|
|
5
|
+
|
|
6
|
+
function parts(i: Interval) {
|
|
7
|
+
return [i.months, i.days, i.microseconds];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
test('parses ISO 8601', () => {
|
|
11
|
+
expect(parts(Interval.parse('P1Y2M3DT4H5M6.5S'))).toEqual([
|
|
12
|
+
14,
|
|
13
|
+
3,
|
|
14
|
+
4 * HOUR + 5 * 60_000_000 + 6_500_000,
|
|
15
|
+
]);
|
|
16
|
+
expect(parts(Interval.parse('P2W'))).toEqual([0, 14, 0]);
|
|
17
|
+
expect(parts(Interval.parse('PT0S'))).toEqual([0, 0, 0]);
|
|
18
|
+
expect(parts(Interval.parse('P-1Y-2M3DT-4H-5M-6.000001S'))).toEqual([
|
|
19
|
+
-14,
|
|
20
|
+
3,
|
|
21
|
+
-(4 * HOUR + 5 * 60_000_000 + 6_000_001),
|
|
22
|
+
]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("parses Postgres's default output", () => {
|
|
26
|
+
expect(parts(Interval.parse('1 year 2 mons 3 days 04:05:06.5'))).toEqual([
|
|
27
|
+
14,
|
|
28
|
+
3,
|
|
29
|
+
4 * HOUR + 5 * 60_000_000 + 6_500_000,
|
|
30
|
+
]);
|
|
31
|
+
expect(parts(Interval.parse('-1 days +02:00:00'))).toEqual([0, -1, 2 * HOUR]);
|
|
32
|
+
expect(parts(Interval.parse('-1 mons +40 days -01:00:00'))).toEqual([
|
|
33
|
+
-1,
|
|
34
|
+
40,
|
|
35
|
+
-HOUR,
|
|
36
|
+
]);
|
|
37
|
+
expect(parts(Interval.parse('100:00:00'))).toEqual([0, 0, 100 * HOUR]);
|
|
38
|
+
expect(parts(Interval.parse('00:00:00'))).toEqual([0, 0, 0]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('refuses what it cannot parse', () => {
|
|
42
|
+
for (const s of ['', 'P', 'PT', '1 fortnight', 'P1.5Y', '1 day 2', 'soon']) {
|
|
43
|
+
expect(() => Interval.parse(s)).toThrow(TypeError);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('writes ISO 8601 as Postgres does', () => {
|
|
48
|
+
expect(
|
|
49
|
+
new Interval(14, 3, 4 * HOUR + 5 * 60_000_000 + 6_500_000).toString(),
|
|
50
|
+
).toBe('P1Y2M3DT4H5M6.5S');
|
|
51
|
+
expect(new Interval(0, 0, 0).toString()).toBe('PT0S');
|
|
52
|
+
expect(new Interval(-14, 3, -HOUR - 1).toString()).toBe(
|
|
53
|
+
'P-1Y-2M3DT-1H-0.000001S',
|
|
54
|
+
);
|
|
55
|
+
expect(new Interval(0, 1, 0).toString()).toBe('P1D');
|
|
56
|
+
expect(new Interval(0, 0, 30_000_000).toString()).toBe('PT30S');
|
|
57
|
+
expect(JSON.stringify({d: new Interval(1, 0, 0)})).toBe('{"d":"P1M"}');
|
|
58
|
+
for (const i of [
|
|
59
|
+
new Interval(14, 3, 4 * HOUR + 6_500_000),
|
|
60
|
+
new Interval(-1, 40, -HOUR),
|
|
61
|
+
new Interval(0, 0, 1),
|
|
62
|
+
]) {
|
|
63
|
+
expect(parts(Interval.parse(i.toString()))).toEqual(parts(i));
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('holds only what Postgres does', () => {
|
|
68
|
+
expect(() => new Interval(1.5, 0, 0)).toThrow(RangeError);
|
|
69
|
+
expect(() => new Interval(0, 2 ** 31, 0)).toThrow(RangeError);
|
|
70
|
+
expect(() => new Interval(0, 0, 2 ** 53)).toThrow(RangeError);
|
|
71
|
+
expect(() => new Interval(0, 0, NaN)).toThrow(RangeError);
|
|
72
|
+
});
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
const MICROS_PER_SECOND = 1_000_000;
|
|
2
|
+
const MICROS_PER_MINUTE = 60 * MICROS_PER_SECOND;
|
|
3
|
+
const MICROS_PER_HOUR = 60 * MICROS_PER_MINUTE;
|
|
4
|
+
const INT32_MAX = 2 ** 31 - 1;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A Postgres `interval`, exactly as Postgres holds it: months, days and
|
|
8
|
+
* microseconds apart, since a month has no fixed number of days, nor a day
|
|
9
|
+
* (across a change of daylight saving time) of microseconds.
|
|
10
|
+
*
|
|
11
|
+
* An `interval` column comes back as a string in ISO 8601's form, as
|
|
12
|
+
* Postgres writes one (`'P1Y2M3DT4H5M6.5S'`, and `'P1M-2D'` where the parts'
|
|
13
|
+
* signs differ), which {@link Interval.parse} reads. A parameter of type
|
|
14
|
+
* `interval` takes an `Interval`, a `Temporal.Duration`, or a string in any
|
|
15
|
+
* form Postgres parses (`'1 day 02:00:00'`, `'P1DT2H'`, `'90 minutes'`).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const row = await db.queryRow`SELECT ${new Interval(1, 0, 0)} + now() AS due`;
|
|
19
|
+
* const {d} = await db.queryRow`SELECT interval '1 day 2 hours' AS d`;
|
|
20
|
+
* Interval.parse(d); // months 0, days 1, microseconds 7_200_000_000
|
|
21
|
+
*/
|
|
22
|
+
export class Interval {
|
|
23
|
+
/**
|
|
24
|
+
* @param months - Whole months, within a Postgres `int4`.
|
|
25
|
+
* @param days - Whole days, within a Postgres `int4`.
|
|
26
|
+
* @param microseconds - Whole microseconds, a safe integer.
|
|
27
|
+
*/
|
|
28
|
+
constructor(
|
|
29
|
+
readonly months: number,
|
|
30
|
+
readonly days: number,
|
|
31
|
+
readonly microseconds: number,
|
|
32
|
+
) {
|
|
33
|
+
check('months', months, INT32_MAX);
|
|
34
|
+
check('days', days, INT32_MAX);
|
|
35
|
+
check('microseconds', microseconds, Number.MAX_SAFE_INTEGER);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parses an interval in ISO 8601's form (`P1Y2M3DT4H5M6.5S`, `P2W`), as
|
|
40
|
+
* an `interval` column comes back, or in the form Postgres writes one by
|
|
41
|
+
* default (`1 year 2 mons 3 days 04:05:06.5`, `-1 days +02:00:00`).
|
|
42
|
+
* Postgres's infinite intervals (`infinity`, `-infinity`) have no exact
|
|
43
|
+
* form, and throw. Each part but the seconds is a whole
|
|
44
|
+
* number. To have Postgres parse other forms, pass the string as a query
|
|
45
|
+
* parameter instead.
|
|
46
|
+
*
|
|
47
|
+
* @throws {TypeError} if `s` is in neither form.
|
|
48
|
+
*/
|
|
49
|
+
static parse(s: string): Interval {
|
|
50
|
+
const parsed = parseISO(s) ?? parsePostgres(s);
|
|
51
|
+
if (parsed === null) {
|
|
52
|
+
throw new TypeError(
|
|
53
|
+
`invalid interval ${JSON.stringify(s)}: expected ISO 8601 ("P1M2DT3S") or Postgres ("1 mon 2 days 00:00:03")`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return new Interval(parsed.months, parsed.days, parsed.microseconds);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The interval in ISO 8601's form, as Postgres writes it: `P1Y2M3DT4H5M6.5S`, `PT0S`. */
|
|
60
|
+
toString(): string {
|
|
61
|
+
const years = Math.trunc(this.months / 12);
|
|
62
|
+
const months = this.months % 12;
|
|
63
|
+
const hours = Math.trunc(this.microseconds / MICROS_PER_HOUR);
|
|
64
|
+
let rest = this.microseconds % MICROS_PER_HOUR;
|
|
65
|
+
const minutes = Math.trunc(rest / MICROS_PER_MINUTE);
|
|
66
|
+
rest %= MICROS_PER_MINUTE;
|
|
67
|
+
const date = part(years, 'Y') + part(months, 'M') + part(this.days, 'D');
|
|
68
|
+
let time = part(hours, 'H') + part(minutes, 'M');
|
|
69
|
+
if (rest !== 0) {
|
|
70
|
+
const sign = rest < 0 ? '-' : '';
|
|
71
|
+
const abs = Math.abs(rest);
|
|
72
|
+
const fraction = String(abs % MICROS_PER_SECOND)
|
|
73
|
+
.padStart(6, '0')
|
|
74
|
+
.replace(/0+$/, '');
|
|
75
|
+
time += `${sign}${Math.trunc(abs / MICROS_PER_SECOND)}${fraction ? '.' + fraction : ''}S`;
|
|
76
|
+
}
|
|
77
|
+
if (date === '' && time === '') return 'PT0S';
|
|
78
|
+
return 'P' + date + (time ? 'T' + time : '');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
toJSON(): string {
|
|
82
|
+
return this.toString();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private get __encore_interval(): boolean {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function check(name: string, value: number, max: number) {
|
|
91
|
+
if (!Number.isInteger(value) || Math.abs(value) > max) {
|
|
92
|
+
throw new RangeError(
|
|
93
|
+
`an Interval's ${name} must be an integer of at most ${max} in magnitude, not ${value}`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function part(n: number, unit: string): string {
|
|
99
|
+
return n === 0 ? '' : `${n}${unit}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
interface Parts {
|
|
103
|
+
months: number;
|
|
104
|
+
days: number;
|
|
105
|
+
microseconds: number;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const ISO =
|
|
109
|
+
/^P(?:([+-]?\d+)Y)?(?:([+-]?\d+)M)?(?:([+-]?\d+)W)?(?:([+-]?\d+)D)?(?:T(?:([+-]?\d+)H)?(?:([+-]?\d+)M)?(?:([+-]?\d+(?:\.\d{1,6})?)S)?)?$/i;
|
|
110
|
+
|
|
111
|
+
function parseISO(s: string): Parts | null {
|
|
112
|
+
const m = ISO.exec(s.trim());
|
|
113
|
+
// "P" and "PT" alone name no part.
|
|
114
|
+
if (m === null || m.slice(1).every(g => g === undefined)) return null;
|
|
115
|
+
const n = (g: string | undefined) => (g === undefined ? 0 : Number(g));
|
|
116
|
+
return {
|
|
117
|
+
months: n(m[1]) * 12 + n(m[2]),
|
|
118
|
+
days: n(m[3]) * 7 + n(m[4]),
|
|
119
|
+
microseconds:
|
|
120
|
+
n(m[5]) * MICROS_PER_HOUR +
|
|
121
|
+
n(m[6]) * MICROS_PER_MINUTE +
|
|
122
|
+
seconds(m[7] ?? '0'),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const UNITS: Record<string, keyof Parts | 'years'> = {
|
|
127
|
+
year: 'years',
|
|
128
|
+
years: 'years',
|
|
129
|
+
mon: 'months',
|
|
130
|
+
mons: 'months',
|
|
131
|
+
day: 'days',
|
|
132
|
+
days: 'days',
|
|
133
|
+
};
|
|
134
|
+
const TIME = /^([+-])?(\d+):(\d{2}):(\d{2}(?:\.\d{1,6})?)$/;
|
|
135
|
+
|
|
136
|
+
function parsePostgres(s: string): Parts | null {
|
|
137
|
+
const words = s.trim().split(/\s+/);
|
|
138
|
+
const parts: Parts = {months: 0, days: 0, microseconds: 0};
|
|
139
|
+
let any = false;
|
|
140
|
+
for (let i = 0; i < words.length; i++) {
|
|
141
|
+
const time = TIME.exec(words[i]);
|
|
142
|
+
if (time !== null) {
|
|
143
|
+
const sign = time[1] === '-' ? -1 : 1;
|
|
144
|
+
parts.microseconds +=
|
|
145
|
+
sign *
|
|
146
|
+
(Number(time[2]) * MICROS_PER_HOUR +
|
|
147
|
+
Number(time[3]) * MICROS_PER_MINUTE +
|
|
148
|
+
seconds(time[4]));
|
|
149
|
+
any = true;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const unit = UNITS[words[i + 1]?.toLowerCase()];
|
|
153
|
+
if (!/^[+-]?\d+$/.test(words[i]) || unit === undefined) return null;
|
|
154
|
+
const n = Number(words[i]);
|
|
155
|
+
if (unit === 'years') parts.months += n * 12;
|
|
156
|
+
else parts[unit] += n;
|
|
157
|
+
any = true;
|
|
158
|
+
i++;
|
|
159
|
+
}
|
|
160
|
+
return any ? parts : null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** `s`, a number of seconds with up to six decimals, in microseconds. */
|
|
164
|
+
function seconds(s: string): number {
|
|
165
|
+
const negative = s.startsWith('-');
|
|
166
|
+
const [whole, fraction = ''] = s.replace(/^[+-]/, '').split('.');
|
|
167
|
+
const micros =
|
|
168
|
+
Number(whole) * MICROS_PER_SECOND + Number(fraction.padEnd(6, '0'));
|
|
169
|
+
return negative ? -micros : micros;
|
|
170
|
+
}
|
package/storage/postgres/mod.ts
CHANGED
|
@@ -5,11 +5,15 @@ export {
|
|
|
5
5
|
Transaction,
|
|
6
6
|
Connection,
|
|
7
7
|
} from './database';
|
|
8
|
+
export {DatabaseError, ErrorCode, TooManyRowsError} from './errors';
|
|
9
|
+
export {Interval} from './interval';
|
|
8
10
|
export type {
|
|
9
11
|
DatabaseConfig,
|
|
10
12
|
ReadOptions,
|
|
11
13
|
ExecResult,
|
|
14
|
+
IsolationLevel,
|
|
12
15
|
MigrationsConfig,
|
|
13
16
|
Primitive,
|
|
14
17
|
Row as ResultRow,
|
|
18
|
+
TransactionOptions,
|
|
15
19
|
} from './database';
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import {numberOf} from './reply';
|
|
2
|
+
import type {Target} from './target';
|
|
3
|
+
import {Cmd, ServerError, type Value} from './wire';
|
|
4
|
+
|
|
5
|
+
/** A bitfield integer's type: signed or unsigned, and its width: `"u8"`, `"i5"`. */
|
|
6
|
+
export type BitfieldType = `${'i' | 'u'}${number}`;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Where a bitfield integer starts: at a bit, or at a multiple of its type's
|
|
10
|
+
* width (`"#2"` of a `u8` is bit 16).
|
|
11
|
+
*/
|
|
12
|
+
export type BitOffset = number | `#${number}`;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What a `set` or `incrBy` that leaves its type's range does, from the
|
|
16
|
+
* `overflow` that says so on: wrap around (the default), stop at the type's
|
|
17
|
+
* smallest or largest value, or fail, leaving the value as it was and replying
|
|
18
|
+
* `null`.
|
|
19
|
+
*/
|
|
20
|
+
export type Overflow = 'wrap' | 'sat' | 'fail';
|
|
21
|
+
|
|
22
|
+
function get(c: Cmd, type: BitfieldType, offset: BitOffset) {
|
|
23
|
+
c.arg('GET').arg(type).arg(offset);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function set(
|
|
27
|
+
c: Cmd,
|
|
28
|
+
type: BitfieldType,
|
|
29
|
+
offset: BitOffset,
|
|
30
|
+
value: number | bigint,
|
|
31
|
+
) {
|
|
32
|
+
c.arg('SET').arg(type).arg(offset).arg(value);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function incrBy(
|
|
36
|
+
c: Cmd,
|
|
37
|
+
type: BitfieldType,
|
|
38
|
+
offset: BitOffset,
|
|
39
|
+
increment: number | bigint,
|
|
40
|
+
) {
|
|
41
|
+
c.arg('INCRBY').arg(type).arg(offset).arg(increment);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function overflow(c: Cmd, mode: Overflow) {
|
|
45
|
+
c.arg('OVERFLOW').arg(mode.toUpperCase());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A bitfield's replies, one per `get`, `set` and `incrBy`. */
|
|
49
|
+
function replies(v: Value): (number | null)[] {
|
|
50
|
+
if (v instanceof ServerError) throw v;
|
|
51
|
+
if (!Array.isArray(v))
|
|
52
|
+
throw new TypeError('redis: BITFIELD replied no array');
|
|
53
|
+
return v.map(item => {
|
|
54
|
+
if (item === null) return null;
|
|
55
|
+
if (typeof item === 'bigint' && !Number.isSafeInteger(Number(item))) {
|
|
56
|
+
throw new RangeError(
|
|
57
|
+
`redis: the bitfield integer ${item} does not fit a number`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return numberOf(item);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A string's bitfield integers, read only (`BITFIELD_RO`), made by
|
|
66
|
+
* `bitmap.bitfieldRo`: add the reads, then {@link BitfieldRo.invoke}.
|
|
67
|
+
*/
|
|
68
|
+
export class BitfieldRo {
|
|
69
|
+
/** @internal */
|
|
70
|
+
constructor(
|
|
71
|
+
protected readonly t: Target,
|
|
72
|
+
protected readonly c: Cmd,
|
|
73
|
+
) {}
|
|
74
|
+
|
|
75
|
+
/** Reads the integer of type `type` at `offset`. */
|
|
76
|
+
get(type: BitfieldType, offset: BitOffset): this {
|
|
77
|
+
get(this.c, type, offset);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Runs the operations, replying one value per `get`, `set` and `incrBy`, in
|
|
83
|
+
* order: `null` for one an `overflow('fail')` stopped. Throws a
|
|
84
|
+
* `RangeError` for a value a `number` can't hold exactly.
|
|
85
|
+
*/
|
|
86
|
+
async invoke(): Promise<(number | null)[]> {
|
|
87
|
+
return replies(await this.t.run(this.c));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A string's bitfield integers, made by `bitmap.bitfield`: add the
|
|
93
|
+
* operations, which run in order as one command, then {@link Bitfield.invoke}.
|
|
94
|
+
*/
|
|
95
|
+
export class Bitfield extends BitfieldRo {
|
|
96
|
+
/** Sets the integer of type `type` at `offset`, replying what it was. */
|
|
97
|
+
set(type: BitfieldType, offset: BitOffset, value: number | bigint): this {
|
|
98
|
+
set(this.c, type, offset, value);
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Adds `increment` to the integer at `offset`, replying what it is then. */
|
|
103
|
+
incrBy(
|
|
104
|
+
type: BitfieldType,
|
|
105
|
+
offset: BitOffset,
|
|
106
|
+
increment: number | bigint,
|
|
107
|
+
): this {
|
|
108
|
+
incrBy(this.c, type, offset, increment);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** What the `set`s and `incrBy`s after this do when they leave their range. */
|
|
113
|
+
overflow(mode: Overflow): this {
|
|
114
|
+
overflow(this.c, mode);
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A {@link BitfieldRo} being queued on a pipeline or a transaction. */
|
|
120
|
+
export class QueuedBitfieldRo {
|
|
121
|
+
/** @internal */
|
|
122
|
+
constructor(
|
|
123
|
+
protected readonly queued: Cmd[],
|
|
124
|
+
protected readonly c: Cmd,
|
|
125
|
+
) {}
|
|
126
|
+
|
|
127
|
+
/** Reads the integer of type `type` at `offset`. */
|
|
128
|
+
get(type: BitfieldType, offset: BitOffset): this {
|
|
129
|
+
get(this.c, type, offset);
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Queues the operations, as one command. */
|
|
134
|
+
queue(): void {
|
|
135
|
+
this.queued.push(this.c);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** A {@link Bitfield} being queued on a pipeline or a transaction. */
|
|
140
|
+
export class QueuedBitfield extends QueuedBitfieldRo {
|
|
141
|
+
/** Sets the integer of type `type` at `offset`, replying what it was. */
|
|
142
|
+
set(type: BitfieldType, offset: BitOffset, value: number | bigint): this {
|
|
143
|
+
set(this.c, type, offset, value);
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Adds `increment` to the integer at `offset`, replying what it is then. */
|
|
148
|
+
incrBy(
|
|
149
|
+
type: BitfieldType,
|
|
150
|
+
offset: BitOffset,
|
|
151
|
+
increment: number | bigint,
|
|
152
|
+
): this {
|
|
153
|
+
incrBy(this.c, type, offset, increment);
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** What the `set`s and `incrBy`s after this do when they leave their range. */
|
|
158
|
+
overflow(mode: Overflow): this {
|
|
159
|
+
overflow(this.c, mode);
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {Reply, ReplyPromise} from './reply';
|
|
2
|
+
import type {Target} from './target';
|
|
3
|
+
import {Cmd, type RedisArg} from './wire';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The command an app names itself: checked by the runtime against Redis's
|
|
7
|
+
* command table before it is sent.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export function rawCommand(name: string, readOnly: boolean): Cmd {
|
|
12
|
+
const c = new Cmd('', name, !readOnly);
|
|
13
|
+
c.raw = true;
|
|
14
|
+
c.readOnly = readOnly;
|
|
15
|
+
return c;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A command the command groups lack, made by `command()`: give its arguments
|
|
20
|
+
* in order with {@link CommandInvocation.key} and {@link CommandInvocation.arg},
|
|
21
|
+
* then {@link CommandInvocation.invoke}.
|
|
22
|
+
*/
|
|
23
|
+
export class CommandInvocation {
|
|
24
|
+
/** @internal */
|
|
25
|
+
constructor(
|
|
26
|
+
private readonly t: Target,
|
|
27
|
+
private readonly c: Cmd,
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
/** The next argument, which is a key: it gets the database's prefix. */
|
|
31
|
+
key(key: string): this {
|
|
32
|
+
this.c.key(key);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The next argument, which is not a key: it is sent as it is. */
|
|
37
|
+
arg(arg: RedisArg): this {
|
|
38
|
+
this.c.arg(arg);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Sends the command, replying what the server did, but for the keys the
|
|
44
|
+
* pops and stream reads reply with, which lose the database's prefix. A
|
|
45
|
+
* command the runtime refuses fails with a `TypeError` saying why.
|
|
46
|
+
*/
|
|
47
|
+
invoke(): ReplyPromise {
|
|
48
|
+
return new ReplyPromise(
|
|
49
|
+
(async () => {
|
|
50
|
+
try {
|
|
51
|
+
return new Reply(await this.t.run(this.c));
|
|
52
|
+
} catch (err) {
|
|
53
|
+
return Reply.failed(err);
|
|
54
|
+
}
|
|
55
|
+
})(),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A command the command groups lack being queued on a pipeline or a transaction. */
|
|
61
|
+
export class QueuedCommandInvocation {
|
|
62
|
+
/** @internal */
|
|
63
|
+
constructor(
|
|
64
|
+
private readonly queued: Cmd[],
|
|
65
|
+
private readonly c: Cmd,
|
|
66
|
+
) {}
|
|
67
|
+
|
|
68
|
+
/** The next argument, which is a key: it gets the database's prefix. */
|
|
69
|
+
key(key: string): this {
|
|
70
|
+
this.c.key(key);
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The next argument, which is not a key: it is sent as it is. */
|
|
75
|
+
arg(arg: RedisArg): this {
|
|
76
|
+
this.c.arg(arg);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Queues the command. */
|
|
81
|
+
queue(): void {
|
|
82
|
+
this.queued.push(this.c);
|
|
83
|
+
}
|
|
84
|
+
}
|