Skip to content

Commit

Permalink
update docs, add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Jul 4, 2024
1 parent 3b69f1c commit 8961385
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ The `body` mixins are the most common way to format the request/response body. M

- [`.arrayBuffer()`](https://fetch.spec.whatwg.org/#dom-body-arraybuffer)
- [`.blob()`](https://fetch.spec.whatwg.org/#dom-body-blob)
- [`.bytes()`](https://fetch.spec.whatwg.org/#dom-body-bytes)
- [`.json()`](https://fetch.spec.whatwg.org/#dom-body-json)
- [`.text()`](https://fetch.spec.whatwg.org/#dom-body-text)

Expand Down
12 changes: 7 additions & 5 deletions docs/docs/api/Dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,13 @@ The `RequestOptions.method` property should not be value `'CONNECT'`.

`body` contains the following additional [body mixin](https://fetch.spec.whatwg.org/#body-mixin) methods and properties:

- `text()`
- `json()`
- `arrayBuffer()`
- `body`
- `bodyUsed`
* [`.arrayBuffer()`](https://fetch.spec.whatwg.org/#dom-body-arraybuffer)
* [`.blob()`](https://fetch.spec.whatwg.org/#dom-body-blob)
* [`.bytes()`](https://fetch.spec.whatwg.org/#dom-body-bytes)
* [`.json()`](https://fetch.spec.whatwg.org/#dom-body-json)
* [`.text()`](https://fetch.spec.whatwg.org/#dom-body-text)
* `body`
* `bodyUsed`

`body` can not be consumed twice. For example, calling `text()` after `json()` throws `TypeError`.

Expand Down
1 change: 1 addition & 0 deletions docs/docs/api/Fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This API is implemented as per the standard, you can find documentation on [MDN]

- [`.arrayBuffer()`](https://fetch.spec.whatwg.org/#dom-body-arraybuffer)
- [`.blob()`](https://fetch.spec.whatwg.org/#dom-body-blob)
- [`.bytes()`](https://fetch.spec.whatwg.org/#dom-body-bytes)
- [`.formData()`](https://fetch.spec.whatwg.org/#dom-body-formdata)
- [`.json()`](https://fetch.spec.whatwg.org/#dom-body-json)
- [`.text()`](https://fetch.spec.whatwg.org/#dom-body-text)
Expand Down
2 changes: 1 addition & 1 deletion lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function chunksConcat (chunks, length) {
// fast-path
return new Uint8Array(chunks[0])
}
const buffer = new Uint8Array(length)
const buffer = new Uint8Array(Buffer.allocUnsafeSlow(length).buffer)

let offset = 0
for (let i = 0; i < chunks.length; ++i) {
Expand Down
21 changes: 21 additions & 0 deletions test/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ describe('Readable', () => {
t.deepStrictEqual(arrayBuffer, expected)
})

test('.bytes()', async function (t) {
t = tspl(t, { plan: 1 })

function resume () {
}
function abort () {
}
const r = new Readable({ resume, abort })

r.push(Buffer.from('hello'))
r.push(Buffer.from(' world'))

process.nextTick(() => {
r.push(null)
})

const bytes = await r.bytes()

t.deepStrictEqual(bytes, new TextEncoder().encode('hello world'))
})

test('.json()', async function (t) {
t = tspl(t, { plan: 1 })

Expand Down

0 comments on commit 8961385

Please sign in to comment.