Skip to content

Commit 02d8a98

Browse files
committed
refactor: avoid redundant buffer to string
1 parent 302e2f7 commit 02d8a98

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
"name": "zx/index",
1111
"path": "build/*.{js,cjs}",
12-
"limit": "809 kB",
12+
"limit": "810 kB",
1313
"brotli": false,
1414
"gzip": false
1515
},

src/cli.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from './index.js'
3131
import { installDeps, parseDeps } from './deps.js'
3232
import { startRepl } from './repl.js'
33-
import { randomId } from './util.js'
33+
import { randomId, bufToString } from './util.js'
3434
import { createRequire } from './vendor.js'
3535

3636
const EXT = '.mjs'
@@ -189,7 +189,7 @@ export async function importPath(
189189
filepath: string,
190190
origin = filepath
191191
): Promise<void> {
192-
const contents = await fs.readFile(filepath)
192+
const contents = await fs.readFile(filepath, 'utf8')
193193
const { ext, base, dir } = path.parse(filepath)
194194
const tempFilename = getFilepath(dir, base)
195195

@@ -224,7 +224,7 @@ export function transformMarkdown(buf: Buffer | string): string {
224224
let state = 'root'
225225
let codeBlockEnd = ''
226226
let prevLineIsEmpty = true
227-
for (const line of buf.toString().split(/\r?\n/)) {
227+
for (const line of bufToString(buf).split(/\r?\n/)) {
228228
switch (state) {
229229
case 'root':
230230
if (tabRe.test(line) && prevLineIsEmpty) {

src/core.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import {
4747
log,
4848
isString,
4949
isStringLiteral,
50+
bufToString,
51+
getLast,
5052
noop,
5153
once,
5254
parseBool,
@@ -64,6 +66,7 @@ export { log, type LogEntry } from './util.js'
6466
const CWD = Symbol('processCwd')
6567
const SYNC = Symbol('syncExec')
6668
const EOL = Buffer.from(_EOL)
69+
const BR_CC = '\n'.charCodeAt(0)
6770
const SIGTERM = 'SIGTERM'
6871
const ENV_PREFIX = 'ZX_'
6972
const storage = new AsyncLocalStorage<Options>()
@@ -330,8 +333,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
330333
}
331334

332335
// Ensures EOL
333-
if (stdout.length && !stdout[stdout.length - 1]!.toString().endsWith('\n')) c.on.stdout!(EOL, c)
334-
if (stderr.length && !stderr[stderr.length - 1]!.toString().endsWith('\n')) c.on.stderr!(EOL, c)
336+
if (stdout.length && getLast(getLast(stdout)) !== BR_CC) c.on.stdout!(EOL, c)
337+
if (stderr.length && getLast(getLast(stderr)) !== BR_CC) c.on.stderr!(EOL, c)
335338

336339
$.log({ kind: 'end', signal, exitCode: status, duration, error, verbose: self.isVerbose(), id })
337340
const output = self._output = new ProcessOutput(dto)
@@ -598,7 +601,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
598601
async *[Symbol.asyncIterator]() {
599602
let last: string | undefined
600603
const getLines = (chunk: Buffer | string) => {
601-
const lines = ((last || '') + chunk.toString()).split('\n')
604+
const lines = ((last || '') + bufToString(chunk)).split('\n')
602605
last = lines.pop()
603606
return lines
604607
}

src/deps.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ const builtins = new Set([
102102
const nameRe = /^(?<name>(@[a-z\d-~][\w-.~]*\/)?[a-z\d-~][\w-.~]*)\/?.*$/i
103103
const versionRe = /^@(?<version>[~^]?(v?[\dx*]+([-.][\d*a-z-]+)*))/i
104104

105-
export function parseDeps(content: Buffer | string): Record<string, string> {
106-
return depseek(content.toString() + '\n', { comments: true }).reduce<
105+
export function parseDeps(content: string): Record<string, string> {
106+
return depseek(content + '\n', { comments: true }).reduce<
107107
Record<string, string>
108108
>((m, { type, value }, i, list) => {
109109
if (type === 'dep') {

src/util.ts

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export function isString(obj: any) {
5959
return typeof obj === 'string'
6060
}
6161

62+
const utf8Decoder = new TextDecoder('utf-8')
63+
export const bufToString = (buf: Buffer | string): string =>
64+
isString(buf) ? buf : utf8Decoder.decode(buf)
65+
66+
export const getLast = <T>(arr: { length: number; [i: number]: any }): T =>
67+
arr[arr.length - 1]
68+
6269
const pad = (v: string) => (v === ' ' ? ' ' : '')
6370

6471
export function preferLocalBin(

0 commit comments

Comments
 (0)