{"version":3,"sources":["js-function.ts"],"sourcesContent":["/**\n * Represents a JS function with source code that can be transferred between endpoints\n */\n\nimport { Pointer } from \"../runtime/pointers.ts\";\nimport { Runtime } from \"../runtime/runtime.ts\";\nimport type { Endpoint } from \"./addressing.ts\";\nimport { ExtensibleFunction, getDeclaredExternalVariables, getDeclaredExternalVariablesAsync, getSourceWithoutUsingDeclaration, Callable, createFunctionWithDependencyInjectionsResolveLazyPointers, hasUnresolvedLazyDependencies } from \"./function-utils.ts\";\n\n\nexport type JSTransferableFunctionOptions = {\n\terrorOnOriginContext?: Error,\n\tisLocal?: boolean,\n\t/**\n\t * Endpoint that originally created the function source.\n\t * If set, permission checks also require this endpoint to be allowed to execute JS\n\t * (needed when a remote function is recreated locally, e.g. to rebind 'this')\n\t */\n\tsourceOrigin?: Endpoint,\n\t/**\n\t * Set to false if the source was received from an unverified (unsigned) sender\n\t */\n\tsourceVerified?: boolean\n}\n\nexport class JSTransferableFunction extends ExtensibleFunction {\n\t\n\t#fn: (...args:unknown[])=>unknown\n\n\t// promise that resolves when all lazy dependencies are resolved\n\tpublic lazyResolved: Promise<void>\n\n\t// endpoint that created the JS source (if known)\n\tpublic readonly sourceOrigin?: Endpoint\n\t// false if the source was received in a message with an unverified (unsigned) sender\n\t#sourceVerified = true\n\n\tpublic get sourceVerified() {\n\t\treturn this.#sourceVerified;\n\t}\n\n\t/**\n\t * Marks the source of this function as unverified (received from an unsigned remote sender).\n\t * Unverified functions can never be executed unless they are local functions.\n\t */\n\tpublic markSourceUnverified() {\n\t\tthis.#sourceVerified = false;\n\t}\n\n\t// deno-lint-ignore constructor-super\n\tprivate constructor(intermediateFn: (...args:unknown[])=>unknown, lazyResolved: Promise<void>, public deps: Record<string,unknown>, public source: string, public flags?: string[], options?: JSTransferableFunctionOptions) {\n\t\tif (options?.errorOnOriginContext) {\n\t\t\tconst invalidIntermediateFunction = () => {throw options.errorOnOriginContext};\n\t\t\tsuper(invalidIntermediateFunction);\n\t\t\tthis.#fn = invalidIntermediateFunction;\n\t\t}\n\t\telse {\n\t\t\tlet ptr: Pointer|undefined;\n\t\t\tconst fn = (...args:any[]) => {\n\t\t\t\tif (!options?.isLocal) {\n\t\t\t\t\tif (!ptr) ptr = Pointer.getByValue(this);\n\t\t\t\t\tif (!ptr) throw new Error(\"Cannot execute js:Function, must be bound to a pointer\");\n\t\t\t\t\t// SECURITY: source must come from a verified (signed or local) sender\n\t\t\t\t\tif (!this.#sourceVerified) {\n\t\t\t\t\t\tthrow new Error(\"Cannot execute js:Function, source was received from an unverified sender\");\n\t\t\t\t\t}\n\t\t\t\t\t// SECURITY: the creator of the source must be allowed to execute JS on this endpoint\n\t\t\t\t\t// (the pointer origin alone is derived from the pointer id, which can be chosen by the sender)\n\t\t\t\t\tif (this.sourceOrigin && !Runtime.canExecuteRemoteJS(this.sourceOrigin)) {\n\t\t\t\t\t\tthrow new Error(\"Cannot execute js:Function, origin \"+this.sourceOrigin+\" has no permission to execute js source code on this endpoint\");\n\t\t\t\t\t}\n\t\t\t\t\tif (!Runtime.canExecuteRemoteJS(ptr.origin)) {\n\t\t\t\t\t\tthrow new Error(\"Cannot execute js:Function, origin \"+ptr.origin+\" has no permission to execute js source code on this endpoint\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn intermediateFn(...args)\n\t\t\t}\n\t\t\tsuper(fn);\n\t\t\tthis.#fn = fn;\n\t\t}\n\n\t\tthis.lazyResolved = lazyResolved;\n\t\tthis.sourceOrigin = options?.sourceOrigin;\n\t\tif (options?.sourceVerified === false) this.#sourceVerified = false;\n\n\t\tthis.source = source;\n\t}\n\n\n\thandleCall(...args:any[]) {\n\t\treturn this.#fn(...args)\n\t}\n\n\t// waits until all lazy dependencies are resolved and then calls the function\n\tasync callLazy() {\n\t\tawait this.lazyResolved;\n\t\treturn this.handleCall()\n\t}\n\n\tpublic get hasUnresolvedLazyDependencies() {\n\t\treturn hasUnresolvedLazyDependencies(this.deps)\n\t}\n\n\t/**\n\t * Returns JS source\n\t */\n\toverride toString() {\n\t\treturn this.source\n\t}\n\n\t/**\n\t * @returns true if the provided function is an async function\n\t */\n\tstatic functionIsAsync(fn: (...args:unknown[])=>unknown): fn is (...args:unknown[])=>Promise<unknown> {\n\t\treturn fn.constructor.name == \"AsyncFunction\"\n\t}\n\n\n\t/**\n\t * Create a new JSTransferableFunction from a regular function.\n\t * Automatically determines dependency variables declared with use()\n\t * \n\t * Important: use createAsync for async functions instead\n\t * @param fn \n\t */\n\tstatic create<T extends (...args:unknown[])=>unknown>(fn: T, options:JSTransferableFunctionOptions = {}, useDeclaration?: {vars:{[k:string]:unknown}, flags?:string[]}): JSTransferableFunction & Callable<Parameters<T>, ReturnType<T>> {\n        const {vars, flags} = useDeclaration ?? getDeclaredExternalVariables(fn);\n\t\toptions.isLocal ??= true;\n\t\treturn this.#createTransferableFunction(getSourceWithoutUsingDeclaration(fn), vars, flags, options) as any;\n\t}\n\n\t/**\n\t * Create a new JSTransferableFunction from a regular async function.\n\t * Automatically determines dependency variables declared with use()\n\t * @param fn \n\t */\n\tstatic async createAsync<T extends (...args:unknown[])=>Promise<unknown>>(fn: T, options:JSTransferableFunctionOptions = {}): Promise<JSTransferableFunction & Callable<Parameters<T>, ReturnType<T>>> {\n\t\tconst {vars, flags} = await getDeclaredExternalVariablesAsync(fn)\t\n\t\toptions.isLocal ??= true;\n\t\treturn this.#createTransferableFunction(getSourceWithoutUsingDeclaration(fn), vars, flags, options) as any;\n\t}\n\n\t/**\n\t * Recreate a JSTransferableFunction with existing source code and dependencies\n\t * @param source \n\t * @param dependencies \n\t */\n\tstatic recreate(source: string, dependencies: Record<string, unknown>, options?: JSTransferableFunctionOptions){\n\t\treturn this.#createTransferableFunction(source, dependencies, undefined, options)\n\t}\n\n\tstatic #createTransferableFunction(source: string, dependencies: Record<string, unknown>, flags?: string[], options?:JSTransferableFunctionOptions) {\n        const {intermediateFn, lazyResolved} = createFunctionWithDependencyInjectionsResolveLazyPointers(source, dependencies, !options?.isLocal);\n\t\treturn new JSTransferableFunction(intermediateFn, lazyResolved, dependencies, source, flags, options);\n\t}\n\n}\n\nexport function transferable<T extends (...args:unknown[])=>unknown>(fn: T): JSTransferableFunction & Callable<Parameters<T>, ReturnType<T>>\nexport function transferable<T extends (...args:unknown[])=>Promise<unknown>>(fn: T): Promise<JSTransferableFunction & Callable<Parameters<T>, ReturnType<T>>>\n\nexport function transferable(fn: (...args:unknown[])=>unknown) {\n\treturn JSTransferableFunction.functionIsAsync(fn) ? \n\t\tJSTransferableFunction.createAsync(fn) :\n\t\tJSTransferableFunction.create(fn)\n}"],"names":["Pointer","Runtime","ExtensibleFunction","getDeclaredExternalVariables","getDeclaredExternalVariablesAsync","getSourceWithoutUsingDeclaration","createFunctionWithDependencyInjectionsResolveLazyPointers","hasUnresolvedLazyDependencies","JSTransferableFunction","lazyResolved","sourceOrigin","sourceVerified","markSourceUnverified","intermediateFn","deps","source","flags","options","errorOnOriginContext","invalidIntermediateFunction","ptr","fn","args","isLocal","getByValue","Error","canExecuteRemoteJS","origin","handleCall","callLazy","toString","functionIsAsync","constructor","name","create","useDeclaration","vars","createAsync","recreate","dependencies","undefined","transferable"],"mappings":"AAIA,SAASA,OAAO,QAAQ,yBAAyB;AACjD,SAASC,OAAO,QAAQ,wBAAwB;AAEhD,SAASC,kBAAkB,EAAEC,4BAA4B,EAAEC,iCAAiC,EAAEC,gCAAgC,EAAYC,yDAAyD,EAAEC,6BAA6B,QAAQ,sBAAsB;AAkBhQ,OAAO,MAAMC,+BAA+BN;;;;IAE3C,CAAA,EAAG,CAA8B;IAG1BO,aAA2B;IAGlBC,aAAuB;IAEvC,CAAA,cAAe,CAAO;IAEtB,IAAWC,iBAAiB;QAC3B,OAAO,IAAI,CAAC,CAAA,cAAe;IAC5B;IAMOC,uBAAuB;QAC7B,IAAI,CAAC,CAAA,cAAe,GAAG,CAAA;IACxB;IAGA,YAAoBC,cAA4C,EAAEJ,YAA2B,EAAE,AAAOK,IAA4B,EAAE,AAAOC,MA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