Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sveltejs/kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 7, 2020
2 parents 442ee4f + d0864c2 commit b04833b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 22 additions & 11 deletions packages/kit/src/api/dev/sourcemap_stacktrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
});
Expand All @@ -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');
}
3 changes: 2 additions & 1 deletion packages/kit/src/renderer/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, options, session, page, status = 200, error }) {
let redirected;
Expand Down Expand Up @@ -275,7 +276,7 @@ export default async function render_page(request, context, options) {
return {
status: 500,
headers: {},
body: error.stack, // TODO probably not in prod?
body: await sourcemap_stacktrace(error.stack), // TODO probably not in prod?
dependencies: {}
};
}
Expand Down
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b04833b

Please sign in to comment.