Skip to content

Commit

Permalink
feat: improved hypercore types and new modules (#9)
Browse files Browse the repository at this point in the history
- Add Hypercore.createProtocolStream types
- Improve core.replicate() types
- Add @hyperswarm/secret-stream (NoiseStream) types
- Add Protomux types
  • Loading branch information
gmaclennan committed Oct 12, 2023
1 parent f2971d2 commit cb27a44
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 2 deletions.
58 changes: 58 additions & 0 deletions vendor/@hyperswarm/secret-stream/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Duplex as NodeDuplex } from 'stream'
import { Duplex, DuplexEvents } from 'streamx'

interface KeyPair {
publicKey: Buffer
secretKey: Buffer
}

interface Opts {
autostart?: boolean
// TODO: Use https://github.com/chm-diederichs/noise-handshake/blob/main/noise.js for specific patterns
pattern?: string
remotePublicKey?: Buffer
keyPair?: KeyPair
handshake?: {
tx: Buffer
rx: Buffer
hash: Buffer
publicKey: Buffer
remotePublicKey: Buffer
}
}

type NoiseStreamEvents = {
connect: () => void
handshake: () => void
}

declare class NoiseSecretStream<
RawStream extends NodeDuplex | Duplex = Duplex
> extends Duplex<
any,
any,
any,
any,
true,
true,
DuplexEvents<any, any> & NoiseStreamEvents
> {
readonly isInitiator: boolean
readonly noiseStream: this
publicKey: Buffer | null
remotePublicKey: Buffer | null
handshakeHash: Buffer | null
rawStream: RawStream
opened: Promise<boolean>
userData: any

constructor(isInitiator: boolean, rawStream?: RawStream, opts?: Opts)

static keyPair(seed?: Buffer): KeyPair

start(rawStream?: NodeDuplex, opts?: Opts): void
setTimeout(ms?: number): void
setKeepAlive(ms?: number): void
}

export = NoiseSecretStream
4 changes: 4 additions & 0 deletions vendor/@hyperswarm/secret-stream/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "",
"types": "index.d.ts"
}
19 changes: 17 additions & 2 deletions vendor/hypercore/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { TypedEmitter } from 'tiny-typed-emitter'
import RandomAccessStorage from 'random-access-storage'
import { type Duplex, type Readable } from 'streamx'
import { type Duplex as NodeDuplex } from 'stream'
import type Protomux from 'protomux'
import type NoiseStream from '@hyperswarm/secret-stream'

interface RemoteBitfield {
get(index: number): boolean
Expand Down Expand Up @@ -119,6 +122,16 @@ declare namespace Hypercore {
| ((name: HypercoreStorageName) => RandomAccessStorage)
}


type ProtocolStream = Omit<NoiseStream, 'userData'> & { userData: Protomux }
type ReplicationStream = Duplex & { noiseStream: ProtocolStream }

type CreateProtocolStreamOpts = {
stream?: Duplex | NodeDuplex
keepAlive?: boolean
ondiscoverykey?: (id: Buffer) => Promise<any>
}

declare class Hypercore<
TValueEncoding extends Hypercore.ValueEncoding = 'binary',
TKey extends Buffer | string | undefined = undefined
Expand All @@ -135,6 +148,7 @@ declare class Hypercore<
readonly contiguousLength: number
readonly fork: number
readonly padding: number
static createProtocolStream(stream: boolean | Duplex | NodeDuplex | NoiseStream | ProtocolStream | ReplicationStream | Protomux, opts: CreateProtocolStreamOpts): ReplicationStream

constructor(storage: Hypercore.HypercoreStorage)
constructor(
Expand Down Expand Up @@ -218,9 +232,10 @@ declare class Hypercore<
}
): HypercoreExtension<T>
replicate(
isInitiatorOrReplicationStream: boolean | Duplex,
isInitiatorOrReplicationStream: boolean | Duplex | NodeDuplex,
opts?: { keepAlive?: boolean }
): Duplex
): ReplicationStream
replicate(protomux: Protomux, opts?: { keepAlive?: boolean }): Protomux
findingPeers(): () => void
}

Expand Down
74 changes: 74 additions & 0 deletions vendor/protomux/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Duplex } from 'streamx'

interface PreEncodingState {
buffer: null
start: number
end: number
}

interface EncodingState {
buffer: null | Buffer
start: number
end: number
}

interface Encoding {
preencode(state: PreEncodingState, value: any): void
encode(state: EncodingState, value: any): void
decode(state: EncodingState): any
}

interface Message {
type: number
send(msg: any): void
onmessage: (message: any) => void
encoding: Encoding
}

type MessageOptions = Partial<Pick<Message, 'onmessage' | 'encoding'>>

interface Channel {
open(handshake?: any): void
userData: any
protocol: string
id: Buffer
messages: Message[]
opened: boolean
closed: boolean
destroyed: boolean
cork(): void
uncork(): void
close(): void
addMessage(opts?: MessageOptions): Message
}

declare class Protomux {
constructor(stream: Duplex)
isProtomux: true
stream: Duplex
static from(stream: Duplex): Protomux
static isProtomux(mux: unknown): mux is Protomux
cork(): void
uncork(): void
pair(
opts: { protocol: string; id?: Buffer },
notify: (id: Buffer) => Promise<void>
): void
unpair(opts: { protocol: string; id?: Buffer }): void
opened(opts: { protocol: string; id?: Buffer }): boolean
createChannel(opts: {
userData?: any
protocol: string
aliases?: string[]
id?: Buffer
unique?: boolean
handshake?: Encoding
messages: MessageOptions[]
onopen?(handshake?: any): Promise<void> | void
onclose?(): Promise<void> | void
ondestroy?(): Promise<void> | void
}): Channel
destroy(err: Error): void
}

export = Protomux
4 changes: 4 additions & 0 deletions vendor/protomux/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "",
"types": "index.d.ts"
}

0 comments on commit cb27a44

Please sign in to comment.