alchemy 0.1.17 → 0.2.0

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 (301) hide show
  1. package/lib/alchemy.d.ts +67 -0
  2. package/lib/alchemy.js +40 -0
  3. package/lib/apply.d.ts +3 -20
  4. package/lib/apply.js +86 -78
  5. package/lib/aws/account-id.d.ts +0 -1
  6. package/lib/aws/account-id.js +0 -1
  7. package/lib/aws/bucket.d.ts +4 -7
  8. package/lib/aws/bucket.js +8 -12
  9. package/lib/aws/function.d.ts +4 -7
  10. package/lib/aws/function.js +9 -30
  11. package/lib/aws/index.d.ts +1 -1
  12. package/lib/aws/index.js +1 -1
  13. package/lib/aws/oidc/github-oidc-provider.d.ts +2 -6
  14. package/lib/aws/oidc/github-oidc-provider.js +10 -15
  15. package/lib/aws/oidc/index.d.ts +0 -1
  16. package/lib/aws/oidc/index.js +0 -1
  17. package/lib/aws/oidc/oidc-provider.d.ts +4 -6
  18. package/lib/aws/oidc/oidc-provider.js +9 -11
  19. package/lib/aws/policy-attachment.d.ts +9 -0
  20. package/lib/aws/policy-attachment.js +20 -0
  21. package/lib/aws/policy.d.ts +4 -16
  22. package/lib/aws/policy.js +9 -40
  23. package/lib/aws/queue.d.ts +4 -34
  24. package/lib/aws/queue.js +10 -20
  25. package/lib/aws/role.d.ts +4 -7
  26. package/lib/aws/role.js +17 -26
  27. package/lib/aws/ses.d.ts +4 -10
  28. package/lib/aws/ses.js +118 -131
  29. package/lib/aws/table.d.ts +4 -7
  30. package/lib/aws/table.js +8 -11
  31. package/lib/cloudflare/api.d.ts +0 -1
  32. package/lib/cloudflare/api.js +0 -1
  33. package/lib/cloudflare/asset-manifest.d.ts +0 -1
  34. package/lib/cloudflare/asset-manifest.js +0 -1
  35. package/lib/cloudflare/auth.d.ts +0 -1
  36. package/lib/cloudflare/auth.js +0 -1
  37. package/lib/cloudflare/bindings.d.ts +4 -3
  38. package/lib/cloudflare/bindings.js +3 -2
  39. package/lib/cloudflare/bound.d.ts +7 -0
  40. package/lib/cloudflare/bound.js +1 -0
  41. package/lib/cloudflare/bucket.d.ts +7 -15
  42. package/lib/cloudflare/bucket.js +12 -19
  43. package/lib/cloudflare/durable-object-namespace.d.ts +0 -3
  44. package/lib/cloudflare/durable-object-namespace.js +0 -4
  45. package/lib/cloudflare/generate-asset-manifest.d.ts +0 -1
  46. package/lib/cloudflare/generate-asset-manifest.js +1 -2
  47. package/lib/cloudflare/index.d.ts +0 -1
  48. package/lib/cloudflare/index.js +0 -1
  49. package/lib/cloudflare/kv-namespace.d.ts +6 -9
  50. package/lib/cloudflare/kv-namespace.js +16 -20
  51. package/lib/cloudflare/state.d.ts +0 -1
  52. package/lib/cloudflare/state.js +0 -1
  53. package/lib/cloudflare/static-site-router.d.ts +0 -1
  54. package/lib/cloudflare/static-site-router.js +0 -4
  55. package/lib/cloudflare/static-site.d.ts +5 -11
  56. package/lib/cloudflare/static-site.js +16 -28
  57. package/lib/cloudflare/types.d.ts +0 -1
  58. package/lib/cloudflare/types.js +0 -1
  59. package/lib/cloudflare/upload-asset-manifest.d.ts +0 -1
  60. package/lib/cloudflare/upload-asset-manifest.js +0 -1
  61. package/lib/cloudflare/worker-metadata.d.ts +0 -1
  62. package/lib/cloudflare/worker-metadata.js +0 -1
  63. package/lib/cloudflare/worker-migration.d.ts +0 -1
  64. package/lib/cloudflare/worker-migration.js +0 -1
  65. package/lib/cloudflare/worker.d.ts +13 -13
  66. package/lib/cloudflare/worker.js +43 -43
  67. package/lib/context.d.ts +58 -0
  68. package/lib/context.js +36 -0
  69. package/lib/destroy.d.ts +10 -13
  70. package/lib/destroy.js +104 -43
  71. package/lib/{esbuild.d.ts → esbuild/bundle.d.ts} +4 -18
  72. package/lib/{esbuild.js → esbuild/bundle.js} +10 -24
  73. package/lib/esbuild/index.d.ts +1 -0
  74. package/lib/esbuild/index.js +1 -0
  75. package/lib/fs/file.d.ts +13 -0
  76. package/lib/fs/file.js +20 -0
  77. package/lib/fs/folder.d.ts +10 -0
  78. package/lib/fs/folder.js +16 -0
  79. package/lib/fs/index.d.ts +2 -0
  80. package/lib/fs/index.js +2 -0
  81. package/lib/github/client.d.ts +0 -1
  82. package/lib/github/client.js +0 -1
  83. package/lib/github/index.d.ts +0 -1
  84. package/lib/github/index.js +0 -1
  85. package/lib/github/secret.d.ts +6 -7
  86. package/lib/github/secret.js +30 -36
  87. package/lib/index.d.ts +7 -10
  88. package/lib/index.js +6 -9
  89. package/lib/resource.d.ts +34 -62
  90. package/lib/resource.js +29 -270
  91. package/lib/scope.d.ts +35 -14
  92. package/lib/scope.js +78 -23
  93. package/lib/secret.d.ts +7 -0
  94. package/lib/secret.js +17 -0
  95. package/lib/state.d.ts +14 -10
  96. package/lib/state.js +15 -13
  97. package/lib/stripe/index.d.ts +0 -1
  98. package/lib/stripe/index.js +0 -1
  99. package/lib/stripe/price.d.ts +4 -5
  100. package/lib/stripe/price.js +10 -20
  101. package/lib/stripe/product.d.ts +4 -5
  102. package/lib/stripe/product.js +10 -20
  103. package/lib/stripe/webhook.d.ts +4 -6
  104. package/lib/stripe/webhook.js +12 -25
  105. package/lib/test/bun.d.ts +19 -0
  106. package/lib/test/bun.js +55 -0
  107. package/lib/{utils → util}/content-type.d.ts +0 -1
  108. package/lib/{utils → util}/content-type.js +0 -1
  109. package/lib/util/encrypt.d.ts +16 -0
  110. package/lib/util/encrypt.js +44 -0
  111. package/lib/{error.d.ts → util/ignore.d.ts} +0 -1
  112. package/lib/{error.js → util/ignore.js} +0 -1
  113. package/lib/{utils → util}/retry.d.ts +0 -1
  114. package/lib/{utils → util}/retry.js +0 -1
  115. package/lib/util/serde.d.ts +3 -0
  116. package/lib/util/serde.js +46 -0
  117. package/lib/util/slugify.d.ts +1 -0
  118. package/lib/util/slugify.js +3 -0
  119. package/package.json +5 -7
  120. package/src/alchemy.ts +120 -0
  121. package/src/apply.ts +109 -111
  122. package/src/aws/bucket.ts +11 -14
  123. package/src/aws/function.ts +13 -33
  124. package/src/aws/index.ts +1 -0
  125. package/src/aws/oidc/github-oidc-provider.ts +14 -19
  126. package/src/aws/oidc/oidc-provider.ts +19 -12
  127. package/src/aws/policy-attachment.ts +51 -0
  128. package/src/aws/policy.ts +15 -60
  129. package/src/aws/queue.ts +17 -21
  130. package/src/aws/role.ts +25 -28
  131. package/src/aws/ses.ts +150 -164
  132. package/src/aws/table.ts +15 -15
  133. package/src/cloudflare/bindings.ts +12 -2
  134. package/src/cloudflare/bound.ts +18 -0
  135. package/src/cloudflare/bucket.ts +40 -48
  136. package/src/cloudflare/durable-object-namespace.ts +0 -10
  137. package/src/cloudflare/generate-asset-manifest.ts +1 -1
  138. package/src/cloudflare/kv-namespace.ts +35 -33
  139. package/src/cloudflare/static-site-router.ts +0 -3
  140. package/src/cloudflare/static-site.ts +43 -59
  141. package/src/cloudflare/worker.ts +76 -64
  142. package/src/context.ts +122 -0
  143. package/src/destroy.ts +124 -73
  144. package/src/{esbuild.ts → esbuild/bundle.ts} +16 -28
  145. package/src/esbuild/index.ts +1 -0
  146. package/src/fs/file.ts +39 -0
  147. package/src/fs/folder.ts +32 -0
  148. package/src/fs/index.ts +2 -0
  149. package/src/github/secret.ts +53 -49
  150. package/src/index.ts +8 -15
  151. package/src/resource.ts +101 -462
  152. package/src/scope.ts +101 -36
  153. package/src/secret.ts +18 -0
  154. package/src/state.ts +30 -20
  155. package/src/stripe/price.ts +17 -22
  156. package/src/stripe/product.ts +17 -22
  157. package/src/stripe/webhook.ts +21 -27
  158. package/src/test/bun.ts +105 -0
  159. package/src/util/encrypt.ts +79 -0
  160. package/src/util/serde.ts +53 -0
  161. package/src/util/slugify.ts +3 -0
  162. package/lib/$.d.ts +0 -12
  163. package/lib/$.d.ts.map +0 -1
  164. package/lib/$.js +0 -42
  165. package/lib/$.js.map +0 -1
  166. package/lib/alchemize.d.ts +0 -43
  167. package/lib/alchemize.d.ts.map +0 -1
  168. package/lib/alchemize.js +0 -112
  169. package/lib/alchemize.js.map +0 -1
  170. package/lib/apply.d.ts.map +0 -1
  171. package/lib/apply.js.map +0 -1
  172. package/lib/aws/account-id.d.ts.map +0 -1
  173. package/lib/aws/account-id.js.map +0 -1
  174. package/lib/aws/bucket.d.ts.map +0 -1
  175. package/lib/aws/bucket.js.map +0 -1
  176. package/lib/aws/function.d.ts.map +0 -1
  177. package/lib/aws/function.js.map +0 -1
  178. package/lib/aws/index.d.ts.map +0 -1
  179. package/lib/aws/index.js.map +0 -1
  180. package/lib/aws/oidc/github-oidc-provider.d.ts.map +0 -1
  181. package/lib/aws/oidc/github-oidc-provider.js.map +0 -1
  182. package/lib/aws/oidc/index.d.ts.map +0 -1
  183. package/lib/aws/oidc/index.js.map +0 -1
  184. package/lib/aws/oidc/oidc-provider.d.ts.map +0 -1
  185. package/lib/aws/oidc/oidc-provider.js.map +0 -1
  186. package/lib/aws/policy.d.ts.map +0 -1
  187. package/lib/aws/policy.js.map +0 -1
  188. package/lib/aws/queue.d.ts.map +0 -1
  189. package/lib/aws/queue.js.map +0 -1
  190. package/lib/aws/role.d.ts.map +0 -1
  191. package/lib/aws/role.js.map +0 -1
  192. package/lib/aws/ses.d.ts.map +0 -1
  193. package/lib/aws/ses.js.map +0 -1
  194. package/lib/aws/table.d.ts.map +0 -1
  195. package/lib/aws/table.js.map +0 -1
  196. package/lib/cloudflare/api.d.ts.map +0 -1
  197. package/lib/cloudflare/api.js.map +0 -1
  198. package/lib/cloudflare/asset-manifest.d.ts.map +0 -1
  199. package/lib/cloudflare/asset-manifest.js.map +0 -1
  200. package/lib/cloudflare/auth.d.ts.map +0 -1
  201. package/lib/cloudflare/auth.js.map +0 -1
  202. package/lib/cloudflare/bindings.d.ts.map +0 -1
  203. package/lib/cloudflare/bindings.js.map +0 -1
  204. package/lib/cloudflare/bucket.d.ts.map +0 -1
  205. package/lib/cloudflare/bucket.js.map +0 -1
  206. package/lib/cloudflare/durable-object-namespace.d.ts.map +0 -1
  207. package/lib/cloudflare/durable-object-namespace.js.map +0 -1
  208. package/lib/cloudflare/generate-asset-manifest.d.ts.map +0 -1
  209. package/lib/cloudflare/generate-asset-manifest.js.map +0 -1
  210. package/lib/cloudflare/index.d.ts.map +0 -1
  211. package/lib/cloudflare/index.js.map +0 -1
  212. package/lib/cloudflare/kv-namespace.d.ts.map +0 -1
  213. package/lib/cloudflare/kv-namespace.js.map +0 -1
  214. package/lib/cloudflare/state.d.ts.map +0 -1
  215. package/lib/cloudflare/state.js.map +0 -1
  216. package/lib/cloudflare/static-site-router.d.ts.map +0 -1
  217. package/lib/cloudflare/static-site-router.js.map +0 -1
  218. package/lib/cloudflare/static-site.d.ts.map +0 -1
  219. package/lib/cloudflare/static-site.js.map +0 -1
  220. package/lib/cloudflare/types.d.ts.map +0 -1
  221. package/lib/cloudflare/types.js.map +0 -1
  222. package/lib/cloudflare/upload-asset-manifest.d.ts.map +0 -1
  223. package/lib/cloudflare/upload-asset-manifest.js.map +0 -1
  224. package/lib/cloudflare/worker-metadata.d.ts.map +0 -1
  225. package/lib/cloudflare/worker-metadata.js.map +0 -1
  226. package/lib/cloudflare/worker-migration.d.ts.map +0 -1
  227. package/lib/cloudflare/worker-migration.js.map +0 -1
  228. package/lib/cloudflare/worker.d.ts.map +0 -1
  229. package/lib/cloudflare/worker.js.map +0 -1
  230. package/lib/destroy.d.ts.map +0 -1
  231. package/lib/destroy.js.map +0 -1
  232. package/lib/error.d.ts.map +0 -1
  233. package/lib/error.js.map +0 -1
  234. package/lib/esbuild.d.ts.map +0 -1
  235. package/lib/esbuild.js.map +0 -1
  236. package/lib/fs.d.ts +0 -12
  237. package/lib/fs.d.ts.map +0 -1
  238. package/lib/fs.js +0 -41
  239. package/lib/fs.js.map +0 -1
  240. package/lib/github/client.d.ts.map +0 -1
  241. package/lib/github/client.js.map +0 -1
  242. package/lib/github/index.d.ts.map +0 -1
  243. package/lib/github/index.js.map +0 -1
  244. package/lib/github/secret.d.ts.map +0 -1
  245. package/lib/github/secret.js.map +0 -1
  246. package/lib/global.d.ts +0 -16
  247. package/lib/global.d.ts.map +0 -1
  248. package/lib/global.js +0 -42
  249. package/lib/global.js.map +0 -1
  250. package/lib/index.d.ts.map +0 -1
  251. package/lib/index.js.map +0 -1
  252. package/lib/input.d.ts +0 -6
  253. package/lib/input.d.ts.map +0 -1
  254. package/lib/input.js +0 -2
  255. package/lib/input.js.map +0 -1
  256. package/lib/main.d.ts +0 -3
  257. package/lib/main.d.ts.map +0 -1
  258. package/lib/main.js +0 -32
  259. package/lib/main.js.map +0 -1
  260. package/lib/output.d.ts +0 -24
  261. package/lib/output.d.ts.map +0 -1
  262. package/lib/output.js +0 -23
  263. package/lib/output.js.map +0 -1
  264. package/lib/print.d.ts +0 -7
  265. package/lib/print.d.ts.map +0 -1
  266. package/lib/print.js +0 -10
  267. package/lib/print.js.map +0 -1
  268. package/lib/resource.d.ts.map +0 -1
  269. package/lib/resource.js.map +0 -1
  270. package/lib/scope.d.ts.map +0 -1
  271. package/lib/scope.js.map +0 -1
  272. package/lib/slug.d.ts +0 -2
  273. package/lib/slug.d.ts.map +0 -1
  274. package/lib/slug.js +0 -4
  275. package/lib/slug.js.map +0 -1
  276. package/lib/state.d.ts.map +0 -1
  277. package/lib/state.js.map +0 -1
  278. package/lib/stripe/index.d.ts.map +0 -1
  279. package/lib/stripe/index.js.map +0 -1
  280. package/lib/stripe/price.d.ts.map +0 -1
  281. package/lib/stripe/price.js.map +0 -1
  282. package/lib/stripe/product.d.ts.map +0 -1
  283. package/lib/stripe/product.js.map +0 -1
  284. package/lib/stripe/webhook.d.ts.map +0 -1
  285. package/lib/stripe/webhook.js.map +0 -1
  286. package/lib/utils/content-type.d.ts.map +0 -1
  287. package/lib/utils/content-type.js.map +0 -1
  288. package/lib/utils/retry.d.ts.map +0 -1
  289. package/lib/utils/retry.js.map +0 -1
  290. package/src/$.ts +0 -69
  291. package/src/alchemize.ts +0 -186
  292. package/src/fs.ts +0 -47
  293. package/src/global.ts +0 -63
  294. package/src/input.ts +0 -16
  295. package/src/main.ts +0 -34
  296. package/src/output.ts +0 -71
  297. package/src/print.ts +0 -18
  298. package/src/slug.ts +0 -3
  299. /package/src/{utils → util}/content-type.ts +0 -0
  300. /package/src/{error.ts → util/ignore.ts} +0 -0
  301. /package/src/{utils → util}/retry.ts +0 -0
package/src/context.ts ADDED
@@ -0,0 +1,122 @@
1
+ import { DestroyedSignal } from "./destroy";
2
+ import type {
3
+ Resource,
4
+ ResourceFQN,
5
+ ResourceID,
6
+ ResourceKind,
7
+ ResourceProps,
8
+ } from "./resource";
9
+ import type { Scope } from "./scope";
10
+ import type { State } from "./state";
11
+
12
+ export type Context<Out extends Resource> =
13
+ | CreateContext<Out>
14
+ | UpdateContext<Out>
15
+ | DeleteContext<Out>;
16
+
17
+ export interface CreateContext<Out extends Resource> extends BaseContext<Out> {
18
+ phase: "create";
19
+ output?: undefined;
20
+ }
21
+
22
+ export interface UpdateContext<Out extends Resource> extends BaseContext<Out> {
23
+ phase: "update";
24
+ output: Out;
25
+ }
26
+
27
+ export interface DeleteContext<Out extends Resource> extends BaseContext<Out> {
28
+ phase: "delete";
29
+ output: Out;
30
+ }
31
+
32
+ export interface BaseContext<Out extends Resource> {
33
+ quiet: boolean;
34
+ stage: string;
35
+ id: ResourceID;
36
+ fqn: ResourceFQN;
37
+ scope: Scope;
38
+ get<T>(key: string): Promise<T | undefined>;
39
+ set<T>(key: string, value: T): Promise<void>;
40
+ delete<T>(key: string): Promise<T | undefined>;
41
+ /**
42
+ * Indicate that this resource is being replaced.
43
+ * This will cause the resource to be deleted at the end of the stack's CREATE phase.
44
+ */
45
+ replace(): void;
46
+
47
+ /**
48
+ * Terminate the resource lifecycle handler and destroy the resource.
49
+ *
50
+ * This is the final operation performed during a delete operation.
51
+ *
52
+ * It is so that the resource lifecycle handler can "return never" instead of
53
+ * "return undefined" so that `await MyResource()` always returns a value.
54
+ */
55
+ destroy(): never;
56
+ /**
57
+ * Create the Resource envelope (with Alchemy + User properties)
58
+ */
59
+ create(props: Omit<Out, keyof Resource>): Out;
60
+ /**
61
+ * Create the Resource envelope (with Alchemy + User properties)
62
+ */
63
+ (props: Omit<Out, keyof Resource>): Out;
64
+ }
65
+
66
+ export function context<
67
+ Kind extends string,
68
+ Props extends ResourceProps,
69
+ Out extends Resource,
70
+ >({
71
+ scope,
72
+ phase,
73
+ kind,
74
+ id,
75
+ fqn,
76
+ seq,
77
+ state,
78
+ replace,
79
+ }: {
80
+ scope: Scope;
81
+ phase: "create" | "update" | "delete";
82
+ kind: ResourceKind;
83
+ id: ResourceID;
84
+ fqn: ResourceFQN;
85
+ seq: number;
86
+ state: State<Kind, Props, Out>;
87
+ replace: () => void;
88
+ }): Context<Out> {
89
+ function create(props: Omit<Out, "Kind" | "ID" | "Scope">): Out {
90
+ return {
91
+ ...props,
92
+ Kind: kind,
93
+ ID: id,
94
+ FQN: fqn,
95
+ Scope: scope,
96
+ Seq: seq,
97
+ } as Out;
98
+ }
99
+ return Object.assign(create, {
100
+ stage: scope.stage,
101
+ scope,
102
+ id: id,
103
+ fqn: fqn,
104
+ phase,
105
+ output: state.output,
106
+ replace,
107
+ get: (key: string) => state.data[key],
108
+ set: async (key: string, value: any) => {
109
+ state.data[key] = value;
110
+ },
111
+ delete: async (key: string) => {
112
+ const value = state.data[key];
113
+ delete state.data[key];
114
+ return value;
115
+ },
116
+ quiet: scope.quiet,
117
+ destroy: () => {
118
+ throw new DestroyedSignal();
119
+ },
120
+ create,
121
+ }) as Context<Out>;
122
+ }
package/src/destroy.ts CHANGED
@@ -1,92 +1,143 @@
1
- import { defaultStage, defaultStateStore } from "./global";
2
- import type { Output } from "./output";
3
- import { Provider, ResourceID, isResource } from "./resource";
4
- import { type Scope, rootScope } from "./scope";
5
- import type { State, StateStore } from "./state";
1
+ import { alchemy } from "./alchemy";
2
+ import { context } from "./context";
3
+ import { PROVIDERS, type Provider, type Resource } from "./resource";
4
+ import { Scope } from "./scope";
5
+
6
+ export class DestroyedSignal extends Error {}
6
7
 
7
8
  export interface DestroyOptions {
8
- stage?: string;
9
- stateStore?: StateStore;
10
- scope?: Scope;
11
9
  quiet?: boolean;
10
+ strategy?: "sequential" | "parallel";
12
11
  }
13
12
 
14
- /**
15
- * Prune all resources from an Output and "down", i.e. that branches from it.
16
- */
17
- export async function destroy<T>(
18
- output: Output<T>,
19
- options?: DestroyOptions,
20
- ): Promise<void>;
13
+ function isScopeArgs(a: any): a is [scope: Scope, options?: DestroyOptions] {
14
+ return a[0] instanceof Scope;
15
+ }
21
16
 
22
17
  /**
23
- * @internal
18
+ * Prune all resources from an Output and "down", i.e. that branches from it.
24
19
  */
25
- export async function destroy<T>(
26
- stage: string,
27
- scope: Scope,
28
- resourceID: ResourceID,
29
- resourceState: State,
30
- resourceProvider: Provider,
31
- stateStore: StateStore,
32
- options: DestroyOptions,
33
- ): Promise<void>;
34
-
35
- export async function destroy<T>(
20
+ export async function destroy<Type extends string>(
36
21
  ...args:
37
- | [Output<T>, DestroyOptions?]
38
- | [string, Scope, ResourceID, State, Provider, StateStore, DestroyOptions]
22
+ | [scope: Scope, options?: DestroyOptions]
23
+ | [resource: Resource<Type> | undefined | null, options?: DestroyOptions]
39
24
  ): Promise<void> {
40
- let resourceID: ResourceID;
41
- let resourceState: State;
42
- let resourceProvider: Provider;
43
- let stage: string;
44
- let stateStore: StateStore;
45
- let scope: Scope | undefined = undefined;
46
- let options: DestroyOptions | undefined = undefined;
47
- if (args.length === 7) {
48
- stage = args[0];
49
- scope = args[1];
50
- resourceID = args[2];
51
- resourceState = args[3];
52
- resourceProvider = args[4];
53
- stateStore = args[5];
54
- options = args[6];
55
- } else if (isResource(args[0])) {
56
- const resource = args[0];
57
- // stage = args[1]?.stage ?? defaultStage;
58
- resourceID = resource[ResourceID];
59
- stage = args[1]?.stage ?? defaultStage;
60
- scope = args[1]?.scope ?? rootScope;
61
- const statePath = scope.getScopePath(stage);
62
- stateStore = args[1]?.stateStore ?? new defaultStateStore(statePath);
63
- // First destroy all dependencies
64
- const _resourceState = await stateStore.get(resourceID);
65
- if (_resourceState === undefined) {
66
- // we have no record of this resource, we must assume it's already deleted
67
- return;
68
- }
69
- resourceState = _resourceState;
70
- resourceProvider = resource[Provider];
71
- options = args[1];
72
- } else {
73
- console.log(args[0]);
74
- throw new Error("Not implemented: must handle destroy a Output chain");
25
+ if (isScopeArgs(args)) {
26
+ const [scope, options] = args;
27
+ const strategy = options?.strategy ?? "sequential";
28
+ // destroy all active resources
29
+ await destroy.all(Array.from(scope.resources.values()), {
30
+ strategy,
31
+ });
32
+ // then detect orphans and destroy them
33
+ const orphans = await scope.state.all();
34
+ await destroy.all(
35
+ Object.values(orphans).map((orphan) => ({
36
+ ...orphan.output,
37
+ Scope: scope,
38
+ })),
39
+ options,
40
+ );
41
+ // finally, destroy the scope container
42
+ await scope.deinit();
43
+ return;
44
+ }
45
+
46
+ const [instance, options] = args;
47
+
48
+ if (!instance) {
49
+ return;
50
+ }
51
+
52
+ const Provider: Provider<Type> | undefined = PROVIDERS.get(instance.Kind);
53
+ if (!Provider) {
54
+ throw new Error(
55
+ `Cannot destroy resource type ${instance.Kind} - no provider found. You may need to import the provider in your alchemy.config.ts.`,
56
+ );
75
57
  }
58
+
59
+ const scope = instance.Scope;
60
+ if (!scope) {
61
+ console.warn(`Resource "${instance.FQN}" has no scope`);
62
+ }
63
+ const quiet = options?.quiet ?? scope.quiet;
64
+
76
65
  try {
77
- await resourceProvider.delete(
78
- stage,
66
+ if (!quiet) {
67
+ console.log(`Delete: "${instance.FQN}"`);
68
+ }
69
+
70
+ const state = (await scope.state.get(instance.ID))!;
71
+
72
+ if (state === undefined) {
73
+ console.warn(`Resource "${instance.FQN}" not found`);
74
+ return;
75
+ }
76
+
77
+ const ctx = context({
79
78
  scope,
80
- resourceID,
81
- resourceState,
82
- resourceState.inputs as [],
83
- options ?? {
84
- quiet: false,
79
+ phase: "delete",
80
+ kind: instance.Kind,
81
+ id: instance.ID,
82
+ fqn: instance.FQN,
83
+ seq: instance.Seq,
84
+ state,
85
+ replace: () => {
86
+ throw new Error("Cannot replace a resource that is being deleted");
85
87
  },
86
- );
88
+ });
89
+
90
+ let nestedScope: Scope | undefined;
91
+ try {
92
+ // BUG: this does not restore persisted scope
93
+ await alchemy.run(instance.ID, async (scope) => {
94
+ nestedScope = scope;
95
+ return Provider.handler.bind(ctx)(instance.ID, state.props);
96
+ });
97
+ } catch (err) {
98
+ if (err instanceof DestroyedSignal) {
99
+ // TODO: should we fail if the DestroyedSignal is not thrown?
100
+ } else {
101
+ throw err;
102
+ }
103
+ }
104
+
105
+ if (nestedScope) {
106
+ await destroy(nestedScope, options);
107
+ }
108
+
109
+ await scope.delete(instance.ID);
110
+
111
+ if (!quiet) {
112
+ console.log(`Deleted: "${instance.FQN}"`);
113
+ }
87
114
  } catch (error) {
88
115
  console.error(error);
89
116
  throw error;
90
117
  }
91
- await stateStore.delete(resourceID);
118
+ }
119
+
120
+ export namespace destroy {
121
+ export async function all(resources: Resource[], options?: DestroyOptions) {
122
+ if (options?.strategy !== "parallel") {
123
+ const sorted = resources.sort((a, b) => b.Seq - a.Seq);
124
+ for (const resource of sorted) {
125
+ await destroy(resource);
126
+ }
127
+ } else {
128
+ await Promise.all(
129
+ resources.map((resource) => destroy(resource, options)),
130
+ );
131
+ }
132
+ }
133
+
134
+ export async function sequentially(
135
+ ...resources: (Resource<string> | undefined | null)[]
136
+ ) {
137
+ for (const resource of resources) {
138
+ if (resource) {
139
+ await destroy(resource);
140
+ }
141
+ }
142
+ }
92
143
  }
@@ -2,8 +2,8 @@ import * as esbuild from "esbuild";
2
2
  import crypto from "node:crypto";
3
3
  import fs from "node:fs";
4
4
  import path from "node:path";
5
- import { Resource } from "./resource";
6
-
5
+ import type { Context } from "../context";
6
+ import { Resource } from "../resource";
7
7
  export interface BundleProps {
8
8
  /**
9
9
  * Entry point for the bundle
@@ -47,7 +47,7 @@ export interface BundleProps {
47
47
  options?: Partial<esbuild.BuildOptions>;
48
48
  }
49
49
 
50
- export interface BundleOutput {
50
+ export interface Bundle extends Resource<"esbuild::Bundle"> {
51
51
  /**
52
52
  * Path to the bundled file
53
53
  */
@@ -58,23 +58,27 @@ export interface BundleOutput {
58
58
  hash: string;
59
59
  }
60
60
 
61
- export class Bundle extends Resource(
61
+ export const Bundle = Resource(
62
62
  "esbuild::Bundle",
63
63
  {
64
64
  alwaysUpdate: true,
65
65
  },
66
- async (ctx, props: BundleProps): Promise<BundleOutput> => {
66
+ async function (
67
+ this: Context<Bundle>,
68
+ id: string,
69
+ props: BundleProps,
70
+ ): Promise<Bundle> {
67
71
  // Determine output path
68
72
  const outputPath = getOutputPath(props);
69
73
 
70
74
  // Ensure output directory exists
71
75
  await fs.promises.mkdir(path.dirname(outputPath), { recursive: true });
72
76
 
73
- if (ctx.event === "delete") {
77
+ if (this.phase === "delete") {
74
78
  await fs.promises.unlink(outputPath).catch(() => {});
75
79
  // Also clean up sourcemap if it exists
76
80
  await fs.promises.unlink(outputPath + ".map").catch(() => {});
77
- return { path: outputPath, hash: "" };
81
+ return this.destroy();
78
82
  }
79
83
 
80
84
  const result = await bundle(props);
@@ -83,32 +87,16 @@ export class Bundle extends Resource(
83
87
  const hash = crypto.createHash("sha256").update(contents).digest("hex");
84
88
 
85
89
  // Store metadata in context
86
- await ctx.set("metafile", result.metafile);
87
- await ctx.set("hash", hash);
90
+ await this.set("metafile", result.metafile);
91
+ await this.set("hash", hash);
88
92
 
89
93
  // Return output info
90
- return {
94
+ return this({
91
95
  path: outputPath,
92
96
  hash,
93
- };
97
+ });
94
98
  },
95
- ) {
96
- /**
97
- * Get the metafile from the last build
98
- */
99
- public async getMetafile(ctx: {
100
- get<T>(key: string): Promise<T | undefined>;
101
- }) {
102
- return ctx.get<esbuild.Metafile>("metafile");
103
- }
104
-
105
- /**
106
- * Get the hash from the last build
107
- */
108
- public async getHash(ctx: { get<T>(key: string): Promise<T | undefined> }) {
109
- return ctx.get<string>("hash");
110
- }
111
- }
99
+ );
112
100
 
113
101
  export async function bundle(props: BundleProps) {
114
102
  const outputPath = getOutputPath(props);
@@ -0,0 +1 @@
1
+ export * from "./bundle";
package/src/fs/file.ts ADDED
@@ -0,0 +1,39 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import type { Context } from "../context";
4
+ import { Resource } from "../resource";
5
+ import { ignore } from "../util/ignore";
6
+
7
+ export interface File extends Resource<"fs::File"> {
8
+ path: string;
9
+ content: string;
10
+ }
11
+
12
+ export const File = Resource(
13
+ "fs::File",
14
+ async function (
15
+ this: Context<File>,
16
+ id: string,
17
+ {
18
+ path: filePath,
19
+ content,
20
+ }: {
21
+ path: string;
22
+ content: string;
23
+ },
24
+ ): Promise<File> {
25
+ if (this.phase === "delete") {
26
+ await ignore("ENOENT", async () => fs.promises.unlink(filePath));
27
+ return this.destroy();
28
+ } else {
29
+ await fs.promises.mkdir(path.dirname(filePath), {
30
+ recursive: true,
31
+ });
32
+ await fs.promises.writeFile(filePath, content);
33
+ }
34
+ return this({
35
+ path: filePath,
36
+ content,
37
+ });
38
+ },
39
+ );
@@ -0,0 +1,32 @@
1
+ import fs from "node:fs";
2
+ import type { Context } from "../context";
3
+ import { Resource } from "../resource";
4
+ import { ignore } from "../util/ignore";
5
+
6
+ export interface Folder extends Resource<"fs::Folder"> {
7
+ path: string;
8
+ }
9
+
10
+ export const Folder = Resource(
11
+ "fs::Folder",
12
+ async function (
13
+ this: Context<Folder>,
14
+ id: string,
15
+ { path: dirPath }: { path: string },
16
+ ): Promise<Folder> {
17
+ if (this.phase === "delete") {
18
+ // we just do a best effort attempt
19
+ await ignore(["ENOENT", "ENOTEMPTY"], async () =>
20
+ fs.promises.rmdir(dirPath),
21
+ );
22
+ return this.destroy();
23
+ } else {
24
+ await ignore("EEXIST", async () =>
25
+ fs.promises.mkdir(dirPath, { recursive: true }),
26
+ );
27
+ }
28
+ return this({
29
+ path: dirPath,
30
+ });
31
+ },
32
+ );
@@ -0,0 +1,2 @@
1
+ export * from "./file";
2
+ export * from "./folder";
@@ -1,7 +1,8 @@
1
1
  import sodium from "libsodium-wrappers";
2
- import { type Context, Resource } from "../resource";
2
+ import type { Context } from "../context";
3
+ import { Resource } from "../resource";
4
+ import type { Secret } from "../secret";
3
5
  import { createGitHubClient, verifyGitHubAuth } from "./client";
4
-
5
6
  /**
6
7
  * Properties for creating or updating a GitHub Secret
7
8
  */
@@ -24,7 +25,7 @@ export interface GitHubSecretProps {
24
25
  /**
25
26
  * Secret value (will be stored securely on GitHub)
26
27
  */
27
- value: string;
28
+ value: Secret;
28
29
 
29
30
  /**
30
31
  * Optional GitHub API token (overrides environment variable)
@@ -35,7 +36,9 @@ export interface GitHubSecretProps {
35
36
  /**
36
37
  * Output returned after Secret creation/update
37
38
  */
38
- export interface GitHubSecretOutput extends Omit<GitHubSecretProps, "value"> {
39
+ export interface GitHubSecretOutput
40
+ extends Resource<"github::Secret">,
41
+ Omit<GitHubSecretProps, "value"> {
39
42
  /**
40
43
  * The ID of the resource
41
44
  */
@@ -47,50 +50,27 @@ export interface GitHubSecretOutput extends Omit<GitHubSecretProps, "value"> {
47
50
  updatedAt: string;
48
51
  }
49
52
 
50
- /**
51
- * Encrypt a value for GitHub using libsodium
52
- * Based on GitHub's documentation for encrypting secrets
53
- *
54
- * @param value - The secret value to encrypt
55
- * @param publicKey - The base64-encoded public key from GitHub
56
- * @returns The base64-encoded encrypted value
57
- */
58
- async function encryptValue(value: string, publicKey: string): Promise<string> {
59
- // Initialize libsodium
60
- await sodium.ready;
61
-
62
- // Convert the public key from base64 to binary
63
- const binKey = sodium.from_base64(publicKey, sodium.base64_variants.ORIGINAL);
64
-
65
- // Convert the message to a Uint8Array
66
- const binMessage = sodium.from_string(value);
67
-
68
- // Encrypt the message with the public key using libsodium's sealed box
69
- const encryptedBin = sodium.crypto_box_seal(binMessage, binKey);
70
-
71
- // Convert the encrypted message to base64
72
- return sodium.to_base64(encryptedBin, sodium.base64_variants.ORIGINAL);
73
- }
74
-
75
53
  /**
76
54
  * Resource for managing GitHub repository secrets
77
55
  */
78
- export class GitHubSecret extends Resource(
56
+ export const GitHubSecret = Resource(
79
57
  "github::Secret",
80
- async (
81
- ctx: Context<GitHubSecretOutput>,
58
+ async function (
59
+ this: Context<GitHubSecretOutput>,
60
+ id: string,
82
61
  props: GitHubSecretProps,
83
- ): Promise<GitHubSecretOutput | void> => {
62
+ ): Promise<GitHubSecretOutput> {
84
63
  // Create authenticated Octokit client - will automatically handle token resolution
64
+ /// TODO: use fetch
85
65
  const octokit = await createGitHubClient({ token: props.token });
86
66
 
87
67
  // Verify authentication and permissions
88
- if (!ctx.quiet) {
68
+ if (!this.quiet) {
89
69
  await verifyGitHubAuth(octokit, props.owner, props.repository);
90
70
  }
91
71
 
92
- if (ctx.event === "delete") {
93
- if (ctx.output?.id) {
72
+ if (this.phase === "delete") {
73
+ if (this.output?.id) {
94
74
  try {
95
75
  // Delete the secret
96
76
  await octokit.rest.actions.deleteRepoSecret({
@@ -109,7 +89,7 @@ export class GitHubSecret extends Resource(
109
89
  }
110
90
 
111
91
  // Return void (a deleted resource has no content)
112
- return undefined;
92
+ return this.destroy();
113
93
  } else {
114
94
  try {
115
95
  // Get the repository's public key for encrypting secrets
@@ -121,10 +101,13 @@ export class GitHubSecret extends Resource(
121
101
  );
122
102
 
123
103
  // Encrypt the secret value using libsodium
124
- const encryptedValue = await encryptValue(props.value, publicKey.key);
104
+ const encryptedValue = await encryptString(
105
+ props.value.unencrypted,
106
+ publicKey.key,
107
+ );
125
108
 
126
109
  // Create or update the secret with the encrypted value and key_id
127
- const response = await octokit.rest.actions.createOrUpdateRepoSecret({
110
+ await octokit.rest.actions.createOrUpdateRepoSecret({
128
111
  owner: props.owner,
129
112
  repo: props.repository,
130
113
  secret_name: props.name,
@@ -132,22 +115,15 @@ export class GitHubSecret extends Resource(
132
115
  key_id: publicKey.key_id,
133
116
  });
134
117
 
135
- // Log success
136
- if (!ctx.quiet) {
137
- console.log(`Successfully created/updated secret: ${props.name}`);
138
- }
139
-
140
118
  // GitHub doesn't return the secret details on create/update, so we need to construct it
141
- const output: GitHubSecretOutput = {
119
+ return this({
142
120
  id: `${props.owner}/${props.repository}/${props.name}`,
143
121
  owner: props.owner,
144
122
  repository: props.repository,
145
123
  name: props.name,
146
124
  token: props.token,
147
125
  updatedAt: new Date().toISOString(),
148
- };
149
-
150
- return output;
126
+ });
151
127
  } catch (error: any) {
152
128
  if (
153
129
  error.status === 403 &&
@@ -169,4 +145,32 @@ export class GitHubSecret extends Resource(
169
145
  }
170
146
  }
171
147
  },
172
- ) {}
148
+ );
149
+
150
+ /**
151
+ * Encrypt a value for GitHub using libsodium
152
+ * Based on GitHub's documentation for encrypting secrets
153
+ *
154
+ * @param value - The secret value to encrypt
155
+ * @param publicKey - The base64-encoded public key from GitHub
156
+ * @returns The base64-encoded encrypted value
157
+ */
158
+ async function encryptString(
159
+ value: string,
160
+ publicKey: string,
161
+ ): Promise<string> {
162
+ // Initialize libsodium
163
+ await sodium.ready;
164
+
165
+ // Convert the public key from base64 to binary
166
+ const binKey = sodium.from_base64(publicKey, sodium.base64_variants.ORIGINAL);
167
+
168
+ // Convert the message to a Uint8Array
169
+ const binMessage = sodium.from_string(value);
170
+
171
+ // Encrypt the message with the public key using libsodium's sealed box
172
+ const encryptedBin = sodium.crypto_box_seal(binMessage, binKey);
173
+
174
+ // Convert the encrypted message to base64
175
+ return sodium.to_base64(encryptedBin, sodium.base64_variants.ORIGINAL);
176
+ }