Skip to content

Commit 332c438

Browse files
rxliulipenalosa
andauthored
fix: close #3620 (#3621)
* fix: close #3620 * chore: format * fix: add base type * Add tests * fix snapshot * comments --------- Co-authored-by: Samuel Macleod <smacleod@cloudflare.com>
1 parent 6091baf commit 332c438

File tree

22 files changed

+953
-709
lines changed

22 files changed

+953
-709
lines changed

types/defines/rpc.d.ts

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ declare namespace Rpc {
66
// TypeScript uses *structural* typing meaning anything with the same shape as type `T` is a `T`.
77
// For the classes exported by `cloudflare:workers` we want *nominal* typing (i.e. we only want to
88
// accept `WorkerEntrypoint` from `cloudflare:workers`, not any other class with the same shape)
9-
export const __RPC_STUB_BRAND: "__RPC_STUB_BRAND";
10-
export const __RPC_TARGET_BRAND: "__RPC_TARGET_BRAND";
11-
export const __WORKER_ENTRYPOINT_BRAND: "__WORKER_ENTRYPOINT_BRAND";
12-
export const __DURABLE_OBJECT_BRAND: "__DURABLE_OBJECT_BRAND";
13-
export const __WORKFLOW_ENTRYPOINT_BRAND: "__WORKFLOW_ENTRYPOINT_BRAND";
9+
export const __RPC_STUB_BRAND: '__RPC_STUB_BRAND';
10+
export const __RPC_TARGET_BRAND: '__RPC_TARGET_BRAND';
11+
export const __WORKER_ENTRYPOINT_BRAND: '__WORKER_ENTRYPOINT_BRAND';
12+
export const __DURABLE_OBJECT_BRAND: '__DURABLE_OBJECT_BRAND';
13+
export const __WORKFLOW_ENTRYPOINT_BRAND: '__WORKFLOW_ENTRYPOINT_BRAND';
1414
export interface RpcTargetBranded {
1515
[__RPC_TARGET_BRAND]: never;
1616
}
@@ -37,19 +37,7 @@ declare namespace Rpc {
3737
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
3838
type Serializable<T> =
3939
// Structured cloneables
40-
| void
41-
| undefined
42-
| null
43-
| boolean
44-
| number
45-
| bigint
46-
| string
47-
| TypedArray
48-
| ArrayBuffer
49-
| DataView
50-
| Date
51-
| Error
52-
| RegExp
40+
| BaseType
5341
// Structured cloneable composites
5442
| Map<
5543
T extends Map<infer U, unknown> ? Serializable<U> : never,
@@ -61,11 +49,6 @@ declare namespace Rpc {
6149
[K in keyof T]: K extends number | string ? Serializable<T[K]> : never;
6250
}
6351
// Special types
64-
| ReadableStream<Uint8Array>
65-
| WritableStream<Uint8Array>
66-
| Request
67-
| Response
68-
| Headers
6952
| Stub<Stubable>
7053
// Serialized as stubs, see `Stubify`
7154
| Stubable;
@@ -78,6 +61,26 @@ declare namespace Rpc {
7861
}
7962
export type Stub<T extends Stubable> = Provider<T> & StubBase<T>;
8063

64+
// This represents all the types that can be sent as-is over an RPC boundary
65+
type BaseType =
66+
| void
67+
| undefined
68+
| null
69+
| boolean
70+
| number
71+
| bigint
72+
| string
73+
| TypedArray
74+
| ArrayBuffer
75+
| DataView
76+
| Date
77+
| Error
78+
| RegExp
79+
| ReadableStream<Uint8Array>
80+
| WritableStream<Uint8Array>
81+
| Request
82+
| Response
83+
| Headers;
8184
// Recursively rewrite all `Stubable` types with `Stub`s
8285
// prettier-ignore
8386
type Stubify<T> =
@@ -86,6 +89,7 @@ declare namespace Rpc {
8689
: T extends Set<infer V> ? Set<Stubify<V>>
8790
: T extends Array<infer V> ? Array<Stubify<V>>
8891
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
92+
: T extends BaseType ? T
8993
// When using "unknown" instead of "any", interfaces are not stubified.
9094
: T extends { [key: string | number]: any } ? { [K in keyof T]: Stubify<T[K]> }
9195
: T;
@@ -100,6 +104,7 @@ declare namespace Rpc {
100104
: T extends Set<infer V> ? Set<Unstubify<V>>
101105
: T extends Array<infer V> ? Array<Unstubify<V>>
102106
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
107+
: T extends BaseType ? T
103108
: T extends { [key: string | number]: unknown } ? { [K in keyof T]: Unstubify<T[K]> }
104109
: T;
105110
type UnstubifyAll<A extends any[]> = { [I in keyof A]: Unstubify<A[I]> };
@@ -152,10 +157,10 @@ declare namespace Rpc {
152157
}
153158

154159
declare namespace Cloudflare {
155-
interface Env {}
160+
interface Env {}
156161
}
157162

158-
declare module "cloudflare:workers" {
163+
declare module 'cloudflare:workers' {
159164
export type RpcStub<T extends Rpc.Stubable> = Rpc.Stub<T>;
160165
export const RpcStub: {
161166
new <T extends Rpc.Stubable>(value: T): Rpc.Stub<T>;
@@ -209,23 +214,23 @@ declare module "cloudflare:workers" {
209214
}
210215

211216
export type WorkflowDurationLabel =
212-
| "second"
213-
| "minute"
214-
| "hour"
215-
| "day"
216-
| "week"
217-
| "month"
218-
| "year";
217+
| 'second'
218+
| 'minute'
219+
| 'hour'
220+
| 'day'
221+
| 'week'
222+
| 'month'
223+
| 'year';
219224

220225
export type WorkflowSleepDuration =
221-
| `${number} ${WorkflowDurationLabel}${"s" | ""}`
226+
| `${number} ${WorkflowDurationLabel}${'s' | ''}`
222227
| number;
223228

224229
export type WorkflowDelayDuration = WorkflowSleepDuration;
225230

226231
export type WorkflowTimeoutDuration = WorkflowSleepDuration;
227232

228-
export type WorkflowBackoff = "constant" | "linear" | "exponential";
233+
export type WorkflowBackoff = 'constant' | 'linear' | 'exponential';
229234

230235
export type WorkflowStepConfig = {
231236
retries?: {
@@ -243,8 +248,15 @@ declare module "cloudflare:workers" {
243248
};
244249

245250
export abstract class WorkflowStep {
246-
do<T extends Rpc.Serializable<T>>(name: string, callback: () => Promise<T>): Promise<T>;
247-
do<T extends Rpc.Serializable<T>>(name: string, config: WorkflowStepConfig, callback: () => Promise<T>): Promise<T>;
251+
do<T extends Rpc.Serializable<T>>(
252+
name: string,
253+
callback: () => Promise<T>
254+
): Promise<T>;
255+
do<T extends Rpc.Serializable<T>>(
256+
name: string,
257+
config: WorkflowStepConfig,
258+
callback: () => Promise<T>
259+
): Promise<T>;
248260
sleep: (name: string, duration: WorkflowSleepDuration) => Promise<void>;
249261
sleepUntil: (name: string, timestamp: Date | number) => Promise<void>;
250262
}
@@ -261,7 +273,10 @@ declare module "cloudflare:workers" {
261273

262274
constructor(ctx: ExecutionContext, env: Env);
263275

264-
run(event: Readonly<WorkflowEvent<T>>, step: WorkflowStep): Promise<unknown>;
276+
run(
277+
event: Readonly<WorkflowEvent<T>>,
278+
step: WorkflowStep
279+
): Promise<unknown>;
265280
}
266281

267282
export const env: Cloudflare.Env;

types/generated-snapshot/2021-11-03/index.d.ts

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6147,19 +6147,7 @@ declare namespace Rpc {
61476147
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
61486148
type Serializable<T> =
61496149
// Structured cloneables
6150-
| void
6151-
| undefined
6152-
| null
6153-
| boolean
6154-
| number
6155-
| bigint
6156-
| string
6157-
| TypedArray
6158-
| ArrayBuffer
6159-
| DataView
6160-
| Date
6161-
| Error
6162-
| RegExp
6150+
| BaseType
61636151
// Structured cloneable composites
61646152
| Map<
61656153
T extends Map<infer U, unknown> ? Serializable<U> : never,
@@ -6171,11 +6159,6 @@ declare namespace Rpc {
61716159
[K in keyof T]: K extends number | string ? Serializable<T[K]> : never;
61726160
}
61736161
// Special types
6174-
| ReadableStream<Uint8Array>
6175-
| WritableStream<Uint8Array>
6176-
| Request
6177-
| Response
6178-
| Headers
61796162
| Stub<Stubable>
61806163
// Serialized as stubs, see `Stubify`
61816164
| Stubable;
@@ -6186,6 +6169,26 @@ declare namespace Rpc {
61866169
dup(): this;
61876170
}
61886171
export type Stub<T extends Stubable> = Provider<T> & StubBase<T>;
6172+
// This represents all the types that can be sent as-is over an RPC boundary
6173+
type BaseType =
6174+
| void
6175+
| undefined
6176+
| null
6177+
| boolean
6178+
| number
6179+
| bigint
6180+
| string
6181+
| TypedArray
6182+
| ArrayBuffer
6183+
| DataView
6184+
| Date
6185+
| Error
6186+
| RegExp
6187+
| ReadableStream<Uint8Array>
6188+
| WritableStream<Uint8Array>
6189+
| Request
6190+
| Response
6191+
| Headers;
61896192
// Recursively rewrite all `Stubable` types with `Stub`s
61906193
type Stubify<T> = T extends Stubable
61916194
? Stub<T>
@@ -6197,13 +6200,15 @@ declare namespace Rpc {
61976200
? Array<Stubify<V>>
61986201
: T extends ReadonlyArray<infer V>
61996202
? ReadonlyArray<Stubify<V>>
6200-
: T extends {
6201-
[key: string | number]: any;
6202-
}
6203-
? {
6204-
[K in keyof T]: Stubify<T[K]>;
6205-
}
6206-
: T;
6203+
: T extends BaseType
6204+
? T
6205+
: T extends {
6206+
[key: string | number]: any;
6207+
}
6208+
? {
6209+
[K in keyof T]: Stubify<T[K]>;
6210+
}
6211+
: T;
62076212
// Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
62086213
// Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
62096214
// `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
@@ -6218,13 +6223,15 @@ declare namespace Rpc {
62186223
? Array<Unstubify<V>>
62196224
: T extends ReadonlyArray<infer V>
62206225
? ReadonlyArray<Unstubify<V>>
6221-
: T extends {
6222-
[key: string | number]: unknown;
6223-
}
6224-
? {
6225-
[K in keyof T]: Unstubify<T[K]>;
6226-
}
6227-
: T;
6226+
: T extends BaseType
6227+
? T
6228+
: T extends {
6229+
[key: string | number]: unknown;
6230+
}
6231+
? {
6232+
[K in keyof T]: Unstubify<T[K]>;
6233+
}
6234+
: T;
62286235
type UnstubifyAll<A extends any[]> = {
62296236
[I in keyof A]: Unstubify<A[I]>;
62306237
};

types/generated-snapshot/2021-11-03/index.ts

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6130,19 +6130,7 @@ export declare namespace Rpc {
61306130
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
61316131
type Serializable<T> =
61326132
// Structured cloneables
6133-
| void
6134-
| undefined
6135-
| null
6136-
| boolean
6137-
| number
6138-
| bigint
6139-
| string
6140-
| TypedArray
6141-
| ArrayBuffer
6142-
| DataView
6143-
| Date
6144-
| Error
6145-
| RegExp
6133+
| BaseType
61466134
// Structured cloneable composites
61476135
| Map<
61486136
T extends Map<infer U, unknown> ? Serializable<U> : never,
@@ -6154,11 +6142,6 @@ export declare namespace Rpc {
61546142
[K in keyof T]: K extends number | string ? Serializable<T[K]> : never;
61556143
}
61566144
// Special types
6157-
| ReadableStream<Uint8Array>
6158-
| WritableStream<Uint8Array>
6159-
| Request
6160-
| Response
6161-
| Headers
61626145
| Stub<Stubable>
61636146
// Serialized as stubs, see `Stubify`
61646147
| Stubable;
@@ -6169,6 +6152,26 @@ export declare namespace Rpc {
61696152
dup(): this;
61706153
}
61716154
export type Stub<T extends Stubable> = Provider<T> & StubBase<T>;
6155+
// This represents all the types that can be sent as-is over an RPC boundary
6156+
type BaseType =
6157+
| void
6158+
| undefined
6159+
| null
6160+
| boolean
6161+
| number
6162+
| bigint
6163+
| string
6164+
| TypedArray
6165+
| ArrayBuffer
6166+
| DataView
6167+
| Date
6168+
| Error
6169+
| RegExp
6170+
| ReadableStream<Uint8Array>
6171+
| WritableStream<Uint8Array>
6172+
| Request
6173+
| Response
6174+
| Headers;
61726175
// Recursively rewrite all `Stubable` types with `Stub`s
61736176
type Stubify<T> = T extends Stubable
61746177
? Stub<T>
@@ -6180,13 +6183,15 @@ export declare namespace Rpc {
61806183
? Array<Stubify<V>>
61816184
: T extends ReadonlyArray<infer V>
61826185
? ReadonlyArray<Stubify<V>>
6183-
: T extends {
6184-
[key: string | number]: any;
6185-
}
6186-
? {
6187-
[K in keyof T]: Stubify<T[K]>;
6188-
}
6189-
: T;
6186+
: T extends BaseType
6187+
? T
6188+
: T extends {
6189+
[key: string | number]: any;
6190+
}
6191+
? {
6192+
[K in keyof T]: Stubify<T[K]>;
6193+
}
6194+
: T;
61906195
// Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
61916196
// Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
61926197
// `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
@@ -6201,13 +6206,15 @@ export declare namespace Rpc {
62016206
? Array<Unstubify<V>>
62026207
: T extends ReadonlyArray<infer V>
62036208
? ReadonlyArray<Unstubify<V>>
6204-
: T extends {
6205-
[key: string | number]: unknown;
6206-
}
6207-
? {
6208-
[K in keyof T]: Unstubify<T[K]>;
6209-
}
6210-
: T;
6209+
: T extends BaseType
6210+
? T
6211+
: T extends {
6212+
[key: string | number]: unknown;
6213+
}
6214+
? {
6215+
[K in keyof T]: Unstubify<T[K]>;
6216+
}
6217+
: T;
62116218
type UnstubifyAll<A extends any[]> = {
62126219
[I in keyof A]: Unstubify<A[I]>;
62136220
};

0 commit comments

Comments
 (0)