Skip to content

Commit ec6272a

Browse files
authored
Fix outgoing messages for rust-crypto (#3025)
It turns out that MatrixClient uses a `FetchHttpApi` instance with `opts.onlyData = true`, so it was returning the json-parsed response rather than the raw response. Change the way we call `authedRequest` so that we get the raw body back.
1 parent 695b773 commit ec6272a

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

spec/unit/rust-crypto.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import MockHttpBackend from "matrix-mock-request";
3030

3131
import { RustCrypto } from "../../src/rust-crypto/rust-crypto";
3232
import { initRustCrypto } from "../../src/rust-crypto";
33-
import { HttpApiEvent, HttpApiEventHandlerMap, IHttpOpts, IToDeviceEvent, MatrixHttpApi } from "../../src";
33+
import { HttpApiEvent, HttpApiEventHandlerMap, IToDeviceEvent, MatrixClient, MatrixHttpApi } from "../../src";
3434
import { TypedEventEmitter } from "../../src/models/typed-event-emitter";
3535

3636
afterEach(() => {
@@ -48,7 +48,7 @@ describe("RustCrypto", () => {
4848
let rustCrypto: RustCrypto;
4949

5050
beforeEach(async () => {
51-
const mockHttpApi = {} as MatrixHttpApi<IHttpOpts>;
51+
const mockHttpApi = {} as MatrixClient["http"];
5252
rustCrypto = (await initRustCrypto(mockHttpApi, TEST_USER, TEST_DEVICE_ID)) as RustCrypto;
5353
});
5454

@@ -62,7 +62,7 @@ describe("RustCrypto", () => {
6262
let rustCrypto: RustCrypto;
6363

6464
beforeEach(async () => {
65-
const mockHttpApi = {} as MatrixHttpApi<IHttpOpts>;
65+
const mockHttpApi = {} as MatrixClient["http"];
6666
rustCrypto = (await initRustCrypto(mockHttpApi, TEST_USER, TEST_DEVICE_ID)) as RustCrypto;
6767
});
6868

@@ -132,6 +132,7 @@ describe("RustCrypto", () => {
132132
baseUrl: "https://example.com",
133133
prefix: "/_matrix",
134134
fetchFn: httpBackend.fetchFn as typeof global.fetch,
135+
onlyData: true,
135136
});
136137

137138
// for these tests we use a mock OlmMachine, with an implementation of outgoingRequests that

src/rust-crypto/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { RUST_SDK_STORE_PREFIX } from "./constants";
2323
import { IHttpOpts, MatrixHttpApi } from "../http-api";
2424

2525
export async function initRustCrypto(
26-
http: MatrixHttpApi<IHttpOpts>,
26+
http: MatrixHttpApi<IHttpOpts & { onlyData: true }>,
2727
userId: string,
2828
deviceId: string,
2929
): Promise<CryptoBackend> {

src/rust-crypto/rust-crypto.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type { IToDeviceEvent } from "../sync-accumulator";
2828
import { MatrixEvent } from "../models/event";
2929
import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
3030
import { logger } from "../logger";
31-
import { IHttpOpts, IRequestOpts, MatrixHttpApi, Method } from "../http-api";
31+
import { IHttpOpts, MatrixHttpApi, Method } from "../http-api";
3232
import { QueryDict } from "../utils";
3333

3434
/**
@@ -54,7 +54,7 @@ export class RustCrypto implements CryptoBackend {
5454

5555
public constructor(
5656
private readonly olmMachine: RustSdkCryptoJs.OlmMachine,
57-
private readonly http: MatrixHttpApi<IHttpOpts>,
57+
private readonly http: MatrixHttpApi<IHttpOpts & { onlyData: true }>,
5858
_userId: string,
5959
_deviceId: string,
6060
) {}
@@ -181,21 +181,21 @@ export class RustCrypto implements CryptoBackend {
181181
}
182182
}
183183

184-
private async rawJsonRequest(
185-
method: Method,
186-
path: string,
187-
queryParams: QueryDict,
188-
body: string,
189-
opts: IRequestOpts = {},
190-
): Promise<string> {
191-
// unbeknownst to HttpApi, we are sending JSON
192-
if (!opts.headers) opts.headers = {};
193-
opts.headers["Content-Type"] = "application/json";
194-
195-
// we use the full prefix
196-
if (!opts.prefix) opts.prefix = "";
197-
198-
const resp = await this.http.authedRequest(method, path, queryParams, body, opts);
199-
return await resp.text();
184+
private async rawJsonRequest(method: Method, path: string, queryParams: QueryDict, body: string): Promise<string> {
185+
const opts = {
186+
// inhibit the JSON stringification and parsing within HttpApi.
187+
json: false,
188+
189+
// nevertheless, we are sending, and accept, JSON.
190+
headers: {
191+
"Content-Type": "application/json",
192+
"Accept": "application/json",
193+
},
194+
195+
// we use the full prefix
196+
prefix: "",
197+
};
198+
199+
return await this.http.authedRequest<string>(method, path, queryParams, body, opts);
200200
}
201201
}

0 commit comments

Comments
 (0)