Skip to content

Commit

Permalink
Split file generation logic apart because fs import breaks mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
islathehut committed Dec 20, 2024
1 parent ef189db commit 354c903
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
15 changes: 15 additions & 0 deletions packages/backend/src/nest/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,18 @@ export async function createPeerId(): Promise<PeerId> {
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/backend/src/nest/storage/storage.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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')

Expand Down
18 changes: 0 additions & 18 deletions packages/common/src/tests.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -87,18 +84,3 @@ export function getValidInvitationUrlTestData<T extends InvitationData>(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()
}
3 changes: 1 addition & 2 deletions packages/e2e-tests/src/tests/multipleClients.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -23,7 +23,6 @@ import {
TEST_IMAGE_FILE_NAME,
UPLOAD_FILE_DIR,
} from '../uploadFile.const'
import { createArbitraryFile } from '@quiet/common'

const logger = createLogger('multipleClients')

Expand Down
16 changes: 16 additions & 0 deletions packages/e2e-tests/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
}

0 comments on commit 354c903

Please sign in to comment.