@desplega.ai/slack-mock 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/README.md +113 -0
- package/dist/body.d.ts +6 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +94 -0
- package/dist/events.d.ts +36 -0
- package/dist/ids.d.ts +8 -0
- package/dist/index-thgwpw70.js +3693 -0
- package/dist/index-v7mbr1cx.js +48 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +17 -0
- package/dist/render/blocks.d.ts +7 -0
- package/dist/render/index.d.ts +25 -0
- package/dist/render/mrkdwn.d.ts +16 -0
- package/dist/screenshot.d.ts +13 -0
- package/dist/screenshot.js +9 -0
- package/dist/server.d.ts +240 -0
- package/dist/socket-mode.d.ts +73 -0
- package/dist/store.d.ts +150 -0
- package/dist/types.d.ts +211 -0
- package/dist/web-api.d.ts +26 -0
- package/package.json +60 -0
- package/src/body.ts +84 -0
- package/src/cli.ts +89 -0
- package/src/events.ts +281 -0
- package/src/ids.ts +29 -0
- package/src/index.ts +13 -0
- package/src/render/blocks.ts +392 -0
- package/src/render/index.ts +846 -0
- package/src/render/mrkdwn.ts +321 -0
- package/src/screenshot.ts +59 -0
- package/src/server.ts +1198 -0
- package/src/socket-mode.ts +261 -0
- package/src/store.ts +654 -0
- package/src/types.ts +159 -0
- package/src/web-api.ts +694 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import type { ServerWebSocket } from "bun";
|
|
2
|
+
|
|
3
|
+
export interface ConnData {
|
|
4
|
+
id: string;
|
|
5
|
+
ticket: string;
|
|
6
|
+
pingTimer?: ReturnType<typeof setInterval>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type EnvelopeType = "events_api" | "interactive" | "slash_commands";
|
|
10
|
+
|
|
11
|
+
export interface Envelope {
|
|
12
|
+
envelope_id: string;
|
|
13
|
+
type: EnvelopeType;
|
|
14
|
+
accepts_response_payload: boolean;
|
|
15
|
+
retry_attempt: number;
|
|
16
|
+
retry_reason: string;
|
|
17
|
+
payload: unknown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AckResult {
|
|
21
|
+
envelope_id: string;
|
|
22
|
+
/** Body the app sent with its ack, e.g. a slash-command response. */
|
|
23
|
+
payload?: unknown;
|
|
24
|
+
attempts: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface SocketModeOptions {
|
|
28
|
+
appId: string;
|
|
29
|
+
/** Server ping frame interval. Slack pings often; the client expects one at least every 30s. */
|
|
30
|
+
pingIntervalMs?: number;
|
|
31
|
+
/** How long to wait for an ack before redelivering (Slack uses ~3s). */
|
|
32
|
+
ackTimeoutMs?: number;
|
|
33
|
+
/** Redeliveries after the first attempt (Slack retries a few times). */
|
|
34
|
+
maxRetries?: number;
|
|
35
|
+
log?: (msg: string) => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface Pending {
|
|
39
|
+
envelope: Envelope;
|
|
40
|
+
record: DeliveryRecord;
|
|
41
|
+
resolve: (r: AckResult) => void;
|
|
42
|
+
reject: (e: Error) => void;
|
|
43
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
44
|
+
attempts: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class SocketModeClosedError extends Error {}
|
|
48
|
+
|
|
49
|
+
export interface DeliveryRecord {
|
|
50
|
+
envelope_id: string;
|
|
51
|
+
type: EnvelopeType;
|
|
52
|
+
/** event.type, command name or interactive payload type. */
|
|
53
|
+
name: string;
|
|
54
|
+
sentAt: string;
|
|
55
|
+
attempts: number;
|
|
56
|
+
acked: boolean;
|
|
57
|
+
ackPayload?: unknown;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function envelopeName(type: EnvelopeType, payload: unknown): string {
|
|
61
|
+
const p = payload as Record<string, unknown>;
|
|
62
|
+
if (type === "events_api")
|
|
63
|
+
return String((p.event as Record<string, unknown> | undefined)?.type ?? "event");
|
|
64
|
+
if (type === "slash_commands") return String(p.command ?? "command");
|
|
65
|
+
return String(p.type ?? "interactive");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The Socket Mode side of the mock: tracks app connections, pushes envelopes,
|
|
70
|
+
* waits for acks, redelivers on timeout and keeps the link alive with pings.
|
|
71
|
+
*/
|
|
72
|
+
export class SocketModeHub {
|
|
73
|
+
private connections = new Map<string, ServerWebSocket<ConnData>>();
|
|
74
|
+
private pending = new Map<string, Pending>();
|
|
75
|
+
private connectionWaiters: Array<() => void> = [];
|
|
76
|
+
private rr = 0;
|
|
77
|
+
readonly tickets = new Set<string>();
|
|
78
|
+
/** Every envelope ever sent, in order, with its ack status. */
|
|
79
|
+
readonly history: DeliveryRecord[] = [];
|
|
80
|
+
readonly opts: Required<Omit<SocketModeOptions, "log">> & { log: (msg: string) => void };
|
|
81
|
+
|
|
82
|
+
constructor(opts: SocketModeOptions) {
|
|
83
|
+
this.opts = {
|
|
84
|
+
appId: opts.appId,
|
|
85
|
+
pingIntervalMs: opts.pingIntervalMs ?? 10_000,
|
|
86
|
+
ackTimeoutMs: opts.ackTimeoutMs ?? 3_000,
|
|
87
|
+
maxRetries: opts.maxRetries ?? 3,
|
|
88
|
+
log: opts.log ?? (() => {}),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get connectionCount(): number {
|
|
93
|
+
return this.connections.size;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
issueTicket(): string {
|
|
97
|
+
const ticket = crypto.randomUUID().replace(/-/g, "");
|
|
98
|
+
this.tickets.add(ticket);
|
|
99
|
+
return ticket;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ------------------------------------------------- Bun websocket handlers
|
|
103
|
+
|
|
104
|
+
open(ws: ServerWebSocket<ConnData>): void {
|
|
105
|
+
this.connections.set(ws.data.id, ws);
|
|
106
|
+
this.opts.log(`socket ${ws.data.id} connected (${this.connections.size} total)`);
|
|
107
|
+
ws.send(
|
|
108
|
+
JSON.stringify({
|
|
109
|
+
type: "hello",
|
|
110
|
+
num_connections: this.connections.size,
|
|
111
|
+
debug_info: {
|
|
112
|
+
host: "slack-mock",
|
|
113
|
+
build_number: 1,
|
|
114
|
+
approximate_connection_time: 18060,
|
|
115
|
+
},
|
|
116
|
+
connection_info: { app_id: this.opts.appId },
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
ws.data.pingTimer = setInterval(() => {
|
|
120
|
+
if (ws.readyState === 1) ws.ping();
|
|
121
|
+
}, this.opts.pingIntervalMs);
|
|
122
|
+
for (const w of this.connectionWaiters.splice(0)) w();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
message(ws: ServerWebSocket<ConnData>, raw: string | Buffer): void {
|
|
126
|
+
let msg: { envelope_id?: string; payload?: unknown };
|
|
127
|
+
try {
|
|
128
|
+
msg = JSON.parse(String(raw));
|
|
129
|
+
} catch {
|
|
130
|
+
this.opts.log(`socket ${ws.data.id} sent non-JSON frame`);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (!msg.envelope_id) return;
|
|
134
|
+
const p = this.pending.get(msg.envelope_id);
|
|
135
|
+
if (!p) {
|
|
136
|
+
this.opts.log(`ack for unknown/expired envelope ${msg.envelope_id}`);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
clearTimeout(p.timer);
|
|
140
|
+
this.pending.delete(msg.envelope_id);
|
|
141
|
+
p.record.acked = true;
|
|
142
|
+
p.record.ackPayload = msg.payload;
|
|
143
|
+
p.resolve({ envelope_id: msg.envelope_id, payload: msg.payload, attempts: p.attempts });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
close(ws: ServerWebSocket<ConnData>): void {
|
|
147
|
+
clearInterval(ws.data.pingTimer);
|
|
148
|
+
this.connections.delete(ws.data.id);
|
|
149
|
+
this.opts.log(`socket ${ws.data.id} closed (${this.connections.size} left)`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ------------------------------------------------------------- delivery
|
|
153
|
+
|
|
154
|
+
waitForConnection(timeoutMs = 10_000): Promise<void> {
|
|
155
|
+
if (this.connections.size > 0) return Promise.resolve();
|
|
156
|
+
return new Promise((resolve, reject) => {
|
|
157
|
+
const timer = setTimeout(() => {
|
|
158
|
+
this.connectionWaiters = this.connectionWaiters.filter((w) => w !== done);
|
|
159
|
+
reject(new Error(`no Socket Mode connection after ${timeoutMs}ms`));
|
|
160
|
+
}, timeoutMs);
|
|
161
|
+
const done = () => {
|
|
162
|
+
clearTimeout(timer);
|
|
163
|
+
resolve();
|
|
164
|
+
};
|
|
165
|
+
this.connectionWaiters.push(done);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Push an envelope to one connected app and resolve when it is acked. */
|
|
170
|
+
send(type: EnvelopeType, payload: unknown, acceptsResponsePayload = false): Promise<AckResult> {
|
|
171
|
+
const envelope: Envelope = {
|
|
172
|
+
envelope_id: crypto.randomUUID(),
|
|
173
|
+
type,
|
|
174
|
+
accepts_response_payload: acceptsResponsePayload,
|
|
175
|
+
retry_attempt: 0,
|
|
176
|
+
retry_reason: "",
|
|
177
|
+
payload,
|
|
178
|
+
};
|
|
179
|
+
const record: DeliveryRecord = {
|
|
180
|
+
envelope_id: envelope.envelope_id,
|
|
181
|
+
type,
|
|
182
|
+
name: envelopeName(type, payload),
|
|
183
|
+
sentAt: new Date().toISOString(),
|
|
184
|
+
attempts: 0,
|
|
185
|
+
acked: false,
|
|
186
|
+
};
|
|
187
|
+
this.history.push(record);
|
|
188
|
+
return new Promise<AckResult>((resolve, reject) => {
|
|
189
|
+
const p: Pending = { envelope, record, resolve, reject, attempts: 0 };
|
|
190
|
+
this.pending.set(envelope.envelope_id, p);
|
|
191
|
+
this.attempt(p);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private attempt(p: Pending): void {
|
|
196
|
+
const ws = this.pickConnection();
|
|
197
|
+
if (!ws) {
|
|
198
|
+
this.pending.delete(p.envelope.envelope_id);
|
|
199
|
+
p.reject(new SocketModeClosedError("no Socket Mode connection; is the app running?"));
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
p.attempts += 1;
|
|
203
|
+
p.record.attempts = p.attempts;
|
|
204
|
+
const wire = { ...p.envelope };
|
|
205
|
+
if (p.attempts > 1) {
|
|
206
|
+
wire.retry_attempt = p.attempts - 1;
|
|
207
|
+
wire.retry_reason = "timeout";
|
|
208
|
+
}
|
|
209
|
+
ws.send(JSON.stringify(wire));
|
|
210
|
+
p.timer = setTimeout(() => {
|
|
211
|
+
if (p.attempts > this.opts.maxRetries) {
|
|
212
|
+
this.pending.delete(p.envelope.envelope_id);
|
|
213
|
+
p.reject(
|
|
214
|
+
new Error(
|
|
215
|
+
`envelope ${p.envelope.envelope_id} (${p.envelope.type}) not acked after ${p.attempts} attempts`,
|
|
216
|
+
),
|
|
217
|
+
);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
this.opts.log(
|
|
221
|
+
`no ack for ${p.envelope.envelope_id}; redelivering (attempt ${p.attempts + 1})`,
|
|
222
|
+
);
|
|
223
|
+
this.attempt(p);
|
|
224
|
+
}, this.opts.ackTimeoutMs);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private pickConnection(): ServerWebSocket<ConnData> | undefined {
|
|
228
|
+
const list = [...this.connections.values()];
|
|
229
|
+
if (list.length === 0) return undefined;
|
|
230
|
+
this.rr = (this.rr + 1) % list.length;
|
|
231
|
+
return list[this.rr];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Emulate Slack asking apps to reconnect (or just drop them). */
|
|
235
|
+
disconnectAll(
|
|
236
|
+
reason:
|
|
237
|
+
| "warning"
|
|
238
|
+
| "refresh_requested"
|
|
239
|
+
| "too_many_websockets"
|
|
240
|
+
| "link_disabled" = "refresh_requested",
|
|
241
|
+
graceMs = 200,
|
|
242
|
+
): void {
|
|
243
|
+
for (const ws of this.connections.values()) {
|
|
244
|
+
ws.send(JSON.stringify({ type: "disconnect", reason, debug_info: { host: "slack-mock" } }));
|
|
245
|
+
setTimeout(() => ws.close(1000, reason), graceMs);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
shutdown(): void {
|
|
250
|
+
for (const p of this.pending.values()) {
|
|
251
|
+
clearTimeout(p.timer);
|
|
252
|
+
p.reject(new SocketModeClosedError("mock stopped"));
|
|
253
|
+
}
|
|
254
|
+
this.pending.clear();
|
|
255
|
+
for (const ws of this.connections.values()) {
|
|
256
|
+
clearInterval(ws.data.pingTimer);
|
|
257
|
+
ws.close(1001, "server shutting down");
|
|
258
|
+
}
|
|
259
|
+
this.connections.clear();
|
|
260
|
+
}
|
|
261
|
+
}
|