refactor: share tlon channel put requests

This commit is contained in:
Peter Steinberger 2026-03-13 16:46:51 +00:00
parent e351a86290
commit acfb95e2c6
1 changed files with 36 additions and 55 deletions

View File

@ -12,6 +12,29 @@ export type UrbitChannelDeps = {
fetchImpl?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>; fetchImpl?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
}; };
async function putUrbitChannel(
deps: UrbitChannelDeps,
params: { body: unknown; auditContext: string },
) {
return await urbitFetch({
baseUrl: deps.baseUrl,
path: `/~/channel/${deps.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: deps.cookie,
},
body: JSON.stringify(params.body),
},
ssrfPolicy: deps.ssrfPolicy,
lookupFn: deps.lookupFn,
fetchImpl: deps.fetchImpl,
timeoutMs: 30_000,
auditContext: params.auditContext,
});
}
export async function pokeUrbitChannel( export async function pokeUrbitChannel(
deps: UrbitChannelDeps, deps: UrbitChannelDeps,
params: { app: string; mark: string; json: unknown; auditContext: string }, params: { app: string; mark: string; json: unknown; auditContext: string },
@ -26,21 +49,8 @@ export async function pokeUrbitChannel(
json: params.json, json: params.json,
}; };
const { response, release } = await urbitFetch({ const { response, release } = await putUrbitChannel(deps, {
baseUrl: deps.baseUrl, body: [pokeData],
path: `/~/channel/${deps.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: deps.cookie,
},
body: JSON.stringify([pokeData]),
},
ssrfPolicy: deps.ssrfPolicy,
lookupFn: deps.lookupFn,
fetchImpl: deps.fetchImpl,
timeoutMs: 30_000,
auditContext: params.auditContext, auditContext: params.auditContext,
}); });
@ -88,23 +98,7 @@ export async function createUrbitChannel(
deps: UrbitChannelDeps, deps: UrbitChannelDeps,
params: { body: unknown; auditContext: string }, params: { body: unknown; auditContext: string },
): Promise<void> { ): Promise<void> {
const { response, release } = await urbitFetch({ const { response, release } = await putUrbitChannel(deps, params);
baseUrl: deps.baseUrl,
path: `/~/channel/${deps.channelId}`,
init: {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: deps.cookie,
},
body: JSON.stringify(params.body),
},
ssrfPolicy: deps.ssrfPolicy,
lookupFn: deps.lookupFn,
fetchImpl: deps.fetchImpl,
timeoutMs: 30_000,
auditContext: params.auditContext,
});
try { try {
if (!response.ok && response.status !== 204) { if (!response.ok && response.status !== 204) {
@ -116,30 +110,17 @@ export async function createUrbitChannel(
} }
export async function wakeUrbitChannel(deps: UrbitChannelDeps): Promise<void> { export async function wakeUrbitChannel(deps: UrbitChannelDeps): Promise<void> {
const { response, release } = await urbitFetch({ const { response, release } = await putUrbitChannel(deps, {
baseUrl: deps.baseUrl, body: [
path: `/~/channel/${deps.channelId}`, {
init: { id: Date.now(),
method: "PUT", action: "poke",
headers: { ship: deps.ship,
"Content-Type": "application/json", app: "hood",
Cookie: deps.cookie, mark: "helm-hi",
json: "Opening API channel",
}, },
body: JSON.stringify([ ],
{
id: Date.now(),
action: "poke",
ship: deps.ship,
app: "hood",
mark: "helm-hi",
json: "Opening API channel",
},
]),
},
ssrfPolicy: deps.ssrfPolicy,
lookupFn: deps.lookupFn,
fetchImpl: deps.fetchImpl,
timeoutMs: 30_000,
auditContext: "tlon-urbit-channel-wake", auditContext: "tlon-urbit-channel-wake",
}); });