@kubun/id 0.3.1 → 0.5.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.
@@ -10,4 +10,3 @@ export declare class AttachmentID extends KubunID<AttachmentType.BINARY> {
10
10
  get digest(): Uint8Array;
11
11
  get mimeType(): string;
12
12
  }
13
- //# sourceMappingURL=attachment.d.ts.map
package/lib/attachment.js CHANGED
@@ -1,96 +1 @@
1
- import { fromUTF, toUTF } from '@enkaku/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
- }
1
+ import{fromUTF as t,toUTF as e}from"@enkaku/codec";import{base36 as r}from"multiformats/bases/base36";import{AttachmentType as n,KubunID as i,NAMESPACE_CODE as s}from"./base.js";import{concat as m}from"./bytes.js";import{decodeVarint as o,digestBytes as c,encodeVarint as a}from"./utils.js";let h="application/octet-stream";export class AttachmentID extends i{static create(t,e){return new AttachmentID(c(t),t.byteLength,e)}static fromBytes(t){let r=0,[i,m]=o(t);if(i!==s)throw Error("Invalid KubunID: namespace mismatch");let[c,a]=o(t,r+=m);if(c!==n.BINARY)throw Error("Invalid KubunID: type mismatch");let[h,f]=o(t,r+=a);r+=f;let p=t.slice(r,r+h),[u,g]=o(t,r+=h),[l,y]=o(t,r+=g);return 0===l?new AttachmentID(p,u):(r+=y,new AttachmentID(p,u,e(t.slice(r,r+l))))}static fromString(t){return AttachmentID.fromBytes(r.decode(t))}static from(t){if(t instanceof AttachmentID)return t;if(t instanceof Uint8Array)return AttachmentID.fromBytes(t);if("string"==typeof t)return AttachmentID.fromString(t);throw Error("Unsupported input type")}#t;#e;#r;constructor(e,r,i=h){let s,o=m([a(e.byteLength),e,a(r)]);if(i===h)s=m([o,a(0)]);else{let e=t(i);s=m([o,a(e.byteLength),e])}super(n.BINARY,s),this.#t=r,this.#e=e,this.#r=i}get contentLength(){return this.#t}get digest(){return this.#e}get mimeType(){return this.#r}}
package/lib/base.d.ts CHANGED
@@ -21,4 +21,3 @@ export declare class KubunID<T extends KubunType = KubunType> {
21
21
  toBytes(): Uint8Array;
22
22
  toString(): string;
23
23
  }
24
- //# sourceMappingURL=base.d.ts.map
package/lib/base.js CHANGED
@@ -1,64 +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
- return ModelType;
18
- }({});
19
- // Mutable
20
- export var DocumentType = /*#__PURE__*/ function(DocumentType) {
21
- DocumentType[DocumentType["MERGE_DOCUMENT"] = 30] = "MERGE_DOCUMENT";
22
- return DocumentType;
23
- }({});
24
- export class KubunID {
25
- #type;
26
- #dataBytes;
27
- #idBytes;
28
- #string;
29
- constructor(type, dataBytes){
30
- this.#type = type;
31
- this.#dataBytes = dataBytes;
32
- }
33
- get type() {
34
- return this.#type;
35
- }
36
- get data() {
37
- return this.#dataBytes;
38
- }
39
- get bytes() {
40
- if (this.#idBytes == null) {
41
- this.#idBytes = this.toBytes();
42
- }
43
- return this.#idBytes;
44
- }
45
- equals(other) {
46
- return other instanceof KubunID && other.type === this.type && equals(other.data, this.data);
47
- }
48
- toBytes() {
49
- return concat([
50
- NAMESPACE_BYTES,
51
- encodeVarint(this.#type),
52
- this.#dataBytes
53
- ]);
54
- }
55
- toString() {
56
- if (this.#string == null) {
57
- this.#string = base36.encode(this.bytes);
58
- }
59
- return this.#string;
60
- }
61
- [Symbol.for('nodejs.util.inspect.custom')]() {
62
- return `KubunID(${this.toString()})`;
63
- }
64
- }
1
+ var t,e,s;import{base36 as r}from"multiformats/bases/base36";import{concat as i,equals as n}from"./bytes.js";import{encodeVarint as o}from"./utils.js";export const NAMESPACE_CODE=181;let a=o(181);export var AttachmentType=((t={})[t.BINARY=1]="BINARY",t);export var ModelType=((e={})[e.SCALAR=10]="SCALAR",e[e.SHAPE=11]="SHAPE",e[e.DOCUMENT=20]="DOCUMENT",e);export var DocumentType=((s={})[s.MERGE_DOCUMENT=30]="MERGE_DOCUMENT",s);export class KubunID{#t;#e;#s;#r;constructor(t,e){this.#t=t,this.#e=e}get type(){return this.#t}get data(){return this.#e}get bytes(){return null==this.#s&&(this.#s=this.toBytes()),this.#s}equals(t){return t instanceof KubunID&&t.type===this.type&&n(t.data,this.data)}toBytes(){return i([a,o(this.#t),this.#e])}toString(){return null==this.#r&&(this.#r=r.encode(this.bytes)),this.#r}[Symbol.for("nodejs.util.inspect.custom")](){return`KubunID(${this.toString()})`}}
package/lib/bytes.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export declare function concat(arrays: Uint8Array[], providedLength?: number): Uint8Array;
2
2
  export declare function equals(a: Uint8Array, b: Uint8Array): boolean;
3
- //# sourceMappingURL=bytes.d.ts.map
package/lib/bytes.js CHANGED
@@ -1,24 +1 @@
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
- }
1
+ export function concat(e,t){let n=new Uint8Array(t??e.reduce((e,t)=>e+t.length,0)),r=0;for(let t of e)n.set(t,r),r+=t.length;return n}export function equals(e,t){if(e===t)return!0;if(e.byteLength!==t.byteLength)return!1;for(let n=0;n<e.byteLength;n++)if(e[n]!==t[n])return!1;return!0}
package/lib/decode.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  import { type KubunID } from './base.js';
2
2
  export declare function decodeID(input: string | Uint8Array): KubunID;
3
- //# sourceMappingURL=decode.d.ts.map
package/lib/decode.js CHANGED
@@ -1,28 +1 @@
1
- import { base36 } from 'multiformats/bases/base36';
2
- import { AttachmentID } from './attachment.js';
3
- import { AttachmentType, DocumentType, ModelType, NAMESPACE_CODE } from './base.js';
4
- import { DocumentID } from './document.js';
5
- import { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
6
- import { decodeVarint } from './utils.js';
7
- export function decodeID(input) {
8
- const bytes = typeof input === 'string' ? base36.decode(input) : input;
9
- const [namespace, namespaceLength] = decodeVarint(bytes);
10
- if (namespace !== NAMESPACE_CODE) {
11
- throw new Error('Invalid KubunID: namespace mismatch');
12
- }
13
- const [type] = decodeVarint(bytes, namespaceLength);
14
- switch(type){
15
- case AttachmentType.BINARY:
16
- return AttachmentID.fromBytes(bytes);
17
- case ModelType.SCALAR:
18
- return ScalarModelID.fromBytes(bytes);
19
- case ModelType.SHAPE:
20
- return ShapeModelID.fromBytes(bytes);
21
- case ModelType.DOCUMENT:
22
- return DocumentModelID.fromBytes(bytes);
23
- case DocumentType.MERGE_DOCUMENT:
24
- return DocumentID.fromBytes(bytes);
25
- default:
26
- throw new Error(`Invalid KubunID: unknown type ${type}`);
27
- }
28
- }
1
+ import{base36 as r}from"multiformats/bases/base36";import{AttachmentID as t}from"./attachment.js";import{AttachmentType as e,DocumentType as o,ModelType as m,NAMESPACE_CODE as s}from"./base.js";import{DocumentID as n}from"./document.js";import{DocumentModelID as a,ScalarModelID as f,ShapeModelID as i}from"./model.js";import{decodeVarint as u}from"./utils.js";export function decodeID(c){let p="string"==typeof c?r.decode(c):c,[d,l]=u(p);if(d!==s)throw Error("Invalid KubunID: namespace mismatch");let[y]=u(p,l);switch(y){case e.BINARY:return t.fromBytes(p);case m.SCALAR:return f.fromBytes(p);case m.SHAPE:return i.fromBytes(p);case m.DOCUMENT:return a.fromBytes(p);case o.MERGE_DOCUMENT:return n.fromBytes(p);default:throw Error(`Invalid KubunID: unknown type ${y}`)}}
package/lib/document.d.ts CHANGED
@@ -10,4 +10,3 @@ export declare class DocumentID extends KubunID<DocumentType.MERGE_DOCUMENT> {
10
10
  get model(): DocumentModelID;
11
11
  get content(): Uint8Array;
12
12
  }
13
- //# sourceMappingURL=document.d.ts.map
package/lib/document.js CHANGED
@@ -1,81 +1 @@
1
- import { fromUTF } from '@enkaku/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
- }
1
+ import{fromUTF as t}from"@enkaku/codec";import{base36 as e}from"multiformats/bases/base36";import{DocumentType as r,KubunID as o,NAMESPACE_CODE as n}from"./base.js";import{concat as m}from"./bytes.js";import{DocumentModelID as i}from"./model.js";import{decodeVarint as s,digestBytes as c,encodeVarint as a}from"./utils.js";export class DocumentID extends o{static create(e,r,o){let n=m(["string"==typeof r?t(r):r,o]);return new DocumentID(i.from(e),c(n))}static fromBytes(t){let e=0,[o,m]=s(t);if(o!==n)throw Error("Invalid KubunID: namespace mismatch");let[c,a]=s(t,e+=m);if(c!==r.MERGE_DOCUMENT)throw Error("Invalid KubunID: type mismatch");let[u,f]=s(t,e+=a);e+=f;let l=t.slice(e,e+u),[D,d]=s(t,e+=u);e+=d;let p=new i(l,D),[I,h]=s(t,e);return e+=h,new DocumentID(p,t.slice(e,e+I))}static fromString(t){return DocumentID.fromBytes(e.decode(t))}static from(t){if(t instanceof DocumentID)return t;if(t instanceof Uint8Array)return DocumentID.fromBytes(t);if("string"==typeof t)return DocumentID.fromString(t);throw Error("Unsupported input type")}#t;#e;constructor(t,e){if(t.isLocal)throw Error("Invalid DocumentID: model cannot be local");let o=m([a(t.data.byteLength),t.data,a(t.index),a(e.byteLength),e]);super(r.MERGE_DOCUMENT,o),this.#t=t,this.#e=e}get model(){return this.#t}get content(){return this.#e}}
package/lib/index.d.ts CHANGED
@@ -4,4 +4,3 @@ export { decodeID } from './decode.js';
4
4
  export { DocumentID } from './document.js';
5
5
  export { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
6
6
  export { digestBytes, digestJSON } from './utils.js';
7
- //# sourceMappingURL=index.d.ts.map
package/lib/index.js CHANGED
@@ -1,6 +1 @@
1
- export { AttachmentID } from './attachment.js';
2
- export { KubunID } from './base.js';
3
- export { decodeID } from './decode.js';
4
- export { DocumentID } from './document.js';
5
- export { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
6
- export { digestBytes, digestJSON } from './utils.js';
1
+ export{AttachmentID}from"./attachment.js";export{KubunID}from"./base.js";export{decodeID}from"./decode.js";export{DocumentID}from"./document.js";export{DocumentModelID,ScalarModelID,ShapeModelID}from"./model.js";export{digestBytes,digestJSON}from"./utils.js";
package/lib/model.d.ts CHANGED
@@ -23,4 +23,3 @@ export declare class DocumentModelID extends KubunID<ModelType.DOCUMENT> {
23
23
  toBytes(): Uint8Array;
24
24
  toGlobal(digestBytesOrModelID: Uint8Array | DocumentModelID): DocumentModelID;
25
25
  }
26
- //# sourceMappingURL=model.d.ts.map
package/lib/model.js CHANGED
@@ -1,111 +1 @@
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
- }
1
+ import{base36 as e}from"multiformats/bases/base36";import{KubunID as t,ModelType as r,NAMESPACE_CODE as o}from"./base.js";import{concat as n}from"./bytes.js";import{decodeVarint as s,digestJSON as i,encodeVarint as a}from"./utils.js";export class ScalarModelID extends t{static create(e){return new ScalarModelID(i(e))}static fromBytes(e){let[t,n]=s(e);if(t!==o)throw Error("Invalid KubunID: namespace mismatch");let[i,a]=s(e,n);if(i!==r.SCALAR)throw Error("Invalid KubunID: type mismatch");return new ScalarModelID(e.slice(n+a))}static fromString(t){return ScalarModelID.fromBytes(e.decode(t))}constructor(e){super(r.SCALAR,e)}}export class ShapeModelID extends t{static create(e){return new ShapeModelID(i(e))}static fromBytes(e){let[t,n]=s(e);if(t!==o)throw Error("Invalid KubunID: namespace mismatch");let[i,a]=s(e,n);if(i!==r.SHAPE)throw Error("Invalid KubunID: type mismatch");return new ShapeModelID(e.slice(n+a))}static fromString(t){return ShapeModelID.fromBytes(e.decode(t))}constructor(e){super(r.SHAPE,e)}}export class DocumentModelID extends t{static fromBytes(e){let[t,n]=s(e);if(t!==o)throw Error("Invalid KubunID: namespace mismatch");let[i,a]=s(e,n);if(i!==r.DOCUMENT)throw Error("Invalid KubunID: type mismatch");let c=e.slice(n+a,e.byteLength-1),[l]=s(e,e.byteLength-1);return new DocumentModelID(c,l)}static fromString(t){return DocumentModelID.fromBytes(e.decode(t))}static from(e){if(e instanceof DocumentModelID)return e;if(e instanceof Uint8Array)return DocumentModelID.fromBytes(e);if("string"==typeof e)return DocumentModelID.fromString(e);throw Error("Unsupported input type")}static local(e){return new DocumentModelID(new Uint8Array(0),e)}#e;#t;constructor(e,t){if(t>127)throw RangeError(`Index ${t} exceeds maximum of 127`);super(r.DOCUMENT,e),this.#e=t,this.#t=0===e.length}get index(){return this.#e}get isLocal(){return this.#t}toBytes(){return n([super.toBytes(),a(this.#e)])}toGlobal(e){let t=e instanceof DocumentModelID?e.data:e;return new DocumentModelID(t,this.#e)}}
package/lib/utils.d.ts CHANGED
@@ -2,4 +2,3 @@ export declare function digestBytes(bytes: Uint8Array): Uint8Array;
2
2
  export declare function digestJSON<T = unknown>(value: T): Uint8Array;
3
3
  export declare function decodeVarint(bytes: Uint8Array, offset?: number): [number, number];
4
4
  export declare function encodeVarint(value: number): Uint8Array;
5
- //# sourceMappingURL=utils.d.ts.map
package/lib/utils.js CHANGED
@@ -1,19 +1 @@
1
- import { canonicalStringify, fromUTF } from '@enkaku/codec';
2
- import { blake3 } from '@noble/hashes/blake3.js';
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
- }
1
+ import{canonicalStringify as e,fromUTF as t}from"@enkaku/codec";import{blake3 as n}from"@noble/hashes/blake3.js";import r from"varint";export function digestBytes(e){return n(e)}export function digestJSON(n){return digestBytes(t(e(n)))}export function decodeVarint(e,t){return[r.decode(e,t),r.decode.bytes??0]}export function encodeVarint(e){return r.encode(e,new Uint8Array(r.encodingLength(e)))}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/id",
3
- "version": "0.3.1",
3
+ "version": "0.5.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
- "@enkaku/codec": "^0.12.0",
19
- "@noble/hashes": "^2.0.0",
20
- "multiformats": "^13.4.0",
18
+ "@enkaku/codec": "^0.13.0",
19
+ "@noble/hashes": "^2.0.1",
20
+ "multiformats": "^13.4.2",
21
21
  "varint": "^6.0.0"
22
22
  },
23
23
  "devDependencies": {
@@ -30,7 +30,7 @@
30
30
  "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
31
31
  "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
32
32
  "test:types": "tsc --noEmit",
33
- "test:unit": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js",
33
+ "test:unit": "vitest run",
34
34
  "test": "pnpm run test:types && pnpm run test:unit"
35
35
  }
36
36
  }
@@ -1 +0,0 @@
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/base.d.ts.map DELETED
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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"}