@libp2p/record 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- import { IRecord } from './record.js';
1
+ import { Record } from './record.js';
2
2
  export declare class Libp2pRecord {
3
3
  key: Uint8Array;
4
4
  value: Uint8Array;
5
- timeReceived?: Date;
6
- constructor(key: Uint8Array, value: Uint8Array, timeReceived?: Date);
5
+ timeReceived: Date;
6
+ constructor(key: Uint8Array, value: Uint8Array, timeReceived: Date);
7
7
  serialize(): Uint8Array;
8
8
  /**
9
9
  * Return the object format ready to be given to the protobuf library.
@@ -11,7 +11,7 @@ export declare class Libp2pRecord {
11
11
  prepareSerialize(): {
12
12
  key: Uint8Array;
13
13
  value: Uint8Array;
14
- timeReceived: string | undefined;
14
+ timeReceived: string;
15
15
  };
16
16
  /**
17
17
  * Decode a protobuf encoded record
@@ -20,6 +20,6 @@ export declare class Libp2pRecord {
20
20
  /**
21
21
  * Create a record from the raw object returned from the protobuf library
22
22
  */
23
- static fromDeserialized(obj: IRecord): Libp2pRecord;
23
+ static fromDeserialized(obj: Record): Libp2pRecord;
24
24
  }
25
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EAER,MAAM,aAAa,CAAA;AAGpB,qBAAa,YAAY;IAChB,GAAG,EAAE,UAAU,CAAA;IACf,KAAK,EAAE,UAAU,CAAA;IACjB,YAAY,CAAC,EAAE,IAAI,CAAA;gBAEb,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,CAAC,EAAE,IAAI;IAcpE,SAAS;IAIT;;OAEG;IACH,gBAAgB;;;;;IAQhB;;OAEG;IACH,MAAM,CAAC,WAAW,CAAE,GAAG,EAAE,UAAU;IAUnC;;OAEG;IACH,MAAM,CAAC,gBAAgB,CAAE,GAAG,EAAE,OAAO;CAoBtC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACP,MAAM,aAAa,CAAA;AAGpB,qBAAa,YAAY;IAChB,GAAG,EAAE,UAAU,CAAA;IACf,KAAK,EAAE,UAAU,CAAA;IACjB,YAAY,EAAE,IAAI,CAAA;gBAEZ,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,IAAI;IAcnE,SAAS;IAIT;;OAEG;IACH,gBAAgB;;;;;IAQhB;;OAEG;IACH,MAAM,CAAC,WAAW,CAAE,GAAG,EAAE,UAAU;IAMnC;;OAEG;IACH,MAAM,CAAC,gBAAgB,CAAE,GAAG,EAAE,MAAM;CAiBrC"}
package/dist/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Record as PBRecord } from './record.js';
1
+ import { Record } from './record.js';
2
2
  import * as utils from './utils.js';
3
3
  export class Libp2pRecord {
4
4
  constructor(key, value, timeReceived) {
@@ -13,7 +13,7 @@ export class Libp2pRecord {
13
13
  this.timeReceived = timeReceived;
14
14
  }
15
15
  serialize() {
16
- return PBRecord.encode(this.prepareSerialize()).finish();
16
+ return Record.encode(this.prepareSerialize());
17
17
  }
18
18
  /**
19
19
  * Return the object format ready to be given to the protobuf library.
@@ -22,29 +22,21 @@ export class Libp2pRecord {
22
22
  return {
23
23
  key: this.key,
24
24
  value: this.value,
25
- timeReceived: this.timeReceived != null ? utils.toRFC3339(this.timeReceived) : undefined
25
+ timeReceived: utils.toRFC3339(this.timeReceived)
26
26
  };
27
27
  }
28
28
  /**
29
29
  * Decode a protobuf encoded record
30
30
  */
31
31
  static deserialize(raw) {
32
- const message = PBRecord.decode(raw);
33
- return Libp2pRecord.fromDeserialized(PBRecord.toObject(message, {
34
- defaults: false,
35
- arrays: true,
36
- longs: Number,
37
- objects: false
38
- }));
32
+ const rec = Record.decode(raw);
33
+ return new Libp2pRecord(rec.key, rec.value, new Date(rec.timeReceived));
39
34
  }
40
35
  /**
41
36
  * Create a record from the raw object returned from the protobuf library
42
37
  */
43
38
  static fromDeserialized(obj) {
44
- let recvtime;
45
- if (obj.timeReceived != null) {
46
- recvtime = utils.parseRFC3339(obj.timeReceived);
47
- }
39
+ const recvtime = utils.parseRFC3339(obj.timeReceived);
48
40
  if (obj.key == null) {
49
41
  throw new Error('key missing from deserialized object');
50
42
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,IAAI,QAAQ,EACnB,MAAM,aAAa,CAAA;AACpB,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AAEnC,MAAM,OAAO,YAAY;IAKvB,YAAa,GAAe,EAAE,KAAiB,EAAE,YAAmB;QAClE,IAAI,CAAC,CAAC,GAAG,YAAY,UAAU,CAAC,EAAE;YAChC,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;SAC5C;QAED,IAAI,CAAC,CAAC,KAAK,YAAY,UAAU,CAAC,EAAE;YAClC,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;SAC9C;QAED,IAAI,CAAC,GAAG,GAAG,GAAG,CAAA;QACd,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAA;IAClC,CAAC;IAED,SAAS;QACP,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAA;IAC1D,CAAC;IAED;;OAEG;IACH,gBAAgB;QACd,OAAO;YACL,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,YAAY,EAAE,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,YAAY,CAA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EAAE,GAAG,CAAC,KAAK,EAAE,QAAQ,CAC7B,CAAA;QAED,OAAO,GAAG,CAAA;IACZ,CAAC;CACF"}
@@ -1,71 +1,11 @@
1
- import * as $protobuf from "protobufjs";
2
- /** Properties of a Record. */
3
- export interface IRecord {
4
-
5
- /** Record key */
6
- key?: (Uint8Array|null);
7
-
8
- /** Record value */
9
- value?: (Uint8Array|null);
10
-
11
- /** Record timeReceived */
12
- timeReceived?: (string|null);
1
+ export interface Record {
2
+ key: Uint8Array;
3
+ value: Uint8Array;
4
+ timeReceived: string;
13
5
  }
14
-
15
- /** Represents a Record. */
16
- export class Record implements IRecord {
17
-
18
- /**
19
- * Constructs a new Record.
20
- * @param [p] Properties to set
21
- */
22
- constructor(p?: IRecord);
23
-
24
- /** Record key. */
25
- public key: Uint8Array;
26
-
27
- /** Record value. */
28
- public value: Uint8Array;
29
-
30
- /** Record timeReceived. */
31
- public timeReceived: string;
32
-
33
- /**
34
- * Encodes the specified Record message. Does not implicitly {@link Record.verify|verify} messages.
35
- * @param m Record message or plain object to encode
36
- * @param [w] Writer to encode to
37
- * @returns Writer
38
- */
39
- public static encode(m: IRecord, w?: $protobuf.Writer): $protobuf.Writer;
40
-
41
- /**
42
- * Decodes a Record message from the specified reader or buffer.
43
- * @param r Reader or buffer to decode from
44
- * @param [l] Message length if known beforehand
45
- * @returns Record
46
- * @throws {Error} If the payload is not a reader or valid buffer
47
- * @throws {$protobuf.util.ProtocolError} If required fields are missing
48
- */
49
- public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Record;
50
-
51
- /**
52
- * Creates a Record message from a plain object. Also converts values to their respective internal types.
53
- * @param d Plain object
54
- * @returns Record
55
- */
56
- public static fromObject(d: { [k: string]: any }): Record;
57
-
58
- /**
59
- * Creates a plain object from a Record message. Also converts values to other types if specified.
60
- * @param m Record
61
- * @param [o] Conversion options
62
- * @returns Plain object
63
- */
64
- public static toObject(m: Record, o?: $protobuf.IConversionOptions): { [k: string]: any };
65
-
66
- /**
67
- * Converts this Record to JSON.
68
- * @returns JSON object
69
- */
70
- public toJSON(): { [k: string]: any };
6
+ export declare namespace Record {
7
+ const codec: () => import("protons-runtime/dist/src/codecs/codec").Codec<Record>;
8
+ const encode: (obj: Record) => Uint8Array;
9
+ const decode: (buf: Uint8Array) => Record;
71
10
  }
11
+ //# sourceMappingURL=record.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../../src/record.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,UAAU,CAAA;IACf,KAAK,EAAE,UAAU,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;CACrB;AAED,yBAAiB,MAAM,CAAC;IACf,MAAM,KAAK,qEAMjB,CAAA;IAEM,MAAM,MAAM,QAAS,MAAM,KAAG,UAEpC,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,KAAG,MAExC,CAAA;CACF"}
@@ -1,202 +1,20 @@
1
- /*eslint-disable*/
2
- import $protobuf from "protobufjs/minimal.js";
3
-
4
- // Common aliases
5
- const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
6
-
7
- // Exported root namespace
8
- const $root = $protobuf.roots["libp2p-record"] || ($protobuf.roots["libp2p-record"] = {});
9
-
10
- export const Record = $root.Record = (() => {
11
-
12
- /**
13
- * Properties of a Record.
14
- * @exports IRecord
15
- * @interface IRecord
16
- * @property {Uint8Array|null} [key] Record key
17
- * @property {Uint8Array|null} [value] Record value
18
- * @property {string|null} [timeReceived] Record timeReceived
19
- */
20
-
21
- /**
22
- * Constructs a new Record.
23
- * @exports Record
24
- * @classdesc Represents a Record.
25
- * @implements IRecord
26
- * @constructor
27
- * @param {IRecord=} [p] Properties to set
28
- */
29
- function Record(p) {
30
- if (p)
31
- for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
32
- if (p[ks[i]] != null)
33
- this[ks[i]] = p[ks[i]];
34
- }
35
-
36
- /**
37
- * Record key.
38
- * @member {Uint8Array} key
39
- * @memberof Record
40
- * @instance
41
- */
42
- Record.prototype.key = $util.newBuffer([]);
43
-
44
- /**
45
- * Record value.
46
- * @member {Uint8Array} value
47
- * @memberof Record
48
- * @instance
49
- */
50
- Record.prototype.value = $util.newBuffer([]);
51
-
52
- /**
53
- * Record timeReceived.
54
- * @member {string} timeReceived
55
- * @memberof Record
56
- * @instance
57
- */
58
- Record.prototype.timeReceived = "";
59
-
60
- /**
61
- * Encodes the specified Record message. Does not implicitly {@link Record.verify|verify} messages.
62
- * @function encode
63
- * @memberof Record
64
- * @static
65
- * @param {IRecord} m Record message or plain object to encode
66
- * @param {$protobuf.Writer} [w] Writer to encode to
67
- * @returns {$protobuf.Writer} Writer
68
- */
69
- Record.encode = function encode(m, w) {
70
- if (!w)
71
- w = $Writer.create();
72
- if (m.key != null && Object.hasOwnProperty.call(m, "key"))
73
- w.uint32(10).bytes(m.key);
74
- if (m.value != null && Object.hasOwnProperty.call(m, "value"))
75
- w.uint32(18).bytes(m.value);
76
- if (m.timeReceived != null && Object.hasOwnProperty.call(m, "timeReceived"))
77
- w.uint32(42).string(m.timeReceived);
78
- return w;
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+ import { encodeMessage, decodeMessage, message, bytes, string } from 'protons-runtime';
4
+ export var Record;
5
+ (function (Record) {
6
+ Record.codec = () => {
7
+ return message({
8
+ 1: { name: 'key', codec: bytes },
9
+ 2: { name: 'value', codec: bytes },
10
+ 5: { name: 'timeReceived', codec: string }
11
+ });
79
12
  };
80
-
81
- /**
82
- * Decodes a Record message from the specified reader or buffer.
83
- * @function decode
84
- * @memberof Record
85
- * @static
86
- * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
87
- * @param {number} [l] Message length if known beforehand
88
- * @returns {Record} Record
89
- * @throws {Error} If the payload is not a reader or valid buffer
90
- * @throws {$protobuf.util.ProtocolError} If required fields are missing
91
- */
92
- Record.decode = function decode(r, l) {
93
- if (!(r instanceof $Reader))
94
- r = $Reader.create(r);
95
- var c = l === undefined ? r.len : r.pos + l, m = new $root.Record();
96
- while (r.pos < c) {
97
- var t = r.uint32();
98
- switch (t >>> 3) {
99
- case 1:
100
- m.key = r.bytes();
101
- break;
102
- case 2:
103
- m.value = r.bytes();
104
- break;
105
- case 5:
106
- m.timeReceived = r.string();
107
- break;
108
- default:
109
- r.skipType(t & 7);
110
- break;
111
- }
112
- }
113
- return m;
13
+ Record.encode = (obj) => {
14
+ return encodeMessage(obj, Record.codec());
114
15
  };
115
-
116
- /**
117
- * Creates a Record message from a plain object. Also converts values to their respective internal types.
118
- * @function fromObject
119
- * @memberof Record
120
- * @static
121
- * @param {Object.<string,*>} d Plain object
122
- * @returns {Record} Record
123
- */
124
- Record.fromObject = function fromObject(d) {
125
- if (d instanceof $root.Record)
126
- return d;
127
- var m = new $root.Record();
128
- if (d.key != null) {
129
- if (typeof d.key === "string")
130
- $util.base64.decode(d.key, m.key = $util.newBuffer($util.base64.length(d.key)), 0);
131
- else if (d.key.length)
132
- m.key = d.key;
133
- }
134
- if (d.value != null) {
135
- if (typeof d.value === "string")
136
- $util.base64.decode(d.value, m.value = $util.newBuffer($util.base64.length(d.value)), 0);
137
- else if (d.value.length)
138
- m.value = d.value;
139
- }
140
- if (d.timeReceived != null) {
141
- m.timeReceived = String(d.timeReceived);
142
- }
143
- return m;
16
+ Record.decode = (buf) => {
17
+ return decodeMessage(buf, Record.codec());
144
18
  };
145
-
146
- /**
147
- * Creates a plain object from a Record message. Also converts values to other types if specified.
148
- * @function toObject
149
- * @memberof Record
150
- * @static
151
- * @param {Record} m Record
152
- * @param {$protobuf.IConversionOptions} [o] Conversion options
153
- * @returns {Object.<string,*>} Plain object
154
- */
155
- Record.toObject = function toObject(m, o) {
156
- if (!o)
157
- o = {};
158
- var d = {};
159
- if (o.defaults) {
160
- if (o.bytes === String)
161
- d.key = "";
162
- else {
163
- d.key = [];
164
- if (o.bytes !== Array)
165
- d.key = $util.newBuffer(d.key);
166
- }
167
- if (o.bytes === String)
168
- d.value = "";
169
- else {
170
- d.value = [];
171
- if (o.bytes !== Array)
172
- d.value = $util.newBuffer(d.value);
173
- }
174
- d.timeReceived = "";
175
- }
176
- if (m.key != null && m.hasOwnProperty("key")) {
177
- d.key = o.bytes === String ? $util.base64.encode(m.key, 0, m.key.length) : o.bytes === Array ? Array.prototype.slice.call(m.key) : m.key;
178
- }
179
- if (m.value != null && m.hasOwnProperty("value")) {
180
- d.value = o.bytes === String ? $util.base64.encode(m.value, 0, m.value.length) : o.bytes === Array ? Array.prototype.slice.call(m.value) : m.value;
181
- }
182
- if (m.timeReceived != null && m.hasOwnProperty("timeReceived")) {
183
- d.timeReceived = m.timeReceived;
184
- }
185
- return d;
186
- };
187
-
188
- /**
189
- * Converts this Record to JSON.
190
- * @function toJSON
191
- * @memberof Record
192
- * @instance
193
- * @returns {Object.<string,*>} JSON object
194
- */
195
- Record.prototype.toJSON = function toJSON() {
196
- return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
197
- };
198
-
199
- return Record;
200
- })();
201
-
202
- export { $root as default };
19
+ })(Record || (Record = {}));
20
+ //# sourceMappingURL=record.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"record.js","sourceRoot":"","sources":["../../src/record.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,oDAAoD;AAEpD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAQtF,MAAM,KAAW,MAAM,CAgBtB;AAhBD,WAAiB,MAAM;IACR,YAAK,GAAG,GAAG,EAAE;QACxB,OAAO,OAAO,CAAS;YACrB,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE;YAChC,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE;YAClC,CAAC,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,EAAE;SAC3C,CAAC,CAAA;IACJ,CAAC,CAAA;IAEY,aAAM,GAAG,CAAC,GAAW,EAAc,EAAE;QAChD,OAAO,aAAa,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAA;IAC3C,CAAC,CAAA;IAEY,aAAM,GAAG,CAAC,GAAe,EAAU,EAAE;QAChD,OAAO,aAAa,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAA;IAC3C,CAAC,CAAA;AACH,CAAC,EAhBgB,MAAM,KAAN,MAAM,QAgBtB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/record",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "libp2p record implementation",
5
5
  "author": "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
6
6
  "license": "Apache-2.0 OR MIT",
@@ -145,30 +145,29 @@
145
145
  ]
146
146
  },
147
147
  "scripts": {
148
+ "clean": "aegir clean",
148
149
  "lint": "aegir lint",
149
- "pretest": "npm run build",
150
- "test": "aegir test -f ./dist/test",
151
- "test:node": "npm run test -- -t node",
152
- "test:chrome": "npm run test -- -t browser",
153
- "test:chrome-webworker": "npm run test -- -t webworker",
154
- "test:firefox": "npm run test -- -t browser -- --browser firefox",
155
- "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
156
- "build": "tsc",
157
- "postbuild": "npm run build:copy-proto-files",
158
- "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",
159
- "generate:proto-types": "pbts -o src/record/record.d.ts src/record/record.js",
160
- "build:copy-proto-files": "cp src/record* dist/src",
161
- "release": "semantic-release"
150
+ "dep-check": "aegir dep-check",
151
+ "test": "aegir test",
152
+ "test:node": "aegir test -t node",
153
+ "test:chrome": "aegir test -t browser",
154
+ "test:chrome-webworker": "aegir test -t webworker",
155
+ "test:firefox": "aegir test -t browser -- --browser firefox",
156
+ "test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
157
+ "build": "aegir build",
158
+ "generate": "protons ./src/record.proto",
159
+ "release": "aegir release"
162
160
  },
163
161
  "dependencies": {
164
162
  "err-code": "^3.0.1",
165
163
  "multiformats": "^9.4.5",
166
- "protobufjs": "^6.11.2",
164
+ "protons-runtime": "^1.0.2",
167
165
  "uint8arrays": "^3.0.0"
168
166
  },
169
167
  "devDependencies": {
170
168
  "@libp2p/crypto": "^0.22.9",
171
- "@libp2p/interfaces": "^1.3.18",
172
- "aegir": "^36.1.3"
169
+ "@libp2p/interfaces": "^1.3.20",
170
+ "aegir": "^37.0.10",
171
+ "protons": "^3.0.2"
173
172
  }
174
173
  }
package/src/index.ts CHANGED
@@ -1,15 +1,14 @@
1
1
  import {
2
- IRecord,
3
- Record as PBRecord
2
+ Record
4
3
  } from './record.js'
5
4
  import * as utils from './utils.js'
6
5
 
7
6
  export class Libp2pRecord {
8
7
  public key: Uint8Array
9
8
  public value: Uint8Array
10
- public timeReceived?: Date
9
+ public timeReceived: Date
11
10
 
12
- constructor (key: Uint8Array, value: Uint8Array, timeReceived?: Date) {
11
+ constructor (key: Uint8Array, value: Uint8Array, timeReceived: Date) {
13
12
  if (!(key instanceof Uint8Array)) {
14
13
  throw new Error('key must be a Uint8Array')
15
14
  }
@@ -24,7 +23,7 @@ export class Libp2pRecord {
24
23
  }
25
24
 
26
25
  serialize () {
27
- return PBRecord.encode(this.prepareSerialize()).finish()
26
+ return Record.encode(this.prepareSerialize())
28
27
  }
29
28
 
30
29
  /**
@@ -34,7 +33,7 @@ export class Libp2pRecord {
34
33
  return {
35
34
  key: this.key,
36
35
  value: this.value,
37
- timeReceived: this.timeReceived != null ? utils.toRFC3339(this.timeReceived) : undefined
36
+ timeReceived: utils.toRFC3339(this.timeReceived)
38
37
  }
39
38
  }
40
39
 
@@ -42,23 +41,16 @@ export class Libp2pRecord {
42
41
  * Decode a protobuf encoded record
43
42
  */
44
43
  static deserialize (raw: Uint8Array) {
45
- const message = PBRecord.decode(raw)
46
- return Libp2pRecord.fromDeserialized(PBRecord.toObject(message, {
47
- defaults: false,
48
- arrays: true,
49
- longs: Number,
50
- objects: false
51
- }))
44
+ const rec = Record.decode(raw)
45
+
46
+ return new Libp2pRecord(rec.key, rec.value, new Date(rec.timeReceived))
52
47
  }
53
48
 
54
49
  /**
55
50
  * Create a record from the raw object returned from the protobuf library
56
51
  */
57
- static fromDeserialized (obj: IRecord) {
58
- let recvtime
59
- if (obj.timeReceived != null) {
60
- recvtime = utils.parseRFC3339(obj.timeReceived)
61
- }
52
+ static fromDeserialized (obj: Record) {
53
+ const recvtime = utils.parseRFC3339(obj.timeReceived)
62
54
 
63
55
  if (obj.key == null) {
64
56
  throw new Error('key missing from deserialized object')
package/src/record.ts ADDED
@@ -0,0 +1,28 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+
4
+ import { encodeMessage, decodeMessage, message, bytes, string } from 'protons-runtime'
5
+
6
+ export interface Record {
7
+ key: Uint8Array
8
+ value: Uint8Array
9
+ timeReceived: string
10
+ }
11
+
12
+ export namespace Record {
13
+ export const codec = () => {
14
+ return message<Record>({
15
+ 1: { name: 'key', codec: bytes },
16
+ 2: { name: 'value', codec: bytes },
17
+ 5: { name: 'timeReceived', codec: string }
18
+ })
19
+ }
20
+
21
+ export const encode = (obj: Record): Uint8Array => {
22
+ return encodeMessage(obj, Record.codec())
23
+ }
24
+
25
+ export const decode = (buf: Uint8Array): Record => {
26
+ return decodeMessage(buf, Record.codec())
27
+ }
28
+ }
@@ -1,20 +0,0 @@
1
- syntax = "proto3";
2
-
3
- // Record represents a dht record that contains a value
4
- // for a key value pair
5
- message Record {
6
- // The key that references this record
7
- bytes key = 1;
8
-
9
- // The actual value this record is storing
10
- bytes value = 2;
11
-
12
- // Note: These fields were removed from the Record message
13
- // hash of the authors public key
14
- // optional bytes author = 3;
15
- // A PKI signature for the key+value+author
16
- // optional bytes signature = 4;
17
-
18
- // Time the record was received, set by receiver
19
- string timeReceived = 5;
20
- }
package/src/record.d.ts DELETED
@@ -1,71 +0,0 @@
1
- import * as $protobuf from "protobufjs";
2
- /** Properties of a Record. */
3
- export interface IRecord {
4
-
5
- /** Record key */
6
- key?: (Uint8Array|null);
7
-
8
- /** Record value */
9
- value?: (Uint8Array|null);
10
-
11
- /** Record timeReceived */
12
- timeReceived?: (string|null);
13
- }
14
-
15
- /** Represents a Record. */
16
- export class Record implements IRecord {
17
-
18
- /**
19
- * Constructs a new Record.
20
- * @param [p] Properties to set
21
- */
22
- constructor(p?: IRecord);
23
-
24
- /** Record key. */
25
- public key: Uint8Array;
26
-
27
- /** Record value. */
28
- public value: Uint8Array;
29
-
30
- /** Record timeReceived. */
31
- public timeReceived: string;
32
-
33
- /**
34
- * Encodes the specified Record message. Does not implicitly {@link Record.verify|verify} messages.
35
- * @param m Record message or plain object to encode
36
- * @param [w] Writer to encode to
37
- * @returns Writer
38
- */
39
- public static encode(m: IRecord, w?: $protobuf.Writer): $protobuf.Writer;
40
-
41
- /**
42
- * Decodes a Record message from the specified reader or buffer.
43
- * @param r Reader or buffer to decode from
44
- * @param [l] Message length if known beforehand
45
- * @returns Record
46
- * @throws {Error} If the payload is not a reader or valid buffer
47
- * @throws {$protobuf.util.ProtocolError} If required fields are missing
48
- */
49
- public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Record;
50
-
51
- /**
52
- * Creates a Record message from a plain object. Also converts values to their respective internal types.
53
- * @param d Plain object
54
- * @returns Record
55
- */
56
- public static fromObject(d: { [k: string]: any }): Record;
57
-
58
- /**
59
- * Creates a plain object from a Record message. Also converts values to other types if specified.
60
- * @param m Record
61
- * @param [o] Conversion options
62
- * @returns Plain object
63
- */
64
- public static toObject(m: Record, o?: $protobuf.IConversionOptions): { [k: string]: any };
65
-
66
- /**
67
- * Converts this Record to JSON.
68
- * @returns JSON object
69
- */
70
- public toJSON(): { [k: string]: any };
71
- }
package/src/record.js DELETED
@@ -1,202 +0,0 @@
1
- /*eslint-disable*/
2
- import $protobuf from "protobufjs/minimal.js";
3
-
4
- // Common aliases
5
- const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
6
-
7
- // Exported root namespace
8
- const $root = $protobuf.roots["libp2p-record"] || ($protobuf.roots["libp2p-record"] = {});
9
-
10
- export const Record = $root.Record = (() => {
11
-
12
- /**
13
- * Properties of a Record.
14
- * @exports IRecord
15
- * @interface IRecord
16
- * @property {Uint8Array|null} [key] Record key
17
- * @property {Uint8Array|null} [value] Record value
18
- * @property {string|null} [timeReceived] Record timeReceived
19
- */
20
-
21
- /**
22
- * Constructs a new Record.
23
- * @exports Record
24
- * @classdesc Represents a Record.
25
- * @implements IRecord
26
- * @constructor
27
- * @param {IRecord=} [p] Properties to set
28
- */
29
- function Record(p) {
30
- if (p)
31
- for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
32
- if (p[ks[i]] != null)
33
- this[ks[i]] = p[ks[i]];
34
- }
35
-
36
- /**
37
- * Record key.
38
- * @member {Uint8Array} key
39
- * @memberof Record
40
- * @instance
41
- */
42
- Record.prototype.key = $util.newBuffer([]);
43
-
44
- /**
45
- * Record value.
46
- * @member {Uint8Array} value
47
- * @memberof Record
48
- * @instance
49
- */
50
- Record.prototype.value = $util.newBuffer([]);
51
-
52
- /**
53
- * Record timeReceived.
54
- * @member {string} timeReceived
55
- * @memberof Record
56
- * @instance
57
- */
58
- Record.prototype.timeReceived = "";
59
-
60
- /**
61
- * Encodes the specified Record message. Does not implicitly {@link Record.verify|verify} messages.
62
- * @function encode
63
- * @memberof Record
64
- * @static
65
- * @param {IRecord} m Record message or plain object to encode
66
- * @param {$protobuf.Writer} [w] Writer to encode to
67
- * @returns {$protobuf.Writer} Writer
68
- */
69
- Record.encode = function encode(m, w) {
70
- if (!w)
71
- w = $Writer.create();
72
- if (m.key != null && Object.hasOwnProperty.call(m, "key"))
73
- w.uint32(10).bytes(m.key);
74
- if (m.value != null && Object.hasOwnProperty.call(m, "value"))
75
- w.uint32(18).bytes(m.value);
76
- if (m.timeReceived != null && Object.hasOwnProperty.call(m, "timeReceived"))
77
- w.uint32(42).string(m.timeReceived);
78
- return w;
79
- };
80
-
81
- /**
82
- * Decodes a Record message from the specified reader or buffer.
83
- * @function decode
84
- * @memberof Record
85
- * @static
86
- * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
87
- * @param {number} [l] Message length if known beforehand
88
- * @returns {Record} Record
89
- * @throws {Error} If the payload is not a reader or valid buffer
90
- * @throws {$protobuf.util.ProtocolError} If required fields are missing
91
- */
92
- Record.decode = function decode(r, l) {
93
- if (!(r instanceof $Reader))
94
- r = $Reader.create(r);
95
- var c = l === undefined ? r.len : r.pos + l, m = new $root.Record();
96
- while (r.pos < c) {
97
- var t = r.uint32();
98
- switch (t >>> 3) {
99
- case 1:
100
- m.key = r.bytes();
101
- break;
102
- case 2:
103
- m.value = r.bytes();
104
- break;
105
- case 5:
106
- m.timeReceived = r.string();
107
- break;
108
- default:
109
- r.skipType(t & 7);
110
- break;
111
- }
112
- }
113
- return m;
114
- };
115
-
116
- /**
117
- * Creates a Record message from a plain object. Also converts values to their respective internal types.
118
- * @function fromObject
119
- * @memberof Record
120
- * @static
121
- * @param {Object.<string,*>} d Plain object
122
- * @returns {Record} Record
123
- */
124
- Record.fromObject = function fromObject(d) {
125
- if (d instanceof $root.Record)
126
- return d;
127
- var m = new $root.Record();
128
- if (d.key != null) {
129
- if (typeof d.key === "string")
130
- $util.base64.decode(d.key, m.key = $util.newBuffer($util.base64.length(d.key)), 0);
131
- else if (d.key.length)
132
- m.key = d.key;
133
- }
134
- if (d.value != null) {
135
- if (typeof d.value === "string")
136
- $util.base64.decode(d.value, m.value = $util.newBuffer($util.base64.length(d.value)), 0);
137
- else if (d.value.length)
138
- m.value = d.value;
139
- }
140
- if (d.timeReceived != null) {
141
- m.timeReceived = String(d.timeReceived);
142
- }
143
- return m;
144
- };
145
-
146
- /**
147
- * Creates a plain object from a Record message. Also converts values to other types if specified.
148
- * @function toObject
149
- * @memberof Record
150
- * @static
151
- * @param {Record} m Record
152
- * @param {$protobuf.IConversionOptions} [o] Conversion options
153
- * @returns {Object.<string,*>} Plain object
154
- */
155
- Record.toObject = function toObject(m, o) {
156
- if (!o)
157
- o = {};
158
- var d = {};
159
- if (o.defaults) {
160
- if (o.bytes === String)
161
- d.key = "";
162
- else {
163
- d.key = [];
164
- if (o.bytes !== Array)
165
- d.key = $util.newBuffer(d.key);
166
- }
167
- if (o.bytes === String)
168
- d.value = "";
169
- else {
170
- d.value = [];
171
- if (o.bytes !== Array)
172
- d.value = $util.newBuffer(d.value);
173
- }
174
- d.timeReceived = "";
175
- }
176
- if (m.key != null && m.hasOwnProperty("key")) {
177
- d.key = o.bytes === String ? $util.base64.encode(m.key, 0, m.key.length) : o.bytes === Array ? Array.prototype.slice.call(m.key) : m.key;
178
- }
179
- if (m.value != null && m.hasOwnProperty("value")) {
180
- d.value = o.bytes === String ? $util.base64.encode(m.value, 0, m.value.length) : o.bytes === Array ? Array.prototype.slice.call(m.value) : m.value;
181
- }
182
- if (m.timeReceived != null && m.hasOwnProperty("timeReceived")) {
183
- d.timeReceived = m.timeReceived;
184
- }
185
- return d;
186
- };
187
-
188
- /**
189
- * Converts this Record to JSON.
190
- * @function toJSON
191
- * @memberof Record
192
- * @instance
193
- * @returns {Object.<string,*>} JSON object
194
- */
195
- Record.prototype.toJSON = function toJSON() {
196
- return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
197
- };
198
-
199
- return Record;
200
- })();
201
-
202
- export { $root as default };