@kubun/id 0.11.0 → 0.12.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.
@@ -1,5 +1,5 @@
1
- import { AttachmentType, KubunID } from './base.js';
2
- export declare class AttachmentID extends KubunID<AttachmentType.BINARY> {
1
+ import { ATTACHMENT_BINARY, KubunID } from './base.js';
2
+ export declare class AttachmentID extends KubunID<typeof ATTACHMENT_BINARY> {
3
3
  #private;
4
4
  static create(value: Uint8Array, mimeType?: string): AttachmentID;
5
5
  static fromBytes(input: Uint8Array): AttachmentID;
package/lib/attachment.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { fromUTF, toUTF } from '@sozai/codec';
2
2
  import { base36 } from 'multiformats/bases/base36';
3
- import { AttachmentType, KubunID, NAMESPACE_CODE } from './base.js';
3
+ import { ATTACHMENT_BINARY, KubunID, NAMESPACE_CODE } from './base.js';
4
4
  import { concat } from './bytes.js';
5
5
  import { decodeVarint, digestBytes, encodeVarint } from './utils.js';
6
6
  const DEFAULT_MIME_TYPE = 'application/octet-stream';
@@ -18,7 +18,7 @@ export class AttachmentID extends KubunID {
18
18
  offset += namespaceLength;
19
19
  // Check type matches
20
20
  const [type, typeLength] = decodeVarint(input, offset);
21
- if (type !== AttachmentType.BINARY) {
21
+ if (type !== ATTACHMENT_BINARY) {
22
22
  throw new Error('Invalid KubunID: type mismatch');
23
23
  }
24
24
  offset += typeLength;
@@ -79,7 +79,7 @@ export class AttachmentID extends KubunID {
79
79
  typeBytes
80
80
  ]);
81
81
  }
82
- super(AttachmentType.BINARY, data);
82
+ super(ATTACHMENT_BINARY, data);
83
83
  this.#contentLength = contentLength;
84
84
  this.#digest = digest;
85
85
  this.#mimeType = mimeType;
package/lib/base.d.ts CHANGED
@@ -1,19 +1,24 @@
1
1
  export declare const NAMESPACE_CODE = 181;
2
- export declare enum AttachmentType {
3
- BINARY = 1
4
- }
5
- export declare enum ModelType {
6
- SCALAR = 10,
7
- SHAPE = 11,
8
- DOCUMENT = 20,
9
- CLUSTER = 21
10
- }
11
- export declare enum DocumentType {
12
- MERGE_DOCUMENT = 30
13
- }
14
- export declare enum ExternalType {
15
- PLUGIN_NODE = 40
16
- }
2
+ /**
3
+ * Immutable references
4
+ */
5
+ export declare const ATTACHMENT_BINARY = 1;
6
+ export type AttachmentType = typeof ATTACHMENT_BINARY;
7
+ export declare const MODEL_SCALAR = 10;
8
+ export declare const MODEL_SHAPE = 11;
9
+ export declare const MODEL_DOCUMENT = 20;
10
+ export declare const MODEL_CLUSTER = 21;
11
+ export type ModelType = typeof MODEL_SCALAR | typeof MODEL_SHAPE | typeof MODEL_DOCUMENT | typeof MODEL_CLUSTER;
12
+ /**
13
+ * Mutable references
14
+ */
15
+ export declare const DOCUMENT_MERGE = 30;
16
+ export type DocumentType = typeof DOCUMENT_MERGE;
17
+ /**
18
+ * External references
19
+ */
20
+ export declare const EXTERNAL_PLUGIN_NODE = 40;
21
+ export type ExternalType = typeof EXTERNAL_PLUGIN_NODE;
17
22
  export type KubunType = AttachmentType | ModelType | DocumentType | ExternalType;
18
23
  export declare class KubunID<T extends KubunType = KubunType> {
19
24
  #private;
package/lib/base.js CHANGED
@@ -3,30 +3,21 @@ import { concat, equals } from './bytes.js';
3
3
  import { encodeVarint } from './utils.js';
4
4
  export const NAMESPACE_CODE = 0xb5;
5
5
  const NAMESPACE_BYTES = encodeVarint(NAMESPACE_CODE);
6
- // Immutable
7
- export var AttachmentType = /*#__PURE__*/ function(AttachmentType) {
8
- AttachmentType[AttachmentType["BINARY"] = 1] = "BINARY";
9
- return AttachmentType;
10
- }({});
11
- export var ModelType = /*#__PURE__*/ function(ModelType) {
12
- // DAG-only
13
- ModelType[ModelType["SCALAR"] = 10] = "SCALAR";
14
- ModelType[ModelType["SHAPE"] = 11] = "SHAPE";
15
- // Circular
16
- ModelType[ModelType["DOCUMENT"] = 20] = "DOCUMENT";
17
- ModelType[ModelType["CLUSTER"] = 21] = "CLUSTER";
18
- return ModelType;
19
- }({});
20
- // Mutable
21
- export var DocumentType = /*#__PURE__*/ function(DocumentType) {
22
- DocumentType[DocumentType["MERGE_DOCUMENT"] = 30] = "MERGE_DOCUMENT";
23
- return DocumentType;
24
- }({});
25
- // External
26
- export var ExternalType = /*#__PURE__*/ function(ExternalType) {
27
- ExternalType[ExternalType["PLUGIN_NODE"] = 40] = "PLUGIN_NODE";
28
- return ExternalType;
29
- }({});
6
+ /**
7
+ * Immutable references
8
+ */ export const ATTACHMENT_BINARY = 1;
9
+ // DAG-only
10
+ export const MODEL_SCALAR = 10;
11
+ export const MODEL_SHAPE = 11;
12
+ // Circular
13
+ export const MODEL_DOCUMENT = 20;
14
+ export const MODEL_CLUSTER = 21;
15
+ /**
16
+ * Mutable references
17
+ */ export const DOCUMENT_MERGE = 30;
18
+ /**
19
+ * External references
20
+ */ export const EXTERNAL_PLUGIN_NODE = 40;
30
21
  export class KubunID {
31
22
  #type;
32
23
  #dataBytes;
package/lib/bytes.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare function concat(arrays: Uint8Array[], providedLength?: number): Uint8Array;
1
+ export declare function concat(arrays: Array<Uint8Array>, providedLength?: number): Uint8Array;
2
2
  export declare function equals(a: Uint8Array, b: Uint8Array): boolean;
@@ -1,5 +1,5 @@
1
- import { KubunID, ModelType } from './base.js';
2
- export declare class ClusterModelID extends KubunID<ModelType.CLUSTER> {
1
+ import { KubunID, MODEL_CLUSTER } from './base.js';
2
+ export declare class ClusterModelID extends KubunID<typeof MODEL_CLUSTER> {
3
3
  static fromDigest(digestBytes: Uint8Array): ClusterModelID;
4
4
  static fromBytes(input: Uint8Array): ClusterModelID;
5
5
  static fromString(input: string): ClusterModelID;
@@ -1,5 +1,5 @@
1
1
  import { base36 } from 'multiformats/bases/base36';
2
- import { KubunID, ModelType, NAMESPACE_CODE } from './base.js';
2
+ import { KubunID, MODEL_CLUSTER, NAMESPACE_CODE } from './base.js';
3
3
  import { decodeVarint } from './utils.js';
4
4
  export class ClusterModelID extends KubunID {
5
5
  static fromDigest(digestBytes) {
@@ -11,7 +11,7 @@ export class ClusterModelID extends KubunID {
11
11
  throw new Error('Invalid KubunID: namespace mismatch');
12
12
  }
13
13
  const [type, typeLength] = decodeVarint(input, namespaceLength);
14
- if (type !== ModelType.CLUSTER) {
14
+ if (type !== MODEL_CLUSTER) {
15
15
  throw new Error('Invalid KubunID: type mismatch');
16
16
  }
17
17
  const digestBytes = input.slice(namespaceLength + typeLength);
@@ -21,6 +21,6 @@ export class ClusterModelID extends KubunID {
21
21
  return ClusterModelID.fromBytes(base36.decode(input));
22
22
  }
23
23
  constructor(digestBytes){
24
- super(ModelType.CLUSTER, digestBytes);
24
+ super(MODEL_CLUSTER, digestBytes);
25
25
  }
26
26
  }
package/lib/decode.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { base36 } from 'multiformats/bases/base36';
2
2
  import { AttachmentID } from './attachment.js';
3
- import { AttachmentType, DocumentType, ExternalType, ModelType, NAMESPACE_CODE } from './base.js';
3
+ import { ATTACHMENT_BINARY, DOCUMENT_MERGE, EXTERNAL_PLUGIN_NODE, MODEL_CLUSTER, MODEL_DOCUMENT, MODEL_SCALAR, MODEL_SHAPE, NAMESPACE_CODE } from './base.js';
4
4
  import { ClusterModelID } from './cluster-model.js';
5
5
  import { DocumentID } from './document.js';
6
6
  import { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
@@ -14,19 +14,19 @@ export function decodeID(input) {
14
14
  }
15
15
  const [type] = decodeVarint(bytes, namespaceLength);
16
16
  switch(type){
17
- case AttachmentType.BINARY:
17
+ case ATTACHMENT_BINARY:
18
18
  return AttachmentID.fromBytes(bytes);
19
- case ModelType.SCALAR:
19
+ case MODEL_SCALAR:
20
20
  return ScalarModelID.fromBytes(bytes);
21
- case ModelType.SHAPE:
21
+ case MODEL_SHAPE:
22
22
  return ShapeModelID.fromBytes(bytes);
23
- case ModelType.DOCUMENT:
23
+ case MODEL_DOCUMENT:
24
24
  return DocumentModelID.fromBytes(bytes);
25
- case ModelType.CLUSTER:
25
+ case MODEL_CLUSTER:
26
26
  return ClusterModelID.fromBytes(bytes);
27
- case DocumentType.MERGE_DOCUMENT:
27
+ case DOCUMENT_MERGE:
28
28
  return DocumentID.fromBytes(bytes);
29
- case ExternalType.PLUGIN_NODE:
29
+ case EXTERNAL_PLUGIN_NODE:
30
30
  return PluginNodeID.fromBytes(bytes);
31
31
  default:
32
32
  throw new Error(`Invalid KubunID: unknown type ${type}`);
package/lib/document.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { DocumentType, KubunID } from './base.js';
1
+ import { DOCUMENT_MERGE, KubunID } from './base.js';
2
2
  import { DocumentModelID } from './model.js';
3
- export declare class DocumentID extends KubunID<DocumentType.MERGE_DOCUMENT> {
3
+ export declare class DocumentID extends KubunID<typeof DOCUMENT_MERGE> {
4
4
  #private;
5
5
  static create(model: DocumentModelID | Uint8Array | string, owner: string | Uint8Array, unique: Uint8Array): DocumentID;
6
6
  static fromBytes(input: Uint8Array): DocumentID;
package/lib/document.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { fromUTF } from '@sozai/codec';
2
2
  import { base36 } from 'multiformats/bases/base36';
3
- import { DocumentType, KubunID, NAMESPACE_CODE } from './base.js';
3
+ import { DOCUMENT_MERGE, KubunID, NAMESPACE_CODE } from './base.js';
4
4
  import { concat } from './bytes.js';
5
5
  import { DocumentModelID } from './model.js';
6
6
  import { decodeVarint, digestBytes, encodeVarint } from './utils.js';
@@ -22,7 +22,7 @@ export class DocumentID extends KubunID {
22
22
  offset += namespaceLength;
23
23
  // Check type matches
24
24
  const [type, typeLength] = decodeVarint(input, offset);
25
- if (type !== DocumentType.MERGE_DOCUMENT) {
25
+ if (type !== DOCUMENT_MERGE) {
26
26
  throw new Error('Invalid KubunID: type mismatch');
27
27
  }
28
28
  offset += typeLength;
@@ -68,7 +68,7 @@ export class DocumentID extends KubunID {
68
68
  encodeVarint(contentDigestBytes.byteLength),
69
69
  contentDigestBytes
70
70
  ]);
71
- super(DocumentType.MERGE_DOCUMENT, data);
71
+ super(DOCUMENT_MERGE, data);
72
72
  this.#model = model;
73
73
  this.#content = contentDigestBytes;
74
74
  }
package/lib/model.d.ts CHANGED
@@ -1,17 +1,17 @@
1
- import { KubunID, ModelType } from './base.js';
2
- export declare class ScalarModelID extends KubunID<ModelType.SCALAR> {
1
+ import { KubunID, MODEL_DOCUMENT, MODEL_SCALAR, MODEL_SHAPE } from './base.js';
2
+ export declare class ScalarModelID extends KubunID<typeof MODEL_SCALAR> {
3
3
  static create(value: unknown): ScalarModelID;
4
4
  static fromBytes(input: Uint8Array): ScalarModelID;
5
5
  static fromString(input: string): ScalarModelID;
6
6
  constructor(digestBytes: Uint8Array);
7
7
  }
8
- export declare class ShapeModelID extends KubunID<ModelType.SHAPE> {
8
+ export declare class ShapeModelID extends KubunID<typeof MODEL_SHAPE> {
9
9
  static create(value: unknown): ShapeModelID;
10
10
  static fromBytes(input: Uint8Array): ShapeModelID;
11
11
  static fromString(input: string): ShapeModelID;
12
12
  constructor(digestBytes: Uint8Array);
13
13
  }
14
- export declare class DocumentModelID extends KubunID<ModelType.DOCUMENT> {
14
+ export declare class DocumentModelID extends KubunID<typeof MODEL_DOCUMENT> {
15
15
  #private;
16
16
  static fromBytes(input: Uint8Array): DocumentModelID;
17
17
  static fromString(input: string): DocumentModelID;
package/lib/model.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { base36 } from 'multiformats/bases/base36';
2
- import { KubunID, ModelType, NAMESPACE_CODE } from './base.js';
2
+ import { KubunID, MODEL_DOCUMENT, MODEL_SCALAR, MODEL_SHAPE, NAMESPACE_CODE } from './base.js';
3
3
  import { concat } from './bytes.js';
4
4
  import { decodeVarint, digestJSON, encodeVarint } from './utils.js';
5
5
  export class ScalarModelID extends KubunID {
@@ -12,7 +12,7 @@ export class ScalarModelID extends KubunID {
12
12
  throw new Error('Invalid KubunID: namespace mismatch');
13
13
  }
14
14
  const [type, typeLength] = decodeVarint(input, namespaceLength);
15
- if (type !== ModelType.SCALAR) {
15
+ if (type !== MODEL_SCALAR) {
16
16
  throw new Error('Invalid KubunID: type mismatch');
17
17
  }
18
18
  const digestBytes = input.slice(namespaceLength + typeLength);
@@ -22,7 +22,7 @@ export class ScalarModelID extends KubunID {
22
22
  return ScalarModelID.fromBytes(base36.decode(input));
23
23
  }
24
24
  constructor(digestBytes){
25
- super(ModelType.SCALAR, digestBytes);
25
+ super(MODEL_SCALAR, digestBytes);
26
26
  }
27
27
  }
28
28
  export class ShapeModelID extends KubunID {
@@ -35,7 +35,7 @@ export class ShapeModelID extends KubunID {
35
35
  throw new Error('Invalid KubunID: namespace mismatch');
36
36
  }
37
37
  const [type, typeLength] = decodeVarint(input, namespaceLength);
38
- if (type !== ModelType.SHAPE) {
38
+ if (type !== MODEL_SHAPE) {
39
39
  throw new Error('Invalid KubunID: type mismatch');
40
40
  }
41
41
  const digestBytes = input.slice(namespaceLength + typeLength);
@@ -45,7 +45,7 @@ export class ShapeModelID extends KubunID {
45
45
  return ShapeModelID.fromBytes(base36.decode(input));
46
46
  }
47
47
  constructor(digestBytes){
48
- super(ModelType.SHAPE, digestBytes);
48
+ super(MODEL_SHAPE, digestBytes);
49
49
  }
50
50
  }
51
51
  const MAX_INDEX = 127;
@@ -56,7 +56,7 @@ export class DocumentModelID extends KubunID {
56
56
  throw new Error('Invalid KubunID: namespace mismatch');
57
57
  }
58
58
  const [type, typeLength] = decodeVarint(input, namespaceLength);
59
- if (type !== ModelType.DOCUMENT) {
59
+ if (type !== MODEL_DOCUMENT) {
60
60
  throw new Error('Invalid KubunID: type mismatch');
61
61
  }
62
62
  const digestBytes = input.slice(namespaceLength + typeLength, input.byteLength - 1);
@@ -88,7 +88,7 @@ export class DocumentModelID 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.DOCUMENT, digestBytes);
91
+ super(MODEL_DOCUMENT, digestBytes);
92
92
  this.#index = index;
93
93
  this.#isLocal = digestBytes.length === 0;
94
94
  }
@@ -1,5 +1,5 @@
1
- import { ExternalType, KubunID } from './base.js';
2
- export declare class PluginNodeID extends KubunID<ExternalType.PLUGIN_NODE> {
1
+ import { EXTERNAL_PLUGIN_NODE, KubunID } from './base.js';
2
+ export declare class PluginNodeID extends KubunID<typeof EXTERNAL_PLUGIN_NODE> {
3
3
  #private;
4
4
  static create(plugin: string, typeName: string, localID: string): PluginNodeID;
5
5
  static fromBytes(input: Uint8Array): PluginNodeID;
@@ -1,6 +1,6 @@
1
1
  import { fromUTF, toUTF } from '@sozai/codec';
2
2
  import { base36 } from 'multiformats/bases/base36';
3
- import { ExternalType, KubunID, NAMESPACE_CODE } from './base.js';
3
+ import { EXTERNAL_PLUGIN_NODE, KubunID, NAMESPACE_CODE } from './base.js';
4
4
  import { concat } from './bytes.js';
5
5
  import { decodeVarint, encodeVarint } from './utils.js';
6
6
  export class PluginNodeID extends KubunID {
@@ -17,7 +17,7 @@ export class PluginNodeID extends KubunID {
17
17
  offset += namespaceLength;
18
18
  // Check type matches
19
19
  const [type, typeLength] = decodeVarint(input, offset);
20
- if (type !== ExternalType.PLUGIN_NODE) {
20
+ if (type !== EXTERNAL_PLUGIN_NODE) {
21
21
  throw new Error('Invalid KubunID: type mismatch');
22
22
  }
23
23
  offset += typeLength;
@@ -67,7 +67,7 @@ export class PluginNodeID extends KubunID {
67
67
  encodeVarint(localIDBytes.byteLength),
68
68
  localIDBytes
69
69
  ]);
70
- super(ExternalType.PLUGIN_NODE, data);
70
+ super(EXTERNAL_PLUGIN_NODE, data);
71
71
  this.#plugin = plugin;
72
72
  this.#typeName = typeName;
73
73
  this.#localID = localID;
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
1
  {
2
2
  "name": "@kubun/id",
3
- "version": "0.11.0",
4
- "license": "see LICENSE.md",
3
+ "version": "0.12.0",
5
4
  "keywords": [],
5
+ "license": "see LICENSE.md",
6
+ "sideEffects": false,
6
7
  "type": "module",
7
- "main": "lib/index.js",
8
- "types": "lib/index.d.ts",
9
8
  "exports": {
10
9
  ".": "./lib/index.js"
11
10
  },
11
+ "main": "lib/index.js",
12
+ "types": "lib/index.d.ts",
12
13
  "files": [
13
14
  "lib/*",
14
15
  "LICENSE.md"
15
16
  ],
16
- "sideEffects": false,
17
17
  "dependencies": {
18
- "@sozai/codec": "^0.1.0",
19
18
  "@noble/hashes": "^2.2.0",
20
- "multiformats": "^14.0.4",
19
+ "@sozai/codec": "^0.4.0",
20
+ "multiformats": "^14.0.5",
21
21
  "varint": "^6.0.0"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/varint": "^6.0.3"
25
25
  },
26
26
  "scripts": {
27
+ "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
27
28
  "build:clean": "del lib",
28
29
  "build:js": "swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
29
30
  "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
30
31
  "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
31
- "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
32
+ "test": "pnpm run test:types && pnpm run test:unit",
32
33
  "test:types": "tsc --noEmit",
33
- "test:unit": "vitest run",
34
- "test": "pnpm run test:types && pnpm run test:unit"
34
+ "test:unit": "vitest run"
35
35
  }
36
36
  }