From e8651afa0b4beaead6bf6049d021cd9b502c428a Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Thu, 24 Mar 2022 10:05:48 -0700 Subject: [PATCH] Configure `Long` before requiring compiled protobuf --- ts/protobuf/index.ts | 6 +----- ts/protobuf/wrap.ts | 10 ++++++++++ ts/test-both/ContactsParser_test.ts | 4 +++- ts/textsecure/ContactsParser.ts | 8 +++++--- 4 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 ts/protobuf/wrap.ts diff --git a/ts/protobuf/index.ts b/ts/protobuf/index.ts index 87b2f1a628..8b8f02710b 100644 --- a/ts/protobuf/index.ts +++ b/ts/protobuf/index.ts @@ -1,12 +1,8 @@ // Copyright 2018-2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -import * as protobuf from 'protobufjs/minimal'; -import Long from 'long'; +import './wrap'; import { signalservice as SignalService } from './compiled'; -protobuf.util.Long = Long; -protobuf.configure(); - export { SignalService }; diff --git a/ts/protobuf/wrap.ts b/ts/protobuf/wrap.ts new file mode 100644 index 0000000000..0c9498d635 --- /dev/null +++ b/ts/protobuf/wrap.ts @@ -0,0 +1,10 @@ +// Copyright 2022 Signal Messenger, LLC +// SPDX-License-Identifier: AGPL-3.0-only + +import * as protobuf from 'protobufjs/minimal'; +import Long from 'long'; + +protobuf.util.Long = Long; +protobuf.configure(); + +export default protobuf; diff --git a/ts/test-both/ContactsParser_test.ts b/ts/test-both/ContactsParser_test.ts index 83a70603e6..f58699bde8 100644 --- a/ts/test-both/ContactsParser_test.ts +++ b/ts/test-both/ContactsParser_test.ts @@ -2,12 +2,14 @@ // SPDX-License-Identifier: AGPL-3.0-only import { assert } from 'chai'; -import { Writer } from 'protobufjs'; +import protobuf from '../protobuf/wrap'; import * as Bytes from '../Bytes'; import { SignalService as Proto } from '../protobuf'; import { ContactBuffer, GroupBuffer } from '../textsecure/ContactsParser'; +const { Writer } = protobuf; + describe('ContactsParser', () => { function generateAvatar(): Uint8Array { const result = new Uint8Array(255); diff --git a/ts/textsecure/ContactsParser.ts b/ts/textsecure/ContactsParser.ts index d89cbf83f8..b2ec603c6a 100644 --- a/ts/textsecure/ContactsParser.ts +++ b/ts/textsecure/ContactsParser.ts @@ -3,7 +3,7 @@ /* eslint-disable max-classes-per-file */ -import { Reader } from 'protobufjs'; +import protobuf from '../protobuf/wrap'; import { SignalService as Proto } from '../protobuf'; import { normalizeUuid } from '../util/normalizeUuid'; @@ -11,10 +11,12 @@ import * as log from '../logging/log'; import Avatar = Proto.ContactDetails.IAvatar; +const { Reader } = protobuf; + type OptionalAvatar = { avatar?: Avatar | null }; type DecoderBase = { - decodeDelimited(reader: Reader): Message | undefined; + decodeDelimited(reader: protobuf.Reader): Message | undefined; }; export type MessageWithAvatar = Omit< @@ -32,7 +34,7 @@ class ParserBase< Message extends OptionalAvatar, Decoder extends DecoderBase > { - protected readonly reader: Reader; + protected readonly reader: protobuf.Reader; constructor(bytes: Uint8Array, private readonly decoder: Decoder) { this.reader = new Reader(bytes);