Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 43 additions & 0 deletions lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,49 @@ function bodyMixinMethods (instance, getInternalState) {
return consumeBody(this, (bytes) => {
return new Uint8Array(bytes)
}, instance, getInternalState)
},

textStream () {
const this_ = getInternalState(this)

// 1. If this is unusable, then throw a TypeError.
if (bodyUnusable(this_)) {
throw new TypeError('Body is unusable: Body has already been read')
}

// 2. If this’s body is null:
if (this_.body == null) {
// 2.1. Let emptyStream be a new ReadableStream in this’s relevant realm.
// 2.2. Set up emptyStream.
/** @type {ReadableStreamDefaultController<any>} */
let controller
const emptyStream = new ReadableStream({
start: (c) => {
controller = c
},
pull: () => Promise.resolve(),
cancel: () => Promise.resolve()
}, {
size: () => 1
})

// 2.3. Close emptyStream.
controller.close()

// 2.4. Return emptyStream.
return emptyStream
}

// 3. Let stream be this’s body’s stream.
/** @type {ReadableStream} */
const stream = this_.body.stream

// 4. Let decoder be a new TextDecoderStream object in this’s relevant realm.
// 5. Set up decoder with UTF-8.
const decoder = new TextDecoderStream('UTF-8')

// 6. Return the result of stream, piped through decoder.
return stream.pipeThrough(decoder)
}
}

Expand Down
1 change: 1 addition & 0 deletions test/types/fetch.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ expectType<Promise<FormData>>(response.formData())
expectType<Promise<Uint8Array>>(response.bytes())
expectType<Promise<unknown>>(response.json())
expectType<Promise<string>>(response.text())
expectType<Promise<ReadableStream<string>>>(response.textStream())
Comment thread
KhafraDev marked this conversation as resolved.
Outdated
expectType<Response>(response.clone())

expectType<Request>(new Request('https://example.com', { body: 'Hello, world', duplex: 'half' }))
Expand Down
61 changes: 61 additions & 0 deletions test/web-platform-tests/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -6762,6 +6762,67 @@
"success": true
}
]
},
"textstream.any.html": {
"success": true,
"cases": [
{
"name": "textStream method existence",
"success": true
},
{
"name": "Response.textStream() on consumed body throws TypeError",
"success": true
},
{
"name": "Request.textStream() on consumed body throws TypeError",
"success": true
},
{
"name": "Response.textStream() on locked body throws TypeError",
"success": true
},
{
"name": "Request.textStream() on locked body throws TypeError",
"success": true
},
{
"name": "Response.textStream() basic functionality",
"success": true
},
{
"name": "Request.textStream() basic functionality",
"success": true
},
{
"name": "textStream() handles chunked byte stream input",
"success": true
},
{
"name": "Response.textStream() with null body",
"success": true
},
{
"name": "Request.textStream() with null body",
"success": true
},
{
"name": "Response.textStream() with empty body",
"success": true
},
{
"name": "Response.textStream() ignores Content-Type charset (UTF-16LE)",
"success": true
},
{
"name": "Request.textStream() ignores Content-Type charset (UTF-16LE)",
"success": true
},
{
"name": "Response.textStream() ignores invalid Content-Type charset (invalid-charset)",
"success": true
}
]
}
},
"cors": {
Expand Down
2 changes: 1 addition & 1 deletion test/web-platform-tests/wpt
Submodule wpt updated 70053 files
1 change: 1 addition & 0 deletions types/fetch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class BodyMixin {
readonly formData: () => Promise<FormData>
readonly json: () => Promise<unknown>
readonly text: () => Promise<string>
readonly textStream: () => Promise<ReadableStream<string>>
Comment thread
KhafraDev marked this conversation as resolved.
Outdated
}

export interface SpecIterator<T, TReturn = any, TNext = undefined> {
Expand Down
Loading