Skip to content

Commit

Permalink
Use TextDecoder instead of Buffer.toString
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Oct 21, 2024
1 parent 453f6d5 commit 3e51291
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 29 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const commonRules = {
"@typescript-eslint/return-await": "error",
"require-await": "off",
"@typescript-eslint/require-await": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"@typescript-eslint/unbound-method": "error",
"curly": "error",
"default-case": "error",
Expand Down
8 changes: 4 additions & 4 deletions src/components/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ export class Builder {
this.extension.logger.info(`External build process spawned. PID: ${pid}.`)

const stepLog = this.extension.compilerLog.createStepLog(rootFile, {name: 'external', command, args}, {stepIndex: 1, totalStepsLength: 1})
this.currentProcess.stdout.on('data', (newStdout: Buffer) => {
this.currentProcess.stdout.on('data', (newStdout: Uint8Array) => {
stepLog.append(newStdout)
})

this.currentProcess.stderr.on('data', (newStderr: Buffer) => {
this.currentProcess.stderr.on('data', (newStderr: Uint8Array) => {
stepLog.appendError(newStderr)
})

Expand Down Expand Up @@ -268,11 +268,11 @@ export class Builder {

const stepLog = this.extension.compilerLog.createStepLog(rootFile, step, {stepIndex, totalStepsLength})

this.currentProcess.stdout.on('data', (newStdout: Buffer) => {
this.currentProcess.stdout.on('data', (newStdout: Uint8Array) => {
stepLog.append(newStdout)
})

this.currentProcess.stderr.on('data', (newStderr: Buffer) => {
this.currentProcess.stderr.on('data', (newStderr: Uint8Array) => {
stepLog.appendError(newStderr)
})

Expand Down
9 changes: 5 additions & 4 deletions src/components/compilerlog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CompilerLogParser, LogEntry } from './compilerloglib/core.js'
import type { Completer } from '../providers/completion.js'
import type { Logger } from './logger.js'
import type { Manager } from './manager.js'
import { decodeUtf8 } from '../utils/utf8.js'

export class BuildStepLog {
private buffer = ''
Expand Down Expand Up @@ -35,13 +36,13 @@ export class BuildStepLog {
return this.buffer
}

append(message: Buffer) {
this.buffer += message
append(message: Uint8Array) {
this.buffer += decodeUtf8(message)
this.logPanel.append(message.toString())
}

appendError(message: Buffer) {
this.errorBuffer += message
appendError(message: Uint8Array) {
this.errorBuffer += decodeUtf8(message)
}

dispose() {
Expand Down
7 changes: 4 additions & 3 deletions src/components/linterlib/linterutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EOL } from 'node:os'
import type { Logger } from '../logger.js'
import { ExternalPromise } from '../../utils/externalpromise.js'
import { decodeXUserDefined } from '../../utils/xuserdefined.js'
import { decodeUtf8 } from '../../utils/utf8.js'

export class LinterUtil {
readonly #currentProcesses = Object.create(null) as Record<string, ChildProcessWithoutNullStreams>
Expand All @@ -22,13 +23,13 @@ export class LinterUtil {
const proc = this.#currentProcesses[linterId]

let stdout = ''
proc.stdout.on('data', (newStdout: Buffer) => {
proc.stdout.on('data', (newStdout: Uint8Array) => {
stdout += decodeXUserDefined(newStdout)
})

let stderr = ''
proc.stderr.on('data', (newStderr: Buffer) => {
stderr += newStderr
proc.stderr.on('data', (newStderr: Uint8Array) => {
stderr += decodeUtf8(newStderr)
})

const resutlPromise = new ExternalPromise<string>()
Expand Down
21 changes: 11 additions & 10 deletions src/components/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { Viewer } from './viewer.js'
import { ExternalPromise } from '../utils/externalpromise.js'
import { inspectCompact } from '../utils/inspect.js'
import { decodeXUserDefined } from '../utils/xuserdefined.js'
import { decodeUtf8 } from '../utils/utf8.js'

export interface SyncTeXRecordForward {
page: number,
Expand Down Expand Up @@ -183,13 +184,13 @@ export class Locator {
const proc = cp.spawn(command, args, {cwd: path.dirname(pdfFile)})

let stdout = ''
proc.stdout.on('data', (newStdout: Buffer) => {
proc.stdout.on('data', (newStdout: Uint8Array) => {
stdout += decodeXUserDefined(newStdout)
})

let stderr = ''
proc.stderr.on('data', (newStderr: Buffer) => {
stderr += newStderr
proc.stderr.on('data', (newStderr: Uint8Array) => {
stderr += decodeUtf8(newStderr)
})

const resultPromise = new ExternalPromise<SyncTeXRecordForward>()
Expand Down Expand Up @@ -231,13 +232,13 @@ export class Locator {
const proc = cp.spawn(command, args, {cwd: path.dirname(pdfPath)})

let stdout = ''
proc.stdout.on('data', (newStdout: Buffer) => {
proc.stdout.on('data', (newStdout: Uint8Array) => {
stdout += decodeXUserDefined(newStdout)
})

let stderr = ''
proc.stderr.on('data', (newStderr: Buffer) => {
stderr += newStderr
proc.stderr.on('data', (newStderr: Uint8Array) => {
stderr += decodeUtf8(newStderr)
})

const resultPromise = new ExternalPromise<SyncTeXRecordBackward>
Expand Down Expand Up @@ -473,12 +474,12 @@ export class Locator {
this.extension.logger.logCommand('Execute external SyncTeX command', command, args)
const proc = cp.spawn(command, args)
let stdout = ''
proc.stdout.on('data', (newStdout: Buffer) => {
stdout += newStdout
proc.stdout.on('data', (newStdout: Uint8Array) => {
stdout += decodeUtf8(newStdout)
})
let stderr = ''
proc.stderr.on('data', (newStderr: Buffer) => {
stderr += newStderr
proc.stderr.on('data', (newStderr: Uint8Array) => {
stderr += decodeUtf8(newStderr)
})
const cb = () => {
void this.extension.logger.info(`The external SyncTeX command stdout: ${stdout}`)
Expand Down
9 changes: 5 additions & 4 deletions src/components/texdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as cs from 'cross-spawn'
import type { Logger } from './logger.js'
import type { Manager } from './manager.js'
import { ExternalPromise } from '../utils/externalpromise.js'
import { decodeUtf8 } from '../utils/utf8.js'


export class TeXDoc {
Expand All @@ -21,13 +22,13 @@ export class TeXDoc {
const proc = cs.spawn(texdocPath, texdocArgs)

let stdout = ''
proc.stdout.on('data', (newStdout: Buffer) => {
stdout += newStdout
proc.stdout.on('data', (newStdout: Uint8Array) => {
stdout += decodeUtf8(newStdout)
})

let stderr = ''
proc.stderr.on('data', (newStderr: Buffer) => {
stderr += newStderr
proc.stderr.on('data', (newStderr: Uint8Array) => {
stderr += decodeUtf8(newStderr)
})

const resultPromise = new ExternalPromise<void>()
Expand Down
9 changes: 5 additions & 4 deletions src/components/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { Server } from './server.js'
import type { LwStatusBarItem } from './statusbaritem.js'
import { ExternalPromise } from '../utils/externalpromise.js'
import { inspectCompact } from '../utils/inspect.js'
import { decodeUtf8 } from '../utils/utf8.js'
export { PdfViewerHookProvider } from './viewerlib/pdfviewerhook.js'


Expand Down Expand Up @@ -236,12 +237,12 @@ export class Viewer {
const proc = cs.spawn(command, args, {cwd: path.dirname(sourceFile), detached: true})
const resultPromise = new ExternalPromise<void>()
let stdout = ''
proc.stdout.on('data', (newStdout: Buffer) => {
stdout += newStdout
proc.stdout.on('data', (newStdout: Uint8Array) => {
stdout += decodeUtf8(newStdout)
})
let stderr = ''
proc.stderr.on('data', (newStderr: Buffer) => {
stderr += newStderr
proc.stderr.on('data', (newStderr: Uint8Array) => {
stderr += decodeUtf8(newStderr)
})
const cb = () => {
void this.extension.logger.info(`The external PDF viewer stdout: ${stdout}`)
Expand Down
10 changes: 10 additions & 0 deletions src/utils/utf8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const decoder = new TextDecoder()
const encoder = new TextEncoder()

export function decodeUtf8(bytes: Uint8Array) {
return decoder.decode(bytes)
}

export function encodeUtf8(str: string) {
return encoder.encode(str)
}

0 comments on commit 3e51291

Please sign in to comment.