diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-01-12 03:37:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-12 03:37:23 +0100 |
commit | a6b3910bdfe0183e458015d00a61295779e46eb1 (patch) | |
tree | 59d206d789c99a2f8f7e1bedf95914811f5a070c /runtime/js | |
parent | 692f9af14a6742cda7e24224eda6240e33d641d4 (diff) |
feat: allow passing a ReadableStream to Deno.writeFile/Deno.writeTextFile (#17329)
Closes #13229
Diffstat (limited to 'runtime/js')
-rw-r--r-- | runtime/js/40_write_file.js | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/runtime/js/40_write_file.js b/runtime/js/40_write_file.js index f8d8b3ab7..a32ef441b 100644 --- a/runtime/js/40_write_file.js +++ b/runtime/js/40_write_file.js @@ -5,6 +5,9 @@ const ops = core.ops; const { abortSignal } = window.__bootstrap; const { pathFromURL } = window.__bootstrap.util; + const { open } = window.__bootstrap.files; + const { ReadableStreamPrototype } = window.__bootstrap.streams; + const { ObjectPrototypeIsPrototypeOf } = window.__bootstrap.primordials; function writeFileSync( path, @@ -36,16 +39,29 @@ options.signal[abortSignal.add](abortHandler); } try { - await core.opAsync( - "op_write_file_async", - pathFromURL(path), - options.mode, - options.append ?? false, - options.create ?? true, - options.createNew ?? false, - data, - cancelRid, - ); + if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) { + const file = await open(path, { + mode: options.mode, + append: options.append ?? false, + create: options.create ?? true, + createNew: options.createNew ?? false, + write: true, + }); + await data.pipeTo(file.writable, { + signal: options.signal, + }); + } else { + await core.opAsync( + "op_write_file_async", + pathFromURL(path), + options.mode, + options.append ?? false, + options.create ?? true, + options.createNew ?? false, + data, + cancelRid, + ); + } } finally { if (options.signal) { options.signal[abortSignal.remove](abortHandler); @@ -70,8 +86,16 @@ data, options = {}, ) { - const encoder = new TextEncoder(); - return writeFile(path, encoder.encode(data), options); + if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) { + return writeFile( + path, + data.pipeThrough(new TextEncoderStream()), + options, + ); + } else { + const encoder = new TextEncoder(); + return writeFile(path, encoder.encode(data), options); + } } window.__bootstrap.writeFile = { |