@miniflare/shared
Advanced tools
Comparing version 2.0.0-rc.4 to 2.0.0-rc.5
@@ -9,2 +9,4 @@ import { Awaitable as Awaitable_2 } from '@miniflare/shared'; | ||
export declare function assertInRequest(): void; | ||
export declare type Awaitable<T> = T | Promise<T>; | ||
@@ -72,3 +74,3 @@ | ||
export declare class InputGatedEventTarget<EventMap extends Record<string, Event>> extends TypedEventTarget<EventMap> { | ||
protected [kWrapListener]<Type extends keyof EventMap>(listener: (event: EventMap[Type]) => void): TypedEventListener<EventMap[Type]>; | ||
protected [kWrapListener]<Type extends keyof EventMap>(listener: (event: EventMap[Type]) => void): (event: EventMap[Type]) => void; | ||
} | ||
@@ -81,5 +83,5 @@ | ||
export declare class Log { | ||
#private; | ||
readonly level: LogLevel; | ||
readonly suffix?: string | undefined; | ||
constructor(level?: LogLevel, suffix?: string | undefined); | ||
constructor(level?: LogLevel, opts?: LogOptions); | ||
log(message: string): void; | ||
@@ -103,2 +105,7 @@ logWithLevel(level: LogLevel, message: string): void; | ||
export declare interface LogOptions { | ||
prefix?: string; | ||
suffix?: string; | ||
} | ||
export declare function logOptions<Plugins extends PluginSignatures>(plugins: PluginEntries<Plugins>, log: Log, options: PluginOptions<Plugins>): void; | ||
@@ -127,2 +134,7 @@ | ||
export declare interface Mount<Request = any, Response = any> { | ||
moduleExports?: Context; | ||
dispatchFetch?: (request: Request) => Promise<Response>; | ||
} | ||
export declare class Mutex { | ||
@@ -146,3 +158,3 @@ private locked; | ||
export declare type OptionMetadata = OptionMetadataType<OptionType.NONE, any> | OptionMetadataType<OptionType.BOOLEAN, boolean> | OptionMetadataType<OptionType.NUMBER, number> | OptionMetadataType<OptionType.STRING, string> | OptionMetadataType<OptionType.STRING_POSITIONAL, string> | OptionMetadataType<OptionType.BOOLEAN_STRING, boolean | string> | OptionMetadataType<OptionType.ARRAY, string[]> | OptionMetadataType<OptionType.OBJECT, any>; | ||
export declare type OptionMetadata = OptionMetadataType<OptionType.NONE, any> | OptionMetadataType<OptionType.BOOLEAN, boolean> | OptionMetadataType<OptionType.NUMBER, number> | OptionMetadataType<OptionType.STRING, string> | OptionMetadataType<OptionType.STRING_POSITIONAL, string> | OptionMetadataType<OptionType.BOOLEAN_STRING, boolean | string> | OptionMetadataType<OptionType.BOOLEAN_NUMBER, boolean | number> | OptionMetadataType<OptionType.ARRAY, string[]> | OptionMetadataType<OptionType.OBJECT, any>; | ||
@@ -155,3 +167,3 @@ export declare interface OptionMetadataType<Type extends OptionType, Value> { | ||
description?: string; | ||
negatable?: Type extends OptionType.BOOLEAN | OptionType.BOOLEAN_STRING ? boolean : undefined; | ||
negatable?: Type extends OptionType.BOOLEAN | OptionType.BOOLEAN_STRING | OptionType.BOOLEAN_NUMBER ? boolean : undefined; | ||
logName?: string; | ||
@@ -172,4 +184,5 @@ logValue?: (value: Value) => string | undefined; | ||
BOOLEAN_STRING = 5, | ||
ARRAY = 6, | ||
OBJECT = 7 | ||
BOOLEAN_NUMBER = 6, | ||
ARRAY = 7, | ||
OBJECT = 8 | ||
} | ||
@@ -199,3 +212,3 @@ | ||
beforeReload?(): Awaitable<void>; | ||
reload?(bindings: Context, moduleExports: Context, mountedModuleExports: Record<string, Context>): Awaitable<void>; | ||
reload?(bindings: Context, moduleExports: Context, mounts: Map<string, Mount>): Awaitable<void>; | ||
dispose?(): Awaitable<void>; | ||
@@ -208,2 +221,3 @@ } | ||
rootPath: string; | ||
globalAsyncIO?: boolean; | ||
} | ||
@@ -244,2 +258,6 @@ | ||
#private; | ||
readonly requestDepth: number; | ||
readonly pipelineDepth: number; | ||
readonly durableObject: boolean; | ||
constructor({ requestDepth, pipelineDepth, durableObject, }?: RequestContextOptions); | ||
runWith<T>(closure: () => T): T; | ||
@@ -250,2 +268,22 @@ get subrequests(): number; | ||
export declare interface RequestContextOptions { | ||
/** | ||
* In this context, a request is the initial entry fetch to a Worker | ||
* (e.g. the incoming HTTP request), or fetch to a Durable Object stub. | ||
* The depth starts at 1, and increments for each recursive request. | ||
*/ | ||
requestDepth?: number; | ||
/** | ||
* The pipeline depth starts at 1, and increments for each recursive service | ||
* binding fetch. The requestDepth should not be incremented in this case. | ||
* The pipeline depth resets for each new request (as described above). | ||
*/ | ||
pipelineDepth?: number; | ||
/** | ||
* Whether this context is for inside a Durable Object fetch. Affects | ||
* WebSocket subrequest limits for incoming messages. | ||
*/ | ||
durableObject?: boolean; | ||
} | ||
export declare function resolveStoragePersist(rootPath: string, persist?: boolean | string): boolean | string | undefined; | ||
@@ -268,3 +306,3 @@ | ||
export declare interface ScriptRunner { | ||
run(globalScope: Context, blueprint: ScriptBlueprint, modulesRules?: ProcessedModuleRule[], additionalModules?: AdditionalModules): Promise<ScriptRunnerResult>; | ||
run(globalScope: Context, blueprint: ScriptBlueprint, modulesRules?: ProcessedModuleRule[], additionalModules?: AdditionalModules, proxyPrimitiveInstanceOf?: boolean): Promise<ScriptRunnerResult>; | ||
} | ||
@@ -357,3 +395,3 @@ | ||
#private; | ||
protected [kWrapListener]<Type extends keyof EventMap>(listener: (event: EventMap[Type]) => void): TypedEventListener<EventMap[Type]>; | ||
protected [kWrapListener]<Type extends keyof EventMap>(listener: (event: EventMap[Type]) => void): (event: EventMap[Type]) => void; | ||
dispatchEvent(event: ValueOf<EventMap>): boolean; | ||
@@ -370,3 +408,3 @@ } | ||
#private; | ||
protected [kWrapListener]?<Type extends keyof EventMap>(listener: (event: EventMap[Type]) => void): TypedEventListener<EventMap[Type]>; | ||
protected [kWrapListener]?<Type extends keyof EventMap>(listener: (event: EventMap[Type]) => void): (event: EventMap[Type]) => void; | ||
addEventListener<Type extends keyof EventMap>(type: Type, listener: TypedEventListener<EventMap[Type]> | null, options?: AddEventListenerOptions | boolean): void; | ||
@@ -459,2 +497,7 @@ removeEventListener<Type extends keyof EventMap>(type: Type, listener: TypedEventListener<EventMap[Type]> | null, options?: EventListenerOptions | boolean): void; | ||
wasm_modules?: Record<string, string>; | ||
experimental_services?: { | ||
name: string; | ||
service: string; | ||
environment: string; | ||
}[]; | ||
miniflare?: { | ||
@@ -485,2 +528,6 @@ globals?: Record<string, any>; | ||
routes?: string[]; | ||
global_async_io?: boolean; | ||
global_timers?: boolean; | ||
global_random?: boolean; | ||
proxy_primitive_instanceof?: boolean; | ||
}; | ||
@@ -487,0 +534,0 @@ } |
@@ -45,2 +45,3 @@ var __create = Object.create; | ||
addAll: () => addAll, | ||
assertInRequest: () => assertInRequest, | ||
base64Decode: () => base64Decode, | ||
@@ -128,4 +129,9 @@ base64Encode: () => base64Encode, | ||
// packages/shared/src/context.ts | ||
var import_assert = __toModule(require("assert")); | ||
var import_async_hooks = __toModule(require("async_hooks")); | ||
var MAX_SUBREQUESTS = 50; | ||
var subrequestLimit = parseInt(process.env.MINIFLARE_SUBREQUEST_LIMIT); | ||
var MAX_SUBREQUESTS = isNaN(subrequestLimit) ? 50 : subrequestLimit; | ||
var MAX_REQUEST_DEPTH = 16; | ||
var MAX_PIPELINE_DEPTH = 32; | ||
var depthError = "Subrequest depth limit exceeded. This request recursed through Workers too many times. This can happen e.g. if you have a Worker or Durable Object that calls other Workers or objects recursively."; | ||
var requestContextStorage = new import_async_hooks.AsyncLocalStorage(); | ||
@@ -135,3 +141,31 @@ function getRequestContext() { | ||
} | ||
function assertInRequest() { | ||
if (!getRequestContext()) { | ||
throw new Error("Some functionality, such as asynchronous I/O (fetch, Cache API, KV), timeouts (setTimeout, setInterval), and generating random values (crypto.getRandomValues, crypto.subtle.generateKey), can only be performed while handling a request."); | ||
} | ||
} | ||
var RequestContext = class { | ||
requestDepth; | ||
pipelineDepth; | ||
durableObject; | ||
constructor({ | ||
requestDepth = 1, | ||
pipelineDepth = 1, | ||
durableObject = false | ||
} = {}) { | ||
(0, import_assert.default)(requestDepth >= 1); | ||
(0, import_assert.default)(pipelineDepth >= 1); | ||
if (requestDepth > MAX_REQUEST_DEPTH) { | ||
throw new Error(`${depthError} | ||
Workers and objects can recurse up to ${MAX_REQUEST_DEPTH} times. | ||
If you're trying to fetch from an origin server, make sure you've set the \`upstream\` option.`); | ||
} | ||
if (pipelineDepth > MAX_PIPELINE_DEPTH) { | ||
throw new Error(`${depthError} | ||
Service bindings can recurse up to ${MAX_PIPELINE_DEPTH} times.`); | ||
} | ||
this.requestDepth = requestDepth; | ||
this.pipelineDepth = pipelineDepth; | ||
this.durableObject = durableObject; | ||
} | ||
runWith(closure) { | ||
@@ -146,3 +180,3 @@ return requestContextStorage.run(this, closure); | ||
this.#subrequests += count; | ||
if (this.#subrequests > MAX_SUBREQUESTS) { | ||
if (MAX_SUBREQUESTS >= 0 && this.#subrequests > MAX_SUBREQUESTS) { | ||
throw new Error(`Too many subrequests. Workers can make up to ${MAX_SUBREQUESTS} subrequests per request. | ||
@@ -333,6 +367,11 @@ A subrequest is a call to fetch(), a redirect, or a call to any Cache API method.`); | ||
var Log = class { | ||
constructor(level = 3, suffix) { | ||
constructor(level = 3, opts = {}) { | ||
this.level = level; | ||
this.suffix = suffix; | ||
const prefix = opts.prefix ?? "mf"; | ||
const suffix = opts.suffix ?? ""; | ||
this.#prefix = prefix ? prefix + ":" : ""; | ||
this.#suffix = suffix ? ":" + suffix : ""; | ||
} | ||
#prefix; | ||
#suffix; | ||
log(message) { | ||
@@ -343,3 +382,4 @@ console.log(message); | ||
if (level <= this.level) { | ||
this.log(LEVEL_COLOUR[level](`[mf:${LEVEL_PREFIX[level]}${this.suffix ? `:${this.suffix}` : ""}] ${message}`)); | ||
const prefix = `[${this.#prefix}${LEVEL_PREFIX[level]}${this.#suffix}]`; | ||
this.log(LEVEL_COLOUR[level](`${prefix} ${message}`)); | ||
} | ||
@@ -356,2 +396,5 @@ } | ||
} | ||
if (message.cause) { | ||
this.error(prefixError("Cause", message.cause)); | ||
} | ||
} | ||
@@ -388,4 +431,5 @@ warn(message) { | ||
OptionType2[OptionType2["BOOLEAN_STRING"] = 5] = "BOOLEAN_STRING"; | ||
OptionType2[OptionType2["ARRAY"] = 6] = "ARRAY"; | ||
OptionType2[OptionType2["OBJECT"] = 7] = "OBJECT"; | ||
OptionType2[OptionType2["BOOLEAN_NUMBER"] = 6] = "BOOLEAN_NUMBER"; | ||
OptionType2[OptionType2["ARRAY"] = 7] = "ARRAY"; | ||
OptionType2[OptionType2["OBJECT"] = 8] = "OBJECT"; | ||
})(OptionType || (OptionType = {})); | ||
@@ -426,5 +470,5 @@ function Option(metadata) { | ||
continue; | ||
} else if (meta.type === 8) { | ||
str = Object.keys(value).join(", "); | ||
} else if (meta.type === 7) { | ||
str = Object.keys(value).join(", "); | ||
} else if (meta.type === 6) { | ||
str = value.join(", "); | ||
@@ -475,3 +519,3 @@ } else { | ||
// packages/shared/src/sync/gate.ts | ||
var import_assert = __toModule(require("assert")); | ||
var import_assert2 = __toModule(require("assert")); | ||
var import_async_hooks2 = __toModule(require("async_hooks")); | ||
@@ -537,3 +581,3 @@ var import_promises = __toModule(require("timers/promises")); | ||
#unlock = async () => { | ||
(0, import_assert.default)(this.#lockCount > 0); | ||
(0, import_assert2.default)(this.#lockCount > 0); | ||
this.#lockCount--; | ||
@@ -575,3 +619,3 @@ while (this.#lockCount === 0 && this.#resolveQueue.length) { | ||
// packages/shared/src/sync/mutex.ts | ||
var import_assert2 = __toModule(require("assert")); | ||
var import_assert3 = __toModule(require("assert")); | ||
var Mutex = class { | ||
@@ -588,3 +632,3 @@ locked = false; | ||
unlock() { | ||
(0, import_assert2.default)(this.locked); | ||
(0, import_assert3.default)(this.locked); | ||
if (this.resolveQueue.length > 0) { | ||
@@ -633,2 +677,3 @@ this.resolveQueue.shift()?.(); | ||
addAll, | ||
assertInRequest, | ||
base64Decode, | ||
@@ -635,0 +680,0 @@ base64Encode, |
{ | ||
"name": "@miniflare/shared", | ||
"version": "2.0.0-rc.4", | ||
"version": "2.0.0-rc.5", | ||
"description": "Shared utility module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -42,4 +42,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"@miniflare/shared-test": "2.0.0-rc.4" | ||
"@miniflare/shared-test": "2.0.0-rc.5" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52008
1126
3