Skip to content

Commit

Permalink
Convert content only if it has been modified
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWr committed Mar 21, 2024
1 parent 889ef7e commit 328e90b
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/scriptlets/json-prune-xhr-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,37 +246,36 @@ export function jsonPruneXhrResponse(
nativeStringify,
},
);
// Convert content to appropriate response type, only if it has been modified
try {
const { responseType } = thisArg;
switch (responseType) {
case '':
case 'text':
modifiedContent = nativeStringify(modifiedContent);
break;
case 'arraybuffer':
modifiedContent = new TextEncoder()
.encode(nativeStringify(modifiedContent))
.buffer;
break;
case 'blob':
modifiedContent = new Blob([nativeStringify(modifiedContent)]);
break;
default:
break;
}
} catch (error) {
const message = `Response body cannot be converted to reponse type: '${content}'`;
logMessage(source, message);
modifiedContent = content;
}
}
} catch (error) {
const message = `Response body cannot be converted to json: '${content}'`;
logMessage(source, message);
modifiedContent = content;
}

// Convert content to appropriate response type
try {
const { responseType } = thisArg;
switch (responseType) {
case '':
case 'text':
modifiedContent = nativeStringify(modifiedContent);
break;
case 'arraybuffer':
modifiedContent = new TextEncoder()
.encode(nativeStringify(modifiedContent))
.buffer;
break;
case 'blob':
modifiedContent = new Blob([nativeStringify(modifiedContent)]);
break;
default:
break;
}
} catch (error) {
const message = `Response body cannot be converted to reponse type: '${content}'`;
logMessage(source, message);
modifiedContent = content;
}
}

// Manually put required values into target XHR object
Expand Down

0 comments on commit 328e90b

Please sign in to comment.