mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-06-13 02:01:35 +00:00
feat: add types (#74)
This commit is contained in:
@ -36,15 +36,30 @@ const fromString = require('uint8arrays/from-string')
|
||||
const ENVELOPE_DOMAIN_PEER_RECORD = 'libp2p-peer-record'
|
||||
const ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = fromString('0301', 'hex')
|
||||
|
||||
class PeerRecord extends Record {
|
||||
/**
|
||||
* @implements {import('libp2p-interfaces/src/record/types').Record}
|
||||
*/
|
||||
class PeerRecord {
|
||||
constructor (peerId, multiaddrs, seqNumber) {
|
||||
super (ENVELOPE_DOMAIN_PEER_RECORD, ENVELOPE_PAYLOAD_TYPE_PEER_RECORD)
|
||||
this.domain = ENVELOPE_DOMAIN_PEER_RECORD
|
||||
this.codec = ENVELOPE_PAYLOAD_TYPE_PEER_RECORD
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshal a record to be used in an envelope.
|
||||
*
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
marshal () {
|
||||
// Implement and return using Protobuf
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if `this` record equals the `other`.
|
||||
*
|
||||
* @param {PeerRecord} other
|
||||
* @returns {other is Record}
|
||||
*/
|
||||
equals (other) {
|
||||
// Verify
|
||||
}
|
||||
@ -73,4 +88,4 @@ Verifies if the other Record is identical to this one.
|
||||
- other is a `Record` to compare with the current instance.
|
||||
|
||||
**Returns**
|
||||
- `boolean`
|
||||
- `other is Record`
|
||||
|
23
src/record/index.d.ts
vendored
23
src/record/index.d.ts
vendored
@ -1,23 +0,0 @@
|
||||
export = Record;
|
||||
/**
|
||||
* Record is the base implementation of a record that can be used as the payload of a libp2p envelope.
|
||||
*/
|
||||
declare class Record {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {String} domain signature domain
|
||||
* @param {Uint8Array} codec identifier of the type of record
|
||||
*/
|
||||
constructor(domain: string, codec: Uint8Array);
|
||||
domain: string;
|
||||
codec: Uint8Array;
|
||||
/**
|
||||
* Marshal a record to be used in an envelope.
|
||||
*/
|
||||
marshal(): void;
|
||||
/**
|
||||
* Verifies if the other provided Record is identical to this one.
|
||||
* @param {Record} other
|
||||
*/
|
||||
equals(other: Record): void;
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const errcode = require('err-code')
|
||||
|
||||
/**
|
||||
* Record is the base implementation of a record that can be used as the payload of a libp2p envelope.
|
||||
*/
|
||||
class Record {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {String} domain signature domain
|
||||
* @param {Uint8Array} codec identifier of the type of record
|
||||
*/
|
||||
constructor (domain, codec) {
|
||||
this.domain = domain
|
||||
this.codec = codec
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshal a record to be used in an envelope.
|
||||
*/
|
||||
marshal () {
|
||||
throw errcode(new Error('marshal must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if the other provided Record is identical to this one.
|
||||
* @param {Record} other
|
||||
*/
|
||||
equals (other) {
|
||||
throw errcode(new Error('equals must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Record
|
21
src/record/types.ts
Normal file
21
src/record/types.ts
Normal file
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Record is the base implementation of a record that can be used as the payload of a libp2p envelope.
|
||||
*/
|
||||
export interface Record {
|
||||
/**
|
||||
* signature domain.
|
||||
*/
|
||||
domain: string;
|
||||
/**
|
||||
* identifier of the type of record
|
||||
*/
|
||||
codec: Uint8Array;
|
||||
/**
|
||||
* Marshal a record to be used in an envelope.
|
||||
*/
|
||||
marshal(): Uint8Array;
|
||||
/**
|
||||
* Verifies if the other provided Record is identical to this one.
|
||||
*/
|
||||
equals(other: unknown): boolean
|
||||
}
|
Reference in New Issue
Block a user