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: validate role records #967

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
"multi-core-indexer": "^1.0.0",
"p-defer": "^4.0.0",
"p-event": "^6.0.1",
"p-reduce": "^3.0.0",
"p-timeout": "^6.1.2",
"protobufjs": "^7.2.3",
"protomux": "^3.4.1",
Expand Down
9 changes: 9 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ export class NotFoundError extends Error {
super(message)
}
}

/**
* @param {unknown} err
* @returns {null}
*/
export function nullIfNotFound(err) {
if (err instanceof NotFoundError) return null
throw err
}
16 changes: 16 additions & 0 deletions src/lib/ponyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ export function abortSignalAny(iterable) {

return controller.signal
}

/**
* Ponyfill of `Set.prototype.isSubsetOf()`.
*
* [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/isSubsetOf
*
* @param {ReadonlySet<unknown>} me
* @param {ReadonlySet<unknown>} other
* @returns {boolean}
*/
export function setIsSubsetOf(me, other) {
for (const value of me) {
if (!other.has(value)) return false
}
return true
}
14 changes: 7 additions & 7 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import { Logger } from './logger.js'
import { IconApi } from './icon-api.js'
import { readConfig } from './config-import.js'
import TranslationApi from './translation-api.js'
import { NotFoundError } from './errors.js'
import { NotFoundError, nullIfNotFound } from './errors.js'
import { getErrorCode, getErrorMessage } from './lib/error.js'
/** @import { ProjectSettingsValue } from '@comapeo/schema' */
/** @import { CoreStorage, BlobFilter, BlobStoreEntriesStream, KeyPair, Namespace, ReplicationStream } from './types.js' */
Expand Down Expand Up @@ -663,9 +663,9 @@ export class MapeoProject extends TypedEmitter {
async $setProjectSettings(settings) {
const { projectSettings } = this.#dataTypes

const existing = await projectSettings.getByDocId(this.#projectId, {
mustBeFound: false,
})
const existing = await projectSettings
.getByDocId(this.#projectId)
.catch(nullIfNotFound)

if (existing) {
return extractEditableProjectSettings(
Expand Down Expand Up @@ -773,9 +773,9 @@ export class MapeoProject extends TypedEmitter {
schemaName: /** @type {const} */ ('deviceInfo'),
}

const existingDoc = await deviceInfo.getByDocId(configCoreId, {
mustBeFound: false,
})
const existingDoc = await deviceInfo
.getByDocId(configCoreId)
.catch(nullIfNotFound)
if (existingDoc) {
return await deviceInfo.update(existingDoc.versionId, doc)
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,12 @@ export class MemberApi extends TypedEmitter {
/**
* @param {string} deviceId
* @param {import('./roles.js').RoleIdAssignableToOthers} roleId
* @param {object} [options]
* @param {boolean} [options.__testOnlyAllowAnyRoleToBeAssigned]
* @returns {Promise<void>}
*/
async assignRole(deviceId, roleId) {
return this.#roles.assignRole(deviceId, roleId)
async assignRole(deviceId, roleId, options) {
return this.#roles.assignRole(deviceId, roleId, options)
}
}

Expand Down
Loading