|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import type {Wakeable, Thenable} from 'shared/ReactTypes'; |
| 11 | + |
| 12 | +import {unstable_getCacheForType} from 'react'; |
| 13 | +import * as fs from 'fs/promises'; |
| 14 | +import {isAbsolute, normalize} from 'path'; |
| 15 | + |
| 16 | +const Pending = 0; |
| 17 | +const Resolved = 1; |
| 18 | +const Rejected = 2; |
| 19 | + |
| 20 | +type PendingResult = {| |
| 21 | + status: 0, |
| 22 | + value: Wakeable, |
| 23 | + cache: Array<mixed>, |
| 24 | +|}; |
| 25 | + |
| 26 | +type ResolvedResult<T> = {| |
| 27 | + status: 1, |
| 28 | + value: T, |
| 29 | + cache: Array<mixed>, |
| 30 | +|}; |
| 31 | + |
| 32 | +type RejectedResult = {| |
| 33 | + status: 2, |
| 34 | + value: mixed, |
| 35 | + cache: Array<mixed>, |
| 36 | +|}; |
| 37 | + |
| 38 | +type Result<T> = PendingResult | ResolvedResult<T> | RejectedResult; |
| 39 | + |
| 40 | +function toResult<T>(thenable: Thenable<T>): Result<T> { |
| 41 | + const result: Result<T> = { |
| 42 | + status: Pending, |
| 43 | + value: thenable, |
| 44 | + cache: [], |
| 45 | + }; |
| 46 | + thenable.then( |
| 47 | + value => { |
| 48 | + if (result.status === Pending) { |
| 49 | + const resolvedResult = ((result: any): ResolvedResult<T>); |
| 50 | + resolvedResult.status = Resolved; |
| 51 | + resolvedResult.value = value; |
| 52 | + } |
| 53 | + }, |
| 54 | + err => { |
| 55 | + if (result.status === Pending) { |
| 56 | + const rejectedResult = ((result: any): RejectedResult); |
| 57 | + rejectedResult.status = Rejected; |
| 58 | + rejectedResult.value = err; |
| 59 | + } |
| 60 | + }, |
| 61 | + ); |
| 62 | + return result; |
| 63 | +} |
| 64 | + |
| 65 | +function readResult<T>(result: Result<T>): T { |
| 66 | + if (result.status === Resolved) { |
| 67 | + return result.value; |
| 68 | + } else { |
| 69 | + throw result.value; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// We don't want to normalize every path ourselves in production. |
| 74 | +// However, relative or non-normalized paths will lead to cache misses. |
| 75 | +// So we encourage the developer to fix it in DEV and normalize on their end. |
| 76 | +function checkPathInDev(path: string) { |
| 77 | + if (__DEV__) { |
| 78 | + if (!isAbsolute(path)) { |
| 79 | + console.error( |
| 80 | + 'The provided path was not absolute: "%s". ' + |
| 81 | + 'Convert it to an absolute path first.', |
| 82 | + path, |
| 83 | + ); |
| 84 | + } else if (path !== normalize(path)) { |
| 85 | + console.error( |
| 86 | + 'The provided path was not normalized: "%s". ' + |
| 87 | + 'Convert it to a normalized path first.', |
| 88 | + path, |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function createReadFileCache(): Map<string, Result<Buffer>> { |
| 95 | + return new Map(); |
| 96 | +} |
| 97 | + |
| 98 | +export function readFile( |
| 99 | + path: string, |
| 100 | + options: |
| 101 | + | string |
| 102 | + | { |
| 103 | + encoding?: string | null, |
| 104 | + // Unsupported: |
| 105 | + flag?: string, // Doesn't make sense except "r" |
| 106 | + signal?: mixed, // We'll have our own signal |
| 107 | + }, |
| 108 | +): string | Buffer { |
| 109 | + const map = unstable_getCacheForType(createReadFileCache); |
| 110 | + checkPathInDev(path); |
| 111 | + let entry = map.get(path); |
| 112 | + if (!entry) { |
| 113 | + const thenable = fs.readFile(path); |
| 114 | + entry = toResult(thenable); |
| 115 | + map.set(path, entry); |
| 116 | + } |
| 117 | + const result: Buffer = readResult(entry); |
| 118 | + if (!options) { |
| 119 | + return result; |
| 120 | + } |
| 121 | + let encoding; |
| 122 | + if (typeof options === 'string') { |
| 123 | + encoding = options; |
| 124 | + } else { |
| 125 | + const flag = options.flag; |
| 126 | + if (flag != null && flag !== 'r') { |
| 127 | + throw Error( |
| 128 | + 'The flag option is not supported, and always defaults to "r".', |
| 129 | + ); |
| 130 | + } |
| 131 | + if (options.signal) { |
| 132 | + throw Error('The signal option is not supported.'); |
| 133 | + } |
| 134 | + encoding = options.encoding; |
| 135 | + } |
| 136 | + if (typeof encoding !== 'string') { |
| 137 | + return result; |
| 138 | + } |
| 139 | + const textCache = entry.cache; |
| 140 | + for (let i = 0; i < textCache.length; i += 2) { |
| 141 | + if (textCache[i] === encoding) { |
| 142 | + return (textCache[i + 1]: any); |
| 143 | + } |
| 144 | + } |
| 145 | + const text = result.toString((encoding: any)); |
| 146 | + textCache.push(encoding, text); |
| 147 | + return text; |
| 148 | +} |
0 commit comments