Compare commits

...
Author SHA1 Message Date
vorotamoroz c4419a837c fix: preserve native HTTP body semantics 2026-08-02 04:08:52 +00:00
2 changed files with 47 additions and 11 deletions
+6 -9
View File
@@ -147,14 +147,9 @@ export class ObsidianAPIService extends InjectableAPIService<ObsidianServiceCont
: req instanceof Request && typeof req.method === "string"
? req.method
: "GET";
if (typeof req !== "string") {
if (opts?.body) {
body = typeof opts.body === "string" ? opts.body : await new Response(opts.body).arrayBuffer();
} else if (req.body) {
body = await new Response(req.body).arrayBuffer();
}
} else {
body = opts?.body as string;
const suppliedBody = opts?.body ?? (req instanceof Request ? req.body : undefined);
if (suppliedBody !== undefined && suppliedBody !== null) {
body = typeof suppliedBody === "string" ? suppliedBody : await new Response(suppliedBody).arrayBuffer();
}
const reqHeaders = new Headers(req instanceof Request ? req.headers : {});
@@ -192,7 +187,9 @@ export class ObsidianAPIService extends InjectableAPIService<ObsidianServiceCont
contentType: contentType,
};
const r = await requestUrl({ ...requestParam, throw: false });
return new Response(r.arrayBuffer, {
const responseHasNoBody =
method.toUpperCase() === "HEAD" || r.status === 204 || r.status === 205 || r.status === 304;
return new Response(responseHasNoBody ? null : r.arrayBuffer, {
headers: r.headers,
status: r.status,
statusText: `${r.status}`,
@@ -4,16 +4,17 @@ const mocks = vi.hoisted(() => ({
platform: {
isMobile: false,
},
requestUrl: vi.fn(),
}));
vi.mock("@/deps.ts", () => ({
Platform: mocks.platform,
requestUrl: vi.fn(),
requestUrl: mocks.requestUrl,
}));
vi.mock("@/deps", () => ({
Platform: mocks.platform,
requestUrl: vi.fn(),
requestUrl: mocks.requestUrl,
}));
vi.mock("@/modules/essentialObsidian/APILib/ObsHttpHandler", () => ({
@@ -65,3 +66,41 @@ describe("ObsidianAPIService.showWindowOnRight", () => {
expect(workspace.revealLeaf).toHaveBeenCalledWith(rightLeaf);
});
});
describe("ObsidianAPIService.nativeFetch", () => {
it("normalises a typed-array body when the request URL is a string", async () => {
mocks.requestUrl.mockResolvedValue({
arrayBuffer: new Uint8Array([9, 8, 7]).buffer,
headers: { etag: '"created"' },
status: 201,
});
const source = new Uint8Array([0, 1, 2, 3, 4]);
const body = source.subarray(1, 4);
const response = await createService({}).nativeFetch("http://127.0.0.1:8088/dav/probe.bin", {
body: body as unknown as BodyInit,
headers: { "Content-Type": "application/octet-stream" },
method: "PUT",
});
expect(response.status).toBe(201);
const request = mocks.requestUrl.mock.calls[0][0] as { body?: unknown };
expect(request.body).toBeInstanceOf(ArrayBuffer);
expect([...new Uint8Array(request.body as ArrayBuffer)]).toEqual([1, 2, 3]);
});
it("constructs a bodyless response for a successful DELETE", async () => {
mocks.requestUrl.mockResolvedValue({
arrayBuffer: new ArrayBuffer(0),
headers: {},
status: 204,
});
const response = await createService({}).nativeFetch("http://127.0.0.1:8088/dav/probe.bin", {
method: "DELETE",
});
expect(response.status).toBe(204);
expect(await response.text()).toBe("");
});
});