aboutsummaryrefslogtreecommitdiff
path: root/bg
diff options
context:
space:
mode:
authorhackademix <giorgio@maone.net>2018-07-31 00:49:30 +0200
committerhackademix <giorgio@maone.net>2018-07-31 00:49:30 +0200
commit4197a81f1e81ceb57e6cbab566e82727c7554ab8 (patch)
tree162797f9b070d3f5e4be2be57f06526e6f608255 /bg
parent5e0ab1515ab1c399a6405f579c3b931563e4020d (diff)
Stateful response processing support.
Diffstat (limited to 'bg')
-rw-r--r--bg/ResponseProcessor.js12
1 files changed, 4 insertions, 8 deletions
diff --git a/bg/ResponseProcessor.js b/bg/ResponseProcessor.js
index 400cb42..0d6e36f 100644
--- a/bg/ResponseProcessor.js
+++ b/bg/ResponseProcessor.js
@@ -73,8 +73,9 @@ class ResponseTextFilter {
process(handler) {
if (!this.canProcess) return ResponseProcessor.ACCEPT;
let {metaData, request} = this;
+ let response = {request, metaData}; // we keep it around allowing callbacks to store state
if (typeof handler.pre === "function") {
- let res = handler.pre({request, metaData});
+ let res = handler.pre(response);
if (res) return res;
if (handler.post) handler = handler.post;
if (typeof handler !== "function") ResponseProcessor.ACCEPT;
@@ -91,22 +92,17 @@ class ResponseTextFilter {
filter.onstop = async event => {
let decoder = metaData.createDecoder();
let params = {stream: true};
- let text = this.text = buffer.map(
+ response.text = buffer.map(
chunk => decoder.decode(chunk, params))
.join('');
let editedText = null;
try {
- let response = {
- request,
- metaData,
- text,
- };
editedText = await handler(response);
} catch(e) {
console.error(e);
}
if (metaData.forcedUTF8 ||
- editedText !== null && text !== editedText) {
+ editedText !== null && response.text !== editedText) {
// if we changed the charset, the text or both, let's re-encode
filter.write(new TextEncoder().encode(editedText));
} else {