Skip to content

Commit 6cdf840

Browse files
authored
chore: use standard Namespace type (#766)
This is a types-only change. The `Namespace` type is pretty central, but was stuffed away in `core-manager/index.js`. Its type was also inferred in many cases. Now it's in a more central spot: `types.ts`. I think this is useful on its own, but it should make a future change easier.
1 parent 4e19cbc commit 6cdf840

13 files changed

+44
-36
lines changed

src/core-manager/core-index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import crypto from 'hypercore-crypto'
2-
3-
/** @typedef {import('./index.js').Namespace} Namespace */
2+
/** @import { Namespace } from '../types.js' */
43
/** @typedef {import('./index.js').CoreRecord} CoreRecord */
54

65
/**

src/core-manager/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ import { coresTable } from '../schema/project.js'
1414
import * as rle from './bitfield-rle.js'
1515
import { CoreIndex } from './core-index.js'
1616

17-
/** @typedef {import('../types.js').HypercorePeer} HypercorePeer */
17+
/** @import { HypercorePeer, Namespace } from '../types.js' */
1818

1919
const WRITER_CORE_PREHAVES_DEBOUNCE_DELAY = 1000
2020

2121
export const kCoreManagerReplicate = Symbol('replicate core manager')
2222

2323
/** @typedef {import('hypercore')<'binary', Buffer>} Core */
24-
/** @typedef {(typeof NAMESPACES)[number]} Namespace */
2524
/** @typedef {{ core: Core, key: Buffer, namespace: Namespace }} CoreRecord */
2625
/** @typedef {import('streamx').Duplex} DuplexStream */
2726
/**
@@ -498,7 +497,7 @@ export class CoreManager extends TypedEmitter {
498497
}
499498

500499
/**
501-
* @param {Exclude<typeof NAMESPACES[number], 'auth'>} namespace
500+
* @param {Exclude<Namespace, 'auth'>} namespace
502501
* @returns {Promise<void>}
503502
*/
504503
async deleteOthersData(namespace) {

src/core-ownership.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ import mapObject from 'map-obj'
99
import { discoveryKey } from 'hypercore-crypto'
1010
import pDefer from 'p-defer'
1111
import { NAMESPACES } from './constants.js'
12-
1312
/**
14-
* @typedef {import('./types.js').CoreOwnershipWithSignatures} CoreOwnershipWithSignatures
13+
* @import {
14+
* CoreOwnershipWithSignatures,
15+
* CoreOwnershipWithSignaturesValue,
16+
* KeyPair,
17+
* Namespace
18+
* } from './types.js'
1519
*/
1620

1721
export class CoreOwnership {
@@ -27,8 +31,8 @@ export class CoreOwnership {
2731
* import('@mapeo/schema').CoreOwnership,
2832
* import('@mapeo/schema').CoreOwnershipValue
2933
* >} opts.dataType
30-
* @param {Record<import('./core-manager/index.js').Namespace, import('./types.js').KeyPair>} opts.coreKeypairs
31-
* @param {import('./types.js').KeyPair} opts.identityKeypair
34+
* @param {Record<Namespace, KeyPair>} opts.coreKeypairs
35+
* @param {KeyPair} opts.identityKeypair
3236
*/
3337
constructor({ dataType, coreKeypairs, identityKeypair }) {
3438
this.#dataType = dataType
@@ -77,7 +81,7 @@ export class CoreOwnership {
7781
/**
7882
*
7983
* @param {string} deviceId
80-
* @param {typeof NAMESPACES[number]} namespace
84+
* @param {Namespace} namespace
8185
* @returns {Promise<string>} coreId of core belonging to `deviceId` for `namespace`
8286
*/
8387
async getCoreId(deviceId, namespace) {
@@ -88,11 +92,11 @@ export class CoreOwnership {
8892

8993
/**
9094
*
91-
* @param {import('./types.js').KeyPair} identityKeypair
92-
* @param {Record<typeof NAMESPACES[number], import('./types.js').KeyPair>} coreKeypairs
95+
* @param {KeyPair} identityKeypair
96+
* @param {Record<Namespace, KeyPair>} coreKeypairs
9397
*/
9498
async #writeOwnership(identityKeypair, coreKeypairs) {
95-
/** @type {import('./types.js').CoreOwnershipWithSignaturesValue} */
99+
/** @type {CoreOwnershipWithSignaturesValue} */
96100
const docValue = {
97101
schemaName: 'coreOwnership',
98102
...mapObject(coreKeypairs, (key, value) => {

src/mapeo-manager.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
kRescindFullStopRequest,
5050
} from './sync/sync-api.js'
5151

52+
/** @import { CoreStorage, Namespace } from './types.js' */
5253
/** @typedef {import("@mapeo/schema").ProjectSettingsValue} ProjectValue */
5354
/** @typedef {import('type-fest').SetNonNullable<ProjectKeys, 'encryptionKeys'>} ValidatedProjectKeys */
5455

@@ -86,7 +87,7 @@ export class MapeoManager extends TypedEmitter {
8687
// Maps project public id -> project instance
8788
/** @type {Map<string, MapeoProject>} */
8889
#activeProjects
89-
/** @type {import('./types.js').CoreStorage} */
90+
/** @type {CoreStorage} */
9091
#coreStorage
9192
#dbFolder
9293
/** @type {string} */
@@ -106,7 +107,7 @@ export class MapeoManager extends TypedEmitter {
106107
* @param {string} opts.dbFolder Folder for sqlite Dbs. Folder must exist. Use ':memory:' to store everything in-memory
107108
* @param {string} opts.projectMigrationsFolder path for drizzle migrations folder for project database
108109
* @param {string} opts.clientMigrationsFolder path for drizzle migrations folder for client database
109-
* @param {string | import('./types.js').CoreStorage} opts.coreStorage Folder for hypercore storage or a function that returns a RandomAccessStorage instance
110+
* @param {string | CoreStorage} opts.coreStorage Folder for hypercore storage or a function that returns a RandomAccessStorage instance
110111
* @param {import('fastify').FastifyInstance} opts.fastify Fastify server instance
111112
* @param {String} [opts.defaultConfigPath]
112113
*/
@@ -359,7 +360,7 @@ export class MapeoManager extends TypedEmitter {
359360
const projectKeypair = KeyManager.generateProjectKeypair()
360361

361362
// 2. Create namespace encryption keys
362-
/** @type {Record<import('./core-manager/core-index.js').Namespace, Buffer>} */
363+
/** @type {Record<Namespace, Buffer>} */
363364
const encryptionKeys = {
364365
auth: randomBytes(32),
365366
blob: randomBytes(32),

src/mapeo-project.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import { IconApi } from './icon-api.js'
5050
import { readConfig } from './config-import.js'
5151
import TranslationApi from './translation-api.js'
5252

53+
/** @import { CoreStorage, KeyPair, Namespace } from './types.js' */
54+
5355
/** @typedef {Omit<import('@mapeo/schema').ProjectSettingsValue, 'schemaName'>} EditableProjectSettings */
5456
/** @typedef {import('@mapeo/schema').ProjectSettingsValue['configMetadata']} ConfigMetadata */
5557

@@ -101,7 +103,7 @@ export class MapeoProject extends TypedEmitter {
101103
* @param {import('./generated/keys.js').EncryptionKeys} opts.encryptionKeys Encryption keys for each namespace
102104
* @param {import('drizzle-orm/better-sqlite3').BetterSQLite3Database} opts.sharedDb
103105
* @param {IndexWriter} opts.sharedIndexWriter
104-
* @param {import('./types.js').CoreStorage} opts.coreStorage Folder to store all hypercore data
106+
* @param {CoreStorage} opts.coreStorage Folder to store all hypercore data
105107
* @param {(mediaType: 'blobs' | 'icons') => Promise<string>} opts.getMediaBaseUrl
106108
* @param {import('./local-peers.js').LocalPeers} opts.localPeers
107109
* @param {Logger} [opts.logger]
@@ -685,7 +687,7 @@ export class MapeoProject extends TypedEmitter {
685687
}
686688

687689
const namespacesWithoutAuth =
688-
/** @satisfies {Exclude<import('./core-manager/index.js').Namespace, 'auth'>[]} */ ([
690+
/** @satisfies {Exclude<Namespace, 'auth'>[]} */ ([
689691
'config',
690692
'data',
691693
'blob',
@@ -908,11 +910,10 @@ async function deleteTranslations(opts) {
908910
* @param {Buffer} opts.projectKey
909911
* @param {Buffer} [opts.projectSecretKey]
910912
* @param {import('@mapeo/crypto').KeyManager} opts.keyManager
911-
* @returns {Record<import('./core-manager/index.js').Namespace, import('./types.js').KeyPair>}
913+
* @returns {Record<Namespace, KeyPair>}
912914
*/
913915
function getCoreKeypairs({ projectKey, projectSecretKey, keyManager }) {
914-
const keypairs =
915-
/** @type {Record<import('./core-manager/index.js').Namespace, import('./types.js').KeyPair>} */ ({})
916+
const keypairs = /** @type {Record<Namespace, KeyPair>} */ ({})
916917

917918
for (const namespace of NAMESPACES) {
918919
keypairs[namespace] =

src/roles.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { currentSchemaVersions } from '@mapeo/schema'
22
import mapObject from 'map-obj'
33
import { kCreateWithDocId } from './datatype/index.js'
44
import { assert, setHas } from './utils.js'
5+
/** @import { Namespace } from './types.js' */
56

67
// Randomly generated 8-byte encoded as hex
78
export const CREATOR_ROLE_ID = 'a12a6702b93bd7ff'
@@ -67,7 +68,7 @@ const isRoleIdAssignableToAnyone = setHas(ROLE_IDS_ASSIGNABLE_TO_ANYONE)
6768
* @property {string} name
6869
* @property {Record<import('@mapeo/schema').MapeoDoc['schemaName'], DocCapability>} docs
6970
* @property {RoleIdAssignableToOthers[]} roleAssignment
70-
* @property {Record<import('./core-manager/core-index.js').Namespace, 'allowed' | 'blocked'>} sync
71+
* @property {Record<Namespace, 'allowed' | 'blocked'>} sync
7172
*/
7273

7374
/**

src/sync/core-sync-state.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { keyToId } from '../utils.js'
22
import RemoteBitfield, {
33
BITS_PER_PAGE,
44
} from '../core-manager/remote-bitfield.js'
5-
/** @typedef {import('../types.js').HypercoreRemoteBitfield} HypercoreRemoteBitfield */
6-
/** @typedef {import('../types.js').HypercorePeer} HypercorePeer */
5+
/** @import { HypercorePeer, HypercoreRemoteBitfield, Namespace } from '../types.js' */
76

87
/**
98
* @typedef {RemoteBitfield} Bitfield
@@ -17,7 +16,7 @@ import RemoteBitfield, {
1716
* @property {PeerState} localState
1817
* @property {Map<PeerId, PeerState>} remoteStates
1918
* @property {Map<string, import('./peer-sync-controller.js').PeerSyncController>} peerSyncControllers
20-
* @property {import('../core-manager/index.js').Namespace} namespace
19+
* @property {Namespace} namespace
2120
*/
2221
/**
2322
* @typedef {object} LocalCoreState
@@ -77,7 +76,7 @@ export class CoreSyncState {
7776
* @param {object} opts
7877
* @param {() => void} opts.onUpdate Called when a state update is available (via getState())
7978
* @param {Map<string, import('./peer-sync-controller.js').PeerSyncController>} opts.peerSyncControllers
80-
* @param {import('../core-manager/index.js').Namespace} opts.namespace
79+
* @param {Namespace} opts.namespace
8180
*/
8281
constructor({ onUpdate, peerSyncControllers, namespace }) {
8382
this.#peerSyncControllers = peerSyncControllers

src/sync/namespace-sync-state.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { CoreSyncState } from './core-sync-state.js'
22
import { discoveryKey } from 'hypercore-crypto'
3+
/** @import { Namespace } from '../types.js' */
34

45
/**
56
* @typedef {Omit<import('./core-sync-state.js').DerivedState, 'coreLength'> & { dataToSync: boolean, coreCount: number }} SyncState
67
*/
78

89
/**
9-
* @template {import('../core-manager/index.js').Namespace} [TNamespace=import('../core-manager/index.js').Namespace]
10+
* @template {Namespace} [TNamespace=Namespace]
1011
*/
1112
export class NamespaceSyncState {
1213
/** @type {Map<string, CoreSyncState>} */

src/sync/peer-sync-controller.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import mapObject from 'map-obj'
22
import { NAMESPACES } from '../constants.js'
33
import { Logger } from '../logger.js'
44
import { ExhaustivenessError, createMap } from '../utils.js'
5+
/** @import { Namespace } from '../types.js' */
56

6-
/**
7-
* @typedef {import('../core-manager/index.js').Namespace} Namespace
8-
*/
97
/**
108
* @typedef {import('../roles.js').Role['sync'][Namespace] | 'unknown'} SyncCapability
119
*/

src/sync/sync-state.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { NAMESPACES } from '../constants.js'
33
import { NamespaceSyncState } from './namespace-sync-state.js'
44
import { throttle } from 'throttle-debounce'
55
import mapObject from 'map-obj'
6+
/** @import { Namespace } from '../types.js' */
67

78
/**
89
* @typedef {Record<
9-
* import('../core-manager/index.js').Namespace,
10+
* Namespace,
1011
* import('./namespace-sync-state.js').SyncState
1112
* >} State
1213
*/
@@ -16,8 +17,7 @@ import mapObject from 'map-obj'
1617
* @extends {TypedEmitter<{ state: (state: State) => void}>}
1718
*/
1819
export class SyncState extends TypedEmitter {
19-
#syncStates =
20-
/** @type {Record<import('../core-manager/index.js').Namespace, NamespaceSyncState> } */ ({})
20+
#syncStates = /** @type {Record<Namespace, NamespaceSyncState> } */ ({})
2121
/**
2222
*
2323
* @param {object} opts

0 commit comments

Comments
 (0)