-
Notifications
You must be signed in to change notification settings - Fork 48.8k
[Flight] Add rudimentary FS binding #20409
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./index.node'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"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/" | ||
], | ||
"peerDependencies": { | ||
"react": "^17.0.0" | ||
}, | ||
"browser": { | ||
"./index.js": "./index.browser.js" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* 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'; | ||
import {isAbsolute, normalize} from 'path'; | ||
|
||
const Pending = 0; | ||
const Resolved = 1; | ||
const Rejected = 2; | ||
|
||
type PendingResult = {| | ||
status: 0, | ||
value: Wakeable, | ||
cache: Array<mixed>, | ||
|}; | ||
|
||
type ResolvedResult<T> = {| | ||
status: 1, | ||
value: T, | ||
cache: Array<mixed>, | ||
|}; | ||
|
||
type RejectedResult = {| | ||
status: 2, | ||
value: mixed, | ||
cache: Array<mixed>, | ||
|}; | ||
|
||
type Result<T> = PendingResult | ResolvedResult<T> | RejectedResult; | ||
|
||
function toResult<T>(thenable: Thenable<T>): Result<T> { | ||
const result: Result<T> = { | ||
status: Pending, | ||
value: thenable, | ||
cache: [], | ||
}; | ||
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; | ||
} | ||
} | ||
|
||
// We don't want to normalize every path ourselves in production. | ||
// However, relative or non-normalized paths will lead to cache misses. | ||
// So we encourage the developer to fix it in DEV and normalize on their end. | ||
function checkPathInDev(path: string) { | ||
if (__DEV__) { | ||
if (!isAbsolute(path)) { | ||
console.error( | ||
'The provided path was not absolute: "%s". ' + | ||
'Convert it to an absolute path first.', | ||
path, | ||
); | ||
} else if (path !== normalize(path)) { | ||
console.error( | ||
'The provided path was not normalized: "%s". ' + | ||
'Convert it to a normalized path first.', | ||
path, | ||
); | ||
} | ||
} | ||
} | ||
|
||
function createReadFileCache(): Map<string, Result<Buffer>> { | ||
return new Map(); | ||
} | ||
|
||
export function readFile( | ||
path: string, | ||
options: | ||
| string | ||
| { | ||
encoding?: string | null, | ||
// Unsupported: | ||
flag?: string, // Doesn't make sense except "r" | ||
signal?: mixed, // We'll have our own signal | ||
}, | ||
): string | Buffer { | ||
const map = unstable_getCacheForType(createReadFileCache); | ||
checkPathInDev(path); | ||
let entry = map.get(path); | ||
if (!entry) { | ||
const thenable = fs.readFile(path); | ||
entry = toResult(thenable); | ||
map.set(path, entry); | ||
} | ||
const result: Buffer = readResult(entry); | ||
if (!options) { | ||
return result; | ||
} | ||
let encoding; | ||
if (typeof options === 'string') { | ||
encoding = options; | ||
} else { | ||
const flag = options.flag; | ||
if (flag != null && flag !== 'r') { | ||
throw Error( | ||
'The flag option is not supported, and always defaults to "r".', | ||
); | ||
} | ||
if (options.signal) { | ||
throw Error('The signal option is not supported.'); | ||
} | ||
encoding = options.encoding; | ||
} | ||
if (typeof encoding !== 'string') { | ||
return result; | ||
} | ||
const textCache = entry.cache; | ||
for (let i = 0; i < textCache.length; i += 2) { | ||
if (textCache[i] === encoding) { | ||
return (textCache[i + 1]: any); | ||
} | ||
} | ||
const text = result.toString((encoding: any)); | ||
textCache.push(encoding, text); | ||
return text; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.