-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathloader.browser.js
More file actions
64 lines (56 loc) · 1.92 KB
/
Copy pathloader.browser.js
File metadata and controls
64 lines (56 loc) · 1.92 KB
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
/**
* Author: Christoph Dorn <christoph@christophdorn.com>
* License: Zero-Clause BSD - https://opensource.org/licenses/0BSD
**/
exports.PINF = (function (global) {
if (!global || typeof global !== "object") {
throw new Error("No root object scope provided!");
}
// If `PINF` gloabl already exists, don't do anything to change it.
if (typeof global.PINF !== "undefined") {
return global.PINF;
}
const LOADER = require('./loader');
const PINF = LOADER.Loader({
document: global.document
});
global.PINF = PINF;
// Attach postMessage handler to listen for sandbox load triggers.
// This is useful in Web Workers where only the loader must be loaded and
// sandboxes can then be loaded like this:
// worker.postMessage(URIJS("notify://pinf-loader-js/sandbox/load").addSearch("uri", uri).toString())
if (typeof global.addEventListener === "function") {
global.addEventListener("message", function (event) {
var m = null;
if (
typeof event.data === "string" &&
(m = event.data.match(/^notify:\/\/pinf-loader-js\/sandbox\/load\?uri=(.+)$/)) &&
(m = decodeURIComponent(m[1])) &&
// SECURITY: Only allow URIs that begin with `/` so that scripts may NOT
// be loaded cross-domain this way. If this was allowed one could
// load malicious code simply by posting a message to this window.
/^\/[^\/]/.test(m)
) {
return PINF.sandbox(m, function (sandbox) {
sandbox.main();
if (typeof global.postMessage === "function") {
global.postMessage(event.data.replace("/load?", "/loaded?"));
}
}, function (err) {
// TODO: Post error back to main frame instead of throwing?
throw err;
});
}
}, false);
}
return global.PINF;
}(
typeof window !== "undefined" ?
// Used in the browser
window :
typeof self !== "undefined" ?
// Used in web worker
self :
// No root scope variable found
null
));