UNPKG

ayakashi

Version:

The next generation web scraping framework

126 lines (125 loc) 5.02 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBridgeClient = void 0; const request_1 = __importDefault(require("@ayakashi/request")); const tough_cookie_1 = require("tough-cookie"); const debug_1 = __importDefault(require("debug")); const d = debug_1.default("ayakashi:bridge:client"); function getBridgeClient(port) { return { getTarget: function () { return __awaiter(this, void 0, void 0, function* () { try { const resp = yield request_1.default.post(`http://localhost:${port}/connection/create_target`); d("getTarget response:", resp); if (resp) { const parsedResp = JSON.parse(resp); if (parsedResp.ok) { return parsedResp.target; } else { return null; } } else { return null; } } catch (e) { d(e); return null; } }); }, getUserAgentData: function (input) { return __awaiter(this, void 0, void 0, function* () { try { const resp = yield request_1.default.post(`http://localhost:${port}/user_agent`, { json: input }); d("getUserAgentData response:", resp); if (resp) { if (resp.ok) { return resp.userAgentData; } else { return null; } } else { return null; } } catch (e) { d(e); return null; } }); }, connectionReleased: function (target) { return __awaiter(this, void 0, void 0, function* () { yield request_1.default.post(`http://localhost:${port}/connection/released`, { json: { targetId: target.targetId, browserContextId: target.browserContextId } }); }); }, getCookieJar: function () { return __awaiter(this, void 0, void 0, function* () { try { const resp = yield request_1.default.post(`http://localhost:${port}/cookies/get_jar`); d("get_jar response:", resp); if (resp) { const parsedResp = JSON.parse(resp); if (parsedResp.ok) { return parsedResp.cookies .map(function (cookie) { return tough_cookie_1.Cookie.fromJSON(cookie); }) .filter(function (cookie) { return cookie !== null; }); } else { return []; } } else { return []; } } catch (e) { d(e); return []; } }); }, updateCookieJar: function (cookies) { return __awaiter(this, void 0, void 0, function* () { try { yield request_1.default.post(`http://localhost:${port}/cookies/update_jar`, { json: { cookies: cookies.map(cookie => cookie.toJSON()) } }); } catch (e) { d(e); } }); } }; } exports.getBridgeClient = getBridgeClient;