Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize http-api - move from browser-request to fetch #2719

Merged
merged 45 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
7231c68
Make `useAuthorizationHeader` default on
t3chguy Sep 30, 2022
e36d41a
Consolidate API prefixes into enums with method
t3chguy Sep 30, 2022
2ff24af
Remove deprecated callbacks support
t3chguy Oct 3, 2022
f8a44b7
Migrate from `request` and `browser-request` to `fetch`
t3chguy Oct 4, 2022
91a8048
Merge branch 'develop' of github.com:matrix-org/matrix-js-sdk into t3…
t3chguy Oct 4, 2022
5389fef
Update matrix-mock-request
t3chguy Oct 4, 2022
74d89f1
Update matrix-mock-request
t3chguy Oct 4, 2022
028d29c
Iterate fetch support
t3chguy Oct 4, 2022
0e832e1
Fix cleanup
t3chguy Oct 4, 2022
835d794
Iterate
t3chguy Oct 4, 2022
abf8bf9
Merge branch 'develop' of github.com:matrix-org/matrix-js-sdk into t3…
t3chguy Oct 4, 2022
6f50723
Test
t3chguy Oct 4, 2022
69c3d04
Iterare
t3chguy Oct 4, 2022
924925c
Iterate
t3chguy Oct 4, 2022
65b6935
Revert tests tweak
t3chguy Oct 4, 2022
a8ecb36
Add test coverage
t3chguy Oct 4, 2022
6289875
Fix tests
t3chguy Oct 4, 2022
064469c
Add tests and fix `retryNetworkOperation`
t3chguy Oct 4, 2022
d2d2e05
Improve coverage
t3chguy Oct 4, 2022
271ea3c
Add more coverage
t3chguy Oct 4, 2022
c9e45c0
Add tests
t3chguy Oct 4, 2022
27ca479
Iterate
t3chguy Oct 4, 2022
d2fb428
Write moar tests
t3chguy Oct 4, 2022
fd72eda
Fix typing
t3chguy Oct 4, 2022
6781d53
Iterate tests
t3chguy Oct 5, 2022
c0bf68a
Merge branch 'develop' of github.com:matrix-org/matrix-js-sdk into t3…
t3chguy Oct 5, 2022
0f16233
Additional coverage
t3chguy Oct 5, 2022
0138730
Please hit 80%
t3chguy Oct 5, 2022
fc2c722
Additional coverage
t3chguy Oct 5, 2022
abc68e7
Remove browser-request some more
t3chguy Oct 5, 2022
ac43f33
Yet more tests
t3chguy Oct 5, 2022
82d60f1
Iterate
t3chguy Oct 5, 2022
50cb854
Stash
t3chguy Oct 5, 2022
494a0e5
Merge branch 'develop' of github.com:matrix-org/matrix-js-sdk into t3…
t3chguy Oct 6, 2022
ffd26bd
Write moar tests
t3chguy Oct 6, 2022
52345e2
Additional coverage
t3chguy Oct 6, 2022
511afee
Update README
t3chguy Oct 6, 2022
b57651d
Update types
t3chguy Oct 6, 2022
88bbeb2
Fix login method
t3chguy Oct 6, 2022
d8ce2d3
Iterate everything
t3chguy Oct 6, 2022
17fc2bd
Iterate uploadContent signature
t3chguy Oct 7, 2022
5ffb1f1
Merge branch 'develop' into t3chguy/fetch
t3chguy Oct 7, 2022
3ca3778
Increase test coverage
t3chguy Oct 10, 2022
f643965
Merge remote-tracking branch 'origin/t3chguy/fetch' into t3chguy/fetch
t3chguy Oct 10, 2022
e4c2fd3
Merge branch 'develop' of github.com:matrix-org/matrix-js-sdk into t3…
t3chguy Oct 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ In Node.js
----------

Ensure you have the latest LTS version of Node.js installed.

This SDK targets Node 12 for compatibility, which translates to ES6. If you're using
a bundler like webpack you'll likely have to transpile dependencies, including this
SDK, to match your target browsers.
This library relies on `fetch` which is available in Node from v18.0.0 - it should work fine also with polyfills.
If you wish to use a ponyfill or adapter of some sort then pass it as `fetchFn` to the MatrixClient constructor options.

Using `yarn` instead of `npm` is recommended. Please see the Yarn [install guide](https://classic.yarnpkg.com/en/docs/install)
if you do not have it already.
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "20.0.2",
"description": "Matrix Client-Server SDK for Javascript",
"engines": {
"node": ">=12.9.0"
"node": ">=16.0.0"
},
"scripts": {
"prepublishOnly": "yarn build",
Expand Down Expand Up @@ -55,14 +55,12 @@
"dependencies": {
"@babel/runtime": "^7.12.5",
"another-json": "^0.2.0",
"browser-request": "^0.3.3",
"bs58": "^5.0.0",
"content-type": "^1.0.4",
"loglevel": "^1.7.1",
"matrix-events-sdk": "^0.0.1-beta.7",
"p-retry": "4",
"qs": "^6.9.6",
"request": "^2.88.2",
"unhomoglyph": "^1.0.6"
},
"devDependencies": {
Expand All @@ -81,9 +79,9 @@
"@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.12.tgz",
"@types/bs58": "^4.0.1",
"@types/content-type": "^1.1.5",
"@types/domexception": "^4.0.0",
"@types/jest": "^29.0.0",
"@types/node": "16",
"@types/request": "^2.48.5",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"allchange": "^1.0.6",
Expand All @@ -92,6 +90,7 @@
"better-docs": "^2.4.0-beta.9",
"browserify": "^17.0.0",
"docdash": "^1.2.0",
"domexception": "^4.0.0",
"eslint": "8.24.0",
"eslint-config-google": "^0.14.0",
"eslint-import-resolver-typescript": "^3.5.1",
Expand All @@ -104,7 +103,7 @@
"jest-mock": "^27.5.1",
"jest-sonar-reporter": "^2.0.0",
"jsdoc": "^3.6.6",
"matrix-mock-request": "^2.1.2",
"matrix-mock-request": "^2.5.0",
"rimraf": "^3.0.2",
"terser": "^5.5.1",
"tsify": "^5.0.2",
Expand All @@ -115,6 +114,9 @@
"testMatch": [
"<rootDir>/spec/**/*.spec.{js,ts}"
],
"setupFilesAfterEnv": [
"<rootDir>/spec/setupTests.ts"
],
"collectCoverageFrom": [
"<rootDir>/src/**/*.{js,ts}"
],
Expand Down
5 changes: 2 additions & 3 deletions spec/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { MockStorageApi } from "./MockStorageApi";
import { encodeUri } from "../src/utils";
import { IDeviceKeys, IOneTimeKey } from "../src/crypto/dehydration";
import { IKeyBackupSession } from "../src/crypto/keybackup";
import { IHttpOpts } from "../src/http-api";
import { IKeysUploadResponse, IUploadKeysRequest } from '../src/client';

/**
Expand All @@ -56,11 +55,11 @@ export class TestClient {
this.httpBackend = new MockHttpBackend();

const fullOptions: ICreateClientOpts = {
baseUrl: "http://" + userId + ".test.server",
baseUrl: "http://" + userId?.slice(1).replace(":", ".") + ".test.server",
userId: userId,
accessToken: accessToken,
deviceId: deviceId,
request: this.httpBackend.requestFn as IHttpOpts["request"],
fetchFn: this.httpBackend.fetchFn as typeof global.fetch,
...options,
};
if (!fullOptions.cryptoStore) {
Expand Down
75 changes: 50 additions & 25 deletions spec/browserify/sync-browserify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,66 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// load XmlHttpRequest mock
import HttpBackend from "matrix-mock-request";

import "./setupTests";
import "../../dist/browser-matrix"; // uses browser-matrix instead of the src
import * as utils from "../test-utils/test-utils";
import { TestClient } from "../TestClient";
import type { MatrixClient, ClientEvent } from "../../src";

const USER_ID = "@user:test.server";
const DEVICE_ID = "device_id";
const ACCESS_TOKEN = "access_token";
const ROOM_ID = "!room_id:server.test";

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface Global {
matrixcs: {
MatrixClient: typeof MatrixClient;
ClientEvent: typeof ClientEvent;
};
}
}
}

describe("Browserify Test", function() {
let client;
let httpBackend;
let client: MatrixClient;
let httpBackend: HttpBackend;

beforeEach(() => {
const testClient = new TestClient(USER_ID, DEVICE_ID, ACCESS_TOKEN);

client = testClient.client;
httpBackend = testClient.httpBackend;
httpBackend = new HttpBackend();
client = new global.matrixcs.MatrixClient({
baseUrl: "http://test.server",
userId: USER_ID,
accessToken: ACCESS_TOKEN,
deviceId: DEVICE_ID,
fetchFn: httpBackend.fetchFn as typeof global.fetch,
});

httpBackend.when("GET", "/versions").respond(200, {});
httpBackend.when("GET", "/pushrules").respond(200, {});
httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });

client.startClient();
});

afterEach(async () => {
client.stopClient();
httpBackend.stop();
client.http.abort();
httpBackend.verifyNoOutstandingRequests();
httpBackend.verifyNoOutstandingExpectation();
await httpBackend.stop();
});

it("Sync", function() {
const event = utils.mkMembership({
room: ROOM_ID,
mship: "join",
user: "@other_user:server.test",
name: "Displayname",
});
it("Sync", async () => {
const event = {
type: "m.room.member",
room_id: ROOM_ID,
content: {
membership: "join",
name: "Displayname",
},
event_id: "$foobar",
};

const syncData = {
next_batch: "batch1",
Expand All @@ -71,11 +91,16 @@ describe("Browserify Test", function() {
};

httpBackend.when("GET", "/sync").respond(200, syncData);
return Promise.race([
httpBackend.flushAllExpected(),
new Promise((_, reject) => {
client.once("sync.unexpectedError", reject);
}),
]);
httpBackend.when("GET", "/sync").respond(200, syncData);

const syncPromise = new Promise(r => client.once(global.matrixcs.ClientEvent.Sync, r));
const unexpectedErrorFn = jest.fn();
client.once(global.matrixcs.ClientEvent.SyncUnexpectedError, unexpectedErrorFn);

client.startClient();

await httpBackend.flushAllExpected();
await syncPromise;
expect(unexpectedErrorFn).not.toHaveBeenCalled();
}, 20000); // additional timeout as this test can take quite a while
});
Loading