From 750003440b6a05a930eeb9bce7b758846b6fcf13 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Wed, 8 Jan 2025 23:27:33 -0800 Subject: [PATCH] Accept PathLike input for createCSVWritableStream To be consistent with createCSVReadableStream and also allow paths relative to JS file (vs. execution directory only). --- src/core.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/core.js b/src/core.js index 1fad6a2..48f327f 100644 --- a/src/core.js +++ b/src/core.js @@ -168,12 +168,21 @@ export function createCSVTransformStream(fn, options = {}) { * A simple wrapper around Node.js's [`fs.writeFile`](https://nodejs.org/api/fs.html#fspromiseswritefilefile-data-options) to write * streamed data to a file. If the input data is JSON, the data is converted to a string representing a RFC 4180 CSV record. If the * data is not JSON (e.g., if it is already converted to a CSV string), the data is written to the CSV file directly. - * @param {string} path A `string` representing a path to a local CSV file destination. If the file already exists, its **data - * will be overwritten**. + * @param {PathLike} path A `string`, `Buffer`, or `URL` representing a path to a local CSV file destination. If the file already + * exists, its **data will be overwritten**. * @returns {WritableStream} A `WritableStream` to write data to a file on disk. */ export function createCSVWritableStream(path) { - const fullPath = normalize(path); + let fullPath; + if (typeof path === 'string') { + fullPath = normalize(path); + } + else if (ArrayBuffer.isView(path) || path instanceof ArrayBuffer) { + fullPath = normalize(new TextDecoder().decode(path)); + } + else { // Assuming URL + fullPath = normalize(path.pathname.substring(1)); + } const dir = dirname(fullPath); let handle; -- 2.47.3