apify-client
Version:
Apify API client for JavaScript
1,112 lines (1,088 loc) • 3.3 MB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object') {
module.exports = factory(require("node:zlib"), require("node:crypto"), require("node:util"));
}else if(typeof define === 'function' && define.amd) {
define(["node:zlib","node:crypto","node:util"], factory);
} else if(typeof exports === 'object'){
exports["Apify"] = factory(require("node:zlib"), require("node:crypto"), require("node:util"));
} else {
root["Apify"] = factory(root["node:zlib"], root["node:crypto"], root["node:util"]);
}
})(this, (__rspack_external_node_zlib, __rspack_external_crypto, __rspack_external_node_util) => {
return (() => {
var __webpack_modules__ = ({
"./node_modules/@tootallnate/quickjs-emscripten/dist/asyncify-helpers.js": (function (__unused_webpack_module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.awaitEachYieldedPromise = exports.maybeAsync = exports.maybeAsyncFn = void 0;
function* awaitYield(value) {
return (yield value);
}
function awaitYieldOf(generator) {
return awaitYield(awaitEachYieldedPromise(generator));
}
const AwaitYield = awaitYield;
AwaitYield.of = awaitYieldOf;
/**
* Create a function that may or may not be async, using a generator
*
* Within the generator, call `yield* awaited(maybePromise)` to await a value
* that may or may not be a promise.
*
* If the inner function never yields a promise, it will return synchronously.
*/
function maybeAsyncFn(that, fn) {
return (...args) => {
const generator = fn.call(that, AwaitYield, ...args);
return awaitEachYieldedPromise(generator);
};
}
exports.maybeAsyncFn = maybeAsyncFn;
class Example {
constructor() {
this.maybeAsyncMethod = maybeAsyncFn(this, function* (awaited, a) {
yield* awaited(new Promise((resolve) => setTimeout(resolve, a)));
return 5;
});
}
}
function maybeAsync(that, startGenerator) {
const generator = startGenerator.call(that, AwaitYield);
return awaitEachYieldedPromise(generator);
}
exports.maybeAsync = maybeAsync;
function awaitEachYieldedPromise(gen) {
function handleNextStep(step) {
if (step.done) {
return step.value;
}
if (step.value instanceof Promise) {
return step.value.then((value) => handleNextStep(gen.next(value)), (error) => handleNextStep(gen.throw(error)));
}
return handleNextStep(gen.next(step.value));
}
return handleNextStep(gen.next());
}
exports.awaitEachYieldedPromise = awaitEachYieldedPromise;
//# sourceMappingURL=asyncify-helpers.js.map
}),
"./node_modules/@tootallnate/quickjs-emscripten/dist/context-asyncify.js": (function (__unused_webpack_module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.QuickJSAsyncContext = void 0;
const context_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/context.js");
const debug_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/debug.js");
const types_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/types.js");
/**
* Asyncified version of [[QuickJSContext]].
*
* *Asyncify* allows normally synchronous code to wait for asynchronous Promises
* or callbacks. The asyncified version of QuickJSContext can wait for async
* host functions as though they were synchronous.
*/
class QuickJSAsyncContext extends context_1.QuickJSContext {
/**
* Asyncified version of [[evalCode]].
*/
async evalCodeAsync(code, filename = "eval.js",
/** See [[EvalFlags]] for number semantics */
options) {
const detectModule = (options === undefined ? 1 : 0);
const flags = (0, types_1.evalOptionsToFlags)(options);
let resultPtr = 0;
try {
resultPtr = await this.memory
.newHeapCharPointer(code)
.consume((charHandle) => this.ffi.QTS_Eval_MaybeAsync(this.ctx.value, charHandle.value, filename, detectModule, flags));
}
catch (error) {
(0, debug_1.debugLog)("QTS_Eval_MaybeAsync threw", error);
throw error;
}
const errorPtr = this.ffi.QTS_ResolveException(this.ctx.value, resultPtr);
if (errorPtr) {
this.ffi.QTS_FreeValuePointer(this.ctx.value, resultPtr);
return { error: this.memory.heapValueHandle(errorPtr) };
}
return { value: this.memory.heapValueHandle(resultPtr) };
}
/**
* Similar to [[newFunction]].
* Convert an async host Javascript function into a synchronous QuickJS function value.
*
* Whenever QuickJS calls this function, the VM's stack will be unwound while
* waiting the async function to complete, and then restored when the returned
* promise resolves.
*
* Asyncified functions must never call other asyncified functions or
* `import`, even indirectly, because the stack cannot be unwound twice.
*
* See [Emscripten's docs on Asyncify](https://emscripten.org/docs/porting/asyncify.html).
*/
newAsyncifiedFunction(name, fn) {
return this.newFunction(name, fn);
}
}
exports.QuickJSAsyncContext = QuickJSAsyncContext;
//# sourceMappingURL=context-asyncify.js.map
}),
"./node_modules/@tootallnate/quickjs-emscripten/dist/context.js": (function (__unused_webpack_module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.QuickJSContext = void 0;
const debug_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/debug.js");
const deferred_promise_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/deferred-promise.js");
const errors_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/errors.js");
const lifetime_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/lifetime.js");
const memory_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/memory.js");
const types_1 = __webpack_require__("./node_modules/@tootallnate/quickjs-emscripten/dist/types.js");
/**
* @private
*/
class ContextMemory extends memory_1.ModuleMemory {
/** @private */
constructor(args) {
super(args.module);
this.scope = new lifetime_1.Scope();
this.copyJSValue = (ptr) => {
return this.ffi.QTS_DupValuePointer(this.ctx.value, ptr);
};
this.freeJSValue = (ptr) => {
this.ffi.QTS_FreeValuePointer(this.ctx.value, ptr);
};
args.ownedLifetimes?.forEach((lifetime) => this.scope.manage(lifetime));
this.owner = args.owner;
this.module = args.module;
this.ffi = args.ffi;
this.rt = args.rt;
this.ctx = this.scope.manage(args.ctx);
}
get alive() {
return this.scope.alive;
}
dispose() {
return this.scope.dispose();
}
/**
* Track `lifetime` so that it is disposed when this scope is disposed.
*/
manage(lifetime) {
return this.scope.manage(lifetime);
}
consumeJSCharPointer(ptr) {
const str = this.module.UTF8ToString(ptr);
this.ffi.QTS_FreeCString(this.ctx.value, ptr);
return str;
}
heapValueHandle(ptr) {
return new lifetime_1.Lifetime(ptr, this.copyJSValue, this.freeJSValue, this.owner);
}
}
/**
* QuickJSContext wraps a QuickJS Javascript context (JSContext*) within a
* runtime. The contexts within the same runtime may exchange objects freely.
* You can think of separate runtimes like different domains in a browser, and
* the contexts within a runtime like the different windows open to the same
* domain. The {@link runtime} references the context's runtime.
*
* This class's methods return {@link QuickJSHandle}, which wrap C pointers (JSValue*).
* It's the caller's responsibility to call `.dispose()` on any
* handles you create to free memory once you're done with the handle.
*
* Use {@link QuickJSRuntime.newContext} or {@link QuickJSWASMModule.newContext}
* to create a new QuickJSContext.
*
* Create QuickJS values inside the interpreter with methods like
* [[newNumber]], [[newString]], [[newArray]], [[newObject]],
* [[newFunction]], and [[newPromise]].
*
* Call [[setProp]] or [[defineProp]] to customize objects. Use those methods
* with [[global]] to expose the values you create to the interior of the
* interpreter, so they can be used in [[evalCode]].
*
* Use [[evalCode]] or [[callFunction]] to execute Javascript inside the VM. If
* you're using asynchronous code inside the QuickJSContext, you may need to also
* call [[executePendingJobs]]. Executing code inside the runtime returns a
* result object representing successful execution or an error. You must dispose
* of any such results to avoid leaking memory inside the VM.
*
* Implement memory and CPU constraints at the runtime level, using [[runtime]].
* See {@link QuickJSRuntime} for more information.
*
*/
// TODO: Manage own callback registration
class QuickJSContext {
/**
* Use {@link QuickJS.createVm} to create a QuickJSContext instance.
*/
constructor(args) {
/** @private */
this._undefined = undefined;
/** @private */
this._null = undefined;
/** @private */
this._false = undefined;
/** @private */
this._true = undefined;
/** @private */
this._global = undefined;
/** @private */
this._BigInt = undefined;
/** @private */
this.fnNextId = -32768; // min value of signed 16bit int used by Quickjs
/** @private */
this.fnMaps = new Map();
/**
* @hidden
*/
this.cToHostCallbacks = {
callFunction: (ctx, this_ptr, argc, argv, fn_id) => {
if (ctx !== this.ctx.value) {
throw new Error("QuickJSContext instance received C -> JS call with mismatched ctx");
}
const fn = this.getFunction(fn_id);
if (!fn) {
// this "throw" is not catch-able from the TS side. could we somehow handle this higher up?
throw new Error(`QuickJSContext had no callback with id ${fn_id}`);
}
return lifetime_1.Scope.withScopeMaybeAsync(this, function* (awaited, scope) {
const thisHandle = scope.manage(new lifetime_1.WeakLifetime(this_ptr, this.memory.copyJSValue, this.memory.freeJSValue, this.runtime));
const argHandles = new Array(argc);
for (let i = 0; i < argc; i++) {
const ptr = this.ffi.QTS_ArgvGetJSValueConstPointer(argv, i);
argHandles[i] = scope.manage(new lifetime_1.WeakLifetime(ptr, this.memory.copyJSValue, this.memory.freeJSValue, this.runtime));
}
try {
const result = yield* awaited(fn.apply(thisHandle, argHandles));
if (result) {
if ("error" in result && result.error) {
(0, debug_1.debugLog)("throw error", result.error);
throw result.error;
}
const handle = scope.manage(result instanceof lifetime_1.Lifetime ? result : result.value);
return this.ffi.QTS_DupValuePointer(this.ctx.value, handle.value);
}
return 0;
}
catch (error) {
return this.errorToHandle(error).consume((errorHandle) => this.ffi.QTS_Throw(this.ctx.value, errorHandle.value));
}
});
},
};
this.runtime = args.runtime;
this.module = args.module;
this.ffi = args.ffi;
this.rt = args.rt;
this.ctx = args.ctx;
this.memory = new ContextMemory({
...args,
owner: this.runtime,
});
args.callbacks.setContextCallbacks(this.ctx.value, this.cToHostCallbacks);
this.dump = this.dump.bind(this);
this.getString = this.getString.bind(this);
this.getNumber = this.getNumber.bind(this);
this.resolvePromise = this.resolvePromise.bind(this);
}
// @implement Disposable ----------------------------------------------------
get alive() {
return this.memory.alive;
}
/**
* Dispose of this VM's underlying resources.
*
* @throws Calling this method without disposing of all created handles
* will result in an error.
*/
dispose() {
this.memory.dispose();
}
// Globals ------------------------------------------------------------------
/**
* [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined).
*/
get undefined() {
if (this._undefined) {
return this._undefined;
}
// Undefined is a constant, immutable value in QuickJS.
const ptr = this.ffi.QTS_GetUndefined();
return (this._undefined = new lifetime_1.StaticLifetime(ptr));
}
/**
* [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null).
*/
get null() {
if (this._null) {
return this._null;
}
// Null is a constant, immutable value in QuickJS.
const ptr = this.ffi.QTS_GetNull();
return (this._null = new lifetime_1.StaticLifetime(ptr));
}
/**
* [`true`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/true).
*/
get true() {
if (this._true) {
return this._true;
}
// True is a constant, immutable value in QuickJS.
const ptr = this.ffi.QTS_GetTrue();
return (this._true = new lifetime_1.StaticLifetime(ptr));
}
/**
* [`false`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/false).
*/
get false() {
if (this._false) {
return this._false;
}
// False is a constant, immutable value in QuickJS.
const ptr = this.ffi.QTS_GetFalse();
return (this._false = new lifetime_1.StaticLifetime(ptr));
}
/**
* [`global`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects).
* A handle to the global object inside the interpreter.
* You can set properties to create global variables.
*/
get global() {
if (this._global) {
return this._global;
}
// The global is a JSValue, but since it's lifetime is as long as the VM's,
// we should manage it.
const ptr = this.ffi.QTS_GetGlobalObject(this.ctx.value);
// Automatically clean up this reference when we dispose
this.memory.manage(this.memory.heapValueHandle(ptr));
// This isn't technically a static lifetime, but since it has the same
// lifetime as the VM, it's okay to fake one since when the VM is
// disposed, no other functions will accept the value.
this._global = new lifetime_1.StaticLifetime(ptr, this.runtime);
return this._global;
}
// New values ---------------------------------------------------------------
/**
* Converts a Javascript number into a QuickJS value.
*/
newNumber(num) {
return this.memory.heapValueHandle(this.ffi.QTS_NewFloat64(this.ctx.value, num));
}
/**
* Create a QuickJS [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) value.
*/
newString(str) {
const ptr = this.memory
.newHeapCharPointer(str)
.consume((charHandle) => this.ffi.QTS_NewString(this.ctx.value, charHandle.value));
return this.memory.heapValueHandle(ptr);
}
/**
* Create a QuickJS [symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) value.
* No two symbols created with this function will be the same value.
*/
newUniqueSymbol(description) {
const key = (typeof description === "symbol" ? description.description : description) ?? "";
const ptr = this.memory
.newHeapCharPointer(key)
.consume((charHandle) => this.ffi.QTS_NewSymbol(this.ctx.value, charHandle.value, 0));
return this.memory.heapValueHandle(ptr);
}
/**
* Get a symbol from the [global registry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#shared_symbols_in_the_global_symbol_registry) for the given key.
* All symbols created with the same key will be the same value.
*/
newSymbolFor(key) {
const description = (typeof key === "symbol" ? key.description : key) ?? "";
const ptr = this.memory
.newHeapCharPointer(description)
.consume((charHandle) => this.ffi.QTS_NewSymbol(this.ctx.value, charHandle.value, 1));
return this.memory.heapValueHandle(ptr);
}
/**
* Create a QuickJS [bigint](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) value.
*/
newBigInt(num) {
if (!this._BigInt) {
const bigIntHandle = this.getProp(this.global, "BigInt");
this.memory.manage(bigIntHandle);
this._BigInt = new lifetime_1.StaticLifetime(bigIntHandle.value, this.runtime);
}
const bigIntHandle = this._BigInt;
const asString = String(num);
return this.newString(asString).consume((handle) => this.unwrapResult(this.callFunction(bigIntHandle, this.undefined, handle)));
}
/**
* `{}`.
* Create a new QuickJS [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer).
*
* @param prototype - Like [`Object.create`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create).
*/
newObject(prototype) {
if (prototype) {
this.runtime.assertOwned(prototype);
}
const ptr = prototype
? this.ffi.QTS_NewObjectProto(this.ctx.value, prototype.value)
: this.ffi.QTS_NewObject(this.ctx.value);
return this.memory.heapValueHandle(ptr);
}
/**
* `[]`.
* Create a new QuickJS [array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
*/
newArray() {
const ptr = this.ffi.QTS_NewArray(this.ctx.value);
return this.memory.heapValueHandle(ptr);
}
newPromise(value) {
const deferredPromise = lifetime_1.Scope.withScope((scope) => {
const mutablePointerArray = scope.manage(this.memory.newMutablePointerArray(2));
const promisePtr = this.ffi.QTS_NewPromiseCapability(this.ctx.value, mutablePointerArray.value.ptr);
const promiseHandle = this.memory.heapValueHandle(promisePtr);
const [resolveHandle, rejectHandle] = Array.from(mutablePointerArray.value.typedArray).map((jsvaluePtr) => this.memory.heapValueHandle(jsvaluePtr));
return new deferred_promise_1.QuickJSDeferredPromise({
context: this,
promiseHandle,
resolveHandle,
rejectHandle,
});
});
if (value && typeof value === "function") {
value = new Promise(value);
}
if (value) {
Promise.resolve(value).then(deferredPromise.resolve, (error) => error instanceof lifetime_1.Lifetime
? deferredPromise.reject(error)
: this.newError(error).consume(deferredPromise.reject));
}
return deferredPromise;
}
/**
* Convert a Javascript function into a QuickJS function value.
* See [[VmFunctionImplementation]] for more details.
*
* A [[VmFunctionImplementation]] should not free its arguments or its return
* value. A VmFunctionImplementation should also not retain any references to
* its return value.
*
* To implement an async function, create a promise with [[newPromise]], then
* return the deferred promise handle from `deferred.handle` from your
* function implementation:
*
* ```
* const deferred = vm.newPromise()
* someNativeAsyncFunction().then(deferred.resolve)
* return deferred.handle
* ```
*/
newFunction(name, fn) {
const fnId = ++this.fnNextId;
this.setFunction(fnId, fn);
return this.memory.heapValueHandle(this.ffi.QTS_NewFunction(this.ctx.value, fnId, name));
}
newError(error) {
const errorHandle = this.memory.heapValueHandle(this.ffi.QTS_NewError(this.ctx.value));
if (error && typeof error === "object") {
if (error.name !== undefined) {
this.newString(error.name).consume((handle) => this.setProp(errorHandle, "name", handle));
}
if (error.message !== undefined) {
this.newString(error.message).consume((handle) => this.setProp(errorHandle, "message", handle));
}
}
else if (typeof error === "string") {
this.newString(error).consume((handle) => this.setProp(errorHandle, "message", handle));
}
else if (error !== undefined) {
// This isn't supported in the type signature but maybe it will make life easier.
this.newString(String(error)).consume((handle) => this.setProp(errorHandle, "message", handle));
}
return errorHandle;
}
// Read values --------------------------------------------------------------
/**
* `typeof` operator. **Not** [standards compliant](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof).
*
* @remarks
* Does not support BigInt values correctly.
*/
typeof(handle) {
this.runtime.assertOwned(handle);
return this.memory.consumeHeapCharPointer(this.ffi.QTS_Typeof(this.ctx.value, handle.value));
}
/**
* Converts `handle` into a Javascript number.
* @returns `NaN` on error, otherwise a `number`.
*/
getNumber(handle) {
this.runtime.assertOwned(handle);
return this.ffi.QTS_GetFloat64(this.ctx.value, handle.value);
}
/**
* Converts `handle` to a Javascript string.
*/
getString(handle) {
this.runtime.assertOwned(handle);
return this.memory.consumeJSCharPointer(this.ffi.QTS_GetString(this.ctx.value, handle.value));
}
/**
* Converts `handle` into a Javascript symbol. If the symbol is in the global
* registry in the guest, it will be created with Symbol.for on the host.
*/
getSymbol(handle) {
this.runtime.assertOwned(handle);
const key = this.memory.consumeJSCharPointer(this.ffi.QTS_GetSymbolDescriptionOrKey(this.ctx.value, handle.value));
const isGlobal = this.ffi.QTS_IsGlobalSymbol(this.ctx.value, handle.value);
return isGlobal ? Symbol.for(key) : Symbol(key);
}
/**
* Converts `handle` to a Javascript bigint.
*/
getBigInt(handle) {
this.runtime.assertOwned(handle);
const asString = this.getString(handle);
return BigInt(asString);
}
/**
* `Promise.resolve(value)`.
* Convert a handle containing a Promise-like value inside the VM into an
* actual promise on the host.
*
* @remarks
* You may need to call [[executePendingJobs]] to ensure that the promise is resolved.
*
* @param promiseLikeHandle - A handle to a Promise-like value with a `.then(onSuccess, onError)` method.
*/
resolvePromise(promiseLikeHandle) {
this.runtime.assertOwned(promiseLikeHandle);
const vmResolveResult = lifetime_1.Scope.withScope((scope) => {
const vmPromise = scope.manage(this.getProp(this.global, "Promise"));
const vmPromiseResolve = scope.manage(this.getProp(vmPromise, "resolve"));
return this.callFunction(vmPromiseResolve, vmPromise, promiseLikeHandle);
});
if (vmResolveResult.error) {
return Promise.resolve(vmResolveResult);
}
return new Promise((resolve) => {
lifetime_1.Scope.withScope((scope) => {
const resolveHandle = scope.manage(this.newFunction("resolve", (value) => {
resolve({ value: value && value.dup() });
}));
const rejectHandle = scope.manage(this.newFunction("reject", (error) => {
resolve({ error: error && error.dup() });
}));
const promiseHandle = scope.manage(vmResolveResult.value);
const promiseThenHandle = scope.manage(this.getProp(promiseHandle, "then"));
this.unwrapResult(this.callFunction(promiseThenHandle, promiseHandle, resolveHandle, rejectHandle)).dispose();
});
});
}
// Properties ---------------------------------------------------------------
/**
* `handle[key]`.
* Get a property from a JSValue.
*
* @param key - The property may be specified as a JSValue handle, or as a
* Javascript string (which will be converted automatically).
*/
getProp(handle, key) {
this.runtime.assertOwned(handle);
const ptr = this.borrowPropertyKey(key).consume((quickJSKey) => this.ffi.QTS_GetProp(this.ctx.value, handle.value, quickJSKey.value));
const result = this.memory.heapValueHandle(ptr);
return result;
}
/**
* `handle[key] = value`.
* Set a property on a JSValue.
*
* @remarks
* Note that the QuickJS authors recommend using [[defineProp]] to define new
* properties.
*
* @param key - The property may be specified as a JSValue handle, or as a
* Javascript string or number (which will be converted automatically to a JSValue).
*/
setProp(handle, key, value) {
this.runtime.assertOwned(handle);
// free newly allocated value if key was a string or number. No-op if string was already
// a QuickJS handle.
this.borrowPropertyKey(key).consume((quickJSKey) => this.ffi.QTS_SetProp(this.ctx.value, handle.value, quickJSKey.value, value.value));
}
/**
* [`Object.defineProperty(handle, key, descriptor)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).
*
* @param key - The property may be specified as a JSValue handle, or as a
* Javascript string or number (which will be converted automatically to a JSValue).
*/
defineProp(handle, key, descriptor) {
this.runtime.assertOwned(handle);
lifetime_1.Scope.withScope((scope) => {
const quickJSKey = scope.manage(this.borrowPropertyKey(key));
const value = descriptor.value || this.undefined;
const configurable = Boolean(descriptor.configurable);
const enumerable = Boolean(descriptor.enumerable);
const hasValue = Boolean(descriptor.value);
const get = descriptor.get
? scope.manage(this.newFunction(descriptor.get.name, descriptor.get))
: this.undefined;
const set = descriptor.set
? scope.manage(this.newFunction(descriptor.set.name, descriptor.set))
: this.undefined;
this.ffi.QTS_DefineProp(this.ctx.value, handle.value, quickJSKey.value, value.value, get.value, set.value, configurable, enumerable, hasValue);
});
}
// Evaluation ---------------------------------------------------------------
/**
* [`func.call(thisVal, ...args)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call).
* Call a JSValue as a function.
*
* See [[unwrapResult]], which will throw if the function returned an error, or
* return the result handle directly. If evaluation returned a handle containing
* a promise, use [[resolvePromise]] to convert it to a native promise and
* [[executePendingJobs]] to finish evaluating the promise.
*
* @returns A result. If the function threw synchronously, `result.error` be a
* handle to the exception. Otherwise `result.value` will be a handle to the
* value.
*/
callFunction(func, thisVal, ...args) {
this.runtime.assertOwned(func);
const resultPtr = this.memory
.toPointerArray(args)
.consume((argsArrayPtr) => this.ffi.QTS_Call(this.ctx.value, func.value, thisVal.value, args.length, argsArrayPtr.value));
const errorPtr = this.ffi.QTS_ResolveException(this.ctx.value, resultPtr);
if (errorPtr) {
this.ffi.QTS_FreeValuePointer(this.ctx.value, resultPtr);
return { error: this.memory.heapValueHandle(errorPtr) };
}
return { value: this.memory.heapValueHandle(resultPtr) };
}
/**
* Like [`eval(code)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Description).
* Evaluates the Javascript source `code` in the global scope of this VM.
* When working with async code, you many need to call [[executePendingJobs]]
* to execute callbacks pending after synchronous evaluation returns.
*
* See [[unwrapResult]], which will throw if the function returned an error, or
* return the result handle directly. If evaluation returned a handle containing
* a promise, use [[resolvePromise]] to convert it to a native promise and
* [[executePendingJobs]] to finish evaluating the promise.
*
* *Note*: to protect against infinite loops, provide an interrupt handler to
* [[setInterruptHandler]]. You can use [[shouldInterruptAfterDeadline]] to
* create a time-based deadline.
*
* @returns The last statement's value. If the code threw synchronously,
* `result.error` will be a handle to the exception. If execution was
* interrupted, the error will have name `InternalError` and message
* `interrupted`.
*/
evalCode(code, filename = "eval.js",
/**
* If no options are passed, a heuristic will be used to detect if `code` is
* an ES module.
*
* See [[EvalFlags]] for number semantics.
*/
options) {
const detectModule = (options === undefined ? 1 : 0);
const flags = (0, types_1.evalOptionsToFlags)(options);
const resultPtr = this.memory
.newHeapCharPointer(code)
.consume((charHandle) => this.ffi.QTS_Eval(this.ctx.value, charHandle.value, filename, detectModule, flags));
const errorPtr = this.ffi.QTS_ResolveException(this.ctx.value, resultPtr);
if (errorPtr) {
this.ffi.QTS_FreeValuePointer(this.ctx.value, resultPtr);
return { error: this.memory.heapValueHandle(errorPtr) };
}
return { value: this.memory.heapValueHandle(resultPtr) };
}
/**
* Throw an error in the VM, interrupted whatever current execution is in progress when execution resumes.
* @experimental
*/
throw(error) {
return this.errorToHandle(error).consume((handle) => this.ffi.QTS_Throw(this.ctx.value, handle.value));
}
/**
* @private
*/
borrowPropertyKey(key) {
if (typeof key === "number") {
return this.newNumber(key);
}
if (typeof key === "string") {
return this.newString(key);
}
// key is already a JSValue, but we're borrowing it. Return a static handle
// for internal use only.
return new lifetime_1.StaticLifetime(key.value, this.runtime);
}
/**
* @private
*/
getMemory(rt) {
if (rt === this.rt.value) {
return this.memory;
}
else {
throw new Error("Private API. Cannot get memory from a different runtime");
}
}
// Utilities ----------------------------------------------------------------
/**
* Dump a JSValue to Javascript in a best-effort fashion.
* Returns `handle.toString()` if it cannot be serialized to JSON.
*/
dump(handle) {
this.runtime.assertOwned(handle);
const type = this.typeof(handle);
if (type === "string") {
return this.getString(handle);
}
else if (type === "number") {
return this.getNumber(handle);
}
else if (type === "bigint") {
return this.getBigInt(handle);
}
else if (type === "undefined") {
return undefined;
}
else if (type === "symbol") {
return this.getSymbol(handle);
}
const str = this.memory.consumeJSCharPointer(this.ffi.QTS_Dump(this.ctx.value, handle.value));
try {
return JSON.parse(str);
}
catch (err) {
return str;
}
}
/**
* Unwrap a SuccessOrFail result such as a [[VmCallResult]] or a
* [[ExecutePendingJobsResult]], where the fail branch contains a handle to a QuickJS error value.
* If the result is a success, returns the value.
* If the result is an error, converts the error to a native object and throws the error.
*/
unwrapResult(result) {
if (result.error) {
const context = "context" in result.error ? result.error.context : this;
const cause = result.error.consume((error) => this.dump(error));
if (cause && typeof cause === "object" && typeof cause.message === "string") {
const { message, name, stack } = cause;
const exception = new errors_1.QuickJSUnwrapError("");
const hostStack = exception.stack;
if (typeof name === "string") {
exception.name = cause.name;
}
if (typeof stack === "string") {
exception.stack = `${name}: ${message}\n${cause.stack}Host: ${hostStack}`;
}
Object.assign(exception, { cause, context, message });
throw exception;
}
throw new errors_1.QuickJSUnwrapError(cause, context);
}
return result.value;
}
/** @private */
getFunction(fn_id) {
const map_id = fn_id >> 8;
const fnMap = this.fnMaps.get(map_id);
if (!fnMap) {
return undefined;
}
return fnMap.get(fn_id);
}
/** @private */
setFunction(fn_id, handle) {
const map_id = fn_id >> 8;
let fnMap = this.fnMaps.get(map_id);
if (!fnMap) {
fnMap = new Map();
this.fnMaps.set(map_id, fnMap);
}
return fnMap.set(fn_id, handle);
}
errorToHandle(error) {
if (error instanceof lifetime_1.Lifetime) {
return error;
}
return this.newError(error);
}
}
exports.QuickJSContext = QuickJSContext;
//# sourceMappingURL=context.js.map
}),
"./node_modules/@tootallnate/quickjs-emscripten/dist/debug.js": (function (__unused_webpack_module, exports, __webpack_require__) {
"use strict";
/* provided dependency */ var process = __webpack_require__("./node_modules/process/browser.js");
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.debugLog = exports.QTS_DEBUG = void 0;
exports.QTS_DEBUG = false || Boolean(typeof process === "object" && process.env.QTS_DEBUG);
exports.debugLog = exports.QTS_DEBUG ? console.log.bind(console) : () => { };
//# sourceMappingURL=debug.js.map
}),
"./node_modules/@tootallnate/quickjs-emscripten/dist/deferred-promise.js": (function (__unused_webpack_module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.QuickJSDeferredPromise = void 0;
/**
* QuickJSDeferredPromise wraps a QuickJS promise [[handle]] and allows
* [[resolve]]ing or [[reject]]ing that promise. Use it to bridge asynchronous
* code on the host to APIs inside a QuickJSContext.
*
* Managing the lifetime of promises is tricky. There are three
* [[QuickJSHandle]]s inside of each deferred promise object: (1) the promise
* itself, (2) the `resolve` callback, and (3) the `reject` callback.
*
* - If the promise will be fulfilled before the end of it's [[owner]]'s lifetime,
* the only cleanup necessary is `deferred.handle.dispose()`, because
* calling [[resolve]] or [[reject]] will dispose of both callbacks automatically.
*
* - As the return value of a [[VmFunctionImplementation]], return [[handle]],
* and ensure that either [[resolve]] or [[reject]] will be called. No other
* clean-up is necessary.
*
* - In other cases, call [[dispose]], which will dispose [[handle]] as well as the
* QuickJS handles that back [[resolve]] and [[reject]]. For this object,
* [[dispose]] is idempotent.
*/
class QuickJSDeferredPromise {
/**
* Use [[QuickJSContext.newPromise]] to create a new promise instead of calling
* this constructor directly.
* @unstable
*/
constructor(args) {
/**
* Resolve [[handle]] with the given value, if any.
* Calling this method after calling [[dispose]] is a no-op.
*
* Note that after resolving a promise, you may need to call
* [[QuickJSContext.executePendingJobs]] to propagate the result to the promise's
* callbacks.
*/
this.resolve = (value) => {
if (!this.resolveHandle.alive) {
return;
}
this.context
.unwrapResult(this.context.callFunction(this.resolveHandle, this.context.undefined, value || this.context.undefined))
.dispose();
this.disposeResolvers();
this.onSettled();
};
/**
* Reject [[handle]] with the given value, if any.
* Calling this method after calling [[dispose]] is a no-op.
*
* Note that after rejecting a promise, you may need to call
* [[QuickJSContext.executePendingJobs]] to propagate the result to the promise's
* callbacks.
*/
this.reject = (value) => {
if (!this.rejectHandle.alive) {
return;
}
this.context
.unwrapResult(this.context.callFunction(this.rejectHandle, this.context.undefined, value || this.context.undefined))
.dispose();
this.disposeResolvers();
this.onSettled();
};
this.dispose = () => {
if (this.handle.alive) {
this.handle.dispose();
}
this.disposeResolvers();
};
this.context = args.context;
this.owner = args.context.runtime;
this.handle = args.promiseHandle;
this.settled = new Promise((resolve) => {
this.onSettled = resolve;
});
this.resolveHandle = args.resolveHandle;
this.rejectHandle = args.rejectHandle;
}
get alive() {
return this.handle.alive || this.resolveHandle.alive || this.rejectHandle.alive;
}
disposeResolvers() {
if (this.resolveHandle.alive) {
this.resolveHandle.dispose();
}
if (this.rejectHandle.alive) {
this.rejectHandle.dispose();
}
}
}
exports.QuickJSDeferredPromise = QuickJSDeferredPromise;
//# sourceMappingURL=deferred-promise.js.map
}),
"./node_modules/@tootallnate/quickjs-emscripten/dist/errors.js": (function (__unused_webpack_module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.QuickJSMemoryLeakDetected = exports.QuickJSAsyncifySuspended = exports.QuickJSAsyncifyError = exports.QuickJSNotImplemented = exports.QuickJSUseAfterFree = exports.QuickJSWrongOwner = exports.QuickJSUnwrapError = void 0;
/**
* Error thrown if [[QuickJSContext.unwrapResult]] unwraps an error value that isn't an object.
*/
class QuickJSUnwrapError extends Error {
constructor(cause, context) {
super(String(cause));
this.cause = cause;
this.context = context;
this.name = "QuickJSUnwrapError";
}
}
exports.QuickJSUnwrapError = QuickJSUnwrapError;
class QuickJSWrongOwner extends Error {
constructor() {
super(...arguments);
this.name = "QuickJSWrongOwner";
}
}
exports.QuickJSWrongOwner = QuickJSWrongOwner;
class QuickJSUseAfterFree extends Error {
constructor() {
super(...arguments);
this.name = "QuickJSUseAfterFree";
}
}
exports.QuickJSUseAfterFree = QuickJSUseAfterFree;
class QuickJSNotImplemented extends Error {
constructor() {
super(...arguments);
this.name = "QuickJSNotImplemented";
}
}
exports.QuickJSNotImplemented = QuickJSNotImplemented;
class QuickJSAsyncifyError extends Error {
constructor() {
super(...arguments);
this.name = "QuickJSAsyncifyError";
}
}
exports.QuickJSAsyncifyError = QuickJSAsyncifyError;
class QuickJSAsyncifySuspended extends Error {
constructor() {
super(...arguments);
this.name = "QuickJSAsyncifySuspended";
}
}
exports.QuickJSAsyncifySuspended = QuickJSAsyncifySuspended;
class QuickJSMemoryLeakDetected extends Error {
constructor() {
super(...arguments);
this.name = "QuickJSMemoryLeakDetected";
}
}
exports.QuickJSMemoryLeakDetected = QuickJSMemoryLeakDetected;
//# sourceMappingURL=errors.js.map
}),
"./node_modules/@tootallnate/quickjs-emscripten/dist/esmHelpers.js": (function (__unused_webpack_module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.unwrapJavascript = exports.unwrapTypescript = void 0;
/** Typescript thinks import('...js/.d.ts') needs mod.default.default */
function fakeUnwrapDefault(mod) {
// console.log("fakeUnwrapDefault", mod)
return mod.default;
}
/** Typescript thinks import('...ts') doesn't need mod.default.default, but does */
function actualUnwrapDefault(mod) {
// console.log("actualUnwrapDefault", mod)
const maybeUnwrap = mod.default;
return maybeUnwrap ?? mod;
}
// I'm not sure if this behavior is needed in all runtimes,
// or just for mocha + ts-node.
exports.unwrapTypescript = actualUnwrapDefault;
exports.unwrapJavascript = fakeUnwrapDefault;
//# sourceMappingURL=esmHelpers.js.map
}),
"./node_modules/@tootallnate/quickjs-emscripten/dist/generated/emscripten-module.WASM_RELEASE_SYNC.js": (function (module, __unused_webpack_exports, __webpack_require__) {
"use strict";
/* provided dependency */ var process = __webpack_require__("./node_modules/process/browser.js");
/* provided dependency */ var Buffer = __webpack_require__("./node_modules/buffer/index.js")["Buffer"];
var QuickJSRaw = (() => {
var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined;
if (true)
_scriptDir = _scriptDir || "/index.js";
return (function (QuickJSRaw = {}) {
var a;
a || (a = typeof QuickJSRaw !== 'undefined' ? QuickJSRaw : {});
var m, n;
a.ready = new Promise(function (b, c) { m = b; n = c; });
var p = Object.assign({}, a), t = "./this.program", u = "object" == typeof window, v = "function" == typeof importScripts, w = "object" == typeof process && "object" == typeof process.versions && "string" == typeof process.versions.node, x = "", y, z, A;
if (w) {
var fs = __webpack_require__("?a2fd"), B = __webpack_require__("./node_modules/path-browserify/index.js");
x = v ? B.dirname(x) + "/" : "/" + "/";
y = (b, c) => { var d = C(b); if (d)
return c ? d : d.toString(); b = b.startsWith("file://") ? new URL(b) : B.normalize(b); return fs.readFileSync(b, c ? void 0 : "utf8"); };
A = b => { b = y(b, !0); b.buffer || (b = new Uint8Array(b)); return b; };
z = (b, c, d) => { var e = C(b); e && c(e); b = b.startsWith("file://") ? new URL(b) : B.normalize(b); fs.readFile(b, function (f, g) { f ? d(f) : c(g.buffer); }); };
!a.thisProgram && 1 < process.argv.length && (t = process.argv[1].replace(/\\/g, "/"));
process.argv.slice(2);
a.inspect = function () { return "[Emscripten Module object]"; };
}
else if (u || v)
v ? x = self.location.href : "undefined" != typeof document && document.currentScript && (x = document.currentScript.src), _scriptDir && (x = _scriptDir), 0 !== x.indexOf("blob:") ? x = x.substr(0, x.replace(/[?#].*/, "").lastIndexOf("/") + 1) : x = "", y = b => {
try {
var c = new XMLHttpRequest;
c.open("GET", b, !1);
c.send(null);
return c.responseText;
}
catch (f) {
if (b = C(b)) {
c = [];
for (var d = 0; d < b.length; d++) {
var e = b[d];
255 < e && (e &= 255);
c.push(String.fromCharCode(e));
}
return c.join("");
}
throw f;
}
}, v && (A = b => { try {
var c = new XMLHttpRequest;
c.open("GET", b, !1);
c.responseType = "arraybuffer";
c.send(null);
return new Uint8Array(c.response);
}
catch (d) {
if (b = C(b))
return b;
throw d;
} }), z = (b, c, d) => { var e = new XMLHttpRequest; e.open("GET", b, !0); e.responseType = "arraybuffer"; e.onload = () => { if (200 == e.status || 0 == e.status && e.response)
c(e.response);
else {
var f = C(b);
f ? c(f.buffer) : d();
} }; e.onerror = d; e.send(null); };
var aa = a.print || console.log.bind(console), D = a.printErr || console.warn.bind(console);
Object.assign(a, p);
p = null;
a.thisProgram && (t = a.thisProgram);
var E;
a.wasmBinary && (E = a.wasmBinary);
var noExitRuntime = a.noExitRuntime || !0;
"object" != typeof WebAssembly && F("no native wasm support detected");
var G, H = !1, I, J, K, L;
function M() { var b = G.buffer; a.HEAP8 = I = new Int8Array(b); a.HEAP16 = new Int16Array(b); a.HEAP32 = K = new Int32Array(b); a.HEAPU8 = J = new Uint8Array(b); a.HEAPU16 = new Uint16Array(b); a.HEAPU32 = L = new Uint32Array(b); a.HEAPF32 = new Float32Array(b); a.HEAPF64 = new Float64Array(b); }
var ba = [], ca = [], da = [];
function ea() { var b = a.preRun.shift(); ba.unshift(b); }
var N = 0, O = null, P = null;
function F(b) { if (a.onAbort)
a.onAbort(b); b = "Aborted(" + b + ")"; D(b); H = !0; b = new WebAssembly.RuntimeError(b + ". Build with -sASSERTIONS for more info."); n(b); throw b; }
var Q = "data:application/octet-stream;base64,", R;
R = "data:application/octet-stream;base64,AGFzbQEAAAAB9QZxYAJ/fwBgA39/fwF/YAR/fn9/AX5gAn9/AX9gAX8Bf2AFf35/f38BfmADf39/AGAEf39/fwF/YAJ/fgF+YAF/AGAFf39/f38Bf2ABfAF8YAJ/fgBgAn9/AX5gAn9+AX9gA39/fgF/YAN/fn8BfmADf35/AGAGf35/f39/AX5gBn9/f39/fwF/YAR/f39/AGADf35/AX9gBn9+fn9/fwF+YAR/f35/AX9gA39+fgF+YAN/f38BfmAFf39/fn4Bf2AEf39/fgF/YAR/f35+AX9gBX9+fn5+AGABfwF+YAN/fn4Bf2AEf39/fwF+YAd/f39/f39/AX9gBX9/f39/AX5gAnx8AXxgAAF/YAV/f39/fwBgBX9+f35/AX9gBX9+fn9/AX5gAX4Bf2AEf35+fwBgB39+f35+fn8Bf2AIf39/f39/f38Bf2AFf35+fn8Bf2AGf35/fn5/AX9gBH9+f34BfmAEf35/fwBgBH9+f34AYAZ/f39/f38BfmAEf35+fwF/YAl/f39/f39/f38Bf2AEf35+fwF+YAR/fn9/AX9gA39+fgBgA35/fwF/YAV/fn5/fwBgA39/fgF+YAd/fn9/f39/AX5gAABgA39/fgBgBH9+f34Bf2AFf39+f38Bf2AEf35+fgF/YAd/f39/f39/AGACfH8BfGABfAF/YAN8fH8BfGACf38BfGAEf39+fwBgBH9+fn4BfmABfgF+YAJ/fAF/YAZ/fH9/f38Bf2AAAXxgBX9+f35/AX5gBn9/fn5+fgF/YAJ+fwBgAn98AGAEf39+fwF+YAV/f39/fgF+YAd/fn5+f39/AX5gBH5+fn4Bf2AHf39/f39/fgF+YAp/f39/f39/f39/AX9gB39/fn5/f38Bf2AFf3x/f38BfmACfn8Bf2AGfH9/f39/AGAFf35/f38AYAV/f35/fwBgBn9+fn5+fwF/YAV/f35+fwF/YAZ/fn9/f38Bf2ADf3x/AX9gBX9+f39/AX9gBX9/fn5+AX5gBX9+fn5+AX9gBn9/fn5/fwF/YAd/f39+fn5/AX9gBH9/f34BfmACfH8Bf2AGf39/f39/AGAIf39/f39/f38AYAN/fnwBfmAAAX5gAnx8AX9gAn5+AXxgAX8BfGADfn5+AX9gA39/fABgCH9+fn5+f35+AX5gCX9/f39/f39/fwACWw8BYQFhABQBYQFiADsBYQFjAAcBYQFkAAQBYQFlAAMBYQFmAAMBYQFnAAcBYQFoAAEBYQFpAAoBYQFqAAQBYQFrAAYBYQFsAAABYQFtAEoBYQFuAAQBYQFvAAoDygnICQwAAAQASwYGAAMmAAkBAAABPCcvDAkIDgEIAwABAw0dJw4OBAYeCR4IDgAGAw8BHgQwAw8KAz0GCAAQAxUHGAcBBgcfKAAEBD4BCAYGDQYGAw4BDSUAEB0pAQE/CQgqDwEdFQYYTD4NDwoABwQJAwEOBBcxAyAyPw4DAAwDAAgKBgEEDhUGCgQeDw4QCQZNATMHAAQPBj0PAgcGA04BFTQmEAQQDhUrAwQBAw8PMixPUAlAEwoKBAMBGAMOCgcIATEmAywDATUPLFEAQTYGAzADQAMJGAoPARAICQEAAFIEJgFTBAkDVAkKIQMfAQ4OBQAGBAMDAFUACAEBNzIIDilWEAAGGQRXOAsHAQAPAAEBBgQBAwQKBgQBCQYCGAUFADVCBAMBDQkJASIIDg8IQiU5AQMXARgUBgAKWFkHCw0UQyMECwZaAAcTAQMEEwMIIAFEBgQHAQAEBwcBAwEEAQMEDhADE1sPGQ4OGEUACgAAEA4BAQkZAQAEAxkHXAMNIyMnBwMDAF0vASQBFAYnBQMNXgMAKAkEAwsDAQoEBwMCBAELAQoIAA5fKAQBAwMDDwEJBwkBCgAHBwMzAwcHBwQDDgMeCBxgAigEAwJhNAAVPAAHDwcKIQEUExEACwBiGQYGAwMUCgMABCkBGAgDFwMGGWMdCA43LTYJDxYHAggQAAADFANGFwxkGAoJBmULExRmKwoJExMhKzdnBwcDBCsDBgEGBwQBBAABAAE7AgIIBAQBAQoOAQUmBWgNR0cBAQVpAgQJDAEAAwQDAQEAAwMJAwETAwEAAAMTMwoTFA0JASECAwEBBwgFBS4BDwZqCA8QEAh