dolphin-server-modules 1.1.1 → 1.3.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/DOLPHIN_MASTER_GUIDE_NEPALI.md +74 -1
- package/README.md +11 -2
- package/TUTORIAL_NEPALI.md +21 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/realtime/codec.d.ts +18 -0
- package/dist/realtime/codec.js +65 -0
- package/dist/realtime/codec.js.map +1 -0
- package/dist/realtime/core.d.ts +63 -0
- package/dist/realtime/core.js +200 -0
- package/dist/realtime/core.js.map +1 -0
- package/dist/realtime/index.d.ts +4 -0
- package/dist/realtime/index.js +21 -0
- package/dist/realtime/index.js.map +1 -0
- package/dist/realtime/plugins.d.ts +34 -0
- package/dist/realtime/plugins.js +47 -0
- package/dist/realtime/plugins.js.map +1 -0
- package/dist/realtime/trie.d.ts +22 -0
- package/dist/realtime/trie.js +76 -0
- package/dist/realtime/trie.js.map +1 -0
- package/dist/router/router.d.ts +15 -2
- package/dist/router/router.js +27 -4
- package/dist/router/router.js.map +1 -1
- package/dist/server/server.d.ts +9 -7
- package/dist/server/server.js +8 -1
- package/dist/server/server.js.map +1 -1
- package/package.json +11 -3
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
- [११. स्केलिङ र पर्फर्मेन्स (Scaling & Performance)](#११-स्केलिङ-र-पर्फर्मेन्स-scaling--performance)
|
|
20
20
|
- [१२. टेस्टिङ र डेभप्स (Testing & DevOps)](#१२-टेस्टिङ-र-डेभप्स-testing--devops)
|
|
21
21
|
- [१३. भविष्य र योगदान (Future Roadmap)](#१३-भविष्य-र-योगदान-future-roadmap)
|
|
22
|
+
- [१४. रियलटाइम र IoT मास्टरक्लास (Realtime & IoT Masterclass) [NEW]](#१४-रियलटाइम-र-iot-मास्टरक्लास-realtime--iot-masterclass-new)
|
|
22
23
|
|
|
23
24
|
---
|
|
24
25
|
|
|
@@ -508,7 +509,8 @@ CMD ["npm", "start"]
|
|
|
508
509
|
|
|
509
510
|
Dolphin अझै विकसित हुँदैछ। हाम्रो आगामी योजनाहरू:
|
|
510
511
|
- **Dolphin CLI**: एउटा कमान्डले प्रोजेक्ट सेटअप गर्ने।
|
|
511
|
-
- **
|
|
512
|
+
- **Dolphin CLI**: एउटा कमान्डले प्रोजेक्ट सेटअप गर्ने।
|
|
513
|
+
- **Realtime & IoT Integration**: उच्च क्षमताको डाटा इन्जेसनको लागि। [DONE]
|
|
512
514
|
- **Native SQL Adapters**: PostgreSQL र MySQL का लागि विशेष एडाप्टरहरू।
|
|
513
515
|
|
|
514
516
|
### योगदान कसरी गर्ने?
|
|
@@ -516,6 +518,77 @@ Dolphin अझै विकसित हुँदैछ। हाम्रो
|
|
|
516
518
|
|
|
517
519
|
---
|
|
518
520
|
|
|
521
|
+
## १४. रियलटाइम र IoT मास्टरक्लास (Realtime & IoT Masterclass) [NEW]
|
|
522
|
+
|
|
523
|
+
आधुनिक एप्लिकेसनहरूलाई केवल "Request-Response" मात्र पुग्दैन। तिनीहरूलाई "Real-time" डाटा चाहिन्छ। Dolphin को Realtime मोड्युलले यसलाई सम्भव बनाउँछ।
|
|
524
|
+
|
|
525
|
+
### १४.१ रियलटाइम कोर (Realtime Core) के हो?
|
|
526
|
+
यो एउटा इन्टरनल "Event Bus" हो जसले मेसेजहरूलाई एक ठाउँबाट अर्को ठाउँमा तुरुन्तै पुर्याउँछ। यसले MQTT जस्तो "Topic-based" सिस्टम प्रयोग गर्छ।
|
|
527
|
+
|
|
528
|
+
### १४.२ मुख्य अवधारणाहरू (Key Concepts)
|
|
529
|
+
१. **TopicTrie**: मेसेजहरू कुन टपिकमा जाने भनेर छिटो पत्ता लगाउने इन्जिन।
|
|
530
|
+
२. **Binary Codec**: डाटालाई सानो बनाउन बाइनरी फर्म्याटमा लैजाने सफ्टवेयर।
|
|
531
|
+
३. **Plugins**: विभिन्न प्रोटोकलहरू (HL7, Modbus) सपोर्ट गर्न।
|
|
532
|
+
|
|
533
|
+
### १४.३ कोड उदाहरण: बेसिक पब-सब (Pub/Sub)
|
|
534
|
+
```typescript
|
|
535
|
+
import { RealtimeCore } from 'dolphin-server-modules/realtime';
|
|
536
|
+
|
|
537
|
+
const rt = new RealtimeCore({
|
|
538
|
+
maxMessageSize: 512 * 1024 // ५१२ KB म्याक्स साइज
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
// १. सब्सक्राइब (Subscribe) गर्ने
|
|
542
|
+
rt.subscribe('sensors/temperature/+', (ctx) => {
|
|
543
|
+
console.log(`नयाँ डाटा आयो: ${ctx.payload.value}°C`);
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
// २. पब्लिस (Publish) गर्ने
|
|
547
|
+
rt.publish('sensors/temperature/room1', { value: 22.5 });
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
### १४.४ वाइल्डकार्डको शक्ति (Power of Wildcards)
|
|
551
|
+
- `sensors/+`: `sensors/temp` र `sensors/hum` दुवै म्याच गर्छ।
|
|
552
|
+
- `sensors/#`: `sensors/a/b/c` जति पनि गहिराइ (depth) सम्म म्याच गर्छ।
|
|
553
|
+
|
|
554
|
+
### १४.५ रेडिस स्केलिङ (Redis Scaling)
|
|
555
|
+
यदि तपाईँको धेरै वटा सर्भरहरू छन् भने, तिनीहरूलाई रेडिस मार्फत जोड्न सक्नुहुन्छ:
|
|
556
|
+
```typescript
|
|
557
|
+
const rt = new RealtimeCore({
|
|
558
|
+
redisUrl: 'redis://localhost:6379'
|
|
559
|
+
});
|
|
560
|
+
```
|
|
561
|
+
यसो गर्दा एउटा सर्भरबाट पठाएको मेसेज अर्को सर्भरमा बस्ने युजरले पनि तुरुन्तै पाउँछ।
|
|
562
|
+
|
|
563
|
+
---
|
|
564
|
+
|
|
565
|
+
## १५. इन्डिपेन्डेन्ट राउटिङ मास्टरक्लास (Independent Routing) [NEW]
|
|
566
|
+
|
|
567
|
+
तपाईँको प्रोजेक्ट ठूलो हुँदै जाँदा एउटै फाइलमा सबै राउट्हरू राख्नु झन्झटिलो हुन्छ। त्यसैले Dolphin ले एक्सप्रेस जस्तै `Router` सपोर्ट गर्छ।
|
|
568
|
+
|
|
569
|
+
### १५.१ किन प्रयोग गर्ने?
|
|
570
|
+
- **Modular Code**: राउट्हरूलाई काम अनुसार (जस्तै: Users, Products, Auth) अलग फाइलमा राख्न।
|
|
571
|
+
- **Prefixing**: एउटा पूरा राउट सेटलाई एउटा 'Prefix' (जस्तै: `/api/v1`) भित्र राख्न।
|
|
572
|
+
- **Clean index.ts**: मुख्य फाइललाई सफा र छोटो राख्न।
|
|
573
|
+
|
|
574
|
+
### १५.२ कोड उदाहरण
|
|
575
|
+
```typescript
|
|
576
|
+
// userRouters.ts
|
|
577
|
+
import { createDolphinRouter } from 'dolphin-server-modules/router';
|
|
578
|
+
export const userRouter = createDolphinRouter();
|
|
579
|
+
|
|
580
|
+
userRouter.get('/profile', (ctx) => ctx.json({ name: "Ram" }));
|
|
581
|
+
|
|
582
|
+
// main.ts
|
|
583
|
+
import { createDolphinServer } from 'dolphin-server-modules/server';
|
|
584
|
+
import { userRouter } from './userRouters';
|
|
585
|
+
|
|
586
|
+
const app = createDolphinServer();
|
|
587
|
+
app.use('/users', userRouter); // अब यो /users/profile मा चल्छ
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
---
|
|
591
|
+
|
|
519
592
|
## निष्कर्ष (Conclusion)
|
|
520
593
|
|
|
521
594
|
बधाई छ! तपाईँले Dolphin Framework को **Master Guide** को अन्त्य सम्म पढ्नुभयो। अब तपाईँ कुनै पनि जटिल ब्याकइन्ड सिस्टम Dolphin प्रयोग गरेर बनाउन पूर्ण सक्षम हुनुहुन्छ।
|
package/README.md
CHANGED
|
@@ -115,12 +115,21 @@ Seamlessly switch between databases with the Adapter pattern.
|
|
|
115
115
|
### ✅ 5. Zod-Powered Validation (`/middleware/zod`)
|
|
116
116
|
Validate payloads and params with 100% type inference.
|
|
117
117
|
|
|
118
|
+
### 🌐 6. Realtime & IoT Core (`/realtime`)
|
|
119
|
+
High-performance pub/sub with MQTT-style matching.
|
|
120
|
+
|
|
121
|
+
### 🛣️ 7. Independent Routing (`/router`) [NEW]
|
|
122
|
+
Express-style standalone routers for clean organization.
|
|
123
|
+
- `app.use('/prefix', subRouter)`: Mount sub-modules with ease.
|
|
124
|
+
- **Nested Routing**: Unlimited nesting with prefix inheritance.
|
|
125
|
+
- **Unified Matcher**: Optimized matching for both static and dynamic routes.
|
|
126
|
+
|
|
118
127
|
---
|
|
119
128
|
|
|
120
129
|
## 🗺️ Roadmap & Future Vision
|
|
121
130
|
1. **`defineModel` Engine**: Define a schema once, auto-generate CRUD, validation, and types.
|
|
122
|
-
2. **Plugin System**: A robust "hook" based system
|
|
123
|
-
3. **
|
|
131
|
+
2. **Plugin System**: A robust "hook" based system. [DONE]
|
|
132
|
+
3. **Independent Routing**: Standalone sub-routers for large apps. [DONE]
|
|
124
133
|
4. **CLI Presets**: `npx dolphin init` for instant project scaffolding.
|
|
125
134
|
|
|
126
135
|
---
|
package/TUTORIAL_NEPALI.md
CHANGED
|
@@ -180,7 +180,27 @@ app.post('/register', validate(registerSchema), (ctx) => {
|
|
|
180
180
|
|
|
181
181
|
---
|
|
182
182
|
|
|
183
|
-
## ११.
|
|
183
|
+
## ११. रियलटाइम र IoT (Realtime & IoT Core) [NEW]
|
|
184
|
+
Dolphin ले अब उच्च क्षमताको रियलटाइम कम्युनिकेसन सपोर्ट गर्छ।
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { RealtimeCore, JSONPlugin } from 'dolphin-server-modules/realtime';
|
|
188
|
+
|
|
189
|
+
const rt = new RealtimeCore();
|
|
190
|
+
rt.use(JSONPlugin);
|
|
191
|
+
|
|
192
|
+
// टपिकहरूमा सब्सक्राइब (Subscribe) गर्नुहोस्
|
|
193
|
+
rt.subscribe('sensors/+', (ctx) => {
|
|
194
|
+
console.log(`टपिक: ${ctx.topic}, डाटा:`, ctx.payload);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// पब्लिस (Publish) गर्नुहोस्
|
|
198
|
+
rt.publish('sensors/temp', { value: 24.5 });
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## १२. अन्तिममा (Conclusion)
|
|
184
204
|
|
|
185
205
|
Dolphin Framework निकै छिटो र सजिलो छ। यसले तपाईँको ब्याकइन्ड डेभलपमेन्टको अनुभवलाई नयाँ उचाइमा पुर्याउँछ।
|
|
186
206
|
|
package/dist/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export * from './controller/controller';
|
|
|
3
3
|
export { createCRUD, BaseDocument, QueryFilter, PaginationOptions, DatabaseAdapter as CrudDatabaseAdapter } from './curd/crud';
|
|
4
4
|
export * from './server/server';
|
|
5
5
|
export * from './router/router';
|
|
6
|
+
export * from './realtime/index';
|
package/dist/index.js
CHANGED
|
@@ -25,4 +25,6 @@ Object.defineProperty(exports, "createCRUD", { enumerable: true, get: function (
|
|
|
25
25
|
// Re-export Server & Router
|
|
26
26
|
__exportStar(require("./server/server"), exports);
|
|
27
27
|
__exportStar(require("./router/router"), exports);
|
|
28
|
+
// Re-export Realtime
|
|
29
|
+
__exportStar(require("./realtime/index"), exports);
|
|
28
30
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iBAAiB;AACjB,8CAA4B;AAC5B,uBAAuB;AACvB,0DAAwC;AACxC,8DAA8D;AAC9D,oCAMqB;AALnB,kGAAA,UAAU,OAAA;AAMZ,4BAA4B;AAC5B,kDAAgC;AAChC,kDAAgC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iBAAiB;AACjB,8CAA4B;AAC5B,uBAAuB;AACvB,0DAAwC;AACxC,8DAA8D;AAC9D,oCAMqB;AALnB,kGAAA,UAAU,OAAA;AAMZ,4BAA4B;AAC5B,kDAAgC;AAChC,kDAAgC;AAEhC,qBAAqB;AACrB,mDAAiC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight binary/JSON codec for Dolphin Realtime.
|
|
3
|
+
* Optimized for small payloads and cross-platform compatibility.
|
|
4
|
+
*/
|
|
5
|
+
export declare function getSize(data: any): number;
|
|
6
|
+
/**
|
|
7
|
+
* Encode data to Buffer.
|
|
8
|
+
* Types:
|
|
9
|
+
* 1: Int32
|
|
10
|
+
* 2: String
|
|
11
|
+
* 3: JSON
|
|
12
|
+
* Default: Buffer as is
|
|
13
|
+
*/
|
|
14
|
+
export declare function encode(data: any): Buffer;
|
|
15
|
+
/**
|
|
16
|
+
* Decode Buffer to data.
|
|
17
|
+
*/
|
|
18
|
+
export declare function decode(buf: Buffer): any;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSize = getSize;
|
|
4
|
+
exports.encode = encode;
|
|
5
|
+
exports.decode = decode;
|
|
6
|
+
/**
|
|
7
|
+
* Lightweight binary/JSON codec for Dolphin Realtime.
|
|
8
|
+
* Optimized for small payloads and cross-platform compatibility.
|
|
9
|
+
*/
|
|
10
|
+
function getSize(data) {
|
|
11
|
+
if (Buffer.isBuffer(data))
|
|
12
|
+
return data.length;
|
|
13
|
+
if (typeof data === 'string')
|
|
14
|
+
return Buffer.byteLength(data);
|
|
15
|
+
return Buffer.byteLength(JSON.stringify(data));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Encode data to Buffer.
|
|
19
|
+
* Types:
|
|
20
|
+
* 1: Int32
|
|
21
|
+
* 2: String
|
|
22
|
+
* 3: JSON
|
|
23
|
+
* Default: Buffer as is
|
|
24
|
+
*/
|
|
25
|
+
function encode(data) {
|
|
26
|
+
if (Buffer.isBuffer(data))
|
|
27
|
+
return data;
|
|
28
|
+
if (typeof data === 'number') {
|
|
29
|
+
const b = Buffer.allocUnsafe(5);
|
|
30
|
+
b[0] = 1;
|
|
31
|
+
b.writeInt32BE(data, 1);
|
|
32
|
+
return b;
|
|
33
|
+
}
|
|
34
|
+
if (typeof data === 'string') {
|
|
35
|
+
const str = Buffer.from(data);
|
|
36
|
+
const len = str.length;
|
|
37
|
+
// Simple header: type(1 byte) + len(2 bytes)
|
|
38
|
+
const b = Buffer.allocUnsafe(3);
|
|
39
|
+
b[0] = 2;
|
|
40
|
+
b.writeUInt16BE(len, 1);
|
|
41
|
+
return Buffer.concat([b, str]);
|
|
42
|
+
}
|
|
43
|
+
const json = Buffer.from(JSON.stringify(data));
|
|
44
|
+
const b = Buffer.allocUnsafe(1);
|
|
45
|
+
b[0] = 3;
|
|
46
|
+
return Buffer.concat([b, json]);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Decode Buffer to data.
|
|
50
|
+
*/
|
|
51
|
+
function decode(buf) {
|
|
52
|
+
if (!buf || buf.length === 0)
|
|
53
|
+
return null;
|
|
54
|
+
const t = buf[0];
|
|
55
|
+
if (t === 1)
|
|
56
|
+
return buf.readInt32BE(1);
|
|
57
|
+
if (t === 2) {
|
|
58
|
+
const len = buf.readUInt16BE(1);
|
|
59
|
+
return buf.slice(3, 3 + len).toString();
|
|
60
|
+
}
|
|
61
|
+
if (t === 3)
|
|
62
|
+
return JSON.parse(buf.slice(1).toString());
|
|
63
|
+
return buf;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../realtime/codec.ts"],"names":[],"mappings":";;AAIA,0BAIC;AAUD,wBAwBC;AAKD,wBAUC;AAzDD;;;GAGG;AACH,SAAgB,OAAO,CAAC,IAAS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AAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACT,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,SAAgB,MAAM,CAAC,GAAW;IAChC,IAAI,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAC1C,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IACjB,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IACvC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACZ,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;QAChC,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;IAC1C,CAAC;IACD,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;IACxD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { RealtimePlugin } from './plugins';
|
|
3
|
+
/**
|
|
4
|
+
* RealtimeCore - High performance unified pub/sub bus for Dolphin.
|
|
5
|
+
* Supports:
|
|
6
|
+
* - Local event emitter (in-process)
|
|
7
|
+
* - Distributed bus via Redis (optional)
|
|
8
|
+
* - Retained messages with TTL
|
|
9
|
+
* - Device/Client tracking
|
|
10
|
+
* - Plugin-based protocol handling
|
|
11
|
+
*/
|
|
12
|
+
export declare class RealtimeCore extends EventEmitter {
|
|
13
|
+
private config;
|
|
14
|
+
private trie;
|
|
15
|
+
private retained;
|
|
16
|
+
private devices;
|
|
17
|
+
private plugins;
|
|
18
|
+
private pending;
|
|
19
|
+
private msgId;
|
|
20
|
+
private redisPub?;
|
|
21
|
+
private redisSub?;
|
|
22
|
+
constructor(config?: {
|
|
23
|
+
maxMessageSize?: number;
|
|
24
|
+
redisUrl?: string;
|
|
25
|
+
acl?: {
|
|
26
|
+
canSubscribe: (deviceId: string, topic: string) => boolean;
|
|
27
|
+
canPublish: (deviceId: string, topic: string) => boolean;
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* Initialize Redis for distributed pub/sub.
|
|
32
|
+
*/
|
|
33
|
+
private initRedis;
|
|
34
|
+
/**
|
|
35
|
+
* Subscribe to a topic pattern.
|
|
36
|
+
*/
|
|
37
|
+
subscribe(topic: string, fn: (data: any) => void, deviceId?: string): void;
|
|
38
|
+
/**
|
|
39
|
+
* Publish a message to a topic.
|
|
40
|
+
*/
|
|
41
|
+
publish(topic: string, payload: any, opts?: {
|
|
42
|
+
retain?: boolean;
|
|
43
|
+
ttl?: number;
|
|
44
|
+
}, deviceId?: string): void;
|
|
45
|
+
private publishInternal;
|
|
46
|
+
/**
|
|
47
|
+
* Handle raw data from a socket.
|
|
48
|
+
*/
|
|
49
|
+
handle(raw: Buffer, socket?: any, deviceId?: string): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Register a plugin.
|
|
52
|
+
*/
|
|
53
|
+
use(plugin: RealtimePlugin): void;
|
|
54
|
+
/**
|
|
55
|
+
* Register a device/client.
|
|
56
|
+
*/
|
|
57
|
+
register(deviceId: string, socket?: any): void;
|
|
58
|
+
/**
|
|
59
|
+
* Heartbeat for a device.
|
|
60
|
+
*/
|
|
61
|
+
touch(deviceId: string): void;
|
|
62
|
+
private startCleanup;
|
|
63
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.RealtimeCore = void 0;
|
|
37
|
+
const events_1 = require("events");
|
|
38
|
+
const trie_1 = require("./trie");
|
|
39
|
+
const codec_1 = require("./codec");
|
|
40
|
+
/**
|
|
41
|
+
* RealtimeCore - High performance unified pub/sub bus for Dolphin.
|
|
42
|
+
* Supports:
|
|
43
|
+
* - Local event emitter (in-process)
|
|
44
|
+
* - Distributed bus via Redis (optional)
|
|
45
|
+
* - Retained messages with TTL
|
|
46
|
+
* - Device/Client tracking
|
|
47
|
+
* - Plugin-based protocol handling
|
|
48
|
+
*/
|
|
49
|
+
class RealtimeCore extends events_1.EventEmitter {
|
|
50
|
+
config;
|
|
51
|
+
trie = new trie_1.TopicTrie();
|
|
52
|
+
retained = new Map();
|
|
53
|
+
devices = new Map();
|
|
54
|
+
plugins = new Map();
|
|
55
|
+
pending = new Map();
|
|
56
|
+
msgId = 0;
|
|
57
|
+
redisPub;
|
|
58
|
+
redisSub;
|
|
59
|
+
constructor(config = {}) {
|
|
60
|
+
super();
|
|
61
|
+
this.config = config;
|
|
62
|
+
if (config.redisUrl) {
|
|
63
|
+
this.initRedis(config.redisUrl);
|
|
64
|
+
}
|
|
65
|
+
this.startCleanup();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Initialize Redis for distributed pub/sub.
|
|
69
|
+
*/
|
|
70
|
+
async initRedis(url) {
|
|
71
|
+
try {
|
|
72
|
+
// @ts-ignore
|
|
73
|
+
const Redis = (await Promise.resolve().then(() => __importStar(require('ioredis')))).default;
|
|
74
|
+
this.redisPub = new Redis(url);
|
|
75
|
+
this.redisSub = new Redis(url);
|
|
76
|
+
this.redisSub.subscribe('dolphin-rt');
|
|
77
|
+
this.redisSub.on('message', (_, msg) => {
|
|
78
|
+
const { topic, payload } = JSON.parse(msg);
|
|
79
|
+
this.publishInternal(topic, payload, { skipRedis: true });
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
console.warn('Redis initialization failed (ioredis not found or connection error):', err);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Subscribe to a topic pattern.
|
|
88
|
+
*/
|
|
89
|
+
subscribe(topic, fn, deviceId) {
|
|
90
|
+
if (deviceId && this.config.acl && !this.config.acl.canSubscribe(deviceId, topic)) {
|
|
91
|
+
throw new Error('ACL deny');
|
|
92
|
+
}
|
|
93
|
+
this.trie.add(topic, fn);
|
|
94
|
+
// Replay retained messages
|
|
95
|
+
for (const [t, data] of this.retained.entries()) {
|
|
96
|
+
// Small hack: if we match the new subscription, replay
|
|
97
|
+
// We can improve this by matching the pattern against the topic
|
|
98
|
+
// For now, only exact match for simplicity or improve TopicTrie
|
|
99
|
+
if (t === topic)
|
|
100
|
+
fn(data.payload);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Publish a message to a topic.
|
|
105
|
+
*/
|
|
106
|
+
publish(topic, payload, opts = {}, deviceId) {
|
|
107
|
+
if ((0, codec_1.getSize)(payload) > (this.config.maxMessageSize || 256 * 1024)) {
|
|
108
|
+
throw new Error('Payload too large');
|
|
109
|
+
}
|
|
110
|
+
if (deviceId && this.config.acl && !this.config.acl.canPublish(deviceId, topic)) {
|
|
111
|
+
throw new Error('ACL deny');
|
|
112
|
+
}
|
|
113
|
+
this.publishInternal(topic, payload, opts);
|
|
114
|
+
}
|
|
115
|
+
publishInternal(topic, payload, opts = {}) {
|
|
116
|
+
if (opts.retain) {
|
|
117
|
+
this.retained.set(topic, { payload, ts: Date.now(), ttl: opts.ttl || 0 });
|
|
118
|
+
}
|
|
119
|
+
// Match and emit locally
|
|
120
|
+
this.trie.match(topic, (fn) => fn({ topic, payload }));
|
|
121
|
+
this.emit('message', { topic, payload });
|
|
122
|
+
// Publish to Redis if available
|
|
123
|
+
if (this.redisPub && !opts.skipRedis) {
|
|
124
|
+
this.redisPub.publish('dolphin-rt', JSON.stringify({ topic, payload }));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Handle raw data from a socket.
|
|
129
|
+
*/
|
|
130
|
+
async handle(raw, socket, deviceId) {
|
|
131
|
+
if (raw.length > (this.config.maxMessageSize || 256 * 1024))
|
|
132
|
+
return;
|
|
133
|
+
// Create initial context
|
|
134
|
+
const ctx = {
|
|
135
|
+
type: 'raw',
|
|
136
|
+
raw,
|
|
137
|
+
socket,
|
|
138
|
+
deviceId,
|
|
139
|
+
ts: Date.now(),
|
|
140
|
+
publish: this.publish.bind(this)
|
|
141
|
+
};
|
|
142
|
+
// Plugin matching and decoding
|
|
143
|
+
for (const p of this.plugins.values()) {
|
|
144
|
+
if (p.match(ctx)) {
|
|
145
|
+
if (p.decode)
|
|
146
|
+
ctx.payload = p.decode(raw);
|
|
147
|
+
p.onMessage?.(ctx);
|
|
148
|
+
// If plugin handled it, we might want to stop or continue.
|
|
149
|
+
// For now, let's continue to allow multiple plugins or default handling.
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// Default handling for 'pub' style packets if no plugin decoded it or just generic
|
|
153
|
+
try {
|
|
154
|
+
const decoded = (0, codec_1.decode)(raw);
|
|
155
|
+
if (decoded && typeof decoded === 'object' && decoded.type === 'pub') {
|
|
156
|
+
this.publish(decoded.topic, decoded.payload, {}, deviceId);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
// Ignore decode errors for raw data
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Register a plugin.
|
|
165
|
+
*/
|
|
166
|
+
use(plugin) {
|
|
167
|
+
this.plugins.set(plugin.name, plugin);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Register a device/client.
|
|
171
|
+
*/
|
|
172
|
+
register(deviceId, socket) {
|
|
173
|
+
this.devices.set(deviceId, { lastSeen: Date.now(), socket });
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Heartbeat for a device.
|
|
177
|
+
*/
|
|
178
|
+
touch(deviceId) {
|
|
179
|
+
const d = this.devices.get(deviceId);
|
|
180
|
+
if (d)
|
|
181
|
+
d.lastSeen = Date.now();
|
|
182
|
+
}
|
|
183
|
+
startCleanup() {
|
|
184
|
+
setInterval(() => {
|
|
185
|
+
const now = Date.now();
|
|
186
|
+
// Clean retained messages
|
|
187
|
+
for (const [k, v] of this.retained) {
|
|
188
|
+
if (v.ttl && now - v.ts > v.ttl)
|
|
189
|
+
this.retained.delete(k);
|
|
190
|
+
}
|
|
191
|
+
// Clean inactive devices (1 minute timeout)
|
|
192
|
+
for (const [id, d] of this.devices) {
|
|
193
|
+
if (now - d.lastSeen > 60000)
|
|
194
|
+
this.devices.delete(id);
|
|
195
|
+
}
|
|
196
|
+
}, 5000);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
exports.RealtimeCore = RealtimeCore;
|
|
200
|
+
//# sourceMappingURL=core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../../realtime/core.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAAsC;AACtC,iCAAmC;AACnC,mCAAkD;AAGlD;;;;;;;;GAQG;AACH,MAAa,YAAa,SAAQ,qBAAY;IAWxB;IAVZ,IAAI,GAAG,IAAI,gBAAS,EAAE,CAAC;IACvB,QAAQ,GAAG,IAAI,GAAG,EAAqD,CAAC;IACxE,OAAO,GAAG,IAAI,GAAG,EAA8C,CAAC;IAChE,OAAO,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC5C,OAAO,GAAG,IAAI,GAAG,EAAe,CAAC;IACjC,KAAK,GAAG,CAAC,CAAC;IAEV,QAAQ,CAAO;IACf,QAAQ,CAAO;IAEvB,YAAoB,SAOhB,EAAE;QACJ,KAAK,EAAE,CAAC;QARU,WAAM,GAAN,MAAM,CAOpB;QAGJ,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;YACpB,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAClC,CAAC;QAED,IAAI,CAAC,YAAY,EAAE,CAAC;IACtB,CAAC;IAED;;OAEG;IACK,KAAK,CAAC,SAAS,CAAC,GAAW;QACjC,IAAI,C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C;YACxD,CAAC;QACH,CAAC,EAAE,IAAI,CAAC,CAAC;IACX,CAAC;CACF;AA3KD,oCA2KC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./core"), exports);
|
|
18
|
+
__exportStar(require("./trie"), exports);
|
|
19
|
+
__exportStar(require("./codec"), exports);
|
|
20
|
+
__exportStar(require("./plugins"), exports);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../realtime/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,yCAAuB;AACvB,0CAAwB;AACxB,4CAA0B"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realtime Plugins for Dolphin.
|
|
3
|
+
* Allows handling custom protocols (e.g. Modbus, HL7) seamlessly.
|
|
4
|
+
*/
|
|
5
|
+
export type RealtimeContext = {
|
|
6
|
+
id?: number;
|
|
7
|
+
type: string;
|
|
8
|
+
topic?: string;
|
|
9
|
+
payload?: any;
|
|
10
|
+
raw?: Buffer;
|
|
11
|
+
socket?: any;
|
|
12
|
+
deviceId?: string;
|
|
13
|
+
ts: number;
|
|
14
|
+
publish: (topic: string, payload: any, opts?: any) => void;
|
|
15
|
+
};
|
|
16
|
+
export type RealtimePlugin = {
|
|
17
|
+
name: string;
|
|
18
|
+
match: (ctx: RealtimeContext) => boolean;
|
|
19
|
+
decode?: (buf: Buffer) => any;
|
|
20
|
+
encode?: (data: any) => Buffer;
|
|
21
|
+
onMessage?: (ctx: RealtimeContext) => void;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Sample HL7 Plugin
|
|
25
|
+
*/
|
|
26
|
+
export declare const HL7Plugin: RealtimePlugin;
|
|
27
|
+
/**
|
|
28
|
+
* Sample Modbus Plugin
|
|
29
|
+
*/
|
|
30
|
+
export declare const ModbusPlugin: RealtimePlugin;
|
|
31
|
+
/**
|
|
32
|
+
* Standard JSON Plugin for Web
|
|
33
|
+
*/
|
|
34
|
+
export declare const JSONPlugin: RealtimePlugin;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Realtime Plugins for Dolphin.
|
|
4
|
+
* Allows handling custom protocols (e.g. Modbus, HL7) seamlessly.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.JSONPlugin = exports.ModbusPlugin = exports.HL7Plugin = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* Sample HL7 Plugin
|
|
10
|
+
*/
|
|
11
|
+
exports.HL7Plugin = {
|
|
12
|
+
name: 'hl7',
|
|
13
|
+
match: (ctx) => ctx.raw?.includes?.(0x0b) ?? false,
|
|
14
|
+
decode: (buf) => ({ msg: buf.toString().split('\r') })
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Sample Modbus Plugin
|
|
18
|
+
*/
|
|
19
|
+
exports.ModbusPlugin = {
|
|
20
|
+
name: 'modbus',
|
|
21
|
+
match: (ctx) => ctx.raw?.length === 8,
|
|
22
|
+
decode: (buf) => ({
|
|
23
|
+
addr: buf[0],
|
|
24
|
+
func: buf[1],
|
|
25
|
+
value: buf.readUInt16BE(2)
|
|
26
|
+
})
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Standard JSON Plugin for Web
|
|
30
|
+
*/
|
|
31
|
+
exports.JSONPlugin = {
|
|
32
|
+
name: 'json',
|
|
33
|
+
match: (ctx) => {
|
|
34
|
+
try {
|
|
35
|
+
if (ctx.raw) {
|
|
36
|
+
JSON.parse(ctx.raw.toString());
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
},
|
|
45
|
+
decode: (buf) => JSON.parse(buf.toString())
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=plugins.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugins.js","sourceRoot":"","sources":["../../realtime/plugins.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAsBH;;GAEG;AACU,QAAA,SAAS,GAAmB;IACvC,IAAI,EAAE,KAAK;IACX,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK;IAClD,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;CACvD,CAAC;AAEF;;GAEG;AACU,QAAA,YAAY,GAAmB;IAC1C,IAAI,EAAE,QAAQ;IACd,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC;IACrC,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QAChB,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;QACZ,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;QACZ,KAAK,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC;KAC3B,CAAC;CACH,CAAC;AAEF;;GAEG;AACU,QAAA,UAAU,GAAmB;IACxC,IAAI,EAAE,MAAM;IACZ,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE;QACb,IAAI,CAAC;YACH,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC;gBACZ,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;gBAC/B,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;CAC5C,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TopicTrie for high-performance MQTT-style topic matching.
|
|
3
|
+
* Supports:
|
|
4
|
+
* - Simple topics: "sensors/temp"
|
|
5
|
+
* - Single level wildcard: "sensors/+" (matches "sensors/temp", "sensors/hum")
|
|
6
|
+
* - Multi level wildcard: "sensors/#" (matches "sensors/temp", "sensors/room1/temp")
|
|
7
|
+
*/
|
|
8
|
+
export declare class TopicTrie {
|
|
9
|
+
private root;
|
|
10
|
+
/**
|
|
11
|
+
* Add a subscriber function to a topic pattern.
|
|
12
|
+
*/
|
|
13
|
+
add(topic: string, fn: Function): void;
|
|
14
|
+
/**
|
|
15
|
+
* Remove a subscriber function from a topic pattern.
|
|
16
|
+
*/
|
|
17
|
+
remove(topic: string, fn: Function): void;
|
|
18
|
+
/**
|
|
19
|
+
* Match a topic and execute callback for each matching subscriber.
|
|
20
|
+
*/
|
|
21
|
+
match(topic: string, cb: (fn: Function) => void): void;
|
|
22
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TopicTrie = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* TopicTrie for high-performance MQTT-style topic matching.
|
|
6
|
+
* Supports:
|
|
7
|
+
* - Simple topics: "sensors/temp"
|
|
8
|
+
* - Single level wildcard: "sensors/+" (matches "sensors/temp", "sensors/hum")
|
|
9
|
+
* - Multi level wildcard: "sensors/#" (matches "sensors/temp", "sensors/room1/temp")
|
|
10
|
+
*/
|
|
11
|
+
class TopicTrie {
|
|
12
|
+
root = {};
|
|
13
|
+
/**
|
|
14
|
+
* Add a subscriber function to a topic pattern.
|
|
15
|
+
*/
|
|
16
|
+
add(topic, fn) {
|
|
17
|
+
const parts = topic.split('/');
|
|
18
|
+
let node = this.root;
|
|
19
|
+
for (const p of parts) {
|
|
20
|
+
if (!node[p])
|
|
21
|
+
node[p] = {};
|
|
22
|
+
node = node[p];
|
|
23
|
+
}
|
|
24
|
+
if (!node._)
|
|
25
|
+
node._ = [];
|
|
26
|
+
node._.push(fn);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Remove a subscriber function from a topic pattern.
|
|
30
|
+
*/
|
|
31
|
+
remove(topic, fn) {
|
|
32
|
+
const parts = topic.split('/');
|
|
33
|
+
let node = this.root;
|
|
34
|
+
for (const p of parts) {
|
|
35
|
+
if (!node[p])
|
|
36
|
+
return;
|
|
37
|
+
node = node[p];
|
|
38
|
+
}
|
|
39
|
+
if (node._) {
|
|
40
|
+
node._ = node._.filter((f) => f !== fn);
|
|
41
|
+
if (node._.length === 0)
|
|
42
|
+
delete node._;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Match a topic and execute callback for each matching subscriber.
|
|
47
|
+
*/
|
|
48
|
+
match(topic, cb) {
|
|
49
|
+
const parts = topic.split('/');
|
|
50
|
+
const walk = (node, i) => {
|
|
51
|
+
if (!node)
|
|
52
|
+
return;
|
|
53
|
+
// Exact match level
|
|
54
|
+
if (i === parts.length) {
|
|
55
|
+
if (node._)
|
|
56
|
+
node._.forEach(cb);
|
|
57
|
+
// Also check if '#' exists at this level (e.g. pattern 'a/#' matches 'a')
|
|
58
|
+
if (node['#'] && node['#']._)
|
|
59
|
+
node['#']._.forEach(cb);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
// 1. Direct match
|
|
63
|
+
if (node[parts[i]])
|
|
64
|
+
walk(node[parts[i]], i + 1);
|
|
65
|
+
// 2. Single level wildcard '+'
|
|
66
|
+
if (node['+'])
|
|
67
|
+
walk(node['+'], i + 1);
|
|
68
|
+
// 3. Multi level wildcard '#'
|
|
69
|
+
if (node['#'] && node['#']._)
|
|
70
|
+
node['#']._.forEach(cb);
|
|
71
|
+
};
|
|
72
|
+
walk(this.root, 0);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.TopicTrie = TopicTrie;
|
|
76
|
+
//# sourceMappingURL=trie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trie.js","sourceRoot":"","sources":["../../realtime/trie.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,MAAa,SAAS;IACZ,IAAI,GAAQ,EAAE,CAAC;IAEvB;;OAEG;IACH,GAAG,CAAC,KAAa,EAAE,EAAY;QAC7B,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC/B,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QAErB,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;YACtB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;gBAAE,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC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AAC,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;oBAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;gBACtD,OAAO;YACT,CAAC;YAED,kBAAkB;YAClB,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;YAEhD,+BAA+B;YAC/B,IAAI,IAAI,CAAC,GAAG,CAAC;gBAAE,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;YAEtC,8BAA8B;YAC9B,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;gBAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACxD,CAAC,CAAC;QAEF,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IACrB,CAAC;CACF;AAlED,8BAkEC"}
|
package/dist/router/router.d.ts
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
|
-
type Handler = (ctx: any) => Promise<void> | void;
|
|
1
|
+
export type Handler = (ctx: any) => Promise<void> | void;
|
|
2
|
+
export interface Route {
|
|
3
|
+
method: string;
|
|
4
|
+
path: string;
|
|
5
|
+
handler: Handler;
|
|
6
|
+
regex: RegExp;
|
|
7
|
+
keys: string[];
|
|
8
|
+
}
|
|
2
9
|
export declare function createDolphinRouter(): {
|
|
3
10
|
get: (path: string, handler: Handler) => void;
|
|
4
11
|
post: (path: string, handler: Handler) => void;
|
|
5
12
|
put: (path: string, handler: Handler) => void;
|
|
6
13
|
delete: (path: string, handler: Handler) => void;
|
|
7
14
|
patch: (path: string, handler: Handler) => void;
|
|
15
|
+
all: (path: string, handler: Handler) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Mount a sub-router or middleware.
|
|
18
|
+
* app.use('/auth', authRouter)
|
|
19
|
+
*/
|
|
20
|
+
use: (prefix: string, subRouter: any) => void;
|
|
21
|
+
_routes: Route[];
|
|
8
22
|
match(method: string, url: string): {
|
|
9
23
|
handler: Handler;
|
|
10
24
|
params: Record<string, string>;
|
|
11
25
|
} | null;
|
|
12
26
|
};
|
|
13
|
-
export {};
|
package/dist/router/router.js
CHANGED
|
@@ -4,9 +4,10 @@ exports.createDolphinRouter = createDolphinRouter;
|
|
|
4
4
|
function createDolphinRouter() {
|
|
5
5
|
const routes = [];
|
|
6
6
|
const addRoute = (method, path, handler) => {
|
|
7
|
-
//
|
|
7
|
+
// Normalize path to remove trailing slashes unless it's just /
|
|
8
|
+
const normalizedPath = path === '/' ? '/' : path.replace(/\/$/, '');
|
|
8
9
|
const keys = [];
|
|
9
|
-
const pattern =
|
|
10
|
+
const pattern = normalizedPath
|
|
10
11
|
.replace(/:([^\/]+)/g, (_, key) => {
|
|
11
12
|
keys.push(key);
|
|
12
13
|
return '([^\\/]+)';
|
|
@@ -14,18 +15,39 @@ function createDolphinRouter() {
|
|
|
14
15
|
.replace(/\//g, '\\/');
|
|
15
16
|
routes.push({
|
|
16
17
|
method: method.toUpperCase(),
|
|
17
|
-
path,
|
|
18
|
+
path: normalizedPath,
|
|
18
19
|
handler,
|
|
19
20
|
regex: new RegExp(`^${pattern}$`),
|
|
20
21
|
keys
|
|
21
22
|
});
|
|
22
23
|
};
|
|
23
|
-
|
|
24
|
+
const router = {
|
|
24
25
|
get: (path, handler) => addRoute('GET', path, handler),
|
|
25
26
|
post: (path, handler) => addRoute('POST', path, handler),
|
|
26
27
|
put: (path, handler) => addRoute('PUT', path, handler),
|
|
27
28
|
delete: (path, handler) => addRoute('DELETE', path, handler),
|
|
28
29
|
patch: (path, handler) => addRoute('PATCH', path, handler),
|
|
30
|
+
all: (path, handler) => addRoute('ALL', path, handler),
|
|
31
|
+
/**
|
|
32
|
+
* Mount a sub-router or middleware.
|
|
33
|
+
* app.use('/auth', authRouter)
|
|
34
|
+
*/
|
|
35
|
+
use: (prefix, subRouter) => {
|
|
36
|
+
if (typeof prefix !== 'string') {
|
|
37
|
+
// Fallback for global middleware handling if needed
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const normalizedPrefix = prefix === '/' ? '' : prefix.replace(/\/$/, '');
|
|
41
|
+
// If subRouter is another Dolphin router, merge its routes
|
|
42
|
+
if (subRouter && typeof subRouter.match === 'function' && subRouter._routes) {
|
|
43
|
+
for (const sr of subRouter._routes) {
|
|
44
|
+
const fullPath = normalizedPrefix + (sr.path === '/' ? '' : sr.path);
|
|
45
|
+
addRoute(sr.method, fullPath || '/', sr.handler);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
// Internal getter for route merging
|
|
50
|
+
_routes: routes,
|
|
29
51
|
match(method, url) {
|
|
30
52
|
const path = url.split('?')[0];
|
|
31
53
|
const m = method.toUpperCase();
|
|
@@ -44,5 +66,6 @@ function createDolphinRouter() {
|
|
|
44
66
|
return null;
|
|
45
67
|
}
|
|
46
68
|
};
|
|
69
|
+
return router;
|
|
47
70
|
}
|
|
48
71
|
//# sourceMappingURL=router.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../router/router.ts"],"names":[],"mappings":";;AAUA,
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../router/router.ts"],"names":[],"mappings":";;AAUA,kDA6EC;AA7ED,SAAgB,mBAAmB;IACjC,MAAM,MAAM,GAAY,EAAE,CAAC;IAE3B,MAAM,QAAQ,GAAG,CAAC,MAAc,EAAE,IAAY,EAAE,OAAgB,EAAE,EAAE;QAClE,+DAA+D;QAC/D,MAAM,cAAc,GAAG,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAA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M,MAAM,GAA2B,EAAE,CAAC;oBAC1C,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE;wBAC5B,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;oBAC7B,CAAC,CAAC,CAAC;oBACH,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;gBAC5C,CAAC;YACH,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;KACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/server/server.d.ts
CHANGED
|
@@ -3,16 +3,18 @@ export declare function createDolphinServer(options?: {
|
|
|
3
3
|
port?: number;
|
|
4
4
|
host?: string;
|
|
5
5
|
}): {
|
|
6
|
-
use: (
|
|
6
|
+
use: (prefixOrMw: string | any, mw?: any) => void;
|
|
7
7
|
listen: (port?: number, callback?: () => void) => void;
|
|
8
8
|
close: () => http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
9
|
-
get: (path: string, handler: (
|
|
10
|
-
post: (path: string, handler: (
|
|
11
|
-
put: (path: string, handler: (
|
|
12
|
-
delete: (path: string, handler: (
|
|
13
|
-
patch: (path: string, handler: (
|
|
9
|
+
get: (path: string, handler: import("../router/router").Handler) => void;
|
|
10
|
+
post: (path: string, handler: import("../router/router").Handler) => void;
|
|
11
|
+
put: (path: string, handler: import("../router/router").Handler) => void;
|
|
12
|
+
delete: (path: string, handler: import("../router/router").Handler) => void;
|
|
13
|
+
patch: (path: string, handler: import("../router/router").Handler) => void;
|
|
14
|
+
all: (path: string, handler: import("../router/router").Handler) => void;
|
|
15
|
+
_routes: import("../router/router").Route[];
|
|
14
16
|
match(method: string, url: string): {
|
|
15
|
-
handler: (
|
|
17
|
+
handler: import("../router/router").Handler;
|
|
16
18
|
params: Record<string, string>;
|
|
17
19
|
} | null;
|
|
18
20
|
};
|
package/dist/server/server.js
CHANGED
|
@@ -77,7 +77,14 @@ function createDolphinServer(options = {}) {
|
|
|
77
77
|
});
|
|
78
78
|
return {
|
|
79
79
|
...router, // Mixin router methods (get, post, etc.)
|
|
80
|
-
use: (mw) =>
|
|
80
|
+
use: (prefixOrMw, mw) => {
|
|
81
|
+
if (typeof prefixOrMw === 'string' && mw && typeof mw.match === 'function') {
|
|
82
|
+
router.use(prefixOrMw, mw);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
middlewares.push(prefixOrMw);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
81
88
|
listen: (port = options.port || 3000, callback) => {
|
|
82
89
|
server.listen(port, options.host || '0.0.0.0', callback);
|
|
83
90
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../server/server.ts"],"names":[],"mappings":";;;;;AAGA,
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../server/server.ts"],"names":[],"mappings":";;;;;AAGA,kDAoFC;AAvFD,0DAA6B;AAC7B,6CAAuD;AAEvD,SAAgB,mBAAmB,CAAC,UAA4C,EAAE;IAChF,MAAM,MAAM,GAAG,IAAA,4BAAmB,GAAE,CAAC;IACrC,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,MAAM,GAAG,mBAAI,CAAC,YAAY,CAAC,KAAK,EAAE,GAAQ,EAAE,GAAQ,EAAE,EAAE;QAC5D,6BAA6B;QAC7B,GAAG,CAAC,IAAI,GAAG,CAAC,IAAS,EAAE,MAAM,GAAG,GAAG,EAAE,EAAE;YACrC,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,CAAC,CAAC;YAC9D,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;QAChC,CAAC,CAAC;QAEF,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,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AAK,QAAQ,IAAI,EAAE,IAAI,OAAO,EAAE,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;gBAC3E,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;YAC7B,CAAC;iBAAM,CAAC;gBACN,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC;QACD,MAAM,EAAE,CAAC,OAAe,OAAO,CAAC,IAAI,IAAI,IAAI,EAAE,QAAqB,EAAE,EAAE;YACrE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,SAAS,EAAE,QAAQ,CAAC,CAAC;QAC3D,CAAC;QACD,KAAK,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE;KAC5B,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dolphin-server-modules",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"homepage": "https://github.com/Phuyalshankar/dolphin-server-modules#readme",
|
|
5
5
|
"description": "Core utility modules for Auth, CRUD, and Controllers",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -21,7 +21,12 @@
|
|
|
21
21
|
"./middleware/zod": "./dist/middleware/zod.js",
|
|
22
22
|
"./adapters/mongoose": "./dist/adapters/mongoose/index.js",
|
|
23
23
|
"./server": "./dist/server/server.js",
|
|
24
|
-
"./router": "./dist/router/router.js"
|
|
24
|
+
"./router": "./dist/router/router.js",
|
|
25
|
+
"./realtime": "./dist/realtime/index.js",
|
|
26
|
+
"./realtime/core": "./dist/realtime/core.js",
|
|
27
|
+
"./realtime/trie": "./dist/realtime/trie.js",
|
|
28
|
+
"./realtime/codec": "./dist/realtime/codec.js",
|
|
29
|
+
"./realtime/plugins": "./dist/realtime/plugins.js"
|
|
25
30
|
},
|
|
26
31
|
"scripts": {
|
|
27
32
|
"build": "tsc",
|
|
@@ -33,12 +38,15 @@
|
|
|
33
38
|
"server",
|
|
34
39
|
"auth",
|
|
35
40
|
"crud",
|
|
36
|
-
"controller"
|
|
41
|
+
"controller",
|
|
42
|
+
"realtime",
|
|
43
|
+
"iot"
|
|
37
44
|
],
|
|
38
45
|
"author": "",
|
|
39
46
|
"license": "ISC",
|
|
40
47
|
"dependencies": {
|
|
41
48
|
"argon2": "^0.40.1",
|
|
49
|
+
"ioredis": "^15.15.5",
|
|
42
50
|
"zod": "^4.3.6"
|
|
43
51
|
},
|
|
44
52
|
"devDependencies": {
|