mirror of
https://github.com/n8n-io/n8n.git
synced 2026-06-05 02:59:27 +02:00
fix(core): Preserve filesystem binding when reading workspace files (no-changelog) (#31667)
Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
parent
43d32fd28f
commit
001d242af6
|
|
@ -46,6 +46,29 @@ describe('workspace-files', () => {
|
|||
expect(target.sandbox?.executeCommand).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('preserves the filesystem as `this` when reading', async () => {
|
||||
// Mirrors LazyRuntimeFilesystem.readFile, whose body dereferences `this`
|
||||
// (e.g. `this.getFilesystem()`). A detached call would lose the binding
|
||||
// and throw "Cannot read properties of undefined".
|
||||
class ThisDependentFilesystem {
|
||||
private readonly files = new Map([['/tmp/manifest.json', '{"ok":true}']]);
|
||||
|
||||
async readFile(path: string): Promise<string> {
|
||||
const content = this.files.get(path);
|
||||
if (content === undefined) throw new Error('missing');
|
||||
return await Promise.resolve(content);
|
||||
}
|
||||
|
||||
async writeFile(): Promise<void> {
|
||||
await Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
const target: WorkspaceFileTarget = { filesystem: new ThisDependentFilesystem() };
|
||||
|
||||
await expect(readWorkspaceFile(target, '/tmp/manifest.json')).resolves.toBe('{"ok":true}');
|
||||
});
|
||||
|
||||
it('reads via sandbox commands when no filesystem reader is available', async () => {
|
||||
const { target } = createWorkspaceTarget(new Map([['/tmp/manifest.json', '{"ok":true}']]));
|
||||
target.filesystem = {
|
||||
|
|
|
|||
|
|
@ -37,10 +37,10 @@ export async function readWorkspaceFile(
|
|||
filePath: string,
|
||||
options?: WorkspaceFileOptions,
|
||||
): Promise<string | null> {
|
||||
const readFile = workspace.filesystem?.readFile;
|
||||
if (readFile) {
|
||||
const filesystem = workspace.filesystem;
|
||||
if (filesystem?.readFile) {
|
||||
try {
|
||||
return decodeWorkspaceFileContent(await readFile(filePath, { encoding: 'utf-8' }));
|
||||
return decodeWorkspaceFileContent(await filesystem.readFile(filePath, { encoding: 'utf-8' }));
|
||||
} catch (error) {
|
||||
options?.logger?.debug(`${resourceLabel(options)} filesystem read missed`, {
|
||||
path: filePath,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user