diff --git a/scripts/build.js b/scripts/build.js index 904a8af..2377c2d 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -22,7 +22,7 @@ const getGitTagFromGit = async () => { ['describe', '--exact-match', '--tags'], { reject: false, - }, + } ) if (exitCode) { if ( @@ -72,8 +72,10 @@ delete packageJson.prettier delete packageJson.jest packageJson.version = version packageJson.main = 'dist/index.js' +packageJson.types = 'dist/index.d.js' await writeJson(join(dist, 'package.json'), packageJson) await cp(join(root, 'README.md'), join(dist, 'README.md')) await cp(join(root, 'LICENSE'), join(dist, 'LICENSE')) +await cp(join(root, 'src', 'main.d.ts'), join(dist, 'dist/index.d.ts')) diff --git a/src/main.d.ts b/src/main.d.ts new file mode 100644 index 0000000..c91829b --- /dev/null +++ b/src/main.d.ts @@ -0,0 +1,7 @@ +interface Options { + readonly workerPath: string + readonly playwrightPath: string + readonly threshold: number +} + +export const measureMemory: (options: Options) => Promise diff --git a/src/main.ts b/src/main.ts index abfaa99..57c7f37 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1 @@ -import * as Main from './parts/Main/Main.ts' - -Main.main() +export * from './parts/Main/Main.ts' diff --git a/src/parts/Main/Main.ts b/src/parts/Main/Main.ts index 6a56a2e..2779d2b 100644 --- a/src/parts/Main/Main.ts +++ b/src/parts/Main/Main.ts @@ -1,16 +1 @@ -import * as MeasureMemory from '../MeasureMemory/MeasureMemory.ts' -import { parseArgs } from '../ParseArgs/ParseArgs.ts' -import { root } from '../Root/Root.ts' - -export const main = async () => { - const options = parseArgs() - await MeasureMemory.measureMemory({ - headless: options.headless, - port: options.port, - remoteDebuggingPort: '9222', - workerPath: 'TODO', - playwrightPath: 'TODO', - root, - threshold: 400_000, - }) -} +export * from '../MeasureMemory/MeasureMemory.ts' diff --git a/src/parts/MeasureMemory/MeasureMemory.ts b/src/parts/MeasureMemory/MeasureMemory.ts index e0a1f12..1febf6b 100644 --- a/src/parts/MeasureMemory/MeasureMemory.ts +++ b/src/parts/MeasureMemory/MeasureMemory.ts @@ -1,53 +1,24 @@ -import { MemoryLimitExceededError } from '../Errors/Errors.ts' -import { getMemoryUsageWs } from '../GetMemoryUsageWs/GetMemoryUsageWs.ts' -import { launchBrowser } from '../LaunchBrowser/LaunchBrowser.ts' -import { startServer } from '../Server/Server.ts' -import { waitForWorkerReady } from '../WaitForWorkerReady/WaitForWorkerReady.ts' +import * as MeasureMemoryInternal from '../MeasureMemoryInternal/MeasureMemoryInternal.ts' +import { parseArgs } from '../ParseArgs/ParseArgs.ts' +import { root } from '../Root/Root.ts' export const measureMemory = async ({ workerPath, - port, - headless, - remoteDebuggingPort, - root, playwrightPath, threshold, }: { workerPath: string - port: number - headless: boolean - remoteDebuggingPort: string - root: string playwrightPath: string threshold: number }) => { - if (process.platform === 'win32') { - // not supported - return - } - - const server = await startServer(port, workerPath, root) - - const { page, browser } = await launchBrowser( - headless, - remoteDebuggingPort, + const options = parseArgs() + await MeasureMemoryInternal.measureMemoryInternal({ + headless: options.headless, + port: options.port, + remoteDebuggingPort: '9222', + workerPath, playwrightPath, - ) - - try { - await page.goto(`http://localhost:${port}`) - await waitForWorkerReady(page) - - const memoryUsage = await getMemoryUsageWs(remoteDebuggingPort) - console.log('[memory] Worker Memory Usage:', memoryUsage) - if (memoryUsage.usedSize >= threshold) { - throw new MemoryLimitExceededError(threshold, memoryUsage.usedSize) - } - } catch (error) { - console.error('[memory] Measurement failed:', error) - process.exit(1) - } finally { - server.close() - await browser.close() - } + root, + threshold, + }) } diff --git a/src/parts/MeasureMemoryInternal/MeasureMemoryInternal.ts b/src/parts/MeasureMemoryInternal/MeasureMemoryInternal.ts new file mode 100644 index 0000000..f56fd54 --- /dev/null +++ b/src/parts/MeasureMemoryInternal/MeasureMemoryInternal.ts @@ -0,0 +1,53 @@ +import { MemoryLimitExceededError } from '../Errors/Errors.ts' +import { getMemoryUsageWs } from '../GetMemoryUsageWs/GetMemoryUsageWs.ts' +import { launchBrowser } from '../LaunchBrowser/LaunchBrowser.ts' +import { startServer } from '../Server/Server.ts' +import { waitForWorkerReady } from '../WaitForWorkerReady/WaitForWorkerReady.ts' + +export const measureMemoryInternal = async ({ + workerPath, + port, + headless, + remoteDebuggingPort, + root, + playwrightPath, + threshold, +}: { + workerPath: string + port: number + headless: boolean + remoteDebuggingPort: string + root: string + playwrightPath: string + threshold: number +}) => { + if (process.platform === 'win32') { + // not supported + return + } + + const server = await startServer(port, workerPath, root) + + const { page, browser } = await launchBrowser( + headless, + remoteDebuggingPort, + playwrightPath + ) + + try { + await page.goto(`http://localhost:${port}`) + await waitForWorkerReady(page) + + const memoryUsage = await getMemoryUsageWs(remoteDebuggingPort) + console.log('[memory] Worker Memory Usage:', memoryUsage) + if (memoryUsage.usedSize >= threshold) { + throw new MemoryLimitExceededError(threshold, memoryUsage.usedSize) + } + } catch (error) { + console.error('[memory] Measurement failed:', error) + process.exit(1) + } finally { + server.close() + await browser.close() + } +} diff --git a/test/Main.test.ts b/test/Main.test.ts index e0d3ac1..e89db4e 100644 --- a/test/Main.test.ts +++ b/test/Main.test.ts @@ -2,5 +2,5 @@ import { expect, test } from '@jest/globals' import * as Main from '../src/parts/Main/Main.ts' test('main', () => { - expect(typeof Main.main).toBe('function') + expect(typeof Main.measureMemory).toBe('function') })