Skip to content

Commit

Permalink
fix: support correct to base64 for non-latin (#3031)
Browse files Browse the repository at this point in the history
Closes #3019
  • Loading branch information
Lightning00Blade authored Jan 24, 2025
1 parent 7c7de38 commit 6fe665f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
3 changes: 1 addition & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default [
'plugin:@typescript-eslint/recommended-requiring-type-checking',
)
.map((config) => ({
name: 'Plugin rules',
name: 'TypeScript plugins',
...config,
files: ['**/*.ts'],
})),
Expand Down Expand Up @@ -258,7 +258,6 @@ export default [
],
},
},

{
name: 'Fix test unused',
files: ['**/*.ts'],
Expand Down
3 changes: 2 additions & 1 deletion src/bidiMapper/modules/network/NetworkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
computeHeadersSize,
getTiming,
networkHeaderFromCookieHeaders,
stringToBase64,
} from './NetworkUtils.js';

const REALM_REGEX = /(?<=realm=").*(?=")/;
Expand Down Expand Up @@ -1105,7 +1106,7 @@ function getCdpBodyFromBiDiBytesValue(
): string | undefined {
let parsedBody: string | undefined;
if (body?.type === 'string') {
parsedBody = btoa(body.value);
parsedBody = stringToBase64(body.value);
} else if (body?.type === 'base64') {
parsedBody = body.value;
}
Expand Down
19 changes: 19 additions & 0 deletions src/bidiMapper/modules/network/NetworkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ export function computeHeadersSize(headers: Network.Header[]): number {
return new TextEncoder().encode(requestHeaders).length;
}

export function stringToBase64(str: string): string {
return typedArrayToBase64(new TextEncoder().encode(str));
}

function typedArrayToBase64(typedArray: Uint8Array): string {
// chunkSize should be less V8 limit on number of arguments!
// https://github.com/v8/v8/blob/d3de848bea727518aee94dd2fd42ba0b62037a27/src/objects/code.h#L444
const chunkSize = 65534;
const chunks = [];

for (let i = 0; i < typedArray.length; i += chunkSize) {
const chunk = typedArray.subarray(i, i + chunkSize);
chunks.push(String.fromCodePoint.apply(null, chunk as unknown as number[]));
}

const binaryString = chunks.join('');
return btoa(binaryString);
}

/** Converts from CDP Network domain headers to BiDi network headers. */
export function bidiNetworkHeadersFromCdpNetworkHeaders(
headers?: Protocol.Network.Headers,
Expand Down
69 changes: 69 additions & 0 deletions tests/network/test_provide_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,72 @@ async def test_provide_response_remove_intercept_inflight_request(
},
"type": "event",
}


@pytest.mark.asyncio
async def test_provide_response_works_with_non_latin(websocket, context_id,
url_example):
await subscribe(websocket,
["network.responseStarted", "network.responseCompleted"],
[context_id])

await execute_command(
websocket, {
"method": "network.addIntercept",
"params": {
"phases": ["responseStarted"],
"urlPatterns": [{
"type": "string",
"pattern": url_example,
}, ],
},
})

await send_JSON_command(
websocket, {
"method": "browsingContext.navigate",
"params": {
"url": url_example,
"context": context_id,
"wait": "complete",
}
})

event_response = await wait_for_event(websocket, "network.responseStarted")
network_id = event_response["params"]["request"]["request"]

await execute_command(
websocket, {
"method": "network.provideResponse",
"params": {
"request": network_id,
"headers": [{
"name": "Content-Type",
"value": {
"type": "string",
"value": "text/plain; charset=utf-8",
}
}],
"body": {
"type": "string",
"value": "Hello 🌍!",
},
},
})

event_response = await wait_for_event(websocket,
"network.responseCompleted")

result = await execute_command(
websocket, {
"method": "script.evaluate",
"params": {
"expression": "document.documentElement.innerText",
"target": {
"context": context_id
},
"awaitPromise": False,
}
})

assert result['result']['value'] == "Hello 🌍!"

0 comments on commit 6fe665f

Please sign in to comment.