import-in-the-middle 1.2.1 → 1.2.2

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.
Files changed (2) hide show
  1. package/hook.mjs +37 -10
  2. package/package.json +1 -1
package/hook.mjs CHANGED
@@ -8,32 +8,58 @@ const EXTENSION_RE = /\.(js|mjs|cjs)$/
8
8
 
9
9
  let entrypoint
10
10
 
11
+ function hasIitm (url) {
12
+ try {
13
+ return new URL(url).searchParams.has('iitm')
14
+ } catch {
15
+ return false
16
+ }
17
+ }
18
+
19
+ function deleteIitm (url) {
20
+ let resultUrl
21
+ try {
22
+ const urlObj = new URL(url)
23
+ if (urlObj.searchParams.has('iitm')) {
24
+ urlObj.searchParams.delete('iitm')
25
+ }
26
+ resultUrl = urlObj.href
27
+ } catch {
28
+ resultUrl = url
29
+ }
30
+ return resultUrl
31
+ }
32
+
33
+ function addIitm (url) {
34
+ const urlObj = new URL(url)
35
+ urlObj.searchParams.set('iitm', 'true')
36
+ return urlObj.href
37
+ }
38
+
11
39
  export async function resolve (specifier, context, parentResolve) {
12
40
  const { parentURL = '' } = context
13
41
 
14
- if (specifier.startsWith('file:iitm:')) {
15
- specifier = specifier.replace('file:iitm:', '')
16
- }
17
- const url = await parentResolve(specifier, context, parentResolve)
42
+ const url = await parentResolve(deleteIitm(specifier), context, parentResolve)
18
43
 
19
44
  if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
20
45
  entrypoint = url.url
21
46
  return { url: url.url, format: 'commonjs' }
22
47
  }
23
48
 
24
- if (parentURL === import.meta.url || parentURL.startsWith('file:iitm:')) {
49
+ if (parentURL === import.meta.url || hasIitm(parentURL)) {
25
50
  return url
26
51
  }
27
52
 
28
53
  specifiers.set(url.url, specifier)
29
54
 
30
55
  return {
31
- url: `file:iitm:${url.url}`
56
+ url: addIitm(url.url),
57
+ shortCircuit: true
32
58
  }
33
59
  }
34
60
 
35
61
  export function getFormat (url, context, parentGetFormat) {
36
- if (url.startsWith('file:iitm:')) {
62
+ if (hasIitm(url)) {
37
63
  return {
38
64
  format: 'module'
39
65
  }
@@ -49,8 +75,8 @@ export function getFormat (url, context, parentGetFormat) {
49
75
 
50
76
  const iitmURL = new URL('lib/register.js', import.meta.url).toString()
51
77
  export async function getSource (url, context, parentGetSource) {
52
- if (url.startsWith('file:iitm:')) {
53
- const realUrl = url.replace('file:iitm:', '')
78
+ if (hasIitm(url)) {
79
+ const realUrl = deleteIitm(url)
54
80
  const realModule = await import(realUrl)
55
81
  const exportNames = Object.keys(realModule)
56
82
  return {
@@ -76,10 +102,11 @@ register('${realUrl}', namespace, set, '${specifiers.get(realUrl)}')
76
102
 
77
103
  // For Node.js 16.12.0 and higher.
78
104
  export async function load (url, context, parentLoad) {
79
- if (url.startsWith('file:iitm:')) {
105
+ if (hasIitm(url)) {
80
106
  const { source } = await getSource(url, context)
81
107
  return {
82
108
  source,
109
+ shortCircuit: true,
83
110
  format: 'module'
84
111
  }
85
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "import-in-the-middle",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {