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

[Flight] Add rudimentary FS binding #20409

Merged
merged 5 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions packages/react-fs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# react-fs

This package is meant to be used alongside yet-to-be-released, experimental React features. It's unlikely to be useful in any other context.

**Do not use in a real application.** We're publishing this early for
demonstration purposes.

**Use it at your own risk.**

# No, Really, It Is Unstable

The API ~~may~~ will change wildly between versions.
12 changes: 12 additions & 0 deletions packages/react-fs/index.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

throw new Error(
'This entry point is not yet supported in the browser environment',
);
12 changes: 12 additions & 0 deletions packages/react-fs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

export * from './index.node';
12 changes: 12 additions & 0 deletions packages/react-fs/index.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

export * from './src/ReactFilesystem';
7 changes: 7 additions & 0 deletions packages/react-fs/npm/index.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react-fs.browser.production.min.js');
} else {
module.exports = require('./cjs/react-fs.browser.development.js');
}
3 changes: 3 additions & 0 deletions packages/react-fs/npm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./index.node');
7 changes: 7 additions & 0 deletions packages/react-fs/npm/index.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react-fs.node.production.min.js');
} else {
module.exports = require('./cjs/react-fs.node.development.js');
}
23 changes: 23 additions & 0 deletions packages/react-fs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"private": true,
"name": "react-fs",
"description": "React bindings for the filesystem",
"version": "0.0.0",
"repository": {
"type" : "git",
"url" : "https://github.com/facebook/react.git",
"directory": "packages/react-fs"
},
"files": [
"LICENSE",
"README.md",
"build-info.json",
"index.js",
"index.node.js",
"index.browser.js",
"cjs/"
],
"browser": {
"./index.js": "./index.browser.js"
}
}
108 changes: 108 additions & 0 deletions packages/react-fs/src/ReactFilesystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Wakeable, Thenable} from 'shared/ReactTypes';

import {unstable_getCacheForType} from 'react';
import * as fs from 'fs/promises';

const Pending = 0;
const Resolved = 1;
const Rejected = 2;

type PendingResult = {|
status: 0,
value: Wakeable,
|};

type ResolvedResult<T> = {|
status: 1,
value: T,
|};

type RejectedResult = {|
status: 2,
value: mixed,
|};

type Result<T> = PendingResult | ResolvedResult<T> | RejectedResult;

function toResult<T>(thenable: Thenable<T>): Result<T> {
const result: Result<T> = {
status: Pending,
value: thenable,
};
thenable.then(
value => {
if (result.status === Pending) {
const resolvedResult = ((result: any): ResolvedResult<T>);
resolvedResult.status = Resolved;
resolvedResult.value = value;
}
},
err => {
if (result.status === Pending) {
const rejectedResult = ((result: any): RejectedResult);
rejectedResult.status = Rejected;
rejectedResult.value = err;
}
},
);
return result;
}

function readResult<T>(result: Result<T>): T {
if (result.status === Resolved) {
return result.value;
} else {
throw result.value;
}
}

function createReadFileCache(): Map<string, Result<Buffer>> {
return new Map();
}

export function readFile(
path: string,
options:
| string
| {
encoding?: string | null,
// Ignored:
flag?: string,
gaearon marked this conversation as resolved.
Show resolved Hide resolved
signal?: mixed,
gaearon marked this conversation as resolved.
Show resolved Hide resolved
},
): string | Buffer {
const map = unstable_getCacheForType(createReadFileCache);
let entry = map.get(path);
gaearon marked this conversation as resolved.
Show resolved Hide resolved
if (!entry) {
const thenable = fs.readFile(path);
entry = toResult(thenable);
map.set(path, entry);
}
const result: Buffer = readResult(entry);
if (!options) {
return result;
}
const encoding = typeof options === 'string' ? options : options.encoding;
if (typeof encoding !== 'string') {
return result;
}
const textCache =
(result: any)._reactTextCache || ((result: any)._reactTextCache = []);
gaearon marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0; i < textCache.length; i += 2) {
if (textCache[i] === encoding) {
return textCache[i + 1];
}
}
const text = result.toString((encoding: any));
textCache.push(encoding, text);
return text;
}
10 changes: 10 additions & 0 deletions scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ declare module 'EventListener' {
declare function __webpack_chunk_load__(id: string): Promise<mixed>;
declare function __webpack_require__(id: string): any;

declare module 'fs/promises' {
declare var readFile: (
path: string,
options?:
| ?string
| {
encoding?: ?string,
},
) => Promise<Buffer>;
}
declare module 'pg' {
declare var Pool: (
options: mixed,
Expand Down
18 changes: 18 additions & 0 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ const bundles = [
externals: ['react', 'http', 'https'],
},

/******* React FS Browser (experimental, new) *******/
{
bundleTypes: [NODE_DEV, NODE_PROD],
moduleType: ISOMORPHIC,
entry: 'react-fs/index.browser',
global: 'ReactFilesystem',
externals: [],
},

/******* React FS Node (experimental, new) *******/
{
bundleTypes: [NODE_DEV, NODE_PROD],
moduleType: ISOMORPHIC,
entry: 'react-fs/index.node',
global: 'ReactFilesystem',
externals: ['react', 'fs/promises'],
},

/******* React PG Browser (experimental, new) *******/
{
bundleTypes: [NODE_DEV, NODE_PROD],
Expand Down