Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat: add types and update deps (#25)
Browse files Browse the repository at this point in the history
Adds types.
  • Loading branch information
achingbrain authored Feb 2, 2021
1 parent a6433e5 commit e2395de
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "aegir lint",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser -t webworker",
"build": "aegir build",
"prepare": "aegir build --no-bundle",
"docs": "aegir docs",
"release": "aegir release",
"release-minor": "aegir release --type minor",
Expand All @@ -36,18 +36,26 @@
"url": "https://github.com/libp2p/js-libp2p-record/issues"
},
"homepage": "https://github.com/libp2p/js-libp2p-record",
"files": [
"src",
"dist"
],
"eslintConfig": {
"extends": "ipfs"
},
"types": "dist/src/index.d.ts",
"devDependencies": {
"aegir": "^25.0.0",
"libp2p-crypto": "^0.18.0",
"aegir": "^30.3.0",
"libp2p-crypto": "^0.19.0",
"multibase": "^3.0.0",
"peer-id": "^0.14.0"
},
"dependencies": {
"err-code": "^2.0.0",
"err-code": "^3.0.0",
"multihashes": "^3.0.1",
"multihashing-async": "^2.0.1",
"multihashing-async": "^2.1.0",
"protons": "^2.0.0",
"uint8arrays": "^1.1.0"
"uint8arrays": "^2.0.5"
},
"contributors": [
"Vasco Santos <[email protected]>",
Expand Down
20 changes: 9 additions & 11 deletions src/record.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
'use strict'

// @ts-ignore
const protons = require('protons')
const pb = protons(require('./record.proto')).Record
const utils = require('./utils')

/**
* @typedef {{ key: Uint8Array, value: Uint8Array, timeReceived: string }} ProtobufRecord
*/

class Record {
/**
* @param {Uint8Array} [key]
* @param {Uint8Array} [value]
* @param {Date} [recvtime]
* @param {Date} [timeReceived]
*/
constructor (key, value, recvtime) {
constructor (key, value, timeReceived) {
if (!(key instanceof Uint8Array)) {
throw new Error('key must be a Uint8Array')
}
Expand All @@ -21,20 +26,15 @@ class Record {

this.key = key
this.value = value
this.timeReceived = recvtime
this.timeReceived = timeReceived
}

/**
* @returns {Uint8Array}
*/
serialize () {
return pb.encode(this.prepareSerialize())
}

/**
* Return the object format ready to be given to the protobuf library.
*
* @returns {Object}
*/
prepareSerialize () {
return {
Expand All @@ -48,7 +48,6 @@ class Record {
* Decode a protobuf encoded record.
*
* @param {Uint8Array} raw
* @returns {Record}
*/
static deserialize (raw) {
const dec = pb.decode(raw)
Expand All @@ -58,8 +57,7 @@ class Record {
/**
* Create a record from the raw object returned from the protobuf library.
*
* @param {Object} obj
* @returns {Record}
* @param {ProtobufRecord} obj
*/
static fromDeserialized (obj) {
let recvtime
Expand Down
3 changes: 1 addition & 2 deletions src/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const uint8ArrayToString = require('uint8arrays/to-string')
/**
* Select the best record out of the given records.
*
* @param {Object} selectors
* @param {{ [key: string]: function (Uint8Array, Uint8Array[]): number }} selectors
* @param {Uint8Array} k
* @param {Array<Uint8Array>} records
* @returns {number} - The index of the best record.
*/
const bestRecord = (selectors, k, records) => {
if (records.length === 0) {
Expand Down
1 change: 0 additions & 1 deletion src/selectors/public-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*
* @param {Uint8Array} k
* @param {Array<Uint8Array>} records
* @returns {number}
*/
const publicKeySelector = (k, records) => {
return 0
Expand Down
2 changes: 0 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* string.
*
* @param {Date} time
* @returns {string}
*/
module.exports.toRFC3339 = (time) => {
const year = time.getUTCFullYear()
Expand All @@ -25,7 +24,6 @@ module.exports.toRFC3339 = (time) => {
* JavaScript Date object.
*
* @param {string} time
* @returns {Date}
*/
module.exports.parseRFC3339 = (time) => {
const rfc3339Matcher = new RegExp(
Expand Down
7 changes: 5 additions & 2 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
const errcode = require('err-code')
const uint8ArrayToString = require('uint8arrays/to-string')

/**
* @typedef {import('./record')} Record
*/

/**
* Checks a record and ensures it is still valid.
* It runs the needed validators.
* If verification fails the returned Promise will reject with the error.
*
* @param {Object} validators
* @param {{ [key: string]: { func: (key: Uint8Array, value: Uint8Array) => Promise<void> }}} validators
* @param {Record} record
* @returns {Promise}
*/
const verifyRecord = (validators, record) => {
const key = record.key
Expand Down
1 change: 0 additions & 1 deletion src/validators/public-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const uint8ArrayEquals = require('uint8arrays/equals')
*
* @param {Uint8Array} key - A valid key is of the form `'/pk/<keymultihash>'`
* @param {Uint8Array} publicKey - The public key to validate against (protobuf encoded).
* @returns {Promise}
*/
const validatePublicKeyRecord = async (key, publicKey) => {
if (!(key instanceof Uint8Array)) {
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"test",
"src"
]
}

0 comments on commit e2395de

Please sign in to comment.