* 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)
| 2827 | * @returns {void} |
| 2828 | */ |
| 2829 | function 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. |
nothing calls this directly
no test coverage detected
searching dependent graphs…