@ms-cloudpack/bundle-server 0.6.93 → 0.6.95
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;AAI1E,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,
|
|
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;AAI1E,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,CAgMf"}
|
|
@@ -80,17 +80,19 @@ export async function handleBundleRequest(options, context) {
|
|
|
80
80
|
// If we are accessing a non-bundled asset, return the file directly.
|
|
81
81
|
if (!bundled) {
|
|
82
82
|
const fullPath = path.join(packageEntry.path, filePath);
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
// If the file path is outside of the package, return a 400 error.
|
|
84
|
+
// This is a security measure to prevent access to files outside of the package.
|
|
85
|
+
if (path.relative(packageEntry.path, fullPath).startsWith('..')) {
|
|
86
|
+
console.debug(`Response (400): "${packageRequestPath}"`);
|
|
85
87
|
res.status(400).send(`Invalid file path.`);
|
|
86
88
|
}
|
|
87
89
|
else {
|
|
88
|
-
console.debug(`Unbundled response returning: "${
|
|
90
|
+
console.debug(`Unbundled response returning: "${packageRequestPath}"`);
|
|
89
91
|
// If the package is external, set cache headers.
|
|
90
92
|
if (isExternalPackage(packageEntry.path)) {
|
|
91
93
|
res.setHeader('Cache-Control', 'public, max-age=31536000');
|
|
92
94
|
}
|
|
93
|
-
res.sendFile(
|
|
95
|
+
res.sendFile(fullPath);
|
|
94
96
|
}
|
|
95
97
|
return;
|
|
96
98
|
}
|
|
@@ -116,7 +118,7 @@ export async function handleBundleRequest(options, context) {
|
|
|
116
118
|
await handleWorker({ req, res }, context);
|
|
117
119
|
return;
|
|
118
120
|
}
|
|
119
|
-
console.debug(`Bundle returning: "${
|
|
121
|
+
console.debug(`Bundle returning: "${packageRequestPath}"`);
|
|
120
122
|
// Try to return the result.
|
|
121
123
|
if (result.isExternal ||
|
|
122
124
|
(hash !== 'pending' &&
|
|
@@ -130,7 +132,13 @@ export async function handleBundleRequest(options, context) {
|
|
|
130
132
|
}
|
|
131
133
|
// The below header is need to be able to track the browser cache hit ratio.
|
|
132
134
|
res.setHeader('Timing-Allow-Origin', '*');
|
|
133
|
-
|
|
135
|
+
const fullPath = path.join(result.outputPath, filePath);
|
|
136
|
+
if (path.relative(result.outputPath, fullPath).startsWith('..')) {
|
|
137
|
+
console.debug(`Response (400): "${packageRequestPath}"`);
|
|
138
|
+
res.status(400).send(`Invalid file path.`);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
res.sendFile(fullPath, (err) => {
|
|
134
142
|
if (err) {
|
|
135
143
|
console.error(`Error serving file ${packageRequestPath}. Please refresh your browser to try again.`);
|
|
136
144
|
// This is a workaround for the browser cache issue.
|
|
@@ -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;AACjD,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AAE/E,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,eAAe,EAAE,GAAG,OAAO,CAAC;IACjE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IACpD,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;IAC9C,MAAM,EAAE,WAAW,EAAE,OAAO,EA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AC/D,KAAK,eAAe,CAAC,MAAM,CAAC,eAAe,CAAC,kBAAkB,EAAE,CAAC,IAAI,EAAE,EAAE;gBACvE,IAAI,CAAC,aAAa,CAAC;oBACjB,UAAU,EAAE,CAAC,CAAC,MAAM,CAAC,YAAY;oBACjC,UAAU,EAAE,MAAM,CAAC,UAAU;oBAC7B,WAAW;oBACX,cAAc,EAAE,OAAO;oBACvB,YAAY,EAAE,MAAM,EAAE,YAAY;oBAClC,OAAO,EAAE,MAAM,EAAE,OAAO;oBACxB,cAAc,EAAE,MAAM,EAAE,cAAc;oBACtC,QAAQ;oBACR,OAAO,EAAE,WAAW;oBACpB,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,SAAS,EAAG,GAAyB,EAAE,IAAI,IAAI,EAAE;iBAClD,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC,CAAC;AACL,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';\nimport { getLinkedPackageRedirectUrl } from './getLinkedPackageRedirectUrl.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, telemetryClient } = context;\n const { resolveMap, config, linkedPaths } = 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 // This is a hot path, so doing a quick check first.\n if (linkedPaths.length) {\n // Try to redirect to remote host if necessary.\n const redirectUrl = getLinkedPackageRedirectUrl({\n linkedPaths,\n packageName,\n version,\n packageEntry,\n originalUrl: req.originalUrl,\n });\n\n if (redirectUrl) {\n console.debug(`Redirecting to linked package: ${redirectUrl}`);\n res.redirect(redirectUrl);\n return;\n }\n }\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), (err) => {\n if (err) {\n console.error(`Error serving file ${packageRequestPath}. Please refresh your browser to try again.`);\n // This is a workaround for the browser cache issue.\n // It seems to happen when a file is cached in the browser but the imported files are not cached and not on disk.\n // Increase the target version to clear browser cache for that specific package.\n session.incrementTargetVersion(packageEntry.path);\n // Only send a response if headers haven't been sent yet\n if (!res.headersSent) {\n res.status(404).send(`File not found: ${packageRequestPath}`);\n }\n // If the file is not found, we need to report it to telemetry.\n void telemetryClient.tracer.startActiveSpan('FILE_SERVE_ERROR', (span) => {\n span.setAttributes({\n isCacheHit: !!result.bundleSource,\n isExternal: result.isExternal,\n packageName,\n packageVersion: version,\n bundleSource: result?.bundleSource,\n bundler: result?.bundler,\n bundlerVersion: result?.bundlerVersion,\n filePath,\n request: requestPath,\n hash: result.hash,\n errorCode: (err as { code?: string })?.code || '',\n });\n });\n }\n });\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;AACjD,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AAE/E,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,eAAe,EAAE,GAAG,OAAO,CAAC;IACjE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IACpD,MAAM,EAAE,QAAQ,GAAG,EAAE,EAAE,GAAG,MAAM,CAAC;IACjC,MAAM,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,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QAC3C,OAAO;IACT,CAAC;IAED,GAAG,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,EAAE;QAC7B,IAAI,GAAG,EAAE,CAAC;YACR,OAAO,CAAC,KAAK,CAAC,sBAAsB,kBAAkB,6CAA6C,CAAC,CAAC;YACrG,oDAAoD;YACpD,iHAAiH;YACjH,gFAAgF;YAChF,OAAO,CAAC,sBAAsB,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;YAClD,wDAAwD;YACxD,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;gBACrB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,mBAAmB,kBAAkB,EAAE,CAAC,CAAC;YAChE,CAAC;YACD,+DAA+D;YAC/D,KAAK,eAAe,CAAC,MAAM,CAAC,eAAe,CAAC,kBAAkB,EAAE,CAAC,IAAI,EAAE,EAAE;gBACvE,IAAI,CAAC,aAAa,CAAC;oBACjB,UAAU,EAAE,CAAC,CAAC,MAAM,CAAC,YAAY;oBACjC,UAAU,EAAE,MAAM,CAAC,UAAU;oBAC7B,WAAW;oBACX,cAAc,EAAE,OAAO;oBACvB,YAAY,EAAE,MAAM,EAAE,YAAY;oBAClC,OAAO,EAAE,MAAM,EAAE,OAAO;oBACxB,cAAc,EAAE,MAAM,EAAE,cAAc;oBACtC,QAAQ;oBACR,OAAO,EAAE,WAAW;oBACpB,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,SAAS,EAAG,GAAyB,EAAE,IAAI,IAAI,EAAE;iBAClD,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC,CAAC;AACL,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';\nimport { getLinkedPackageRedirectUrl } from './getLinkedPackageRedirectUrl.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, telemetryClient } = context;\n const { resolveMap, config, linkedPaths } = 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 // This is a hot path, so doing a quick check first.\n if (linkedPaths.length) {\n // Try to redirect to remote host if necessary.\n const redirectUrl = getLinkedPackageRedirectUrl({\n linkedPaths,\n packageName,\n version,\n packageEntry,\n originalUrl: req.originalUrl,\n });\n\n if (redirectUrl) {\n console.debug(`Redirecting to linked package: ${redirectUrl}`);\n res.redirect(redirectUrl);\n return;\n }\n }\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 the file path is outside of the package, return a 400 error.\n // This is a security measure to prevent access to files outside of the package.\n if (path.relative(packageEntry.path, fullPath).startsWith('..')) {\n console.debug(`Response (400): \"${packageRequestPath}\"`);\n\n res.status(400).send(`Invalid file path.`);\n } else {\n console.debug(`Unbundled response returning: \"${packageRequestPath}\"`);\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(fullPath);\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: \"${packageRequestPath}\"`);\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 const fullPath = path.join(result.outputPath, filePath);\n if (path.relative(result.outputPath, fullPath).startsWith('..')) {\n console.debug(`Response (400): \"${packageRequestPath}\"`);\n\n res.status(400).send(`Invalid file path.`);\n return;\n }\n\n res.sendFile(fullPath, (err) => {\n if (err) {\n console.error(`Error serving file ${packageRequestPath}. Please refresh your browser to try again.`);\n // This is a workaround for the browser cache issue.\n // It seems to happen when a file is cached in the browser but the imported files are not cached and not on disk.\n // Increase the target version to clear browser cache for that specific package.\n session.incrementTargetVersion(packageEntry.path);\n // Only send a response if headers haven't been sent yet\n if (!res.headersSent) {\n res.status(404).send(`File not found: ${packageRequestPath}`);\n }\n // If the file is not found, we need to report it to telemetry.\n void telemetryClient.tracer.startActiveSpan('FILE_SERVE_ERROR', (span) => {\n span.setAttributes({\n isCacheHit: !!result.bundleSource,\n isExternal: result.isExternal,\n packageName,\n packageVersion: version,\n bundleSource: result?.bundleSource,\n bundler: result?.bundler,\n bundlerVersion: result?.bundlerVersion,\n filePath,\n request: requestPath,\n hash: result.hash,\n errorCode: (err as { code?: string })?.code || '',\n });\n });\n }\n });\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/bundle-server",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.95",
|
|
4
4
|
"description": "An implementation of the Bundle server for Cloudpack.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,14 +14,14 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/api-server": "^0.61.
|
|
17
|
+
"@ms-cloudpack/api-server": "^0.61.33",
|
|
18
18
|
"@ms-cloudpack/common-types": "^0.24.16",
|
|
19
|
-
"@ms-cloudpack/create-express-app": "^1.10.
|
|
20
|
-
"@ms-cloudpack/import-map": "^0.10.
|
|
21
|
-
"@ms-cloudpack/inline-scripts": "^0.1.
|
|
22
|
-
"@ms-cloudpack/package-utilities": "^12.3.
|
|
19
|
+
"@ms-cloudpack/create-express-app": "^1.10.29",
|
|
20
|
+
"@ms-cloudpack/import-map": "^0.10.18",
|
|
21
|
+
"@ms-cloudpack/inline-scripts": "^0.1.18",
|
|
22
|
+
"@ms-cloudpack/package-utilities": "^12.3.3",
|
|
23
23
|
"@ms-cloudpack/path-string-parsing": "^1.2.7",
|
|
24
|
-
"@ms-cloudpack/path-utilities": "^3.0
|
|
24
|
+
"@ms-cloudpack/path-utilities": "^3.1.0",
|
|
25
25
|
"es-module-shims": "^1.10.0",
|
|
26
26
|
"ts-dedent": "^2.2.0"
|
|
27
27
|
},
|