From 399b9e4ff6c60fcbf7f0b19698778c48fd183daa Mon Sep 17 00:00:00 2001 From: tmont Date: Mon, 7 Dec 2020 11:20:13 -0800 Subject: [PATCH] chore: moved Buffer handling for debug_sources to read phase --- packages/pug-runtime/index.js | 7 +-- packages/pug-runtime/test/index.test.js | 13 +++--- packages/pug/lib/index.js | 2 +- .../pug/test/__snapshots__/pug.test.js.snap | 44 ++++++++++--------- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/packages/pug-runtime/index.js b/packages/pug-runtime/index.js index 4376afce7..2239a0a61 100644 --- a/packages/pug-runtime/index.js +++ b/packages/pug-runtime/index.js @@ -249,16 +249,13 @@ function pug_rethrow(err, filename, lineno, str) { } var context, lines, start, end; try { - var encoding = 'utf8'; - str = str || require('fs').readFileSync(filename, {encoding: encoding}); - if (str.type === 'Buffer') { - str = Buffer.from(str.data).toString(encoding); - } + str = str || require('fs').readFileSync(filename, {encoding: 'utf8'}); context = 3; lines = str.split('\n'); start = Math.max(lineno - context, 0); end = Math.min(lines.length, lineno + context); } catch (ex) { + err.message += ' - could not read from ' + filename + ' (' + ex.message + ')'; pug_rethrow(err, null, lineno); return; } diff --git a/packages/pug-runtime/test/index.test.js b/packages/pug-runtime/test/index.test.js index 61921b625..0b5d78edd 100644 --- a/packages/pug-runtime/test/index.test.js +++ b/packages/pug-runtime/test/index.test.js @@ -252,18 +252,15 @@ foo.pug:3 throw new Error('expected rethrow to throw'); }); - it("should rethrow error with toJSON()'d Buffer", () => { - const err = new Error(); - const str = Buffer.from('hello world').toJSON(); + it("should handle bad arguments gracefully", () => { + const err = new Error('hello world'); + const str = { not: 'a string' }; try { runtime.rethrow(err, 'foo.pug', 3, str); } catch (e) { expect(e).toBe(err); - expect(e.message.trim()).toBe( - ` -foo.pug:3 - 1| hello world`.trim() - ); + expect(e.message) + .toBe('hello world - could not read from foo.pug (str.split is not a function) on line 3'); return; } diff --git a/packages/pug/lib/index.js b/packages/pug/lib/index.js index ec734f7bb..54846126f 100644 --- a/packages/pug/lib/index.js +++ b/packages/pug/lib/index.js @@ -164,7 +164,7 @@ function compileBody(str, options) { contents = load.read(filename, loadOptions); } - debug_sources[filename] = contents; + debug_sources[filename] = Buffer.isBuffer(contents) ? contents.toString('utf8') : contents; return contents; }, }); diff --git a/packages/pug/test/__snapshots__/pug.test.js.snap b/packages/pug/test/__snapshots__/pug.test.js.snap index 44c29c602..8e3600c04 100644 --- a/packages/pug/test/__snapshots__/pug.test.js.snap +++ b/packages/pug/test/__snapshots__/pug.test.js.snap @@ -56,33 +56,35 @@ exports[`pug .compileClient() should support module syntax in pug.compileClient( return c !== r ? s + a.substring(c, r) : s; } var pug_match_html = /[\\"&<>]/; -function pug_rethrow(n, t, e, r) { - if (!(n instanceof Error)) throw n; - if (!((\\"undefined\\" == typeof window && t) || r)) - throw ((n.message += \\" on line \\" + e), n); - var i, o, a, f; +function pug_rethrow(e, n, r, t) { + if (!(e instanceof Error)) throw e; + if (!((\\"undefined\\" == typeof window && n) || t)) + throw ((e.message += \\" on line \\" + r), e); + var o, a, i, s; try { - (r = r || require(\\"fs\\").readFileSync(t, { encoding: \\"utf8\\" })), - \\"Buffer\\" === r.type && (r = Buffer.from(r.data).toString(\\"utf8\\")), - (i = 3), - (o = r.split(\\"\\\\n\\")), - (a = Math.max(e - i, 0)), - (f = Math.min(o.length, e + i)); + (t = t || require(\\"fs\\").readFileSync(n, { encoding: \\"utf8\\" })), + (o = 3), + (a = t.split(\\"\\\\n\\")), + (i = Math.max(r - o, 0)), + (s = Math.min(a.length, r + o)); } catch (t) { - return void pug_rethrow(n, null, e); + return ( + (e.message += \\" - could not read from \\" + n + \\" (\\" + t.message + \\")\\"), + void pug_rethrow(e, null, r) + ); } - (i = o - .slice(a, f) - .map(function(n, t) { - var r = t + a + 1; - return (r == e ? \\" > \\" : \\" \\") + r + \\"| \\" + n; + (o = a + .slice(i, s) + .map(function(e, n) { + var t = n + i + 1; + return (t == r ? \\" > \\" : \\" \\") + t + \\"| \\" + e; }) .join(\\"\\\\n\\")), - (n.path = t); + (e.path = n); try { - n.message = (t || \\"Pug\\") + \\":\\" + e + \\"\\\\n\\" + i + \\"\\\\n\\\\n\\" + n.message; - } catch (n) {} - throw n; + e.message = (n || \\"Pug\\") + \\":\\" + r + \\"\\\\n\\" + o + \\"\\\\n\\\\n\\" + e.message; + } catch (e) {} + throw e; } function template(locals) { var pug_html = \\"\\",