@encore.dev/sdk 0.0.0 → 2.0.0-beta.5
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/LICENSE +362 -0
- package/README.md +122 -2
- package/app_meta.test.ts +134 -0
- package/app_meta.ts +186 -0
- package/config/config.test.ts +381 -0
- package/config/config.ts +178 -0
- package/config/mod.ts +14 -0
- package/config/secrets.ts +147 -0
- package/config/value.ts +243 -0
- package/cron/mod.ts +23 -0
- package/dist/app_meta.d.ts +93 -0
- package/dist/app_meta.js +65 -0
- package/dist/app_meta.js.map +1 -0
- package/dist/config/config.d.ts +105 -0
- package/dist/config/config.js +87 -0
- package/dist/config/config.js.map +1 -0
- package/dist/config/mod.d.ts +4 -0
- package/dist/config/mod.js +3 -0
- package/dist/config/mod.js.map +1 -0
- package/dist/config/secrets.d.ts +92 -0
- package/dist/config/secrets.js +80 -0
- package/dist/config/secrets.js.map +1 -0
- package/dist/config/value.d.ts +51 -0
- package/dist/config/value.js +178 -0
- package/dist/config/value.js.map +1 -0
- package/dist/cron/mod.d.ts +22 -0
- package/dist/cron/mod.js +9 -0
- package/dist/cron/mod.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +50 -0
- package/dist/internal/otel/backend.js +2 -0
- package/dist/internal/otel/backend.js.map +1 -0
- package/dist/internal/otel/context.d.ts +10 -0
- package/dist/internal/otel/context.js +34 -0
- package/dist/internal/otel/context.js.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +22 -0
- package/dist/internal/otel/context_manager.js +120 -0
- package/dist/internal/otel/context_manager.js.map +1 -0
- package/dist/internal/otel/encode.d.ts +112 -0
- package/dist/internal/otel/encode.js +290 -0
- package/dist/internal/otel/encode.js.map +1 -0
- package/dist/internal/otel/index.d.ts +9 -0
- package/dist/internal/otel/index.js +9 -0
- package/dist/internal/otel/index.js.map +1 -0
- package/dist/internal/otel/meter.d.ts +73 -0
- package/dist/internal/otel/meter.js +265 -0
- package/dist/internal/otel/meter.js.map +1 -0
- package/dist/internal/otel/propagator.d.ts +14 -0
- package/dist/internal/otel/propagator.js +61 -0
- package/dist/internal/otel/propagator.js.map +1 -0
- package/dist/internal/otel/provider.d.ts +9 -0
- package/dist/internal/otel/provider.js +19 -0
- package/dist/internal/otel/provider.js.map +1 -0
- package/dist/internal/otel/request_context.d.ts +11 -0
- package/dist/internal/otel/request_context.js +20 -0
- package/dist/internal/otel/request_context.js.map +1 -0
- package/dist/internal/otel/span.d.ts +61 -0
- package/dist/internal/otel/span.js +206 -0
- package/dist/internal/otel/span.js.map +1 -0
- package/dist/internal/otel/tracer.d.ts +15 -0
- package/dist/internal/otel/tracer.js +65 -0
- package/dist/internal/otel/tracer.js.map +1 -0
- package/dist/internal/reqtrack/mod.d.ts +6 -0
- package/dist/internal/reqtrack/mod.js +28 -0
- package/dist/internal/reqtrack/mod.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +11 -0
- package/dist/internal/runtime/meter.js +22 -0
- package/dist/internal/runtime/meter.js.map +1 -0
- package/dist/internal/runtime/mod.d.ts +12 -0
- package/dist/internal/runtime/mod.js +28 -0
- package/dist/internal/runtime/mod.js.map +1 -0
- package/dist/internal/runtime/napi/napi.cjs +122 -0
- package/dist/internal/runtime/napi/napi.d.cts +601 -0
- package/dist/internal/runtime/shutdown.d.ts +13 -0
- package/dist/internal/runtime/shutdown.js +56 -0
- package/dist/internal/runtime/shutdown.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +9 -0
- package/dist/internal/utils/constraints.js +2 -0
- package/dist/internal/utils/constraints.js.map +1 -0
- package/dist/log/index.d.ts +39 -0
- package/dist/log/index.js +44 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +123 -0
- package/dist/log/logger.js +203 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/log/runtime.d.ts +19 -0
- package/dist/log/runtime.js +22 -0
- package/dist/log/runtime.js.map +1 -0
- package/dist/log/stderr.d.ts +9 -0
- package/dist/log/stderr.js +25 -0
- package/dist/log/stderr.js.map +1 -0
- package/dist/metrics/mod.d.ts +315 -0
- package/dist/metrics/mod.js +488 -0
- package/dist/metrics/mod.js.map +1 -0
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +4 -0
- package/dist/mod.js.map +1 -0
- package/dist/otel.d.ts +9 -0
- package/dist/otel.js +13 -0
- package/dist/otel.js.map +1 -0
- package/dist/pubsub/encoding.d.ts +3 -0
- package/dist/pubsub/encoding.js +10 -0
- package/dist/pubsub/encoding.js.map +1 -0
- package/dist/pubsub/mod.d.ts +6 -0
- package/dist/pubsub/mod.js +3 -0
- package/dist/pubsub/mod.js.map +1 -0
- package/dist/pubsub/refs.d.ts +12 -0
- package/dist/pubsub/refs.js +11 -0
- package/dist/pubsub/refs.js.map +1 -0
- package/dist/pubsub/subscription.d.ts +128 -0
- package/dist/pubsub/subscription.js +39 -0
- package/dist/pubsub/subscription.js.map +1 -0
- package/dist/pubsub/topic.d.ts +148 -0
- package/dist/pubsub/topic.js +164 -0
- package/dist/pubsub/topic.js.map +1 -0
- package/dist/req_meta.d.ts +47 -0
- package/dist/req_meta.js +26 -0
- package/dist/req_meta.js.map +1 -0
- package/dist/services/index.d.ts +12 -0
- package/dist/services/index.js +22 -0
- package/dist/services/index.js.map +1 -0
- package/dist/shutdown/mod.d.ts +23 -0
- package/dist/shutdown/mod.js +24 -0
- package/dist/shutdown/mod.js.map +1 -0
- package/dist/storage/objects/bucket.d.ts +216 -0
- package/dist/storage/objects/bucket.js +123 -0
- package/dist/storage/objects/bucket.js.map +1 -0
- package/dist/storage/objects/error.d.ts +14 -0
- package/dist/storage/objects/error.js +81 -0
- package/dist/storage/objects/error.js.map +1 -0
- package/dist/storage/objects/mod.d.ts +4 -0
- package/dist/storage/objects/mod.js +3 -0
- package/dist/storage/objects/mod.js.map +1 -0
- package/dist/storage/objects/refs.d.ts +31 -0
- package/dist/storage/objects/refs.js +20 -0
- package/dist/storage/objects/refs.js.map +1 -0
- package/dist/storage/postgres/database.d.ts +295 -0
- package/dist/storage/postgres/database.js +378 -0
- package/dist/storage/postgres/database.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +2 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +573 -0
- package/dist/storage/redis/commands.js +637 -0
- package/dist/storage/redis/commands.js.map +1 -0
- package/dist/storage/redis/database.d.ts +302 -0
- package/dist/storage/redis/database.js +336 -0
- package/dist/storage/redis/database.js.map +1 -0
- package/dist/storage/redis/mod.d.ts +88 -0
- package/dist/storage/redis/mod.js +84 -0
- package/dist/storage/redis/mod.js.map +1 -0
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.js +11 -0
- package/dist/storage/redis/native.js.map +1 -0
- package/dist/storage/redis/options.d.ts +89 -0
- package/dist/storage/redis/options.js +53 -0
- package/dist/storage/redis/options.js.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +53 -0
- package/dist/storage/redis/pubsub.js +88 -0
- package/dist/storage/redis/pubsub.js.map +1 -0
- package/dist/storage/redis/reply.d.ts +256 -0
- package/dist/storage/redis/reply.js +572 -0
- package/dist/storage/redis/reply.js.map +1 -0
- package/dist/storage/redis/scan.d.ts +11 -0
- package/dist/storage/redis/scan.js +76 -0
- package/dist/storage/redis/scan.js.map +1 -0
- package/dist/storage/redis/script.d.ts +91 -0
- package/dist/storage/redis/script.js +143 -0
- package/dist/storage/redis/script.js.map +1 -0
- package/dist/storage/redis/target.d.ts +47 -0
- package/dist/storage/redis/target.js +82 -0
- package/dist/storage/redis/target.js.map +1 -0
- package/dist/storage/redis/wire.d.ts +105 -0
- package/dist/storage/redis/wire.js +333 -0
- package/dist/storage/redis/wire.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/mod.d.ts +42 -0
- package/dist/types/mod.js +65 -0
- package/dist/types/mod.js.map +1 -0
- package/internal/otel/backend.ts +60 -0
- package/internal/otel/context.test.ts +70 -0
- package/internal/otel/context.ts +48 -0
- package/internal/otel/context_manager.test.ts +154 -0
- package/internal/otel/context_manager.ts +141 -0
- package/internal/otel/encode.test.ts +266 -0
- package/internal/otel/encode.ts +408 -0
- package/internal/otel/index.ts +20 -0
- package/internal/otel/meter.test.ts +231 -0
- package/internal/otel/meter.ts +332 -0
- package/internal/otel/propagator.test.ts +105 -0
- package/internal/otel/propagator.ts +68 -0
- package/internal/otel/provider.ts +23 -0
- package/internal/otel/request_context.ts +32 -0
- package/internal/otel/span.ts +252 -0
- package/internal/otel/tracer.test.ts +352 -0
- package/internal/otel/tracer.ts +104 -0
- package/internal/reqtrack/mod.ts +39 -0
- package/internal/runtime/meter.ts +26 -0
- package/internal/runtime/mod.ts +41 -0
- package/internal/runtime/napi/napi.cjs +122 -0
- package/internal/runtime/napi/napi.d.cts +601 -0
- package/internal/runtime/shutdown.test.ts +248 -0
- package/internal/runtime/shutdown.ts +64 -0
- package/internal/utils/constraints.ts +12 -0
- package/log/index.ts +107 -0
- package/log/logger.test.ts +194 -0
- package/log/logger.ts +308 -0
- package/log/runtime.test.ts +65 -0
- package/log/runtime.ts +40 -0
- package/log/stderr.test.ts +23 -0
- package/log/stderr.ts +24 -0
- package/metrics/export.test.ts +337 -0
- package/metrics/mod.test.ts +180 -0
- package/metrics/mod.ts +619 -0
- package/mod.ts +22 -0
- package/otel.test.ts +84 -0
- package/otel.ts +17 -0
- package/package.json +121 -5
- package/pubsub/encoding.ts +8 -0
- package/pubsub/mod.ts +14 -0
- package/pubsub/pubsub.test.ts +186 -0
- package/pubsub/refs.ts +22 -0
- package/pubsub/subscription.ts +180 -0
- package/pubsub/topic.test.ts +121 -0
- package/pubsub/topic.ts +212 -0
- package/req_meta.ts +75 -0
- package/services/index.test.ts +30 -0
- package/services/index.ts +21 -0
- package/shutdown/mod.ts +24 -0
- package/storage/objects/bucket.ts +338 -0
- package/storage/objects/error.ts +94 -0
- package/storage/objects/mod.ts +35 -0
- package/storage/objects/refs.ts +69 -0
- package/storage/postgres/database.integration.test.ts +175 -0
- package/storage/postgres/database.test.ts +241 -0
- package/storage/postgres/database.ts +497 -0
- package/storage/postgres/mod.ts +15 -0
- package/storage/redis/commands.ts +1221 -0
- package/storage/redis/database.ts +466 -0
- package/storage/redis/mod.ts +135 -0
- package/storage/redis/native.ts +11 -0
- package/storage/redis/options.ts +119 -0
- package/storage/redis/pubsub.ts +92 -0
- package/storage/redis/redis.test.ts +706 -0
- package/storage/redis/reply.ts +705 -0
- package/storage/redis/runtime.test.ts +233 -0
- package/storage/redis/scan.ts +125 -0
- package/storage/redis/script.ts +162 -0
- package/storage/redis/target.ts +131 -0
- package/storage/redis/wire.ts +369 -0
- package/types/mod.ts +92 -0
package/app_meta.ts
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import * as runtime from './internal/runtime/mod';
|
|
2
|
+
|
|
3
|
+
/** Describes the running Encore application. */
|
|
4
|
+
export interface AppMeta {
|
|
5
|
+
/**
|
|
6
|
+
* The Encore application ID. If the application is not linked to the Encore platform this will be an empty string.
|
|
7
|
+
* To link to the Encore platform run `encore app link` from your terminal in the root directory of the Encore app.
|
|
8
|
+
*/
|
|
9
|
+
appId: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The Encore application slug — the human-readable identifier shown in the Encore
|
|
13
|
+
* Cloud dashboard and used by the CLI. If the application is not linked to the
|
|
14
|
+
* Encore platform this will be an empty string.
|
|
15
|
+
*
|
|
16
|
+
* Distinct from `appId`: the id is opaque and stable, the slug is readable and may
|
|
17
|
+
* be renamed. Locally the two are the same.
|
|
18
|
+
*/
|
|
19
|
+
appSlug: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The API gateways that serve this application, keyed by gateway name.
|
|
23
|
+
*
|
|
24
|
+
* An application can declare several gateways, so there is no single base URL for
|
|
25
|
+
* the API — look up the gateway you mean by name. A process that serves no gateway
|
|
26
|
+
* (a worker) reports an empty object.
|
|
27
|
+
*/
|
|
28
|
+
gateways: Record<string, GatewayMeta>;
|
|
29
|
+
|
|
30
|
+
/** Information about the environment the app is running in. */
|
|
31
|
+
environment: EnvironmentMeta;
|
|
32
|
+
|
|
33
|
+
/** Information about the build. */
|
|
34
|
+
build: BuildMeta;
|
|
35
|
+
|
|
36
|
+
/** Information about the deployment. */
|
|
37
|
+
deploy: DeployMeta;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Describes an API gateway serving the application. */
|
|
41
|
+
export interface GatewayMeta {
|
|
42
|
+
/** The name of the gateway, as declared by the application. */
|
|
43
|
+
name: string;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The base URL which can be used to call the API of this running application
|
|
47
|
+
* through this gateway.
|
|
48
|
+
*
|
|
49
|
+
* For local development it is "http://localhost:<port>", typically "http://localhost:4000".
|
|
50
|
+
*
|
|
51
|
+
* If a custom domain is used for this environment it is returned here, but note that
|
|
52
|
+
* changes only take effect at the time of deployment while custom domains can be updated at any time.
|
|
53
|
+
*/
|
|
54
|
+
baseUrl: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Describes the environment the Encore application is running in. */
|
|
58
|
+
export interface EnvironmentMeta {
|
|
59
|
+
/**
|
|
60
|
+
* The name of environment that this application.
|
|
61
|
+
* For local development it is "local".
|
|
62
|
+
*/
|
|
63
|
+
name: string;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The type of environment is this application running in.
|
|
67
|
+
* For local development it is "development".
|
|
68
|
+
*/
|
|
69
|
+
type: EnvironmentType;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The cloud this is running in.
|
|
73
|
+
* For local development it is "local".
|
|
74
|
+
*/
|
|
75
|
+
cloud: CloudProvider;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Describes what type of environment the application is running in. */
|
|
79
|
+
export type EnvironmentType =
|
|
80
|
+
// A production environment.
|
|
81
|
+
| 'production'
|
|
82
|
+
// A long-lived cloud-hosted, non-production environment, such as test environments, or local development.
|
|
83
|
+
| 'development'
|
|
84
|
+
// A short-lived cloud-hosted, non-production environments, such as preview environments
|
|
85
|
+
| 'ephemeral'
|
|
86
|
+
// When running automated tests.
|
|
87
|
+
| 'test';
|
|
88
|
+
|
|
89
|
+
/** Describes what cloud provider the application is running in. */
|
|
90
|
+
export type CloudProvider =
|
|
91
|
+
| 'aws' // Amazon Web Services
|
|
92
|
+
| 'gcp' // Google Cloud Platform
|
|
93
|
+
| 'azure' // Microsoft Azure
|
|
94
|
+
| 'encore' // Encore Cloud.
|
|
95
|
+
| 'local'; // Local development
|
|
96
|
+
|
|
97
|
+
/** Information about the build that formed the running application. */
|
|
98
|
+
export interface BuildMeta {
|
|
99
|
+
/** The VCS revision the deployed application was built from. */
|
|
100
|
+
revision: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Information about the deployment of the running application. */
|
|
104
|
+
export interface DeployMeta {
|
|
105
|
+
/** The unique id of the deployment. Generated by the Encore Platform. */
|
|
106
|
+
id: string;
|
|
107
|
+
|
|
108
|
+
/** The time the Encore Platform deployed this build to the environment. */
|
|
109
|
+
time: Date;
|
|
110
|
+
|
|
111
|
+
/** The services hosted by this deployment, keyed by the service name. */
|
|
112
|
+
hostedServices: Record<string, HostedService>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface HostedService {
|
|
116
|
+
/** The name of the service */
|
|
117
|
+
name: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Returns metadata about the running Encore application.
|
|
122
|
+
*
|
|
123
|
+
* The metadata is cached and is the same object each call,
|
|
124
|
+
* and therefore must not be modified by the caller.
|
|
125
|
+
*/
|
|
126
|
+
export function appMeta(): AppMeta {
|
|
127
|
+
// Compute the metadata on first use.
|
|
128
|
+
if (cached === null) {
|
|
129
|
+
const rt = runtime.RT.appMeta();
|
|
130
|
+
cached = {
|
|
131
|
+
appId: rt.appId,
|
|
132
|
+
appSlug: rt.appSlug,
|
|
133
|
+
gateways: rt.gateways,
|
|
134
|
+
environment: {
|
|
135
|
+
name: rt.environment.name,
|
|
136
|
+
type: envType(rt.environment.type),
|
|
137
|
+
cloud: cloudProvider(rt.environment.cloud),
|
|
138
|
+
},
|
|
139
|
+
build: {
|
|
140
|
+
revision: rt.build.revision,
|
|
141
|
+
},
|
|
142
|
+
deploy: {
|
|
143
|
+
id: rt.deploy.id,
|
|
144
|
+
time: new Date(rt.deploy.deployTime),
|
|
145
|
+
hostedServices: rt.deploy.hostedServices,
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return cached;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function envType(rtType: runtime.EnvironmentType): EnvironmentType {
|
|
154
|
+
switch (rtType) {
|
|
155
|
+
case runtime.EnvironmentType.Production:
|
|
156
|
+
return 'production';
|
|
157
|
+
case runtime.EnvironmentType.Development:
|
|
158
|
+
return 'development';
|
|
159
|
+
case runtime.EnvironmentType.Ephemeral:
|
|
160
|
+
return 'ephemeral';
|
|
161
|
+
case runtime.EnvironmentType.Test:
|
|
162
|
+
return 'test';
|
|
163
|
+
default:
|
|
164
|
+
return 'development';
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function cloudProvider(rtType: runtime.CloudProvider): CloudProvider {
|
|
169
|
+
switch (rtType) {
|
|
170
|
+
case runtime.CloudProvider.AWS:
|
|
171
|
+
return 'aws';
|
|
172
|
+
case runtime.CloudProvider.GCP:
|
|
173
|
+
return 'gcp';
|
|
174
|
+
case runtime.CloudProvider.Azure:
|
|
175
|
+
return 'azure';
|
|
176
|
+
case runtime.CloudProvider.Encore:
|
|
177
|
+
return 'encore';
|
|
178
|
+
case runtime.CloudProvider.Local:
|
|
179
|
+
return 'local';
|
|
180
|
+
default:
|
|
181
|
+
return 'local';
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// The cached app metadata. Set on first use.
|
|
186
|
+
let cached: AppMeta | null = null;
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import {describe, expect, test} from 'bun:test';
|
|
2
|
+
import {mkdtempSync} from 'node:fs';
|
|
3
|
+
import {tmpdir} from 'node:os';
|
|
4
|
+
import {join} from 'node:path';
|
|
5
|
+
|
|
6
|
+
import {build, Duration} from './value';
|
|
7
|
+
|
|
8
|
+
/** What a duration string looks like, as `encore_appconfig` checks it. */
|
|
9
|
+
const DURATION_PATTERN = /^([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$/;
|
|
10
|
+
|
|
11
|
+
describe('Duration', () => {
|
|
12
|
+
test("reads exactly what the schema's pattern accepts", () => {
|
|
13
|
+
const table: [string, number | null][] = [
|
|
14
|
+
['30s', 30_000],
|
|
15
|
+
['250ms', 250],
|
|
16
|
+
['1h30m', 5_400_000],
|
|
17
|
+
['1.5s', 1500],
|
|
18
|
+
['0.1s', 100],
|
|
19
|
+
['2m3s4ms', 123_004],
|
|
20
|
+
['10us', 0.01],
|
|
21
|
+
['10µs', 0.01],
|
|
22
|
+
['7ns', 0.000007],
|
|
23
|
+
['0s', 0],
|
|
24
|
+
['', null],
|
|
25
|
+
['30', null],
|
|
26
|
+
['-1s', null],
|
|
27
|
+
['1 s', null],
|
|
28
|
+
['1d', null],
|
|
29
|
+
['s', null],
|
|
30
|
+
['.5s', null],
|
|
31
|
+
];
|
|
32
|
+
for (const [text, ms] of table) {
|
|
33
|
+
expect(DURATION_PATTERN.test(text)).toBe(ms !== null);
|
|
34
|
+
if (ms === null) {
|
|
35
|
+
expect(() => Duration.parse(text)).toThrow();
|
|
36
|
+
} else {
|
|
37
|
+
expect(Duration.parse(text).milliseconds).toBeCloseTo(ms, 9);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('writes itself as a config file would', () => {
|
|
43
|
+
expect(Duration.parse('90s').toString()).toBe('1m30s');
|
|
44
|
+
expect(Duration.parse('1h0m250ms').toString()).toBe('1h250ms');
|
|
45
|
+
expect(Duration.parse('0s').toString()).toBe('0s');
|
|
46
|
+
expect(Duration.parse('90s').seconds).toBe(90);
|
|
47
|
+
expect(JSON.stringify({t: Duration.parse('2h')})).toBe('{"t":"2h"}');
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('build', () => {
|
|
52
|
+
const schema = {
|
|
53
|
+
$defs: {
|
|
54
|
+
Promo: {
|
|
55
|
+
type: 'object',
|
|
56
|
+
properties: {
|
|
57
|
+
code: {type: 'string'},
|
|
58
|
+
ends_in: {type: 'string', 'x-encore-format': 'duration'},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
type: 'object',
|
|
63
|
+
properties: {
|
|
64
|
+
timeout: {type: 'string', 'x-encore-format': 'duration'},
|
|
65
|
+
promo: {anyOf: [{$ref: '#/$defs/Promo'}, {type: 'null'}]},
|
|
66
|
+
retries: {
|
|
67
|
+
type: 'array',
|
|
68
|
+
items: {type: 'string', 'x-encore-format': 'duration'},
|
|
69
|
+
},
|
|
70
|
+
windows: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
additionalProperties: {type: 'string', 'x-encore-format': 'duration'},
|
|
73
|
+
},
|
|
74
|
+
payment: {
|
|
75
|
+
oneOf: [
|
|
76
|
+
{
|
|
77
|
+
type: 'object',
|
|
78
|
+
properties: {
|
|
79
|
+
kind: {const: 'card'},
|
|
80
|
+
hold: {type: 'string', 'x-encore-format': 'duration'},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
type: 'object',
|
|
85
|
+
properties: {kind: {const: 'invoice'}, hold: {type: 'string'}},
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
apiKey: {'x-encore-secret': {name: 'StripeKey', scope: 'shared'}},
|
|
90
|
+
name: {type: 'string'},
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
test('makes durations and secrets of what the schema says', () => {
|
|
95
|
+
const value = build(
|
|
96
|
+
schema,
|
|
97
|
+
{
|
|
98
|
+
timeout: '30s',
|
|
99
|
+
promo: {code: 'FALL26', ends_in: '1h'},
|
|
100
|
+
retries: ['1s', '5s'],
|
|
101
|
+
windows: {eu: '2m'},
|
|
102
|
+
payment: {kind: 'card', hold: '10m'},
|
|
103
|
+
apiKey: {$encore_secret: {name: 'StripeKey', scope: 'shared'}},
|
|
104
|
+
name: '30s',
|
|
105
|
+
},
|
|
106
|
+
(name, scope) => `secret ${name} (${scope})`,
|
|
107
|
+
) as any;
|
|
108
|
+
expect(value.timeout).toBeInstanceOf(Duration);
|
|
109
|
+
expect(value.timeout.seconds).toBe(30);
|
|
110
|
+
expect(value.promo.ends_in.milliseconds).toBe(3_600_000);
|
|
111
|
+
expect(value.retries.map((d: Duration) => d.seconds)).toEqual([1, 5]);
|
|
112
|
+
expect(value.windows.eu.seconds).toBe(120);
|
|
113
|
+
expect(value.payment.hold.seconds).toBe(600);
|
|
114
|
+
expect(value.apiKey).toBe('secret StripeKey (shared)');
|
|
115
|
+
// A string the schema doesn't mark stays a string.
|
|
116
|
+
expect(value.name).toBe('30s');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('makes a secret only of a field the schema says reads one', () => {
|
|
120
|
+
const forged = {$encore_secret: {name: 'StripeKey', scope: 'shared'}};
|
|
121
|
+
const value = build(
|
|
122
|
+
{type: 'object', properties: {extra: {}, name: {type: 'string'}}},
|
|
123
|
+
{extra: forged},
|
|
124
|
+
() => {
|
|
125
|
+
throw new Error('no secret is read');
|
|
126
|
+
},
|
|
127
|
+
) as any;
|
|
128
|
+
expect(value.extra).toEqual(forged);
|
|
129
|
+
|
|
130
|
+
const secretOf = (name: string) => `secret ${name}`;
|
|
131
|
+
expect(() =>
|
|
132
|
+
build(
|
|
133
|
+
schema,
|
|
134
|
+
{apiKey: {$encore_secret: {name: 'Other', scope: 'shared'}}},
|
|
135
|
+
secretOf,
|
|
136
|
+
),
|
|
137
|
+
).toThrow('a secret field reads the secret StripeKey');
|
|
138
|
+
expect(() => build(schema, {apiKey: 'sk_live_123'}, secretOf)).not.toThrow(
|
|
139
|
+
/sk_live/,
|
|
140
|
+
);
|
|
141
|
+
expect(() => build(schema, {apiKey: 'sk_live_123'}, secretOf)).toThrow();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('follows the variant a tagged value is', () => {
|
|
145
|
+
const value = build(
|
|
146
|
+
schema,
|
|
147
|
+
{payment: {kind: 'invoice', hold: 'net 30'}, promo: null},
|
|
148
|
+
() => null,
|
|
149
|
+
) as any;
|
|
150
|
+
expect(value.payment.hold).toBe('net 30');
|
|
151
|
+
expect(value.promo).toBe(null);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The config module against the native runtime, in a child process that
|
|
157
|
+
* starts with the runtime config in its environment (see app_meta.test.ts for
|
|
158
|
+
* why). The probe prints what it read as JSON lines.
|
|
159
|
+
*/
|
|
160
|
+
describe('config', () => {
|
|
161
|
+
const lib = process.env.ENCORE_RUNTIME_LIB;
|
|
162
|
+
const dir = mkdtempSync(join(tmpdir(), 'encore-config-test-'));
|
|
163
|
+
const live = join(dir, 'configs.json');
|
|
164
|
+
|
|
165
|
+
// Each probe is given its own config source, `live` unless told otherwise.
|
|
166
|
+
const run = (probe: string, source = live) => {
|
|
167
|
+
const proc = Bun.spawnSync({
|
|
168
|
+
cmd: [process.execPath, '-e', probe],
|
|
169
|
+
cwd: import.meta.dir,
|
|
170
|
+
env: {...process.env, ENCORE_RUNTIME_CONFIG: runtimeConfig(source)},
|
|
171
|
+
});
|
|
172
|
+
const stdout = proc.stdout.toString();
|
|
173
|
+
if (proc.exitCode !== 0) {
|
|
174
|
+
throw new Error(
|
|
175
|
+
`probe failed (exit ${proc.exitCode}):\n${stdout}\n${proc.stderr}`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
return stdout
|
|
179
|
+
.trim()
|
|
180
|
+
.split('\n')
|
|
181
|
+
.filter(l => l.startsWith('{'))
|
|
182
|
+
.map(l => JSON.parse(l));
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
test.skipIf(!lib)('reads typed values, secret fields and changes', () => {
|
|
186
|
+
const mod = JSON.stringify(`${import.meta.dir}/mod`);
|
|
187
|
+
const out = run(`
|
|
188
|
+
import { writeFileSync } from "node:fs";
|
|
189
|
+
import { config, sharedConfig, Duration } from ${mod};
|
|
190
|
+
const checkout = config("checkout");
|
|
191
|
+
const flags = sharedConfig("flags");
|
|
192
|
+
const missing = config("nothing");
|
|
193
|
+
const a = checkout(), b = checkout();
|
|
194
|
+
console.log(JSON.stringify({
|
|
195
|
+
maxItems: a.max_items,
|
|
196
|
+
timeout: a.timeout instanceof Duration ? a.timeout.seconds : null,
|
|
197
|
+
key: a.stripe_key(),
|
|
198
|
+
keyName: a.stripe_key.name,
|
|
199
|
+
same: a === b,
|
|
200
|
+
flags: flags().new_checkout,
|
|
201
|
+
flagKey: flags().key(),
|
|
202
|
+
missing: missing(),
|
|
203
|
+
name: checkout.name,
|
|
204
|
+
}));
|
|
205
|
+
const stop = checkout.onChange((v) => {
|
|
206
|
+
console.log(JSON.stringify({ changed: v.max_items, timeout: v.timeout.seconds, now: checkout().max_items }));
|
|
207
|
+
stop();
|
|
208
|
+
process.exit(0);
|
|
209
|
+
});
|
|
210
|
+
writeFileSync(${JSON.stringify(live)}, JSON.stringify({
|
|
211
|
+
revision: "2",
|
|
212
|
+
configs: [{ name: "checkout", service: "orders", value: { max_items: 7, timeout: "1m" } }],
|
|
213
|
+
}));
|
|
214
|
+
setTimeout(() => { console.log(JSON.stringify({ timedOut: true })); process.exit(1); }, 5000);
|
|
215
|
+
`);
|
|
216
|
+
expect(out[0]).toEqual({
|
|
217
|
+
maxItems: 50,
|
|
218
|
+
timeout: 30,
|
|
219
|
+
key: 'sk_test_123',
|
|
220
|
+
keyName: 'StripeKey',
|
|
221
|
+
same: true,
|
|
222
|
+
flags: true,
|
|
223
|
+
flagKey: 'shared-flag-key',
|
|
224
|
+
missing: {},
|
|
225
|
+
name: 'checkout',
|
|
226
|
+
});
|
|
227
|
+
expect(out[1]).toEqual({changed: 7, timeout: 60, now: 7});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test.skipIf(!lib)(
|
|
231
|
+
'calls a listener once with the latest of rapid changes',
|
|
232
|
+
() => {
|
|
233
|
+
const mod = JSON.stringify(`${import.meta.dir}/mod`);
|
|
234
|
+
const source = join(dir, 'rapid.json');
|
|
235
|
+
const out = run(
|
|
236
|
+
`
|
|
237
|
+
import { writeFileSync } from "node:fs";
|
|
238
|
+
import { config } from ${mod};
|
|
239
|
+
const checkout = config("checkout");
|
|
240
|
+
const write = (revision, maxItems) => writeFileSync(${JSON.stringify(source)}, JSON.stringify({
|
|
241
|
+
revision,
|
|
242
|
+
configs: [{ name: "checkout", service: "orders", value: { max_items: maxItems } }],
|
|
243
|
+
}));
|
|
244
|
+
checkout.onChange((v) => console.log(JSON.stringify({ changed: v.max_items })));
|
|
245
|
+
// Two changes, each seen by the runtime, before the event loop gets
|
|
246
|
+
// to the listener.
|
|
247
|
+
write("2", 7);
|
|
248
|
+
Bun.sleepSync(1500);
|
|
249
|
+
write("3", 8);
|
|
250
|
+
Bun.sleepSync(1500);
|
|
251
|
+
setTimeout(() => process.exit(0), 1500);
|
|
252
|
+
`,
|
|
253
|
+
source,
|
|
254
|
+
);
|
|
255
|
+
expect(out).toEqual([{changed: 8}]);
|
|
256
|
+
},
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
test.skipIf(!lib)(
|
|
260
|
+
'keeps calling a listener whose stop function was dropped',
|
|
261
|
+
() => {
|
|
262
|
+
const mod = JSON.stringify(`${import.meta.dir}/mod`);
|
|
263
|
+
const source = join(dir, 'dropped.json');
|
|
264
|
+
const out = run(
|
|
265
|
+
`
|
|
266
|
+
import { writeFileSync } from "node:fs";
|
|
267
|
+
import { config } from ${mod};
|
|
268
|
+
const checkout = config("checkout");
|
|
269
|
+
checkout.onChange((v) => {
|
|
270
|
+
console.log(JSON.stringify({ changed: v.max_items }));
|
|
271
|
+
process.exit(0);
|
|
272
|
+
});
|
|
273
|
+
// The stop function is gone. Collect over a few turns of the event
|
|
274
|
+
// loop, which is when the native watch's finalizer gets to run.
|
|
275
|
+
let collections = 0;
|
|
276
|
+
const collect = setInterval(() => {
|
|
277
|
+
Bun.gc(true);
|
|
278
|
+
if (++collections < 10) return;
|
|
279
|
+
clearInterval(collect);
|
|
280
|
+
writeFileSync(${JSON.stringify(source)}, JSON.stringify({
|
|
281
|
+
revision: "2",
|
|
282
|
+
configs: [{ name: "checkout", service: "orders", value: { max_items: 9 } }],
|
|
283
|
+
}));
|
|
284
|
+
}, 20);
|
|
285
|
+
setTimeout(() => { console.log(JSON.stringify({ timedOut: true })); process.exit(1); }, 5000);
|
|
286
|
+
`,
|
|
287
|
+
source,
|
|
288
|
+
);
|
|
289
|
+
expect(out[0]).toEqual({changed: 9});
|
|
290
|
+
},
|
|
291
|
+
);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* A serialized `encore.runtime.v1.RuntimeConfig` hosting service `orders`,
|
|
296
|
+
* with two configs, the secrets their fields read, and `live` as the config
|
|
297
|
+
* source. Hand-encoded, as in app_meta.test.ts.
|
|
298
|
+
*/
|
|
299
|
+
function runtimeConfig(live: string): string {
|
|
300
|
+
const varint = (n: number): number[] => {
|
|
301
|
+
const out: number[] = [];
|
|
302
|
+
for (;;) {
|
|
303
|
+
const b = n & 0x7f;
|
|
304
|
+
n >>>= 7;
|
|
305
|
+
out.push(n ? b | 0x80 : b);
|
|
306
|
+
if (!n) return out;
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
const tag = (field: number, wire: number) => varint((field << 3) | wire);
|
|
310
|
+
const bytes = (field: number, raw: number[]): number[] => [
|
|
311
|
+
...tag(field, 2),
|
|
312
|
+
...varint(raw.length),
|
|
313
|
+
...raw,
|
|
314
|
+
];
|
|
315
|
+
const str = (field: number, value: string) =>
|
|
316
|
+
bytes(field, [...new TextEncoder().encode(value)]);
|
|
317
|
+
const msg = bytes;
|
|
318
|
+
|
|
319
|
+
const checkoutSchema = {
|
|
320
|
+
type: 'object',
|
|
321
|
+
properties: {
|
|
322
|
+
max_items: {type: 'integer', minimum: 1},
|
|
323
|
+
timeout: {type: 'string', 'x-encore-format': 'duration'},
|
|
324
|
+
stripe_key: {'x-encore-secret': {name: 'StripeKey', scope: 'service'}},
|
|
325
|
+
},
|
|
326
|
+
required: ['max_items'],
|
|
327
|
+
additionalProperties: false,
|
|
328
|
+
};
|
|
329
|
+
const flagsSchema = {
|
|
330
|
+
type: 'object',
|
|
331
|
+
properties: {
|
|
332
|
+
new_checkout: {type: 'boolean'},
|
|
333
|
+
key: {'x-encore-secret': {name: 'FlagKey', scope: 'shared'}},
|
|
334
|
+
},
|
|
335
|
+
additionalProperties: false,
|
|
336
|
+
};
|
|
337
|
+
// AppConfig{rid=1, encore_name=2, service=3, json_schema=4, value=5}
|
|
338
|
+
const checkout = [
|
|
339
|
+
...str(1, 'config:orders.checkout'),
|
|
340
|
+
...str(2, 'checkout'),
|
|
341
|
+
...str(3, 'orders'),
|
|
342
|
+
...str(4, JSON.stringify(checkoutSchema)),
|
|
343
|
+
...str(5, JSON.stringify({max_items: 50, timeout: '30s'})),
|
|
344
|
+
];
|
|
345
|
+
const flags = [
|
|
346
|
+
...str(1, 'config:flags'),
|
|
347
|
+
...str(2, 'flags'),
|
|
348
|
+
...str(4, JSON.stringify(flagsSchema)),
|
|
349
|
+
...str(5, JSON.stringify({new_checkout: true})),
|
|
350
|
+
];
|
|
351
|
+
// AppSecret{rid=1, encore_name=2, data=3 SecretData{embedded=1}, services=4}:
|
|
352
|
+
// the service config's field reads service orders' own secret, the shared
|
|
353
|
+
// config's one given to every service.
|
|
354
|
+
const secret = [
|
|
355
|
+
...str(1, 'secret:orders.StripeKey'),
|
|
356
|
+
...str(2, 'StripeKey'),
|
|
357
|
+
...msg(3, str(1, 'sk_test_123')),
|
|
358
|
+
...str(4, 'orders'),
|
|
359
|
+
];
|
|
360
|
+
const flagSecret = [
|
|
361
|
+
...str(1, 'secret:FlagKey'),
|
|
362
|
+
...str(2, 'FlagKey'),
|
|
363
|
+
...msg(3, str(1, 'shared-flag-key')),
|
|
364
|
+
...str(4, '*'),
|
|
365
|
+
];
|
|
366
|
+
// Infrastructure.Resources{app_secrets=5, app_configs=9, config_source=10{file=1}}
|
|
367
|
+
const resources = [
|
|
368
|
+
...msg(5, secret),
|
|
369
|
+
...msg(5, flagSecret),
|
|
370
|
+
...msg(9, checkout),
|
|
371
|
+
...msg(9, flags),
|
|
372
|
+
...msg(10, str(1, live)),
|
|
373
|
+
];
|
|
374
|
+
const infra = msg(1, resources);
|
|
375
|
+
// Deployment{deploy_id=1, hosted_services=5}
|
|
376
|
+
const deployment = [...str(1, 'deploy-1'), ...msg(5, str(1, 'orders'))];
|
|
377
|
+
// Environment{app_id=1, env_name=4}
|
|
378
|
+
const environment = [...str(1, 'app'), ...str(4, 'local')];
|
|
379
|
+
const cfg = [...msg(1, environment), ...msg(2, infra), ...msg(3, deployment)];
|
|
380
|
+
return Buffer.from(Uint8Array.from(cfg)).toString('base64');
|
|
381
|
+
}
|