import-in-the-middle 1.1.2 → 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.
package/hook.mjs CHANGED
@@ -4,39 +4,79 @@
4
4
 
5
5
  const specifiers = new Map()
6
6
 
7
+ const EXTENSION_RE = /\.(js|mjs|cjs)$/
8
+
9
+ let entrypoint
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
+
7
39
  export async function resolve (specifier, context, parentResolve) {
8
40
  const { parentURL = '' } = context
9
41
 
10
- if (specifier.startsWith('iitm:')) {
11
- specifier = specifier.replace('iitm:', '')
42
+ const url = await parentResolve(deleteIitm(specifier), context, parentResolve)
43
+
44
+ if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
45
+ entrypoint = url.url
46
+ return { url: url.url, format: 'commonjs' }
12
47
  }
13
- const url = await parentResolve(specifier, context, parentResolve)
14
48
 
15
- if (parentURL === import.meta.url || parentURL.startsWith('iitm:')) {
49
+ if (parentURL === import.meta.url || hasIitm(parentURL)) {
16
50
  return url
17
51
  }
18
52
 
19
53
  specifiers.set(url.url, specifier)
20
54
 
21
55
  return {
22
- url: `iitm:${url.url}`
56
+ url: addIitm(url.url),
57
+ shortCircuit: true
23
58
  }
24
59
  }
25
60
 
26
61
  export function getFormat (url, context, parentGetFormat) {
27
- if (url.startsWith('iitm:')) {
62
+ if (hasIitm(url)) {
28
63
  return {
29
64
  format: 'module'
30
65
  }
31
66
  }
67
+ if (url === entrypoint) {
68
+ return {
69
+ format: 'commonjs'
70
+ }
71
+ }
32
72
 
33
73
  return parentGetFormat(url, context, parentGetFormat)
34
74
  }
35
75
 
36
76
  const iitmURL = new URL('lib/register.js', import.meta.url).toString()
37
77
  export async function getSource (url, context, parentGetSource) {
38
- if (url.startsWith('iitm:')) {
39
- const realUrl = url.replace('iitm:', '')
78
+ if (hasIitm(url)) {
79
+ const realUrl = deleteIitm(url)
40
80
  const realModule = await import(realUrl)
41
81
  const exportNames = Object.keys(realModule)
42
82
  return {
@@ -62,10 +102,11 @@ register('${realUrl}', namespace, set, '${specifiers.get(realUrl)}')
62
102
 
63
103
  // For Node.js 16.12.0 and higher.
64
104
  export async function load (url, context, parentLoad) {
65
- if (url.startsWith('iitm:')) {
105
+ if (hasIitm(url)) {
66
106
  const { source } = await getSource(url, context)
67
107
  return {
68
108
  source,
109
+ shortCircuit: true,
69
110
  format: 'module'
70
111
  }
71
112
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "import-in-the-middle",
3
- "version": "1.1.2",
3
+ "version": "1.2.2",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "c8 --check-coverage --lines 90 imhotap --runner test/runtest --files test/{hook,low-level}/*.*js",
8
- "coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level}/*.*js && echo '\nNow open coverage/index.html\n'"
7
+ "test": "c8 --check-coverage --lines 90 imhotap --runner test/runtest --files test/{hook,low-level,other}/*",
8
+ "coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level,other}/* && echo '\nNow open coverage/index.html\n'"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
@@ -27,7 +27,7 @@
27
27
  "homepage": "https://github.com/DataDog/import-in-the-middle#readme",
28
28
  "devDependencies": {
29
29
  "c8": "^7.8.0",
30
- "imhotap": "^1.1.0"
30
+ "imhotap": "^2.0.0"
31
31
  },
32
32
  "dependencies": {
33
33
  "module-details-from-path": "^1.0.3"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
3
+ //
4
+ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
5
+
6
+ // This script doesn't actually have to do anything.
@@ -0,0 +1,11 @@
1
+ // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
2
+ //
3
+ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4
+
5
+ import { rejects } from 'assert'
6
+ (async () => {
7
+ await rejects(() => import('./executable'), {
8
+ name: 'TypeError',
9
+ code: 'ERR_UNKNOWN_FILE_EXTENSION'
10
+ })
11
+ })()
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))