Skip to content

Commit

Permalink
Merge pull request #159 from mcandeia/fix/encoding-undefined
Browse files Browse the repository at this point in the history
Defaulting to utf-8 when encoding is not present
  • Loading branch information
james-pre authored Dec 17, 2024
2 parents c6c0dc9 + f725c4b commit 14e05f2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/emulation/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { Dir, Dirent } from './dir.js';
import { dirname, join, parse, resolve } from './path.js';
import { _statfs, fd2file, fdMap, file2fd, fixError, resolveMount } from './shared.js';
import { ReadStream, WriteStream } from './streams.js';
import { FSWatcher, emitChange } from './watchers.js';
import type { GlobOptionsU, InternalOptions, NullEnc, ReaddirOptions, ReaddirOptsI, ReaddirOptsU } from './types.js';
import { FSWatcher, emitChange } from './watchers.js';
export * as constants from './constants.js';

export class FileHandle implements promises.FileHandle {
Expand Down Expand Up @@ -857,7 +857,8 @@ export async function readlink(this: V_Context, path: fs.PathLike, options?: fs.
await using handle = await _open.call(this, normalizePath(path), 'r', 0o644, false);
const value = await handle.readFile();
const encoding = typeof options == 'object' ? options?.encoding : options;
return encoding == 'buffer' ? value : value.toString(encoding! as BufferEncoding);
// always defaults to utf-8 to avoid wrangler (cloudflare) worker "unknown encoding" exception
return encoding == 'buffer' ? value : value.toString((encoding ?? 'utf-8') as BufferEncoding);
}
readlink satisfies typeof promises.readlink;

Expand Down
7 changes: 4 additions & 3 deletions src/emulation/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Buffer } from 'buffer';
import type * as fs from 'node:fs';
import type { V_Context } from '../context.js';
import { Errno, ErrnoError } from '../error.js';
import type { File } from '../file.js';
import { flagToMode, isAppendable, isExclusive, isReadable, isTruncating, isWriteable, parseFlag } from '../file.js';
Expand All @@ -12,9 +13,8 @@ import * as constants from './constants.js';
import { Dir, Dirent } from './dir.js';
import { dirname, join, parse, resolve } from './path.js';
import { _statfs, fd2file, fdMap, file2fd, fixError, resolveMount } from './shared.js';
import type { GlobOptionsU, InternalOptions, NullEnc, ReaddirOptions, ReaddirOptsI, ReaddirOptsU } from './types.js';
import { emitChange } from './watchers.js';
import type { V_Context } from '../context.js';
import type { GlobOptionsU, ReaddirOptsI, ReaddirOptsU, InternalOptions, ReaddirOptions, NullEnc } from './types.js';

export function renameSync(this: V_Context, oldPath: fs.PathLike, newPath: fs.PathLike): void {
oldPath = normalizePath(oldPath);
Expand Down Expand Up @@ -558,7 +558,8 @@ export function readlinkSync(this: V_Context, path: fs.PathLike, options?: fs.En
if (encoding == 'buffer') {
return value;
}
return value.toString(encoding!);
// always defaults to utf-8 to avoid wrangler (cloudflare) worker "unknown encoding" exception
return value.toString(encoding ?? 'utf-8');
}
readlinkSync satisfies typeof fs.readlinkSync;

Expand Down

0 comments on commit 14e05f2

Please sign in to comment.