-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.d.ts
209 lines (182 loc) · 5.54 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
AbstractLevel,
AbstractDatabaseOptions,
AbstractOpenOptions,
NodeCallback
} from 'abstract-level'
// Requires `npm install @types/readable-stream`.
import { Duplex } from 'readable-stream'
/**
* Guest database that reads and writes to the host's database.
*
* @template KDefault The default type of keys if not overridden on operations.
* @template VDefault The default type of values if not overridden on operations.
*/
export class ManyLevelGuest<KDefault = string, VDefault = string>
extends AbstractLevel<Buffer, KDefault, VDefault> {
/**
* Database constructor.
*
* @param options Options.
*/
constructor (options?: GuestDatabaseOptions<KDefault, VDefault> | undefined)
open (): Promise<void>
open (options: GuestOpenOptions): Promise<void>
open (callback: NodeCallback<void>): void
open (options: GuestOpenOptions, callback: NodeCallback<void>): void
/**
* Create a duplex guest stream to be piped into a host stream. Until that's done,
* operations made on this database are queued up in memory. Will throw if
* {@link createRpcStream()} was previously called and that stream has not (yet)
* closed.
*/
createRpcStream (options?: GuestRpcStreamOptions | undefined): Duplex
/**
* Alias to {@link createRpcStream()} for [`multileveldown`][1] API compatibility.
*
* [1]: https://github.com/Level/multileveldown
*/
connect (options?: GuestRpcStreamOptions | undefined): Duplex
/**
* Instead of talking to a host, forward all database operations to {@link db2}. This
* method is used by `rave-level` and serves a narrow use case. Which is to say, it may
* not work for anything other than `rave-level`. Among other things, it assumes that
* {@link db2} is open and that it uses the same encoding options as the guest database.
*
* @param db2 Another {@link AbstractLevel} database.
*/
forward (db2: AbstractLevel<Buffer, KDefault, VDefault>): void
/**
* Returns `true` if there are no operations pending to be sent to a host.
*/
isFlushed (): boolean
}
/**
* A host that exposes a given database.
*/
export class ManyLevelHost {
/**
* Host constructor.
*
* @param db An `abstract-level` database that supports the `'buffer'` encoding (most
* if not all do). It can also be a {@link AbstractLevel.sublevel()} which allows for
* exposing only a specific section of the database.
* @param options Host options.
*/
constructor (db: AbstractLevel<Buffer, any, any>, options?: HostOptions | undefined)
/**
* Create a duplex host stream to be piped into a guest stream. One per guest.
*/
createRpcStream (): Duplex
}
/**
* Options for the {@link ManyLevelGuest} constructor.
*/
declare interface GuestDatabaseOptions<K, V> extends AbstractDatabaseOptions<K, V> {
/**
* If true, resend operations when reconnected. If false, abort operations when
* disconnected, which means to yield an error on e.g. `db.get()`.
*
* @defaultValue `false`
*/
retry?: boolean
/**
* An {@link AbstractLevel} option that has no effect on {@link ManyLevelGuest}.
*/
createIfMissing?: boolean
/**
* An {@link AbstractLevel} option that has no effect on {@link ManyLevelGuest}.
*/
errorIfExists?: boolean
}
/**
* Options for the {@link ManyLevelGuest.open} method.
*/
declare interface GuestOpenOptions extends AbstractOpenOptions {
/**
* An {@link AbstractLevel} option that has no effect on {@link ManyLevelGuest}.
*/
createIfMissing?: boolean
/**
* An {@link AbstractLevel} option that has no effect on {@link ManyLevelGuest}.
*/
errorIfExists?: boolean
}
/**
* Options for the {@link ManyLevelGuest.createRpcStream} method.
*/
declare interface GuestRpcStreamOptions {
/**
* An object to only keep the Node.js event loop alive while there are pending database
* operations. Could be set to a Node.js `net` socket for example. Not relevant when
* {@link ManyLevelGuest} is used in a browser environment.
*/
ref?: GuestRef
}
/**
* A socket-like object with `ref()` and `unref()` methods.
*/
declare interface GuestRef {
/**
* Called when there's a new database operation like `db.get()`.
*/
ref: () => void
/**
* Called when all operations have finished (or when the database is closed).
*/
unref: () => void
}
/**
* Options for the {@link ManyLevelHost} constructor.
*/
declare interface HostOptions {
/**
* Reject write operations like `db.put()`.
*
* @defaultValue `false`
*/
readonly?: boolean
/**
* A function to be called before `db.put()` operations.
*/
preput?: (key: Buffer, value: Buffer, callback: NodeCallback<void>) => void
/**
* A function to be called before `db.del()` operations.
*/
predel?: (key: Buffer, callback: NodeCallback<void>) => void
/**
* A function to be called before `db.batch()` operations.
*/
prebatch?: (operations: HostBatchOperation[], callback: NodeCallback<void>) => void
}
declare type HostBatchOperation = HostBatchPutOperation | HostBatchDelOperation
/**
* A _put_ operation to be committed by a {@link ManyLevelHost}.
*/
declare interface HostBatchPutOperation {
/**
* Type of operation.
*/
type: 'put'
/**
* Key of the entry to be added to the database.
*/
key: Buffer
/**
* Value of the entry to be added to the database.
*/
value: Buffer
}
/**
* A _del_ operation to be committed by a {@link ManyLevelHost}.
*/
declare interface HostBatchDelOperation {
/**
* Type of operation.
*/
type: 'del'
/**
* Key of the entry to be deleted from the database.
*/
key: Buffer
}