welsonjs/lib/har.js

112 lines
3.0 KiB
JavaScript
Raw Normal View History

2024-08-03 07:49:30 +00:00
// har.js
// HAR(HTTP Archive) manipulate and replay tools
2024-08-03 06:25:41 +00:00
// https://github.com/gnh1201/welsonjs
var PipeIPC = require("lib/pipe-ipc");
2024-08-03 06:53:30 +00:00
var FILE = require("lib/file");
2024-08-03 12:56:26 +00:00
var HTTP = require("lib/http");
2024-08-03 06:25:41 +00:00
var HARObject = function() {
this.filename = null;
2024-08-03 07:41:36 +00:00
this.data = {};
2024-08-03 06:25:41 +00:00
this.entryIndex = 0;
2024-08-03 06:51:47 +00:00
this.onEntry = null;
2024-08-03 08:10:34 +00:00
this.filters = [];
2024-08-03 12:56:26 +00:00
this.isSimulated = true;
2024-08-03 06:25:41 +00:00
this.setEntryIndex = function(index) {
this.entryIndex = index;
};
2024-08-03 12:56:26 +00:00
this.setIsSimulated = function(isSimulated) {
this.isSimulated = isSimulated;
};
2024-08-03 06:25:41 +00:00
this.load = function(filename) {
this.filename = filename;
2024-08-03 07:41:36 +00:00
this.data = JSON.parse(FILE.readFile(this.filename, PipeIPC.CdoUTF_8));
2024-08-03 06:25:41 +00:00
return this;
};
2024-08-03 08:49:22 +00:00
this.test = function(entry, test) {
if (typeof test !== "function")
return true;
try {
return test(this, entry, entry.request, entry.response, this.entryIndex);
} catch (e) {
console.warn(e.message);
}
return true;
};
2024-08-03 12:56:26 +00:00
this.send = function(entry, done) {
if (typeof entry === "undefined" || entry == null)
return;
2024-08-03 06:25:41 +00:00
2024-08-03 12:56:26 +00:00
var callback = null;
if (!this.isSimulated) {
callback = function() {
console.log("callback");
};
}
try {
done(this, entry, entry.request, entry.response, callback);
} catch (e) {
console.error(e.message);
}
};
this.play = function(done, edit, test) {
2024-08-03 07:41:36 +00:00
var entries = this.data.log.entries;
2024-08-03 12:56:26 +00:00
if (!this.isSimulated) {
console.warn("Take care! This is not a simulated mode.");
}
2024-08-03 08:49:22 +00:00
2024-08-03 06:25:41 +00:00
while (this.entryIndex < entries.length) {
2024-08-03 07:41:36 +00:00
var entry = entries[this.entryIndex];
2024-08-03 06:25:41 +00:00
2024-08-03 08:49:22 +00:00
if (this.test(entry, test)) {
2024-08-03 12:56:26 +00:00
if (typeof edit !== "function") {
2024-08-03 08:49:22 +00:00
console.log(
'[' + entry.startedDateTime + ']',
'"' + [entry.request.method, entry.request.url, entry.request.httpVersion].join(' ') + '"',
entry.request.url,
entry.request.httpVersion,
entry.response.status,
entry.response.content.size
);
} else {
try {
2024-08-03 12:56:26 +00:00
entry = edit(this, entry, entry.request, entry.response, this.entryIndex);
2024-08-03 08:49:22 +00:00
} catch (e) {
console.error(e.message);
}
2024-08-03 06:51:47 +00:00
}
2024-08-03 06:25:41 +00:00
}
2024-08-03 12:56:26 +00:00
this.send(entry, done);
2024-08-03 06:25:41 +00:00
this.entryIndex++;
}
2024-08-03 07:41:36 +00:00
return this;
2024-08-03 06:25:41 +00:00
};
2024-08-03 06:51:47 +00:00
this.rewind = function() {
2024-08-03 12:56:26 +00:00
this.setEntryIndex(0);
2024-08-03 07:41:36 +00:00
return this;
2024-08-03 06:51:47 +00:00
};
2024-08-03 06:25:41 +00:00
};
exports.HARObject = HARObject;
2024-08-03 12:56:26 +00:00
exports.VERSIONINFO = "HAR(HTTP Archive) manipulate and replay tools version 0.1.8";
2024-08-03 06:25:41 +00:00
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = require;