@sixb/app 0.1.1 → 0.1.3
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 +21 -0
- package/dist/agents.d.ts +6 -1
- package/dist/agents.d.ts.map +1 -1
- package/dist/agents.js +16 -5
- package/dist/agents.js.map +3 -3
- package/dist/auth.d.ts +22 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +3 -0
- package/dist/auth.js.map +9 -0
- package/dist/build.d.ts +13 -2
- package/dist/build.d.ts.map +1 -1
- package/dist/codegen.d.ts +10 -0
- package/dist/codegen.d.ts.map +1 -1
- package/dist/createCustomApp.d.ts +7 -0
- package/dist/createCustomApp.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +404 -55
- package/dist/index.js.map +5 -5
- package/package.json +10 -4
- package/src/agents.ts +28 -5
- package/src/auth.ts +17 -0
- package/src/build.ts +218 -40
- package/src/codegen.ts +153 -0
- package/src/createCustomApp.ts +162 -12
- package/src/index.ts +2 -0
package/src/createCustomApp.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { watch } from "node:fs"
|
|
2
|
-
import { access, cp, mkdir, readFile, writeFile } from "node:fs/promises"
|
|
2
|
+
import { access, cp, mkdir, readFile, rm, writeFile } from "node:fs/promises"
|
|
3
3
|
import { dirname, isAbsolute, join, normalize, relative, resolve } from "node:path"
|
|
4
4
|
import { pathToFileURL } from "node:url"
|
|
5
5
|
import type { AuthSessionAudience } from "@sixb/core"
|
|
6
6
|
import {
|
|
7
7
|
type BuildAppResult,
|
|
8
8
|
buildApp,
|
|
9
|
+
buildAuthExperience,
|
|
9
10
|
prepareAppHtmlBundleEntry,
|
|
10
11
|
restoreAppStaticUrls,
|
|
11
12
|
} from "./build"
|
|
12
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
type BuiltInRouteManifestEntry,
|
|
15
|
+
generateAppEntry,
|
|
16
|
+
generateAuthExperienceEntry,
|
|
17
|
+
generateRouteManifest,
|
|
18
|
+
} from "./codegen"
|
|
13
19
|
import { renderCustomAppRuntimeScript } from "./runtime"
|
|
14
20
|
import { type PageRoute, scanPages } from "./scanner"
|
|
15
21
|
import { type CustomAppStylesheet, resolveCustomAppStylesheet } from "./styles"
|
|
@@ -35,6 +41,14 @@ export interface CustomAppBuildOptions {
|
|
|
35
41
|
outdir?: string
|
|
36
42
|
}
|
|
37
43
|
|
|
44
|
+
export interface CustomAuthExperienceBuildOptions {
|
|
45
|
+
outdir?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface CustomAuthExperienceBuildResult {
|
|
49
|
+
readonly outdir: string
|
|
50
|
+
}
|
|
51
|
+
|
|
38
52
|
export interface CustomAppStartOptions {
|
|
39
53
|
host?: string
|
|
40
54
|
port?: number
|
|
@@ -54,6 +68,9 @@ export interface CustomAppDevServer {
|
|
|
54
68
|
export interface CustomAppInstance {
|
|
55
69
|
scanRoutes(): Promise<PageRoute[]>
|
|
56
70
|
hasRoutes(): Promise<boolean>
|
|
71
|
+
prepareAuthExperience(
|
|
72
|
+
options?: CustomAuthExperienceBuildOptions
|
|
73
|
+
): Promise<CustomAuthExperienceBuildResult | null>
|
|
57
74
|
dev(options?: CustomAppDevOptions): Promise<CustomAppDevServer>
|
|
58
75
|
build(options?: CustomAppBuildOptions): Promise<BuildAppResult>
|
|
59
76
|
start(options?: CustomAppStartOptions): Promise<CustomAppDevServer>
|
|
@@ -90,6 +107,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
90
107
|
|
|
91
108
|
let tailwindCompiler: TailwindCssCompiler | null = null
|
|
92
109
|
let tailwindCompilerKey: string | null = null
|
|
110
|
+
let authTailwindCompiler: TailwindCssCompiler | null = null
|
|
93
111
|
let builtInAgentCssCompiler: TailwindCssCompiler | null = null
|
|
94
112
|
|
|
95
113
|
// `app/globals.css` is source. When it uses Tailwind, compile it to
|
|
@@ -214,6 +232,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
214
232
|
|
|
215
233
|
async function prepareGeneratedApp(): Promise<{
|
|
216
234
|
htmlPath: string
|
|
235
|
+
mainPath: string
|
|
217
236
|
manifestPath: string
|
|
218
237
|
routes: PageRoute[]
|
|
219
238
|
}> {
|
|
@@ -225,7 +244,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
225
244
|
const builtInRoutes = agentRoutesEnabled ? builtInAgentRoutesFor(routes) : []
|
|
226
245
|
const stylesheets = await prepareStylesheets(builtInRoutes)
|
|
227
246
|
await generateRouteManifest(routes, generatedDir, { builtInRoutes })
|
|
228
|
-
const { htmlPath, manifestPath } = await generateAppEntry(rootDir, generatedDir, {
|
|
247
|
+
const { htmlPath, mainPath, manifestPath } = await generateAppEntry(rootDir, generatedDir, {
|
|
229
248
|
apiBaseUrl,
|
|
230
249
|
audience,
|
|
231
250
|
authEnabled,
|
|
@@ -234,7 +253,59 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
234
253
|
frameworkStylesheetPaths: stylesheets.frameworkStylesheetPaths,
|
|
235
254
|
stylesheetPath: stylesheets.stylesheetPath,
|
|
236
255
|
})
|
|
237
|
-
return { htmlPath, manifestPath, routes }
|
|
256
|
+
return { htmlPath, mainPath, manifestPath, routes }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function prepareAuthExperience(
|
|
260
|
+
authOptions: CustomAuthExperienceBuildOptions = {}
|
|
261
|
+
): Promise<CustomAuthExperienceBuildResult | null> {
|
|
262
|
+
const outdir = authOptions.outdir
|
|
263
|
+
? resolve(rootDir, authOptions.outdir)
|
|
264
|
+
: join(generatedDir, "auth")
|
|
265
|
+
if (!(await pathExists(join(appDir, "auth.tsx")))) {
|
|
266
|
+
await rm(outdir, { recursive: true, force: true })
|
|
267
|
+
return null
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const stylesheet = await resolveCustomAppStylesheet({ appDir, generatedDir, rootDir })
|
|
271
|
+
let stylesheetPath: string | null
|
|
272
|
+
if (stylesheet.kind === "none") {
|
|
273
|
+
stylesheetPath = null
|
|
274
|
+
} else if (stylesheet.kind === "static") {
|
|
275
|
+
stylesheetPath = stylesheet.path
|
|
276
|
+
} else {
|
|
277
|
+
const outputPath = join(generatedDir, "auth.css")
|
|
278
|
+
authTailwindCompiler ??= createTailwindCssCompiler({
|
|
279
|
+
inputPath: stylesheet.sourcePath,
|
|
280
|
+
outputPath,
|
|
281
|
+
cwd: appDir,
|
|
282
|
+
resolveFrom: rootDir,
|
|
283
|
+
label: "[SixbCustomApp]",
|
|
284
|
+
})
|
|
285
|
+
await authTailwindCompiler.compile()
|
|
286
|
+
stylesheetPath = outputPath
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const entry = await generateAuthExperienceEntry(rootDir, generatedDir, {
|
|
290
|
+
appDir,
|
|
291
|
+
publicDir,
|
|
292
|
+
stylesheetPath,
|
|
293
|
+
})
|
|
294
|
+
if (!entry) {
|
|
295
|
+
return null
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const result = await buildAuthExperience({
|
|
299
|
+
entryPath: entry.htmlPath,
|
|
300
|
+
scriptEntryPath: entry.mainPath,
|
|
301
|
+
outdir,
|
|
302
|
+
})
|
|
303
|
+
if (!result.success) {
|
|
304
|
+
throw new Error(
|
|
305
|
+
`[SixbCustomApp] Failed to build the auth experience: ${(result.logs ?? []).join("\n")}`
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
return { outdir: result.outdir }
|
|
238
309
|
}
|
|
239
310
|
|
|
240
311
|
return {
|
|
@@ -247,10 +318,15 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
247
318
|
return routes.length > 0
|
|
248
319
|
},
|
|
249
320
|
|
|
321
|
+
async prepareAuthExperience(options) {
|
|
322
|
+
return await prepareAuthExperience(options)
|
|
323
|
+
},
|
|
324
|
+
|
|
250
325
|
async dev(devOptions: CustomAppDevOptions = {}) {
|
|
251
326
|
const host = devOptions.host ?? "0.0.0.0"
|
|
252
327
|
const port = devOptions.port ?? 3001
|
|
253
328
|
const { htmlPath, manifestPath } = await prepareGeneratedApp()
|
|
329
|
+
await prepareAuthExperience()
|
|
254
330
|
let htmlImportVersion = 0
|
|
255
331
|
let htmlBundle = await importHtmlBundle(htmlPath, htmlImportVersion)
|
|
256
332
|
const publicRoutes = (await pathExists(publicDir)) ? await createPublicRoutes(publicDir) : {}
|
|
@@ -281,6 +357,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
281
357
|
rebuildChain = rebuildChain
|
|
282
358
|
.then(async () => {
|
|
283
359
|
const { htmlPath: nextHtmlPath } = await prepareGeneratedApp()
|
|
360
|
+
await prepareAuthExperience()
|
|
284
361
|
htmlImportVersion++
|
|
285
362
|
htmlBundle = await importHtmlBundle(nextHtmlPath, htmlImportVersion)
|
|
286
363
|
server.reload(serveOptions(htmlBundle))
|
|
@@ -330,6 +407,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
330
407
|
watcher.close()
|
|
331
408
|
await rebuildChain
|
|
332
409
|
await tailwindCompiler?.stop()
|
|
410
|
+
await authTailwindCompiler?.stop()
|
|
333
411
|
await builtInAgentCssCompiler?.stop()
|
|
334
412
|
server.stop(true)
|
|
335
413
|
},
|
|
@@ -338,18 +416,29 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
338
416
|
|
|
339
417
|
async build(buildOptions: CustomAppBuildOptions = {}) {
|
|
340
418
|
const outdir = resolve(rootDir, buildOptions.outdir ?? join(".sixb", "dist", "app"))
|
|
341
|
-
const { htmlPath, manifestPath } = await prepareGeneratedApp()
|
|
419
|
+
const { htmlPath, mainPath, manifestPath } = await prepareGeneratedApp()
|
|
342
420
|
const result = await buildApp({
|
|
343
421
|
entryPath: htmlPath,
|
|
422
|
+
scriptEntryPath: mainPath,
|
|
344
423
|
manifestPath,
|
|
345
424
|
outdir,
|
|
346
425
|
})
|
|
347
426
|
|
|
427
|
+
if (result.success) {
|
|
428
|
+
await prepareAuthExperience({ outdir: join(outdir, "auth") })
|
|
429
|
+
}
|
|
430
|
+
|
|
348
431
|
if (result.success && (await pathExists(publicDir))) {
|
|
349
432
|
await cp(publicDir, outdir, {
|
|
350
433
|
recursive: true,
|
|
351
434
|
force: true,
|
|
352
|
-
filter: (source) =>
|
|
435
|
+
filter: (source) => {
|
|
436
|
+
const resolvedSource = resolve(source)
|
|
437
|
+
return (
|
|
438
|
+
resolvedSource !== join(publicDir, "app.webmanifest") &&
|
|
439
|
+
resolvedSource !== join(publicDir, "auth")
|
|
440
|
+
)
|
|
441
|
+
},
|
|
353
442
|
})
|
|
354
443
|
}
|
|
355
444
|
|
|
@@ -396,7 +485,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
396
485
|
|
|
397
486
|
const directFile = Bun.file(resolvedPath)
|
|
398
487
|
if (await directFile.exists()) {
|
|
399
|
-
return
|
|
488
|
+
return staticFileResponse(req, resolvedPath, staticAssetHeaders(url.pathname))
|
|
400
489
|
}
|
|
401
490
|
|
|
402
491
|
if (isAssetRequest(url.pathname)) {
|
|
@@ -633,11 +722,11 @@ function htmlResponse(request: Request, html: string): Response {
|
|
|
633
722
|
})
|
|
634
723
|
}
|
|
635
724
|
|
|
636
|
-
//
|
|
637
|
-
//
|
|
638
|
-
//
|
|
639
|
-
|
|
640
|
-
|
|
725
|
+
// buildApp emits content-hashed entry, chunk, and asset names. Their contents can never change
|
|
726
|
+
// under the same URL, so they are safe to cache forever. Files copied from `public/` keep their
|
|
727
|
+
// names across deploys and stay uncached.
|
|
728
|
+
const IMMUTABLE_ASSET_PATTERN =
|
|
729
|
+
/^(?:(?:app|chunk)-[a-z0-9]+|(?:chunk|asset)-.+-[a-z0-9]+)\.[a-z0-9]+(?:\.map)?$/
|
|
641
730
|
|
|
642
731
|
function staticAssetHeaders(pathname: string): Record<string, string> {
|
|
643
732
|
if (pathname === customAppManifestRoute) {
|
|
@@ -655,6 +744,67 @@ function staticAssetHeaders(pathname: string): Record<string, string> {
|
|
|
655
744
|
return { "cache-control": "public, max-age=31536000, immutable" }
|
|
656
745
|
}
|
|
657
746
|
|
|
747
|
+
async function staticFileResponse(
|
|
748
|
+
request: Request,
|
|
749
|
+
path: string,
|
|
750
|
+
headers: Record<string, string>
|
|
751
|
+
): Promise<Response> {
|
|
752
|
+
const responseHeaders = new Headers(headers)
|
|
753
|
+
const originalFile = Bun.file(path)
|
|
754
|
+
let responseFile = originalFile
|
|
755
|
+
|
|
756
|
+
if (isPrecompressedAsset(path)) {
|
|
757
|
+
responseHeaders.append("vary", "Accept-Encoding")
|
|
758
|
+
if (!request.headers.has("range")) {
|
|
759
|
+
for (const encoding of acceptedPrecompressedEncodings(request)) {
|
|
760
|
+
const candidate = Bun.file(`${path}.${encoding === "br" ? "br" : "gz"}`)
|
|
761
|
+
if (!(await candidate.exists())) continue
|
|
762
|
+
|
|
763
|
+
responseFile = candidate
|
|
764
|
+
responseHeaders.set("content-encoding", encoding)
|
|
765
|
+
responseHeaders.set("content-type", originalFile.type)
|
|
766
|
+
break
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
return new Response(request.method === "HEAD" ? null : responseFile, {
|
|
772
|
+
headers: responseHeaders,
|
|
773
|
+
})
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
function isPrecompressedAsset(path: string): boolean {
|
|
777
|
+
return path.endsWith(".js") || path.endsWith(".css")
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function acceptedPrecompressedEncodings(request: Request): ("br" | "gzip")[] {
|
|
781
|
+
const header = request.headers.get("accept-encoding")
|
|
782
|
+
if (!header) return []
|
|
783
|
+
|
|
784
|
+
const qualities = new Map<string, number>()
|
|
785
|
+
for (const item of header.split(",")) {
|
|
786
|
+
const [rawName, ...parameters] = item.trim().split(";")
|
|
787
|
+
const name = rawName.toLowerCase()
|
|
788
|
+
let quality = 1
|
|
789
|
+
for (const parameter of parameters) {
|
|
790
|
+
const match = parameter.trim().match(/^q=(0(?:\.\d+)?|1(?:\.0+)?)$/)
|
|
791
|
+
if (match) quality = Number(match[1])
|
|
792
|
+
}
|
|
793
|
+
qualities.set(name, quality)
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
const wildcard = qualities.get("*") ?? 0
|
|
797
|
+
return (["br", "gzip"] as const)
|
|
798
|
+
.map((encoding, preference) => ({
|
|
799
|
+
encoding,
|
|
800
|
+
preference,
|
|
801
|
+
quality: qualities.get(encoding) ?? wildcard,
|
|
802
|
+
}))
|
|
803
|
+
.filter((candidate) => candidate.quality > 0)
|
|
804
|
+
.sort((left, right) => right.quality - left.quality || left.preference - right.preference)
|
|
805
|
+
.map((candidate) => candidate.encoding)
|
|
806
|
+
}
|
|
807
|
+
|
|
658
808
|
function fileResponse(
|
|
659
809
|
request: Request,
|
|
660
810
|
file: Bun.BunFile,
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,8 @@ export {
|
|
|
5
5
|
type CustomAppDevServer,
|
|
6
6
|
type CustomAppInstance,
|
|
7
7
|
type CustomAppStartOptions,
|
|
8
|
+
type CustomAuthExperienceBuildOptions,
|
|
9
|
+
type CustomAuthExperienceBuildResult,
|
|
8
10
|
createCustomApp,
|
|
9
11
|
} from "./createCustomApp"
|
|
10
12
|
export type { AppMetadata } from "./metadata"
|