-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource.js
42 lines (37 loc) · 1.57 KB
/
resource.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
import { readFileSync } from 'fs'
import { runInThisContext } from 'vm'
import { tryRequireResolve } from 'source/env/tryRequire'
import { fetchLikeRequest } from 'source/node/net'
import { readFileAsync } from 'source/node/file/function'
const DR_BROWSER_FILE_PATH = () => [
'./Dr.browser.js', // maybe after webpack
'../Dr.browser.js', // relative to source/env/tryRequire
'@dr-js/core/library/Dr.browser.js' // within normal node_module structure
].reduce((o, path) => o || tryRequireResolve(path), null)
let cache = ''
const DR_BROWSER_SCRIPT_TAG = () => {
if (cache === '') cache = `<script>${readFileSync(DR_BROWSER_FILE_PATH())}</script>`
return cache
}
// TODO: check if is needed, or simplify
const loadRemoteScript = async (uri) => {
const scriptString = await (await fetchLikeRequest(uri)).text()
return runInThisContext(scriptString, { filename: uri, displayErrors: true })
}
const loadLocalScript = async (filePath) => {
const scriptString = String(await readFileAsync(filePath))
return runInThisContext(scriptString, { filename: filePath, displayErrors: true })
}
const loadScript = (uri) => uri.includes('://')
? loadRemoteScript(uri)
: loadLocalScript(uri)
const loadRemoteJSON = async (uri) => (await fetchLikeRequest(uri)).json()
const loadLocalJSON = async (filePath) => JSON.parse(String(await readFileAsync(filePath)))
const loadJSON = (uri) => uri.includes('://')
? loadRemoteJSON(uri)
: loadLocalJSON(uri)
export {
DR_BROWSER_FILE_PATH, DR_BROWSER_SCRIPT_TAG,
loadRemoteScript, loadLocalScript, loadScript,
loadRemoteJSON, loadLocalJSON, loadJSON
}