Skip to content

Commit

Permalink
pkg/lib: change API of upload helper to Blob
Browse files Browse the repository at this point in the history
Instead of taking a ReadableStream, take a Blob.

The reason for this is to gain access to the `.size` attribute on the
Blob, allowing us to send a `size` hint on the `fsreplace1` channel.

I considered giving the possibility of accepting either, but this API is
totally new (only appearing in the last release), there are no known
existing users, and is bundled, anyway.
  • Loading branch information
allisonkarlitskaya committed Apr 29, 2024
1 parent c095d17 commit 63cd368
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pkg/lib/cockpit-upload-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Waiter {

export async function upload(
destination: string,
stream: ReadableStream,
contents: Blob,
progress?: (bytes_sent: number) => void,
signal?: AbortSignal,
options?: cockpit.JsonObject
Expand All @@ -65,6 +65,7 @@ export async function upload(
payload: 'fsreplace1',
path: destination,
binary: true,
size: contents.size,
'send-acks': 'bytes',
...options,
} as const;
Expand All @@ -91,8 +92,8 @@ export async function upload(
});

try {
debug('starting file send', stream);
const reader = stream.getReader({ mode: 'byob' }); // byob to choose the block size
debug('starting file send', contents);
const reader = contents.stream().getReader({ mode: 'byob' }); // byob to choose the block size
let eof = false;

// eslint-disable-next-line no-unmodified-loop-condition
Expand Down
2 changes: 1 addition & 1 deletion pkg/playground/react-demo-file-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const UploadButton = () => {
});

try {
await upload(destination, file.stream(), (progress) => {
await upload(destination, file, (progress) => {
const now = performance.now();
if (now < next_progress)
return;
Expand Down

0 comments on commit 63cd368

Please sign in to comment.