Skip to content

Commit 085854b

Browse files
authored
Merge pull request element-hq#28626 from element-hq/t3chguy/web-friendly-buffers
Remove usages of Buffer so we don't need to ship the polyfill
2 parents ee24989 + 188f910 commit 085854b

File tree

17 files changed

+24
-34
lines changed

17 files changed

+24
-34
lines changed

Diff for: .eslintrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ module.exports = {
4242
name: "setImmediate",
4343
message: "Use setTimeout instead.",
4444
},
45+
{
46+
name: "Buffer",
47+
message: "Buffer is not available in the web.",
48+
},
4549
],
4650

4751
"import/no-duplicates": ["error"],
@@ -255,6 +259,9 @@ module.exports = {
255259
additionalTestBlockFunctions: ["beforeAll", "beforeEach", "oldBackendOnly"],
256260
},
257261
],
262+
263+
// These are fine in tests
264+
"no-restricted-globals": "off",
258265
},
259266
},
260267
{

Diff for: package.json

-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@
216216
"babel-loader": "^9.0.0",
217217
"babel-plugin-jsx-remove-data-test-id": "^3.0.0",
218218
"blob-polyfill": "^9.0.0",
219-
"buffer": "^6.0.3",
220219
"chokidar": "^4.0.0",
221220
"concurrently": "^9.0.0",
222221
"copy-webpack-plugin": "^12.0.0",

Diff for: playwright/e2e/crypto/device-verification.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test.describe("Device verification", () => {
102102
// feed the QR code into the verification request.
103103
const qrData = await readQrCode(infoDialog);
104104
const verifier = await verificationRequest.evaluateHandle(
105-
(request, qrData) => request.scanQRCode(new Uint8Array(qrData)),
105+
(request, qrData) => request.scanQRCode(new Uint8ClampedArray(qrData)),
106106
[...qrData],
107107
);
108108

Diff for: src/@types/png-chunks-extract.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare module "png-chunks-extract" {
1212
data: Uint8Array;
1313
}
1414

15-
function extractPngChunks(data: Uint8Array | Buffer): IChunk[];
15+
function extractPngChunks(data: Uint8Array): IChunk[];
1616

1717
export default extractPngChunks;
1818
}

Diff for: src/DecryptionFailureTracker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
66
Please see LICENSE files in the repository root for full details.
77
*/
88

9-
import { ScalableBloomFilter } from "bloom-filters";
9+
import ScalableBloomFilter from "bloom-filters/dist/bloom/scalable-bloom-filter";
1010
import { HttpApiEvent, MatrixClient, MatrixEventEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";
1111
import { Error as ErrorEvent } from "@matrix-org/analytics-events/types/typescript/Error";
1212
import { DecryptionFailureCode, CryptoEvent } from "matrix-js-sdk/src/crypto-api";

Diff for: src/components/views/auth/LoginWithQR.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ interface IState {
3737
userCode?: string;
3838
checkCode?: string;
3939
failureReason?: FailureReason;
40-
lastScannedCode?: Buffer;
4140
}
4241

4342
export enum LoginWithQRFailureReason {
@@ -154,7 +153,7 @@ export default class LoginWithQR extends React.Component<IProps, IState> {
154153
throw new Error("Rendezvous not found");
155154
}
156155

157-
if (!this.state.lastScannedCode && this.state.rendezvous?.checkCode !== checkCode) {
156+
if (this.state.rendezvous?.checkCode !== checkCode) {
158157
this.setState({ failureReason: LoginWithQRFailureReason.CheckCodeMismatch });
159158
return;
160159
}
@@ -201,7 +200,6 @@ export default class LoginWithQR extends React.Component<IProps, IState> {
201200
failureReason: undefined,
202201
userCode: undefined,
203202
checkCode: undefined,
204-
lastScannedCode: undefined,
205203
mediaPermissionError: false,
206204
});
207205
}

Diff for: src/components/views/elements/crypto/VerificationQRCode.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import QRCode from "../QRCode";
1212

1313
interface IProps {
1414
/** The data for the QR code. If `undefined`, a spinner is shown. */
15-
qrCodeBytes: undefined | Buffer;
15+
qrCodeBytes: undefined | Uint8ClampedArray;
1616
}
1717

1818
export default class VerificationQRCode extends React.PureComponent<IProps> {

Diff for: src/components/views/right_panel/VerificationPanel.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ interface IState {
4646
* We attempt to calculate this once the verification request transitions into the "Ready" phase. If the other
4747
* side cannot scan QR codes, it will remain `undefined`.
4848
*/
49-
qrCodeBytes: Buffer | undefined;
49+
qrCodeBytes: Uint8ClampedArray | undefined;
5050

5151
sasEvent: ShowSasCallbacks | null;
5252
emojiButtonClicked?: boolean;

Diff for: src/stores/ThreepidInviteStore.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default class ThreepidInviteStore extends EventEmitter {
9999

100100
private generateIdOf(persisted: IPersistedThreepidInvite): string {
101101
// Use a consistent "hash" to form an ID.
102-
return base32.stringify(Buffer.from(JSON.stringify(persisted)));
102+
return base32.stringify(new TextEncoder().encode(JSON.stringify(persisted)));
103103
}
104104

105105
private translateInvite(persisted: IPersistedThreepidInvite): IThreepidInvite {

Diff for: src/utils/WidgetUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ export default class WidgetUtils {
445445
// For compatibility with Jitsi, use base32 without padding.
446446
// More details here:
447447
// https://github.com/matrix-org/prosody-mod-auth-matrix-user-verification
448-
confId = base32.stringify(Buffer.from(roomId), { pad: false });
448+
confId = base32.stringify(new TextEncoder().encode(roomId), { pad: false });
449449
} else {
450450
// Create a random conference ID
451451
confId = `Jitsi${randomUppercaseString(1)}${randomLowercaseString(23)}`;

Diff for: src/utils/exportUtils/HtmlExport.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ export default class HTMLExporter extends Exporter {
431431
!this.needsDateSeparator(event, prevEvent) &&
432432
shouldFormContinuation(prevEvent, event, this.room.client, false);
433433
const body = await this.createMessageBody(event, shouldBeJoined);
434-
this.totalSize += Buffer.byteLength(body);
434+
this.totalSize += new TextEncoder().encode(body).byteLength;
435435
content += body;
436436
prevEvent = event;
437437
}

Diff for: src/utils/tokens/pickling.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export async function buildAndEncodePickleKey(
118118
data.encrypted,
119119
);
120120
if (pickleKeyBuf) {
121-
return encodeUnpaddedBase64(pickleKeyBuf);
121+
return encodeUnpaddedBase64(new Uint8Array(pickleKeyBuf));
122122
}
123123
} catch {
124124
logger.error("Error decrypting pickle key");

Diff for: test/unit-tests/Lifecycle-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ describe("Lifecycle", () => {
418418
undefined,
419419
);
420420

421-
expect(MatrixClientPeg.start).toHaveBeenCalledWith({ rustCryptoStoreKey: expect.any(Buffer) });
421+
expect(MatrixClientPeg.start).toHaveBeenCalledWith({ rustCryptoStoreKey: expect.any(Uint8Array) });
422422
});
423423

424424
describe("with a refresh token", () => {

Diff for: test/unit-tests/components/views/elements/crypto/VerificationQRCode-test.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ describe("<VerificationQRCode />", () => {
1717
});
1818

1919
it("renders a QR code", async () => {
20-
const { container, getAllByAltText } = render(<VerificationQRCode qrCodeBytes={Buffer.from("asd")} />);
20+
const { container, getAllByAltText } = render(
21+
<VerificationQRCode qrCodeBytes={new Uint8ClampedArray(Buffer.from("asd"))} />,
22+
);
2123
// wait for the spinner to go away
2224
await waitFor(() => getAllByAltText("QR Code").length === 1);
2325
expect(container).toMatchSnapshot();

Diff for: test/unit-tests/components/views/right_panel/VerificationPanel-test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("<VerificationPanel />", () => {
4646
const request = makeMockVerificationRequest({
4747
phase: Phase.Ready,
4848
});
49-
request.generateQRCode.mockResolvedValue(Buffer.from("test", "utf-8"));
49+
request.generateQRCode.mockResolvedValue(new Uint8ClampedArray(Buffer.from("test", "utf-8")));
5050
const container = renderComponent({
5151
request: request,
5252
layout: "dialog",
@@ -71,7 +71,7 @@ describe("<VerificationPanel />", () => {
7171
const request = makeMockVerificationRequest({
7272
phase: Phase.Ready,
7373
});
74-
request.generateQRCode.mockResolvedValue(Buffer.from("test", "utf-8"));
74+
request.generateQRCode.mockResolvedValue(new Uint8ClampedArray(Buffer.from("test", "utf-8")));
7575
const container = renderComponent({
7676
request: request,
7777
member: new User("@other:user"),

Diff for: webpack.config.js

-3
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ module.exports = (env, argv) => {
231231

232232
// Polyfill needed by counterpart
233233
"util": require.resolve("util/"),
234-
// Polyfill needed by matrix-js-sdk/src/crypto
235-
"buffer": require.resolve("buffer/"),
236234
// Polyfill needed by sentry
237235
"process/browser": require.resolve("process/browser"),
238236
},
@@ -679,7 +677,6 @@ module.exports = (env, argv) => {
679677
// Automatically load buffer & process modules as we use them without explicitly
680678
// importing them
681679
new webpack.ProvidePlugin({
682-
Buffer: ["buffer", "Buffer"],
683680
process: "process/browser",
684681
}),
685682

Diff for: yarn.lock

+1-14
Original file line numberDiff line numberDiff line change
@@ -4054,11 +4054,6 @@ base64-arraybuffer@^1.0.2:
40544054
resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz#1c37589a7c4b0746e34bd1feb951da2df01c1bdc"
40554055
integrity sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ==
40564056

4057-
base64-js@^1.3.1:
4058-
version "1.5.1"
4059-
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
4060-
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==
4061-
40624057
40634058
version "0.6.1"
40644059
resolved "https://registry.yarnpkg.com/batch/-/batch-0.6.1.tgz#dc34314f4e679318093fc760272525f94bf25c16"
@@ -4180,14 +4175,6 @@ buffer-from@^1.0.0:
41804175
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5"
41814176
integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==
41824177

4183-
buffer@^6.0.3:
4184-
version "6.0.3"
4185-
resolved "https://registry.yarnpkg.com/buffer/-/buffer-6.0.3.tgz#2ace578459cc8fbe2a70aaa8f52ee63b6a74c6c6"
4186-
integrity sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==
4187-
dependencies:
4188-
base64-js "^1.3.1"
4189-
ieee754 "^1.2.1"
4190-
41914178
builtin-modules@^3.3.0:
41924179
version "3.3.0"
41934180
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.3.0.tgz#cae62812b89801e9656336e46223e030386be7b6"
@@ -6787,7 +6774,7 @@ icss-utils@^5.0.0, icss-utils@^5.1.0:
67876774
resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-5.1.0.tgz#c6be6858abd013d768e98366ae47e25d5887b1ae"
67886775
integrity sha512-soFhflCVWLfRNOPU3iv5Z9VUdT44xFRbzjLsEzSr5AQmgqPMTHdU3PMT1Cf1ssx8fLNJDA1juftYl+PUcv3MqA==
67896776

6790-
ieee754@^1.1.12, ieee754@^1.2.1:
6777+
ieee754@^1.1.12:
67916778
version "1.2.1"
67926779
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
67936780
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==

0 commit comments

Comments
 (0)