Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions packages/metro-cache/types/Cache.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
* @oncall react_native
*/

import {CacheStore} from './types';

import type {CacheStore} from './types';
/**
* Main cache class. Receives an array of cache instances, and sequentially
* traverses them to return a previously stored value. It also ensures setting
* the value in all instances.
*
* All get/set operations are logged via Metro's logger.
*/
export default class Cache<T> {
declare class Cache<T> {
constructor(stores: ReadonlyArray<CacheStore<T>>);
get(key: Buffer): Promise<T | null>;
get(key: Buffer): Promise<null | undefined | T>;
set(key: Buffer, value: T): Promise<void>;
get isDisabled(): boolean;
}
export default Cache;
16 changes: 8 additions & 8 deletions packages/metro-cache/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ export {
HttpStore,
stableHash,
};

export interface MetroCache {
AutoCleanFileStore: typeof AutoCleanFileStore;
Cache: typeof Cache;
FileStore: typeof FileStore;
HttpGetStore: typeof HttpGetStore;
HttpStore: typeof HttpStore;
stableHash: typeof stableHash;
readonly AutoCleanFileStore: typeof AutoCleanFileStore;
readonly Cache: typeof Cache;
readonly FileStore: typeof FileStore;
readonly HttpGetStore: typeof HttpGetStore;
readonly HttpStore: typeof HttpStore;
readonly stableHash: typeof stableHash;
}

/**
* Backwards-compatibility with CommonJS consumers using interopRequireDefault.
* Do not add to this list.
*
* @deprecated Default import from 'metro-cache' is deprecated, use named exports.
*/
declare const $$EXPORT_DEFAULT_DECLARATION$$: MetroCache;
declare type $$EXPORT_DEFAULT_DECLARATION$$ =
typeof $$EXPORT_DEFAULT_DECLARATION$$;
export default $$EXPORT_DEFAULT_DECLARATION$$;
3 changes: 2 additions & 1 deletion packages/metro-cache/types/stableHash.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
* @oncall react_native
*/

export default function stableHash(value: unknown): Buffer;
declare function stableHash(value: unknown): Buffer;
export default stableHash;
15 changes: 13 additions & 2 deletions packages/metro-cache/types/stores/AutoCleanFileStore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
* @oncall react_native
*/

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

import FileStore from './FileStore';

type CleanOptions = Readonly<
Omit<Options, keyof {intervalMs?: number; cleanupThresholdMs?: number}> & {
intervalMs?: number;
cleanupThresholdMs?: number;
}
>;
/**
* A FileStore that, at a given interval, stats the content of the cache root
* and deletes any file last modified a set threshold in the past.
Expand All @@ -18,4 +26,7 @@ import type FileStore from './FileStore';
* redundant I/O when caches are large. Prefer your own cleanup scripts, or a
* custom Metro cache that uses watches, hooks get/set, and/or implements LRU.
*/
export default class AutoCleanFileStore<T> extends FileStore<T> {}
declare class AutoCleanFileStore<T> extends FileStore<T> {
constructor(opts: CleanOptions);
}
export default AutoCleanFileStore;
10 changes: 4 additions & 6 deletions packages/metro-cache/types/stores/FileStore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
* @oncall react_native
*/

export interface Options {
root: string;
}

export default class FileStore<T> {
export type Options = Readonly<{root: string}>;
declare class FileStore<T> {
constructor(options: Options);
get(key: Buffer): Promise<T | null>;
get(key: Buffer): Promise<null | undefined | T>;
set(key: Buffer, value: T): Promise<void>;
clear(): void;
}
export default FileStore;
14 changes: 14 additions & 0 deletions packages/metro-cache/types/stores/HttpError.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

declare class HttpError extends Error {
code: number;
constructor(message: string, code: number);
}
export default HttpError;
14 changes: 8 additions & 6 deletions packages/metro-cache/types/stores/HttpGetStore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
* @oncall react_native
*/

import type {Options} from './HttpStore';
import type {Options as HttpOptions} from './HttpStore';

export default class HttpGetStore<T> {
constructor(options: Options);
get(key: Buffer): Promise<T | null>;
set(key: Buffer, value: T): Promise<void>;
clear(): void;
import HttpStore from './HttpStore';

declare class HttpGetStore<T> extends HttpStore<T> {
constructor(options: HttpOptions);
get(key: Buffer): Promise<null | undefined | T>;
set(_key: Buffer, _value: T): Promise<void>;
}
export default HttpGetStore;
33 changes: 28 additions & 5 deletions packages/metro-cache/types/stores/HttpStore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,41 @@
* @oncall react_native
*/

export interface Options {
import HttpError from './HttpError';
import NetworkError from './NetworkError';

export type Options =
| EndpointOptions
| {getOptions: EndpointOptions; setOptions: EndpointOptions};
type EndpointOptions = {
endpoint: string;
family?: 4 | 6;
timeout?: number;
key?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
cert?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
ca?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
}

export default class HttpStore<T> {
params?: URLSearchParams;
headers?: {[$$Key$$: string]: string};
additionalSuccessStatuses?: ReadonlyArray<number>;
/**
* Whether to include additional debug information in error messages.
*/
debug?: boolean;
/**
* Retry configuration
*/
maxAttempts?: number;
retryNetworkErrors?: boolean;
retryStatuses?: ReadonlySet<number>;
socketPath?: string;
proxy?: string;
};
declare class HttpStore<T> {
static HttpError: typeof HttpError;
static NetworkError: typeof NetworkError;
constructor(options: Options);
get(key: Buffer): Promise<T | null>;
get(key: Buffer): Promise<null | undefined | T>;
set(key: Buffer, value: T): Promise<void>;
clear(): void;
}
export default HttpStore;
14 changes: 14 additions & 0 deletions packages/metro-cache/types/stores/NetworkError.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

declare class NetworkError extends Error {
code: string;
constructor(message: string, code: string);
}
export default NetworkError;
3 changes: 2 additions & 1 deletion packages/metro-cache/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
*/

export interface CacheStore<T> {
get(key: Buffer): T | undefined | Promise<T> | Promise<undefined>;
name?: string;
get(key: Buffer): (null | undefined | T) | Promise<null | undefined | T>;
set(key: Buffer, value: T): void | Promise<void>;
clear(): void | Promise<void>;
}
6 changes: 4 additions & 2 deletions scripts/generateTypeScriptDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const WORKSPACE_ROOT = path.resolve(__dirname, '..');
const TYPES_DIR = 'types';

export const AUTO_GENERATED_PATTERNS: $ReadOnlyArray<string> = [
// TODO: Add globs
'packages/metro-cache/**',
];

// Globs of paths for which we do not generate TypeScript definitions,
Expand All @@ -39,6 +39,7 @@ const IGNORED_PATTERNS = [
'**/__fixtures__/**',
'**/node_modules/**',
'packages/metro-babel-register/**',
'packages/*/build/**',
'packages/metro/src/integration_tests/**',
];

Expand Down Expand Up @@ -67,7 +68,8 @@ export async function generateTsDefsForJsGlobs(
cwd: WORKSPACE_ROOT,
}),
)
.reduce((toProcess, filePath) => {
.reduce((toProcess, posixFilePath) => {
const filePath = path.normalize(posixFilePath);
if (filePath.endsWith('.flow.js')) {
// For .flow.js files, record the `.flow.js` as the source for the
// corresponding `.js` file, which is enforced to be a transparent
Expand Down