import-in-the-middle 1.2.0 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hook.mjs +47 -10
- package/package.json +1 -1
- package/fuck.mjs +0 -12
package/hook.mjs
CHANGED
|
@@ -6,34 +6,70 @@ const specifiers = new Map()
|
|
|
6
6
|
|
|
7
7
|
const EXTENSION_RE = /\.(js|mjs|cjs)$/
|
|
8
8
|
|
|
9
|
+
const NODE_MAJOR = Number(process.versions.node.split('.')[0])
|
|
10
|
+
|
|
9
11
|
let entrypoint
|
|
10
12
|
|
|
13
|
+
function hasIitm (url) {
|
|
14
|
+
try {
|
|
15
|
+
return new URL(url).searchParams.has('iitm')
|
|
16
|
+
} catch {
|
|
17
|
+
return false
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function deleteIitm (url) {
|
|
22
|
+
let resultUrl
|
|
23
|
+
try {
|
|
24
|
+
const urlObj = new URL(url)
|
|
25
|
+
if (urlObj.searchParams.has('iitm')) {
|
|
26
|
+
urlObj.searchParams.delete('iitm')
|
|
27
|
+
resultUrl = urlObj.href
|
|
28
|
+
if (resultUrl.startsWith('file:node:')) {
|
|
29
|
+
resultUrl = resultUrl.replace('file:', '')
|
|
30
|
+
}
|
|
31
|
+
if (resultUrl.startsWith('file:///node:')) {
|
|
32
|
+
resultUrl = resultUrl.replace('file:///', '')
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
resultUrl = urlObj.href
|
|
36
|
+
}
|
|
37
|
+
} catch {
|
|
38
|
+
resultUrl = url
|
|
39
|
+
}
|
|
40
|
+
return resultUrl
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function addIitm (url) {
|
|
44
|
+
const urlObj = new URL(url)
|
|
45
|
+
urlObj.searchParams.set('iitm', 'true')
|
|
46
|
+
return urlObj.protocol !== 'file:' && NODE_MAJOR < 18 ? 'file:' + urlObj.href : urlObj.href
|
|
47
|
+
}
|
|
48
|
+
|
|
11
49
|
export async function resolve (specifier, context, parentResolve) {
|
|
12
50
|
const { parentURL = '' } = context
|
|
13
51
|
|
|
14
|
-
|
|
15
|
-
specifier = specifier.replace('iitm:', '')
|
|
16
|
-
}
|
|
17
|
-
const url = await parentResolve(specifier, context, parentResolve)
|
|
52
|
+
const url = await parentResolve(deleteIitm(specifier), context, parentResolve)
|
|
18
53
|
|
|
19
54
|
if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
|
|
20
55
|
entrypoint = url.url
|
|
21
56
|
return { url: url.url, format: 'commonjs' }
|
|
22
57
|
}
|
|
23
58
|
|
|
24
|
-
if (parentURL === import.meta.url || parentURL
|
|
59
|
+
if (parentURL === import.meta.url || hasIitm(parentURL)) {
|
|
25
60
|
return url
|
|
26
61
|
}
|
|
27
62
|
|
|
28
63
|
specifiers.set(url.url, specifier)
|
|
29
64
|
|
|
30
65
|
return {
|
|
31
|
-
url:
|
|
66
|
+
url: addIitm(url.url),
|
|
67
|
+
shortCircuit: true
|
|
32
68
|
}
|
|
33
69
|
}
|
|
34
70
|
|
|
35
71
|
export function getFormat (url, context, parentGetFormat) {
|
|
36
|
-
if (url
|
|
72
|
+
if (hasIitm(url)) {
|
|
37
73
|
return {
|
|
38
74
|
format: 'module'
|
|
39
75
|
}
|
|
@@ -49,8 +85,8 @@ export function getFormat (url, context, parentGetFormat) {
|
|
|
49
85
|
|
|
50
86
|
const iitmURL = new URL('lib/register.js', import.meta.url).toString()
|
|
51
87
|
export async function getSource (url, context, parentGetSource) {
|
|
52
|
-
if (url
|
|
53
|
-
const realUrl = url
|
|
88
|
+
if (hasIitm(url)) {
|
|
89
|
+
const realUrl = deleteIitm(url)
|
|
54
90
|
const realModule = await import(realUrl)
|
|
55
91
|
const exportNames = Object.keys(realModule)
|
|
56
92
|
return {
|
|
@@ -76,10 +112,11 @@ register('${realUrl}', namespace, set, '${specifiers.get(realUrl)}')
|
|
|
76
112
|
|
|
77
113
|
// For Node.js 16.12.0 and higher.
|
|
78
114
|
export async function load (url, context, parentLoad) {
|
|
79
|
-
if (url
|
|
115
|
+
if (hasIitm(url)) {
|
|
80
116
|
const { source } = await getSource(url, context)
|
|
81
117
|
return {
|
|
82
118
|
source,
|
|
119
|
+
shortCircuit: true,
|
|
83
120
|
format: 'module'
|
|
84
121
|
}
|
|
85
122
|
}
|
package/package.json
CHANGED
package/fuck.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import vm from 'vm'
|
|
2
|
-
|
|
3
|
-
const context = vm.createContext()
|
|
4
|
-
console.log(vm)
|
|
5
|
-
const mod = new vm.SourceTextModule(`
|
|
6
|
-
export const bar = 5
|
|
7
|
-
`, { context })
|
|
8
|
-
await mod.link(async () => {
|
|
9
|
-
return new vm.SyntheticModule([], function () {
|
|
10
|
-
}, { context })
|
|
11
|
-
})
|
|
12
|
-
console.log(Reflect.ownKeys(mod.namespace))
|