Skip to content

Commit

Permalink
switch to CJS for index, and remove enabled
Browse files Browse the repository at this point in the history
CJS allows us to add hooks from CJS files synchronously.

"enabled" is just too hard to properly track when accounting for CJS.
  • Loading branch information
bengl committed Aug 18, 2021
1 parent 9cf7948 commit f2b732a
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 35 deletions.
2 changes: 1 addition & 1 deletion hook.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getFormat (url, context, parentGetFormat) {
return parentGetFormat(url, context, parentGetFormat)
}

const iitmURL = new URL('index.mjs', import.meta.url).toString()
const iitmURL = new URL('index.js', import.meta.url).toString()
export async function getSource (url, context, parentGetSource) {
if (url.startsWith('iitm:')) {
const realName = url.replace('iitm:', '')
Expand Down
13 changes: 3 additions & 10 deletions index.mjs → index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,19 @@ const proxyHandler = {
}
}

let isEnabled = false

export function enabled() {
return isEnabled
}

export function _register(name, namespace, set) {
isEnabled = true
exports._register = function _register(name, namespace, set) {
setters.set(namespace, set)
const proxy = new Proxy(namespace, proxyHandler)
importHooks.forEach(hook => hook(name, proxy))
toHook.push([name, proxy])
}

export function addHook(hook) {
exports.addHook = function addHook(hook) {
importHooks.push(hook)
toHook.forEach(([name, namespace]) => hook(name, namespace))
}

export function removeHook(hook) {
exports.removeHook = function removeHook(hook) {
const index = importHooks.indexOf(hook)
if (index > -1) {
importHooks.splice(index, 1)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Intercept imports in Node.js",
"main": "index.mjs",
"scripts": {
"test": "c8 --check-coverage --lines 100 imhotap --runner test/runtest --files test/*.mjs"
"test": "c8 --check-coverage --lines 100 imhotap --runner test/runtest --files test/*.*js"
},
"repository": {
"type": "git",
Expand Down
23 changes: 23 additions & 0 deletions test/dynamic-import-default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

const { addHook } = require('../index.js')
const { strictEqual } = require('assert')

addHook((name, exports) => {
if (name.match(/something\.m?js/)) {
const orig = exports.default
exports.default = function bar() {
return orig() + 15
}
}
})

;(async () => {
const { default: barMjs } = await import('./fixtures/something.mjs')
const { default: barJs } = await import('./fixtures/something.js')

strictEqual(barMjs(), 57)
strictEqual(barJs(), 57)
})()
6 changes: 2 additions & 4 deletions test/dynamic-import-default.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import { addHook, enabled } from '../index.mjs'
import { strictEqual, ok } from 'assert'

ok(enabled())
import { addHook } from '../index.js'
import { strictEqual } from 'assert'

addHook((name, exports) => {
if (name.match(/something\.m?js/)) {
Expand Down
25 changes: 25 additions & 0 deletions test/dynamic-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

const { addHook } = require('../index.js')
const { strictEqual } = require('assert')

addHook((name, exports) => {
if (name.match(/something\.m?js/)) {
exports.foo += 15
}
if (name.match('os')) {
exports.freemem = () => 47
}
})

;(async () => {
const { foo: fooMjs } = await import('./fixtures/something.mjs')
const { foo: fooJs } = await import('./fixtures/something.js')
const { freemem } = await import('os')

strictEqual(fooMjs, 57)
strictEqual(fooJs, 57)
strictEqual(freemem(), 47)
})()
6 changes: 2 additions & 4 deletions test/dynamic-import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import { addHook, enabled } from '../index.mjs'
import { strictEqual, ok } from 'assert'

ok(enabled())
import { addHook } from '../index.js'
import { strictEqual } from 'assert'

addHook((name, exports) => {
if (name.match(/something\.m?js/)) {
Expand Down
4 changes: 1 addition & 3 deletions test/remove.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import { addHook, removeHook, enabled } from '../index.mjs'
import { addHook, removeHook } from '../index.js'
import { strictEqual, ok } from 'assert'

ok(enabled())

const hook = (name, exports) => {
if (name.match(/something\.m?js/)) {
exports.foo += 15
Expand Down
6 changes: 2 additions & 4 deletions test/static-import-default.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import { addHook, enabled } from '../index.mjs'
import { addHook } from '../index.js'
import barMjs from './fixtures/something.mjs'
import barJs from './fixtures/something.js'
import { strictEqual, ok } from 'assert'

ok(enabled())
import { strictEqual } from 'assert'

addHook((name, exports) => {
if (name.match(/something\.m?js/)) {
Expand Down
6 changes: 2 additions & 4 deletions test/static-import-disabled.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import { addHook, enabled } from '../index.mjs'
import { addHook } from '../index.js'
import { foo as fooMjs } from './fixtures/something.mjs'
import { foo as fooJs } from './fixtures/something.js'
import { strictEqual, fail, ok} from 'assert'

ok(!enabled())
import { strictEqual, fail } from 'assert'

addHook(() => {
fail('should not have been called at all')
Expand Down
6 changes: 2 additions & 4 deletions test/static-import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import { addHook, enabled } from '../index.mjs'
import { addHook } from '../index.js'
import { foo as fooMjs } from './fixtures/something.mjs'
import { foo as fooJs } from './fixtures/something.js'
import { freemem } from 'os'
import { strictEqual, ok } from 'assert'

ok(enabled())
import { strictEqual } from 'assert'

addHook((name, exports) => {
if (name.match(/something\.m?js/)) {
Expand Down

0 comments on commit f2b732a

Please sign in to comment.