( id: string, type: T, payload: ResponseMessageMap[T], )
| 264 | } |
| 265 | |
| 266 | export async function encodeResponseMessage<T extends keyof ResponseMessageMap>( |
| 267 | id: string, |
| 268 | type: T, |
| 269 | payload: ResponseMessageMap[T], |
| 270 | ): Promise<EncodedMessage> { |
| 271 | if (type === MessageType.EVENT_ITERATOR || type === MessageType.ABORT_SIGNAL) { |
| 272 | return encodeRawMessage(serializeResponseMessage(id, type, payload)) |
| 273 | } |
| 274 | |
| 275 | const response = payload as StandardResponse |
| 276 | const { body: processedBody, headers: processedHeaders } = await serializeBodyAndHeaders( |
| 277 | response.body, |
| 278 | response.headers, |
| 279 | ) |
| 280 | |
| 281 | const modifiedResponse: StandardResponse = { |
| 282 | ...response, |
| 283 | body: processedBody instanceof Blob ? undefined : processedBody, |
| 284 | headers: processedHeaders, |
| 285 | } |
| 286 | |
| 287 | const baseMessage = serializeResponseMessage(id, MessageType.RESPONSE, modifiedResponse) |
| 288 | |
| 289 | if (processedBody instanceof Blob) { |
| 290 | return encodeRawMessage(baseMessage, processedBody) |
| 291 | } |
| 292 | |
| 293 | return encodeRawMessage(baseMessage) |
| 294 | } |
| 295 | |
| 296 | export async function decodeResponseMessage(raw: EncodedMessage): Promise<DecodedResponseMessage> { |
| 297 | const { json: message, buffer } = await decodeRawMessage(raw) |
no test coverage detected