( req: IncomingMessage, res: ServerResponse, content: string | Buffer, type: string, options: SendOptions, )
| 30 | } |
| 31 | |
| 32 | export function send( |
| 33 | req: IncomingMessage, |
| 34 | res: ServerResponse, |
| 35 | content: string | Buffer, |
| 36 | type: string, |
| 37 | options: SendOptions, |
| 38 | ): void { |
| 39 | const { |
| 40 | etag = getEtag(content, { weak: true }), |
| 41 | cacheControl = 'no-cache', |
| 42 | headers, |
| 43 | map, |
| 44 | } = options |
| 45 | |
| 46 | if (res.writableEnded) { |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | if (req.headers['if-none-match'] === etag) { |
| 51 | res.statusCode = 304 |
| 52 | res.end() |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | res.setHeader('Content-Type', alias[type] || type) |
| 57 | res.setHeader('Cache-Control', cacheControl) |
| 58 | res.setHeader('Etag', etag) |
| 59 | |
| 60 | if (headers) { |
| 61 | for (const name in headers) { |
| 62 | res.setHeader(name, headers[name]!) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // inject source map reference |
| 67 | if (map && 'version' in map && map.mappings) { |
| 68 | if (type === 'js' || type === 'css') { |
| 69 | content = getCodeWithSourcemap(type, content.toString(), map) |
| 70 | } |
| 71 | } |
| 72 | // inject fallback sourcemap for js for improved debugging |
| 73 | // https://github.com/vitejs/vite/pull/13514#issuecomment-1592431496 |
| 74 | else if (type === 'js' && (!map || map.mappings !== '')) { |
| 75 | const code = content.toString() |
| 76 | // if the code has existing inline sourcemap, assume it's correct and skip |
| 77 | if (convertSourceMap.mapFileCommentRegex.test(code)) { |
| 78 | debug?.(`Skipped injecting fallback sourcemap for ${req.url}`) |
| 79 | } else { |
| 80 | const urlWithoutTimestamp = removeTimestampQuery(req.url!) |
| 81 | const ms = new MagicString(code) |
| 82 | content = getCodeWithSourcemap( |
| 83 | type, |
| 84 | code, |
| 85 | ms.generateMap({ |
| 86 | source: path.basename(urlWithoutTimestamp), |
| 87 | hires: 'boundary', |
| 88 | includeContent: true, |
| 89 | }) as SourceMap, |
no test coverage detected