@mono-labs/dev 0.1.266 → 0.1.267
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
|
-
import
|
|
2
|
+
import { attachSocketAdapter } from '../websocket';
|
|
3
3
|
import type { SocketAdapterConfig } from '../websocket/types';
|
|
4
4
|
import type { ApiGatewayHandler, ALBHandler, LambdaOptionsApiGateway, LambdaOptionsALB, LocalServerConfig } from './types';
|
|
5
5
|
export type { ApiGatewayHandler, ALBHandler, LocalServerConfig } from './types';
|
|
@@ -11,7 +11,7 @@ export declare class LocalServer {
|
|
|
11
11
|
lambda(path: string, handler: ApiGatewayHandler): this;
|
|
12
12
|
lambda(path: string, handler: ApiGatewayHandler, options: LambdaOptionsApiGateway): this;
|
|
13
13
|
lambda(path: string, handler: ALBHandler, options: LambdaOptionsALB): this;
|
|
14
|
-
attachSocket(
|
|
14
|
+
attachSocket(config?: SocketAdapterConfig): ReturnType<typeof attachSocketAdapter>;
|
|
15
15
|
listen(port: number, hostname?: string): void;
|
|
16
16
|
}
|
|
17
17
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/local-server/index.ts"],"names":[],"mappings":"AAEA,OAAO,OAAO,MAAM,SAAS,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/local-server/index.ts"],"names":[],"mappings":"AAEA,OAAO,OAAO,MAAM,SAAS,CAAC;AAa9B,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EACX,iBAAiB,EACjB,UAAU,EACV,uBAAuB,EACvB,gBAAgB,EAChB,iBAAiB,EACjB,MAAM,SAAS,CAAC;AAEjB,YAAY,EAAE,iBAAiB,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEhF,qBAAa,WAAW;IACvB,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC;IAC9B,OAAO,CAAC,UAAU,CAAc;IAChC,OAAO,CAAC,MAAM,CAAoB;gBAEtB,MAAM,CAAC,EAAE,iBAAiB;IAgBtC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,iBAAiB,GAAG,IAAI;IACtD,MAAM,CACL,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,iBAAiB,EAC1B,OAAO,EAAE,uBAAuB,GAC9B,IAAI;IACP,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,gBAAgB,GAAG,IAAI;IAmC1E,YAAY,CAEX,MAAM,CAAC,EAAE,mBAAmB,GAC1B,UAAU,CAAC,OAAO,mBAAmB,CAAC;IAUzC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;CAS7C"}
|
|
@@ -10,6 +10,7 @@ const cors_1 = __importDefault(require("cors"));
|
|
|
10
10
|
const ws_1 = require("ws");
|
|
11
11
|
const aws_event_synthesis_1 = require("../aws-event-synthesis");
|
|
12
12
|
const event_synthesizer_1 = require("./event-synthesizer");
|
|
13
|
+
const websocket_1 = require("../websocket");
|
|
13
14
|
class LocalServer {
|
|
14
15
|
app;
|
|
15
16
|
httpServer;
|
|
@@ -49,14 +50,16 @@ class LocalServer {
|
|
|
49
50
|
});
|
|
50
51
|
return this;
|
|
51
52
|
}
|
|
52
|
-
attachSocket(
|
|
53
|
+
attachSocket(
|
|
54
|
+
//adapterFn: typeof attachSocketAdapter,
|
|
55
|
+
config) {
|
|
53
56
|
const wss = new ws_1.WebSocketServer({ server: this.httpServer });
|
|
54
57
|
const mergedConfig = {
|
|
55
58
|
...config,
|
|
56
59
|
useRedis: config?.useRedis ?? this.config.useRedis,
|
|
57
60
|
debug: config?.debug ?? this.config.debug,
|
|
58
61
|
};
|
|
59
|
-
return
|
|
62
|
+
return (0, websocket_1.attachSocketAdapter)(wss, mergedConfig);
|
|
60
63
|
}
|
|
61
64
|
listen(port, hostname) {
|
|
62
65
|
const host = hostname ?? '0.0.0.0';
|
|
@@ -69,23 +69,23 @@ class RedisChannelStore {
|
|
|
69
69
|
}
|
|
70
70
|
async subscribe(connectionId, channel) {
|
|
71
71
|
const relay = this.getRelay();
|
|
72
|
-
await relay.
|
|
73
|
-
await relay.
|
|
72
|
+
await relay.sets.add(`${this.channelKeyPrefix}${channel}`, connectionId);
|
|
73
|
+
await relay.sets.add(`${this.connChannelsPrefix}${connectionId}`, channel);
|
|
74
74
|
}
|
|
75
75
|
async unsubscribe(connectionId, channel) {
|
|
76
76
|
const relay = this.getRelay();
|
|
77
|
-
await relay.
|
|
78
|
-
await relay.
|
|
77
|
+
await relay.sets.rem(`${this.channelKeyPrefix}${channel}`, connectionId);
|
|
78
|
+
await relay.sets.rem(`${this.connChannelsPrefix}${connectionId}`, channel);
|
|
79
79
|
}
|
|
80
80
|
async getSubscribers(channel) {
|
|
81
81
|
const relay = this.getRelay();
|
|
82
|
-
return relay.
|
|
82
|
+
return relay.sets.members(`${this.channelKeyPrefix}${channel}`);
|
|
83
83
|
}
|
|
84
84
|
async removeAll(connectionId) {
|
|
85
85
|
const relay = this.getRelay();
|
|
86
|
-
const channels = await relay.
|
|
87
|
-
await Promise.allSettled(channels.map((channel) => relay.
|
|
88
|
-
await relay.
|
|
86
|
+
const channels = await relay.sets.members(`${this.connChannelsPrefix}${connectionId}`);
|
|
87
|
+
await Promise.allSettled(channels.map((channel) => relay.sets.rem(`${this.channelKeyPrefix}${channel}`, connectionId)));
|
|
88
|
+
await relay.del(`${this.connChannelsPrefix}${connectionId}`);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
exports.RedisChannelStore = RedisChannelStore;
|
package/package.json
CHANGED
|
@@ -1,99 +1,112 @@
|
|
|
1
|
-
import http from 'node:http'
|
|
2
|
-
|
|
3
|
-
import express from 'express'
|
|
4
|
-
import cors from 'cors'
|
|
5
|
-
import { WebSocketServer } from 'ws'
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
|
|
3
|
+
import express from 'express';
|
|
4
|
+
import cors from 'cors';
|
|
5
|
+
import { WebSocketServer } from 'ws';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
createMockLambdaContext,
|
|
9
|
+
sendLambdaResult,
|
|
10
|
+
} from '../aws-event-synthesis';
|
|
11
|
+
import {
|
|
12
|
+
synthesizeApiGatewayEvent,
|
|
13
|
+
synthesizeALBEvent,
|
|
14
|
+
} from './event-synthesizer';
|
|
15
|
+
|
|
16
|
+
import { attachSocketAdapter } from '../websocket';
|
|
17
|
+
import type { SocketAdapterConfig } from '../websocket/types';
|
|
12
18
|
import type {
|
|
13
19
|
ApiGatewayHandler,
|
|
14
20
|
ALBHandler,
|
|
15
21
|
LambdaOptionsApiGateway,
|
|
16
22
|
LambdaOptionsALB,
|
|
17
23
|
LocalServerConfig,
|
|
18
|
-
} from './types'
|
|
24
|
+
} from './types';
|
|
19
25
|
|
|
20
|
-
export type { ApiGatewayHandler, ALBHandler, LocalServerConfig } from './types'
|
|
26
|
+
export type { ApiGatewayHandler, ALBHandler, LocalServerConfig } from './types';
|
|
21
27
|
|
|
22
28
|
export class LocalServer {
|
|
23
|
-
readonly app: express.Express
|
|
24
|
-
private httpServer: http.Server
|
|
25
|
-
private config: LocalServerConfig
|
|
29
|
+
readonly app: express.Express;
|
|
30
|
+
private httpServer: http.Server;
|
|
31
|
+
private config: LocalServerConfig;
|
|
26
32
|
|
|
27
33
|
constructor(config?: LocalServerConfig) {
|
|
28
|
-
this.config = config ?? {}
|
|
29
|
-
this.app = express()
|
|
34
|
+
this.config = config ?? {};
|
|
35
|
+
this.app = express();
|
|
30
36
|
|
|
31
|
-
this.app.use(express.json())
|
|
32
|
-
this.app.use(cors())
|
|
37
|
+
this.app.use(express.json());
|
|
38
|
+
this.app.use(cors());
|
|
33
39
|
|
|
34
40
|
this.app.get('/', (_req, res) => {
|
|
35
|
-
res.send('Hello from Express HTTP Server')
|
|
36
|
-
})
|
|
41
|
+
res.send('Hello from Express HTTP Server');
|
|
42
|
+
});
|
|
37
43
|
|
|
38
|
-
this.httpServer = http.createServer(this.app)
|
|
44
|
+
this.httpServer = http.createServer(this.app);
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
// --- Type-safe overloads ---
|
|
42
48
|
|
|
43
|
-
lambda(path: string, handler: ApiGatewayHandler): this
|
|
44
|
-
lambda(
|
|
45
|
-
|
|
49
|
+
lambda(path: string, handler: ApiGatewayHandler): this;
|
|
50
|
+
lambda(
|
|
51
|
+
path: string,
|
|
52
|
+
handler: ApiGatewayHandler,
|
|
53
|
+
options: LambdaOptionsApiGateway
|
|
54
|
+
): this;
|
|
55
|
+
lambda(path: string, handler: ALBHandler, options: LambdaOptionsALB): this;
|
|
46
56
|
lambda(
|
|
47
57
|
path: string,
|
|
48
58
|
handler: ApiGatewayHandler | ALBHandler,
|
|
49
|
-
options?: LambdaOptionsApiGateway | LambdaOptionsALB
|
|
59
|
+
options?: LambdaOptionsApiGateway | LambdaOptionsALB
|
|
50
60
|
): this {
|
|
51
|
-
const eventType = options?.eventType ?? 'api-gateway'
|
|
61
|
+
const eventType = options?.eventType ?? 'api-gateway';
|
|
52
62
|
|
|
53
63
|
this.app.use(path, async (req: express.Request, res: express.Response) => {
|
|
54
64
|
try {
|
|
55
|
-
const context = createMockLambdaContext()
|
|
65
|
+
const context = createMockLambdaContext();
|
|
56
66
|
|
|
57
67
|
if (eventType === 'alb') {
|
|
58
|
-
const event = synthesizeALBEvent(req)
|
|
59
|
-
const result = await (handler as ALBHandler)(event, context)
|
|
60
|
-
sendLambdaResult(res, result)
|
|
68
|
+
const event = synthesizeALBEvent(req);
|
|
69
|
+
const result = await (handler as ALBHandler)(event, context);
|
|
70
|
+
sendLambdaResult(res, result);
|
|
61
71
|
} else {
|
|
62
|
-
const event = synthesizeApiGatewayEvent(req)
|
|
63
|
-
const result = await (handler as ApiGatewayHandler)(event, context)
|
|
64
|
-
sendLambdaResult(res, result ?? {})
|
|
72
|
+
const event = synthesizeApiGatewayEvent(req);
|
|
73
|
+
const result = await (handler as ApiGatewayHandler)(event, context);
|
|
74
|
+
sendLambdaResult(res, result ?? {});
|
|
65
75
|
}
|
|
66
76
|
} catch (err) {
|
|
67
|
-
console.error(
|
|
77
|
+
console.error(
|
|
78
|
+
`[LocalServer] Error handling ${req.method} ${req.originalUrl}:`,
|
|
79
|
+
err
|
|
80
|
+
);
|
|
68
81
|
if (!res.headersSent) {
|
|
69
|
-
res.status(500).json({ error: 'Internal Server Error' })
|
|
82
|
+
res.status(500).json({ error: 'Internal Server Error' });
|
|
70
83
|
}
|
|
71
84
|
}
|
|
72
|
-
})
|
|
85
|
+
});
|
|
73
86
|
|
|
74
|
-
return this
|
|
87
|
+
return this;
|
|
75
88
|
}
|
|
76
89
|
|
|
77
90
|
attachSocket(
|
|
78
|
-
adapterFn: typeof attachSocketAdapter,
|
|
79
|
-
config?: SocketAdapterConfig
|
|
91
|
+
//adapterFn: typeof attachSocketAdapter,
|
|
92
|
+
config?: SocketAdapterConfig
|
|
80
93
|
): ReturnType<typeof attachSocketAdapter> {
|
|
81
|
-
const wss = new WebSocketServer({ server: this.httpServer })
|
|
94
|
+
const wss = new WebSocketServer({ server: this.httpServer });
|
|
82
95
|
const mergedConfig: SocketAdapterConfig = {
|
|
83
96
|
...config,
|
|
84
97
|
useRedis: config?.useRedis ?? this.config.useRedis,
|
|
85
98
|
debug: config?.debug ?? this.config.debug,
|
|
86
|
-
}
|
|
87
|
-
return
|
|
99
|
+
};
|
|
100
|
+
return attachSocketAdapter(wss, mergedConfig);
|
|
88
101
|
}
|
|
89
102
|
|
|
90
103
|
listen(port: number, hostname?: string): void {
|
|
91
|
-
const host = hostname ?? '0.0.0.0'
|
|
104
|
+
const host = hostname ?? '0.0.0.0';
|
|
92
105
|
this.httpServer.listen(port, host, () => {
|
|
93
106
|
if (this.config.debug) {
|
|
94
|
-
console.info(`HTTP Server running at http://localhost:${port}`)
|
|
95
|
-
console.info(`WebSocket server running on ws://localhost:${port}`)
|
|
107
|
+
console.info(`HTTP Server running at http://localhost:${port}`);
|
|
108
|
+
console.info(`WebSocket server running on ws://localhost:${port}`);
|
|
96
109
|
}
|
|
97
|
-
})
|
|
110
|
+
});
|
|
98
111
|
}
|
|
99
112
|
}
|
|
@@ -86,31 +86,31 @@ export class RedisChannelStore implements ChannelStore {
|
|
|
86
86
|
|
|
87
87
|
async subscribe(connectionId: ConnectionId, channel: string): Promise<void> {
|
|
88
88
|
const relay = this.getRelay()
|
|
89
|
-
await relay.
|
|
90
|
-
await relay.
|
|
89
|
+
await relay.sets.add(`${this.channelKeyPrefix}${channel}`, connectionId)
|
|
90
|
+
await relay.sets.add(`${this.connChannelsPrefix}${connectionId}`, channel)
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
async unsubscribe(connectionId: ConnectionId, channel: string): Promise<void> {
|
|
94
94
|
const relay = this.getRelay()
|
|
95
|
-
await relay.
|
|
96
|
-
await relay.
|
|
95
|
+
await relay.sets.rem(`${this.channelKeyPrefix}${channel}`, connectionId)
|
|
96
|
+
await relay.sets.rem(`${this.connChannelsPrefix}${connectionId}`, channel)
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
async getSubscribers(channel: string): Promise<ConnectionId[]> {
|
|
100
100
|
const relay = this.getRelay()
|
|
101
|
-
return relay.
|
|
101
|
+
return relay.sets.members(`${this.channelKeyPrefix}${channel}`)
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
async removeAll(connectionId: ConnectionId): Promise<void> {
|
|
105
105
|
const relay = this.getRelay()
|
|
106
|
-
const channels: string[] = await relay.
|
|
106
|
+
const channels: string[] = await relay.sets.members(`${this.connChannelsPrefix}${connectionId}`)
|
|
107
107
|
|
|
108
108
|
await Promise.allSettled(
|
|
109
109
|
channels.map((channel) =>
|
|
110
|
-
relay.
|
|
110
|
+
relay.sets.rem(`${this.channelKeyPrefix}${channel}`, connectionId)
|
|
111
111
|
)
|
|
112
112
|
)
|
|
113
113
|
|
|
114
|
-
await relay.
|
|
114
|
+
await relay.del(`${this.connChannelsPrefix}${connectionId}`)
|
|
115
115
|
}
|
|
116
116
|
}
|