Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/js/internal/fs/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,12 @@ interface FsBinding
writev(fd: number, buffers: readonly NodeJS.ArrayBufferView[], position?: number | null): Promise<number>;
}

export default $rust("node_fs_binding.rs", "createBinding") as FsBinding;
// `xCb(callback, ...args)`: the promise arm's arguments after the callback. `fs.cp` is a JS chain, so no `cpCb`.
type FsCallbackArm<T> = {
[K in Exclude<keyof T & string, "cp"> as T[K] extends (...args: any[]) => Promise<any> ? `${K}Cb` : never]: (
callback: (err: any, value?: any) => void,
...args: any[]
) => void;
};

export default $rust("node_fs_binding.rs", "createBinding") as FsBinding & FsCallbackArm<FsBinding>;
23 changes: 23 additions & 0 deletions src/js/internal/trace_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ function installFsInstrumentation() {
for (const method in asyncOps) {
const original = binding[method];
if (typeof original === "function") binding[method] = wrapFsAsyncMethod(original, asyncOps[method]);
const originalCb = binding[method + "Cb"];
if (typeof originalCb === "function") binding[method + "Cb"] = wrapFsCallbackMethod(originalCb, asyncOps[method]);
}
// fs.opendir never reaches the binding (Bun's Dir reads lazily), so wrap the
// node:fs export for the node.fs_dir.async category.
Expand Down Expand Up @@ -690,6 +692,27 @@ function wrapFsAsyncMethod(original, names: string[]) {
};
}

// The callback arm of the binding (`xCb(callback, ...args)`): the span ends when the operation calls back.
function wrapFsCallbackMethod(original, names: string[]) {
return function (callback, ...args) {
if (suppressFsEvents || !isCategoryGroupEnabled(kFsAsyncCat)) return original.$apply(this, arguments);
for (let i = 0; i < names.length; i++) emitEvent("b", kFsAsyncCat, names[i]);
try {
return original.$call(
this,
function () {
emitFsAsyncEnd(names);
return callback.$apply(undefined, arguments);
},
...args,
);
} catch (err) {
emitFsAsyncEnd(names);
throw err;
}
};
}

// The global console is native (not the JS Console class), so wrap its
// counter/timer methods directly. Counts and timer labels are tracked in
// parallel maps because the native implementations don't expose theirs;
Expand Down
Loading
Loading