@zlink-systems/framework-locations-etcd 0.0.0-stage → 0.34.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.
Files changed (40) hide show
  1. package/LICENSE +93 -0
  2. package/README.en.md +66 -0
  3. package/README.ko.md +116 -0
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/admin.d.ts +2 -0
  6. package/dist/admin.js +143 -0
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.js +5 -0
  9. package/dist/internal/admin-engine.d.ts +36 -0
  10. package/dist/internal/admin-engine.js +171 -0
  11. package/dist/internal/client.d.ts +61 -0
  12. package/dist/internal/client.js +182 -0
  13. package/dist/internal/cursor.d.ts +18 -0
  14. package/dist/internal/cursor.js +67 -0
  15. package/dist/internal/encoding.d.ts +29 -0
  16. package/dist/internal/encoding.js +119 -0
  17. package/dist/internal/input.d.ts +6 -0
  18. package/dist/internal/input.js +81 -0
  19. package/dist/internal/options.d.ts +4 -0
  20. package/dist/internal/options.js +65 -0
  21. package/dist/location-store.d.ts +20 -0
  22. package/dist/location-store.js +285 -0
  23. package/dist/options.d.ts +19 -0
  24. package/dist/options.js +2 -0
  25. package/dist/proto/README.ko.md +39 -0
  26. package/dist/proto/v3.6.16/etcd/api/authpb/auth.proto +44 -0
  27. package/dist/proto/v3.6.16/etcd/api/etcdserverpb/rpc.proto +1436 -0
  28. package/dist/proto/v3.6.16/etcd/api/mvccpb/kv.proto +51 -0
  29. package/dist/proto/v3.6.16/etcd/api/versionpb/version.proto +30 -0
  30. package/dist/proto/v3.6.16/gogoproto/gogo.proto +144 -0
  31. package/dist/proto/v3.6.16/google/api/annotations.proto +31 -0
  32. package/dist/proto/v3.6.16/google/api/http.proto +379 -0
  33. package/dist/proto/v3.6.16/licenses/etcd-LICENSE +202 -0
  34. package/dist/proto/v3.6.16/licenses/gogo-protobuf-LICENSE +35 -0
  35. package/dist/proto/v3.6.16/licenses/googleapis-LICENSE +201 -0
  36. package/dist/proto/v3.6.16/licenses/grpc-gateway-LICENSE +27 -0
  37. package/dist/proto/v3.6.16/protoc-gen-openapiv2/options/annotations.proto +51 -0
  38. package/dist/proto/v3.6.16/protoc-gen-openapiv2/options/openapiv2.proto +762 -0
  39. package/package.json +24 -4
  40. package/README.md +0 -3
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScanCursor = exports.monotonicMs = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const encoding_1 = require("./encoding");
6
+ const monotonicMs = () => process.hrtime.bigint() / 1000000n;
7
+ exports.monotonicMs = monotonicMs;
8
+ /** Binary fields keep the longest UTF-8 prefix/key cursor below the SPI bound. */
9
+ class ScanCursor {
10
+ namespace;
11
+ now;
12
+ instance = (0, node_crypto_1.randomBytes)(16);
13
+ constructor(namespace, now = exports.monotonicMs) {
14
+ this.namespace = namespace;
15
+ this.now = now;
16
+ }
17
+ encode(snapshot, prefix) {
18
+ const fields = [Buffer.from(this.namespace), Buffer.from(prefix), snapshot.last];
19
+ const header = Buffer.alloc(54);
20
+ header[0] = 1;
21
+ this.instance.copy(header, 1);
22
+ header.writeBigUInt64BE(BigInt(snapshot.cluster), 17);
23
+ header.writeBigUInt64BE(BigInt(snapshot.revision), 25);
24
+ header.writeBigUInt64BE(snapshot.clock, 33);
25
+ header.writeBigUInt64BE(snapshot.started, 41);
26
+ header[49] = fields[0].length;
27
+ header.writeUInt16BE(fields[1].length, 50);
28
+ header.writeUInt16BE(fields[2].length, 52);
29
+ return Buffer.concat([header, ...fields]).toString('base64url');
30
+ }
31
+ decode(value, prefix) {
32
+ if (!/^[A-Za-z0-9_-]+$/.test(value))
33
+ return undefined;
34
+ const bytes = Buffer.from(value, 'base64url');
35
+ if (bytes.length < 54 ||
36
+ bytes.toString('base64url') !== value ||
37
+ bytes[0] !== 1 ||
38
+ !bytes.subarray(1, 17).equals(this.instance))
39
+ return undefined;
40
+ const namespaceEnd = 54 + bytes[49];
41
+ const prefixEnd = namespaceEnd + bytes.readUInt16BE(50);
42
+ if (bytes.length !== prefixEnd + bytes.readUInt16BE(52) ||
43
+ !bytes.subarray(54, namespaceEnd).equals(Buffer.from(this.namespace)) ||
44
+ !bytes.subarray(namespaceEnd, prefixEnd).equals(Buffer.from(prefix)))
45
+ return undefined;
46
+ const snapshot = {
47
+ cluster: bytes.readBigUInt64BE(17).toString(),
48
+ revision: bytes.readBigUInt64BE(25).toString(),
49
+ clock: bytes.readBigUInt64BE(33),
50
+ started: bytes.readBigUInt64BE(41),
51
+ last: bytes.subarray(prefixEnd)
52
+ };
53
+ if (snapshot.clock > encoding_1.MAX_TIME_MS ||
54
+ snapshot.revision === '0' ||
55
+ BigInt(snapshot.revision) > 0x7fffffffffffffffn ||
56
+ snapshot.last.length > 1024 ||
57
+ !snapshot.last.subarray(0, Buffer.byteLength(prefix)).equals(Buffer.from(prefix)) ||
58
+ !this.valid(snapshot))
59
+ return undefined;
60
+ return snapshot;
61
+ }
62
+ valid(snapshot) {
63
+ const elapsed = this.now() - snapshot.started;
64
+ return elapsed >= 0n && elapsed < 300000n;
65
+ }
66
+ }
67
+ exports.ScanCursor = ScanCursor;
@@ -0,0 +1,29 @@
1
+ export declare const MAX_TIME_MS = 9223372036854n;
2
+ export declare class StoreFormatError extends Error {
3
+ constructor();
4
+ }
5
+ export declare function validateNamespace(namespace: string): void;
6
+ export declare function keys(namespace: string): {
7
+ namespacePrefix: Buffer<ArrayBuffer>;
8
+ metadataPrefix: Buffer<ArrayBuffer>;
9
+ payloadPrefix: Buffer<ArrayBuffer>;
10
+ clock: Buffer<ArrayBuffer>;
11
+ metadata: (key: Uint8Array) => Buffer<ArrayBuffer>;
12
+ payload: (key: Uint8Array) => Buffer<ArrayBuffer>;
13
+ };
14
+ export declare function prefixEnd(prefix: Buffer): Buffer;
15
+ export declare function encodeClock(value: bigint): Buffer;
16
+ export declare function decodeClock(bytes: Buffer): bigint;
17
+ export interface Metadata {
18
+ hasExpiry: boolean;
19
+ expiresAtMs: bigint;
20
+ payloadLength: number;
21
+ }
22
+ export declare function encodeMetadata(value: Metadata): Buffer;
23
+ export declare function decodeMetadata(bytes: Buffer): Metadata;
24
+ export declare function validateRecord(metadata: Buffer | null, payload: Buffer | null, metadataRevision: string, payloadRevision: string): Metadata | null;
25
+ export declare function encodeVersion(clusterId: string, revision: string): string;
26
+ export declare function decodeVersion(value: string): {
27
+ clusterId: string;
28
+ revision: string;
29
+ } | null;
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StoreFormatError = exports.MAX_TIME_MS = void 0;
4
+ exports.validateNamespace = validateNamespace;
5
+ exports.keys = keys;
6
+ exports.prefixEnd = prefixEnd;
7
+ exports.encodeClock = encodeClock;
8
+ exports.decodeClock = decodeClock;
9
+ exports.encodeMetadata = encodeMetadata;
10
+ exports.decodeMetadata = decodeMetadata;
11
+ exports.validateRecord = validateRecord;
12
+ exports.encodeVersion = encodeVersion;
13
+ exports.decodeVersion = decodeVersion;
14
+ exports.MAX_TIME_MS = 9223372036854n;
15
+ const MAX_PAYLOAD_BYTES = 1_048_576;
16
+ const MAX_REVISION = 9223372036854775807n;
17
+ class StoreFormatError extends Error {
18
+ constructor() {
19
+ super('Invalid etcd Store encoding');
20
+ }
21
+ }
22
+ exports.StoreFormatError = StoreFormatError;
23
+ function validateNamespace(namespace) {
24
+ if (typeof namespace !== 'string' || namespace.includes('\0'))
25
+ throw new TypeError('Invalid namespace');
26
+ const length = Buffer.byteLength(namespace, 'utf8');
27
+ if (length < 1 || length > 64)
28
+ throw new RangeError('Namespace must contain 1..64 UTF-8 bytes');
29
+ }
30
+ function keys(namespace) {
31
+ validateNamespace(namespace);
32
+ const namespacePrefix = Buffer.from(`${namespace}\0`);
33
+ const metadataPrefix = Buffer.concat([namespacePrefix, Buffer.from('m\0')]);
34
+ const payloadPrefix = Buffer.concat([namespacePrefix, Buffer.from('p\0')]);
35
+ return {
36
+ namespacePrefix,
37
+ metadataPrefix,
38
+ payloadPrefix,
39
+ clock: Buffer.concat([namespacePrefix, Buffer.from('c')]),
40
+ metadata: (key) => Buffer.concat([metadataPrefix, key]),
41
+ payload: (key) => Buffer.concat([payloadPrefix, key])
42
+ };
43
+ }
44
+ function prefixEnd(prefix) {
45
+ const end = Buffer.from(prefix);
46
+ for (let i = end.length - 1; i >= 0; i--) {
47
+ if (end[i] < 255) {
48
+ end[i]++;
49
+ return end.subarray(0, i + 1);
50
+ }
51
+ }
52
+ return Buffer.from([0]);
53
+ }
54
+ function encodeClock(value) {
55
+ if (value < 0n || value > exports.MAX_TIME_MS)
56
+ throw new RangeError('Store time exceeds its encoding range');
57
+ const bytes = Buffer.alloc(8);
58
+ bytes.writeBigUInt64BE(value);
59
+ return bytes;
60
+ }
61
+ function decodeClock(bytes) {
62
+ if (bytes.length !== 8)
63
+ throw new StoreFormatError();
64
+ const value = bytes.readBigUInt64BE();
65
+ if (value > exports.MAX_TIME_MS)
66
+ throw new StoreFormatError();
67
+ return value;
68
+ }
69
+ function encodeMetadata(value) {
70
+ if (!Number.isInteger(value.payloadLength) ||
71
+ value.payloadLength < 0 ||
72
+ value.payloadLength > MAX_PAYLOAD_BYTES ||
73
+ (!value.hasExpiry && value.expiresAtMs !== 0n))
74
+ throw new RangeError('Invalid metadata');
75
+ const bytes = Buffer.alloc(14);
76
+ bytes[0] = 1;
77
+ bytes[1] = value.hasExpiry ? 1 : 0;
78
+ encodeClock(value.expiresAtMs).copy(bytes, 2);
79
+ bytes.writeUInt32BE(value.payloadLength, 10);
80
+ return bytes;
81
+ }
82
+ function decodeMetadata(bytes) {
83
+ if (bytes.length !== 14 || bytes[0] !== 1 || bytes[1] > 1)
84
+ throw new StoreFormatError();
85
+ const value = {
86
+ hasExpiry: bytes[1] === 1,
87
+ expiresAtMs: decodeClock(bytes.subarray(2, 10)),
88
+ payloadLength: bytes.readUInt32BE(10)
89
+ };
90
+ if ((!value.hasExpiry && value.expiresAtMs !== 0n) || value.payloadLength > MAX_PAYLOAD_BYTES)
91
+ throw new StoreFormatError();
92
+ return value;
93
+ }
94
+ function validateRecord(metadata, payload, metadataRevision, payloadRevision) {
95
+ if (metadata === null && payload === null)
96
+ return null;
97
+ if (metadata === null || payload === null || metadataRevision !== payloadRevision)
98
+ throw new StoreFormatError();
99
+ const value = decodeMetadata(metadata);
100
+ if (value.payloadLength !== payload.length)
101
+ throw new StoreFormatError();
102
+ return value;
103
+ }
104
+ function encodeVersion(clusterId, revision) {
105
+ const cluster = BigInt(clusterId);
106
+ if (cluster < 0n ||
107
+ cluster > 0xffffffffffffffffn ||
108
+ !/^[1-9][0-9]*$/.test(revision) ||
109
+ BigInt(revision).toString() !== revision ||
110
+ BigInt(revision) > MAX_REVISION)
111
+ throw new RangeError('Invalid version');
112
+ return `e1:${cluster.toString(16).padStart(16, '0')}:${revision}`;
113
+ }
114
+ function decodeVersion(value) {
115
+ const match = /^e1:([0-9a-f]{16}):([1-9][0-9]*)$/.exec(value);
116
+ if (match === null || match[0] !== value || BigInt(match[2]) > MAX_REVISION)
117
+ return null;
118
+ return { clusterId: BigInt(`0x${match[1]}`).toString(), revision: match[2] };
119
+ }
@@ -0,0 +1,6 @@
1
+ import type { ZLinkStoreKey, ZLinkStoreWriteRequest, ZLinkStoreScanRequest } from '@zlink-systems/framework';
2
+ export declare const bounds: Record<"ZLINK_PROVIDER_MAX_KEY_BYTES" | "ZLINK_PROVIDER_MAX_VALUE_BYTES" | "ZLINK_PROVIDER_MAX_VERSION_BYTES" | "ZLINK_PROVIDER_MAX_WRITE_KEYS" | "ZLINK_PROVIDER_MAX_WRITE_BYTES" | "ZLINK_PROVIDER_MAX_SCAN_CURSOR_BYTES" | "ZLINK_PROVIDER_MAX_PAGE_SIZE" | "ZLINK_PROVIDER_MAX_ENCODED_PAGE_BYTES", number>;
3
+ export declare function text(value: string, minimum: number, maximum: number): string;
4
+ export declare function key(value: ZLinkStoreKey): Buffer;
5
+ export declare function validateWrite(request: ZLinkStoreWriteRequest): void;
6
+ export declare function validateScan(request: ZLinkStoreScanRequest): void;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bounds = void 0;
4
+ exports.text = text;
5
+ exports.key = key;
6
+ exports.validateWrite = validateWrite;
7
+ exports.validateScan = validateScan;
8
+ const node_module_1 = require("node:module");
9
+ const node_path_1 = require("node:path");
10
+ const encoding_1 = require("./encoding");
11
+ const requireFramework = (0, node_module_1.createRequire)(__filename);
12
+ exports.bounds = requireFramework((0, node_path_1.join)((0, node_path_1.dirname)(requireFramework.resolve('@zlink-systems/framework')), 'internal'));
13
+ function text(value, minimum, maximum) {
14
+ if (typeof value !== 'string')
15
+ throw new TypeError('Store input must be a string');
16
+ const size = Buffer.byteLength(value);
17
+ if (size < minimum || size > maximum)
18
+ throw new RangeError('Store input exceeds its UTF-8 bound');
19
+ return value;
20
+ }
21
+ function key(value) {
22
+ return Buffer.from(text(value.value, 1, exports.bounds.ZLINK_PROVIDER_MAX_KEY_BYTES));
23
+ }
24
+ function bytes(value) {
25
+ if (!(value instanceof Uint8Array))
26
+ throw new TypeError('Store value must be bytes');
27
+ if (value.byteLength > exports.bounds.ZLINK_PROVIDER_MAX_VALUE_BYTES)
28
+ throw new RangeError('Store value exceeds its bound');
29
+ }
30
+ function validateWrite(request) {
31
+ const conditionKeys = new Set();
32
+ const mutationKeys = new Set();
33
+ let size = 0;
34
+ for (const condition of request.conditions) {
35
+ if (!['missing', 'version', 'value'].includes(condition.kind))
36
+ throw new TypeError('Invalid Store condition');
37
+ size += key(condition.key).length;
38
+ if (conditionKeys.has(condition.key.value))
39
+ throw new RangeError('Duplicate condition key');
40
+ conditionKeys.add(condition.key.value);
41
+ if (condition.kind === 'value') {
42
+ bytes(condition.expected);
43
+ size += condition.expected.byteLength;
44
+ }
45
+ else if (condition.kind === 'version')
46
+ size += Buffer.byteLength(text(condition.expected.value, 1, exports.bounds.ZLINK_PROVIDER_MAX_VERSION_BYTES));
47
+ }
48
+ for (const mutation of request.mutations) {
49
+ if (!['put', 'delete'].includes(mutation.kind))
50
+ throw new TypeError('Invalid Store mutation');
51
+ size += key(mutation.key).length;
52
+ if (mutationKeys.has(mutation.key.value))
53
+ throw new RangeError('Duplicate mutation key');
54
+ mutationKeys.add(mutation.key.value);
55
+ if (mutation.kind === 'put') {
56
+ bytes(mutation.bytes);
57
+ size += mutation.bytes.byteLength;
58
+ const retention = mutation.retentionMs;
59
+ if (retention !== undefined &&
60
+ (!Number.isFinite(retention) ||
61
+ retention <= 0 ||
62
+ !Number.isSafeInteger(Math.ceil(retention)) ||
63
+ Math.ceil(retention) > Number(encoding_1.MAX_TIME_MS))) {
64
+ throw new RangeError('Invalid Store retention');
65
+ }
66
+ }
67
+ }
68
+ if (new Set([...conditionKeys, ...mutationKeys]).size > exports.bounds.ZLINK_PROVIDER_MAX_WRITE_KEYS ||
69
+ size > exports.bounds.ZLINK_PROVIDER_MAX_WRITE_BYTES)
70
+ throw new RangeError('Store batch exceeds its bound');
71
+ }
72
+ function validateScan(request) {
73
+ text(request.prefix, 0, exports.bounds.ZLINK_PROVIDER_MAX_KEY_BYTES);
74
+ if (!Number.isSafeInteger(request.limit) ||
75
+ request.limit < 1 ||
76
+ request.limit > exports.bounds.ZLINK_PROVIDER_MAX_PAGE_SIZE) {
77
+ throw new RangeError('Invalid Store page limit');
78
+ }
79
+ if (request.cursor !== undefined)
80
+ text(request.cursor.value, 1, exports.bounds.ZLINK_PROVIDER_MAX_SCAN_CURSOR_BYTES);
81
+ }
@@ -0,0 +1,4 @@
1
+ import type { ZLinkEtcdLocationOptions } from '../options';
2
+ export declare const DEFAULT_OPERATION_TIMEOUT_MS = 5000;
3
+ export type EtcdOptions = ZLinkEtcdLocationOptions;
4
+ export declare function validateOptions(options: EtcdOptions): URL[];
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_OPERATION_TIMEOUT_MS = void 0;
4
+ exports.validateOptions = validateOptions;
5
+ const encoding_1 = require("./encoding");
6
+ const node_tls_1 = require("node:tls");
7
+ exports.DEFAULT_OPERATION_TIMEOUT_MS = 5000;
8
+ function nonempty(value) {
9
+ return typeof value === 'string' && value.length > 0;
10
+ }
11
+ function object(value) {
12
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
13
+ }
14
+ function validateOptions(options) {
15
+ (0, encoding_1.validateNamespace)(options.namespace);
16
+ if (!Array.isArray(options.endpoints) || options.endpoints.length === 0)
17
+ throw new TypeError('Endpoints are required');
18
+ const urls = options.endpoints.map((endpoint) => {
19
+ if (!nonempty(endpoint))
20
+ throw new TypeError('Invalid endpoint');
21
+ const url = new URL(endpoint);
22
+ if (!['http:', 'https:'].includes(url.protocol) ||
23
+ url.hostname.length === 0 ||
24
+ url.username !== '' ||
25
+ url.password !== '' ||
26
+ url.search !== '' ||
27
+ url.hash !== '' ||
28
+ url.pathname !== '/')
29
+ throw new TypeError('Invalid endpoint URL');
30
+ return url;
31
+ });
32
+ if (urls.some((url) => url.protocol !== urls[0].protocol))
33
+ throw new TypeError('Endpoint schemes must match');
34
+ if (options.tls !== undefined) {
35
+ if (!object(options.tls) || urls[0].protocol !== 'https:')
36
+ throw new TypeError('TLS requires HTTPS');
37
+ if (options.tls.trustedCaPem !== undefined && !nonempty(options.tls.trustedCaPem))
38
+ throw new TypeError('Invalid CA');
39
+ const identity = options.tls.clientIdentity;
40
+ if (identity !== undefined &&
41
+ (!object(identity) || !nonempty(identity.certificatePem) || !nonempty(identity.keyPem)))
42
+ throw new TypeError('Invalid client identity');
43
+ try {
44
+ (0, node_tls_1.createSecureContext)({
45
+ ca: options.tls.trustedCaPem,
46
+ cert: identity?.certificatePem,
47
+ key: identity?.keyPem
48
+ });
49
+ }
50
+ catch {
51
+ throw new TypeError('Invalid TLS material');
52
+ }
53
+ }
54
+ if (options.credentials !== undefined &&
55
+ (!object(options.credentials) ||
56
+ !nonempty(options.credentials.user) ||
57
+ !nonempty(options.credentials.password)))
58
+ throw new TypeError('Invalid credentials');
59
+ let timeout = exports.DEFAULT_OPERATION_TIMEOUT_MS;
60
+ if (options.operationTimeoutMs !== undefined)
61
+ timeout = options.operationTimeoutMs;
62
+ if (!Number.isFinite(timeout) || timeout <= 0)
63
+ throw new RangeError('Invalid operation timeout');
64
+ return urls;
65
+ }
@@ -0,0 +1,20 @@
1
+ import type { ZLinkLocationStore, ZLinkStoreKey, ZLinkStoreReadResult, ZLinkStoreWriteRequest, ZLinkStoreWriteResult, ZLinkStoreScanRequest, ZLinkStoreScanResult } from '@zlink-systems/framework';
2
+ import type { ZLinkEtcdLocationOptions } from './options';
3
+ /** Opaque Location Store SPI backed by etcd linearizable transactions. */
4
+ export declare class ZLinkEtcdLocationStore implements ZLinkLocationStore {
5
+ private readonly client;
6
+ private readonly keys;
7
+ private readonly cursors;
8
+ private readonly pending;
9
+ private disposal;
10
+ constructor(options: ZLinkEtcdLocationOptions);
11
+ read(subject: ZLinkStoreKey, signal?: AbortSignal): Promise<ZLinkStoreReadResult>;
12
+ write(request: ZLinkStoreWriteRequest, signal?: AbortSignal): Promise<ZLinkStoreWriteResult>;
13
+ scan(request: ZLinkStoreScanRequest, signal?: AbortSignal): Promise<ZLinkStoreScanResult>;
14
+ dispose(): Promise<void>;
15
+ private perform;
16
+ private readTxn;
17
+ private clock;
18
+ private value;
19
+ private scanPage;
20
+ }