@ms-cloudpack/bundle-server 0.6.75 → 0.6.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"handleBundleRequest.d.ts","sourceRoot":"","sources":["../src/handleBundleRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8D,KAAK,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACpH,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAM1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAG1E,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,mBAAmB,GAAG;IAC7B,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,EACD,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,IAAI,CAAC,CA4Hf"}
1
+ {"version":3,"file":"handleBundleRequest.d.ts","sourceRoot":"","sources":["../src/handleBundleRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8D,KAAK,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACpH,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAM1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAG1E,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,mBAAmB,GAAG;IAC7B,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,EACD,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,IAAI,CAAC,CAwIf"}
@@ -40,7 +40,16 @@ export async function handleBundleRequest(options, context) {
40
40
  ctx: context,
41
41
  });
42
42
  if (addedPackageOverride) {
43
- res.status(500).type('text/plain').send('Reload required.');
43
+ // Trigger a re-bundle to ensure there is no stale cache.
44
+ await ensurePackageBundled({
45
+ name: packageName,
46
+ version,
47
+ shouldForce: true,
48
+ shouldRerun: true,
49
+ disableCache,
50
+ }, context);
51
+ // This will never be seen by the user, but it is needed to satisfy the response type.
52
+ res.status(202).type('application/javascript').send('');
44
53
  return;
45
54
  }
46
55
  // If no entry was added, the import path could not resolve to a file in the package.
@@ -1 +1 @@
1
- {"version":3,"file":"handleBundleRequest.js","sourceRoot":"","sources":["../src/handleBundleRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,gBAAgB,EAAgB,MAAM,0BAA0B,CAAC;AAEpH,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,OAGC,EACD,OAAgB;IAEhB,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;IAC3C,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAChD,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACvC,MAAM,EAAE,QAAQ,GAAG,EAAE,EAAE,GAAG,MAAM,CAAC;IACjC,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IACjD,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,KAAK,SAAS,CAAC;IAC5C,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,MAAM,KAAK,SAAS,CAAC;I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;iBAC7B,CAAC,CAAC,CAAC,EACR,CAAC;QACD,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,0BAA0B,CAAC,CAAC;IAC7D,CAAC;IAED,4EAA4E;IAC5E,GAAG,CAAC,SAAS,CAAC,qBAAqB,EAAE,GAAG,CAAC,CAAC;IAE1C,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC;AACvD,CAAC","sourcesContent":["import { ensurePackageBundled, addPackageOverride, errorEntrySource, type Context } from '@ms-cloudpack/api-server';\nimport type { Request, Response } from '@ms-cloudpack/create-express-app';\nimport { parseRequestInfo } from '@ms-cloudpack/import-map';\nimport { findResolveMapEntry } from '@ms-cloudpack/package-utilities';\nimport { isExternalPackage } from '@ms-cloudpack/path-utilities';\nimport { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\nimport type { BundleServerOptions } from './types/BundleServerOptions.js';\nimport { handleWorker } from './handleWorker.js';\n\nexport async function handleBundleRequest(\n options: BundleServerOptions & {\n req: Request;\n res: Response;\n },\n context: Context,\n): Promise<void> {\n const { req, res, disableCache } = options;\n const { session, packageHashes, bus } = context;\n const { resolveMap, config } = session;\n const { features = {} } = config;\n const requestPath = slash(req.path.substring(1));\n const force = req.query.force !== undefined;\n const worker = req.query.worker !== undefined;\n const { packageName, version, hash, bundled, filePath, missing } = parseRequestInfo(requestPath);\n const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });\n\n const packageRequestPath = `${packageName}@${version || '<unknown>'}${filePath}`;\n\n if (!packageEntry) {\n // If the package is missing, invalid arguments.\n console.debug(`Response (404): Unrecognized package \"${packageRequestPath}\"`);\n res.status(404).send(`Unrecognized package \"${packageRequestPath}\".`);\n return;\n }\n\n console.debug(`Request: \"${packageRequestPath}\"`);\n\n // If the package is missing, invalid arguments.\n if (!requestPath || !packageName) {\n console.debug(`Response (400): \"${packageRequestPath}\"`);\n\n res.status(400).send(`Requests must be in the format \"/{packageName}@{version}/{type}/path/file.ext\".`);\n return;\n }\n\n // If the version is missing, redirect to the primary version.\n if (!version) {\n res.redirect(`/${packageName}@${packageEntry.version}${filePath}`);\n return;\n }\n\n if (missing && features.autoUpdateEntries) {\n const addedPackageOverride = await addPackageOverride({\n input: { packageName, version, importPath: filePath, initiatedBy: 'autoUpdateEntries' },\n ctx: context,\n });\n\n if (addedPackageOverride) {\n res.status(500).type('text/plain').send('Reload required.');\n return;\n }\n // If no entry was added, the import path could not resolve to a file in the package.\n const message = `Failed to resolve module specifier \"${packageRequestPath}\".`;\n // We need to pass the error to the bus so that it can be displayed in the UI,\n // and notify the user when autoUpdateEntries is enabled.\n bus.publish(errorEntrySource, message);\n res.status(500).type('text/plain').send(message);\n return;\n }\n\n // If we are accessing a non-bundled asset, return the file directly.\n if (!bundled) {\n const fullPath = path.join(packageEntry.path, filePath);\n\n if (fullPath.length < packageEntry.path.length) {\n console.debug(`Response (400): \"${packageName}@${version}${filePath}\"`);\n\n res.status(400).send(`Invalid file path.`);\n } else {\n console.debug(`Unbundled response returning: \"${packageName}@${version}${filePath}\"`);\n // If the package is external, set cache headers.\n if (isExternalPackage(packageEntry.path)) {\n res.setHeader('Cache-Control', 'public, max-age=31536000');\n }\n\n res.sendFile(path.join(packageEntry.path, filePath));\n }\n return;\n }\n\n console.debug(`Trying to bundle \"${packageName}@${version}\"`);\n const { result } = await ensurePackageBundled(\n {\n name: packageName,\n version,\n shouldForce: force,\n shouldRerun: force,\n shouldWatch: true, // This will only watch the package if it is internal.\n disableCache,\n },\n context,\n );\n\n if (result.errors?.length || !result.outputPath) {\n res.status(500).type('json').send(result);\n return;\n }\n\n // If worker is not set but the file is a worker, handle it.\n if (\n features.enableModuleWorkers &&\n !worker &&\n // Currently, only webpack and rspack setup isWorker in the result.\n // Webpack is fully supported, but support for rspack is still in progress.\n result.outputFiles?.find((file) => file.isWorker && file.outputPath === normalizeRelativePath(filePath))\n ) {\n await handleWorker({ req, res }, context);\n return;\n }\n\n console.debug(`Bundle returning: \"${packageName}@${version}${filePath}\"`);\n // Try to return the result.\n if (\n result.isExternal ||\n (hash !== 'pending' &&\n hash ===\n (await packageHashes.get({\n packagePath: packageEntry.path,\n isSourceHashingEnabled: true,\n targetEnvironment: 'browser',\n })))\n ) {\n res.setHeader('Cache-Control', 'public, max-age=31536000');\n }\n\n // The below header is need to be able to track the browser cache hit ratio.\n res.setHeader('Timing-Allow-Origin', '*');\n\n res.sendFile(path.join(result.outputPath, filePath));\n}\n"]}
1
+ {"version":3,"file":"handleBundleRequest.js","sourceRoot":"","sources":["../src/handleBundleRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,gBAAgB,EAAgB,MAAM,0BAA0B,CAAC;AAEpH,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,OAGC,EACD,OAAgB;IAEhB,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;IAC3C,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAChD,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACvC,MAAM,EAAE,QAAQ,GAAG,EAAE,EAAE,GAAG,MAAM,CAAC;IACjC,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CA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;QAC1C,OAAO;IACT,CAAC;IAED,OAAO,CAAC,KAAK,CAAC,sBAAsB,WAAW,IAAI,OAAO,GAAG,QAAQ,GAAG,CAAC,CAAC;IAC1E,4BAA4B;IAC5B,IACE,MAAM,CAAC,UAAU;QACjB,CAAC,IAAI,KAAK,SAAS;YACjB,IAAI;gBACF,CAAC,MAAM,aAAa,CAAC,GAAG,CAAC;oBACvB,WAAW,EAAE,YAAY,CAAC,IAAI;oBAC9B,sBAAsB,EAAE,IAAI;oBAC5B,iBAAiB,EAAE,SAAS;iBAC7B,CAAC,CAAC,CAAC,EACR,CAAC;QACD,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,0BAA0B,CAAC,CAAC;IAC7D,CAAC;IAED,4EAA4E;IAC5E,GAAG,CAAC,SAAS,CAAC,qBAAqB,EAAE,GAAG,CAAC,CAAC;IAE1C,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC;AACvD,CAAC","sourcesContent":["import { ensurePackageBundled, addPackageOverride, errorEntrySource, type Context } from '@ms-cloudpack/api-server';\nimport type { Request, Response } from '@ms-cloudpack/create-express-app';\nimport { parseRequestInfo } from '@ms-cloudpack/import-map';\nimport { findResolveMapEntry } from '@ms-cloudpack/package-utilities';\nimport { isExternalPackage } from '@ms-cloudpack/path-utilities';\nimport { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\nimport type { BundleServerOptions } from './types/BundleServerOptions.js';\nimport { handleWorker } from './handleWorker.js';\n\nexport async function handleBundleRequest(\n options: BundleServerOptions & {\n req: Request;\n res: Response;\n },\n context: Context,\n): Promise<void> {\n const { req, res, disableCache } = options;\n const { session, packageHashes, bus } = context;\n const { resolveMap, config } = session;\n const { features = {} } = config;\n const requestPath = slash(req.path.substring(1));\n const force = req.query.force !== undefined;\n const worker = req.query.worker !== undefined;\n const { packageName, version, hash, bundled, filePath, missing } = parseRequestInfo(requestPath);\n const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });\n\n const packageRequestPath = `${packageName}@${version || '<unknown>'}${filePath}`;\n\n if (!packageEntry) {\n // If the package is missing, invalid arguments.\n console.debug(`Response (404): Unrecognized package \"${packageRequestPath}\"`);\n res.status(404).send(`Unrecognized package \"${packageRequestPath}\".`);\n return;\n }\n\n console.debug(`Request: \"${packageRequestPath}\"`);\n\n // If the package is missing, invalid arguments.\n if (!requestPath || !packageName) {\n console.debug(`Response (400): \"${packageRequestPath}\"`);\n\n res.status(400).send(`Requests must be in the format \"/{packageName}@{version}/{type}/path/file.ext\".`);\n return;\n }\n\n // If the version is missing, redirect to the primary version.\n if (!version) {\n res.redirect(`/${packageName}@${packageEntry.version}${filePath}`);\n return;\n }\n\n if (missing && features.autoUpdateEntries) {\n const addedPackageOverride = await addPackageOverride({\n input: { packageName, version, importPath: filePath, initiatedBy: 'autoUpdateEntries' },\n ctx: context,\n });\n\n if (addedPackageOverride) {\n // Trigger a re-bundle to ensure there is no stale cache.\n await ensurePackageBundled(\n {\n name: packageName,\n version,\n shouldForce: true,\n shouldRerun: true,\n disableCache,\n },\n context,\n );\n // This will never be seen by the user, but it is needed to satisfy the response type.\n res.status(202).type('application/javascript').send('');\n return;\n }\n // If no entry was added, the import path could not resolve to a file in the package.\n const message = `Failed to resolve module specifier \"${packageRequestPath}\".`;\n // We need to pass the error to the bus so that it can be displayed in the UI,\n // and notify the user when autoUpdateEntries is enabled.\n bus.publish(errorEntrySource, message);\n res.status(500).type('text/plain').send(message);\n return;\n }\n\n // If we are accessing a non-bundled asset, return the file directly.\n if (!bundled) {\n const fullPath = path.join(packageEntry.path, filePath);\n\n if (fullPath.length < packageEntry.path.length) {\n console.debug(`Response (400): \"${packageName}@${version}${filePath}\"`);\n\n res.status(400).send(`Invalid file path.`);\n } else {\n console.debug(`Unbundled response returning: \"${packageName}@${version}${filePath}\"`);\n // If the package is external, set cache headers.\n if (isExternalPackage(packageEntry.path)) {\n res.setHeader('Cache-Control', 'public, max-age=31536000');\n }\n\n res.sendFile(path.join(packageEntry.path, filePath));\n }\n return;\n }\n\n console.debug(`Trying to bundle \"${packageName}@${version}\"`);\n const { result } = await ensurePackageBundled(\n {\n name: packageName,\n version,\n shouldForce: force,\n shouldRerun: force,\n shouldWatch: true, // This will only watch the package if it is internal.\n disableCache,\n },\n context,\n );\n\n if (result.errors?.length || !result.outputPath) {\n res.status(500).type('json').send(result);\n return;\n }\n\n // If worker is not set but the file is a worker, handle it.\n if (\n features.enableModuleWorkers &&\n !worker &&\n // Currently, only webpack and rspack setup isWorker in the result.\n // Webpack is fully supported, but support for rspack is still in progress.\n result.outputFiles?.find((file) => file.isWorker && file.outputPath === normalizeRelativePath(filePath))\n ) {\n await handleWorker({ req, res }, context);\n return;\n }\n\n console.debug(`Bundle returning: \"${packageName}@${version}${filePath}\"`);\n // Try to return the result.\n if (\n result.isExternal ||\n (hash !== 'pending' &&\n hash ===\n (await packageHashes.get({\n packagePath: packageEntry.path,\n isSourceHashingEnabled: true,\n targetEnvironment: 'browser',\n })))\n ) {\n res.setHeader('Cache-Control', 'public, max-age=31536000');\n }\n\n // The below header is need to be able to track the browser cache hit ratio.\n res.setHeader('Timing-Allow-Origin', '*');\n\n res.sendFile(path.join(result.outputPath, filePath));\n}\n"]}
@@ -14,5 +14,5 @@ import type { Request, Response } from '@ms-cloudpack/create-express-app';
14
14
  export declare function handleWorker(options: {
15
15
  req: Request;
16
16
  res: Response;
17
- }, context: PartialContext<'session', 'importMap' | 'urls'>): Promise<void>;
17
+ }, context: PartialContext<'session', 'importMap' | 'urls' | 'config'>): Promise<void>;
18
18
  //# sourceMappingURL=handleWorker.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"handleWorker.d.ts","sourceRoot":"","sources":["../src/handleWorker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAW1E;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE;IACP,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,EACD,OAAO,EAAE,cAAc,CAAC,SAAS,EAAE,WAAW,GAAG,MAAM,CAAC,GACvD,OAAO,CAAC,IAAI,CAAC,CAyEf"}
1
+ {"version":3,"file":"handleWorker.d.ts","sourceRoot":"","sources":["../src/handleWorker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAY1E;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE;IACP,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,EACD,OAAO,EAAE,cAAc,CAAC,SAAS,EAAE,WAAW,GAAG,MAAM,GAAG,QAAQ,CAAC,GAClE,OAAO,CAAC,IAAI,CAAC,CAwEf"}
@@ -4,6 +4,7 @@ import { resolve } from '@ms-cloudpack/path-utilities';
4
4
  import path from 'path';
5
5
  import { fileURLToPath } from 'url';
6
6
  import fsPromises from 'fs/promises';
7
+ import { getInlineScript } from '@ms-cloudpack/inline-scripts';
7
8
  const parentDir = path.dirname(fileURLToPath(import.meta.url) + '/../');
8
9
  let esModuleShim;
9
10
  /**
@@ -65,12 +66,11 @@ export async function handleWorker(options, context) {
65
66
  : {};
66
67
  var window = globalObject;
67
68
 
68
- // Shim process in browser
69
- window.process ??= {};
70
- window.process.env ??= {};
69
+ ${await getInlineScript('defineProcess')}
71
70
 
72
- // Assign true to process.browser for packages like readable-stream
73
- process.browser = true;
71
+ ${await getInlineScript('registerDefineFlags')}
72
+
73
+ registerDefineFlags(window, ${JSON.stringify(session.config.define ?? {})});
74
74
 
75
75
  ${esModuleShim}
76
76
  importShim.addImportMap(${JSON.stringify(session.importMap)});
@@ -1 +1 @@
1
- {"version":3,"file":"handleWorker.js","sourceRoot":"","sources":["../src/handleWorker.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAC5D,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AACvD,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,UAAU,MAAM,aAAa,CAAC;AAErC,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;AACxE,IAAI,YAAgC,CAAC;AAErC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAGC,EACD,OAAwD;IAExD,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAC7B,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAE5B,YAAY,KAAK,MAAM,OAAO,EAAE,CAAC;IAEjC,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,+DAA+D;QAC/D,OAAO,CAAC,KAAK,CAAC,2DAA2D,CAAC,CAAC;QAC3E,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC;QAClE,OAAO;IACT,CAAC;IAED,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAChE,+EAA+E;IAC/E,WAAW,CAAC,YAAY,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAE/C,iHAAiH;IACjH,uFAAuF;IACvF,2FAA2F;IAC3F,oFAAoF;IACpF,2CAA2C;IAC3C,gFAAgF;IAChF,iEAAiE;IACjE,gDAAgD;IAChD,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC,MAAM,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAkCnC,YAAY;8BACY,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC;kBAC7C,WAAW,CAAC,IAAI;;;;;;;;;;;KAW7B,CAAC,CAAC;AACP,CAAC;AAED,KAAK,UAAU,OAAO;IACpB,oEAAoE;IACpE,2FAA2F;IAC3F,MAAM,oBAAoB,GAAG,iBAAiB,CAAC;IAC/C,MAAM,iBAAiB,GAAG,gCAAgC,CAAC;IAC3D,MAAM,iBAAiB,GAAG,MAAM,OAAO,CAAC,oBAAoB,EAAE,SAAS,CAAC,CAAC;IACzE,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,OAAO;IACT,CAAC;IACD,MAAM,iBAAiB,GAAG,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,CAAC,CAAC;IAC1E,IAAI,kBAA0B,CAAC;IAC/B,IAAI,CAAC;QACH,kBAAkB,GAAG,MAAM,UAAU,CAAC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;IAC5E,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO;IACT,CAAC;IACD,OAAO,kBAAkB,CAAC;AAC5B,CAAC","sourcesContent":["import type { PartialContext } from '@ms-cloudpack/api-server';\nimport type { Request, Response } from '@ms-cloudpack/create-express-app';\nimport { makeUrl } from '@ms-cloudpack/path-string-parsing';\nimport { dedent } from 'ts-dedent';\nimport { resolve } from '@ms-cloudpack/path-utilities';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\nimport fsPromises from 'fs/promises';\n\nconst parentDir = path.dirname(fileURLToPath(import.meta.url) + '/../');\nlet esModuleShim: string | undefined;\n\n/**\n * The function handles a worker request by shimming import maps, a global object and process.browser\n * It also adds the worker query parameter so the bundle server knows it has been shimmed,\n * while preserving other query parameters\n *\n * It is called when the feature `enableModuleWorkers` is enabled, the worker has not been handled yet,\n * and the output file is a worker.\n *\n * This works even for workers that are not in the import map, as long as they are in the bundle. It also works\n * when the worker is requested through a blob as the the request goes through the bundle server.\n */\nexport async function handleWorker(\n options: {\n req: Request;\n res: Response;\n },\n context: PartialContext<'session', 'importMap' | 'urls'>,\n): Promise<void> {\n const { req, res } = options;\n const { session } = context;\n\n esModuleShim ??= await getShim();\n\n if (!esModuleShim) {\n // If we can't get the shim, we can't handle the worker request\n console.debug(`Response (500): Failed to load import map shim for worker`);\n res.status(500).send(`Failed to load import map shim for worker`);\n return;\n }\n\n const originalUrl = makeUrl(req.url, session.urls.bundleServer);\n // Add the worker query parameter so the bundle server knows it has ben shimmed\n originalUrl.searchParams.set('worker', 'shim');\n\n // The following code is based on https://github.com/guybedford/es-module-shims?tab=readme-ov-file#module-workers\n // but it uses the bundle server to avoid sending a blob and to preserve url parameters\n // it also shims the global object and process.browser for compatibility with some packages\n // We also need to set up web worker and shared worker temporary handlers right away\n // to receive messages from the main thread\n // This is necessary as there is a bug in the browser that when the handlers are\n // set up inside an async function the messages are not received.\n // In this case importShim is an async function.\n res.type('text/javascript').send(dedent`\n var pending = true;\n var messageQueue = [];\n self.onmessage = function (event) {\n if (pending) {\n messageQueue.push(event);\n }\n };\n self.onconnect = function (event) {\n if (pending) {\n messageQueue.push(event);\n }\n };\n\n // Shim the global object in workers\n var globalObject =\n typeof globalThis !== 'undefined'\n ? globalThis\n : typeof self !== 'undefined'\n ? self\n : typeof window !== 'undefined'\n ? window\n : typeof global !== 'undefined'\n ? global\n : {};\n var window = globalObject;\n\n // Shim process in browser\n window.process ??= {};\n window.process.env ??= {};\n\n // Assign true to process.browser for packages like readable-stream\n process.browser = true;\n\n ${esModuleShim}\n importShim.addImportMap(${JSON.stringify(session.importMap)});\n importShim('${originalUrl.href}')\n .then(() => {\n pending = false;\n // Process the message queue\n while (messageQueue.length) {\n const message = messageQueue.shift();\n self.onmessage(message);\n self.onconnect(message);\n }\n })\n .catch(e => setTimeout(() => { throw e; }));\n `);\n}\n\nasync function getShim(): Promise<string | undefined> {\n // esModuleShims must point to the non-CSP build of ES Module Shims,\n // namely the `es-module-shim.wasm.js` output: es-module-shims/dist/es-module-shims.wasm.js\n const esModuleShimsPackage = 'es-module-shims';\n const esModuleShimsFile = `./dist/es-module-shims.wasm.js`;\n const esModuleShimsPath = await resolve(esModuleShimsPackage, parentDir);\n if (!esModuleShimsPath) {\n return;\n }\n const esModuleShimsWasm = path.join(esModuleShimsPath, esModuleShimsFile);\n let esModuleShimString: string;\n try {\n esModuleShimString = await fsPromises.readFile(esModuleShimsWasm, 'utf8');\n } catch (e) {\n return;\n }\n return esModuleShimString;\n}\n"]}
1
+ {"version":3,"file":"handleWorker.js","sourceRoot":"","sources":["../src/handleWorker.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAC5D,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AACvD,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;AACxE,IAAI,YAAgC,CAAC;AAErC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAGC,EACD,OAAmE;IAEnE,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAC7B,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CA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;IACT,CAAC;IACD,MAAM,iBAAiB,GAAG,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,CAAC,CAAC;IAC1E,IAAI,kBAA0B,CAAC;IAC/B,IAAI,CAAC;QACH,kBAAkB,GAAG,MAAM,UAAU,CAAC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;IAC5E,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO;IACT,CAAC;IACD,OAAO,kBAAkB,CAAC;AAC5B,CAAC","sourcesContent":["import type { PartialContext } from '@ms-cloudpack/api-server';\nimport type { Request, Response } from '@ms-cloudpack/create-express-app';\nimport { makeUrl } from '@ms-cloudpack/path-string-parsing';\nimport { dedent } from 'ts-dedent';\nimport { resolve } from '@ms-cloudpack/path-utilities';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\nimport fsPromises from 'fs/promises';\nimport { getInlineScript } from '@ms-cloudpack/inline-scripts';\n\nconst parentDir = path.dirname(fileURLToPath(import.meta.url) + '/../');\nlet esModuleShim: string | undefined;\n\n/**\n * The function handles a worker request by shimming import maps, a global object and process.browser\n * It also adds the worker query parameter so the bundle server knows it has been shimmed,\n * while preserving other query parameters\n *\n * It is called when the feature `enableModuleWorkers` is enabled, the worker has not been handled yet,\n * and the output file is a worker.\n *\n * This works even for workers that are not in the import map, as long as they are in the bundle. It also works\n * when the worker is requested through a blob as the the request goes through the bundle server.\n */\nexport async function handleWorker(\n options: {\n req: Request;\n res: Response;\n },\n context: PartialContext<'session', 'importMap' | 'urls' | 'config'>,\n): Promise<void> {\n const { req, res } = options;\n const { session } = context;\n\n esModuleShim ??= await getShim();\n\n if (!esModuleShim) {\n // If we can't get the shim, we can't handle the worker request\n console.debug(`Response (500): Failed to load import map shim for worker`);\n res.status(500).send(`Failed to load import map shim for worker`);\n return;\n }\n\n const originalUrl = makeUrl(req.url, session.urls.bundleServer);\n // Add the worker query parameter so the bundle server knows it has ben shimmed\n originalUrl.searchParams.set('worker', 'shim');\n\n // The following code is based on https://github.com/guybedford/es-module-shims?tab=readme-ov-file#module-workers\n // but it uses the bundle server to avoid sending a blob and to preserve url parameters\n // it also shims the global object and process.browser for compatibility with some packages\n // We also need to set up web worker and shared worker temporary handlers right away\n // to receive messages from the main thread\n // This is necessary as there is a bug in the browser that when the handlers are\n // set up inside an async function the messages are not received.\n // In this case importShim is an async function.\n res.type('text/javascript').send(dedent`\n var pending = true;\n var messageQueue = [];\n self.onmessage = function (event) {\n if (pending) {\n messageQueue.push(event);\n }\n };\n self.onconnect = function (event) {\n if (pending) {\n messageQueue.push(event);\n }\n };\n\n // Shim the global object in workers\n var globalObject =\n typeof globalThis !== 'undefined'\n ? globalThis\n : typeof self !== 'undefined'\n ? self\n : typeof window !== 'undefined'\n ? window\n : typeof global !== 'undefined'\n ? global\n : {};\n var window = globalObject;\n\n ${await getInlineScript('defineProcess')}\n\n ${await getInlineScript('registerDefineFlags')}\n\n registerDefineFlags(window, ${JSON.stringify(session.config.define ?? {})});\n\n ${esModuleShim}\n importShim.addImportMap(${JSON.stringify(session.importMap)});\n importShim('${originalUrl.href}')\n .then(() => {\n pending = false;\n // Process the message queue\n while (messageQueue.length) {\n const message = messageQueue.shift();\n self.onmessage(message);\n self.onconnect(message);\n }\n })\n .catch(e => setTimeout(() => { throw e; }));\n `);\n}\n\nasync function getShim(): Promise<string | undefined> {\n // esModuleShims must point to the non-CSP build of ES Module Shims,\n // namely the `es-module-shim.wasm.js` output: es-module-shims/dist/es-module-shims.wasm.js\n const esModuleShimsPackage = 'es-module-shims';\n const esModuleShimsFile = `./dist/es-module-shims.wasm.js`;\n const esModuleShimsPath = await resolve(esModuleShimsPackage, parentDir);\n if (!esModuleShimsPath) {\n return;\n }\n const esModuleShimsWasm = path.join(esModuleShimsPath, esModuleShimsFile);\n let esModuleShimString: string;\n try {\n esModuleShimString = await fsPromises.readFile(esModuleShimsWasm, 'utf8');\n } catch (e) {\n return;\n }\n return esModuleShimString;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/bundle-server",
3
- "version": "0.6.75",
3
+ "version": "0.6.77",
4
4
  "description": "An implementation of the Bundle server for Cloudpack.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,12 +14,13 @@
14
14
  }
15
15
  },
16
16
  "dependencies": {
17
- "@ms-cloudpack/api-server": "^0.61.14",
18
- "@ms-cloudpack/create-express-app": "^1.10.22",
19
- "@ms-cloudpack/import-map": "^0.10.3",
20
- "@ms-cloudpack/package-utilities": "^12.0.1",
17
+ "@ms-cloudpack/api-server": "^0.61.16",
18
+ "@ms-cloudpack/create-express-app": "^1.10.23",
19
+ "@ms-cloudpack/import-map": "^0.10.4",
20
+ "@ms-cloudpack/inline-scripts": "^0.1.1",
21
+ "@ms-cloudpack/package-utilities": "^12.0.2",
21
22
  "@ms-cloudpack/path-string-parsing": "^1.2.6",
22
- "@ms-cloudpack/path-utilities": "^3.0.7",
23
+ "@ms-cloudpack/path-utilities": "^3.0.8",
23
24
  "es-module-shims": "^1.10.0",
24
25
  "ts-dedent": "^2.2.0"
25
26
  },