@sixb/app 0.1.7 → 0.1.8

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/README.md CHANGED
@@ -8,6 +8,26 @@ Reusable toolkit for project-specific Sixb apps. It owns custom app route scanni
8
8
  bun add @sixb/app
9
9
  ```
10
10
 
11
+ ## Public environment variables
12
+
13
+ Set browser-safe values with the `SIXB_PUBLIC_` prefix:
14
+
15
+ ```dotenv
16
+ SIXB_PUBLIC_GOOGLE_MAPS_API_KEY=your-browser-key
17
+ ```
18
+
19
+ ```ts
20
+ import { publicEnv } from "@sixb/app"
21
+
22
+ const key = publicEnv.SIXB_PUBLIC_GOOGLE_MAPS_API_KEY // string | undefined
23
+ ```
24
+
25
+ Values are strings; missing keys return `undefined`. Only prefixed variables are available.
26
+ Outside the browser, `publicEnv` is empty. With `sixb dev` and `sixb app`, restart the app server
27
+ after changing values; no rebuild is required.
28
+
29
+ Public values are visible to every user. Never use `SIXB_PUBLIC_` for backend secrets.
30
+
11
31
  ## How It Works
12
32
 
13
33
  The build pipeline has three stages: **scan**, **codegen**, and **build**.
@@ -0,0 +1,3 @@
1
+ export type { AppMetadata } from "./metadata";
2
+ export { publicEnv } from "./public-env";
3
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA"}
@@ -0,0 +1,9 @@
1
+ // @bun
2
+ import {
3
+ publicEnv2
4
+ } from "./chunk-e1gj8c1a.js";
5
+ export {
6
+ publicEnv2 as publicEnv
7
+ };
8
+
9
+ //# debugId=A40A02AAC9805E5664756E2164756E21
@@ -0,0 +1,9 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [
5
+ ],
6
+ "mappings": "",
7
+ "debugId": "A40A02AAC9805E5664756E2164756E21",
8
+ "names": []
9
+ }
@@ -0,0 +1,8 @@
1
+ // @bun
2
+ // src/public-env.ts
3
+ var runtime = typeof window === "undefined" ? undefined : window.__SIXB_RUNTIME__;
4
+ var publicEnv2 = Object.freeze(Object.assign(Object.create(null), runtime?.publicEnv));
5
+
6
+ export { publicEnv2 };
7
+
8
+ //# debugId=62C3FDDD48F648C464756E2164756E21
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/public-env.ts"],
4
+ "sourcesContent": [
5
+ "const runtime =\n typeof window === \"undefined\"\n ? undefined\n : (window as Window & { __SIXB_RUNTIME__?: { publicEnv?: Record<string, string> } })\n .__SIXB_RUNTIME__\n\n/** Public environment captured at app-server startup. Missing values are undefined. */\nexport const publicEnv: Readonly<Record<string, string | undefined>> = Object.freeze(\n Object.assign(Object.create(null), runtime?.publicEnv)\n)\n"
6
+ ],
7
+ "mappings": ";;AAAA,IAAM,UACJ,OAAO,WAAW,cACd,YACC,OACE;AAGF,IAAM,aAA0D,OAAO,OAC5E,OAAO,OAAO,OAAO,OAAO,IAAI,GAAG,SAAS,SAAS,CACvD;",
8
+ "debugId": "62C3FDDD48F648C464756E2164756E21",
9
+ "names": []
10
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"createCustomApp.d.ts","sourceRoot":"","sources":["../src/createCustomApp.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EACL,KAAK,cAAc,EAQpB,MAAM,SAAS,CAAA;AAShB,OAAO,EAAE,KAAK,SAAS,EAAkC,MAAM,WAAW,CAAA;AAI1E,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,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AAE,MAAM,CAAA;IACnB,QAAQ,CAAC,EAAE,mBAAmB,CAAA;IAC9B,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB;AAED,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACtB;AAED,MAAM,WAAW,iBAAiB;IAChC,UAAU,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,CAAA;IAClC,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;IAC7B,qBAAqB,CACnB,OAAO,CAAC,EAAE,gCAAgC,GACzC,OAAO,CAAC,+BAA+B,GAAG,IAAI,CAAC,CAAA;IAClD,GAAG,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAA;IAC/D,KAAK,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;IAC/D,KAAK,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAA;CACpE;AAYD,wBAAsB,eAAe,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAkjBjG"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { type CreateCustomAppOptions, type CustomAppBuildOptions, type CustomAppDevOptions, type CustomAppDevServer, type CustomAppInstance, type CustomAppStartOptions, type CustomAuthExperienceBuildOptions, type CustomAuthExperienceBuildResult, createCustomApp, } from "./createCustomApp";
2
2
  export { devReloadResponse } from "./dev-reload";
3
3
  export type { AppMetadata } from "./metadata";
4
+ export { publicEnv } from "./public-env";
4
5
  export { createTailwindCssCompiler, type TailwindCssCompiler, type TailwindCssCompilerOptions, } from "./tailwind";
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACtB,KAAK,qBAAqB,EAC1B,KAAK,gCAAgC,EACrC,KAAK,+BAA+B,EACpC,eAAe,GAChB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAChD,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EACL,yBAAyB,EACzB,KAAK,mBAAmB,EACxB,KAAK,0BAA0B,GAChC,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACtB,KAAK,qBAAqB,EAC1B,KAAK,gCAAgC,EACrC,KAAK,+BAA+B,EACpC,eAAe,GAChB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAChD,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EACL,yBAAyB,EACzB,KAAK,mBAAmB,EACxB,KAAK,0BAA0B,GAChC,MAAM,YAAY,CAAA"}
package/dist/index.js CHANGED
@@ -1,4 +1,8 @@
1
1
  // @bun
2
+ import {
3
+ publicEnv2
4
+ } from "./chunk-e1gj8c1a.js";
5
+
2
6
  // src/createCustomApp.ts
3
7
  import { randomBytes } from "crypto";
4
8
  import { watch } from "fs";
@@ -427,6 +431,9 @@ async function fileExists(path) {
427
431
  }
428
432
 
429
433
  // src/runtime.ts
434
+ function collectPublicEnv() {
435
+ return Object.fromEntries(Object.entries(process.env).filter((entry) => entry[0].startsWith("SIXB_PUBLIC_") && entry[1] !== undefined));
436
+ }
430
437
  function renderCustomAppRuntimeScript(config) {
431
438
  const safeConfig = JSON.stringify(config).replaceAll("<", "\\u003c");
432
439
  return `<script>window.__SIXB_RUNTIME__ = ${safeConfig};</script>`;
@@ -2135,6 +2142,8 @@ async function createCustomApp(options) {
2135
2142
  return await prepareAuthExperience(options);
2136
2143
  },
2137
2144
  async dev(devOptions = {}) {
2145
+ const publicEnv = collectPublicEnv();
2146
+ const withRuntime = (html) => injectRuntimeConfig(html, { apiBaseUrl, audience, authEnabled, publicEnv });
2138
2147
  const host = devOptions.host ?? "0.0.0.0";
2139
2148
  const port = devOptions.port ?? 3001;
2140
2149
  const { htmlPath, manifestPath, sharedHtmlPath, sharedMainPath } = await prepareGeneratedApp();
@@ -2165,9 +2174,20 @@ async function createCustomApp(options) {
2165
2174
  "/__sixb/dev-reload.js": getHeadRoute(devReloadResponse),
2166
2175
  [customAppManifestRoute]: manifestRoute(manifestPath),
2167
2176
  [privateAppShellRoute]: htmlBundleRoute(bundle),
2168
- ...sharedAppDevRoutes(async () => (await ensureSharedBuild()).html, apiBaseUrl),
2169
- "/": spaHtmlRoute(() => internalOrigin, { shellRoute: privateAppShellRoute }),
2170
- "/*": spaHtmlRoute(() => internalOrigin, { shellRoute: privateAppShellRoute })
2177
+ ...sharedAppDevRoutes(async () => injectRuntimeConfig((await ensureSharedBuild()).html, {
2178
+ apiBaseUrl,
2179
+ audience: "app",
2180
+ authEnabled: false,
2181
+ publicEnv
2182
+ }), apiBaseUrl),
2183
+ "/": spaHtmlRoute(() => internalOrigin, {
2184
+ shellRoute: privateAppShellRoute,
2185
+ transformHtml: withRuntime
2186
+ }),
2187
+ "/*": spaHtmlRoute(() => internalOrigin, {
2188
+ shellRoute: privateAppShellRoute,
2189
+ transformHtml: withRuntime
2190
+ })
2171
2191
  }
2172
2192
  });
2173
2193
  async function ensureSharedBuild() {
@@ -2291,6 +2311,7 @@ async function createCustomApp(options) {
2291
2311
  return result;
2292
2312
  },
2293
2313
  async start(startOptions = {}) {
2314
+ const publicEnv = collectPublicEnv();
2294
2315
  const host = startOptions.host ?? "0.0.0.0";
2295
2316
  const port = startOptions.port ?? 3001;
2296
2317
  const outdir = resolve3(rootDir, startOptions.outdir ?? join7(".sixb", "dist", "app"));
@@ -2305,12 +2326,14 @@ async function createCustomApp(options) {
2305
2326
  const indexHtml = injectRuntimeConfig(await Bun.file(indexPath).text(), {
2306
2327
  apiBaseUrl: startOptions.apiBaseUrl ?? apiBaseUrl,
2307
2328
  audience: startOptions.audience ?? audience,
2308
- authEnabled: startOptions.authEnabled ?? authEnabled
2329
+ authEnabled: startOptions.authEnabled ?? authEnabled,
2330
+ publicEnv
2309
2331
  });
2310
2332
  const sharedIndexHtml = injectRuntimeConfig(await Bun.file(sharedIndexPath).text(), {
2311
2333
  apiBaseUrl: startOptions.apiBaseUrl ?? apiBaseUrl,
2312
2334
  audience: "app",
2313
- authEnabled: false
2335
+ authEnabled: false,
2336
+ publicEnv
2314
2337
  });
2315
2338
  const runtimeApiBaseUrl = startOptions.apiBaseUrl ?? apiBaseUrl;
2316
2339
  const server = Bun.serve({
@@ -2346,6 +2369,9 @@ async function createCustomApp(options) {
2346
2369
  }
2347
2370
  const directFile = Bun.file(resolvedPath);
2348
2371
  if (await directFile.exists()) {
2372
+ if (resolvedPath === indexPath) {
2373
+ return htmlResponse(req, indexHtml);
2374
+ }
2349
2375
  return staticFileResponse(req, resolvedPath, staticAssetHeaders(url.pathname));
2350
2376
  }
2351
2377
  if (isAssetRequest(url.pathname)) {
@@ -2353,6 +2379,9 @@ async function createCustomApp(options) {
2353
2379
  }
2354
2380
  const htmlFile = Bun.file(`${resolvedPath}.html`);
2355
2381
  if (await htmlFile.exists()) {
2382
+ if (`${resolvedPath}.html` === indexPath) {
2383
+ return htmlResponse(req, indexHtml);
2384
+ }
2356
2385
  return fileResponse(req, htmlFile);
2357
2386
  }
2358
2387
  const nestedIndexFile = Bun.file(join7(resolvedPath, "index.html"));
@@ -2563,20 +2592,23 @@ function resolveStaticPath(appRoot, pathname) {
2563
2592
  return resolvedPath;
2564
2593
  }
2565
2594
  function injectRuntimeConfig(html, config) {
2566
- if (!config.apiBaseUrl) {
2567
- return html;
2568
- }
2595
+ const existingRuntimeScript = /<script>window\.__SIXB_RUNTIME__ = (.*?);<\/script>/;
2596
+ const existingConfig = html.match(existingRuntimeScript)?.[1];
2569
2597
  const script = renderCustomAppRuntimeScript({
2570
- api: { baseUrl: config.apiBaseUrl },
2571
- auth: { audience: config.audience, enabled: config.authEnabled }
2598
+ auth: { audience: config.audience, enabled: config.authEnabled },
2599
+ ...existingConfig ? JSON.parse(existingConfig) : {},
2600
+ ...config.apiBaseUrl ? {
2601
+ api: { baseUrl: config.apiBaseUrl },
2602
+ auth: { audience: config.audience, enabled: config.authEnabled }
2603
+ } : {},
2604
+ publicEnv: config.publicEnv
2572
2605
  });
2573
- const existingRuntimeScript = /<script>window\.__SIXB_RUNTIME__ = .*?;<\/script>/;
2574
2606
  if (existingRuntimeScript.test(html)) {
2575
- return html.replace(existingRuntimeScript, script);
2607
+ return html.replace(existingRuntimeScript, () => script);
2576
2608
  }
2577
- if (html.includes("</head>")) {
2578
- return html.replace("</head>", ` ${script}
2579
- </head>`);
2609
+ if (/<head\b[^>]*>/i.test(html)) {
2610
+ return html.replace(/<head\b[^>]*>/i, (head) => `${head}
2611
+ ${script}`);
2580
2612
  }
2581
2613
  return `${script}
2582
2614
  ${html}`;
@@ -2785,7 +2817,7 @@ function spaHtmlRoute(internalOrigin, options = {}) {
2785
2817
  });
2786
2818
  }
2787
2819
  const html = restoreAppStaticUrls(await bundleResponse.text()).replace("</head>", '<script src="/__sixb/dev-reload.js"></script></head>');
2788
- return new Response(html, {
2820
+ return new Response(options.transformHtml?.(html) ?? html, {
2789
2821
  status: bundleResponse.status,
2790
2822
  statusText: bundleResponse.statusText,
2791
2823
  headers: responseHeaders
@@ -2795,7 +2827,8 @@ function spaHtmlRoute(internalOrigin, options = {}) {
2795
2827
  export {
2796
2828
  createCustomApp,
2797
2829
  createTailwindCssCompiler,
2798
- devReloadResponse
2830
+ devReloadResponse,
2831
+ publicEnv2 as publicEnv
2799
2832
  };
2800
2833
 
2801
- //# debugId=A302C1A40E576F2664756E2164756E21
2834
+ //# debugId=6856E0633EA3EB7F64756E2164756E21