Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(worker): read request/response body from as ArrayBuffer #1332

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/mockServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,24 @@ async function handleRequest(event, requestId) {
if (client && activeClientIds.has(client.id)) {
;(async function () {
const clonedResponse = response.clone()
sendToClient(client, {
const body =
clonedResponse.body === null
? undefined
: await clonedResponse.arrayBuffer()
const message = {
type: 'RESPONSE',
payload: {
requestId,
type: clonedResponse.type,
ok: clonedResponse.ok,
status: clonedResponse.status,
statusText: clonedResponse.statusText,
body:
clonedResponse.body === null ? null : await clonedResponse.text(),
body,
headers: Object.fromEntries(clonedResponse.headers.entries()),
redirected: clonedResponse.redirected,
},
})
}
sendToClient(client, message, body && [body])
})()
}

Expand Down Expand Up @@ -200,7 +204,7 @@ async function getResponse(event, client, requestId) {

function passthrough() {
// Clone the request because it might've been already used
// (i.e. its body has been read and sent to the cilent).
// (i.e. its body has been read and sent to the client).
const headers = Object.fromEntries(clonedRequest.headers.entries())

// Remove MSW-specific request headers so the bypassed requests
Expand Down Expand Up @@ -239,7 +243,7 @@ async function getResponse(event, client, requestId) {
)

// Notify the client that a request has been intercepted.
const clientMessage = await sendToClient(client, {
const message = {
type: 'REQUEST',
payload: {
id: requestId,
Expand All @@ -254,11 +258,14 @@ async function getResponse(event, client, requestId) {
redirect: request.redirect,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
body: await request.text(),
body: await request.arrayBuffer(),
bodyUsed: request.bodyUsed,
keepalive: request.keepalive,
},
})
}
const clientMessage = await sendToClient(client, message, [
message.payload.body,
])

switch (clientMessage.type) {
case 'MOCK_RESPONSE': {
Expand Down Expand Up @@ -304,7 +311,7 @@ This exception has been gracefully handled as a 500 response, however, it's stro
return passthrough()
}

function sendToClient(client, message) {
function sendToClient(client, message, transfer) {
return new Promise((resolve, reject) => {
const channel = new MessageChannel()

Expand All @@ -316,7 +323,7 @@ function sendToClient(client, message) {
resolve(event.data)
}

client.postMessage(message, [channel.port2])
client.postMessage(message, [channel.port2].concat(transfer || []))
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/setupWorker/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export interface ServiceWorkerIncomingRequest extends RequestWithoutMethods {
id: string

/**
* Text response body.
* Response body.
*/
body?: string
body?: ArrayBuffer
}

export type ServiceWorkerIncomingResponse = Pick<
Expand Down
7 changes: 1 addition & 6 deletions src/utils/request/parseWorkerRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { encodeBuffer } from '@mswjs/interceptors'
import { Headers } from 'headers-polyfill'
import { ServiceWorkerIncomingRequest } from '../../setupWorker/glossary'
import { MockedRequest } from './MockedRequest'
Expand All @@ -13,9 +12,5 @@ export function parseWorkerRequest(
const url = new URL(rawRequest.url)
const headers = new Headers(rawRequest.headers)

return new MockedRequest(url, {
...rawRequest,
body: encodeBuffer(rawRequest.body || ''),
headers,
})
return new MockedRequest(url, { ...rawRequest, headers })
}
38 changes: 0 additions & 38 deletions src/utils/request/pruneGetRequestBody.test.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/utils/request/pruneGetRequestBody.ts

This file was deleted.