(path, data, options)
| 2071 | } |
| 2072 | |
| 2073 | async 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 | |
| 2114 | function isCustomIterable(obj) { |
| 2115 | return isIterable(obj) && !isArrayBufferView(obj) && typeof obj !== 'string'; |
no test coverage detected
searching dependent graphs…