-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixed issue with CJS being imported as ESM (#2168)
- Loading branch information
1 parent
cb21d2c
commit 9a14cb0
Showing
14 changed files
with
394 additions
and
848 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
/** | ||
* Determines if a given string represents an absolute path to a module. | ||
* | ||
* @param {string} target Path to a module. | ||
* | ||
* @returns {boolean} True if it is an absolute path. | ||
*/ | ||
module.exports = function isAbsolutePath(target) { | ||
const leadChar = target.slice(0, 1) | ||
if (leadChar !== '.' && leadChar !== '/') { | ||
return false | ||
} | ||
|
||
const suffix = target.slice(-4) | ||
/* eslint-disable-next-line */ | ||
if (suffix.slice(-3) !== '.js' && suffix !== '.cjs' && suffix !== '.mjs') { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2020 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
// A fake module that will be imported as ESM. Basically, the issue is that | ||
// CJS imported as ESM needs its exports proxied and our `shim.wrapExport` | ||
// needs to recognize the "original" export in order to pass it in to the | ||
// instrumentation. | ||
|
||
function foo() { | ||
return Object.create({ | ||
name() { | ||
return 'foo' | ||
} | ||
}) | ||
} | ||
|
||
// This triplet export replicates they way Fastify solves the CJS utilized in | ||
// ESM issue. It makes it possible to `import foo from './foo.cjs'` or to | ||
// `import { foo } from './foo.cjs'`. It also allows us to replicate the | ||
// issue at hand. | ||
module.exports = foo | ||
module.exports.default = foo | ||
module.exports.foo = foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import tap from 'tap' | ||
import crypto from 'crypto' | ||
import path from 'path' | ||
import url from 'url' | ||
import helper from '../../lib/agent_helper.js' | ||
import shimmer from '../../../lib/shimmer.js' | ||
import InstrumentationDescriptor from '../../../lib/instrumentation-descriptor.js' | ||
|
||
let modPath | ||
if (import.meta.dirname) { | ||
modPath = path.join(import.meta.dirname, 'foo.cjs') | ||
} else { | ||
modPath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), 'foo.cjs') | ||
} | ||
|
||
function instrumentation(shim, resolvedModule) { | ||
shim.wrapExport(resolvedModule, function wrapModule(shim, fn) { | ||
return function wrappedModule() { | ||
// `fn` _should_ be the `foo()` function exported by the module. | ||
// If it is anything else, i.e. the proxy object, then we have an error | ||
// in our handling of CJS modules as ESM. | ||
const foo = fn.apply(this, arguments) | ||
const _name = foo.name | ||
foo.name = () => { | ||
const value = _name.call(foo) | ||
return `wrapped: ${value}` | ||
} | ||
return foo | ||
} | ||
}) | ||
} | ||
|
||
tap.beforeEach(async (t) => { | ||
shimmer.registerInstrumentation({ | ||
type: InstrumentationDescriptor.TYPE_GENERIC, | ||
moduleName: 'foo', | ||
absolutePath: modPath, | ||
onRequire: instrumentation | ||
}) | ||
|
||
const agent = helper.instrumentMockedAgent() | ||
t.context.agent = agent | ||
|
||
const { default: foo } = await import('./foo.cjs?v=' + crypto.randomBytes(16).toString('hex')) | ||
t.context.mod = foo | ||
}) | ||
|
||
tap.afterEach((t) => { | ||
helper.unloadAgent(t.context.agent) | ||
}) | ||
|
||
tap.test('CJS imported as ESM gets wrapped correctly', async (t) => { | ||
const { mod } = t.context | ||
const instance = mod() | ||
t.equal(instance.name(), 'wrapped: foo') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const tap = require('tap') | ||
const isAbsolutePath = require('../../lib/util/is-absolute-path') | ||
|
||
tap.test('verifies paths correctly', async (t) => { | ||
const tests = [ | ||
['./foo/bar.js', true], | ||
['/foo/bar.cjs', true], | ||
['/foo.mjs', true], | ||
['/foo.smj', false], | ||
['foo', false], | ||
['foo.js', false] | ||
] | ||
|
||
for (const [input, expected] of tests) { | ||
t.equal(isAbsolutePath(input), expected) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.