Skip to content

Commit ffdf6c8

Browse files
committed
fix: esm default export patch
1 parent a99ba69 commit ffdf6c8

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

plugins/node/instrumentation-dataloader/src/instrumentation.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ type DataloaderInternal = typeof Dataloader.prototype & {
4444
type LoadFn = (typeof Dataloader.prototype)['load'];
4545
type LoadManyFn = (typeof Dataloader.prototype)['loadMany'];
4646

47+
function extractModuleExports(module: any) {
48+
return module[Symbol.toStringTag] === 'Module'
49+
? module.default // ESM
50+
: module; // CommonJS
51+
}
52+
4753
export class DataloaderInstrumentation extends InstrumentationBase<DataloaderInstrumentationConfig> {
4854
constructor(config: DataloaderInstrumentationConfig = {}) {
4955
super(PACKAGE_NAME, PACKAGE_VERSION, config);
@@ -55,18 +61,20 @@ export class DataloaderInstrumentation extends InstrumentationBase<DataloaderIns
5561
MODULE_NAME,
5662
['>=2.0.0 <3'],
5763
dataloader => {
58-
this._patchLoad(dataloader.prototype);
59-
this._patchLoadMany(dataloader.prototype);
64+
const dataloaderExports = extractModuleExports(dataloader);
65+
this._patchLoad(dataloaderExports.prototype);
66+
this._patchLoadMany(dataloaderExports.prototype);
6067

61-
return this._getPatchedConstructor(dataloader);
68+
return this._getPatchedConstructor(dataloaderExports);
6269
},
6370
dataloader => {
64-
if (isWrapped(dataloader.prototype.load)) {
65-
this._unwrap(dataloader.prototype, 'load');
71+
const dataloaderExports = extractModuleExports(dataloader);
72+
if (isWrapped(dataloaderExports.prototype.load)) {
73+
this._unwrap(dataloaderExports.prototype, 'load');
6674
}
6775

68-
if (isWrapped(dataloader.prototype.loadMany)) {
69-
this._unwrap(dataloader.prototype, 'loadMany');
76+
if (isWrapped(dataloaderExports.prototype.loadMany)) {
77+
this._unwrap(dataloaderExports.prototype, 'loadMany');
7078
}
7179
}
7280
) as InstrumentationNodeModuleDefinition,

0 commit comments

Comments
 (0)