Skip to content

Commit

Permalink
chore: add explicit return types to BlobStore methods (#904)
Browse files Browse the repository at this point in the history
This is a types-only change that should have no user impact.
  • Loading branch information
EvanHahn authored Oct 16, 2024
1 parent d59508a commit a7cec42
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/blob-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import util from 'node:util'
import { discoveryKey } from 'hypercore-crypto'
import { TypedEmitter } from 'tiny-typed-emitter'
import { LiveDownload } from './live-download.js'
/** @import { Readable as NodeReadable } from 'node:stream' */
/** @import { Readable as StreamxReadable, Writable } from 'streamx' */
/** @import { BlobId } from '../types.js' */
/** @import { BlobDownloadEvents } from './live-download.js' */

/**
* @internal
* @typedef {NodeReadable | StreamxReadable} Readable
*/

/** @typedef {TypedEmitter<{ 'add-drive': (drive: import('hyperdrive')) => void }>} InternalDriveEmitter */

Expand Down Expand Up @@ -74,12 +82,16 @@ export class BlobStore {
})
}

/**
* @returns {string}
*/
get writerDriveId() {
return getDiscoveryId(this.#writer.key)
}

/**
* @param {string} driveId hex-encoded discovery key
* @returns {Hyperdrive}
*/
#getDrive(driveId) {
const drive = this.#hyperdrives.get(driveId)
Expand All @@ -92,6 +104,7 @@ export class BlobStore {
* @param {object} opts
* @param {false} [opts.wait=false] Set to `true` to wait for a blob to download, otherwise will throw if blob is not available locally
* @param {never} [opts.timeout] Optional timeout to wait for a blob to download
* @returns {Promise<Uint8Array>}
*/
async get({ type, variant, name, driveId }, { wait = false, timeout } = {}) {
const drive = this.#getDrive(driveId)
Expand All @@ -112,7 +125,7 @@ export class BlobStore {
* @param {import('../types.js').BlobFilter} [filter] Filter blob types and/or variants to download. Filter is { [BlobType]: BlobVariants[] }. At least one blob variant must be specified for each blob type.
* @param {object} options
* @param {AbortSignal} [options.signal] Optional AbortSignal to cancel in-progress download
* @returns EventEmitter with `.state` propery, emits `state` with new state when it updates
* @returns {TypedEmitter<BlobDownloadEvents>}
*/
download(filter, { signal } = {}) {
return new LiveDownload(this.#hyperdrives.values(), this.#driveEmitter, {
Expand All @@ -126,6 +139,7 @@ export class BlobStore {
* @param {object} [options]
* @param {boolean} [options.wait=false] Set to `true` to wait for a blob to download, otherwise will throw if blob is not available locally
* @param {number} [options.timeout] Optional timeout to wait for a blob to download
* @returns {Readable}
*/
createReadStream(
{ type, variant, name, driveId },
Expand All @@ -146,6 +160,7 @@ export class BlobStore {
* @param {import('hyperdrive').HyperdriveEntry} entry Hyperdrive entry
* @param {object} [options]
* @param {boolean} [options.wait=false] Set to `true` to wait for a blob to download, otherwise will throw if blob is not available locally
* @returns {Promise<Readable>}
*/
async createEntryReadStream(driveId, entry, options = { wait: false }) {
const drive = this.#getDrive(driveId)
Expand All @@ -165,7 +180,6 @@ export class BlobStore {
* @param {import('hyperdrive').HyperdriveEntry} entry Hyperdrive entry
* @param {object} [opts]
* @param {number} [opts.length]
*
* @returns {Promise<Buffer | null>}
*/
async getEntryBlob(driveId, entry, { length } = {}) {
Expand Down Expand Up @@ -199,6 +213,7 @@ export class BlobStore {
* @param {Omit<BlobId, 'driveId'>} blobId
* @param {object} [options]
* @param {{mimeType: string}} [options.metadata] Metadata to store with the blob
* @returns {Writable & { driveId: string }}
*/
createWriteStream({ type, variant, name }, options) {
const path = makePath({ type, variant, name })
Expand Down

0 comments on commit a7cec42

Please sign in to comment.