UNPKG

playwright-performance-reporter

Version:

Measure and publish performance metrics from browser dev-tools when running playwright

78 lines (77 loc) 1.8 kB
import path from 'node:path'; import fs from 'node:fs'; import JSONStream from 'JSONStream'; import { Logger } from './logger.js'; /** * Write JSON chunks in an array of entries */ export class JsonChunkWriter { /** * Locator where to store the output */ filePath; /** * Status whether writer is usable */ isClosed = false; /** * File writer */ fileStream; /** * JSON chunk writer */ jsonStream = JSONStream.stringify('[', ',', ']'); /** * Initialize writer * * @param options defines target output */ initialize(options) { this.filePath = path.join(options.outputDir, options.outputFile); this.fileStream = fs.createWriteStream(this.filePath, { flags: 'w' }); this.jsonStream.pipe(this.fileStream); } /** * Create new entry of an object, that is serializable. * * @param content */ write(content) { if (this.isClosed) { return false; } try { // @ts-expect-error: writer is able to handle more types return this.jsonStream.write(content); } catch (error) { Logger.error('JSONChunkWriter write operation failed', String(error)); return false; } } /** * Finish json stream */ close() { this.isClosed = true; this.jsonStream.end(); this.fileStream?.end(); } /** * Delete created file */ delete() { try { this.close(); } catch { return false; } if (this.filePath) { fs.rmSync(this.filePath, { maxRetries: 5, retryDelay: 500 }); return true; } return false; } }