@korajs/core 0.1.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/dist/chunk-ZMUY7AVV.js +421 -0
- package/dist/chunk-ZMUY7AVV.js.map +1 -0
- package/dist/events-D_kDPDC9.d.cts +324 -0
- package/dist/events-D_kDPDC9.d.ts +324 -0
- package/dist/index.cjs +1153 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +401 -0
- package/dist/index.d.ts +401 -0
- package/dist/index.js +707 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.cjs +397 -0
- package/dist/internal.cjs.map +1 -0
- package/dist/internal.d.cts +58 -0
- package/dist/internal.d.ts +58 -0
- package/dist/internal.js +55 -0
- package/dist/internal.js.map +1 -0
- package/package.json +49 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { p as OperationInput, j as KoraEventEmitter, l as KoraEventType, k as KoraEventListener, i as KoraEventByType, d as Operation } from './events-D_kDPDC9.js';
|
|
2
|
+
export { t as validateOperationParams } from './events-D_kDPDC9.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Compute the content-addressed ID for an operation using SHA-256.
|
|
6
|
+
* The same operation content always produces the same hash, ensuring deduplication.
|
|
7
|
+
*
|
|
8
|
+
* @param input - The operation input (without id/timestamp, which are assigned separately)
|
|
9
|
+
* @param timestamp - The HLC timestamp serialized as a string
|
|
10
|
+
* @returns A hex-encoded SHA-256 hash
|
|
11
|
+
*/
|
|
12
|
+
declare function computeOperationId(input: OperationInput, timestamp: string): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* Deterministic JSON serialization with sorted keys.
|
|
15
|
+
* Ensures identical objects always produce identical strings regardless of property insertion order.
|
|
16
|
+
*
|
|
17
|
+
* @param obj - The value to serialize
|
|
18
|
+
* @returns A deterministic JSON string
|
|
19
|
+
*/
|
|
20
|
+
declare function canonicalize(obj: unknown): string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Concrete implementation of KoraEventEmitter.
|
|
24
|
+
* Simple, synchronous event emitter for internal use across @kora packages.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const emitter = new SimpleEventEmitter()
|
|
29
|
+
* const unsub = emitter.on('operation:created', (event) => {
|
|
30
|
+
* console.log(event.operation.id)
|
|
31
|
+
* })
|
|
32
|
+
* emitter.emit({ type: 'operation:created', operation: someOp })
|
|
33
|
+
* unsub() // unsubscribe
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare class SimpleEventEmitter implements KoraEventEmitter {
|
|
37
|
+
private listeners;
|
|
38
|
+
on<T extends KoraEventType>(type: T, listener: KoraEventListener<T>): () => void;
|
|
39
|
+
off<T extends KoraEventType>(type: T, listener: KoraEventListener<T>): void;
|
|
40
|
+
emit<T extends KoraEventType>(event: KoraEventByType<T>): void;
|
|
41
|
+
/** Remove all listeners for all event types. */
|
|
42
|
+
clear(): void;
|
|
43
|
+
/** Get the number of listeners for a specific event type. */
|
|
44
|
+
listenerCount(type: KoraEventType): number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Topological sort of operations based on their causal dependency DAG.
|
|
49
|
+
* Uses Kahn's algorithm with deterministic tie-breaking via HLC timestamp.
|
|
50
|
+
* Time complexity: O(V + E) where V = operations, E = causal dependency edges.
|
|
51
|
+
*
|
|
52
|
+
* @param operations - The operations to sort
|
|
53
|
+
* @returns Operations in causal order (dependencies before dependents)
|
|
54
|
+
* @throws {OperationError} If a cycle is detected in the dependency graph
|
|
55
|
+
*/
|
|
56
|
+
declare function topologicalSort(operations: Operation[]): Operation[];
|
|
57
|
+
|
|
58
|
+
export { SimpleEventEmitter, canonicalize, computeOperationId, topologicalSort };
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
canonicalize,
|
|
3
|
+
computeOperationId,
|
|
4
|
+
topologicalSort,
|
|
5
|
+
validateOperationParams
|
|
6
|
+
} from "./chunk-ZMUY7AVV.js";
|
|
7
|
+
|
|
8
|
+
// src/events/event-emitter.ts
|
|
9
|
+
var SimpleEventEmitter = class {
|
|
10
|
+
listeners = /* @__PURE__ */ new Map();
|
|
11
|
+
on(type, listener) {
|
|
12
|
+
let set = this.listeners.get(type);
|
|
13
|
+
if (!set) {
|
|
14
|
+
set = /* @__PURE__ */ new Set();
|
|
15
|
+
this.listeners.set(type, set);
|
|
16
|
+
}
|
|
17
|
+
set.add(listener);
|
|
18
|
+
return () => {
|
|
19
|
+
this.off(type, listener);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
off(type, listener) {
|
|
23
|
+
const set = this.listeners.get(type);
|
|
24
|
+
if (set) {
|
|
25
|
+
set.delete(listener);
|
|
26
|
+
if (set.size === 0) {
|
|
27
|
+
this.listeners.delete(type);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
emit(event) {
|
|
32
|
+
const set = this.listeners.get(event.type);
|
|
33
|
+
if (!set) return;
|
|
34
|
+
for (const listener of set) {
|
|
35
|
+
;
|
|
36
|
+
listener(event);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Remove all listeners for all event types. */
|
|
40
|
+
clear() {
|
|
41
|
+
this.listeners.clear();
|
|
42
|
+
}
|
|
43
|
+
/** Get the number of listeners for a specific event type. */
|
|
44
|
+
listenerCount(type) {
|
|
45
|
+
return this.listeners.get(type)?.size ?? 0;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
export {
|
|
49
|
+
SimpleEventEmitter,
|
|
50
|
+
canonicalize,
|
|
51
|
+
computeOperationId,
|
|
52
|
+
topologicalSort,
|
|
53
|
+
validateOperationParams
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=internal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/events/event-emitter.ts"],"sourcesContent":["import type { KoraEventByType, KoraEventEmitter, KoraEventListener, KoraEventType } from './events'\n\ntype AnyListener = (event: never) => void\n\n/**\n * Concrete implementation of KoraEventEmitter.\n * Simple, synchronous event emitter for internal use across @kora packages.\n *\n * @example\n * ```typescript\n * const emitter = new SimpleEventEmitter()\n * const unsub = emitter.on('operation:created', (event) => {\n * console.log(event.operation.id)\n * })\n * emitter.emit({ type: 'operation:created', operation: someOp })\n * unsub() // unsubscribe\n * ```\n */\nexport class SimpleEventEmitter implements KoraEventEmitter {\n\tprivate listeners = new Map<string, Set<AnyListener>>()\n\n\ton<T extends KoraEventType>(type: T, listener: KoraEventListener<T>): () => void {\n\t\tlet set = this.listeners.get(type)\n\t\tif (!set) {\n\t\t\tset = new Set()\n\t\t\tthis.listeners.set(type, set)\n\t\t}\n\t\tset.add(listener as AnyListener)\n\n\t\treturn () => {\n\t\t\tthis.off(type, listener)\n\t\t}\n\t}\n\n\toff<T extends KoraEventType>(type: T, listener: KoraEventListener<T>): void {\n\t\tconst set = this.listeners.get(type)\n\t\tif (set) {\n\t\t\tset.delete(listener as AnyListener)\n\t\t\tif (set.size === 0) {\n\t\t\t\tthis.listeners.delete(type)\n\t\t\t}\n\t\t}\n\t}\n\n\temit<T extends KoraEventType>(event: KoraEventByType<T>): void {\n\t\tconst set = this.listeners.get(event.type)\n\t\tif (!set) return\n\t\tfor (const listener of set) {\n\t\t\t;(listener as (event: KoraEventByType<T>) => void)(event)\n\t\t}\n\t}\n\n\t/** Remove all listeners for all event types. */\n\tclear(): void {\n\t\tthis.listeners.clear()\n\t}\n\n\t/** Get the number of listeners for a specific event type. */\n\tlistenerCount(type: KoraEventType): number {\n\t\treturn this.listeners.get(type)?.size ?? 0\n\t}\n}\n"],"mappings":";;;;;;;;AAkBO,IAAM,qBAAN,MAAqD;AAAA,EACnD,YAAY,oBAAI,IAA8B;AAAA,EAEtD,GAA4B,MAAS,UAA4C;AAChF,QAAI,MAAM,KAAK,UAAU,IAAI,IAAI;AACjC,QAAI,CAAC,KAAK;AACT,YAAM,oBAAI,IAAI;AACd,WAAK,UAAU,IAAI,MAAM,GAAG;AAAA,IAC7B;AACA,QAAI,IAAI,QAAuB;AAE/B,WAAO,MAAM;AACZ,WAAK,IAAI,MAAM,QAAQ;AAAA,IACxB;AAAA,EACD;AAAA,EAEA,IAA6B,MAAS,UAAsC;AAC3E,UAAM,MAAM,KAAK,UAAU,IAAI,IAAI;AACnC,QAAI,KAAK;AACR,UAAI,OAAO,QAAuB;AAClC,UAAI,IAAI,SAAS,GAAG;AACnB,aAAK,UAAU,OAAO,IAAI;AAAA,MAC3B;AAAA,IACD;AAAA,EACD;AAAA,EAEA,KAA8B,OAAiC;AAC9D,UAAM,MAAM,KAAK,UAAU,IAAI,MAAM,IAAI;AACzC,QAAI,CAAC,IAAK;AACV,eAAW,YAAY,KAAK;AAC3B;AAAC,MAAC,SAAiD,KAAK;AAAA,IACzD;AAAA,EACD;AAAA;AAAA,EAGA,QAAc;AACb,SAAK,UAAU,MAAM;AAAA,EACtB;AAAA;AAAA,EAGA,cAAc,MAA6B;AAC1C,WAAO,KAAK,UAAU,IAAI,IAAI,GAAG,QAAQ;AAAA,EAC1C;AACD;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@korajs/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Kora.js core package",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"./internal": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/internal.d.ts",
|
|
23
|
+
"default": "./dist/internal.js"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/internal.d.cts",
|
|
27
|
+
"default": "./dist/internal.cjs"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"fast-check": "^4.6.0",
|
|
36
|
+
"tsup": "^8.3.6",
|
|
37
|
+
"typescript": "^5.7.3",
|
|
38
|
+
"vitest": "^3.0.4"
|
|
39
|
+
},
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"dev": "tsup --watch",
|
|
44
|
+
"test": "vitest run",
|
|
45
|
+
"test:watch": "vitest",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"clean": "rm -rf dist .turbo coverage"
|
|
48
|
+
}
|
|
49
|
+
}
|