From d0864c23112140005a4b3783c2b160c4db98f94a Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 7 Dec 2020 09:14:54 -0800 Subject: [PATCH] Upgrade to latest version of source-map library (#233) --- packages/kit/package.json | 1 + .../kit/src/api/dev/sourcemap_stacktrace.js | 33 ++++++++++++------- packages/kit/src/renderer/page.js | 3 +- pnpm-lock.yaml | 3 +- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/kit/package.json b/packages/kit/package.json index bd56cdf8273c..6bf8ba3aca09 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -29,6 +29,7 @@ "require-relative": "^0.8.7", "rimraf": "^3.0.2", "sirv": "^1.0.7", + "source-map": "^0.7.3", "source-map-support": "^0.5.19", "svelte": "^3.29.0", "tiny-glob": "^0.2.8" diff --git a/packages/kit/src/api/dev/sourcemap_stacktrace.js b/packages/kit/src/api/dev/sourcemap_stacktrace.js index fb35d712ff89..6795dc9e1baa 100644 --- a/packages/kit/src/api/dev/sourcemap_stacktrace.js +++ b/packages/kit/src/api/dev/sourcemap_stacktrace.js @@ -13,25 +13,36 @@ function get_sourcemap_url(contents) { const file_cache = new Map(); -function get_file_contents(path) { - if (file_cache.has(path)) { - return file_cache.get(path); +function get_file_contents(file_path) { + if (file_cache.has(file_path)) { + return file_cache.get(file_path); } try { - const data = fs.readFileSync(path, 'utf8'); - file_cache.set(path, data); + const data = fs.readFileSync(file_path, 'utf8'); + file_cache.set(file_path, data); return data; } catch { return undefined; } } -export function sourcemap_stacktrace(stack) { +async function replace_async(str, regex, asyncFn) { + const promises = []; + str.replace(regex, (match, ...args) => { + const promise = asyncFn(match, ...args); + promises.push(promise); + }); + const data = await Promise.all(promises); + return str.replace(regex, () => data.shift()); +} + +export async function sourcemap_stacktrace(stack) { const replace = (line) => - line.replace( + replace_async( + line, /^ {4}at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?)\)?/, - (input, var_name, file_path, line, column) => { + async (input, var_name, file_path, line_number, column) => { if (!file_path) return input; const contents = get_file_contents(file_path); @@ -69,9 +80,9 @@ export function sourcemap_stacktrace(stack) { // TODO: according to typings, this code cannot work; // the constructor returns a promise that needs to be awaited - const consumer = new SourceMapConsumer(raw_sourcemap); + const consumer = await new SourceMapConsumer(raw_sourcemap); const pos = consumer.originalPositionFor({ - line: Number(line), + line: Number(line_number), column: Number(column), bias: SourceMapConsumer.LEAST_UPPER_BOUND }); @@ -88,5 +99,5 @@ export function sourcemap_stacktrace(stack) { file_cache.clear(); - return stack.split('\n').map(replace).join('\n'); + return (await Promise.all(stack.split('\n').map(replace))).join('\n'); } diff --git a/packages/kit/src/renderer/page.js b/packages/kit/src/renderer/page.js index fd707254ea63..3e0f36daf443 100644 --- a/packages/kit/src/renderer/page.js +++ b/packages/kit/src/renderer/page.js @@ -5,6 +5,7 @@ import fetch, { Response } from 'node-fetch'; import { writable } from 'svelte/store'; import { parse, resolve, URLSearchParams } from 'url'; import { render } from './index'; +import { sourcemap_stacktrace } from '../api/dev/sourcemap_stacktrace' async function get_response({ request, @@ -284,7 +285,7 @@ export default async function render_page( return { status: 500, headers: {}, - body: error.stack, // TODO probably not in prod? + body: await sourcemap_stacktrace(error.stack), // TODO probably not in prod? dependencies: {} }; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d5a49729f756..abb0daae0800 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -189,6 +189,7 @@ importers: require-relative: 0.8.7 rimraf: 3.0.2 sirv: 1.0.7 + source-map: 0.7.3 source-map-support: 0.5.19 svelte: 3.29.0 tiny-glob: 0.2.8 @@ -218,6 +219,7 @@ importers: scorta: ^1.0.0 sirv: ^1.0.7 snowpack: ^2.17.0 + source-map: ^0.7.3 source-map-support: ^0.5.19 svelte: ^3.29.0 tiny-glob: ^0.2.8 @@ -3787,7 +3789,6 @@ packages: resolution: integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== /source-map/0.7.3: - dev: false engines: node: '>= 8' resolution: