-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathinterface.ts
73 lines (66 loc) · 2.45 KB
/
interface.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
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
/* eslint-disable no-use-before-define */
import { Link, Version } from '../link/interface.js'
import { CID } from '../cid.js'
/**
* A byte-encoded representation of some type of `Data`.
*
* A `ByteView` is essentially a `Uint8Array` that's been "tagged" with
* a `Data` type parameter indicating the type of encoded data.
*
* For example, a `ByteView<{ hello: "world" }>` is a `Uint8Array` containing a
* binary representation of a `{hello: "world"}.
*/
export interface ByteView<Data> extends Uint8Array, Phantom<Data> {}
declare const Marker: unique symbol
/**
* A utility type to retain an unused type parameter `T`.
* Similar to [phantom type parameters in Rust](https://doc.rust-lang.org/rust-by-example/generics/phantom.html).
*
* Capturing unused type parameters allows us to define "nominal types," which
* TypeScript does not natively support. Nominal types in turn allow us to capture
* semantics not represented in the actual type structure, without requring us to define
* new classes or pay additional runtime costs.
*
* For a concrete example, see {@link ByteView}, which extends the `Uint8Array` type to capture
* type information about the structure of the data encoded into the array.
*/
export interface Phantom<T> {
// This field can not be represented because field name is nonexistent
// unique symbol. But given that field is optional any object will valid
// type contstraint.
[Marker]?: T
}
/**
* Represents an IPLD block (including its CID) that can be decoded to data of
* type `T`.
*
* @template T - Logical type of the data encoded in the block
* @template C - multicodec code corresponding to codec used to encode the block
* @template A - multicodec code corresponding to the hashing algorithm used in CID creation.
* @template V - CID version
*/
export interface Block<
T = unknown,
C extends number = number,
A extends number = number,
V extends Version = 1
> {
bytes: ByteView<T>
cid: Link<T, C, A, V>
}
export type BlockCursorView<T extends unknown = unknown> =
| { value: T, remaining?: undefined }
| { value: CID, remaining: string }
export interface BlockView<
T = unknown,
C extends number = number,
A extends number = number,
V extends Version = 1
> extends Block<T, C, A, V> {
cid: CID<T, C, A, V>
value: T
links: () => Iterable<[string, CID]>
tree: () => Iterable<string>
get: (path: string) => BlockCursorView<unknown>
}