@ms-cloudpack/bundle-server 0.5.5 → 0.5.7

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":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAK1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAE1E,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,mBAAmB,GAAG;IAC7B,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,EACD,OAAO,EAAE,mBAAmB,iBAyG7B"}
1
+ {"version":3,"file":"handleBundleRequest.d.ts","sourceRoot":"","sources":["../src/handleBundleRequest.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAK1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,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,mBAAmB,iBAsH7B"}
@@ -1,8 +1,9 @@
1
1
  import { ensurePackageBundled, addPackageOverride, errorEntrySource } from '@ms-cloudpack/api-server';
2
2
  import { parseRequestInfo } from '@ms-cloudpack/import-map';
3
3
  import { findResolveMapEntry, isExternalPackage } from '@ms-cloudpack/package-utilities';
4
- import { slash } from '@ms-cloudpack/path-string-parsing';
4
+ import { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';
5
5
  import path from 'path';
6
+ import { handleWorker } from './handleWorker.js';
6
7
  export async function handleBundleRequest(options, context) {
7
8
  const { req, res, disableCache } = options;
8
9
  const { session, packageHashes, bus } = context;
@@ -10,6 +11,7 @@ export async function handleBundleRequest(options, context) {
10
11
  const { features = {} } = config;
11
12
  const requestPath = slash(req.path.substring(1));
12
13
  const force = req.query.force !== undefined;
14
+ const worker = req.query.worker !== undefined;
13
15
  const { packageName, version, hash, bundled, filePath, missing } = parseRequestInfo(requestPath);
14
16
  const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });
15
17
  if (!packageEntry) {
@@ -80,6 +82,15 @@ export async function handleBundleRequest(options, context) {
80
82
  res.status(500).type('json').send(result);
81
83
  return;
82
84
  }
85
+ // If worker is not set but the file is a worker, handle it.
86
+ if (features.enableModuleWorkers &&
87
+ !worker &&
88
+ // This is only supported for webpack for now.
89
+ result.bundler === 'webpack' &&
90
+ result.outputFiles?.find((file) => file.isWorker && file.outputPath === normalizeRelativePath(filePath))) {
91
+ await handleWorker({ req, res }, context);
92
+ return;
93
+ }
83
94
  console.debug(`Bundle returning: "${packageName}@${version}${filePath}"`);
84
95
  // Try to return the result.
85
96
  if (result.isExternal ||
@@ -1 +1 @@
1
- {"version":3,"file":"handleBundleRequest.js","sourceRoot":"","sources":["../src/handleBundleRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAEtG,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACzF,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAC1D,OAAO,IAAI,MAAM,MAAM,CAAC;AAIxB,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,OAGC,EACD,OAA4B;IAE5B,MAAM,EAAE,GA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,EACvG,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 } from '@ms-cloudpack/api-server';\nimport type { Request, Response } from '@ms-cloudpack/create-express-app';\nimport { parseRequestInfo } from '@ms-cloudpack/import-map';\nimport { findResolveMapEntry, isExternalPackage } from '@ms-cloudpack/package-utilities';\nimport { slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\nimport type { BundleServerContext } from './types/BundleServerContext.js';\nimport type { BundleServerOptions } from './types/BundleServerOptions.js';\n\nexport async function handleBundleRequest(\n options: BundleServerOptions & {\n req: Request;\n res: Response;\n },\n context: BundleServerContext,\n) {\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 { packageName, version, hash, bundled, filePath, missing } = parseRequestInfo(requestPath);\n const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });\n\n if (!packageEntry) {\n // If the package is missing, invalid arguments.\n console.debug(`Response (404): Unrecognized package \"${packageName}\"`);\n res.status(404).send(`Unrecognized package \"${packageName}\".`);\n return;\n }\n\n console.debug(`Request: \"${packageName}@${version}${filePath}\"`);\n\n // If the package is missing, invalid arguments.\n if (!requestPath || !packageName) {\n console.debug(`Response (400): \"${packageName}@${version}${filePath}\"`);\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 },\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 \"${packageName}@${version}${filePath}\".`;\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 input: {\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 ctx: context,\n });\n\n if (result.errors?.length) {\n res.status(500).type('json').send(result);\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 === (await packageHashes.get({ packagePath: packageEntry.path, isSourceHashingEnabled: true })))\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":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ensurePackageBundled, addPackageOverride, errorEntrySource } from '@ms-cloudpack/api-server';\nimport type { Request, Response } from '@ms-cloudpack/create-express-app';\nimport { parseRequestInfo } from '@ms-cloudpack/import-map';\nimport { findResolveMapEntry, isExternalPackage } from '@ms-cloudpack/package-utilities';\nimport { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\nimport type { BundleServerContext } from './types/BundleServerContext.js';\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: BundleServerContext,\n) {\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 if (!packageEntry) {\n // If the package is missing, invalid arguments.\n console.debug(`Response (404): Unrecognized package \"${packageName}\"`);\n res.status(404).send(`Unrecognized package \"${packageName}\".`);\n return;\n }\n\n console.debug(`Request: \"${packageName}@${version}${filePath}\"`);\n\n // If the package is missing, invalid arguments.\n if (!requestPath || !packageName) {\n console.debug(`Response (400): \"${packageName}@${version}${filePath}\"`);\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 },\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 \"${packageName}@${version}${filePath}\".`;\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 input: {\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 ctx: context,\n });\n\n if (result.errors?.length) {\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 // This is only supported for webpack for now.\n result.bundler === 'webpack' &&\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 === (await packageHashes.get({ packagePath: packageEntry.path, isSourceHashingEnabled: true })))\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"]}
@@ -0,0 +1,18 @@
1
+ import type { Request, Response } from '@ms-cloudpack/create-express-app';
2
+ import type { BundleServerContext } from './types/BundleServerContext.js';
3
+ /**
4
+ * The function handles a worker request by shimming import maps, a global object and process.browser
5
+ * It also adds the worker query parameter so the bundle server knows it has been shimmed,
6
+ * while preserving other query parameters
7
+ *
8
+ * It is called when the feature `enableModuleWorkers` is enabled, the worker has not been handled yet,
9
+ * and the output file is a worker.
10
+ *
11
+ * This works even for workers that are not in the import map, as long as they are in the bundle. It also works
12
+ * when the worker is requested through a blob as the the request goes through the bundle server.
13
+ */
14
+ export declare function handleWorker(options: {
15
+ req: Request;
16
+ res: Response;
17
+ }, context: BundleServerContext): Promise<void>;
18
+ //# sourceMappingURL=handleWorker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handleWorker.d.ts","sourceRoot":"","sources":["../src/handleWorker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAW1E;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE;IACP,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,EACD,OAAO,EAAE,mBAAmB,iBA8C7B"}
@@ -0,0 +1,81 @@
1
+ import { makeUrl } from '@ms-cloudpack/path-string-parsing';
2
+ import { dedent } from 'ts-dedent';
3
+ import { resolve } from '@ms-cloudpack/package-utilities';
4
+ import path from 'path';
5
+ import { fileURLToPath } from 'url';
6
+ import fsPromises from 'fs/promises';
7
+ const parentDir = path.dirname(fileURLToPath(import.meta.url) + '/../');
8
+ let esModuleShim;
9
+ /**
10
+ * The function handles a worker request by shimming import maps, a global object and process.browser
11
+ * It also adds the worker query parameter so the bundle server knows it has been shimmed,
12
+ * while preserving other query parameters
13
+ *
14
+ * It is called when the feature `enableModuleWorkers` is enabled, the worker has not been handled yet,
15
+ * and the output file is a worker.
16
+ *
17
+ * This works even for workers that are not in the import map, as long as they are in the bundle. It also works
18
+ * when the worker is requested through a blob as the the request goes through the bundle server.
19
+ */
20
+ export async function handleWorker(options, context) {
21
+ const { req, res } = options;
22
+ const { session } = context;
23
+ esModuleShim ??= await getShim();
24
+ if (!esModuleShim) {
25
+ // If we can't get the shim, we can't handle the worker request
26
+ console.debug(`Response (500): Failed to load import map shim for worker`);
27
+ res.status(500).send(`Failed to load import map shim for worker`);
28
+ return;
29
+ }
30
+ const originalUrl = makeUrl(req.url, session.urls.bundleServer);
31
+ // Add the worker query parameter so the bundle server knows it has ben shimmed
32
+ originalUrl.searchParams.set('worker', 'shim');
33
+ // The following code is based on https://github.com/guybedford/es-module-shims?tab=readme-ov-file#module-workers
34
+ // but it uses the bundle server to avoid sending a blob and to preserve url parameters
35
+ // it also shims the global object and process.browser for compatibility with some packages
36
+ res.type('text/javascript').send(dedent `
37
+ // Shim the global object in workers
38
+ var globalObject =
39
+ typeof globalThis !== 'undefined'
40
+ ? globalThis
41
+ : typeof self !== 'undefined'
42
+ ? self
43
+ : typeof window !== 'undefined'
44
+ ? window
45
+ : typeof global !== 'undefined'
46
+ ? global
47
+ : {};
48
+ var window = globalObject;
49
+
50
+ // Shim process in browser
51
+ window.process ??= {};
52
+ window.process.env ??= {};
53
+
54
+ // Assign true to process.browser for packages like readable-stream
55
+ process.browser = true;
56
+
57
+ ${esModuleShim}
58
+ importShim.addImportMap(${JSON.stringify(session.importMap)});
59
+ importShim('${originalUrl.href}').catch(e => setTimeout(() => { throw e; }))
60
+ `);
61
+ }
62
+ async function getShim() {
63
+ // esModuleShims must point to the non-CSP build of ES Module Shims,
64
+ // namely the `es-module-shim.wasm.js` output: es-module-shims/dist/es-module-shims.wasm.js
65
+ const esModuleShimsPackage = 'es-module-shims';
66
+ const esModuleShimsFile = `./dist/es-module-shims.wasm.js`;
67
+ const esModuleShimsPath = await resolve(esModuleShimsPackage, parentDir);
68
+ if (!esModuleShimsPath) {
69
+ return;
70
+ }
71
+ const esModuleShimsWasm = path.join(esModuleShimsPath, esModuleShimsFile);
72
+ let esModuleShimString;
73
+ try {
74
+ esModuleShimString = await fsPromises.readFile(esModuleShimsWasm, 'utf8');
75
+ }
76
+ catch (e) {
77
+ return;
78
+ }
79
+ return esModuleShimString;
80
+ }
81
+ //# sourceMappingURL=handleWorker.js.map
@@ -0,0 +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,iCAAiC,CAAC;AAC1D,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,OAA4B;IAE5B,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,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC,MAAM,CAAA;;;;;;;;;;;;;;;;;;;;;MAqBnC,YAAY;8BACY,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC;kBAC7C,WAAW,CAAC,IAAI;KAC7B,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 { Request, Response } from '@ms-cloudpack/create-express-app';\nimport type { BundleServerContext } from './types/BundleServerContext.js';\nimport { makeUrl } from '@ms-cloudpack/path-string-parsing';\nimport { dedent } from 'ts-dedent';\nimport { resolve } from '@ms-cloudpack/package-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: BundleServerContext,\n) {\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 res.type('text/javascript').send(dedent`\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}').catch(e => setTimeout(() => { throw e; }))\n `);\n}\n\nasync function getShim() {\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"]}
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.47.6"
8
+ "packageVersion": "7.47.7"
9
9
  }
10
10
  ]
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/bundle-server",
3
- "version": "0.5.5",
3
+ "version": "0.5.7",
4
4
  "description": "An implementation of the Bundle server for Cloudpack.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,11 +14,13 @@
14
14
  }
15
15
  },
16
16
  "dependencies": {
17
- "@ms-cloudpack/api-server": "^0.51.1",
18
- "@ms-cloudpack/create-express-app": "^1.8.4",
19
- "@ms-cloudpack/import-map": "^0.6.8",
20
- "@ms-cloudpack/package-utilities": "^10.0.1",
21
- "@ms-cloudpack/path-string-parsing": "^1.2.4"
17
+ "@ms-cloudpack/api-server": "^0.51.3",
18
+ "@ms-cloudpack/create-express-app": "^1.8.5",
19
+ "@ms-cloudpack/import-map": "^0.6.9",
20
+ "@ms-cloudpack/package-utilities": "^10.0.2",
21
+ "@ms-cloudpack/path-string-parsing": "^1.2.4",
22
+ "es-module-shims": "^1.10.0",
23
+ "ts-dedent": "^2.2.0"
22
24
  },
23
25
  "devDependencies": {
24
26
  "@ms-cloudpack/eslint-plugin-internal": "^0.0.1",