Skip to content

Commit

Permalink
fix: binary is latin1
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Oct 27, 2023
1 parent aaeb582 commit d929241
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down

0 comments on commit d929241

Please sign in to comment.