[Pkg-javascript-commits] [node-browser-unpack] 24/40: convert input buffers to strings before passing them to esprima
Bastien Roucariès
rouca at moszumanska.debian.org
Thu Nov 9 12:27:24 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-browser-unpack.
commit 7ca231af39eab56ede6ad7b80f8619033e5e2eaf
Author: Ian Henry <ian at trello.com>
Date: Wed Feb 18 19:59:40 2015 -0500
convert input buffers to strings before passing them to esprima
This fixes https://github.com/substack/browser-unpack/issues/10
Previously, if you passed a UTF-8 encoded buffer to browser-unpack, it would
generate invalid "source" values for any files that included or came after a
multi-bute UTF-8 sequence.
---
index.js | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/index.js b/index.js
index 59947b3..abe867a 100644
--- a/index.js
+++ b/index.js
@@ -1,6 +1,14 @@
var parse = require('esprima-fb').parse;
module.exports = function (src) {
+ // If src is a Buffer, esprima will just stringify it, so we beat them to
+ // the punch. This avoids the problem where we're using esprima's range
+ // indexes -- which are meant for a UTF-16 string -- in a buffer that
+ // contains UTF-8 encoded text.
+ if (typeof src !== 'string') {
+ src = String(src);
+ }
+
var ast = parse(src, { range: true });
ast.body = ast.body.filter(function(node) {
@@ -57,7 +65,7 @@ module.exports = function (src) {
}, {});
var row = {
id: file.key.value,
- source: src.slice(start, end).toString('utf8'),
+ source: src.slice(start, end),
deps: deps
};
if (entries.indexOf(row.id) >= 0) row.entry = true;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browser-unpack.git
More information about the Pkg-javascript-commits
mailing list