@desplega.ai/slack-mock 0.3.0 → 0.4.1
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 +22 -3
- package/dist/cli.js +2 -2
- package/dist/{index-fhjq5qns.js → index-86bw4z6k.js} +2278 -2056
- package/dist/index-xs37gskt.js +156 -0
- package/dist/index.js +2 -2
- package/dist/screenshot.js +1 -1
- package/dist/server.d.ts +11 -4
- package/dist/store.d.ts +1 -0
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/render/index.ts +269 -44
- package/src/screenshot.ts +116 -1
- package/src/server.ts +29 -6
- package/src/store.ts +2 -0
- package/src/types.ts +1 -0
- package/src/web-api.ts +17 -2
- package/dist/index-v2hesj4s.js +0 -52
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/screenshot.ts
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
var CHROME_CANDIDATES = [
|
|
5
|
+
process.env.SLACK_MOCK_CHROME,
|
|
6
|
+
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
|
7
|
+
"/Applications/Chromium.app/Contents/MacOS/Chromium",
|
|
8
|
+
"/usr/bin/google-chrome",
|
|
9
|
+
"/usr/bin/google-chrome-stable",
|
|
10
|
+
"/usr/bin/chromium",
|
|
11
|
+
"/usr/bin/chromium-browser"
|
|
12
|
+
];
|
|
13
|
+
function findChrome2() {
|
|
14
|
+
for (const c of CHROME_CANDIDATES)
|
|
15
|
+
if (c && existsSync(c))
|
|
16
|
+
return c;
|
|
17
|
+
const fromPath = Bun.which("google-chrome") ?? Bun.which("chromium") ?? Bun.which("chrome");
|
|
18
|
+
return fromPath ?? undefined;
|
|
19
|
+
}
|
|
20
|
+
function requireChrome2() {
|
|
21
|
+
const chrome = findChrome2();
|
|
22
|
+
if (!chrome)
|
|
23
|
+
throw new Error("no Chrome/Chromium binary found; set SLACK_MOCK_CHROME=/path/to/chrome");
|
|
24
|
+
return chrome;
|
|
25
|
+
}
|
|
26
|
+
async function screenshotWithCdp(chrome, target, opts, width, height) {
|
|
27
|
+
const proc = Bun.spawn([
|
|
28
|
+
chrome,
|
|
29
|
+
"--headless=new",
|
|
30
|
+
"--disable-gpu",
|
|
31
|
+
"--hide-scrollbars",
|
|
32
|
+
"--no-first-run",
|
|
33
|
+
"--no-default-browser-check",
|
|
34
|
+
"--remote-debugging-port=0",
|
|
35
|
+
"about:blank"
|
|
36
|
+
], { stdout: "ignore", stderr: "pipe" });
|
|
37
|
+
const timer = setTimeout(() => proc.kill(), opts.timeoutMs ?? 30000);
|
|
38
|
+
let socket;
|
|
39
|
+
const reader = proc.stderr.getReader();
|
|
40
|
+
try {
|
|
41
|
+
const decoder = new TextDecoder;
|
|
42
|
+
let stderr = "";
|
|
43
|
+
let endpoint = null;
|
|
44
|
+
while (!endpoint) {
|
|
45
|
+
const { value, done } = await reader.read();
|
|
46
|
+
if (done)
|
|
47
|
+
throw new Error("Chrome closed before opening DevTools");
|
|
48
|
+
stderr += decoder.decode(value, { stream: true });
|
|
49
|
+
endpoint = /DevTools listening on (ws:\/\/[^\s]+)/.exec(stderr);
|
|
50
|
+
}
|
|
51
|
+
socket = new WebSocket(endpoint[1]);
|
|
52
|
+
await new Promise((resolve, reject) => {
|
|
53
|
+
socket.addEventListener("open", () => resolve(), { once: true });
|
|
54
|
+
socket.addEventListener("error", () => reject(new Error("Cannot connect to Chrome DevTools")), { once: true });
|
|
55
|
+
socket.addEventListener("close", () => reject(new Error("Chrome DevTools closed")), {
|
|
56
|
+
once: true
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
let nextId = 0;
|
|
60
|
+
let sessionId;
|
|
61
|
+
const pending = new Map;
|
|
62
|
+
let pageLoaded = () => {};
|
|
63
|
+
let loadFailed = (_error) => {};
|
|
64
|
+
socket.onmessage = (event) => {
|
|
65
|
+
const message = JSON.parse(String(event.data));
|
|
66
|
+
if (message.method === "Page.loadEventFired" && message.sessionId === sessionId)
|
|
67
|
+
pageLoaded();
|
|
68
|
+
const request = pending.get(message.id);
|
|
69
|
+
if (!request)
|
|
70
|
+
return;
|
|
71
|
+
pending.delete(message.id);
|
|
72
|
+
if (message.error)
|
|
73
|
+
request.reject(new Error(message.error.message));
|
|
74
|
+
else
|
|
75
|
+
request.resolve(message.result ?? {});
|
|
76
|
+
};
|
|
77
|
+
socket.onclose = socket.onerror = () => {
|
|
78
|
+
const error = new Error("Chrome DevTools closed before capture completed");
|
|
79
|
+
for (const request of pending.values())
|
|
80
|
+
request.reject(error);
|
|
81
|
+
pending.clear();
|
|
82
|
+
loadFailed(error);
|
|
83
|
+
};
|
|
84
|
+
const command = (method, params = {}) => {
|
|
85
|
+
const id = ++nextId;
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
pending.set(id, { resolve, reject });
|
|
88
|
+
socket.send(JSON.stringify({ id, method, params, sessionId }));
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
const { targetId } = await command("Target.createTarget", { url: "about:blank" });
|
|
92
|
+
const attached = await command("Target.attachToTarget", { targetId, flatten: true });
|
|
93
|
+
if (typeof attached.sessionId !== "string")
|
|
94
|
+
throw new Error("Chrome returned no page session");
|
|
95
|
+
sessionId = attached.sessionId;
|
|
96
|
+
await command("Page.enable");
|
|
97
|
+
await command("Emulation.setDeviceMetricsOverride", {
|
|
98
|
+
width,
|
|
99
|
+
height,
|
|
100
|
+
deviceScaleFactor: 1,
|
|
101
|
+
mobile: false
|
|
102
|
+
});
|
|
103
|
+
const loaded = new Promise((resolve, reject) => {
|
|
104
|
+
pageLoaded = resolve;
|
|
105
|
+
loadFailed = reject;
|
|
106
|
+
});
|
|
107
|
+
await Promise.all([command("Page.navigate", { url: target.toString() }), loaded]);
|
|
108
|
+
await command("Runtime.evaluate", { expression: "document.fonts.ready", awaitPromise: true });
|
|
109
|
+
const { data } = await command("Page.captureScreenshot", {
|
|
110
|
+
format: "png",
|
|
111
|
+
captureBeyondViewport: false
|
|
112
|
+
});
|
|
113
|
+
if (typeof data !== "string")
|
|
114
|
+
throw new Error("Chrome returned no screenshot data");
|
|
115
|
+
await Bun.write(opts.out, Buffer.from(data, "base64"));
|
|
116
|
+
return opts.out;
|
|
117
|
+
} finally {
|
|
118
|
+
socket?.close();
|
|
119
|
+
clearTimeout(timer);
|
|
120
|
+
reader.releaseLock();
|
|
121
|
+
if (proc.exitCode === null)
|
|
122
|
+
proc.kill();
|
|
123
|
+
await proc.exited;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async function screenshot2(url, opts) {
|
|
127
|
+
const chrome = requireChrome2();
|
|
128
|
+
const target = new URL(url);
|
|
129
|
+
if (!target.searchParams.has("screenshot"))
|
|
130
|
+
target.searchParams.set("screenshot", "1");
|
|
131
|
+
const width = opts.width ?? 800;
|
|
132
|
+
const height = opts.height ?? 1000;
|
|
133
|
+
if (width < 500)
|
|
134
|
+
return screenshotWithCdp(chrome, target, opts, width, height);
|
|
135
|
+
const proc = Bun.spawn([
|
|
136
|
+
chrome,
|
|
137
|
+
"--headless=new",
|
|
138
|
+
"--disable-gpu",
|
|
139
|
+
"--hide-scrollbars",
|
|
140
|
+
"--no-first-run",
|
|
141
|
+
"--no-default-browser-check",
|
|
142
|
+
`--window-size=${width},${height}`,
|
|
143
|
+
`--screenshot=${opts.out}`,
|
|
144
|
+
target.toString()
|
|
145
|
+
], { stdout: "ignore", stderr: "pipe" });
|
|
146
|
+
const timer = setTimeout(() => proc.kill(), opts.timeoutMs ?? 30000);
|
|
147
|
+
const code = await proc.exited;
|
|
148
|
+
clearTimeout(timer);
|
|
149
|
+
if (code !== 0 || !existsSync(opts.out)) {
|
|
150
|
+
const err = await new Response(proc.stderr).text();
|
|
151
|
+
throw new Error(`chrome exited with ${code}: ${err.slice(-500)}`);
|
|
152
|
+
}
|
|
153
|
+
return opts.out;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export { findChrome2, requireChrome2, screenshot2 };
|
package/dist/index.js
CHANGED
|
@@ -4,11 +4,11 @@ import {
|
|
|
4
4
|
Store2,
|
|
5
5
|
SlackMock2,
|
|
6
6
|
frames2
|
|
7
|
-
} from "./index-
|
|
7
|
+
} from "./index-86bw4z6k.js";
|
|
8
8
|
import {
|
|
9
9
|
findChrome2,
|
|
10
10
|
screenshot2
|
|
11
|
-
} from "./index-
|
|
11
|
+
} from "./index-xs37gskt.js";
|
|
12
12
|
export {
|
|
13
13
|
SlackApiError2 as SlackApiError,
|
|
14
14
|
SlackMock2 as SlackMock,
|
package/dist/screenshot.js
CHANGED
package/dist/server.d.ts
CHANGED
|
@@ -60,9 +60,10 @@ export interface SlackMockOptions extends StoreOptions {
|
|
|
60
60
|
*/
|
|
61
61
|
publicUi?: boolean;
|
|
62
62
|
/**
|
|
63
|
-
* "user:password" that may
|
|
64
|
-
* (`GET /mock/presenter`). With `publicUi` the HTML
|
|
65
|
-
* can drive a public-read workspace without the
|
|
63
|
+
* "user:password" that may post messages (`POST /mock/messages`), mutate reactions
|
|
64
|
+
* (`POST /mock/reactions`), and check its role (`GET /mock/presenter`). With `publicUi` the HTML
|
|
65
|
+
* composer shows a sign-in for it, so a presenter can drive a public-read workspace without the
|
|
66
|
+
* admin credential. `adminAuth` always implies it.
|
|
66
67
|
*/
|
|
67
68
|
presenterAuth?: string;
|
|
68
69
|
log?: boolean | ((msg: string) => void);
|
|
@@ -133,7 +134,7 @@ export declare class SlackMock {
|
|
|
133
134
|
/** Base URL for links generated while serving `req`: publicUrl, else the request's own host. */
|
|
134
135
|
private publicBase;
|
|
135
136
|
private authorizedAdmin;
|
|
136
|
-
/** `presenterAuth` (or `adminAuth`) may post messages and read its role
|
|
137
|
+
/** `presenterAuth` (or `adminAuth`) may post messages, mutate reactions, and read its role. */
|
|
137
138
|
private authorizedPresenter;
|
|
138
139
|
/** Visitors can read the UI but posting needs a credential: the composer shows a sign-in. */
|
|
139
140
|
private get writeGated();
|
|
@@ -210,6 +211,12 @@ export declare class SlackMock {
|
|
|
210
211
|
name: string;
|
|
211
212
|
user?: string;
|
|
212
213
|
}): Promise<void>;
|
|
214
|
+
removeReaction(input: {
|
|
215
|
+
channel: string;
|
|
216
|
+
ts: string;
|
|
217
|
+
name: string;
|
|
218
|
+
user?: string;
|
|
219
|
+
}): Promise<void>;
|
|
213
220
|
private issueResponseUrl;
|
|
214
221
|
private issueTriggerId;
|
|
215
222
|
/** A human runs a slash command. Resolves with the app's ack payload and any message it produced. */
|
package/dist/store.d.ts
CHANGED
package/dist/types.d.ts
CHANGED