@kubun/id 0.17.0 → 0.18.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/base.d.ts +3 -1
- package/lib/base.js +1 -0
- package/lib/catalog.d.ts +15 -0
- package/lib/catalog.js +91 -0
- package/lib/decode.js +4 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +2 -2
package/lib/base.d.ts
CHANGED
|
@@ -17,12 +17,14 @@ export type DocumentType = typeof DOCUMENT_MERGE;
|
|
|
17
17
|
export declare const CREDENTIAL_KEY = 31;
|
|
18
18
|
export declare const CREDENTIAL_ENTRY = 32;
|
|
19
19
|
export type CredentialType = typeof CREDENTIAL_KEY | typeof CREDENTIAL_ENTRY;
|
|
20
|
+
export declare const CATALOG = 33;
|
|
21
|
+
export type CatalogType = typeof CATALOG;
|
|
20
22
|
/**
|
|
21
23
|
* External references
|
|
22
24
|
*/
|
|
23
25
|
export declare const EXTERNAL_PLUGIN_NODE = 40;
|
|
24
26
|
export type ExternalType = typeof EXTERNAL_PLUGIN_NODE;
|
|
25
|
-
export type KubunType = AttachmentType | ModelType | DocumentType | CredentialType | ExternalType;
|
|
27
|
+
export type KubunType = AttachmentType | ModelType | DocumentType | CredentialType | CatalogType | ExternalType;
|
|
26
28
|
export declare class KubunID<T extends KubunType = KubunType> {
|
|
27
29
|
#private;
|
|
28
30
|
constructor(type: T, dataBytes: Uint8Array);
|
package/lib/base.js
CHANGED
package/lib/catalog.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CATALOG, KubunID } from './base.js';
|
|
2
|
+
export declare class CatalogID extends KubunID<typeof CATALOG> {
|
|
3
|
+
#private;
|
|
4
|
+
static create(owner: string, localID: string): CatalogID;
|
|
5
|
+
static fromBytes(input: Uint8Array): CatalogID;
|
|
6
|
+
static fromString(input: string): CatalogID;
|
|
7
|
+
static from(input: CatalogID | Uint8Array | string): CatalogID;
|
|
8
|
+
constructor(owner: string, localID: string);
|
|
9
|
+
get owner(): string;
|
|
10
|
+
get localID(): string;
|
|
11
|
+
toRecordKey(): {
|
|
12
|
+
owner: string;
|
|
13
|
+
id: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
package/lib/catalog.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { fromUTF, toUTF } from '@sozai/codec';
|
|
2
|
+
import { base36 } from 'multiformats/bases/base36';
|
|
3
|
+
import { CATALOG, KubunID, NAMESPACE_CODE } from './base.js';
|
|
4
|
+
import { concat } from './bytes.js';
|
|
5
|
+
import { decodeVarint, encodeVarint } from './utils.js';
|
|
6
|
+
const EMPTY_PARTS_MESSAGE = 'Invalid CatalogID: owner and local ID must be non-empty';
|
|
7
|
+
export class CatalogID extends KubunID {
|
|
8
|
+
static create(owner, localID) {
|
|
9
|
+
return new CatalogID(owner, localID);
|
|
10
|
+
}
|
|
11
|
+
static fromBytes(input) {
|
|
12
|
+
let offset = 0;
|
|
13
|
+
// Check namespace
|
|
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 !== CATALOG) {
|
|
22
|
+
throw new Error('Invalid KubunID: type mismatch');
|
|
23
|
+
}
|
|
24
|
+
offset += typeLength;
|
|
25
|
+
// Decode owner
|
|
26
|
+
const [ownerLength, ownerVarintLength] = decodeVarint(input, offset);
|
|
27
|
+
offset += ownerVarintLength;
|
|
28
|
+
if (offset + ownerLength > input.byteLength) {
|
|
29
|
+
throw new Error('Invalid CatalogID: truncated owner');
|
|
30
|
+
}
|
|
31
|
+
const owner = toUTF(input.slice(offset, offset + ownerLength));
|
|
32
|
+
offset += ownerLength;
|
|
33
|
+
// Decode localID
|
|
34
|
+
const [localIDLength, localIDVarintLength] = decodeVarint(input, offset);
|
|
35
|
+
offset += localIDVarintLength;
|
|
36
|
+
if (offset + localIDLength > input.byteLength) {
|
|
37
|
+
throw new Error('Invalid CatalogID: truncated local ID');
|
|
38
|
+
}
|
|
39
|
+
const localID = toUTF(input.slice(offset, offset + localIDLength));
|
|
40
|
+
offset += localIDLength;
|
|
41
|
+
if (offset !== input.byteLength) {
|
|
42
|
+
throw new Error('Invalid CatalogID: unexpected trailing bytes');
|
|
43
|
+
}
|
|
44
|
+
return new CatalogID(owner, localID);
|
|
45
|
+
}
|
|
46
|
+
static fromString(input) {
|
|
47
|
+
return CatalogID.fromBytes(base36.decode(input));
|
|
48
|
+
}
|
|
49
|
+
static from(input) {
|
|
50
|
+
if (input instanceof CatalogID) {
|
|
51
|
+
return input;
|
|
52
|
+
}
|
|
53
|
+
if (input instanceof Uint8Array) {
|
|
54
|
+
return CatalogID.fromBytes(input);
|
|
55
|
+
}
|
|
56
|
+
if (typeof input === 'string') {
|
|
57
|
+
return CatalogID.fromString(input);
|
|
58
|
+
}
|
|
59
|
+
throw new Error('Unsupported input type');
|
|
60
|
+
}
|
|
61
|
+
#owner;
|
|
62
|
+
#localID;
|
|
63
|
+
constructor(owner, localID){
|
|
64
|
+
if (owner.length === 0 || localID.length === 0) {
|
|
65
|
+
throw new Error(EMPTY_PARTS_MESSAGE);
|
|
66
|
+
}
|
|
67
|
+
const ownerBytes = fromUTF(owner);
|
|
68
|
+
const localIDBytes = fromUTF(localID);
|
|
69
|
+
const data = concat([
|
|
70
|
+
encodeVarint(ownerBytes.byteLength),
|
|
71
|
+
ownerBytes,
|
|
72
|
+
encodeVarint(localIDBytes.byteLength),
|
|
73
|
+
localIDBytes
|
|
74
|
+
]);
|
|
75
|
+
super(CATALOG, data);
|
|
76
|
+
this.#owner = owner;
|
|
77
|
+
this.#localID = localID;
|
|
78
|
+
}
|
|
79
|
+
get owner() {
|
|
80
|
+
return this.#owner;
|
|
81
|
+
}
|
|
82
|
+
get localID() {
|
|
83
|
+
return this.#localID;
|
|
84
|
+
}
|
|
85
|
+
toRecordKey() {
|
|
86
|
+
return {
|
|
87
|
+
owner: this.#owner,
|
|
88
|
+
id: this.#localID
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
package/lib/decode.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { base36 } from 'multiformats/bases/base36';
|
|
2
2
|
import { AttachmentID } from './attachment.js';
|
|
3
|
-
import { ATTACHMENT_BINARY, CREDENTIAL_ENTRY, CREDENTIAL_KEY, DOCUMENT_MERGE, EXTERNAL_PLUGIN_NODE, MODEL_CLUSTER, MODEL_DOCUMENT, MODEL_SCALAR, MODEL_SHAPE, NAMESPACE_CODE } from './base.js';
|
|
3
|
+
import { ATTACHMENT_BINARY, CATALOG, CREDENTIAL_ENTRY, CREDENTIAL_KEY, DOCUMENT_MERGE, EXTERNAL_PLUGIN_NODE, MODEL_CLUSTER, MODEL_DOCUMENT, MODEL_SCALAR, MODEL_SHAPE, NAMESPACE_CODE } from './base.js';
|
|
4
|
+
import { CatalogID } from './catalog.js';
|
|
4
5
|
import { ClusterModelID } from './cluster-model.js';
|
|
5
6
|
import { CredentialEntryID, CredentialKeyID } from './credential.js';
|
|
6
7
|
import { DocumentID } from './document.js';
|
|
@@ -31,6 +32,8 @@ export function decodeID(input) {
|
|
|
31
32
|
return CredentialKeyID.fromBytes(bytes);
|
|
32
33
|
case CREDENTIAL_ENTRY:
|
|
33
34
|
return CredentialEntryID.fromBytes(bytes);
|
|
35
|
+
case CATALOG:
|
|
36
|
+
return CatalogID.fromBytes(bytes);
|
|
34
37
|
case EXTERNAL_PLUGIN_NODE:
|
|
35
38
|
return PluginNodeID.fromBytes(bytes);
|
|
36
39
|
default:
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { AttachmentID } from './attachment.js';
|
|
2
2
|
export { KubunID } from './base.js';
|
|
3
|
+
export { CatalogID } from './catalog.js';
|
|
3
4
|
export { ClusterModelID } from './cluster-model.js';
|
|
4
5
|
export { CredentialEntryID, CredentialKeyID } from './credential.js';
|
|
5
6
|
export { decodeID } from './decode.js';
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { AttachmentID } from './attachment.js';
|
|
2
2
|
export { KubunID } from './base.js';
|
|
3
|
+
export { CatalogID } from './catalog.js';
|
|
3
4
|
export { ClusterModelID } from './cluster-model.js';
|
|
4
5
|
export { CredentialEntryID, CredentialKeyID } from './credential.js';
|
|
5
6
|
export { decodeID } from './decode.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/id",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"license": "see LICENSE.md",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@noble/hashes": "^2.4.0",
|
|
19
|
-
"@sozai/codec": "^0.4.
|
|
19
|
+
"@sozai/codec": "^0.4.3",
|
|
20
20
|
"multiformats": "^14.0.5",
|
|
21
21
|
"varint": "^6.0.0"
|
|
22
22
|
},
|