From 354c9030b4595761cd3f41d7a7e416cbd867a033 Mon Sep 17 00:00:00 2001 From: Isla Koenigsknecht Date: Fri, 20 Dec 2024 15:00:36 -0500 Subject: [PATCH] Split file generation logic apart because fs import breaks mobile --- packages/backend/src/nest/common/utils.ts | 15 +++++++++++++++ .../ipfs-file-manager/big-files.long.spec.ts | 3 +-- .../src/nest/storage/storage.service.spec.ts | 3 +-- packages/common/src/tests.ts | 18 ------------------ .../src/tests/multipleClients.test.ts | 3 +-- packages/e2e-tests/src/utils.ts | 16 ++++++++++++++++ 6 files changed, 34 insertions(+), 24 deletions(-) diff --git a/packages/backend/src/nest/common/utils.ts b/packages/backend/src/nest/common/utils.ts index 3f92afc04..f689d5782 100644 --- a/packages/backend/src/nest/common/utils.ts +++ b/packages/backend/src/nest/common/utils.ts @@ -259,3 +259,18 @@ export async function createPeerId(): Promise { const peerId = await createEd25519PeerId() return peerIdFromKeys(peerId.publicKey, peerId.privateKey) } + +export const createArbitraryFile = (filePath: string, sizeBytes: number) => { + const stream = fs.createWriteStream(filePath) + const maxChunkSize = 1048576 // 1MB + + let remainingSize = sizeBytes + + while (remainingSize > 0) { + const chunkSize = Math.min(maxChunkSize, remainingSize) + stream.write(crypto.randomBytes(chunkSize)) + remainingSize -= chunkSize + } + + stream.end() +} diff --git a/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts b/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts index bb0a98d6d..3ad536b46 100644 --- a/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts +++ b/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts @@ -5,7 +5,7 @@ import { DownloadState, FileMetadata } from '@quiet/types' import { DirResult } from 'tmp' import waitForExpect from 'wait-for-expect' import { TestModule } from '../common/test.module' -import { createTmpDir, libp2pInstanceParams } from '../common/utils' +import { createArbitraryFile, createTmpDir, libp2pInstanceParams } from '../common/utils' import { IpfsModule } from '../ipfs/ipfs.module' import { IpfsService } from '../ipfs/ipfs.service' import { Libp2pModule } from '../libp2p/libp2p.module' @@ -16,7 +16,6 @@ import { IpfsFileManagerModule } from './ipfs-file-manager.module' import { IpfsFileManagerService } from './ipfs-file-manager.service' import fs from 'fs' import { createLogger } from '../common/logger' -import { createArbitraryFile } from '@quiet/common' const logger = createLogger('bigFiles:test') const BIG_FILE_SIZE = 2147483000 diff --git a/packages/backend/src/nest/storage/storage.service.spec.ts b/packages/backend/src/nest/storage/storage.service.spec.ts index 65c089790..231700e5c 100644 --- a/packages/backend/src/nest/storage/storage.service.spec.ts +++ b/packages/backend/src/nest/storage/storage.service.spec.ts @@ -23,7 +23,7 @@ import path from 'path' import { type PeerId } from '@libp2p/interface' import waitForExpect from 'wait-for-expect' import { TestModule } from '../common/test.module' -import { libp2pInstanceParams } from '../common/utils' +import { createArbitraryFile, libp2pInstanceParams } from '../common/utils' import { IpfsModule } from '../ipfs/ipfs.module' import { IpfsService } from '../ipfs/ipfs.service' import { Libp2pModule } from '../libp2p/libp2p.module' @@ -39,7 +39,6 @@ import { LocalDbService } from '../local-db/local-db.service' import { ORBIT_DB_DIR } from '../const' import { createLogger } from '../common/logger' import { createUserCertificateTestHelper, createTestRootCA } from '@quiet/identity' -import { createArbitraryFile } from '@quiet/common' const logger = createLogger('storageService:test') diff --git a/packages/common/src/tests.ts b/packages/common/src/tests.ts index c7ab3567a..6eead0dfd 100644 --- a/packages/common/src/tests.ts +++ b/packages/common/src/tests.ts @@ -1,6 +1,3 @@ -import fs from 'fs' -import crypto from 'crypto' - import { InvitationData, InvitationDataV1, InvitationDataV2, InvitationDataVersion } from '@quiet/types' import { composeInvitationDeepUrl, composeInvitationShareUrl } from './invitationLink/invitationLink' import { QUIET_JOIN_PAGE } from './const' @@ -87,18 +84,3 @@ export function getValidInvitationUrlTestData(data: T) // data: data, // } // } - -export const createArbitraryFile = (filePath: string, sizeBytes: number) => { - const stream = fs.createWriteStream(filePath) - const maxChunkSize = 1048576 // 1MB - - let remainingSize = sizeBytes - - while (remainingSize > 0) { - const chunkSize = Math.min(maxChunkSize, remainingSize) - stream.write(crypto.randomBytes(chunkSize)) - remainingSize -= chunkSize - } - - stream.end() -} diff --git a/packages/e2e-tests/src/tests/multipleClients.test.ts b/packages/e2e-tests/src/tests/multipleClients.test.ts index a01faf1d4..3c03a7103 100644 --- a/packages/e2e-tests/src/tests/multipleClients.test.ts +++ b/packages/e2e-tests/src/tests/multipleClients.test.ts @@ -11,7 +11,7 @@ import { RegisterUsernameModal, Sidebar, } from '../selectors' -import { promiseWithRetries, sleep } from '../utils' +import { promiseWithRetries, sleep, createArbitraryFile } from '../utils' import { MessageIds, UserTestData } from '../types' import { createLogger } from '../logger' import * as path from 'path' @@ -23,7 +23,6 @@ import { TEST_IMAGE_FILE_NAME, UPLOAD_FILE_DIR, } from '../uploadFile.const' -import { createArbitraryFile } from '@quiet/common' const logger = createLogger('multipleClients') diff --git a/packages/e2e-tests/src/utils.ts b/packages/e2e-tests/src/utils.ts index 7c1100f2b..b4df97830 100644 --- a/packages/e2e-tests/src/utils.ts +++ b/packages/e2e-tests/src/utils.ts @@ -4,6 +4,7 @@ import { type SupportedPlatformDesktop } from '@quiet/types' import getPort from 'get-port' import path from 'path' import fs from 'fs' +import crypto from 'crypto' import { DESKTOP_DATA_DIR, getAppDataPath } from '@quiet/common' import { RetryConfig, TimeoutMetadata } from './types' import { config } from 'dotenv' @@ -437,3 +438,18 @@ export const logAndReturnError = (error: string | Error): Error => { logger.error(errorText) return err } + +export const createArbitraryFile = (filePath: string, sizeBytes: number) => { + const stream = fs.createWriteStream(filePath) + const maxChunkSize = 1048576 // 1MB + + let remainingSize = sizeBytes + + while (remainingSize > 0) { + const chunkSize = Math.min(maxChunkSize, remainingSize) + stream.write(crypto.randomBytes(chunkSize)) + remainingSize -= chunkSize + } + + stream.end() +}