UNPKG

@sentry/nextjs

Version:
68 lines (66 loc) • 2.48 kB
const NEXT_MANIFESTS_SINGLETON = /* @__PURE__ */ Symbol.for("next.server.manifests"); function readMultipartField(cacheKey, start) { const colon = cacheKey.indexOf(":", start); if (colon <= start || !/^[0-9a-f]+$/.test(cacheKey.slice(start, colon))) { return void 0; } const contentStart = colon + 1; const contentEnd = contentStart + parseInt(cacheKey.slice(start, colon), 16); if (contentEnd > cacheKey.length) { return void 0; } return { content: cacheKey.slice(contentStart, contentEnd), end: contentEnd }; } function readKeyPartsFromMultipartKey(cacheKey) { let position = 0; while (position < cacheKey.length) { const name = readMultipartField(cacheKey, position); const content = name && readMultipartField(cacheKey, name.end); if (name === void 0 || content === void 0) { return void 0; } if (name.content === "0") { return content.content; } position = content.end; } return void 0; } function readFunctionIdFromKeyParts(keyParts) { if (!Array.isArray(keyParts)) { return void 0; } const functionId = Array.isArray(keyParts[1]) ? keyParts[0] : keyParts[1]; return typeof functionId === "string" ? functionId : void 0; } function toProjectRelativePath(filename) { const cwdSegments = process.cwd().split(/[\\/]/).filter(Boolean); const fileSegments = filename.split("/"); const maxOverlap = Math.min(fileSegments.length - 1, cwdSegments.length); for (let overlap = maxOverlap; overlap > 0; overlap--) { const cwdTail = cwdSegments.slice(-overlap); if (cwdTail.every((segment, index) => segment === fileSegments[index])) { return fileSegments.slice(overlap).join("/"); } } return filename; } function getCacheFunctionSourceFile(cacheKey) { try { const keyPartsJson = cacheKey.startsWith("[") ? cacheKey : readKeyPartsFromMultipartKey(cacheKey); if (!keyPartsJson) { return void 0; } const functionId = readFunctionIdFromKeyParts(JSON.parse(keyPartsJson)); if (functionId === void 0) { return void 0; } const manifest = globalThis[NEXT_MANIFESTS_SINGLETON]?.serverActionsManifest; const filename = manifest?.[process.env.NEXT_RUNTIME === "edge" ? "edge" : "node"]?.[functionId]?.filename; return typeof filename === "string" && filename ? toProjectRelativePath(filename) : void 0; } catch { return void 0; } } export { getCacheFunctionSourceFile }; //# sourceMappingURL=useCacheSourceFile.js.map