flaghoist 0.2.0 → 0.3.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.
@@ -3,6 +3,7 @@ import { parse } from "smol-toml";
3
3
  var DEFAULT_CONFIG = {
4
4
  name: "team-flags",
5
5
  storage: "cloudflare-kv",
6
+ platform: "cloudflare",
6
7
  auth: { admin: "bearer-token", read: "api-key" },
7
8
  dashboard: true
8
9
  };
@@ -12,21 +13,32 @@ var STORAGE_KINDS = [
12
13
  "postgres",
13
14
  "memory"
14
15
  ];
16
+ var PLATFORM_KINDS = ["cloudflare", "container"];
17
+ function containerStorageDefault(storage) {
18
+ return storage === "cloudflare-kv" ? "postgres" : storage;
19
+ }
20
+ function asContainer(config) {
21
+ return { ...config, platform: "container", storage: containerStorageDefault(config.storage) };
22
+ }
15
23
  function parseConfig(text) {
16
24
  const raw = parse(text);
17
25
  const name = typeof raw.name === "string" && raw.name ? raw.name : DEFAULT_CONFIG.name;
18
26
  const storage = STORAGE_KINDS.includes(raw.storage) ? raw.storage : DEFAULT_CONFIG.storage;
27
+ const platform = raw.platform === "container" ? "container" : DEFAULT_CONFIG.platform;
19
28
  const authRaw = typeof raw.auth === "object" && raw.auth !== null ? raw.auth : {};
20
29
  const admin = authRaw.admin === "oidc" ? "oidc" : "bearer-token";
21
30
  const allowedOrigins = Array.isArray(raw.allowedOrigins) ? raw.allowedOrigins.filter((x) => typeof x === "string") : void 0;
22
31
  const dashboard = raw.dashboard !== false;
23
- return { name, storage, auth: { admin, read: "api-key" }, allowedOrigins, dashboard };
32
+ return { name, storage, platform, auth: { admin, read: "api-key" }, allowedOrigins, dashboard };
24
33
  }
25
34
  function serializeConfig(config) {
26
35
  const lines = [
27
36
  `name = ${JSON.stringify(config.name)}`,
28
37
  `storage = ${JSON.stringify(config.storage)}`
29
38
  ];
39
+ if (config.platform !== "cloudflare") {
40
+ lines.push(`platform = ${JSON.stringify(config.platform)}`);
41
+ }
30
42
  if (config.allowedOrigins && config.allowedOrigins.length > 0) {
31
43
  lines.push(`allowedOrigins = ${JSON.stringify(config.allowedOrigins)}`);
32
44
  }
@@ -46,6 +58,9 @@ function serializeConfig(config) {
46
58
  export {
47
59
  DEFAULT_CONFIG,
48
60
  STORAGE_KINDS,
61
+ PLATFORM_KINDS,
62
+ containerStorageDefault,
63
+ asContainer,
49
64
  parseConfig,
50
65
  serializeConfig
51
66
  };
package/dist/index.js CHANGED
@@ -1,10 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  DEFAULT_CONFIG,
4
+ PLATFORM_KINDS,
4
5
  STORAGE_KINDS,
6
+ asContainer,
7
+ containerStorageDefault,
5
8
  parseConfig,
6
9
  serializeConfig
7
- } from "./chunk-S4XXPMMW.js";
10
+ } from "./chunk-HOSNPNBP.js";
8
11
 
9
12
  // src/index.ts
10
13
  import { spawnSync } from "child_process";
@@ -88,6 +91,133 @@ async function setRules(client, key, rules) {
88
91
  });
89
92
  }
90
93
 
94
+ // src/generate-container.ts
95
+ var CONTAINER_DEPS = {
96
+ "@flaghoist/adapter-memory": "^0.1.2",
97
+ "@flaghoist/adapter-postgres": "^0.1.2",
98
+ "@flaghoist/adapter-redis": "^0.1.2",
99
+ "@flaghoist/server": "^0.3.0",
100
+ "@hono/node-server": "^2.1.1",
101
+ ioredis: "^5.4.0",
102
+ pg: "^8.13.0"
103
+ };
104
+ var CONTAINER_PORT = 8080;
105
+ function adminExpr(admin) {
106
+ return admin === "oidc" ? "oidc({ issuer: env.OIDC_ISSUER, audience: env.OIDC_AUDIENCE, groupsClaim: 'cognito:groups', allowedGroups: (env.ADMIN_GROUPS ?? '').split(',') })" : "bearerToken(env.ADMIN_TOKEN)";
107
+ }
108
+ function generateNodeEntry(config) {
109
+ const defaultStorage = containerStorageDefault(config.storage);
110
+ const serverImports = [
111
+ "apiKey",
112
+ "createFlagServer",
113
+ "memoryRateLimit",
114
+ config.auth.admin === "oidc" ? "oidc" : "bearerToken"
115
+ ].sort();
116
+ const imports = [
117
+ `import { memoryAdapter } from '@flaghoist/adapter-memory'`,
118
+ `import { initPostgres, postgresAdapter } from '@flaghoist/adapter-postgres'`,
119
+ `import { redisAdapter } from '@flaghoist/adapter-redis'`,
120
+ `import { ${serverImports.join(", ")} } from '@flaghoist/server'`,
121
+ ...config.dashboard ? [`import { dashboardHtml } from '@flaghoist/server/dashboard'`] : [],
122
+ `import { serve } from '@hono/node-server'`
123
+ ].join("\n");
124
+ const dashboard = config.dashboard ? "\n dashboard: dashboardHtml," : "";
125
+ const origins = config.allowedOrigins && config.allowedOrigins.length > 0 ? JSON.stringify(config.allowedOrigins) : "undefined";
126
+ return `${imports}
127
+
128
+ // One image, any host. Everything is driven by environment variables, so the same container runs on
129
+ // a plain VPS, Fly.io, Railway, DigitalOcean, Cloud Run, ECS, or Kubernetes, with no code to edit.
130
+ const env = process.env
131
+ const kind = env.FLAGS_STORAGE ?? '${defaultStorage}'
132
+
133
+ async function makeStorage() {
134
+ if (kind === 'postgres') {
135
+ if (!env.DATABASE_URL) throw new Error('FLAGS_STORAGE=postgres requires DATABASE_URL')
136
+ const { Pool } = await import('pg')
137
+ const table = env.FLAGS_TABLE ?? 'flaghoist_flags'
138
+ const pool = new Pool({
139
+ connectionString: env.DATABASE_URL,
140
+ // Managed Postgres (Supabase, Neon, Render) requires TLS. Skip this if your URL sets sslmode.
141
+ ssl: /sslmode=/.test(env.DATABASE_URL) ? undefined : { rejectUnauthorized: false },
142
+ })
143
+ await initPostgres(pool, table)
144
+ return postgresAdapter(pool, { table })
145
+ }
146
+
147
+ if (kind === 'redis') {
148
+ if (!env.REDIS_URL) throw new Error('FLAGS_STORAGE=redis requires REDIS_URL')
149
+ const { default: Redis } = await import('ioredis')
150
+ return redisAdapter(new Redis(env.REDIS_URL), { hashKey: env.FLAGS_HASH_KEY ?? 'flaghoist:flags' })
151
+ }
152
+
153
+ if (kind === 'memory') {
154
+ console.warn('[flaghoist] FLAGS_STORAGE=memory: flags are in-process and are lost on restart.')
155
+ return memoryAdapter()
156
+ }
157
+
158
+ throw new Error(\`Unknown FLAGS_STORAGE "\${kind}". Use postgres, redis, or memory.\`)
159
+ }
160
+
161
+ const app = createFlagServer({
162
+ storage: await makeStorage(),${dashboard}
163
+ auth: {
164
+ admin: ${adminExpr(config.auth.admin)},
165
+ read: apiKey(env.READ_API_KEY),
166
+ },
167
+ rateLimit: memoryRateLimit(),
168
+ // Comma-separated list of browser origins allowed to read flags cross-origin.
169
+ allowedOrigins: env.FLAGS_CORS ? env.FLAGS_CORS.split(',').map((o) => o.trim()) : ${origins},
170
+ })
171
+
172
+ const port = Number(env.PORT ?? ${CONTAINER_PORT})
173
+ serve({ fetch: app.fetch, port, hostname: '0.0.0.0' })
174
+ console.log(\`[flaghoist] listening on :\${port} (storage: \${kind})\`)
175
+ `;
176
+ }
177
+ function generateDockerfile(config) {
178
+ const defaultStorage = containerStorageDefault(config.storage);
179
+ return `# Flaghoist as a portable container. Build once, run on any host: a VPS, Fly.io, Railway,
180
+ # DigitalOcean, Cloud Run, ECS, or Kubernetes. Configure it entirely with environment variables.
181
+ FROM node:22-alpine
182
+
183
+ WORKDIR /app
184
+
185
+ # Install the published Flaghoist packages. Copying only package.json first keeps this layer cached
186
+ # across code changes.
187
+ COPY package.json ./
188
+ RUN npm install --omit=dev --no-audit --no-fund
189
+
190
+ COPY server.mjs ./
191
+
192
+ ENV PORT=${CONTAINER_PORT}
193
+ ENV FLAGS_STORAGE=${defaultStorage}
194
+ EXPOSE ${CONTAINER_PORT}
195
+
196
+ # The /health route is unauthenticated, so it makes a good container healthcheck.
197
+ HEALTHCHECK --interval=30s --timeout=3s --start-period=10s \\
198
+ CMD node -e "fetch('http://127.0.0.1:${CONTAINER_PORT}/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
199
+
200
+ CMD ["node", "server.mjs"]
201
+ `;
202
+ }
203
+ function generateContainerPackageJson(config) {
204
+ const pkg = {
205
+ name: config.name,
206
+ version: "0.0.0",
207
+ private: true,
208
+ type: "module",
209
+ scripts: { start: "node server.mjs" },
210
+ dependencies: CONTAINER_DEPS
211
+ };
212
+ return `${JSON.stringify(pkg, null, 2)}
213
+ `;
214
+ }
215
+ function generateDockerignore() {
216
+ return ["node_modules", "npm-debug.log", "Dockerfile", ".dockerignore", "README.md", ""].join(
217
+ "\n"
218
+ );
219
+ }
220
+
91
221
  // src/generate.ts
92
222
  function storageSnippet(storage) {
93
223
  switch (storage) {
@@ -125,7 +255,7 @@ function storageSnippet(storage) {
125
255
  };
126
256
  }
127
257
  }
128
- function adminExpr(admin) {
258
+ function adminExpr2(admin) {
129
259
  return admin === "oidc" ? "oidc({ issuer: env.OIDC_ISSUER, audience: env.OIDC_AUDIENCE, groupsClaim: 'cognito:groups', allowedGroups: (env.ADMIN_GROUPS ?? '').split(',') })" : "bearerToken(env.ADMIN_TOKEN)";
130
260
  }
131
261
  function generateWorkerEntry(config) {
@@ -149,7 +279,7 @@ function generateWorkerEntry(config) {
149
279
  export default createFlagServer((env) => ({
150
280
  storage: ${storage.expr},
151
281
  auth: {
152
- admin: ${adminExpr(config.auth.admin)},
282
+ admin: ${adminExpr2(config.auth.admin)},
153
283
  read: apiKey(env.READ_API_KEY),
154
284
  },${origins}${dashboard}
155
285
  }))
@@ -317,27 +447,49 @@ function runInit(args) {
317
447
  const { values } = parseArgs({
318
448
  args,
319
449
  allowPositionals: true,
320
- options: { name: { type: "string" }, storage: { type: "string" } }
450
+ options: {
451
+ name: { type: "string" },
452
+ storage: { type: "string" },
453
+ platform: { type: "string" }
454
+ }
321
455
  });
322
456
  if (existsSync("flaghoist.toml")) throw new Error("flaghoist.toml already exists.");
323
457
  if (values.storage && !STORAGE_KINDS.includes(values.storage)) {
324
458
  throw new Error(`Unknown storage "${values.storage}". One of: ${STORAGE_KINDS.join(", ")}.`);
325
459
  }
326
- const config = {
460
+ if (values.platform && !PLATFORM_KINDS.includes(values.platform)) {
461
+ throw new Error(`Unknown platform "${values.platform}". One of: ${PLATFORM_KINDS.join(", ")}.`);
462
+ }
463
+ const base = {
327
464
  ...DEFAULT_CONFIG,
328
465
  name: values.name ?? DEFAULT_CONFIG.name,
329
466
  storage: values.storage ?? DEFAULT_CONFIG.storage
330
467
  };
468
+ const config = values.platform === "container" ? asContainer(base) : base;
331
469
  writeFileSync("flaghoist.toml", serializeConfig(config));
332
470
  console.log("Created flaghoist.toml");
333
471
  console.log("Next: `flaghoist deploy` to ship it, or `flaghoist eject` to own the code.");
334
472
  }
335
- function writeProject(config, dir) {
336
- const files = [
473
+ function entryFile(platform) {
474
+ return platform === "container" ? "server.mjs" : "src/index.ts";
475
+ }
476
+ function projectFiles(config) {
477
+ if (config.platform === "container") {
478
+ return [
479
+ ["server.mjs", generateNodeEntry(config)],
480
+ ["Dockerfile", generateDockerfile(config)],
481
+ [".dockerignore", generateDockerignore()],
482
+ ["package.json", generateContainerPackageJson(config)]
483
+ ];
484
+ }
485
+ return [
337
486
  ["src/index.ts", generateWorkerEntry(config)],
338
487
  ["wrangler.toml", generateWranglerToml(config)],
339
488
  ["package.json", generatePackageJson(config)]
340
489
  ];
490
+ }
491
+ function writeProject(config, dir) {
492
+ const files = projectFiles(config);
341
493
  const existing = files.map(([name]) => name).filter((name) => existsSync(join(dir, name)));
342
494
  if (existing.length > 0) {
343
495
  throw new Error(
@@ -350,8 +502,15 @@ Flaghoist deploys as its own service, so give it a directory of its own:
350
502
  }
351
503
  function runEject() {
352
504
  const config = loadConfig();
353
- if (existsSync("src/index.ts")) throw new Error("src/index.ts already exists. Already ejected?");
505
+ const entry = entryFile(config.platform);
506
+ if (existsSync(entry)) throw new Error(`${entry} already exists. Already ejected?`);
354
507
  writeProject(config, ".");
508
+ if (config.platform === "container") {
509
+ console.log("Ejected to a code project you own: server.mjs, Dockerfile, package.json");
510
+ console.log("Edit server.mjs freely, then build and run the container, or deploy to a host:");
511
+ console.log(" https://docs.flaghoist.dev/deploy/overview/");
512
+ return;
513
+ }
355
514
  console.log("Ejected to a code project you own: src/index.ts, wrangler.toml, package.json");
356
515
  if (config.storage === "cloudflare-kv") {
357
516
  console.log("Create your KV namespace and paste the id into wrangler.toml:");
@@ -412,30 +571,30 @@ async function promptDeployTarget() {
412
571
  rl.close();
413
572
  }
414
573
  }
415
- function printOtherTargets() {
574
+ function deployContainer(config) {
575
+ const container = asContainer(config);
576
+ if (config.platform !== container.platform || config.storage !== container.storage) {
577
+ writeFileSync("flaghoist.toml", serializeConfig(container));
578
+ }
579
+ if (!existsSync(entryFile("container"))) writeProject(container, ".");
416
580
  console.log(`
417
- Flaghoist runs on any Node, Bun, Deno, or container host, not just Cloudflare.
581
+ Scaffolded a container project: server.mjs, Dockerfile, package.json.
582
+
583
+ It runs on any container or Node host, configured by environment variables (FLAGS_STORAGE,
584
+ DATABASE_URL, ADMIN_TOKEN, READ_API_KEY). Build and run it with Docker:
418
585
 
419
- The project scaffolded here is a Cloudflare Worker. To run it elsewhere you serve the same
420
- createFlagServer() app on Node and point it at Postgres or Redis instead of Workers KV.
586
+ docker build -t ${container.name} .
587
+ docker run -p 8080:8080 -e ADMIN_TOKEN=... -e READ_API_KEY=... ${container.name}
421
588
 
422
- Guides:
589
+ Or deploy it to a host:
423
590
  Render https://docs.flaghoist.dev/deploy/render/
591
+ Fly.io https://docs.flaghoist.dev/deploy/fly/
592
+ Railway https://docs.flaghoist.dev/deploy/railway/
424
593
  All targets https://docs.flaghoist.dev/deploy/overview/
425
594
 
426
- More platforms are on the way. Missing one you need? Open an issue at
427
- https://github.com/flaghoist/flaghoist/issues.`);
595
+ Missing a platform you need? Open an issue at https://github.com/flaghoist/flaghoist/issues.`);
428
596
  }
429
- async function runDeploy(args) {
430
- let target = parseDeployTarget(args);
431
- if (target === null) {
432
- target = process.stdin.isTTY ? await promptDeployTarget() : "cloudflare";
433
- }
434
- if (target === "other") {
435
- printOtherTargets();
436
- return;
437
- }
438
- const config = loadConfig();
597
+ async function deployCloudflare(config) {
439
598
  if (!existsSync("src/index.ts")) writeProject(config, ".");
440
599
  ensureDependencies();
441
600
  ensureKvNamespace(config);
@@ -443,14 +602,24 @@ async function runDeploy(args) {
443
602
  const result = spawnSync("npx", ["wrangler", "deploy"], { stdio: "inherit" });
444
603
  process.exit(result.status ?? 0);
445
604
  }
605
+ async function runDeploy(args) {
606
+ const config = loadConfig();
607
+ let target = parseDeployTarget(args);
608
+ if (target === null) {
609
+ if (config.platform === "container") target = "other";
610
+ else target = process.stdin.isTTY ? await promptDeployTarget() : "cloudflare";
611
+ }
612
+ if (target === "other") return deployContainer(config);
613
+ return deployCloudflare(config);
614
+ }
446
615
  function printHelp() {
447
616
  console.log(`flaghoist ${VERSION} \u2014 hoist your own feature flags
448
617
 
449
618
  Usage: flaghoist <command>
450
619
 
451
620
  Scaffolding
452
- init [--name N] [--storage cloudflare-kv|redis|postgres|memory]
453
- eject Generate a code project you own
621
+ init [--name N] [--storage cloudflare-kv|redis|postgres|memory] [--platform cloudflare|container]
622
+ eject Generate a code project you own (a Worker, or a container)
454
623
  deploy [--target T] Deploy (prompts for the platform; T is cloudflare or other)
455
624
 
456
625
  Flag management (needs --url/--token or FLAGS_URL/FLAGS_ADMIN_TOKEN)
package/dist/lib.d.ts CHANGED
@@ -1,8 +1,17 @@
1
1
  type StorageKind = 'cloudflare-kv' | 'redis' | 'postgres' | 'memory';
2
2
  type AdminAuthKind = 'bearer-token' | 'oidc';
3
+ /**
4
+ * The shape of project `flaghoist deploy`/`eject` scaffolds. `cloudflare` is a Worker plus a
5
+ * `wrangler.toml`; `container` is a Node entry plus a `Dockerfile`, which runs on any container or
6
+ * Node host (Render, Fly, Railway, a VPS). Cloudflare is the default, so a config written before
7
+ * this key existed still scaffolds a Worker.
8
+ */
9
+ type PlatformKind = 'cloudflare' | 'container';
3
10
  interface FlaghoistConfig {
4
11
  name: string;
5
12
  storage: StorageKind;
13
+ /** Deploy shape to scaffold. Defaults to `cloudflare`. */
14
+ platform: PlatformKind;
6
15
  auth: {
7
16
  admin: AdminAuthKind;
8
17
  read: 'api-key';
@@ -14,9 +23,27 @@ interface FlaghoistConfig {
14
23
  declare const DEFAULT_CONFIG: FlaghoistConfig;
15
24
  /** Every storage backend selectable by name in `flaghoist.toml`. */
16
25
  declare const STORAGE_KINDS: readonly StorageKind[];
26
+ /** Every deploy shape selectable by name in `flaghoist.toml`. */
27
+ declare const PLATFORM_KINDS: readonly PlatformKind[];
28
+ /** The stores a container can reach. Cloudflare KV is a Worker binding, so it is not one of them. */
29
+ type ContainerStorage = 'postgres' | 'redis' | 'memory';
30
+ /**
31
+ * The storage a container project uses. Cloudflare KV cannot be reached off Workers, so a config
32
+ * that still names it (the scaffolding default) becomes postgres, the store every container deploy
33
+ * guide uses. This is the one rule for a container's storage: the CLI reuses it to write a coherent
34
+ * `flaghoist.toml`, and the generated entry bakes it as the `FLAGS_STORAGE` default. Every kind
35
+ * stays overridable at runtime via the env var.
36
+ */
37
+ declare function containerStorageDefault(storage: StorageKind): ContainerStorage;
38
+ /**
39
+ * Recast a config for the container platform, running its storage through the container rule so the
40
+ * result never names a store the platform cannot use. This is the one place the platform switch
41
+ * rewrites a config, so `init`, `create-flaghoist`, `deploy` and `eject` all stay consistent.
42
+ */
43
+ declare function asContainer(config: FlaghoistConfig): FlaghoistConfig;
17
44
  /** Parse a `flaghoist.toml` into a validated config, falling back to defaults for unknown values. */
18
45
  declare function parseConfig(text: string): FlaghoistConfig;
19
46
  /** Render a config back to `flaghoist.toml` text. */
20
47
  declare function serializeConfig(config: FlaghoistConfig): string;
21
48
 
22
- export { type AdminAuthKind, DEFAULT_CONFIG, type FlaghoistConfig, STORAGE_KINDS, type StorageKind, parseConfig, serializeConfig };
49
+ export { type AdminAuthKind, type ContainerStorage, DEFAULT_CONFIG, type FlaghoistConfig, PLATFORM_KINDS, type PlatformKind, STORAGE_KINDS, type StorageKind, asContainer, containerStorageDefault, parseConfig, serializeConfig };
package/dist/lib.js CHANGED
@@ -1,12 +1,18 @@
1
1
  import {
2
2
  DEFAULT_CONFIG,
3
+ PLATFORM_KINDS,
3
4
  STORAGE_KINDS,
5
+ asContainer,
6
+ containerStorageDefault,
4
7
  parseConfig,
5
8
  serializeConfig
6
- } from "./chunk-S4XXPMMW.js";
9
+ } from "./chunk-HOSNPNBP.js";
7
10
  export {
8
11
  DEFAULT_CONFIG,
12
+ PLATFORM_KINDS,
9
13
  STORAGE_KINDS,
14
+ asContainer,
15
+ containerStorageDefault,
10
16
  parseConfig,
11
17
  serializeConfig
12
18
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flaghoist",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Scaffold, deploy, and manage your Flaghoist feature-flag service from the terminal.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -26,9 +26,9 @@
26
26
  "smol-toml": "^1.8.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@flaghoist/core": "0.1.2",
30
29
  "@flaghoist/adapter-memory": "0.1.2",
31
- "@flaghoist/server": "0.3.0"
30
+ "@flaghoist/core": "0.1.2",
31
+ "@flaghoist/server": "0.3.1"
32
32
  },
33
33
  "publishConfig": {
34
34
  "access": "public"