Skip to content

Commit

Permalink
support setEncoding on readables (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh authored May 31, 2024
1 parent d7c409a commit bcf577b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
24 changes: 23 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const PREMATURE_CLOSE = new Error('Premature close')

const queueTick = require('queue-tick')
const FIFO = require('fast-fifo')
const TextDecoder = require('text-decoder')

This comment has been minimized.

Copy link
@ThaUnknown

ThaUnknown Jun 28, 2024

Contributor

@mafintosh this package brought in ~21kb of dependencies on browser almost doubling streamx's bundle size ;-;

This comment has been minimized.

Copy link
@ThaUnknown

ThaUnknown Jun 28, 2024

Contributor

this really should just use w3c's text decoder, at worst maybe w3c's text decoder stream if leftovers are necessary

This comment has been minimized.

Copy link
@mafintosh

mafintosh Jul 3, 2024

Author Owner

feel free to send me a gist that uses the w3c one

This comment has been minimized.

Copy link
@ThaUnknown

ThaUnknown Jul 4, 2024

Contributor

#90


/* eslint-disable no-multi-spaces */

Expand Down Expand Up @@ -289,7 +290,11 @@ class ReadableState {
return false
}

if (this.map !== null) data = this.map(data)
if (this.map !== null) {
data = this.map(data)
if (data === null) return this.buffered < this.highWaterMark
}

this.buffered += this.byteLength(data)
this.queue.push(data)

Expand Down Expand Up @@ -686,6 +691,19 @@ class Readable extends Stream {
if (this._readableState.readAhead === false) this._duplexState &= READ_NO_READ_AHEAD
if (opts.read) this._read = opts.read
if (opts.eagerOpen) this._readableState.updateNextTick()
if (opts.encoding) this.setEncoding(opts.encoding)
}
}

setEncoding (encoding) {
const dec = new TextDecoder(encoding)
const map = this._readableState.map || echo
this._readableState.map = mapOrSkip
return this

function mapOrSkip (data) {
const next = dec.push(data)
return next === '' ? null : map(next)
}
}

Expand Down Expand Up @@ -1080,6 +1098,10 @@ function pipeline (stream, ...streams) {
}
}

function echo (s) {
return s
}

function isStream (stream) {
return !!stream._readableState || !!stream._writableState
}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"description": "An iteration of the Node.js core streams with a series of improvements",
"main": "index.js",
"dependencies": {
"fast-fifo": "^1.1.0",
"queue-tick": "^1.0.1"
"fast-fifo": "^1.3.2",
"queue-tick": "^1.0.1",
"text-decoder": "^1.1.0"
},
"devDependencies": {
"b4a": "^1.6.6",
"brittle": "^3.1.1",
"end-of-stream": "^1.4.4",
"standard": "^17.0.0"
Expand Down
33 changes: 33 additions & 0 deletions test/readable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const test = require('brittle')
const b4a = require('b4a')
const { Readable } = require('../')

test('ondata', function (t) {
Expand Down Expand Up @@ -355,6 +356,38 @@ test('no read-ahead with async iterator', async function (t) {
t.is(expectedTick, 10)
})

test('setEncoding', async function (t) {
const r = new Readable()

r.setEncoding('utf-8')
const buffer = b4a.from('hællå wørld!')
for (let i = 0; i < buffer.byteLength; i++) {
r.push(buffer.subarray(i, i + 1))
}
r.push(null)
const expected = b4a.toString(buffer).split('')
for await (const data of r) {
t.is(data, expected.shift())
}
t.is(expected.length, 0)
})

test('setEncoding respects existing map', async function (t) {
t.plan(1)

const r = new Readable({
encoding: 'utf-8',
map (data) {
return JSON.parse(data)
}
})

r.push('{"hello":"world"}')
r.once('data', function (data) {
t.alike(data, { hello: 'world' })
})
})

function nextImmediate () {
return new Promise(resolve => setImmediate(resolve))
}

0 comments on commit bcf577b

Please sign in to comment.