Implement fs.readFile / fs.writeFile
Summary: Per title. Made an explicit distinction between binary and non binary files, since they need to be encoded differently. This keeps both the implementation and API simpler (in terms of overloading / type checking) Reviewed By: aigoncharov Differential Revision: D33016031 fbshipit-source-id: 3c99956eb016849a908a171d88a7a64a88b76268
This commit is contained in:
committed by
Facebook GitHub Bot
parent
7e9ad72baa
commit
d95b15094f
@@ -163,6 +163,22 @@ export type FlipperServerCommands = {
|
|||||||
) => Promise<void>;
|
) => Promise<void>;
|
||||||
'node-api-fs-stat': (path: string) => Promise<FSStatsLike>;
|
'node-api-fs-stat': (path: string) => Promise<FSStatsLike>;
|
||||||
'node-api-fs-readlink': (path: string) => Promise<string>;
|
'node-api-fs-readlink': (path: string) => Promise<string>;
|
||||||
|
'node-api-fs-readfile': (
|
||||||
|
path: string,
|
||||||
|
options?: {encoding?: BufferEncoding},
|
||||||
|
) => Promise<string>;
|
||||||
|
'node-api-fs-readfile-binary': (
|
||||||
|
path: string,
|
||||||
|
) => Promise<string /* base64 encoded */>;
|
||||||
|
'node-api-fs-writefile': (
|
||||||
|
path: string,
|
||||||
|
contents: string,
|
||||||
|
options?: {encoding?: BufferEncoding},
|
||||||
|
) => Promise<void>;
|
||||||
|
'node-api-fs-writefile-binary': (
|
||||||
|
path: string,
|
||||||
|
base64contents: string,
|
||||||
|
) => Promise<void>;
|
||||||
/**
|
/**
|
||||||
* @throws ExecError
|
* @throws ExecError
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -66,6 +66,17 @@ export type RemoteServerContext = {
|
|||||||
copyFile(src: string, dest: string, flags?: number): Promise<void>;
|
copyFile(src: string, dest: string, flags?: number): Promise<void>;
|
||||||
stat(path: string): Promise<FSStatsLike>;
|
stat(path: string): Promise<FSStatsLike>;
|
||||||
readlink(path: string): Promise<string>;
|
readlink(path: string): Promise<string>;
|
||||||
|
readFile(
|
||||||
|
path: string,
|
||||||
|
options?: {encoding?: BufferEncoding},
|
||||||
|
): Promise<string>;
|
||||||
|
readFileBinary(path: string): Promise<Uint8Array>; // No Buffer, which is not a browser type
|
||||||
|
writeFile(
|
||||||
|
path: string,
|
||||||
|
contents: string,
|
||||||
|
options?: {encoding?: BufferEncoding},
|
||||||
|
): Promise<void>;
|
||||||
|
writeFileBinary(path: string, contents: Uint8Array): Promise<void>;
|
||||||
};
|
};
|
||||||
downloadFile(
|
downloadFile(
|
||||||
url: string,
|
url: string,
|
||||||
|
|||||||
@@ -415,6 +415,10 @@ export function createMockFlipperLib(options?: StartPluginOptions): FlipperLib {
|
|||||||
constants: fsConstants,
|
constants: fsConstants,
|
||||||
stat: jest.fn(),
|
stat: jest.fn(),
|
||||||
readlink: jest.fn(),
|
readlink: jest.fn(),
|
||||||
|
readFile: jest.fn(),
|
||||||
|
readFileBinary: jest.fn(),
|
||||||
|
writeFile: jest.fn(),
|
||||||
|
writeFileBinary: jest.fn(),
|
||||||
},
|
},
|
||||||
downloadFile: jest.fn(),
|
downloadFile: jest.fn(),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -48,8 +48,10 @@ import {commandDownloadFileStartFactory} from './commands/DownloadFile';
|
|||||||
import {promises} from 'fs';
|
import {promises} from 'fs';
|
||||||
// Electron 11 runs on Node 12 which does not support fs.promises.rm
|
// Electron 11 runs on Node 12 which does not support fs.promises.rm
|
||||||
import rm from 'rimraf';
|
import rm from 'rimraf';
|
||||||
|
import assert from 'assert';
|
||||||
|
|
||||||
const {access, copyFile, mkdir, unlink, stat, readlink} = promises;
|
const {access, copyFile, mkdir, unlink, stat, readlink, readFile, writeFile} =
|
||||||
|
promises;
|
||||||
|
|
||||||
export const SERVICE_FLIPPER = 'flipper.oAuthToken';
|
export const SERVICE_FLIPPER = 'flipper.oAuthToken';
|
||||||
|
|
||||||
@@ -258,6 +260,22 @@ export class FlipperServerImpl implements FlipperServer {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
'node-api-fs-readlink': readlink,
|
'node-api-fs-readlink': readlink,
|
||||||
|
'node-api-fs-readfile': async (path, options) => {
|
||||||
|
const contents = await readFile(path, options ?? 'utf8');
|
||||||
|
assert(
|
||||||
|
typeof contents === 'string',
|
||||||
|
`File ${path} was not read with a string encoding`,
|
||||||
|
);
|
||||||
|
return contents;
|
||||||
|
},
|
||||||
|
'node-api-fs-readfile-binary': async (path) => {
|
||||||
|
const contents = await readFile(path);
|
||||||
|
return Base64.fromUint8Array(contents);
|
||||||
|
},
|
||||||
|
'node-api-fs-writefile': (path, contents, options) =>
|
||||||
|
writeFile(path, contents, options ?? 'utf8'),
|
||||||
|
'node-api-fs-writefile-binary': (path, base64contents) =>
|
||||||
|
writeFile(path, Base64.toUint8Array(base64contents), 'binary'),
|
||||||
// TODO: Do we need API to cancel an active download?
|
// TODO: Do we need API to cancel an active download?
|
||||||
'download-file-start': commandDownloadFileStartFactory(
|
'download-file-start': commandDownloadFileStartFactory(
|
||||||
this.emit.bind(this),
|
this.emit.bind(this),
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import {DetailSidebarImpl} from '../../sandy-chrome/DetailSidebarImpl';
|
|||||||
import {RenderHost} from '../../RenderHost';
|
import {RenderHost} from '../../RenderHost';
|
||||||
import {setMenuEntries} from '../../reducers/connections';
|
import {setMenuEntries} from '../../reducers/connections';
|
||||||
import {downloadFileFactory} from './downloadFile';
|
import {downloadFileFactory} from './downloadFile';
|
||||||
|
import {Base64} from 'js-base64';
|
||||||
|
|
||||||
export function initializeFlipperLibImplementation(
|
export function initializeFlipperLibImplementation(
|
||||||
renderHost: RenderHost,
|
renderHost: RenderHost,
|
||||||
@@ -115,6 +116,30 @@ export function initializeFlipperLibImplementation(
|
|||||||
renderHost.flipperServer.exec('node-api-fs-stat', path),
|
renderHost.flipperServer.exec('node-api-fs-stat', path),
|
||||||
readlink: async (path: string) =>
|
readlink: async (path: string) =>
|
||||||
renderHost.flipperServer.exec('node-api-fs-readlink', path),
|
renderHost.flipperServer.exec('node-api-fs-readlink', path),
|
||||||
|
readFile: (path, options) =>
|
||||||
|
renderHost.flipperServer.exec('node-api-fs-readfile', path, options),
|
||||||
|
readFileBinary: async (path) =>
|
||||||
|
Base64.toUint8Array(
|
||||||
|
await renderHost.flipperServer.exec(
|
||||||
|
'node-api-fs-readfile-binary',
|
||||||
|
path,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
writeFile: (path, contents, options) =>
|
||||||
|
renderHost.flipperServer.exec(
|
||||||
|
'node-api-fs-writefile',
|
||||||
|
path,
|
||||||
|
contents,
|
||||||
|
options,
|
||||||
|
),
|
||||||
|
writeFileBinary: async (path, contents) => {
|
||||||
|
const base64contents = Base64.fromUint8Array(contents);
|
||||||
|
return await renderHost.flipperServer.exec(
|
||||||
|
'node-api-fs-writefile-binary',
|
||||||
|
path,
|
||||||
|
base64contents,
|
||||||
|
);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
downloadFile: downloadFileFactory(renderHost),
|
downloadFile: downloadFileFactory(renderHost),
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user