Skip to content

Commit 2db758c

Browse files
committed
iojs: introduce internal modules
Internal modules can be used to share private code between public modules without risk to expose private APIs to the user. PR-URL: #848 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 4581421 commit 2db758c

File tree

12 files changed

+91
-35
lines changed

12 files changed

+91
-35
lines changed

lib/_http_common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const FreeList = require('freelist').FreeList;
3+
const FreeList = require('internal/freelist').FreeList;
44
const HTTPParser = process.binding('http_parser').HTTPParser;
55

66
const incoming = require('_http_incoming');

lib/freelist.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
11
'use strict';
22

3-
// This is a free list to avoid creating so many of the same object.
4-
exports.FreeList = function(name, max, constructor) {
5-
this.name = name;
6-
this.constructor = constructor;
7-
this.max = max;
8-
this.list = [];
9-
};
10-
11-
12-
exports.FreeList.prototype.alloc = function() {
13-
//debug("alloc " + this.name + " " + this.list.length);
14-
return this.list.length ? this.list.shift() :
15-
this.constructor.apply(this, arguments);
16-
};
17-
18-
19-
exports.FreeList.prototype.free = function(obj) {
20-
//debug("free " + this.name + " " + this.list.length);
21-
if (this.list.length < this.max) {
22-
this.list.push(obj);
23-
return true;
24-
}
25-
return false;
26-
};
3+
module.exports = require('internal/freelist');

lib/internal/freelist.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
// This is a free list to avoid creating so many of the same object.
4+
exports.FreeList = function(name, max, constructor) {
5+
this.name = name;
6+
this.constructor = constructor;
7+
this.max = max;
8+
this.list = [];
9+
};
10+
11+
12+
exports.FreeList.prototype.alloc = function() {
13+
return this.list.length ? this.list.shift() :
14+
this.constructor.apply(this, arguments);
15+
};
16+
17+
18+
exports.FreeList.prototype.free = function(obj) {
19+
if (this.list.length < this.max) {
20+
this.list.push(obj);
21+
return true;
22+
}
23+
return false;
24+
};

lib/module.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Module._nodeModulePaths = function(from) {
200200

201201

202202
Module._resolveLookupPaths = function(request, parent) {
203-
if (NativeModule.exists(request)) {
203+
if (NativeModule.nonInternalExists(request)) {
204204
return [request, []];
205205
}
206206

@@ -262,7 +262,7 @@ Module._load = function(request, parent, isMain) {
262262
return cachedModule.exports;
263263
}
264264

265-
if (NativeModule.exists(filename)) {
265+
if (NativeModule.nonInternalExists(filename)) {
266266
// REPL is a special case, because it needs the real require.
267267
if (filename == 'repl') {
268268
var replModule = new Module('repl');
@@ -299,7 +299,7 @@ Module._load = function(request, parent, isMain) {
299299
};
300300

301301
Module._resolveFilename = function(request, parent) {
302-
if (NativeModule.exists(request)) {
302+
if (NativeModule.nonInternalExists(request)) {
303303
return request;
304304
}
305305

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
'lib/v8.js',
7070
'lib/vm.js',
7171
'lib/zlib.js',
72+
73+
'lib/internal/freelist.js',
7274
],
7375
},
7476

src/node.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3133,6 +3133,9 @@ static void ParseArgs(int* argc,
31333133
} else if (strncmp(arg, "--icu-data-dir=", 15) == 0) {
31343134
icu_data_dir = arg + 15;
31353135
#endif
3136+
} else if (strcmp(arg, "--expose-internals") == 0 ||
3137+
strcmp(arg, "--expose_internals") == 0) {
3138+
// consumed in js
31363139
} else {
31373140
// V8 option. Pass through as-is.
31383141
new_v8_argv[new_v8_argc] = arg;

src/node.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,27 @@
838838
return NativeModule._source.hasOwnProperty(id);
839839
};
840840

841+
const EXPOSE_INTERNALS = process.execArgv.some(function(arg) {
842+
return arg.match(/^--expose[-_]internals$/);
843+
});
844+
845+
if (EXPOSE_INTERNALS) {
846+
NativeModule.nonInternalExists = NativeModule.exists;
847+
848+
NativeModule.isInternal = function(id) {
849+
return false;
850+
};
851+
} else {
852+
NativeModule.nonInternalExists = function(id) {
853+
return NativeModule.exists(id) && !NativeModule.isInternal(id);
854+
};
855+
856+
NativeModule.isInternal = function(id) {
857+
return id.startsWith('internal/');
858+
};
859+
}
860+
861+
841862
NativeModule.getSource = function(id) {
842863
return NativeModule._source[id];
843864
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('internal/freelist');

test/fixtures/internal-modules/node_modules/internal/freelist.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Flags: --expose_internals
2+
3+
var common = require('../common');
4+
var assert = require('assert');
5+
6+
assert.equal(typeof require('internal/freelist').FreeList, 'function');

0 commit comments

Comments
 (0)