Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/hypercore-type-improvements #12

Open
wants to merge 2 commits into
base: feat/protomux-stream-type
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions vendor/@hyperswarm/secret-stream/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
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
declare namespace NoiseSecretStream {
interface KeyPair {
publicKey: Buffer
remotePublicKey: Buffer
secretKey: Buffer
}
}

type NoiseStreamEvents = {
connect: () => void
handshake: () => void
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
RawStream extends NodeDuplex | Duplex = NodeDuplex | Duplex
> extends Duplex<
any,
any,
any,
any,
true,
true,
DuplexEvents<any, any> & NoiseStreamEvents
DuplexEvents<any, any> & NoiseSecretStream.NoiseStreamEvents
> {
readonly isInitiator: boolean
readonly noiseStream: this
Expand All @@ -46,11 +48,15 @@ declare class NoiseSecretStream<
opened: Promise<boolean>
userData: any

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

static keyPair(seed?: Buffer): KeyPair
static keyPair(seed?: Buffer): NoiseSecretStream.KeyPair

start(rawStream?: NodeDuplex, opts?: Opts): void
start(rawStream?: NodeDuplex, opts?: NoiseSecretStream.Opts): void
setTimeout(ms?: number): void
setKeepAlive(ms?: number): void
}
Expand Down
67 changes: 54 additions & 13 deletions vendor/hypercore/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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'
import RandomAccessFile from '../random-access-file/index.js'

interface RemoteBitfield {
get(index: number): boolean
Expand Down Expand Up @@ -49,11 +50,6 @@ interface HypercorePeer {
onrange(options: PeerOnRangeOptions): void
}

interface DownloadingRange {
destroy(): void
done(): Promise<void>
}

type HypercoreStorageName = 'oplog' | 'tree' | 'bitfield' | 'data'

interface HypercoreInfo {
Expand Down Expand Up @@ -120,18 +116,38 @@ declare namespace Hypercore {
type HypercoreStorage =
| string
| ((name: HypercoreStorageName) => RandomAccessStorage)
}

interface DownloadingRange {
destroy(): void
done(): Promise<void>
}

type ReplicationStream<
TStream extends Duplex | NodeDuplex = Duplex | NodeDuplex
> = TStream & {
noiseStream: ProtocolStream
}
}

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

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

interface DefaultStorageOptions {
unlocked: boolean
lock: boolean
poolSize: number
pool: RandomAccessFile.RAFOptions['pool']
rmdir: boolean
writable: boolean
}

declare class Hypercore<
TValueEncoding extends Hypercore.ValueEncoding = 'binary',
TKey extends Buffer | string | undefined = undefined
Expand All @@ -148,8 +164,30 @@ 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

static createProtocolStream(
stream: boolean,
opts?: CreateProtocolStreamOpts
): Hypercore.ReplicationStream<Duplex>
static createProtocolStream(
stream:
| Duplex
| NodeDuplex
| NoiseStream
| ProtocolStream
| Hypercore.ReplicationStream
| Protomux<NoiseStream>,
opts?: CreateProtocolStreamOpts
): Hypercore.ReplicationStream
static defaultStorage<TStorage extends typeof RandomAccessStorage>(
storage: TStorage
): (name: string) => InstanceType<TStorage>
static defaultStorage<
TStorageFn extends (name: string) => RandomAccessStorage
>(storage: TStorageFn): TStorageFn
static defaultStorage(
storage: string,
opts: DefaultStorageOptions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not adamant about this suggestion but in case it's helpful:

Suggested change
opts: DefaultStorageOptions
opts?: Partial<DefaultStorageOptions>

): (name: string) => RandomAccessFile
constructor(storage: Hypercore.HypercoreStorage)
constructor(
storage: Hypercore.HypercoreStorage,
Expand Down Expand Up @@ -220,7 +258,7 @@ declare class Hypercore<
end?: number
blocks?: number[]
linear?: boolean
}): DownloadingRange
}): Hypercore.DownloadingRange
session(options?: Hypercore.HypercoreOptions<TValueEncoding>): Hypercore
close(): Promise<void>
ready(): Promise<void>
Expand All @@ -234,8 +272,11 @@ declare class Hypercore<
replicate(
isInitiatorOrReplicationStream: boolean | Duplex | NodeDuplex,
opts?: { keepAlive?: boolean }
): ReplicationStream
replicate(protomux: Protomux, opts?: { keepAlive?: boolean }): Protomux
): Hypercore.ReplicationStream
replicate(
protomux: Protomux<NoiseStream>,
opts?: { keepAlive?: boolean }
): Protomux<NoiseStream>
findingPeers(): () => void
}

Expand Down