UNPKG

@bs-core/astro

Version:
1 lines 12.7 kB
{"version":3,"file":"astro.mjs","sources":["../out/main.js"],"sourcesContent":["// imports here\nimport { bs, Router, } from \"@bs-core/shell\";\nimport { App } from \"astro/app\";\nimport { pathToFileURL } from \"url\";\nimport { performance } from \"node:perf_hooks\";\nimport * as streams from \"node:stream/promises\";\n// Misc consts here\nconst ADAPTER_NAME = \"@bs-core/astro\";\nconst HTTP_CERT_FILE = \"HTTP_CERT_FILE\";\nconst HTTP_KEY_FILE = \"HTTP_KEY_FILE\";\nconst HTTP_ENABLE_HTTPS = \"HTTP_ENABLE_HTTPS\";\nconst HTTP_IF = \"HTTP_IF\";\nconst HTTP_PORT = \"HTTP_PORT\";\n// Module properties here\n/** The Astro App instance. */\nlet _app;\nexport class WebRequest extends Request {\n req;\n res;\n constructor(req, res) {\n // Get the headers so we can pass them on\n const headers = new Headers();\n for (const [name, value] of Object.entries(req.headers)) {\n // Skip headers with no values\n if (value === undefined) {\n continue;\n }\n // Value can be an array so we need to handle that\n if (Array.isArray(value)) {\n // Append each value as the header name\n for (const item of value) {\n headers.append(name, item);\n }\n }\n else {\n headers.append(name, value);\n }\n }\n // Now call the super constructor to create the Request\n super(req.urlObj, { method: req.method, body: req.body, headers });\n // Make sure to tack on the nodeJs req/res\n this.req = req;\n this.res = res;\n }\n}\n// Private functions here\nfunction matcher(_) {\n return (url) => {\n const routeData = _app.match(new Request(url));\n if (routeData === undefined) {\n return false;\n }\n // For now we will ignore the params because they are available\n // in the Web Request object\n // NOTE: We need to return routeData so it can be use with _app.render()\n return {\n params: {},\n matchedInfo: routeData,\n };\n };\n}\nasync function ssrEndpoint(req, res) {\n // Create a webRequest object to pass to the render\n const webReq = new WebRequest(req, res);\n // Measure the time it took to render the page so capture when we started\n const startedAt = performance.now();\n // Now render the page\n const webRes = await _app.render(webReq, {\n routeData: req.matchedInfo,\n });\n // Now figure out how long it took to render and store it in the metrics\n const ssrLatency = Math.round(performance.now() - startedAt);\n res.addServerTimingMetric(\"ssr\", ssrLatency);\n // The default status code is always 200. If webRes.status is NOT 200\n // then it was set by the user so we need to use that status code\n if (webRes.status !== 200) {\n res.statusCode = webRes.status;\n }\n // Check if the user set any headers on webRes\n const SET_COOKIE = \"set-cookie\";\n for (let [name, value] of webRes.headers.entries()) {\n // Dont bother setting set-cookie headers, we will do that later\n if (name === SET_COOKIE) {\n continue;\n }\n res.setHeader(name, value);\n }\n // Check if there were any set-cookie headers and if so set them as an array\n const cookies = webRes.headers.getSetCookie();\n if (cookies.length > 0) {\n res.setHeader(SET_COOKIE, cookies);\n }\n // This is our last chance to set headers so set the server timings header\n res.setServerTimingHeader();\n // Now check if there is a body in the webRes\n if (webRes.body !== null) {\n // NOTE1: pipeline will close the res when it is finished\n // NOTE2: You need to cast as ReadableStream<Uint8Array> or TS will complain\n await streams\n .pipeline(webRes.body, res)\n .catch((e) => {\n // We can't do anything else here because either:\n // - the stream is closed which means we can't send back an error\n // - we have an internal error, but we have already started streaming\n // so we can't do anything\n bs.warn(\"ssrEndpoint had this error during rendering: (%s)\", e);\n });\n }\n}\n// Exported functions here\n// The default function is called when the bundle script is being built\nexport default (args) => {\n return {\n name: ADAPTER_NAME,\n hooks: {\n \"astro:config:done\": ({ setAdapter }) => {\n setAdapter({\n name: ADAPTER_NAME,\n serverEntrypoint: \"@bs-core/astro\",\n // previewEntrypoint: '@bs-core/astro',\n args,\n exports: [],\n supportedAstroFeatures: {\n hybridOutput: \"stable\",\n staticOutput: \"stable\",\n serverOutput: \"stable\",\n assets: {\n supportKind: \"stable\",\n isSharpCompatible: false,\n isSquooshCompatible: false,\n },\n },\n });\n },\n \"astro:build:done\": async () => {\n // We could update the bundle file here if needed\n },\n },\n };\n};\n// We need a createExports() exported or Astro will complain\nexport const createExports = () => {\n return {};\n};\n// This function that will be called when the bundled script is run\nexport const start = async (manifest, options) => {\n // Create the app first before doing anything else\n _app = new App(manifest);\n // Setup the config for the HTTP server\n let httpConfig = {\n keepAliveTimeout: options.keepAliveTimeout,\n headerTimeout: options.headerTimeout,\n defaultRouterBasePath: options.defaultRouterBasePath,\n healthcheckPath: options.healthcheckPath,\n healthcheckGoodRes: options.healthcheckGoodRes,\n healthcheckBadRes: options.healthcheckBadRes,\n };\n if (options.staticFilesPath !== undefined) {\n httpConfig.staticFileServer = {\n path: options.staticFilesPath,\n extraContentTypes: options.extraContentTypes,\n immutableRegExp: options.immutableRegexSrc,\n securityHeaders: options.securityHeaders,\n };\n }\n const enableHttps = bs.getConfigBool(HTTP_ENABLE_HTTPS, false);\n if (enableHttps) {\n httpConfig.enableHttps = true;\n httpConfig.httpsCertFile = bs.getConfigStr(HTTP_CERT_FILE);\n httpConfig.httpsKeyFile = bs.getConfigStr(HTTP_KEY_FILE);\n }\n const networkIf = bs.getConfigStr(HTTP_IF, \"127.0.0.1\");\n const networkPort = bs.getConfigNum(HTTP_PORT, 8080);\n // Create the HTTP server\n const httpServer = await bs.addHttpServer(networkIf, networkPort, httpConfig, false);\n // Call setupEntryPoint here in case you want to setup any default\n // middleware for the SSR endpoint\n if (options.setupEntryPoint !== undefined) {\n // NOTE: We expect an exported function named \"setup\"\n const { setup } = await import(pathToFileURL(options.setupEntryPoint).href);\n await setup();\n }\n // Add the main SSR route - NOTE: the path is not important since the\n // matcher will decide if there is a matching page\n httpServer.ssrRouter.all(\"/\", ssrEndpoint, {\n generateMatcher: matcher,\n etag: true,\n middlewareList: [Router.setLatencyMetricName(\"astro\")],\n });\n // Start the http server now!\n await httpServer.start();\n bs.startupMsg(\"Astro adapter is primed - party on dudes!!\");\n};\n//# sourceMappingURL=main.js.map"],"names":[],"mappings":";;;;;;AAAA;AAMA;AACA,MAAM,YAAY,GAAG,gBAAgB;AACrC,MAAM,cAAc,GAAG,gBAAgB;AACvC,MAAM,aAAa,GAAG,eAAe;AACrC,MAAM,iBAAiB,GAAG,mBAAmB;AAC7C,MAAM,OAAO,GAAG,SAAS;AACzB,MAAM,SAAS,GAAG,WAAW;AAC7B;AACA;AACA,IAAI,IAA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