/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
ChromeUtils.defineLazyGetter(this, "ZipWriter", function () { return Components.Constructor("@mozilla.org/zipwriter;1", "nsIZipWriter");
});
/** * Helper API for HAR export features.
*/ var HarUtils = {
getHarFileName(defaultFileName, jsonp, compress, hostname, pathname = "") { const extension = jsonp ? ".harp" : ".har";
const now = new Date();
let name = defaultFileName.replace(/%date/g, formatDate(now));
name = name.replace(/%hostname/g, hostname + pathname);
name = name.replace(/\:/gm, "-", "");
name = name.replace(/\//gm, "_", "");
let fileName = name + extension;
// Default file extension is zip if compressing is on. if (compress) {
fileName += ".zip";
}
return fileName;
},
/** * Save HAR string into a given file. The file might be compressed * if specified in the options. * * @param {File} file Target file where the HAR string (JSON) * should be stored. * @param {String} jsonString HAR data (JSON or JSONP) * @param {Boolean} compress The result file is zipped if set to true.
*/
saveToFile(file, jsonString, compress) { const openFlags =
OPEN_FLAGS.WRONLY | OPEN_FLAGS.CREATE_FILE | OPEN_FLAGS.TRUNCATE;
// The entire jsonString can be huge so, write the data in chunks. const chunkLength = 1024 * 1024; for (let i = 0; i <= jsonString.length; i++) { const data = jsonString.substr(i, chunkLength + 1); if (data) {
convertor.writeString(data);
}
// If no compressing then bail out. if (!compress) { returntrue;
}
// Remember name of the original file, it'll be replaced by a zip file. const originalFilePath = file.path; const originalFileName = file.leafName;
try { // Rename using unique name (the file is going to be removed).
file.moveTo(null, "temp" + new Date().getTime() + "temphar");
// Create compressed file with the original file path name. const zipFile = Cc["@mozilla.org/file/local;1"].createInstance(
Ci.nsIFile
);
zipFile.initWithPath(originalFilePath);
// The file within the zipped file doesn't use .zip extension.
let fileName = originalFileName; if (fileName.indexOf(".zip") == fileName.length - 4) {
fileName = fileName.substr(0, fileName.indexOf(".zip"));
}
const zip = new ZipWriter();
zip.open(zipFile, openFlags);
zip.addEntryFile(
fileName,
Ci.nsIZipWriter.COMPRESSION_DEFAULT,
file, false
);
zip.close();
// Remove the original file (now zipped).
file.remove(true); returntrue;
} catch (err) {
console.error(err);
// Something went wrong (disk space?) rename the original file back.
file.moveTo(null, originalFileName);
}
returnfalse;
},
getLocalDirectory(path) {
let dir;
if (!path) {
dir = Services.dirsvc.get("ProfD", Ci.nsIFile);
dir.append("har");
dir.append("logs");
} else {
dir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
dir.initWithPath(path);
}
return dir;
},
};
// Exports from this module
exports.HarUtils = HarUtils;
¤ Dauer der Verarbeitung: 0.26 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.