blob.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var blob_exports = {};
  30. __export(blob_exports, {
  31. BlobReporter: () => BlobReporter,
  32. currentBlobReportVersion: () => currentBlobReportVersion
  33. });
  34. module.exports = __toCommonJS(blob_exports);
  35. var import_fs = __toESM(require("fs"));
  36. var import_path = __toESM(require("path"));
  37. var import_stream = require("stream");
  38. var import_utils = require("playwright-core/lib/utils");
  39. var import_utils2 = require("playwright-core/lib/utils");
  40. var import_utilsBundle = require("playwright-core/lib/utilsBundle");
  41. var import_zipBundle = require("playwright-core/lib/zipBundle");
  42. var import_base = require("./base");
  43. var import_teleEmitter = require("./teleEmitter");
  44. const currentBlobReportVersion = 2;
  45. class BlobReporter extends import_teleEmitter.TeleReporterEmitter {
  46. constructor(options) {
  47. super((message) => this._messages.push(message));
  48. this._messages = [];
  49. this._attachments = [];
  50. this._options = options;
  51. if (this._options.fileName && !this._options.fileName.endsWith(".zip"))
  52. throw new Error(`Blob report file name must end with .zip extension: ${this._options.fileName}`);
  53. this._salt = (0, import_utils2.createGuid)();
  54. }
  55. onConfigure(config) {
  56. const metadata = {
  57. version: currentBlobReportVersion,
  58. userAgent: (0, import_utils2.getUserAgent)(),
  59. name: process.env.PWTEST_BOT_NAME,
  60. shard: config.shard ?? void 0,
  61. pathSeparator: import_path.default.sep
  62. };
  63. this._messages.push({
  64. method: "onBlobReportMetadata",
  65. params: metadata
  66. });
  67. this._config = config;
  68. super.onConfigure(config);
  69. }
  70. async onEnd(result) {
  71. await super.onEnd(result);
  72. const zipFileName = await this._prepareOutputFile();
  73. const zipFile = new import_zipBundle.yazl.ZipFile();
  74. const zipFinishPromise = new import_utils2.ManualPromise();
  75. const finishPromise = zipFinishPromise.catch((e) => {
  76. throw new Error(`Failed to write report ${zipFileName}: ` + e.message);
  77. });
  78. zipFile.on("error", (error) => zipFinishPromise.reject(error));
  79. zipFile.outputStream.pipe(import_fs.default.createWriteStream(zipFileName)).on("close", () => {
  80. zipFinishPromise.resolve(void 0);
  81. }).on("error", (error) => zipFinishPromise.reject(error));
  82. for (const { originalPath, zipEntryPath } of this._attachments) {
  83. if (!import_fs.default.statSync(originalPath, { throwIfNoEntry: false })?.isFile())
  84. continue;
  85. zipFile.addFile(originalPath, zipEntryPath);
  86. }
  87. const lines = this._messages.map((m) => JSON.stringify(m) + "\n");
  88. const content = import_stream.Readable.from(lines);
  89. zipFile.addReadStream(content, "report.jsonl");
  90. zipFile.end();
  91. await finishPromise;
  92. }
  93. async _prepareOutputFile() {
  94. const { outputFile, outputDir } = (0, import_base.resolveOutputFile)("BLOB", {
  95. ...this._options,
  96. default: {
  97. fileName: this._defaultReportName(this._config),
  98. outputDir: "blob-report"
  99. }
  100. });
  101. if (!process.env.PWTEST_BLOB_DO_NOT_REMOVE)
  102. await (0, import_utils.removeFolders)([outputDir]);
  103. await import_fs.default.promises.mkdir(import_path.default.dirname(outputFile), { recursive: true });
  104. return outputFile;
  105. }
  106. _defaultReportName(config) {
  107. let reportName = "report";
  108. if (this._options._commandHash)
  109. reportName += "-" + (0, import_utils.sanitizeForFilePath)(this._options._commandHash);
  110. if (config.shard) {
  111. const paddedNumber = `${config.shard.current}`.padStart(`${config.shard.total}`.length, "0");
  112. reportName = `${reportName}-${paddedNumber}`;
  113. }
  114. return `${reportName}.zip`;
  115. }
  116. _serializeAttachments(attachments) {
  117. return super._serializeAttachments(attachments).map((attachment) => {
  118. if (!attachment.path)
  119. return attachment;
  120. const sha1 = (0, import_utils2.calculateSha1)(attachment.path + this._salt);
  121. const extension = import_utilsBundle.mime.getExtension(attachment.contentType) || "dat";
  122. const newPath = `resources/${sha1}.${extension}`;
  123. this._attachments.push({ originalPath: attachment.path, zipEntryPath: newPath });
  124. return {
  125. ...attachment,
  126. path: newPath
  127. };
  128. });
  129. }
  130. }
  131. // Annotate the CommonJS export names for ESM import in node:
  132. 0 && (module.exports = {
  133. BlobReporter,
  134. currentBlobReportVersion
  135. });