@kubun/id 0.1.0 → 0.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/lib/attachment.d.ts +2 -8
- package/lib/attachment.d.ts.map +1 -1
- package/lib/attachment.js +23 -36
- package/lib/base.d.ts +2 -3
- package/lib/base.d.ts.map +1 -1
- package/lib/base.js +17 -18
- package/lib/bytes.d.ts +3 -0
- package/lib/bytes.d.ts.map +1 -0
- package/lib/bytes.js +24 -0
- package/lib/decode.d.ts.map +1 -1
- package/lib/decode.js +7 -6
- package/lib/document.d.ts +4 -4
- package/lib/document.d.ts.map +1 -1
- package/lib/document.js +13 -11
- package/lib/index.d.ts +3 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -3
- package/lib/model.d.ts +6 -6
- package/lib/model.d.ts.map +1 -1
- package/lib/model.js +24 -24
- package/lib/utils.d.ts +3 -6
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +15 -13
- package/package.json +8 -4
- package/lib/common.d.ts +0 -10
- package/lib/common.d.ts.map +0 -1
- package/lib/common.js +0 -9
- package/lib/types.d.ts +0 -3
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -1
package/lib/attachment.d.ts
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import { AttachmentType, KubunID } from './base.js';
|
|
2
|
-
import { CID } from './common.js';
|
|
3
|
-
import type { SHA256Digest } from './types.js';
|
|
4
|
-
export type AttachmentCID = CID<Uint8Array, 85, 18, 1>;
|
|
5
|
-
export declare function digestAttachment(value: Uint8Array): SHA256Digest;
|
|
6
|
-
export declare function createAttachmentCID(digest: SHA256Digest): AttachmentCID;
|
|
7
2
|
export declare class AttachmentID extends KubunID<AttachmentType.BINARY> {
|
|
8
3
|
#private;
|
|
9
4
|
static create(value: Uint8Array, mimeType?: string): AttachmentID;
|
|
10
5
|
static fromBytes(input: Uint8Array): AttachmentID;
|
|
11
|
-
static fromDigest(digest: SHA256Digest, contentLength: number, mimeType?: string): AttachmentID;
|
|
12
6
|
static fromString(input: string): AttachmentID;
|
|
13
7
|
static from(input: AttachmentID | Uint8Array | string): AttachmentID;
|
|
14
|
-
constructor(
|
|
15
|
-
get cid(): CID;
|
|
8
|
+
constructor(digest: Uint8Array, contentLength: number, mimeType?: string);
|
|
16
9
|
get contentLength(): number;
|
|
10
|
+
get digest(): Uint8Array;
|
|
17
11
|
get mimeType(): string;
|
|
18
12
|
}
|
|
19
13
|
//# sourceMappingURL=attachment.d.ts.map
|
package/lib/attachment.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"attachment.d.ts","sourceRoot":"","sources":["../src/attachment.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"attachment.d.ts","sourceRoot":"","sources":["../src/attachment.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAkB,MAAM,WAAW,CAAA;AAMnE,qBAAa,YAAa,SAAQ,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC;;IAC9D,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,YAAY;IAIjE,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY;IAwCjD,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;IAI9C,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,GAAG,UAAU,GAAG,MAAM,GAAG,YAAY;gBAiBxD,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,SAAoB;IAenF,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED,IAAI,MAAM,IAAI,UAAU,CAEvB;IAED,IAAI,QAAQ,IAAI,MAAM,CAErB;CACF"}
|
package/lib/attachment.js
CHANGED
|
@@ -1,57 +1,45 @@
|
|
|
1
|
+
import { fromUTF, toUTF } from '@enkaku/codec';
|
|
2
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
1
3
|
import { AttachmentType, KubunID, NAMESPACE_CODE } from './base.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
+
import { concat } from './bytes.js';
|
|
5
|
+
import { decodeVarint, digestBytes, encodeVarint } from './utils.js';
|
|
4
6
|
const DEFAULT_MIME_TYPE = 'application/octet-stream';
|
|
5
|
-
export function digestAttachment(value) {
|
|
6
|
-
const bytes = encodeRaw(value);
|
|
7
|
-
return toSHA256Digest(bytes);
|
|
8
|
-
}
|
|
9
|
-
export function createAttachmentCID(digest) {
|
|
10
|
-
return CID.createV1(85, digest);
|
|
11
|
-
}
|
|
12
7
|
export class AttachmentID extends KubunID {
|
|
13
8
|
static create(value, mimeType) {
|
|
14
|
-
|
|
15
|
-
return AttachmentID.fromDigest(digest, value.byteLength, mimeType);
|
|
9
|
+
return new AttachmentID(digestBytes(value), value.byteLength, mimeType);
|
|
16
10
|
}
|
|
17
11
|
static fromBytes(input) {
|
|
18
12
|
let offset = 0;
|
|
19
13
|
// Check namespace = is it a KubunID?
|
|
20
|
-
const [namespace, namespaceLength] =
|
|
14
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
21
15
|
if (namespace !== NAMESPACE_CODE) {
|
|
22
16
|
throw new Error('Invalid KubunID: namespace mismatch');
|
|
23
17
|
}
|
|
24
18
|
offset += namespaceLength;
|
|
25
19
|
// Check type matches
|
|
26
|
-
const [type, typeLength] =
|
|
20
|
+
const [type, typeLength] = decodeVarint(input, offset);
|
|
27
21
|
if (type !== AttachmentType.BINARY) {
|
|
28
22
|
throw new Error('Invalid KubunID: type mismatch');
|
|
29
23
|
}
|
|
30
24
|
offset += typeLength;
|
|
31
25
|
// Decode digest
|
|
32
|
-
const [
|
|
26
|
+
const [digestLength, digestVarintLength] = decodeVarint(input, offset);
|
|
33
27
|
offset += digestVarintLength;
|
|
34
|
-
const
|
|
35
|
-
offset +=
|
|
36
|
-
const digest = decodeDigest(digestBytes);
|
|
28
|
+
const digest = input.slice(offset, offset + digestLength);
|
|
29
|
+
offset += digestLength;
|
|
37
30
|
// Extract content length
|
|
38
|
-
const [contentLength, contentVarintLength] =
|
|
31
|
+
const [contentLength, contentVarintLength] = decodeVarint(input, offset);
|
|
39
32
|
offset += contentVarintLength;
|
|
40
33
|
// Check if MIME type is provided
|
|
41
|
-
const [mimeTypeBytesLength, mimeTypeVarintLength] =
|
|
34
|
+
const [mimeTypeBytesLength, mimeTypeVarintLength] = decodeVarint(input, offset);
|
|
42
35
|
if (mimeTypeBytesLength === 0) {
|
|
43
36
|
// No MIME type encoded, use default
|
|
44
|
-
return AttachmentID
|
|
37
|
+
return new AttachmentID(digest, contentLength);
|
|
45
38
|
}
|
|
46
39
|
// Decode MIME type value
|
|
47
40
|
offset += mimeTypeVarintLength;
|
|
48
41
|
const mimeTypeBytes = input.slice(offset, offset + mimeTypeBytesLength);
|
|
49
|
-
|
|
50
|
-
return AttachmentID.fromDigest(digest, contentLength, mimeType);
|
|
51
|
-
}
|
|
52
|
-
static fromDigest(digest, contentLength, mimeType) {
|
|
53
|
-
const cid = createAttachmentCID(digest);
|
|
54
|
-
return new AttachmentID(cid, contentLength, mimeType);
|
|
42
|
+
return new AttachmentID(digest, contentLength, toUTF(mimeTypeBytes));
|
|
55
43
|
}
|
|
56
44
|
static fromString(input) {
|
|
57
45
|
return AttachmentID.fromBytes(base36.decode(input));
|
|
@@ -68,15 +56,14 @@ export class AttachmentID extends KubunID {
|
|
|
68
56
|
}
|
|
69
57
|
throw new Error('Unsupported input type');
|
|
70
58
|
}
|
|
71
|
-
#cid;
|
|
72
59
|
#contentLength;
|
|
60
|
+
#digest;
|
|
73
61
|
#mimeType;
|
|
74
|
-
constructor(
|
|
62
|
+
constructor(digest, contentLength, mimeType = DEFAULT_MIME_TYPE){
|
|
75
63
|
let data;
|
|
76
|
-
const digestBytes = cid.multihash.bytes;
|
|
77
64
|
const baseData = concat([
|
|
78
|
-
encodeVarint(
|
|
79
|
-
|
|
65
|
+
encodeVarint(digest.byteLength),
|
|
66
|
+
digest,
|
|
80
67
|
encodeVarint(contentLength)
|
|
81
68
|
]);
|
|
82
69
|
if (mimeType === DEFAULT_MIME_TYPE) {
|
|
@@ -85,7 +72,7 @@ export class AttachmentID extends KubunID {
|
|
|
85
72
|
encodeVarint(0)
|
|
86
73
|
]);
|
|
87
74
|
} else {
|
|
88
|
-
const typeBytes =
|
|
75
|
+
const typeBytes = fromUTF(mimeType);
|
|
89
76
|
data = concat([
|
|
90
77
|
baseData,
|
|
91
78
|
encodeVarint(typeBytes.byteLength),
|
|
@@ -93,16 +80,16 @@ export class AttachmentID extends KubunID {
|
|
|
93
80
|
]);
|
|
94
81
|
}
|
|
95
82
|
super(AttachmentType.BINARY, data);
|
|
96
|
-
this.#cid = cid;
|
|
97
83
|
this.#contentLength = contentLength;
|
|
84
|
+
this.#digest = digest;
|
|
98
85
|
this.#mimeType = mimeType;
|
|
99
86
|
}
|
|
100
|
-
get cid() {
|
|
101
|
-
return this.#cid;
|
|
102
|
-
}
|
|
103
87
|
get contentLength() {
|
|
104
88
|
return this.#contentLength;
|
|
105
89
|
}
|
|
90
|
+
get digest() {
|
|
91
|
+
return this.#digest;
|
|
92
|
+
}
|
|
106
93
|
get mimeType() {
|
|
107
94
|
return this.#mimeType;
|
|
108
95
|
}
|
package/lib/base.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
export declare const NAMESPACE_CODE = 2794;
|
|
1
|
+
export declare const NAMESPACE_CODE = 181;
|
|
3
2
|
export declare enum AttachmentType {
|
|
4
3
|
BINARY = 1
|
|
5
4
|
}
|
|
6
5
|
export declare enum ModelType {
|
|
7
6
|
SCALAR = 10,
|
|
8
7
|
SHAPE = 11,
|
|
9
|
-
|
|
8
|
+
DOCUMENT = 20
|
|
10
9
|
}
|
|
11
10
|
export declare enum DocumentType {
|
|
12
11
|
MERGE_DOCUMENT = 30
|
package/lib/base.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,cAAc,MAAO,CAAA;AAIlC,oBAAY,cAAc;IACxB,MAAM,IAAI;CACX;AACD,oBAAY,SAAS;IAEnB,MAAM,KAAK;IACX,KAAK,KAAK;IAEV,QAAQ,KAAK;CACd;AAED,oBAAY,YAAY;IACtB,cAAc,KAAK;CACpB;AAED,MAAM,MAAM,SAAS,GAAG,cAAc,GAAG,SAAS,GAAG,YAAY,CAAA;AAEjE,qBAAa,OAAO,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS;;gBAMtC,IAAI,EAAE,CAAC,EAAE,SAAS,EAAE,UAAU;IAK1C,IAAI,IAAI,IAAI,CAAC,CAEZ;IAED,IAAI,IAAI,IAAI,UAAU,CAErB;IAED,IAAI,KAAK,IAAI,UAAU,CAKtB;IAED,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO;IAI/B,OAAO,IAAI,UAAU;IAIrB,QAAQ;CAUT"}
|
package/lib/base.js
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
import { base36
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export const NAMESPACE_CODE = 0xaea;
|
|
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;
|
|
6
5
|
const NAMESPACE_BYTES = encodeVarint(NAMESPACE_CODE);
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
// Immutable
|
|
7
|
+
export var AttachmentType = /*#__PURE__*/ function(AttachmentType) {
|
|
9
8
|
AttachmentType[AttachmentType["BINARY"] = 1] = "BINARY";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
return AttachmentType;
|
|
10
|
+
}({});
|
|
11
|
+
export var ModelType = /*#__PURE__*/ function(ModelType) {
|
|
13
12
|
// DAG-only
|
|
14
13
|
ModelType[ModelType["SCALAR"] = 10] = "SCALAR";
|
|
15
14
|
ModelType[ModelType["SHAPE"] = 11] = "SHAPE";
|
|
16
15
|
// Circular
|
|
17
|
-
ModelType[ModelType["
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
ModelType[ModelType["DOCUMENT"] = 20] = "DOCUMENT";
|
|
17
|
+
return ModelType;
|
|
18
|
+
}({});
|
|
19
|
+
// Mutable
|
|
20
|
+
export var DocumentType = /*#__PURE__*/ function(DocumentType) {
|
|
21
21
|
DocumentType[DocumentType["MERGE_DOCUMENT"] = 30] = "MERGE_DOCUMENT";
|
|
22
|
-
|
|
22
|
+
return DocumentType;
|
|
23
|
+
}({});
|
|
23
24
|
export class KubunID {
|
|
24
25
|
#type;
|
|
25
26
|
#dataBytes;
|
|
@@ -37,9 +38,7 @@ export class KubunID {
|
|
|
37
38
|
}
|
|
38
39
|
get bytes() {
|
|
39
40
|
if (this.#idBytes == null) {
|
|
40
|
-
|
|
41
|
-
this.#idBytes = bytes;
|
|
42
|
-
return bytes;
|
|
41
|
+
this.#idBytes = this.toBytes();
|
|
43
42
|
}
|
|
44
43
|
return this.#idBytes;
|
|
45
44
|
}
|
package/lib/bytes.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,UAAU,CAShF;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAa5D"}
|
package/lib/bytes.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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/decode.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgC,KAAK,OAAO,EAA6B,MAAM,WAAW,CAAA;AAKjG,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAuB5D"}
|
package/lib/decode.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
1
2
|
import { AttachmentID } from './attachment.js';
|
|
2
3
|
import { AttachmentType, DocumentType, ModelType, NAMESPACE_CODE } from './base.js';
|
|
3
|
-
import { base36, varint } from './common.js';
|
|
4
4
|
import { DocumentID } from './document.js';
|
|
5
|
-
import {
|
|
5
|
+
import { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
|
|
6
|
+
import { decodeVarint } from './utils.js';
|
|
6
7
|
export function decodeID(input) {
|
|
7
8
|
const bytes = typeof input === 'string' ? base36.decode(input) : input;
|
|
8
|
-
const [namespace, namespaceLength] =
|
|
9
|
+
const [namespace, namespaceLength] = decodeVarint(bytes);
|
|
9
10
|
if (namespace !== NAMESPACE_CODE) {
|
|
10
11
|
throw new Error('Invalid KubunID: namespace mismatch');
|
|
11
12
|
}
|
|
12
|
-
const [type] =
|
|
13
|
+
const [type] = decodeVarint(bytes, namespaceLength);
|
|
13
14
|
switch(type){
|
|
14
15
|
case AttachmentType.BINARY:
|
|
15
16
|
return AttachmentID.fromBytes(bytes);
|
|
@@ -17,8 +18,8 @@ export function decodeID(input) {
|
|
|
17
18
|
return ScalarModelID.fromBytes(bytes);
|
|
18
19
|
case ModelType.SHAPE:
|
|
19
20
|
return ShapeModelID.fromBytes(bytes);
|
|
20
|
-
case ModelType.
|
|
21
|
-
return
|
|
21
|
+
case ModelType.DOCUMENT:
|
|
22
|
+
return DocumentModelID.fromBytes(bytes);
|
|
22
23
|
case DocumentType.MERGE_DOCUMENT:
|
|
23
24
|
return DocumentID.fromBytes(bytes);
|
|
24
25
|
default:
|
package/lib/document.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { DocumentType, KubunID } from './base.js';
|
|
2
|
-
import {
|
|
2
|
+
import { DocumentModelID } from './model.js';
|
|
3
3
|
export declare class DocumentID extends KubunID<DocumentType.MERGE_DOCUMENT> {
|
|
4
4
|
#private;
|
|
5
|
-
static create(model:
|
|
5
|
+
static create(model: DocumentModelID | Uint8Array | string, owner: string | Uint8Array, unique: Uint8Array): DocumentID;
|
|
6
6
|
static fromBytes(input: Uint8Array): DocumentID;
|
|
7
7
|
static fromString(input: string): DocumentID;
|
|
8
8
|
static from(input: DocumentID | Uint8Array | string): DocumentID;
|
|
9
|
-
constructor(model:
|
|
10
|
-
get model():
|
|
9
|
+
constructor(model: DocumentModelID, contentDigestBytes: Uint8Array);
|
|
10
|
+
get model(): DocumentModelID;
|
|
11
11
|
get content(): Uint8Array;
|
|
12
12
|
}
|
|
13
13
|
//# sourceMappingURL=document.d.ts.map
|
package/lib/document.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAkB,MAAM,WAAW,CAAA;AAEjE,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAG5C,qBAAa,UAAW,SAAQ,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC;;IAClE,MAAM,CAAC,MAAM,CACX,KAAK,EAAE,eAAe,GAAG,UAAU,GAAG,MAAM,EAC5C,KAAK,EAAE,MAAM,GAAG,UAAU,EAC1B,MAAM,EAAE,UAAU,GACjB,UAAU;IAKb,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU;IAkC/C,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU;IAI5C,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,GAAG,MAAM,GAAG,UAAU;gBAgBpD,KAAK,EAAE,eAAe,EAAE,kBAAkB,EAAE,UAAU;IAkBlE,IAAI,KAAK,IAAI,eAAe,CAE3B;IAED,IAAI,OAAO,IAAI,UAAU,CAExB;CACF"}
|
package/lib/document.js
CHANGED
|
@@ -1,39 +1,41 @@
|
|
|
1
|
+
import { fromUTF } from '@enkaku/codec';
|
|
2
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
1
3
|
import { DocumentType, KubunID, NAMESPACE_CODE } from './base.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { concat } from './bytes.js';
|
|
5
|
+
import { DocumentModelID } from './model.js';
|
|
6
|
+
import { decodeVarint, digestBytes, encodeVarint } from './utils.js';
|
|
5
7
|
export class DocumentID extends KubunID {
|
|
6
8
|
static create(model, owner, unique) {
|
|
7
9
|
const contentBytes = concat([
|
|
8
|
-
typeof owner === 'string' ?
|
|
10
|
+
typeof owner === 'string' ? fromUTF(owner) : owner,
|
|
9
11
|
unique
|
|
10
12
|
]);
|
|
11
|
-
return new DocumentID(
|
|
13
|
+
return new DocumentID(DocumentModelID.from(model), digestBytes(contentBytes));
|
|
12
14
|
}
|
|
13
15
|
static fromBytes(input) {
|
|
14
16
|
let offset = 0;
|
|
15
17
|
// Check namespace = is it a KubunID?
|
|
16
|
-
const [namespace, namespaceLength] =
|
|
18
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
17
19
|
if (namespace !== NAMESPACE_CODE) {
|
|
18
20
|
throw new Error('Invalid KubunID: namespace mismatch');
|
|
19
21
|
}
|
|
20
22
|
offset += namespaceLength;
|
|
21
23
|
// Check type matches
|
|
22
|
-
const [type, typeLength] =
|
|
24
|
+
const [type, typeLength] = decodeVarint(input, offset);
|
|
23
25
|
if (type !== DocumentType.MERGE_DOCUMENT) {
|
|
24
26
|
throw new Error('Invalid KubunID: type mismatch');
|
|
25
27
|
}
|
|
26
28
|
offset += typeLength;
|
|
27
29
|
// Decode model ID: digest bytes followed by index
|
|
28
|
-
const [modelDataByteLength, modelDataVarintLength] =
|
|
30
|
+
const [modelDataByteLength, modelDataVarintLength] = decodeVarint(input, offset);
|
|
29
31
|
offset += modelDataVarintLength;
|
|
30
32
|
const modelDigest = input.slice(offset, offset + modelDataByteLength);
|
|
31
33
|
offset += modelDataByteLength;
|
|
32
|
-
const [modelIndex, modelIndexVarintLength] =
|
|
34
|
+
const [modelIndex, modelIndexVarintLength] = decodeVarint(input, offset);
|
|
33
35
|
offset += modelIndexVarintLength;
|
|
34
|
-
const modelID = new
|
|
36
|
+
const modelID = new DocumentModelID(modelDigest, modelIndex);
|
|
35
37
|
// Extract content digest bytes
|
|
36
|
-
const [contentByteLength, contentVarintLength] =
|
|
38
|
+
const [contentByteLength, contentVarintLength] = decodeVarint(input, offset);
|
|
37
39
|
offset += contentVarintLength;
|
|
38
40
|
const contentBytes = input.slice(offset, offset + contentByteLength);
|
|
39
41
|
return new DocumentID(modelID, contentBytes);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { AttachmentID } from './attachment.js';
|
|
2
2
|
export { KubunID } from './base.js';
|
|
3
3
|
export { decodeID } from './decode.js';
|
|
4
4
|
export { DocumentID } from './document.js';
|
|
5
|
-
export {
|
|
6
|
-
export
|
|
7
|
-
export { toDigestBytes, toSHA256Digest } from './utils.js';
|
|
5
|
+
export { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
|
|
6
|
+
export { digestBytes, digestJSON } from './utils.js';
|
|
8
7
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AACzE,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA"}
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { AttachmentID
|
|
1
|
+
export { AttachmentID } from './attachment.js';
|
|
2
2
|
export { KubunID } from './base.js';
|
|
3
3
|
export { decodeID } from './decode.js';
|
|
4
4
|
export { DocumentID } from './document.js';
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
5
|
+
export { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
|
|
6
|
+
export { digestBytes, digestJSON } from './utils.js';
|
package/lib/model.d.ts
CHANGED
|
@@ -11,16 +11,16 @@ export declare class ShapeModelID extends KubunID<ModelType.SHAPE> {
|
|
|
11
11
|
static fromString(input: string): ShapeModelID;
|
|
12
12
|
constructor(digestBytes: Uint8Array);
|
|
13
13
|
}
|
|
14
|
-
export declare class
|
|
14
|
+
export declare class DocumentModelID extends KubunID<ModelType.DOCUMENT> {
|
|
15
15
|
#private;
|
|
16
|
-
static fromBytes(input: Uint8Array):
|
|
17
|
-
static fromString(input: string):
|
|
18
|
-
static from(input:
|
|
19
|
-
static local(index: number):
|
|
16
|
+
static fromBytes(input: Uint8Array): DocumentModelID;
|
|
17
|
+
static fromString(input: string): DocumentModelID;
|
|
18
|
+
static from(input: DocumentModelID | Uint8Array | string): DocumentModelID;
|
|
19
|
+
static local(index: number): DocumentModelID;
|
|
20
20
|
constructor(digestBytes: Uint8Array, index: number);
|
|
21
21
|
get index(): number;
|
|
22
22
|
get isLocal(): boolean;
|
|
23
23
|
toBytes(): Uint8Array;
|
|
24
|
-
toGlobal(digestBytesOrModelID: Uint8Array |
|
|
24
|
+
toGlobal(digestBytesOrModelID: Uint8Array | DocumentModelID): DocumentModelID;
|
|
25
25
|
}
|
|
26
26
|
//# sourceMappingURL=model.d.ts.map
|
package/lib/model.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAkB,MAAM,WAAW,CAAA;AAI9D,qBAAa,aAAc,SAAQ,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC;IAC1D,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa;IAI5C,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,aAAa;IAclD,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,aAAa;gBAInC,WAAW,EAAE,UAAU;CAGpC;AAED,qBAAa,YAAa,SAAQ,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC;IACxD,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY;IAI3C,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY;IAcjD,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;gBAIlC,WAAW,EAAE,UAAU;CAGpC;AAID,qBAAa,eAAgB,SAAQ,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC;;IAC9D,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe;IAepD,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe;IAIjD,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,eAAe,GAAG,UAAU,GAAG,MAAM,GAAG,eAAe;IAa1E,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe;gBAOhC,WAAW,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM;IAWlD,IAAI,KAAK,IAAI,MAAM,CAElB;IAED,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,OAAO,IAAI,UAAU;IAIrB,QAAQ,CAAC,oBAAoB,EAAE,UAAU,GAAG,eAAe,GAAG,eAAe;CAO9E"}
|
package/lib/model.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
1
2
|
import { KubunID, ModelType, NAMESPACE_CODE } from './base.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
3
|
+
import { concat } from './bytes.js';
|
|
4
|
+
import { decodeVarint, digestJSON, encodeVarint } from './utils.js';
|
|
4
5
|
export class ScalarModelID extends KubunID {
|
|
5
6
|
static create(value) {
|
|
6
|
-
return new ScalarModelID(
|
|
7
|
+
return new ScalarModelID(digestJSON(value));
|
|
7
8
|
}
|
|
8
9
|
static fromBytes(input) {
|
|
9
|
-
const [namespace, namespaceLength] =
|
|
10
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
10
11
|
if (namespace !== NAMESPACE_CODE) {
|
|
11
12
|
throw new Error('Invalid KubunID: namespace mismatch');
|
|
12
13
|
}
|
|
13
|
-
const [type, typeLength] =
|
|
14
|
+
const [type, typeLength] = decodeVarint(input, namespaceLength);
|
|
14
15
|
if (type !== ModelType.SCALAR) {
|
|
15
16
|
throw new Error('Invalid KubunID: type mismatch');
|
|
16
17
|
}
|
|
@@ -26,14 +27,14 @@ export class ScalarModelID extends KubunID {
|
|
|
26
27
|
}
|
|
27
28
|
export class ShapeModelID extends KubunID {
|
|
28
29
|
static create(value) {
|
|
29
|
-
return new ShapeModelID(
|
|
30
|
+
return new ShapeModelID(digestJSON(value));
|
|
30
31
|
}
|
|
31
32
|
static fromBytes(input) {
|
|
32
|
-
const [namespace, namespaceLength] =
|
|
33
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
33
34
|
if (namespace !== NAMESPACE_CODE) {
|
|
34
35
|
throw new Error('Invalid KubunID: namespace mismatch');
|
|
35
36
|
}
|
|
36
|
-
const [type, typeLength] =
|
|
37
|
+
const [type, typeLength] = decodeVarint(input, namespaceLength);
|
|
37
38
|
if (type !== ModelType.SHAPE) {
|
|
38
39
|
throw new Error('Invalid KubunID: type mismatch');
|
|
39
40
|
}
|
|
@@ -47,39 +48,38 @@ export class ShapeModelID extends KubunID {
|
|
|
47
48
|
super(ModelType.SHAPE, digestBytes);
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
|
-
const LOCAL_DIGEST_BYTES = identity.digest(encodeCBOR('local')).bytes;
|
|
51
51
|
const MAX_INDEX = 127;
|
|
52
|
-
export class
|
|
52
|
+
export class DocumentModelID extends KubunID {
|
|
53
53
|
static fromBytes(input) {
|
|
54
|
-
const [namespace, namespaceLength] =
|
|
54
|
+
const [namespace, namespaceLength] = decodeVarint(input);
|
|
55
55
|
if (namespace !== NAMESPACE_CODE) {
|
|
56
56
|
throw new Error('Invalid KubunID: namespace mismatch');
|
|
57
57
|
}
|
|
58
|
-
const [type, typeLength] =
|
|
59
|
-
if (type !== ModelType.
|
|
58
|
+
const [type, typeLength] = decodeVarint(input, namespaceLength);
|
|
59
|
+
if (type !== ModelType.DOCUMENT) {
|
|
60
60
|
throw new Error('Invalid KubunID: type mismatch');
|
|
61
61
|
}
|
|
62
62
|
const digestBytes = input.slice(namespaceLength + typeLength, input.byteLength - 1);
|
|
63
|
-
const [index] =
|
|
64
|
-
return new
|
|
63
|
+
const [index] = decodeVarint(input, input.byteLength - 1);
|
|
64
|
+
return new DocumentModelID(digestBytes, index);
|
|
65
65
|
}
|
|
66
66
|
static fromString(input) {
|
|
67
|
-
return
|
|
67
|
+
return DocumentModelID.fromBytes(base36.decode(input));
|
|
68
68
|
}
|
|
69
69
|
static from(input) {
|
|
70
|
-
if (input instanceof
|
|
70
|
+
if (input instanceof DocumentModelID) {
|
|
71
71
|
return input;
|
|
72
72
|
}
|
|
73
73
|
if (input instanceof Uint8Array) {
|
|
74
|
-
return
|
|
74
|
+
return DocumentModelID.fromBytes(input);
|
|
75
75
|
}
|
|
76
76
|
if (typeof input === 'string') {
|
|
77
|
-
return
|
|
77
|
+
return DocumentModelID.fromString(input);
|
|
78
78
|
}
|
|
79
79
|
throw new Error('Unsupported input type');
|
|
80
80
|
}
|
|
81
81
|
static local(index) {
|
|
82
|
-
return new
|
|
82
|
+
return new DocumentModelID(new Uint8Array(0), index);
|
|
83
83
|
}
|
|
84
84
|
#index;
|
|
85
85
|
#isLocal;
|
|
@@ -88,9 +88,9 @@ export class GraphModelID extends KubunID {
|
|
|
88
88
|
// index should be encoded in a single byte
|
|
89
89
|
throw new RangeError(`Index ${index} exceeds maximum of ${MAX_INDEX}`);
|
|
90
90
|
}
|
|
91
|
-
super(ModelType.
|
|
91
|
+
super(ModelType.DOCUMENT, digestBytes);
|
|
92
92
|
this.#index = index;
|
|
93
|
-
this.#isLocal =
|
|
93
|
+
this.#isLocal = digestBytes.length === 0;
|
|
94
94
|
}
|
|
95
95
|
get index() {
|
|
96
96
|
return this.#index;
|
|
@@ -105,7 +105,7 @@ export class GraphModelID extends KubunID {
|
|
|
105
105
|
]);
|
|
106
106
|
}
|
|
107
107
|
toGlobal(digestBytesOrModelID) {
|
|
108
|
-
const digestBytes = digestBytesOrModelID instanceof
|
|
109
|
-
return new
|
|
108
|
+
const digestBytes = digestBytesOrModelID instanceof DocumentModelID ? digestBytesOrModelID.data : digestBytesOrModelID;
|
|
109
|
+
return new DocumentModelID(digestBytes, this.#index);
|
|
110
110
|
}
|
|
111
111
|
}
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export declare function digestBytes(bytes: Uint8Array): Uint8Array;
|
|
2
|
+
export declare function digestJSON<T = unknown>(value: T): Uint8Array;
|
|
3
|
+
export declare function decodeVarint(bytes: Uint8Array, offset?: number): [number, number];
|
|
3
4
|
export declare function encodeVarint(value: number): Uint8Array;
|
|
4
|
-
export declare function toSHA256Digest(bytes: ByteView<unknown>): SHA256Digest;
|
|
5
|
-
export declare function toDigestBytes<T = unknown>(value: T): Uint8Array;
|
|
6
|
-
export declare function decodeUTF8(bytes: Uint8Array): string;
|
|
7
|
-
export declare function encodeUTF8(value: string): Uint8Array;
|
|
8
5
|
//# sourceMappingURL=utils.d.ts.map
|
package/lib/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAIA,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAEzD;AAED,wBAAgB,UAAU,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,CAAC,GAAG,UAAU,CAE5D;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAGjF;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAEtD"}
|
package/lib/utils.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return sha256.digest(bytes);
|
|
1
|
+
import { canonicalStringify, fromUTF } from '@enkaku/codec';
|
|
2
|
+
import { blake3 } from '@noble/hashes/blake3';
|
|
3
|
+
import varint from 'varint';
|
|
4
|
+
export function digestBytes(bytes) {
|
|
5
|
+
return blake3(bytes);
|
|
7
6
|
}
|
|
8
|
-
export function
|
|
9
|
-
|
|
10
|
-
return toSHA256Digest(bytes).bytes;
|
|
7
|
+
export function digestJSON(value) {
|
|
8
|
+
return digestBytes(fromUTF(canonicalStringify(value)));
|
|
11
9
|
}
|
|
12
|
-
export function
|
|
13
|
-
|
|
10
|
+
export function decodeVarint(bytes, offset) {
|
|
11
|
+
const value = varint.decode(bytes, offset);
|
|
12
|
+
return [
|
|
13
|
+
value,
|
|
14
|
+
varint.decode.bytes ?? 0
|
|
15
|
+
];
|
|
14
16
|
}
|
|
15
|
-
export function
|
|
16
|
-
return new
|
|
17
|
+
export function encodeVarint(value) {
|
|
18
|
+
return varint.encode(value, new Uint8Array(varint.encodingLength(value)));
|
|
17
19
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/id",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -15,9 +15,13 @@
|
|
|
15
15
|
],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@
|
|
19
|
-
"
|
|
20
|
-
"
|
|
18
|
+
"@enkaku/codec": "^0.12.0",
|
|
19
|
+
"@noble/hashes": "^1.6.1",
|
|
20
|
+
"multiformats": "^13.3.2",
|
|
21
|
+
"varint": "^6.0.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/varint": "^6.0.3"
|
|
21
25
|
},
|
|
22
26
|
"scripts": {
|
|
23
27
|
"build:clean": "del lib",
|
package/lib/common.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { encode as encodeCBOR } from '@ipld/dag-cbor';
|
|
2
|
-
export { type ByteView, varint } from 'multiformats';
|
|
3
|
-
export { base36 } from 'multiformats/bases/base36';
|
|
4
|
-
export { CID } from 'multiformats/cid';
|
|
5
|
-
export { encode as encodeRaw } from 'multiformats/codecs/raw';
|
|
6
|
-
export { type Digest, decode as decodeDigest } from 'multiformats/hashes/digest';
|
|
7
|
-
export { identity } from 'multiformats/hashes/identity';
|
|
8
|
-
export { sha256 } from 'multiformats/hashes/sha2';
|
|
9
|
-
export { concat, equals } from 'uint8arrays';
|
|
10
|
-
//# sourceMappingURL=common.d.ts.map
|
package/lib/common.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,gBAAgB,CAAA;AACrD,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAClD,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAC7D,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,4BAA4B,CAAA;AAChF,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
|
package/lib/common.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export { encode as encodeCBOR } from '@ipld/dag-cbor';
|
|
2
|
-
export { varint } from 'multiformats';
|
|
3
|
-
export { base36 } from 'multiformats/bases/base36';
|
|
4
|
-
export { CID } from 'multiformats/cid';
|
|
5
|
-
export { encode as encodeRaw } from 'multiformats/codecs/raw';
|
|
6
|
-
export { decode as decodeDigest } from 'multiformats/hashes/digest';
|
|
7
|
-
export { identity } from 'multiformats/hashes/identity';
|
|
8
|
-
export { sha256 } from 'multiformats/hashes/sha2';
|
|
9
|
-
export { concat, equals } from 'uint8arrays';
|
package/lib/types.d.ts
DELETED
package/lib/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAA"}
|
package/lib/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|