@terraforge/core 0.0.9 → 0.0.11

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.
@@ -0,0 +1,447 @@
1
+ import { S3Client } from "@aws-sdk/client-s3";
2
+ import { DynamoDB } from "@aws-sdk/client-dynamodb";
3
+ import { UUID } from "node:crypto";
4
+ import { AwsCredentialIdentity, AwsCredentialIdentityProvider } from "@aws-sdk/types";
5
+
6
+ //#region src/future.d.ts
7
+ declare class Future<T = unknown> {
8
+ protected callback: (resolve: (data: T) => void, reject: (error: unknown) => void) => void;
9
+ protected listeners: Set<{
10
+ resolve: (data: T) => void;
11
+ reject?: (error: unknown) => void;
12
+ }>;
13
+ protected status: 0 | 1 | 2 | 3;
14
+ protected data?: T;
15
+ protected error?: unknown;
16
+ constructor(callback: (resolve: (data: T) => void, reject: (error: unknown) => void) => void);
17
+ get [Symbol.toStringTag](): string;
18
+ pipe<N>(cb: (value: T) => N): Future<Awaited<N>>;
19
+ then(resolve: (data: T) => void, reject?: (error: unknown) => void): void;
20
+ }
21
+ //#endregion
22
+ //#region src/input.d.ts
23
+ type Input<T = unknown> = T | Output<T> | Future<T> | Promise<T>;
24
+ type OptionalInput<T = unknown> = Input<T> | Input<T | undefined> | Input<undefined>;
25
+ type UnwrapInputArray<T extends Input[]> = { [K in keyof T]: UnwrapInput<T[K]> };
26
+ type UnwrapInput<T> = T extends Input<infer V> ? V : T;
27
+ declare const findInputDeps: (props: unknown) => Meta[];
28
+ declare const resolveInputs: <T>(inputs: T) => Promise<T>;
29
+ //#endregion
30
+ //#region src/output.d.ts
31
+ type OptionalOutput<T = unknown> = Output<T | undefined>;
32
+ declare class Output<T = unknown> extends Future<T> {
33
+ readonly dependencies: Set<Meta>;
34
+ constructor(dependencies: Set<Meta>, callback: (resolve: (data: T) => void, reject: (error: unknown) => void) => void);
35
+ pipe<N>(cb: (value: T) => N): Output<Awaited<N>>;
36
+ }
37
+ declare const deferredOutput: <T>(cb: (resolve: (data: T) => void) => void) => Output<T>;
38
+ declare const output: <T>(value: T) => Output<T>;
39
+ declare const combine: <T extends Input[], R = UnwrapInputArray<T>>(...inputs: T) => Output<R>;
40
+ declare const resolve: <T extends [Input, ...Input[]], R>(inputs: T, transformer: (...inputs: UnwrapInputArray<T>) => R) => Output<Awaited<R>>;
41
+ declare const interpolate: (literals: TemplateStringsArray, ...placeholders: Input<any>[]) => Output<string>;
42
+ //#endregion
43
+ //#region src/urn.d.ts
44
+ type URN = `urn:${string}`;
45
+ //#endregion
46
+ //#region src/node.d.ts
47
+ declare const nodeMetaSymbol: unique symbol;
48
+ type Node<T extends Tag = Tag, O extends State = State> = {
49
+ readonly [nodeMetaSymbol]: Meta<T>;
50
+ readonly urn: URN;
51
+ } & O;
52
+ declare const isNode: (obj: object) => obj is {
53
+ [nodeMetaSymbol]: Meta;
54
+ };
55
+ declare function getMeta(node: Resource): ResourceMeta;
56
+ declare function getMeta(node: DataSource): DataSourceMeta;
57
+ declare function getMeta(node: Node): Meta;
58
+ declare const isResource: (obj: object) => obj is Resource;
59
+ declare const isDataSource: (obj: object) => obj is DataSource;
60
+ //#endregion
61
+ //#region src/resource.d.ts
62
+ type ResourceConfig = Config & {
63
+ /** Import an existing resource instead of creating a new resource. */
64
+ import?: string;
65
+ /** If true the resource will be retained in the backing cloud provider during a delete operation. */
66
+ retainOnDelete?: boolean;
67
+ /** If set, the provider’s Delete method will not be called for this resource if the specified resource is being deleted as well. */
68
+ /** Declare that changes to certain properties should be treated as forcing a replacement. */
69
+ replaceOnChanges?: string[];
70
+ /** If true, create the replacement before deleting the existing resource. */
71
+ createBeforeReplace?: boolean;
72
+ };
73
+ type ResourceMeta = Meta<'resource', ResourceConfig>;
74
+ type Resource<O extends State = State> = O & {
75
+ readonly [nodeMetaSymbol]: ResourceMeta;
76
+ readonly urn: URN;
77
+ };
78
+ type ResourceClass<I extends State = State, O extends State = State> = {
79
+ new (parent: Group, id: string, props: I, config?: ResourceConfig): Resource<O>;
80
+ get(parent: Group, id: string, physicalId: string): DataSource<O>;
81
+ };
82
+ //#endregion
83
+ //#region src/stack.d.ts
84
+ declare class Stack extends Group {
85
+ readonly app: App;
86
+ readonly dependencies: Set<Stack>;
87
+ constructor(app: App, name: string);
88
+ dependsOn(...stacks: Stack[]): this;
89
+ }
90
+ //#endregion
91
+ //#region src/meta.d.ts
92
+ type Tag = 'resource' | 'data';
93
+ type State = Record<string, any>;
94
+ type Config = {
95
+ /** Specify additional explicit dependencies in addition to the ones in the dependency graph. */
96
+ dependsOn?: Resource[];
97
+ /** Pass an ID of an explicitly configured provider, instead of using the default provider. */
98
+ provider?: string;
99
+ };
100
+ type Meta<T extends Tag = Tag, C extends Config = Config> = {
101
+ readonly tag: T;
102
+ readonly urn: URN;
103
+ readonly logicalId: string;
104
+ readonly type: string;
105
+ readonly stack: Stack;
106
+ readonly provider: string;
107
+ readonly input: State;
108
+ readonly config?: C;
109
+ readonly dependencies: Set<URN>;
110
+ readonly resolve: (data: State) => void;
111
+ readonly output: <V>(cb: (data: State) => V) => Output<V>;
112
+ };
113
+ declare const createMeta: <T extends Tag = Tag, C extends Config = Config>(tag: T, provider: string, parent: Group, type: string, logicalId: string, input: State, config?: C) => Meta<T, C>;
114
+ //#endregion
115
+ //#region src/data-source.d.ts
116
+ type DataSourceMeta = Meta<'data'>;
117
+ type DataSource<O extends State = State> = {
118
+ readonly [nodeMetaSymbol]: DataSourceMeta;
119
+ readonly urn: URN;
120
+ } & O;
121
+ type DataSourceFunction<I extends State = State, O extends State = State> = (parent: Group, id: string, input: I, config?: Config) => DataSource<O>;
122
+ //#endregion
123
+ //#region src/group.d.ts
124
+ declare class Group {
125
+ readonly parent: Group | undefined;
126
+ readonly type: string;
127
+ readonly name: string;
128
+ protected children: Array<Group | Node>;
129
+ constructor(parent: Group | undefined, type: string, name: string);
130
+ get urn(): URN;
131
+ protected addChild(child: Group | Node): void;
132
+ add(...children: Array<Group | Node>): void;
133
+ get nodes(): Node[];
134
+ get resources(): Resource[];
135
+ get dataSources(): DataSource[];
136
+ }
137
+ //#endregion
138
+ //#region src/app.d.ts
139
+ declare class App extends Group {
140
+ readonly name: string;
141
+ constructor(name: string);
142
+ get stacks(): Stack[];
143
+ }
144
+ //#endregion
145
+ //#region src/debug.d.ts
146
+ declare const enableDebug: () => void;
147
+ declare const createDebugger: (group: string) => (...args: unknown[]) => void;
148
+ //#endregion
149
+ //#region src/backend/lock.d.ts
150
+ interface LockBackend {
151
+ insecureReleaseLock(urn: URN): Promise<void>;
152
+ locked(urn: URN): Promise<boolean>;
153
+ lock(urn: URN): Promise<() => Promise<void>>;
154
+ }
155
+ //#endregion
156
+ //#region src/workspace/state.d.ts
157
+ type AppState = {
158
+ name: string;
159
+ version?: number;
160
+ idempotentToken?: UUID;
161
+ stacks: Record<URN, StackState>;
162
+ };
163
+ type StackState = {
164
+ name: string;
165
+ nodes: Record<URN, NodeState>;
166
+ };
167
+ type NodeState = {
168
+ tag: 'resource' | 'data';
169
+ type: string;
170
+ version?: number;
171
+ provider: string;
172
+ input: State;
173
+ output: State;
174
+ dependencies: URN[];
175
+ lifecycle?: {
176
+ retainOnDelete?: boolean;
177
+ deleteAfterCreate?: boolean;
178
+ };
179
+ };
180
+ //#endregion
181
+ //#region src/backend/state.d.ts
182
+ type StateBackend = {
183
+ get(urn: URN): Promise<AppState | undefined>;
184
+ update(urn: URN, state: AppState): Promise<void>;
185
+ delete(urn: URN): Promise<void>;
186
+ };
187
+ //#endregion
188
+ //#region src/provider.d.ts
189
+ type CreateProps<T = State> = {
190
+ type: string;
191
+ state: T;
192
+ idempotantToken?: string;
193
+ };
194
+ type UpdateProps<T = State> = {
195
+ type: string;
196
+ priorState: T;
197
+ proposedState: T;
198
+ idempotantToken?: string;
199
+ };
200
+ type DeleteProps<T = State> = {
201
+ type: string;
202
+ state: T;
203
+ idempotantToken?: string;
204
+ };
205
+ type PlanProps<T = State> = {
206
+ type: string;
207
+ priorState: T;
208
+ proposedState: T;
209
+ };
210
+ type GetProps<T = State> = {
211
+ type: string;
212
+ state: T;
213
+ };
214
+ type GetDataProps<T = State> = {
215
+ type: string;
216
+ state: T;
217
+ };
218
+ interface Provider {
219
+ ownResource(id: string): boolean;
220
+ getResource(props: GetProps): Promise<{
221
+ version: number;
222
+ state: State;
223
+ }>;
224
+ createResource(props: CreateProps): Promise<{
225
+ version: number;
226
+ state: State;
227
+ }>;
228
+ updateResource(props: UpdateProps): Promise<{
229
+ version: number;
230
+ state: State;
231
+ }>;
232
+ deleteResource(props: DeleteProps): Promise<void>;
233
+ planResourceChange?(props: PlanProps): Promise<{
234
+ version: number;
235
+ state: State;
236
+ requiresReplacement: boolean;
237
+ }>;
238
+ getData?(props: GetDataProps): Promise<{
239
+ state: State;
240
+ }>;
241
+ destroy?(): Promise<void>;
242
+ }
243
+ //#endregion
244
+ //#region src/workspace/hooks.d.ts
245
+ type ResourceEvent = {
246
+ urn: URN;
247
+ type: string;
248
+ };
249
+ type BeforeResourceCreateEvent = ResourceEvent & {
250
+ resource: Resource;
251
+ newInput: State;
252
+ };
253
+ type AfterResourceCreateEvent = ResourceEvent & {
254
+ resource: Resource;
255
+ newInput: State;
256
+ newOutput: State;
257
+ };
258
+ type BeforeResourceUpdateEvent = ResourceEvent & {
259
+ resource: Resource;
260
+ oldInput: State;
261
+ newInput: State;
262
+ oldOutput: State;
263
+ };
264
+ type AfterResourceUpdateEvent = ResourceEvent & {
265
+ resource: Resource;
266
+ oldInput: State;
267
+ newInput: State;
268
+ oldOutput: State;
269
+ newOutput: State;
270
+ };
271
+ type BeforeResourceDeleteEvent = ResourceEvent & {
272
+ oldInput: State;
273
+ oldOutput: State;
274
+ };
275
+ type AfterResourceDeleteEvent = ResourceEvent & {
276
+ oldInput: State;
277
+ oldOutput: State;
278
+ };
279
+ type Hooks = {
280
+ beforeResourceCreate?: (event: BeforeResourceCreateEvent) => Promise<void> | void;
281
+ beforeResourceUpdate?: (event: BeforeResourceUpdateEvent) => Promise<void> | void;
282
+ beforeResourceDelete?: (event: BeforeResourceDeleteEvent) => Promise<void> | void;
283
+ afterResourceCreate?: (event: AfterResourceCreateEvent) => Promise<void> | void;
284
+ afterResourceUpdate?: (event: AfterResourceUpdateEvent) => Promise<void> | void;
285
+ afterResourceDelete?: (event: AfterResourceDeleteEvent) => Promise<void> | void;
286
+ };
287
+ //#endregion
288
+ //#region src/workspace/workspace.d.ts
289
+ type ProcedureOptions = {
290
+ filters?: string[];
291
+ idempotentToken?: UUID;
292
+ };
293
+ type WorkSpaceOptions = {
294
+ providers: Provider[];
295
+ concurrency?: number;
296
+ backend: {
297
+ state: StateBackend;
298
+ lock: LockBackend;
299
+ };
300
+ hooks?: Hooks;
301
+ };
302
+ declare class WorkSpace {
303
+ protected props: WorkSpaceOptions;
304
+ constructor(props: WorkSpaceOptions);
305
+ /**
306
+ * Deploy the entire app or use the filter option to deploy specific stacks inside your app.
307
+ */
308
+ deploy(app: App, options?: ProcedureOptions): Promise<void>;
309
+ /**
310
+ * Delete the entire app or use the filter option to delete specific stacks inside your app.
311
+ */
312
+ delete(app: App, options?: ProcedureOptions): Promise<void>;
313
+ /**
314
+ * Hydrate the outputs of the resources & data-sources inside your app.
315
+ */
316
+ hydrate(app: App): Promise<void>;
317
+ /**
318
+ * Refresh the state of the resources & data-sources inside your app.
319
+ */
320
+ refresh(app: App): Promise<void>;
321
+ protected destroyProviders(): Promise<void>;
322
+ }
323
+ //#endregion
324
+ //#region src/workspace/operation.d.ts
325
+ type ResourceOperation = 'create' | 'update' | 'delete' | 'replace' | 'import' | 'resolve' | 'get';
326
+ //#endregion
327
+ //#region src/workspace/error.d.ts
328
+ declare class ResourceError extends Error {
329
+ readonly urn: URN;
330
+ readonly type: string;
331
+ readonly operation: ResourceOperation;
332
+ static wrap(urn: URN, type: string, operation: ResourceOperation, error: unknown): ResourceError;
333
+ constructor(urn: URN, type: string, operation: ResourceOperation, message: string);
334
+ }
335
+ declare class AppError extends Error {
336
+ readonly app: string;
337
+ readonly issues: (ResourceError | Error)[];
338
+ constructor(app: string, issues: (ResourceError | Error)[], message: string);
339
+ }
340
+ declare class ResourceNotFound extends Error {}
341
+ declare class ResourceAlreadyExists extends Error {}
342
+ //#endregion
343
+ //#region src/backend/memory/state.d.ts
344
+ declare class MemoryStateBackend implements StateBackend {
345
+ protected states: Map<`urn:${string}`, AppState>;
346
+ get(urn: URN): Promise<AppState | undefined>;
347
+ update(urn: URN, state: AppState): Promise<void>;
348
+ delete(urn: URN): Promise<void>;
349
+ clear(): void;
350
+ }
351
+ //#endregion
352
+ //#region src/backend/memory/lock.d.ts
353
+ declare class MemoryLockBackend implements LockBackend {
354
+ protected locks: Map<`urn:${string}`, number>;
355
+ insecureReleaseLock(urn: URN): Promise<void>;
356
+ locked(urn: URN): Promise<boolean>;
357
+ lock(urn: URN): Promise<() => Promise<void>>;
358
+ clear(): void;
359
+ }
360
+ //#endregion
361
+ //#region src/backend/file/state.d.ts
362
+ declare class FileStateBackend implements StateBackend {
363
+ private props;
364
+ constructor(props: {
365
+ dir: string;
366
+ });
367
+ private stateFile;
368
+ private mkdir;
369
+ get(urn: URN): Promise<AppState | undefined>;
370
+ update(urn: URN, state: AppState): Promise<void>;
371
+ delete(urn: URN): Promise<void>;
372
+ }
373
+ //#endregion
374
+ //#region src/backend/file/lock.d.ts
375
+ declare class FileLockBackend implements LockBackend {
376
+ private props;
377
+ constructor(props: {
378
+ dir: string;
379
+ });
380
+ private lockFile;
381
+ private mkdir;
382
+ insecureReleaseLock(urn: URN): Promise<void>;
383
+ locked(urn: URN): Promise<boolean>;
384
+ lock(urn: URN): Promise<() => Promise<void>>;
385
+ }
386
+ //#endregion
387
+ //#region src/backend/aws/s3-state.d.ts
388
+ type Props$1 = {
389
+ credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider;
390
+ region: string;
391
+ bucket: string;
392
+ };
393
+ declare class S3StateBackend implements StateBackend {
394
+ private props;
395
+ protected client: S3Client;
396
+ constructor(props: Props$1);
397
+ get(urn: URN): Promise<any>;
398
+ update(urn: URN, state: AppState): Promise<void>;
399
+ delete(urn: URN): Promise<void>;
400
+ }
401
+ //#endregion
402
+ //#region src/backend/aws/dynamodb-lock.d.ts
403
+ type Props = {
404
+ credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider;
405
+ region: string;
406
+ tableName: string;
407
+ };
408
+ declare class DynamoLockBackend implements LockBackend {
409
+ private props;
410
+ protected client: DynamoDB;
411
+ constructor(props: Props);
412
+ insecureReleaseLock(urn: URN): Promise<void>;
413
+ locked(urn: URN): Promise<boolean>;
414
+ lock(urn: URN): Promise<() => Promise<void>>;
415
+ }
416
+ //#endregion
417
+ //#region src/helpers.d.ts
418
+ declare const file: (path: string, encoding?: BufferEncoding) => Future<string>;
419
+ declare const hash: (path: string, algo?: string) => Future<string>;
420
+ //#endregion
421
+ //#region src/globals.d.ts
422
+ declare global {
423
+ var $resolve: typeof resolve;
424
+ var $combine: typeof combine;
425
+ var $interpolate: typeof interpolate;
426
+ var $hash: typeof hash;
427
+ var $file: typeof file;
428
+ }
429
+ //#endregion
430
+ //#region src/custom/resource.d.ts
431
+ declare const createCustomResourceClass: <I extends State, O extends State>(providerId: string, resourceType: string) => ResourceClass<I, O>;
432
+ //#endregion
433
+ //#region src/custom/provider.d.ts
434
+ type CustomResourceProvider = Partial<{
435
+ getResource?(props: Omit<GetProps, 'type'>): Promise<State>;
436
+ updateResource?(props: Omit<UpdateProps, 'type'>): Promise<State>;
437
+ createResource?(props: Omit<CreateProps, 'type'>): Promise<State>;
438
+ deleteResource?(props: Omit<DeleteProps, 'type'>): Promise<void>;
439
+ getData?(props: Omit<GetDataProps, 'type'>): Promise<State>;
440
+ planResourceChange?(props: Omit<PlanProps, 'type'>): Promise<{
441
+ state: State;
442
+ requiresReplacement: boolean;
443
+ }>;
444
+ }>;
445
+ declare const createCustomProvider: (providerId: string, resourceProviders: Record<string, CustomResourceProvider>) => Provider;
446
+ //#endregion
447
+ export { App, AppError, type Config, type CreateProps, type CustomResourceProvider, type DataSource, type DataSourceFunction, type DataSourceMeta, type DeleteProps, DynamoLockBackend, FileLockBackend, FileStateBackend, Future, type GetDataProps, type GetProps, Group, type Input, LockBackend, MemoryLockBackend, MemoryStateBackend, type Meta, type Node, type OptionalInput, type OptionalOutput, Output, type PlanProps, type ProcedureOptions, type Provider, type Resource, ResourceAlreadyExists, type ResourceClass, type ResourceConfig, ResourceError, type ResourceMeta, ResourceNotFound, S3StateBackend, Stack, type State, StateBackend, type Tag, type URN, type UpdateProps, WorkSpace, type WorkSpaceOptions, createCustomProvider, createCustomResourceClass, createDebugger, createMeta, deferredOutput, enableDebug, findInputDeps, getMeta, isDataSource, isNode, isResource, nodeMetaSymbol, output, resolveInputs };