GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: use Buffer.from when constructor is a Buffer · nodejs/node@ee4fc7d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/webstreams
    • readablestream.js
  • test/parallel
    • test-whatwg-readablebytestreambyob.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/webstreams/readablestream.js‎

Copy file name to clipboardExpand all lines: lib/internal/webstreams/readablestream.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ const {
130130
writableStreamDefaultWriterWrite,
131131
} = require('internal/webstreams/writablestream');
132132

133+
const { Buffer } = require('buffer');
134+
133135
const assert = require('internal/assert');
134136

135137
const kCancel = Symbol('kCancel');
@@ -1846,6 +1848,11 @@ function readableByteStreamControllerConvertPullIntoDescriptor(desc) {
18461848
throw new ERR_INVALID_STATE.RangeError('The buffer size is invalid');
18471849
assert(!(bytesFilled % elementSize));
18481850
const transferredBuffer = transferArrayBuffer(buffer);
1851+
1852+
if (ctor === Buffer) {
1853+
return Buffer.from(transferredBuffer, byteOffset, bytesFilled / elementSize);
1854+
}
1855+
18491856
return new ctor(transferredBuffer, byteOffset, bytesFilled / elementSize);
18501857
}
18511858

Collapse file

‎test/parallel/test-whatwg-readablebytestreambyob.js‎

Copy file name to clipboard
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const {
6+
open,
7+
} = require('fs/promises');
8+
9+
const {
10+
Buffer,
11+
} = require('buffer');
12+
13+
class Source {
14+
async start(controller) {
15+
this.file = await open(__filename);
16+
this.controller = controller;
17+
}
18+
19+
async pull(controller) {
20+
const byobRequest = controller.byobRequest;
21+
const view = byobRequest.view;
22+
23+
const {
24+
bytesRead,
25+
} = await this.file.read({
26+
buffer: view,
27+
offset: view.byteOffset,
28+
length: view.byteLength
29+
});
30+
31+
if (bytesRead === 0) {
32+
await this.file.close();
33+
this.controller.close();
34+
}
35+
36+
byobRequest.respond(bytesRead);
37+
}
38+
39+
get type() { return 'bytes'; }
40+
41+
get autoAllocateChunkSize() { return 1024; }
42+
}
43+
44+
(async () => {
45+
const source = new Source();
46+
const stream = new ReadableStream(source);
47+
48+
const { emitWarning } = process;
49+
50+
process.emitWarning = common.mustNotCall();
51+
52+
try {
53+
const reader = stream.getReader({ mode: 'byob' });
54+
55+
let result;
56+
do {
57+
result = await reader.read(Buffer.alloc(100));
58+
} while (!result.done);
59+
} finally {
60+
process.emitWarning = emitWarning;
61+
}
62+
})().then(common.mustCall());

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.