Skip to content

Commit 7dc09b7

Browse files
committed
fix(slack): resolve startup auth fetch through plugin runtime
1 parent dea7450 commit 7dc09b7

3 files changed

Lines changed: 46 additions & 27 deletions

File tree

‎extensions/slack/src/client-options.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createRequire } from "node:module";
33
import type { RetryOptions, WebClientOptions } from "@slack/web-api";
44
import {
55
addActiveManagedProxyTlsOptions,
6+
resolveFetch,
67
resolveEnvHttpProxyAgentOptions,
78
} from "openclaw/plugin-sdk/fetch-runtime";
89
import type { EnvHttpProxyAgent } from "undici";
@@ -53,9 +54,12 @@ export function resolveSlackProxyDispatcher(): SlackProxyDispatcher | undefined
5354
}
5455
}
5556

56-
function createSlackDispatcherFetch(
57-
dispatcher: SlackProxyDispatcher,
58-
): NonNullable<WebClientOptions["fetch"]> {
57+
function buildSlackFetch(
58+
dispatcher?: SlackProxyDispatcher,
59+
): NonNullable<WebClientOptions["fetch"]> | undefined {
60+
if (!dispatcher) {
61+
return resolveFetch() as NonNullable<WebClientOptions["fetch"]> | undefined;
62+
}
5963
const { fetch: slackFetch } = loadSlackUndiciRuntime();
6064
return ((input: RequestInfo | URL, init?: RequestInit) => {
6165
// Slack Web API invokes this hook with URL/string inputs. The cast only bridges
@@ -76,7 +80,7 @@ function applySlackApiUrlAndProxyOptions(
7680
): void {
7781
const slackApiUrl = options.slackApiUrl ?? resolveSlackApiUrlFromEnv();
7882
if (dispatcher && !options.fetch) {
79-
options.fetch = createSlackDispatcherFetch(dispatcher);
83+
options.fetch = buildSlackFetch(dispatcher);
8084
}
8185
if (slackApiUrl !== undefined) {
8286
options.slackApiUrl = slackApiUrl;
@@ -91,6 +95,7 @@ export function resolveSlackWebClientOptions(
9195
): WebClientOptions {
9296
const resolved: WebClientOptions = Object.assign({}, options);
9397
applySlackApiUrlAndProxyOptions(resolved, dispatcher);
98+
resolved.fetch ??= buildSlackFetch(dispatcher);
9499
resolved.retryConfig ??= SLACK_DEFAULT_RETRY_OPTIONS;
95100
return resolved;
96101
}

‎extensions/slack/src/client.test.ts‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,14 @@ describe("slack web client config", () => {
138138
try {
139139
createSlackReadClient("xoxb-read");
140140

141-
expect(WebClient).toHaveBeenCalledWith("xoxb-read", {
142-
retryConfig: SLACK_DEFAULT_RETRY_OPTIONS,
143-
timeout: 30_000,
144-
});
141+
expect(WebClient).toHaveBeenCalledWith(
142+
"xoxb-read",
143+
expect.objectContaining({
144+
fetch: expect.any(Function),
145+
retryConfig: SLACK_DEFAULT_RETRY_OPTIONS,
146+
timeout: 30_000,
147+
}),
148+
);
145149
} finally {
146150
restoreProxyEnvForTest();
147151
}
@@ -152,10 +156,14 @@ describe("slack web client config", () => {
152156
try {
153157
createSlackReadClient("xoxb-read", { timeout: 60_000 });
154158

155-
expect(WebClient).toHaveBeenCalledWith("xoxb-read", {
156-
retryConfig: SLACK_DEFAULT_RETRY_OPTIONS,
157-
timeout: 60_000,
158-
});
159+
expect(WebClient).toHaveBeenCalledWith(
160+
"xoxb-read",
161+
expect.objectContaining({
162+
fetch: expect.any(Function),
163+
retryConfig: SLACK_DEFAULT_RETRY_OPTIONS,
164+
timeout: 60_000,
165+
}),
166+
);
159167
} finally {
160168
restoreProxyEnvForTest();
161169
}
@@ -250,10 +258,14 @@ describe("slack web client config", () => {
250258
try {
251259
createSlackWebClient("xoxb-test", { timeout: 1234 });
252260

253-
expect(WebClient).toHaveBeenCalledWith("xoxb-test", {
254-
retryConfig: SLACK_DEFAULT_RETRY_OPTIONS,
255-
timeout: 1234,
256-
});
261+
expect(WebClient).toHaveBeenCalledWith(
262+
"xoxb-test",
263+
expect.objectContaining({
264+
fetch: expect.any(Function),
265+
retryConfig: SLACK_DEFAULT_RETRY_OPTIONS,
266+
timeout: 1234,
267+
}),
268+
);
257269
} finally {
258270
restoreProxyEnvForTest();
259271
}
@@ -413,9 +425,9 @@ describe("slack proxy dispatcher", () => {
413425
await dispatcher?.close();
414426
});
415427

416-
it("does not attach a fetch when no proxy env var is configured", () => {
428+
it("attaches the shared fetch when no proxy env var is configured", () => {
417429
expect(resolveSlackProxyDispatcher()).toBeUndefined();
418-
expect(resolveSlackWebClientOptions().fetch).toBeUndefined();
430+
expect(requireFetch(resolveSlackWebClientOptions())).toBeTypeOf("function");
419431
});
420432

421433
it("preserves an explicitly provided fetch", async () => {
@@ -464,6 +476,6 @@ describe("slack proxy dispatcher", () => {
464476
process.env.HTTPS_PROXY = "not-a-valid-url://:::bad";
465477

466478
expect(resolveSlackProxyDispatcher()).toBeUndefined();
467-
expect(resolveSlackWebClientOptions().fetch).toBeUndefined();
479+
expect(requireFetch(resolveSlackWebClientOptions())).toBeTypeOf("function");
468480
});
469481
});

‎extensions/slack/src/client.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ export function createSlackReadClient(token: string, options: WebClientOptions =
4040
return new WebClient(token, resolveSlackReadClientOptions(options));
4141
}
4242

43-
function createSlackStartupAuthFetch(fetch: SlackFetch): SlackFetch {
43+
function createSlackStartupAuthFetch(baseFetch: SlackFetch): SlackFetch {
4444
const deadline = Date.now() + SLACK_STARTUP_AUTH_RETRY_BUDGET_MS;
4545
return async (input, init) => {
46-
const response = await fetch(input, init);
46+
const response = await baseFetch(input, init);
4747
if (response.status !== 429) {
4848
return response;
4949
}
@@ -62,12 +62,14 @@ function createSlackStartupAuthFetch(fetch: SlackFetch): SlackFetch {
6262
}
6363

6464
export function createSlackStartupAuthClient(token: string, options: WebClientOptions = {}) {
65-
const fetch = createSlackStartupAuthFetch(
66-
options.fetch ?? (globalThis.fetch as NonNullable<WebClientOptions["fetch"]>),
67-
);
68-
return createSlackWebClient(token, {
69-
...options,
70-
fetch,
65+
const resolvedOptions = resolveSlackWebClientOptions(options);
66+
const baseFetch = resolvedOptions.fetch;
67+
if (!baseFetch) {
68+
throw new Error("Slack startup auth fetch is unavailable");
69+
}
70+
return new WebClient(token, {
71+
...resolvedOptions,
72+
fetch: createSlackStartupAuthFetch(baseFetch),
7173
retryConfig: {
7274
...SLACK_DEFAULT_RETRY_OPTIONS,
7375
maxRetryTime: SLACK_STARTUP_AUTH_RETRY_BUDGET_MS,

0 commit comments

Comments
 (0)