@kubun/id 0.1.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/LICENSE.md ADDED
@@ -0,0 +1,57 @@
1
+ # The Prosperity Public License 3.0.0
2
+
3
+ Contributor: Paul Le Cam
4
+
5
+ Source Code: https://github.com/PaulLeCam/kubun
6
+
7
+ ## Purpose
8
+
9
+ This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.
10
+
11
+ ## Agreement
12
+
13
+ In order to receive this license, you have to agree to its rules. Those rules are both obligations under that agreement and conditions to your license. Don't do anything with this software that triggers a rule you can't or won't follow.
14
+
15
+ ## Notices
16
+
17
+ Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above.
18
+
19
+ ## Commercial Trial
20
+
21
+ Limit your use of this software for commercial purposes to a thirty-day trial period. If you use this software for work, your company gets one trial period for all personnel, not one trial per person.
22
+
23
+ ## Contributions Back
24
+
25
+ Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn't count as use for a commercial purpose.
26
+
27
+ ## Personal Uses
28
+
29
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn't count as use for a commercial purpose.
30
+
31
+ ## Noncommercial Organizations
32
+
33
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn't count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding.
34
+
35
+ ## Defense
36
+
37
+ Don't make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent.
38
+
39
+ ## Copyright
40
+
41
+ The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it.
42
+
43
+ ## Patent
44
+
45
+ The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license.
46
+
47
+ ## Reliability
48
+
49
+ The contributor can't revoke this license.
50
+
51
+ ## Excuse
52
+
53
+ You're excused for unknowingly breaking [Notices](#notices) if you take all practical steps to comply within thirty days of learning you broke the rule.
54
+
55
+ ## No Liability
56
+
57
+ ***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won't be liable to anyone for any damages related to this software or this license, under any kind of legal claim.***
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Kubun identifiers
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ npm install @kubun/id
7
+ ```
8
+
9
+ ## License
10
+
11
+ [Prosperity Public License 3.0.0](LICENSE.md)
@@ -0,0 +1,19 @@
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
+ export declare class AttachmentID extends KubunID<AttachmentType.BINARY> {
8
+ #private;
9
+ static create(value: Uint8Array, mimeType?: string): AttachmentID;
10
+ static fromBytes(input: Uint8Array): AttachmentID;
11
+ static fromDigest(digest: SHA256Digest, contentLength: number, mimeType?: string): AttachmentID;
12
+ static fromString(input: string): AttachmentID;
13
+ static from(input: AttachmentID | Uint8Array | string): AttachmentID;
14
+ constructor(cid: CID, contentLength: number, mimeType?: string);
15
+ get cid(): CID;
16
+ get contentLength(): number;
17
+ get mimeType(): string;
18
+ }
19
+ //# sourceMappingURL=attachment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment.d.ts","sourceRoot":"","sources":["../src/attachment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAkB,MAAM,WAAW,CAAA;AACnE,OAAO,EAAE,GAAG,EAAmD,MAAM,aAAa,CAAA;AAClF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAK9C,MAAM,MAAM,aAAa,GAAG,GAAG,CAAC,UAAU,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,CAAA;AAEtD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY,CAGhE;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,YAAY,GAAG,aAAa,CAEvE;AAED,qBAAa,YAAa,SAAQ,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC;;IAC9D,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,YAAY;IAKjE,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY;IA0CjD,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,YAAY;IAK/F,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;IAI9C,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,GAAG,UAAU,GAAG,MAAM,GAAG,YAAY;gBAiBxD,GAAG,EAAE,GAAG,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,SAAoB;IAoBzE,IAAI,GAAG,IAAI,GAAG,CAEb;IAED,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED,IAAI,QAAQ,IAAI,MAAM,CAErB;CACF"}
@@ -0,0 +1,109 @@
1
+ import { AttachmentType, KubunID, NAMESPACE_CODE } from './base.js';
2
+ import { CID, base36, concat, decodeDigest, encodeRaw, varint } from './common.js';
3
+ import { decodeUTF8, encodeUTF8, encodeVarint, toSHA256Digest } from './utils.js';
4
+ 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
+ export class AttachmentID extends KubunID {
13
+ static create(value, mimeType) {
14
+ const digest = digestAttachment(value);
15
+ return AttachmentID.fromDigest(digest, value.byteLength, mimeType);
16
+ }
17
+ static fromBytes(input) {
18
+ let offset = 0;
19
+ // Check namespace = is it a KubunID?
20
+ const [namespace, namespaceLength] = varint.decode(input);
21
+ if (namespace !== NAMESPACE_CODE) {
22
+ throw new Error('Invalid KubunID: namespace mismatch');
23
+ }
24
+ offset += namespaceLength;
25
+ // Check type matches
26
+ const [type, typeLength] = varint.decode(input, offset);
27
+ if (type !== AttachmentType.BINARY) {
28
+ throw new Error('Invalid KubunID: type mismatch');
29
+ }
30
+ offset += typeLength;
31
+ // Decode digest
32
+ const [digestByteLength, digestVarintLength] = varint.decode(input, offset);
33
+ offset += digestVarintLength;
34
+ const digestBytes = input.slice(offset, offset + digestByteLength);
35
+ offset += digestByteLength;
36
+ const digest = decodeDigest(digestBytes);
37
+ // Extract content length
38
+ const [contentLength, contentVarintLength] = varint.decode(input, offset);
39
+ offset += contentVarintLength;
40
+ // Check if MIME type is provided
41
+ const [mimeTypeBytesLength, mimeTypeVarintLength] = varint.decode(input, offset);
42
+ if (mimeTypeBytesLength === 0) {
43
+ // No MIME type encoded, use default
44
+ return AttachmentID.fromDigest(digest, contentLength);
45
+ }
46
+ // Decode MIME type value
47
+ offset += mimeTypeVarintLength;
48
+ const mimeTypeBytes = input.slice(offset, offset + mimeTypeBytesLength);
49
+ const mimeType = decodeUTF8(mimeTypeBytes);
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);
55
+ }
56
+ static fromString(input) {
57
+ return AttachmentID.fromBytes(base36.decode(input));
58
+ }
59
+ static from(input) {
60
+ if (input instanceof AttachmentID) {
61
+ return input;
62
+ }
63
+ if (input instanceof Uint8Array) {
64
+ return AttachmentID.fromBytes(input);
65
+ }
66
+ if (typeof input === 'string') {
67
+ return AttachmentID.fromString(input);
68
+ }
69
+ throw new Error('Unsupported input type');
70
+ }
71
+ #cid;
72
+ #contentLength;
73
+ #mimeType;
74
+ constructor(cid, contentLength, mimeType = DEFAULT_MIME_TYPE){
75
+ let data;
76
+ const digestBytes = cid.multihash.bytes;
77
+ const baseData = concat([
78
+ encodeVarint(digestBytes.byteLength),
79
+ digestBytes,
80
+ encodeVarint(contentLength)
81
+ ]);
82
+ if (mimeType === DEFAULT_MIME_TYPE) {
83
+ data = concat([
84
+ baseData,
85
+ encodeVarint(0)
86
+ ]);
87
+ } else {
88
+ const typeBytes = encodeUTF8(mimeType);
89
+ data = concat([
90
+ baseData,
91
+ encodeVarint(typeBytes.byteLength),
92
+ typeBytes
93
+ ]);
94
+ }
95
+ super(AttachmentType.BINARY, data);
96
+ this.#cid = cid;
97
+ this.#contentLength = contentLength;
98
+ this.#mimeType = mimeType;
99
+ }
100
+ get cid() {
101
+ return this.#cid;
102
+ }
103
+ get contentLength() {
104
+ return this.#contentLength;
105
+ }
106
+ get mimeType() {
107
+ return this.#mimeType;
108
+ }
109
+ }
package/lib/base.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ export declare function encodeVarint(value: number): Uint8Array;
2
+ export declare const NAMESPACE_CODE = 2794;
3
+ export declare enum AttachmentType {
4
+ BINARY = 1
5
+ }
6
+ export declare enum ModelType {
7
+ SCALAR = 10,
8
+ SHAPE = 11,
9
+ GRAPH = 20
10
+ }
11
+ export declare enum DocumentType {
12
+ MERGE_DOCUMENT = 30
13
+ }
14
+ export type KubunType = AttachmentType | ModelType | DocumentType;
15
+ export declare class KubunID<T extends KubunType = KubunType> {
16
+ #private;
17
+ constructor(type: T, dataBytes: Uint8Array);
18
+ get type(): T;
19
+ get data(): Uint8Array;
20
+ get bytes(): Uint8Array;
21
+ equals(other: KubunID): boolean;
22
+ toBytes(): Uint8Array;
23
+ toString(): string;
24
+ }
25
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAEtD;AAED,eAAO,MAAM,cAAc,OAAQ,CAAA;AAInC,oBAAY,cAAc;IACxB,MAAM,IAAI;CACX;AACD,oBAAY,SAAS;IAEnB,MAAM,KAAK;IACX,KAAK,KAAK;IAEV,KAAK,KAAK;CACX;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,CAOtB;IAED,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO;IAI/B,OAAO,IAAI,UAAU;IAIrB,QAAQ;CAUT"}
package/lib/base.js ADDED
@@ -0,0 +1,65 @@
1
+ import { base36, concat, equals, varint } from './common.js';
2
+ export function encodeVarint(value) {
3
+ return varint.encodeTo(value, new Uint8Array(varint.encodingLength(value)));
4
+ }
5
+ export const NAMESPACE_CODE = 0xaea;
6
+ const NAMESPACE_BYTES = encodeVarint(NAMESPACE_CODE);
7
+ export var AttachmentType;
8
+ (function(AttachmentType) {
9
+ AttachmentType[AttachmentType["BINARY"] = 1] = "BINARY";
10
+ })(AttachmentType || (AttachmentType = {}));
11
+ export var ModelType;
12
+ (function(ModelType) {
13
+ // DAG-only
14
+ ModelType[ModelType["SCALAR"] = 10] = "SCALAR";
15
+ ModelType[ModelType["SHAPE"] = 11] = "SHAPE";
16
+ // Circular
17
+ ModelType[ModelType["GRAPH"] = 20] = "GRAPH";
18
+ })(ModelType || (ModelType = {}));
19
+ export var DocumentType;
20
+ (function(DocumentType) {
21
+ DocumentType[DocumentType["MERGE_DOCUMENT"] = 30] = "MERGE_DOCUMENT";
22
+ })(DocumentType || (DocumentType = {}));
23
+ export class KubunID {
24
+ #type;
25
+ #dataBytes;
26
+ #idBytes;
27
+ #string;
28
+ constructor(type, dataBytes){
29
+ this.#type = type;
30
+ this.#dataBytes = dataBytes;
31
+ }
32
+ get type() {
33
+ return this.#type;
34
+ }
35
+ get data() {
36
+ return this.#dataBytes;
37
+ }
38
+ get bytes() {
39
+ if (this.#idBytes == null) {
40
+ const bytes = this.toBytes();
41
+ this.#idBytes = bytes;
42
+ return bytes;
43
+ }
44
+ return this.#idBytes;
45
+ }
46
+ equals(other) {
47
+ return other instanceof KubunID && other.type === this.type && equals(other.data, this.data);
48
+ }
49
+ toBytes() {
50
+ return concat([
51
+ NAMESPACE_BYTES,
52
+ encodeVarint(this.#type),
53
+ this.#dataBytes
54
+ ]);
55
+ }
56
+ toString() {
57
+ if (this.#string == null) {
58
+ this.#string = base36.encode(this.bytes);
59
+ }
60
+ return this.#string;
61
+ }
62
+ [Symbol.for('nodejs.util.inspect.custom')]() {
63
+ return `KubunID(${this.toString()})`;
64
+ }
65
+ }
@@ -0,0 +1,10 @@
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
@@ -0,0 +1 @@
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 ADDED
@@ -0,0 +1,9 @@
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';
@@ -0,0 +1,3 @@
1
+ import { type KubunID } from './base.js';
2
+ export declare function decodeID(input: string | Uint8Array): KubunID;
3
+ //# sourceMappingURL=decode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../src/decode.ts"],"names":[],"mappings":"AACA,OAAO,EAAgC,KAAK,OAAO,EAA6B,MAAM,WAAW,CAAA;AAKjG,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAuB5D"}
package/lib/decode.js ADDED
@@ -0,0 +1,27 @@
1
+ import { AttachmentID } from './attachment.js';
2
+ import { AttachmentType, DocumentType, ModelType, NAMESPACE_CODE } from './base.js';
3
+ import { base36, varint } from './common.js';
4
+ import { DocumentID } from './document.js';
5
+ import { GraphModelID, ScalarModelID, ShapeModelID } from './model.js';
6
+ export function decodeID(input) {
7
+ const bytes = typeof input === 'string' ? base36.decode(input) : input;
8
+ const [namespace, namespaceLength] = varint.decode(bytes);
9
+ if (namespace !== NAMESPACE_CODE) {
10
+ throw new Error('Invalid KubunID: namespace mismatch');
11
+ }
12
+ const [type] = varint.decode(bytes, namespaceLength);
13
+ switch(type){
14
+ case AttachmentType.BINARY:
15
+ return AttachmentID.fromBytes(bytes);
16
+ case ModelType.SCALAR:
17
+ return ScalarModelID.fromBytes(bytes);
18
+ case ModelType.SHAPE:
19
+ return ShapeModelID.fromBytes(bytes);
20
+ case ModelType.GRAPH:
21
+ return GraphModelID.fromBytes(bytes);
22
+ case DocumentType.MERGE_DOCUMENT:
23
+ return DocumentID.fromBytes(bytes);
24
+ default:
25
+ throw new Error(`Invalid KubunID: unknown type ${type}`);
26
+ }
27
+ }
@@ -0,0 +1,13 @@
1
+ import { DocumentType, KubunID } from './base.js';
2
+ import { GraphModelID } from './model.js';
3
+ export declare class DocumentID extends KubunID<DocumentType.MERGE_DOCUMENT> {
4
+ #private;
5
+ static create(model: GraphModelID | Uint8Array | string, owner: string | Uint8Array, unique: Uint8Array): DocumentID;
6
+ static fromBytes(input: Uint8Array): DocumentID;
7
+ static fromString(input: string): DocumentID;
8
+ static from(input: DocumentID | Uint8Array | string): DocumentID;
9
+ constructor(model: GraphModelID, contentDigestBytes: Uint8Array);
10
+ get model(): GraphModelID;
11
+ get content(): Uint8Array;
12
+ }
13
+ //# sourceMappingURL=document.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAkB,MAAM,WAAW,CAAA;AAEjE,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAGzC,qBAAa,UAAW,SAAQ,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC;;IAClE,MAAM,CAAC,MAAM,CACX,KAAK,EAAE,YAAY,GAAG,UAAU,GAAG,MAAM,EACzC,KAAK,EAAE,MAAM,GAAG,UAAU,EAC1B,MAAM,EAAE,UAAU,GACjB,UAAU;IAQb,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,YAAY,EAAE,kBAAkB,EAAE,UAAU;IAkB/D,IAAI,KAAK,IAAI,YAAY,CAExB;IAED,IAAI,OAAO,IAAI,UAAU,CAExB;CACF"}
@@ -0,0 +1,79 @@
1
+ import { DocumentType, KubunID, NAMESPACE_CODE } from './base.js';
2
+ import { base36, concat, varint } from './common.js';
3
+ import { GraphModelID } from './model.js';
4
+ import { encodeVarint, toSHA256Digest } from './utils.js';
5
+ export class DocumentID extends KubunID {
6
+ static create(model, owner, unique) {
7
+ const contentBytes = concat([
8
+ typeof owner === 'string' ? new TextEncoder().encode(owner) : owner,
9
+ unique
10
+ ]);
11
+ return new DocumentID(GraphModelID.from(model), toSHA256Digest(contentBytes).bytes);
12
+ }
13
+ static fromBytes(input) {
14
+ let offset = 0;
15
+ // Check namespace = is it a KubunID?
16
+ const [namespace, namespaceLength] = varint.decode(input);
17
+ if (namespace !== NAMESPACE_CODE) {
18
+ throw new Error('Invalid KubunID: namespace mismatch');
19
+ }
20
+ offset += namespaceLength;
21
+ // Check type matches
22
+ const [type, typeLength] = varint.decode(input, offset);
23
+ if (type !== DocumentType.MERGE_DOCUMENT) {
24
+ throw new Error('Invalid KubunID: type mismatch');
25
+ }
26
+ offset += typeLength;
27
+ // Decode model ID: digest bytes followed by index
28
+ const [modelDataByteLength, modelDataVarintLength] = varint.decode(input, offset);
29
+ offset += modelDataVarintLength;
30
+ const modelDigest = input.slice(offset, offset + modelDataByteLength);
31
+ offset += modelDataByteLength;
32
+ const [modelIndex, modelIndexVarintLength] = varint.decode(input, offset);
33
+ offset += modelIndexVarintLength;
34
+ const modelID = new GraphModelID(modelDigest, modelIndex);
35
+ // Extract content digest bytes
36
+ const [contentByteLength, contentVarintLength] = varint.decode(input, offset);
37
+ offset += contentVarintLength;
38
+ const contentBytes = input.slice(offset, offset + contentByteLength);
39
+ return new DocumentID(modelID, contentBytes);
40
+ }
41
+ static fromString(input) {
42
+ return DocumentID.fromBytes(base36.decode(input));
43
+ }
44
+ static from(input) {
45
+ if (input instanceof DocumentID) {
46
+ return input;
47
+ }
48
+ if (input instanceof Uint8Array) {
49
+ return DocumentID.fromBytes(input);
50
+ }
51
+ if (typeof input === 'string') {
52
+ return DocumentID.fromString(input);
53
+ }
54
+ throw new Error('Unsupported input type');
55
+ }
56
+ #model;
57
+ #content;
58
+ constructor(model, contentDigestBytes){
59
+ if (model.isLocal) {
60
+ throw new Error('Invalid DocumentID: model cannot be local');
61
+ }
62
+ const data = concat([
63
+ encodeVarint(model.data.byteLength),
64
+ model.data,
65
+ encodeVarint(model.index),
66
+ encodeVarint(contentDigestBytes.byteLength),
67
+ contentDigestBytes
68
+ ]);
69
+ super(DocumentType.MERGE_DOCUMENT, data);
70
+ this.#model = model;
71
+ this.#content = contentDigestBytes;
72
+ }
73
+ get model() {
74
+ return this.#model;
75
+ }
76
+ get content() {
77
+ return this.#content;
78
+ }
79
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export { type AttachmentCID, AttachmentID, createAttachmentCID, digestAttachment, } from './attachment.js';
2
+ export { KubunID } from './base.js';
3
+ export { decodeID } from './decode.js';
4
+ export { DocumentID } from './document.js';
5
+ export { GraphModelID, ScalarModelID, ShapeModelID } from './model.js';
6
+ export type { SHA256Digest } from './types.js';
7
+ export { toDigestBytes, toSHA256Digest } from './utils.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,YAAY,EACZ,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,iBAAiB,CAAA;AACxB,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,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AACtE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA"}
package/lib/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export { AttachmentID, createAttachmentCID, digestAttachment } from './attachment.js';
2
+ export { KubunID } from './base.js';
3
+ export { decodeID } from './decode.js';
4
+ export { DocumentID } from './document.js';
5
+ export { GraphModelID, ScalarModelID, ShapeModelID } from './model.js';
6
+ export { toDigestBytes, toSHA256Digest } from './utils.js';
package/lib/model.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { KubunID, ModelType } from './base.js';
2
+ export declare class ScalarModelID extends KubunID<ModelType.SCALAR> {
3
+ static create(value: unknown): ScalarModelID;
4
+ static fromBytes(input: Uint8Array): ScalarModelID;
5
+ static fromString(input: string): ScalarModelID;
6
+ constructor(digestBytes: Uint8Array);
7
+ }
8
+ export declare class ShapeModelID extends KubunID<ModelType.SHAPE> {
9
+ static create(value: unknown): ShapeModelID;
10
+ static fromBytes(input: Uint8Array): ShapeModelID;
11
+ static fromString(input: string): ShapeModelID;
12
+ constructor(digestBytes: Uint8Array);
13
+ }
14
+ export declare class GraphModelID extends KubunID<ModelType.GRAPH> {
15
+ #private;
16
+ static fromBytes(input: Uint8Array): GraphModelID;
17
+ static fromString(input: string): GraphModelID;
18
+ static from(input: GraphModelID | Uint8Array | string): GraphModelID;
19
+ static local(index: number): GraphModelID;
20
+ constructor(digestBytes: Uint8Array, index: number);
21
+ get index(): number;
22
+ get isLocal(): boolean;
23
+ toBytes(): Uint8Array;
24
+ toGlobal(digestBytesOrModelID: Uint8Array | GraphModelID): GraphModelID;
25
+ }
26
+ //# sourceMappingURL=model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA,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;AAKD,qBAAa,YAAa,SAAQ,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC;;IACxD,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY;IAejD,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;IAI9C,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,GAAG,UAAU,GAAG,MAAM,GAAG,YAAY;IAapE,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY;gBAO7B,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,YAAY,GAAG,YAAY;CAOxE"}
package/lib/model.js ADDED
@@ -0,0 +1,111 @@
1
+ import { KubunID, ModelType, NAMESPACE_CODE } from './base.js';
2
+ import { base36, concat, encodeCBOR, equals, identity, varint } from './common.js';
3
+ import { encodeVarint, toDigestBytes } from './utils.js';
4
+ export class ScalarModelID extends KubunID {
5
+ static create(value) {
6
+ return new ScalarModelID(toDigestBytes(value));
7
+ }
8
+ static fromBytes(input) {
9
+ const [namespace, namespaceLength] = varint.decode(input);
10
+ if (namespace !== NAMESPACE_CODE) {
11
+ throw new Error('Invalid KubunID: namespace mismatch');
12
+ }
13
+ const [type, typeLength] = varint.decode(input, namespaceLength);
14
+ if (type !== ModelType.SCALAR) {
15
+ throw new Error('Invalid KubunID: type mismatch');
16
+ }
17
+ const digestBytes = input.slice(namespaceLength + typeLength);
18
+ return new ScalarModelID(digestBytes);
19
+ }
20
+ static fromString(input) {
21
+ return ScalarModelID.fromBytes(base36.decode(input));
22
+ }
23
+ constructor(digestBytes){
24
+ super(ModelType.SCALAR, digestBytes);
25
+ }
26
+ }
27
+ export class ShapeModelID extends KubunID {
28
+ static create(value) {
29
+ return new ShapeModelID(toDigestBytes(value));
30
+ }
31
+ static fromBytes(input) {
32
+ const [namespace, namespaceLength] = varint.decode(input);
33
+ if (namespace !== NAMESPACE_CODE) {
34
+ throw new Error('Invalid KubunID: namespace mismatch');
35
+ }
36
+ const [type, typeLength] = varint.decode(input, namespaceLength);
37
+ if (type !== ModelType.SHAPE) {
38
+ throw new Error('Invalid KubunID: type mismatch');
39
+ }
40
+ const digestBytes = input.slice(namespaceLength + typeLength);
41
+ return new ShapeModelID(digestBytes);
42
+ }
43
+ static fromString(input) {
44
+ return ShapeModelID.fromBytes(base36.decode(input));
45
+ }
46
+ constructor(digestBytes){
47
+ super(ModelType.SHAPE, digestBytes);
48
+ }
49
+ }
50
+ const LOCAL_DIGEST_BYTES = identity.digest(encodeCBOR('local')).bytes;
51
+ const MAX_INDEX = 127;
52
+ export class GraphModelID extends KubunID {
53
+ static fromBytes(input) {
54
+ const [namespace, namespaceLength] = varint.decode(input);
55
+ if (namespace !== NAMESPACE_CODE) {
56
+ throw new Error('Invalid KubunID: namespace mismatch');
57
+ }
58
+ const [type, typeLength] = varint.decode(input, namespaceLength);
59
+ if (type !== ModelType.GRAPH) {
60
+ throw new Error('Invalid KubunID: type mismatch');
61
+ }
62
+ const digestBytes = input.slice(namespaceLength + typeLength, input.byteLength - 1);
63
+ const [index] = varint.decode(input, input.byteLength - 1);
64
+ return new GraphModelID(digestBytes, index);
65
+ }
66
+ static fromString(input) {
67
+ return GraphModelID.fromBytes(base36.decode(input));
68
+ }
69
+ static from(input) {
70
+ if (input instanceof GraphModelID) {
71
+ return input;
72
+ }
73
+ if (input instanceof Uint8Array) {
74
+ return GraphModelID.fromBytes(input);
75
+ }
76
+ if (typeof input === 'string') {
77
+ return GraphModelID.fromString(input);
78
+ }
79
+ throw new Error('Unsupported input type');
80
+ }
81
+ static local(index) {
82
+ return new GraphModelID(LOCAL_DIGEST_BYTES, 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.GRAPH, digestBytes);
92
+ this.#index = index;
93
+ this.#isLocal = equals(digestBytes, LOCAL_DIGEST_BYTES);
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 GraphModelID ? digestBytesOrModelID.data : digestBytesOrModelID;
109
+ return new GraphModelID(digestBytes, this.#index);
110
+ }
111
+ }
package/lib/types.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { Digest } from './common.js';
2
+ export type SHA256Digest = Digest<18, number>;
3
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
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 ADDED
@@ -0,0 +1 @@
1
+ export { };
package/lib/utils.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { type ByteView } from './common.js';
2
+ import type { SHA256Digest } from './types.js';
3
+ 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
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAA8B,MAAM,aAAa,CAAA;AACvE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAEtD;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,YAAY,CAErE;AAED,wBAAgB,aAAa,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,CAAC,GAAG,UAAU,CAG/D;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAEpD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAEpD"}
package/lib/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ import { encodeCBOR, sha256, varint } from './common.js';
2
+ export function encodeVarint(value) {
3
+ return varint.encodeTo(value, new Uint8Array(varint.encodingLength(value)));
4
+ }
5
+ export function toSHA256Digest(bytes) {
6
+ return sha256.digest(bytes);
7
+ }
8
+ export function toDigestBytes(value) {
9
+ const bytes = encodeCBOR(value);
10
+ return toSHA256Digest(bytes).bytes;
11
+ }
12
+ export function decodeUTF8(bytes) {
13
+ return new TextDecoder().decode(bytes);
14
+ }
15
+ export function encodeUTF8(value) {
16
+ return new TextEncoder().encode(value);
17
+ }
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@kubun/id",
3
+ "version": "0.1.0",
4
+ "license": "see LICENSE.md",
5
+ "keywords": [],
6
+ "type": "module",
7
+ "main": "lib/index.js",
8
+ "types": "lib/index.d.ts",
9
+ "exports": {
10
+ ".": "./lib/index.js"
11
+ },
12
+ "files": [
13
+ "lib/*",
14
+ "LICENSE.md"
15
+ ],
16
+ "sideEffects": false,
17
+ "dependencies": {
18
+ "@ipld/dag-cbor": "^9.2.1",
19
+ "multiformats": "^13.3.0",
20
+ "uint8arrays": "^5.1.0"
21
+ },
22
+ "scripts": {
23
+ "build:clean": "del lib",
24
+ "build:js": "swc src -d ./lib --config-file ../../swc.json --strip-leading-paths",
25
+ "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
26
+ "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
27
+ "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
28
+ "test:types": "tsc --noEmit",
29
+ "test:unit": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js",
30
+ "test": "pnpm run test:types && pnpm run test:unit"
31
+ }
32
+ }