capnweb 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -12,18 +12,26 @@ import { IncomingMessage, ServerResponse, OutgoingHttpHeaders, OutgoingHttpHeade
12
12
  // accept `WorkerEntrypoint` from `cloudflare:workers`, not any other class with the same shape)
13
13
  declare const __RPC_STUB_BRAND: '__RPC_STUB_BRAND';
14
14
  declare const __RPC_TARGET_BRAND: '__RPC_TARGET_BRAND';
15
+ // Distinguishes mapper placeholders from regular values so param unwrapping can accept them.
16
+ declare const __RPC_MAP_VALUE_BRAND: unique symbol;
15
17
  interface RpcTargetBranded {
16
18
  [__RPC_TARGET_BRAND]: never;
17
19
  }
18
20
 
19
21
  // Types that can be used through `Stub`s
20
- type Stubable = RpcTargetBranded | ((...args: any[]) => any);
22
+ // `never[]` preserves compatibility with strongly-typed function signatures without introducing
23
+ // `any` into inference.
24
+ type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
25
+
26
+ type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
21
27
 
22
28
  // Types that can be passed over RPC
23
29
  // The reason for using a generic type here is to build a serializable subset of structured
24
30
  // cloneable composite types. This allows types defined with the "interface" keyword to pass the
25
31
  // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
26
32
  type RpcCompatible<T> =
33
+ // Allow `unknown` as a leaf so records/interfaces with `unknown` fields remain compatible.
34
+ | (IsUnknown<T> extends true ? unknown : never)
27
35
  // Structured cloneables
28
36
  | BaseType
29
37
  // Structured cloneable composites
@@ -35,7 +43,7 @@ type RpcCompatible<T> =
35
43
  | Array<T extends Array<infer U> ? RpcCompatible<U> : never>
36
44
  | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
37
45
  | {
38
- [K in keyof T]: K extends number | string ? RpcCompatible<T[K]> : never;
46
+ [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>;
39
47
  }
40
48
  | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
41
49
  // Special types
@@ -45,7 +53,7 @@ type RpcCompatible<T> =
45
53
 
46
54
  // Base type for all RPC stubs, including common memory management methods.
47
55
  // `T` is used as a marker type for unwrapping `Stub`s later.
48
- interface StubBase<T extends RpcCompatible<T>> extends Disposable {
56
+ interface StubBase<T = unknown> extends Disposable {
49
57
  [__RPC_STUB_BRAND]: T;
50
58
  dup(): this;
51
59
  onRpcBroken(callback: (error: any) => void): void;
@@ -82,7 +90,7 @@ type BaseType =
82
90
  | Error
83
91
  | RegExp
84
92
  | ReadableStream<Uint8Array>
85
- | WritableStream<Uint8Array>
93
+ | WritableStream<any> // Chunk type can be any RPC-compatible type
86
94
  | Request
87
95
  | Response
88
96
  | Headers;
@@ -102,7 +110,7 @@ type Stubify<T> =
102
110
  : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
103
111
  : T extends BaseType ? T
104
112
  // When using "unknown" instead of "any", interfaces are not stubified.
105
- : T extends { [key: string | number]: any } ? { [K in keyof T]: Stubify<T[K]> }
113
+ : T extends { [key: string | number]: any } ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]> }
106
114
  : T;
107
115
 
108
116
  // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
@@ -110,7 +118,10 @@ type Stubify<T> =
110
118
  // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
111
119
  // prettier-ignore
112
120
  type UnstubifyInner<T> =
113
- T extends StubBase<infer V> ? (T | V) // can provide either stub or local RpcTarget
121
+ // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
122
+ // is already assignable to the value type (important for callback contextual typing).
123
+ T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>))
124
+ : T extends Promise<infer U> ? UnstubifyInner<U>
114
125
  : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
115
126
  : T extends Set<infer V> ? Set<Unstubify<V>>
116
127
  : T extends [] ? []
@@ -120,14 +131,30 @@ type UnstubifyInner<T> =
120
131
  : T extends Array<infer V> ? Array<Unstubify<V>>
121
132
  : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
122
133
  : T extends BaseType ? T
123
- : T extends { [key: string | number]: unknown } ? { [K in keyof T]: Unstubify<T[K]> }
134
+ : T extends { [key: string | number]: unknown } ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]> }
124
135
  : T;
125
136
 
126
137
  // You can put promises anywhere in the params and they'll be resolved before delivery.
127
138
  // (This also covers RpcPromise, because it's defined as being a Promise.)
128
- type Unstubify<T> = UnstubifyInner<T> | Promise<UnstubifyInner<T>>
139
+ // Map placeholders are also allowed so primitive map callback inputs can be forwarded directly
140
+ // into RPC params.
141
+ //
142
+ // Keep raw non-stub members so generic assignability still works when UnstubifyInner<T> is deferred.
143
+ // Remove stub members from mixed unions so callback params don’t get both stub and unstubbed signatures.
144
+ // Marker carried by map() callback inputs. This lets primitive placeholders flow through params.
145
+ type Unstubify<T> =
146
+ | NonStubMembers<T>
147
+ | UnstubifyInner<T>
148
+ | Promise<UnstubifyInner<T>>
149
+ | MapValuePlaceholder<UnstubifyInner<T>>;
150
+
151
+ type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
152
+
153
+ interface MapValuePlaceholder<T> {
154
+ [__RPC_MAP_VALUE_BRAND]: T;
155
+ }
129
156
 
130
- type UnstubifyAll<A extends any[]> = { [I in keyof A]: Unstubify<A[I]> };
157
+ type NonStubMembers<T> = Exclude<T, StubBase<any>>;
131
158
 
132
159
  // Utility type for adding `Disposable`s to `object` types only.
133
160
  // Note `unknown & T` is equivalent to `T`.
@@ -142,17 +169,22 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
142
169
  // Intersecting with `(Maybe)Provider` allows pipelining.
143
170
  // prettier-ignore
144
171
  type Result<R> =
145
- R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
172
+ IsAny<R> extends true ? UnknownResult
173
+ : IsUnknown<R> extends true ? UnknownResult
174
+ : R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
146
175
  : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
147
176
  : never;
148
177
 
178
+ type IsAny<T> = 0 extends (1 & T) ? true : false;
179
+ type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
180
+
149
181
  // Type for method or property on an RPC interface.
150
182
  // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
151
183
  // Unwrapping `Stub`s allows calling with `Stubable` arguments.
152
184
  // For properties, rewrite types to be `Result`s.
153
185
  // In each case, unwrap `Promise`s.
154
186
  type MethodOrProperty<V> = V extends (...args: infer P) => infer R
155
- ? (...args: UnstubifyAll<P>) => Result<Awaited<R>>
187
+ ? (...args: UnstubifyAll<P>) => IsAny<R> extends true ? UnknownResult : Result<Awaited<R>>
156
188
  : Result<Awaited<V>>;
157
189
 
158
190
  // Type for the callable part of an `Provider` if `T` is callable.
@@ -161,24 +193,78 @@ type MaybeCallableProvider<T> = T extends (...args: any[]) => any
161
193
  ? MethodOrProperty<T>
162
194
  : unknown;
163
195
 
196
+ type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
197
+ type MapCallbackValue<T> =
198
+ // `Omit` removes call signatures, so re-intersect callable provider behavior.
199
+ T extends unknown
200
+ ? Omit<Result<T>, keyof Promise<unknown>> &
201
+ MaybeCallableProvider<T> &
202
+ MapValuePlaceholder<T>
203
+ : never;
204
+ type InvalidNativePromiseInMapResult<T, Seen = never> =
205
+ T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
206
+ type InvalidNativePromiseInMapResultImpl<T, Seen> =
207
+ [T] extends [Seen] ? never
208
+ // RpcPromise is modeled as Promise & StubBase, so allow promise-like stub values.
209
+ : T extends StubBase<any> ? never
210
+ // Native thenables cannot be represented in map recordings, even when typed as PromiseLike.
211
+ : T extends PromiseLike<unknown> ? T
212
+ : T extends Map<infer K, infer V>
213
+ ? InvalidNativePromiseInMapResult<K, Seen | T> |
214
+ InvalidNativePromiseInMapResult<V, Seen | T>
215
+ : T extends Set<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T>
216
+ : T extends readonly [] ? never
217
+ : T extends readonly [infer Head, ...infer Tail]
218
+ ? InvalidNativePromiseInMapResult<Head, Seen | T> |
219
+ InvalidNativePromiseInMapResult<Tail[number], Seen | T>
220
+ : T extends ReadonlyArray<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T>
221
+ : T extends { [key: string | number]: unknown }
222
+ ? InvalidNativePromiseInMapResult<
223
+ T[Extract<keyof T, string | number>],
224
+ Seen | T
225
+ >
226
+ : never;
227
+ type MapCallbackReturn<T> =
228
+ InvalidNativePromiseInMapResult<T> extends never ? T : never;
229
+ type ArrayProvider<E> = {
230
+ [K in number]: MethodOrProperty<E>;
231
+ } & {
232
+ map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
233
+ };
234
+ type TupleProvider<T extends ReadonlyArray<unknown>> = {
235
+ [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>;
236
+ } & ArrayProvider<T[number]>;
237
+
164
238
  // Base type for all other types providing RPC-like interfaces.
165
239
  // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
166
240
  type Provider<T> = MaybeCallableProvider<T> &
167
- (T extends Array<infer U>
168
- ? {
169
- [key: number]: MethodOrProperty<U>;
170
- } & {
171
- map<V>(callback: (elem: U) => V): Result<Array<V>>;
172
- }
241
+ (T extends ReadonlyArray<unknown>
242
+ ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T>
173
243
  : {
174
244
  [K in Exclude<
175
245
  keyof T,
176
246
  symbol | keyof StubBase<never>
177
247
  >]: MethodOrProperty<T[K]>;
178
248
  } & {
179
- map<V>(callback: (value: NonNullable<T>) => V): Result<Array<V>>;
249
+ map<V>(
250
+ callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>
251
+ ): Result<Array<V>>;
180
252
  });
181
253
 
254
+ declare global {
255
+ interface Uint8Array {
256
+ toBase64?(options?: {
257
+ alphabet?: "base64" | "base64url";
258
+ omitPadding?: boolean;
259
+ }): string;
260
+ }
261
+ interface Uint8ArrayConstructor {
262
+ fromBase64?(text: string, options?: {
263
+ alphabet?: "base64" | "base64url";
264
+ lastChunkHandling?: "loose" | "strict" | "stop-before-partial";
265
+ }): Uint8Array;
266
+ }
267
+ }
182
268
  /**
183
269
  * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
184
270
  * RPC stubs, but it will support basic data types.
package/dist/index.d.ts CHANGED
@@ -12,18 +12,26 @@ import { IncomingMessage, ServerResponse, OutgoingHttpHeaders, OutgoingHttpHeade
12
12
  // accept `WorkerEntrypoint` from `cloudflare:workers`, not any other class with the same shape)
13
13
  declare const __RPC_STUB_BRAND: '__RPC_STUB_BRAND';
14
14
  declare const __RPC_TARGET_BRAND: '__RPC_TARGET_BRAND';
15
+ // Distinguishes mapper placeholders from regular values so param unwrapping can accept them.
16
+ declare const __RPC_MAP_VALUE_BRAND: unique symbol;
15
17
  interface RpcTargetBranded {
16
18
  [__RPC_TARGET_BRAND]: never;
17
19
  }
18
20
 
19
21
  // Types that can be used through `Stub`s
20
- type Stubable = RpcTargetBranded | ((...args: any[]) => any);
22
+ // `never[]` preserves compatibility with strongly-typed function signatures without introducing
23
+ // `any` into inference.
24
+ type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
25
+
26
+ type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
21
27
 
22
28
  // Types that can be passed over RPC
23
29
  // The reason for using a generic type here is to build a serializable subset of structured
24
30
  // cloneable composite types. This allows types defined with the "interface" keyword to pass the
25
31
  // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
26
32
  type RpcCompatible<T> =
33
+ // Allow `unknown` as a leaf so records/interfaces with `unknown` fields remain compatible.
34
+ | (IsUnknown<T> extends true ? unknown : never)
27
35
  // Structured cloneables
28
36
  | BaseType
29
37
  // Structured cloneable composites
@@ -35,7 +43,7 @@ type RpcCompatible<T> =
35
43
  | Array<T extends Array<infer U> ? RpcCompatible<U> : never>
36
44
  | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
37
45
  | {
38
- [K in keyof T]: K extends number | string ? RpcCompatible<T[K]> : never;
46
+ [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>;
39
47
  }
40
48
  | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
41
49
  // Special types
@@ -45,7 +53,7 @@ type RpcCompatible<T> =
45
53
 
46
54
  // Base type for all RPC stubs, including common memory management methods.
47
55
  // `T` is used as a marker type for unwrapping `Stub`s later.
48
- interface StubBase<T extends RpcCompatible<T>> extends Disposable {
56
+ interface StubBase<T = unknown> extends Disposable {
49
57
  [__RPC_STUB_BRAND]: T;
50
58
  dup(): this;
51
59
  onRpcBroken(callback: (error: any) => void): void;
@@ -82,7 +90,7 @@ type BaseType =
82
90
  | Error
83
91
  | RegExp
84
92
  | ReadableStream<Uint8Array>
85
- | WritableStream<Uint8Array>
93
+ | WritableStream<any> // Chunk type can be any RPC-compatible type
86
94
  | Request
87
95
  | Response
88
96
  | Headers;
@@ -102,7 +110,7 @@ type Stubify<T> =
102
110
  : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
103
111
  : T extends BaseType ? T
104
112
  // When using "unknown" instead of "any", interfaces are not stubified.
105
- : T extends { [key: string | number]: any } ? { [K in keyof T]: Stubify<T[K]> }
113
+ : T extends { [key: string | number]: any } ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]> }
106
114
  : T;
107
115
 
108
116
  // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
@@ -110,7 +118,10 @@ type Stubify<T> =
110
118
  // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
111
119
  // prettier-ignore
112
120
  type UnstubifyInner<T> =
113
- T extends StubBase<infer V> ? (T | V) // can provide either stub or local RpcTarget
121
+ // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
122
+ // is already assignable to the value type (important for callback contextual typing).
123
+ T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>))
124
+ : T extends Promise<infer U> ? UnstubifyInner<U>
114
125
  : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
115
126
  : T extends Set<infer V> ? Set<Unstubify<V>>
116
127
  : T extends [] ? []
@@ -120,14 +131,30 @@ type UnstubifyInner<T> =
120
131
  : T extends Array<infer V> ? Array<Unstubify<V>>
121
132
  : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
122
133
  : T extends BaseType ? T
123
- : T extends { [key: string | number]: unknown } ? { [K in keyof T]: Unstubify<T[K]> }
134
+ : T extends { [key: string | number]: unknown } ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]> }
124
135
  : T;
125
136
 
126
137
  // You can put promises anywhere in the params and they'll be resolved before delivery.
127
138
  // (This also covers RpcPromise, because it's defined as being a Promise.)
128
- type Unstubify<T> = UnstubifyInner<T> | Promise<UnstubifyInner<T>>
139
+ // Map placeholders are also allowed so primitive map callback inputs can be forwarded directly
140
+ // into RPC params.
141
+ //
142
+ // Keep raw non-stub members so generic assignability still works when UnstubifyInner<T> is deferred.
143
+ // Remove stub members from mixed unions so callback params don’t get both stub and unstubbed signatures.
144
+ // Marker carried by map() callback inputs. This lets primitive placeholders flow through params.
145
+ type Unstubify<T> =
146
+ | NonStubMembers<T>
147
+ | UnstubifyInner<T>
148
+ | Promise<UnstubifyInner<T>>
149
+ | MapValuePlaceholder<UnstubifyInner<T>>;
150
+
151
+ type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
152
+
153
+ interface MapValuePlaceholder<T> {
154
+ [__RPC_MAP_VALUE_BRAND]: T;
155
+ }
129
156
 
130
- type UnstubifyAll<A extends any[]> = { [I in keyof A]: Unstubify<A[I]> };
157
+ type NonStubMembers<T> = Exclude<T, StubBase<any>>;
131
158
 
132
159
  // Utility type for adding `Disposable`s to `object` types only.
133
160
  // Note `unknown & T` is equivalent to `T`.
@@ -142,17 +169,22 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
142
169
  // Intersecting with `(Maybe)Provider` allows pipelining.
143
170
  // prettier-ignore
144
171
  type Result<R> =
145
- R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
172
+ IsAny<R> extends true ? UnknownResult
173
+ : IsUnknown<R> extends true ? UnknownResult
174
+ : R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
146
175
  : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
147
176
  : never;
148
177
 
178
+ type IsAny<T> = 0 extends (1 & T) ? true : false;
179
+ type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
180
+
149
181
  // Type for method or property on an RPC interface.
150
182
  // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
151
183
  // Unwrapping `Stub`s allows calling with `Stubable` arguments.
152
184
  // For properties, rewrite types to be `Result`s.
153
185
  // In each case, unwrap `Promise`s.
154
186
  type MethodOrProperty<V> = V extends (...args: infer P) => infer R
155
- ? (...args: UnstubifyAll<P>) => Result<Awaited<R>>
187
+ ? (...args: UnstubifyAll<P>) => IsAny<R> extends true ? UnknownResult : Result<Awaited<R>>
156
188
  : Result<Awaited<V>>;
157
189
 
158
190
  // Type for the callable part of an `Provider` if `T` is callable.
@@ -161,24 +193,78 @@ type MaybeCallableProvider<T> = T extends (...args: any[]) => any
161
193
  ? MethodOrProperty<T>
162
194
  : unknown;
163
195
 
196
+ type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
197
+ type MapCallbackValue<T> =
198
+ // `Omit` removes call signatures, so re-intersect callable provider behavior.
199
+ T extends unknown
200
+ ? Omit<Result<T>, keyof Promise<unknown>> &
201
+ MaybeCallableProvider<T> &
202
+ MapValuePlaceholder<T>
203
+ : never;
204
+ type InvalidNativePromiseInMapResult<T, Seen = never> =
205
+ T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
206
+ type InvalidNativePromiseInMapResultImpl<T, Seen> =
207
+ [T] extends [Seen] ? never
208
+ // RpcPromise is modeled as Promise & StubBase, so allow promise-like stub values.
209
+ : T extends StubBase<any> ? never
210
+ // Native thenables cannot be represented in map recordings, even when typed as PromiseLike.
211
+ : T extends PromiseLike<unknown> ? T
212
+ : T extends Map<infer K, infer V>
213
+ ? InvalidNativePromiseInMapResult<K, Seen | T> |
214
+ InvalidNativePromiseInMapResult<V, Seen | T>
215
+ : T extends Set<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T>
216
+ : T extends readonly [] ? never
217
+ : T extends readonly [infer Head, ...infer Tail]
218
+ ? InvalidNativePromiseInMapResult<Head, Seen | T> |
219
+ InvalidNativePromiseInMapResult<Tail[number], Seen | T>
220
+ : T extends ReadonlyArray<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T>
221
+ : T extends { [key: string | number]: unknown }
222
+ ? InvalidNativePromiseInMapResult<
223
+ T[Extract<keyof T, string | number>],
224
+ Seen | T
225
+ >
226
+ : never;
227
+ type MapCallbackReturn<T> =
228
+ InvalidNativePromiseInMapResult<T> extends never ? T : never;
229
+ type ArrayProvider<E> = {
230
+ [K in number]: MethodOrProperty<E>;
231
+ } & {
232
+ map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
233
+ };
234
+ type TupleProvider<T extends ReadonlyArray<unknown>> = {
235
+ [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>;
236
+ } & ArrayProvider<T[number]>;
237
+
164
238
  // Base type for all other types providing RPC-like interfaces.
165
239
  // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
166
240
  type Provider<T> = MaybeCallableProvider<T> &
167
- (T extends Array<infer U>
168
- ? {
169
- [key: number]: MethodOrProperty<U>;
170
- } & {
171
- map<V>(callback: (elem: U) => V): Result<Array<V>>;
172
- }
241
+ (T extends ReadonlyArray<unknown>
242
+ ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T>
173
243
  : {
174
244
  [K in Exclude<
175
245
  keyof T,
176
246
  symbol | keyof StubBase<never>
177
247
  >]: MethodOrProperty<T[K]>;
178
248
  } & {
179
- map<V>(callback: (value: NonNullable<T>) => V): Result<Array<V>>;
249
+ map<V>(
250
+ callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>
251
+ ): Result<Array<V>>;
180
252
  });
181
253
 
254
+ declare global {
255
+ interface Uint8Array {
256
+ toBase64?(options?: {
257
+ alphabet?: "base64" | "base64url";
258
+ omitPadding?: boolean;
259
+ }): string;
260
+ }
261
+ interface Uint8ArrayConstructor {
262
+ fromBase64?(text: string, options?: {
263
+ alphabet?: "base64" | "base64url";
264
+ lastChunkHandling?: "loose" | "strict" | "stop-before-partial";
265
+ }): Uint8Array;
266
+ }
267
+ }
182
268
  /**
183
269
  * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
184
270
  * RPC stubs, but it will support basic data types.