diff --git a/helpers/strategies/AxiosStrategy.js b/helpers/strategies/AxiosStrategy.js index b319932b2..583cdcd19 100644 --- a/helpers/strategies/AxiosStrategy.js +++ b/helpers/strategies/AxiosStrategy.js @@ -1,5 +1,5 @@ import axios from "axios" -// import { isJSONContentType } from "../utils/contenttypes" +import { decodeB64StringToArrayBuffer } from "../utils/b64" let cancelSource = axios.CancelToken.source() @@ -14,11 +14,19 @@ const axiosWithProxy = async (req, { state }) => { try { const { data } = await axios.post( state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/", - req, + { + ...req, + wantsBinary: true, + }, { cancelToken: cancelSource.token, } ) + + if (data.isBinary) { + data.data = decodeB64StringToArrayBuffer(data.data) + } + return data } catch (e) { // Check if the throw is due to a cancellation diff --git a/helpers/strategies/ExtensionStrategy.js b/helpers/strategies/ExtensionStrategy.js index 1231b47df..c98204c35 100644 --- a/helpers/strategies/ExtensionStrategy.js +++ b/helpers/strategies/ExtensionStrategy.js @@ -1,3 +1,5 @@ +import { decodeB64StringToArrayBuffer } from "../utils/b64" + export const hasExtensionInstalled = () => typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== "undefined" @@ -17,8 +19,16 @@ const extensionWithProxy = async (req, { state }) => { const { data } = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({ method: "post", url: state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/", - data: req, + data: { + ...req, + wantsBinary: true, + }, }) + + if (data.isBinary) { + data.data = decodeB64StringToArrayBuffer(data.data) + } + return data } diff --git a/helpers/utils/b64.js b/helpers/utils/b64.js new file mode 100644 index 000000000..8155c4fdf --- /dev/null +++ b/helpers/utils/b64.js @@ -0,0 +1,31 @@ +export const decodeB64StringToArrayBuffer = (input) => { + const bytes = Math.floor((input / 4) * 3) + const ab = new ArrayBuffer(bytes) + const uarray = new Uint8Array(ab) + + const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" + + let chr1, chr2, chr3 + let enc1, enc2, enc3, enc4 + let j = 0 + + input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "") + + for (let i = 0; i < bytes; i += 3) { + //get the 3 octets in 4 ASCII chars + enc1 = keyStr.indexOf(input.charAt(j++)) + enc2 = keyStr.indexOf(input.charAt(j++)) + enc3 = keyStr.indexOf(input.charAt(j++)) + enc4 = keyStr.indexOf(input.charAt(j++)) + + chr1 = (enc1 << 2) | (enc2 >> 4) + chr2 = ((enc2 & 15) << 4) | (enc3 >> 2) + chr3 = ((enc3 & 3) << 6) | enc4 + + uarray[i] = chr1 + if (enc3 != 64) uarray[i + 1] = chr2 + if (enc4 != 64) uarray[i + 2] = chr3 + } + + return ab +}