@kubun/id 0.12.0 → 0.14.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 CHANGED
@@ -14,12 +14,15 @@ export type ModelType = typeof MODEL_SCALAR | typeof MODEL_SHAPE | typeof MODEL_
14
14
  */
15
15
  export declare const DOCUMENT_MERGE = 30;
16
16
  export type DocumentType = typeof DOCUMENT_MERGE;
17
+ export declare const CREDENTIAL_KEY = 31;
18
+ export declare const CREDENTIAL_ENTRY = 32;
19
+ export type CredentialType = typeof CREDENTIAL_KEY | typeof CREDENTIAL_ENTRY;
17
20
  /**
18
21
  * External references
19
22
  */
20
23
  export declare const EXTERNAL_PLUGIN_NODE = 40;
21
24
  export type ExternalType = typeof EXTERNAL_PLUGIN_NODE;
22
- export type KubunType = AttachmentType | ModelType | DocumentType | ExternalType;
25
+ export type KubunType = AttachmentType | ModelType | DocumentType | CredentialType | ExternalType;
23
26
  export declare class KubunID<T extends KubunType = KubunType> {
24
27
  #private;
25
28
  constructor(type: T, dataBytes: Uint8Array);
package/lib/base.js CHANGED
@@ -15,6 +15,8 @@ export const MODEL_CLUSTER = 21;
15
15
  /**
16
16
  * Mutable references
17
17
  */ export const DOCUMENT_MERGE = 30;
18
+ export const CREDENTIAL_KEY = 31;
19
+ export const CREDENTIAL_ENTRY = 32;
18
20
  /**
19
21
  * External references
20
22
  */ export const EXTERNAL_PLUGIN_NODE = 40;
@@ -0,0 +1,34 @@
1
+ import { CREDENTIAL_ENTRY, CREDENTIAL_KEY, KubunID } from './base.js';
2
+ /**
3
+ * A credential key: the unit a wrapping addresses, a grant shares and a rotation
4
+ * bumps.
5
+ *
6
+ * Not content-addressed. Rotation preserves the id and moves `version`, so a
7
+ * document referencing a key keeps referencing it across rotations — which is
8
+ * the whole reason rotation does not mint a new id.
9
+ */
10
+ export declare class CredentialKeyID extends KubunID<typeof CREDENTIAL_KEY> {
11
+ #private;
12
+ static create(owner: string | Uint8Array, unique: Uint8Array): CredentialKeyID;
13
+ static fromBytes(input: Uint8Array): CredentialKeyID;
14
+ static fromString(input: string): CredentialKeyID;
15
+ static from(input: CredentialKeyID | Uint8Array | string): CredentialKeyID;
16
+ constructor(digest: Uint8Array);
17
+ get digest(): Uint8Array;
18
+ }
19
+ /**
20
+ * A credential entry: one encrypted value under a key.
21
+ *
22
+ * Does not embed its key's id. An entry can be re-keyed without every document
23
+ * that references it changing, and its value changes — a refreshed OAuth token —
24
+ * without the reference changing either.
25
+ */
26
+ export declare class CredentialEntryID extends KubunID<typeof CREDENTIAL_ENTRY> {
27
+ #private;
28
+ static create(owner: string | Uint8Array, unique: Uint8Array): CredentialEntryID;
29
+ static fromBytes(input: Uint8Array): CredentialEntryID;
30
+ static fromString(input: string): CredentialEntryID;
31
+ static from(input: CredentialEntryID | Uint8Array | string): CredentialEntryID;
32
+ constructor(digest: Uint8Array);
33
+ get digest(): Uint8Array;
34
+ }
@@ -0,0 +1,110 @@
1
+ import { fromUTF } from '@sozai/codec';
2
+ import { base36 } from 'multiformats/bases/base36';
3
+ import { CREDENTIAL_ENTRY, CREDENTIAL_KEY, KubunID, NAMESPACE_CODE } from './base.js';
4
+ import { concat } from './bytes.js';
5
+ import { decodeVarint, digestBytes, encodeVarint } from './utils.js';
6
+ function encodeData(digest) {
7
+ return concat([
8
+ encodeVarint(digest.byteLength),
9
+ digest
10
+ ]);
11
+ }
12
+ function decodeDigest(input, expected) {
13
+ let offset = 0;
14
+ // Check namespace = is it a KubunID?
15
+ const [namespace, namespaceLength] = decodeVarint(input);
16
+ if (namespace !== NAMESPACE_CODE) {
17
+ throw new Error('Invalid KubunID: namespace mismatch');
18
+ }
19
+ offset += namespaceLength;
20
+ // Check type matches
21
+ const [type, typeLength] = decodeVarint(input, offset);
22
+ if (type !== expected) {
23
+ throw new Error('Invalid KubunID: type mismatch');
24
+ }
25
+ offset += typeLength;
26
+ const [digestLength, digestVarintLength] = decodeVarint(input, offset);
27
+ offset += digestVarintLength;
28
+ return input.slice(offset, offset + digestLength);
29
+ }
30
+ function createDigest(owner, unique) {
31
+ return digestBytes(concat([
32
+ typeof owner === 'string' ? fromUTF(owner) : owner,
33
+ unique
34
+ ]));
35
+ }
36
+ /**
37
+ * A credential key: the unit a wrapping addresses, a grant shares and a rotation
38
+ * bumps.
39
+ *
40
+ * Not content-addressed. Rotation preserves the id and moves `version`, so a
41
+ * document referencing a key keeps referencing it across rotations — which is
42
+ * the whole reason rotation does not mint a new id.
43
+ */ export class CredentialKeyID extends KubunID {
44
+ static create(owner, unique) {
45
+ return new CredentialKeyID(createDigest(owner, unique));
46
+ }
47
+ static fromBytes(input) {
48
+ return new CredentialKeyID(decodeDigest(input, CREDENTIAL_KEY));
49
+ }
50
+ static fromString(input) {
51
+ return CredentialKeyID.fromBytes(base36.decode(input));
52
+ }
53
+ static from(input) {
54
+ if (input instanceof CredentialKeyID) {
55
+ return input;
56
+ }
57
+ if (input instanceof Uint8Array) {
58
+ return CredentialKeyID.fromBytes(input);
59
+ }
60
+ if (typeof input === 'string') {
61
+ return CredentialKeyID.fromString(input);
62
+ }
63
+ throw new Error('Unsupported input type');
64
+ }
65
+ #digest;
66
+ constructor(digest){
67
+ super(CREDENTIAL_KEY, encodeData(digest));
68
+ this.#digest = digest;
69
+ }
70
+ get digest() {
71
+ return this.#digest;
72
+ }
73
+ }
74
+ /**
75
+ * A credential entry: one encrypted value under a key.
76
+ *
77
+ * Does not embed its key's id. An entry can be re-keyed without every document
78
+ * that references it changing, and its value changes — a refreshed OAuth token —
79
+ * without the reference changing either.
80
+ */ export class CredentialEntryID extends KubunID {
81
+ static create(owner, unique) {
82
+ return new CredentialEntryID(createDigest(owner, unique));
83
+ }
84
+ static fromBytes(input) {
85
+ return new CredentialEntryID(decodeDigest(input, CREDENTIAL_ENTRY));
86
+ }
87
+ static fromString(input) {
88
+ return CredentialEntryID.fromBytes(base36.decode(input));
89
+ }
90
+ static from(input) {
91
+ if (input instanceof CredentialEntryID) {
92
+ return input;
93
+ }
94
+ if (input instanceof Uint8Array) {
95
+ return CredentialEntryID.fromBytes(input);
96
+ }
97
+ if (typeof input === 'string') {
98
+ return CredentialEntryID.fromString(input);
99
+ }
100
+ throw new Error('Unsupported input type');
101
+ }
102
+ #digest;
103
+ constructor(digest){
104
+ super(CREDENTIAL_ENTRY, encodeData(digest));
105
+ this.#digest = digest;
106
+ }
107
+ get digest() {
108
+ return this.#digest;
109
+ }
110
+ }
package/lib/decode.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { base36 } from 'multiformats/bases/base36';
2
2
  import { AttachmentID } from './attachment.js';
3
- import { ATTACHMENT_BINARY, DOCUMENT_MERGE, EXTERNAL_PLUGIN_NODE, MODEL_CLUSTER, MODEL_DOCUMENT, MODEL_SCALAR, MODEL_SHAPE, NAMESPACE_CODE } from './base.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';
4
4
  import { ClusterModelID } from './cluster-model.js';
5
+ import { CredentialEntryID, CredentialKeyID } from './credential.js';
5
6
  import { DocumentID } from './document.js';
6
7
  import { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
7
8
  import { PluginNodeID } from './plugin-node.js';
@@ -26,6 +27,10 @@ export function decodeID(input) {
26
27
  return ClusterModelID.fromBytes(bytes);
27
28
  case DOCUMENT_MERGE:
28
29
  return DocumentID.fromBytes(bytes);
30
+ case CREDENTIAL_KEY:
31
+ return CredentialKeyID.fromBytes(bytes);
32
+ case CREDENTIAL_ENTRY:
33
+ return CredentialEntryID.fromBytes(bytes);
29
34
  case EXTERNAL_PLUGIN_NODE:
30
35
  return PluginNodeID.fromBytes(bytes);
31
36
  default:
package/lib/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { AttachmentID } from './attachment.js';
2
2
  export { KubunID } from './base.js';
3
3
  export { ClusterModelID } from './cluster-model.js';
4
+ export { CredentialEntryID, CredentialKeyID } from './credential.js';
4
5
  export { decodeID } from './decode.js';
5
6
  export { DocumentID } from './document.js';
6
7
  export { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { AttachmentID } from './attachment.js';
2
2
  export { KubunID } from './base.js';
3
3
  export { ClusterModelID } from './cluster-model.js';
4
+ export { CredentialEntryID, CredentialKeyID } from './credential.js';
4
5
  export { decodeID } from './decode.js';
5
6
  export { DocumentID } from './document.js';
6
7
  export { DocumentModelID, ScalarModelID, ShapeModelID } from './model.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/id",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "keywords": [],
5
5
  "license": "see LICENSE.md",
6
6
  "sideEffects": false,
@@ -15,7 +15,7 @@
15
15
  "LICENSE.md"
16
16
  ],
17
17
  "dependencies": {
18
- "@noble/hashes": "^2.2.0",
18
+ "@noble/hashes": "^2.3.0",
19
19
  "@sozai/codec": "^0.4.0",
20
20
  "multiformats": "^14.0.5",
21
21
  "varint": "^6.0.0"
@@ -23,6 +23,9 @@
23
23
  "devDependencies": {
24
24
  "@types/varint": "^6.0.3"
25
25
  },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
26
29
  "scripts": {
27
30
  "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
28
31
  "build:clean": "del lib",