Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defaulting to utf-8 when encoding is not present #159

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Defaulting to utf-8 when encoding is not present
Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
  • Loading branch information
mcandeia committed Dec 17, 2024
commit f725c4b426cb249f431e7402be7413de31b000a4
5 changes: 3 additions & 2 deletions src/emulation/promises.ts
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@
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 {
@@ -231,7 +231,7 @@
/**
* @todo Implement
*/
public readLines(options?: promises.CreateReadStreamOptions): ReadlineInterface {

Check warning on line 234 in src/emulation/promises.ts

GitHub Actions / CI

'options' is defined but never used
throw ErrnoError.With('ENOSYS', this.file.path, 'FileHandle.readLines');
}

@@ -857,7 +857,8 @@
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;

@@ -911,7 +912,7 @@
*/
export async function realpath(this: V_Context, path: fs.PathLike, options: fs.BufferEncodingOption): Promise<Buffer>;
export async function realpath(this: V_Context, path: fs.PathLike, options?: fs.EncodingOption | BufferEncoding): Promise<string>;
export async function realpath(this: V_Context, path: fs.PathLike, options?: fs.EncodingOption | BufferEncoding | fs.BufferEncodingOption): Promise<string | Buffer> {

Check warning on line 915 in src/emulation/promises.ts

GitHub Actions / CI

'options' is defined but never used
path = normalizePath(path);
const ctx_path = (this?.root || '') + path;
if (cache.paths.hasAsync(ctx_path)) return cache.paths.getAsync(ctx_path)!;
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';
@@ -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);
@@ -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;

Loading