Skip to content

Commit

Permalink
chore: remove usage of opaque ids types related to project ids (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Sep 26, 2023
1 parent 3eca4b2 commit 5f1c4e8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/ipc-wrapper/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const CLOSE = Symbol('close')
* @returns {import('rpc-reflector/client.js').ClientApi<import('../mapeo-manager.js').MapeoManager>}
*/
export function createMapeoClient(messagePort) {
/** @type {Map<import('../types.js').ProjectPublicId, Promise<import('rpc-reflector/client.js').ClientApi<import('../mapeo-project.js').MapeoProject>>>} */
/** @type {Map<string, Promise<import('rpc-reflector/client.js').ClientApi<import('../mapeo-project.js').MapeoProject>>>} */
const projectClientPromises = new Map()

const managerChannel = new SubChannel(messagePort, MANAGER_CHANNEL_ID)
Expand Down Expand Up @@ -54,7 +54,7 @@ export function createMapeoClient(messagePort) {
return client

/**
* @param {import('../types.js').ProjectPublicId} projectPublicId
* @param {string} projectPublicId
* @returns {Promise<import('rpc-reflector/client.js').ClientApi<import('../mapeo-project.js').MapeoProject>>}
*/
async function createProjectClient(projectPublicId) {
Expand Down
8 changes: 2 additions & 6 deletions src/ipc-wrapper/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ export function createMapeoServer(manager, messagePort) {

let project
try {
project = await manager.getProject(
/** @type {import('../types.js').ProjectPublicId} */ (id)
)
project = await manager.getProject(id)
} catch (err) {
// TODO: how to respond to client so that method errors?
projectChannel.close()
Expand Down Expand Up @@ -106,9 +104,7 @@ export class MapeoRpcApi {
* @returns {Promise<boolean>}
*/
async assertProjectExists(projectId) {
const project = await this.#manager.getProject(
/** @type {import('../types.js').ProjectPublicId} */ (projectId)
)
const project = await this.#manager.getProject(projectId)
return !!project
}
}
29 changes: 13 additions & 16 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import { RandomAccessFilePool } from './core-manager/random-access-file-pool.js'
import { MapeoRPC } from './rpc/index.js'

/** @typedef {import("@mapeo/schema").ProjectSettingsValue} ProjectValue */
/** @typedef {import('./types.js').ProjectId} ProjectId */
/** @typedef {import('./types.js').ProjectPublicId} ProjectPublicId */

const CLIENT_SQLITE_FILE_NAME = 'client.db'

Expand All @@ -39,7 +37,8 @@ export class MapeoManager {
#keyManager
#projectSettingsIndexWriter
#db
/** @type {Map<ProjectPublicId, MapeoProject>} */
// Maps project public id -> project instance
/** @type {Map<string, MapeoProject>} */
#activeProjects
/** @type {import('./types.js').CoreStorage} */
#coreStorage
Expand Down Expand Up @@ -85,7 +84,7 @@ export class MapeoManager {

/**
* @param {Buffer} keysCipher
* @param {ProjectId} projectId
* @param {string} projectId
* @returns {ProjectKeys}
*/
#decodeProjectKeysCipher(keysCipher, projectId) {
Expand All @@ -96,7 +95,7 @@ export class MapeoManager {
}

/**
* @param {ProjectId} projectId
* @param {string} projectId
* @returns {Pick<ConstructorParameters<typeof MapeoProject>[0], 'dbPath' | 'coreStorage'>}
*/
#projectStorage(projectId) {
Expand All @@ -111,8 +110,8 @@ export class MapeoManager {

/**
* @param {Object} opts
* @param {ProjectId} opts.projectId
* @param {ProjectPublicId} opts.projectPublicId
* @param {string} opts.projectId
* @param {string} opts.projectPublicId
* @param {ProjectKeys} opts.projectKeys
* @param {import('./generated/rpc.js').Invite_ProjectInfo} [opts.projectInfo]
*/
Expand Down Expand Up @@ -142,7 +141,7 @@ export class MapeoManager {
/**
* Create a new project.
* @param {import('type-fest').Simplify<Partial<Pick<ProjectValue, 'name'>>>} [settings]
* @returns {Promise<ProjectPublicId>}
* @returns {Promise<string>} Project public id
*/
async createProject(settings = {}) {
// 1. Create project keypair
Expand Down Expand Up @@ -198,7 +197,7 @@ export class MapeoManager {
}

/**
* @param {ProjectPublicId} projectPublicId
* @param {string} projectPublicId
* @returns {Promise<MapeoProject>}
*/
async getProject(projectPublicId) {
Expand All @@ -221,9 +220,7 @@ export class MapeoManager {
throw new Error(`NotFound: project ID ${projectPublicId} not found`)
}

const projectId = /** @type {ProjectId} */ (
projectKeysTableResult.projectId
)
const { projectId } = projectKeysTableResult

const projectKeys = this.#decodeProjectKeysCipher(
projectKeysTableResult.keysCipher,
Expand All @@ -246,7 +243,7 @@ export class MapeoManager {
}

/**
* @returns {Promise<Array<Pick<ProjectValue, 'name'> & { projectId: ProjectPublicId, createdAt?: string, updatedAt?: string}>>}
* @returns {Promise<Array<Pick<ProjectValue, 'name'> & { projectId: string, createdAt?: string, updatedAt?: string}>>}
*/
async listProjects() {
// We use the project keys table as the source of truth for projects that exist
Expand All @@ -271,7 +268,7 @@ export class MapeoManager {
.from(projectSettingsTable)
.all()

/** @type {Array<Pick<ProjectValue, 'name'> & { projectId: ProjectPublicId, createdAt?: string, updatedAt?: string, createdBy?: string }>} */
/** @type {Array<Pick<ProjectValue, 'name'> & { projectId: string, createdAt?: string, updatedAt?: string, createdBy?: string }>} */
const result = []

for (const {
Expand All @@ -285,7 +282,7 @@ export class MapeoManager {

result.push(
deNullify({
projectId: /** @type {ProjectPublicId} */ (projectPublicId),
projectId: projectPublicId,
createdAt: existingProject?.createdAt,
updatedAt: existingProject?.updatedAt,
name: existingProject?.name || projectInfo.name,
Expand All @@ -298,7 +295,7 @@ export class MapeoManager {

/**
* @param {import('./generated/rpc.js').Invite} invite
* @returns {Promise<ProjectPublicId>}
* @returns {Promise<string>}
*/
async addProject({ projectKey, encryptionKeys, projectInfo }) {
const projectPublicId = projectKeyToPublicId(projectKey)
Expand Down
5 changes: 0 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
ValueOf,
RequireAtLeastOne,
SetOptional,
Opaque,
} from 'type-fest'
import { SUPPORTED_BLOB_VARIANTS } from './blob-store/index.js'
import { MapeoCommon, MapeoDoc, MapeoValue, decode } from '@mapeo/schema'
Expand Down Expand Up @@ -143,10 +142,6 @@ export type TopicKey = Buffer
export type TopicId = string
/** 52 character base32 encoding of `Topic` Buffer */
export type MdnsTopicId = string
/** hex string representation of project key buffer */
export type ProjectId = Opaque<string, 'ProjectId'>
/** z32-encoded hash of project key */
export type ProjectPublicId = Opaque<string, 'ProjectPublicId'>

// TODO: Figure out where those extra fields come from and find more elegant way to represent this
export type RawDhtConnectionStream = Duplex & {
Expand Down
14 changes: 5 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,23 @@ export function valueOf(doc) {
/**
* Create an internal ID from a project key
* @param {Buffer} projectKey
* @returns {import('./types.js').ProjectId}
* @returns {string}
*/
export function projectKeyToId(projectKey) {
return /** @type {import('./types.js').ProjectId} */ (
projectKey.toString('hex')
)
return projectKey.toString('hex')
}

/**
* Create a public ID from a project key
* @param {Buffer} projectKey
* @returns {import('./types.js').ProjectPublicId}
* @returns {string}
*/
export function projectKeyToPublicId(projectKey) {
return /** @type {import('./types.js').ProjectPublicId} */ (
keyToPublicId(projectKey)
)
return keyToPublicId(projectKey)
}

/**
* @param {import('./types.js').ProjectId} projectId
* @param {string} projectId Project internal ID
* @returns {Buffer} 24-byte nonce (same length as sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)
*/
export function projectIdToNonce(projectId) {
Expand Down
3 changes: 0 additions & 3 deletions test-e2e/ipc-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,11 @@ test('Attempting to get non-existent project fails', async (t) => {
const { client, cleanup } = setup()

await t.exception(async () => {
// @ts-expect-error
await client.getProject('mapeo')
})

const results = await Promise.allSettled([
// @ts-expect-error
client.getProject('mapeo'),
// @ts-expect-error
client.getProject('mapeo'),
])

Expand Down

0 comments on commit 5f1c4e8

Please sign in to comment.