"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { createBackgroundHandler: () => createBackgroundHandler, createWebextRpcCaller: () => createWebextRpcCaller }); module.exports = __toCommonJS(src_exports); // src/createBackgroundHandler.ts var import_webextension_polyfill = __toESM(require("webextension-polyfill"), 1); // src/utils/handleCall.ts async function handleCall(func, args, sendMessage) { const gen = func(...args); if (gen instanceof Promise) { const result = await gen; sendMessage({ isStream: false, value: result }); return; } if (typeof gen?.next === "function") { if (typeof gen[Symbol.asyncIterator] === "function") { let iterationResult = await gen.next(); while (!iterationResult.done) { sendMessage({ isStream: true, value: iterationResult.value, done: false }); iterationResult = await gen.next(); } sendMessage({ isStream: true, value: null, done: true }); return; } if (typeof gen[Symbol.iterator] === "function") { let iterationResult = gen.next(); const value = []; while (!iterationResult.done) { value.push(iterationResult.value); iterationResult = gen.next(); } sendMessage({ isStream: false, value }); return; } const error = { message: "Unsupported type of generator", stack: new Error().stack, name: "UnsupportedType" }; sendMessage({ error }); throw error; } sendMessage({ isStream: false, value: gen }); } // src/port_name.ts var DEFAULT_PORT_NAME = "webext-rpc"; // src/utils/readerToAsyncGenerator.ts async function* readerToAsyncGenerator(reader, pipe) { while (true) { const { done, value } = await reader.read(); if (done) { break; } if (pipe !== void 0) { yield pipe(value); } else { yield value; } } } // src/utils/MessageStream.ts var MessageStream = class { readableStream; controller; constructor() { this.readableStream = new ReadableStream({ start: (controller) => { this.controller = controller; } }); } // 添加消息到流 addMessage(message) { if (this.controller) { this.controller.enqueue(message); } } // 关闭流 close() { if (this.controller) { this.controller.close(); } } // 返回一个异步生成器来获取数据 async *getMessages() { const reader = this.readableStream.getReader(); yield* readerToAsyncGenerator(reader); } }; // src/utils/util.ts function toErrorMessage(error) { if (error instanceof Error) { return { message: error.message, stack: error.stack, name: error.name }; } return { message: String(error), stack: "", name: "UnknownError" }; } // src/utils/createRpcCaller.ts function createRpcCaller(callbacks) { const createProxyObject = (calls = []) => { const handler = { get(_thisArg, prop, receiver) { return createProxyObject([...calls, prop.toString()]); }, async apply(_target, _thisArg, args) { return callbacks(calls, args); } }; return new Proxy(() => { }, handler); }; return createProxyObject(); } // src/utils/ExposedPromise.ts var ExposedPromise = class { resolve; reject; promise; constructor() { this.promise = new Promise((resolve, reject) => { this.resolve = resolve; this.reject = reject; }); } }; // src/createBackgroundHandler.ts function createBackgroundHandler(router, port_name = DEFAULT_PORT_NAME) { import_webextension_polyfill.default.runtime.onConnect.addListener((port) => { if (port.name !== port_name) { return; } port.onMessage.addListener(async (message) => { const { calls, args } = message; function throwError(error) { const error_message = toErrorMessage(error); port.postMessage({ error: error_message }); throw error; } try { const func = calls.reduce((acc, call) => acc[call], router); await handleCall(func, args, (message2) => { port.postMessage(message2); }); } catch (error) { throwError(error); } finally { port.disconnect(); } }); }); } // src/createWebextRpcCaller.ts var import_webextension_polyfill2 = __toESM(require("webextension-polyfill"), 1); var createWebextRpcCaller = (port_name = DEFAULT_PORT_NAME) => { return createRpcCaller((calls, args) => { const port = import_webextension_polyfill2.default.runtime.connect({ name: port_name }); const exposedPromise = new ExposedPromise(); let messageStream = null; port.onMessage.addListener((message) => { if (message.error) { const error = new Error(message.error.message); error.stack = "[webext-rpc background function error]" + message.error.stack; error.name = message.error.name; exposedPromise.reject(error); return; } if (message.isStream) { if (message.done) { return; } if (messageStream === null) { messageStream = new MessageStream(); messageStream.addMessage(message.value); exposedPromise.resolve(messageStream.getMessages()); return; } messageStream.addMessage(message.value); return; } exposedPromise.resolve(message.value); }); port.onDisconnect.addListener(() => { if (messageStream) { messageStream.close(); } }); port.postMessage({ calls, args }); return exposedPromise.promise; }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createBackgroundHandler, createWebextRpcCaller });