[Pkg-javascript-commits] [node-browser-unpack] 19/40: Prevent errors when passed empty modules in bundle.

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 1d12ee424ef6486c12b4d8a4d76869ed2067dfaf
Author: Hugh Kennedy <hughskennedy at gmail.com>
Date:   Sat Feb 22 15:45:55 2014 +1100

    Prevent errors when passed empty modules in bundle.
    
    If the function body is empty, a `range` value will not be
    defined on the body and an error will be thrown trying to
    access its properties.
---
 index.js            |  9 +++++--
 test/files/empty.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/unpack.js      | 18 ++++++++++++++
 3 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/index.js b/index.js
index f5e4be8..669e6eb 100644
--- a/index.js
+++ b/index.js
@@ -35,8 +35,13 @@ module.exports = function (src) {
         var body = file.value.elements[0].body.body;
         var start, end;
         if (body.length === 0) {
-            start = body.range[0];
-            end = body.range[1];
+            if (body.range) {
+                start = body.range[0];
+                end = body.range[1];
+            } else {
+                start = 0;
+                end = 0;
+            }
         }
         else {
             start = body[0].range[0];
diff --git a/test/files/empty.js b/test/files/empty.js
new file mode 100644
index 0000000..e6f3a25
--- /dev/null
+++ b/test/files/empty.js
@@ -0,0 +1,71 @@
+(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
+// transliterated from the python snippet here:
+// http://en.wikipedia.org/wiki/Lanczos_approximation
+
+var g = 7;
+var p = [
+    0.99999999999980993,
+    676.5203681218851,
+    -1259.1392167224028,
+    771.32342877765313,
+    -176.61502916214059,
+    12.507343278686905,
+    -0.13857109526572012,
+    9.9843695780195716e-6,
+    1.5056327351493116e-7
+];
+
+var g_ln = 607/128;
+var p_ln = [
+    0.99999999999999709182,
+    57.156235665862923517,
+    -59.597960355475491248,
+    14.136097974741747174,
+    -0.49191381609762019978,
+    0.33994649984811888699e-4,
+    0.46523628927048575665e-4,
+    -0.98374475304879564677e-4,
+    0.15808870322491248884e-3,
+    -0.21026444172410488319e-3,
+    0.21743961811521264320e-3,
+    -0.16431810653676389022e-3,
+    0.84418223983852743293e-4,
+    -0.26190838401581408670e-4,
+    0.36899182659531622704e-5
+];
+
+// Spouge approximation (suitable for large arguments)
+function lngamma(z) {
+
+    if(z < 0) return Number('0/0');
+    var x = p_ln[0];
+    for(var i = p_ln.length - 1; i > 0; --i) x += p_ln[i] / (z + i);
+    var t = z + g_ln + 0.5;
+    return .5*Math.log(2*Math.PI)+(z+.5)*Math.log(t)-t+Math.log(x)-Math.log(z);
+}
+
+module.exports = function gamma (z) {
+    if (z < 0.5) {
+        return Math.PI / (Math.sin(Math.PI * z) * gamma(1 - z));
+    }
+    else if(z > 100) return Math.exp(lngamma(z));
+    else {
+        z -= 1;
+        var x = p[0];
+        for (var i = 1; i < g + 2; i++) {
+            x += p[i] / (z + i);
+        }
+        var t = z + g + 0.5;
+
+        return Math.sqrt(2 * Math.PI)
+            * Math.pow(t, z + 0.5)
+            * Math.exp(-t)
+            * x
+        ;
+    }
+};
+
+module.exports.log = lngamma;
+
+},{}],2:[function(require,module,exports){
+},{}],3:[function(require,module,exports){},{"./foo.js":2,"gamma":1}]},{},[3])
diff --git a/test/unpack.js b/test/unpack.js
index 83c0a5c..105f680 100644
--- a/test/unpack.js
+++ b/test/unpack.js
@@ -6,6 +6,7 @@ var vm = require('vm');
 
 var fs = require('fs');
 var src = fs.readFileSync(__dirname + '/files/bundle.js', 'utf8');
+var empty = fs.readFileSync(__dirname + '/files/empty.js', 'utf8');
 
 test('unpack', function (t) {
     t.plan(1);
@@ -23,3 +24,20 @@ test('unpack', function (t) {
     rows.forEach(function (row) { p.write(row) });
     p.end();
 });
+
+test('does not break on empty modules', function (t) {
+    t.plan(1);
+
+    var p = pack({ raw: true });
+    var rows = unpack(empty);
+
+    p.pipe(concat(function (body) {
+        t.pass('did not emit an error');
+    }))
+
+    rows.forEach(function (row) {
+        p.write(row);
+    })
+
+    p.end();
+})

-- 
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