Skip to content

Commit adb3d21

Browse files
committed
Fix requiring JSON arrays or other non-objects.
It was doing element-wise copy, but it should just be assigning the exports.
1 parent 2fe5c6e commit adb3d21

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

lib/sandboxed_module.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,7 @@ SandboxedModule.prototype._requireInterceptor = function() {
233233

234234
SandboxedModule.prototype._compile = function() {
235235
if (/\.json$/.test(this.filename)){
236-
var json = require(this.filename);
237-
for( var key in json){
238-
this.exports[key] = json[key];
239-
}
236+
this.module.exports = require(this.filename);
240237
return;
241238
}
242239
var compile = this._getCompileInfo();

test/fixture/includeJson.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
var json =require('./json');
2-
3-
module.exports.json = json;
1+
exports.json = require('./json');
2+
exports.jsonArray = require('./jsonArray.json');

test/fixture/jsonArray.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["foo", "bar"]

test/integration/test-recursive-json.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ var fakeBar = 'fakeBar';
55
var requireModule = SandboxedModule.load('../fixture/includeJson');
66
var recursiveExports = requireModule.exports;
77
assert.strictEqual(recursiveExports.json.value, 'value from json');
8+
9+
assert.deepEqual(recursiveExports.jsonArray, ['foo', 'bar']);
10+
assert.ok(Array.isArray(recursiveExports.jsonArray));

0 commit comments

Comments
 (0)