-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix: Fixed issue with CJS being imported as ESM #2168
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ca6060
fix: Fixed issue with CJS being imported as ESM
jsumners-nr 0f12ddc
run esm integration tests correctly
jsumners-nr e33c410
fix lint
jsumners-nr 1209698
complete test
jsumners-nr 161cc0e
better? impl
jsumners-nr e3a4227
only de-proxy in wrapExport
jsumners-nr b3e477c
add unit test
jsumners-nr b6c0484
address feedback
jsumners-nr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => { | ||
bizob2828 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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] | ||
bizob2828 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should you just import this and then access all 3 different exports and verify it works as expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I understand the question. I have to import it after the "agent" is setup because otherwise the
registerHooks
will not pick up the module.node-newrelic/test/lib/agent_helper.js
Lines 180 to 190 in cb21d2c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh so i meant importing the file to get at
default
,foo
and just the raw module.exports. Looking at this right now you're just getting default correct?