Last active
March 2, 2025 11:17
-
-
Save AimWhy/b4b3f13d0f3fea77af7bae5b0a1227e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface BlobWritable { | |
| getWriter: () => { | |
| abort: () => Promise<void>; | |
| close: () => Promise<void>; | |
| closed: Promise<undefined>; | |
| desiredSize: number | null; | |
| ready: Promise<undefined>; | |
| releaseLock: () => void; | |
| write: (chunk: Uint8Array) => Promise<void>; | |
| }; | |
| locked: boolean; | |
| abort: () => Promise<void>; | |
| close: () => Promise<void>; | |
| } | |
| function getBlobWritable(filename: string, onClose: (result: Blob) => void): BlobWritable { | |
| let blobParts: Uint8Array[] = []; | |
| return { | |
| getWriter: () => { | |
| return { | |
| abort: async () => { | |
| blobParts = []; | |
| }, | |
| close: async () => { | |
| onClose(new File(blobParts, filename)); | |
| }, | |
| closed: Promise.resolve(undefined), | |
| desiredSize: 3 * 1024 * 1024, | |
| ready: Promise.resolve(undefined), | |
| releaseLock: () => { | |
| // no op | |
| }, | |
| write: async (chunk) => { | |
| blobParts.push(chunk); | |
| }, | |
| }; | |
| }, | |
| locked: false, | |
| abort: async () => { | |
| blobParts = []; | |
| }, | |
| close: async () => { | |
| onClose(new File(blobParts, filename)); | |
| }, | |
| }; | |
| } | |
| async function pipe(readable: ReadableStream, writable: BlobWritable): Promise<void> { | |
| const reader = readable.getReader(); | |
| const writer = writable.getWriter(); | |
| let done = false; | |
| while (!done) { | |
| const status = await reader.read(); | |
| if (!status.done) { | |
| await writer.write(status.value); | |
| } | |
| done = status.done; | |
| } | |
| await reader.closed; | |
| await writer.close(); | |
| } | |
| // (source.pipeTo && source.pipeTo(destination, { signal: abortController?.signal })) || pipe(source, destination) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment