-
Notifications
You must be signed in to change notification settings - Fork 207
/
index.ts
104 lines (99 loc) · 3.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { z } from "zod";
import { ValueOf } from "../shared";
import { CACHE_PLUGIN, CACHE_PLUGIN_NAME } from "./cache";
import { CORE_PLUGIN, CORE_PLUGIN_NAME } from "./core";
import { D1_PLUGIN, D1_PLUGIN_NAME } from "./d1";
import { DURABLE_OBJECTS_PLUGIN, DURABLE_OBJECTS_PLUGIN_NAME } from "./do";
import { KV_PLUGIN, KV_PLUGIN_NAME } from "./kv";
import { QUEUES_PLUGIN, QUEUES_PLUGIN_NAME } from "./queues";
import { R2_PLUGIN, R2_PLUGIN_NAME } from "./r2";
export const PLUGINS = {
[CORE_PLUGIN_NAME]: CORE_PLUGIN,
[CACHE_PLUGIN_NAME]: CACHE_PLUGIN,
[D1_PLUGIN_NAME]: D1_PLUGIN,
[DURABLE_OBJECTS_PLUGIN_NAME]: DURABLE_OBJECTS_PLUGIN,
[KV_PLUGIN_NAME]: KV_PLUGIN,
[QUEUES_PLUGIN_NAME]: QUEUES_PLUGIN,
[R2_PLUGIN_NAME]: R2_PLUGIN,
};
export type Plugins = typeof PLUGINS;
// Note, we used to define these as...
//
// ```ts
// // A | B | ... => A & B & ... (https://stackoverflow.com/a/50375286)
// export type UnionToIntersection<U> = (
// U extends any ? (k: U) => void : never
// ) extends (k: infer I) => void
// ? I
// : never;
// export type WorkerOptions = UnionToIntersection<
// z.infer<ValueOf<Plugins>["options"]>
// >;
// export type SharedOptions = UnionToIntersection<
// z.infer<Exclude<ValueOf<Plugins>["sharedOptions"], undefined>>
// >;
// ```
//
// This caused issues when we tried to make `CORE_PLUGIN.options` an
// intersection of a union type (source options) and a regular object type.
//
// ```ts
// type A = { x: 1 } | { x: 2 };
// type B = A & { y: string };
// type C = UnionToIntersection<B>;
// ```
//
// In the above example, `C` is typed `{x: 1} & {x: 2} & {y: string}` which
// simplifies to `never`. Using `[U] extends [any]` instead of `U extends any`
// disables distributivity of union types over conditional types, which types
// `C` `({x: 1} | {x: 2}) & {y: string}` as expected. Unfortunately, this
// appears to prevent us assigning to any `MiniflareOptions` instances after
// creation, which we do quite a lot in tests.
//
// Considering we don't have too many plugins, we now just define these types
// manually, which has the added benefit of faster type checking.
export type WorkerOptions = z.infer<typeof CORE_PLUGIN.options> &
z.infer<typeof CACHE_PLUGIN.options> &
z.infer<typeof D1_PLUGIN.options> &
z.infer<typeof DURABLE_OBJECTS_PLUGIN.options> &
z.infer<typeof KV_PLUGIN.options> &
z.infer<typeof QUEUES_PLUGIN.options> &
z.infer<typeof R2_PLUGIN.options>;
export type SharedOptions = z.infer<typeof CORE_PLUGIN.sharedOptions> &
z.infer<typeof CACHE_PLUGIN.sharedOptions> &
z.infer<typeof D1_PLUGIN.sharedOptions> &
z.infer<typeof DURABLE_OBJECTS_PLUGIN.sharedOptions> &
z.infer<typeof KV_PLUGIN.sharedOptions> &
z.infer<typeof R2_PLUGIN.sharedOptions>;
export const PLUGIN_ENTRIES = Object.entries(PLUGINS) as [
keyof Plugins,
ValueOf<Plugins>
][];
export * from "./shared";
// TODO: be more liberal on exports?
export * from "./cache";
export {
CORE_PLUGIN,
CORE_PLUGIN_NAME,
SERVICE_ENTRY,
CoreOptionsSchema,
CoreSharedOptionsSchema,
getGlobalServices,
ModuleRuleTypeSchema,
ModuleRuleSchema,
ModuleDefinitionSchema,
SourceOptionsSchema,
} from "./core";
export type {
ModuleRuleType,
ModuleRule,
ModuleDefinition,
GlobalServicesOptions,
SourceOptions,
KeyPair,
} from "./core";
export * from "./d1";
export * from "./do";
export * from "./kv";
export * from "./queues";
export * from "./r2";