|
| 1 | +import { consoleSandbox, flatten } from '@sentry/utils'; |
| 2 | +import type { InputPluginOption } from 'rollup'; |
| 3 | + |
| 4 | +export const SENTRY_WRAPPED_ENTRY = '?sentry-query-wrapped-entry'; |
| 5 | +export const SENTRY_WRAPPED_FUNCTIONS = '?sentry-query-wrapped-functions='; |
| 6 | +export const SENTRY_REEXPORTED_FUNCTIONS = '?sentry-query-reexported-functions='; |
| 7 | +export const QUERY_END_INDICATOR = 'SENTRY-QUERY-END'; |
| 8 | + |
| 9 | +/** |
| 10 | + * A Rollup plugin which wraps the server entry with a dynamic `import()`. This makes it possible to initialize Sentry first |
| 11 | + * by using a regular `import` and load the server after that. |
| 12 | + * This also works with serverless `handler` functions, as it re-exports the `handler`. |
| 13 | + * |
| 14 | + * @param config Configuration options for the Rollup Plugin |
| 15 | + * @param config.serverConfigFileName Name of the Sentry server config (without file extension). E.g. 'sentry.server.config' |
| 16 | + * @param config.resolvedServerConfigPath Resolved path of the Sentry server config (based on `src` directory) |
| 17 | + * @param config.entryPointWrappedFunctions Exported bindings of the server entry file, which are wrapped as async function. E.g. ['default', 'handler', 'server'] |
| 18 | + * @param config.additionalImports Adds additional imports to the entry file. Can be e.g. 'import-in-the-middle/hook.mjs' |
| 19 | + * @param config.debug Whether debug logs are enabled in the build time environment |
| 20 | + */ |
| 21 | +export function wrapServerEntryWithDynamicImport(config: { |
| 22 | + serverConfigFileName: string; |
| 23 | + resolvedServerConfigPath: string; |
| 24 | + entrypointWrappedFunctions: string[]; |
| 25 | + additionalImports?: string[]; |
| 26 | + debug?: boolean; |
| 27 | +}): InputPluginOption { |
| 28 | + const { serverConfigFileName, resolvedServerConfigPath, entrypointWrappedFunctions, additionalImports, debug } = |
| 29 | + config; |
| 30 | + |
| 31 | + return { |
| 32 | + name: 'sentry-wrap-server-entry-with-dynamic-import', |
| 33 | + async resolveId(source, importer, options) { |
| 34 | + if (source.includes(`/${serverConfigFileName}`)) { |
| 35 | + return { id: source, moduleSideEffects: true }; |
| 36 | + } |
| 37 | + |
| 38 | + if (additionalImports && additionalImports.includes(source)) { |
| 39 | + // When importing additional imports like "import-in-the-middle/hook.mjs" in the returned code of the `load()` function below: |
| 40 | + // By setting `moduleSideEffects` to `true`, the import is added to the bundle, although nothing is imported from it |
| 41 | + // By importing "import-in-the-middle/hook.mjs", we can make sure this file is included, as not all node builders are including files imported with `module.register()`. |
| 42 | + // Prevents the error "Failed to register ESM hook Error: Cannot find module 'import-in-the-middle/hook.mjs'" |
| 43 | + return { id: source, moduleSideEffects: true, external: true }; |
| 44 | + } |
| 45 | + |
| 46 | + if (options.isEntry && source.includes('.mjs') && !source.includes(`.mjs${SENTRY_WRAPPED_ENTRY}`)) { |
| 47 | + const resolution = await this.resolve(source, importer, options); |
| 48 | + |
| 49 | + // If it cannot be resolved or is external, just return it so that Rollup can display an error |
| 50 | + if (!resolution || (resolution && resolution.external)) return resolution; |
| 51 | + |
| 52 | + const moduleInfo = await this.load(resolution); |
| 53 | + |
| 54 | + moduleInfo.moduleSideEffects = true; |
| 55 | + |
| 56 | + // The enclosing `if` already checks for the suffix in `source`, but a check in `resolution.id` is needed as well to prevent multiple attachment of the suffix |
| 57 | + return resolution.id.includes(`.mjs${SENTRY_WRAPPED_ENTRY}`) |
| 58 | + ? resolution.id |
| 59 | + : resolution.id |
| 60 | + // Concatenates the query params to mark the file (also attaches names of re-exports - this is needed for serverless functions to re-export the handler) |
| 61 | + .concat(SENTRY_WRAPPED_ENTRY) |
| 62 | + .concat( |
| 63 | + constructWrappedFunctionExportQuery(moduleInfo.exportedBindings, entrypointWrappedFunctions, debug), |
| 64 | + ) |
| 65 | + .concat(QUERY_END_INDICATOR); |
| 66 | + } |
| 67 | + return null; |
| 68 | + }, |
| 69 | + load(id: string) { |
| 70 | + if (id.includes(`.mjs${SENTRY_WRAPPED_ENTRY}`)) { |
| 71 | + const entryId = removeSentryQueryFromPath(id); |
| 72 | + |
| 73 | + // Mostly useful for serverless `handler` functions |
| 74 | + const reExportedFunctions = |
| 75 | + id.includes(SENTRY_WRAPPED_FUNCTIONS) || id.includes(SENTRY_REEXPORTED_FUNCTIONS) |
| 76 | + ? constructFunctionReExport(id, entryId) |
| 77 | + : ''; |
| 78 | + |
| 79 | + return ( |
| 80 | + // Regular `import` of the Sentry config |
| 81 | + `import ${JSON.stringify(resolvedServerConfigPath)};\n` + |
| 82 | + // Dynamic `import()` for the previous, actual entry point. |
| 83 | + // `import()` can be used for any code that should be run after the hooks are registered (https://nodejs.org/api/module.html#enabling) |
| 84 | + `import(${JSON.stringify(entryId)});\n` + |
| 85 | + // By importing additional imports like "import-in-the-middle/hook.mjs", we can make sure this file wil be included, as not all node builders are including files imported with `module.register()`. |
| 86 | + `${additionalImports ? additionalImports.map(importPath => `import "${importPath}";\n`) : ''}` + |
| 87 | + `${reExportedFunctions}\n` |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return null; |
| 92 | + }, |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Strips the Sentry query part from a path. |
| 98 | + * Example: example/path?sentry-query-wrapped-entry?sentry-query-functions-reexport=foo,SENTRY-QUERY-END -> /example/path |
| 99 | + * |
| 100 | + * **Only exported for testing** |
| 101 | + */ |
| 102 | +export function removeSentryQueryFromPath(url: string): string { |
| 103 | + // eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor |
| 104 | + const regex = new RegExp(`\\${SENTRY_WRAPPED_ENTRY}.*?\\${QUERY_END_INDICATOR}`); |
| 105 | + return url.replace(regex, ''); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Extracts and sanitizes function re-export and function wrap query parameters from a query string. |
| 110 | + * If it is a default export, it is not considered for re-exporting. |
| 111 | + * |
| 112 | + * **Only exported for testing** |
| 113 | + */ |
| 114 | +export function extractFunctionReexportQueryParameters(query: string): { wrap: string[]; reexport: string[] } { |
| 115 | + // Regex matches the comma-separated params between the functions query |
| 116 | + // eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor |
| 117 | + const wrapRegex = new RegExp( |
| 118 | + `\\${SENTRY_WRAPPED_FUNCTIONS}(.*?)(\\${QUERY_END_INDICATOR}|\\${SENTRY_REEXPORTED_FUNCTIONS})`, |
| 119 | + ); |
| 120 | + // eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor |
| 121 | + const reexportRegex = new RegExp(`\\${SENTRY_REEXPORTED_FUNCTIONS}(.*?)(\\${QUERY_END_INDICATOR})`); |
| 122 | + |
| 123 | + const wrapMatch = query.match(wrapRegex); |
| 124 | + const reexportMatch = query.match(reexportRegex); |
| 125 | + |
| 126 | + const wrap = |
| 127 | + wrapMatch && wrapMatch[1] |
| 128 | + ? wrapMatch[1] |
| 129 | + .split(',') |
| 130 | + .filter(param => param !== '') |
| 131 | + // Sanitize, as code could be injected with another rollup plugin |
| 132 | + .map((str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) |
| 133 | + : []; |
| 134 | + |
| 135 | + const reexport = |
| 136 | + reexportMatch && reexportMatch[1] |
| 137 | + ? reexportMatch[1] |
| 138 | + .split(',') |
| 139 | + .filter(param => param !== '' && param !== 'default') |
| 140 | + // Sanitize, as code could be injected with another rollup plugin |
| 141 | + .map((str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) |
| 142 | + : []; |
| 143 | + |
| 144 | + return { wrap, reexport }; |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Constructs a comma-separated string with all functions that need to be re-exported later from the server entry. |
| 149 | + * It uses Rollup's `exportedBindings` to determine the functions to re-export. Functions which should be wrapped |
| 150 | + * (e.g. serverless handlers) are wrapped by Sentry. |
| 151 | + * |
| 152 | + * **Only exported for testing** |
| 153 | + */ |
| 154 | +export function constructWrappedFunctionExportQuery( |
| 155 | + exportedBindings: Record<string, string[]> | null, |
| 156 | + entrypointWrappedFunctions: string[], |
| 157 | + debug?: boolean, |
| 158 | +): string { |
| 159 | + // `exportedBindings` can look like this: `{ '.': [ 'handler' ] }` or `{ '.': [], './firebase-gen-1.mjs': [ 'server' ] }` |
| 160 | + // The key `.` refers to exports within the current file, while other keys show from where exports were imported first. |
| 161 | + const functionsToExport = flatten(Object.values(exportedBindings || {})).reduce( |
| 162 | + (functions, currFunctionName) => { |
| 163 | + if (entrypointWrappedFunctions.includes(currFunctionName)) { |
| 164 | + functions.wrap.push(currFunctionName); |
| 165 | + } else { |
| 166 | + functions.reexport.push(currFunctionName); |
| 167 | + } |
| 168 | + return functions; |
| 169 | + }, |
| 170 | + { wrap: [], reexport: [] } as { wrap: string[]; reexport: string[] }, |
| 171 | + ); |
| 172 | + |
| 173 | + if (debug && functionsToExport.wrap.length === 0) { |
| 174 | + consoleSandbox(() => |
| 175 | + // eslint-disable-next-line no-console |
| 176 | + console.warn( |
| 177 | + "[Sentry] No functions found to wrap. In case the server needs to export async functions other than `handler` or `server`, consider adding the name(s) to Sentry's build options `sentry.entrypointWrappedFunctions` in `nuxt.config.ts`.", |
| 178 | + ), |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + const wrapQuery = functionsToExport.wrap.length |
| 183 | + ? `${SENTRY_WRAPPED_FUNCTIONS}${functionsToExport.wrap.join(',')}` |
| 184 | + : ''; |
| 185 | + const reexportQuery = functionsToExport.reexport.length |
| 186 | + ? `${SENTRY_REEXPORTED_FUNCTIONS}${functionsToExport.reexport.join(',')}` |
| 187 | + : ''; |
| 188 | + |
| 189 | + return [wrapQuery, reexportQuery].join(''); |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * Constructs a code snippet with function reexports (can be used in Rollup plugins as a return value for `load()`) |
| 194 | + * |
| 195 | + * **Only exported for testing** |
| 196 | + */ |
| 197 | +export function constructFunctionReExport(pathWithQuery: string, entryId: string): string { |
| 198 | + const { wrap: wrapFunctions, reexport: reexportFunctions } = extractFunctionReexportQueryParameters(pathWithQuery); |
| 199 | + |
| 200 | + return wrapFunctions |
| 201 | + .reduce( |
| 202 | + (functionsCode, currFunctionName) => |
| 203 | + functionsCode.concat( |
| 204 | + `async function ${currFunctionName}_sentryWrapped(...args) {\n` + |
| 205 | + ` const res = await import(${JSON.stringify(entryId)});\n` + |
| 206 | + ` return res.${currFunctionName}.call(this, ...args);\n` + |
| 207 | + '}\n' + |
| 208 | + `export { ${currFunctionName}_sentryWrapped as ${currFunctionName} };\n`, |
| 209 | + ), |
| 210 | + '', |
| 211 | + ) |
| 212 | + .concat( |
| 213 | + reexportFunctions.reduce( |
| 214 | + (functionsCode, currFunctionName) => |
| 215 | + functionsCode.concat(`export { ${currFunctionName} } from ${JSON.stringify(entryId)};`), |
| 216 | + '', |
| 217 | + ), |
| 218 | + ); |
| 219 | +} |
0 commit comments