From e86a6119cdf005aa7a4eb06cdf3b1e1687f55acb Mon Sep 17 00:00:00 2001 From: Josh Perez <60019601+josh-signal@users.noreply.github.com> Date: Mon, 20 Sep 2021 12:27:15 -0400 Subject: [PATCH] Moves stringToArrayBuffer into util folder --- js/modules/string_to_array_buffer.d.ts | 4 ---- test/app/attachments_test.js | 4 +--- test/modules/types/contact_test.js | 4 +--- test/modules/types/message_test.js | 4 +--- ts/test-node/types/Attachment_test.ts | 2 +- .../message/initializeAttachmentMetadata_test.ts | 2 +- ts/textsecure/Helpers.ts | 14 ++------------ .../util/stringToArrayBuffer.ts | 6 +++--- 8 files changed, 10 insertions(+), 30 deletions(-) delete mode 100644 js/modules/string_to_array_buffer.d.ts rename js/modules/string_to_array_buffer.js => ts/util/stringToArrayBuffer.ts (72%) diff --git a/js/modules/string_to_array_buffer.d.ts b/js/modules/string_to_array_buffer.d.ts deleted file mode 100644 index 4dc49306acef..000000000000 --- a/js/modules/string_to_array_buffer.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright 2020 Signal Messenger, LLC -// SPDX-License-Identifier: AGPL-3.0-only - -export function stringToArrayBuffer(string: string): ArrayBuffer; diff --git a/test/app/attachments_test.js b/test/app/attachments_test.js index 7aae89c3132c..9e99af1c969c 100644 --- a/test/app/attachments_test.js +++ b/test/app/attachments_test.js @@ -9,9 +9,7 @@ const { assert } = require('chai'); const { app } = require('electron'); const Attachments = require('../../app/attachments'); -const { - stringToArrayBuffer, -} = require('../../js/modules/string_to_array_buffer'); +const { stringToArrayBuffer } = require('../../ts/util/stringToArrayBuffer'); const PREFIX_LENGTH = 2; const NUM_SEPARATORS = 1; diff --git a/test/modules/types/contact_test.js b/test/modules/types/contact_test.js index 7ce6b70a7245..f51cb725a154 100644 --- a/test/modules/types/contact_test.js +++ b/test/modules/types/contact_test.js @@ -5,9 +5,7 @@ const { assert } = require('chai'); const sinon = require('sinon'); const Contact = require('../../../js/modules/types/contact'); -const { - stringToArrayBuffer, -} = require('../../../js/modules/string_to_array_buffer'); +const { stringToArrayBuffer } = require('../../../ts/util/stringToArrayBuffer'); describe('Contact', () => { const NUMBER = '+12025550099'; diff --git a/test/modules/types/message_test.js b/test/modules/types/message_test.js index 261032290ac2..36418c1cee6d 100644 --- a/test/modules/types/message_test.js +++ b/test/modules/types/message_test.js @@ -6,9 +6,7 @@ const sinon = require('sinon'); const Message = require('../../../js/modules/types/message'); const { SignalService } = require('../../../ts/protobuf'); -const { - stringToArrayBuffer, -} = require('../../../js/modules/string_to_array_buffer'); +const { stringToArrayBuffer } = require('../../../ts/util/stringToArrayBuffer'); describe('Message', () => { const logger = { diff --git a/ts/test-node/types/Attachment_test.ts b/ts/test-node/types/Attachment_test.ts index 0722d26efb2b..fffc136b7383 100644 --- a/ts/test-node/types/Attachment_test.ts +++ b/ts/test-node/types/Attachment_test.ts @@ -6,7 +6,7 @@ import { assert } from 'chai'; import * as Attachment from '../../types/Attachment'; import * as MIME from '../../types/MIME'; import { SignalService } from '../../protobuf'; -import { stringToArrayBuffer } from '../../../js/modules/string_to_array_buffer'; +import { stringToArrayBuffer } from '../../util/stringToArrayBuffer'; import * as logger from '../../logging/log'; describe('Attachment', () => { diff --git a/ts/test-node/types/message/initializeAttachmentMetadata_test.ts b/ts/test-node/types/message/initializeAttachmentMetadata_test.ts index 50dca001c70e..f8f2bc273f0f 100644 --- a/ts/test-node/types/message/initializeAttachmentMetadata_test.ts +++ b/ts/test-node/types/message/initializeAttachmentMetadata_test.ts @@ -7,7 +7,7 @@ import * as Message from '../../../types/message/initializeAttachmentMetadata'; import { IncomingMessage } from '../../../types/Message'; import { SignalService } from '../../../protobuf'; import * as MIME from '../../../types/MIME'; -import { stringToArrayBuffer } from '../../../../js/modules/string_to_array_buffer'; +import { stringToArrayBuffer } from '../../../util/stringToArrayBuffer'; describe('Message', () => { describe('initializeAttachmentMetadata', () => { diff --git a/ts/textsecure/Helpers.ts b/ts/textsecure/Helpers.ts index ccd303729fb6..3cbc042bd0d5 100644 --- a/ts/textsecure/Helpers.ts +++ b/ts/textsecure/Helpers.ts @@ -1,6 +1,8 @@ // Copyright 2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only +import { stringToArrayBuffer } from '../util/stringToArrayBuffer'; + /* eslint-disable guard-for-in */ /* eslint-disable no-restricted-syntax */ /* eslint-disable no-proto */ @@ -62,18 +64,6 @@ function ensureStringed(thing: any): any { throw new Error(`unsure of how to jsonify object of type ${typeof thing}`); } -function stringToArrayBuffer(string: string): ArrayBuffer { - if (typeof string !== 'string') { - throw new TypeError("'string' must be a string"); - } - - const array = new Uint8Array(string.length); - for (let i = 0; i < string.length; i += 1) { - array[i] = string.charCodeAt(i); - } - return array.buffer; -} - // Number formatting utils const utils = { getString, diff --git a/js/modules/string_to_array_buffer.js b/ts/util/stringToArrayBuffer.ts similarity index 72% rename from js/modules/string_to_array_buffer.js rename to ts/util/stringToArrayBuffer.ts index a7c8787e3ae0..74c799ddcd4d 100644 --- a/js/modules/string_to_array_buffer.js +++ b/ts/util/stringToArrayBuffer.ts @@ -1,7 +1,7 @@ -// Copyright 2018-2020 Signal Messenger, LLC +// Copyright 2018-2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -exports.stringToArrayBuffer = string => { +export function stringToArrayBuffer(string: string): ArrayBuffer { if (typeof string !== 'string') { throw new TypeError("'string' must be a string"); } @@ -11,4 +11,4 @@ exports.stringToArrayBuffer = string => { array[i] = string.charCodeAt(i); } return array.buffer; -}; +}