change order of headers to try to make mastodon happy, and handle
redirects.
This commit is contained in:
parent
a680d293c8
commit
563df3018c
|
@ -210,6 +210,8 @@ export const sendAccept = async (user: User, followId: string, follower: string,
|
||||||
console.log("response status:", result.status);
|
console.log("response status:", result.status);
|
||||||
const response = await streamToString(result.body as ReadableStream);
|
const response = await streamToString(result.body as ReadableStream);
|
||||||
console.log("Response body:", response);
|
console.log("Response body:", response);
|
||||||
|
|
||||||
|
if (result.status >= 400) throw "bad http status code";
|
||||||
};
|
};
|
||||||
|
|
||||||
export const sendAll = async (keyId: string, privateKey: string, activity: Record<string, any> | string, inboxes: string[]) => {
|
export const sendAll = async (keyId: string, privateKey: string, activity: Record<string, any> | string, inboxes: string[]) => {
|
||||||
|
|
36
src/net.ts
36
src/net.ts
|
@ -18,10 +18,10 @@ export interface SignedInit {
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: Handle redirects.
|
// TODO: Handle redirects.
|
||||||
export const signedFetch = async (url: string, init: RequestInit, signedInit: SignedInit) => {
|
export const signedFetch = async (url: string, init: RequestInit, signedInit: SignedInit): Promise<globalThis.Response> => {
|
||||||
const signedHeaders: HeadersInit = [
|
const signedHeaders: HeadersInit = [
|
||||||
["Date", new Date().toUTCString()],
|
|
||||||
["Host", new URL(url).host],
|
["Host", new URL(url).host],
|
||||||
|
["Date", new Date().toUTCString()]
|
||||||
];
|
];
|
||||||
|
|
||||||
const headerNames = ["(request-target)", "host", "date"];
|
const headerNames = ["(request-target)", "host", "date"];
|
||||||
|
@ -57,33 +57,39 @@ export const signedFetch = async (url: string, init: RequestInit, signedInit: Si
|
||||||
|
|
||||||
const newHeaders = new Headers(
|
const newHeaders = new Headers(
|
||||||
[
|
[
|
||||||
|
...signedHeaders,
|
||||||
["Signature", signature],
|
["Signature", signature],
|
||||||
["Content-Type", `application/ld+json; profile="${CONTEXT}"`]
|
["Content-Type", `application/ld+json; profile="${CONTEXT}"`]
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (Array.isArray(init.headers) || !init.headers) {
|
if (Array.isArray(init.headers))
|
||||||
for (const header of (init.headers || [])) {
|
init.headers
|
||||||
if (Array.isArray(header))
|
.filter((header) => Array.isArray(header))
|
||||||
newHeaders.set(header[0], header[1]);
|
.forEach(([key, value]) => newHeaders.set(key, value));
|
||||||
else throw "unsupported headers type"; // Lazy.
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const [key, value] of signedHeaders) {
|
|
||||||
newHeaders.set(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else throw "unsupported headers type"; // Lazy.
|
|
||||||
|
|
||||||
console.log("Headers:");
|
console.log("Headers:");
|
||||||
for (const [key, value] of newHeaders) {
|
for (const [key, value] of newHeaders) {
|
||||||
console.log(`${key}: ${value}`);
|
console.log(`${key}: ${value}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(url, {
|
const result = await fetch(url, {
|
||||||
...init,
|
...init,
|
||||||
headers: newHeaders
|
headers: newHeaders
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (result.status >= 300 && result.status <= 399) {
|
||||||
|
const newUrl = result.headers.get("location");
|
||||||
|
if (newUrl) {
|
||||||
|
return await signedFetch(newUrl, init, {
|
||||||
|
// Omit premade digest since url changed, it is invalid.
|
||||||
|
privateKey: signedInit.privateKey,
|
||||||
|
keyId: signedInit.keyId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else throw "signed fetch redirect without location header";
|
||||||
|
}
|
||||||
|
else return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getActor = async (actorUrl: string, signedInit: SignedInit): Promise<Actor | null> => {
|
export const getActor = async (actorUrl: string, signedInit: SignedInit): Promise<Actor | null> => {
|
||||||
|
|
Loading…
Reference in New Issue