@leofcoin/codec-format-interface 1.5.2 → 1.5.3
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/README.md +30 -0
- package/package.json +7 -3
- package/rollup.config.js +3 -5
- package/src/basic-interface.ts +27 -30
- package/src/codec-format-interface.ts +23 -14
- package/test/index.js +6 -7
- package/tsconfig.json +18 -0
- package/dist/index.js +0 -1659
- package/tsconfig.js +0 -17
package/README.md
CHANGED
|
@@ -1,2 +1,32 @@
|
|
|
1
1
|
# codec-format-interface
|
|
2
2
|
|
|
3
|
+
## usage
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
import FormatInterface from '@leofcoin/codec-format-interface'
|
|
7
|
+
|
|
8
|
+
class MyFormat extends FormatInterface {
|
|
9
|
+
get proto() {
|
|
10
|
+
return {
|
|
11
|
+
key: 'value',
|
|
12
|
+
hello: 'world'
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
constructor(data) {
|
|
17
|
+
super(data, { name: 'my-format', hashFormat = 'bs32' })
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## build for browser
|
|
24
|
+
### rollup
|
|
25
|
+
```js
|
|
26
|
+
import resolve from '@rollup/plugin-node-resolve'
|
|
27
|
+
...
|
|
28
|
+
plugins: [
|
|
29
|
+
resolve()
|
|
30
|
+
]
|
|
31
|
+
...
|
|
32
|
+
```
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/codec-format-interface",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
5
|
"type": "module",
|
|
7
|
-
"
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"typings": "dist/index.d.ts",
|
|
8
10
|
"scripts": {
|
|
9
11
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
10
12
|
"build": "rollup -c"
|
|
@@ -24,6 +26,8 @@
|
|
|
24
26
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
25
27
|
"@vandeurenglenn/base58": "^1.1.1",
|
|
26
28
|
"@vandeurenglenn/is-hex": "^1.0.0",
|
|
29
|
+
"@vandeurenglenn/proto-array": "^1.0.0",
|
|
30
|
+
"@vandeurenglenn/typed-array-utils": "^1.1.0",
|
|
27
31
|
"hash-wasm": "^4.9.0",
|
|
28
32
|
"varint": "^6.0.0"
|
|
29
33
|
},
|
package/rollup.config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import resolve from '@rollup/plugin-node-resolve'
|
|
2
|
-
import commonjs from '@rollup/plugin-commonjs'
|
|
3
1
|
import typescript from '@rollup/plugin-typescript'
|
|
2
|
+
import tsConfig from './tsconfig.json' assert { type: 'json' }
|
|
3
|
+
|
|
4
4
|
export default [{
|
|
5
5
|
input: ['src/index.ts'],
|
|
6
6
|
output: [{
|
|
@@ -8,8 +8,6 @@ export default [{
|
|
|
8
8
|
format: 'es'
|
|
9
9
|
}],
|
|
10
10
|
plugins: [
|
|
11
|
-
|
|
12
|
-
resolve({preferBuiltins: true}),
|
|
13
|
-
typescript()
|
|
11
|
+
typescript(tsConfig)
|
|
14
12
|
]
|
|
15
13
|
}]
|
package/src/basic-interface.ts
CHANGED
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
import bs32 from '@vandeurenglenn/base32';
|
|
2
2
|
import base58 from '@vandeurenglenn/base58';
|
|
3
3
|
import isHex from '@vandeurenglenn/is-hex';
|
|
4
|
+
import proto from '@vandeurenglenn/proto-array'
|
|
5
|
+
import { fromBase32, fromBase58, fromString, fromHex, fromArrayLike, fromUintArrayString, toBase32, toBase58, toHex } from '@vandeurenglenn/typed-array-utils'
|
|
4
6
|
|
|
5
7
|
|
|
6
|
-
export default class BasicInterface
|
|
8
|
+
export default class BasicInterface {
|
|
7
9
|
encoded: Uint8Array;
|
|
8
10
|
decoded: object | string;
|
|
9
11
|
keys: string[]
|
|
10
12
|
name: string
|
|
11
13
|
// get Codec(): Codec {}
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
protoEncode(data: object): Uint8Array {
|
|
14
16
|
// check schema
|
|
15
|
-
|
|
17
|
+
|
|
18
|
+
return proto.encode(this.proto, data)
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
|
|
21
|
+
protoDecode(data: Uint8Array): object {
|
|
19
22
|
// check schema
|
|
20
|
-
return
|
|
23
|
+
return proto.decode(this.proto, data)
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
decode: () => DecodeResult;
|
|
24
|
-
|
|
25
|
-
encode:() => Promise<Uint8Array>;
|
|
26
|
-
|
|
27
26
|
isHex(string: any) {
|
|
28
27
|
return isHex(string)
|
|
29
28
|
}
|
|
@@ -34,14 +33,14 @@ export default class BasicInterface implements basicInterface {
|
|
|
34
33
|
return base58.isBase58(string)
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
fromBs32(encoded:
|
|
36
|
+
fromBs32(encoded: base58String): object {
|
|
38
37
|
this.encoded = bs32.decode(encoded)
|
|
39
38
|
return this.decode()
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
|
|
43
|
-
fromBs58(encoded: any):
|
|
44
|
-
this.encoded =
|
|
42
|
+
fromBs58(encoded: any): object {
|
|
43
|
+
this.encoded = fromBase58(encoded)
|
|
45
44
|
return this.decode()
|
|
46
45
|
}
|
|
47
46
|
|
|
@@ -60,6 +59,11 @@ export default class BasicInterface implements basicInterface {
|
|
|
60
59
|
return this.decode()
|
|
61
60
|
}
|
|
62
61
|
|
|
62
|
+
fromHex(string) {
|
|
63
|
+
this.encoded = fromHex(string)
|
|
64
|
+
return this.decode()
|
|
65
|
+
}
|
|
66
|
+
|
|
63
67
|
fromArray(array: number[]): object {
|
|
64
68
|
this.encoded = Uint8Array.from([...array])
|
|
65
69
|
return this.decode()
|
|
@@ -70,37 +74,30 @@ export default class BasicInterface implements basicInterface {
|
|
|
70
74
|
return this.decode()
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
|
|
74
|
-
this.encoded
|
|
75
|
-
return this.
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async toString(encoding: string = 'utf8'): Promise<string> {
|
|
79
|
-
if (!this.encoded) await this.encode()
|
|
80
|
-
return this.encoded.toString(encoding)
|
|
77
|
+
toString(): string {
|
|
78
|
+
if (!this.encoded) this.encode()
|
|
79
|
+
return this.encoded.toString()
|
|
81
80
|
}
|
|
82
81
|
|
|
83
|
-
/**
|
|
84
|
-
* @return {String} encoded
|
|
85
|
-
*/
|
|
86
82
|
toHex() {
|
|
87
|
-
|
|
83
|
+
if (!this.encoded) this.encode()
|
|
84
|
+
return toHex(this.encoded)
|
|
88
85
|
}
|
|
89
86
|
|
|
90
87
|
/**
|
|
91
88
|
* @return {String} encoded
|
|
92
89
|
*/
|
|
93
|
-
|
|
94
|
-
if (!this.encoded)
|
|
95
|
-
return
|
|
90
|
+
toBs32() {
|
|
91
|
+
if (!this.encoded) this.encode()
|
|
92
|
+
return toBase32(this.encoded)
|
|
96
93
|
}
|
|
97
94
|
|
|
98
95
|
/**
|
|
99
96
|
* @return {String} encoded
|
|
100
97
|
*/
|
|
101
|
-
|
|
102
|
-
if (!this.encoded)
|
|
103
|
-
return
|
|
98
|
+
toBs58() {
|
|
99
|
+
if (!this.encoded) this.encode()
|
|
100
|
+
return toBase58(this.encoded)
|
|
104
101
|
}
|
|
105
102
|
|
|
106
103
|
/**
|
|
@@ -3,17 +3,26 @@ import Hash from './codec-hash.js'
|
|
|
3
3
|
import Codec from './codec.js';
|
|
4
4
|
|
|
5
5
|
export default class FormatInterface extends BasicInterface implements FormatInterface {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
|
|
7
|
+
set proto(value) {
|
|
8
|
+
this._proto = value
|
|
9
|
+
this.keys = Object.keys(this._proto)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get proto() {
|
|
13
|
+
return this._proto
|
|
14
|
+
}
|
|
15
|
+
init(buffer) {
|
|
16
|
+
if (buffer instanceof Uint8Array) this.fromUint8Array(buffer)
|
|
17
|
+
else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer)
|
|
9
18
|
else if (buffer?.name === this.name) return buffer
|
|
10
19
|
else if (buffer instanceof String) {
|
|
11
|
-
if (this.isHex(buffer))
|
|
12
|
-
else if (this.
|
|
13
|
-
else if (this.
|
|
14
|
-
else throw new Error(`unsupported
|
|
20
|
+
if (this.isHex(buffer)) this.fromHex(buffer)
|
|
21
|
+
else if (this.isBase58(buffer)) this.fromBs58(buffer)
|
|
22
|
+
else if (this.isBase32(buffer)) this.fromBs32(buffer)
|
|
23
|
+
else throw new Error(`unsupported ${buffer}`)
|
|
15
24
|
} else {
|
|
16
|
-
|
|
25
|
+
this.create(buffer)
|
|
17
26
|
}
|
|
18
27
|
return this
|
|
19
28
|
}
|
|
@@ -24,13 +33,13 @@ export default class FormatInterface extends BasicInterface implements FormatInt
|
|
|
24
33
|
if (codec.name) return true
|
|
25
34
|
}
|
|
26
35
|
|
|
27
|
-
|
|
36
|
+
decode() {
|
|
28
37
|
let encoded = this.encoded;
|
|
29
38
|
const codec = new Codec(this.encoded)
|
|
30
39
|
if (codec.codecBuffer) {
|
|
31
40
|
encoded = encoded.slice(codec.codecBuffer.length)
|
|
32
41
|
this.name = codec.name
|
|
33
|
-
this.decoded =
|
|
42
|
+
this.decoded = this.protoDecode(encoded)
|
|
34
43
|
try {
|
|
35
44
|
this.decoded = JSON.parse(this.decoded)
|
|
36
45
|
} catch {
|
|
@@ -44,13 +53,13 @@ export default class FormatInterface extends BasicInterface implements FormatInt
|
|
|
44
53
|
}
|
|
45
54
|
|
|
46
55
|
|
|
47
|
-
|
|
56
|
+
encode(decoded?: object) {
|
|
48
57
|
let encoded: Uint8Array
|
|
49
58
|
if (!decoded) decoded = this.decoded;
|
|
50
59
|
const codec = new Codec(this.name)
|
|
51
60
|
|
|
52
61
|
if (decoded instanceof Uint8Array) encoded = decoded
|
|
53
|
-
else encoded =
|
|
62
|
+
else encoded = this.protoEncode(decoded)
|
|
54
63
|
|
|
55
64
|
if (codec.codecBuffer) {
|
|
56
65
|
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length)
|
|
@@ -64,7 +73,7 @@ export default class FormatInterface extends BasicInterface implements FormatInt
|
|
|
64
73
|
}
|
|
65
74
|
/**
|
|
66
75
|
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
67
|
-
* @param {Object} proto - {
|
|
76
|
+
* @param {Object} proto - {protoObject}
|
|
68
77
|
* @param {Object} options - {hashFormat, name}
|
|
69
78
|
*/
|
|
70
79
|
constructor(buffer, proto, options = {}) {
|
|
@@ -72,7 +81,7 @@ export default class FormatInterface extends BasicInterface implements FormatInt
|
|
|
72
81
|
this.proto = proto
|
|
73
82
|
this.hashFormat = options.hashFormat || 'bs32'
|
|
74
83
|
if (options.name) this.name = options.name
|
|
75
|
-
|
|
84
|
+
this.init(buffer)
|
|
76
85
|
}
|
|
77
86
|
|
|
78
87
|
/**
|
package/test/index.js
CHANGED
|
@@ -3,13 +3,12 @@ import {FormatInterface, codecs} from './../dist/index.js'
|
|
|
3
3
|
|
|
4
4
|
globalThis.peernet = {codecs: {}}
|
|
5
5
|
class FormatTest extends FormatInterface {
|
|
6
|
-
get keys() { return ['somedata'] }
|
|
7
6
|
get messageName() { return 'Message' }
|
|
8
7
|
|
|
9
8
|
constructor(data) {
|
|
10
|
-
super(data,
|
|
11
|
-
|
|
12
|
-
}
|
|
9
|
+
super(data, {
|
|
10
|
+
somedata: 'test'
|
|
11
|
+
} , {name: 'disco-hash'})
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
14
|
|
|
@@ -17,9 +16,9 @@ const encoded = [48, 10, 5, 104, 101, 108, 108, 111]
|
|
|
17
16
|
|
|
18
17
|
test('format', async (tape) => {
|
|
19
18
|
tape.plan(2)
|
|
20
|
-
const message =
|
|
19
|
+
const message = new FormatTest({somedata: 'hello'})
|
|
21
20
|
|
|
22
|
-
const m2 =
|
|
21
|
+
const m2 = new FormatTest(message.encoded)
|
|
23
22
|
|
|
24
23
|
tape.ok(message.encoded, 'can encode')
|
|
25
24
|
tape.ok(m2.decoded.somedata === 'hello', 'can decode')
|
|
@@ -27,7 +26,7 @@ test('format', async (tape) => {
|
|
|
27
26
|
|
|
28
27
|
test('format can hash', async (tape) => {
|
|
29
28
|
tape.plan(1)
|
|
30
|
-
const message =
|
|
29
|
+
const message = new FormatTest({somedata: 'hello'})
|
|
31
30
|
const hash = await message.hash()
|
|
32
31
|
tape.ok(hash, 'can hash')
|
|
33
32
|
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "./dist",
|
|
4
|
+
"esModuleInterop": true,
|
|
5
|
+
"allowSyntheticDefaultImports": true,
|
|
6
|
+
"allowJs": true,
|
|
7
|
+
"target": "es2022",
|
|
8
|
+
"module": "es2022",
|
|
9
|
+
"moduleResolution": "nodeNext",
|
|
10
|
+
"declaration": true
|
|
11
|
+
},
|
|
12
|
+
"include": [
|
|
13
|
+
"./src/**/*"
|
|
14
|
+
],
|
|
15
|
+
"exclude": [
|
|
16
|
+
"./node_modules/**/*"
|
|
17
|
+
]
|
|
18
|
+
}
|