@kubun/id 0.9.0 → 0.11.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/lib/attachment.js +96 -1
- package/lib/base.js +70 -1
- package/lib/bytes.js +24 -1
- package/lib/cluster-model.js +26 -1
- package/lib/decode.js +34 -1
- package/lib/document.js +81 -1
- package/lib/index.js +8 -1
- package/lib/model.js +111 -1
- package/lib/plugin-node.js +84 -1
- package/lib/utils.js +19 -1
- package/package.json +4 -4
package/lib/attachment.js
CHANGED
|
@@ -1 +1,96 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { fromUTF, toUTF } from '@sozai/codec';
|
|
2
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
3
|
+
import { AttachmentType, KubunID, NAMESPACE_CODE } from './base.js';
|
|
4
|
+
import { concat } from './bytes.js';
|
|
5
|
+
import { decodeVarint, digestBytes, encodeVarint } from './utils.js';
|
|
6
|
+
const DEFAULT_MIME_TYPE = 'application/octet-stream';
|
|
7
|
+
export class AttachmentID extends KubunID {
|
|
8
|
+
static create(value, mimeType) {
|
|
9
|
+
return new AttachmentID(digestBytes(value), value.byteLength, mimeType);
|
|
10
|
+
}
|
|
11
|
+
static fromBytes(input) {
|
|
12
|
+
let offset = 0;
|
|
13
|
+
// Check namespace = is it a KubunID?
|
|
14
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
15
|
+
if (namespace !== NAMESPACE_CODE) {
|
|
16
|
+
throw new Error('Invalid KubunID: namespace mismatch');
|
|
17
|
+
}
|
|
18
|
+
offset += namespaceLength;
|
|
19
|
+
// Check type matches
|
|
20
|
+
const [type, typeLength] = decodeVarint(input, offset);
|
|
21
|
+
if (type !== AttachmentType.BINARY) {
|
|
22
|
+
throw new Error('Invalid KubunID: type mismatch');
|
|
23
|
+
}
|
|
24
|
+
offset += typeLength;
|
|
25
|
+
// Decode digest
|
|
26
|
+
const [digestLength, digestVarintLength] = decodeVarint(input, offset);
|
|
27
|
+
offset += digestVarintLength;
|
|
28
|
+
const digest = input.slice(offset, offset + digestLength);
|
|
29
|
+
offset += digestLength;
|
|
30
|
+
// Extract content length
|
|
31
|
+
const [contentLength, contentVarintLength] = decodeVarint(input, offset);
|
|
32
|
+
offset += contentVarintLength;
|
|
33
|
+
// Check if MIME type is provided
|
|
34
|
+
const [mimeTypeBytesLength, mimeTypeVarintLength] = decodeVarint(input, offset);
|
|
35
|
+
if (mimeTypeBytesLength === 0) {
|
|
36
|
+
// No MIME type encoded, use default
|
|
37
|
+
return new AttachmentID(digest, contentLength);
|
|
38
|
+
}
|
|
39
|
+
// Decode MIME type value
|
|
40
|
+
offset += mimeTypeVarintLength;
|
|
41
|
+
const mimeTypeBytes = input.slice(offset, offset + mimeTypeBytesLength);
|
|
42
|
+
return new AttachmentID(digest, contentLength, toUTF(mimeTypeBytes));
|
|
43
|
+
}
|
|
44
|
+
static fromString(input) {
|
|
45
|
+
return AttachmentID.fromBytes(base36.decode(input));
|
|
46
|
+
}
|
|
47
|
+
static from(input) {
|
|
48
|
+
if (input instanceof AttachmentID) {
|
|
49
|
+
return input;
|
|
50
|
+
}
|
|
51
|
+
if (input instanceof Uint8Array) {
|
|
52
|
+
return AttachmentID.fromBytes(input);
|
|
53
|
+
}
|
|
54
|
+
if (typeof input === 'string') {
|
|
55
|
+
return AttachmentID.fromString(input);
|
|
56
|
+
}
|
|
57
|
+
throw new Error('Unsupported input type');
|
|
58
|
+
}
|
|
59
|
+
#contentLength;
|
|
60
|
+
#digest;
|
|
61
|
+
#mimeType;
|
|
62
|
+
constructor(digest, contentLength, mimeType = DEFAULT_MIME_TYPE){
|
|
63
|
+
let data;
|
|
64
|
+
const baseData = concat([
|
|
65
|
+
encodeVarint(digest.byteLength),
|
|
66
|
+
digest,
|
|
67
|
+
encodeVarint(contentLength)
|
|
68
|
+
]);
|
|
69
|
+
if (mimeType === DEFAULT_MIME_TYPE) {
|
|
70
|
+
data = concat([
|
|
71
|
+
baseData,
|
|
72
|
+
encodeVarint(0)
|
|
73
|
+
]);
|
|
74
|
+
} else {
|
|
75
|
+
const typeBytes = fromUTF(mimeType);
|
|
76
|
+
data = concat([
|
|
77
|
+
baseData,
|
|
78
|
+
encodeVarint(typeBytes.byteLength),
|
|
79
|
+
typeBytes
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
super(AttachmentType.BINARY, data);
|
|
83
|
+
this.#contentLength = contentLength;
|
|
84
|
+
this.#digest = digest;
|
|
85
|
+
this.#mimeType = mimeType;
|
|
86
|
+
}
|
|
87
|
+
get contentLength() {
|
|
88
|
+
return this.#contentLength;
|
|
89
|
+
}
|
|
90
|
+
get digest() {
|
|
91
|
+
return this.#digest;
|
|
92
|
+
}
|
|
93
|
+
get mimeType() {
|
|
94
|
+
return this.#mimeType;
|
|
95
|
+
}
|
|
96
|
+
}
|
package/lib/base.js
CHANGED
|
@@ -1 +1,70 @@
|
|
|
1
|
-
|
|
1
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
2
|
+
import { concat, equals } from './bytes.js';
|
|
3
|
+
import { encodeVarint } from './utils.js';
|
|
4
|
+
export const NAMESPACE_CODE = 0xb5;
|
|
5
|
+
const NAMESPACE_BYTES = encodeVarint(NAMESPACE_CODE);
|
|
6
|
+
// Immutable
|
|
7
|
+
export var AttachmentType = /*#__PURE__*/ function(AttachmentType) {
|
|
8
|
+
AttachmentType[AttachmentType["BINARY"] = 1] = "BINARY";
|
|
9
|
+
return AttachmentType;
|
|
10
|
+
}({});
|
|
11
|
+
export var ModelType = /*#__PURE__*/ function(ModelType) {
|
|
12
|
+
// DAG-only
|
|
13
|
+
ModelType[ModelType["SCALAR"] = 10] = "SCALAR";
|
|
14
|
+
ModelType[ModelType["SHAPE"] = 11] = "SHAPE";
|
|
15
|
+
// Circular
|
|
16
|
+
ModelType[ModelType["DOCUMENT"] = 20] = "DOCUMENT";
|
|
17
|
+
ModelType[ModelType["CLUSTER"] = 21] = "CLUSTER";
|
|
18
|
+
return ModelType;
|
|
19
|
+
}({});
|
|
20
|
+
// Mutable
|
|
21
|
+
export var DocumentType = /*#__PURE__*/ function(DocumentType) {
|
|
22
|
+
DocumentType[DocumentType["MERGE_DOCUMENT"] = 30] = "MERGE_DOCUMENT";
|
|
23
|
+
return DocumentType;
|
|
24
|
+
}({});
|
|
25
|
+
// External
|
|
26
|
+
export var ExternalType = /*#__PURE__*/ function(ExternalType) {
|
|
27
|
+
ExternalType[ExternalType["PLUGIN_NODE"] = 40] = "PLUGIN_NODE";
|
|
28
|
+
return ExternalType;
|
|
29
|
+
}({});
|
|
30
|
+
export class KubunID {
|
|
31
|
+
#type;
|
|
32
|
+
#dataBytes;
|
|
33
|
+
#idBytes;
|
|
34
|
+
#string;
|
|
35
|
+
constructor(type, dataBytes){
|
|
36
|
+
this.#type = type;
|
|
37
|
+
this.#dataBytes = dataBytes;
|
|
38
|
+
}
|
|
39
|
+
get type() {
|
|
40
|
+
return this.#type;
|
|
41
|
+
}
|
|
42
|
+
get data() {
|
|
43
|
+
return this.#dataBytes;
|
|
44
|
+
}
|
|
45
|
+
get bytes() {
|
|
46
|
+
if (this.#idBytes == null) {
|
|
47
|
+
this.#idBytes = this.toBytes();
|
|
48
|
+
}
|
|
49
|
+
return this.#idBytes;
|
|
50
|
+
}
|
|
51
|
+
equals(other) {
|
|
52
|
+
return other instanceof KubunID && other.type === this.type && equals(other.data, this.data);
|
|
53
|
+
}
|
|
54
|
+
toBytes() {
|
|
55
|
+
return concat([
|
|
56
|
+
NAMESPACE_BYTES,
|
|
57
|
+
encodeVarint(this.#type),
|
|
58
|
+
this.#dataBytes
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
toString() {
|
|
62
|
+
if (this.#string == null) {
|
|
63
|
+
this.#string = base36.encode(this.bytes);
|
|
64
|
+
}
|
|
65
|
+
return this.#string;
|
|
66
|
+
}
|
|
67
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
68
|
+
return `KubunID(${this.toString()})`;
|
|
69
|
+
}
|
|
70
|
+
}
|
package/lib/bytes.js
CHANGED
|
@@ -1 +1,24 @@
|
|
|
1
|
-
export function concat(
|
|
1
|
+
export function concat(arrays, providedLength) {
|
|
2
|
+
const length = providedLength ?? arrays.reduce((acc, curr)=>acc + curr.length, 0);
|
|
3
|
+
const output = new Uint8Array(length);
|
|
4
|
+
let offset = 0;
|
|
5
|
+
for (const arr of arrays){
|
|
6
|
+
output.set(arr, offset);
|
|
7
|
+
offset += arr.length;
|
|
8
|
+
}
|
|
9
|
+
return output;
|
|
10
|
+
}
|
|
11
|
+
export function equals(a, b) {
|
|
12
|
+
if (a === b) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
if (a.byteLength !== b.byteLength) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
for(let i = 0; i < a.byteLength; i++){
|
|
19
|
+
if (a[i] !== b[i]) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
}
|
package/lib/cluster-model.js
CHANGED
|
@@ -1 +1,26 @@
|
|
|
1
|
-
import{base36
|
|
1
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
2
|
+
import { KubunID, ModelType, NAMESPACE_CODE } from './base.js';
|
|
3
|
+
import { decodeVarint } from './utils.js';
|
|
4
|
+
export class ClusterModelID extends KubunID {
|
|
5
|
+
static fromDigest(digestBytes) {
|
|
6
|
+
return new ClusterModelID(digestBytes);
|
|
7
|
+
}
|
|
8
|
+
static fromBytes(input) {
|
|
9
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
10
|
+
if (namespace !== NAMESPACE_CODE) {
|
|
11
|
+
throw new Error('Invalid KubunID: namespace mismatch');
|
|
12
|
+
}
|
|
13
|
+
const [type, typeLength] = decodeVarint(input, namespaceLength);
|
|
14
|
+
if (type !== ModelType.CLUSTER) {
|
|
15
|
+
throw new Error('Invalid KubunID: type mismatch');
|
|
16
|
+
}
|
|
17
|
+
const digestBytes = input.slice(namespaceLength + typeLength);
|
|
18
|
+
return new ClusterModelID(digestBytes);
|
|
19
|
+
}
|
|
20
|
+
static fromString(input) {
|
|
21
|
+
return ClusterModelID.fromBytes(base36.decode(input));
|
|
22
|
+
}
|
|
23
|
+
constructor(digestBytes){
|
|
24
|
+
super(ModelType.CLUSTER, digestBytes);
|
|
25
|
+
}
|
|
26
|
+
}
|
package/lib/decode.js
CHANGED
|
@@ -1 +1,34 @@
|
|
|
1
|
-
import{base36
|
|
1
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
2
|
+
import { AttachmentID } from './attachment.js';
|
|
3
|
+
import { AttachmentType, DocumentType, ExternalType, ModelType, NAMESPACE_CODE } from './base.js';
|
|
4
|
+
import { ClusterModelID } from './cluster-model.js';
|
|
5
|
+
import { DocumentID } from './document.js';
|
|
6
|
+
import { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
|
|
7
|
+
import { PluginNodeID } from './plugin-node.js';
|
|
8
|
+
import { decodeVarint } from './utils.js';
|
|
9
|
+
export function decodeID(input) {
|
|
10
|
+
const bytes = typeof input === 'string' ? base36.decode(input) : input;
|
|
11
|
+
const [namespace, namespaceLength] = decodeVarint(bytes);
|
|
12
|
+
if (namespace !== NAMESPACE_CODE) {
|
|
13
|
+
throw new Error('Invalid KubunID: namespace mismatch');
|
|
14
|
+
}
|
|
15
|
+
const [type] = decodeVarint(bytes, namespaceLength);
|
|
16
|
+
switch(type){
|
|
17
|
+
case AttachmentType.BINARY:
|
|
18
|
+
return AttachmentID.fromBytes(bytes);
|
|
19
|
+
case ModelType.SCALAR:
|
|
20
|
+
return ScalarModelID.fromBytes(bytes);
|
|
21
|
+
case ModelType.SHAPE:
|
|
22
|
+
return ShapeModelID.fromBytes(bytes);
|
|
23
|
+
case ModelType.DOCUMENT:
|
|
24
|
+
return DocumentModelID.fromBytes(bytes);
|
|
25
|
+
case ModelType.CLUSTER:
|
|
26
|
+
return ClusterModelID.fromBytes(bytes);
|
|
27
|
+
case DocumentType.MERGE_DOCUMENT:
|
|
28
|
+
return DocumentID.fromBytes(bytes);
|
|
29
|
+
case ExternalType.PLUGIN_NODE:
|
|
30
|
+
return PluginNodeID.fromBytes(bytes);
|
|
31
|
+
default:
|
|
32
|
+
throw new Error(`Invalid KubunID: unknown type ${type}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
package/lib/document.js
CHANGED
|
@@ -1 +1,81 @@
|
|
|
1
|
-
import{fromUTF
|
|
1
|
+
import { fromUTF } from '@sozai/codec';
|
|
2
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
3
|
+
import { DocumentType, KubunID, NAMESPACE_CODE } from './base.js';
|
|
4
|
+
import { concat } from './bytes.js';
|
|
5
|
+
import { DocumentModelID } from './model.js';
|
|
6
|
+
import { decodeVarint, digestBytes, encodeVarint } from './utils.js';
|
|
7
|
+
export class DocumentID extends KubunID {
|
|
8
|
+
static create(model, owner, unique) {
|
|
9
|
+
const contentBytes = concat([
|
|
10
|
+
typeof owner === 'string' ? fromUTF(owner) : owner,
|
|
11
|
+
unique
|
|
12
|
+
]);
|
|
13
|
+
return new DocumentID(DocumentModelID.from(model), digestBytes(contentBytes));
|
|
14
|
+
}
|
|
15
|
+
static fromBytes(input) {
|
|
16
|
+
let offset = 0;
|
|
17
|
+
// Check namespace = is it a KubunID?
|
|
18
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
19
|
+
if (namespace !== NAMESPACE_CODE) {
|
|
20
|
+
throw new Error('Invalid KubunID: namespace mismatch');
|
|
21
|
+
}
|
|
22
|
+
offset += namespaceLength;
|
|
23
|
+
// Check type matches
|
|
24
|
+
const [type, typeLength] = decodeVarint(input, offset);
|
|
25
|
+
if (type !== DocumentType.MERGE_DOCUMENT) {
|
|
26
|
+
throw new Error('Invalid KubunID: type mismatch');
|
|
27
|
+
}
|
|
28
|
+
offset += typeLength;
|
|
29
|
+
// Decode model ID: digest bytes followed by index
|
|
30
|
+
const [modelDataByteLength, modelDataVarintLength] = decodeVarint(input, offset);
|
|
31
|
+
offset += modelDataVarintLength;
|
|
32
|
+
const modelDigest = input.slice(offset, offset + modelDataByteLength);
|
|
33
|
+
offset += modelDataByteLength;
|
|
34
|
+
const [modelIndex, modelIndexVarintLength] = decodeVarint(input, offset);
|
|
35
|
+
offset += modelIndexVarintLength;
|
|
36
|
+
const modelID = new DocumentModelID(modelDigest, modelIndex);
|
|
37
|
+
// Extract content digest bytes
|
|
38
|
+
const [contentByteLength, contentVarintLength] = decodeVarint(input, offset);
|
|
39
|
+
offset += contentVarintLength;
|
|
40
|
+
const contentBytes = input.slice(offset, offset + contentByteLength);
|
|
41
|
+
return new DocumentID(modelID, contentBytes);
|
|
42
|
+
}
|
|
43
|
+
static fromString(input) {
|
|
44
|
+
return DocumentID.fromBytes(base36.decode(input));
|
|
45
|
+
}
|
|
46
|
+
static from(input) {
|
|
47
|
+
if (input instanceof DocumentID) {
|
|
48
|
+
return input;
|
|
49
|
+
}
|
|
50
|
+
if (input instanceof Uint8Array) {
|
|
51
|
+
return DocumentID.fromBytes(input);
|
|
52
|
+
}
|
|
53
|
+
if (typeof input === 'string') {
|
|
54
|
+
return DocumentID.fromString(input);
|
|
55
|
+
}
|
|
56
|
+
throw new Error('Unsupported input type');
|
|
57
|
+
}
|
|
58
|
+
#model;
|
|
59
|
+
#content;
|
|
60
|
+
constructor(model, contentDigestBytes){
|
|
61
|
+
if (model.isLocal) {
|
|
62
|
+
throw new Error('Invalid DocumentID: model cannot be local');
|
|
63
|
+
}
|
|
64
|
+
const data = concat([
|
|
65
|
+
encodeVarint(model.data.byteLength),
|
|
66
|
+
model.data,
|
|
67
|
+
encodeVarint(model.index),
|
|
68
|
+
encodeVarint(contentDigestBytes.byteLength),
|
|
69
|
+
contentDigestBytes
|
|
70
|
+
]);
|
|
71
|
+
super(DocumentType.MERGE_DOCUMENT, data);
|
|
72
|
+
this.#model = model;
|
|
73
|
+
this.#content = contentDigestBytes;
|
|
74
|
+
}
|
|
75
|
+
get model() {
|
|
76
|
+
return this.#model;
|
|
77
|
+
}
|
|
78
|
+
get content() {
|
|
79
|
+
return this.#content;
|
|
80
|
+
}
|
|
81
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
-
export{AttachmentID}from
|
|
1
|
+
export { AttachmentID } from './attachment.js';
|
|
2
|
+
export { KubunID } from './base.js';
|
|
3
|
+
export { ClusterModelID } from './cluster-model.js';
|
|
4
|
+
export { decodeID } from './decode.js';
|
|
5
|
+
export { DocumentID } from './document.js';
|
|
6
|
+
export { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
|
|
7
|
+
export { PluginNodeID } from './plugin-node.js';
|
|
8
|
+
export { digestBytes, digestJSON } from './utils.js';
|
package/lib/model.js
CHANGED
|
@@ -1 +1,111 @@
|
|
|
1
|
-
import{base36
|
|
1
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
2
|
+
import { KubunID, ModelType, NAMESPACE_CODE } from './base.js';
|
|
3
|
+
import { concat } from './bytes.js';
|
|
4
|
+
import { decodeVarint, digestJSON, encodeVarint } from './utils.js';
|
|
5
|
+
export class ScalarModelID extends KubunID {
|
|
6
|
+
static create(value) {
|
|
7
|
+
return new ScalarModelID(digestJSON(value));
|
|
8
|
+
}
|
|
9
|
+
static fromBytes(input) {
|
|
10
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
11
|
+
if (namespace !== NAMESPACE_CODE) {
|
|
12
|
+
throw new Error('Invalid KubunID: namespace mismatch');
|
|
13
|
+
}
|
|
14
|
+
const [type, typeLength] = decodeVarint(input, namespaceLength);
|
|
15
|
+
if (type !== ModelType.SCALAR) {
|
|
16
|
+
throw new Error('Invalid KubunID: type mismatch');
|
|
17
|
+
}
|
|
18
|
+
const digestBytes = input.slice(namespaceLength + typeLength);
|
|
19
|
+
return new ScalarModelID(digestBytes);
|
|
20
|
+
}
|
|
21
|
+
static fromString(input) {
|
|
22
|
+
return ScalarModelID.fromBytes(base36.decode(input));
|
|
23
|
+
}
|
|
24
|
+
constructor(digestBytes){
|
|
25
|
+
super(ModelType.SCALAR, digestBytes);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class ShapeModelID extends KubunID {
|
|
29
|
+
static create(value) {
|
|
30
|
+
return new ShapeModelID(digestJSON(value));
|
|
31
|
+
}
|
|
32
|
+
static fromBytes(input) {
|
|
33
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
34
|
+
if (namespace !== NAMESPACE_CODE) {
|
|
35
|
+
throw new Error('Invalid KubunID: namespace mismatch');
|
|
36
|
+
}
|
|
37
|
+
const [type, typeLength] = decodeVarint(input, namespaceLength);
|
|
38
|
+
if (type !== ModelType.SHAPE) {
|
|
39
|
+
throw new Error('Invalid KubunID: type mismatch');
|
|
40
|
+
}
|
|
41
|
+
const digestBytes = input.slice(namespaceLength + typeLength);
|
|
42
|
+
return new ShapeModelID(digestBytes);
|
|
43
|
+
}
|
|
44
|
+
static fromString(input) {
|
|
45
|
+
return ShapeModelID.fromBytes(base36.decode(input));
|
|
46
|
+
}
|
|
47
|
+
constructor(digestBytes){
|
|
48
|
+
super(ModelType.SHAPE, digestBytes);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const MAX_INDEX = 127;
|
|
52
|
+
export class DocumentModelID extends KubunID {
|
|
53
|
+
static fromBytes(input) {
|
|
54
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
55
|
+
if (namespace !== NAMESPACE_CODE) {
|
|
56
|
+
throw new Error('Invalid KubunID: namespace mismatch');
|
|
57
|
+
}
|
|
58
|
+
const [type, typeLength] = decodeVarint(input, namespaceLength);
|
|
59
|
+
if (type !== ModelType.DOCUMENT) {
|
|
60
|
+
throw new Error('Invalid KubunID: type mismatch');
|
|
61
|
+
}
|
|
62
|
+
const digestBytes = input.slice(namespaceLength + typeLength, input.byteLength - 1);
|
|
63
|
+
const [index] = decodeVarint(input, input.byteLength - 1);
|
|
64
|
+
return new DocumentModelID(digestBytes, index);
|
|
65
|
+
}
|
|
66
|
+
static fromString(input) {
|
|
67
|
+
return DocumentModelID.fromBytes(base36.decode(input));
|
|
68
|
+
}
|
|
69
|
+
static from(input) {
|
|
70
|
+
if (input instanceof DocumentModelID) {
|
|
71
|
+
return input;
|
|
72
|
+
}
|
|
73
|
+
if (input instanceof Uint8Array) {
|
|
74
|
+
return DocumentModelID.fromBytes(input);
|
|
75
|
+
}
|
|
76
|
+
if (typeof input === 'string') {
|
|
77
|
+
return DocumentModelID.fromString(input);
|
|
78
|
+
}
|
|
79
|
+
throw new Error('Unsupported input type');
|
|
80
|
+
}
|
|
81
|
+
static local(index) {
|
|
82
|
+
return new DocumentModelID(new Uint8Array(0), index);
|
|
83
|
+
}
|
|
84
|
+
#index;
|
|
85
|
+
#isLocal;
|
|
86
|
+
constructor(digestBytes, index){
|
|
87
|
+
if (index > MAX_INDEX) {
|
|
88
|
+
// index should be encoded in a single byte
|
|
89
|
+
throw new RangeError(`Index ${index} exceeds maximum of ${MAX_INDEX}`);
|
|
90
|
+
}
|
|
91
|
+
super(ModelType.DOCUMENT, digestBytes);
|
|
92
|
+
this.#index = index;
|
|
93
|
+
this.#isLocal = digestBytes.length === 0;
|
|
94
|
+
}
|
|
95
|
+
get index() {
|
|
96
|
+
return this.#index;
|
|
97
|
+
}
|
|
98
|
+
get isLocal() {
|
|
99
|
+
return this.#isLocal;
|
|
100
|
+
}
|
|
101
|
+
toBytes() {
|
|
102
|
+
return concat([
|
|
103
|
+
super.toBytes(),
|
|
104
|
+
encodeVarint(this.#index)
|
|
105
|
+
]);
|
|
106
|
+
}
|
|
107
|
+
toGlobal(digestBytesOrModelID) {
|
|
108
|
+
const digestBytes = digestBytesOrModelID instanceof DocumentModelID ? digestBytesOrModelID.data : digestBytesOrModelID;
|
|
109
|
+
return new DocumentModelID(digestBytes, this.#index);
|
|
110
|
+
}
|
|
111
|
+
}
|
package/lib/plugin-node.js
CHANGED
|
@@ -1 +1,84 @@
|
|
|
1
|
-
import{fromUTF
|
|
1
|
+
import { fromUTF, toUTF } from '@sozai/codec';
|
|
2
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
3
|
+
import { ExternalType, KubunID, NAMESPACE_CODE } from './base.js';
|
|
4
|
+
import { concat } from './bytes.js';
|
|
5
|
+
import { decodeVarint, encodeVarint } from './utils.js';
|
|
6
|
+
export class PluginNodeID extends KubunID {
|
|
7
|
+
static create(plugin, typeName, localID) {
|
|
8
|
+
return new PluginNodeID(plugin, typeName, localID);
|
|
9
|
+
}
|
|
10
|
+
static fromBytes(input) {
|
|
11
|
+
let offset = 0;
|
|
12
|
+
// Check namespace
|
|
13
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
14
|
+
if (namespace !== NAMESPACE_CODE) {
|
|
15
|
+
throw new Error('Invalid KubunID: namespace mismatch');
|
|
16
|
+
}
|
|
17
|
+
offset += namespaceLength;
|
|
18
|
+
// Check type matches
|
|
19
|
+
const [type, typeLength] = decodeVarint(input, offset);
|
|
20
|
+
if (type !== ExternalType.PLUGIN_NODE) {
|
|
21
|
+
throw new Error('Invalid KubunID: type mismatch');
|
|
22
|
+
}
|
|
23
|
+
offset += typeLength;
|
|
24
|
+
// Decode plugin
|
|
25
|
+
const [pluginLength, pluginVarintLength] = decodeVarint(input, offset);
|
|
26
|
+
offset += pluginVarintLength;
|
|
27
|
+
const plugin = pluginLength === 0 ? '' : toUTF(input.slice(offset, offset + pluginLength));
|
|
28
|
+
offset += pluginLength;
|
|
29
|
+
// Decode typeName
|
|
30
|
+
const [typeNameLength, typeNameVarintLength] = decodeVarint(input, offset);
|
|
31
|
+
offset += typeNameVarintLength;
|
|
32
|
+
const typeName = typeNameLength === 0 ? '' : toUTF(input.slice(offset, offset + typeNameLength));
|
|
33
|
+
offset += typeNameLength;
|
|
34
|
+
// Decode localID
|
|
35
|
+
const [localIDLength, localIDVarintLength] = decodeVarint(input, offset);
|
|
36
|
+
offset += localIDVarintLength;
|
|
37
|
+
const localID = localIDLength === 0 ? '' : toUTF(input.slice(offset, offset + localIDLength));
|
|
38
|
+
return new PluginNodeID(plugin, typeName, localID);
|
|
39
|
+
}
|
|
40
|
+
static fromString(input) {
|
|
41
|
+
return PluginNodeID.fromBytes(base36.decode(input));
|
|
42
|
+
}
|
|
43
|
+
static from(input) {
|
|
44
|
+
if (input instanceof PluginNodeID) {
|
|
45
|
+
return input;
|
|
46
|
+
}
|
|
47
|
+
if (input instanceof Uint8Array) {
|
|
48
|
+
return PluginNodeID.fromBytes(input);
|
|
49
|
+
}
|
|
50
|
+
if (typeof input === 'string') {
|
|
51
|
+
return PluginNodeID.fromString(input);
|
|
52
|
+
}
|
|
53
|
+
throw new Error('Unsupported input type');
|
|
54
|
+
}
|
|
55
|
+
#plugin;
|
|
56
|
+
#typeName;
|
|
57
|
+
#localID;
|
|
58
|
+
constructor(plugin, typeName, localID){
|
|
59
|
+
const pluginBytes = fromUTF(plugin);
|
|
60
|
+
const typeNameBytes = fromUTF(typeName);
|
|
61
|
+
const localIDBytes = fromUTF(localID);
|
|
62
|
+
const data = concat([
|
|
63
|
+
encodeVarint(pluginBytes.byteLength),
|
|
64
|
+
pluginBytes,
|
|
65
|
+
encodeVarint(typeNameBytes.byteLength),
|
|
66
|
+
typeNameBytes,
|
|
67
|
+
encodeVarint(localIDBytes.byteLength),
|
|
68
|
+
localIDBytes
|
|
69
|
+
]);
|
|
70
|
+
super(ExternalType.PLUGIN_NODE, data);
|
|
71
|
+
this.#plugin = plugin;
|
|
72
|
+
this.#typeName = typeName;
|
|
73
|
+
this.#localID = localID;
|
|
74
|
+
}
|
|
75
|
+
get plugin() {
|
|
76
|
+
return this.#plugin;
|
|
77
|
+
}
|
|
78
|
+
get typeName() {
|
|
79
|
+
return this.#typeName;
|
|
80
|
+
}
|
|
81
|
+
get localID() {
|
|
82
|
+
return this.#localID;
|
|
83
|
+
}
|
|
84
|
+
}
|
package/lib/utils.js
CHANGED
|
@@ -1 +1,19 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { blake3 } from '@noble/hashes/blake3.js';
|
|
2
|
+
import { canonicalStringify, fromUTF } from '@sozai/codec';
|
|
3
|
+
import varint from 'varint';
|
|
4
|
+
export function digestBytes(bytes) {
|
|
5
|
+
return blake3(bytes);
|
|
6
|
+
}
|
|
7
|
+
export function digestJSON(value) {
|
|
8
|
+
return digestBytes(fromUTF(canonicalStringify(value)));
|
|
9
|
+
}
|
|
10
|
+
export function decodeVarint(bytes, offset) {
|
|
11
|
+
const value = varint.decode(bytes, offset);
|
|
12
|
+
return [
|
|
13
|
+
value,
|
|
14
|
+
varint.decode.bytes ?? 0
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
export function encodeVarint(value) {
|
|
18
|
+
return varint.encode(value, new Uint8Array(varint.encodingLength(value)));
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/id",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@
|
|
18
|
+
"@sozai/codec": "^0.1.0",
|
|
19
19
|
"@noble/hashes": "^2.2.0",
|
|
20
|
-
"multiformats": "^14.0.
|
|
20
|
+
"multiformats": "^14.0.4",
|
|
21
21
|
"varint": "^6.0.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
27
|
"build:clean": "del lib",
|
|
28
|
-
"build:js": "swc src -d ./lib --config-file ../../swc.json --strip-leading-paths",
|
|
28
|
+
"build:js": "swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
|
|
29
29
|
"build:types": "tsc --emitDeclarationOnly --skipLibCheck",
|
|
30
30
|
"build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
|
|
31
31
|
"build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
|