@ms-cloudpack/app-server 0.20.34 → 0.20.36
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { javascriptRegex, sourceRegex, typescriptRegex } from '@ms-cloudpack/path-utilities';
|
|
2
2
|
import fsPromises from 'fs/promises';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { getDefaultHtmlResponse } from './getDefaultHtmlResponse.js';
|
|
@@ -20,7 +20,7 @@ export async function renderRoute(options, context) {
|
|
|
20
20
|
// Prefer serverEntry if given
|
|
21
21
|
if (route.serverEntry) {
|
|
22
22
|
const serverEntryPath = path.resolve(appPath, route.serverEntry.sourcePath);
|
|
23
|
-
if (
|
|
23
|
+
if (sourceRegex.test(serverEntryPath)) {
|
|
24
24
|
// Server entry is a source file, so bundle it
|
|
25
25
|
const serverEntryResult = await bundleServerEntry({ definition: options.definition, handleCustomRenderError, serverEntryPath }, context);
|
|
26
26
|
if (typeof serverEntryResult === 'string') {
|
|
@@ -56,7 +56,7 @@ export async function renderRoute(options, context) {
|
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
else if (
|
|
59
|
+
else if (javascriptRegex.test(scriptExt)) {
|
|
60
60
|
// JS file (potentially was bundled): run its function
|
|
61
61
|
rawResult = await renderCustomScript({ ...options, renderScriptPath, handleCustomRenderError }, context);
|
|
62
62
|
}
|
|
@@ -65,7 +65,7 @@ export async function renderRoute(options, context) {
|
|
|
65
65
|
// content type logic, but this probably indicates there's been a misunderstanding and we
|
|
66
66
|
// should direct them to use a more appropriate type of route instead.
|
|
67
67
|
let message = 'is not a supported file type. ';
|
|
68
|
-
if (
|
|
68
|
+
if (typescriptRegex.test(scriptExt)) {
|
|
69
69
|
message += 'Use `serverEntry` for TS files.';
|
|
70
70
|
}
|
|
71
71
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"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AAgD;IAChD,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YAC1D,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACjC,CAAC;IACH,CAAC;IAED,wFAAwF;IACxF,IACE,MAAM,CAAC,aAAa,KAAK,KAAK;QAC9B,MAAM,CAAC,UAAU,KAAK,GAAG;QACzB,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ;QAClC,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,EAC9B,CAAC;QACD,IAAI,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAClC,qCAAqC;YACrC,MAAM,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC;aAAM,CAAC;YACN,yFAAyF;YACzF,gGAAgG;YAChG,OAAO,uBAAuB,CAAC;gBAC7B,OAAO,EACL,qFAAqF;oBACrF,wFAAwF;aAC3F,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { EnsurePackageBundledContext } from '@ms-cloudpack/api-server';\nimport type { ExpandedRenderFunctionResult, RenderFunctionResult } from '@ms-cloudpack/common-types';\nimport { javascriptExtensions, sourceExtensions, typescriptExtensions } from '@ms-cloudpack/path-utilities';\nimport fsPromises from 'fs/promises';\nimport path from 'path';\nimport { getDefaultHtmlResponse } from './getDefaultHtmlResponse.js';\nimport { getCustomRenderErrorHandler } from './getCustomRenderErrorHandler.js';\nimport { injectScripts } from './injectScripts.js';\nimport { isHtmlAcceptable, isHtmlType } from './isHtmlAcceptable.js';\nimport { bundleServerEntry } from './ssr/bundleServerEntry.js';\nimport { renderCustomScript } from './ssr/renderCustomScript.js';\nimport type { RenderRouteOptions } from '../types/RenderRouteOptions.js';\n\n/**\n * Get the response for the given route. If the route has a custom render script, use that.\n * Returns an error response if the file referenced by the script doesn't exist or throws an error.\n */\nexport async function renderRoute(\n options: RenderRouteOptions,\n context: EnsurePackageBundledContext,\n): Promise<ExpandedRenderFunctionResult | null> {\n const { route } = options;\n const { appPath, features } = context.session.config;\n\n const handleCustomRenderError = getCustomRenderErrorHandler({ req: options.req, route, appPath });\n\n /** Absolute custom render script path (if any). For `serverEntry`, this is the bundled path. */\n let renderScriptPath: string | undefined;\n\n // Prefer serverEntry if given\n if (route.serverEntry) {\n const serverEntryPath = path.resolve(appPath, route.serverEntry.sourcePath);\n\n if (sourceExtensions.includes(path.extname(serverEntryPath).toLowerCase())) {\n // Server entry is a source file, so bundle it\n const serverEntryResult = await bundleServerEntry(\n { definition: options.definition, handleCustomRenderError, serverEntryPath },\n context,\n );\n if (typeof serverEntryResult === 'string') {\n renderScriptPath = serverEntryResult;\n } else {\n // This means there was an error\n return serverEntryResult;\n }\n } else {\n // HTML or something, so use it as-is\n renderScriptPath = path.resolve(appPath, serverEntryPath);\n }\n } else {\n // Fall back to renderScript or the default renderer\n // eslint-disable-next-line etc/no-deprecated\n renderScriptPath = route.renderScript && path.resolve(appPath, route.renderScript);\n }\n\n // Get an initial result from either the renderScript/serverEntry or default handler.\n let rawResult: RenderFunctionResult;\n if (renderScriptPath) {\n const scriptExt = path.extname(renderScriptPath).toLowerCase();\n if (scriptExt === '.html' || scriptExt === '.htm') {\n // HTML file: just read it\n try {\n rawResult = (await fsPromises.readFile(renderScriptPath, 'utf8')) || '';\n } catch (e) {\n return handleCustomRenderError({\n message: `could not be read:\\n${(e as Error).message || e}`,\n });\n }\n } else if (javascriptExtensions.includes(scriptExt)) {\n // JS file (potentially was bundled): run its function\n rawResult = await renderCustomScript({ ...options, renderScriptPath, handleCustomRenderError }, context);\n } else {\n // Other file type: error. Technically we could just serve it as-is following the non-HTML\n // content type logic, but this probably indicates there's been a misunderstanding and we\n // should direct them to use a more appropriate type of route instead.\n let message = 'is not a supported file type. ';\n if (typescriptExtensions.includes(scriptExt)) {\n message += 'Use `serverEntry` for TS files.';\n } else {\n message +=\n `\\n\\nSupported file types are ${route.serverEntry ? 'TS, JS, or HTML' : 'JS or HTML (use `serverEntry` for TS)'}. ` +\n 'If you want to serve a static non-HTML file, use a route with `staticPath`, or a `serverEntry` ' +\n 'which returns the file content for advanced scenarios.';\n }\n return handleCustomRenderError({ message });\n }\n } else {\n rawResult = getDefaultHtmlResponse(options);\n }\n\n if (rawResult === null) {\n if (features?.enableSSR) {\n // null is not valid in new SSR mode\n // (technically this error came from the bundled script, but that's irrelevant to the issue)\n return handleCustomRenderError({\n message: `returned null, which is not supported with the \"enableSSR\" feature.`,\n });\n }\n // This means the custom renderScript fully handled the request.\n return null;\n }\n\n if (rawResult === undefined) {\n return handleCustomRenderError({\n message: `returned undefined. Ensure that the function returns the content to be rendered.`,\n });\n }\n\n const result: ExpandedRenderFunctionResult = {\n contentType: 'text/html',\n statusCode: 200,\n ...(typeof rawResult === 'string' ? { content: rawResult } : rawResult),\n };\n\n // Add any custom headers (for success or error)\n if (result.headers) {\n for (const [key, value] of Object.entries(result.headers)) {\n options.res.header(key, value);\n }\n }\n\n // If it's HTML and a success statusCode, inject the import map and appropriate scripts.\n if (\n result.injectScripts !== false &&\n result.statusCode === 200 &&\n typeof result.content === 'string' &&\n isHtmlType(result.contentType)\n ) {\n if (isHtmlAcceptable(options.req)) {\n // Inject the import map and scripts.\n await injectScripts(options, result, context);\n } else {\n // It appears we're accidentally returning HTML when a different file type was requested.\n // This could happen for overly broad route matches such as '*', or if no routes are configured.\n return handleCustomRenderError({\n message:\n 'returned HTML, but this appears to be a non-HTML request. This is likely due to an ' +\n 'overly broad match or other misconfiguration of server.routes in the cloudpack config.',\n });\n }\n }\n\n return result;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"renderRoute.js","sourceRoot":"","sources":["../../src/renderRoute/renderRoute.ts"],"names":[],"mappings":"AA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O,CAAC,EAAE,CAAC;YAC1D,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACjC,CAAC;IACH,CAAC;IAED,wFAAwF;IACxF,IACE,MAAM,CAAC,aAAa,KAAK,KAAK;QAC9B,MAAM,CAAC,UAAU,KAAK,GAAG;QACzB,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ;QAClC,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,EAC9B,CAAC;QACD,IAAI,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAClC,qCAAqC;YACrC,MAAM,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC;aAAM,CAAC;YACN,yFAAyF;YACzF,gGAAgG;YAChG,OAAO,uBAAuB,CAAC;gBAC7B,OAAO,EACL,qFAAqF;oBACrF,wFAAwF;aAC3F,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { EnsurePackageBundledContext } from '@ms-cloudpack/api-server';\nimport type { ExpandedRenderFunctionResult, RenderFunctionResult } from '@ms-cloudpack/common-types';\nimport { javascriptRegex, sourceRegex, typescriptRegex } from '@ms-cloudpack/path-utilities';\nimport fsPromises from 'fs/promises';\nimport path from 'path';\nimport { getDefaultHtmlResponse } from './getDefaultHtmlResponse.js';\nimport { getCustomRenderErrorHandler } from './getCustomRenderErrorHandler.js';\nimport { injectScripts } from './injectScripts.js';\nimport { isHtmlAcceptable, isHtmlType } from './isHtmlAcceptable.js';\nimport { bundleServerEntry } from './ssr/bundleServerEntry.js';\nimport { renderCustomScript } from './ssr/renderCustomScript.js';\nimport type { RenderRouteOptions } from '../types/RenderRouteOptions.js';\n\n/**\n * Get the response for the given route. If the route has a custom render script, use that.\n * Returns an error response if the file referenced by the script doesn't exist or throws an error.\n */\nexport async function renderRoute(\n options: RenderRouteOptions,\n context: EnsurePackageBundledContext,\n): Promise<ExpandedRenderFunctionResult | null> {\n const { route } = options;\n const { appPath, features } = context.session.config;\n\n const handleCustomRenderError = getCustomRenderErrorHandler({ req: options.req, route, appPath });\n\n /** Absolute custom render script path (if any). For `serverEntry`, this is the bundled path. */\n let renderScriptPath: string | undefined;\n\n // Prefer serverEntry if given\n if (route.serverEntry) {\n const serverEntryPath = path.resolve(appPath, route.serverEntry.sourcePath);\n\n if (sourceRegex.test(serverEntryPath)) {\n // Server entry is a source file, so bundle it\n const serverEntryResult = await bundleServerEntry(\n { definition: options.definition, handleCustomRenderError, serverEntryPath },\n context,\n );\n if (typeof serverEntryResult === 'string') {\n renderScriptPath = serverEntryResult;\n } else {\n // This means there was an error\n return serverEntryResult;\n }\n } else {\n // HTML or something, so use it as-is\n renderScriptPath = path.resolve(appPath, serverEntryPath);\n }\n } else {\n // Fall back to renderScript or the default renderer\n // eslint-disable-next-line etc/no-deprecated\n renderScriptPath = route.renderScript && path.resolve(appPath, route.renderScript);\n }\n\n // Get an initial result from either the renderScript/serverEntry or default handler.\n let rawResult: RenderFunctionResult;\n if (renderScriptPath) {\n const scriptExt = path.extname(renderScriptPath).toLowerCase();\n if (scriptExt === '.html' || scriptExt === '.htm') {\n // HTML file: just read it\n try {\n rawResult = (await fsPromises.readFile(renderScriptPath, 'utf8')) || '';\n } catch (e) {\n return handleCustomRenderError({\n message: `could not be read:\\n${(e as Error).message || e}`,\n });\n }\n } else if (javascriptRegex.test(scriptExt)) {\n // JS file (potentially was bundled): run its function\n rawResult = await renderCustomScript({ ...options, renderScriptPath, handleCustomRenderError }, context);\n } else {\n // Other file type: error. Technically we could just serve it as-is following the non-HTML\n // content type logic, but this probably indicates there's been a misunderstanding and we\n // should direct them to use a more appropriate type of route instead.\n let message = 'is not a supported file type. ';\n if (typescriptRegex.test(scriptExt)) {\n message += 'Use `serverEntry` for TS files.';\n } else {\n message +=\n `\\n\\nSupported file types are ${route.serverEntry ? 'TS, JS, or HTML' : 'JS or HTML (use `serverEntry` for TS)'}. ` +\n 'If you want to serve a static non-HTML file, use a route with `staticPath`, or a `serverEntry` ' +\n 'which returns the file content for advanced scenarios.';\n }\n return handleCustomRenderError({ message });\n }\n } else {\n rawResult = getDefaultHtmlResponse(options);\n }\n\n if (rawResult === null) {\n if (features?.enableSSR) {\n // null is not valid in new SSR mode\n // (technically this error came from the bundled script, but that's irrelevant to the issue)\n return handleCustomRenderError({\n message: `returned null, which is not supported with the \"enableSSR\" feature.`,\n });\n }\n // This means the custom renderScript fully handled the request.\n return null;\n }\n\n if (rawResult === undefined) {\n return handleCustomRenderError({\n message: `returned undefined. Ensure that the function returns the content to be rendered.`,\n });\n }\n\n const result: ExpandedRenderFunctionResult = {\n contentType: 'text/html',\n statusCode: 200,\n ...(typeof rawResult === 'string' ? { content: rawResult } : rawResult),\n };\n\n // Add any custom headers (for success or error)\n if (result.headers) {\n for (const [key, value] of Object.entries(result.headers)) {\n options.res.header(key, value);\n }\n }\n\n // If it's HTML and a success statusCode, inject the import map and appropriate scripts.\n if (\n result.injectScripts !== false &&\n result.statusCode === 200 &&\n typeof result.content === 'string' &&\n isHtmlType(result.contentType)\n ) {\n if (isHtmlAcceptable(options.req)) {\n // Inject the import map and scripts.\n await injectScripts(options, result, context);\n } else {\n // It appears we're accidentally returning HTML when a different file type was requested.\n // This could happen for overly broad route matches such as '*', or if no routes are configured.\n return handleCustomRenderError({\n message:\n 'returned HTML, but this appears to be a non-HTML request. This is likely due to an ' +\n 'overly broad match or other misconfiguration of server.routes in the cloudpack config.',\n });\n }\n }\n\n return result;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/app-server",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.36",
|
|
4
4
|
"description": "Internal app server for Cloudpack",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -24,15 +24,15 @@
|
|
|
24
24
|
"test": "cloudpack-scripts test"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@ms-cloudpack/api-server": "^0.65.
|
|
28
|
-
"@ms-cloudpack/bundle-server": "^0.8.
|
|
29
|
-
"@ms-cloudpack/common-types": "^0.33.
|
|
30
|
-
"@ms-cloudpack/create-express-app": "^1.10.
|
|
27
|
+
"@ms-cloudpack/api-server": "^0.65.12",
|
|
28
|
+
"@ms-cloudpack/bundle-server": "^0.8.36",
|
|
29
|
+
"@ms-cloudpack/common-types": "^0.33.1",
|
|
30
|
+
"@ms-cloudpack/create-express-app": "^1.10.66",
|
|
31
31
|
"@ms-cloudpack/environment": "^0.1.1",
|
|
32
|
-
"@ms-cloudpack/import-map": "^0.10.
|
|
33
|
-
"@ms-cloudpack/inline-scripts": "^0.2.
|
|
32
|
+
"@ms-cloudpack/import-map": "^0.10.62",
|
|
33
|
+
"@ms-cloudpack/inline-scripts": "^0.2.58",
|
|
34
34
|
"@ms-cloudpack/path-string-parsing": "^1.2.7",
|
|
35
|
-
"@ms-cloudpack/path-utilities": "^3.2.
|
|
35
|
+
"@ms-cloudpack/path-utilities": "^3.2.2",
|
|
36
36
|
"@ms-cloudpack/task-reporter": "^0.17.4",
|
|
37
37
|
"@ms-cloudpack/worker-pool": "^0.4.1",
|
|
38
38
|
"happy-dom": "^20.0.2",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@ms-cloudpack/common-types-browser": "^0.6.4",
|
|
43
|
-
"@ms-cloudpack/config": "^0.38.
|
|
43
|
+
"@ms-cloudpack/config": "^0.38.21",
|
|
44
44
|
"@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
|
|
45
45
|
"@ms-cloudpack/scripts": "^0.0.1",
|
|
46
46
|
"@ms-cloudpack/test-utilities": "^0.5.0"
|