@geekmidas/manifest 10.0.0-alpha.13 → 10.0.0-alpha.131

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.
package/dist/index.cjs CHANGED
@@ -34,6 +34,19 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
34
34
  */
35
35
  const DEFAULT_POSTGRES_VERSION = 18;
36
36
  /**
37
+ * What a `Telemetry` node provides to each process with an edge to it.
38
+ *
39
+ * `OTEL_SERVICE_NAME` is resolved per process — the app's name — so it is
40
+ * listed here but composed by whichever target hands it over.
41
+ */
42
+ const TELEMETRY_KEYS = [
43
+ "OTEL_EXPORTER_OTLP_ENDPOINT",
44
+ "OTEL_EXPORTER_OTLP_HEADERS",
45
+ "OTEL_SERVICE_NAME",
46
+ "OTEL_TRACES_SAMPLER",
47
+ "OTEL_TRACES_SAMPLER_ARG"
48
+ ];
49
+ /**
37
50
  * What each kind may derive from.
38
51
  *
39
52
  * Small enough to state exhaustively, and stating it makes cycles impossible
@@ -57,21 +70,26 @@ const DERIVES_FROM = {
57
70
  const PUBLIC = {
58
71
  objects: [],
59
72
  "file-server": ["url"],
60
- email: [],
73
+ email: ["inboxUrl"],
61
74
  database: [],
62
75
  "database-reader": [],
63
76
  "database-schema": [],
64
77
  cache: [],
65
78
  secret: [],
66
79
  credential: [],
80
+ encryption: [],
67
81
  "rest-api": ["url"],
82
+ "websocket-api": ["url"],
68
83
  queue: [],
69
84
  topic: [],
70
85
  oidc: ["issuer", "audience"],
86
+ "external-api": [],
71
87
  function: [],
72
88
  cron: [],
89
+ "mobile-app": [],
73
90
  site: ["url"],
74
- worker: []
91
+ worker: [],
92
+ telemetry: []
75
93
  };
76
94
  //#endregion
77
95
  //#region src/errors.ts
@@ -846,7 +864,9 @@ function cookieDomain(urls) {
846
864
  * cannot catch anything.
847
865
  */
848
866
  function providedKeyFor(id, kind, role) {
849
- return kind === "secret" ? environmentCase(id) : provideKey(id, role);
867
+ if (kind === "secret") return environmentCase(id);
868
+ if (kind === "telemetry") return "OTEL_EXPORTER_OTLP_ENDPOINT";
869
+ return provideKey(id, role);
850
870
  }
851
871
  //#endregion
852
872
  //#region src/derive.ts
@@ -909,10 +929,15 @@ function dependenciesOf(declaration) {
909
929
  const own = "dependencies" in declaration ? declaration.dependencies ?? [] : [];
910
930
  const calls = "calls" in declaration ? declaration.calls ?? [] : [];
911
931
  const nested = declaration.kind === "rest-api" ? declaration.endpoints.flatMap((endpoint) => endpoint.dependencies) : [];
932
+ const telemetry = "telemetry" in declaration && typeof declaration.telemetry === "string" ? [{
933
+ target: declaration.telemetry,
934
+ kind: "telemetry"
935
+ }] : [];
912
936
  return [
913
937
  ...own,
914
938
  ...calls,
915
- ...nested
939
+ ...nested,
940
+ ...telemetry
916
941
  ];
917
942
  }
918
943
  /**
@@ -946,11 +971,13 @@ function dependentsOf(manifest, id) {
946
971
  const PUBLIC_PREFIX = {
947
972
  static: "VITE_",
948
973
  tanstack: "VITE_",
949
- next: "NEXT_PUBLIC_"
974
+ next: "NEXT_PUBLIC_",
975
+ expo: "EXPO_PUBLIC_"
950
976
  };
951
977
  /**
952
978
  * The keys a site's bundle needs, mapped to the key each value comes from —
953
- * `{ VITE_API_URL: 'API_URL' }`.
979
+ * `{ VITE_API_URL: 'API_URL' }` — its edges' public values, and a site's own
980
+ * address (`{ VITE_WEB_URL: 'WEB_URL' }`).
954
981
  *
955
982
  * A rename, not a second derivation: `API_URL` is resolved once, by whatever
956
983
  * resolved it for the server, and the site reads the same value under the name
@@ -969,6 +996,10 @@ const PUBLIC_PREFIX = {
969
996
  function publicEnvFor(declaration, manifest) {
970
997
  const prefix = PUBLIC_PREFIX[declaration.variant];
971
998
  const keys = {};
999
+ if (declaration.kind === "site") {
1000
+ const own = provideKey(declaration.id, "url");
1001
+ keys[`${prefix}${own}`] = own;
1002
+ }
972
1003
  for (const edge of declaration.dependencies) {
973
1004
  const target = manifest[edge.target];
974
1005
  if (!target) continue;
@@ -980,36 +1011,288 @@ function publicEnvFor(declaration, manifest) {
980
1011
  return keys;
981
1012
  }
982
1013
  //#endregion
983
- //#region src/index.ts
984
- /** Flatten a manifest field (array or partitioned) into a plain array. */
985
- function flattenManifestField(field) {
986
- if (!field) return [];
987
- return Array.isArray(field) ? [...field] : Object.values(field).flat();
1014
+ //#region src/derived.ts
1015
+ /** Where a processor keeps what it made, in the bucket it reads. */
1016
+ const DERIVED_PREFIX = ".gkm-derived/";
1017
+ /** How long a derivative is kept when its file server does not say. */
1018
+ const DEFAULT_DERIVED_DAYS = 30;
1019
+ /**
1020
+ * The days a bucket keeps what is under each prefix: what it declared, and —
1021
+ * when a file server over it has processors — the derived prefix, for the
1022
+ * longest any of those servers asked. `undefined` when it expires nothing.
1023
+ */
1024
+ function bucketExpiry(manifest, bucketId) {
1025
+ const expire = { ...manifest[bucketId]?.expire ?? {} };
1026
+ const processed = Object.values(manifest).filter((d) => d.kind === "file-server" && d.of === bucketId && (d.processors?.length ?? 0) > 0);
1027
+ if (processed.length > 0 && expire[".gkm-derived/"] === void 0) expire[DERIVED_PREFIX] = Math.max(...processed.map((s) => s.derivedDays ?? 30));
1028
+ return Object.keys(expire).length > 0 ? expire : void 0;
1029
+ }
1030
+ //#endregion
1031
+ //#region src/encryption.ts
1032
+ /**
1033
+ * The `kms://` URL an `Encryption` construct reads on AWS.
1034
+ *
1035
+ * Here because two packages meet on it: `@geekmidas/cloud` composes it from
1036
+ * the keys it provisioned, and `@geekmidas/constructs` parses it to reach
1037
+ * them. One function, so the two cannot disagree about a parameter's name.
1038
+ */
1039
+ const KMS_SCHEME = "kms:";
1040
+ /**
1041
+ * `kms://<region>?key=<key arn>&index=<hmac key arn>[&endpoint=<url>]`
1042
+ *
1043
+ * `endpoint` reaches an emulator instead of AWS — floci, locally and in tests —
1044
+ * the same parameter an `sns://` connection string carries for the same reason.
1045
+ */
1046
+ function kmsUrl(input) {
1047
+ const query = new URLSearchParams({
1048
+ key: input.key,
1049
+ index: input.index,
1050
+ ...input.endpoint ? { endpoint: input.endpoint } : {}
1051
+ });
1052
+ return `${KMS_SCHEME}//${input.region}?${query}`;
1053
+ }
1054
+ //#endregion
1055
+ //#region src/external.ts
1056
+ /** The key in a by-stage `url` that answers for every stage not listed. */
1057
+ const DEFAULT_STAGE_URL = "default";
1058
+ /**
1059
+ * Where an external API answers on a deployed stage.
1060
+ *
1061
+ * One URL answers every stage; a record answers by stage name, falling back
1062
+ * to `default`. Read at build and deploy, so a stage nobody gave a URL fails
1063
+ * there — with the stage named — rather than at the first request.
1064
+ *
1065
+ * @throws {NoUrlForStage} when a record names neither the stage nor a default.
1066
+ */
1067
+ function externalApiUrl(declaration, stage) {
1068
+ const { url } = declaration;
1069
+ if (typeof url === "string") return url;
1070
+ const resolved = url[stage] ?? url["default"];
1071
+ if (!resolved) throw new NoUrlForStage(declaration.id, stage, Object.keys(url));
1072
+ return resolved;
1073
+ }
1074
+ /** An external API's `url` record has nothing for the stage being deployed. */
1075
+ var NoUrlForStage = class extends Error {
1076
+ id;
1077
+ stage;
1078
+ stages;
1079
+ constructor(id, stage, stages) {
1080
+ super(`'${id}' has no URL for the stage '${stage}': its url names ${stages.length ? stages.map((s) => `'${s}'`).join(", ") : "no stages"}. Add '${stage}', or a '${DEFAULT_STAGE_URL}' for every stage not listed.`);
1081
+ this.id = id;
1082
+ this.stage = stage;
1083
+ this.stages = stages;
1084
+ this.name = "NoUrlForStage";
1085
+ }
1086
+ };
1087
+ //#endregion
1088
+ //#region src/headers.ts
1089
+ /**
1090
+ * Headers gkm reserves for its own services' calls to each other.
1091
+ *
1092
+ * Shared by `@geekmidas/constructs`, which sends and reads them, and
1093
+ * `@geekmidas/cli`, whose edges strip them from every request that arrives
1094
+ * from outside — so the two cannot name them differently.
1095
+ */
1096
+ /**
1097
+ * The address of the client a service-to-service call is made on behalf of.
1098
+ *
1099
+ * An API asking its auth server for a session sends the client's address in
1100
+ * this rather than `X-Forwarded-For`: a forwarding header marks a request as
1101
+ * outside traffic, which would start the auth server's span in a new trace,
1102
+ * and the address is still needed — Better Auth rate-limits per client.
1103
+ *
1104
+ * Reserved: the stack's edge (Caddy, Traefik, the `gkm dev` edge) removes it
1105
+ * from incoming requests, and the auth server ignores it on any request that
1106
+ * is not an internal caller's, so it cannot be spoofed from outside.
1107
+ */
1108
+ const CLIENT_IP_HEADER = "x-gkm-client-ip";
1109
+ //#endregion
1110
+ //#region src/migrations.ts
1111
+ /** Where every construct's folder lives, from the project root. */
1112
+ const MIGRATIONS_ROOT = "db";
1113
+ /** The two folders a construct's folder holds, and nothing else. */
1114
+ const DATABASE_FOLDERS = ["migrations", "seeds"];
1115
+ /**
1116
+ * The constructs that take migrations, parents before the tenants inside them.
1117
+ *
1118
+ * A database and each schema tenant: every one has an owner role and a schema
1119
+ * of its own, and so a history of its own. A reader has neither — it reads its
1120
+ * parent's schema — and a cache's table is created by provisioning, not by a
1121
+ * migration anyone writes.
1122
+ */
1123
+ function migrationTargets(manifest) {
1124
+ return provisionOrder(manifest).flatMap((id) => {
1125
+ const declaration = manifest[id];
1126
+ if (!declaration) return [];
1127
+ if (declaration.kind === "database") return [target(id)];
1128
+ if (declaration.kind === "database-schema") return [{
1129
+ ...target(id),
1130
+ of: declaration.of
1131
+ }];
1132
+ return [];
1133
+ });
1134
+ }
1135
+ /**
1136
+ * A construct's folder, relative to the project root.
1137
+ *
1138
+ * Named by the construct — `AuthDatabase` is `db/auth-database` — in the case
1139
+ * every other name outside TypeScript takes. Lowercase, because a folder that
1140
+ * differs from another only in case is two folders on Linux and one on a Mac;
1141
+ * and `canonicalId` reads it back to the construct exactly, which is how a
1142
+ * folder naming nothing is caught rather than skipped.
1143
+ */
1144
+ function databaseFolder(id) {
1145
+ return `db/${kebabCase(id)}`;
1146
+ }
1147
+ /** Where a construct's migrations live: `db/auth-database/migrations`. */
1148
+ function migrationFolder(id) {
1149
+ return `${databaseFolder(id)}/migrations`;
1150
+ }
1151
+ /**
1152
+ * Where a construct's seeds live: `db/auth-database/seeds`.
1153
+ *
1154
+ * Reference data the app needs to run — a permission catalogue, roles, lookup
1155
+ * tables. Seeds are upserts run on every pass, after the migrations, so a
1156
+ * changed seed is applied by running it again. Sample data is factories'.
1157
+ */
1158
+ function seedFolder(id) {
1159
+ return `${databaseFolder(id)}/seeds`;
1160
+ }
1161
+ function target(id) {
1162
+ return {
1163
+ id,
1164
+ folder: databaseFolder(id),
1165
+ migrations: migrationFolder(id),
1166
+ seeds: seedFolder(id)
1167
+ };
1168
+ }
1169
+ //#endregion
1170
+ //#region src/mobile.ts
1171
+ /**
1172
+ * How a mobile app is reached: its URL scheme, and the origins a surface trusts
1173
+ * for it.
1174
+ *
1175
+ * Stated once, here, because three targets ask — the local one (`gkm dev`,
1176
+ * `gkm test`), Dokploy and AWS — and a scheme one of them derived differently
1177
+ * would be an app whose sign-in callback the auth server refuses on exactly one
1178
+ * of them.
1179
+ */
1180
+ /**
1181
+ * An app's scheme: the one given, or the project's name made a valid scheme.
1182
+ *
1183
+ * One for every stage. A development build and the store build register the
1184
+ * same scheme, so the auth server of whichever stage a build talks to trusts
1185
+ * it, and nothing about the app differs between stages but its URLs.
1186
+ *
1187
+ * A scheme is a letter followed by letters, digits, `+`, `-` or `.` — so
1188
+ * `Corner Shop` becomes `corner-shop`, and a name that starts with a digit gets a
1189
+ * leading `app`.
1190
+ */
1191
+ function schemeBase(project, given) {
1192
+ const raw = (given ?? project).toLowerCase().replace(/[^a-z0-9+.-]+/g, "-").replace(/^-+|-+$/g, "");
1193
+ if (!raw) return "app";
1194
+ return /^[a-z]/.test(raw) ? raw : `app${raw}`;
1195
+ }
1196
+ /**
1197
+ * The origins a surface trusts for a mobile caller.
1198
+ *
1199
+ * Better Auth's Expo plugin sends the app's scheme as its origin
1200
+ * (`shop://`), and a link can carry a path after it (`shop://*`).
1201
+ * In development, Expo Go sends `exp://<host>:<port>` instead — so a local
1202
+ * stage adds the hosts a device actually reaches Metro on, and Metro's port
1203
+ * when the target placed it: a whole subnet is not trusted because one phone
1204
+ * is on it.
1205
+ */
1206
+ function mobileOrigins(scheme, metro) {
1207
+ return [
1208
+ `${scheme}://`,
1209
+ `${scheme}://*`,
1210
+ ...(metro?.hosts ?? []).flatMap((host) => [`exp://${host}:${metro?.port ?? "*"}`, `exp://${host}:${metro?.port ?? "*"}/**`])
1211
+ ];
1212
+ }
1213
+ /** Whether an origin is a web one — what a cookie domain is derived from. */
1214
+ function isWebOrigin(origin) {
1215
+ return origin.startsWith("http://") || origin.startsWith("https://");
1216
+ }
1217
+ //#endregion
1218
+ //#region src/secret.ts
1219
+ /**
1220
+ * What a generated secret looks like, and the one way every stage draws it.
1221
+ *
1222
+ * A server deploy draws from `crypto`, `gkm dev` and `gkm test` from a hash of
1223
+ * the project, stage and id, and an AWS deploy asks Pulumi for the same
1224
+ * alphabet. Sharing this is what keeps a password that passes a third party's
1225
+ * rules on one stage from failing them on the next.
1226
+ */
1227
+ /** 43 characters from 64 is 256 bits — as much as anything here needs. */
1228
+ const SECRET_LENGTH = 43;
1229
+ /**
1230
+ * Two symbols nothing has to escape — not a URL, a connection string, a shell
1231
+ * or a `.env` file — which with letters and digits is base64url's alphabet.
1232
+ */
1233
+ const SECRET_SYMBOLS = "-_";
1234
+ const LETTERS_AND_DIGITS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
1235
+ /**
1236
+ * A secret in `format`, each character chosen by `pick(n)` — an integer in
1237
+ * `[0, n)` from whatever source the caller trusts.
1238
+ *
1239
+ * When there are symbols, one is guaranteed: a rule that demands a symbol is
1240
+ * the reason anyone names them, and leaving it to chance fails it often —
1241
+ * about one draw in seven at 24 characters.
1242
+ */
1243
+ function drawSecret(pick, format = {}) {
1244
+ const length = format.length ?? 43;
1245
+ const symbols = [...new Set(format.symbols ?? "-_")].join("");
1246
+ const alphabet = LETTERS_AND_DIGITS + symbols;
1247
+ const chars = Array.from({ length }, () => alphabet[pick(alphabet.length)]);
1248
+ if (symbols && !chars.some((char) => symbols.includes(char))) chars[pick(length)] = symbols[pick(symbols.length)];
1249
+ return chars.join("");
988
1250
  }
989
1251
  //#endregion
1252
+ exports.CLIENT_IP_HEADER = CLIENT_IP_HEADER;
1253
+ exports.DATABASE_FOLDERS = DATABASE_FOLDERS;
1254
+ exports.DEFAULT_DERIVED_DAYS = DEFAULT_DERIVED_DAYS;
990
1255
  exports.DEFAULT_POSTGRES_VERSION = DEFAULT_POSTGRES_VERSION;
1256
+ exports.DEFAULT_STAGE_URL = DEFAULT_STAGE_URL;
1257
+ exports.DERIVED_PREFIX = DERIVED_PREFIX;
991
1258
  exports.DERIVES_FROM = DERIVES_FROM;
992
1259
  exports.IllegalDerivation = IllegalDerivation;
993
1260
  exports.InvalidConstructId = InvalidConstructId;
1261
+ exports.KMS_SCHEME = KMS_SCHEME;
1262
+ exports.MIGRATIONS_ROOT = MIGRATIONS_ROOT;
1263
+ exports.NoUrlForStage = NoUrlForStage;
994
1264
  exports.PUBLIC = PUBLIC;
995
1265
  exports.PUBLIC_PREFIX = PUBLIC_PREFIX;
1266
+ exports.SECRET_LENGTH = SECRET_LENGTH;
1267
+ exports.SECRET_SYMBOLS = SECRET_SYMBOLS;
1268
+ exports.TELEMETRY_KEYS = TELEMETRY_KEYS;
996
1269
  exports.UnknownParent = UnknownParent;
997
1270
  exports.assertDerivations = assertDerivations;
1271
+ exports.bucketExpiry = bucketExpiry;
998
1272
  exports.cacheTable = cacheTable;
999
1273
  exports.canonicalId = canonicalId;
1000
1274
  exports.cloudName = cloudName;
1001
1275
  exports.cookieDomain = cookieDomain;
1276
+ exports.databaseFolder = databaseFolder;
1002
1277
  exports.dependenciesOf = dependenciesOf;
1003
1278
  exports.dependentsOf = dependentsOf;
1279
+ exports.drawSecret = drawSecret;
1004
1280
  exports.environmentCase = environmentCase;
1005
- exports.flattenManifestField = flattenManifestField;
1281
+ exports.externalApiUrl = externalApiUrl;
1006
1282
  exports.isDerived = isDerived;
1283
+ exports.isWebOrigin = isWebOrigin;
1007
1284
  exports.kebabCase = kebabCase;
1285
+ exports.kmsUrl = kmsUrl;
1286
+ exports.migrationFolder = migrationFolder;
1287
+ exports.migrationTargets = migrationTargets;
1288
+ exports.mobileOrigins = mobileOrigins;
1008
1289
  exports.provideKey = provideKey;
1009
1290
  exports.providedKeyFor = providedKeyFor;
1010
1291
  exports.provisionOrder = provisionOrder;
1011
1292
  exports.publicEnvFor = publicEnvFor;
1293
+ exports.schemeBase = schemeBase;
1012
1294
  exports.scopedName = scopedName;
1295
+ exports.seedFolder = seedFolder;
1013
1296
  exports.serviceKey = serviceKey;
1014
1297
 
1015
1298
  //# sourceMappingURL=index.cjs.map