chunk_upload.ts

Uploads a file as a sequence of chunks through the backend's byte routes, one at a time and in order, as they're produced.

view source

Declarations
#

3 declarations

CHUNK_UPLOAD_RETRY_DELAYS
#

chunk_upload.ts view source

readonly number[] import {CHUNK_UPLOAD_RETRY_DELAYS} from '@fuzdev/zzz/chunk_upload.js';

Waits between attempts of one chunk; a chunk fails once these run out.

ChunkUpload
#

chunk_upload.ts view source

import {ChunkUpload} from '@fuzdev/zzz/chunk_upload.js';

One file's upload. create makes the file, enqueue appends each chunk after the ones before it, and flush waits for everything queued.

Each chunk is appended at the offset the upload expects, so one can never land twice or out of order. A request that gets no answer or a server error is retried; when the retry finds the file already grew by exactly this chunk, the earlier attempt landed and the chunk is done. Any other refusal — the file changed underneath, the session ended — fails the upload.

path

type string

readonly

uploaded_bytes

The file's size as the backend last confirmed it.

type number

error

Why the upload failed, once it has.

type string | null

constructor

type new (options: ChunkUploadOptions): ChunkUpload

options

create

Creates the file, empty. Call it once, before any enqueue.

type (): Promise<void>

returns Promise<void>

throws

  • Error - when the file can't be created — `already exists` for a taken path

enqueue

Queues a chunk to append after every chunk queued before it. Chunks queued after the upload failed are dropped.

type (chunk: Blob): void

chunk

type Blob
returns void

flush

Waits for every queued chunk.

type (): Promise<void>

returns Promise<void>

throws

  • Error - with the failure when the upload failed

ChunkUploadOptions
#

chunk_upload.ts view source

ChunkUploadOptions import type {ChunkUploadOptions} from '@fuzdev/zzz/chunk_upload.js';

fetch

type (input: string, init?: RequestInit | undefined): Promise<Response>

input

type string

init?

type RequestInit
optional
returns Promise<Response>

api_url

The API's URL or path, with no trailing slash.

type string

path

The absolute path of the file to create and append to.

type string

on_progress?

Called with the file's size each time a chunk lands.

type (uploaded_bytes: number): void

uploaded_bytes

type number
returns void

on_error?

Called once, when a chunk fails for good. Nothing is uploaded after.

type (message: string): void

message

type string
returns void

retry_delays?

Waits between attempts of one chunk (default CHUNK_UPLOAD_RETRY_DELAYS).

type readonly number[]

sleep?

Waits ms (default wait).

type (ms: number): Promise<unknown>

ms

type number
returns Promise<unknown>

Depends on
#

Imported by
#