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

Function writeFile

lib/internal/fs/promises.js:2073–2112  ·  view source on GitHub ↗
(path, data, options)

Source from the content-addressed store, hash-verified

2071}
2072
2073async function writeFile(path, data, options) {
2074 options = getOptions(options, {
2075 encoding: 'utf8',
2076 mode: 0o666,
2077 flag: 'w',
2078 flush: false,
2079 });
2080 const flush = options.flush ?? false;
2081 validateBoolean(flush, 'options.flush');
2082 parseFileMode(options.mode, 'mode', 0o666);
2083
2084 const h = vfsState.handlers;
2085 if (h !== null) {
2086 checkAborted(options.signal);
2087 const promise = h.writeFile(path, data, options);
2088 if (promise !== undefined) { await promise; return; }
2089 }
2090
2091 const flag = options.flag || 'w';
2092
2093 if (!isArrayBufferView(data) && !isCustomIterable(data)) {
2094 validateStringAfterArrayBufferView(data, 'data');
2095 data = Buffer.from(data, options.encoding || 'utf8');
2096 }
2097
2098 validateAbortSignal(options.signal);
2099 if (path instanceof FileHandle)
2100 return writeFileHandle(path, data, options.signal, options.encoding);
2101
2102 checkAborted(options.signal);
2103
2104 const fd = await open(path, flag, options.mode);
2105 let writeOp = writeFileHandle(fd, data, options.signal, options.encoding);
2106
2107 if (flush) {
2108 writeOp = handleFdSync(writeOp, fd);
2109 }
2110
2111 return handleFdClose(writeOp, fd.close);
2112}
2113
2114function isCustomIterable(obj) {
2115 return isIterable(obj) && !isArrayBufferView(obj) && typeof obj !== 'string';

Calls 11

parseFileModeFunction · 0.85
isCustomIterableFunction · 0.85
validateAbortSignalFunction · 0.85
writeFileHandleFunction · 0.85
handleFdSyncFunction · 0.85
handleFdCloseFunction · 0.85
getOptionsFunction · 0.70
checkAbortedFunction · 0.70
openFunction · 0.70
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…