Skip to content

Commit

Permalink
stream: add fast path for utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 16, 2022
1 parent 7675749 commit e27a518
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions lib/internal/webstreams/adapters.js
Expand Up @@ -54,6 +54,7 @@ const {
const {
createDeferredPromise,
kEmptyObject,
normalizeEncoding,
} = require('internal/util');

const {
Expand All @@ -73,6 +74,8 @@ const finished = require('internal/streams/end-of-stream');

const { UV_EOF } = internalBinding('uv');

const encoder = new TextEncoder();

/**
* @typedef {import('../../stream').Writable} Writable
* @typedef {import('../../stream').Readable} Readable
Expand Down Expand Up @@ -255,11 +258,17 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj

write(chunk, encoding, callback) {
if (typeof chunk === 'string' && decodeStrings && !objectMode) {
chunk = Buffer.from(chunk, encoding);
chunk = new Uint8Array(
chunk.buffer,
chunk.byteOffset,
chunk.byteLength);
const enc = normalizeEncoding(encoding);

if (enc === 'utf8') {
chunk = encoder.encode(chunk);
} else {
chunk = Buffer.from(chunk, encoding);
chunk = new Uint8Array(
chunk.buffer,
chunk.byteOffset,
chunk.byteLength);
}
}

function done(error) {
Expand Down Expand Up @@ -674,11 +683,17 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =

write(chunk, encoding, callback) {
if (typeof chunk === 'string' && decodeStrings && !objectMode) {
chunk = Buffer.from(chunk, encoding);
chunk = new Uint8Array(
chunk.buffer,
chunk.byteOffset,
chunk.byteLength);
const enc = normalizeEncoding(encoding);

if (enc === 'utf8') {
chunk = encoder.encode(chunk);
} else {
chunk = Buffer.from(chunk, encoding);
chunk = new Uint8Array(
chunk.buffer,
chunk.byteOffset,
chunk.byteLength);
}
}

function done(error) {
Expand Down

0 comments on commit e27a518

Please sign in to comment.