-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.js
27 lines (24 loc) · 996 Bytes
/
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
import { runInThisContext } from 'node:vm'
import { readText, readJSON } from '@dr-js/core/module/node/fs/File.js'
import { fetchLikeRequest } from '@dr-js/core/module/node/net.js'
// 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 = await readText(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 = readJSON
const loadJSON = (uri) => uri.includes('://')
? loadRemoteJSON(uri)
: loadLocalJSON(uri)
export {
loadRemoteScript, loadLocalScript, loadScript,
loadRemoteJSON, loadLocalJSON, loadJSON
}