@ms-cloudpack/bundle-server 0.9.0 → 0.9.2
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;AAK1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAW1E,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;AAK1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAW1E,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,CA6Nf"}
|
|
@@ -75,6 +75,7 @@ export async function handleBundleRequest(options, context) {
|
|
|
75
75
|
shouldForce: true,
|
|
76
76
|
shouldRerun: true,
|
|
77
77
|
disableCache,
|
|
78
|
+
shouldGetBundleInfo: config.logBundleInfo,
|
|
78
79
|
}, context);
|
|
79
80
|
// This will never be seen by the user, but it is needed to satisfy the response type.
|
|
80
81
|
res.status(202).type('application/javascript').send('');
|
|
@@ -106,6 +107,7 @@ export async function handleBundleRequest(options, context) {
|
|
|
106
107
|
shouldRerun: force,
|
|
107
108
|
shouldWatch: config.mode !== 'production', // Only watch in non-production modes.
|
|
108
109
|
disableCache,
|
|
110
|
+
shouldGetBundleInfo: config.logBundleInfo,
|
|
109
111
|
}, context);
|
|
110
112
|
if (result.errors?.length || !result.outputPath) {
|
|
111
113
|
// Bundling errors will be logged by ensurePackageBundled on bundle task completion.
|
|
@@ -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,qBAAqB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AAC/E,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAKtE,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,SAAS;AAElC,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,GAA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DAAyD;IACzD,GAAG,CAAC,QAAQ,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;QAC1F,MAAM,WAAW,GAAG,GAAiC,CAAC;QACtD,6FAA6F;QAC7F,8FAA8F;QAC9F,gFAAgF;QAChF,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;YACxD,OAAO;QACT,CAAC;QAED,kCAAkC;QAClC,OAAO,EAAE,CAAC,WAAW,CAAC,CAAC;QAEvB,MAAM,MAAM,GAAG,WAAW,CAAC,UAAU,IAAI,GAAG,CAAC;QAC7C,IAAI,OAAO,GAAG,uBAAuB,WAAW,CAAC,OAAO,EAAE,CAAC;QAE3D,IAAI,WAAW,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAClC,mGAAmG;YACnG,8DAA8D;YAC9D,OAAO;gBACL,wBAAwB,YAAY,0BAA0B;oBAC9D,wHAAwH,CAAC;QAC7H,CAAC;QAED,iBAAiB,CAAC;YAChB,GAAG;YACH,MAAM;YACN,WAAW;YACX,OAAO;YACP,cAAc,EAAE,GAAG,CAAC,KAAK,IAAI,iCAAiC,GAAG,CAAC,KAAK,EAAE;SAC1E,CAAC,CAAC;IACL,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 { 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 { rewriteImportsFromFile } from './rewriteImports.js';\nimport { getLinkedPackageRedirectUrl } from './getLinkedPackageRedirectUrl.js';\nimport { logResponse, sendErrorResponse } from './responseHelpers.js';\n\n/** File read errors have a `code` like `ENOENT`. HTTP errors from `sendFile` have a `statusCode`. */\ntype ErrorWithCodes = Error & { statusCode?: number; code?: string };\n\nconst maxAge = 31536000; // 1 year\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\n console.debug(`Bundle request: ${requestPath}`);\n\n // If the package is missing, invalid arguments.\n if (!requestPath || !packageName) {\n return sendErrorResponse({\n res,\n status: 400,\n requestPath,\n message: `Requests must be in the format \"/{packageName}@{version}/{type}/path/file.ext\"`,\n });\n }\n\n const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });\n if (!packageEntry) {\n // If the package is missing, invalid arguments.\n return sendErrorResponse({\n res,\n status: 404,\n requestPath,\n message: `Unrecognized package \"${packageName}@${version || '(unknown)'}\"`,\n });\n }\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 return sendRedirect({ res, requestPath, message: `Redirecting to linked package ${redirectUrl}`, redirectUrl });\n }\n }\n\n // If the version is missing, redirect to the primary version.\n if (!version) {\n return sendRedirect({\n res,\n requestPath,\n message: `Redirecting versionless request to primary version ${packageEntry.version}`,\n redirectUrl: `/${packageName}@${packageEntry.version}${filePath}`,\n });\n }\n\n const packageRequestPath = `${packageName}@${version}${filePath}`;\n\n if (missing && features.autoUpdateEntries) {\n // Exports map for this package is missing, so try adding an override for the requested file.\n const addedPackageOverride = await addPackageOverride({\n input: { packageName, version, importPath: filePath, initiatedBy: 'autoUpdateEntries' },\n ctx: context,\n });\n\n if (addedPackageOverride) {\n // The requested file exists. 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\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 return sendErrorResponse({ res, status: 404, requestPath, message });\n }\n\n // If we are accessing a non-bundled asset, return the file directly.\n if (!bundled) {\n return sendFile({\n res,\n fileFullPath: path.join(packageEntry.path, filePath),\n requestPath: packageRequestPath,\n parentPath: packageEntry.path,\n isBundled: false,\n shouldCache: !!packageEntry.isExternal,\n });\n }\n\n console.debug(`Bundle request: bundling \"${packageName}@${version}\"`);\n const { result } = await ensurePackageBundled(\n {\n name: packageName,\n version,\n shouldForce: force,\n shouldRerun: force,\n shouldWatch: config.mode !== 'production', // Only watch in non-production modes.\n disableCache,\n },\n context,\n );\n\n if (result.errors?.length || !result.outputPath) {\n // Bundling errors will be logged by ensurePackageBundled on bundle task completion.\n // The only case where they wouldn't be logged is if the user manually refreshes the page and\n // the same failed bundle task result is reused. There's not an easy way to detect that condition\n // (and the overlay and response details will show the error regardless), so only do a debug log\n // to avoid extra noise in the more common case.\n logResponse({\n status: 500,\n requestPath: packageRequestPath,\n message: 'Bundling failed (see error overlay or response data)',\n level: 'debug',\n });\n res.status(500).type('json').send(result);\n return;\n }\n\n // If the worker query param 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 result.outputFiles?.find((file) => file.isWorker && file.outputPath === normalizeRelativePath(filePath))\n ) {\n await handleWorker(\n {\n req,\n res,\n workerFilePath: path.join(result.outputPath, filePath),\n },\n context,\n );\n return;\n }\n\n const fileFullPath = path.join(result.outputPath, filePath);\n\n // If rewriteImports query param is set, rewrite bare imports in the file\n // This is used for files imported by workers to ensure nested imports also work\n if (req.query.rewriteimports !== undefined && session.importMap) {\n try {\n const rewrittenSource = await rewriteImportsFromFile(fileFullPath, context);\n\n logResponse({ status: 200, requestPath: packageRequestPath, message: 'Returning file with rewritten imports' });\n // Do not cache these responses as they are based on the import map at request time.\n res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');\n res.type('text/javascript').send(rewrittenSource);\n return;\n } catch (err) {\n const error = err as Error;\n console.error('Failed to rewrite imports for', packageRequestPath, ':', error);\n return sendErrorResponse({\n res,\n status: 500,\n requestPath: packageRequestPath,\n message: `Failed to rewrite imports: ${error.message}`,\n });\n }\n }\n\n sendFile({\n res,\n fileFullPath,\n requestPath: packageRequestPath,\n parentPath: result.outputPath,\n isBundled: true,\n shouldCache:\n result.isExternal ||\n (hash !== 'pending' &&\n // For internal packages, cache if the request hash matches the current source hash\n hash ===\n (await packageHashes.get({\n packagePath: packageEntry.path,\n isSourceHashingEnabled: true,\n targetEnvironment: 'browser',\n }))),\n onError: (err) => {\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\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 && result.bundleSource !== 'bundler',\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 // Actual FS errors will have a `code`, which is more informative than `statusCode`.\n // If `sendFile` manually returns an HTTP error, only `statusCode` is set.\n errorCode: String(err.code || err.statusCode || ''),\n });\n });\n },\n });\n}\n\n/** Debug log about the redirect and then send it */\nfunction sendRedirect(params: { res: Response; requestPath: string; message: string; redirectUrl: string }): void {\n const { res, requestPath, message, redirectUrl } = params;\n logResponse({ status: 302, requestPath, message });\n res.redirect(redirectUrl);\n}\n\n/**\n * Do some error checks, then send the file.\n */\nfunction sendFile(params: {\n res: Response;\n /** Output path for bundled files, package path for unbundled files */\n parentPath: string;\n /** Full path to the file to return */\n fileFullPath: string;\n requestPath: string;\n isBundled: boolean;\n shouldCache: boolean;\n /** Extra error handling such as telemetry */\n onError?: (err: ErrorWithCodes) => void;\n}): void {\n const { res, fileFullPath, requestPath, parentPath, isBundled, shouldCache, onError } = params;\n\n // If the file path is outside of the package, return a 403 error.\n // This is a security measure to prevent access to files outside of the package.\n // (On Windows, path.relative() returns an absolute path for different drives.)\n const relativePath = path.relative(parentPath, fileFullPath);\n if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {\n sendErrorResponse({\n res,\n status: 403,\n requestPath,\n message: `Resolved file path is outside of the package`,\n consoleMessage: `File resolved to \"${fileFullPath}\" which is outside of \"${parentPath}\"`,\n });\n return;\n }\n\n console.debug(`Bundle response: ${isBundled ? 'bundled' : 'unbundled'} file \"${requestPath}\"`);\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 // Express v5 default behavior is to ignore (404) anything with a leading dot in any path segment,\n // such as .cloudpack, so we have to explicitly allow it.\n res.sendFile(fileFullPath, { dotfiles: 'allow', maxAge: shouldCache ? maxAge : 0 }, (err) => {\n const errWithCode = err as ErrorWithCodes | undefined;\n // It appears this callback is called on success (even though the types don't indicate that),\n // so only continue if there's an error. Also ignore ECONNABORTED errors (don't log to console\n // or telemetry) since this probably means the user refreshed or closed the tab.\n if (!errWithCode || errWithCode.code === 'ECONNABORTED') {\n return;\n }\n\n // Send to telemetry if applicable\n onError?.(errWithCode);\n\n const status = errWithCode.statusCode || 500;\n let message = `Error serving file: ${errWithCode.message}`;\n\n if (errWithCode.code === 'ENOENT') {\n // If it's an actual file not found error (not something else where sendFile manually returns 404),\n // use a custom message since often it's due to a stale cache.\n message =\n `Request resolved to \"${fileFullPath}\" which does not exist. ` +\n 'This might be due to an outdated browser cache, so please try reloading the page (or report the issue if it persists).';\n }\n\n sendErrorResponse({\n res,\n status,\n requestPath,\n message,\n consoleDetails: err.stack && `Bundle request error details: ${err.stack}`,\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,qBAAqB,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AAC/E,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAKtE,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,SAAS;AAElC,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;I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SAAS,CAAC,qBAAqB,EAAE,GAAG,CAAC,CAAC;IAE1C,kGAAkG;IAClG,yDAAyD;IACzD,GAAG,CAAC,QAAQ,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;QAC1F,MAAM,WAAW,GAAG,GAAiC,CAAC;QACtD,6FAA6F;QAC7F,8FAA8F;QAC9F,gFAAgF;QAChF,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;YACxD,OAAO;QACT,CAAC;QAED,kCAAkC;QAClC,OAAO,EAAE,CAAC,WAAW,CAAC,CAAC;QAEvB,MAAM,MAAM,GAAG,WAAW,CAAC,UAAU,IAAI,GAAG,CAAC;QAC7C,IAAI,OAAO,GAAG,uBAAuB,WAAW,CAAC,OAAO,EAAE,CAAC;QAE3D,IAAI,WAAW,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAClC,mGAAmG;YACnG,8DAA8D;YAC9D,OAAO;gBACL,wBAAwB,YAAY,0BAA0B;oBAC9D,wHAAwH,CAAC;QAC7H,CAAC;QAED,iBAAiB,CAAC;YAChB,GAAG;YACH,MAAM;YACN,WAAW;YACX,OAAO;YACP,cAAc,EAAE,GAAG,CAAC,KAAK,IAAI,iCAAiC,GAAG,CAAC,KAAK,EAAE;SAC1E,CAAC,CAAC;IACL,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 { 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 { rewriteImportsFromFile } from './rewriteImports.js';\nimport { getLinkedPackageRedirectUrl } from './getLinkedPackageRedirectUrl.js';\nimport { logResponse, sendErrorResponse } from './responseHelpers.js';\n\n/** File read errors have a `code` like `ENOENT`. HTTP errors from `sendFile` have a `statusCode`. */\ntype ErrorWithCodes = Error & { statusCode?: number; code?: string };\n\nconst maxAge = 31536000; // 1 year\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\n console.debug(`Bundle request: ${requestPath}`);\n\n // If the package is missing, invalid arguments.\n if (!requestPath || !packageName) {\n return sendErrorResponse({\n res,\n status: 400,\n requestPath,\n message: `Requests must be in the format \"/{packageName}@{version}/{type}/path/file.ext\"`,\n });\n }\n\n const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });\n if (!packageEntry) {\n // If the package is missing, invalid arguments.\n return sendErrorResponse({\n res,\n status: 404,\n requestPath,\n message: `Unrecognized package \"${packageName}@${version || '(unknown)'}\"`,\n });\n }\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 return sendRedirect({ res, requestPath, message: `Redirecting to linked package ${redirectUrl}`, redirectUrl });\n }\n }\n\n // If the version is missing, redirect to the primary version.\n if (!version) {\n return sendRedirect({\n res,\n requestPath,\n message: `Redirecting versionless request to primary version ${packageEntry.version}`,\n redirectUrl: `/${packageName}@${packageEntry.version}${filePath}`,\n });\n }\n\n const packageRequestPath = `${packageName}@${version}${filePath}`;\n\n if (missing && features.autoUpdateEntries) {\n // Exports map for this package is missing, so try adding an override for the requested file.\n const addedPackageOverride = await addPackageOverride({\n input: { packageName, version, importPath: filePath, initiatedBy: 'autoUpdateEntries' },\n ctx: context,\n });\n\n if (addedPackageOverride) {\n // The requested file exists. 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 shouldGetBundleInfo: config.logBundleInfo,\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\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 return sendErrorResponse({ res, status: 404, requestPath, message });\n }\n\n // If we are accessing a non-bundled asset, return the file directly.\n if (!bundled) {\n return sendFile({\n res,\n fileFullPath: path.join(packageEntry.path, filePath),\n requestPath: packageRequestPath,\n parentPath: packageEntry.path,\n isBundled: false,\n shouldCache: !!packageEntry.isExternal,\n });\n }\n\n console.debug(`Bundle request: bundling \"${packageName}@${version}\"`);\n const { result } = await ensurePackageBundled(\n {\n name: packageName,\n version,\n shouldForce: force,\n shouldRerun: force,\n shouldWatch: config.mode !== 'production', // Only watch in non-production modes.\n disableCache,\n shouldGetBundleInfo: config.logBundleInfo,\n },\n context,\n );\n\n if (result.errors?.length || !result.outputPath) {\n // Bundling errors will be logged by ensurePackageBundled on bundle task completion.\n // The only case where they wouldn't be logged is if the user manually refreshes the page and\n // the same failed bundle task result is reused. There's not an easy way to detect that condition\n // (and the overlay and response details will show the error regardless), so only do a debug log\n // to avoid extra noise in the more common case.\n logResponse({\n status: 500,\n requestPath: packageRequestPath,\n message: 'Bundling failed (see error overlay or response data)',\n level: 'debug',\n });\n res.status(500).type('json').send(result);\n return;\n }\n\n // If the worker query param 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 result.outputFiles?.find((file) => file.isWorker && file.outputPath === normalizeRelativePath(filePath))\n ) {\n await handleWorker(\n {\n req,\n res,\n workerFilePath: path.join(result.outputPath, filePath),\n },\n context,\n );\n return;\n }\n\n const fileFullPath = path.join(result.outputPath, filePath);\n\n // If rewriteImports query param is set, rewrite bare imports in the file\n // This is used for files imported by workers to ensure nested imports also work\n if (req.query.rewriteimports !== undefined && session.importMap) {\n try {\n const rewrittenSource = await rewriteImportsFromFile(fileFullPath, context);\n\n logResponse({ status: 200, requestPath: packageRequestPath, message: 'Returning file with rewritten imports' });\n // Do not cache these responses as they are based on the import map at request time.\n res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');\n res.type('text/javascript').send(rewrittenSource);\n return;\n } catch (err) {\n const error = err as Error;\n console.error('Failed to rewrite imports for', packageRequestPath, ':', error);\n return sendErrorResponse({\n res,\n status: 500,\n requestPath: packageRequestPath,\n message: `Failed to rewrite imports: ${error.message}`,\n });\n }\n }\n\n sendFile({\n res,\n fileFullPath,\n requestPath: packageRequestPath,\n parentPath: result.outputPath,\n isBundled: true,\n shouldCache:\n result.isExternal ||\n (hash !== 'pending' &&\n // For internal packages, cache if the request hash matches the current source hash\n hash ===\n (await packageHashes.get({\n packagePath: packageEntry.path,\n isSourceHashingEnabled: true,\n targetEnvironment: 'browser',\n }))),\n onError: (err) => {\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\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 && result.bundleSource !== 'bundler',\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 // Actual FS errors will have a `code`, which is more informative than `statusCode`.\n // If `sendFile` manually returns an HTTP error, only `statusCode` is set.\n errorCode: String(err.code || err.statusCode || ''),\n });\n });\n },\n });\n}\n\n/** Debug log about the redirect and then send it */\nfunction sendRedirect(params: { res: Response; requestPath: string; message: string; redirectUrl: string }): void {\n const { res, requestPath, message, redirectUrl } = params;\n logResponse({ status: 302, requestPath, message });\n res.redirect(redirectUrl);\n}\n\n/**\n * Do some error checks, then send the file.\n */\nfunction sendFile(params: {\n res: Response;\n /** Output path for bundled files, package path for unbundled files */\n parentPath: string;\n /** Full path to the file to return */\n fileFullPath: string;\n requestPath: string;\n isBundled: boolean;\n shouldCache: boolean;\n /** Extra error handling such as telemetry */\n onError?: (err: ErrorWithCodes) => void;\n}): void {\n const { res, fileFullPath, requestPath, parentPath, isBundled, shouldCache, onError } = params;\n\n // If the file path is outside of the package, return a 403 error.\n // This is a security measure to prevent access to files outside of the package.\n // (On Windows, path.relative() returns an absolute path for different drives.)\n const relativePath = path.relative(parentPath, fileFullPath);\n if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {\n sendErrorResponse({\n res,\n status: 403,\n requestPath,\n message: `Resolved file path is outside of the package`,\n consoleMessage: `File resolved to \"${fileFullPath}\" which is outside of \"${parentPath}\"`,\n });\n return;\n }\n\n console.debug(`Bundle response: ${isBundled ? 'bundled' : 'unbundled'} file \"${requestPath}\"`);\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 // Express v5 default behavior is to ignore (404) anything with a leading dot in any path segment,\n // such as .cloudpack, so we have to explicitly allow it.\n res.sendFile(fileFullPath, { dotfiles: 'allow', maxAge: shouldCache ? maxAge : 0 }, (err) => {\n const errWithCode = err as ErrorWithCodes | undefined;\n // It appears this callback is called on success (even though the types don't indicate that),\n // so only continue if there's an error. Also ignore ECONNABORTED errors (don't log to console\n // or telemetry) since this probably means the user refreshed or closed the tab.\n if (!errWithCode || errWithCode.code === 'ECONNABORTED') {\n return;\n }\n\n // Send to telemetry if applicable\n onError?.(errWithCode);\n\n const status = errWithCode.statusCode || 500;\n let message = `Error serving file: ${errWithCode.message}`;\n\n if (errWithCode.code === 'ENOENT') {\n // If it's an actual file not found error (not something else where sendFile manually returns 404),\n // use a custom message since often it's due to a stale cache.\n message =\n `Request resolved to \"${fileFullPath}\" which does not exist. ` +\n 'This might be due to an outdated browser cache, so please try reloading the page (or report the issue if it persists).';\n }\n\n sendErrorResponse({\n res,\n status,\n requestPath,\n message,\n consoleDetails: err.stack && `Bundle request error details: ${err.stack}`,\n });\n });\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/bundle-server",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
4
4
|
"description": "Internal bundle server for Cloudpack",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"lint": "cloudpack-scripts lint"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@ms-cloudpack/api-server": "^0.66.
|
|
24
|
+
"@ms-cloudpack/api-server": "^0.66.21",
|
|
25
25
|
"@ms-cloudpack/common-types": "^0.33.3",
|
|
26
26
|
"@ms-cloudpack/create-express-app": "^1.10.72",
|
|
27
27
|
"@ms-cloudpack/import-map": "^0.12.5",
|
|
28
|
-
"@ms-cloudpack/inline-scripts": "^0.2.
|
|
28
|
+
"@ms-cloudpack/inline-scripts": "^0.2.82",
|
|
29
29
|
"@ms-cloudpack/package-utilities": "^13.7.1",
|
|
30
30
|
"@ms-cloudpack/path-string-parsing": "^1.3.0",
|
|
31
31
|
"@ms-cloudpack/path-utilities": "^3.2.8",
|