@desplega.ai/slack-mock 0.1.1 → 0.2.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 +33 -0
- package/dist/cli.js +45 -6
- package/dist/frames.d.ts +41 -0
- package/dist/{index-eyfkn32t.js → index-6c88z809.js} +169 -75
- package/dist/{index-v7mbr1cx.js → index-v2hesj4s.js} +8 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +14 -12
- package/dist/screenshot.d.ts +2 -0
- package/dist/screenshot.js +7 -5
- package/dist/server.d.ts +4 -0
- package/dist/store.d.ts +12 -1
- package/package.json +1 -1
- package/src/cli.ts +37 -0
- package/src/frames.ts +128 -0
- package/src/index.ts +2 -0
- package/src/render/index.ts +28 -1
- package/src/screenshot.ts +9 -3
- package/src/server.ts +16 -8
- package/src/store.ts +39 -15
- package/src/web-api.ts +5 -1
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
import {
|
|
3
|
+
requireChrome2,
|
|
4
|
+
screenshot2
|
|
5
|
+
} from "./index-v2hesj4s.js";
|
|
6
|
+
|
|
2
7
|
// src/store.ts
|
|
3
8
|
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "fs";
|
|
4
9
|
import { dirname } from "path";
|
|
@@ -26,7 +31,7 @@ function nowUnix() {
|
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
// src/store.ts
|
|
29
|
-
class
|
|
34
|
+
class SlackApiError2 extends Error {
|
|
30
35
|
code;
|
|
31
36
|
extra;
|
|
32
37
|
constructor(code, extra = {}) {
|
|
@@ -35,8 +40,24 @@ class SlackApiError extends Error {
|
|
|
35
40
|
this.extra = extra;
|
|
36
41
|
}
|
|
37
42
|
}
|
|
43
|
+
function parseJournal(text, source = "journal") {
|
|
44
|
+
const entries = [];
|
|
45
|
+
let line = 0;
|
|
46
|
+
for (const raw of text.split(`
|
|
47
|
+
`)) {
|
|
48
|
+
line += 1;
|
|
49
|
+
if (!raw.trim())
|
|
50
|
+
continue;
|
|
51
|
+
try {
|
|
52
|
+
entries.push({ line, change: JSON.parse(raw) });
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.warn(`[slack-mock] skipping corrupt journal line ${line} in ${source}: ${e instanceof Error ? e.message : e}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return entries;
|
|
58
|
+
}
|
|
38
59
|
|
|
39
|
-
class
|
|
60
|
+
class Store2 {
|
|
40
61
|
team;
|
|
41
62
|
app;
|
|
42
63
|
bot;
|
|
@@ -67,7 +88,9 @@ class Store {
|
|
|
67
88
|
};
|
|
68
89
|
this.dataFile = opts.dataFile;
|
|
69
90
|
if (this.dataFile && existsSync(this.dataFile))
|
|
70
|
-
this.replay(this.dataFile);
|
|
91
|
+
this.replay(parseJournal(readFileSync(this.dataFile, "utf8"), this.dataFile).map((e) => e.change));
|
|
92
|
+
if (opts.replay)
|
|
93
|
+
this.replay(opts.replay);
|
|
71
94
|
if (!this.users.has(this.bot.userId)) {
|
|
72
95
|
this.addUser({
|
|
73
96
|
id: this.bot.userId,
|
|
@@ -92,21 +115,11 @@ class Store {
|
|
|
92
115
|
for (const l of this.listeners)
|
|
93
116
|
l(change);
|
|
94
117
|
}
|
|
95
|
-
replay(
|
|
118
|
+
replay(changes) {
|
|
96
119
|
this.replaying = true;
|
|
97
120
|
try {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
`)) {
|
|
101
|
-
lineNo += 1;
|
|
102
|
-
if (!line.trim())
|
|
103
|
-
continue;
|
|
104
|
-
try {
|
|
105
|
-
this.applyReplayed(JSON.parse(line));
|
|
106
|
-
} catch (e) {
|
|
107
|
-
console.warn(`[slack-mock] skipping corrupt journal line ${lineNo} in ${file}: ${e instanceof Error ? e.message : e}`);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
121
|
+
for (const change of changes)
|
|
122
|
+
this.applyReplayed(change);
|
|
110
123
|
} finally {
|
|
111
124
|
this.replaying = false;
|
|
112
125
|
}
|
|
@@ -181,7 +194,7 @@ class Store {
|
|
|
181
194
|
user(id) {
|
|
182
195
|
const u = this.users.get(id);
|
|
183
196
|
if (!u)
|
|
184
|
-
throw new
|
|
197
|
+
throw new SlackApiError2("user_not_found");
|
|
185
198
|
return u;
|
|
186
199
|
}
|
|
187
200
|
userByEmail(email) {
|
|
@@ -197,7 +210,7 @@ class Store {
|
|
|
197
210
|
const name = input.name.replace(/^#/, "");
|
|
198
211
|
for (const c of this.channels.values()) {
|
|
199
212
|
if (c.name === name && !c.is_im)
|
|
200
|
-
throw new
|
|
213
|
+
throw new SlackApiError2("name_taken");
|
|
201
214
|
}
|
|
202
215
|
const id = input.id ?? slackId(input.is_private ? "G" : "C");
|
|
203
216
|
const creator = input.creator ?? this.bot.userId;
|
|
@@ -254,7 +267,7 @@ class Store {
|
|
|
254
267
|
channel(id) {
|
|
255
268
|
const c = this.channels.get(id);
|
|
256
269
|
if (!c)
|
|
257
|
-
throw new
|
|
270
|
+
throw new SlackApiError2("channel_not_found");
|
|
258
271
|
return c;
|
|
259
272
|
}
|
|
260
273
|
channelByName(name) {
|
|
@@ -276,7 +289,7 @@ class Store {
|
|
|
276
289
|
join(channelId, userId, inviter) {
|
|
277
290
|
const channel = this.channel(channelId);
|
|
278
291
|
if (channel.is_archived)
|
|
279
|
-
throw new
|
|
292
|
+
throw new SlackApiError2("is_archived");
|
|
280
293
|
if (channel.members.includes(userId))
|
|
281
294
|
return { alreadyMember: true };
|
|
282
295
|
channel.members.push(userId);
|
|
@@ -286,9 +299,9 @@ class Store {
|
|
|
286
299
|
archive(channelId) {
|
|
287
300
|
const channel = this.channel(channelId);
|
|
288
301
|
if (channel.is_archived)
|
|
289
|
-
throw new
|
|
302
|
+
throw new SlackApiError2("already_archived");
|
|
290
303
|
if (channel.is_general)
|
|
291
|
-
throw new
|
|
304
|
+
throw new SlackApiError2("cant_archive_general");
|
|
292
305
|
channel.is_archived = true;
|
|
293
306
|
this.emit({ kind: "channel.update", channel });
|
|
294
307
|
}
|
|
@@ -323,28 +336,28 @@ class Store {
|
|
|
323
336
|
updateEphemeral(channelId, ts, patch) {
|
|
324
337
|
const m = this.ephemerals.find((x) => x.channel === channelId && x.ts === ts);
|
|
325
338
|
if (!m)
|
|
326
|
-
throw new
|
|
339
|
+
throw new SlackApiError2("message_not_found");
|
|
327
340
|
Object.assign(m, patch);
|
|
328
341
|
return m;
|
|
329
342
|
}
|
|
330
343
|
deleteEphemeral(channelId, ts) {
|
|
331
344
|
const i = this.ephemerals.findIndex((x) => x.channel === channelId && x.ts === ts);
|
|
332
345
|
if (i < 0)
|
|
333
|
-
throw new
|
|
346
|
+
throw new SlackApiError2("message_not_found");
|
|
334
347
|
this.ephemerals.splice(i, 1);
|
|
335
348
|
}
|
|
336
349
|
message(channelId, ts) {
|
|
337
350
|
const m = this.findMessage(channelId, ts);
|
|
338
351
|
if (!m)
|
|
339
|
-
throw new
|
|
352
|
+
throw new SlackApiError2("message_not_found");
|
|
340
353
|
return m;
|
|
341
354
|
}
|
|
342
355
|
addMessage(input) {
|
|
343
356
|
const channel = this.channel(input.channel);
|
|
344
357
|
if (channel.is_archived)
|
|
345
|
-
throw new
|
|
358
|
+
throw new SlackApiError2("is_archived");
|
|
346
359
|
if (input.thread_ts && !this.findMessage(input.channel, input.thread_ts)) {
|
|
347
|
-
throw new
|
|
360
|
+
throw new SlackApiError2("thread_not_found");
|
|
348
361
|
}
|
|
349
362
|
const message = {
|
|
350
363
|
type: "message",
|
|
@@ -359,10 +372,10 @@ class Store {
|
|
|
359
372
|
this.emit({ kind: "message.add", message });
|
|
360
373
|
return message;
|
|
361
374
|
}
|
|
362
|
-
updateMessage(channelId, ts, patch, editor) {
|
|
375
|
+
updateMessage(channelId, ts, patch, editor, opts = {}) {
|
|
363
376
|
const message = this.message(channelId, ts);
|
|
364
377
|
const previous = structuredClone(message);
|
|
365
|
-
Object.assign(message, patch, { edited: { user: editor, ts: nextTs() } });
|
|
378
|
+
Object.assign(message, patch, opts.markEdited === false ? {} : { edited: { user: editor, ts: nextTs() } });
|
|
366
379
|
this.emit({ kind: "message.update", message, previous });
|
|
367
380
|
return message;
|
|
368
381
|
}
|
|
@@ -370,7 +383,7 @@ class Store {
|
|
|
370
383
|
const list = this.messages.get(channelId);
|
|
371
384
|
const i = list?.findIndex((m) => m.ts === ts) ?? -1;
|
|
372
385
|
if (!list || i < 0)
|
|
373
|
-
throw new
|
|
386
|
+
throw new SlackApiError2("message_not_found");
|
|
374
387
|
const [message] = list.splice(i, 1);
|
|
375
388
|
if (message.thread_ts && message.thread_ts !== ts) {
|
|
376
389
|
const parent = this.findMessage(channelId, message.thread_ts);
|
|
@@ -385,7 +398,7 @@ class Store {
|
|
|
385
398
|
message.reactions ??= [];
|
|
386
399
|
let reaction = message.reactions.find((r) => r.name === name);
|
|
387
400
|
if (reaction?.users.includes(userId))
|
|
388
|
-
throw new
|
|
401
|
+
throw new SlackApiError2("already_reacted");
|
|
389
402
|
if (!reaction) {
|
|
390
403
|
reaction = { name, users: [], count: 0 };
|
|
391
404
|
message.reactions.push(reaction);
|
|
@@ -399,7 +412,7 @@ class Store {
|
|
|
399
412
|
const message = this.message(channelId, ts);
|
|
400
413
|
const reaction = message.reactions?.find((r) => r.name === name);
|
|
401
414
|
if (!reaction?.users.includes(userId))
|
|
402
|
-
throw new
|
|
415
|
+
throw new SlackApiError2("no_reaction");
|
|
403
416
|
reaction.users = reaction.users.filter((u) => u !== userId);
|
|
404
417
|
reaction.count = reaction.users.length;
|
|
405
418
|
message.reactions = message.reactions.filter((r) => r.count > 0);
|
|
@@ -458,7 +471,7 @@ class Store {
|
|
|
458
471
|
file(id) {
|
|
459
472
|
const f = this.files.get(id);
|
|
460
473
|
if (!f)
|
|
461
|
-
throw new
|
|
474
|
+
throw new SlackApiError2("file_not_found");
|
|
462
475
|
return f;
|
|
463
476
|
}
|
|
464
477
|
openView(view) {
|
|
@@ -590,9 +603,9 @@ async function parseArgs(req) {
|
|
|
590
603
|
return args;
|
|
591
604
|
const contentType = req.headers.get("content-type") ?? "";
|
|
592
605
|
if (contentType.includes("application/json")) {
|
|
593
|
-
const
|
|
594
|
-
if (
|
|
595
|
-
Object.assign(args, JSON.parse(
|
|
606
|
+
const text = await req.text();
|
|
607
|
+
if (text.trim())
|
|
608
|
+
Object.assign(args, JSON.parse(text));
|
|
596
609
|
return args;
|
|
597
610
|
}
|
|
598
611
|
if (contentType.includes("multipart/form-data")) {
|
|
@@ -1812,7 +1825,12 @@ function renderMessage(store, ctx, m, opts, flags = {}) {
|
|
|
1812
1825
|
const isApp = Boolean(m.bot_id);
|
|
1813
1826
|
const blocks = Array.isArray(m.blocks) && m.blocks.length ? m.blocks : undefined;
|
|
1814
1827
|
const body = [];
|
|
1815
|
-
if (
|
|
1828
|
+
if (m.streaming_state !== undefined) {
|
|
1829
|
+
if (m.text)
|
|
1830
|
+
body.push(`<div class="sm-text">${mrkdwnToHtml(markdownToMrkdwn(m.text), ctx)}</div>`);
|
|
1831
|
+
if (blocks)
|
|
1832
|
+
body.push(renderBlocks(blocks, ctx));
|
|
1833
|
+
} else if (blocks)
|
|
1816
1834
|
body.push(renderBlocks(blocks, ctx));
|
|
1817
1835
|
else if (m.text)
|
|
1818
1836
|
body.push(`<div class="sm-text">${mrkdwnToHtml(m.text, ctx)}</div>`);
|
|
@@ -1833,6 +1851,9 @@ function renderMessage(store, ctx, m, opts, flags = {}) {
|
|
|
1833
1851
|
const open = flags.openTs && flags.openTs === m.ts ? " sm-msg-open" : "";
|
|
1834
1852
|
return `<div class="sm-msg${flags.ephemeral ? " sm-msg-eph" : ""}${open}" data-ts="${escapeHtml(m.ts)}">${avatarHtml(m, name)}<div class="sm-msg-body"><div class="sm-msg-head"><span class="sm-name">${escapeHtml(name)}</span>${isApp ? `<span class="sm-badge">APP</span>` : ""}<span class="sm-time">${timeOf(m.ts)}</span>${streaming}</div>${body.join("")}</div></div>`;
|
|
1835
1853
|
}
|
|
1854
|
+
function markdownToMrkdwn(text) {
|
|
1855
|
+
return text.split(/(```[\s\S]*?```)/).map((part, i) => i % 2 === 1 ? part : part.replace(/\*\*(.+?)\*\*/g, "*$1*").replace(/__(.+?)__/g, "*$1*").replace(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g, "<$2|$1>").replace(/^#{1,6}\s+(.+)$/gm, "*$1*").replace(/^(\s*)[-*]\s+/gm, "$1\u2022 ")).join("");
|
|
1856
|
+
}
|
|
1836
1857
|
function renderMessageList(store, ctx, messages, opts, flags = {}) {
|
|
1837
1858
|
const out = [];
|
|
1838
1859
|
let day = "";
|
|
@@ -2300,7 +2321,7 @@ class SocketModeHub {
|
|
|
2300
2321
|
function required(args, key) {
|
|
2301
2322
|
const v = str(args, key);
|
|
2302
2323
|
if (!v)
|
|
2303
|
-
throw new
|
|
2324
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2304
2325
|
response_metadata: { messages: [`[ERROR] missing required field: ${key}`] }
|
|
2305
2326
|
});
|
|
2306
2327
|
return v;
|
|
@@ -2309,7 +2330,7 @@ function resolveChannel(store, raw) {
|
|
|
2309
2330
|
if (raw.startsWith("#")) {
|
|
2310
2331
|
const c = store.channelByName(raw);
|
|
2311
2332
|
if (!c)
|
|
2312
|
-
throw new
|
|
2333
|
+
throw new SlackApiError2("channel_not_found");
|
|
2313
2334
|
return c;
|
|
2314
2335
|
}
|
|
2315
2336
|
if (raw.startsWith("U") || raw.startsWith("W"))
|
|
@@ -2318,7 +2339,7 @@ function resolveChannel(store, raw) {
|
|
|
2318
2339
|
}
|
|
2319
2340
|
function requireMember(channel, actor) {
|
|
2320
2341
|
if (!channel.members.includes(actor.userId))
|
|
2321
|
-
throw new
|
|
2342
|
+
throw new SlackApiError2("not_in_channel");
|
|
2322
2343
|
}
|
|
2323
2344
|
function botFields(ctx) {
|
|
2324
2345
|
if (!ctx.actor.isBot)
|
|
@@ -2340,9 +2361,9 @@ function messageContent(args) {
|
|
|
2340
2361
|
const blocks = Array.isArray(args.blocks) ? args.blocks : undefined;
|
|
2341
2362
|
const attachments = Array.isArray(args.attachments) ? args.attachments : undefined;
|
|
2342
2363
|
if (!text && !blocks?.length && !attachments?.length)
|
|
2343
|
-
throw new
|
|
2364
|
+
throw new SlackApiError2("no_text");
|
|
2344
2365
|
if (blocks && blocks.length > 50)
|
|
2345
|
-
throw new
|
|
2366
|
+
throw new SlackApiError2("invalid_blocks", {
|
|
2346
2367
|
response_metadata: { messages: ["[ERROR] must have at most 50 blocks"] }
|
|
2347
2368
|
});
|
|
2348
2369
|
const metadata = args.metadata && typeof args.metadata === "object" ? args.metadata : undefined;
|
|
@@ -2443,7 +2464,7 @@ var handlers = {
|
|
|
2443
2464
|
const ts = required(args, "ts");
|
|
2444
2465
|
const existing = ctx.store.message(channel.id, ts);
|
|
2445
2466
|
if (existing.user !== ctx.actor.userId && !ctx.actor.isBot)
|
|
2446
|
-
throw new
|
|
2467
|
+
throw new SlackApiError2("cant_update_message");
|
|
2447
2468
|
const patch = {};
|
|
2448
2469
|
if (args.text !== undefined)
|
|
2449
2470
|
patch.text = str(args, "text") ?? "";
|
|
@@ -2454,7 +2475,7 @@ var handlers = {
|
|
|
2454
2475
|
if (args.metadata !== undefined)
|
|
2455
2476
|
patch.metadata = args.metadata;
|
|
2456
2477
|
if (patch.text === "" && !patch.blocks?.length && !patch.attachments?.length && !existing.blocks?.length)
|
|
2457
|
-
throw new
|
|
2478
|
+
throw new SlackApiError2("no_text");
|
|
2458
2479
|
const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId);
|
|
2459
2480
|
return { channel: channel.id, ts, text: message.text, message: wireMessage(message) };
|
|
2460
2481
|
},
|
|
@@ -2485,8 +2506,8 @@ var handlers = {
|
|
|
2485
2506
|
const ts = required(args, "ts");
|
|
2486
2507
|
const existing = ctx.store.message(channel.id, ts);
|
|
2487
2508
|
if (existing.streaming_state !== "in_progress")
|
|
2488
|
-
throw new
|
|
2489
|
-
const message = ctx.store.updateMessage(channel.id, ts, { text: existing.text + streamText(args) }, ctx.actor.userId);
|
|
2509
|
+
throw new SlackApiError2("message_not_in_streaming_state");
|
|
2510
|
+
const message = ctx.store.updateMessage(channel.id, ts, { text: existing.text + streamText(args) }, ctx.actor.userId, { markEdited: false });
|
|
2490
2511
|
return { channel: channel.id, ts: message.ts };
|
|
2491
2512
|
},
|
|
2492
2513
|
"chat.stopStream": (ctx, args) => {
|
|
@@ -2494,14 +2515,16 @@ var handlers = {
|
|
|
2494
2515
|
const ts = required(args, "ts");
|
|
2495
2516
|
const existing = ctx.store.message(channel.id, ts);
|
|
2496
2517
|
if (existing.streaming_state !== "in_progress")
|
|
2497
|
-
throw new
|
|
2518
|
+
throw new SlackApiError2("message_not_in_streaming_state");
|
|
2498
2519
|
const patch = {
|
|
2499
2520
|
streaming_state: "completed",
|
|
2500
2521
|
text: existing.text + streamText(args)
|
|
2501
2522
|
};
|
|
2502
2523
|
if (Array.isArray(args.blocks))
|
|
2503
2524
|
patch.blocks = args.blocks;
|
|
2504
|
-
const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId
|
|
2525
|
+
const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId, {
|
|
2526
|
+
markEdited: false
|
|
2527
|
+
});
|
|
2505
2528
|
return { channel: channel.id, ts, message: wireMessage(message) };
|
|
2506
2529
|
},
|
|
2507
2530
|
"chat.getPermalink": (ctx, args) => {
|
|
@@ -2543,7 +2566,7 @@ var handlers = {
|
|
|
2543
2566
|
"conversations.create": (ctx, args) => {
|
|
2544
2567
|
const name = required(args, "name");
|
|
2545
2568
|
if (!/^[a-z0-9_-]{1,80}$/.test(name))
|
|
2546
|
-
throw new
|
|
2569
|
+
throw new SlackApiError2("invalid_name_specials");
|
|
2547
2570
|
const channel = ctx.store.addChannel({
|
|
2548
2571
|
name,
|
|
2549
2572
|
is_private: bool(args, "is_private") ?? false,
|
|
@@ -2554,7 +2577,7 @@ var handlers = {
|
|
|
2554
2577
|
"conversations.join": (ctx, args) => {
|
|
2555
2578
|
const channel = ctx.store.channel(required(args, "channel"));
|
|
2556
2579
|
if (channel.is_private)
|
|
2557
|
-
throw new
|
|
2580
|
+
throw new SlackApiError2("method_not_supported_for_channel_type");
|
|
2558
2581
|
const { alreadyMember } = ctx.store.join(channel.id, ctx.actor.userId);
|
|
2559
2582
|
return {
|
|
2560
2583
|
channel: channelInfo(ctx.store, channel, ctx.actor),
|
|
@@ -2575,7 +2598,7 @@ var handlers = {
|
|
|
2575
2598
|
errors.push({ user: u, ok: false, error: "already_in_channel" });
|
|
2576
2599
|
}
|
|
2577
2600
|
if (errors.length === users.length)
|
|
2578
|
-
throw new
|
|
2601
|
+
throw new SlackApiError2(String(errors[0].error), { errors });
|
|
2579
2602
|
return {
|
|
2580
2603
|
channel: channelInfo(ctx.store, channel, ctx.actor),
|
|
2581
2604
|
...errors.length ? { errors } : {}
|
|
@@ -2592,7 +2615,7 @@ var handlers = {
|
|
|
2592
2615
|
"conversations.open": (ctx, args) => {
|
|
2593
2616
|
const users = (str(args, "users") ?? "").split(",").map((u) => u.trim()).filter(Boolean);
|
|
2594
2617
|
if (users.length !== 1)
|
|
2595
|
-
throw new
|
|
2618
|
+
throw new SlackApiError2("users_list_not_supplied");
|
|
2596
2619
|
const dm = ctx.store.openDm(users[0]);
|
|
2597
2620
|
return { channel: { id: dm.id, is_im: true, user: dm.user, created: dm.created } };
|
|
2598
2621
|
},
|
|
@@ -2618,7 +2641,7 @@ var handlers = {
|
|
|
2618
2641
|
requireMember(channel, ctx.actor);
|
|
2619
2642
|
const ts = required(args, "ts");
|
|
2620
2643
|
if (!ctx.store.findMessage(channel.id, ts))
|
|
2621
|
-
throw new
|
|
2644
|
+
throw new SlackApiError2("thread_not_found");
|
|
2622
2645
|
const p = ctx.store.replies(channel.id, ts, {
|
|
2623
2646
|
oldest: str(args, "oldest"),
|
|
2624
2647
|
latest: str(args, "latest"),
|
|
@@ -2640,7 +2663,7 @@ var handlers = {
|
|
|
2640
2663
|
"users.lookupByEmail": (ctx, args) => {
|
|
2641
2664
|
const user = ctx.store.userByEmail(required(args, "email"));
|
|
2642
2665
|
if (!user)
|
|
2643
|
-
throw new
|
|
2666
|
+
throw new SlackApiError2("users_not_found");
|
|
2644
2667
|
return { user };
|
|
2645
2668
|
},
|
|
2646
2669
|
"users.conversations": (ctx, args) => {
|
|
@@ -2674,7 +2697,7 @@ var handlers = {
|
|
|
2674
2697
|
const filename = required(args, "filename");
|
|
2675
2698
|
const length = num(args, "length", -1);
|
|
2676
2699
|
if (length < 0)
|
|
2677
|
-
throw new
|
|
2700
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2678
2701
|
response_metadata: { messages: ["[ERROR] missing required field: length"] }
|
|
2679
2702
|
});
|
|
2680
2703
|
const id = slackId("F");
|
|
@@ -2689,7 +2712,7 @@ var handlers = {
|
|
|
2689
2712
|
"files.completeUploadExternal": (ctx, args) => {
|
|
2690
2713
|
const list = Array.isArray(args.files) ? args.files : [];
|
|
2691
2714
|
if (list.length === 0)
|
|
2692
|
-
throw new
|
|
2715
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2693
2716
|
response_metadata: { messages: ["[ERROR] missing required field: files"] }
|
|
2694
2717
|
});
|
|
2695
2718
|
const files = list.map(({ id, title }) => {
|
|
@@ -2727,13 +2750,13 @@ var handlers = {
|
|
|
2727
2750
|
const triggerId = required(args, "trigger_id");
|
|
2728
2751
|
const expires = ctx.triggerIds.get(triggerId);
|
|
2729
2752
|
if (expires === undefined)
|
|
2730
|
-
throw new
|
|
2753
|
+
throw new SlackApiError2("invalid_trigger_id");
|
|
2731
2754
|
ctx.triggerIds.delete(triggerId);
|
|
2732
2755
|
if (Date.now() > expires)
|
|
2733
|
-
throw new
|
|
2756
|
+
throw new SlackApiError2("expired_trigger_id");
|
|
2734
2757
|
const view = viewArg(args);
|
|
2735
2758
|
if (view.type !== "modal")
|
|
2736
|
-
throw new
|
|
2759
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2737
2760
|
response_metadata: { messages: ["[ERROR] view type must be modal"] }
|
|
2738
2761
|
});
|
|
2739
2762
|
const stored = ctx.store.openView({
|
|
@@ -2757,7 +2780,7 @@ var handlers = {
|
|
|
2757
2780
|
const id = required(args, "view_id");
|
|
2758
2781
|
const existing = ctx.store.views.get(id);
|
|
2759
2782
|
if (!existing)
|
|
2760
|
-
throw new
|
|
2783
|
+
throw new SlackApiError2("not_found");
|
|
2761
2784
|
const view = viewArg(args);
|
|
2762
2785
|
Object.assign(existing, {
|
|
2763
2786
|
callback_id: str(view, "callback_id") ?? existing.callback_id,
|
|
@@ -2838,14 +2861,14 @@ function viewArg(args) {
|
|
|
2838
2861
|
try {
|
|
2839
2862
|
return JSON.parse(v);
|
|
2840
2863
|
} catch {
|
|
2841
|
-
throw new
|
|
2864
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2842
2865
|
response_metadata: { messages: ["[ERROR] view must be valid JSON"] }
|
|
2843
2866
|
});
|
|
2844
2867
|
}
|
|
2845
2868
|
}
|
|
2846
2869
|
if (v && typeof v === "object")
|
|
2847
2870
|
return v;
|
|
2848
|
-
throw new
|
|
2871
|
+
throw new SlackApiError2("invalid_arguments", {
|
|
2849
2872
|
response_metadata: { messages: ["[ERROR] missing required field: view"] }
|
|
2850
2873
|
});
|
|
2851
2874
|
}
|
|
@@ -2866,6 +2889,12 @@ function newTriggerId() {
|
|
|
2866
2889
|
}
|
|
2867
2890
|
|
|
2868
2891
|
// src/server.ts
|
|
2892
|
+
function loadManifest(manifest) {
|
|
2893
|
+
return typeof manifest === "string" ? JSON.parse(readFileSync2(manifest, "utf8")) : manifest;
|
|
2894
|
+
}
|
|
2895
|
+
function manifestAppName(manifest) {
|
|
2896
|
+
return manifest?.features?.bot_user?.display_name ?? manifest?.display_information?.name;
|
|
2897
|
+
}
|
|
2869
2898
|
var DEFAULT_EVENTS = [
|
|
2870
2899
|
"message.channels",
|
|
2871
2900
|
"message.groups",
|
|
@@ -2879,7 +2908,7 @@ var DEFAULT_EVENTS = [
|
|
|
2879
2908
|
"member_joined_channel"
|
|
2880
2909
|
];
|
|
2881
2910
|
|
|
2882
|
-
class
|
|
2911
|
+
class SlackMock2 {
|
|
2883
2912
|
store;
|
|
2884
2913
|
hub;
|
|
2885
2914
|
opts;
|
|
@@ -2893,15 +2922,15 @@ class SlackMock {
|
|
|
2893
2922
|
faults = [];
|
|
2894
2923
|
lastRequestAt = 0;
|
|
2895
2924
|
log;
|
|
2896
|
-
static Error =
|
|
2925
|
+
static Error = SlackApiError2;
|
|
2897
2926
|
constructor(opts = {}) {
|
|
2898
|
-
const manifest =
|
|
2899
|
-
const appName = opts.appName ?? manifest
|
|
2927
|
+
const manifest = loadManifest(opts.manifest);
|
|
2928
|
+
const appName = opts.appName ?? manifestAppName(manifest);
|
|
2900
2929
|
this.opts = { ...opts, appName };
|
|
2901
2930
|
this.log = typeof opts.log === "function" ? opts.log : opts.log ? (m) => console.log(`[slack-mock] ${m}`) : () => {};
|
|
2902
2931
|
if (opts.dataFile)
|
|
2903
2932
|
ensureDir(opts.dataFile);
|
|
2904
|
-
this.store = new
|
|
2933
|
+
this.store = new Store2({ ...opts, appName });
|
|
2905
2934
|
this.hub = new SocketModeHub({
|
|
2906
2935
|
appId: this.store.app.id,
|
|
2907
2936
|
pingIntervalMs: opts.pingIntervalMs,
|
|
@@ -2917,7 +2946,7 @@ class SlackMock {
|
|
|
2917
2946
|
this.store.onChange((change) => this.dispatch(change));
|
|
2918
2947
|
}
|
|
2919
2948
|
static async start(opts = {}) {
|
|
2920
|
-
const mock = new
|
|
2949
|
+
const mock = new SlackMock2(opts);
|
|
2921
2950
|
mock.listen();
|
|
2922
2951
|
return mock;
|
|
2923
2952
|
}
|
|
@@ -3140,7 +3169,7 @@ class SlackMock {
|
|
|
3140
3169
|
record(true);
|
|
3141
3170
|
return Response.json({ ok: true, ...body });
|
|
3142
3171
|
} catch (e) {
|
|
3143
|
-
if (e instanceof
|
|
3172
|
+
if (e instanceof SlackApiError2) {
|
|
3144
3173
|
record(false, e.code);
|
|
3145
3174
|
this.log(`${method} -> ${e.code}`);
|
|
3146
3175
|
return slackError(e.code, e.extra);
|
|
@@ -3222,7 +3251,7 @@ class SlackMock {
|
|
|
3222
3251
|
return new Response("ok");
|
|
3223
3252
|
}
|
|
3224
3253
|
} catch (e) {
|
|
3225
|
-
if (e instanceof
|
|
3254
|
+
if (e instanceof SlackApiError2)
|
|
3226
3255
|
return Response.json({ ok: false, error: e.code });
|
|
3227
3256
|
throw e;
|
|
3228
3257
|
}
|
|
@@ -3442,7 +3471,7 @@ data: ${JSON.stringify(data)}
|
|
|
3442
3471
|
for (const u of this.store.users.values())
|
|
3443
3472
|
if (u.name === idOrName)
|
|
3444
3473
|
return u;
|
|
3445
|
-
throw new
|
|
3474
|
+
throw new SlackApiError2("user_not_found");
|
|
3446
3475
|
}
|
|
3447
3476
|
channel(idOrName) {
|
|
3448
3477
|
return this.resolveChannel(idOrName);
|
|
@@ -3563,7 +3592,7 @@ data: ${JSON.stringify(data)}
|
|
|
3563
3592
|
const channel = this.resolveChannel(input.channel);
|
|
3564
3593
|
const message = this.store.findMessage(channel.id, input.ts) ?? this.store.ephemerals.find((m) => m.channel === channel.id && m.ts === input.ts);
|
|
3565
3594
|
if (!message)
|
|
3566
|
-
throw new
|
|
3595
|
+
throw new SlackApiError2("message_not_found");
|
|
3567
3596
|
const found = findButton(message.blocks, input.action_id);
|
|
3568
3597
|
if (!found && !input.value && !input.block_id)
|
|
3569
3598
|
throw new Error(`no button with action_id ${input.action_id} on message ${input.ts}`);
|
|
@@ -3760,7 +3789,7 @@ data: ${JSON.stringify(data)}
|
|
|
3760
3789
|
return this.store.onChange(listener);
|
|
3761
3790
|
}
|
|
3762
3791
|
async handleAdmin(req, path, url) {
|
|
3763
|
-
const json = (
|
|
3792
|
+
const json = (body, status = 200) => Response.json(body, { status });
|
|
3764
3793
|
const body = req.method === "POST" ? await req.json().catch(() => ({})) : {};
|
|
3765
3794
|
try {
|
|
3766
3795
|
if (path === "presenter")
|
|
@@ -3812,7 +3841,7 @@ data: ${JSON.stringify(data)}
|
|
|
3812
3841
|
return json((m[2] ? this.thread(m[1], m[2]) : this.messages(m[1])).map(wireMessage));
|
|
3813
3842
|
return json({ ok: false, error: "unknown_admin_route" }, 404);
|
|
3814
3843
|
} catch (e) {
|
|
3815
|
-
return json({ ok: false, error: e instanceof
|
|
3844
|
+
return json({ ok: false, error: e instanceof SlackApiError2 ? e.code : String(e) }, 400);
|
|
3816
3845
|
}
|
|
3817
3846
|
}
|
|
3818
3847
|
}
|
|
@@ -3864,4 +3893,69 @@ function denied(req, path) {
|
|
|
3864
3893
|
});
|
|
3865
3894
|
}
|
|
3866
3895
|
|
|
3867
|
-
|
|
3896
|
+
// src/frames.ts
|
|
3897
|
+
import { copyFileSync, mkdirSync as mkdirSync2, mkdtempSync, readFileSync as readFileSync3, rmSync, writeFileSync } from "fs";
|
|
3898
|
+
import { tmpdir } from "os";
|
|
3899
|
+
import { join, resolve } from "path";
|
|
3900
|
+
var FRAME_KINDS = new Set([
|
|
3901
|
+
"message.add",
|
|
3902
|
+
"message.update",
|
|
3903
|
+
"message.delete",
|
|
3904
|
+
"reaction.add",
|
|
3905
|
+
"reaction.remove",
|
|
3906
|
+
"file.add"
|
|
3907
|
+
]);
|
|
3908
|
+
function selectFrames(entries, channel, thread) {
|
|
3909
|
+
return entries.filter(({ change }) => {
|
|
3910
|
+
if (!FRAME_KINDS.has(change.kind) || !("message" in change))
|
|
3911
|
+
return false;
|
|
3912
|
+
const m = change.message;
|
|
3913
|
+
if (m.channel !== channel)
|
|
3914
|
+
return false;
|
|
3915
|
+
return thread ? m.ts === thread || m.thread_ts === thread : true;
|
|
3916
|
+
});
|
|
3917
|
+
}
|
|
3918
|
+
async function frames2(opts) {
|
|
3919
|
+
const entries = parseJournal(readFileSync3(opts.journal, "utf8"), opts.journal);
|
|
3920
|
+
const appName = manifestAppName(loadManifest(opts.manifest));
|
|
3921
|
+
const storeAfter = (line) => new Store2({ replay: entries.filter((e) => e.line <= line).map((e) => e.change), appName });
|
|
3922
|
+
const full = storeAfter(Number.POSITIVE_INFINITY);
|
|
3923
|
+
const channel = full.channels.get(opts.channel) ?? full.channelByName(opts.channel);
|
|
3924
|
+
if (!channel)
|
|
3925
|
+
throw new Error(`channel ${opts.channel} not found in ${opts.journal}`);
|
|
3926
|
+
const what = opts.thread ? `thread ${opts.thread} in ${channel.id}` : `channel ${channel.id}`;
|
|
3927
|
+
const selected = selectFrames(entries, channel.id, opts.thread);
|
|
3928
|
+
if (!selected.length)
|
|
3929
|
+
throw new Error(`no line of ${opts.journal} touches ${what}`);
|
|
3930
|
+
requireChrome2();
|
|
3931
|
+
const view = opts.thread ? { kind: "thread", channel: channel.id, ts: opts.thread } : { kind: "channel", channel: channel.id };
|
|
3932
|
+
const out = resolve(opts.out);
|
|
3933
|
+
mkdirSync2(out, { recursive: true });
|
|
3934
|
+
const tmp = mkdtempSync(join(tmpdir(), "slack-mock-frames-"));
|
|
3935
|
+
const html = join(tmp, "frame.html");
|
|
3936
|
+
const shoot = async (store, file, render, size) => {
|
|
3937
|
+
writeFileSync(html, renderPage(store, view, render));
|
|
3938
|
+
await screenshot2(Bun.pathToFileURL(html).href, { out: file, ...size });
|
|
3939
|
+
};
|
|
3940
|
+
try {
|
|
3941
|
+
const frames = [];
|
|
3942
|
+
const size = { width: opts.width ?? 800, height: opts.height ?? 700 };
|
|
3943
|
+
for (const [i, { line, change }] of selected.entries()) {
|
|
3944
|
+
const path = join(out, `${String(i + 1).padStart(2, "0")}-${change.kind}.png`);
|
|
3945
|
+
await shoot(storeAfter(line), path, { screenshot: true }, size);
|
|
3946
|
+
frames.push({ index: line, kind: change.kind, path });
|
|
3947
|
+
}
|
|
3948
|
+
const finalThread = join(out, "final-thread.png");
|
|
3949
|
+
copyFileSync(frames[frames.length - 1].path, finalThread);
|
|
3950
|
+
let finalDesktop;
|
|
3951
|
+
if (opts.desktop !== false) {
|
|
3952
|
+
finalDesktop = join(out, "final-desktop.png");
|
|
3953
|
+
await shoot(full, finalDesktop, { screenshot: false, live: false }, { width: 1280, height: 900 });
|
|
3954
|
+
}
|
|
3955
|
+
return { frames, finalThread, finalDesktop };
|
|
3956
|
+
} finally {
|
|
3957
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
3958
|
+
}
|
|
3959
|
+
}
|
|
3960
|
+
|
|
3961
|
+
export { SlackApiError2, Store2, SlackMock2, frames2 };
|
|
@@ -10,17 +10,21 @@ var CHROME_CANDIDATES = [
|
|
|
10
10
|
"/usr/bin/chromium",
|
|
11
11
|
"/usr/bin/chromium-browser"
|
|
12
12
|
];
|
|
13
|
-
function
|
|
13
|
+
function findChrome2() {
|
|
14
14
|
for (const c of CHROME_CANDIDATES)
|
|
15
15
|
if (c && existsSync(c))
|
|
16
16
|
return c;
|
|
17
17
|
const fromPath = Bun.which("google-chrome") ?? Bun.which("chromium") ?? Bun.which("chrome");
|
|
18
18
|
return fromPath ?? undefined;
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
const chrome =
|
|
20
|
+
function requireChrome2() {
|
|
21
|
+
const chrome = findChrome2();
|
|
22
22
|
if (!chrome)
|
|
23
23
|
throw new Error("no Chrome/Chromium binary found; set SLACK_MOCK_CHROME=/path/to/chrome");
|
|
24
|
+
return chrome;
|
|
25
|
+
}
|
|
26
|
+
async function screenshot2(url, opts) {
|
|
27
|
+
const chrome = requireChrome2();
|
|
24
28
|
const target = new URL(url);
|
|
25
29
|
if (!target.searchParams.has("screenshot"))
|
|
26
30
|
target.searchParams.set("screenshot", "1");
|
|
@@ -45,4 +49,4 @@ async function screenshot(url, opts) {
|
|
|
45
49
|
return opts.out;
|
|
46
50
|
}
|
|
47
51
|
|
|
48
|
-
export {
|
|
52
|
+
export { findChrome2, requireChrome2, screenshot2 };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export type { Frame, FramesOptions, FramesResult } from "./frames.ts";
|
|
2
|
+
export { frames } from "./frames.ts";
|
|
1
3
|
export { findChrome, screenshot } from "./screenshot.ts";
|
|
2
4
|
export type { Fault, MessageQuery, PostMessageInput, SlackManifest, SlackMockOptions, } from "./server.ts";
|
|
3
5
|
export { SlackApiError, SlackMock } from "./server.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
SlackApiError2,
|
|
4
|
+
Store2,
|
|
5
|
+
SlackMock2,
|
|
6
|
+
frames2
|
|
7
|
+
} from "./index-6c88z809.js";
|
|
7
8
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from "./index-
|
|
9
|
+
findChrome2,
|
|
10
|
+
screenshot2
|
|
11
|
+
} from "./index-v2hesj4s.js";
|
|
11
12
|
export {
|
|
12
|
-
SlackApiError,
|
|
13
|
-
SlackMock,
|
|
14
|
-
Store,
|
|
15
|
-
findChrome,
|
|
16
|
-
|
|
13
|
+
SlackApiError2 as SlackApiError,
|
|
14
|
+
SlackMock2 as SlackMock,
|
|
15
|
+
Store2 as Store,
|
|
16
|
+
findChrome2 as findChrome,
|
|
17
|
+
frames2 as frames,
|
|
18
|
+
screenshot2 as screenshot
|
|
17
19
|
};
|