@ms-cloudpack/app-server 0.20.27 → 0.20.29
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/lib/createRoutes.d.ts.map +1 -1
- package/lib/createRoutes.js +2 -0
- package/lib/createRoutes.js.map +1 -1
- package/package.json +10 -10
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRoutes.d.ts","sourceRoot":"","sources":["../src/createRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,EAML,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,aAAa,EAGnB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAYnF,UAAU,mBAAmB;IAC3B,GAAG,EAAE,OAAO,CAAC;IACb,8CAA8C;IAC9C,GAAG,EAAE,MAAM,CAAC;IACZ,+BAA+B;IAC/B,UAAU,EAAE,WAAW,CAAC;CACzB;AAcD;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,EAAE,OAAO,EAAE,2BAA2B,GAAG,IAAI,CA2DrG;AAED,wBAAsB,cAAc,CAClC,OAAO,EAAE,IAAI,CAAC,mBAAmB,EAAE,KAAK,CAAC,GAAG;IAC1C,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;IACd,KAAK,EAAE,aAAa,GAAG,cAAc,CAAC;CACvC,EACD,OAAO,EAAE,2BAA2B,GACnC,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"createRoutes.d.ts","sourceRoot":"","sources":["../src/createRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,EAML,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,aAAa,EAGnB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAYnF,UAAU,mBAAmB;IAC3B,GAAG,EAAE,OAAO,CAAC;IACb,8CAA8C;IAC9C,GAAG,EAAE,MAAM,CAAC;IACZ,+BAA+B;IAC/B,UAAU,EAAE,WAAW,CAAC;CACzB;AAcD;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,EAAE,OAAO,EAAE,2BAA2B,GAAG,IAAI,CA2DrG;AAED,wBAAsB,cAAc,CAClC,OAAO,EAAE,IAAI,CAAC,mBAAmB,EAAE,KAAK,CAAC,GAAG;IAC1C,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;IACd,KAAK,EAAE,aAAa,GAAG,cAAc,CAAC;CACvC,EACD,OAAO,EAAE,2BAA2B,GACnC,OAAO,CAAC,IAAI,CAAC,CAqDf"}
|
package/lib/createRoutes.js
CHANGED
|
@@ -95,6 +95,7 @@ export async function _handleRequest(options, context) {
|
|
|
95
95
|
res.header('Access-Control-Allow-Origin', '*');
|
|
96
96
|
let rendered;
|
|
97
97
|
if (isBootstrap) {
|
|
98
|
+
const { deferScripts } = route;
|
|
98
99
|
// Render the bootstrap script
|
|
99
100
|
rendered = await renderBootstrapRoute({
|
|
100
101
|
session,
|
|
@@ -103,6 +104,7 @@ export async function _handleRequest(options, context) {
|
|
|
103
104
|
inlineScripts,
|
|
104
105
|
entryScripts,
|
|
105
106
|
requestPath: req.path,
|
|
107
|
+
deferScripts,
|
|
106
108
|
});
|
|
107
109
|
}
|
|
108
110
|
else {
|
package/lib/createRoutes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRoutes.js","sourceRoot":"","sources":["../src/createRoutes.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,EAChB,YAAY,EAC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CAAC,MAAM,gBAAgB,EAAE,CAAC,CAAC,CAAC;IAEhF,yDAAyD;IACzD,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;IACxC,GAAG,CAAC,MAAM,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAC;IAE/C,IAAI,QAA6C,CAAC;IAElD,IAAI,WAAW,EAAE,CAAC;QAChB,8BAA8B;QAC9B,QAAQ,GAAG,MAAM,oBAAoB,CAAC;YACpC,OAAO;YACP,SAAS;YACT,aAAa;YACb,aAAa;YACb,YAAY;YACZ,WAAW,EAAE,GAAG,CAAC,IAAI;SACtB,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,kCAAkC;QAClC,QAAQ,GAAG,MAAM,WAAW,CAAC,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,YAAY,EAAE,EAAE,OAAO,CAAC,CAAC;IAC7G,CAAC;IAED,IAAI,QAAQ,EAAE,CAAC;QACb,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,CAAC;IAC1F,CAAC;IAED,OAAO,CAAC,KAAK,CAAC,wBAAwB,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import type { EnsurePackageBundledContext } from '@ms-cloudpack/api-server';\nimport {\n isBootstrapRoute,\n isProxyRoute,\n isRedirectRoute,\n isRenderedRoute,\n isStaticRoute,\n type BootstrapRoute,\n type PackageJson,\n type RenderedRoute,\n type ExpandedRenderFunctionResult,\n type RequestHandler,\n} from '@ms-cloudpack/common-types';\nimport type { Express, Request, Response } from '@ms-cloudpack/create-express-app';\nimport { express } from '@ms-cloudpack/create-express-app';\nimport { createImportMap } from '@ms-cloudpack/import-map';\nimport { slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\nimport { getInlineScripts } from '@ms-cloudpack/inline-scripts';\nimport { renderRoute } from './renderRoute/renderRoute.js';\nimport { renderBootstrapRoute } from './renderBootstrapRoute.js';\nimport { createPageSessionContext } from './createPageSessionContext.js';\nimport { parseRouteMatch } from './parseRouteMatch.js';\nimport { createProxyMiddleware } from 'http-proxy-middleware';\n\ninterface CreateRoutesOptions {\n app: Express;\n /** Base app server URL, including the port */\n url: string;\n /** package.json for the app */\n definition: PackageJson;\n}\n\n/** Express middleware that will match the domain to continue the route execution. */\nfunction matchDomain(domain?: RegExp): RequestHandler {\n return (req, _, next) => {\n if (!domain || domain.test(req.hostname)) {\n return next();\n }\n\n // Jump to next route.\n return next('route');\n };\n}\n\n/**\n * Creates the routes for the express app based on the config.\n */\nexport function createRoutes(options: CreateRoutesOptions, context: EnsurePackageBundledContext): void {\n const { app, ...otherOptions } = options;\n const { session } = context;\n const { config } = session;\n const { appPath } = config;\n const routes = [...(session.config.routes || [])];\n\n if (!options.definition.name) {\n throw new Error('Name field is required in the package.json file.');\n }\n\n if (!routes.length) {\n // If no routes have been defined, register a default (no path) route only.\n // This will render default HTML which includes the app's default path ('.') from the import map.\n routes.push({\n match: '/',\n // sourcePath and requestPath are required by types but unused in this case.\n entry: [{ importPath: '.', sourcePath: '' }],\n });\n }\n\n for (const route of routes) {\n const matches = Array.isArray(route.match) ? route.match : [route.match];\n\n for (const match of matches) {\n const { pathMatch, domainMatch } = parseRouteMatch(match);\n\n if (isStaticRoute(route)) {\n const resolvedPath = path.resolve(appPath, route.staticPath);\n app.use(\n pathMatch,\n matchDomain(domainMatch),\n // Express v5 default behavior with dotfiles is to ignore them (404), even if the original\n // path had a directory with a leading dot...\n express.static(resolvedPath, { dotfiles: slash(resolvedPath).includes('/.') ? 'allow' : 'deny' }),\n );\n } else if (isRedirectRoute(route)) {\n app.get(pathMatch, matchDomain(domainMatch), (req, res) => {\n console.debug(`App server: redirect ${req.url} to ${route.redirectTo}`);\n res.redirect(route.redirectTo);\n });\n } else if (isRenderedRoute(route) || isBootstrapRoute(route)) {\n app.get(pathMatch, matchDomain(domainMatch), (req, res) => {\n _handleRequest({ ...otherOptions, route, req, res }, context).catch((err) => {\n console.error((err as Error)?.stack || err);\n res.status(500).send(`Error loading app: ${err}`);\n });\n });\n } else if (isProxyRoute(route)) {\n app.use(\n pathMatch,\n matchDomain(domainMatch),\n createProxyMiddleware({ target: route.proxyTo, ...route }) as RequestHandler,\n );\n } else {\n throw new Error(`Unknown route: ${JSON.stringify(route)}`);\n }\n }\n }\n}\n\nexport async function _handleRequest(\n options: Omit<CreateRoutesOptions, 'app'> & {\n req: Request;\n res: Response;\n route: RenderedRoute | BootstrapRoute;\n },\n context: EnsurePackageBundledContext,\n): Promise<void> {\n const { req, res, definition, route, url: baseUrl } = options;\n const { session } = context;\n const isBootstrap = isBootstrapRoute(route);\n console.debug(`App server: ${isBootstrap ? 'bootstrap' : 'render'} request ${req.path}`);\n\n // Build the import map if it hasn't been built yet for this session version.\n // (TS can't infer that urls.bundleServer is set on session from a check above.)\n const importMap = (session.importMap ??= await createImportMap({ targetEnvironment: 'browser' }, context));\n\n // For production mode, no overlay script.\n const overlayScript = session.config.mode !== 'production' ? importMap.imports['@ms-cloudpack/overlay'] : undefined;\n\n // Get all entry scripts for the route.\n const entryScripts = route.entry?.map(\n (entry) => importMap.imports[slash(path.join(definition.name, entry.importPath))],\n );\n\n const pageSessionContextScript =\n `window.__cloudpack ??= {};\\n` +\n `window.__cloudpack.pageSessionContext = ${JSON.stringify(createPageSessionContext(session, req.path))};`;\n\n // Add the pageSessionContext to the top of the inlineScripts\n const inlineScripts = [pageSessionContextScript, ...(await getInlineScripts())];\n\n // Set the appropriate Cloudpack headers in the response.\n res.header('Cache-Control', 'no-cache');\n res.header('Access-Control-Allow-Origin', '*');\n\n let rendered: ExpandedRenderFunctionResult | null;\n\n if (isBootstrap) {\n // Render the bootstrap script\n rendered = await renderBootstrapRoute({\n session,\n importMap,\n overlayScript,\n inlineScripts,\n entryScripts,\n requestPath: req.path,\n });\n } else {\n // HTML or custom script rendering\n rendered = await renderRoute({ ...options, baseUrl, overlayScript, inlineScripts, entryScripts }, context);\n }\n\n if (rendered) {\n res.type(rendered.contentType).status(rendered.statusCode).send(rendered.content).end();\n }\n\n console.debug(`App server: response ${req.path}`);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"createRoutes.js","sourceRoot":"","sources":["../src/createRoutes.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,eAAe,EACf,aAAa,GAMd,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,OAAO,EAAE,MAAM,kCAAkC,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAC1D,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAU9D,qFAAqF;AACrF,SAAS,WAAW,CAAC,MAAe;IAClC,OAAO,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE;QACtB,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;YACzC,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,sBAAsB;QACtB,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,M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AI,EAAE,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import type { EnsurePackageBundledContext } from '@ms-cloudpack/api-server';\nimport {\n isBootstrapRoute,\n isProxyRoute,\n isRedirectRoute,\n isRenderedRoute,\n isStaticRoute,\n type BootstrapRoute,\n type PackageJson,\n type RenderedRoute,\n type ExpandedRenderFunctionResult,\n type RequestHandler,\n} from '@ms-cloudpack/common-types';\nimport type { Express, Request, Response } from '@ms-cloudpack/create-express-app';\nimport { express } from '@ms-cloudpack/create-express-app';\nimport { createImportMap } from '@ms-cloudpack/import-map';\nimport { slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\nimport { getInlineScripts } from '@ms-cloudpack/inline-scripts';\nimport { renderRoute } from './renderRoute/renderRoute.js';\nimport { renderBootstrapRoute } from './renderBootstrapRoute.js';\nimport { createPageSessionContext } from './createPageSessionContext.js';\nimport { parseRouteMatch } from './parseRouteMatch.js';\nimport { createProxyMiddleware } from 'http-proxy-middleware';\n\ninterface CreateRoutesOptions {\n app: Express;\n /** Base app server URL, including the port */\n url: string;\n /** package.json for the app */\n definition: PackageJson;\n}\n\n/** Express middleware that will match the domain to continue the route execution. */\nfunction matchDomain(domain?: RegExp): RequestHandler {\n return (req, _, next) => {\n if (!domain || domain.test(req.hostname)) {\n return next();\n }\n\n // Jump to next route.\n return next('route');\n };\n}\n\n/**\n * Creates the routes for the express app based on the config.\n */\nexport function createRoutes(options: CreateRoutesOptions, context: EnsurePackageBundledContext): void {\n const { app, ...otherOptions } = options;\n const { session } = context;\n const { config } = session;\n const { appPath } = config;\n const routes = [...(session.config.routes || [])];\n\n if (!options.definition.name) {\n throw new Error('Name field is required in the package.json file.');\n }\n\n if (!routes.length) {\n // If no routes have been defined, register a default (no path) route only.\n // This will render default HTML which includes the app's default path ('.') from the import map.\n routes.push({\n match: '/',\n // sourcePath and requestPath are required by types but unused in this case.\n entry: [{ importPath: '.', sourcePath: '' }],\n });\n }\n\n for (const route of routes) {\n const matches = Array.isArray(route.match) ? route.match : [route.match];\n\n for (const match of matches) {\n const { pathMatch, domainMatch } = parseRouteMatch(match);\n\n if (isStaticRoute(route)) {\n const resolvedPath = path.resolve(appPath, route.staticPath);\n app.use(\n pathMatch,\n matchDomain(domainMatch),\n // Express v5 default behavior with dotfiles is to ignore them (404), even if the original\n // path had a directory with a leading dot...\n express.static(resolvedPath, { dotfiles: slash(resolvedPath).includes('/.') ? 'allow' : 'deny' }),\n );\n } else if (isRedirectRoute(route)) {\n app.get(pathMatch, matchDomain(domainMatch), (req, res) => {\n console.debug(`App server: redirect ${req.url} to ${route.redirectTo}`);\n res.redirect(route.redirectTo);\n });\n } else if (isRenderedRoute(route) || isBootstrapRoute(route)) {\n app.get(pathMatch, matchDomain(domainMatch), (req, res) => {\n _handleRequest({ ...otherOptions, route, req, res }, context).catch((err) => {\n console.error((err as Error)?.stack || err);\n res.status(500).send(`Error loading app: ${err}`);\n });\n });\n } else if (isProxyRoute(route)) {\n app.use(\n pathMatch,\n matchDomain(domainMatch),\n createProxyMiddleware({ target: route.proxyTo, ...route }) as RequestHandler,\n );\n } else {\n throw new Error(`Unknown route: ${JSON.stringify(route)}`);\n }\n }\n }\n}\n\nexport async function _handleRequest(\n options: Omit<CreateRoutesOptions, 'app'> & {\n req: Request;\n res: Response;\n route: RenderedRoute | BootstrapRoute;\n },\n context: EnsurePackageBundledContext,\n): Promise<void> {\n const { req, res, definition, route, url: baseUrl } = options;\n const { session } = context;\n const isBootstrap = isBootstrapRoute(route);\n console.debug(`App server: ${isBootstrap ? 'bootstrap' : 'render'} request ${req.path}`);\n\n // Build the import map if it hasn't been built yet for this session version.\n // (TS can't infer that urls.bundleServer is set on session from a check above.)\n const importMap = (session.importMap ??= await createImportMap({ targetEnvironment: 'browser' }, context));\n\n // For production mode, no overlay script.\n const overlayScript = session.config.mode !== 'production' ? importMap.imports['@ms-cloudpack/overlay'] : undefined;\n\n // Get all entry scripts for the route.\n const entryScripts = route.entry?.map(\n (entry) => importMap.imports[slash(path.join(definition.name, entry.importPath))],\n );\n\n const pageSessionContextScript =\n `window.__cloudpack ??= {};\\n` +\n `window.__cloudpack.pageSessionContext = ${JSON.stringify(createPageSessionContext(session, req.path))};`;\n\n // Add the pageSessionContext to the top of the inlineScripts\n const inlineScripts = [pageSessionContextScript, ...(await getInlineScripts())];\n\n // Set the appropriate Cloudpack headers in the response.\n res.header('Cache-Control', 'no-cache');\n res.header('Access-Control-Allow-Origin', '*');\n\n let rendered: ExpandedRenderFunctionResult | null;\n\n if (isBootstrap) {\n const { deferScripts } = route;\n // Render the bootstrap script\n rendered = await renderBootstrapRoute({\n session,\n importMap,\n overlayScript,\n inlineScripts,\n entryScripts,\n requestPath: req.path,\n deferScripts,\n });\n } else {\n // HTML or custom script rendering\n rendered = await renderRoute({ ...options, baseUrl, overlayScript, inlineScripts, entryScripts }, context);\n }\n\n if (rendered) {\n res.type(rendered.contentType).status(rendered.statusCode).send(rendered.content).end();\n }\n\n console.debug(`App server: response ${req.path}`);\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.29",
|
|
4
4
|
"description": "Internal app server for Cloudpack",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,23 +14,23 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/api-server": "^0.65.
|
|
18
|
-
"@ms-cloudpack/bundle-server": "^0.8.
|
|
19
|
-
"@ms-cloudpack/common-types": "^0.32.
|
|
20
|
-
"@ms-cloudpack/create-express-app": "^1.10.
|
|
17
|
+
"@ms-cloudpack/api-server": "^0.65.5",
|
|
18
|
+
"@ms-cloudpack/bundle-server": "^0.8.29",
|
|
19
|
+
"@ms-cloudpack/common-types": "^0.32.2",
|
|
20
|
+
"@ms-cloudpack/create-express-app": "^1.10.63",
|
|
21
21
|
"@ms-cloudpack/environment": "^0.1.1",
|
|
22
|
-
"@ms-cloudpack/import-map": "^0.10.
|
|
23
|
-
"@ms-cloudpack/inline-scripts": "^0.2.
|
|
22
|
+
"@ms-cloudpack/import-map": "^0.10.59",
|
|
23
|
+
"@ms-cloudpack/inline-scripts": "^0.2.51",
|
|
24
24
|
"@ms-cloudpack/path-string-parsing": "^1.2.7",
|
|
25
|
-
"@ms-cloudpack/path-utilities": "^3.1.
|
|
25
|
+
"@ms-cloudpack/path-utilities": "^3.1.34",
|
|
26
26
|
"@ms-cloudpack/task-reporter": "^0.17.4",
|
|
27
27
|
"@ms-cloudpack/worker-pool": "^0.4.1",
|
|
28
28
|
"happy-dom": "^18.0.0",
|
|
29
29
|
"http-proxy-middleware": "^2.0.6"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@ms-cloudpack/common-types-browser": "^0.6.
|
|
33
|
-
"@ms-cloudpack/config": "^0.38.
|
|
32
|
+
"@ms-cloudpack/common-types-browser": "^0.6.4",
|
|
33
|
+
"@ms-cloudpack/config": "^0.38.18",
|
|
34
34
|
"@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
|
|
35
35
|
"@ms-cloudpack/scripts": "^0.0.1",
|
|
36
36
|
"@ms-cloudpack/test-utilities": "^0.5.0"
|