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AG;4BAGrB,KAAK,OAAA;4BACL,IAAI,MAAA;yBACL,CAAC;wBACF,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;oBAC1B,CAAC,CAAC,CAAC;iBACJ;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;IACJ,CAAC;IA9ED,6BA8EC","sourcesContent":["import { stat, readFile } from 'fs';\nimport createError from 'http-errors';\nimport { lookup } from 'mime-types';\nimport { join, resolve } from 'path';\nimport { RequestHandler } from 'express';\n\nimport { Context } from '../Server';\n\nexport default function instrument(context: Context): RequestHandler {\n const codeCache: {\n [filename: string]: { mtime: number; data: string };\n } = Object.create(null);\n\n return (request, response, next) => {\n const { basePath, executor } = context;\n const wholePath = resolve(join(basePath, request.url));\n\n if (\n !(request.method === 'HEAD' || request.method === 'GET') ||\n !executor.shouldInstrumentFile(wholePath)\n ) {\n return next();\n }\n\n stat(wholePath, (error, stats) => {\n // The server was stopped before this file was served\n if (context.stopped) {\n return;\n }\n\n if (error || !stats.isFile()) {\n executor.log('Unable to serve', wholePath, '(unreadable)');\n return next(createError(404, error as Error, { expose: false }));\n }\n\n executor.log('Serving', wholePath);\n\n const send = (contentType: string, data: string) => {\n response.writeHead(200, {\n 'Content-Type': contentType,\n 'Content-Length': Buffer.byteLength(data),\n });\n response.end(request.method === 'HEAD' ? '' : data, callback);\n };\n const callback = (error?: Error) => {\n if (error) {\n executor.emit(\n 'error',\n new Error(`Error serving ${wholePath}: ${error.message}`)\n );\n } else {\n executor.log('Served', wholePath);\n }\n };\n\n const contentType = lookup(wholePath) || 'application/octet-stream';\n const mtime = stats.mtime.getTime();\n\n if (codeCache[wholePath] && codeCache[wholePath].mtime === mtime) {\n send(contentType, codeCache[wholePath].data);\n } else {\n readFile(wholePath, 'utf8', (error, data) => {\n // The server was stopped in the middle of the file read\n if (context.stopped) {\n return;\n }\n\n if (error) {\n return next(createError(404, error, { expose: false }));\n }\n\n // providing `wholePath` to the instrumenter instead of a\n // partial filename is necessary because lcov.info requires\n // full path names as per the lcov spec\n data = executor.instrumentCode(data, wholePath);\n codeCache[wholePath] = {\n // strictly speaking mtime could reflect a previous\n // version, assume those race conditions are rare\n mtime,\n data,\n };\n send(contentType, data);\n });\n }\n });\n };\n}\n"]}
\No newline at end of file