@dxos/vendor-hypercore 0.8.4-main.03d5cd7b56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,299 @@
1
+ //
2
+ // Copyright 2021 DXOS.org
3
+ //
4
+
5
+ /**
6
+ * Hypercore Typescript Definitions version 9.12.0
7
+ * NOTE: Must not clash with 'hypercore' package name.
8
+ *
9
+ * https://hypercore-protocol.org
10
+ * https://www.npmjs.com/package/hypercore/v/9.12.0
11
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0
12
+ * https://github.com/hypercore-protocol/hypercore/blob/v9.12.0/index.js#L53
13
+ */
14
+ import type { ProtocolStream } from 'hypercore-protocol';
15
+ import type { Nanoresource, NanoresourceProperties } from 'nanoresource';
16
+ import type { RandomAccessStorageConstructor } from 'random-access-storage';
17
+ import { type Readable, type Writable } from 'streamx';
18
+
19
+ export type Callback<T> = (err: Error | null, result: T) => void;
20
+
21
+ /**
22
+ * Download range.
23
+ */
24
+ export type Range = {
25
+ start: number;
26
+ end?: number;
27
+ linear?: boolean;
28
+ };
29
+
30
+ /**
31
+ * https://github.com/mafintosh/abstract-encoding
32
+ */
33
+ export type AbstractValueEncoding<T = any> = {
34
+ encode: (obj: T) => Buffer;
35
+ decode: (buffer: Buffer) => T;
36
+ };
37
+
38
+ export type ValueEncoding<T = any> = 'json' | 'utf-8' | 'binary' | AbstractValueEncoding<T>;
39
+
40
+ /**
41
+ * Crypto
42
+ */
43
+ export interface Crypto {
44
+ sign: (data: any, secretKey: Buffer, cb: Callback<any>) => void;
45
+ verify: (signature: any, data: any, key: Buffer, cb: Callback<boolean>) => void;
46
+ }
47
+
48
+ /**
49
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedcreatereadstreamoptions
50
+ */
51
+ export type ReadStreamOptions = {
52
+ start?: number;
53
+ end?: number;
54
+ snapshot?: boolean;
55
+ tail?: boolean;
56
+ live?: boolean;
57
+ timeout?: number;
58
+ wait?: boolean;
59
+ batch?: number;
60
+ };
61
+
62
+ /**
63
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#const-id--feedgetindex-options-callback
64
+ */
65
+ // TODO(burdon): Change all value defs to default.
66
+ export type GetOptions = {
67
+ wait?: true;
68
+ onwait?: () => {};
69
+ timeout?: 0;
70
+ valueEncoding?: ValueEncoding;
71
+ };
72
+
73
+ /**
74
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedcreatewritestreamopts
75
+ */
76
+ export type WriteStreamOptions = {
77
+ maxBlockSize?: number;
78
+ };
79
+
80
+ /**
81
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedreplicateisinitiator-options
82
+ */
83
+ export type ReplicationOptions = {
84
+ initiator?: ProtocolStream | boolean;
85
+ stream?: ProtocolStream;
86
+ live?: boolean;
87
+ ack?: boolean;
88
+ download?: boolean;
89
+ upload?: boolean;
90
+ encrypted?: boolean;
91
+ noise?: boolean;
92
+ keyPair?: { publicKey: Buffer; secretKey: Buffer };
93
+ onauthenticate?: (remotePublicKey: Buffer, cb: () => void) => void;
94
+ onfeedauthenticate?: (feed: Hypercore, remotePublicKey: Buffer, cb: () => void) => void;
95
+ maxRequests?: number; // Default 16.
96
+ };
97
+
98
+ /**
99
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedstats
100
+ */
101
+ export type Stats = {
102
+ peers: Stats[];
103
+ totals: {
104
+ uploadedBytes: number;
105
+ uploadedBlocks: number;
106
+ downloadedBytes: number;
107
+ downloadedBlocks: number;
108
+ };
109
+ };
110
+
111
+ /**
112
+ * Bi-directional custom message path for non-feed data exchange.
113
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#ext--feedregisterextensionname-handlers
114
+ */
115
+ export interface StreamExtension {
116
+ // Send message to extension handler on other side.
117
+ send: (message: Buffer, peer: Buffer) => void;
118
+
119
+ // Send message to every peer.
120
+ broadcast: (message: Buffer) => void;
121
+
122
+ // Destroy and unregister from stream.
123
+ destroy: () => void;
124
+ }
125
+
126
+ interface StreamExtensionHandlers<T> {
127
+ encoding?: ValueEncoding<T>;
128
+ onmessage?: (message: Buffer, peer: Buffer) => any;
129
+ onerror?: (error: any) => any;
130
+ }
131
+
132
+ /**
133
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-feed--hypercorestorage-key-options
134
+ */
135
+ export type HypercoreOptions = {
136
+ sparse?: boolean; // Do not mark the entire feed to be downloaded.
137
+ eagerUpdate?: boolean;
138
+ maxRequests?: number;
139
+ createIfMissing?: boolean;
140
+ secretKey?: Buffer;
141
+ valueEncoding?: ValueEncoding;
142
+ crypto?: Crypto;
143
+ writable?: boolean;
144
+ stats?: boolean;
145
+ };
146
+
147
+ export type Proof = {
148
+ nodes: {
149
+ index: number;
150
+ hash: Buffer;
151
+ size: number;
152
+ }[];
153
+ signature: Buffer;
154
+ };
155
+
156
+ /**
157
+ * Shared property definitions for raw and wrapped objects.
158
+ */
159
+ export interface HypercoreProperties extends NanoresourceProperties {
160
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedwritable
161
+ readonly writable: boolean;
162
+
163
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedreadable
164
+ readonly readable: boolean;
165
+
166
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedkey
167
+ readonly key: Buffer;
168
+
169
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedkey
170
+ readonly secretKey: Buffer;
171
+
172
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#discoveryKey
173
+ readonly discoveryKey: Buffer;
174
+
175
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedlength
176
+ readonly length: number;
177
+
178
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedbytelength
179
+ readonly byteLength: number;
180
+
181
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedpeers
182
+ readonly peers: {
183
+ publicKey: Buffer;
184
+ };
185
+
186
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedstats
187
+ readonly stats: Stats;
188
+
189
+ bitfield?: HypercoreBitfield;
190
+
191
+ readonly sparse: boolean;
192
+ }
193
+
194
+ export interface HypercoreBitfield {
195
+ data: any; // sparse-bitfield package
196
+
197
+ total(start: number, end: number): number;
198
+
199
+ // TODO(dmaretskyi): More props.
200
+ }
201
+
202
+ /**
203
+ * Raw hypercore feed.
204
+ * https://github.com/hypercore-protocol/hypercore/blob/v9.12.0/index.js#L53
205
+ *
206
+ * Events: [`ready`, `error`, `download`, `upload`, `append`, `sync`, `close`]
207
+ * https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedonready
208
+ */
209
+ export interface Hypercore<T = any> extends Nanoresource, HypercoreProperties {
210
+ // Alias for open.
211
+ ready(cb: Callback<void>): void;
212
+
213
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedappenddata-callback
214
+ append(data: T | T[], cb: Callback<number>): void;
215
+
216
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedcreatereadstreamoptions
217
+ createReadStream(options?: ReadStreamOptions): Readable;
218
+
219
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedcreatewritestreamopts
220
+ createWriteStream(options?: WriteStreamOptions): Writable;
221
+
222
+ /**
223
+ * Sets up a replication stream.
224
+ * Blocks are downloaded:
225
+ * - explicitly when download or get called;
226
+ * - implicitly if options.sparse and options.eagerUpdate are true.
227
+ * @param initiator
228
+ * @param options
229
+ */
230
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-stream--feedreplicateisinitiator-options
231
+ replicate(initiator: boolean, options?: ReplicationOptions): ProtocolStream;
232
+
233
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feeddestroystoragecallback
234
+ destroyStorage(cb: Callback<{ valid: number; invalid: number }>): void;
235
+
236
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedauditcallback
237
+ audit(cb: Callback<{ valid: number; invalid: number }>): void;
238
+
239
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-bool--feedhasindex
240
+ has(index: number): void;
241
+ has(start: number, end?: number): boolean;
242
+
243
+ // https://github.com/holepunchto/hypercore/tree/v9.12.0#feedclearstart-end-callback
244
+ clear(start: number, end?: number, cb?: Callback<void>): void;
245
+
246
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedheadoptions-callback
247
+ /** @deprecated remove in v10 */
248
+ head(options: any, cb: Callback<T>): void;
249
+ head(cb: Callback<T>): void;
250
+
251
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#const-id--feedgetindex-options-callback
252
+ get(index: number, options: GetOptions, cb: Callback<T>): void;
253
+ get(index: number, cb: Callback<T>): void;
254
+
255
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedgetbatchstart-end-options-callback
256
+ /** @deprecated remove in v10 */
257
+ getBatch(start: number, end: number, options?: GetOptions, cb?: Callback<T[]>): void;
258
+
259
+ // TODO(burdon): Documented signature is different from code.
260
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#const-id--feeddownloadrange-callback
261
+ download(range?: Range, cb?: Callback<number>): number;
262
+
263
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-number--feeddownloadedstart-end
264
+ downloaded(start?: number, end?: number): boolean;
265
+
266
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#feedundownloaddownloadid
267
+ undownload(id: number): void;
268
+
269
+ // Define custom messages paths (unrelated to hypercore exchange), which are multiplexed on the stream.
270
+ // https://github.com/hypercore-protocol/hypercore-protocol#stream-message-extensions
271
+ registerExtension(name: string, handlers?: StreamExtensionHandlers<T>): StreamExtension;
272
+
273
+ // https://github.com/holepunchto/hypercore/tree/v9.12.0#feedsetdownloadingbool
274
+ setDownloading(bool): void;
275
+
276
+ proof(index: number, cb?: Callback<Proof>): void;
277
+
278
+ put(index: number, data: T, proof: Proof, cb?: Callback<void>): void;
279
+
280
+ // TODO(dmaretskyi): Add other events.
281
+ on(event: string, cb: (...args: any) => void): void;
282
+ on(event: 'download', cb: (index: number, data: any) => void): void;
283
+ }
284
+
285
+ export type HypercoreConstructor = (
286
+ storage: string | RandomAccessStorageConstructor,
287
+ key?: Buffer | string,
288
+ options?: HypercoreOptions,
289
+ ) => Hypercore;
290
+
291
+ // Default constructor.
292
+ // https://github.com/hypercore-protocol/hypercore/tree/v9.12.0#var-feed--hypercorestorage-key-options
293
+ export function hypercore<T = any>(
294
+ storage: string | RandomAccessStorageConstructor,
295
+ key?: Buffer | string,
296
+ options?: HypercoreOptions,
297
+ ): Hypercore<T>;
298
+
299
+ export default hypercore;
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ export * from 'hypercore';
6
+ export { default } from 'hypercore';