Skip to content

Commit 839a0ad

Browse files
authored
chore: use separate helper file for NoiseSecretStreams (#816)
This change should have no impact on functionality. This change creates `src/lib/noise-secret-stream-helpers.js`. It moves `openedNoiseSecretStream()` and two types in there. It also tweaks the types for `OpenedNoiseStream` and `DestroyedNoiseStream`. I think this is a useful change on its own but may also make an upcoming change easier.
1 parent 10ea50e commit 839a0ad

File tree

8 files changed

+49
-30
lines changed

8 files changed

+49
-30
lines changed

src/discovery/local-discovery.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import StartStopStateMachine from 'start-stop-state-machine'
99
import pTimeout from 'p-timeout'
1010
import { keyToPublicId } from '@mapeo/crypto'
1111
import { Logger } from '../logger.js'
12-
/** @import { OpenedNoiseStream } from '../utils.js' */
12+
/** @import { OpenedNoiseStream } from '../lib/noise-secret-stream-helpers.js' */
1313

1414
/** @typedef {{ publicKey: Buffer, secretKey: Buffer }} Keypair */
1515
/** @typedef {OpenedNoiseStream<net.Socket>} OpenedNetNoiseStream */
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/** @import { Duplex as NodeDuplex } from 'node:stream' */
2+
/** @import { Duplex as StreamxDuplex } from 'streamx' */
3+
/** @import NoiseSecretStream from '@hyperswarm/secret-stream' */
4+
5+
/**
6+
* @internal
7+
* @typedef {NodeDuplex | StreamxDuplex} RawStream
8+
*/
9+
10+
/**
11+
* @template {RawStream} [T=RawStream]
12+
* @typedef {NoiseSecretStream<T> & { destroyed: true }} DestroyedNoiseStream
13+
*/
14+
15+
/**
16+
* @template {RawStream} [T=RawStream]
17+
* @typedef {NoiseSecretStream<T> & {
18+
* publicKey: Buffer,
19+
* remotePublicKey: Buffer,
20+
* handshake: Buffer,
21+
* destroyed: false
22+
* }} OpenedNoiseStream
23+
*/
24+
25+
/**
26+
* Utility to await a NoiseSecretStream to open, that returns a stream with the
27+
* correct types for publicKey and remotePublicKey (which can be null before
28+
* stream is opened)
29+
*
30+
* @template {RawStream} T
31+
* @param {NoiseSecretStream<T>} stream
32+
* @returns {Promise<OpenedNoiseStream<T> | DestroyedNoiseStream<T>>}
33+
*/
34+
export async function openedNoiseSecretStream(stream) {
35+
await stream.opened
36+
return /** @type {OpenedNoiseStream<T> | DestroyedNoiseStream<T>} */ (stream)
37+
}

src/local-peers.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import pDefer from 'p-defer'
1414
import { Logger } from './logger.js'
1515
import pTimeout, { TimeoutError } from 'p-timeout'
16+
/** @import { OpenedNoiseStream } from './lib/noise-secret-stream-helpers.js' */
1617

1718
// Unique identifier for the mapeo rpc protocol
1819
const PROTOCOL_NAME = 'mapeo/rpc'
@@ -387,7 +388,7 @@ export class LocalPeers extends TypedEmitter {
387388
}
388389

389390
/**
390-
* @param {Protomux<import('./utils.js').OpenedNoiseStream>} protomux
391+
* @param {Protomux<OpenedNoiseStream>} protomux
391392
* @param {() => void} done
392393
*/
393394
#makePeer(protomux, done) {
@@ -460,7 +461,7 @@ export class LocalPeers extends TypedEmitter {
460461
}
461462

462463
/**
463-
* @param {Protomux<import('./utils.js').OpenedNoiseStream>} protomux
464+
* @param {Protomux<OpenedNoiseStream>} protomux
464465
*/
465466
#getPeerByProtomux(protomux) {
466467
// We could also index peers by protomux to avoid this, but that would mean
@@ -511,7 +512,7 @@ export class LocalPeers extends TypedEmitter {
511512

512513
/**
513514
*
514-
* @param {Protomux<import('./utils.js').OpenedNoiseStream>} protomux
515+
* @param {Protomux<OpenedNoiseStream>} protomux
515516
* @param {keyof typeof MESSAGE_TYPES} type
516517
* @param {Buffer} value
517518
*/

src/mapeo-manager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import {
2727
deNullify,
2828
getDeviceId,
2929
keyToId,
30-
openedNoiseSecretStream,
3130
projectIdToNonce,
3231
projectKeyToId,
3332
projectKeyToProjectInviteId,
3433
projectKeyToPublicId,
3534
} from './utils.js'
35+
import { openedNoiseSecretStream } from './lib/noise-secret-stream-helpers.js'
3636
import { RandomAccessFilePool } from './core-manager/random-access-file-pool.js'
3737
import BlobServerPlugin from './fastify-plugins/blobs.js'
3838
import IconServerPlugin from './fastify-plugins/icons.js'
@@ -51,6 +51,7 @@ import {
5151
/** @import { ProjectSettingsValue as ProjectValue } from '@mapeo/schema' */
5252
/** @import { SetNonNullable } from 'type-fest' */
5353
/** @import { CoreStorage, Namespace } from './types.js' */
54+
/** @import { OpenedNoiseStream } from './lib/noise-secret-stream-helpers.js' */
5455

5556
/** @typedef {SetNonNullable<ProjectKeys, 'encryptionKeys'>} ValidatedProjectKeys */
5657

src/sync/peer-sync-controller.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { unreplicate } from '../lib/hypercore-helpers.js'
77
/** @import { Role } from '../roles.js' */
88
/** @import { SyncEnabledState } from './sync-api.js' */
99
/** @import { Namespace } from '../types.js' */
10+
/** @import { OpenedNoiseStream } from '../lib/noise-secret-stream-helpers.js' */
1011

1112
/**
1213
* @typedef {Role['sync'][Namespace] | 'unknown'} SyncCapability
@@ -35,7 +36,7 @@ export class PeerSyncController {
3536

3637
/**
3738
* @param {object} opts
38-
* @param {import("protomux")<import('../utils.js').OpenedNoiseStream>} opts.protomux
39+
* @param {import('protomux')<OpenedNoiseStream>} opts.protomux
3940
* @param {import("../core-manager/index.js").CoreManager} opts.coreManager
4041
* @param {import("./sync-state.js").SyncState} opts.syncState
4142
* @param {import('../roles.js').Roles} opts.roles

src/sync/sync-api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ExhaustivenessError, assert, keyToId, noop } from '../utils.js'
1111
import { NO_ROLE_ID } from '../roles.js'
1212
/** @import { CoreOwnership as CoreOwnershipDoc } from '@mapeo/schema' */
1313
/** @import { CoreOwnership } from '../core-ownership.js' */
14+
/** @import { OpenedNoiseStream } from '../lib/noise-secret-stream-helpers.js' */
1415

1516
export const kHandleDiscoveryKey = Symbol('handle discovery key')
1617
export const kSyncState = Symbol('sync state')
@@ -359,7 +360,7 @@ export class SyncApi extends TypedEmitter {
359360
* will then handle validation of role records to ensure that the peer is
360361
* actually still part of the project.
361362
*
362-
* @param {{ protomux: import('protomux')<import('../utils.js').OpenedNoiseStream> }} peer
363+
* @param {{ protomux: import('protomux')<OpenedNoiseStream> }} peer
363364
*/
364365
#handlePeerAdd = (peer) => {
365366
const { protomux } = peer

src/utils.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import sodium from 'sodium-universal'
33
import { keyToPublicId } from '@mapeo/crypto'
44
import { createHash } from 'node:crypto'
55
import stableStringify from 'json-stable-stringify'
6-
/** @import { Duplex as NodeDuplex } from 'node:stream' */
7-
/** @import { Duplex as StreamxDuplex } from 'streamx' */
8-
/** @import NoiseStream from '@hyperswarm/secret-stream' */
96

107
const PROJECT_INVITE_ID_SALT = Buffer.from('mapeo project invite id', 'ascii')
118

@@ -46,25 +43,6 @@ export function parseVersion(version) {
4643
}
4744
}
4845

49-
/** @typedef {NoiseStream & { destroyed: true }} DestroyedNoiseStream */
50-
/**
51-
* @template {NodeDuplex | StreamxDuplex} [T=NodeDuplex | StreamxDuplex]
52-
* @typedef {NoiseStream<T> & { publicKey: Buffer, remotePublicKey: Buffer, handshake: Buffer }} OpenedNoiseStream
53-
*/
54-
55-
/**
56-
* Utility to await a NoiseSecretStream to open, that returns a stream with the
57-
* correct types for publicKey and remotePublicKey (which can be null before
58-
* stream is opened)
59-
*
60-
* @param {NoiseStream} stream
61-
* @returns {Promise<OpenedNoiseStream | DestroyedNoiseStream>}
62-
*/
63-
export async function openedNoiseSecretStream(stream) {
64-
await stream.opened
65-
return /** @type {OpenedNoiseStream | DestroyedNoiseStream} */ (stream)
66-
}
67-
6846
export class ExhaustivenessError extends Error {
6947
/** @param {never} value */
7048
constructor(value) {

tests/discovery/local-discovery.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
LocalDiscovery,
1313
} from '../../src/discovery/local-discovery.js'
1414
import NoiseSecretStream from '@hyperswarm/secret-stream'
15-
/** @import { OpenedNoiseStream } from '../../src/utils.js' */
15+
/** @import { OpenedNoiseStream } from '../../src/lib/noise-secret-stream-helpers.js' */
1616

1717
test('peer discovery - discovery and sharing of data', async (t) => {
1818
const deferred = pDefer()

0 commit comments

Comments
 (0)