Reference / Blobs API for Bun and Node
Blobs API for Bun and Node
Every public class, function and type of the Blobs engine in @tinyshed/tinystore, generated from its source. The Blobs guide explains how to use them, and the Python, Go and Rust pages list the same API.
Size
type Size = number | `${number}${'KiB' | 'MiB' | 'GiB' | 'TiB'}`A size in bytes: a number, or text in binary units, 64KiB or 2GiB; '2GB' does not compile.
FilesOptions
interface FilesOptions {
/** the term of every file written without its own, from its write; not a ceiling */
ttl?: Duration | undefined
/** the term of every file written without its own, from its last get or head; not beside ttl */
idle?: Duration | undefined
/** the largest file a write may leave: past it, a write is refused and leaves nothing */
maxFileSize?: Size | undefined
/**
* the most the set's files hold together, kept in the set: a write past it
* removes the files used least recently, and an open with another is ConflictError
*/
maxSize?: Size | undefined
/** what a write past maxSize does: 'evict', the default, or 'refuse', a LimitError that removes nothing */
whenFull?: WhenFull | undefined
}WhenFull
type WhenFull = 'evict' | 'refuse'What a write past a set's maxSize does.
FilesUsage
interface FilesUsage {
count: number
size: number
/** undefined when the set keeps no ceiling */
maxSize: number | undefined
/** the files the ceiling removed since the store opened */
evictions: number
}What a folder holds now, and the set's ceiling and what it evicted since the store opened.
Trim
interface Trim {
evictions: number
freed: number
}What a trim removed: its files, and their sizes as usage counts them.
FileInfo
interface FileInfo {
/** its path within the folder it was asked from */
path: string
size: number
/** the SHA-256 of its bytes in hex, quoted, as an HTTP ETag is */
etag: string
contentType: string
lastModified: Date
/** undefined for a file that does not expire */
expires: Date | undefined
meta: Record<string, string>
}What a file carries: what serving it needs.
FileOptions
interface FileOptions {
contentType?: string | undefined
meta?: Record<string, string> | undefined
/** the file's own term, from this write */
ttl?: Duration | undefined
/** the ETag of the one file this write may replace: another is ConflictError */
ifMatch?: string | undefined
/** the body's length: one longer or shorter is refused and leaves nothing */
size?: number | undefined
signal?: AbortSignal | undefined
}Body
type Body = string | Uint8Array | ArrayBuffer | Blob | ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>What a write takes: text, bytes, a Blob or a file, or a stream of bytes.
Fill
interface Fill {
data?: Body | undefined
path?: string | undefined
contentType?: string | undefined
meta?: Record<string, string> | undefined
}What a getOrCreate's function gives: the file's bytes as data, or the path of a file of this machine, streamed as it is written; its type and meta win over the call's.
GetOrCreateOptions
interface GetOrCreateOptions {
/** the file's type when the function gives none */
contentType?: string | undefined
/** the file's meta when the function gives none */
meta?: Record<string, string> | undefined
/** the file's own term, from its write */
ttl?: Duration | undefined
/** how long the claim of the path is renewed while the function runs: 10 minutes when absent */
timeout?: Duration | undefined
/** stops this call's wait; a run it waits for goes on */
signal?: AbortSignal | undefined
}FileListOptions
interface FileListOptions {
/** text within the folder: 'photos/1' finds photos/10.jpg too */
prefix?: string | undefined
/** a page's next */
after?: string | undefined
/** 1000 at most, and when absent */
limit?: number | undefined
}FilePage
interface FilePage {
files: FileInfo[]
/** undefined on the last page */
next: string | undefined
}A page of a folder's files in the byte order of their paths, and where the next starts.
Files
class Files {
readonly name: string
}A named set of files by path, or a folder of it, whose calls are the folder's: a path in it is the folder's segments and then its own.
Files.folder
folder(...segments: (string | number | bigint)[]): FilesThe folder of these segments, one each: folder('users', 42) is users/42, never users/420.
Files.put
put(path: string, body: Body, options?: FileOptions): Promise<FileInfo>Writes the file whole and replaces whatever the path held, returning once it is on disk as the store's durability says. Nobody sees half of it, while it is written or after a crash.
Files.create
create(path: string, body: Body, options?: Omit<FileOptions, 'ifMatch'>): Promise<boolean>Writes the file only where the path holds none, and says whether it did.
Files.get
get(path: string): Promise<StoredFile | undefined>What the file carries, and its bytes: a file up to about a megabyte comes with them, and a larger one's are read when they are read, on its ETag, as S3's GET with If-Match reads them. Undefined when the path holds none.
Files.head
head(path: string): Promise<FileInfo | undefined>What the file carries and none of its bytes; undefined when the path holds none.
Files.delete
delete(path: string, options?: {
ifMatch?: string | undefined
}): Promise<void>Deletes the file, or with ifMatch only that version of it; no file there is not an error.
Files.copy
copy(from: string, to: string, options?: {
ifMatch?: string | undefined
}): Promise<FileInfo>A copy at to that shares the bytes, which are not copied. A file at
to is ConflictError unless ifMatch names the version to replace.
Files.rename
rename(from: string, to: string, options?: {
ifMatch?: string | undefined
}): Promise<FileInfo>The file moved to to in one write: a file there is ConflictError
unless ifMatch names the version to replace, and none at from NotFoundError.
Files.expire
expire(path: string, after: Duration | Date): Promise<boolean>Sets when the file expires, a span from now or a moment; says whether there was a file.
Files.list
list(options?: FileListOptions): Promise<FilePage>A page of the files in the folder and in the folders in it, in the byte order of their paths.
Files.all
all(options?: {
prefix?: string | undefined
}): AsyncGenerator<FileInfo>Every file of the folder, a page at a time, holding nothing between pages.
Files.usage
usage(): Promise<FilesUsage>The files in the folder and their bytes, as it is now, with the set's ceiling and what it evicted since the store opened. It renews nothing.
Files.setMaxSize
setMaxSize(size: Size | 'none', options?: {
ifCurrent?: Size | undefined
whenFull?: WhenFull | undefined
}): Promise<void>Changes the set's ceiling for every handle, 'none' removing it; a lower one evicts at once. With ifCurrent it changes only while the set keeps that ceiling, and another is ConflictError; one the set keeps already writes nothing.
Files.trim
trim(options: {
to: Size
}): Promise<Trim>Removes files in the eviction's order until the set holds at most to,
and leaves its ceiling where it was; a set whose ceiling refuses is InvalidError.
Files.clear
clear(): Promise<void>Removes the folder's every file and every folder in it, at once for every reader.
Files.upload
upload(path: string, options?: FileOptions): Promise<Upload>Begins a file written a piece at a time, which appears at commit and
never before: await using upload = await files.upload(path), and
leaving the block without commit aborts it.
Files.getOrCreate
getOrCreate(path: string, fn: () => Fill | Promise<Fill>, options?: GetOrCreateOptions): Promise<StoredFile>The file at the path, or, on a miss, the one fn's Fill makes. While
one call runs fn, every other getOrCreate of the path, in this process
or another, waits for it and gets the file it wrote. The call claims
the path and the store renews the claim while fn runs, up to
timeout; fn's file lands only while the claim still holds the path
and no file is there. A put of the path meanwhile wins, and the calls
get the put's file; a delete ends the claim, fn's file is dropped and
one of the calls runs fn again, so fn may run more than once, as it
may after a lost connection. A throw of fn is this call's own error
and the ConflictError of every call that waited for it, with its
message, or the store's own error as it is; the next call runs fn
again.
const page = await pages.getOrCreate(`${hash}.pdf`, async () => ({ data: await convert(doc), contentType: 'application/pdf' }))Upload
class Upload implements AsyncDisposable {}A file written a piece at a time, as files.upload begins it: bytes go in
with write, the file appears at commit and never before, and an upload
that ends otherwise leaves nothing.
Upload.write
write(bytes: Uint8Array | string): Promise<void>Takes bytes, text as UTF-8, waiting while the server catches up.
Upload.commit
commit(): Promise<FileInfo>Publishes the file whole, replacing what the path held as put does,
once it is on disk as the store's durability says.
Upload.abort
abort(): Promise<void>Ends the upload, which leaves nothing; after commit it does nothing.
Upload.[Symbol.asyncDispose]
[Symbol.asyncDispose](): Promise<void>FileBytes
class FileBytes {
readonly size: number
}Bytes of a file, read when they are read, as a Blob's are: stream(), or
all of them with bytes(), text(), json() or arrayBuffer(), which
hold them in memory. Each read is a read of its own.
FileBytes.stream
stream(): ReadableStream<Uint8Array>Its bytes as they come, the credit of each given back once it is taken.
FileBytes.bytes
bytes(): Promise<Uint8Array<ArrayBuffer>>FileBytes.arrayBuffer
arrayBuffer(): Promise<ArrayBuffer>FileBytes.text
text(): Promise<string>FileBytes.json
json(): Promise<unknown>StoredFile
class StoredFile extends FileBytes implements FileInfo {
readonly path: string
readonly etag: string
readonly contentType: string
readonly lastModified: Date
readonly expires: Date | undefined
readonly meta: Record<string, string>
}A file as get found it: what it carries, and its bytes. A whole read is
checked against its SHA-256 and fails CorruptError before its last bytes
when they changed; slice reads a range, which is not checked.
StoredFile.slice
slice(start?: number, end?: number): FileBytesA range of its bytes, as Blob.slice takes one: an end left out is the
file's, and a negative place counts back from it, so slice(-500) is the
last 500 bytes.