MCPcopy Index your code
hub / github.com/nodejs/node / writeFile

Function writeFile

lib/fs.js:2829–2875  ·  view source on GitHub ↗

* Asynchronously writes data to the file. * @param {string | Buffer | URL | number} path * @param {string | Buffer | TypedArray | DataView} data * @param {{ * encoding?: string | null; * mode?: number; * flag?: string; * signal?: AbortSignal; * flush?: boolean; * } | string} [op

(path, data, options, callback)

Source from the content-addressed store, hash-verified

2827 * @returns {void}
2828 */
2829function writeFile(path, data, options, callback) {
2830 callback ||= options;
2831 validateFunction(callback, 'cb');
2832
2833 options = getOptions(typeof options === 'function' ? null : options, {
2834 encoding: 'utf8',
2835 mode: 0o666,
2836 flag: 'w',
2837 flush: false,
2838 });
2839 const flush = options.flush ?? false;
2840 validateBoolean(flush, 'options.flush');
2841 parseFileMode(options.mode, 'mode', 0o666);
2842
2843 const h = vfsState.handlers;
2844 if (h !== null) {
2845 if (checkAborted(options.signal, callback)) return;
2846 if (vfsVoid(h.writeFile(path, data, options), callback)) return;
2847 }
2848
2849 const flag = options.flag || 'w';
2850
2851 if (!isArrayBufferView(data)) {
2852 validateStringAfterArrayBufferView(data, 'data');
2853 data = Buffer.from(data, options.encoding || 'utf8');
2854 }
2855
2856 if (isFd(path)) {
2857 const isUserFd = true;
2858 const signal = options.signal;
2859 writeAll(path, isUserFd, data, 0, data.byteLength, signal, flush, callback);
2860 return;
2861 }
2862
2863 if (checkAborted(options.signal, callback))
2864 return;
2865
2866 fs.open(path, flag, options.mode, (openErr, fd) => {
2867 if (openErr) {
2868 callback(openErr);
2869 } else {
2870 const isUserFd = false;
2871 const signal = options.signal;
2872 writeAll(fd, isUserFd, data, 0, data.byteLength, signal, flush, callback);
2873 }
2874 });
2875}
2876
2877/**
2878 * Synchronously writes data to the file.

Callers

nothing calls this directly

Calls 9

parseFileModeFunction · 0.85
vfsVoidFunction · 0.85
checkAbortedFunction · 0.70
writeAllFunction · 0.70
openMethod · 0.65
getOptionsFunction · 0.50
callbackFunction · 0.50
writeFileMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…