@ericminassian/platform-engine 0.0.0-stage → 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 (90) hide show
  1. package/dist/clients.d.ts +50 -0
  2. package/dist/clients.js +103 -0
  3. package/dist/clients.js.map +1 -0
  4. package/dist/compat.d.ts +19 -0
  5. package/dist/compat.js +63 -0
  6. package/dist/compat.js.map +1 -0
  7. package/dist/drivers/cognito.d.ts +18 -0
  8. package/dist/drivers/cognito.js +113 -0
  9. package/dist/drivers/cognito.js.map +1 -0
  10. package/dist/drivers/dynamodb.d.ts +26 -0
  11. package/dist/drivers/dynamodb.js +151 -0
  12. package/dist/drivers/dynamodb.js.map +1 -0
  13. package/dist/drivers/events.d.ts +21 -0
  14. package/dist/drivers/events.js +62 -0
  15. package/dist/drivers/events.js.map +1 -0
  16. package/dist/drivers/iam.d.ts +20 -0
  17. package/dist/drivers/iam.js +86 -0
  18. package/dist/drivers/iam.js.map +1 -0
  19. package/dist/drivers/index.d.ts +12 -0
  20. package/dist/drivers/index.js +13 -0
  21. package/dist/drivers/index.js.map +1 -0
  22. package/dist/drivers/kvs.d.ts +5 -0
  23. package/dist/drivers/kvs.js +28 -0
  24. package/dist/drivers/kvs.js.map +1 -0
  25. package/dist/drivers/lambda.d.ts +48 -0
  26. package/dist/drivers/lambda.js +158 -0
  27. package/dist/drivers/lambda.js.map +1 -0
  28. package/dist/drivers/logs.d.ts +20 -0
  29. package/dist/drivers/logs.js +62 -0
  30. package/dist/drivers/logs.js.map +1 -0
  31. package/dist/drivers/s3.d.ts +30 -0
  32. package/dist/drivers/s3.js +126 -0
  33. package/dist/drivers/s3.js.map +1 -0
  34. package/dist/drivers/scheduler.d.ts +21 -0
  35. package/dist/drivers/scheduler.js +59 -0
  36. package/dist/drivers/scheduler.js.map +1 -0
  37. package/dist/drivers/sqs.d.ts +18 -0
  38. package/dist/drivers/sqs.js +46 -0
  39. package/dist/drivers/sqs.js.map +1 -0
  40. package/dist/drivers/ssm.d.ts +16 -0
  41. package/dist/drivers/ssm.js +45 -0
  42. package/dist/drivers/ssm.js.map +1 -0
  43. package/dist/drivers/web-function.d.ts +71 -0
  44. package/dist/drivers/web-function.js +227 -0
  45. package/dist/drivers/web-function.js.map +1 -0
  46. package/dist/emulator.d.ts +51 -0
  47. package/dist/emulator.js +142 -0
  48. package/dist/emulator.js.map +1 -0
  49. package/dist/index.d.ts +140 -0
  50. package/dist/index.js +72 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/local.d.ts +12 -0
  53. package/dist/local.js +71 -0
  54. package/dist/local.js.map +1 -0
  55. package/dist/policy.d.ts +41 -0
  56. package/dist/policy.js +316 -0
  57. package/dist/policy.js.map +1 -0
  58. package/dist/stage.d.ts +19 -0
  59. package/dist/stage.js +479 -0
  60. package/dist/stage.js.map +1 -0
  61. package/dist/static.d.ts +9 -0
  62. package/dist/static.js +55 -0
  63. package/dist/static.js.map +1 -0
  64. package/dist/util.d.ts +56 -0
  65. package/dist/util.js +127 -0
  66. package/dist/util.js.map +1 -0
  67. package/package.json +48 -3
  68. package/src/clients.ts +140 -0
  69. package/src/compat.ts +63 -0
  70. package/src/drivers/cognito.ts +147 -0
  71. package/src/drivers/dynamodb.ts +206 -0
  72. package/src/drivers/events.ts +92 -0
  73. package/src/drivers/iam.ts +125 -0
  74. package/src/drivers/index.ts +12 -0
  75. package/src/drivers/kvs.ts +55 -0
  76. package/src/drivers/lambda.ts +283 -0
  77. package/src/drivers/logs.ts +81 -0
  78. package/src/drivers/s3.ts +196 -0
  79. package/src/drivers/scheduler.ts +90 -0
  80. package/src/drivers/sqs.ts +70 -0
  81. package/src/drivers/ssm.ts +65 -0
  82. package/src/drivers/web-function.ts +352 -0
  83. package/src/emulator.ts +203 -0
  84. package/src/index.ts +183 -0
  85. package/src/local.ts +75 -0
  86. package/src/policy.ts +377 -0
  87. package/src/stage.ts +568 -0
  88. package/src/static.ts +56 -0
  89. package/src/util.ts +175 -0
  90. package/README.md +0 -3
package/src/util.ts ADDED
@@ -0,0 +1,175 @@
1
+ import { TAGS } from '@ericminassian/platform-core';
2
+ import type { AwsClients } from './clients.ts';
3
+
4
+ export type EngineLog = (level: 'info' | 'warn' | 'error', msg: string, resource?: string) => void;
5
+
6
+ /** What every driver receives. */
7
+ export interface EngineContext {
8
+ clients: AwsClients;
9
+ region: string;
10
+ target: 'aws' | 'emulator';
11
+ log: EngineLog;
12
+ accountId(): Promise<string>;
13
+ }
14
+
15
+ export const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
16
+
17
+ export function errorName(e: unknown): string {
18
+ const err = e as { name?: string; Code?: string; code?: string; __type?: string } | undefined;
19
+ return err?.name ?? err?.Code ?? err?.code ?? err?.__type ?? '';
20
+ }
21
+
22
+ export function errorMessage(e: unknown): string {
23
+ return e instanceof Error ? e.message : String(e);
24
+ }
25
+
26
+ const NOT_FOUND = new Set([
27
+ 'ResourceNotFoundException',
28
+ 'ResourceNotFound',
29
+ 'NoSuchEntity',
30
+ 'NoSuchEntityException',
31
+ 'NotFoundException',
32
+ 'NotFound',
33
+ 'NoSuchBucket',
34
+ 'NoSuchKey',
35
+ 'ParameterNotFound',
36
+ 'QueueDoesNotExist',
37
+ 'AWS.SimpleQueueService.NonExistentQueue',
38
+ 'NoSuchHostedZone',
39
+ 'NoSuchFunctionExists',
40
+ 'NoSuchOriginAccessControl',
41
+ 'NoSuchCachePolicy',
42
+ 'NoSuchDistribution',
43
+ 'EntityNotFound',
44
+ ]);
45
+
46
+ export function isNotFound(e: unknown): boolean {
47
+ const name = errorName(e);
48
+ if (NOT_FOUND.has(name)) return true;
49
+ const status = (e as { $metadata?: { httpStatusCode?: number } })?.$metadata?.httpStatusCode;
50
+ return status === 404 && !name.includes('Conflict');
51
+ }
52
+
53
+ export function isNotImplemented(e: unknown): boolean {
54
+ return errorName(e) === 'NotImplemented' || /not implemented/i.test(errorMessage(e));
55
+ }
56
+
57
+ /** Swallow not-found errors (for deletes and probes). */
58
+ export async function ignoreNotFound<T>(p: Promise<T>): Promise<T | undefined> {
59
+ try {
60
+ return await p;
61
+ } catch (e) {
62
+ if (isNotFound(e)) return undefined;
63
+ throw e;
64
+ }
65
+ }
66
+
67
+ /** Poll until `fn` returns a truthy value. */
68
+ export async function waitFor<T>(
69
+ what: string,
70
+ fn: () => Promise<T | undefined | null | false>,
71
+ opts: { timeoutMs?: number; intervalMs?: number } = {},
72
+ ): Promise<T> {
73
+ const deadline = Date.now() + (opts.timeoutMs ?? 120_000);
74
+ let interval = opts.intervalMs ?? 500;
75
+ for (;;) {
76
+ const v = await fn();
77
+ if (v) return v;
78
+ if (Date.now() > deadline) throw new Error(`Timed out waiting for ${what}`);
79
+ await sleep(interval);
80
+ interval = Math.min(interval * 1.5, 5_000);
81
+ }
82
+ }
83
+
84
+ /** Retry while `when(error)` is true (e.g. IAM eventual consistency). */
85
+ export async function retry<T>(
86
+ fn: () => Promise<T>,
87
+ opts: { when: (e: unknown) => boolean; attempts?: number; delayMs?: number },
88
+ ): Promise<T> {
89
+ const attempts = opts.attempts ?? 10;
90
+ let delay = opts.delayMs ?? 1_000;
91
+ for (let i = 1; ; i++) {
92
+ try {
93
+ return await fn();
94
+ } catch (e) {
95
+ if (i >= attempts || !opts.when(e)) throw e;
96
+ await sleep(delay);
97
+ delay = Math.min(delay * 1.5, 10_000);
98
+ }
99
+ }
100
+ }
101
+
102
+ /** Run async work over items with bounded concurrency. */
103
+ export async function mapLimit<T, R>(
104
+ items: readonly T[],
105
+ limit: number,
106
+ fn: (item: T) => Promise<R>,
107
+ ): Promise<R[]> {
108
+ const out: R[] = new Array(items.length);
109
+ let next = 0;
110
+ const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
111
+ while (next < items.length) {
112
+ const i = next++;
113
+ out[i] = await fn(items[i]!);
114
+ }
115
+ });
116
+ await Promise.all(workers);
117
+ return out;
118
+ }
119
+
120
+ // ------------------------------------------------------------------ tags
121
+
122
+ export function stageTags(app: string, stage: string): Record<string, string> {
123
+ return { [TAGS.managed]: 'true', [TAGS.app]: app, [TAGS.stage]: stage };
124
+ }
125
+
126
+ export function platformTags(component: string): Record<string, string> {
127
+ return { [TAGS.managed]: 'true', 'platform:component': component };
128
+ }
129
+
130
+ export function tagList(tags: Record<string, string>): { Key: string; Value: string }[] {
131
+ return Object.entries(tags).map(([Key, Value]) => ({ Key, Value }));
132
+ }
133
+
134
+ export function tagListLower(tags: Record<string, string>): { key: string; value: string }[] {
135
+ return Object.entries(tags).map(([key, value]) => ({ key, value }));
136
+ }
137
+
138
+ // ------------------------------------------------------------------ arns
139
+
140
+ export interface PolicyStatement {
141
+ Sid?: string;
142
+ Effect: 'Allow' | 'Deny';
143
+ Action: string | string[];
144
+ Resource?: string | string[];
145
+ NotResource?: string | string[];
146
+ Principal?: Record<string, string | string[]>;
147
+ Condition?: Record<string, Record<string, string | string[] | boolean>>;
148
+ }
149
+
150
+ export interface PolicyDocument {
151
+ Version: '2012-10-17';
152
+ Statement: PolicyStatement[];
153
+ }
154
+
155
+ export function policy(...statements: PolicyStatement[]): PolicyDocument {
156
+ return { Version: '2012-10-17', Statement: statements };
157
+ }
158
+
159
+ /** Deep-equal for policy documents that may come back URL-encoded from IAM. */
160
+ export function samePolicy(a: string | PolicyDocument | undefined, b: PolicyDocument): boolean {
161
+ if (!a) return false;
162
+ const parsed = typeof a === 'string' ? JSON.parse(decodeURIComponent(a)) : a;
163
+ return canonical(parsed) === canonical(b);
164
+ }
165
+
166
+ function canonical(v: unknown): string {
167
+ if (Array.isArray(v)) return `[${v.map(canonical).join(',')}]`;
168
+ if (v && typeof v === 'object') {
169
+ return `{${Object.keys(v as object)
170
+ .sort()
171
+ .map((k) => `${JSON.stringify(k)}:${canonical((v as Record<string, unknown>)[k])}`)
172
+ .join(',')}}`;
173
+ }
174
+ return JSON.stringify(v);
175
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.