Skip to content

Commit

Permalink
Prefer Uint8Array.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Oct 24, 2023
1 parent 5489e97 commit 29dfac0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/components/locatorlib/synctex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as zlib from 'zlib'
import { PdfSyncObject, parseSyncTex, Block, SyncTexJsError } from '../../lib/synctexjs/synctexjs'
import {iconvLiteSupportedEncodings} from '../../utils/convertfilename'
import {isSameRealPath} from '../../utils/pathnormalize'
import { existsPath, readFileAsBuffer } from '../../lib/lwfs/lwfs'
import { existsPath, readFileAsUint8Array } from '../../lib/lwfs/lwfs'
import type { ILogger } from '../../interfaces'
import { inspectCompact } from '../../utils/inspect'

Expand Down Expand Up @@ -85,7 +85,8 @@ export class SyncTexJs {
const synctexFileGz = synctexFile + '.gz'

try {
const s = (await readFileAsBuffer(vscode.Uri.file(synctexFile))).toString('binary')
const u8array = await readFileAsUint8Array(vscode.Uri.file(synctexFile))
const s = Buffer.from(u8array).toString('binary')
return parseSyncTex(s)
} catch (e) {
if (await existsPath(synctexFile)) {
Expand All @@ -95,7 +96,7 @@ export class SyncTexJs {
}

try {
const data = await readFileAsBuffer(vscode.Uri.file(synctexFileGz))
const data = await readFileAsUint8Array(vscode.Uri.file(synctexFileGz))
const b = zlib.gunzipSync(data)
const s = b.toString('binary')
return parseSyncTex(s)
Expand Down
8 changes: 4 additions & 4 deletions src/components/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from 'path'
import * as vscode from 'vscode'

import {decodePathWithPrefix, pdfFilePrefix} from '../utils/encodepdffilepath'
import { readFileAsBuffer } from '../lib/lwfs/lwfs'
import { readFileAsUint8Array } from '../lib/lwfs/lwfs'
import { ExternalPromise } from '../utils/externalpromise'
import type { Logger } from './logger'
import type { Viewer } from './viewer'
Expand Down Expand Up @@ -146,7 +146,7 @@ export class Server {

private sendOkResponse(
response: http.ServerResponse,
content: Buffer,
content: string | Uint8Array,
contentType: string,
{ isVeiewerHtml }: { isVeiewerHtml: boolean } = { isVeiewerHtml: false }
) {
Expand Down Expand Up @@ -185,7 +185,7 @@ export class Server {
return
}
try {
const buf: Buffer = await readFileAsBuffer(fileUri)
const buf = await readFileAsUint8Array(fileUri)
this.sendOkResponse(response, buf, 'application/pdf')
this.extension.logger.info(`[Server] Preview PDF file: ${fileUri.toString(true)}`)
} catch (e) {
Expand All @@ -198,7 +198,7 @@ export class Server {
} else if (request.url === '/config.json') {
const params = this.extension.viewer.viewerParams()
const content = JSON.stringify(params)
this.sendOkResponse(response, Buffer.from(content), 'application/json')
this.sendOkResponse(response, content, 'application/json')
return
} else {
let root: string
Expand Down
7 changes: 3 additions & 4 deletions src/lib/lwfs/lwfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function readFilePathGracefully(filepath: string): Promise<string |
}

export async function readFile(fileUri: vscode.Uri): Promise<string> {
const result = await readFileAsBuffer(fileUri)
const result = await readFileAsUint8Array(fileUri)
return result.toString()
}

Expand All @@ -53,12 +53,11 @@ export async function readFileGracefully(fileUri: vscode.Uri): Promise<string |
}
}

export async function readFileAsBuffer(fileUri: vscode.Uri): Promise<Buffer> {
export async function readFileAsUint8Array(fileUri: vscode.Uri): Promise<Uint8Array> {
if (isLocalUri(fileUri)) {
return fs.promises.readFile(fileUri.fsPath)
} else {
const resultUint8 = await vscode.workspace.fs.readFile(fileUri)
return Buffer.from(resultUint8)
return vscode.workspace.fs.readFile(fileUri)
}
}

Expand Down

0 comments on commit 29dfac0

Please sign in to comment.