RFC: Isomorphic response #1284
Replies: 3 comments 1 reply
-
Responding with
|
Beta Was this translation helpful? Give feedback.
-
UPDATED Although I do not know
I think using strings to present data is not the best idea. In JS, its string is a list of UTF-16 char codes. More Information. Each char code holds two bytes of space. But we cannot use it as a bytes array --- some char code list is not a valid string. JS will replace invalid character to be So if we want to store binary data into a string, we will: const a = new Uint8Array([12, 255, 56])
let str = ''
for (let i = 0; i < a.length; i++) {
str += String.fromCharCode(a[i])
} We can make sure each bi-bytes' range is from I do not know |
Beta Was this translation helpful? Give feedback.
-
This may be made obsolete by #1404. |
Beta Was this translation helpful? Give feedback.
-
Goals
The isomorphic response aims to achieve two primary things.
Standardized response representation
An HTTP response can be represented in multiple ways depending on the environment, request type, or even the framework you’re using. For example:
http
usesIncomingMessage
.Response
from Fetch API.XMLHttpRequest
(never exposed directly to the consumer).But in actuality, the intentions behind any response are the same: describe an HTTP response. I agree that responses may include additional information but we’re speaking about a barebone response representation:
The biggest complexity and, as a result, the biggest divergence between response representations in different tools come from the fact that those tools also execute responses. That’s where sockets, agents, and friends come into play, adding the execution capabilities to otherwise static response representation.
Stringified response representation
Apart from that, any response should be serializable. This way the intention behind the response can be represented as plain text so it’s possible to signal that intention to other tools. For example, to a Service Worker via a message channel, or between Node processes via IPC.
That is not possible with any object-based response representation because you cannot serialize functions. Such implementations often describe certain properties like getters (i.e.
IncomingMessage.headers
), so their values cannot be serialized unless explicitly read.Implementation
Below, is a draft of what such an isomoprhic response API may look like from the consumer’s (MSW) perspective.
This way, during the request handling, MSW could create an
IsomorphicResponse
instance in theres()
composition chain, and send that instance to either the Service Worker or to the Node.js interceptor.Response serialization
In order to be reconstructed, a serialized response must contain the following information:
ResponseInit
(status
,statusText
,headers
).text
,json
,Uint8Array
,FormData
, etc.). This way it’s apparent how to parse the string body representation.Serialized format
Proposal 1: a plain string
Proposal 2: formatted string
Something similar to chat CURL prints out.
Benefits:
Downsides:
.split(',')
.Questions
IsomorphicResponse
be compatible withfetch.Response
?type
,ok()
,url
, etc.fetch.Response
? Like the Service Worker’sevent.respondWith()
in the “fetch” event listener?ReadableStream
) with text?.from()
.IsomorphicResponse
implementation injected? If it needs to de-serialize the stringified response, it needs the logic to do that based on the response type (i.e. to properly emulateReadableStream
mocked responses).Beta Was this translation helpful? Give feedback.
All reactions