"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, { ProxyCountry: () => ProxyCountry, ProxyRegion: () => ProxyRegion, ScrapingBrowser: () => ScrapingBrowser, default: () => ScrapingBrowser }); module.exports = __toCommonJS(src_exports); var import_puppeteer_core = __toESM(require("puppeteer-core"), 1); // src/errors.ts var ConfigurationError = class extends Error { constructor(message) { super(message); this.name = "ConfigurationError"; } }; var ParameterValidationError = class extends Error { constructor(message) { super(message); this.name = "ParameterValidationError"; } }; var MissingAPIKeyError = new ConfigurationError( "An API key is required to use the ZenRows Scraping Browser API" ); // src/types.ts var ProxyCountry = /* @__PURE__ */ ((ProxyCountry2) => { ProxyCountry2["AF"] = "af"; ProxyCountry2["AL"] = "al"; ProxyCountry2["DZ"] = "dz"; ProxyCountry2["AS"] = "as"; ProxyCountry2["AD"] = "ad"; ProxyCountry2["AO"] = "ao"; ProxyCountry2["AI"] = "ai"; ProxyCountry2["AQ"] = "aq"; ProxyCountry2["AG"] = "ag"; ProxyCountry2["AR"] = "ar"; ProxyCountry2["AM"] = "am"; ProxyCountry2["AW"] = "aw"; ProxyCountry2["AU"] = "au"; ProxyCountry2["AT"] = "at"; ProxyCountry2["AZ"] = "az"; ProxyCountry2["BS"] = "bs"; ProxyCountry2["BH"] = "bh"; ProxyCountry2["BD"] = "bd"; ProxyCountry2["BB"] = "bb"; ProxyCountry2["BY"] = "by"; ProxyCountry2["BE"] = "be"; ProxyCountry2["BZ"] = "bz"; ProxyCountry2["BJ"] = "bj"; ProxyCountry2["BM"] = "bm"; ProxyCountry2["BT"] = "bt"; ProxyCountry2["BO"] = "bo"; ProxyCountry2["BA"] = "ba"; ProxyCountry2["BW"] = "bw"; ProxyCountry2["BV"] = "bv"; ProxyCountry2["BR"] = "br"; ProxyCountry2["IO"] = "io"; ProxyCountry2["BN"] = "bn"; ProxyCountry2["BG"] = "bg"; ProxyCountry2["BF"] = "bf"; ProxyCountry2["BI"] = "bi"; ProxyCountry2["KH"] = "kh"; ProxyCountry2["CM"] = "cm"; ProxyCountry2["CA"] = "ca"; ProxyCountry2["CV"] = "cv"; ProxyCountry2["KY"] = "ky"; ProxyCountry2["CF"] = "cf"; ProxyCountry2["TD"] = "td"; ProxyCountry2["CL"] = "cl"; ProxyCountry2["CN"] = "cn"; ProxyCountry2["CX"] = "cx"; ProxyCountry2["CC"] = "cc"; ProxyCountry2["CO"] = "co"; ProxyCountry2["KM"] = "km"; ProxyCountry2["CG"] = "cg"; ProxyCountry2["CD"] = "cd"; ProxyCountry2["CK"] = "ck"; ProxyCountry2["CR"] = "cr"; ProxyCountry2["CI"] = "ci"; ProxyCountry2["HR"] = "hr"; ProxyCountry2["CU"] = "cu"; ProxyCountry2["CY"] = "cy"; ProxyCountry2["CZ"] = "cz"; ProxyCountry2["DK"] = "dk"; ProxyCountry2["DJ"] = "dj"; ProxyCountry2["DM"] = "dm"; ProxyCountry2["DO"] = "do"; ProxyCountry2["TL"] = "tl"; ProxyCountry2["EC"] = "ec"; ProxyCountry2["EG"] = "eg"; ProxyCountry2["SV"] = "sv"; ProxyCountry2["GQ"] = "gq"; ProxyCountry2["ER"] = "er"; ProxyCountry2["EE"] = "ee"; ProxyCountry2["ET"] = "et"; ProxyCountry2["FK"] = "fk"; ProxyCountry2["FO"] = "fo"; ProxyCountry2["FJ"] = "fj"; ProxyCountry2["FI"] = "fi"; ProxyCountry2["FR"] = "fr"; ProxyCountry2["GF"] = "gf"; ProxyCountry2["PF"] = "pf"; ProxyCountry2["TF"] = "tf"; ProxyCountry2["GA"] = "ga"; ProxyCountry2["GM"] = "gm"; ProxyCountry2["GE"] = "ge"; ProxyCountry2["DE"] = "de"; ProxyCountry2["GH"] = "gh"; ProxyCountry2["GI"] = "gi"; ProxyCountry2["GB"] = "gb"; ProxyCountry2["GR"] = "gr"; ProxyCountry2["GL"] = "gl"; ProxyCountry2["GD"] = "gd"; ProxyCountry2["GP"] = "gp"; ProxyCountry2["GU"] = "gu"; ProxyCountry2["GT"] = "gt"; ProxyCountry2["GN"] = "gn"; ProxyCountry2["GW"] = "gw"; ProxyCountry2["GY"] = "gy"; ProxyCountry2["HT"] = "ht"; ProxyCountry2["HM"] = "hm"; ProxyCountry2["VA"] = "va"; ProxyCountry2["HN"] = "hn"; ProxyCountry2["HK"] = "hk"; ProxyCountry2["HU"] = "hu"; ProxyCountry2["IS"] = "is"; ProxyCountry2["IN"] = "in"; ProxyCountry2["ID"] = "id"; ProxyCountry2["IR"] = "ir"; ProxyCountry2["IQ"] = "iq"; ProxyCountry2["IE"] = "ie"; ProxyCountry2["IL"] = "il"; ProxyCountry2["IT"] = "it"; ProxyCountry2["JM"] = "jm"; ProxyCountry2["JP"] = "jp"; ProxyCountry2["JO"] = "jo"; ProxyCountry2["KZ"] = "kz"; ProxyCountry2["KE"] = "ke"; ProxyCountry2["KI"] = "ki"; ProxyCountry2["KP"] = "kp"; ProxyCountry2["KR"] = "kr"; ProxyCountry2["KW"] = "kw"; ProxyCountry2["KG"] = "kg"; ProxyCountry2["LA"] = "la"; ProxyCountry2["LV"] = "lv"; ProxyCountry2["LB"] = "lb"; ProxyCountry2["LS"] = "ls"; ProxyCountry2["LR"] = "lr"; ProxyCountry2["LY"] = "ly"; ProxyCountry2["LI"] = "li"; ProxyCountry2["LT"] = "lt"; ProxyCountry2["LU"] = "lu"; ProxyCountry2["MO"] = "mo"; ProxyCountry2["MK"] = "mk"; ProxyCountry2["MG"] = "mg"; ProxyCountry2["MW"] = "mw"; ProxyCountry2["MY"] = "my"; ProxyCountry2["MV"] = "mv"; ProxyCountry2["ML"] = "ml"; ProxyCountry2["MT"] = "mt"; ProxyCountry2["MH"] = "mh"; ProxyCountry2["MQ"] = "mq"; ProxyCountry2["MR"] = "mr"; ProxyCountry2["MU"] = "mu"; ProxyCountry2["YT"] = "yt"; ProxyCountry2["MX"] = "mx"; ProxyCountry2["FM"] = "fm"; ProxyCountry2["MD"] = "md"; ProxyCountry2["MC"] = "mc"; ProxyCountry2["MN"] = "mn"; ProxyCountry2["ME"] = "me"; ProxyCountry2["MS"] = "ms"; ProxyCountry2["MA"] = "ma"; ProxyCountry2["MZ"] = "mz"; ProxyCountry2["MM"] = "mm"; ProxyCountry2["NA"] = "na"; ProxyCountry2["NR"] = "nr"; ProxyCountry2["NP"] = "np"; ProxyCountry2["NL"] = "nl"; ProxyCountry2["AN"] = "an"; ProxyCountry2["NC"] = "nc"; ProxyCountry2["NZ"] = "nz"; ProxyCountry2["NI"] = "ni"; ProxyCountry2["NE"] = "ne"; ProxyCountry2["NG"] = "ng"; ProxyCountry2["NU"] = "nu"; ProxyCountry2["NF"] = "nf"; ProxyCountry2["MP"] = "mp"; ProxyCountry2["NO"] = "no"; ProxyCountry2["OM"] = "om"; ProxyCountry2["PK"] = "pk"; ProxyCountry2["PW"] = "pw"; ProxyCountry2["PA"] = "pa"; ProxyCountry2["PG"] = "pg"; ProxyCountry2["PY"] = "py"; ProxyCountry2["PE"] = "pe"; ProxyCountry2["PH"] = "ph"; ProxyCountry2["PN"] = "pn"; ProxyCountry2["PL"] = "pl"; ProxyCountry2["PT"] = "pt"; ProxyCountry2["PR"] = "pr"; ProxyCountry2["QA"] = "qa"; ProxyCountry2["RE"] = "re"; ProxyCountry2["RO"] = "ro"; ProxyCountry2["RU"] = "ru"; ProxyCountry2["RW"] = "rw"; ProxyCountry2["SH"] = "sh"; ProxyCountry2["KN"] = "kn"; ProxyCountry2["LC"] = "lc"; ProxyCountry2["PM"] = "pm"; ProxyCountry2["VC"] = "vc"; ProxyCountry2["WS"] = "ws"; ProxyCountry2["SM"] = "sm"; ProxyCountry2["ST"] = "st"; ProxyCountry2["SA"] = "sa"; ProxyCountry2["SN"] = "sn"; ProxyCountry2["SC"] = "sc"; ProxyCountry2["SL"] = "sl"; ProxyCountry2["SG"] = "sg"; ProxyCountry2["SK"] = "sk"; ProxyCountry2["SI"] = "si"; ProxyCountry2["SB"] = "sb"; ProxyCountry2["SO"] = "so"; ProxyCountry2["ZA"] = "za"; ProxyCountry2["GS"] = "gs"; ProxyCountry2["ES"] = "es"; ProxyCountry2["LK"] = "lk"; ProxyCountry2["SD"] = "sd"; ProxyCountry2["SR"] = "sr"; ProxyCountry2["SJ"] = "sj"; ProxyCountry2["SZ"] = "sz"; ProxyCountry2["SE"] = "se"; ProxyCountry2["CH"] = "ch"; ProxyCountry2["SY"] = "sy"; ProxyCountry2["TW"] = "tw"; ProxyCountry2["TJ"] = "tj"; ProxyCountry2["TZ"] = "tz"; ProxyCountry2["TH"] = "th"; ProxyCountry2["TG"] = "tg"; ProxyCountry2["TK"] = "tk"; ProxyCountry2["TO"] = "to"; ProxyCountry2["TT"] = "tt"; ProxyCountry2["TN"] = "tn"; ProxyCountry2["TR"] = "tr"; ProxyCountry2["TM"] = "tm"; ProxyCountry2["TC"] = "tc"; ProxyCountry2["TV"] = "tv"; ProxyCountry2["UG"] = "ug"; ProxyCountry2["UA"] = "ua"; ProxyCountry2["AE"] = "ae"; ProxyCountry2["US"] = "us"; ProxyCountry2["UY"] = "uy"; ProxyCountry2["UZ"] = "uz"; ProxyCountry2["VU"] = "vu"; ProxyCountry2["VE"] = "ve"; ProxyCountry2["VN"] = "vn"; ProxyCountry2["VG"] = "vg"; ProxyCountry2["VI"] = "vi"; ProxyCountry2["WF"] = "wf"; ProxyCountry2["EH"] = "eh"; ProxyCountry2["YE"] = "ye"; ProxyCountry2["ZM"] = "zm"; ProxyCountry2["ZW"] = "zw"; return ProxyCountry2; })(ProxyCountry || {}); var ProxyRegion = /* @__PURE__ */ ((ProxyRegion2) => { ProxyRegion2["Africa"] = "af"; ProxyRegion2["AsiaPacific"] = "ap"; ProxyRegion2["Europe"] = "eu"; ProxyRegion2["MiddleEast"] = "me"; ProxyRegion2["NorthAmerica"] = "na"; ProxyRegion2["SouthLatinAmerica"] = "sa"; return ProxyRegion2; })(ProxyRegion || {}); ((ProxyRegion2) => { function isValid(region) { return Object.values(ProxyRegion2).includes(region); } ProxyRegion2.isValid = isValid; })(ProxyRegion || (ProxyRegion = {})); ((ProxyCountry2) => { function isValid(countryCode) { return Object.values(ProxyCountry2).includes(countryCode); } ProxyCountry2.isValid = isValid; })(ProxyCountry || (ProxyCountry = {})); // src/index.ts var ScrapingBrowser = class { /* The ZenRows API key to use. Can be obtained from the ZenRows dashboard */ apiKey; /* The URL of the ZenRows' Scraping Browser API */ apiURL; constructor(options = {}) { this.apiKey = options.apiKey || process.env.ZENROWS_API_KEY || ""; this.apiURL = (options.insecure ? "ws://" : "wss://") + (options.apiURL || "browser.zenrows.com"); if (!this.apiKey) { throw MissingAPIKeyError; } } /** * Get the WebSocket URL to connect to the ZenRows' Scraping Browser API * @param opts - Options to configure the connection * @throws {ParameterValidationError} If an invalid option is provided * @returns The WebSocket URL */ getConnectURL(opts = {}) { let url = `${this.apiURL}?apikey=${this.apiKey}`; if (opts.proxy?.location) { url += ProxyCountry.isValid(opts.proxy.location) ? `&proxy_country=${opts.proxy.location}` : ProxyRegion.isValid(opts.proxy.location) ? `&proxy_region=${opts.proxy.location}` : ""; } if (opts.sessionTTL) { if (!Number.isInteger(opts.sessionTTL) || opts.sessionTTL < 60 || opts.sessionTTL > 900) { throw new ParameterValidationError("SessionTTL must be a number between 60 and 900 seconds"); } url += `&session_ttl=${convertSecondsToDuration(opts.sessionTTL)}`; } return url; } /** * Take a screenshot of a page using the ZenRows' Scraping Browser API. * @param url - The URL of the page to take a screenshot of * @param options - Options to configure the screenshot */ async screenshot(url, options = { fullPage: false }) { if (!url) { throw new Error("A URL is required to take a screenshot"); } const browser = await import_puppeteer_core.default.connect({ browserWSEndpoint: this.getConnectURL({ proxy: options.proxy }) }); const pages = await browser.pages(); const page = pages[0]; await page?.goto(url); const screenshot = await page?.screenshot({ fullPage: options.fullPage }); await browser.close(); return screenshot; } }; function convertSecondsToDuration(seconds) { const minutes = Math.floor(seconds / 60); const remainingSeconds = seconds % 60; let duration = ""; if (minutes > 0) { duration += `${minutes}m`; } if (remainingSeconds > 0 || minutes === 0) { duration += `${remainingSeconds}s`; } return duration; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ProxyCountry, ProxyRegion, ScrapingBrowser });