Interface Storage<Context>

Interface for the storage required by RangeFinder.

DefaultStorage for a default implementation.

interface Storage<Context> {
    createStream(context: Context): Readable;
    getCacheKey(context: Context): unknown;
    put(entry: Readonly<{
        offset: number;
        stream: Readable;
    }>, context: Context): void;
    remove(entry: Readonly<{
        offset: number;
        stream: Readable;
    }>, context: Context): void;
    take(startOffset: number, context: Context): undefined | Readonly<{
        offset: number;
        stream: Readable;
    }>;
}

Type Parameters

  • Context = void

Implemented by

Methods

  • Create a new stream for a given context.

    Parameters

    • context: Context

      Typically a file path, but could be an arbitrary object.

    Returns Readable

    A readable stream.

  • Get a cache key from a given Context. Useful when context object is rich and not unique, but a sub-property of it (e.g. context.path) uniquely represents the cached value.

    Parameters

    • context: Context

      Typically a file path, but could be an arbitrary object.

    Returns unknown

    An arbitrary string or object to be used as a key for an Map

  • Put managed stream into the storage.

    Parameters

    • entry: Readonly<{
          offset: number;
          stream: Readable;
      }>

      Entry to be stored.

    • context: Context

      Context reference.

    Returns void

  • Remove managed stream from the storage. Called when managed stream gets destroyed.

    Parameters

    • entry: Readonly<{
          offset: number;
          stream: Readable;
      }>

      Entry to be removed.

    • context: Context

      A context reference that was provided to put().

    Returns void

  • Take stored stream out of the storage (and remove it). The returned entry MUST have an entry.offset less or equal to startOffset.

    Ideally, it should be as close as possible to startOffset.

    Parameters

    • startOffset: number

      Starting offset into the stream.

    • context: Context

      A context reference that was provided to put().

    Returns undefined | Readonly<{
        offset: number;
        stream: Readable;
    }>

    Stored entry or undefined.