|
@@ -1,13 +1,13 @@
|
|
|
// thanks to airbnb/hypernova
|
|
// thanks to airbnb/hypernova
|
|
|
|
|
|
|
|
-const NativeModule = require('module')
|
|
|
|
|
-const path = require('path')
|
|
|
|
|
-const { ok } = require('assert')
|
|
|
|
|
-const { runInNewContext } = require('vm')
|
|
|
|
|
|
|
+var NativeModule = require('module')
|
|
|
|
|
+var path = require('path')
|
|
|
|
|
+var assert = require('assert')
|
|
|
|
|
+var vm = require('vm')
|
|
|
|
|
|
|
|
-const NativeModules = process.binding('natives')
|
|
|
|
|
|
|
+var NativeModules = process.binding('natives')
|
|
|
|
|
|
|
|
-const moduleExtensions = Object.assign({}, NativeModule._extensions)
|
|
|
|
|
|
|
+var moduleExtensions = Object.assign({}, NativeModule._extensions)
|
|
|
|
|
|
|
|
function isNativeModule (id) {
|
|
function isNativeModule (id) {
|
|
|
return Object.prototype.hasOwnProperty.call(NativeModules, id)
|
|
return Object.prototype.hasOwnProperty.call(NativeModules, id)
|
|
@@ -15,7 +15,7 @@ function isNativeModule (id) {
|
|
|
|
|
|
|
|
// Creates a sandbox so we don't share globals across different runs.
|
|
// Creates a sandbox so we don't share globals across different runs.
|
|
|
function createContext () {
|
|
function createContext () {
|
|
|
- const sandbox = {
|
|
|
|
|
|
|
+ var sandbox = {
|
|
|
Buffer,
|
|
Buffer,
|
|
|
clearImmediate,
|
|
clearImmediate,
|
|
|
clearInterval,
|
|
clearInterval,
|
|
@@ -30,105 +30,102 @@ function createContext () {
|
|
|
return sandbox
|
|
return sandbox
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// This class should satisfy the Module interface that NodeJS defines in their native m.js
|
|
|
|
|
-// implementation.
|
|
|
|
|
-class Module {
|
|
|
|
|
- constructor (id, parent, isBundle) {
|
|
|
|
|
- const cache = parent ? parent.cache : null
|
|
|
|
|
- this.id = id
|
|
|
|
|
- this.exports = {}
|
|
|
|
|
- this.cache = cache || {}
|
|
|
|
|
- this.parent = parent
|
|
|
|
|
- this.filename = null
|
|
|
|
|
- this.loaded = false
|
|
|
|
|
- this.context = parent ? parent.context : createContext()
|
|
|
|
|
- this.isBundle = isBundle
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- load (filename) {
|
|
|
|
|
- ok(!this.loaded)
|
|
|
|
|
- this.filename = filename
|
|
|
|
|
- this.paths = NativeModule._nodeModulePaths(path.dirname(filename))
|
|
|
|
|
- }
|
|
|
|
|
|
|
+function Module (id, parent, isBundle) {
|
|
|
|
|
+ var cache = parent ? parent.cache : null
|
|
|
|
|
+ this.id = id
|
|
|
|
|
+ this.exports = {}
|
|
|
|
|
+ this.cache = cache || {}
|
|
|
|
|
+ this.parent = parent
|
|
|
|
|
+ this.filename = null
|
|
|
|
|
+ this.loaded = false
|
|
|
|
|
+ this.context = parent ? parent.context : createContext()
|
|
|
|
|
+ this.isBundle = isBundle
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- run (filename) {
|
|
|
|
|
- const ext = path.extname(filename)
|
|
|
|
|
- const extension = moduleExtensions[ext] ? ext : '.js'
|
|
|
|
|
- moduleExtensions[extension](this, filename)
|
|
|
|
|
- this.loaded = true
|
|
|
|
|
- }
|
|
|
|
|
|
|
+Module.prototype.load = function (filename) {
|
|
|
|
|
+ assert.ok(!this.loaded)
|
|
|
|
|
+ this.filename = filename
|
|
|
|
|
+ this.paths = NativeModule._nodeModulePaths(path.dirname(filename))
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- require (filePath) {
|
|
|
|
|
- ok(typeof filePath === 'string', 'path must be a string')
|
|
|
|
|
- return Module.loadFile(filePath, this)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+Module.prototype.run = function (filename) {
|
|
|
|
|
+ var ext = path.extname(filename)
|
|
|
|
|
+ var extension = moduleExtensions[ext] ? ext : '.js'
|
|
|
|
|
+ moduleExtensions[extension](this, filename)
|
|
|
|
|
+ this.loaded = true
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- _compile (content, filename) {
|
|
|
|
|
- const self = this
|
|
|
|
|
|
|
+Module.prototype.require = function (filePath) {
|
|
|
|
|
+ assert.ok(typeof filePath === 'string', 'path must be a string')
|
|
|
|
|
+ return Module.loadFile(filePath, this)
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- function r (filePath) {
|
|
|
|
|
- return self.require(filePath)
|
|
|
|
|
- }
|
|
|
|
|
- r.resolve = request => NativeModule._resolveFilename(request, this)
|
|
|
|
|
- r.main = process.mainModule
|
|
|
|
|
- r.extensions = moduleExtensions
|
|
|
|
|
- r.cache = this.cache
|
|
|
|
|
|
|
+Module.prototype._compile = function (content, filename) {
|
|
|
|
|
+ var self = this
|
|
|
|
|
|
|
|
- const dirname = path.dirname(filename)
|
|
|
|
|
|
|
+ function r (filePath) {
|
|
|
|
|
+ return self.require(filePath)
|
|
|
|
|
+ }
|
|
|
|
|
+ r.resolve = request => NativeModule._resolveFilename(request, this)
|
|
|
|
|
+ r.main = process.mainModule
|
|
|
|
|
+ r.extensions = moduleExtensions
|
|
|
|
|
+ r.cache = this.cache
|
|
|
|
|
|
|
|
- // create wrapper function
|
|
|
|
|
- const wrapper = NativeModule.wrap(content)
|
|
|
|
|
|
|
+ var dirname = path.dirname(filename)
|
|
|
|
|
|
|
|
- const options = {
|
|
|
|
|
- filename,
|
|
|
|
|
- displayErrors: true
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // create wrapper function
|
|
|
|
|
+ var wrapper = NativeModule.wrap(content)
|
|
|
|
|
|
|
|
- const compiledWrapper = runInNewContext(wrapper, this.context, options)
|
|
|
|
|
- return compiledWrapper.call(this.exports, this.exports, r, this, filename, dirname)
|
|
|
|
|
|
|
+ var options = {
|
|
|
|
|
+ filename,
|
|
|
|
|
+ displayErrors: true
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static load (id, filename = id) {
|
|
|
|
|
- const m = new Module(id)
|
|
|
|
|
- m.load(filename)
|
|
|
|
|
- m.run(filename)
|
|
|
|
|
- return m
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ var compiledWrapper = vm.runInNewContext(wrapper, this.context, options)
|
|
|
|
|
+ return compiledWrapper.call(this.exports, this.exports, r, this, filename, dirname)
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- static loadFile (file, parent) {
|
|
|
|
|
- const filename = NativeModule._resolveFilename(file, parent)
|
|
|
|
|
|
|
+Module.load = function (id, filename) {
|
|
|
|
|
+ var m = new Module(id)
|
|
|
|
|
+ filename = filename || id
|
|
|
|
|
+ m.load(filename)
|
|
|
|
|
+ m.run(filename)
|
|
|
|
|
+ return m
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if (parent) {
|
|
|
|
|
- const cachedModule = parent.cache[filename]
|
|
|
|
|
- if (cachedModule) return cachedModule.exports
|
|
|
|
|
- }
|
|
|
|
|
|
|
+Module.loadFile = function (file, parent) {
|
|
|
|
|
+ var filename = NativeModule._resolveFilename(file, parent)
|
|
|
|
|
|
|
|
- if (parent.isBundle || isNativeModule(filename)) {
|
|
|
|
|
- return require(filename)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (parent) {
|
|
|
|
|
+ var cachedModule = parent.cache[filename]
|
|
|
|
|
+ if (cachedModule) return cachedModule.exports
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (parent.isBundle || isNativeModule(filename)) {
|
|
|
|
|
+ return require(filename)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- const m = new Module(filename, parent)
|
|
|
|
|
|
|
+ var m = new Module(filename, parent)
|
|
|
|
|
|
|
|
- m.cache[filename] = m
|
|
|
|
|
|
|
+ m.cache[filename] = m
|
|
|
|
|
|
|
|
- let hadException = true
|
|
|
|
|
|
|
+ var hadException = true
|
|
|
|
|
|
|
|
- try {
|
|
|
|
|
- m.load(filename)
|
|
|
|
|
- m.run(filename)
|
|
|
|
|
- hadException = false
|
|
|
|
|
- } finally {
|
|
|
|
|
- if (hadException) {
|
|
|
|
|
- delete m.cache[filename]
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ m.load(filename)
|
|
|
|
|
+ m.run(filename)
|
|
|
|
|
+ hadException = false
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (hadException) {
|
|
|
|
|
+ delete m.cache[filename]
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return m.exports
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static addExtension (ext, f) {
|
|
|
|
|
- moduleExtensions[ext] = f
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return m.exports
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Module.addExtension = function (ext, f) {
|
|
|
|
|
+ moduleExtensions[ext] = f
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
module.exports = Module
|
|
module.exports = Module
|