UNPKG

2captcha-node

Version:

A NodeJS wrapper to solve image captchas with 2captcha

3 lines (2 loc) 9.97 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,r=(t=require("node-fetch"))&&"object"==typeof t&&"default"in t?t.default:t;function e(t,r,e,n,o,a,i){try{var c=t[a](i),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function n(t){return function(){var r=this,n=arguments;return new Promise((function(o,a){var i=t.apply(r,n);function c(t){e(i,o,a,c,u,"next",t)}function u(t){e(i,o,a,c,u,"throw",t)}c(void 0)}))}}var o,a=(function(t){var r=function(t){var r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function c(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{c({},"")}catch(t){c=function(t,r,e){return t[r]=e}}function u(t,r,e,n){var o=Object.create((r&&r.prototype instanceof f?r:f).prototype),a=new E(n||[]);return o._invoke=function(t,r,e){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(e.method=o,e.arg=a;;){var i=e.delegate;if(i){var c=x(i,e);if(c){if(c===h)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var u=s(t,r,e);if("normal"===u.type){if(n=e.done?"completed":"suspendedYield",u.arg===h)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n="completed",e.method="throw",e.arg=u.arg)}}}(t,e,a),o}function s(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var h={};function f(){}function p(){}function l(){}var v={};v[o]=function(){return this};var d=Object.getPrototypeOf,y=d&&d(d(j([])));y&&y!==r&&e.call(y,o)&&(v=y);var m=l.prototype=f.prototype=Object.create(v);function g(t){["next","throw","return"].forEach((function(r){c(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){var n;this._invoke=function(o,a){function i(){return new r((function(n,i){!function n(o,a,i,c){var u=s(t[o],t,a);if("throw"!==u.type){var h=u.arg,f=h.value;return f&&"object"==typeof f&&e.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,i,c)}),(function(t){n("throw",t,i,c)})):r.resolve(f).then((function(t){h.value=t,i(h)}),(function(t){return n("throw",t,i,c)}))}c(u.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function x(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,x(t,r),"throw"===r.method))return h;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return h}var n=s(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,h;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,h):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,h)}function b(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function L(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function j(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,a=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return a.next=a}}return{next:S}}function S(){return{value:void 0,done:!0}}return p.prototype=m.constructor=l,l.constructor=p,p.displayName=c(l,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===p||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,l):(t.__proto__=l,c(t,i,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},g(w.prototype),w.prototype[a]=function(){return this},t.AsyncIterator=w,t.async=function(r,e,n,o,a){void 0===a&&(a=Promise);var i=new w(u(r,e,n,o),a);return t.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},g(m),c(m,i,"Generator"),m[o]=function(){return this},m.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=j,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(L),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e,n){return i.type="throw",i.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var c=e.call(a,"catchLoc"),u=e.call(a,"finallyLoc");if(c&&u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,h):this.complete(i)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),h},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),L(e),h}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;L(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:j(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),h}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}}(o={exports:{}}),o.exports),i=new URL("http://2captcha.com/in.php"),c=new URL("http://2captcha.com/res.php"),u=function(t){return new Promise((function(r){return setTimeout(r,t)}))};exports.default=function(t){var e=function(){var e=n(a.mark((function e(n){var o,c;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o={method:"POST",headers:{"cache-control":"no-cache","content-type":"multipart/form-data"},body:JSON.stringify({body:n})},i.search=new URLSearchParams({key:t,json:"1",method:"base64"}).toString(),e.prev=3,e.next=6,r(i.toString(),o);case 6:return c=e.sent,e.next=9,c.json();case 9:return e.abrupt("return",e.sent.request);case 13:throw e.prev=13,e.t0=e.catch(3),new Error(e.t0);case 16:case"end":return e.stop()}}),e,null,[[3,13]])})));return function(t){return e.apply(this,arguments)}}(),o=function(){var e=n(a.mark((function e(n,i){var s,h;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(i){e.next=2;break}throw new Error("This request has reached the maximum number of attempts");case 2:return c.search=new URLSearchParams({key:t,action:"get",id:n,json:"1"}).toString(),e.prev=4,e.next=7,r(c.toString());case 7:return s=e.sent,e.next=10,s.json();case 10:if(1!==(h=e.sent).status){e.next=13;break}return e.abrupt("return",{id:n,text:h.request});case 13:return e.next=15,u(1e3);case 15:return e.abrupt("return",o(n,i-1));case 18:throw e.prev=18,e.t0=e.catch(4),new Error(e.t0);case 21:case"end":return e.stop()}}),e,null,[[4,18]])})));return function(t,r){return e.apply(this,arguments)}}();return{solve:function(){var t=n(a.mark((function t(r){var n,i,c,s;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=r.image,c=void 0===(i=r.maxAttempts)?60:i,t.next=3,e(n);case 3:return s=t.sent,t.next=6,u(5e3);case 6:return t.abrupt("return",o(s,c));case 7:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),balance:function(){var e=n(a.mark((function e(){var n;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return c.search=new URLSearchParams({key:t,action:"getbalance",json:"1"}).toString(),e.prev=2,e.next=5,r(c.toString());case 5:return n=e.sent,e.next=8,n.json();case 8:return e.abrupt("return",e.sent.request);case 12:throw e.prev=12,e.t0=e.catch(2),new Error(e.t0);case 15:case"end":return e.stop()}}),e,null,[[2,12]])})));return function(){return e.apply(this,arguments)}}(),report:function(){var e=n(a.mark((function e(n,o){var i;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return c.search=new URLSearchParams({key:t,action:o?"reportgood":"reportbad",json:"1",id:n}).toString(),e.prev=2,e.next=5,r(c.toString());case 5:return i=e.sent,e.next=8,i.json();case 8:return e.abrupt("return",!!e.sent.request);case 12:throw e.prev=12,e.t0=e.catch(2),new Error(e.t0);case 15:case"end":return e.stop()}}),e,null,[[2,12]])})));return function(t,r){return e.apply(this,arguments)}}()}}; //# sourceMappingURL=captcha-node.cjs.production.min.js.map