|
| 1 | +import { Readable } from 'readable-stream' |
| 2 | + |
| 3 | +// Assumed to be installed side-by-side, declared as an optional peerDependency. |
| 4 | +import { |
| 5 | + AbstractLevel, |
| 6 | + AbstractIteratorOptions, |
| 7 | + AbstractKeyIteratorOptions, |
| 8 | + AbstractValueIteratorOptions |
| 9 | +} from 'abstract-level' |
| 10 | + |
| 11 | +// NOTE: the types of readable-stream don't have generic type parameters |
| 12 | +declare class LevelReadStream<T, TDatabase> extends Readable { |
| 13 | + /** |
| 14 | + * A read-only reference to the database that this stream is reading from. |
| 15 | + */ |
| 16 | + get db (): TDatabase |
| 17 | + |
| 18 | + [Symbol.asyncIterator] (): AsyncIterableIterator<T> |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * A Node.js readable stream that yields entries. |
| 23 | + */ |
| 24 | +export class EntryStream<K, V, TDatabase = AbstractLevel<any, any, any>> extends LevelReadStream<{ key: K, value: V }, TDatabase> { |
| 25 | + /** |
| 26 | + * Create a Node.js readable stream that yields entries from {@link db}. |
| 27 | + * @param db Database to read from. |
| 28 | + * @param options Options for the stream and its underlying iterator. |
| 29 | + */ |
| 30 | + constructor (db: TDatabase, options?: (ReadStreamOptions & Omit<AbstractIteratorOptions<K, V>, 'keys' | 'values'>) | undefined) |
| 31 | + |
| 32 | + // TODO: support passing in an iterator so that its implementation-specific options are typed? |
| 33 | + // constructor (iterator: AbstractIterator<TDatabase, K, V>, options?: LevelReadStreamOptions | undefined) |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * A Node.js readable stream that yields keys. |
| 38 | + */ |
| 39 | +export class KeyStream<K, TDatabase = AbstractLevel<any, any, any>> extends LevelReadStream<K, TDatabase> { |
| 40 | + /** |
| 41 | + * Create a Node.js readable stream that yields keys from {@link db}. |
| 42 | + * @param db Database to read from. |
| 43 | + * @param options Options for the stream and its underlying iterator. |
| 44 | + */ |
| 45 | + constructor (db: TDatabase, options?: (ReadStreamOptions & AbstractKeyIteratorOptions<K>) | undefined) |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * A Node.js readable stream that yields values. |
| 50 | + */ |
| 51 | +export class ValueStream<K, V, TDatabase = AbstractLevel<any, any, any>> extends LevelReadStream<V, TDatabase> { |
| 52 | + /** |
| 53 | + * Create a Node.js readable stream that yields values from {@link db}. |
| 54 | + * @param db Database to read from. |
| 55 | + * @param options Options for the stream and its underlying iterator. |
| 56 | + */ |
| 57 | + constructor (db: TDatabase, options?: (ReadStreamOptions & AbstractValueIteratorOptions<K, V>) | undefined) |
| 58 | +} |
| 59 | + |
| 60 | +export interface ReadStreamOptions { |
| 61 | + /** |
| 62 | + * The maximum number of items to buffer internally before ceasing to read further |
| 63 | + * items. |
| 64 | + * |
| 65 | + * @defaultValue `1000` |
| 66 | + */ |
| 67 | + highWaterMark?: number | undefined |
| 68 | + |
| 69 | + /** |
| 70 | + * Limit the amount of data that the underlying iterator will hold in memory. |
| 71 | + * |
| 72 | + * Only supported by [`classic-level`][1] and [`rocks-level`][2], and possibly by |
| 73 | + * similar `abstract-level` implementations that are backed by a database on disk. |
| 74 | + * |
| 75 | + * [1]: https://github.com/Level/classic-level |
| 76 | + * [2]: https://github.com/Level/rocks-level |
| 77 | + */ |
| 78 | + highWaterMarkBytes?: number | undefined |
| 79 | + |
| 80 | + /** |
| 81 | + * Only supported by [`classic-level`][1] and [`rocks-level`][2], and possibly by |
| 82 | + * similar `abstract-level` implementations that are backed by a database on disk. |
| 83 | + * |
| 84 | + * [1]: https://github.com/Level/classic-level |
| 85 | + * [2]: https://github.com/Level/rocks-level |
| 86 | + */ |
| 87 | + fillCache?: boolean | undefined |
| 88 | +} |
0 commit comments