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

Commit

Permalink
fix: use protons (#39)
Browse files Browse the repository at this point in the history
Also updates aegir and removes boilerplate config.
  • Loading branch information
achingbrain authored Apr 9, 2022
1 parent c703e81 commit 10b4cc2
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 321 deletions.
31 changes: 15 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,29 @@
]
},
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"pretest": "npm run build",
"test": "aegir test -f ./dist/test",
"test:node": "npm run test -- -t node",
"test:chrome": "npm run test -- -t browser",
"test:chrome-webworker": "npm run test -- -t webworker",
"test:firefox": "npm run test -- -t browser -- --browser firefox",
"test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
"build": "tsc",
"postbuild": "npm run build:copy-proto-files",
"generate:proto": "pbjs -t static-module -w es6 -r libp2p-record --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/record/record.js ./src/record/record.proto",
"generate:proto-types": "pbts -o src/record/record.d.ts src/record/record.js",
"build:copy-proto-files": "cp src/record* dist/src",
"release": "semantic-release"
"dep-check": "aegir dep-check",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:chrome": "aegir test -t browser",
"test:chrome-webworker": "aegir test -t webworker",
"test:firefox": "aegir test -t browser -- --browser firefox",
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
"build": "aegir build",
"generate": "protons ./src/record.proto",
"release": "aegir release"
},
"dependencies": {
"err-code": "^3.0.1",
"multiformats": "^9.4.5",
"protobufjs": "^6.11.2",
"protons-runtime": "^1.0.2",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@libp2p/crypto": "^0.22.9",
"@libp2p/interfaces": "^1.3.18",
"aegir": "^36.1.3"
"@libp2p/interfaces": "^1.3.20",
"aegir": "^37.0.10",
"protons": "^3.0.2"
}
}
28 changes: 10 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {
IRecord,
Record as PBRecord
Record
} from './record.js'
import * as utils from './utils.js'

export class Libp2pRecord {
public key: Uint8Array
public value: Uint8Array
public timeReceived?: Date
public timeReceived: Date

constructor (key: Uint8Array, value: Uint8Array, timeReceived?: Date) {
constructor (key: Uint8Array, value: Uint8Array, timeReceived: Date) {
if (!(key instanceof Uint8Array)) {
throw new Error('key must be a Uint8Array')
}
Expand All @@ -24,7 +23,7 @@ export class Libp2pRecord {
}

serialize () {
return PBRecord.encode(this.prepareSerialize()).finish()
return Record.encode(this.prepareSerialize())
}

/**
Expand All @@ -34,31 +33,24 @@ export class Libp2pRecord {
return {
key: this.key,
value: this.value,
timeReceived: this.timeReceived != null ? utils.toRFC3339(this.timeReceived) : undefined
timeReceived: utils.toRFC3339(this.timeReceived)
}
}

/**
* Decode a protobuf encoded record
*/
static deserialize (raw: Uint8Array) {
const message = PBRecord.decode(raw)
return Libp2pRecord.fromDeserialized(PBRecord.toObject(message, {
defaults: false,
arrays: true,
longs: Number,
objects: false
}))
const rec = Record.decode(raw)

return new Libp2pRecord(rec.key, rec.value, new Date(rec.timeReceived))
}

/**
* Create a record from the raw object returned from the protobuf library
*/
static fromDeserialized (obj: IRecord) {
let recvtime
if (obj.timeReceived != null) {
recvtime = utils.parseRFC3339(obj.timeReceived)
}
static fromDeserialized (obj: Record) {
const recvtime = utils.parseRFC3339(obj.timeReceived)

if (obj.key == null) {
throw new Error('key missing from deserialized object')
Expand Down
71 changes: 0 additions & 71 deletions src/record.d.ts

This file was deleted.

202 changes: 0 additions & 202 deletions src/record.js

This file was deleted.

Loading

0 comments on commit 10b4cc2

Please sign in to comment.