Skip to content

Commit 6ca9ef1

Browse files
authored
Add TypeScript type declarations (#3)
1 parent db4e833 commit 6ca9ef1

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ With [npm](https://npmjs.org) do:
6767
npm install level-read-stream
6868
```
6969

70+
Usage from TypeScript also requires `npm install @types/readable-stream`.
71+
7072
## API
7173

7274
### `stream = new EntryStream(db[, options])`
@@ -93,7 +95,7 @@ An instance of `EntryStream`, `KeyStream` or `ValueStream` has the following spe
9395

9496
#### `stream.db`
9597

96-
A read-only reference to the `db` that was passed to the stream constructor.
98+
A read-only reference to the database that this stream is reading from.
9799

98100
## Contributing
99101

index.d.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

package.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,33 @@
44
"description": "Read from an abstract-level database using Node.js streams",
55
"license": "MIT",
66
"scripts": {
7-
"test": "standard && hallmark && (nyc -s node test.js | faucet) && nyc report",
7+
"test": "standard && ts-standard *.ts && hallmark && (nyc -s node test.js | faucet) && nyc report",
88
"test-browsers-local": "airtap --coverage test.js",
99
"coverage": "nyc report -r lcovonly",
1010
"hallmark": "hallmark fix"
1111
},
12+
"main": "index.js",
13+
"types": "./index.d.ts",
1214
"files": [
1315
"index.js",
16+
"index.d.ts",
1417
"CHANGELOG.md",
1518
"UPGRADING.md"
1619
],
1720
"dependencies": {
1821
"readable-stream": "^3.4.0"
1922
},
23+
"peerDependencies": {
24+
"abstract-level": "^1.0.0"
25+
},
26+
"peerDependenciesMeta": {
27+
"abstract-level": {
28+
"optional": true
29+
}
30+
},
2031
"devDependencies": {
32+
"@types/readable-stream": "^2.3.13",
33+
"@voxpelli/tsconfig": "^3.1.0",
2134
"airtap": "^4.0.3",
2235
"airtap-playwright": "^1.0.1",
2336
"faucet": "^0.0.1",
@@ -26,7 +39,9 @@
2639
"nyc": "^15.1.0",
2740
"secret-event-listener": "^1.0.0",
2841
"standard": "^16.0.3",
29-
"tape": "^5.0.1"
42+
"tape": "^5.0.1",
43+
"ts-standard": "^11.0.0",
44+
"typescript": "^4.5.5"
3045
},
3146
"repository": "Level/read-stream",
3247
"homepage": "https://github.com/Level/read-stream",

tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@voxpelli/tsconfig/node12.json",
3+
"compilerOptions": {
4+
"checkJs": false
5+
},
6+
"include": ["*.ts", "types/*.ts"]
7+
}

0 commit comments

Comments
 (0)