UNPKG

tlsclientwrapper

Version:

A wrapper for `bogdanfinn/tls-client` based on koffi for unparalleled performance and usability. Inspired by @dryft/tlsclient

37 lines (35 loc) 1.2 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/utils/worker.ts import koffi from "koffi"; import { workerData } from "worker_threads"; var lib = koffi.load(workerData.libraryPath); var freeMemory = lib.func("freeMemory", "void", ["string"]); var functions = { request: lib.func("request", "string", ["string"]), getCookiesFromSession: lib.func("getCookiesFromSession", "string", ["string"]), addCookiesToSession: lib.func("addCookiesToSession", "string", ["string"]), destroyAll: lib.func("destroyAll", "string", []), destroySession: lib.func("destroySession", "string", ["string"]) }; function handler(task) { const { fn, args = [] } = task; const func = functions[fn]; if (!func) { throw new Error(`Unknown function: ${fn}`); } const result = func(...args); if (typeof result === "string" && result.trim().startsWith("{")) { const parsedResult = JSON.parse(result); if (parsedResult.id) { freeMemory(parsedResult.id); delete parsedResult.id; } return parsedResult; } return result; } __name(handler, "handler"); export { handler as default };