Skip to content

Commit

Permalink
chore: getVersionId should throw on invalid input (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn authored Sep 16, 2024
1 parent 149845f commit 01d8f9f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export function getOwn<T extends object, K extends keyof T>(
return Object.hasOwn(obj, key) ? obj[key] : undefined
}

export function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message)
}

export class ExhaustivenessError extends Error {
constructor(value: never) {
super(`Exhaustiveness check failed. ${value} should be impossible`)
Expand Down Expand Up @@ -46,6 +50,14 @@ export type VersionIdObject = {
* @returns versionId string
*/
export function getVersionId({ coreDiscoveryKey, index }: VersionIdObject) {
assert(
coreDiscoveryKey.byteLength >= 32,
'version ID core discovery key must be have at least 32 bytes'
)
assert(
Number.isSafeInteger(index) && index >= 0,
'version ID index must be a non-negative integer'
)
return coreDiscoveryKey.toString('hex') + '/' + index
}

Expand Down
51 changes: 50 additions & 1 deletion test/lib/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// @ts-check
import assert from 'node:assert/strict'
import test from 'node:test'
import { ExhaustivenessError, getOwn } from '../../dist/lib/utils.js'
import {
ExhaustivenessError,
getOwn,
getVersionId,
} from '../../dist/lib/utils.js'

test('getOwn', () => {
class Foo {
Expand Down Expand Up @@ -38,3 +42,48 @@ test('ExhaustivenessError', () => {
}
})
})

test('getVersionId', () => {
const coreDiscoveryKeyHex =
'1f1c774ac1041092c5d8334316919f43fc9e4db48b2074a2d9d7ecf6df7a1181'
const valid = {
coreDiscoveryKey: Buffer.from(coreDiscoveryKeyHex, 'hex'),
index: 123,
}

assert.equal(
getVersionId(valid),
coreDiscoveryKeyHex + '/' + 123,
'serializing version ID'
)

assert.throws(
() => getVersionId({ ...valid, index: -1 }),
'throws when index is negative'
)
assert.throws(
() => getVersionId({ ...valid, index: 1.2 }),
'throws when index is not an integer'
)
assert.throws(
() => getVersionId({ ...valid, index: NaN }),
'throws when index is NaN'
)
assert.throws(
() => getVersionId({ ...valid, index: Infinity }),
'throws when index is Infinity'
)

assert.throws(
() => getVersionId({ ...valid, coreDiscoveryKey: Buffer.alloc(0) }),
'throws when core discovery key is empty'
)
assert.throws(
() =>
getVersionId({
...valid,
coreDiscoveryKey: valid.coreDiscoveryKey.subarray(0, 31),
}),
'throws when core discovery key is too short'
)
})

0 comments on commit 01d8f9f

Please sign in to comment.