This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
64 lines (58 loc) · 2.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// A simple shim for all languages that isolates your JS code's interface to manticore services
//
const closure = {
exports: {},
};
const g = global;
/**
* The native interface is where services provided by the hosting process will be injected.
* This is usually just "global.manticore," but in Node.js we don't really want to pollute
* global like that.
*/
export const native = (() => {
if (g.manticore) {
return g.manticore;
}
closure.manticore = {};
return closure.manticore;
})();
/**
* Export a set of objects to the native layer, as well as
* adding them to your module exports (for JS platforms)
* @param module Your module (e.g. put "module" here)
* @param exportsObj The object containing items to export
*/
export function nativeExport(module, exportsObj) {
// const exportsObj = (exports === undefined ? module.exports : exports); // won't work; don't try
if (exportsObj === null || typeof exportsObj !== 'object') {
throw new Error(`nativeExport expected exports to be an object, got ${typeof exportsObj}`);
}
const nativeExports = g.exports || closure.exports;
for (const k of Object.getOwnPropertyNames(exportsObj)) {
nativeExports[k] = module.exports[k] = exportsObj[k];
}
}
let _fetch = g.fetch;
// Trick browserify to avoid pulling node-fetch into all native bundles
// since the native bundles are in charge of providing their own fetch function
if (!_fetch) {
const fakeBrowserify = require;
_fetch = fakeBrowserify('node-fetch');
}
/**
* The HTTP "fetch" function, exposed via manticore so that you can write your JS code
* to work in both node and other supported manticore environments. Your code should
* call manticore.fetch, and let higher modules (e.g. the app/tool) decide what
* implementation that should use.
* TODO this is not a faithful implementation of fetch yet. It's close enough for simple
* tasks, but we should flesh this out a lot more in the polyfills for the various
* runtimes (including node).
*/
export function fetch(...args) {
return _fetch.apply(null, args);
}
/**
* This allows "import manticore from 'manticore';" to do something useful
*/
export default native;