@torpor/build 0.1.7 → 0.1.9
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/CHANGELOG.md +14 -0
- package/dist/{Site-Df1k0TuQ.d.cts → Server-B45PD5Ys.d.cts} +2 -27
- package/dist/{Site-Df1k0TuQ.d.ts → Server-B45PD5Ys.d.ts} +2 -27
- package/dist/Site-PxPCXGZ-.d.cts +29 -0
- package/dist/Site-Qpcmx0Xq.d.ts +29 -0
- package/dist/bin/index.cjs +21 -5
- package/dist/bin/index.cjs.map +1 -1
- package/dist/bin/index.js +3 -2
- package/dist/bin/index.js.map +1 -1
- package/dist/chunk-65Z6BF2N.js +355 -0
- package/dist/chunk-65Z6BF2N.js.map +1 -0
- package/dist/chunk-T5K52YVJ.js +12 -0
- package/dist/chunk-T5K52YVJ.js.map +1 -0
- package/dist/{chunk-GQ4JCTRP.js → chunk-ZSWD4UBH.js} +8 -170
- package/dist/chunk-ZSWD4UBH.js.map +1 -0
- package/dist/index.cjs +104 -142
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -3
- package/dist/index.d.ts +49 -3
- package/dist/index.js +129 -3
- package/dist/index.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/dist/metafile-esm.json +1 -1
- package/dist/run.cjs +21 -5
- package/dist/run.cjs.map +1 -1
- package/dist/run.d.cts +2 -1
- package/dist/run.d.ts +2 -1
- package/dist/run.js +3 -2
- package/dist/server.cjs +387 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +17 -0
- package/dist/server.d.ts +17 -0
- package/dist/server.js +16 -0
- package/dist/server.js.map +1 -0
- package/package.json +9 -14
- package/src/index.ts +2 -2
- package/src/{site → server}/Routerx.ts +4 -4
- package/src/server/Server.ts +22 -2
- package/src/server/connect/{createMiddlewareHandler.ts → connectMiddleware.ts} +2 -2
- package/src/{types → server/types}/MiddlewareFunction.ts +1 -1
- package/src/server/{ServerFunction.ts → types/ServerFunction.ts} +1 -1
- package/src/server.ts +7 -0
- package/src/site/Router.ts +35 -12
- package/src/site/manifest.ts +2 -2
- package/src/site/runDev.ts +2 -2
- package/src/types/ManifestRoute.ts +0 -1
- package/tsup.config.ts +8 -1
- package/dist/chunk-GQ4JCTRP.js.map +0 -1
- package/dist/chunk-L3QBUPXY.js +0 -176
- package/dist/chunk-L3QBUPXY.js.map +0 -1
- package/src/types/RouteHandlerCollection.ts +0 -9
- /package/src/{types → server/types}/HttpMethod.ts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/site/runBuild.ts","../src/site/manifest.ts","../src/site/runDev.ts","../src/site/prepareTemplate.ts","../src/site/runPreview.ts","../src/server/contentType.ts","../src/site/run.ts"],"sourcesContent":["import torpor from \"@torpor/unplugin/vite\";\nimport { existsSync, promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { build, defineConfig } from \"vite\";\nimport Site from \"./Site.ts\";\nimport manifest from \"./manifest.ts\";\n\n// TODO: Don't cache index.html in dev?\n// TODO: Multiple hook.server locations\n// TODO: Don't reload layouts during client routing\n// TODO: Call the correct +page and +server routes when in the same folder\n\nexport default async function runBuild(site: Site) {\n\t// Delete the dist folder if it exists\n\tconst distFolder = path.resolve(site.root, \"dist\");\n\tif (existsSync(distFolder)) {\n\t\tawait fs.rm(distFolder, { recursive: true });\n\t}\n\tconst clientFolder = path.join(distFolder, \"client\");\n\tconst serverFolder = path.join(distFolder, \"server\");\n\n\t// HOOK: Prebuild\n\tif (site.adapter.prebuild) {\n\t\tawait site.adapter.prebuild(site);\n\t}\n\n\t// TODO: From a setting\n\tlet siteHtml = path.resolve(site.root, \"src/site.html\");\n\n\t// TODO: Is this going to be the correct path after installing from npm?\n\tconst siteFolder = path.resolve(site.root, \"./node_modules/@torpor/build/src/site/\");\n\tlet clientScript = path.join(siteFolder, \"clientEntry.ts\");\n\tlet serverScript = path.join(siteFolder, \"serverEntry.ts\");\n\n\t// Build the client assets, including site.html and the route files\n\t// EXCLUDING anything with `server.js` in the name\n\tawait build(\n\t\tdefineConfig({\n\t\t\tplugins: [\n\t\t\t\tmanifest(site),\n\t\t\t\t// @ts-ignore\n\t\t\t\ttorpor(),\n\t\t\t\t...site.plugins,\n\t\t\t],\n\t\t\tbuild: {\n\t\t\t\toutDir: clientFolder,\n\t\t\t\trollupOptions: {\n\t\t\t\t\tinput: [\n\t\t\t\t\t\tsiteHtml,\n\t\t\t\t\t\tclientScript,\n\t\t\t\t\t\t...site.routes\n\t\t\t\t\t\t\t.filter((r) => !/server\\.(ts|js)$/.test(r.file))\n\t\t\t\t\t\t\t.map((r) => path.resolve(site.root, r.file)),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\tssrManifest: true,\n\t\t\t},\n\t\t}),\n\t);\n\n\t// Build the server assets, including the server entry script and the route\n\t// files\n\tawait build(\n\t\tdefineConfig({\n\t\t\tplugins: [\n\t\t\t\tmanifest(site, true),\n\t\t\t\t// @ts-ignore\n\t\t\t\ttorpor(),\n\t\t\t\t...site.plugins,\n\t\t\t],\n\t\t\tbuild: {\n\t\t\t\toutDir: serverFolder,\n\t\t\t\trollupOptions: {\n\t\t\t\t\tinput: [serverScript, ...site.routes.map((r) => path.resolve(site.root, r.file))],\n\t\t\t\t},\n\t\t\t\tssr: serverScript,\n\t\t\t},\n\t\t}),\n\t);\n\n\t// Move the site.html file into /client\n\t// HACK: Should do this in Rollup if possible?\n\tsiteHtml = path.join(clientFolder, \"site.html\");\n\tawait fs.rename(path.join(clientFolder, \"src\", \"site.html\"), siteHtml);\n\tawait fs.rm(path.join(clientFolder, \"src\"), { recursive: true });\n\n\t// HOOK: Postbuild\n\tif (site.adapter.postbuild) {\n\t\tawait site.adapter.postbuild(site);\n\t}\n}\n","import { readFileSync } from \"node:fs\";\nimport path from \"node:path\";\nimport { Plugin } from \"vite\";\nimport Site from \"./Site\";\n\nconst moduleId = \"@torpor/build/manifest\";\n\n/**\n * A Vite plugin that provides information about the app to both clientEntry and\n * serverEntry.\n */\nexport default function manifest(site: Site, server = false): Plugin {\n\treturn {\n\t\tname: \"torpor-manifest\",\n\t\tresolveId: {\n\t\t\torder: \"pre\",\n\t\t\thandler(id) {\n\t\t\t\tif (id === moduleId) {\n\t\t\t\t\treturn id;\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\t// @ts-ignore\n\t\tload(id, viteOptions) {\n\t\t\tif (id === moduleId) {\n\t\t\t\tlet serverRequest = server && !!viteOptions?.ssr;\n\t\t\t\treturn `\n${!serverRequest ? \"const load = { default: { load: true } };\" : \"\"}\nexport default {\n routes: [\n ${site.routes\n\t\t\t.map((r) => {\n\t\t\t\tif (serverRequest || !/server\\.(ts|js)$/.test(r.file)) {\n\t\t\t\t\treturn `{ path: \"${r.path}\", type: ${r.type}, endPoint: ${serverRequest || !/server\\.(ts|js)$/.test(r.file) ? `() => import(/* @vite-ignore */ \"${path.join(site.root, r.file)}\")` : \"undefined\"} }`;\n\t\t\t\t} else {\n\t\t\t\t\t// On the client, for a server route, we need to check\n\t\t\t\t\t// whether there's a load function and set a dummy endPoint\n\t\t\t\t\t// if so. The correct endpoint will be hit in clientEntry\n\t\t\t\t\t// HACK: Should do this better than checking for `load:` in source...\n\t\t\t\t\tconst haveLoad = readFileSync(path.join(site.root, r.file)).includes(\"load:\");\n\t\t\t\t\treturn `{ path: \"${r.path}\", type: ${r.type}, endPoint: ${haveLoad ? \"() => load\" : \"undefined\"} }`;\n\t\t\t\t}\n\t\t\t})\n\t\t\t.join(\",\\n \")}\n ],\n};\n`;\n\t\t\t}\n\t\t},\n\t};\n}\n","import torpor from \"@torpor/unplugin/vite\";\nimport { configDotenv } from \"dotenv\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { createServer as createViteServer } from \"vite\";\nimport { serverError } from \"../response.ts\";\nimport Server from \"../server/Server.ts\";\nimport connectMiddleware from \"../server/connect/connectMiddleware.ts\";\nimport Site from \"./Site.ts\";\nimport manifest from \"./manifest.ts\";\nimport prepareTemplate from \"./prepareTemplate.ts\";\n\nexport default async function runDev(site: Site) {\n\tconst server = new Server();\n\n\t// Create the Vite server in middleware mode and configure the app type as\n\t// \"custom\", disabling Vite's own HTML serving logic so the parent server\n\t// can take control\n\tconst vite = await createViteServer({\n\t\tserver: { middlewareMode: true },\n\t\tappType: \"custom\",\n\t\tplugins: [\n\t\t\tmanifest(site, true),\n\t\t\t// @ts-ignore not sure...\n\t\t\ttorpor(),\n\t\t\t...site.plugins,\n\t\t],\n\t});\n\n\t// Read site.html\n\t// It's called site.html because @torpor/build builds the site (html, routes\n\t// etc) while the user builds the app (components etc)\n\tconst templateFile = path.resolve(site.root, \"src/site.html\");\n\tlet template = await fs.readFile(templateFile, \"utf-8\");\n\n\t// Apply Vite HTML transforms. This injects the Vite HMR client, and also\n\t// applies HTML transforms from Vite plugins. Note that we only support a\n\t// universal transform, not individual transforms for each route\n\ttemplate = await vite.transformIndexHtml(\"\", template);\n\n\t// TODO: Is this going to be the correct path after installing from npm?\n\tconst siteFolder = path.resolve(site.root, \"./node_modules/@torpor/build/src/site/\");\n\tlet clientScript = path.join(siteFolder, \"clientEntry.ts\");\n\tlet serverScript = path.join(siteFolder, \"serverEntry.ts\");\n\n\t// Prepare site.html so that we can just splice components into it\n\ttemplate = prepareTemplate(template, clientScript);\n\n\t// Use vite's Connect instance as middleware. We need to wrap it with\n\t// createMiddlewareHandler that converts Connect middleware to\n\t// Request/Response web handlers\n\tserver.use(connectMiddleware(vite.middlewares));\n\n\t// Every request (GET, POST, etc) goes through loadEndPoint\n\tserver.add(\"*\", async (ev) => {\n\t\ttry {\n\t\t\t// Load the server entry. ssrLoadModule automatically transforms ESM\n\t\t\t// source code to be usable in Node.js. No bundling is required, and\n\t\t\t// it provides efficient invalidation similar to HMR\n\t\t\tconst { load } = await vite.ssrLoadModule(serverScript);\n\n\t\t\t// Render the app HTML (or fetch server data etc) via serverEntry's\n\t\t\t// exported `load` function\n\t\t\treturn await load(ev, template);\n\t\t} catch (e: any) {\n\t\t\t// If an error is caught, let Vite fix the stack trace so it maps\n\t\t\t// back to your actual source code\n\t\t\tvite.ssrFixStacktrace(e);\n\t\t\treturn serverError(e);\n\t\t}\n\t});\n\n\t// Load environment variables from a `.env` file, with defaults if not set\n\tconfigDotenv();\n\n\tprocess.env.PROTOCOL ??= \"http:\";\n\tprocess.env.HOST ??= \"localhost\";\n\tprocess.env.PORT ??= \"7059\";\n\n\t// Serve the site\n\tsite.adapter.serve(server, site);\n}\n","export default function prepareTemplate(template: string, clientScript: string) {\n\tlet contentStart = regexIndexOf(template, /\\<div\\s+id=(\"app\"|'app'|app)\\s+/);\n\tcontentStart = template.indexOf(\">\", contentStart) + 1;\n\tlet contentEnd = template.indexOf(\"</div>\", contentStart);\n\tif (contentStart === -1 || contentEnd === -1) {\n\t\tthrow new Error(`Couldn't find <div id=\"app\"></div>`);\n\t}\n\treturn (\n\t\ttemplate.substring(0, contentStart) +\n\t\t\"%COMPONENT_HTML%\" +\n\t\ttemplate.substring(contentEnd) +\n\t\t`<script type=\"module\" src=\"${clientScript}\"></script>`\n\t);\n}\n\n// From https://stackoverflow.com/a/274094\nfunction regexIndexOf(string: string, regex: RegExp, position?: number) {\n\tvar indexOf = string.substring(position || 0).search(regex);\n\treturn indexOf >= 0 ? indexOf + (position || 0) : indexOf;\n}\n","import { configDotenv } from \"dotenv\";\nimport { existsSync, promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { serverError } from \"../response.ts\";\nimport Server from \"../server/Server.ts\";\nimport contentType from \"../server/contentType.ts\";\nimport Site from \"./Site\";\nimport prepareTemplate from \"./prepareTemplate.ts\";\nimport runBuild from \"./runBuild\";\n\nexport default async function runPreview(site: Site) {\n\t// Build the site\n\tawait runBuild(site);\n\n\tconst distFolder = path.resolve(site.root, \"dist\");\n\tconst clientFolder = path.join(distFolder, \"client\");\n\tconst serverFolder = path.join(distFolder, \"server\");\n\n\t// Find the client script file in /assets\n\tlet clientScript = (await fs.readdir(path.join(clientFolder, \"assets\"))).find((f) =>\n\t\tf.startsWith(\"clientEntry-\"),\n\t);\n\tif (!clientScript) {\n\t\tthrow new Error(\"clientEntry.js not found\");\n\t}\n\tclientScript = `/assets/${clientScript}`;\n\n\t// Read site.html\n\t// It's called site.html because @torpor/build builds the site (html, routes\n\t// etc) while the user builds the app (components etc)\n\tlet siteHtml = path.join(clientFolder, \"site.html\");\n\tlet template = await fs.readFile(siteHtml, \"utf-8\");\n\n\t// Prepare site.html so that we can just splice components into it\n\ttemplate = prepareTemplate(template, clientScript);\n\n\t// Configure the server\n\tconst server = new Server();\n\n\t// Serve dist/client/assets statically\n\tserver.add(\"/assets/*\", async (ev) => {\n\t\ttry {\n\t\t\tconst url = new URL(ev.request.url);\n\t\t\tconst file = path.join(site.root, \"dist\", \"client\", url.pathname);\n\t\t\tif (existsSync(file)) {\n\t\t\t\t// TODO: Stream the data?\n\t\t\t\treturn new Response(await fs.readFile(file), {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\": contentType(path.extname(file)),\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (e: any) {\n\t\t\treturn serverError(e);\n\t\t}\n\t});\n\n\t// Every request (GET, POST, etc) goes through loadEndPoint\n\tconst serverScript = path.resolve(serverFolder, \"serverEntry.js\");\n\tserver.add(\"*\", async (ev) => {\n\t\ttry {\n\t\t\t// Load the server entry\n\t\t\tconst { load } = await import(serverScript);\n\n\t\t\t// Render the app HTML (or fetch server data etc) via serverEntry's\n\t\t\t// exported `load` function\n\t\t\treturn await load(ev, template);\n\t\t} catch (e: any) {\n\t\t\treturn serverError(e);\n\t\t}\n\t});\n\n\t// Load environment variables from a `.env` file, with defaults if not set\n\tconfigDotenv();\n\n\tprocess.env.PROTOCOL ??= \"http:\";\n\tprocess.env.HOST ??= \"localhost\";\n\tprocess.env.PORT ??= \"7059\";\n\n\t// Serve the site\n\tsite.adapter.serve(server, site);\n}\n","// From https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types\n\nexport default function contentType(extension: string) {\n\treturn types[extension] || \"application/octet-stream\";\n}\n\nconst types: Record<string, string> = {\n\t\".aac\": \"audio/aac\",\n\t\".abw\": \"application/x-abiword\",\n\t\".apng\": \"image/apng\",\n\t\".arc\": \"application/x-freearc\",\n\t\".avif\": \"image/avif\",\n\t\".avi\": \"video/x-msvideo\",\n\t\".azw\": \"application/vnd.amazon.ebook\",\n\t\".bin\": \"application/octet-stream\",\n\t\".bmp\": \"image/bmp\",\n\t\".bz\": \"application/x-bzip\",\n\t\".bz2\": \"application/x-bzip2\",\n\t\".cda\": \"application/x-cdf\",\n\t\".csh\": \"application/x-csh\",\n\t\".css\": \"text/css\",\n\t\".csv\": \"text/csv\",\n\t\".doc\": \"application/msword\",\n\t\".docx\": \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",\n\t\".eot\": \"application/vnd.ms-fontobject\",\n\t\".epub\": \"application/epub+zip\",\n\t\".gz\": \"application/gzip\",\n\t\".gif\": \"image/gif\",\n\t\".htm\": \"text/html\",\n\t\".html\": \"text/html\",\n\t\".ico\": \"image/vnd.microsoft.icon\",\n\t\".ics\": \"text/calendar\",\n\t\".jar\": \"application/java-archive\",\n\t\".jpeg\": \"image/jpeg\",\n\t\".jpg\": \"image/jpeg\",\n\t\".js\": \"text/javascript\",\n\t\".json\": \"application/json\",\n\t\".jsonld\": \"application/ld+json\",\n\t\".mid\": \"audio/midi\",\n\t\".midi\": \"audio/midi\",\n\t\".mjs\": \"text/javascript\",\n\t\".mp3\": \"audio/mpeg\",\n\t\".mp4\": \"video/mp4\",\n\t\".mpeg\": \"video/mpeg\",\n\t\".mpkg\": \"application/vnd.apple.installer+xml\",\n\t\".odp\": \"application/vnd.oasis.opendocument.presentation\",\n\t\".ods\": \"application/vnd.oasis.opendocument.spreadsheet\",\n\t\".odt\": \"application/vnd.oasis.opendocument.text\",\n\t\".oga\": \"audio/ogg\",\n\t\".ogv\": \"video/ogg\",\n\t\".ogx\": \"application/ogg\",\n\t\".opus\": \"audio/ogg\",\n\t\".otf\": \"font/otf\",\n\t\".png\": \"image/png\",\n\t\".pdf\": \"application/pdf\",\n\t\".php\": \"application/x-httpd-php\",\n\t\".ppt\": \"application/vnd.ms-powerpoint\",\n\t\".pptx\": \"application/vnd.openxmlformats-officedocument.presentationml.presentation\",\n\t\".rar\": \"application/vnd.rar\",\n\t\".rtf\": \"application/rtf\",\n\t\".sh\": \"application/x-sh\",\n\t\".svg\": \"image/svg+xml\",\n\t\".tar\": \"application/x-tar\",\n\t\".tif\": \"image/tiff\",\n\t\".tiff\": \"image/tiff\",\n\t\".ts\": \"video/mp2t\",\n\t\".ttf\": \"font/ttf\",\n\t\".txt\": \"text/plain\",\n\t\".vsd\": \"application/vnd.visio\",\n\t\".wav\": \"audio/wav\",\n\t\".weba\": \"audio/webm\",\n\t\".webm\": \"video/webm\",\n\t\".webp\": \"image/webp\",\n\t\".woff\": \"font/woff\",\n\t\".woff2\": \"font/woff2\",\n\t\".xhtml\": \"application/xhtml+xml\",\n\t\".xls\": \"application/vnd.ms-excel\",\n\t\".xlsx\": \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\n\t\".xml\": \"application/xml\",\n\t\".xul\": \"application/vnd.mozilla.xul+xml\",\n\t\".zip\": \"application/zip\",\n\t\".3gp\": \"video/3gpp\",\n\t\".3g2\": \"video/3gpp2\",\n\t\".7z\": \"application/x-7z-compressed\",\n};\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { transformWithEsbuild } from \"vite\";\nimport Site from \"./Site\";\nimport runBuild from \"./runBuild\";\nimport runDev from \"./runDev\";\nimport runPreview from \"./runPreview\";\n\nexport default async function run(folder: string, mode: \"dev\" | \"build\" | \"preview\") {\n\t// Look for and load a site.config.js/ts file in the working directory\n\tlet configFile = \"\";\n\tlet deleteConfigFile = false;\n\tconst jsConfigFile = path.join(folder, \"site.config.js\");\n\tconst tsConfigFile = path.join(folder, \"site.config.ts\");\n\tif (fs.existsSync(jsConfigFile)) {\n\t\t// If it's JS, we can just import it straight up\n\t\tconfigFile = jsConfigFile;\n\t} else if (fs.existsSync(tsConfigFile)) {\n\t\t// If it's TS, we need to convert it to JS\n\t\t// TODO: Put the generated file somewhere better (dist?)\n\t\tconfigFile = path.join(folder, \"site.config.temp.js\");\n\t\tlet source = (\n\t\t\tawait transformWithEsbuild(fs.readFileSync(tsConfigFile, \"utf-8\"), tsConfigFile, {\n\t\t\t\tloader: \"ts\",\n\t\t\t})\n\t\t).code;\n\t\tfs.writeFileSync(configFile, source);\n\t\tdeleteConfigFile = true;\n\t} else {\n\t\tthrow new Error(\"site.config file not found\");\n\t}\n\n\tconst site = (await import(configFile)).default as Site;\n\tif (!site || !site.root || !site.routes) {\n\t\tthrow new Error(\"Invalid site in config file\");\n\t}\n\tif (deleteConfigFile) {\n\t\tfs.rmSync(configFile);\n\t}\n\n\tswitch (mode) {\n\t\tcase \"dev\": {\n\t\t\tawait runDev(site);\n\t\t\tbreak;\n\t\t}\n\t\tcase \"build\": {\n\t\t\tawait runBuild(site);\n\t\t\tbreak;\n\t\t}\n\t\tcase \"preview\": {\n\t\t\tawait runPreview(site);\n\t\t\tbreak;\n\t\t}\n\t\tdefault: {\n\t\t\tconsole.log(\"No mode passed, running in dev\");\n\t\t\tawait runDev(site);\n\t\t\tbreak;\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;AAAA,OAAO,YAAY;AACnB,SAAS,YAAY,YAAY,UAAU;AAC3C,OAAOA,WAAU;AACjB,SAAS,OAAO,oBAAoB;;;ACHpC,SAAS,oBAAoB;AAC7B,OAAO,UAAU;AAIjB,IAAM,WAAW;AAMF,SAAR,SAA0B,MAAY,SAAS,OAAe;AACpE,SAAO;AAAA,IACN,MAAM;AAAA,IACN,WAAW;AAAA,MACV,OAAO;AAAA,MACP,QAAQ,IAAI;AACX,YAAI,OAAO,UAAU;AACpB,iBAAO;AAAA,QACR;AAAA,MACD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BC,IAAG,aAAa,cAAc,OAAO,GAAG,cAAc;AAAA,MAChF,QAAQ;AAAA,IACT,CAAC,GACA;AACF,IAAAA,IAAG,cAAc,YAAY,MAAM;AACnC,uBAAmB;AAAA,EACpB,OAAO;AACN,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAC7C;AAEA,QAAM,QAAQ,MAAM,OAAO,aAAa;AACxC,MAAI,CAAC,QAAQ,CAAC,KAAK,QAAQ,CAAC,KAAK,QAAQ;AACxC,UAAM,IAAI,MAAM,6BAA6B;AAAA,EAC9C;AACA,MAAI,kBAAkB;AACrB,IAAAA,IAAG,OAAO,UAAU;AAAA,EACrB;AAEA,UAAQ,MAAM;AAAA,IACb,KAAK,OAAO;AACX,YAAM,OAAO,IAAI;AACjB;AAAA,IACD;AAAA,IACA,KAAK,SAAS;AACb,YAAM,SAAS,IAAI;AACnB;AAAA,IACD;AAAA,IACA,KAAK,WAAW;AACf,YAAM,WAAW,IAAI;AACrB;AAAA,IACD;AAAA,IACA,SAAS;AACR,cAAQ,IAAI,gCAAgC;AAC5C,YAAM,OAAO,IAAI;AACjB;AAAA,IACD;AAAA,EACD;AACD;","names":["path","path","torpor","fs","path","torpor","path","fs","configDotenv","existsSync","fs","path","path","fs","existsSync","configDotenv","fs","path","path","fs"]}
|
package/dist/index.cjs
CHANGED
|
@@ -30,7 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
|
|
33
|
+
Router: () => Router,
|
|
34
34
|
Site: () => Site
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -43,167 +43,129 @@ function pathToRegex(path2) {
|
|
|
43
43
|
return new RegExp(`^${pattern}$`);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
// src/
|
|
47
|
-
var
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
cookies = /* @__PURE__ */ new Map();
|
|
51
|
-
constructor(request) {
|
|
52
|
-
this.request = request;
|
|
46
|
+
// src/site/Router.ts
|
|
47
|
+
var Router = class {
|
|
48
|
+
routes = [];
|
|
49
|
+
constructor() {
|
|
53
50
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
//add(path: string, file: string) {
|
|
52
|
+
// // TODO: Call the specific addX method based on the file path
|
|
53
|
+
// this.addPage(route, file);
|
|
54
|
+
//}
|
|
55
|
+
addPages(routes) {
|
|
56
|
+
for (let r of routes) {
|
|
57
|
+
this.routes.push(
|
|
58
|
+
new Route(r.path, {
|
|
59
|
+
path: r.path,
|
|
60
|
+
type: r.type,
|
|
61
|
+
endPoint: r.endPoint
|
|
62
|
+
})
|
|
63
|
+
);
|
|
57
64
|
}
|
|
58
|
-
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
set(name, value, options) {
|
|
62
|
-
options ??= {};
|
|
63
|
-
options.httpOnly ??= true;
|
|
64
|
-
options.secure ??= true;
|
|
65
|
-
options.sameSite ??= "lax";
|
|
66
|
-
this.cookies.set(name, (0, import_cookie.serialize)(name, value, options));
|
|
65
|
+
this.#sortRoutes();
|
|
66
|
+
return this;
|
|
67
67
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
// TODO: Allow calling with the endpoint itself, so that you can setup an app with no scaffold
|
|
69
|
+
addPage(path2, type, endPoint) {
|
|
70
|
+
this.routes.push(
|
|
71
|
+
new Route(path2, {
|
|
72
|
+
path: path2,
|
|
73
|
+
type,
|
|
74
|
+
endPoint
|
|
75
|
+
})
|
|
76
|
+
);
|
|
77
|
+
this.#sortRoutes();
|
|
78
|
+
return this;
|
|
72
79
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
this.request = request;
|
|
80
|
+
/*
|
|
81
|
+
loadPage(ev: ServerEvent, file: string): Response {
|
|
82
|
+
let page = this.pages.get(file);
|
|
83
|
+
if (!page) {
|
|
84
|
+
page = {};
|
|
85
|
+
this.pages.set(file, page);
|
|
86
|
+
}
|
|
81
87
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
88
|
+
*/
|
|
89
|
+
match(path2, query) {
|
|
90
|
+
for (let route of this.routes) {
|
|
91
|
+
let match = path2.match(route.regex);
|
|
92
|
+
if (match) {
|
|
93
|
+
if (!route.handler.loaded) {
|
|
94
|
+
this.#loadHandler(route.handler, route.path);
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
handler: route.handler,
|
|
98
|
+
params: match.groups,
|
|
99
|
+
query
|
|
100
|
+
};
|
|
101
|
+
}
|
|
85
102
|
}
|
|
86
|
-
|
|
103
|
+
console.log(`not found: '${path2}'`);
|
|
87
104
|
}
|
|
88
|
-
|
|
89
|
-
this
|
|
105
|
+
#loadHandler(handler, path2) {
|
|
106
|
+
handler.layouts = this.#findLayouts(path2);
|
|
107
|
+
handler.serverEndPoint = this.#findServer(path2);
|
|
108
|
+
handler.serverHook = this.#findServerHook(path2);
|
|
90
109
|
}
|
|
91
|
-
|
|
92
|
-
|
|
110
|
+
#findLayouts(path2) {
|
|
111
|
+
let layouts = [];
|
|
112
|
+
let parts = path2.replace(/\/~server$/, "").split("/");
|
|
113
|
+
let basePath = "";
|
|
114
|
+
for (let i = 0; i < parts.length; i++) {
|
|
115
|
+
if (i > 0) {
|
|
116
|
+
basePath += "/" + parts[i];
|
|
117
|
+
}
|
|
118
|
+
const layoutPath = basePath + "/_layout";
|
|
119
|
+
const layoutRoute = this.routes.find((r) => r.path === layoutPath);
|
|
120
|
+
if (layoutRoute) {
|
|
121
|
+
layouts.push({
|
|
122
|
+
path: layoutPath,
|
|
123
|
+
endPoint: layoutRoute.handler.endPoint,
|
|
124
|
+
serverEndPoint: this.#findServer(layoutPath)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return layouts.length ? layouts : void 0;
|
|
93
129
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
request;
|
|
99
|
-
response;
|
|
100
|
-
// TODO: Should we cast types??
|
|
101
|
-
params;
|
|
102
|
-
cookies;
|
|
103
|
-
headers;
|
|
104
|
-
constructor(request, params) {
|
|
105
|
-
this.request = request;
|
|
106
|
-
this.params = params;
|
|
107
|
-
this.cookies = new CookieHelper(request);
|
|
108
|
-
this.headers = new HeaderHelper(request);
|
|
130
|
+
#findServer(path2) {
|
|
131
|
+
const serverPath = path2.replace(/\/$/, "") + "/~server";
|
|
132
|
+
const serverRoute = this.routes.find((r) => r.path === serverPath);
|
|
133
|
+
return serverRoute && serverRoute.handler.endPoint;
|
|
109
134
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
for (let cookie of this.cookies.cookies.values()) {
|
|
115
|
-
this.response.headers.append("set-cookie", cookie);
|
|
116
|
-
}
|
|
117
|
-
for (let header of this.headers.headers.entries()) {
|
|
118
|
-
this.response.headers.append(header[0], header[1]);
|
|
135
|
+
#findServerHook(path2) {
|
|
136
|
+
let serverHookPath = "/_hook/~server";
|
|
137
|
+
if (path2.startsWith("/api/")) {
|
|
138
|
+
serverHookPath = "/api" + serverHookPath;
|
|
119
139
|
}
|
|
140
|
+
const serverHookRoute = this.routes.find((r) => r.path === serverHookPath);
|
|
141
|
+
return serverHookRoute && serverHookRoute.handler.endPoint;
|
|
120
142
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
constructor() {
|
|
132
|
-
this.routes = [];
|
|
133
|
-
this.middleware = [];
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* The standard Fetch API method that should work in any runtime (including
|
|
137
|
-
* Node with polyfills).
|
|
138
|
-
* @returns
|
|
139
|
-
*/
|
|
140
|
-
fetch = async (input, init) => {
|
|
141
|
-
const request = new Request(input, init);
|
|
142
|
-
const url = new URL(request.url);
|
|
143
|
-
const match = this.match(url.pathname);
|
|
144
|
-
let ev = new ServerEvent(request, match?.params);
|
|
145
|
-
if (this.middleware.length) {
|
|
146
|
-
let buildNext2 = function(middleware, i) {
|
|
147
|
-
if (ev.response) {
|
|
148
|
-
return noop;
|
|
149
|
-
}
|
|
150
|
-
if (i < middleware.length - 1) {
|
|
151
|
-
let next = middleware[i + 1];
|
|
152
|
-
return async () => next(ev, buildNext2(middleware, i + 1));
|
|
143
|
+
// HACK: We're doing this here as well as in Site, so that you can use the
|
|
144
|
+
// Router externally without Site
|
|
145
|
+
#sortRoutes() {
|
|
146
|
+
this.routes = this.routes.sort((a, b) => {
|
|
147
|
+
for (let i = 0; i < Math.min(a.path.length, b.path.length); i++) {
|
|
148
|
+
if (a.path[i] === "[" && b.path[i] !== "[") {
|
|
149
|
+
return 1;
|
|
150
|
+
} else if (b.path[i] === "[" && a.path[i] !== "[") {
|
|
151
|
+
return -1;
|
|
152
|
+
} else if (a.path[i] === b.path[i]) {
|
|
153
153
|
} else {
|
|
154
|
-
return
|
|
155
|
-
ev.response = await match.fn(ev);
|
|
156
|
-
} : noop;
|
|
154
|
+
return a.path[i].localeCompare(b.path[i]);
|
|
157
155
|
}
|
|
158
|
-
};
|
|
159
|
-
var buildNext = buildNext2;
|
|
160
|
-
await this.middleware[0](ev, buildNext2(this.middleware, 0));
|
|
161
|
-
} else if (match) {
|
|
162
|
-
ev.response = await match.fn(ev);
|
|
163
|
-
} else {
|
|
164
|
-
return new Response(null, { status: 404 });
|
|
165
|
-
}
|
|
166
|
-
ev.response ??= new Response(null, { status: 200 });
|
|
167
|
-
ev.addHeaders();
|
|
168
|
-
return ev.response;
|
|
169
|
-
};
|
|
170
|
-
/**
|
|
171
|
-
* Adds a server function at a route path.
|
|
172
|
-
* @param path The route path pattern.
|
|
173
|
-
* @param fn The function to call when the pattern is matched.
|
|
174
|
-
* @returns
|
|
175
|
-
*/
|
|
176
|
-
add(path2, fn) {
|
|
177
|
-
this.routes.push(new RouteHandler(path2, fn));
|
|
178
|
-
return this;
|
|
179
|
-
}
|
|
180
|
-
match(path2) {
|
|
181
|
-
for (let handler of this.routes) {
|
|
182
|
-
let match = path2.match(handler.regex);
|
|
183
|
-
if (match) {
|
|
184
|
-
return {
|
|
185
|
-
fn: handler.fn,
|
|
186
|
-
params: match.groups
|
|
187
|
-
};
|
|
188
156
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
// TODO:
|
|
193
|
-
//use(path: string, ...fn: MiddlewareFunction[])
|
|
194
|
-
use(...fn) {
|
|
195
|
-
this.middleware = this.middleware.concat(fn);
|
|
196
|
-
return this;
|
|
157
|
+
return a.path.length - b.path.length;
|
|
158
|
+
});
|
|
197
159
|
}
|
|
198
160
|
};
|
|
199
|
-
var
|
|
161
|
+
var Route = class {
|
|
200
162
|
path;
|
|
201
163
|
regex;
|
|
202
|
-
|
|
203
|
-
constructor(path2,
|
|
164
|
+
handler;
|
|
165
|
+
constructor(path2, handler) {
|
|
204
166
|
this.path = path2;
|
|
205
167
|
this.regex = pathToRegex(path2);
|
|
206
|
-
this.
|
|
168
|
+
this.handler = handler;
|
|
207
169
|
}
|
|
208
170
|
};
|
|
209
171
|
|
|
@@ -302,7 +264,7 @@ var Site = class {
|
|
|
302
264
|
};
|
|
303
265
|
// Annotate the CommonJS export names for ESM import in node:
|
|
304
266
|
0 && (module.exports = {
|
|
305
|
-
|
|
267
|
+
Router,
|
|
306
268
|
Site
|
|
307
269
|
});
|
|
308
270
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/site/pathToRegex.ts","../src/server/CookieHelper.ts","../src/server/HeaderHelper.ts","../src/server/ServerEvent.ts","../src/server/Server.ts","../src/site/Site.ts","../src/types/RouteType.ts","../src/site/defaultAdapter.ts"],"sourcesContent":["import Server from \"./server/Server\";\nimport Site from \"./site/Site\";\nimport type Adapter from \"./types/Adapter\";\nimport type PageEndPoint from \"./types/PageEndPoint\";\nimport type PageLoadEvent from \"./types/PageLoadEvent\";\nimport type PageServerAction from \"./types/PageServerAction\";\nimport type PageServerEndPoint from \"./types/PageServerEndPoint\";\nimport type PageServerLoad from \"./types/PageServerLoad\";\nimport type ServerEndPoint from \"./types/ServerEndPoint\";\nimport type ServerHook from \"./types/ServerHook\";\nimport type ServerLoadEvent from \"./types/ServerLoadEvent\";\nimport type ServerRequest from \"./types/ServerRequest\";\n\nexport { Server, Site };\n\nexport type {\n\tAdapter,\n\tPageEndPoint,\n\tPageServerEndPoint,\n\tPageServerLoad,\n\tPageServerAction,\n\tServerEndPoint,\n\tServerRequest,\n\tServerHook,\n\tPageLoadEvent,\n\tServerLoadEvent,\n};\n","export default function pathToRegex(path: string): RegExp {\n\tconst pattern = path\n\t\t.split(\"/\")\n\t\t.map((p) => {\n\t\t\treturn p.replace(/\\[([^\\/]+?)\\]/, \"(?<$1>[^\\\\/]+?)\");\n\t\t})\n\t\t.join(\"\\\\/\")\n\t\t.replaceAll(\"*\", \".*\");\n\treturn new RegExp(`^${pattern}$`);\n}\n","import { SerializeOptions, parse, serialize } from \"cookie\";\n\nexport default class CookieHelper {\n\trequest: Request;\n\tcookies: Map<string, string> = new Map();\n\n\tconstructor(request: Request) {\n\t\tthis.request = request;\n\t}\n\n\tget(name: string): string | undefined {\n\t\t// If it's been set, return the updated value\n\t\tif (this.cookies.has(name)) {\n\t\t\treturn this.cookies.get(name);\n\t\t}\n\t\t// Otherwise, return the cookie value from the `Cookie` header\n\t\tconst header = this.request.headers.get(\"cookie\");\n\t\treturn parse(header ?? \"\")[name];\n\t}\n\n\tset(name: string, value: string, options?: SerializeOptions) {\n\t\t// Set a cookie header with some default values for security\n\t\toptions ??= {};\n\t\toptions.httpOnly ??= true;\n\t\toptions.secure ??= true;\n\t\toptions.sameSite ??= \"lax\";\n\t\tthis.cookies.set(name, serialize(name, value, options));\n\t}\n\n\tdelete(name: string, options?: SerializeOptions) {\n\t\t// Set a cookie header with an immediate expiry date\n\t\toptions ??= {};\n\t\toptions.expires = new Date(0);\n\t\tthis.cookies.set(name, serialize(name, \"\", options));\n\t}\n}\n","export default class HeaderHelper {\n\trequest: Request;\n\theaders: Map<string, string> = new Map();\n\n\tconstructor(request: Request) {\n\t\tthis.request = request;\n\t}\n\n\tget(name: string): string | undefined {\n\t\t// If it's been set, return the updated value\n\t\tif (this.headers.has(name)) {\n\t\t\treturn this.headers.get(name);\n\t\t}\n\t\t// Otherwise, return the value from the header\n\t\treturn this.request.headers.get(name) ?? undefined;\n\t}\n\n\tset(name: string, value: string) {\n\t\tthis.headers.set(name, value);\n\t}\n\n\tdelete(name: string) {\n\t\tthis.headers.delete(name);\n\t}\n}\n","import CookieHelper from \"./CookieHelper\";\nimport HeaderHelper from \"./HeaderHelper\";\n\nexport default class ServerEvent {\n\trequest: Request;\n\tresponse?: Response;\n\t// TODO: Should we cast types??\n\tparams?: { [key: string]: string };\n\n\tcookies: CookieHelper;\n\theaders: HeaderHelper;\n\n\tconstructor(request: Request, params?: { [key: string]: string }) {\n\t\tthis.request = request;\n\t\tthis.params = params;\n\t\tthis.cookies = new CookieHelper(request);\n\t\tthis.headers = new HeaderHelper(request);\n\t}\n\n\taddHeaders() {\n\t\tif (!this.response) {\n\t\t\tthrow new Error(\"Response not created yet\");\n\t\t}\n\n\t\tfor (let cookie of this.cookies.cookies.values()) {\n\t\t\tthis.response.headers.append(\"set-cookie\", cookie);\n\t\t}\n\n\t\tfor (let header of this.headers.headers.entries()) {\n\t\t\tthis.response.headers.append(header[0], header[1]);\n\t\t}\n\t}\n}\n","import pathToRegex from \"../site/pathToRegex\";\nimport type MiddlewareFunction from \"../types/MiddlewareFunction\";\nimport ServerEvent from \"./ServerEvent\";\nimport type ServerFunction from \"./ServerFunction\";\n\nconst noop = () => {};\n\n/**\n * A Fetch API Request/Response server.\n */\nexport default class Server {\n\t// TODO: We don't use any of the routes, just add(\"*\") in runDev/runBuild...\n\troutes: RouteHandler[] = [];\n\t// TODO: We only use the Vite middleware here\n\tmiddleware: MiddlewareFunction[] = [];\n\n\tconstructor() {\n\t\tthis.routes = [];\n\t\tthis.middleware = [];\n\t}\n\n\t/**\n\t * The standard Fetch API method that should work in any runtime (including\n\t * Node with polyfills).\n\t * @returns\n\t */\n\tfetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {\n\t\tconst request = new Request(input, init);\n\t\tconst url = new URL(request.url);\n\t\tconst match = this.match(url.pathname);\n\t\tlet ev = new ServerEvent(request, match?.params);\n\n\t\tif (this.middleware.length) {\n\t\t\t// TODO: Get middleware that applies to this route only?\n\t\t\tawait this.middleware[0](ev, buildNext(this.middleware, 0));\n\t\t\tfunction buildNext(middleware: MiddlewareFunction[], i: number): () => void | Promise<void> {\n\t\t\t\tif (ev.response) {\n\t\t\t\t\treturn noop;\n\t\t\t\t}\n\n\t\t\t\tif (i < middleware.length - 1) {\n\t\t\t\t\tlet next = middleware[i + 1];\n\t\t\t\t\treturn async () => next(ev, buildNext(middleware, i + 1));\n\t\t\t\t} else {\n\t\t\t\t\treturn match\n\t\t\t\t\t\t? async () => {\n\t\t\t\t\t\t\t\tev.response = await match.fn(ev);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: noop;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (match) {\n\t\t\tev.response = await match.fn(ev);\n\t\t} else {\n\t\t\treturn new Response(null, { status: 404 });\n\t\t}\n\n\t\t// TODO: Should I be returning 200 if there's no response? Because it's not an error??\n\t\tev.response ??= new Response(null, { status: 200 });\n\t\tev.addHeaders();\n\n\t\treturn ev.response;\n\t};\n\n\t/**\n\t * Adds a server function at a route path.\n\t * @param path The route path pattern.\n\t * @param fn The function to call when the pattern is matched.\n\t * @returns\n\t */\n\tadd(path: string, fn: ServerFunction): Server {\n\t\tthis.routes.push(new RouteHandler(path, fn));\n\t\treturn this;\n\t}\n\n\tmatch(path: string) {\n\t\tfor (let handler of this.routes) {\n\t\t\tlet match = path.match(handler.regex);\n\t\t\tif (match) {\n\t\t\t\treturn {\n\t\t\t\t\tfn: handler.fn,\n\t\t\t\t\tparams: match.groups,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\tconsole.log(`Server path not found: ${path}`);\n\t}\n\n\t// TODO:\n\t//use(path: string, ...fn: MiddlewareFunction[])\n\tuse(...fn: MiddlewareFunction[]): Server {\n\t\tthis.middleware = this.middleware.concat(fn);\n\t\treturn this;\n\t}\n}\n\nclass RouteHandler {\n\tpath: string;\n\tregex: RegExp;\n\tfn: ServerFunction;\n\n\tconstructor(path: string, fn: ServerFunction) {\n\t\tthis.path = path;\n\t\tthis.regex = pathToRegex(path);\n\t\tthis.fn = fn;\n\t}\n}\n\n//class MiddlewareHandler {\n//\tpath: string;\n//\tregex: RegExp;\n//\tfn: MiddlewareFunction;\n//\n//\tconstructor(path: string, fn: MiddlewareFunction) {\n//\t\tthis.path = path;\n//\t\tthis.regex = pathToRegex(path);\n//\t\tthis.fn = fn;\n//\t}\n//}\n","import { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { Plugin } from \"vite\";\nimport tsconfigPaths from \"vite-tsconfig-paths\";\nimport Adapter from \"../types/Adapter\";\nimport {\n\tHOOK_ROUTE,\n\tHOOK_SERVER_ROUTE,\n\tLAYOUT_ROUTE,\n\tLAYOUT_SERVER_ROUTE,\n\tPAGE_ROUTE,\n\tPAGE_SERVER_ROUTE,\n\tSERVER_ROUTE,\n} from \"../types/RouteType\";\nimport defaultAdapter from \"./defaultAdapter\";\n\n// 1. The user sets this up\n// 2. It is passed into serverEntry and clientEntry through a Vite plugin\n// (defined in manifest.ts)\n// 3. In those files, it is used to build a Router\n\n/**\n * The Site class contains the information necessary to setup a @torpor/build\n * site, including routes, plugins, and options.\n */\nexport default class Site {\n\troot: string;\n\troutes: { path: string; file: string; type: number }[] = [];\n\t// Is default plugins a bad idea?\n\t// HACK: Set loose because otherwise it uses a list of allowed extensions\n\t// TODO: Should probably just do this ourselves and let the user pass in\n\t// tsconfigPaths themselves if they want to do something funky\n\tplugins: Plugin[] = [tsconfigPaths({ loose: true })];\n\t// Is default adapter a bad idea?\n\tadapter: Adapter = defaultAdapter;\n\n\tconstructor() {\n\t\tthis.root = process.cwd();\n\t}\n\n\tasync addRouteFolder(folder: string) {\n\t\tconst routeFolder = path.join(this.root, folder);\n\t\tconst routeFiles = await fs.readdir(routeFolder, { recursive: true });\n\n\t\tfor (let file of routeFiles) {\n\t\t\t// The file must start with `+` or `_` and end with `.js` or `.ts`\n\t\t\tif (/^(\\+|_).+(\\.js|\\.ts)$/.test(path.basename(file))) {\n\t\t\t\tlet routePath = file\n\t\t\t\t\t.replace(/^\\//, \"\")\n\t\t\t\t\t.replace(/(\\.ts|\\.js)$/, \"\")\n\t\t\t\t\t.replace(/_hook.server$/, \"_hook/~server\")\n\t\t\t\t\t.replace(/_layout.server$/, \"_layout/~server\")\n\t\t\t\t\t.replace(/\\+page.server$/, \"~server\")\n\t\t\t\t\t.replace(/\\+server$/, \"\")\n\t\t\t\t\t.replace(/\\+page$/, \"\")\n\t\t\t\t\t.replace(/\\/$/, \"\");\n\t\t\t\troutePath = routePath.length > 0 ? `/${routePath}` : \"/\";\n\t\t\t\tlet type = this.#routeType(file);\n\t\t\t\tfile = path.relative(this.root, path.resolve(routeFolder, file));\n\t\t\t\tthis.routes.push({ path: routePath, file, type });\n\t\t\t}\n\t\t}\n\t\tthis.#sortRoutes();\n\t}\n\n\taddRouteFile(path: string, file: string) {\n\t\tlet type = this.#routeType(file);\n\t\tthis.routes.push({ path, file, type });\n\t\tthis.#sortRoutes();\n\t}\n\n\t#routeType(file: string): number {\n\t\tlet routePath = file.replace(/^\\//, \"\").replace(/(\\.ts|\\.js)$/, \"\");\n\t\tif (routePath.endsWith(\"+page\")) {\n\t\t\treturn PAGE_ROUTE;\n\t\t} else if (routePath.endsWith(\"+page.server\")) {\n\t\t\treturn PAGE_SERVER_ROUTE;\n\t\t} else if (routePath.endsWith(\"+server\")) {\n\t\t\treturn SERVER_ROUTE;\n\t\t} else if (routePath.endsWith(\"_layout\")) {\n\t\t\treturn LAYOUT_ROUTE;\n\t\t} else if (routePath.endsWith(\"_layout.server\")) {\n\t\t\treturn LAYOUT_SERVER_ROUTE;\n\t\t} else if (routePath.endsWith(\"_hook\")) {\n\t\t\treturn HOOK_ROUTE;\n\t\t} else if (routePath.endsWith(\"_hook.server\")) {\n\t\t\treturn HOOK_SERVER_ROUTE;\n\t\t}\n\t\treturn -1;\n\t}\n\n\t// TODO: You could make your site in a single file, like e.g. Hono...\n\t//addPage(path: string, endPoint: PageEndPoint, serverEndPoint?: PageServerEndPoint) {}\n\t//addEndPoint(path: string, endPoint: ServerEndPoint) {}\n\n\t#sortRoutes() {\n\t\tthis.routes = this.routes.sort((a, b) => {\n\t\t\t// Sort [param]s after paths\n\t\t\t// There might be a quicker/easier way to do this\n\t\t\tfor (let i = 0; i < Math.min(a.path.length, b.path.length); i++) {\n\t\t\t\tif (a.path[i] === \"[\" && b.path[i] !== \"[\") {\n\t\t\t\t\treturn 1;\n\t\t\t\t} else if (b.path[i] === \"[\" && a.path[i] !== \"[\") {\n\t\t\t\t\treturn -1;\n\t\t\t\t} else if (a.path[i] === b.path[i]) {\n\t\t\t\t\t// Keep going...\n\t\t\t\t} else {\n\t\t\t\t\treturn a.path[i].localeCompare(b.path[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn a.path.length - b.path.length;\n\t\t});\n\t}\n}\n","export const PAGE_ROUTE = 0;\nexport const PAGE_SERVER_ROUTE = 1;\nexport const SERVER_ROUTE = 3;\nexport const LAYOUT_ROUTE = 4;\nexport const LAYOUT_SERVER_ROUTE = 5;\nexport const HOOK_ROUTE = 6;\nexport const HOOK_SERVER_ROUTE = 7;\n","import Adapter from \"../types/Adapter\";\n\n// TODO: This should be an adapter-auto package that conditionally loads other packages...\n\nconst defaultAdapter: Adapter = {\n\tserve: () => {\n\t\tconsole.log(\"An adapter needs to be set in site.config\");\n\t},\n};\n\nexport default defaultAdapter;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAe,SAAR,YAA6BA,OAAsB;AACzD,QAAM,UAAUA,MACd,MAAM,GAAG,EACT,IAAI,CAAC,MAAM;AACX,WAAO,EAAE,QAAQ,iBAAiB,iBAAiB;AAAA,EACpD,CAAC,EACA,KAAK,KAAK,EACV,WAAW,KAAK,IAAI;AACtB,SAAO,IAAI,OAAO,IAAI,OAAO,GAAG;AACjC;;;ACTA,oBAAmD;AAEnD,IAAqB,eAArB,MAAkC;AAAA,EACjC;AAAA,EACA,UAA+B,oBAAI,IAAI;AAAA,EAEvC,YAAY,SAAkB;AAC7B,SAAK,UAAU;AAAA,EAChB;AAAA,EAEA,IAAI,MAAkC;AAErC,QAAI,KAAK,QAAQ,IAAI,IAAI,GAAG;AAC3B,aAAO,KAAK,QAAQ,IAAI,IAAI;AAAA,IAC7B;AAEA,UAAM,SAAS,KAAK,QAAQ,QAAQ,IAAI,QAAQ;AAChD,eAAO,qBAAM,UAAU,EAAE,EAAE,IAAI;AAAA,EAChC;AAAA,EAEA,IAAI,MAAc,OAAe,SAA4B;AAE5D,gBAAY,CAAC;AACb,YAAQ,aAAa;AACrB,YAAQ,WAAW;AACnB,YAAQ,aAAa;AACrB,SAAK,QAAQ,IAAI,UAAM,yBAAU,MAAM,OAAO,OAAO,CAAC;AAAA,EACvD;AAAA,EAEA,OAAO,MAAc,SAA4B;AAEhD,gBAAY,CAAC;AACb,YAAQ,UAAU,oBAAI,KAAK,CAAC;AAC5B,SAAK,QAAQ,IAAI,UAAM,yBAAU,MAAM,IAAI,OAAO,CAAC;AAAA,EACpD;AACD;;;ACnCA,IAAqB,eAArB,M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,EAMA,cAAc;AACb,SAAK,SAAS,KAAK,OAAO,KAAK,CAAC,GAAG,MAAM;AAGxC,eAAS,IAAI,GAAG,IAAI,KAAK,IAAI,EAAE,KAAK,QAAQ,EAAE,KAAK,MAAM,GAAG,KAAK;AAChE,YAAI,EAAE,KAAK,CAAC,MAAM,OAAO,EAAE,KAAK,CAAC,MAAM,KAAK;AAC3C,iBAAO;AAAA,QACR,WAAW,EAAE,KAAK,CAAC,MAAM,OAAO,EAAE,KAAK,CAAC,MAAM,KAAK;AAClD,iBAAO;AAAA,QACR,WAAW,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG;AAAA,QAEpC,OAAO;AACN,iBAAO,EAAE,KAAK,CAAC,EAAE,cAAc,EAAE,KAAK,CAAC,CAAC;AAAA,QACzC;AAAA,MACD;AACA,aAAO,EAAE,KAAK,SAAS,EAAE,KAAK;AAAA,IAC/B,CAAC;AAAA,EACF;AACD;","names":["path","buildNext","path","tsconfigPaths","path","fs"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/site/pathToRegex.ts","../src/site/Router.ts","../src/site/Site.ts","../src/types/RouteType.ts","../src/site/defaultAdapter.ts"],"sourcesContent":["import Router from \"./site/Router\";\nimport Site from \"./site/Site\";\nimport type Adapter from \"./types/Adapter\";\nimport type PageEndPoint from \"./types/PageEndPoint\";\nimport type PageLoadEvent from \"./types/PageLoadEvent\";\nimport type PageServerAction from \"./types/PageServerAction\";\nimport type PageServerEndPoint from \"./types/PageServerEndPoint\";\nimport type PageServerLoad from \"./types/PageServerLoad\";\nimport type ServerEndPoint from \"./types/ServerEndPoint\";\nimport type ServerHook from \"./types/ServerHook\";\nimport type ServerLoadEvent from \"./types/ServerLoadEvent\";\nimport type ServerRequest from \"./types/ServerRequest\";\n\nexport { Site, Router };\n\nexport type {\n\tAdapter,\n\tPageEndPoint,\n\tPageServerEndPoint,\n\tPageServerLoad,\n\tPageServerAction,\n\tServerEndPoint,\n\tServerRequest,\n\tServerHook,\n\tPageLoadEvent,\n\tServerLoadEvent,\n};\n","export default function pathToRegex(path: string): RegExp {\n\tconst pattern = path\n\t\t.split(\"/\")\n\t\t.map((p) => {\n\t\t\treturn p.replace(/\\[([^\\/]+?)\\]/, \"(?<$1>[^\\\\/]+?)\");\n\t\t})\n\t\t.join(\"\\\\/\")\n\t\t.replaceAll(\"*\", \".*\");\n\treturn new RegExp(`^${pattern}$`);\n}\n","import type LayoutHandler from \"../types/LayoutHandler\";\nimport type ManifestRoute from \"../types/ManifestRoute\";\nimport type RouteHandler from \"../types/RouteHandler\";\nimport pathToRegex from \"./pathToRegex\";\n\n/**\n * A router that handles file routes with layouts, hooks, etc.\n */\nexport default class Router {\n\troutes: Route[] = [];\n\n\tconstructor() {}\n\n\t//add(path: string, file: string) {\n\t//\t// TODO: Call the specific addX method based on the file path\n\t//\tthis.addPage(route, file);\n\t//}\n\n\taddPages(routes: ManifestRoute[]) {\n\t\tfor (let r of routes) {\n\t\t\tthis.routes.push(\n\t\t\t\tnew Route(r.path, {\n\t\t\t\t\tpath: r.path,\n\t\t\t\t\ttype: r.type,\n\t\t\t\t\tendPoint: r.endPoint,\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\t\tthis.#sortRoutes();\n\t\treturn this;\n\t}\n\n\t// TODO: Allow calling with the endpoint itself, so that you can setup an app with no scaffold\n\taddPage(path: string, type: number, endPoint: () => Promise<any>) {\n\t\tthis.routes.push(\n\t\t\tnew Route(path, {\n\t\t\t\tpath,\n\t\t\t\ttype,\n\t\t\t\tendPoint,\n\t\t\t}),\n\t\t);\n\t\tthis.#sortRoutes();\n\t\treturn this;\n\t}\n\n\t/*\n\tloadPage(ev: ServerEvent, file: string): Response {\n\t\tlet page = this.pages.get(file);\n\t\tif (!page) {\n\t\t\tpage = {};\n\t\t\tthis.pages.set(file, page);\n\t\t}\n\t}\n\t*/\n\n\tmatch(path: string, query: URLSearchParams) {\n\t\tfor (let route of this.routes) {\n\t\t\tlet match = path.match(route.regex);\n\t\t\tif (match) {\n\t\t\t\t// Lazy load server endpoints and layouts\n\t\t\t\tif (!route.handler.loaded) {\n\t\t\t\t\tthis.#loadHandler(route.handler, route.path);\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\thandler: route.handler,\n\t\t\t\t\tparams: match.groups,\n\t\t\t\t\tquery,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\tconsole.log(`not found: '${path}'`);\n\t}\n\n\t#loadHandler(handler: RouteHandler, path: string) {\n\t\thandler.layouts = this.#findLayouts(path);\n\t\thandler.serverEndPoint = this.#findServer(path);\n\t\thandler.serverHook = this.#findServerHook(path);\n\t}\n\n\t#findLayouts(path: string): LayoutHandler[] | undefined {\n\t\tlet layouts: LayoutHandler[] = [];\n\t\tlet parts = path\n\t\t\t// Strip the trailing`~/server` (so we don't look for `~/server/_layout`\n\t\t\t// which will never exist)\n\t\t\t.replace(/\\/~server$/, \"\")\n\t\t\t// The path will always start with / so splitting e.g. `/first/second`\n\t\t\t// will result in ['', 'first', 'second'] and checks for `/_layout`,\n\t\t\t// `/first/_layout` and `/second/_layout`\n\t\t\t.split(\"/\");\n\t\tlet basePath = \"\";\n\t\tfor (let i = 0; i < parts.length; i++) {\n\t\t\tif (i > 0) {\n\t\t\t\tbasePath += \"/\" + parts[i];\n\t\t\t}\n\t\t\tconst layoutPath = basePath + \"/_layout\";\n\t\t\tconst layoutRoute = this.routes.find((r) => r.path === layoutPath);\n\t\t\tif (layoutRoute) {\n\t\t\t\tlayouts.push({\n\t\t\t\t\tpath: layoutPath,\n\t\t\t\t\tendPoint: layoutRoute.handler.endPoint,\n\t\t\t\t\tserverEndPoint: this.#findServer(layoutPath),\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn layouts.length ? layouts : undefined;\n\t}\n\n\t#findServer(path: string): (() => Promise<any>) | undefined {\n\t\tconst serverPath = path.replace(/\\/$/, \"\") + \"/~server\";\n\t\tconst serverRoute = this.routes.find((r) => r.path === serverPath);\n\t\treturn serverRoute && serverRoute.handler.endPoint;\n\t}\n\n\t#findServerHook(path: string): (() => Promise<any>) | undefined {\n\t\t// TODO: Should this be a collection, like layouts?\n\t\tlet serverHookPath = \"/_hook/~server\";\n\t\t// HACK: Generalise this\n\t\tif (path.startsWith(\"/api/\")) {\n\t\t\tserverHookPath = \"/api\" + serverHookPath;\n\t\t}\n\t\tconst serverHookRoute = this.routes.find((r) => r.path === serverHookPath);\n\t\treturn serverHookRoute && serverHookRoute.handler.endPoint;\n\t}\n\n\t// HACK: We're doing this here as well as in Site, so that you can use the\n\t// Router externally without Site\n\t#sortRoutes() {\n\t\tthis.routes = this.routes.sort((a, b) => {\n\t\t\t// Sort [param]s after paths\n\t\t\t// There might be a quicker/easier way to do this\n\t\t\tfor (let i = 0; i < Math.min(a.path.length, b.path.length); i++) {\n\t\t\t\tif (a.path[i] === \"[\" && b.path[i] !== \"[\") {\n\t\t\t\t\treturn 1;\n\t\t\t\t} else if (b.path[i] === \"[\" && a.path[i] !== \"[\") {\n\t\t\t\t\treturn -1;\n\t\t\t\t} else if (a.path[i] === b.path[i]) {\n\t\t\t\t\t// Keep going...\n\t\t\t\t} else {\n\t\t\t\t\treturn a.path[i].localeCompare(b.path[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn a.path.length - b.path.length;\n\t\t});\n\t}\n}\n\nclass Route {\n\tpath: string;\n\tregex: RegExp;\n\thandler: RouteHandler;\n\n\tconstructor(path: string, handler: RouteHandler) {\n\t\tthis.path = path;\n\t\tthis.regex = pathToRegex(path);\n\t\tthis.handler = handler;\n\t}\n}\n","import { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { Plugin } from \"vite\";\nimport tsconfigPaths from \"vite-tsconfig-paths\";\nimport Adapter from \"../types/Adapter\";\nimport {\n\tHOOK_ROUTE,\n\tHOOK_SERVER_ROUTE,\n\tLAYOUT_ROUTE,\n\tLAYOUT_SERVER_ROUTE,\n\tPAGE_ROUTE,\n\tPAGE_SERVER_ROUTE,\n\tSERVER_ROUTE,\n} from \"../types/RouteType\";\nimport defaultAdapter from \"./defaultAdapter\";\n\n// 1. The user sets this up\n// 2. It is passed into serverEntry and clientEntry through a Vite plugin\n// (defined in manifest.ts)\n// 3. In those files, it is used to build a Router\n\n/**\n * The Site class contains the information necessary to setup a @torpor/build\n * site, including routes, plugins, and options.\n */\nexport default class Site {\n\troot: string;\n\troutes: { path: string; file: string; type: number }[] = [];\n\t// Is default plugins a bad idea?\n\t// HACK: Set loose because otherwise it uses a list of allowed extensions\n\t// TODO: Should probably just do this ourselves and let the user pass in\n\t// tsconfigPaths themselves if they want to do something funky\n\tplugins: Plugin[] = [tsconfigPaths({ loose: true })];\n\t// Is default adapter a bad idea?\n\tadapter: Adapter = defaultAdapter;\n\n\tconstructor() {\n\t\tthis.root = process.cwd();\n\t}\n\n\tasync addRouteFolder(folder: string) {\n\t\tconst routeFolder = path.join(this.root, folder);\n\t\tconst routeFiles = await fs.readdir(routeFolder, { recursive: true });\n\n\t\tfor (let file of routeFiles) {\n\t\t\t// The file must start with `+` or `_` and end with `.js` or `.ts`\n\t\t\tif (/^(\\+|_).+(\\.js|\\.ts)$/.test(path.basename(file))) {\n\t\t\t\tlet routePath = file\n\t\t\t\t\t.replace(/^\\//, \"\")\n\t\t\t\t\t.replace(/(\\.ts|\\.js)$/, \"\")\n\t\t\t\t\t.replace(/_hook.server$/, \"_hook/~server\")\n\t\t\t\t\t.replace(/_layout.server$/, \"_layout/~server\")\n\t\t\t\t\t.replace(/\\+page.server$/, \"~server\")\n\t\t\t\t\t.replace(/\\+server$/, \"\")\n\t\t\t\t\t.replace(/\\+page$/, \"\")\n\t\t\t\t\t.replace(/\\/$/, \"\");\n\t\t\t\troutePath = routePath.length > 0 ? `/${routePath}` : \"/\";\n\t\t\t\tlet type = this.#routeType(file);\n\t\t\t\tfile = path.relative(this.root, path.resolve(routeFolder, file));\n\t\t\t\tthis.routes.push({ path: routePath, file, type });\n\t\t\t}\n\t\t}\n\t\tthis.#sortRoutes();\n\t}\n\n\taddRouteFile(path: string, file: string) {\n\t\tlet type = this.#routeType(file);\n\t\tthis.routes.push({ path, file, type });\n\t\tthis.#sortRoutes();\n\t}\n\n\t#routeType(file: string): number {\n\t\tlet routePath = file.replace(/^\\//, \"\").replace(/(\\.ts|\\.js)$/, \"\");\n\t\tif (routePath.endsWith(\"+page\")) {\n\t\t\treturn PAGE_ROUTE;\n\t\t} else if (routePath.endsWith(\"+page.server\")) {\n\t\t\treturn PAGE_SERVER_ROUTE;\n\t\t} else if (routePath.endsWith(\"+server\")) {\n\t\t\treturn SERVER_ROUTE;\n\t\t} else if (routePath.endsWith(\"_layout\")) {\n\t\t\treturn LAYOUT_ROUTE;\n\t\t} else if (routePath.endsWith(\"_layout.server\")) {\n\t\t\treturn LAYOUT_SERVER_ROUTE;\n\t\t} else if (routePath.endsWith(\"_hook\")) {\n\t\t\treturn HOOK_ROUTE;\n\t\t} else if (routePath.endsWith(\"_hook.server\")) {\n\t\t\treturn HOOK_SERVER_ROUTE;\n\t\t}\n\t\treturn -1;\n\t}\n\n\t// TODO: You could make your site in a single file, like e.g. Hono...\n\t//addPage(path: string, endPoint: PageEndPoint, serverEndPoint?: PageServerEndPoint) {}\n\t//addEndPoint(path: string, endPoint: ServerEndPoint) {}\n\n\t#sortRoutes() {\n\t\tthis.routes = this.routes.sort((a, b) => {\n\t\t\t// Sort [param]s after paths\n\t\t\t// There might be a quicker/easier way to do this\n\t\t\tfor (let i = 0; i < Math.min(a.path.length, b.path.length); i++) {\n\t\t\t\tif (a.path[i] === \"[\" && b.path[i] !== \"[\") {\n\t\t\t\t\treturn 1;\n\t\t\t\t} else if (b.path[i] === \"[\" && a.path[i] !== \"[\") {\n\t\t\t\t\treturn -1;\n\t\t\t\t} else if (a.path[i] === b.path[i]) {\n\t\t\t\t\t// Keep going...\n\t\t\t\t} else {\n\t\t\t\t\treturn a.path[i].localeCompare(b.path[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn a.path.length - b.path.length;\n\t\t});\n\t}\n}\n","export const PAGE_ROUTE = 0;\nexport const PAGE_SERVER_ROUTE = 1;\nexport const SERVER_ROUTE = 3;\nexport const LAYOUT_ROUTE = 4;\nexport const LAYOUT_SERVER_ROUTE = 5;\nexport const HOOK_ROUTE = 6;\nexport const HOOK_SERVER_ROUTE = 7;\n","import Adapter from \"../types/Adapter\";\n\n// TODO: This should be an adapter-auto package that conditionally loads other packages...\n\nconst defaultAdapter: Adapter = {\n\tserve: () => {\n\t\tconsole.log(\"An adapter needs to be set in site.config\");\n\t},\n};\n\nexport default defaultAdapter;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAe,SAAR,YAA6BA,OAAsB;AACzD,QAAM,UAAUA,MACd,MAAM,GAAG,EACT,IAAI,CAAC,MAAM;AACX,WAAO,EAAE,QAAQ,iBAAiB,iBAAiB;AAAA,EACpD,CAAC,EACA,KAAK,KAAK,EACV,WAAW,KAAK,IAAI;AACtB,SAAO,IAAI,OAAO,IAAI,OAAO,GAAG;AACjC;;;ACDA,IAAqB,SAArB,MAA4B;AAAA,EAC3B,SAAkB,CAAC;AAAA,EAEnB,cAAc;AAAA,EAAC;AAAA;AAAA;AAAA;AAAA;AAAA,EAOf,SAAS,QAAyB;AACjC,aAAS,KAAK,QAAQ;AACrB,WAAK,OAAO;AAAA,QACX,IAAI,MAAM,EAAE,MAAM;AAAA,UACjB,MAAM,EAAE;AAAA,UACR,MAAM,EAAE;AAAA,UACR,UAAU,EAAE;AAAA,QACb,CAAC;AAAA,MACF;AAAA,IACD;AACA,SAAK,YAAY;AACjB,WAAO;AAAA,EACR;AAAA;AAAA,EAGA,QAAQC,OAAc,MAAc,UAA8B;AACjE,SAAK,OAAO;AAAA,MACX,IAAI,MAAMA,OAAM;AAAA,QACf,MAAAA;AAAA,QACA;AAAA,QACA;AAAA,MACD,CAAC;AAAA,IACF;AACA,SAAK,YAAY;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;AAAA,IAC/B,CAAC;AAAA,EACF;AACD;","names":["path","path","tsconfigPaths","path","fs"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
export { A as Adapter, a as Server, S as Site } from './Site-Df1k0TuQ.cjs';
|
|
1
|
+
export { A as Adapter, S as Site } from './Site-PxPCXGZ-.cjs';
|
|
3
2
|
import { Component } from '@torpor/view';
|
|
3
|
+
import { C as CookieHelper, H as HeaderHelper } from './Server-B45PD5Ys.cjs';
|
|
4
4
|
import 'vite';
|
|
5
5
|
import 'cookie';
|
|
6
6
|
|
|
7
|
+
type ManifestRoute = {
|
|
8
|
+
path: string;
|
|
9
|
+
type: number;
|
|
10
|
+
endPoint: () => Promise<any>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type RouteLayoutHandler = {
|
|
14
|
+
path: string;
|
|
15
|
+
endPoint: () => Promise<any>;
|
|
16
|
+
serverEndPoint?: () => Promise<any>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type RouteHandler = {
|
|
20
|
+
path: string;
|
|
21
|
+
type: number;
|
|
22
|
+
endPoint: () => Promise<any>;
|
|
23
|
+
loaded?: boolean;
|
|
24
|
+
layouts?: RouteLayoutHandler[];
|
|
25
|
+
serverEndPoint?: () => Promise<any>;
|
|
26
|
+
serverHook?: () => Promise<any>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A router that handles file routes with layouts, hooks, etc.
|
|
31
|
+
*/
|
|
32
|
+
declare class Router {
|
|
33
|
+
#private;
|
|
34
|
+
routes: Route[];
|
|
35
|
+
constructor();
|
|
36
|
+
addPages(routes: ManifestRoute[]): this;
|
|
37
|
+
addPage(path: string, type: number, endPoint: () => Promise<any>): this;
|
|
38
|
+
match(path: string, query: URLSearchParams): {
|
|
39
|
+
handler: RouteHandler;
|
|
40
|
+
params: {
|
|
41
|
+
[key: string]: string;
|
|
42
|
+
} | undefined;
|
|
43
|
+
query: URLSearchParams;
|
|
44
|
+
} | undefined;
|
|
45
|
+
}
|
|
46
|
+
declare class Route {
|
|
47
|
+
path: string;
|
|
48
|
+
regex: RegExp;
|
|
49
|
+
handler: RouteHandler;
|
|
50
|
+
constructor(path: string, handler: RouteHandler);
|
|
51
|
+
}
|
|
52
|
+
|
|
7
53
|
type PageLoadEvent = {
|
|
8
54
|
/**
|
|
9
55
|
* The URL for the server function.
|
|
@@ -147,4 +193,4 @@ type ServerHook = {
|
|
|
147
193
|
|
|
148
194
|
type ServerLoad = (event: ServerLoadEvent) => Promise<void> | void;
|
|
149
195
|
|
|
150
|
-
export type
|
|
196
|
+
export { type PageEndPoint, type PageLoadEvent, type PageServerAction, type PageServerEndPoint, type PageServerLoad, Router, type ServerEndPoint, type ServerHook, type ServerLoadEvent, type ServerRequest };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
export { A as Adapter, a as Server, S as Site } from './Site-Df1k0TuQ.js';
|
|
1
|
+
export { A as Adapter, S as Site } from './Site-Qpcmx0Xq.js';
|
|
3
2
|
import { Component } from '@torpor/view';
|
|
3
|
+
import { C as CookieHelper, H as HeaderHelper } from './Server-B45PD5Ys.js';
|
|
4
4
|
import 'vite';
|
|
5
5
|
import 'cookie';
|
|
6
6
|
|
|
7
|
+
type ManifestRoute = {
|
|
8
|
+
path: string;
|
|
9
|
+
type: number;
|
|
10
|
+
endPoint: () => Promise<any>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type RouteLayoutHandler = {
|
|
14
|
+
path: string;
|
|
15
|
+
endPoint: () => Promise<any>;
|
|
16
|
+
serverEndPoint?: () => Promise<any>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type RouteHandler = {
|
|
20
|
+
path: string;
|
|
21
|
+
type: number;
|
|
22
|
+
endPoint: () => Promise<any>;
|
|
23
|
+
loaded?: boolean;
|
|
24
|
+
layouts?: RouteLayoutHandler[];
|
|
25
|
+
serverEndPoint?: () => Promise<any>;
|
|
26
|
+
serverHook?: () => Promise<any>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A router that handles file routes with layouts, hooks, etc.
|
|
31
|
+
*/
|
|
32
|
+
declare class Router {
|
|
33
|
+
#private;
|
|
34
|
+
routes: Route[];
|
|
35
|
+
constructor();
|
|
36
|
+
addPages(routes: ManifestRoute[]): this;
|
|
37
|
+
addPage(path: string, type: number, endPoint: () => Promise<any>): this;
|
|
38
|
+
match(path: string, query: URLSearchParams): {
|
|
39
|
+
handler: RouteHandler;
|
|
40
|
+
params: {
|
|
41
|
+
[key: string]: string;
|
|
42
|
+
} | undefined;
|
|
43
|
+
query: URLSearchParams;
|
|
44
|
+
} | undefined;
|
|
45
|
+
}
|
|
46
|
+
declare class Route {
|
|
47
|
+
path: string;
|
|
48
|
+
regex: RegExp;
|
|
49
|
+
handler: RouteHandler;
|
|
50
|
+
constructor(path: string, handler: RouteHandler);
|
|
51
|
+
}
|
|
52
|
+
|
|
7
53
|
type PageLoadEvent = {
|
|
8
54
|
/**
|
|
9
55
|
* The URL for the server function.
|
|
@@ -147,4 +193,4 @@ type ServerHook = {
|
|
|
147
193
|
|
|
148
194
|
type ServerLoad = (event: ServerLoadEvent) => Promise<void> | void;
|
|
149
195
|
|
|
150
|
-
export type
|
|
196
|
+
export { type PageEndPoint, type PageLoadEvent, type PageServerAction, type PageServerEndPoint, type PageServerLoad, Router, type ServerEndPoint, type ServerHook, type ServerLoadEvent, type ServerRequest };
|