diff --git a/package.json b/package.json index 28f75b753a..63e21913d9 100644 --- a/package.json +++ b/package.json @@ -154,7 +154,7 @@ "scripts": { "clean": "aegir clean", "lint": "aegir lint", - "dep-check": "aegir dep-check", + "dep-check": "aegir dep-check -i protons", "test": "aegir test", "test:node": "aegir test -t node", "test:chrome": "aegir test -t browser", @@ -176,7 +176,7 @@ }, "devDependencies": { "@libp2p/crypto": "^1.0.11", - "aegir": "^37.9.1", + "aegir": "^38.1.7", "protons": "^6.0.0" } } diff --git a/src/index.ts b/src/index.ts index db154db2f8..d6e9ec3249 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,14 +23,14 @@ export class Libp2pRecord { this.timeReceived = timeReceived } - serialize () { + serialize (): Uint8Array { return Record.encode(this.prepareSerialize()) } /** * Return the object format ready to be given to the protobuf library. */ - prepareSerialize () { + prepareSerialize (): Record { return { key: this.key, value: this.value, @@ -41,7 +41,7 @@ export class Libp2pRecord { /** * Decode a protobuf encoded record */ - static deserialize (raw: Uint8Array | Uint8ArrayList) { + static deserialize (raw: Uint8Array | Uint8ArrayList): Libp2pRecord { const rec = Record.decode(raw) return new Libp2pRecord(rec.key, rec.value, new Date(rec.timeReceived)) @@ -50,7 +50,7 @@ export class Libp2pRecord { /** * Create a record from the raw object returned from the protobuf library */ - static fromDeserialized (obj: Record) { + static fromDeserialized (obj: Record): Libp2pRecord { const recvtime = utils.parseRFC3339(obj.timeReceived) if (obj.key == null) { diff --git a/src/selectors.ts b/src/selectors.ts index 3a3fe06d9f..ed28d0124d 100644 --- a/src/selectors.ts +++ b/src/selectors.ts @@ -5,7 +5,7 @@ import type { Selectors } from '@libp2p/interface-dht' /** * Select the best record out of the given records */ -export function bestRecord (selectors: Selectors, k: Uint8Array, records: Uint8Array[]) { +export function bestRecord (selectors: Selectors, k: Uint8Array, records: Uint8Array[]): number { if (records.length === 0) { const errMsg = 'No records given' @@ -41,7 +41,7 @@ export function bestRecord (selectors: Selectors, k: Uint8Array, records: Uint8A * Simply returns the first record, as all valid public key * records are equal */ -function publickKey (k: Uint8Array, records: Uint8Array[]) { +function publickKey (k: Uint8Array, records: Uint8Array[]): number { return 0 } diff --git a/src/utils.ts b/src/utils.ts index 34ce92763d..b9ee448887 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,7 +2,7 @@ * Convert a JavaScript date into an `RFC3339Nano` formatted * string */ -export function toRFC3339 (time: Date) { +export function toRFC3339 (time: Date): string { const year = time.getUTCFullYear() const month = String(time.getUTCMonth() + 1).padStart(2, '0') const day = String(time.getUTCDate()).padStart(2, '0') @@ -19,7 +19,7 @@ export function toRFC3339 (time: Date) { * Parses a date string formatted as `RFC3339Nano` into a * JavaScript Date object */ -export function parseRFC3339 (time: string) { +export function parseRFC3339 (time: string): Date { const rfc3339Matcher = new RegExp( // 2006-01-02T '(\\d{4})-(\\d{2})-(\\d{2})T' + diff --git a/src/validators.ts b/src/validators.ts index 298433f9e3..3296460103 100644 --- a/src/validators.ts +++ b/src/validators.ts @@ -10,7 +10,7 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals' * It runs the needed validators. * If verification fails the returned Promise will reject with the error. */ -export function verifyRecord (validators: Validators, record: Libp2pRecord) { +export async function verifyRecord (validators: Validators, record: Libp2pRecord): Promise { const key = record.key const keyString = uint8ArrayToString(key) const parts = keyString.split('/') @@ -28,7 +28,7 @@ export function verifyRecord (validators: Validators, record: Libp2pRecord) { throw new CodeError(errMsg, 'ERR_INVALID_RECORD_KEY_TYPE') } - return validator(key, record.value) + await validator(key, record.value) } /** @@ -40,7 +40,7 @@ export function verifyRecord (validators: Validators, record: Libp2pRecord) { * @param {Uint8Array} key - A valid key is of the form `'/pk/'` * @param {Uint8Array} publicKey - The public key to validate against (protobuf encoded). */ -const validatePublicKeyRecord = async (key: Uint8Array, publicKey: Uint8Array) => { +const validatePublicKeyRecord = async (key: Uint8Array, publicKey: Uint8Array): Promise => { if (!(key instanceof Uint8Array)) { throw new CodeError('"key" must be a Uint8Array', 'ERR_INVALID_RECORD_KEY_NOT_BUFFER') } diff --git a/test/validator.spec.ts b/test/validator.spec.ts index 3340bb5ede..e73ccc2645 100644 --- a/test/validator.spec.ts +++ b/test/validator.spec.ts @@ -62,7 +62,7 @@ describe('validator', () => { }) describe('verifyRecord', () => { - it('calls matching validator', () => { + it('calls matching validator', async () => { const k = uint8ArrayFromString('/hello/you') const rec = new Libp2pRecord(k, uint8ArrayFromString('world'), new Date()) @@ -72,10 +72,10 @@ describe('validator', () => { expect(value).to.eql(uint8ArrayFromString('world')) } } - return validator.verifyRecord(validators, rec) + await validator.verifyRecord(validators, rec) }) - it('calls not matching any validator', () => { + it('calls not matching any validator', async () => { const k = uint8ArrayFromString('/hallo/you') const rec = new Libp2pRecord(k, uint8ArrayFromString('world'), new Date()) @@ -85,11 +85,10 @@ describe('validator', () => { expect(value).to.eql(uint8ArrayFromString('world')) } } - return expect( - () => validator.verifyRecord(validators, rec) - ).to.throw( - /Invalid record keytype/ - ) + await expect(validator.verifyRecord(validators, rec)) + .to.eventually.rejectedWith( + /Invalid record keytype/ + ) }) }) @@ -107,7 +106,7 @@ describe('validator', () => { it('does not error on valid record', async () => { return await Promise.all(cases.valid.publicKey.map(async (k) => { - return await validator.validators.pk(k, key.public.bytes) + await validator.validators.pk(k, key.public.bytes) })) }) @@ -132,7 +131,7 @@ describe('validator', () => { const hash = await pubKey.hash() const k = Uint8Array.of(...uint8ArrayFromString('/pk/'), ...hash) - return await validator.validators.pk(k, pubKey.bytes) + await validator.validators.pk(k, pubKey.bytes) }) }) })