diff options
Diffstat (limited to 'runtime/js/40_write_file.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 = { |