@ms-cloudpack/app-server 0.10.3 → 0.10.4
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/createBootstrapRouteHandler.d.ts.map +1 -1
- package/lib/createBootstrapRouteHandler.js +4 -4
- package/lib/createBootstrapRouteHandler.js.map +1 -1
- package/lib/createPageSessionContext.d.ts +7 -0
- package/lib/createPageSessionContext.d.ts.map +1 -0
- package/lib/createPageSessionContext.js +12 -0
- package/lib/createPageSessionContext.js.map +1 -0
- package/lib/createRoutes.d.ts.map +1 -1
- package/lib/createRoutes.js +8 -6
- package/lib/createRoutes.js.map +1 -1
- package/lib/inlineScripts/bootstrap.inline.js +3 -5
- package/lib/inlineScripts/bootstrap.inline.js.map +1 -1
- package/lib/inlineScripts/getBrowserCacheRatio.inline.js +3 -16
- package/lib/inlineScripts/getBrowserCacheRatio.inline.js.map +1 -1
- package/lib/renderRoute/renderRoute.d.ts.map +1 -1
- package/lib/renderRoute/renderRoute.js +9 -0
- package/lib/renderRoute/renderRoute.js.map +1 -1
- package/lib/setHeaders.d.ts +1 -3
- package/lib/setHeaders.d.ts.map +1 -1
- package/lib/setHeaders.js +2 -8
- package/lib/setHeaders.js.map +1 -1
- package/package.json +9 -8
- package/lib/cookieNames.d.ts +0 -8
- package/lib/cookieNames.d.ts.map +0 -1
- package/lib/cookieNames.js +0 -9
- package/lib/cookieNames.js.map +0 -1
- package/lib/getCookies.d.ts +0 -6
- package/lib/getCookies.d.ts.map +0 -1
- package/lib/getCookies.js +0 -14
- package/lib/getCookies.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createBootstrapRouteHandler.d.ts","sourceRoot":"","sources":["../src/createBootstrapRouteHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"createBootstrapRouteHandler.d.ts","sourceRoot":"","sources":["../src/createBootstrapRouteHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AAKxD,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAkB1E,KAAK,kCAAkC,GAAG,IAAI,CAC5C,OAAO,EACP,UAAU,GAAG,oBAAoB,GAAG,eAAe,GAAG,SAAS,CAChE,CAAC;AAEF,wBAAgB,2BAA2B,CACzC,OAAO,EAAE;IACP,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,EACD,OAAO,EAAE,kCAAkC,UAE7B,OAAO,OAAO,QAAQ,UAqCrC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createImportMap } from '@ms-cloudpack/import-map';
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
import { getBoostrapInlineScript, getInlineScripts } from './inlineScripts/getInlineScripts.js';
|
|
4
|
-
import { getCookies } from './getCookies.js';
|
|
5
4
|
import { slash } from '@ms-cloudpack/path-string-parsing';
|
|
5
|
+
import { createPageSessionContext } from './createPageSessionContext.js';
|
|
6
6
|
const getBootstrapScript = async (input) => {
|
|
7
7
|
let bootstrapInlineScript = await getBoostrapInlineScript();
|
|
8
8
|
// Remove the export statement from the inline script, the bootstrap can not be an esm.
|
|
@@ -19,7 +19,7 @@ export function createBootstrapRouteHandler(options, context) {
|
|
|
19
19
|
(async () => {
|
|
20
20
|
const { definitionName, exportEntry } = options;
|
|
21
21
|
const { session } = context;
|
|
22
|
-
const { bundleServer: bundleServerUrl
|
|
22
|
+
const { bundleServer: bundleServerUrl } = session.urls;
|
|
23
23
|
if (!bundleServerUrl) {
|
|
24
24
|
// Sanity check...
|
|
25
25
|
throw new Error('The bundle server URL is not yet set in the session (this is a Cloudpack bug).');
|
|
@@ -30,12 +30,12 @@ export function createBootstrapRouteHandler(options, context) {
|
|
|
30
30
|
urls: { bundleServer: bundleServerUrl },
|
|
31
31
|
}));
|
|
32
32
|
const inlineScripts = await getInlineScripts();
|
|
33
|
-
const
|
|
33
|
+
const pageSessionContext = createPageSessionContext(session);
|
|
34
34
|
const entryScript = exportEntry && importMap.imports[slash(join(definitionName, exportEntry))];
|
|
35
35
|
const overlayScript = importMap.imports['@ms-cloudpack/overlay'];
|
|
36
36
|
const renderedContent = await getBootstrapScript({
|
|
37
37
|
importMap,
|
|
38
|
-
|
|
38
|
+
pageSessionContext,
|
|
39
39
|
overlayScript,
|
|
40
40
|
inlineScripts,
|
|
41
41
|
entryScript,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createBootstrapRouteHandler.js","sourceRoot":"","sources":["../src/createBootstrapRouteHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"createBootstrapRouteHandler.js","sourceRoot":"","sources":["../src/createBootstrapRouteHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAChG,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAG1D,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAEzE,MAAM,kBAAkB,GAAG,KAAK,EAAE,KAAqB,EAAE,EAAE;IACzD,IAAI,qBAAqB,GAAG,MAAM,uBAAuB,EAAE,CAAC;IAE5D,uFAAuF;IACvF,qBAAqB,GAAG,qBAAqB,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;IAExE,oCAAoC;IACpC,6CAA6C;IAC7C,OAAO;MACH,qBAAqB;mCACQ,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;GACrD,CAAC;AACJ,CAAC,CAAC;AAOF,MAAM,UAAU,2BAA2B,CACzC,OAGC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AAI,GAAG,CAAC,CAAC;YAC5C,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,sBAAsB,GAAG,EAAE,CAAC,CAAC;QAC3D,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { Context } from '@ms-cloudpack/api-server';\nimport { createImportMap } from '@ms-cloudpack/import-map';\nimport { join } from 'path';\nimport { getBoostrapInlineScript, getInlineScripts } from './inlineScripts/getInlineScripts.js';\nimport { slash } from '@ms-cloudpack/path-string-parsing';\nimport type { Request, Response } from '@ms-cloudpack/create-express-app';\nimport type { BootstrapInput } from '@ms-cloudpack/common-types-browser';\nimport { createPageSessionContext } from './createPageSessionContext.js';\n\nconst getBootstrapScript = async (input: BootstrapInput) => {\n let bootstrapInlineScript = await getBoostrapInlineScript();\n\n // Remove the export statement from the inline script, the bootstrap can not be an esm.\n bootstrapInlineScript = bootstrapInlineScript.replace('export {};', '');\n\n // Inject the boostrap inline script\n // Call the bootstrap function with the input\n return `\n ${bootstrapInlineScript}\n window.__cloudpack.bootstrap(${JSON.stringify(input)});\n `;\n};\n\ntype CreateBootstrapRouteHandlerContext = Pick<\n Context,\n 'packages' | 'packageImportPaths' | 'packageHashes' | 'session'\n>;\n\nexport function createBootstrapRouteHandler(\n options: {\n definitionName: string;\n exportEntry?: string;\n },\n context: CreateBootstrapRouteHandlerContext,\n) {\n return (_req: Request, res: Response) => {\n (async () => {\n const { definitionName, exportEntry } = options;\n\n const { session } = context;\n const { bundleServer: bundleServerUrl } = session.urls;\n\n if (!bundleServerUrl) {\n // Sanity check...\n throw new Error('The bundle server URL is not yet set in the session (this is a Cloudpack bug).');\n }\n\n const importMap = (session.importMap ??= await createImportMap({\n ...context,\n ...session,\n urls: { bundleServer: bundleServerUrl },\n }));\n\n const inlineScripts = await getInlineScripts();\n const pageSessionContext = createPageSessionContext(session);\n const entryScript = exportEntry && importMap.imports[slash(join(definitionName, exportEntry))];\n const overlayScript = importMap.imports['@ms-cloudpack/overlay'];\n\n const renderedContent = await getBootstrapScript({\n importMap,\n pageSessionContext,\n overlayScript,\n inlineScripts,\n entryScript,\n });\n\n return res.type('application/javascript').send(renderedContent).end();\n })().catch((err) => {\n console.error((err as Error)?.stack || err);\n return res.status(500).send(`Error loading app: ${err}`);\n });\n };\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Session } from '@ms-cloudpack/api-server';
|
|
2
|
+
import type { PageSessionContext } from '@ms-cloudpack/common-types-browser';
|
|
3
|
+
/**
|
|
4
|
+
* Create a page session context that needs to be on the page for Cloudpack to work properly.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createPageSessionContext(session: Pick<Session, 'id' | 'sequence' | 'projectName' | 'urls'>): PageSessionContext;
|
|
7
|
+
//# sourceMappingURL=createPageSessionContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createPageSessionContext.d.ts","sourceRoot":"","sources":["../src/createPageSessionContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAE7E;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,IAAI,GAAG,UAAU,GAAG,aAAa,GAAG,MAAM,CAAC,GACjE,kBAAkB,CAOpB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a page session context that needs to be on the page for Cloudpack to work properly.
|
|
3
|
+
*/
|
|
4
|
+
export function createPageSessionContext(session) {
|
|
5
|
+
return {
|
|
6
|
+
sessionId: session.id,
|
|
7
|
+
currentSequence: session.sequence,
|
|
8
|
+
apiUrl: session.urls.apiServer ?? '',
|
|
9
|
+
bundleServerUrl: session.urls.bundleServer ?? '',
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=createPageSessionContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createPageSessionContext.js","sourceRoot":"","sources":["../src/createPageSessionContext.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACtC,OAAkE;IAElE,OAAO;QACL,SAAS,EAAE,OAAO,CAAC,EAAE;QACrB,eAAe,EAAE,OAAO,CAAC,QAAQ;QACjC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,SAAS,IAAI,EAAE;QACpC,eAAe,EAAE,OAAO,CAAC,IAAI,CAAC,YAAY,IAAI,EAAE;KACjD,CAAC;AACJ,CAAC","sourcesContent":["import type { Session } from '@ms-cloudpack/api-server';\nimport type { PageSessionContext } from '@ms-cloudpack/common-types-browser';\n\n/**\n * Create a page session context that needs to be on the page for Cloudpack to work properly.\n */\nexport function createPageSessionContext(\n session: Pick<Session, 'id' | 'sequence' | 'projectName' | 'urls'>,\n): PageSessionContext {\n return {\n sessionId: session.id,\n currentSequence: session.sequence,\n apiUrl: session.urls.apiServer ?? '',\n bundleServerUrl: session.urls.bundleServer ?? '',\n };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRoutes.d.ts","sourceRoot":"","sources":["../src/createRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,
|
|
1
|
+
{"version":3,"file":"createRoutes.d.ts","sourceRoot":"","sources":["../src/createRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAKL,KAAK,WAAW,EAEjB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,kCAAkC,CAAC;AAUnF,UAAU,mBAAmB;IAC3B,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,WAAW,CAAC;CACzB;AAED,KAAK,mBAAmB,GAAG,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,oBAAoB,GAAG,eAAe,GAAG,SAAS,CAAC,CAAC;AAE1G;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,EAAE,OAAO,EAAE,mBAAmB,QAoDtF"}
|
package/lib/createRoutes.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isBootstrapRoute, isRenderedRoute, isStaticRoute, } from '@ms-cloudpack/common-types';
|
|
1
|
+
import { isBootstrapRoute, isRedirectRoute, isRenderedRoute, isStaticRoute, } from '@ms-cloudpack/common-types';
|
|
2
2
|
import { express } from '@ms-cloudpack/create-express-app';
|
|
3
3
|
import { createImportMap } from '@ms-cloudpack/import-map';
|
|
4
4
|
import { slash } from '@ms-cloudpack/path-string-parsing';
|
|
@@ -34,6 +34,11 @@ export function createRoutes(options, context) {
|
|
|
34
34
|
if (isStaticRoute(route)) {
|
|
35
35
|
app.use(route.match, express.static(path.resolve(appPath, route.staticPath)));
|
|
36
36
|
}
|
|
37
|
+
else if (isRedirectRoute(route)) {
|
|
38
|
+
app.get(route.match, (req, res) => {
|
|
39
|
+
res.redirect(route.redirectTo);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
37
42
|
else if (isBootstrapRoute(route)) {
|
|
38
43
|
app.get(route.match, createBootstrapRouteHandler({
|
|
39
44
|
exportEntry: route.exportEntry,
|
|
@@ -70,11 +75,8 @@ async function handleRequest(options, context) {
|
|
|
70
75
|
const overlayScript = importMap.imports['@ms-cloudpack/overlay'];
|
|
71
76
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
72
77
|
const entryScript = route.exportEntry && importMap.imports[slash(path.join(definition.name, route.exportEntry))];
|
|
73
|
-
// Set the appropriate Cloudpack headers
|
|
74
|
-
|
|
75
|
-
// that can only accept changing the scripts. We should consider moving to a model where an initial script
|
|
76
|
-
// fetch loads the import map and sets Cloudpack settings in local storage rather than headers/cookies.
|
|
77
|
-
setHeaders({ res, session });
|
|
78
|
+
// Set the appropriate Cloudpack headers in the response.
|
|
79
|
+
setHeaders({ res });
|
|
78
80
|
const inlineScripts = await getInlineScripts();
|
|
79
81
|
const { content, statusCode, contentType } = await renderRoute({
|
|
80
82
|
...options,
|
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,eAAe,EACf,aAAa,GAGd,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,qCAAqC,CAAC;AACvE,OAAO,EAAE,WAAW,EAAsB,MAAM,8BAA8B,CAAC;AAC/E,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AAU/E;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,OAA4B,EAAE,OAA4B;IACrF,MAAM,EAAE,GAAG,EAAE,GAAG,YAAY,EAAE,GAAG,OAAO,CAAC;IACzC,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAC5B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC3B,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IAC3B,MAAM,MAAM,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC;IAElD,IAAI,CAAC,OAAO,CAAC,UAAU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AO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAK,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAElH,yDAAyD;IACzD,UAAU,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IAEpB,MAAM,aAAa,GAAG,MAAM,gBAAgB,EAAE,CAAC;IAC/C,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,MAAM,WAAW,CAAC;QAC7D,GAAG,OAAO;QACV,OAAO;QACP,OAAO,EAAE,GAAG;QACZ,SAAS;QACT,aAAa;QACb,WAAW;QACX,aAAa;KACd,CAAC,CAAC;IAEH,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,CAAC;IAE7D,OAAO,CAAC,KAAK,CAAC,wBAAwB,WAAW,UAAU,UAAU,EAAE,CAAC,CAAC;AAC3E,CAAC","sourcesContent":["import type { Context } from '@ms-cloudpack/api-server';\nimport {\n isBootstrapRoute,\n isRedirectRoute,\n isRenderedRoute,\n isStaticRoute,\n type PackageJson,\n type RenderedRoute,\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 './inlineScripts/getInlineScripts.js';\nimport { renderRoute, type RouteInternal } from './renderRoute/renderRoute.js';\nimport { setHeaders } from './setHeaders.js';\nimport { createBootstrapRouteHandler } from './createBootstrapRouteHandler.js';\n\ninterface CreateRoutesOptions {\n app: Express;\n url: string;\n definition: PackageJson;\n}\n\ntype CreateRoutesContext = Pick<Context, 'packages' | 'packageImportPaths' | 'packageHashes' | 'session'>;\n\n/**\n * Creates the routes for the express app based on the config.\n */\nexport function createRoutes(options: CreateRoutesOptions, context: CreateRoutesContext) {\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 // Only as a basic fallback if no routes are specified, add a default catch-all route.\n // Adding this automatically has the risk of hiding errors, like if an unexpected path\n // being requested, or a JS file somehow being loaded from the app server unexpectedly.\n if (!routes.length) {\n const fallbackRoute: RouteInternal = {\n match: '*',\n exportEntry: '.',\n isDefaultFallback: true, // this internal property is read by renderRoute\n };\n routes.push(fallbackRoute);\n }\n\n for (const route of routes) {\n if (isStaticRoute(route)) {\n app.use(route.match, express.static(path.resolve(appPath, route.staticPath)));\n } else if (isRedirectRoute(route)) {\n app.get(route.match, (req, res) => {\n res.redirect(route.redirectTo);\n });\n } else if (isBootstrapRoute(route)) {\n app.get(\n route.match,\n createBootstrapRouteHandler(\n {\n exportEntry: route.exportEntry,\n definitionName: options.definition.name,\n },\n context,\n ),\n );\n } else if (isRenderedRoute(route)) {\n app.get(route.match, (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 {\n throw new Error(`Unknown route: ${JSON.stringify(route)}`);\n }\n }\n}\n\nasync function handleRequest(\n options: Omit<CreateRoutesOptions, 'app'> & {\n req: Request;\n res: Response;\n route: RenderedRoute;\n },\n context: CreateRoutesContext,\n) {\n const { req, res, definition, route, url } = options;\n const { session } = context;\n const { bundleServer } = session.urls;\n if (!bundleServer) {\n // Sanity check...\n throw new Error('The bundle server URL is not yet set in the session (this is a Cloudpack bug).');\n }\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({ ...context, ...session, urls: { bundleServer } }));\n\n // Parse the request path, extension, grab the overlay script path.\n const requestPath = slash(req.path.substring(1));\n const requestExt = path.extname(requestPath);\n const overlayScript = importMap.imports['@ms-cloudpack/overlay'];\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const entryScript = route.exportEntry && importMap.imports[slash(path.join(definition.name!, route.exportEntry))];\n\n // Set the appropriate Cloudpack headers in the response.\n setHeaders({ res });\n\n const inlineScripts = await getInlineScripts();\n const { content, statusCode, contentType } = await renderRoute({\n ...options,\n session,\n baseUrl: url,\n importMap,\n overlayScript,\n entryScript,\n inlineScripts,\n });\n\n res.type(contentType).status(statusCode).send(content).end();\n\n console.debug(`App server: Request: ${requestPath}, ext: ${requestExt}`);\n}\n"]}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
window.__cloudpack ??= {};
|
|
2
|
-
window.__cloudpack.bootstrap = ({ inlineScripts, overlayScript, importMap,
|
|
2
|
+
window.__cloudpack.bootstrap = ({ inlineScripts, overlayScript, importMap, pageSessionContext, entryScript }) => {
|
|
3
3
|
// Inject the import map
|
|
4
4
|
const importMapScriptTag = document.createElement('script');
|
|
5
5
|
importMapScriptTag.type = 'importmap';
|
|
6
6
|
importMapScriptTag.textContent = JSON.stringify(importMap);
|
|
7
7
|
document.head.appendChild(importMapScriptTag);
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
localStorage.setItem(key, value);
|
|
11
|
-
});
|
|
8
|
+
window.__cloudpack ??= {};
|
|
9
|
+
window.__cloudpack.pageSessionContext = pageSessionContext;
|
|
12
10
|
// Inject the inline scripts
|
|
13
11
|
for (const inlineScript of inlineScripts) {
|
|
14
12
|
const inlineTag = document.createElement('script');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bootstrap.inline.js","sourceRoot":"","sources":["../../src/inlineScripts/bootstrap.inline.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,WAAW,KAAK,EAAE,CAAC;AAC1B,MAAM,CAAC,WAAW,CAAC,SAAS,GAAG,CAAC,EAAE,aAAa,EAAE,aAAa,EAAE,SAAS,EAAE,
|
|
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Ac,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACxD,cAAc,CAAC,IAAI,GAAG,QAAQ,CAAC;QAC/B,cAAc,CAAC,GAAG,GAAG,WAAW,CAAC;QACjC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC;IAC5C,CAAC;AACH,CAAC,CAAC","sourcesContent":["window.__cloudpack ??= {};\nwindow.__cloudpack.bootstrap = ({ inlineScripts, overlayScript, importMap, pageSessionContext, entryScript }) => {\n // Inject the import map\n const importMapScriptTag = document.createElement('script');\n importMapScriptTag.type = 'importmap';\n importMapScriptTag.textContent = JSON.stringify(importMap);\n document.head.appendChild(importMapScriptTag);\n\n window.__cloudpack ??= {};\n window.__cloudpack.pageSessionContext = pageSessionContext;\n\n // Inject the inline scripts\n for (const inlineScript of inlineScripts) {\n const inlineTag = document.createElement('script');\n inlineTag.type = 'module';\n inlineTag.textContent = inlineScript;\n document.head.appendChild(inlineTag);\n }\n\n // Inject the overlay script\n const overlayScriptTag = document.createElement('script');\n overlayScriptTag.type = 'module';\n overlayScriptTag.src = overlayScript;\n document.head.appendChild(overlayScriptTag);\n\n // Inject the main script\n if (entryScript) {\n const entryScriptTag = document.createElement('script');\n entryScriptTag.type = 'module';\n entryScriptTag.src = entryScript;\n document.head.appendChild(entryScriptTag);\n }\n};\n"]}
|
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
// This file must NOT import other files (except types), because they won't be resolved at runtime.
|
|
2
|
-
// Typing hack to ensure this name is correct while working around the limitation on runtime imports
|
|
3
|
-
const cloudpackBundleServerUrlCookie = 'cloudpack-bundle-server-url';
|
|
4
2
|
const scriptLoadResults = {};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const cookies = decodedCookies.split(';');
|
|
8
|
-
const keyValues = cookies.map((cookie) => cookie.split('='));
|
|
9
|
-
const cookie = keyValues.find(([key]) => key.trim() === cname);
|
|
10
|
-
if (!cookie) {
|
|
11
|
-
const result = localStorage.getItem(cname);
|
|
12
|
-
if (result) {
|
|
13
|
-
return result;
|
|
14
|
-
}
|
|
15
|
-
throw new Error(`Cookie ${cname} not found`);
|
|
16
|
-
}
|
|
17
|
-
return cookie[1].trim();
|
|
3
|
+
if (!window.__cloudpack?.pageSessionContext) {
|
|
4
|
+
throw new Error('window.__cloudpack.pageSessionContext is not defined');
|
|
18
5
|
}
|
|
19
|
-
const bundleServerUrl =
|
|
6
|
+
const bundleServerUrl = window.__cloudpack.pageSessionContext.bundleServerUrl;
|
|
20
7
|
function isPerformanceResourceTiming(entry) {
|
|
21
8
|
return entry.entryType === 'resource';
|
|
22
9
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBrowserCacheRatio.inline.js","sourceRoot":"","sources":["../../src/inlineScripts/getBrowserCacheRatio.inline.ts"],"names":[],"mappings":"AAAA,mGAAmG;
|
|
1
|
+
{"version":3,"file":"getBrowserCacheRatio.inline.js","sourceRoot":"","sources":["../../src/inlineScripts/getBrowserCacheRatio.inline.ts"],"names":[],"mappings":"AAAA,mGAAmG;AAEnG,MAAM,iBAAiB,GAAwC,EAAE,CAAC;AAElE,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,kBAAkB,EAAE,CAAC;IAC5C,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;AAC1E,CAAC;AAED,MAAM,eAAe,GAAG,MAAM,CAAC,WAAW,CAAC,kBAAkB,CAAC,eAAe,CAAC;AAE9E,SAAS,2BAA2B,CAAC,KAAuB;IAC1D,OAAO,KAAK,CAAC,SAAS,KAAK,UAAU,CAAC;AACxC,CAAC;AAED,SAAS,sBAAsB,CAAC,IAAsB;IACpD,IAAI,2BAA2B,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,EAAE,CAAC;QAC/E,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IAC/E,CAAC;AACH,CAAC;AAED,MAAM,YAAY,GAAG,IAAI,mBAAmB,CAAC,CAAC,IAAI,EAAE,EAAE;IACpD,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;QACrC,sBAAsB,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,4DAA4D;AAC5D,KAAK,MAAM,KAAK,IAAI,WAAW,CAAC,gBAAgB,CAAC,UAAU,CAAC,EAAE,CAAC;IAC7D,sBAAsB,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAED,0DAA0D;AAC1D,YAAY,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AAE3D,SAAS,oBAAoB;IAC3B,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,IAAI,OAAO,CAAC,CAAC,MAAM,CAAC;IACjG,MAAM,KAAK,GAAG,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAAC;IAEjE,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,CAAC,WAAW,KAAK,EAAE,CAAC;AAC1B,MAAM,CAAC,WAAW,CAAC,oBAAoB,GAAG,oBAAoB,CAAC","sourcesContent":["// This file must NOT import other files (except types), because they won't be resolved at runtime.\n\nconst scriptLoadResults: Record<string, 'cache' | 'network'> = {};\n\nif (!window.__cloudpack?.pageSessionContext) {\n throw new Error('window.__cloudpack.pageSessionContext is not defined');\n}\n\nconst bundleServerUrl = window.__cloudpack.pageSessionContext.bundleServerUrl;\n\nfunction isPerformanceResourceTiming(entry: PerformanceEntry): entry is PerformanceResourceTiming {\n return entry.entryType === 'resource';\n}\n\nfunction handlePerformanceEntry(item: PerformanceEntry) {\n if (isPerformanceResourceTiming(item) && item.name.startsWith(bundleServerUrl)) {\n scriptLoadResults[item.name] = item.transferSize === 0 ? 'cache' : 'network';\n }\n}\n\nconst perfObserver = new PerformanceObserver((list) => {\n for (const item of list.getEntries()) {\n handlePerformanceEntry(item);\n }\n});\n\n// Get all the resources that were loaded before this script\nfor (const entry of performance.getEntriesByType('resource')) {\n handlePerformanceEntry(entry);\n}\n\n// Observe all resources that are loaded after this script\nperfObserver.observe({ type: 'resource', buffered: true });\n\nfunction getBrowserCacheRatio() {\n const cacheCount = Object.values(scriptLoadResults).filter((source) => source == 'cache').length;\n const ratio = cacheCount / Object.keys(scriptLoadResults).length;\n\n return ratio;\n}\n\nwindow.__cloudpack ??= {};\nwindow.__cloudpack.getBrowserCacheRatio = getBrowserCacheRatio;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderRoute.d.ts","sourceRoot":"","sources":["../../src/renderRoute/renderRoute.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAOhE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,KAAK,EAAE,yBAAyB,EAAqB,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"renderRoute.d.ts","sourceRoot":"","sources":["../../src/renderRoute/renderRoute.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAOhE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,KAAK,EAAE,yBAAyB,EAAqB,MAAM,+BAA+B,CAAC;AAMlG,MAAM,MAAM,aAAa,GAAG,aAAa,GAAG;IAAE,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAE5E;;;GAGG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAsDjG"}
|
|
@@ -5,6 +5,7 @@ import path from 'path';
|
|
|
5
5
|
import { pathToFileURL } from 'url';
|
|
6
6
|
import { getDefaultHtmlResponse } from './getDefaultHtmlResponse.js';
|
|
7
7
|
import { getHtmlErrorResponse } from './getHtmlErrorResponse.js';
|
|
8
|
+
import { createPageSessionContext } from '../createPageSessionContext.js';
|
|
8
9
|
/**
|
|
9
10
|
* Get the response for the given route. If the route has a custom render script, use that.
|
|
10
11
|
* Returns an error response if the file referenced by the script doesn't exist or throws an error.
|
|
@@ -140,6 +141,14 @@ function injectScripts(options, result) {
|
|
|
140
141
|
// Inject the import map at the top of the head, in case other scripts use it
|
|
141
142
|
addScript({ document, type: 'importmap', prepend: true, content: JSON.stringify(importMap, null, 2) });
|
|
142
143
|
}
|
|
144
|
+
if (overlayScript || inlineScripts.length > 0) {
|
|
145
|
+
addScript({
|
|
146
|
+
document,
|
|
147
|
+
content: `
|
|
148
|
+
window.__cloudpack ??= {};
|
|
149
|
+
window.__cloudpack.pageSessionContext = ${JSON.stringify(createPageSessionContext(options.session))};`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
143
152
|
for (const inlineScript of inlineScripts) {
|
|
144
153
|
addScript({ document, content: inlineScript });
|
|
145
154
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderRoute.js","sourceRoot":"","sources":["../../src/renderRoute/renderRoute.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAC5D,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAKpC,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,oBAAoB,E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,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,cAAc,CAAC,gBAAwB;IACpD,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,UAAU,CAAC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;IACrE,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,MAAM,OAAO,GAAG,+BAA+B,gBAAgB,OAAQ,CAAW,CAAC,KAAK,IAAI,CAAC,EAAE,CAAC;QAChG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACvB,OAAO,oBAAoB,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;AACH,CAAC","sourcesContent":["import type { RenderedRoute } from '@ms-cloudpack/common-types';\nimport { makeUrl } from '@ms-cloudpack/path-string-parsing';\nimport fsPromises from 'fs/promises';\nimport { JSDOM } from 'jsdom';\nimport path from 'path';\nimport { pathToFileURL } from 'url';\nimport type { RenderRouteFunction } from '../types/RenderRouteFunction.js';\nimport type { RenderRouteOptions } from '../types/RenderRouteOptions.js';\nimport type { CompleteRenderRouteResult, RenderRouteResult } from '../types/RenderRouteResult.js';\nimport type { RenderRouteScript } from '../types/RenderRouteScript.js';\nimport { getDefaultHtmlResponse } from './getDefaultHtmlResponse.js';\nimport { getHtmlErrorResponse } from './getHtmlErrorResponse.js';\n\nexport type RouteInternal = RenderedRoute & { isDefaultFallback?: boolean };\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(options: RenderRouteOptions): Promise<CompleteRenderRouteResult> {\n const { route, session, req } = options;\n const { config } = session;\n const { appPath } = config;\n\n // Get the request path and extension (the URL.pathname conversion gets rid of any query or hash)\n const requestPath = makeUrl(req.url, options.baseUrl).pathname;\n const requestExt = path.extname(requestPath);\n // Determine whether HTML is (probably) an acceptable response type (can be refined later if needed).\n // In theory we should be able to use req.accepts() but not all requests set the accept header.\n const isHtmlAcceptable =\n (!requestExt || requestExt.startsWith('.htm')) && !req.xhr && isHtmlType(req.headers.accept || '*/*');\n\n // Get an initial result from either the render script or default handler.\n let rawResult: RenderRouteResult;\n const renderScriptPath = route.renderScript && path.resolve(appPath, route.renderScript);\n if (renderScriptPath) {\n const scriptExt = path.extname(renderScriptPath).toLowerCase();\n if (scriptExt === '.html' || scriptExt === '.htm') {\n rawResult = await readStaticHtml(renderScriptPath);\n } else {\n rawResult = await renderCustomScript({ ...options, renderScriptPath });\n }\n } else {\n rawResult = await getDefaultHtmlResponse(options);\n }\n\n const result: CompleteRenderRouteResult = {\n contentType: 'text/html',\n statusCode: 200,\n ...(typeof rawResult === 'string' ? { content: rawResult } : rawResult),\n };\n\n // If it's HTML and a success statusCode, inject the import map and appropriate scripts.\n if (result.statusCode === 200 && isHtmlType(result.contentType)) {\n // First verify that we're not accidentally returning HTML if a different file type is requested.\n // This could happen for overly broad route matches such as '*', or if no routes are configured.\n if (!isHtmlAcceptable) {\n const renderer = route.renderScript ? `renderScript \"${renderScriptPath}\"` : 'the default renderer';\n const content = (route as RouteInternal).isDefaultFallback\n ? // no routes configured\n `Cloudpack's default route can only return HTML, but this appears to be a non-HTML request. ` +\n `Please add a route under devServer.routes in the cloudpack config.`\n : // route with or without custom renderer\n `Route \"${route.match}\" with ${renderer} returned HTML, but this appears to be a non-HTML request. ` +\n `This is likely a misconfiguration of devServer.routes in the cloudpack config.`;\n return { statusCode: 500, contentType: 'text/plain', content };\n }\n\n // Inject the import map and scripts.\n injectScripts(options, result);\n }\n\n return result;\n}\n\nfunction isHtmlType(acceptOrContentType: string) {\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types\n return acceptOrContentType\n .split(',')\n .map((s) => s.split(';')[0].trim())\n .some((mime) => mime === '*/*' || /xml|x?html/i.test(mime) || /^(text|application)\\/\\*$/.test(mime));\n}\n\n/**\n * Load the default export from a JS file passed as the `renderScript` in a route,\n * and return either the result of running the function, or an error page if something fails.\n */\nasync function renderCustomScript(\n params: RenderRouteOptions & { renderScriptPath: string },\n): Promise<RenderRouteResult> {\n const { renderScriptPath, ...options } = params;\n // Get the html factory function from a script default export.\n const renderScriptUrl = pathToFileURL(renderScriptPath).toString();\n\n // Note: because there isn't a way to purge the require cache, we need to add a cache breaker query param to the\n // script path to ensure we get the latest version of the script. This could be improved by using the git hash of\n // the file if it exists, or a hash of the content, or even the timestamp of the file.\n let cacheBreakerQueryParam = `?t=${Date.now()}`;\n try {\n const { mtime } = await fsPromises.stat(renderScriptPath);\n cacheBreakerQueryParam = `?t=${mtime.getTime()}`;\n } catch {\n /* no-op */\n }\n\n // Try importing the script\n let createHtml: RenderRouteFunction | undefined;\n let errorMessage: string | undefined;\n try {\n const importResult = ((await import(renderScriptUrl + cacheBreakerQueryParam)) as RenderRouteScript).default;\n if (typeof importResult === 'function') {\n createHtml = importResult;\n } else {\n errorMessage = `The render script at \"${renderScriptPath}\" does not export a default function.`;\n }\n } catch (e) {\n errorMessage = `Error importing render script at \"${renderScriptUrl}\":\\n${(e as Error).stack || e}`;\n }\n\n if (createHtml) {\n // Try running the script\n try {\n return await createHtml(options);\n } catch (e) {\n errorMessage = `Error running render script at \"${renderScriptPath}\":\\n${(e as Error).stack || e}`;\n }\n }\n\n // Return an error page. Doing this instead of returning a default or empty response ensures that\n // the user is aware of any configuration issues.\n errorMessage ??= `Unknown error loading render script at \"${renderScriptPath}\".`;\n console.error(errorMessage);\n return getHtmlErrorResponse(errorMessage);\n}\n\n/**\n * Modify the HTML response by injecting the import map, inline scripts, and entry/overlay scripts.\n *\n * Note that this just logs an error rather than returning an error response if something fails, for now.\n */\nfunction injectScripts(options: RenderRouteOptions, result: CompleteRenderRouteResult): void {\n const { route, overlayScript, entryScript, inlineScripts, importMap } = options;\n let jsdom: JSDOM;\n try {\n jsdom = new JSDOM(result.content, { contentType: result.contentType } as ConstructorParameters<typeof JSDOM>[1]);\n } catch (e) {\n // Trying to write a test for the above, it seemed very permissive, but catch just in case\n console.error(`Error parsing html response for rendering route \"${route.match}\":\\n${(e as Error).stack || e}`);\n return;\n }\n\n try {\n const { document } = jsdom.window;\n\n if (entryScript || overlayScript) {\n // Inject the import map at the top of the head, in case other scripts use it\n addScript({ document, type: 'importmap', prepend: true, content: JSON.stringify(importMap, null, 2) });\n }\n\n for (const inlineScript of inlineScripts) {\n addScript({ document, content: inlineScript });\n }\n\n // inject the overlay and entry scripts\n addScript({ document, url: overlayScript });\n addScript({ document, url: entryScript });\n\n result.content = jsdom.serialize();\n } catch (e) {\n console.error(`Error injecting scripts for route \"${route.match}\":\\n${(e as Error).stack || e}`);\n }\n}\n\n/**\n * Helper function to add a script to the document.\n */\nfunction addScript(params: { document: Document; type?: string; prepend?: boolean; url?: string; content?: string }) {\n const { document, type = 'module', prepend, url, content } = params;\n if (!url && !content) {\n return;\n }\n\n const script = document.createElement('script');\n script.type = type;\n if (url) {\n script.src = url;\n } else if (content) {\n script.innerHTML = content;\n }\n\n if (prepend) {\n document.head.prepend(script);\n } else {\n document.head.appendChild(script);\n }\n}\n\n/**\n * Read a static HTML file, or return an error response if the file can't be read.\n */\nasync function readStaticHtml(renderScriptPath: string): Promise<RenderRouteResult> {\n try {\n return (await fsPromises.readFile(renderScriptPath, 'utf8')) || '';\n } catch (e) {\n const message = `Error reading HTML file at \"${renderScriptPath}\":\\n${(e as Error).stack || e}`;\n console.error(message);\n return getHtmlErrorResponse(message);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"renderRoute.js","sourceRoot":"","sources":["../../src/renderRoute/renderRoute.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAC5D,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,E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AAO;IACT,CAAC;IAED,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChD,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,IAAI,GAAG,EAAE,CAAC;QACR,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC;IACnB,CAAC;SAAM,IAAI,OAAO,EAAE,CAAC;QACnB,MAAM,CAAC,SAAS,GAAG,OAAO,CAAC;IAC7B,CAAC;IAED,IAAI,OAAO,EAAE,CAAC;QACZ,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;SAAM,CAAC;QACN,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC;AACH,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,cAAc,CAAC,gBAAwB;IACpD,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,UAAU,CAAC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;IACrE,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,MAAM,OAAO,GAAG,+BAA+B,gBAAgB,OAAQ,CAAW,CAAC,KAAK,IAAI,CAAC,EAAE,CAAC;QAChG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACvB,OAAO,oBAAoB,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;AACH,CAAC","sourcesContent":["import type { RenderedRoute } from '@ms-cloudpack/common-types';\nimport { makeUrl } from '@ms-cloudpack/path-string-parsing';\nimport fsPromises from 'fs/promises';\nimport { JSDOM } from 'jsdom';\nimport path from 'path';\nimport { pathToFileURL } from 'url';\nimport type { RenderRouteFunction } from '../types/RenderRouteFunction.js';\nimport type { RenderRouteOptions } from '../types/RenderRouteOptions.js';\nimport type { CompleteRenderRouteResult, RenderRouteResult } from '../types/RenderRouteResult.js';\nimport type { RenderRouteScript } from '../types/RenderRouteScript.js';\nimport { getDefaultHtmlResponse } from './getDefaultHtmlResponse.js';\nimport { getHtmlErrorResponse } from './getHtmlErrorResponse.js';\nimport { createPageSessionContext } from '../createPageSessionContext.js';\n\nexport type RouteInternal = RenderedRoute & { isDefaultFallback?: boolean };\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(options: RenderRouteOptions): Promise<CompleteRenderRouteResult> {\n const { route, session, req } = options;\n const { config } = session;\n const { appPath } = config;\n\n // Get the request path and extension (the URL.pathname conversion gets rid of any query or hash)\n const requestPath = makeUrl(req.url, options.baseUrl).pathname;\n const requestExt = path.extname(requestPath);\n // Determine whether HTML is (probably) an acceptable response type (can be refined later if needed).\n // In theory we should be able to use req.accepts() but not all requests set the accept header.\n const isHtmlAcceptable =\n (!requestExt || requestExt.startsWith('.htm')) && !req.xhr && isHtmlType(req.headers.accept || '*/*');\n\n // Get an initial result from either the render script or default handler.\n let rawResult: RenderRouteResult;\n const renderScriptPath = route.renderScript && path.resolve(appPath, route.renderScript);\n if (renderScriptPath) {\n const scriptExt = path.extname(renderScriptPath).toLowerCase();\n if (scriptExt === '.html' || scriptExt === '.htm') {\n rawResult = await readStaticHtml(renderScriptPath);\n } else {\n rawResult = await renderCustomScript({ ...options, renderScriptPath });\n }\n } else {\n rawResult = await getDefaultHtmlResponse(options);\n }\n\n const result: CompleteRenderRouteResult = {\n contentType: 'text/html',\n statusCode: 200,\n ...(typeof rawResult === 'string' ? { content: rawResult } : rawResult),\n };\n\n // If it's HTML and a success statusCode, inject the import map and appropriate scripts.\n if (result.statusCode === 200 && isHtmlType(result.contentType)) {\n // First verify that we're not accidentally returning HTML if a different file type is requested.\n // This could happen for overly broad route matches such as '*', or if no routes are configured.\n if (!isHtmlAcceptable) {\n const renderer = route.renderScript ? `renderScript \"${renderScriptPath}\"` : 'the default renderer';\n const content = (route as RouteInternal).isDefaultFallback\n ? // no routes configured\n `Cloudpack's default route can only return HTML, but this appears to be a non-HTML request. ` +\n `Please add a route under devServer.routes in the cloudpack config.`\n : // route with or without custom renderer\n `Route \"${route.match}\" with ${renderer} returned HTML, but this appears to be a non-HTML request. ` +\n `This is likely a misconfiguration of devServer.routes in the cloudpack config.`;\n return { statusCode: 500, contentType: 'text/plain', content };\n }\n\n // Inject the import map and scripts.\n injectScripts(options, result);\n }\n\n return result;\n}\n\nfunction isHtmlType(acceptOrContentType: string) {\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types\n return acceptOrContentType\n .split(',')\n .map((s) => s.split(';')[0].trim())\n .some((mime) => mime === '*/*' || /xml|x?html/i.test(mime) || /^(text|application)\\/\\*$/.test(mime));\n}\n\n/**\n * Load the default export from a JS file passed as the `renderScript` in a route,\n * and return either the result of running the function, or an error page if something fails.\n */\nasync function renderCustomScript(\n params: RenderRouteOptions & { renderScriptPath: string },\n): Promise<RenderRouteResult> {\n const { renderScriptPath, ...options } = params;\n // Get the html factory function from a script default export.\n const renderScriptUrl = pathToFileURL(renderScriptPath).toString();\n\n // Note: because there isn't a way to purge the require cache, we need to add a cache breaker query param to the\n // script path to ensure we get the latest version of the script. This could be improved by using the git hash of\n // the file if it exists, or a hash of the content, or even the timestamp of the file.\n let cacheBreakerQueryParam = `?t=${Date.now()}`;\n try {\n const { mtime } = await fsPromises.stat(renderScriptPath);\n cacheBreakerQueryParam = `?t=${mtime.getTime()}`;\n } catch {\n /* no-op */\n }\n\n // Try importing the script\n let createHtml: RenderRouteFunction | undefined;\n let errorMessage: string | undefined;\n try {\n const importResult = ((await import(renderScriptUrl + cacheBreakerQueryParam)) as RenderRouteScript).default;\n if (typeof importResult === 'function') {\n createHtml = importResult;\n } else {\n errorMessage = `The render script at \"${renderScriptPath}\" does not export a default function.`;\n }\n } catch (e) {\n errorMessage = `Error importing render script at \"${renderScriptUrl}\":\\n${(e as Error).stack || e}`;\n }\n\n if (createHtml) {\n // Try running the script\n try {\n return await createHtml(options);\n } catch (e) {\n errorMessage = `Error running render script at \"${renderScriptPath}\":\\n${(e as Error).stack || e}`;\n }\n }\n\n // Return an error page. Doing this instead of returning a default or empty response ensures that\n // the user is aware of any configuration issues.\n errorMessage ??= `Unknown error loading render script at \"${renderScriptPath}\".`;\n console.error(errorMessage);\n return getHtmlErrorResponse(errorMessage);\n}\n\n/**\n * Modify the HTML response by injecting the import map, inline scripts, and entry/overlay scripts.\n *\n * Note that this just logs an error rather than returning an error response if something fails, for now.\n */\nfunction injectScripts(options: RenderRouteOptions, result: CompleteRenderRouteResult): void {\n const { route, overlayScript, entryScript, inlineScripts, importMap } = options;\n let jsdom: JSDOM;\n try {\n jsdom = new JSDOM(result.content, { contentType: result.contentType } as ConstructorParameters<typeof JSDOM>[1]);\n } catch (e) {\n // Trying to write a test for the above, it seemed very permissive, but catch just in case\n console.error(`Error parsing html response for rendering route \"${route.match}\":\\n${(e as Error).stack || e}`);\n return;\n }\n\n try {\n const { document } = jsdom.window;\n\n if (entryScript || overlayScript) {\n // Inject the import map at the top of the head, in case other scripts use it\n addScript({ document, type: 'importmap', prepend: true, content: JSON.stringify(importMap, null, 2) });\n }\n\n if (overlayScript || inlineScripts.length > 0) {\n addScript({\n document,\n content: `\n window.__cloudpack ??= {};\n window.__cloudpack.pageSessionContext = ${JSON.stringify(createPageSessionContext(options.session))};`,\n });\n }\n\n for (const inlineScript of inlineScripts) {\n addScript({ document, content: inlineScript });\n }\n\n // inject the overlay and entry scripts\n addScript({ document, url: overlayScript });\n addScript({ document, url: entryScript });\n\n result.content = jsdom.serialize();\n } catch (e) {\n console.error(`Error injecting scripts for route \"${route.match}\":\\n${(e as Error).stack || e}`);\n }\n}\n\n/**\n * Helper function to add a script to the document.\n */\nfunction addScript(params: { document: Document; type?: string; prepend?: boolean; url?: string; content?: string }) {\n const { document, type = 'module', prepend, url, content } = params;\n if (!url && !content) {\n return;\n }\n\n const script = document.createElement('script');\n script.type = type;\n if (url) {\n script.src = url;\n } else if (content) {\n script.innerHTML = content;\n }\n\n if (prepend) {\n document.head.prepend(script);\n } else {\n document.head.appendChild(script);\n }\n}\n\n/**\n * Read a static HTML file, or return an error response if the file can't be read.\n */\nasync function readStaticHtml(renderScriptPath: string): Promise<RenderRouteResult> {\n try {\n return (await fsPromises.readFile(renderScriptPath, 'utf8')) || '';\n } catch (e) {\n const message = `Error reading HTML file at \"${renderScriptPath}\":\\n${(e as Error).stack || e}`;\n console.error(message);\n return getHtmlErrorResponse(message);\n }\n}\n"]}
|
package/lib/setHeaders.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import type { Response } from '@ms-cloudpack/create-express-app';
|
|
2
|
-
import type { Session } from '@ms-cloudpack/api-server';
|
|
3
2
|
/**
|
|
4
|
-
* Set the Cloudpack headers
|
|
3
|
+
* Set the Cloudpack headers in the response.
|
|
5
4
|
*/
|
|
6
5
|
export declare function setHeaders(params: {
|
|
7
6
|
res: Response;
|
|
8
|
-
session: Pick<Session, 'id' | 'sequence' | 'projectName' | 'sessionVersion' | 'urls'>;
|
|
9
7
|
}): void;
|
|
10
8
|
//# sourceMappingURL=setHeaders.d.ts.map
|
package/lib/setHeaders.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setHeaders.d.ts","sourceRoot":"","sources":["../src/setHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"setHeaders.d.ts","sourceRoot":"","sources":["../src/setHeaders.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAEjE;;GAEG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE;IAAE,GAAG,EAAE,QAAQ,CAAA;CAAE,QAKnD"}
|
package/lib/setHeaders.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
import { getCookies } from './getCookies.js';
|
|
2
1
|
/**
|
|
3
|
-
* Set the Cloudpack headers
|
|
2
|
+
* Set the Cloudpack headers in the response.
|
|
4
3
|
*/
|
|
5
4
|
export function setHeaders(params) {
|
|
6
|
-
const { res
|
|
7
|
-
const { apiServer, bundleServer } = session.urls;
|
|
5
|
+
const { res } = params;
|
|
8
6
|
res.header('Cache-Control', 'no-cache');
|
|
9
7
|
res.header('Access-Control-Allow-Origin', '*');
|
|
10
|
-
const cookies = getCookies(session, apiServer, bundleServer);
|
|
11
|
-
for (const [key, value] of Object.entries(cookies)) {
|
|
12
|
-
res.cookie(key, value);
|
|
13
|
-
}
|
|
14
8
|
}
|
|
15
9
|
//# sourceMappingURL=setHeaders.js.map
|
package/lib/setHeaders.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setHeaders.js","sourceRoot":"","sources":["../src/setHeaders.ts"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"setHeaders.js","sourceRoot":"","sources":["../src/setHeaders.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,MAAyB;IAClD,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC;IAEvB,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;IACxC,GAAG,CAAC,MAAM,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import type { Response } from '@ms-cloudpack/create-express-app';\n\n/**\n * Set the Cloudpack headers in the response.\n */\nexport function setHeaders(params: { res: Response }) {\n const { res } = params;\n\n res.header('Cache-Control', 'no-cache');\n res.header('Access-Control-Allow-Origin', '*');\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/app-server",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.4",
|
|
4
4
|
"description": "An implementation of the App server for Cloudpack.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,18 +14,19 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/api-server": "^0.
|
|
18
|
-
"@ms-cloudpack/bundle-server": "^0.4.
|
|
19
|
-
"@ms-cloudpack/common-types": "^0.
|
|
20
|
-
"@ms-cloudpack/create-express-app": "^1.6.
|
|
21
|
-
"@ms-cloudpack/import-map": "^0.5.
|
|
22
|
-
"@ms-cloudpack/overlay": "^0.17.
|
|
17
|
+
"@ms-cloudpack/api-server": "^0.47.0",
|
|
18
|
+
"@ms-cloudpack/bundle-server": "^0.4.33",
|
|
19
|
+
"@ms-cloudpack/common-types": "^0.12.0",
|
|
20
|
+
"@ms-cloudpack/create-express-app": "^1.6.27",
|
|
21
|
+
"@ms-cloudpack/import-map": "^0.5.21",
|
|
22
|
+
"@ms-cloudpack/overlay": "^0.17.31",
|
|
23
23
|
"@ms-cloudpack/path-string-parsing": "^1.2.3",
|
|
24
|
-
"@ms-cloudpack/path-utilities": "^2.7.
|
|
24
|
+
"@ms-cloudpack/path-utilities": "^2.7.24",
|
|
25
25
|
"@ms-cloudpack/task-reporter": "^0.14.1",
|
|
26
26
|
"jsdom": "^24.0.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
+
"@ms-cloudpack/common-types-browser": "^0.2.0",
|
|
29
30
|
"@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
|
|
30
31
|
"@ms-cloudpack/scripts": "^0.0.1",
|
|
31
32
|
"@ms-cloudpack/test-utilities": "^0.5.0",
|
package/lib/cookieNames.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export declare const cookieNames: {
|
|
2
|
-
readonly bundleServerUrl: "cloudpack-bundle-server-url";
|
|
3
|
-
readonly sessionId: string;
|
|
4
|
-
readonly sessionSequence: string;
|
|
5
|
-
readonly apiUrl: string;
|
|
6
|
-
};
|
|
7
|
-
export declare function getCloudpackSessionVersionCookie(projectName: string): string;
|
|
8
|
-
//# sourceMappingURL=cookieNames.d.ts.map
|
package/lib/cookieNames.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cookieNames.d.ts","sourceRoot":"","sources":["../src/cookieNames.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW;;;;;CAGd,CAAC;AAEX,wBAAgB,gCAAgC,CAAC,WAAW,EAAE,MAAM,UAEnE"}
|
package/lib/cookieNames.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { cookieNames as overlayCookieNames } from '@ms-cloudpack/overlay/constants';
|
|
2
|
-
export const cookieNames = {
|
|
3
|
-
...overlayCookieNames,
|
|
4
|
-
bundleServerUrl: 'cloudpack-bundle-server-url',
|
|
5
|
-
};
|
|
6
|
-
export function getCloudpackSessionVersionCookie(projectName) {
|
|
7
|
-
return 'cloudpack-sessionVersion-' + projectName;
|
|
8
|
-
}
|
|
9
|
-
//# sourceMappingURL=cookieNames.js.map
|
package/lib/cookieNames.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cookieNames.js","sourceRoot":"","sources":["../src/cookieNames.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAEpF,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,GAAG,kBAAkB;IACrB,eAAe,EAAE,6BAA6B;CACtC,CAAC;AAEX,MAAM,UAAU,gCAAgC,CAAC,WAAmB;IAClE,OAAO,2BAA2B,GAAG,WAAW,CAAC;AACnD,CAAC","sourcesContent":["import { cookieNames as overlayCookieNames } from '@ms-cloudpack/overlay/constants';\n\nexport const cookieNames = {\n ...overlayCookieNames,\n bundleServerUrl: 'cloudpack-bundle-server-url',\n} as const;\n\nexport function getCloudpackSessionVersionCookie(projectName: string) {\n return 'cloudpack-sessionVersion-' + projectName;\n}\n"]}
|
package/lib/getCookies.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { Session } from '@ms-cloudpack/api-server';
|
|
2
|
-
/**
|
|
3
|
-
* Get the cookies that needs to be written on the page for Cloudpack to work properly.
|
|
4
|
-
*/
|
|
5
|
-
export declare function getCookies(session: Pick<Session, 'id' | 'sequence' | 'projectName' | 'sessionVersion'>, apiServerUrl?: string, bundleServerUrl?: string): Record<string, string>;
|
|
6
|
-
//# sourceMappingURL=getCookies.d.ts.map
|
package/lib/getCookies.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getCookies.d.ts","sourceRoot":"","sources":["../src/getCookies.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AAGxD;;GAEG;AACH,wBAAgB,UAAU,CACxB,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,IAAI,GAAG,UAAU,GAAG,aAAa,GAAG,gBAAgB,CAAC,EAC5E,YAAY,CAAC,EAAE,MAAM,EACrB,eAAe,CAAC,EAAE,MAAM,GACvB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQxB"}
|
package/lib/getCookies.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { cookieNames, getCloudpackSessionVersionCookie } from './cookieNames.js';
|
|
2
|
-
/**
|
|
3
|
-
* Get the cookies that needs to be written on the page for Cloudpack to work properly.
|
|
4
|
-
*/
|
|
5
|
-
export function getCookies(session, apiServerUrl, bundleServerUrl) {
|
|
6
|
-
return {
|
|
7
|
-
[cookieNames.sessionId]: session.id,
|
|
8
|
-
[cookieNames.sessionSequence]: session.sequence.toString(),
|
|
9
|
-
[getCloudpackSessionVersionCookie(session.projectName)]: session.sessionVersion.toString(),
|
|
10
|
-
[cookieNames.apiUrl]: apiServerUrl ?? '',
|
|
11
|
-
[cookieNames.bundleServerUrl]: bundleServerUrl ?? '',
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=getCookies.js.map
|
package/lib/getCookies.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getCookies.js","sourceRoot":"","sources":["../src/getCookies.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,gCAAgC,EAAE,MAAM,kBAAkB,CAAC;AAEjF;;GAEG;AACH,MAAM,UAAU,UAAU,CACxB,OAA4E,EAC5E,YAAqB,EACrB,eAAwB;IAExB,OAAO;QACL,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,EAAE;QACnC,CAAC,WAAW,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE;QAC1D,CAAC,gCAAgC,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,QAAQ,EAAE;QAC1F,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,YAAY,IAAI,EAAE;QACxC,CAAC,WAAW,CAAC,eAAe,CAAC,EAAE,eAAe,IAAI,EAAE;KACrD,CAAC;AACJ,CAAC","sourcesContent":["import type { Session } from '@ms-cloudpack/api-server';\nimport { cookieNames, getCloudpackSessionVersionCookie } from './cookieNames.js';\n\n/**\n * Get the cookies that needs to be written on the page for Cloudpack to work properly.\n */\nexport function getCookies(\n session: Pick<Session, 'id' | 'sequence' | 'projectName' | 'sessionVersion'>,\n apiServerUrl?: string,\n bundleServerUrl?: string,\n): Record<string, string> {\n return {\n [cookieNames.sessionId]: session.id,\n [cookieNames.sessionSequence]: session.sequence.toString(),\n [getCloudpackSessionVersionCookie(session.projectName)]: session.sessionVersion.toString(),\n [cookieNames.apiUrl]: apiServerUrl ?? '',\n [cookieNames.bundleServerUrl]: bundleServerUrl ?? '',\n };\n}\n"]}
|