import type { BodyInit, DurableObject, DurableObjectId, DurableObjectState, DurableObjectStorage, ExecutionContext, FetchEvent, Headers, ScheduledEvent, } from "@cloudflare/workers-types/experimental"; declare global { /** * Get object containing all bindings (e.g. KV namespaces, R2 buckets). * This is the `env` parameter passed to module workers. */ function getMiniflareBindings>(): Bindings; /** * Get the underlying Durable Object storage for the specified ID. * This is the `storage` property on the `state` parameter passed to the * object constructor. */ function getMiniflareDurableObjectStorage( id: DurableObjectId ): Promise; /** * Get the Durable Object state for the specified ID. * This is the `state` parameter passed to the object constructor. */ function getMiniflareDurableObjectState( id: DurableObjectId ): Promise; /** * Gets the singleton Durable Object instance for the specified ID. * This is the same instance requests will be sent to via stubs obtained from * Durable Object namespace bindings. */ function getMiniflareDurableObjectInstance( id: DurableObjectId ): Promise; /** * Waits for the Durable Object's associated input gate to open, closes the * input gate, runs the closure under a new output gate, opens the input gate, * then waits for the output gate to open. If you're calling `fetch` directly * on a Durable Object instance, make sure to wrap the call with this to * prevent race conditions. */ function runWithMiniflareDurableObjectGates( state: DurableObjectState, closure: () => T | Promise ): Promise; /** * Gets the preconfigured `MockAgent` attached to Miniflare's `fetch` * function. Use this to mock responses to `fetch` requests. */ function getMiniflareFetchMock(): MockAgent; /** * Waits for all `waitUntil`ed `Promise`s on the specified event or context * to resolve, returning a `Promise` that resolves to an array of the values * they resolve with. */ function getMiniflareWaitUntil( event: FetchEvent | ScheduledEvent | ExecutionContext ): Promise; /** * Immediately invokes scheduled Durable Object alarms. If an array of IDs is * specified, only those Durable Objects will have their scheduled alarms * invoked, otherwise all scheduled alarms will be invoked. */ function flushMiniflareDurableObjectAlarms( ids?: DurableObjectId[] ): Promise; /** * Gets an array containing IDs for all Durable Object instances Miniflare * has constructed for the specified namespace. */ function getMiniflareDurableObjectIds( namespace: string ): Promise; // eslint-disable-next-line no-var var ExecutionContext: { prototype: ExecutionContext; new (): ExecutionContext; }; } // Taken from `undici` (https://github.com/nodejs/undici/tree/main/types) with // no dependency on `@types/node` and with unusable functions removed // // MIT License // // Copyright (c) Matteo Collina and Undici contributors // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. type IncomingHttpHeaders = Record; /** The scope associated with a mock dispatch. */ declare abstract class MockScope { /** Delay a reply by a set amount of time in ms. */ delay(waitInMs: number): MockScope; /** Persist the defined mock data for the associated reply. It will return the defined mock data indefinitely. */ persist(): MockScope; /** Define a reply for a set amount of matching requests. */ times(repeatTimes: number): MockScope; } /** The interceptor for a Mock. */ declare abstract class MockInterceptor { /** Mock an undici request with the defined reply. */ reply( replyOptionsCallback: MockInterceptor.MockReplyOptionsCallback ): MockScope; reply( statusCode: number, data?: | TData | Buffer | string | MockInterceptor.MockResponseDataHandler, responseOptions?: MockInterceptor.MockResponseOptions ): MockScope; /** Mock an undici request by throwing the defined reply error. */ replyWithError(error: TError): MockScope; /** Set default reply headers on the interceptor for subsequent mocked replies. */ defaultReplyHeaders(headers: IncomingHttpHeaders): MockInterceptor; /** Set default reply trailers on the interceptor for subsequent mocked replies. */ defaultReplyTrailers(trailers: Record): MockInterceptor; /** Set automatically calculated content-length header on subsequent mocked replies. */ replyContentLength(): MockInterceptor; } declare namespace MockInterceptor { /** MockInterceptor options. */ export interface Options { /** Path to intercept on. */ path: string | RegExp | ((path: string) => boolean); /** Method to intercept on. Defaults to GET. */ method?: string | RegExp | ((method: string) => boolean); /** Body to intercept on. */ body?: string | RegExp | ((body: string) => boolean); /** Headers to intercept on. */ headers?: | Record boolean)> | ((headers: Record) => boolean); /** Query params to intercept on */ query?: Record; } export interface MockDispatch< TData extends object = object, TError extends Error = Error > extends Options { times: number | null; persist: boolean; consumed: boolean; data: MockDispatchData; } export interface MockDispatchData< TData extends object = object, TError extends Error = Error > extends MockResponseOptions { error: TError | null; statusCode?: number; data?: TData | string; } export interface MockResponseOptions { headers?: IncomingHttpHeaders; trailers?: Record; } export interface MockResponseCallbackOptions { path: string; origin: string; method: string; body?: BodyInit; headers: Headers | Record; maxRedirections: number; } export type MockResponseDataHandler = ( opts: MockResponseCallbackOptions ) => TData | Buffer | string; export type MockReplyOptionsCallback = ( opts: MockResponseCallbackOptions ) => { statusCode: number; data?: TData | Buffer | string; responseOptions?: MockResponseOptions; }; } interface Interceptable { /** Intercepts any matching requests that use the same origin as this mock client. */ intercept(options: MockInterceptor.Options): MockInterceptor; } interface PendingInterceptor extends MockInterceptor.MockDispatch { origin: string; } interface PendingInterceptorsFormatter { format(pendingInterceptors: readonly PendingInterceptor[]): string; } /** A mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. */ declare abstract class MockAgent { /** Creates and retrieves mock Dispatcher instances which can then be used to intercept HTTP requests. If the number of connections on the mock agent is set to 1, a MockClient instance is returned. Otherwise a MockPool instance is returned. */ get(origin: string | RegExp | ((origin: string) => boolean)): Interceptable; /** Disables mocking in MockAgent. */ deactivate(): void; /** Enables mocking in a MockAgent instance. When instantiated, a MockAgent is automatically activated. Therefore, this method is only effective after MockAgent.deactivate has been called. */ activate(): void; /** Define host matchers so only matching requests that aren't intercepted by the mock dispatchers will be attempted. */ enableNetConnect(host?: string | RegExp | ((host: string) => boolean)): void; /** Causes all requests to throw when requests are not matched in a MockAgent intercept. */ disableNetConnect(): void; pendingInterceptors(): PendingInterceptor[]; assertNoPendingInterceptors(options?: { pendingInterceptorsFormatter?: PendingInterceptorsFormatter; }): void; }