@encore.dev/sdk 0.0.0 → 2.0.0-beta.4

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.
Files changed (250) hide show
  1. package/LICENSE +362 -0
  2. package/README.md +122 -2
  3. package/app_meta.test.ts +134 -0
  4. package/app_meta.ts +186 -0
  5. package/config/config.test.ts +381 -0
  6. package/config/config.ts +178 -0
  7. package/config/mod.ts +14 -0
  8. package/config/secrets.ts +147 -0
  9. package/config/value.ts +243 -0
  10. package/cron/mod.ts +23 -0
  11. package/dist/app_meta.d.ts +93 -0
  12. package/dist/app_meta.js +65 -0
  13. package/dist/app_meta.js.map +1 -0
  14. package/dist/config/config.d.ts +105 -0
  15. package/dist/config/config.js +87 -0
  16. package/dist/config/config.js.map +1 -0
  17. package/dist/config/mod.d.ts +4 -0
  18. package/dist/config/mod.js +3 -0
  19. package/dist/config/mod.js.map +1 -0
  20. package/dist/config/secrets.d.ts +92 -0
  21. package/dist/config/secrets.js +80 -0
  22. package/dist/config/secrets.js.map +1 -0
  23. package/dist/config/value.d.ts +51 -0
  24. package/dist/config/value.js +178 -0
  25. package/dist/config/value.js.map +1 -0
  26. package/dist/cron/mod.d.ts +22 -0
  27. package/dist/cron/mod.js +9 -0
  28. package/dist/cron/mod.js.map +1 -0
  29. package/dist/internal/otel/backend.d.ts +50 -0
  30. package/dist/internal/otel/backend.js +2 -0
  31. package/dist/internal/otel/backend.js.map +1 -0
  32. package/dist/internal/otel/context.d.ts +10 -0
  33. package/dist/internal/otel/context.js +34 -0
  34. package/dist/internal/otel/context.js.map +1 -0
  35. package/dist/internal/otel/context_manager.d.ts +22 -0
  36. package/dist/internal/otel/context_manager.js +120 -0
  37. package/dist/internal/otel/context_manager.js.map +1 -0
  38. package/dist/internal/otel/encode.d.ts +112 -0
  39. package/dist/internal/otel/encode.js +290 -0
  40. package/dist/internal/otel/encode.js.map +1 -0
  41. package/dist/internal/otel/index.d.ts +9 -0
  42. package/dist/internal/otel/index.js +9 -0
  43. package/dist/internal/otel/index.js.map +1 -0
  44. package/dist/internal/otel/meter.d.ts +73 -0
  45. package/dist/internal/otel/meter.js +265 -0
  46. package/dist/internal/otel/meter.js.map +1 -0
  47. package/dist/internal/otel/propagator.d.ts +14 -0
  48. package/dist/internal/otel/propagator.js +61 -0
  49. package/dist/internal/otel/propagator.js.map +1 -0
  50. package/dist/internal/otel/provider.d.ts +9 -0
  51. package/dist/internal/otel/provider.js +19 -0
  52. package/dist/internal/otel/provider.js.map +1 -0
  53. package/dist/internal/otel/request_context.d.ts +11 -0
  54. package/dist/internal/otel/request_context.js +20 -0
  55. package/dist/internal/otel/request_context.js.map +1 -0
  56. package/dist/internal/otel/span.d.ts +61 -0
  57. package/dist/internal/otel/span.js +206 -0
  58. package/dist/internal/otel/span.js.map +1 -0
  59. package/dist/internal/otel/tracer.d.ts +15 -0
  60. package/dist/internal/otel/tracer.js +65 -0
  61. package/dist/internal/otel/tracer.js.map +1 -0
  62. package/dist/internal/reqtrack/mod.d.ts +6 -0
  63. package/dist/internal/reqtrack/mod.js +28 -0
  64. package/dist/internal/reqtrack/mod.js.map +1 -0
  65. package/dist/internal/runtime/meter.d.ts +11 -0
  66. package/dist/internal/runtime/meter.js +22 -0
  67. package/dist/internal/runtime/meter.js.map +1 -0
  68. package/dist/internal/runtime/mod.d.ts +12 -0
  69. package/dist/internal/runtime/mod.js +28 -0
  70. package/dist/internal/runtime/mod.js.map +1 -0
  71. package/dist/internal/runtime/napi/napi.cjs +122 -0
  72. package/dist/internal/runtime/napi/napi.d.cts +601 -0
  73. package/dist/internal/runtime/shutdown.d.ts +13 -0
  74. package/dist/internal/runtime/shutdown.js +56 -0
  75. package/dist/internal/runtime/shutdown.js.map +1 -0
  76. package/dist/internal/utils/constraints.d.ts +9 -0
  77. package/dist/internal/utils/constraints.js +2 -0
  78. package/dist/internal/utils/constraints.js.map +1 -0
  79. package/dist/log/index.d.ts +39 -0
  80. package/dist/log/index.js +44 -0
  81. package/dist/log/index.js.map +1 -0
  82. package/dist/log/logger.d.ts +123 -0
  83. package/dist/log/logger.js +203 -0
  84. package/dist/log/logger.js.map +1 -0
  85. package/dist/log/runtime.d.ts +19 -0
  86. package/dist/log/runtime.js +22 -0
  87. package/dist/log/runtime.js.map +1 -0
  88. package/dist/log/stderr.d.ts +9 -0
  89. package/dist/log/stderr.js +25 -0
  90. package/dist/log/stderr.js.map +1 -0
  91. package/dist/metrics/mod.d.ts +315 -0
  92. package/dist/metrics/mod.js +488 -0
  93. package/dist/metrics/mod.js.map +1 -0
  94. package/dist/mod.d.ts +6 -0
  95. package/dist/mod.js +4 -0
  96. package/dist/mod.js.map +1 -0
  97. package/dist/otel.d.ts +9 -0
  98. package/dist/otel.js +13 -0
  99. package/dist/otel.js.map +1 -0
  100. package/dist/pubsub/encoding.d.ts +3 -0
  101. package/dist/pubsub/encoding.js +10 -0
  102. package/dist/pubsub/encoding.js.map +1 -0
  103. package/dist/pubsub/mod.d.ts +6 -0
  104. package/dist/pubsub/mod.js +3 -0
  105. package/dist/pubsub/mod.js.map +1 -0
  106. package/dist/pubsub/refs.d.ts +12 -0
  107. package/dist/pubsub/refs.js +11 -0
  108. package/dist/pubsub/refs.js.map +1 -0
  109. package/dist/pubsub/subscription.d.ts +128 -0
  110. package/dist/pubsub/subscription.js +39 -0
  111. package/dist/pubsub/subscription.js.map +1 -0
  112. package/dist/pubsub/topic.d.ts +148 -0
  113. package/dist/pubsub/topic.js +164 -0
  114. package/dist/pubsub/topic.js.map +1 -0
  115. package/dist/req_meta.d.ts +47 -0
  116. package/dist/req_meta.js +26 -0
  117. package/dist/req_meta.js.map +1 -0
  118. package/dist/services/index.d.ts +7 -0
  119. package/dist/services/index.js +17 -0
  120. package/dist/services/index.js.map +1 -0
  121. package/dist/shutdown/mod.d.ts +23 -0
  122. package/dist/shutdown/mod.js +24 -0
  123. package/dist/shutdown/mod.js.map +1 -0
  124. package/dist/storage/objects/bucket.d.ts +216 -0
  125. package/dist/storage/objects/bucket.js +123 -0
  126. package/dist/storage/objects/bucket.js.map +1 -0
  127. package/dist/storage/objects/error.d.ts +14 -0
  128. package/dist/storage/objects/error.js +81 -0
  129. package/dist/storage/objects/error.js.map +1 -0
  130. package/dist/storage/objects/mod.d.ts +4 -0
  131. package/dist/storage/objects/mod.js +3 -0
  132. package/dist/storage/objects/mod.js.map +1 -0
  133. package/dist/storage/objects/refs.d.ts +31 -0
  134. package/dist/storage/objects/refs.js +20 -0
  135. package/dist/storage/objects/refs.js.map +1 -0
  136. package/dist/storage/postgres/database.d.ts +295 -0
  137. package/dist/storage/postgres/database.js +378 -0
  138. package/dist/storage/postgres/database.js.map +1 -0
  139. package/dist/storage/postgres/mod.d.ts +2 -0
  140. package/dist/storage/postgres/mod.js +2 -0
  141. package/dist/storage/postgres/mod.js.map +1 -0
  142. package/dist/storage/redis/commands.d.ts +573 -0
  143. package/dist/storage/redis/commands.js +637 -0
  144. package/dist/storage/redis/commands.js.map +1 -0
  145. package/dist/storage/redis/database.d.ts +302 -0
  146. package/dist/storage/redis/database.js +336 -0
  147. package/dist/storage/redis/database.js.map +1 -0
  148. package/dist/storage/redis/mod.d.ts +88 -0
  149. package/dist/storage/redis/mod.js +84 -0
  150. package/dist/storage/redis/mod.js.map +1 -0
  151. package/dist/storage/redis/native.d.ts +8 -0
  152. package/dist/storage/redis/native.js +11 -0
  153. package/dist/storage/redis/native.js.map +1 -0
  154. package/dist/storage/redis/options.d.ts +89 -0
  155. package/dist/storage/redis/options.js +53 -0
  156. package/dist/storage/redis/options.js.map +1 -0
  157. package/dist/storage/redis/pubsub.d.ts +53 -0
  158. package/dist/storage/redis/pubsub.js +88 -0
  159. package/dist/storage/redis/pubsub.js.map +1 -0
  160. package/dist/storage/redis/reply.d.ts +256 -0
  161. package/dist/storage/redis/reply.js +572 -0
  162. package/dist/storage/redis/reply.js.map +1 -0
  163. package/dist/storage/redis/scan.d.ts +11 -0
  164. package/dist/storage/redis/scan.js +76 -0
  165. package/dist/storage/redis/scan.js.map +1 -0
  166. package/dist/storage/redis/script.d.ts +91 -0
  167. package/dist/storage/redis/script.js +143 -0
  168. package/dist/storage/redis/script.js.map +1 -0
  169. package/dist/storage/redis/target.d.ts +47 -0
  170. package/dist/storage/redis/target.js +82 -0
  171. package/dist/storage/redis/target.js.map +1 -0
  172. package/dist/storage/redis/wire.d.ts +105 -0
  173. package/dist/storage/redis/wire.js +333 -0
  174. package/dist/storage/redis/wire.js.map +1 -0
  175. package/dist/tsconfig.build.tsbuildinfo +1 -0
  176. package/dist/types/mod.d.ts +42 -0
  177. package/dist/types/mod.js +65 -0
  178. package/dist/types/mod.js.map +1 -0
  179. package/internal/otel/backend.ts +60 -0
  180. package/internal/otel/context.test.ts +70 -0
  181. package/internal/otel/context.ts +48 -0
  182. package/internal/otel/context_manager.test.ts +154 -0
  183. package/internal/otel/context_manager.ts +141 -0
  184. package/internal/otel/encode.test.ts +266 -0
  185. package/internal/otel/encode.ts +408 -0
  186. package/internal/otel/index.ts +20 -0
  187. package/internal/otel/meter.test.ts +231 -0
  188. package/internal/otel/meter.ts +332 -0
  189. package/internal/otel/propagator.test.ts +105 -0
  190. package/internal/otel/propagator.ts +68 -0
  191. package/internal/otel/provider.ts +23 -0
  192. package/internal/otel/request_context.ts +32 -0
  193. package/internal/otel/span.ts +252 -0
  194. package/internal/otel/tracer.test.ts +352 -0
  195. package/internal/otel/tracer.ts +104 -0
  196. package/internal/reqtrack/mod.ts +39 -0
  197. package/internal/runtime/meter.ts +26 -0
  198. package/internal/runtime/mod.ts +41 -0
  199. package/internal/runtime/napi/napi.cjs +122 -0
  200. package/internal/runtime/napi/napi.d.cts +601 -0
  201. package/internal/runtime/shutdown.test.ts +248 -0
  202. package/internal/runtime/shutdown.ts +64 -0
  203. package/internal/utils/constraints.ts +12 -0
  204. package/log/index.ts +107 -0
  205. package/log/logger.test.ts +194 -0
  206. package/log/logger.ts +308 -0
  207. package/log/runtime.test.ts +65 -0
  208. package/log/runtime.ts +40 -0
  209. package/log/stderr.test.ts +23 -0
  210. package/log/stderr.ts +24 -0
  211. package/metrics/export.test.ts +337 -0
  212. package/metrics/mod.test.ts +180 -0
  213. package/metrics/mod.ts +619 -0
  214. package/mod.ts +22 -0
  215. package/otel.test.ts +84 -0
  216. package/otel.ts +17 -0
  217. package/package.json +121 -5
  218. package/pubsub/encoding.ts +8 -0
  219. package/pubsub/mod.ts +14 -0
  220. package/pubsub/pubsub.test.ts +186 -0
  221. package/pubsub/refs.ts +22 -0
  222. package/pubsub/subscription.ts +180 -0
  223. package/pubsub/topic.test.ts +121 -0
  224. package/pubsub/topic.ts +212 -0
  225. package/req_meta.ts +75 -0
  226. package/services/index.test.ts +30 -0
  227. package/services/index.ts +16 -0
  228. package/shutdown/mod.ts +24 -0
  229. package/storage/objects/bucket.ts +338 -0
  230. package/storage/objects/error.ts +94 -0
  231. package/storage/objects/mod.ts +35 -0
  232. package/storage/objects/refs.ts +69 -0
  233. package/storage/postgres/database.integration.test.ts +175 -0
  234. package/storage/postgres/database.test.ts +241 -0
  235. package/storage/postgres/database.ts +497 -0
  236. package/storage/postgres/mod.ts +15 -0
  237. package/storage/redis/commands.ts +1221 -0
  238. package/storage/redis/database.ts +466 -0
  239. package/storage/redis/mod.ts +135 -0
  240. package/storage/redis/native.ts +11 -0
  241. package/storage/redis/options.ts +119 -0
  242. package/storage/redis/pubsub.ts +92 -0
  243. package/storage/redis/redis.test.ts +706 -0
  244. package/storage/redis/reply.ts +705 -0
  245. package/storage/redis/runtime.test.ts +233 -0
  246. package/storage/redis/scan.ts +125 -0
  247. package/storage/redis/script.ts +162 -0
  248. package/storage/redis/target.ts +131 -0
  249. package/storage/redis/wire.ts +369 -0
  250. package/types/mod.ts +92 -0
@@ -0,0 +1,65 @@
1
+ import * as runtime from './internal/runtime/mod.js';
2
+ /**
3
+ * Returns metadata about the running Encore application.
4
+ *
5
+ * The metadata is cached and is the same object each call,
6
+ * and therefore must not be modified by the caller.
7
+ */
8
+ export function appMeta() {
9
+ // Compute the metadata on first use.
10
+ if (cached === null) {
11
+ const rt = runtime.RT.appMeta();
12
+ cached = {
13
+ appId: rt.appId,
14
+ appSlug: rt.appSlug,
15
+ gateways: rt.gateways,
16
+ environment: {
17
+ name: rt.environment.name,
18
+ type: envType(rt.environment.type),
19
+ cloud: cloudProvider(rt.environment.cloud),
20
+ },
21
+ build: {
22
+ revision: rt.build.revision,
23
+ },
24
+ deploy: {
25
+ id: rt.deploy.id,
26
+ time: new Date(rt.deploy.deployTime),
27
+ hostedServices: rt.deploy.hostedServices,
28
+ },
29
+ };
30
+ }
31
+ return cached;
32
+ }
33
+ function envType(rtType) {
34
+ switch (rtType) {
35
+ case runtime.EnvironmentType.Production:
36
+ return 'production';
37
+ case runtime.EnvironmentType.Development:
38
+ return 'development';
39
+ case runtime.EnvironmentType.Ephemeral:
40
+ return 'ephemeral';
41
+ case runtime.EnvironmentType.Test:
42
+ return 'test';
43
+ default:
44
+ return 'development';
45
+ }
46
+ }
47
+ function cloudProvider(rtType) {
48
+ switch (rtType) {
49
+ case runtime.CloudProvider.AWS:
50
+ return 'aws';
51
+ case runtime.CloudProvider.GCP:
52
+ return 'gcp';
53
+ case runtime.CloudProvider.Azure:
54
+ return 'azure';
55
+ case runtime.CloudProvider.Encore:
56
+ return 'encore';
57
+ case runtime.CloudProvider.Local:
58
+ return 'local';
59
+ default:
60
+ return 'local';
61
+ }
62
+ }
63
+ // The cached app metadata. Set on first use.
64
+ let cached = null;
65
+ //# sourceMappingURL=app_meta.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app_meta.js","sourceRoot":"","sources":["../app_meta.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,wBAAwB,CAAC;AAuHlD;;;;;GAKG;AACH,MAAM,UAAU,OAAO;IACrB,qCAAqC;IACrC,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,MAAM,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC;QAChC,M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AAK,OAAO,CAAC,aAAa,CAAC,KAAK;YAC9B,OAAO,OAAO,CAAC;QACjB,KAAK,OAAO,CAAC,aAAa,CAAC,MAAM;YAC/B,OAAO,QAAQ,CAAC;QAClB,KAAK,OAAO,CAAC,aAAa,CAAC,KAAK;YAC9B,OAAO,OAAO,CAAC;QACjB;YACE,OAAO,OAAO,CAAC;IACnB,CAAC;AACH,CAAC;AAED,6CAA6C;AAC7C,IAAI,MAAM,GAAmB,IAAI,CAAC"}
@@ -0,0 +1,105 @@
1
+ export { Duration } from './value.js';
2
+ /**
3
+ * A config: a typed value a service reads, written in files beside the code
4
+ * and changed while the app runs. Calling it reads the value now.
5
+ *
6
+ * Declare one with {@link config}, {@link sharedConfig} or
7
+ * {@link globalConfig}, giving the value's type as the type argument. Encore
8
+ * reads that type to know what the config's files may hold, and checks them
9
+ * against it before the value reaches the code.
10
+ *
11
+ * @example
12
+ * import { config, type Min, type Max, type Duration } from "@encore.dev/sdk/config";
13
+ *
14
+ * interface Checkout {
15
+ * // Most items one order may hold.
16
+ * maxItems: number & Int & Min<1> & Max<500>;
17
+ * currencies?: string[];
18
+ * paymentTimeout: Duration;
19
+ * }
20
+ *
21
+ * export const checkout = config<Checkout>("checkout");
22
+ *
23
+ * if (items.length > checkout().maxItems) { ... }
24
+ *
25
+ * # Values
26
+ *
27
+ * A config's value is written in a file named after it — `checkout.config.toml`,
28
+ * or `.json` — beside the declaring service's `encore.service.json`, or beside
29
+ * `encore.app.json` for a shared or global config. Files over it change it for some
30
+ * environments only: `checkout.production.config.toml` for environments of a
31
+ * type, `checkout.@prod-eu.config.toml` for one environment, and
32
+ * `checkout.user.config.toml` — gitignored, the user override — for a
33
+ * developer's own `encore run`, never in tests.
34
+ *
35
+ * Calling the config returns a snapshot: the same object until the value
36
+ * changes, which it may while the app runs, without a restart. Read it where
37
+ * it is used rather than keeping it, or use {@link Config.onChange}.
38
+ */
39
+ export interface Config<T> {
40
+ /** The config's value now. */
41
+ (): T;
42
+ /** The name the config was declared with. */
43
+ readonly name: string;
44
+ /**
45
+ * Calls `listener` with each new value the config is given from now on,
46
+ * while the app runs, until the returned function is called, whether or
47
+ * not it is kept. Values that come faster than `listener` is called are
48
+ * coalesced: it gets the latest, once. Does not keep the process alive.
49
+ */
50
+ onChange(listener: (value: T) => void): () => void;
51
+ }
52
+ /**
53
+ * config declares a {@link Config} belonging to the service that declares it,
54
+ * which holds its own value for it. Two services may each declare a config of
55
+ * the same name. The declaration must live inside a service, as a module-level
56
+ * `export const`, with the name a string literal.
57
+ */
58
+ export declare function config<T>(name: string): Config<T>;
59
+ /**
60
+ * sharedConfig declares a {@link Config} with one value for the whole app. A
61
+ * service that reads it is recorded as using it. Unlike {@link config} it may
62
+ * be declared outside a service.
63
+ */
64
+ export declare function sharedConfig<T>(name: string): Config<T>;
65
+ /**
66
+ * globalConfig declares a {@link Config} with one value for the whole app,
67
+ * available to every service without being recorded as used.
68
+ */
69
+ export declare function globalConfig<T>(name: string): Config<T>;
70
+ /**
71
+ * The value is at least `N`. A constraint on a `number` field of a config's
72
+ * type, written as an intersection: `number & Min<1>`. Encore checks the
73
+ * config's files against it; at runtime the field is a plain `number`.
74
+ */
75
+ export type Min<N extends number> = {
76
+ readonly '~encore.min'?: N;
77
+ };
78
+ /** The value is at most `N`: `number & Max<500>`. See {@link Min}. */
79
+ export type Max<N extends number> = {
80
+ readonly '~encore.max'?: N;
81
+ };
82
+ /** The value is a whole number: `number & Int`. See {@link Min}. */
83
+ export type Int = {
84
+ readonly '~encore.int'?: true;
85
+ };
86
+ /** The string is at least `N` characters long: `string & MinLen<3>`. */
87
+ export type MinLen<N extends number> = {
88
+ readonly '~encore.min_len'?: N;
89
+ };
90
+ /** The string is at most `N` characters long: `string & MaxLen<64>`. */
91
+ export type MaxLen<N extends number> = {
92
+ readonly '~encore.max_len'?: N;
93
+ };
94
+ /** The string matches the regular expression `S`: `string & Pattern<"^[A-Z]+$">`. */
95
+ export type Pattern<S extends string> = {
96
+ readonly '~encore.pattern'?: S;
97
+ };
98
+ /** The array has at least `N` items: `string[] & MinItems<1>`. */
99
+ export type MinItems<N extends number> = {
100
+ readonly '~encore.min_items'?: N;
101
+ };
102
+ /** The array has at most `N` items: `string[] & MaxItems<10>`. */
103
+ export type MaxItems<N extends number> = {
104
+ readonly '~encore.max_items'?: N;
105
+ };
@@ -0,0 +1,87 @@
1
+ import * as runtime from '../internal/runtime/mod.js';
2
+ import { secretOfField } from './secrets.js';
3
+ import { build } from './value.js';
4
+ export { Duration } from './value.js';
5
+ /**
6
+ * config declares a {@link Config} belonging to the service that declares it,
7
+ * which holds its own value for it. Two services may each declare a config of
8
+ * the same name. The declaration must live inside a service, as a module-level
9
+ * `export const`, with the name a string literal.
10
+ */
11
+ export function config(name) {
12
+ return makeConfig(name, runtime.RT.config(name));
13
+ }
14
+ /**
15
+ * sharedConfig declares a {@link Config} with one value for the whole app. A
16
+ * service that reads it is recorded as using it. Unlike {@link config} it may
17
+ * be declared outside a service.
18
+ */
19
+ export function sharedConfig(name) {
20
+ return makeConfig(name, runtime.RT.sharedConfig(name));
21
+ }
22
+ /**
23
+ * globalConfig declares a {@link Config} with one value for the whole app,
24
+ * available to every service without being recorded as used.
25
+ */
26
+ export function globalConfig(name) {
27
+ return makeConfig(name, runtime.RT.sharedConfig(name));
28
+ }
29
+ /**
30
+ * The native watches not yet stopped. A watch stops when it is collected, and
31
+ * a caller that drops the function `onChange` returns would otherwise leave
32
+ * nothing that is sure to hold it: its calls could end at any collection.
33
+ */
34
+ const watches = new Set();
35
+ function makeConfig(name, impl) {
36
+ let schema;
37
+ let cached;
38
+ /** The value of one revision, built once for it. */
39
+ const valueOf = (impl, revision) => {
40
+ if (cached?.revision === revision.number)
41
+ return cached.value;
42
+ schema ??= JSON.parse(impl.schema());
43
+ const value = build(schema, JSON.parse(revision.value), secretOfField);
44
+ // Only a newer revision replaces the snapshot: a listener may build one
45
+ // that the value has since moved on from.
46
+ if (cached === undefined || cached.revision < revision.number) {
47
+ cached = { revision: revision.number, value };
48
+ }
49
+ return value;
50
+ };
51
+ const read = () => {
52
+ if (impl === null) {
53
+ // No value at all: every field takes its default, which in TypeScript is
54
+ // none. The runtime config has every config the app declares, so this
55
+ // is an app run outside Encore.
56
+ if (cached === undefined)
57
+ cached = { revision: -1, value: {} };
58
+ return cached.value;
59
+ }
60
+ if (cached === undefined || cached.revision !== impl.revision()) {
61
+ return valueOf(impl, impl.current());
62
+ }
63
+ return cached.value;
64
+ };
65
+ const handle = (() => read());
66
+ Object.defineProperty(handle, 'name', { value: name });
67
+ handle.onChange = (listener) => {
68
+ if (impl === null)
69
+ return () => { };
70
+ const watch = impl.onChange(revision => {
71
+ // A later revision is on its way to this listener too: the latest is
72
+ // the one it gets, once.
73
+ if (revision.number < impl.revision())
74
+ return;
75
+ // The revision it was told of, not the value now, which may have moved
76
+ // on since.
77
+ listener(valueOf(impl, revision));
78
+ });
79
+ watches.add(watch);
80
+ return () => {
81
+ if (watches.delete(watch))
82
+ watch.stop();
83
+ };
84
+ };
85
+ return handle;
86
+ }
87
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../config/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAC,aAAa,EAAC,MAAM,WAAW,CAAC;AACxC,OAAO,EAAC,KAAK,EAAC,MAAM,SAAS,CAAC;AAE9B,OAAO,EAAC,QAAQ,EAAC,MAAM,SAAS,CAAC;AAuDjC;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAI,IAAY;IACpC,OAAO,UAAU,CAAI,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACtD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAI,IAAY;IAC1C,OAAO,UAAU,CAAI,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,YAAY,CAAC,IAAI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AAC,KAAK,CAAC;QACtB,CAAC;QACD,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,QAAQ,KAAK,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC;YAChE,OAAO,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QACvC,CAAC;QACD,OAAO,MAAM,CAAC,KAAK,CAAC;IACtB,CAAC,CAAC;IAEF,MAAM,MAAM,GAAG,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,CAAc,CAAC;IAC3C,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,EAAC,KAAK,EAAE,IAAI,EAAC,CAAC,CAAC;IACrD,MAAM,CAAC,QAAQ,GAAG,CAAC,QAA4B,EAAE,EAAE;QACjD,IAAI,IAAI,KAAK,IAAI;YAAE,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;QACnC,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;YACrC,qEAAqE;YACrE,yBAAyB;YACzB,IAAI,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE;gBAAE,OAAO;YAC9C,uEAAuE;YACvE,YAAY;YACZ,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;QACpC,CAAC,CAAC,CAAC;QACH,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACnB,OAAO,GAAG,EAAE;YACV,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;gBAAE,KAAK,CAAC,IAAI,EAAE,CAAC;QAC1C,CAAC,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export type { Secret, AnySecret } from './secrets.js';
2
+ export { secret, sharedSecret, globalSecret } from './secrets.js';
3
+ export type { Config, Min, Max, Int, MinLen, MaxLen, Pattern, MinItems, MaxItems, } from './config.js';
4
+ export { config, sharedConfig, globalConfig, Duration } from './config.js';
@@ -0,0 +1,3 @@
1
+ export { secret, sharedSecret, globalSecret } from './secrets.js';
2
+ export { config, sharedConfig, globalConfig, Duration } from './config.js';
3
+ //# sourceMappingURL=mod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sourceRoot":"","sources":["../../config/mod.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,MAAM,EAAE,YAAY,EAAE,YAAY,EAAC,MAAM,WAAW,CAAC;AAY7D,OAAO,EAAC,MAAM,EAAE,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAC,MAAM,UAAU,CAAC"}
@@ -0,0 +1,92 @@
1
+ import { StringLiteral } from '../internal/utils/constraints.js';
2
+ /**
3
+ * Secret represents a single secret value that is loaded
4
+ * into the application. It is strongly typed for that secret,
5
+ * so that you can write functions which expect a specific one.
6
+ *
7
+ * You can use {@link AnySecret} to represent any secret without knowing
8
+ * it's name.
9
+ *
10
+ * @example
11
+ *
12
+ * function doFoo(s: Secret<"foo">): void {
13
+ * const foo = s();
14
+ * }
15
+ */
16
+ export interface Secret<Name extends string> {
17
+ /**
18
+ * Returns the current value of the secret.
19
+ *
20
+ * Encore will periodically refresh the value of the secret, so this
21
+ * value may change over time and could be stale for upto a couple of
22
+ * minutes. If you need to ensure you have the latest value, use
23
+ * `latest`.
24
+ */
25
+ (): string;
26
+ /**
27
+ * The name of the secret.
28
+ */
29
+ readonly name: Name;
30
+ /**
31
+ * Marks the type as an Encore secret, for Encore's parser, which reads a
32
+ * config's type to know which of its fields read a secret. Never set.
33
+ *
34
+ * As a field of a config's type, `apiKey: Secret<"StripeKey">` reads the
35
+ * secret of that name in the config's own scope: the declaring service's
36
+ * own for `config()`, the app's for `sharedConfig()` and `globalConfig()`.
37
+ * The secret is declared as any other is, with {@link secret},
38
+ * {@link sharedSecret} or {@link globalSecret}.
39
+ */
40
+ readonly '~encore.secret'?: true;
41
+ }
42
+ /**
43
+ * AnySecret is a type which can be used to represent any {@link Secret}
44
+ * without knowing its name.
45
+ */
46
+ export type AnySecret = Secret<string>;
47
+ /**
48
+ * secret declares a {@link Secret} belonging to the service that declares it.
49
+ *
50
+ * Two services may each declare a secret of the same name and hold different
51
+ * values in them, so adding a secret to one service can never collide with — or
52
+ * silently reuse — a name another service already took. The declaration must
53
+ * therefore live inside a service; Encore reports an error otherwise.
54
+ *
55
+ * For a value several services genuinely share, use {@link sharedSecret}.
56
+ *
57
+ * @example loading a single secret
58
+ * import {secret} from "@encore.dev/sdk/config";
59
+ * const foo = secret("foo");
60
+ */
61
+ export declare function secret<Name extends string>(name: StringLiteral<Name>): Secret<Name>;
62
+ /**
63
+ * sharedSecret declares a {@link Secret} shared by the whole app, readable from
64
+ * any service. Unlike {@link secret} it may be declared outside a service.
65
+ *
66
+ * Use it only for a value several services genuinely share: a shared secret
67
+ * couples every service that reads it, and widening a secret's scope later is
68
+ * easier than narrowing it.
69
+ *
70
+ * @example
71
+ * import {sharedSecret} from "@encore.dev/sdk/config";
72
+ * const signingKey = sharedSecret("SigningKey");
73
+ */
74
+ export declare function sharedSecret<Name extends string>(name: StringLiteral<Name>): Secret<Name>;
75
+ /**
76
+ * globalSecret declares a {@link Secret} with one value for the whole app,
77
+ * available to every service without being granted. Like
78
+ * {@link sharedSecret} it may be declared outside a service.
79
+ *
80
+ * @example
81
+ * import {globalSecret} from "@encore.dev/sdk/config";
82
+ * const telemetryKey = globalSecret("TelemetryKey");
83
+ */
84
+ export declare function globalSecret<Name extends string>(name: StringLiteral<Name>): Secret<Name>;
85
+ /**
86
+ * The secret a config's secret field reads: what the runtime's placeholder
87
+ * for the field stands for. A secret name has one scope across the app, so it
88
+ * is read by name whatever the config's scope. Internal to the config module.
89
+ *
90
+ * @internal
91
+ */
92
+ export declare function secretOfField(name: string): AnySecret;
@@ -0,0 +1,80 @@
1
+ import * as runtime from '../internal/runtime/mod.js';
2
+ /**
3
+ * secret declares a {@link Secret} belonging to the service that declares it.
4
+ *
5
+ * Two services may each declare a secret of the same name and hold different
6
+ * values in them, so adding a secret to one service can never collide with — or
7
+ * silently reuse — a name another service already took. The declaration must
8
+ * therefore live inside a service; Encore reports an error otherwise.
9
+ *
10
+ * For a value several services genuinely share, use {@link sharedSecret}.
11
+ *
12
+ * @example loading a single secret
13
+ * import {secret} from "@encore.dev/sdk/config";
14
+ * const foo = secret("foo");
15
+ */
16
+ export function secret(name) {
17
+ return makeSecret(name, runtime.RT.secret(name));
18
+ }
19
+ /**
20
+ * sharedSecret declares a {@link Secret} shared by the whole app, readable from
21
+ * any service. Unlike {@link secret} it may be declared outside a service.
22
+ *
23
+ * Use it only for a value several services genuinely share: a shared secret
24
+ * couples every service that reads it, and widening a secret's scope later is
25
+ * easier than narrowing it.
26
+ *
27
+ * @example
28
+ * import {sharedSecret} from "@encore.dev/sdk/config";
29
+ * const signingKey = sharedSecret("SigningKey");
30
+ */
31
+ export function sharedSecret(name) {
32
+ return makeSecret(name, runtime.RT.secret(name));
33
+ }
34
+ /**
35
+ * globalSecret declares a {@link Secret} with one value for the whole app,
36
+ * available to every service without being granted. Like
37
+ * {@link sharedSecret} it may be declared outside a service.
38
+ *
39
+ * @example
40
+ * import {globalSecret} from "@encore.dev/sdk/config";
41
+ * const telemetryKey = globalSecret("TelemetryKey");
42
+ */
43
+ export function globalSecret(name) {
44
+ return makeSecret(name, runtime.RT.secret(name));
45
+ }
46
+ /**
47
+ * The secret a config's secret field reads: what the runtime's placeholder
48
+ * for the field stands for. A secret name has one scope across the app, so it
49
+ * is read by name whatever the config's scope. Internal to the config module.
50
+ *
51
+ * @internal
52
+ */
53
+ export function secretOfField(name) {
54
+ return makeSecret(name, runtime.RT.secret(name));
55
+ }
56
+ /**
57
+ * Builds the secret handle around an already-resolved implementation. `impl`
58
+ * is null when the runtime config gives the service no secret of that name.
59
+ */
60
+ function makeSecret(name, impl) {
61
+ const secretObj = () => {
62
+ if (impl === null) {
63
+ // During local development we don't consider missing secrets a fatal error.
64
+ if (runtime.RT.appMeta().environment.cloud === runtime.CloudProvider.Local) {
65
+ return '';
66
+ }
67
+ throw new Error(`secret ${name} is not set`);
68
+ }
69
+ return impl.cached();
70
+ };
71
+ secretObj.toString = () => {
72
+ if (impl === null) {
73
+ return `Secret<${name}>(not set)`;
74
+ }
75
+ return `Secret<${name}>(*********)`;
76
+ };
77
+ Object.defineProperty(secretObj, 'name', { value: name });
78
+ return secretObj;
79
+ }
80
+ //# sourceMappingURL=secrets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secrets.js","sourceRoot":"","sources":["../../config/secrets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,yBAAyB,CAAC;AAoDnD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,MAAM,CACpB,IAAyB;IAEzB,OAAO,UAAU,CAAe,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACjE,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAC1B,IAAyB;IAEzB,OAAO,UAAU,CAAe,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACjE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAC1B,IAAyB;IAEzB,OAAO,UAAU,CAAe,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACjE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,OAAO,UAAU,CAAY,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED;;;GAGG;AACH,SAAS,UAAU,CACjB,IAAY,EACZ,IAA0C;IAE1C,MAAM,SAAS,GAAG,GAAG,EAAE;QACrB,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YAClB,4EAA4E;YAC5E,IACE,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,WAAW,CAAC,KAAK,KAAK,OAAO,CAAC,aAAa,CAAC,KAAK,EACtE,CAAC;gBACD,OAAO,EAAE,CAAC;YACZ,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,UAAU,IAAI,aAAa,CAAC,CAAC;QAC/C,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;IAEF,SAAS,CAAC,QAAQ,GAAG,GAAG,EAAE;QACxB,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YAClB,OAAO,UAAU,IAAI,YAAY,CAAC;QACpC,CAAC;QACD,OAAO,UAAU,IAAI,cAAc,CAAC;IACtC,CAAC,CAAC;IACF,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,EAAC,KAAK,EAAE,IAAI,EAAC,CAAC,CAAC;IAExD,OAAO,SAAyB,CAAC;AACnC,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * What a config's value is built from: the JSON the runtime hands over,
3
+ * turned into what the declared type says, guided by the config's schema.
4
+ * Durations become {@link Duration}s, and the runtime's placeholder for each
5
+ * secret field becomes the secret itself. Kept apart from the runtime, so it
6
+ * can be tested without one.
7
+ */
8
+ /**
9
+ * A length of time, written in config files as a number with a unit, or
10
+ * several: `"30s"`, `"250ms"`, `"1h30m"` (units `ns`, `us`, `ms`, `s`, `m`,
11
+ * `h`, as Go's `time.ParseDuration` reads them, without a sign).
12
+ *
13
+ * @example
14
+ * interface Checkout { paymentTimeout: Duration }
15
+ * setTimeout(retry, checkout().paymentTimeout.milliseconds);
16
+ */
17
+ export declare class Duration {
18
+ /**
19
+ * Marks the type as Encore's duration, for Encore's parser, which reads a
20
+ * config's type to know what its files may hold. Never set.
21
+ */
22
+ readonly '~encore.duration'?: true;
23
+ private readonly ms;
24
+ private constructor();
25
+ /** Reads a duration string; throws on anything else. */
26
+ static parse(text: string): Duration;
27
+ /** A duration of `ms` milliseconds. */
28
+ static ofMilliseconds(ms: number): Duration;
29
+ /** The duration in milliseconds: what `setTimeout` takes. */
30
+ get milliseconds(): number;
31
+ /** The duration in seconds. */
32
+ get seconds(): number;
33
+ /** As a config file writes it: `"1h30m"`, `"250ms"`, `"0s"`. */
34
+ toString(): string;
35
+ toJSON(): string;
36
+ }
37
+ /** The key of the runtime's placeholder for a secret field. */
38
+ export declare const PLACEHOLDER_KEY = "$encore_secret";
39
+ /** A secret's scope, as a placeholder names it. */
40
+ export type SecretScope = 'service' | 'shared' | 'global';
41
+ /** Builds the secret a placeholder stands for. */
42
+ export type SecretOf = (name: string, scope: SecretScope) => unknown;
43
+ type Schema = Record<string, any>;
44
+ /**
45
+ * `value`, as the declared type has it: the JSON the runtime hands over, with
46
+ * each duration string `schema` marks made a {@link Duration}, and the
47
+ * runtime's placeholder at each secret field `schema` marks made the secret
48
+ * `secretOf` builds.
49
+ */
50
+ export declare function build(schema: Schema, value: unknown, secretOf: SecretOf): unknown;
51
+ export {};