Skip to content
Myr

split.js

This file

Path

lib/stdio/split.js

95 lines · 2.7 KiB · JavaScript

Last changed by

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

History of this file

Take it

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 1why, one step back
1// Split chunks line-wise for generators passed to the `std*` options

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 2why, one step back
2export const getSplitLinesGenerator = ({binary, preserveNewlines, writableObjectMode, state}) => {

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 3–6why, one step back
3 if (binary || writableObjectMode) {
4 return;5 }6

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 7why, one step back
7 state.previousChunks = '';

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 8why, one step back
8 return {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 9why, one step back
9 transform: splitGenerator.bind(undefined, state, preserveNewlines),

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 10–14why, one step back
10 final: linesFinal.bind(undefined, state),
11 };12};1314// This imperative logic is much faster than using `String.split()` and uses very low memory.

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 15why, one step back
15const splitGenerator = function * (state, preserveNewlines, chunk) {

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 16–19why, one step back
16 let {previousChunks} = state;
17 let start = -1;1819 for (let end = 0; end < chunk.length; end += 1) {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 20–21why, one step back
20 if (chunk[end] === '\n') {
21 const newlineLength = getNewlineLength(chunk, end, preserveNewlines, state);

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

line 22why, one step back
22 let line = chunk.slice(start + 1, end + 1 - newlineLength);

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 23–24why, one step back
23
24 if (previousChunks.length > 0) {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 25–26why, one step back
25 line = concatString(previousChunks, line);
26 previousChunks = '';

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 27–34why, one step back
27 }
2829 yield line;30 start = end;31 }32 }3334 if (start !== chunk.length - 1) {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 35why, one step back
35 previousChunks = concatString(previousChunks, chunk.slice(start + 1));

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 36–40why, one step back
36 }
3738 state.previousChunks = previousChunks;39};40

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 41why, one step back
41const getNewlineLength = (chunk, end, preserveNewlines, state) => {

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 42–45why, one step back
42 if (preserveNewlines) {
43 return 0;44 }45

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 46why, one step back
46 state.isWindowsNewline = end !== 0 && chunk[end - 1] === '\r';

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 47–49why, one step back
47 return state.isWindowsNewline ? 2 : 1;
48};49

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

lines 50–53why, one step back
50const linesFinal = function * ({previousChunks}) {
51 if (previousChunks.length > 0) {52 yield previousChunks;53 }

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 54–62why, one step back
54};
5556// Unless `preserveNewlines: true` is used, we strip the newline of each line.57// This re-adds them after the user `transform` code has run.58export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state}) => binary || preserveNewlines || readableObjectMode59 ? undefined60 : {transform: appendNewlineGenerator.bind(undefined, state)};6162const appendNewlineGenerator = function * ({isWindowsNewline = false}, chunk) {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 63why, one step back
63 const {unixNewline, windowsNewline, LF, concatBytes} = typeof chunk === 'string' ? linesStringInfo : linesUint8ArrayInfo;

Strip newlines in transforms (#919)ehmickybefore adoption26e9150

lines 64–71why, one step back
64
65 if (chunk.at(-1) === LF) {66 yield chunk;67 return;68 }6970 const newline = isWindowsNewline ? windowsNewline : unixNewline;71 yield concatBytes(chunk, newline);

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 72–94why, one step back
72};
7374const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;7576const linesStringInfo = {77 windowsNewline: '\r\n',78 unixNewline: '\n',79 LF: '\n',80 concatBytes: concatString,81};8283const concatUint8Array = (firstChunk, secondChunk) => {84 const chunk = new Uint8Array(firstChunk.length + secondChunk.length);85 chunk.set(firstChunk, 0);86 chunk.set(secondChunk, firstChunk.length);87 return chunk;88};8990const linesUint8ArrayInfo = {91 windowsNewline: new Uint8Array([0x0D, 0x0A]),92 unixNewline: new Uint8Array([0x0A]),93 LF: 0x0A,94 concatBytes: concatUint8Array,

Fix strings returned by transforms in `objectMode` (#917)ehmickybefore adoption98f403c

line 95why, one step back
95};