From d929241e79b6899d1ccf8675027806a155ef3795 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Fri, 27 Oct 2023 13:46:25 +0100 Subject: [PATCH] fix: binary is latin1 --- .../dependencies/github-dependency-resolver.ts | 2 +- .../compile/noir/file-manager/file-manager.test.ts | 2 +- .../src/compile/noir/file-manager/file-manager.ts | 14 +++++++------- .../noir/file-manager/memfs-file-manager.ts | 3 +-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/yarn-project/noir-compiler/src/compile/noir/dependencies/github-dependency-resolver.ts b/yarn-project/noir-compiler/src/compile/noir/dependencies/github-dependency-resolver.ts index 8a97c4587de4..2d8c552f6078 100644 --- a/yarn-project/noir-compiler/src/compile/noir/dependencies/github-dependency-resolver.ts +++ b/yarn-project/noir-compiler/src/compile/noir/dependencies/github-dependency-resolver.ts @@ -76,7 +76,7 @@ export class GithubDependencyResolver implements DependencyResolver { return packagePath; } - const { entries } = await unzip(this.#fm.readFileSync(archivePath, 'binary')); + const { entries } = await unzip(this.#fm.readFileSync(archivePath)); for (const entry of Object.values(entries)) { if (entry.isDirectory) { diff --git a/yarn-project/noir-compiler/src/compile/noir/file-manager/file-manager.test.ts b/yarn-project/noir-compiler/src/compile/noir/file-manager/file-manager.test.ts index 01f6025758f4..e0290a5c3b4a 100644 --- a/yarn-project/noir-compiler/src/compile/noir/file-manager/file-manager.test.ts +++ b/yarn-project/noir-compiler/src/compile/noir/file-manager/file-manager.test.ts @@ -60,7 +60,7 @@ describe.each([memFS, nodeFM])('FileManager', setup => { it('saves files and correctly reads bytes back', async () => { await fm.writeFile('test.txt', new Blob([testFileBytes]).stream()); - expect(fm.readFileSync('test.txt', 'binary')).toEqual(testFileBytes); + expect(fm.readFileSync('test.txt')).toEqual(testFileBytes); }); it('saves files and correctly reads UTF-8 string back', async () => { diff --git a/yarn-project/noir-compiler/src/compile/noir/file-manager/file-manager.ts b/yarn-project/noir-compiler/src/compile/noir/file-manager/file-manager.ts index b49699028fb1..a05b6479d3fd 100644 --- a/yarn-project/noir-compiler/src/compile/noir/file-manager/file-manager.ts +++ b/yarn-project/noir-compiler/src/compile/noir/file-manager/file-manager.ts @@ -17,7 +17,7 @@ export interface FileSystem { /** Writes a file */ writeFileSync: (path: string, data: Uint8Array) => void; /** Reads a file */ - readFileSync: (path: string, encoding: 'binary' | 'utf-8') => Uint8Array | string; + readFileSync: (path: string, encoding?: 'utf-8') => Uint8Array | string; /** Renames a file */ renameSync: (oldPath: string, newPath: string) => void; } @@ -89,9 +89,8 @@ export class FileManager { /** * Reads a file from the disk and returns a buffer * @param name - File to read - * @param encoding - Encoding to use */ - public readFileSync(name: string, encoding: 'binary'): Uint8Array; + public readFileSync(name: string): Uint8Array; /** * Reads a file from the filesystem as a string * @param name - File to read @@ -103,12 +102,13 @@ export class FileManager { * @param name - File to read * @param encoding - Encoding to use */ - public readFileSync(name: string, encoding: 'binary' | 'utf-8'): string | Uint8Array { - const data = this.#fs.readFileSync(this.#getPath(name), encoding); + public readFileSync(name: string, encoding?: 'utf-8'): string | Uint8Array { + const path = this.#getPath(name); + const data = this.#fs.readFileSync(path, encoding); - if (encoding === 'binary') { + if (!encoding) { return typeof data === 'string' - ? new TextEncoder().encode(data) + ? new TextEncoder().encode(data) // this branch shouldn't be hit, but just in case : new Uint8Array(data.buffer, data.byteOffset, data.byteLength / Uint8Array.BYTES_PER_ELEMENT); } diff --git a/yarn-project/noir-compiler/src/compile/noir/file-manager/memfs-file-manager.ts b/yarn-project/noir-compiler/src/compile/noir/file-manager/memfs-file-manager.ts index 1acf3d06cf14..20df80837b86 100644 --- a/yarn-project/noir-compiler/src/compile/noir/file-manager/memfs-file-manager.ts +++ b/yarn-project/noir-compiler/src/compile/noir/file-manager/memfs-file-manager.ts @@ -14,8 +14,7 @@ export function createMemFSFileManager(memFS: IFs = fs, dataDir = '/'): FileMana mkdirSync: memFS.mkdirSync.bind(memFS), writeFileSync: memFS.writeFileSync.bind(memFS), renameSync: memFS.renameSync.bind(memFS), - // memfs looks for `buffer` instead of `binary` - readFileSync: (path, enc) => memFS.readFileSync(path, enc === 'binary' ? 'buffer' : 'utf-8'), + readFileSync: memFS.readFileSync.bind(memFS), }, dataDir, );