Manav Rathi 1 年之前
父節點
當前提交
18deac3a41
共有 1 個文件被更改,包括 24 次插入16 次删除
  1. 24 16
      desktop/src/preload.ts

+ 24 - 16
desktop/src/preload.ts

@@ -75,10 +75,27 @@ export function logError(error: Error, message: string, info?: string): void {
 
 // -
 
-export const convertBrowserStreamToNode = (
-    fileStream: ReadableStream<Uint8Array>,
-) => {
-    const reader = fileStream.getReader();
+/* preload: duplicated writeStream */
+/**
+ * Write a (web) ReadableStream to a file at the given {@link filePath}.
+ *
+ * The returned promise resolves when the write completes.
+ *
+ * @param filePath The local filesystem path where the file should be written.
+ * @param readableStream A [web
+ * ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)
+ */
+const writeStream = (filePath: string, readableStream: ReadableStream) =>
+    writeNodeStream(filePath, convertWebReadableStreamToNode(readableStream));
+
+/**
+ * Convert a Web ReadableStream into a Node.js ReadableStream
+ *
+ * This can be used to, for example, write a ReadableStream obtained via
+ * `net.fetch` into a file using the Node.js `fs` APIs
+ */
+const convertWebReadableStreamToNode = (readableStream: ReadableStream) => {
+    const reader = readableStream.getReader();
     const rs = new Readable();
 
     rs._read = async () => {
@@ -99,10 +116,10 @@ export const convertBrowserStreamToNode = (
     return rs;
 };
 
-export async function writeNodeStream(
+const writeNodeStream = async (
     filePath: string,
     fileStream: NodeJS.ReadableStream,
-) {
+) => {
     const writeable = fs.createWriteStream(filePath);
 
     fileStream.on("error", (error) => {
@@ -120,16 +137,7 @@ export async function writeNodeStream(
             reject(e);
         });
     });
-}
-
-/* preload: duplicated writeStream */
-export async function writeStream(
-    filePath: string,
-    fileStream: ReadableStream<Uint8Array>,
-) {
-    const readable = convertBrowserStreamToNode(fileStream);
-    await writeNodeStream(filePath, readable);
-}
+};
 
 // - Export