@leofcoin/codec-format-interface 1.7.11 → 1.7.13

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,5 +1,7 @@
1
1
  import type { base58String } from '@vandeurenglenn/base58';
2
2
  import type { base32String } from '@vandeurenglenn/base32';
3
+ export declare const jsonStringifyBigInt: (key: any, value: any) => any;
4
+ export declare const jsonParseBigInt: (key: any, value: any) => any;
3
5
  export default class BasicInterface {
4
6
  #private;
5
7
  name: string;
@@ -4,6 +4,8 @@ import isHex from '@vandeurenglenn/is-hex';
4
4
  import proto from '@vandeurenglenn/proto-array';
5
5
  import { fromBase58, fromHex, toHex, toBase32, toBase58 } from '@vandeurenglenn/typed-array-utils';
6
6
 
7
+ const jsonStringifyBigInt = (key, value) => typeof value === 'bigint' ? { $bigint: value.toString() } : value;
8
+ const jsonParseBigInt = (key, value) => typeof value === 'object' && value.$bigint ? BigInt(value.$bigint) : value;
7
9
  class BasicInterface {
8
10
  #encoded;
9
11
  #decoded;
@@ -120,4 +122,4 @@ class BasicInterface {
120
122
  }
121
123
  }
122
124
 
123
- export { BasicInterface as default };
125
+ export { BasicInterface as default, jsonParseBigInt, jsonStringifyBigInt };
@@ -1,4 +1,4 @@
1
- import BasicInterface from './basic-interface.js';
1
+ import BasicInterface, { jsonParseBigInt } from './basic-interface.js';
2
2
  import Codec from './codec.js';
3
3
  import CodecHash from './codec-hash.js';
4
4
  import '@vandeurenglenn/base32';
@@ -128,13 +128,13 @@ class FormatInterface extends BasicInterface {
128
128
  this.encoded = buffer;
129
129
  return this.hasCodec()
130
130
  ? this.decode()
131
- : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
131
+ : this.create(JSON.parse(new TextDecoder().decode(this.encoded), jsonParseBigInt));
132
132
  }
133
133
  fromArrayBuffer(buffer) {
134
134
  this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
135
135
  return this.hasCodec()
136
136
  ? this.decode()
137
- : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
137
+ : this.create(JSON.parse(new TextDecoder().decode(this.encoded), jsonParseBigInt));
138
138
  }
139
139
  /**
140
140
  * @param {Object} data
@@ -1,8 +1,14 @@
1
1
  import BasicInterface from './basic-interface.js';
2
+ import Codec from './codec.js';
3
+ type CodecHashOptions = {
4
+ name: string;
5
+ codecs: object;
6
+ };
2
7
  export default class CodecHash extends BasicInterface {
3
- codec: any;
4
- discoCodec: any;
5
- constructor(buffer: any, options?: {});
8
+ codec: Uint8Array;
9
+ discoCodec: Codec;
10
+ size: number;
11
+ constructor(buffer: any, options: CodecHashOptions);
6
12
  init(uint8Array: any): Promise<this>;
7
13
  get prefix(): Uint8Array;
8
14
  get length(): number[];
@@ -12,10 +18,11 @@ export default class CodecHash extends BasicInterface {
12
18
  encode(buffer: any, name?: any): Promise<Uint8Array>;
13
19
  validate(buffer: any): Promise<void>;
14
20
  decode(buffer: any): {
15
- codec: any;
21
+ codec: Uint8Array;
16
22
  name: string;
17
- size: any;
23
+ size: number;
18
24
  length: number[];
19
25
  digest: any;
20
26
  };
21
27
  }
28
+ export {};
@@ -1,5 +1,5 @@
1
1
  import varint from 'varint';
2
- import BasicInterface from './basic-interface.js';
2
+ import BasicInterface, { jsonStringifyBigInt } from './basic-interface.js';
3
3
  import Codec from './codec.js';
4
4
  import '@vandeurenglenn/base32';
5
5
  import '@vandeurenglenn/base58';
@@ -11,20 +11,21 @@ import '@leofcoin/codecs';
11
11
  class CodecHash extends BasicInterface {
12
12
  codec;
13
13
  discoCodec;
14
- constructor(buffer, options = {}) {
14
+ size;
15
+ constructor(buffer, options) {
15
16
  super();
16
- if (options.name)
17
+ if (options?.name)
17
18
  this.name = options.name;
18
19
  else
19
20
  this.name = 'disco-hash';
20
- if (options.codecs)
21
+ if (options?.codecs)
21
22
  this.codecs = options.codecs;
22
23
  return this.init(buffer);
23
24
  }
24
25
  async init(uint8Array) {
25
26
  if (uint8Array) {
26
27
  if (uint8Array instanceof Uint8Array) {
27
- this.discoCodec = new Codec(uint8Array, this.codecs);
28
+ this.discoCodec = new Codec(uint8Array);
28
29
  const name = this.discoCodec.name;
29
30
  if (name) {
30
31
  this.name = name;
@@ -66,7 +67,7 @@ class CodecHash extends BasicInterface {
66
67
  return this.encoded;
67
68
  }
68
69
  fromJSON(json) {
69
- return this.encode(new TextEncoder().encode(JSON.stringify(json)));
70
+ return this.encode(new TextEncoder().encode(JSON.stringify(json, jsonStringifyBigInt)));
70
71
  }
71
72
  async encode(buffer, name) {
72
73
  if (!this.name && name)
@@ -140,7 +141,7 @@ class CodecHash extends BasicInterface {
140
141
  name: this.name,
141
142
  size: this.size,
142
143
  length: this.length,
143
- digest: this.digest,
144
+ digest: this.digest
144
145
  };
145
146
  }
146
147
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/codec-format-interface",
3
- "version": "1.7.11",
3
+ "version": "1.7.13",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -11,7 +11,6 @@
11
11
  ],
12
12
  "typings": "exports/index.d.ts",
13
13
  "scripts": {
14
- "prepublish": "npm run build && npm version patch",
15
14
  "build": "rollup -c",
16
15
  "test": "node test"
17
16
  },
@@ -27,24 +26,22 @@
27
26
  },
28
27
  "homepage": "https://github.com/leofcoin/codec-format-interface#readme",
29
28
  "dependencies": {
30
- "@leofcoin/codecs": "^1.0.0",
31
- "@leofcoin/crypto": "^0.2.7",
32
- "@vandeurenglenn/base32": "^1.1.0",
33
- "@vandeurenglenn/base58": "^1.1.1",
34
- "@vandeurenglenn/is-hex": "^1.0.0",
35
- "@vandeurenglenn/proto-array": "^1.0.0",
36
- "@vandeurenglenn/typed-array-utils": "^1.1.0",
37
- "hash-wasm": "^4.9.0",
29
+ "@leofcoin/codecs": "^1.0.7",
30
+ "@leofcoin/crypto": "^0.2.28",
31
+ "@vandeurenglenn/base32": "^1.2.4",
32
+ "@vandeurenglenn/base58": "^1.1.9",
33
+ "@vandeurenglenn/is-hex": "^1.1.1",
34
+ "@vandeurenglenn/proto-array": "^1.0.16",
35
+ "@vandeurenglenn/typed-array-utils": "^1.2.0",
36
+ "hash-wasm": "^4.11.0",
38
37
  "varint": "^6.0.0"
39
38
  },
40
39
  "devDependencies": {
41
- "@rollup/plugin-commonjs": "^23.0.3",
42
- "@rollup/plugin-node-resolve": "^15.0.1",
43
- "@rollup/plugin-typescript": "^10.0.1",
44
- "@types/varint": "^6.0.1",
45
- "rollup": "^3.5.1",
46
- "tape": "^5.5.3",
47
- "tinybench": "^2.5.1",
48
- "tslib": "^2.4.1"
40
+ "@rollup/plugin-typescript": "^11.1.6",
41
+ "@types/varint": "^6.0.3",
42
+ "rollup": "^4.22.4",
43
+ "tape": "^5.9.0",
44
+ "tinybench": "^2.9.0",
45
+ "tslib": "^2.7.0"
49
46
  }
50
47
  }