Skip to content

Commit 7dc99c8

Browse files
robhoganfacebook-github-bot
authored andcommitted
metro-cache: make Flow-private class members JS-private, misc fixes
Summary: Prefer actually-private class field/method syntax (`#` prefix) over private-by-convention (`_` prefix). Flow will already error on use of `_`-private fields, but they will generate TypeScript types as if part of the public API. Also cleans up some inconsistent headers and a stray self-import of a type. This prepares `metro-cache` for fully auto-generated TypeScript types. Changelog: Internal Reviewed By: huntie Differential Revision: D81879389 fbshipit-source-id: 6c2d0071da02db4a9bc2b79a205486b61d270069
1 parent a264732 commit 7dc99c8

9 files changed

Lines changed: 66 additions & 58 deletions

File tree

packages/metro-cache/src/Cache.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @oncall react_native
1010
*/
1111

12-
import type {CacheStore} from 'metro-cache';
12+
import type {CacheStore} from './types';
1313

1414
import {Logger} from 'metro-core';
1515

@@ -21,17 +21,15 @@ import {Logger} from 'metro-core';
2121
* All get/set operations are logged via Metro's logger.
2222
*/
2323
export default class Cache<T> {
24-
_stores: $ReadOnlyArray<CacheStore<T>>;
25-
26-
_hits: WeakMap<Buffer, CacheStore<T>>;
24+
+#stores: $ReadOnlyArray<CacheStore<T>>;
25+
+#hits: WeakMap<Buffer, CacheStore<T>> = new WeakMap();
2726

2827
constructor(stores: $ReadOnlyArray<CacheStore<T>>) {
29-
this._hits = new WeakMap();
30-
this._stores = stores;
28+
this.#stores = stores;
3129
}
3230

3331
async get(key: Buffer): Promise<?T> {
34-
const stores = this._stores;
32+
const stores = this.#stores;
3533
const length = stores.length;
3634

3735
for (let i = 0; i < length; i++) {
@@ -74,7 +72,7 @@ export default class Cache<T> {
7472
);
7573

7674
if (value != null) {
77-
this._hits.set(key, store);
75+
this.#hits.set(key, store);
7876

7977
return value;
8078
}
@@ -85,8 +83,8 @@ export default class Cache<T> {
8583
}
8684

8785
async set(key: Buffer, value: T): Promise<void> {
88-
const stores = this._stores;
89-
const stop = this._hits.get(key);
86+
const stores = this.#stores;
87+
const stop = this.#hits.get(key);
9088
const length = stores.length;
9189
const promises = [];
9290
const writeErrors = [];
@@ -133,6 +131,6 @@ export default class Cache<T> {
133131
// writing to the cache is a no-op and reading from the cache will always
134132
// return null.
135133
get isDisabled(): boolean {
136-
return this._stores.length === 0;
134+
return this.#stores.length === 0;
137135
}
138136
}

packages/metro-cache/src/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ export {
2929
stableHash,
3030
};
3131

32+
export interface MetroCache {
33+
+AutoCleanFileStore: typeof AutoCleanFileStore;
34+
+Cache: typeof Cache;
35+
+FileStore: typeof FileStore;
36+
+HttpGetStore: typeof HttpGetStore;
37+
+HttpStore: typeof HttpStore;
38+
+stableHash: typeof stableHash;
39+
}
40+
3241
/**
3342
* Backwards-compatibility with CommonJS consumers using interopRequireDefault.
3443
* Do not add to this list.
@@ -42,4 +51,4 @@ export default {
4251
HttpGetStore,
4352
HttpStore,
4453
stableHash,
45-
};
54+
} as MetroCache;

packages/metro-cache/src/stores/AutoCleanFileStore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
* @flow strict-local
88
* @format
9+
* @oncall react_native
910
*/
1011

1112
import type {Options} from './FileStore';

packages/metro-cache/src/stores/FileStore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
* @flow
88
* @format
9+
* @oncall react_native
910
*/
1011

1112
import fs from 'fs';

packages/metro-cache/src/stores/HttpError.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict
8+
* @format
99
*/
1010

1111
export default class HttpError extends Error {

packages/metro-cache/src/stores/HttpGetStore.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict-local
8+
* @format
9+
* @oncall react_native
910
*/
1011

1112
import type HttpError from './HttpError';
13+
import type {Options as HttpOptions} from './HttpStore';
1214
import type NetworkError from './NetworkError';
13-
import type {HttpOptions} from 'metro-cache';
1415

1516
import HttpStore from './HttpStore';
1617
import {Logger} from 'metro-core';
1718

1819
export default class HttpGetStore<T> extends HttpStore<T> {
19-
_warned: boolean;
20+
#warned: boolean;
2021

2122
constructor(options: HttpOptions) {
2223
super(options);
2324

24-
this._warned = false;
25+
this.#warned = false;
2526
}
2627

2728
async get(key: Buffer): Promise<?T> {
@@ -35,18 +36,16 @@ export default class HttpGetStore<T> extends HttpStore<T> {
3536
throw err;
3637
}
3738

38-
this._warn(err);
39+
this.#warn(err);
3940

4041
return null;
4142
}
4243
}
4344

44-
set(): Promise<void> {
45-
return Promise.resolve(undefined);
46-
}
45+
async set(_key: Buffer, _value: T): Promise<void> {}
4746

48-
_warn(err: HttpError | NetworkError) {
49-
if (!this._warned) {
47+
#warn(err: HttpError | NetworkError) {
48+
if (!this.#warned) {
5049
process.emitWarning(
5150
[
5251
'Could not connect to the HTTP cache.',
@@ -60,7 +59,7 @@ export default class HttpGetStore<T> extends HttpStore<T> {
6059
log_entry_label: `${err.message} (${err.code})`,
6160
}),
6261
);
63-
this._warned = true;
62+
this.#warned = true;
6463
}
6564
}
6665
}

packages/metro-cache/src/stores/HttpStore.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow
8+
* @format
9+
* @oncall react_native
910
*/
1011

1112
import type {HttpsProxyAgentOptions} from 'https-proxy-agent';
@@ -78,19 +79,19 @@ export default class HttpStore<T> {
7879
static HttpError: typeof HttpError = HttpError;
7980
static NetworkError: typeof NetworkError = NetworkError;
8081

81-
_getEndpoint: Endpoint;
82-
_setEndpoint: Endpoint;
82+
#getEndpoint: Endpoint;
83+
#setEndpoint: Endpoint;
8384

8485
constructor(options: Options) {
85-
this._getEndpoint = this.createEndpointConfig(
86+
this.#getEndpoint = this.#createEndpointConfig(
8687
options.getOptions != null ? options.getOptions : options,
8788
);
88-
this._setEndpoint = this.createEndpointConfig(
89+
this.#setEndpoint = this.#createEndpointConfig(
8990
options.setOptions != null ? options.setOptions : options,
9091
);
9192
}
9293

93-
createEndpointConfig(options: EndpointOptions): Endpoint {
94+
#createEndpointConfig(options: EndpointOptions): Endpoint {
9495
const agentConfig: http$agentOptions & HttpsProxyAgentOptions = {
9596
family: options.family,
9697
keepAlive: true,
@@ -151,32 +152,32 @@ export default class HttpStore<T> {
151152
}
152153

153154
get(key: Buffer): Promise<?T> {
154-
return this.#withRetries(() => this.#getOnce(key), this._getEndpoint);
155+
return this.#withRetries(() => this.#getOnce(key), this.#getEndpoint);
155156
}
156157

157158
#getOnce(key: Buffer): Promise<?T> {
158159
return new Promise((resolve, reject) => {
159-
let searchParamsString = this._getEndpoint.params.toString();
160+
let searchParamsString = this.#getEndpoint.params.toString();
160161
if (searchParamsString != '') {
161162
searchParamsString = '?' + searchParamsString;
162163
}
163164
const options = {
164-
agent: this._getEndpoint.agent,
165-
headers: this._getEndpoint.headers,
166-
host: this._getEndpoint.host,
165+
agent: this.#getEndpoint.agent,
166+
headers: this.#getEndpoint.headers,
167+
host: this.#getEndpoint.host,
167168
method: 'GET',
168-
path: `${this._getEndpoint.path}/${key.toString(
169+
path: `${this.#getEndpoint.path}/${key.toString(
169170
'hex',
170171
)}${searchParamsString}`,
171-
port: this._getEndpoint.port,
172-
timeout: this._getEndpoint.timeout,
172+
port: this.#getEndpoint.port,
173+
timeout: this.#getEndpoint.timeout,
173174
};
174175

175176
// $FlowFixMe[incompatible-type]
176177
/* $FlowFixMe[missing-local-annot](>=0.101.0 site=react_native_fb) This comment suppresses an
177178
* error found when Flow v0.101 was deployed. To see the error, delete
178179
* this comment and run Flow. */
179-
const req = this._getEndpoint.module.request(options, res => {
180+
const req = this.#getEndpoint.module.request(options, res => {
180181
const code = res.statusCode;
181182
const data = [];
182183

@@ -187,9 +188,9 @@ export default class HttpStore<T> {
187188
return;
188189
} else if (
189190
code !== 200 &&
190-
!this._getEndpoint.additionalSuccessStatuses.has(code)
191+
!this.#getEndpoint.additionalSuccessStatuses.has(code)
191192
) {
192-
if (this._getEndpoint.debug) {
193+
if (this.#getEndpoint.debug) {
193194
res.on('data', chunk => {
194195
data.push(chunk);
195196
});
@@ -275,43 +276,43 @@ export default class HttpStore<T> {
275276
set(key: Buffer, value: T): Promise<void> {
276277
return this.#withRetries(
277278
() => this.#setOnce(key, value),
278-
this._setEndpoint,
279+
this.#setEndpoint,
279280
);
280281
}
281282

282283
#setOnce(key: Buffer, value: T): Promise<void> {
283284
return new Promise((resolve, reject) => {
284285
const gzip = zlib.createGzip(ZLIB_OPTIONS);
285286

286-
let searchParamsString = this._setEndpoint.params.toString();
287+
let searchParamsString = this.#setEndpoint.params.toString();
287288
if (searchParamsString != '') {
288289
searchParamsString = '?' + searchParamsString;
289290
}
290291

291292
const options = {
292-
agent: this._setEndpoint.agent,
293-
headers: this._setEndpoint.headers,
294-
host: this._setEndpoint.host,
293+
agent: this.#setEndpoint.agent,
294+
headers: this.#setEndpoint.headers,
295+
host: this.#setEndpoint.host,
295296
method: 'PUT',
296-
path: `${this._setEndpoint.path}/${key.toString(
297+
path: `${this.#setEndpoint.path}/${key.toString(
297298
'hex',
298299
)}${searchParamsString}`,
299-
port: this._setEndpoint.port,
300-
timeout: this._setEndpoint.timeout,
300+
port: this.#setEndpoint.port,
301+
timeout: this.#setEndpoint.timeout,
301302
};
302303

303304
// $FlowFixMe[incompatible-type]
304305
/* $FlowFixMe[missing-local-annot](>=0.101.0 site=react_native_fb) This comment suppresses an
305306
* error found when Flow v0.101 was deployed. To see the error, delete
306307
* this comment and run Flow. */
307-
const req = this._setEndpoint.module.request(options, res => {
308+
const req = this.#setEndpoint.module.request(options, res => {
308309
const code = res.statusCode;
309310

310311
if (
311312
(code < 200 || code > 299) &&
312-
!this._setEndpoint.additionalSuccessStatuses.has(code)
313+
!this.#setEndpoint.additionalSuccessStatuses.has(code)
313314
) {
314-
if (this._setEndpoint.debug) {
315+
if (this.#setEndpoint.debug) {
315316
const data = [];
316317
res.on('data', chunk => {
317318
data.push(chunk);
@@ -395,13 +396,13 @@ export default class HttpStore<T> {
395396
return backOff(fn, {
396397
jitter: 'full',
397398
maxDelay: 30000,
398-
numOfAttempts: this._getEndpoint.maxAttempts || Number.POSITIVE_INFINITY,
399+
numOfAttempts: this.#getEndpoint.maxAttempts || Number.POSITIVE_INFINITY,
399400
retry: (e: Error) => {
400401
if (e instanceof HttpError) {
401-
return this._getEndpoint.retryStatuses.has(e.code);
402+
return this.#getEndpoint.retryStatuses.has(e.code);
402403
}
403404
return (
404-
e instanceof NetworkError && this._getEndpoint.retryNetworkErrors
405+
e instanceof NetworkError && this.#getEndpoint.retryNetworkErrors
405406
);
406407
},
407408
});

packages/metro-cache/src/stores/NetworkError.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict
8+
* @format
99
*/
1010

1111
export default class NetworkError extends Error {

packages/metro-config/src/types.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
*/
1111

1212
import type {HandleFunction, Server} from 'connect';
13-
import type {CacheStore} from 'metro-cache';
14-
import typeof * as MetroCache from 'metro-cache';
13+
import type {CacheStore, MetroCache} from 'metro-cache';
1514
import type {CacheManagerFactory} from 'metro-file-map';
1615
import type {CustomResolver} from 'metro-resolver';
1716
import type {JsTransformerConfig} from 'metro-transform-worker';

0 commit comments

Comments
 (0)