-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
183 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// this is dummy module overlayed by interface.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable no-use-before-define */ | ||
import { Link, CIDView, CIDVersion } from '../cid/interface.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> {} | ||
|
||
/** | ||
* 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 non-existings | ||
// unique symbol. But given that field is optional any object will valid | ||
// type contstraint. | ||
[Marker]?: T | ||
} | ||
declare const Marker: unique symbol | ||
|
||
/** | ||
* 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 CIDVersion = 1 | ||
> { | ||
bytes: ByteView<T> | ||
cid: Link<T, C, A, V> | ||
} | ||
|
||
export type BlockCursorView = | ||
| { value: unknown, remaining?: undefined } | ||
| { value: CIDView, remaining: string } | ||
|
||
export interface BlockView< | ||
T = unknown, | ||
C extends number = number, | ||
A extends number = number, | ||
V extends CIDVersion = 1 | ||
> extends Block<T, C, A, V> { | ||
cid: CIDView<C, A, V> & Link<T, C, A, V> | ||
|
||
links(): Iterable<[string, CIDView]> | ||
tree(): Iterable<string> | ||
get(path:string): BlockCursorView | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.