-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved hypercore types and new modules (#9)
- Add Hypercore.createProtocolStream types - Improve core.replicate() types - Add @hyperswarm/secret-stream (NoiseStream) types - Add Protomux types
- Loading branch information
1 parent
f2971d2
commit cb27a44
Showing
5 changed files
with
157 additions
and
2 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
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 |
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,4 @@ | ||
{ | ||
"main": "", | ||
"types": "index.d.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
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,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 |
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,4 @@ | ||
{ | ||
"main": "", | ||
"types": "index.d.ts" | ||
} |