Skip to content

Commit

Permalink
release: v1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Aug 10, 2024
1 parent 9e30d21 commit 079fe9b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.5.1 (10 Aug 2024)

* fix: WebSocket message base64 encoded

## 1.5.0 (31 Jul 2024)

* feat: support IndexedDB
Expand Down
14 changes: 14 additions & 0 deletions devtools/target.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
location.reload()
}
</script>
<script>
function testWs() {
const text = 'This is the text used for testing!'

const enc = new TextEncoder()
const ws = new WebSocket('wss://echo.websocket.org')

ws.onopen = function () {
ws.send(text)
ws.send(enc.encode(text))
}
}
setTimeout(() => testWs(), 5000)
</script>
<script>
// https://gist.github.com/enjalot/6472041
var indexedDB = window.indexedDB
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chobitsu",
"version": "1.5.0",
"version": "1.5.1",
"description": "Chrome devtools protocol JavaScript implementation",
"main": "dist/chobitsu.js",
"exports": {
Expand Down
28 changes: 24 additions & 4 deletions src/domains/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import isNative from 'licia/isNative'
import contain from 'licia/contain'
import now from 'licia/now'
import isStr from 'licia/isStr'
import isBlob from 'licia/isBlob'
import isUndef from 'licia/isUndef'
import convertBin from 'licia/convertBin'
import { XhrRequest, FetchRequest } from '../lib/request'
import connector from '../lib/connector'
Expand Down Expand Up @@ -222,11 +224,18 @@ function enableWebSocket() {
})
})

ws.addEventListener('message', function (e) {
ws.addEventListener('message', async function (e) {
let payloadData = e.data
if (isUndef(payloadData)) {
return
}

let opcode = 1
if (!isStr(payloadData)) {
opcode = 2
if (isBlob(payloadData)) {
payloadData = await convertBin.blobToArrBuffer(payloadData)
}
payloadData = convertBin(payloadData, 'base64')
}

Expand All @@ -242,21 +251,32 @@ function enableWebSocket() {

const origSend = ws.send
ws.send = function (data: any) {
if (!isUndef(data)) {
frameSent(data)
}

return origSend.call(this, data)
}

async function frameSent(data: any) {
let opcode = 1
let payloadData = data
if (!isStr(data)) {
opcode = 2
data = convertBin(data, 'base64')
if (isBlob(payloadData)) {
payloadData = await convertBin.blobToArrBuffer(payloadData)
}
payloadData = convertBin(data, 'base64')
}

connector.trigger('Network.webSocketFrameSent', {
requestId,
timestamp: now() / 1000,
response: {
opcode,
payloadData: data,
payloadData,
},
})
return origSend.call(this, data)
}

ws.addEventListener('close', function () {
Expand Down

0 comments on commit 079fe9b

Please sign in to comment.