observed-js 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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,aAAa,EACb,YAAY,EACZ,WAAW,EACX,KAAK,iBAAiB,EACtB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,YAAY,EACpB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACH,kBAAkB,EACrB,MAAM,sBAAsB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { ActionBuilder, createAction, withContext } from "./actions/index.js";
2
+ export { ExecutionScheduler } from "./scheduler/index.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,aAAa,EACb,YAAY,EACZ,WAAW,EAKd,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACH,kBAAkB,EACrB,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * ExecutionScheduler manages concurrency and rate limiting for action execution.
3
+ *
4
+ * This class is logically separated from ActionBuilder to maintain clean architecture
5
+ * and single responsibility principle.
6
+ */
7
+ export declare class ExecutionScheduler {
8
+ /** Maximum number of concurrent executions (default: 1 = sequential) */
9
+ private concurrencyLimit;
10
+ /** Maximum executions per second (default: Infinity = no limit) */
11
+ private rateLimitPerSecond;
12
+ /** Currently running execution count */
13
+ private activeExecutions;
14
+ /** Queue of pending tasks */
15
+ private queue;
16
+ /** Timestamps of recent executions for rate limiting (sliding window) */
17
+ private executionTimestamps;
18
+ constructor(concurrencyLimit?: number, rateLimitPerSecond?: number);
19
+ /**
20
+ * Update the concurrency limit
21
+ */
22
+ setConcurrency(limit: number): void;
23
+ /**
24
+ * Update the rate limit
25
+ */
26
+ setRateLimit(limit: number): void;
27
+ /**
28
+ * Schedule a task for execution. Returns a promise that resolves when the task completes.
29
+ */
30
+ schedule<T>(execute: () => Promise<T> | T): Promise<T>;
31
+ /**
32
+ * Check if we can execute based on concurrency limit
33
+ */
34
+ private canExecuteConcurrency;
35
+ /**
36
+ * Check if we can execute based on rate limit (sliding window algorithm)
37
+ */
38
+ private canExecuteRateLimit;
39
+ /**
40
+ * Calculate how long to wait before we can execute (for rate limiting)
41
+ */
42
+ private getWaitTime;
43
+ /**
44
+ * Process the queue - execute tasks if limits allow
45
+ */
46
+ private processQueue;
47
+ /**
48
+ * Execute a single task with proper tracking
49
+ */
50
+ private executeTask;
51
+ /**
52
+ * Get current queue length (useful for monitoring)
53
+ */
54
+ getQueueLength(): number;
55
+ /**
56
+ * Get current active execution count (useful for monitoring)
57
+ */
58
+ getActiveCount(): number;
59
+ }
60
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/scheduler/index.ts"],"names":[],"mappings":"AASA;;;;;GAKG;AACH,qBAAa,kBAAkB;IAC3B,wEAAwE;IACxE,OAAO,CAAC,gBAAgB,CAAS;IACjC,mEAAmE;IACnE,OAAO,CAAC,kBAAkB,CAAS;IACnC,wCAAwC;IACxC,OAAO,CAAC,gBAAgB,CAAa;IACrC,6BAA6B;IAC7B,OAAO,CAAC,KAAK,CAA4B;IACzC,yEAAyE;IACzE,OAAO,CAAC,mBAAmB,CAAgB;gBAE/B,gBAAgB,GAAE,MAAU,EAAE,kBAAkB,GAAE,MAAiB;IAK/E;;OAEG;IACH,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAMnC;;OAEG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAMjC;;OAEG;IACH,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAQtD;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAI7B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAc3B;;OAEG;IACH,OAAO,CAAC,WAAW;IAoBnB;;OAEG;IACH,OAAO,CAAC,YAAY;IAkCpB;;OAEG;YACW,WAAW;IAgBzB;;OAEG;IACH,cAAc,IAAI,MAAM;IAIxB;;OAEG;IACH,cAAc,IAAI,MAAM;CAG3B"}
@@ -0,0 +1,146 @@
1
+ /**
2
+ * ExecutionScheduler manages concurrency and rate limiting for action execution.
3
+ *
4
+ * This class is logically separated from ActionBuilder to maintain clean architecture
5
+ * and single responsibility principle.
6
+ */
7
+ export class ExecutionScheduler {
8
+ /** Maximum number of concurrent executions (default: 1 = sequential) */
9
+ concurrencyLimit;
10
+ /** Maximum executions per second (default: Infinity = no limit) */
11
+ rateLimitPerSecond;
12
+ /** Currently running execution count */
13
+ activeExecutions = 0;
14
+ /** Queue of pending tasks */
15
+ queue = [];
16
+ /** Timestamps of recent executions for rate limiting (sliding window) */
17
+ executionTimestamps = [];
18
+ constructor(concurrencyLimit = 1, rateLimitPerSecond = Infinity) {
19
+ this.concurrencyLimit = concurrencyLimit;
20
+ this.rateLimitPerSecond = rateLimitPerSecond;
21
+ }
22
+ /**
23
+ * Update the concurrency limit
24
+ */
25
+ setConcurrency(limit) {
26
+ this.concurrencyLimit = limit;
27
+ // Try to process queue in case new limit allows more executions
28
+ this.processQueue();
29
+ }
30
+ /**
31
+ * Update the rate limit
32
+ */
33
+ setRateLimit(limit) {
34
+ this.rateLimitPerSecond = limit;
35
+ // Try to process queue in case new limit allows more executions
36
+ this.processQueue();
37
+ }
38
+ /**
39
+ * Schedule a task for execution. Returns a promise that resolves when the task completes.
40
+ */
41
+ schedule(execute) {
42
+ return new Promise((resolve, reject) => {
43
+ const task = { execute, resolve, reject };
44
+ this.queue.push(task);
45
+ this.processQueue();
46
+ });
47
+ }
48
+ /**
49
+ * Check if we can execute based on concurrency limit
50
+ */
51
+ canExecuteConcurrency() {
52
+ return this.activeExecutions < this.concurrencyLimit;
53
+ }
54
+ /**
55
+ * Check if we can execute based on rate limit (sliding window algorithm)
56
+ */
57
+ canExecuteRateLimit() {
58
+ if (this.rateLimitPerSecond === Infinity) {
59
+ return true;
60
+ }
61
+ const now = Date.now();
62
+ // Clean up timestamps older than 1 second
63
+ this.executionTimestamps = this.executionTimestamps.filter(ts => now - ts < 1000);
64
+ return this.executionTimestamps.length < this.rateLimitPerSecond;
65
+ }
66
+ /**
67
+ * Calculate how long to wait before we can execute (for rate limiting)
68
+ */
69
+ getWaitTime() {
70
+ if (this.rateLimitPerSecond === Infinity || this.executionTimestamps.length === 0) {
71
+ return 0;
72
+ }
73
+ const now = Date.now();
74
+ // Clean up old timestamps
75
+ this.executionTimestamps = this.executionTimestamps.filter(ts => now - ts < 1000);
76
+ if (this.executionTimestamps.length < this.rateLimitPerSecond) {
77
+ return 0;
78
+ }
79
+ // Wait until the oldest timestamp expires
80
+ const oldestTimestamp = Math.min(...this.executionTimestamps);
81
+ return Math.max(0, 1000 - (now - oldestTimestamp) + 1);
82
+ }
83
+ /**
84
+ * Process the queue - execute tasks if limits allow
85
+ */
86
+ processQueue() {
87
+ if (this.queue.length === 0) {
88
+ return;
89
+ }
90
+ // Check concurrency limit
91
+ if (!this.canExecuteConcurrency()) {
92
+ return;
93
+ }
94
+ // Check rate limit
95
+ if (!this.canExecuteRateLimit()) {
96
+ // Schedule retry after wait time
97
+ const waitTime = this.getWaitTime();
98
+ if (waitTime > 0) {
99
+ setTimeout(() => this.processQueue(), waitTime);
100
+ }
101
+ return;
102
+ }
103
+ // Get next task and execute
104
+ const task = this.queue.shift();
105
+ if (!task) {
106
+ return;
107
+ }
108
+ this.executeTask(task);
109
+ // Continue processing queue for parallel execution
110
+ if (this.concurrencyLimit > 1) {
111
+ this.processQueue();
112
+ }
113
+ }
114
+ /**
115
+ * Execute a single task with proper tracking
116
+ */
117
+ async executeTask(task) {
118
+ this.activeExecutions++;
119
+ this.executionTimestamps.push(Date.now());
120
+ try {
121
+ const result = await task.execute();
122
+ task.resolve(result);
123
+ }
124
+ catch (error) {
125
+ task.reject(error);
126
+ }
127
+ finally {
128
+ this.activeExecutions--;
129
+ // Process next item in queue
130
+ this.processQueue();
131
+ }
132
+ }
133
+ /**
134
+ * Get current queue length (useful for monitoring)
135
+ */
136
+ getQueueLength() {
137
+ return this.queue.length;
138
+ }
139
+ /**
140
+ * Get current active execution count (useful for monitoring)
141
+ */
142
+ getActiveCount() {
143
+ return this.activeExecutions;
144
+ }
145
+ }
146
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/scheduler/index.ts"],"names":[],"mappings":"AASA;;;;;GAKG;AACH,MAAM,OAAO,kBAAkB;IAC3B,wEAAwE;IAChE,gBAAgB,CAAS;IACjC,mEAAmE;IAC3D,kBAAkB,CAAS;IACnC,wCAAwC;IAChC,gBAAgB,GAAW,CAAC,CAAC;IACrC,6BAA6B;IACrB,KAAK,GAAyB,EAAE,CAAC;IACzC,yEAAyE;IACjE,mBAAmB,GAAa,EAAE,CAAC;IAE3C,YAAY,mBAA2B,CAAC,EAAE,qBAA6B,QAAQ;QAC3E,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;IACjD,CAAC;IAED;;OAEG;IACH,cAAc,CAAC,KAAa;QACxB,IAAI,CAAC,gBAAgB,GAAG,KAAK,CAAC;QAC9B,gEAAgE;QAChE,IAAI,CAAC,YAAY,EAAE,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,YAAY,CAAC,KAAa;QACtB,IAAI,CAAC,kBAAkB,GAAG,KAAK,CAAC;QAChC,gEAAgE;QAChE,IAAI,CAAC,YAAY,EAAE,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,QAAQ,CAAI,OAA6B;QACrC,OAAO,IAAI,OAAO,CAAI,CA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,CAAC;QAE1C,IAAI,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;YACpC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,KAAc,CAAC,CAAC;QAChC,CAAC;gBAAS,CAAC;YACP,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACxB,6BAA6B;YAC7B,IAAI,CAAC,YAAY,EAAE,CAAC;QACxB,CAAC;IACL,CAAC;IAED;;OAEG;IACH,cAAc;QACV,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,cAAc;QACV,OAAO,IAAI,CAAC,gBAAgB,CAAC;IACjC,CAAC;CACJ"}
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "observed-js",
3
+ "version": "0.1.0",
4
+ "description": "enterprise ready, durable wide event scheduling and action observation library",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "scripts": {
19
+ "test": "tsx ./src/tests/actions.test.ts && tsx ./src/tests/scheduler.test.ts && tsx ./src/tests/wide-events.test.ts && tsx ./src/tests/retry.test.ts",
20
+ "test:actions": "tsx ./src/tests/actions.test.ts",
21
+ "test:scheduler": "tsx ./src/tests/scheduler.test.ts",
22
+ "test:wide-events": "tsx ./src/tests/wide-events.test.ts",
23
+ "test:retry": "tsx ./src/tests/retry.test.ts",
24
+ "dev": "tsc --watch",
25
+ "build": "tsc",
26
+ "prepublishOnly": "npm run build && npm test"
27
+ },
28
+ "keywords": [
29
+ "observability",
30
+ "concurrency",
31
+ "rate-limiting",
32
+ "retry",
33
+ "async",
34
+ "scheduling",
35
+ "wide-events",
36
+ "telemetry",
37
+ "monitoring",
38
+ "typescript"
39
+ ],
40
+ "author": "Alexander Mason <aemason93@gmail.com>",
41
+ "license": "MIT",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/AlexMason/observed-js.git"
45
+ },
46
+ "bugs": {
47
+ "url": "https://github.com/AlexMason/observed-js/issues"
48
+ },
49
+ "homepage": "https://github.com/AlexMason/observed-js#readme",
50
+ "devDependencies": {
51
+ "@types/node": "^25.0.8",
52
+ "tsx": "^4.21.0",
53
+ "typescript": "^5.9.3"
54
+ },
55
+ "type": "module",
56
+ "engines": {
57
+ "node": ">=18.0.0"
58
+ }
59
+ }