diff --git a/fetch.bs b/fetch.bs index 44548a758..41cb5141d 100644 --- a/fetch.bs +++ b/fetch.bs @@ -2950,9 +2950,10 @@ and a response response, is to run these steps: false. -
Wait for 1024 bytes of response's body or end-of-file, - whichever comes first and let bytes be those bytes. - +
Let bytes be the result of running + obtain a copy of the first 1024 bytes of response given response. + +
If bytes is failure, then return false.
If the audio or video type pattern matching algorithm given bytes does not @@ -2990,6 +2991,79 @@ and a response response, is to run these steps:
If mimeType's essence starts with
"audio/
", "image/
", or "video/
", then return false.
+
Return determine if response is JavaScript and not JSON given response. + + +
To obtain a copy of the first 1024 bytes of response, given a response +response, run these steps: + +
Let first1024Bytes be null. + +
In parallel: + +
Let bytes be the empty byte sequence. + +
Let transformStream be a new {{TransformStream}}. + +
Let transformAlgorithm given a chunk be these steps: + +
Enqueue chunk in transformStream. + +
If first1024Bytes is null, then: + +
Let chunkBytes be + a copy of the bytes held by + chunk. + +
Append chunkBytes to bytes. + +
If bytes's length is greater than 1024, then: + +
Truncate bytes from the end so that it only contains 1024 bytes. + +
Set first1024Bytes to bytes. +
Let flushAlgorithm be this step: if first1024Bytes is null, then set + first1024Bytes to bytes. + +
Set up transformStream with + transformAlgorithm set to + transformAlgorithm and flushAlgorithm set + to flushAlgorithm. + +
Set response's body's stream to the result + of response's body's stream + piped through transformStream. +
Wait until first1024Bytes is non-null or response's + body's stream is errored. + +
If first1024Bytes is null, then return failure. + +
To determine if response is JavaScript and not JSON given a response +response, run these steps:
+ +Let responseBodyBytes be null.
Assert: responseBodyBytes is a byte sequence. -
If parse JSON bytes to a JavaScript value given responseBodyBytes does not - throw, then return false. If it throws, catch the exception and ignore it. +
If parse JSON bytes to a JavaScript value given responseBodyBytes does not + throw, then return false. If it throws, catch the exception and ignore it. + +
If there is an exception, response is not JSON. If there is not, it is.
Let sourceText be the result of decoding responseBodyBytes given request's