@ericminassian/platform-core 0.3.0 → 0.4.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 (88) hide show
  1. package/dist/api.d.ts +942 -45
  2. package/dist/api.js +372 -13
  3. package/dist/api.js.map +1 -1
  4. package/dist/auth.js +5 -2
  5. package/dist/auth.js.map +1 -1
  6. package/dist/base64url.d.ts +3 -0
  7. package/dist/base64url.js +15 -0
  8. package/dist/base64url.js.map +1 -0
  9. package/dist/bindings.d.ts +62 -12
  10. package/dist/bindings.js +1 -1
  11. package/dist/bindings.js.map +1 -1
  12. package/dist/cloudfront-function.d.ts +5 -1
  13. package/dist/cloudfront-function.js +64 -2
  14. package/dist/cloudfront-function.js.map +1 -1
  15. package/dist/compute.d.ts +52 -0
  16. package/dist/compute.js +65 -0
  17. package/dist/compute.js.map +1 -0
  18. package/dist/config.d.ts +18 -0
  19. package/dist/config.js +9 -0
  20. package/dist/config.js.map +1 -1
  21. package/dist/constants.d.ts +56 -1
  22. package/dist/constants.js +56 -1
  23. package/dist/constants.js.map +1 -1
  24. package/dist/email.d.ts +84 -0
  25. package/dist/email.js +111 -0
  26. package/dist/email.js.map +1 -0
  27. package/dist/fingerprint.d.ts +9 -0
  28. package/dist/fingerprint.js +19 -0
  29. package/dist/fingerprint.js.map +1 -0
  30. package/dist/index.d.ts +6 -0
  31. package/dist/index.js +6 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/jobs.d.ts +111 -0
  34. package/dist/jobs.js +214 -0
  35. package/dist/jobs.js.map +1 -0
  36. package/dist/manifest.d.ts +270 -29
  37. package/dist/manifest.js +190 -22
  38. package/dist/manifest.js.map +1 -1
  39. package/dist/naming.d.ts +83 -7
  40. package/dist/naming.js +89 -8
  41. package/dist/naming.js.map +1 -1
  42. package/dist/platform-api.d.ts +3733 -1278
  43. package/dist/platform-api.js +120 -8
  44. package/dist/platform-api.js.map +1 -1
  45. package/dist/realtime.d.ts +139 -0
  46. package/dist/realtime.js +201 -0
  47. package/dist/realtime.js.map +1 -0
  48. package/dist/router/decide.d.ts +2 -0
  49. package/dist/router/decide.js +38 -6
  50. package/dist/router.d.ts +35 -13
  51. package/dist/router.js +36 -0
  52. package/dist/router.js.map +1 -1
  53. package/dist/runtime-config.d.ts +57 -0
  54. package/dist/runtime-config.js +12 -0
  55. package/dist/runtime-config.js.map +1 -1
  56. package/dist/storage.d.ts +55 -0
  57. package/dist/storage.js +112 -0
  58. package/dist/storage.js.map +1 -0
  59. package/dist/telemetry.d.ts +124 -19
  60. package/dist/telemetry.js +97 -21
  61. package/dist/telemetry.js.map +1 -1
  62. package/dist/web-compute.d.ts +191 -0
  63. package/dist/web-compute.js +257 -0
  64. package/dist/web-compute.js.map +1 -0
  65. package/package.json +19 -7
  66. package/src/api.ts +445 -15
  67. package/src/auth.ts +6 -2
  68. package/src/base64url.ts +13 -0
  69. package/src/bindings.ts +67 -6
  70. package/src/cloudfront-function.ts +64 -2
  71. package/src/compute.ts +86 -0
  72. package/src/config.ts +31 -1
  73. package/src/constants.ts +64 -1
  74. package/src/email.ts +168 -0
  75. package/src/fingerprint.ts +20 -0
  76. package/src/index.ts +6 -0
  77. package/src/jobs.ts +281 -0
  78. package/src/manifest.ts +415 -45
  79. package/src/naming.ts +93 -9
  80. package/src/platform-api.ts +134 -8
  81. package/src/realtime.ts +298 -0
  82. package/src/router/decide.d.ts +2 -0
  83. package/src/router/decide.js +38 -6
  84. package/src/router.ts +33 -13
  85. package/src/runtime-config.ts +70 -0
  86. package/src/storage.ts +120 -0
  87. package/src/telemetry.ts +104 -21
  88. package/src/web-compute.ts +342 -0
@@ -5,21 +5,34 @@
5
5
  // when inlined.
6
6
  //
7
7
  // rec: route record from the KeyValueStore (see RouteRecord in router.ts)
8
- // o - Web Function endpoint domain (server), optional
8
+ // o - the server's domain, optional: a Web Function endpoint, or a function URL when c is 'r'
9
+ // (request mode). Which one only changes how the CloudFront Function calls it (OAC, or
10
+ // the origin secret k in a header); `plat dev` runs the server itself.
9
11
  // s - static key prefix in the sites bucket, e.g. "foo/prod/d01j..." , optional
10
12
  // r - server route prefixes when both o and s exist, e.g. ["/api/"]
11
13
  // spa - 1 to serve index.html for unknown non-file paths
12
- // returns { t: 'server' } | { t: 'static', key: '/<prefix>/<path>' } | { t: 'health' } | { t: 'notfound' }
13
- // health - the router answers /__platform/health itself (HealthResponse with rec.d as the
14
- // revision): a stage without a server has nothing else to report what it serves.
14
+ // rt - realtime: { n: channel namespace, m: 'p' | 's' }, optional
15
+ // p - public store names, optional
16
+ // returns { t: 'server' } | { t: 'static', key: '/<prefix>/<path>' } | { t: 'health' } | { t: 'realtime' }
17
+ // | { t: 'storage', key: '/<app>/<stage>/<store>/<key>' } | { t: 'notfound' }
18
+ // health - the router answers /__platform/health itself (HealthResponse with rec.d as the
19
+ // revision): a stage without a server has nothing else to report what it serves.
20
+ // realtime - /__platform/realtime is the router's own: it answers the browser's config, or passes
21
+ // a WebSocket to AppSync. Other /__platform/realtime/* paths (the token) are the server's.
22
+ // storage - /__platform/storage/<store>/<key> of a public store, served from the storage bucket
23
+ // (whose origin path adds `_public`); every other path below /__platform/storage/ is
24
+ // not found, so it never reaches the server. The key stays percent-encoded, as S3
25
+ // decodes it, and keys with `.` or `..` segments (also encoded) are refused.
15
26
 
16
27
  export function decide(rec, uri) {
17
- if (!rec || (!rec.o && !rec.s)) return { t: 'notfound' };
28
+ if (!rec || (!rec.o && !rec.s && !rec.p)) return { t: 'notfound' };
18
29
  if (uri.indexOf('/__platform/') === 0) {
30
+ if (uri.indexOf('/__platform/storage/') === 0) return storage(rec, uri.substring(20));
31
+ if (uri === '/__platform/realtime' && rec.rt) return { t: 'realtime' };
19
32
  if (rec.o) return { t: 'server' };
20
33
  return uri === '/__platform/health' ? { t: 'health' } : { t: 'notfound' };
21
34
  }
22
- if (!rec.s) return { t: 'server' };
35
+ if (!rec.s) return rec.o ? { t: 'server' } : { t: 'notfound' };
23
36
  if (rec.o) {
24
37
  var routes = rec.r || [];
25
38
  for (var i = 0; i < routes.length; i++) {
@@ -35,3 +48,22 @@ export function decide(rec, uri) {
35
48
  var dir = uri.charAt(uri.length - 1) === '/' ? uri : uri + '/';
36
49
  return { t: 'static', key: '/' + rec.s + dir + 'index.html' };
37
50
  }
51
+
52
+ function storage(rec, rest) {
53
+ var slash = rest.indexOf('/');
54
+ if (slash <= 0) return { t: 'notfound' };
55
+ var store = rest.substring(0, slash);
56
+ var key = rest.substring(slash + 1);
57
+ if (!rec.p || rec.p.indexOf(store) === -1 || !servableKey(key)) return { t: 'notfound' };
58
+ return { t: 'storage', key: '/' + rec.app + '/' + rec.stage + '/' + store + '/' + key };
59
+ }
60
+
61
+ // isServableKey in storage.ts. S3 decodes the key once, so an encoded dot or slash counts as one.
62
+ function servableKey(key) {
63
+ if (!key) return false;
64
+ var parts = key.replace(/%2e/gi, '.').replace(/%2f/gi, '/').split('/');
65
+ for (var i = 0; i < parts.length; i++) {
66
+ if (parts[i] === '.' || parts[i] === '..') return false;
67
+ }
68
+ return true;
69
+ }
package/dist/router.d.ts CHANGED
@@ -1,21 +1,43 @@
1
+ import { Schema } from 'effect';
1
2
  export type { RouteDecision } from './router/decide.js';
2
3
  export { decide } from './router/decide.js';
3
4
  /**
4
- * Value stored in the CloudFront KeyValueStore under the public hostname.
5
- * Keys are short because KVS values are capped at 1 KB.
5
+ * Value stored in the CloudFront KeyValueStore under the public hostname, and
6
+ * kept with each deployment for rollbacks. Keys are short because KVS values
7
+ * are capped at 1 KB.
6
8
  */
7
- export interface RouteRecord {
8
- v: 1;
9
- app: string;
10
- stage: string;
9
+ export declare const RouteRecordSchema: Schema.Struct<{
10
+ readonly v: Schema.Literal<1>;
11
+ readonly app: Schema.String;
12
+ readonly stage: Schema.String;
11
13
  /** Deployment id currently live. */
12
- d: string;
13
- /** Web Function endpoint domain. */
14
- o?: string;
14
+ readonly d: Schema.String;
15
+ /** The server's domain: a Web Function endpoint's, or with `c` a function URL's. */
16
+ readonly o: Schema.optional<Schema.String>;
17
+ /**
18
+ * `r`: `o` is a request-mode server (a function URL, `WebCompute`
19
+ * `request`), which the router calls without OAC, sending `k` in
20
+ * `ORIGIN_HEADER`. Absent: a Web Function endpoint, called with OAC.
21
+ */
22
+ readonly c: Schema.optional<Schema.Literal<"r">>;
23
+ /** The request-mode server's `originSecret`. */
24
+ readonly k: Schema.optional<Schema.String>;
15
25
  /** Static prefix in the sites bucket (no leading slash). */
16
- s?: string;
26
+ readonly s: Schema.optional<Schema.String>;
17
27
  /** Server route prefixes (only meaningful when both `o` and `s` exist). */
18
- r?: readonly string[];
19
- spa?: 0 | 1;
20
- }
28
+ readonly r: Schema.optional<Schema.$Array<Schema.String>>;
29
+ readonly spa: Schema.optional<Schema.Literals<readonly [0, 1]>>;
30
+ /**
31
+ * Realtime (`plat.config.ts` `realtime`): the stage's channel namespace and
32
+ * who subscribes (`p` public with the API key, `s` signed-in with a token).
33
+ * The router answers `REALTIME_PATH` only for records that have it.
34
+ */
35
+ readonly rt: Schema.optional<Schema.Struct<{
36
+ readonly n: Schema.String;
37
+ readonly m: Schema.Literals<readonly ["p", "s"]>;
38
+ }>>;
39
+ /** Public stores, which the router serves from the storage bucket at `STORAGE_PATH`. */
40
+ readonly p: Schema.optional<Schema.$Array<Schema.String>>;
41
+ }>;
42
+ export type RouteRecord = typeof RouteRecordSchema.Type;
21
43
  export declare function encodeRouteRecord(rec: RouteRecord): string;
package/dist/router.js CHANGED
@@ -1,4 +1,40 @@
1
+ import { Schema } from 'effect';
1
2
  export { decide } from './router/decide.js';
3
+ /**
4
+ * Value stored in the CloudFront KeyValueStore under the public hostname, and
5
+ * kept with each deployment for rollbacks. Keys are short because KVS values
6
+ * are capped at 1 KB.
7
+ */
8
+ export const RouteRecordSchema = Schema.Struct({
9
+ v: Schema.Literal(1),
10
+ app: Schema.String,
11
+ stage: Schema.String,
12
+ /** Deployment id currently live. */
13
+ d: Schema.String,
14
+ /** The server's domain: a Web Function endpoint's, or with `c` a function URL's. */
15
+ o: Schema.optional(Schema.String),
16
+ /**
17
+ * `r`: `o` is a request-mode server (a function URL, `WebCompute`
18
+ * `request`), which the router calls without OAC, sending `k` in
19
+ * `ORIGIN_HEADER`. Absent: a Web Function endpoint, called with OAC.
20
+ */
21
+ c: Schema.optional(Schema.Literal('r')),
22
+ /** The request-mode server's `originSecret`. */
23
+ k: Schema.optional(Schema.String),
24
+ /** Static prefix in the sites bucket (no leading slash). */
25
+ s: Schema.optional(Schema.String),
26
+ /** Server route prefixes (only meaningful when both `o` and `s` exist). */
27
+ r: Schema.optional(Schema.Array(Schema.String)),
28
+ spa: Schema.optional(Schema.Literals([0, 1])),
29
+ /**
30
+ * Realtime (`plat.config.ts` `realtime`): the stage's channel namespace and
31
+ * who subscribes (`p` public with the API key, `s` signed-in with a token).
32
+ * The router answers `REALTIME_PATH` only for records that have it.
33
+ */
34
+ rt: Schema.optional(Schema.Struct({ n: Schema.String, m: Schema.Literals(['p', 's']) })),
35
+ /** Public stores, which the router serves from the storage bucket at `STORAGE_PATH`. */
36
+ p: Schema.optional(Schema.Array(Schema.String)),
37
+ });
2
38
  export function encodeRouteRecord(rec) {
3
39
  const json = JSON.stringify(rec);
4
40
  if (new TextEncoder().encode(json).length > 1024)
@@ -1 +1 @@
1
- {"version":3,"file":"router.js","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAqB5C,MAAM,UAAU,iBAAiB,CAAC,GAAgB;IAChD,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,IAAI;QAC9C,MAAM,IAAI,KAAK,CAAC,oBAAoB,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,eAAe,CAAC,CAAC;IAC3E,OAAO,IAAI,CAAC;AACd,CAAC"}
1
+ {"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MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;CAChD,CAAC,CAAC;AAGH,MAAM,UAAU,iBAAiB,CAAC,GAAgB;IAChD,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,IAAI;QAC9C,MAAM,IAAI,KAAK,CAAC,oBAAoB,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,eAAe,CAAC,CAAC;IAC3E,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -26,6 +26,8 @@ export interface PlatformRuntimeConfig {
26
26
  kvsArn?: string;
27
27
  /** The router distribution; Web Function resource policies trust only this. */
28
28
  distributionArn?: string;
29
+ /** The edge floor's alarm and trigger; absent without `limits.edgeFloorAt`. */
30
+ edgeFloor?: EdgeFloorRuntimeConfig;
29
31
  /** Shared Cognito user pool for app end users. */
30
32
  userPoolId?: string;
31
33
  /** Hosted login origin, e.g. `https://login.platform.ericminassian.com`. */
@@ -36,7 +38,62 @@ export interface PlatformRuntimeConfig {
36
38
  alertsTopicArn?: string;
37
39
  /** Telemetry processor (account-level log subscription target). */
38
40
  telemetryFunctionArn?: string;
41
+ /** The AppSync Event API stages' channel namespaces live in (absent on platforms that predate it). */
42
+ realtime?: RealtimeRuntimeConfig;
43
+ /** The SES domain every stage sends from (absent on platforms that predate email). */
44
+ email?: EmailRuntimeConfig;
45
+ /** Container compute, ECS on Fargate (absent on platforms that predate it). */
46
+ containers?: ContainersRuntimeConfig;
39
47
  }
48
+ /**
49
+ * Where containers run. Tasks get a public IPv4 address in a public subnet
50
+ * (there is no NAT gateway) and accept no inbound traffic.
51
+ */
52
+ export interface ContainersRuntimeConfig {
53
+ /** ECS cluster name. */
54
+ cluster: string;
55
+ subnets: string[];
56
+ securityGroups: string[];
57
+ /** ECR repository name. */
58
+ repository: string;
59
+ /** `<account>.dkr.ecr.<region>.amazonaws.com/<repository>`. */
60
+ repositoryUri: string;
61
+ /** Pulls images and writes container logs. */
62
+ executionRoleArn: string;
63
+ /** Assumed by EventBridge Scheduler to scale a stage's services (preview stop schedules). */
64
+ schedulerRoleArn: string;
65
+ }
66
+ /**
67
+ * The edge floor (`limits.edgeFloorAt`): a CloudWatch alarm on the router
68
+ * distribution's requests per minute, and an EventBridge rule that invokes
69
+ * the control plane's worker when it goes off. The web ACL itself belongs to
70
+ * the control plane, which creates it only while the floor is on.
71
+ */
72
+ export interface EdgeFloorRuntimeConfig {
73
+ alarmArn: string;
74
+ /** The rule's ARN, which the worker's resource policy names. */
75
+ ruleArn: string;
76
+ }
77
+ export interface EmailRuntimeConfig {
78
+ /** Verified domain identity: `mail.<zone>` (`mail.localhost` in the emulator). */
79
+ domain: string;
80
+ identityArn: string;
81
+ }
82
+ export interface RealtimeRuntimeConfig {
83
+ apiId: string;
84
+ /** HTTP domain (`<id>.appsync-api.<region>.amazonaws.com`; `127.0.0.1:<port>` in the emulator). */
85
+ httpDomain: string;
86
+ /** Realtime (WebSocket) domain. */
87
+ realtimeDomain: string;
88
+ }
89
+ /**
90
+ * Where to publish and connect. AWS serves TLS; the emulator serves plain
91
+ * HTTP and WebSocket on local ports.
92
+ */
93
+ export declare function realtimeEndpoints(cfg: Pick<PlatformRuntimeConfig, 'target'>, rt: RealtimeRuntimeConfig): {
94
+ publishUrl: string;
95
+ websocketUrl: string;
96
+ };
40
97
  export declare function cognitoIssuer(region: string, userPoolId: string): string;
41
98
  export declare function cognitoJwksUri(cfg: Pick<PlatformRuntimeConfig, 'region' | 'jwksBaseUrl'>, userPoolId: string): string;
42
99
  export declare function publicUrl(cfg: Pick<PlatformRuntimeConfig, 'publicScheme' | 'publicPort'>, host: string): string;
@@ -1,3 +1,15 @@
1
+ import { APPSYNC_PUBLISH_PATH, APPSYNC_REALTIME_PATH } from './realtime.js';
2
+ /**
3
+ * Where to publish and connect. AWS serves TLS; the emulator serves plain
4
+ * HTTP and WebSocket on local ports.
5
+ */
6
+ export function realtimeEndpoints(cfg, rt) {
7
+ const tls = cfg.target === 'aws';
8
+ return {
9
+ publishUrl: `${tls ? 'https' : 'http'}://${rt.httpDomain}${APPSYNC_PUBLISH_PATH}`,
10
+ websocketUrl: `${tls ? 'wss' : 'ws'}://${rt.realtimeDomain}${APPSYNC_REALTIME_PATH}`,
11
+ };
12
+ }
1
13
  export function cognitoIssuer(region, userPoolId) {
2
14
  return `https://cognito-idp.${region}.amazonaws.com/${userPoolId}`;
3
15
  }
@@ -1 +1 @@
1
- {"version":3,"file":"runtime-config.js","sourceRoot":"","sources":["../src/runtime-config.ts"],"names":[],"mappings":"AAwCA,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,UAAkB;IAC9D,OAAO,uBAAuB,MAAM,kBAAkB,UAAU,EAAE,CAAC;AACrE,CAAC;AAED,MAAM,UAAU,cAAc,CAC5B,GAA0D,EAC1D,UAAkB;IAElB,MAAM,IAAI,GAAG,GAAG,CAAC,WAAW,IAAI,uBAAuB,GAAG,CAAC,MAAM,gBAAgB,CAAC;IAClF,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,UAAU,wBAAwB,CAAC;AAC1E,CAAC;AAED,MAAM,UAAU,SAAS,CACvB,GAA+D,EAC/D,IAAY;IAEZ,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACxD,OAAO,GAAG,GAAG,CAAC,YAAY,MAAM,IAAI,GAAG,IAAI,EAAE,CAAC;AAChD,CAAC"}
1
+ {"version":3,"file":"runtime-config.js","sourceRoot":"","sources":["../src/runtime-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AA+F5E;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAC/B,GAA0C,EAC1C,EAAyB;IAEzB,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,KAAK,KAAK,CAAC;IACjC,OAAO;QACL,UAAU,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,MAAM,EAAE,CAAC,UAAU,GAAG,oBAAoB,EAAE;QACjF,YAAY,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC,cAAc,GAAG,qBAAqB,EAAE;KACrF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,UAAkB;IAC9D,OAAO,uBAAuB,MAAM,kBAAkB,UAAU,EAAE,CAAC;AACrE,CAAC;AAED,MAAM,UAAU,cAAc,CAC5B,GAA0D,EAC1D,UAAkB;IAElB,MAAM,IAAI,GAAG,GAAG,CAAC,WAAW,IAAI,uBAAuB,GAAG,CAAC,MAAM,gBAAgB,CAAC;IAClF,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,UAAU,wBAAwB,CAAC;AAC1E,CAAC;AAED,MAAM,UAAU,SAAS,CACvB,GAA+D,EAC/D,IAAY;IAEZ,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACxD,OAAO,GAAG,GAAG,CAAC,YAAY,MAAM,IAAI,GAAG,IAAI,EAAE,CAAC;AAChD,CAAC"}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * File stores (`storage` in `plat.config.ts`). Every stage's stores share the
3
+ * platform's storage bucket:
4
+ *
5
+ * - a private store is `<app>/<stage>/<store>/` (`names.storagePrefix`);
6
+ * browsers reach its files only through presigned URLs;
7
+ * - a public store is `_public/<app>/<stage>/<store>/`, which the edge serves
8
+ * at `/__platform/storage/<store>/<key>` on the app's own host. The router
9
+ * distribution may read `_public/*` and nothing else, so a routing mistake
10
+ * can't expose a private store. `_` can't start an app name, so no app's
11
+ * prefix collides with it.
12
+ *
13
+ * Isomorphic: the SDK, its browser client, the CLI and the engine share it.
14
+ */
15
+ /** Where the edge serves public stores on every app host: `/__platform/storage/<store>/<key>`. */
16
+ export declare const STORAGE_PATH = "/__platform/storage/";
17
+ /** Top-level prefix of public stores in the storage bucket. */
18
+ export declare const PUBLIC_STORAGE_ROOT = "_public/";
19
+ /**
20
+ * Headers the edge adds to every public-store response (the router
21
+ * distribution's response headers policy; `plat dev` sends the same).
22
+ * `script-src 'none'` keeps an uploaded HTML or SVG file from running script
23
+ * on the app's origin, where its session cookie lives (`sandbox` would also
24
+ * stop PDFs from opening in Chrome).
25
+ */
26
+ export declare const STORAGE_RESPONSE_HEADERS: {
27
+ readonly 'x-content-type-options': 'nosniff';
28
+ readonly 'content-security-policy': "script-src 'none'";
29
+ };
30
+ /**
31
+ * The router's `x-platform-host` for a public store's file, in place of the
32
+ * host. CloudFront's cache key is the rewritten URI plus that header, not the
33
+ * behavior or origin, and an app's server could be asked for the same path
34
+ * (`/<app>/<stage>/<store>/<key>`) on the app's host: with the host in both,
35
+ * its response would be served for the file, and the file for that path. No
36
+ * routed host has this value.
37
+ */
38
+ export declare const STORAGE_CACHE_HOST = "__storage";
39
+ /** `Cache-Control` the edge adds to a public file that carries none: browsers revalidate it. */
40
+ export declare const STORAGE_DEFAULT_CACHE_CONTROL = "no-cache";
41
+ /**
42
+ * Whether the edge serves `key` (as it appears in a URL path, so still
43
+ * percent-encoded): not empty, and without `.` or `..` segments. S3 decodes
44
+ * the key once, so `%2e` counts as a dot and `%2f` as a slash. The router
45
+ * inlines the same check (`router/decide.js`).
46
+ */
47
+ export declare function isServableKey(key: string): boolean;
48
+ /**
49
+ * The path the edge serves a public store's file at, with each key segment
50
+ * percent-encoded: `storagePath('snapshots', 'fires/today 1.json')` →
51
+ * `/__platform/storage/snapshots/fires/today%201.json`.
52
+ */
53
+ export declare function storagePath(store: string, key: string): string;
54
+ /** The content type for a file name (or key), `application/octet-stream` when the extension is unknown. */
55
+ export declare function contentTypeFor(path: string): string;
@@ -0,0 +1,112 @@
1
+ /**
2
+ * File stores (`storage` in `plat.config.ts`). Every stage's stores share the
3
+ * platform's storage bucket:
4
+ *
5
+ * - a private store is `<app>/<stage>/<store>/` (`names.storagePrefix`);
6
+ * browsers reach its files only through presigned URLs;
7
+ * - a public store is `_public/<app>/<stage>/<store>/`, which the edge serves
8
+ * at `/__platform/storage/<store>/<key>` on the app's own host. The router
9
+ * distribution may read `_public/*` and nothing else, so a routing mistake
10
+ * can't expose a private store. `_` can't start an app name, so no app's
11
+ * prefix collides with it.
12
+ *
13
+ * Isomorphic: the SDK, its browser client, the CLI and the engine share it.
14
+ */
15
+ /** Where the edge serves public stores on every app host: `/__platform/storage/<store>/<key>`. */
16
+ export const STORAGE_PATH = '/__platform/storage/';
17
+ /** Top-level prefix of public stores in the storage bucket. */
18
+ export const PUBLIC_STORAGE_ROOT = '_public/';
19
+ /**
20
+ * Headers the edge adds to every public-store response (the router
21
+ * distribution's response headers policy; `plat dev` sends the same).
22
+ * `script-src 'none'` keeps an uploaded HTML or SVG file from running script
23
+ * on the app's origin, where its session cookie lives (`sandbox` would also
24
+ * stop PDFs from opening in Chrome).
25
+ */
26
+ export const STORAGE_RESPONSE_HEADERS = {
27
+ 'x-content-type-options': 'nosniff',
28
+ 'content-security-policy': "script-src 'none'",
29
+ };
30
+ /**
31
+ * The router's `x-platform-host` for a public store's file, in place of the
32
+ * host. CloudFront's cache key is the rewritten URI plus that header, not the
33
+ * behavior or origin, and an app's server could be asked for the same path
34
+ * (`/<app>/<stage>/<store>/<key>`) on the app's host: with the host in both,
35
+ * its response would be served for the file, and the file for that path. No
36
+ * routed host has this value.
37
+ */
38
+ export const STORAGE_CACHE_HOST = '__storage';
39
+ /** `Cache-Control` the edge adds to a public file that carries none: browsers revalidate it. */
40
+ export const STORAGE_DEFAULT_CACHE_CONTROL = 'no-cache';
41
+ /**
42
+ * Whether the edge serves `key` (as it appears in a URL path, so still
43
+ * percent-encoded): not empty, and without `.` or `..` segments. S3 decodes
44
+ * the key once, so `%2e` counts as a dot and `%2f` as a slash. The router
45
+ * inlines the same check (`router/decide.js`).
46
+ */
47
+ export function isServableKey(key) {
48
+ if (!key)
49
+ return false;
50
+ return key
51
+ .replace(/%2e/gi, '.')
52
+ .replace(/%2f/gi, '/')
53
+ .split('/')
54
+ .every((part) => part !== '.' && part !== '..');
55
+ }
56
+ /**
57
+ * The path the edge serves a public store's file at, with each key segment
58
+ * percent-encoded: `storagePath('snapshots', 'fires/today 1.json')` →
59
+ * `/__platform/storage/snapshots/fires/today%201.json`.
60
+ */
61
+ export function storagePath(store, key) {
62
+ const k = key.replace(/^\/+/, '');
63
+ const path = k.split('/').map(encodeURIComponent).join('/');
64
+ if (!isServableKey(path))
65
+ throw new Error(`Storage key "${key}" can't be served from the edge`);
66
+ return `${STORAGE_PATH}${store}/${path}`;
67
+ }
68
+ /** Content types by file extension: static files, and store files put without one. */
69
+ const CONTENT_TYPES = {
70
+ html: 'text/html; charset=utf-8',
71
+ htm: 'text/html; charset=utf-8',
72
+ css: 'text/css; charset=utf-8',
73
+ js: 'text/javascript; charset=utf-8',
74
+ mjs: 'text/javascript; charset=utf-8',
75
+ cjs: 'text/javascript; charset=utf-8',
76
+ map: 'application/json; charset=utf-8',
77
+ json: 'application/json; charset=utf-8',
78
+ geojson: 'application/geo+json',
79
+ ndjson: 'application/x-ndjson',
80
+ webmanifest: 'application/manifest+json',
81
+ txt: 'text/plain; charset=utf-8',
82
+ csv: 'text/csv; charset=utf-8',
83
+ md: 'text/markdown; charset=utf-8',
84
+ xml: 'application/xml; charset=utf-8',
85
+ svg: 'image/svg+xml',
86
+ png: 'image/png',
87
+ jpg: 'image/jpeg',
88
+ jpeg: 'image/jpeg',
89
+ gif: 'image/gif',
90
+ webp: 'image/webp',
91
+ avif: 'image/avif',
92
+ ico: 'image/x-icon',
93
+ woff: 'font/woff',
94
+ woff2: 'font/woff2',
95
+ ttf: 'font/ttf',
96
+ otf: 'font/otf',
97
+ wasm: 'application/wasm',
98
+ pdf: 'application/pdf',
99
+ zip: 'application/zip',
100
+ gz: 'application/gzip',
101
+ mp4: 'video/mp4',
102
+ webm: 'video/webm',
103
+ mp3: 'audio/mpeg',
104
+ wav: 'audio/wav',
105
+ };
106
+ /** The content type for a file name (or key), `application/octet-stream` when the extension is unknown. */
107
+ export function contentTypeFor(path) {
108
+ const file = path.slice(path.lastIndexOf('/') + 1);
109
+ const dot = file.lastIndexOf('.');
110
+ return ((dot === -1 ? undefined : CONTENT_TYPES[file.slice(dot + 1).toLowerCase()]) ?? 'application/octet-stream');
111
+ }
112
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,kGAAkG;AAClG,MAAM,CAAC,MAAM,YAAY,GAAG,sBAAsB,CAAC;AAEnD,+DAA+D;AAC/D,MAAM,CAAC,MAAM,mBAAmB,GAAG,UAAU,CAAC;AAE9C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG;IACtC,wBAAwB,EAAE,SAAS;IACnC,yBAAyB,EAAE,mBAAmB;CACtC,CAAC;AAEX;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,WAAW,CAAC;AAE9C,gGAAgG;AAChG,MAAM,CAAC,MAAM,6BAA6B,GAAG,UAAU,CAAC;AAExD;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,GAAW;IACvC,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IACvB,OAAO,GAAG;SACP,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,KAAK,CAAC,GAAG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