@ms-cloudpack/app-server 0.6.0 → 0.7.0

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.
@@ -6,7 +6,7 @@ interface CreateRoutesOptions {
6
6
  url: string;
7
7
  definition: PackageJson;
8
8
  }
9
- export type CreateRoutesContext = Pick<Context, 'packages' | 'packageImportPaths' | 'packageHashes' | 'session'>;
9
+ type CreateRoutesContext = Pick<Context, 'packages' | 'packageImportPaths' | 'packageHashes' | 'session'>;
10
10
  /**
11
11
  * Creates the routes for the express app based on the config.
12
12
  */
@@ -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,KAAK,EAAE,WAAW,EAAS,MAAM,4BAA4B,CAAC;AACrE,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,kCAAkC,CAAC;AASnF,UAAU,mBAAmB;IAC3B,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,WAAW,CAAC;CACzB;AAED,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,oBAAoB,GAAG,eAAe,GAAG,SAAS,CAAC,CAAC;AAEjH;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,EAAE,OAAO,EAAE,mBAAmB,QA6BtF"}
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,KAAK,EAAE,WAAW,EAAS,MAAM,4BAA4B,CAAC;AACrE,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,kCAAkC,CAAC;AASnF,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,QA6BtF"}
@@ -40,8 +40,14 @@ export function createRoutes(options, context) {
40
40
  async function handleRequest(options, context) {
41
41
  const { req, res, definition, route, url } = options;
42
42
  const { session } = context;
43
+ const { bundleServer } = session.urls;
44
+ if (!bundleServer) {
45
+ // Sanity check...
46
+ throw new Error('The bundle server URL is not yet set in the session (this is a Cloudpack bug).');
47
+ }
43
48
  // Build the import map if it hasn't been built yet for this session version.
44
- const importMap = (session.importMap ??= await createImportMap({ ...context, ...context.session }));
49
+ // (TS can't infer that urls.bundleServer is set on session from a check above.)
50
+ const importMap = (session.importMap ??= await createImportMap({ ...context, ...session, urls: { bundleServer } }));
45
51
  // Parse the request path, extension, grab the overlay script path.
46
52
  const requestPath = slash(req.path.substring(1));
47
53
  const requestExt = path.extname(requestPath);
@@ -1 +1 @@
1
- {"version":3,"file":"createRoutes.js","sourceRoot":"","sources":["../src/createRoutes.ts"],"names":[],"mappings":"AAGA,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;AAU7C;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,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,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 type { PackageJson, Route } 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';\n\ninterface CreateRoutesOptions {\n app: Express;\n url: string;\n definition: PackageJson;\n}\n\nexport type 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 routes = [...(session.config.devServer?.routes || [])];\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 (route.staticPath) {\n app.use(route.match, express.static(path.resolve(session.appPath, route.staticPath)));\n } else {\n app.get(route.match, (req: Request, res: Response) => {\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 }\n }\n}\n\nasync function handleRequest(\n options: Omit<CreateRoutesOptions, 'app'> & {\n req: Request;\n res: Response;\n route: Route;\n },\n context: CreateRoutesContext,\n) {\n const { req, res, definition, route, url } = options;\n const { session } = context;\n\n // Build the import map if it hasn't been built yet for this session version.\n const importMap = (session.importMap ??= await createImportMap({ ...context, ...context.session }));\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 const entryScript =\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n route.exportEntry && importMap.imports[slash(path.join(definition.name!, route.exportEntry))];\n\n // Set the appropriate Cloudpack headers/cookies in the response.\n // TODO: setting the headers here prohibits cases where the page rendering is owned by existing server code\n // that can only accept changing the scripts. We should consider moving to a model where an initial script\n // fetch loads the import map and sets Cloudpack settings in local storage rather than headers/cookies.\n setHeaders({ res, session });\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
+ {"version":3,"file":"createRoutes.js","sourceRoot":"","sources":["../src/createRoutes.ts"],"names":[],"mappings":"AAGA,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;AAU7C;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,OAA4B,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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 type { PackageJson, Route } 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';\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 routes = [...(session.config.devServer?.routes || [])];\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 (route.staticPath) {\n app.use(route.match, express.static(path.resolve(session.appPath, route.staticPath)));\n } else {\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 }\n }\n}\n\nasync function handleRequest(\n options: Omit<CreateRoutesOptions, 'app'> & {\n req: Request;\n res: Response;\n route: Route;\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 const entryScript =\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n route.exportEntry && importMap.imports[slash(path.join(definition.name!, route.exportEntry))];\n\n // Set the appropriate Cloudpack headers/cookies in the response.\n // TODO: setting the headers here prohibits cases where the page rendering is owned by existing server code\n // that can only accept changing the scripts. We should consider moving to a model where an initial script\n // fetch loads the import map and sets Cloudpack settings in local storage rather than headers/cookies.\n setHeaders({ res, session });\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"]}
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { startAppServer } from './startAppServer.js';
1
+ export { startServers } from './startServers.js';
2
2
  export type { AppServer } from './types/AppServer.js';
3
3
  export type { RenderRouteFunction } from './types/RenderRouteFunction.js';
4
4
  export type { RenderRouteOptions } from './types/RenderRouteOptions.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,YAAY,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACtD,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,YAAY,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACxE,YAAY,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACtE,YAAY,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACtD,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,YAAY,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACxE,YAAY,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACtE,YAAY,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC"}
package/lib/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { startAppServer } from './startAppServer.js';
1
+ export { startServers } from './startServers.js';
2
2
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC","sourcesContent":["export { startAppServer } from './startAppServer.js';\nexport type { AppServer } from './types/AppServer.js';\nexport type { RenderRouteFunction } from './types/RenderRouteFunction.js';\nexport type { RenderRouteOptions } from './types/RenderRouteOptions.js';\nexport type { RenderRouteResult } from './types/RenderRouteResult.js';\nexport type { RenderRouteScript } from './types/RenderRouteScript.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC","sourcesContent":["export { startServers } from './startServers.js';\nexport type { AppServer } from './types/AppServer.js';\nexport type { RenderRouteFunction } from './types/RenderRouteFunction.js';\nexport type { RenderRouteOptions } from './types/RenderRouteOptions.js';\nexport type { RenderRouteResult } from './types/RenderRouteResult.js';\nexport type { RenderRouteScript } from './types/RenderRouteScript.js';\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"renderRoute.d.ts","sourceRoot":"","sources":["../../src/renderRoute/renderRoute.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AAMxD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,KAAK,EAAE,yBAAyB,EAAqB,MAAM,+BAA+B,CAAC;AAMlG,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG;IAAE,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAEpE;;;GAGG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAoDjG"}
1
+ {"version":3,"file":"renderRoute.d.ts","sourceRoot":"","sources":["../../src/renderRoute/renderRoute.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AAOxD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,KAAK,EAAE,yBAAyB,EAAqB,MAAM,+BAA+B,CAAC;AAKlG,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG;IAAE,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAEpE;;;GAGG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAoDjG"}
@@ -1,10 +1,10 @@
1
+ import { makeUrl } from '@ms-cloudpack/path-string-parsing';
1
2
  import fsPromises from 'fs/promises';
2
3
  import { JSDOM } from 'jsdom';
3
4
  import path from 'path';
4
5
  import { pathToFileURL } from 'url';
5
6
  import { getDefaultHtmlResponse } from './getDefaultHtmlResponse.js';
6
7
  import { getHtmlErrorResponse } from './getHtmlErrorResponse.js';
7
- import { makeUrl } from '@ms-cloudpack/path-string-parsing';
8
8
  /**
9
9
  * Get the response for the given route. If the route has a custom render script, use that.
10
10
  * Returns an error response if the file referenced by the script doesn't exist or throws an error.
@@ -1 +1 @@
1
- {"version":3,"file":"renderRoute.js","sourceRoot":"","sources":["../../src/renderRoute/renderRoute.ts"],"names":[],"mappings":"AACA,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,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAI5D;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,OAA2B;IAC3D,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAExC,iGAAiG;IACjG,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC;IAC/D,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IAC7C,qGAAqG;IACrG,+FA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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 { makeUrl } from '@ms-cloudpack/path-string-parsing';\n\nexport type RouteInternal = Route & { 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\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(session.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 // inject the import map.\n if (entryScript || overlayScript) {\n addScript({ document, type: 'importmap', 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,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,EAAE,MAAM,2BAA2B,CAAC;AAIjE;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,OAA2B;IAC3D,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAExC,iGAAiG;IACjG,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC;IAC/D,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;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import { 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 = Route & { 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\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(session.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 // inject the import map.\n if (entryScript || overlayScript) {\n addScript({ document, type: 'importmap', 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,20 +1,30 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import type { PartialContext } from '@ms-cloudpack/api-server';
3
+ import { type BundleServer, type BundleServerContext, type BundleServerOptions } from '@ms-cloudpack/bundle-server';
3
4
  import type { PackageJson } from '@ms-cloudpack/common-types';
4
5
  import type { Server } from 'http';
5
- import { type CreateRoutesContext } from './createRoutes.js';
6
6
  import type { AppServer } from './types/AppServer.js';
7
7
  /**
8
+ * Start the app server, and (usually) the bundle server (but see below).
9
+ *
8
10
  * The app server hosts the appropriate routes for the web app, primarily returning html content
9
11
  * which loads resources from the bundle server.
10
12
  *
11
13
  * Separating the app server from the bundle service keeps the routes separate - the app server
12
14
  * can support whichever routes the app needs, while the bundle server can provide package
13
15
  * assets in various forms using its own routing.
16
+ *
17
+ * Exception: some projects need the bundle server and app server on the same origin for local
18
+ * development due to security policies. If the `useSingleWebServer` feature is enabled, the app
19
+ * server will also serve bundles rather than creating a separate bundle server.
14
20
  */
15
- export declare function startAppServer(options: {
21
+ export declare function startServers(options: {
16
22
  definition: PackageJson;
17
23
  middlewareMode?: boolean;
18
24
  server?: Server;
19
- }, context: PartialContext<'reporter', 'config' | 'projectName'> & CreateRoutesContext): Promise<AppServer>;
20
- //# sourceMappingURL=startAppServer.d.ts.map
25
+ bundleServerOptions: BundleServerOptions;
26
+ }, context: PartialContext<'reporter', 'config' | 'projectName'> & BundleServerContext): Promise<{
27
+ appServer: AppServer;
28
+ bundleServer?: BundleServer;
29
+ }>;
30
+ //# sourceMappingURL=startServers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startServers.d.ts","sourceRoot":"","sources":["../src/startServers.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EACzB,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAI9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE;IACP,UAAU,EAAE,WAAW,CAAC;IACxB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mBAAmB,EAAE,mBAAmB,CAAC;CAC1C,EACD,OAAO,EAAE,cAAc,CAAC,UAAU,EAAE,QAAQ,GAAG,aAAa,CAAC,GAAG,mBAAmB,GAClF,OAAO,CAAC;IACT,SAAS,EAAE,SAAS,CAAC;IACrB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC,CAqDD"}
@@ -1,17 +1,25 @@
1
+ import { handleBundleRequest, startBundleServer, } from '@ms-cloudpack/bundle-server';
1
2
  import { createExpressApp } from '@ms-cloudpack/create-express-app';
3
+ import { combinedServerBundleRequestPrefix } from '@ms-cloudpack/import-map';
2
4
  import { cyan } from '@ms-cloudpack/task-reporter';
3
5
  import { createRoutes } from './createRoutes.js';
4
6
  import { handleErrorPortUnavailable } from './handleErrorPortUnavailable.js';
5
7
  /**
8
+ * Start the app server, and (usually) the bundle server (but see below).
9
+ *
6
10
  * The app server hosts the appropriate routes for the web app, primarily returning html content
7
11
  * which loads resources from the bundle server.
8
12
  *
9
13
  * Separating the app server from the bundle service keeps the routes separate - the app server
10
14
  * can support whichever routes the app needs, while the bundle server can provide package
11
15
  * assets in various forms using its own routing.
16
+ *
17
+ * Exception: some projects need the bundle server and app server on the same origin for local
18
+ * development due to security policies. If the `useSingleWebServer` feature is enabled, the app
19
+ * server will also serve bundles rather than creating a separate bundle server.
12
20
  */
13
- export async function startAppServer(options, context) {
14
- const { definition, middlewareMode, server } = options;
21
+ export async function startServers(options, context) {
22
+ const { definition, middlewareMode, server, bundleServerOptions } = options;
15
23
  const { session, reporter } = context;
16
24
  const { config } = session;
17
25
  const { devServer = {} } = config;
@@ -29,9 +37,28 @@ export async function startAppServer(options, context) {
29
37
  middlewareMode,
30
38
  server,
31
39
  });
40
+ session.urls.appServer = url;
41
+ let bundleServer;
42
+ if (config.features?.useSingleWebServer) {
43
+ // Add a route on this server for bundles.
44
+ session.urls.bundleServer = `${url}/${combinedServerBundleRequestPrefix}`;
45
+ app.get(`/${combinedServerBundleRequestPrefix}/*`, (req, res) => {
46
+ handleBundleRequest({ req, res, ...bundleServerOptions }, context).catch((err) => {
47
+ console.error(err?.stack || err);
48
+ res.status(500).send(`Error bundling: ${err}`);
49
+ });
50
+ });
51
+ }
52
+ else {
53
+ // Start the bundle server.
54
+ bundleServer = await startBundleServer(bundleServerOptions, context);
55
+ }
32
56
  createRoutes({ ...options, app, url }, context);
33
57
  task.complete({ message: `Available @ ${cyan(url)}`, forceShow: true });
34
- return { app, close, port, url };
58
+ return {
59
+ appServer: { app, close, port, url },
60
+ bundleServer,
61
+ };
35
62
  }
36
63
  catch (err) {
37
64
  task.complete({ status: 'fail', message: 'Failed to start app server', forceShow: true });
@@ -39,4 +66,4 @@ export async function startAppServer(options, context) {
39
66
  throw err;
40
67
  }
41
68
  }
42
- //# sourceMappingURL=startAppServer.js.map
69
+ //# sourceMappingURL=startServers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startServers.js","sourceRoot":"","sources":["../src/startServers.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,iBAAiB,GAIlB,MAAM,6BAA6B,CAAC;AAErC,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAE,iCAAiC,EAAE,MAAM,0BAA0B,CAAC;AAC7E,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;AAG7E;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAKC,EACD,OAAmF;IAKnF,MAAM,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,EAAE,mBAAmB,EAAE,GAAG,OAAO,CAAC;IAC5E,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IACtC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC3B,MAAM,EAAE,SAAS,GAAG,EAAE,EAAE,GAAG,MAAM,CAAC;IAElC,mEAAmE;IACnE,MAAM,oBAAoB,GAAG,SAAS,CAAC,IAAI,KAAK,SAAS,CAAC;IAC1D,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAEzD,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,4BAA4B,UAAU,CAAC,IAAI,GAAG,CAAC,CAAC;IAE9E,IAAI,CAAC;QACH,MAAM,EAAE,GAAG,EAAE,KAAK,EAA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AAE,IAAI,EAAE,CAAC,CAAC;QAExE,OAAO;YACL,SAAS,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE;YACpC,YAAY;SACb,CAAC;IACJ,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACtB,IAAI,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,4BAA4B,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1F,0BAA0B,CAAC,GAAG,EAAE,OAAO,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;QAE5D,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC","sourcesContent":["import type { PartialContext } from '@ms-cloudpack/api-server';\nimport {\n handleBundleRequest,\n startBundleServer,\n type BundleServer,\n type BundleServerContext,\n type BundleServerOptions,\n} from '@ms-cloudpack/bundle-server';\nimport type { PackageJson } from '@ms-cloudpack/common-types';\nimport { createExpressApp } from '@ms-cloudpack/create-express-app';\nimport { combinedServerBundleRequestPrefix } from '@ms-cloudpack/import-map';\nimport { cyan } from '@ms-cloudpack/task-reporter';\nimport type { Server } from 'http';\nimport { createRoutes } from './createRoutes.js';\nimport { handleErrorPortUnavailable } from './handleErrorPortUnavailable.js';\nimport type { AppServer } from './types/AppServer.js';\n\n/**\n * Start the app server, and (usually) the bundle server (but see below).\n *\n * The app server hosts the appropriate routes for the web app, primarily returning html content\n * which loads resources from the bundle server.\n *\n * Separating the app server from the bundle service keeps the routes separate - the app server\n * can support whichever routes the app needs, while the bundle server can provide package\n * assets in various forms using its own routing.\n *\n * Exception: some projects need the bundle server and app server on the same origin for local\n * development due to security policies. If the `useSingleWebServer` feature is enabled, the app\n * server will also serve bundles rather than creating a separate bundle server.\n */\nexport async function startServers(\n options: {\n definition: PackageJson;\n middlewareMode?: boolean;\n server?: Server;\n bundleServerOptions: BundleServerOptions;\n },\n context: PartialContext<'reporter', 'config' | 'projectName'> & BundleServerContext,\n): Promise<{\n appServer: AppServer;\n bundleServer?: BundleServer;\n}> {\n const { definition, middlewareMode, server, bundleServerOptions } = options;\n const { session, reporter } = context;\n const { config } = session;\n const { devServer = {} } = config;\n\n // Read the port from the config file or default to array of ports.\n const requireSpecifiedPort = devServer.port !== undefined;\n const ports = devServer.port ?? [5000, 5001, 5002, 5003];\n\n const task = reporter.addTask(`Starting app server for \"${definition.name}\"`);\n\n try {\n const { app, close, port, url } = await createExpressApp({\n portRange: ports,\n requireSpecifiedPort,\n hostname: devServer?.domain,\n requestHeaders: devServer?.requestHeaders,\n sslOptions: devServer?.https,\n middlewareMode,\n server,\n });\n session.urls.appServer = url;\n\n let bundleServer: BundleServer | undefined;\n if (config.features?.useSingleWebServer) {\n // Add a route on this server for bundles.\n session.urls.bundleServer = `${url}/${combinedServerBundleRequestPrefix}`;\n app.get(`/${combinedServerBundleRequestPrefix}/*`, (req, res) => {\n handleBundleRequest({ req, res, ...bundleServerOptions }, context).catch((err) => {\n console.error((err as Error)?.stack || err);\n res.status(500).send(`Error bundling: ${err}`);\n });\n });\n } else {\n // Start the bundle server.\n bundleServer = await startBundleServer(bundleServerOptions, context);\n }\n\n createRoutes({ ...options, app, url }, context);\n\n task.complete({ message: `Available @ ${cyan(url)}`, forceShow: true });\n\n return {\n appServer: { app, close, port, url },\n bundleServer,\n };\n } catch (err: unknown) {\n task.complete({ status: 'fail', message: 'Failed to start app server', forceShow: true });\n handleErrorPortUnavailable(err, session.projectName, ports);\n\n throw err;\n }\n}\n"]}
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.39.4"
8
+ "packageVersion": "7.43.2"
9
9
  }
10
10
  ]
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/app-server",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "An implementation of the App server for Cloudpack.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,15 +14,16 @@
14
14
  }
15
15
  },
16
16
  "dependencies": {
17
- "@ms-cloudpack/api-server": "^0.42.1",
18
- "@ms-cloudpack/common-types": "^0.5.0",
19
- "@ms-cloudpack/create-express-app": "^1.6.10",
20
- "@ms-cloudpack/import-map": "^0.4.0",
21
- "@ms-cloudpack/overlay": "^0.16.112",
17
+ "@ms-cloudpack/api-server": "^0.43.0",
18
+ "@ms-cloudpack/bundle-server": "^0.4.1",
19
+ "@ms-cloudpack/common-types": "^0.5.1",
20
+ "@ms-cloudpack/create-express-app": "^1.6.11",
21
+ "@ms-cloudpack/import-map": "^0.5.0",
22
+ "@ms-cloudpack/overlay": "^0.16.113",
22
23
  "@ms-cloudpack/path-string-parsing": "^1.2.2",
23
- "@ms-cloudpack/path-utilities": "^2.7.7",
24
+ "@ms-cloudpack/path-utilities": "^2.7.8",
24
25
  "@ms-cloudpack/task-reporter": "^0.14.0",
25
- "jsdom": "^22.0.0"
26
+ "jsdom": "^24.0.0"
26
27
  },
27
28
  "devDependencies": {
28
29
  "@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
@@ -1 +0,0 @@
1
- {"version":3,"file":"startAppServer.d.ts","sourceRoot":"","sources":["../src/startAppServer.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAG9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,EAAgB,KAAK,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAE3E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE;IACP,UAAU,EAAE,WAAW,CAAC;IACxB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAA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EAAE,SAAS,EAAE,cAAc;YACzC,UAAU,EAAE,SAAS,EAAE,KAAK;YAC5B,cAAc;YACd,MAAM;SACP,CAAC,CAAC;QAEH,YAAY,CAAC,EAAE,GAAG,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,OAAO,CAAC,CAAC;QAEhD,IAAI,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,eAAe,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAExE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;IACnC,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACtB,IAAI,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,4BAA4B,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1F,0BAA0B,CAAC,GAAG,EAAE,OAAO,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;QAE5D,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC","sourcesContent":["import type { PartialContext } from '@ms-cloudpack/api-server';\nimport type { PackageJson } from '@ms-cloudpack/common-types';\nimport { createExpressApp } from '@ms-cloudpack/create-express-app';\nimport { cyan } from '@ms-cloudpack/task-reporter';\nimport type { Server } from 'http';\nimport { createRoutes, type CreateRoutesContext } from './createRoutes.js';\nimport { handleErrorPortUnavailable } from './handleErrorPortUnavailable.js';\nimport type { AppServer } from './types/AppServer.js';\n\n/**\n * The app server hosts the appropriate routes for the web app, primarily returning html content\n * which loads resources from the bundle server.\n *\n * Separating the app server from the bundle service keeps the routes separate - the app server\n * can support whichever routes the app needs, while the bundle server can provide package\n * assets in various forms using its own routing.\n */\nexport async function startAppServer(\n options: {\n definition: PackageJson;\n middlewareMode?: boolean;\n server?: Server;\n },\n context: PartialContext<'reporter', 'config' | 'projectName'> & CreateRoutesContext,\n): Promise<AppServer> {\n const { definition, middlewareMode, server } = options;\n const { session, reporter } = context;\n const { config } = session;\n const { devServer = {} } = config;\n\n // Read the port from the config file or default to array of ports.\n const requireSpecifiedPort = devServer.port !== undefined;\n const ports = devServer.port ?? [5000, 5001, 5002, 5003];\n\n const task = reporter.addTask(`Starting app server for \"${definition.name}\"`);\n\n try {\n const { app, close, port, url } = await createExpressApp({\n portRange: ports,\n requireSpecifiedPort,\n hostname: devServer?.domain,\n requestHeaders: devServer?.requestHeaders,\n sslOptions: devServer?.https,\n middlewareMode,\n server,\n });\n\n createRoutes({ ...options, app, url }, context);\n\n task.complete({ message: `Available @ ${cyan(url)}`, forceShow: true });\n\n return { app, close, port, url };\n } catch (err: unknown) {\n task.complete({ status: 'fail', message: 'Failed to start app server', forceShow: true });\n handleErrorPortUnavailable(err, session.projectName, ports);\n\n throw err;\n }\n}\n"]}