fetchlet 0.1.0

Rust-hosted JavaScript functions and Fetch handlers with explicit host services
Documentation
// Generated by `vp run build:scripts` from the TypeScript sources in js-src/.
(function() {
	// js-src/stack-trace.ts
	const defineProperty = Object.defineProperty;
	/** The V8 call site methods both engines leave out or Boa needs. */
	const callSiteDefaults = {
		getThis() {},
		getTypeName() {
			return null;
		},
		getFunction() {},
		getMethodName() {
			return null;
		},
		getEvalOrigin() {},
		isToplevel() {
			return true;
		},
		isEval() {
			return false;
		},
		isNative() {
			return false;
		},
		isConstructor() {
			return false;
		},
		isAsync() {
			return false;
		},
		isPromiseAll() {
			return false;
		},
		getPromiseIndex() {
			return null;
		},
		getScriptNameOrSourceURL() {
			return this.getFileName();
		},
		toString() {
			let location = this.getFileName() ?? "<anonymous>";
			const line = this.getLineNumber();
			if (line !== null) location += `:${line}:${this.getColumnNumber()}`;
			const name = this.getFunctionName();
			return name === null ? location : `${name} (${location})`;
		}
	};
	/** Define the methods `prototype` lacks, as V8 does: not enumerable. */
	function completeCallSite(prototype) {
		for (const [name, method] of Object.entries(callSiteDefaults)) if (!Object.hasOwn(prototype, name)) defineProperty(prototype, name, {
			value: method,
			writable: true,
			configurable: true
		});
	}
	/** A frame Boa reported, as a V8 call site. */
	var FrameCallSite = class {
		#frame;
		constructor(frame) {
			this.#frame = frame;
		}
		getFunctionName() {
			return this.#frame[0];
		}
		getFileName() {
			return this.#frame[1] ?? void 0;
		}
		getLineNumber() {
			return this.#frame[2];
		}
		getColumnNumber() {
			return this.#frame[3];
		}
		isEval() {
			return this.#frame[4];
		}
	};
	completeCallSite(FrameCallSite.prototype);
	/** The first line of a stack V8 formats: `Error.prototype.toString`. */
	function header(target) {
		const { name, message } = target;
		const named = name === void 0 ? "Error" : String(name);
		const described = message === void 0 ? "" : String(message);
		if (named === "") return described;
		return described === "" ? named : `${named}: ${described}`;
	}
	/** `Error.stackTraceLimit` as a frame count: none when it is not a number. */
	function stackTraceLimit() {
		const limit = Error.stackTraceLimit;
		return typeof limit === "number" && limit > 0 ? Math.min(limit, 2 ** 32 - 1) : 0;
	}
	function installCaptureStackTrace(frames) {
		let preparing = false;
		defineProperty(Error, "captureStackTrace", {
			value: function captureStackTrace(target, constructorOpt) {
				if (typeof target !== "object" && typeof target !== "function" || target === null) throw new TypeError("Error.captureStackTrace requires an object");
				const sites = frames(constructorOpt, stackTraceLimit()).map((frame) => new FrameCallSite(frame));
				const prepare = Error.prepareStackTrace;
				let stack;
				if (typeof prepare === "function" && !preparing) {
					preparing = true;
					try {
						stack = prepare(target, sites);
					} finally {
						preparing = false;
					}
				} else stack = [header(target), ...sites.map((site) => `    at ${site.toString()}`)].join("\n");
				defineProperty(target, "stack", {
					value: stack,
					writable: true,
					configurable: true
				});
			},
			writable: true,
			configurable: true
		});
		if (!Object.hasOwn(Error, "stackTraceLimit")) defineProperty(Error, "stackTraceLimit", {
			value: 10,
			writable: true,
			enumerable: true,
			configurable: true
		});
	}
	/** The prototype of the engine's own call sites, read from one capture. */
	function nativeCallSitePrototype() {
		const hadPrepare = Object.hasOwn(Error, "prepareStackTrace");
		const previous = Error.prepareStackTrace;
		let site;
		Error.prepareStackTrace = (_error, sites) => {
			site = sites[0];
			return "";
		};
		try {
			const target = {};
			Error.captureStackTrace?.(target);
			target.stack;
		} finally {
			if (hadPrepare) Error.prepareStackTrace = previous;
			else delete Error.prepareStackTrace;
		}
		return site === void 0 ? void 0 : Object.getPrototypeOf(site);
	}
	const frames = globalThis.__fetchletStackFrames;
	delete globalThis.__fetchletStackFrames;
	if (typeof Error.captureStackTrace !== "function") {
		if (frames !== void 0) installCaptureStackTrace(frames);
	} else {
		const prototype = nativeCallSitePrototype();
		if (prototype !== void 0) completeCallSite(prototype);
	}
})();