@leofcoin/codec-format-interface 1.2.6 → 1.3.0

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.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@leofcoin/codec-format-interface",
3
- "version": "1.2.6",
3
+ "version": "1.3.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
- "module": "src/index.js",
6
+ "module": "dist/module.js",
7
7
  "scripts": {
8
8
  "test": "echo \"Error: no test specified\" && exit 1",
9
9
  "c": "rollup -c"
@@ -24,7 +24,6 @@
24
24
  "@vandeurenglenn/base58": "^1.1.0",
25
25
  "@vandeurenglenn/is-hex": "^1.0.0",
26
26
  "keccak": "^3.0.2",
27
- "protons": "^2.0.1",
28
27
  "varint": "^6.0.0"
29
28
  },
30
29
  "devDependencies": {
package/rollup.config.js CHANGED
@@ -1,7 +1,19 @@
1
+ import resolve from 'rollup-plugin-node-resolve'
2
+ import commonjs from 'rollup-plugin-commonjs'
1
3
  export default [{
2
4
  input: ['src/index.js'],
3
5
  output: [{
4
6
  dir: 'dist',
5
7
  format: 'cjs'
6
8
  }]
9
+ }, {
10
+ input: ['src/index.js'],
11
+ output: [{
12
+ file: 'dist/module.js',
13
+ format: 'es'
14
+ }],
15
+ plugins: [
16
+ commonjs(),
17
+ resolve()
18
+ ]
7
19
  }]
@@ -3,12 +3,12 @@ import bs58 from '@vandeurenglenn/base58';
3
3
  import isHex from '@vandeurenglenn/is-hex';
4
4
 
5
5
  export default class BasicInterface {
6
- #handleDecode() {
6
+ handleDecode() {
7
7
  if (!this.decode) throw new Error('bad implementation: needs decode func')
8
8
  return this.decode()
9
9
  }
10
10
 
11
- #handleEncode() {
11
+ handleEncode() {
12
12
  if (!this.encode) throw new Error('bad implementation: needs encode func')
13
13
  return this.encode()
14
14
  }
@@ -26,7 +26,7 @@ export default class BasicInterface {
26
26
  */
27
27
  fromBs32(encoded) {
28
28
  this.encoded = bs32.decode(encoded)
29
- return this.#handleDecode()
29
+ return this.handleDecode()
30
30
  }
31
31
 
32
32
  /**
@@ -34,7 +34,7 @@ export default class BasicInterface {
34
34
  */
35
35
  fromBs58(encoded) {
36
36
  this.encoded = bs58.decode(encoded)
37
- return this.#handleDecode()
37
+ return this.handleDecode()
38
38
  }
39
39
 
40
40
  async toArray() {
@@ -47,12 +47,12 @@ export default class BasicInterface {
47
47
 
48
48
  fromString(string) {
49
49
  this.encoded = new Uint8Array(string.split(','))
50
- return this.#handleDecode()
50
+ return this.handleDecode()
51
51
  }
52
52
 
53
53
  fromArray(array) {
54
54
  this.encoded = new Uint8Array([...array])
55
- return this.#handleDecode()
55
+ return this.handleDecode()
56
56
  }
57
57
 
58
58
  /**
@@ -60,7 +60,7 @@ export default class BasicInterface {
60
60
  */
61
61
  fromEncoded(encoded) {
62
62
  this.encoded = encoded
63
- return this.#handleDecode()
63
+ return this.handleDecode()
64
64
  }
65
65
 
66
66
  /**
@@ -68,11 +68,11 @@ export default class BasicInterface {
68
68
  */
69
69
  fromHex(encoded) {
70
70
  this.encoded = Buffer.from(encoded, 'hex')
71
- return this.#handleDecode()
71
+ return this.handleDecode()
72
72
  }
73
73
 
74
74
  async toString(encoding = 'utf8') {
75
- if (!this.encoded) await this.#handleEncode()
75
+ if (!this.encoded) await this.handleEncode()
76
76
  return this.encoded.toString(encoding)
77
77
  }
78
78
 
@@ -87,7 +87,7 @@ export default class BasicInterface {
87
87
  * @return {String} encoded
88
88
  */
89
89
  async toBs32() {
90
- if (!this.encoded) await this.#handleEncode()
90
+ if (!this.encoded) await this.handleEncode()
91
91
  return bs32.encode(this.encoded)
92
92
  }
93
93
 
@@ -95,7 +95,7 @@ export default class BasicInterface {
95
95
  * @return {String} encoded
96
96
  */
97
97
  async toBs58() {
98
- if (!this.encoded) await this.#handleEncode()
98
+ if (!this.encoded) await this.handleEncode()
99
99
  return bs58.encode(this.encoded)
100
100
  }
101
101
 
@@ -1,25 +1,20 @@
1
1
  import BasicInterface from './basic-interface.js'
2
2
  import Codec from './codec.js';
3
3
  import Hash from './codec-hash.js'
4
- let protons
5
4
 
6
5
  export default class FormatInterface extends BasicInterface {
7
- async #importProtons() {
8
- let importee = await import(/* webpackChunkName: "protons" */ 'protons')
9
- return importee.default
10
- }
11
6
 
12
7
  async protoEncode(data) {
13
- if (!protons) protons = await this.#importProtons()
14
- return protons(this.proto)[this.messageName].encode(data)
8
+ // check schema
9
+ return new TextEncoder().encode(data)
15
10
  }
16
11
 
17
12
  async protoDecode(data) {
18
- if (!protons) protons = await this.#importProtons()
19
- return protons(this.proto)[this.messageName].decode(data)
13
+ // check schema
14
+ return new TextDecoder().decode(data)
20
15
  }
21
16
 
22
- async #init(buffer) {
17
+ async init(buffer) {
23
18
  if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer)
24
19
  else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer)
25
20
  else if (buffer?.name === this.name) return buffer
@@ -44,7 +39,7 @@ export default class FormatInterface extends BasicInterface {
44
39
  this.proto = proto
45
40
  this.hashFormat = options.hashFormat || 'bs32'
46
41
  if (options.name) this.name = options.name
47
- return this.#init(buffer)
42
+ return this.init(buffer)
48
43
  }
49
44
 
50
45
  /**
@@ -72,6 +67,11 @@ export default class FormatInterface extends BasicInterface {
72
67
  encoded = encoded.slice(discoCodec.codecBuffer.length)
73
68
  this.name = discoCodec.name
74
69
  this.decoded = await this.protoDecode(encoded)
70
+ try {
71
+ this.decoded = JSON.parse(this.decoded)
72
+ } catch {
73
+
74
+ }
75
75
  return this.decoded
76
76
  }
77
77
 
@@ -81,7 +81,7 @@ export default class FormatInterface extends BasicInterface {
81
81
  async encode(decoded) {
82
82
  if (!decoded) decoded = this.decoded;
83
83
  const codec = new Codec(this.name)
84
- const encoded = await this.protoEncode(decoded)
84
+ const encoded = await this.protoEncode(typeof decoded === 'object' ? JSON.stringify(decoded) : decoded)
85
85
  const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length)
86
86
  uint8Array.set(codec.codecBuffer)
87
87
  uint8Array.set(encoded, codec.codecBuffer.length)
package/src/index.js CHANGED
@@ -1,5 +1,11 @@
1
- export { default as BasicInterface } from './basic-interface.js'
2
- export { default as FormatInterface } from './codec-format-interface.js'
3
- export { default as CodecHash } from './codec-hash.js'
4
- export { default as Codec } from './codec.js'
5
- export { default as codecs } from './codecs.js'
1
+ import basicInterface from './basic-interface.js'
2
+ import formatInterface from './codec-format-interface.js'
3
+ import codecHash from './codec-hash.js'
4
+ import codec from './codec.js'
5
+ import codecs from './codecs.js'
6
+
7
+ export const BasicInterface = basicInterface
8
+ export const FormatInterface = formatInterface
9
+ export const CodecHash = codecHash
10
+ export const Codec = codec
11
+ export const Codecs = codecs
File without changes