2016-05-06 04:52:36 +00:00
|
|
|
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-boot
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2015-03-13 01:03:35 +00:00
|
|
|
var vm = require('vm');
|
|
|
|
|
|
|
|
function createContext() {
|
|
|
|
var context = {
|
|
|
|
// required by browserify
|
|
|
|
XMLHttpRequest: function() { throw new Error('not implemented'); },
|
2015-07-31 17:02:16 +00:00
|
|
|
FormData: function() { throw new Error('not implemented'); },
|
2015-03-13 01:03:35 +00:00
|
|
|
|
|
|
|
localStorage: {
|
|
|
|
// used by `debug` module
|
2016-03-31 11:29:39 +00:00
|
|
|
debug: process.env.DEBUG,
|
2015-03-13 01:03:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// used by DataSource.prototype.ready
|
|
|
|
setTimeout: setTimeout,
|
|
|
|
|
|
|
|
// used by `debug` module
|
2016-03-31 11:29:39 +00:00
|
|
|
document: { documentElement: { style: {}}},
|
2015-03-13 01:03:35 +00:00
|
|
|
|
|
|
|
// used by `debug` module
|
|
|
|
navigator: { userAgent: 'sandbox' },
|
|
|
|
|
|
|
|
// used by crypto-browserify & friends
|
|
|
|
Int32Array: Int32Array,
|
|
|
|
DataView: DataView,
|
2016-07-01 13:59:37 +00:00
|
|
|
crypto: {
|
|
|
|
getRandomValues: function(typedArray) {
|
|
|
|
var randomBuffer = require('crypto').randomBytes(typedArray.length);
|
|
|
|
// This implementation is not secure: we take random 8bit values
|
|
|
|
// and assign them to 8/16/32bit values, leaving high-order bits
|
|
|
|
// filled with zeroes.
|
|
|
|
// Fortunately, the bootstrapping process does not rely on secure
|
|
|
|
// random numbers, therefore we can afford such shortcut.
|
|
|
|
typedArray.set(randomBuffer);
|
|
|
|
},
|
|
|
|
},
|
2015-03-13 01:03:35 +00:00
|
|
|
|
|
|
|
// allow the browserified code to log messages
|
|
|
|
// call `printContextLogs(context)` to print the accumulated messages
|
|
|
|
console: {
|
|
|
|
log: function() {
|
|
|
|
this._logs.log.push(Array.prototype.slice.call(arguments));
|
|
|
|
},
|
|
|
|
warn: function() {
|
|
|
|
this._logs.warn.push(Array.prototype.slice.call(arguments));
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
this._logs.error.push(Array.prototype.slice.call(arguments));
|
|
|
|
},
|
|
|
|
_logs: {
|
|
|
|
log: [],
|
|
|
|
warn: [],
|
2016-03-31 11:29:39 +00:00
|
|
|
error: [],
|
2015-03-13 01:03:35 +00:00
|
|
|
},
|
2016-03-31 11:29:39 +00:00
|
|
|
},
|
2015-03-13 01:03:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// `window` is used by loopback to detect browser runtime
|
|
|
|
context.window = context;
|
|
|
|
|
|
|
|
return vm.createContext(context);
|
|
|
|
}
|
|
|
|
exports.createContext = createContext;
|
|
|
|
|
|
|
|
function printContextLogs(context) {
|
2016-03-31 11:29:39 +00:00
|
|
|
var k, ix; // see https://github.com/eslint/eslint/issues/5744
|
|
|
|
for (k in context.console._logs) {
|
2015-03-13 01:03:35 +00:00
|
|
|
var items = context.console._logs[k];
|
2016-03-31 11:29:39 +00:00
|
|
|
for (ix in items) {
|
2015-03-13 01:03:35 +00:00
|
|
|
console[k].apply(console, items[ix]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.printContextLogs = printContextLogs;
|