Skip to content

Commit

Permalink
Fix outgoing messages for rust-crypto (#3025)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
richvdh authored Jan 5, 2023
1 parent 695b773 commit ec6272a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
7 changes: 4 additions & 3 deletions spec/unit/rust-crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import MockHttpBackend from "matrix-mock-request";

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

afterEach(() => {
Expand All @@ -48,7 +48,7 @@ describe("RustCrypto", () => {
let rustCrypto: RustCrypto;

beforeEach(async () => {
const mockHttpApi = {} as MatrixHttpApi<IHttpOpts>;
const mockHttpApi = {} as MatrixClient["http"];
rustCrypto = (await initRustCrypto(mockHttpApi, TEST_USER, TEST_DEVICE_ID)) as RustCrypto;
});

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

beforeEach(async () => {
const mockHttpApi = {} as MatrixHttpApi<IHttpOpts>;
const mockHttpApi = {} as MatrixClient["http"];
rustCrypto = (await initRustCrypto(mockHttpApi, TEST_USER, TEST_DEVICE_ID)) as RustCrypto;
});

Expand Down Expand Up @@ -132,6 +132,7 @@ describe("RustCrypto", () => {
baseUrl: "https://example.com",
prefix: "/_matrix",
fetchFn: httpBackend.fetchFn as typeof global.fetch,
onlyData: true,
});

// for these tests we use a mock OlmMachine, with an implementation of outgoingRequests that
Expand Down
2 changes: 1 addition & 1 deletion src/rust-crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { RUST_SDK_STORE_PREFIX } from "./constants";
import { IHttpOpts, MatrixHttpApi } from "../http-api";

export async function initRustCrypto(
http: MatrixHttpApi<IHttpOpts>,
http: MatrixHttpApi<IHttpOpts & { onlyData: true }>,
userId: string,
deviceId: string,
): Promise<CryptoBackend> {
Expand Down
36 changes: 18 additions & 18 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type { IToDeviceEvent } from "../sync-accumulator";
import { MatrixEvent } from "../models/event";
import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
import { logger } from "../logger";
import { IHttpOpts, IRequestOpts, MatrixHttpApi, Method } from "../http-api";
import { IHttpOpts, MatrixHttpApi, Method } from "../http-api";
import { QueryDict } from "../utils";

/**
Expand All @@ -54,7 +54,7 @@ export class RustCrypto implements CryptoBackend {

public constructor(
private readonly olmMachine: RustSdkCryptoJs.OlmMachine,
private readonly http: MatrixHttpApi<IHttpOpts>,
private readonly http: MatrixHttpApi<IHttpOpts & { onlyData: true }>,
_userId: string,
_deviceId: string,
) {}
Expand Down Expand Up @@ -181,21 +181,21 @@ export class RustCrypto implements CryptoBackend {
}
}

private async rawJsonRequest(
method: Method,
path: string,
queryParams: QueryDict,
body: string,
opts: IRequestOpts = {},
): Promise<string> {
// unbeknownst to HttpApi, we are sending JSON
if (!opts.headers) opts.headers = {};
opts.headers["Content-Type"] = "application/json";

// we use the full prefix
if (!opts.prefix) opts.prefix = "";

const resp = await this.http.authedRequest(method, path, queryParams, body, opts);
return await resp.text();
private async rawJsonRequest(method: Method, path: string, queryParams: QueryDict, body: string): Promise<string> {
const opts = {
// inhibit the JSON stringification and parsing within HttpApi.
json: false,

// nevertheless, we are sending, and accept, JSON.
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
},

// we use the full prefix
prefix: "",
};

return await this.http.authedRequest<string>(method, path, queryParams, body, opts);
}
}

0 comments on commit ec6272a

Please sign in to comment.