Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

threads-api

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

threads-api - npm Package Compare versions

Comparing version 1.6.2 to 1.6.3

16

build/threads-api.d.ts

@@ -248,6 +248,6 @@ /// <reference types="node" />

deviceID: string;
device?: AndroidDevice;
userID: string | undefined;
locale?: string | undefined;
maxRetries?: number;
device: AndroidDevice;
userID?: string;
locale: string;
maxRetries: number;
constructor(options?: ThreadsAPI.Options);

@@ -275,3 +275,3 @@ sign(payload: object | string): {

Accept: string;
'Accept-Language': string | undefined;
'Accept-Language': string;
'cache-control': string;

@@ -312,3 +312,3 @@ Origin: string;

accept: string;
'accept-language': string | undefined;
'accept-language': string;
'cache-control': string;

@@ -343,3 +343,3 @@ origin: string;

Accept: string;
'Accept-Language': string | undefined;
'Accept-Language': string;
'cache-control': string;

@@ -406,3 +406,3 @@ Origin: string;

timezone_offset: string;
device: AndroidDevice | undefined;
device: AndroidDevice;
};

@@ -409,0 +409,0 @@ publish: (rawOptions: ThreadsAPI.PublishOptions | string) => Promise<string | undefined>;

@@ -1,1 +0,1 @@

"use strict";!function(e,t){for(var r in t)Object.defineProperty(e,r,{enumerable:!0,get:t[r]})}(exports,{DEFAULT_DEVICE:function(){return p},ThreadsAPI:function(){return f},default:function(){return g}});var e=require("axios"),t=require("crypto"),r=require("mrmime"),n=require("uuid"),s=require("./constants"),a=require("./dynamic-data"),o=require("./error");function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function u(e,t,r,n,s,a,o){try{var i=e[a](o),u=i.value}catch(e){r(e);return}i.done?t(u):Promise.resolve(u).then(n,s)}function c(e){return function(){var t=this,r=arguments;return new Promise(function(n,s){var a=e.apply(t,r);function o(e){u(a,n,s,o,i,"next",e)}function i(e){u(a,n,s,o,i,"throw",e)}o(void 0)})}}function l(){return(l=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function d(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return i(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);if("Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r)return Array.from(r);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return i(e,t)}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function h(e,t){var r,n,s,a,o={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(a){return function(i){return function(a){if(r)throw TypeError("Generator is already executing.");for(;o;)try{if(r=1,n&&(s=2&a[0]?n.return:a[0]?n.throw||((s=n.return)&&s.call(n),0):n.next)&&!(s=s.call(n,a[1])).done)return s;switch(n=0,s&&(a=[2&a[0],s.value]),a[0]){case 0:case 1:s=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,n=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(s=(s=o.trys).length>0&&s[s.length-1])&&(6===a[0]||2===a[0])){o=0;continue}if(3===a[0]&&(!s||a[1]>s[0]&&a[1]<s[3])){o.label=a[1];break}if(6===a[0]&&o.label<s[1]){o.label=s[1],s=a;break}if(s&&o.label<s[2]){o.label=s[2],o.ops.push(a);break}s[2]&&o.ops.pop(),o.trys.pop();continue}a=t.call(e,o)}catch(e){a=[6,e],n=0}finally{r=s=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,i])}}}var p={manufacturer:"OnePlus",model:"ONEPLUS+A3010",os_version:25,os_release:"7.1.1"},f=function(){"use strict";function i(i){var u,f,g,_,v=this;this.verbose=!1,this.token=void 0,this.fbLSDToken=s.DEFAULT_LSD_TOKEN,this.noUpdateToken=!1,this.noUpdateLSD=!1,this.device=p,this.userID=void 0,this.locale=void 0,this.maxRetries=1;var b=this;this.syncLoginExperiments=c(function(){var t,r,a;return h(this,function(o){switch(o.label){case 0:r={id:t=(0,n.v4)(),experiments:s.LOGIN_EXPERIMENTS},o.label=1;case 1:return o.trys.push([1,3,,4]),[4,e.default.post(""+s.BASE_API_URL+"/api/v1/qe/sync/",b.sign(r),{httpAgent:b.httpAgent,httpsAgent:b.httpsAgent,headers:l({},b._getAppHeaders(),{Authorization:void 0,"Sec-Fetch-Site":"same-origin","X-DEVICE-ID":t})})];case 2:return[2,o.sent()];case 3:throw a=o.sent(),b.verbose&&console.log("[SYNC LOGIN EXPERIMENT FAILED]",a.response.data),Error("Sync login experiment failed");case 4:return[2]}})});var m=this;this.encryptPassword=c(function(e){var r,n,s,a,o,i,u,c,l,d,p;return h(this,function(h){switch(h.label){case 0:return r=t.randomBytes(32),n=t.randomBytes(12),[4,m.syncLoginExperiments()];case 1:return s=h.sent().headers,m.verbose&&console.log("[SYNC LOGIN EXPERIMENT HEADERS]",JSON.stringify(s)),a=s["ig-set-password-encryption-key-id"],o=s["ig-set-password-encryption-pub-key"],i=t.publicEncrypt({key:Buffer.from(o||"","base64").toString(),padding:t.constants.RSA_PKCS1_PADDING},r),u=t.createCipheriv("aes-256-gcm",r,n),c=Math.floor(Date.now()/1e3).toString(),u.setAAD(Buffer.from(c)),l=Buffer.concat([u.update(e,"utf8"),u.final()]),(d=Buffer.alloc(2,0)).writeInt16LE(i.byteLength,0),p=u.getAuthTag(),[2,{time:c,password:Buffer.concat([Buffer.from([1,a||0]),n,d,i,p,l]).toString("base64")}]}})});var I=this;this.login=c(function(){var t,r,n,a;return h(this,function(o){switch(o.label){case 0:t=function(){var e;return h(this,function(t){switch(t.label){case 0:return t.trys.push([0,1,,3]),[2,{v:n()}];case 1:return t.sent(),I.verbose&&console.error("[LOGIN] Failed to login, retrying... ("+(r+1)+"/"+I.maxRetries+")"),e=1e3*Math.pow(2,r),[4,new Promise(function(t){return setTimeout(t,e)})];case 2:return t.sent(),r++,[3,3];case 3:return[2]}})},r=0,n=c(function(){var t,r,n,a,o,i,u,c;return h(this,function(l){switch(l.label){case 0:return I.verbose&&console.log("[LOGIN] Logging in..."),[4,I.encryptPassword(I.password)];case 1:return r=encodeURIComponent(JSON.stringify({client_input_params:{password:"#PWD_INSTAGRAM:4:"+(t=l.sent()).time+":"+t.password,contact_point:I.username,device_id:I.deviceID},server_params:{credential_type:"password",device_id:I.deviceID}})),n=encodeURIComponent(JSON.stringify({bloks_version:s.BLOKS_VERSION,styles_id:"instagram"})),a={httpAgent:I.httpAgent,httpsAgent:I.httpsAgent,method:"POST",headers:I._getAppHeaders(),responseType:"text",data:"params="+r+"&bk_client_context="+n+"&bloks_versioning_id="+s.BLOKS_VERSION},[4,(0,e.default)(""+s.BASE_API_URL+"/api/v1/bloks/apps/com.bloks.www.bloks.caa.login.async.send_login_request/",a)];case 2:o=JSON.stringify((o=l.sent().data).replaceAll("\\","")),I.verbose&&console.log("[LOGIN] Cleaned output",o);try{return u=o.split("Bearer IGT:2:")[1].split('"')[0].replaceAll("\\",""),c=null==(i=o.match(/pk_id(.{18})/))?void 0:i[1].replaceAll(/[\\":]/g,""),I.noUpdateToken||(I.verbose&&console.debug("[token] UPDATED",u),I.token=u),I.userID=c,I.verbose&&console.debug("[userID] UPDATED",I.userID),[2,{token:u,userID:c}]}catch(e){throw I.verbose&&console.error("[LOGIN] Failed to login",e),Error("Login Failed")}return[2]}})}),o.label=1;case 1:if(!(r<I.maxRetries))return[3,3];return[5,function(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(t())];case 2:var i;if("object"==((i=a=o.sent())&&"undefined"!=typeof Symbol&&i.constructor===Symbol?"symbol":typeof i))return[2,a.v];return[3,1];case 3:throw Error("[LOGIN] Failed to login after "+I.maxRetries+" retries")}})}),this._getUnAuthenticatedHeaders=function(){return{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.199 Safari/537.36","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}},this._getDefaultUserDataHeaders=function(e){return l({},v._getUnAuthenticatedHeaders(),{Host:"www.threads.net",Accept:"*/*","Accept-Language":v.locale,"cache-control":"no-cache",Origin:"https://www.threads.net",Pragma:"no-cache","Sec-Fetch-Site":"same-origin","X-Asbd-id":"129477","X-FB-Friendly-Name":"BarcelonaProfileRootQuery","X-FB-Lsd":v.fbLSDToken,"X-Ig-App-Id":"238260118697367"},e?{Referer:"https://www.threads.net/@"+e}:void 0)},this._getAppHeaders=function(){return l({"User-Agent":"Barcelona "+a.LATEST_ANDROID_APP_VERSION+" Android","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},v.token&&{Authorization:"Bearer IGT:2:"+v.token})},this._getInstaHeaders=function(){return l({},v._getAppHeaders(),{"X-Bloks-Is-Layout-Rtl":"false","X-Bloks-Version-Id":s.BLOKS_VERSION,"X-Ig-Android-Id":v.deviceID,"X-Ig-App-Id":s.IG_APP_ID,"Accept-Language":v.locale||"en-US"},v.userID&&{"Ig-U-Ds-User-Id":v.userID,"Ig-Intended-User-Id":v.userID},v.locale&&{"X-Ig-App-Locale":v.locale.replace("-","_"),"X-Ig-Device-Locale":v.locale.replace("-","_"),"X-Ig-Mapped-Locale":v.locale.replace("-","_")})},this._getDefaultHeaders=function(e){return l({},v._getAppHeaders(),{authority:"www.threads.net",accept:"*/*","accept-language":v.locale,"cache-control":"no-cache",origin:"https://www.threads.net",pragma:"no-cache","Sec-Fetch-Site":"same-origin","x-asbd-id":"129477","x-fb-lsd":v.fbLSDToken,"x-ig-app-id":"238260118697367"},e?{referer:"https://www.threads.net/@"+e}:void 0)};var A=this;this._getCleanedProfileHTML=c(function(t,r,n){return h(this,function(s){switch(s.label){case 0:return[4,e.default.get(""+t+r,l({},n,{httpAgent:A.httpAgent,httpsAgent:A.httpsAgent,headers:l({},A._getDefaultHeaders(r),{accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","accept-language":"ko,en;q=0.9,ko-KR;q=0.8,ja;q=0.7",Authorization:void 0,referer:"https://www.instagram.com/","sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"cross-site","sec-fetch-user":"?1","upgrade-insecure-requests":"1","x-asbd-id":void 0,"x-fb-lsd":void 0,"x-ig-app-id":void 0})}))];case 1:return[2,(0,s.sent().data).replace(/\s/g,"").replace(/\n/g,"")]}})});var S=this;this.getUserIDfromUsernameWithInstagram=c(function(e,t){var r,n,s,a,o;return h(this,function(i){switch(i.label){case 0:return[4,S._getCleanedProfileHTML("https://www.instagram.com/",e,t)];case 1:return a=null==(r=(s=i.sent()).match(/"user_id":"(\d+)",/))?void 0:r[1],o=null==(n=s.match(/"LSD",\[\],{"token":"(\w+)"},\d+\]/))?void 0:n[1],!S.noUpdateLSD&&o&&(S.fbLSDToken=o,S.verbose&&console.debug("[fbLSDToken] UPDATED",S.fbLSDToken)),[2,a]}})});var w=this;this.getUserIDfromUsername=c(function(e,t){var r,n,s,a,o;return h(this,function(i){switch(i.label){case 0:return[4,w._getCleanedProfileHTML("https://www.threads.net/@",e,t)];case 1:if(a=null==(r=(s=i.sent()).match(/"user_id":"(\d+)"/))?void 0:r[1],o=null==(n=s.match(/"LSD",\[\],{"token":"(\w+)"},\d+\]/))?void 0:n[1],!a)return[2,w.getUserIDfromUsernameWithInstagram(e,t)];return!w.noUpdateLSD&&o&&(w.fbLSDToken=o,w.verbose&&console.debug("[fbLSDToken] UPDATED",w.fbLSDToken)),[2,a]}})});var D=this;this.getCurrentUserID=c(function(e){var t;return h(this,function(r){switch(r.label){case 0:if(D.userID)return D.verbose&&console.debug("[userID] USING",D.userID),[2,D.userID];if(!D.username)throw Error("username is not defined");r.label=1;case 1:return r.trys.push([1,3,,5]),[4,D.getUserIDfromUsername(D.username,e)];case 2:return D.userID=r.sent(),D.verbose&&console.debug("[userID] UPDATED",D.userID),[2,D.userID];case 3:return t=r.sent(),D.verbose&&console.error("[userID] Failed to fetch userID, Fallbacking to login",t),[4,D.login()];case 4:return[2,r.sent().userID];case 5:return[2]}})}),this._requestQuery=function(t,r,n){return Object.keys(r).forEach(function(e){return void 0===r[e]&&delete r[e]}),e.default.post(t,new URLSearchParams(r),l({httpAgent:v.httpAgent,httpsAgent:v.httpsAgent,headers:v._getDefaultHeaders()},n))},this._requestUserDataQuery=function(t,r,n){return Object.keys(r).forEach(function(e){return void 0===r[e]&&delete r[e]}),e.default.post(t,new URLSearchParams(r),l({httpAgent:v.httpAgent,httpsAgent:v.httpsAgent,headers:v._getDefaultUserDataHeaders()},n))},this._destructureFromUserIDQuerier=function(e){var t,r;return"string"==typeof e[0]&&"string"==typeof e[1]?(t=e[1],r=e[2]):(t=e[0],r=e[1]),{userID:t,options:r}};var y=this;this.getUserProfile=c(function(){var e,t,r,n,s,a,o=arguments;return h(this,function(i){switch(i.label){case 0:for(t=Array(e=o.length),r=0;r<e;r++)t[r]=o[r];return s=(n=y._destructureFromUserIDQuerier(t)).userID,a=n.options,y.verbose&&console.debug("[fbLSDToken] USING",y.fbLSDToken),[4,y._requestUserDataQuery("https://www.threads.net/api/graphql",{lsd:y.fbLSDToken,variables:JSON.stringify({userID:s}),doc_id:"23996318473300828"},a)];case 1:return[2,i.sent().data.data.userData.user]}})});var U=this;this.getUserProfileLoggedIn=c(function(e,t){var r,n,a;return h(this,function(i){switch(i.label){case 0:void 0===t&&(t={}),n=void 0,i.label=1;case 1:return i.trys.push([1,3,,4]),[4,U._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/users/"+e+"/info?is_prefetch=false&entry_point=profile&from_module=ProfileViewModel",t)];case 2:return n=i.sent().data,[3,4];case 3:return n=null==(a=i.sent().response)?void 0:a.data,[3,4];case 4:if((null==(r=n)?void 0:r.status)!=="ok")throw U.verbose&&console.log("[USER PROFILE] Failed to fetch",n),new o.ThreadsAPIError("Failed to fetch user profile: "+JSON.stringify(n),n);return[2,n]}})});var E=this;this.getUserProfileThreads=c(function(){var e,t,r,n,s,a,o,i,u=arguments;return h(this,function(c){switch(c.label){case 0:for(t=Array(e=u.length),r=0;r<e;r++)t[r]=u[r];return o=(a=E._destructureFromUserIDQuerier(t)).userID,i=a.options,E.verbose&&console.debug("[fbLSDToken] USING",E.fbLSDToken),[4,E._requestUserDataQuery("https://www.threads.net/api/graphql",{lsd:E.fbLSDToken,variables:JSON.stringify({userID:o}),doc_id:"6232751443445612"},i)];case 1:return[2,(null==(s=c.sent().data.data)?void 0:null==(n=s.mediaData)?void 0:n.threads)||[]]}})});var L=this;this.getUserProfileThreadsLoggedIn=c(function(e,t,r){var n,a,i;return h(this,function(u){switch(u.label){case 0:void 0===t&&(t=""),void 0===r&&(r={}),a=void 0,u.label=1;case 1:return u.trys.push([1,3,,4]),[4,L._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/"+e+"/profile/"+(t?"?max_id="+t:""),r)];case 2:return a=u.sent().data,[3,4];case 3:return a=null==(i=u.sent().response)?void 0:i.data,[3,4];case 4:if((null==(n=a)?void 0:n.status)!=="ok")throw L.verbose&&console.log("[USER THREADS] Failed to fetch",a),new o.ThreadsAPIError("Failed to fetch user threads: "+JSON.stringify(a),a);return[2,a]}})}),this._getDefaultRepliesHeaders=function(e){return l({},v._getUnAuthenticatedHeaders(),{Host:"www.threads.net",Accept:"*/*","Accept-Language":v.locale,"cache-control":"no-cache",Origin:"https://www.threads.net",Pragma:"no-cache","Sec-Fetch-Site":"same-origin","X-Asbd-id":"129477","X-FB-Friendly-Name":"BarcelonaProfileProfileRepliesTabQuery","X-FB-Lsd":v.fbLSDToken,"X-Ig-App-Id":"238260118697367"},e?{Referer:"https://www.threads.net/@"+e+"/replies"}:void 0)},this._requestRepliesQuery=function(t,r,n){return Object.keys(r).forEach(function(e){return void 0===r[e]&&delete r[e]}),e.default.post(t,new URLSearchParams(r),l({httpAgent:v.httpAgent,httpsAgent:v.httpsAgent,headers:v._getDefaultRepliesHeaders()},n))};var T=this;this.getUserProfileReplies=c(function(){var e,t,r,n,s,a,o,i=arguments;return h(this,function(u){switch(u.label){case 0:for(t=Array(e=i.length),r=0;r<e;r++)t[r]=i[r];return a=(s=T._destructureFromUserIDQuerier(t)).userID,o=s.options,T.verbose&&console.debug("[fbLSDToken] USING",T.fbLSDToken),[4,T._requestRepliesQuery("https://www.threads.net/api/graphql",{lsd:T.fbLSDToken,variables:JSON.stringify({userID:a}),doc_id:"6684830921547925"},o)];case 1:return[2,(null==(n=u.sent().data.data.mediaData)?void 0:n.threads)||[]]}})});var R=this;this.getUserProfileRepliesLoggedIn=c(function(e,t,r){var n,a,i;return h(this,function(u){switch(u.label){case 0:if(void 0===t&&(t=""),void 0===r&&(r={}),R.token)return[3,2];return[4,R.getToken()];case 1:u.sent(),u.label=2;case 2:if(!R.token)throw Error("Token not found");a=void 0,u.label=3;case 3:return u.trys.push([3,5,,6]),[4,R._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/"+e+"/profile/replies/"+(t?"?max_id="+t:""),r)];case 4:return a=u.sent().data,[3,6];case 5:return a=null==(i=u.sent().response)?void 0:i.data,[3,6];case 6:if((null==(n=a)?void 0:n.status)!=="ok")throw R.verbose&&console.log("[USER REPLIES] Failed to fetch",a),new o.ThreadsAPIError("Failed to fetch user replies: "+JSON.stringify(a),a);return[2,a]}})});var P=this;this.getUserFollowers=c(function(e,t,r){var n,a,i,u,c,d,p,f;return h(this,function(h){switch(h.label){case 0:a=(n=void 0===t?{}:t).maxID,i=n.query,c=void 0,d=new URLSearchParams(s.BASE_FOLLOW_PARAMS),a&&d.append("max_id",a),i&&d.append("query",i),h.label=1;case 1:return h.trys.push([1,3,,4]),[4,P._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/friendships/"+e+"/followers/?"+d.toString(),l({},r,{headers:l({"X-Ig-Nav-Chain":s.FOLLOW_NAV_CHAIN},null==(p=r)?void 0:p.headers)}))];case 2:return c=h.sent().data,[3,4];case 3:return c=null==(f=h.sent().response)?void 0:f.data,[3,4];case 4:if((null==(u=c)?void 0:u.status)!=="ok")throw P.verbose&&console.log("[USER FOLLOWERS] Failed to fetch",c),new o.ThreadsAPIError("Failed to fetch user followers: "+JSON.stringify(c),c);return[2,c]}})});var k=this;this.getUserFollowings=c(function(e,t,r){var n,a,i,u,c,d,p,f;return h(this,function(h){switch(h.label){case 0:a=(n=void 0===t?{}:t).maxID,i=n.query,c=void 0,d=new URLSearchParams(s.BASE_FOLLOW_PARAMS),a&&d.append("max_id",a),i&&d.append("query",i),h.label=1;case 1:return h.trys.push([1,3,,4]),[4,k._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/friendships/"+e+"/following/?"+d.toString(),l({},r,{headers:l({"X-Ig-Nav-Chain":s.FOLLOW_NAV_CHAIN},null==(p=r)?void 0:p.headers)}))];case 2:return c=h.sent().data,[3,4];case 3:return c=null==(f=h.sent().response)?void 0:f.data,[3,4];case 4:if((null==(u=c)?void 0:u.status)!=="ok")throw k.verbose&&console.log("[USER FOLLOWINGS] Failed to fetch",c),new o.ThreadsAPIError("Failed to fetch user followings: "+JSON.stringify(c),c);return[2,c]}})}),this.getPostIDfromThreadID=function(e){e=(e=(e=e.split("?")[0]).replace(/\s/g,"")).replace(/\//g,"");for(var t,r=0n,n=d(e);!(t=n()).done;){var s=t.value;r=64n*r+BigInt("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".indexOf(s))}return r.toString()},this.getPostIDfromURL=function(e){var t,r,n=null==e?void 0:e.split("?")[0];return(null==(t=n)?void 0:t.endsWith("/"))&&(n=n.slice(0,-1)),n=(null==(r=n)?void 0:r.split("/").pop())||"",v.getPostIDfromThreadID(n||"")};var O=this;this.getThreads=c(function(e,t){return h(this,function(r){switch(r.label){case 0:return O.verbose&&console.debug("[fbLSDToken] USING",O.fbLSDToken),[4,O._requestUserDataQuery("https://www.threads.net/api/graphql",{lsd:O.fbLSDToken,variables:JSON.stringify({postID:e}),doc_id:"5587632691339264"},t)];case 1:return[2,r.sent().data.data.data]}})});var N=this;this.getThreadsLoggedIn=c(function(e,t,r){var n,a,i;return h(this,function(u){switch(u.label){case 0:void 0===t&&(t=""),void 0===r&&(r={}),a=void 0,u.label=1;case 1:return u.trys.push([1,3,,4]),[4,N._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/"+e+"/replies/"+(t?"?paging_token="+t:""),r)];case 2:return a=u.sent().data,[3,4];case 3:return a=null==(i=u.sent().response)?void 0:i.data,[3,4];case 4:if((null==(n=a)?void 0:n.status)!=="ok")throw N.verbose&&console.log("[USER FEED] Failed to fetch",a),new o.ThreadsAPIError("Failed to fetch user feed: "+JSON.stringify(a),a);return[2,a]}})});var x=this;this.getThreadLikers=c(function(e,t){return h(this,function(r){switch(r.label){case 0:return x.verbose&&console.debug("[fbLSDToken] USING",x.fbLSDToken),[4,x._requestUserDataQuery("https://www.threads.net/api/graphql",{lsd:x.fbLSDToken,variables:JSON.stringify({mediaID:e}),doc_id:"9360915773983802"},t)];case 1:return[2,r.sent().data.data.likers]}})});var F=this;this.getTimeline=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:if(void 0===e&&(e=""),!F.token&&(!F.username||!F.password))throw Error("Username or password not set");return[4,F.getToken()];case 1:if(!n.sent())throw Error("Token not found");n.label=2;case 2:return n.trys.push([2,4,,5]),[4,F._requestQuery(""+s.BASE_API_URL+"/api/v1/feed/text_post_app_timeline/",{pagination_source:"text_post_feed_threads",max_id:e||void 0},l({},t,{headers:F._getAppHeaders()}))];case 3:return[2,n.sent().data];case 4:throw r=n.sent(),F.verbose&&console.log("[TIMELINE FETCH FAILED]",r.response.data),Error("Failed to fetch timeline");case 5:return[2]}})});var q=this;this._fetchAuthGetRequest=c(function(t,r){var n;return h(this,function(s){switch(s.label){case 0:return[4,q.getToken()];case 1:if(!s.sent())throw Error("Token not found");return[4,e.default.get(t,l({},r,{headers:l({},q._getInstaHeaders(),null==(n=r)?void 0:n.headers)}))];case 2:return[2,s.sent()]}})});var B=this;this._toggleAuthPostRequest=c(function(t,r,n){return h(this,function(s){switch(s.label){case 0:return[4,B.getToken()];case 1:if(!s.sent())throw Error("Token not found");return[4,e.default.post(t,r?new URLSearchParams(r):void 0,l({},n,{httpAgent:B.httpAgent,httpsAgent:B.httpsAgent,headers:B._getDefaultHeaders()}))];case 2:return[2,s.sent()]}})});var G=this;this.like=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,G.getCurrentUserID()];case 1:return r=n.sent(),[4,G._toggleAuthPostRequest(s.BASE_API_URL+"/api/v1/media/"+e+"_"+r+"/like/",void 0,t)];case 2:return[2,n.sent().data]}})});var C=this;this.unlike=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,C.getCurrentUserID()];case 1:return r=n.sent(),[4,C._toggleAuthPostRequest(s.BASE_API_URL+"/api/v1/media/"+e+"_"+r+"/unlike/",void 0,t)];case 2:return[2,n.sent().data]}})});var H=this;this.follow=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,H._toggleAuthPostRequest(s.BASE_API_URL+"/api/v1/friendships/create/"+e+"/",void 0,t)];case 1:return r=n.sent(),H.verbose&&console.debug("[FOLLOW]",r.data),[2,r.data]}})});var M=this;this.unfollow=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,M._toggleAuthPostRequest(s.BASE_API_URL+"/api/v1/friendships/destroy/"+e+"/",void 0,t)];case 1:return r=n.sent(),M.verbose&&console.debug("[UNFOLLOW]",r.data),[2,r.data]}})});var J=this;this.repost=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,J._toggleAuthPostRequest(""+s.BASE_API_URL+"/api/v1/repost/create_repost/",{media_id:e},t)];case 1:return r=n.sent(),J.verbose&&console.debug("[REPOST]",r.data),[2,r.data]}})});var X=this;this.unrepost=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,X._toggleAuthPostRequest(""+s.BASE_API_URL+"/api/v1/repost/delete_text_app_repost/",{original_media_id:e},t)];case 1:return r=n.sent(),X.verbose&&console.debug("[UNREPOST]",r.data),[2,r.data]}})});var W=this;this.mute=c(function(e,t){var r,n,a,o;return h(this,function(i){switch(i.label){case 0:return r=""+s.BASE_API_URL+"/api/v1/friendships/mute_posts_or_story_from_follow/",n={_uid:W.userID,_uuid:W.deviceID,container_module:"ig_text_feed_timeline"},e.postID&&(n.media_id=String(e.postID)),n.target_posts_author_id=String(e.userID),a={signed_body:"SIGNATURE."+encodeURIComponent(JSON.stringify(n))},[4,W._toggleAuthPostRequest(r,a,t)];case 1:return o=i.sent(),W.verbose&&console.debug("[MUTE]",o.data),[2,o.data]}})});var j=this;this.unmute=c(function(e,t){var r,n,a,o;return h(this,function(i){switch(i.label){case 0:return r=""+s.BASE_API_URL+"/api/v1/friendships/unmute_posts_or_story_from_follow/",n={_uid:j.userID,_uuid:j.deviceID,container_module:"ig_text_feed_timeline"},e.postID&&(n.media_id=String(e.postID)),n.target_posts_author_id=String(e.userID),a={signed_body:"SIGNATURE."+encodeURIComponent(JSON.stringify(n))},[4,j._toggleAuthPostRequest(r,a,t)];case 1:return o=i.sent(),j.verbose&&console.debug("[UNMUTE]",o.data),[2,o.data]}})});var Q=this;this.block=c(function(e,t){var r,n,a;return h(this,function(o){switch(o.label){case 0:return r=s.BASE_API_URL+"/api/v1/friendships/block/"+e+"/",n={signed_body:"SIGNATURE."+encodeURIComponent(JSON.stringify({surface:"ig_text_feed_timeline",is_auto_block_enabled:!0,user_id:e,_uid:Q.userID,_uuid:Q.deviceID}))},[4,Q._toggleAuthPostRequest(r,n,t)];case 1:return a=o.sent(),Q.verbose&&console.debug("[MUTE]",a.data),[2,a.data]}})});var z=this;this.unblock=c(function(e,t){var r,n,a;return h(this,function(o){switch(o.label){case 0:return r=s.BASE_API_URL+"/api/v1/friendships/unblock/"+e+"/",n={signed_body:"SIGNATURE."+encodeURIComponent(JSON.stringify({user_id:e,_uid:z.userID,_uuid:z.deviceID,container_module:"ig_text_feed_timeline"}))},[4,z._toggleAuthPostRequest(r,n,t)];case 1:return a=o.sent(),z.verbose&&console.debug("[MUTE]",a.data),[2,a.data]}})});var V=this;this.getNotifications=c(function(e,t,r){var n,a,i,u,c;return h(this,function(l){switch(l.label){case 0:void 0===r&&(r={}),a={feed_type:"all",mark_as_seen:!1,timezone_offset:-25200,timezone_name:"America%2FLos_Angeles"},e&&(a.selected_filters=e),t&&(a.max_id=t.maxID,a.pagination_first_record_timestamp=t.firstRecordTimestamp),i=Object.entries(a).map(function(e){return e[0]+"="+e[1]}).join("&"),u=void 0,l.label=1;case 1:return l.trys.push([1,3,,4]),[4,V._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/text_app_notifications/?"+i,r)];case 2:return u=l.sent().data,[3,4];case 3:return u=null==(c=l.sent().response)?void 0:c.data,[3,4];case 4:if((null==(n=u)?void 0:n.status)!=="ok")throw V.verbose&&console.log("[NOTIFICATIONS] Failed to fetch",u),new o.ThreadsAPIError("Failed to fetch notifications: "+JSON.stringify(u),u);return[2,u]}})});var K=this;this.setNotificationsSeen=c(function(e){var t,r,n;return h(this,function(a){switch(a.label){case 0:return t=""+s.BASE_API_URL+"/api/v1/text_feed/text_app_inbox_seen/",r={_uuid:""+K.userID},[4,K._toggleAuthPostRequest(t,r,e)];case 1:return n=a.sent(),K.verbose&&console.debug("[SET_NOTIFICATIONS_SEEN]",n.data),[2,n.data]}})});var Y=this;this.searchUsers=c(function(e,t,r){var n,a,i,u;return h(this,function(c){switch(c.label){case 0:void 0===t&&(t=30),void 0===r&&(r={}),a=Object.entries({q:e,count:t}).map(function(e){return e[0]+"="+e[1]}).join("&"),i=void 0,c.label=1;case 1:return c.trys.push([1,3,,4]),[4,Y._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/users/search/?"+a,r)];case 2:return i=c.sent().data,[3,4];case 3:return i=null==(u=c.sent().response)?void 0:u.data,[3,4];case 4:if((null==(n=i)?void 0:n.status)!=="ok")throw Y.verbose&&console.log("[USER SEARCH] Failed to fetch",i),new o.ThreadsAPIError("Failed to fetch user search results: "+JSON.stringify(i),i);return[2,i]}})});var $=this;this.getRecommendedUsers=c(function(e,t){var r,n,a;return h(this,function(i){switch(i.label){case 0:void 0===e&&(e=""),void 0===t&&(t={}),n=void 0,i.label=1;case 1:return i.trys.push([1,3,,4]),[4,$._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/recommended_users/?"+(e?"?max_id="+e:""),t)];case 2:return n=i.sent().data,[3,4];case 3:return n=null==(a=i.sent().response)?void 0:a.data,[3,4];case 4:if((null==(r=n)?void 0:r.status)!=="ok")throw $.verbose&&console.log("[RECOMMENDED] Failed to fetch",n),new o.ThreadsAPIError("Failed to fetch recommended users: "+JSON.stringify(n),n);return[2,n]}})});var Z=this;this.getToken=c(function(){return h(this,function(e){switch(e.label){case 0:if(Z.token)return Z.verbose&&console.debug("[token] USING",Z.token),[2,Z.token];if(!Z.username||!Z.password)throw Error("Username and password are required");return[4,Z.login()];case 1:return e.sent(),[2,Z.token]}})}),this._lastUploadID=0,this._nextUploadID=function(){var e=Date.now(),t=v._lastUploadID;return(v._lastUploadID=e<t?t+1:e).toString()},this._createUploadMetadata=function(e){var t;return void 0===e&&(e=v._nextUploadID()),{upload_id:e,source_type:"4",timezone_offset:(null!=(t=v._timezoneOffset)?t:v._timezoneOffset=-(60*new Date().getTimezoneOffset())).toString(),device:v.device}};var ee=this;this.publish=c(function(t){var r,n,a,o,i,u,c,p,f,g,_,v;return h(this,function(h){switch(h.label){case 0:if(r="string"==typeof t?{text:t}:t,!ee.token&&(!ee.username||!ee.password))throw Error("Username or password not set");return[4,ee.getCurrentUserID()];case 1:if(!(n=h.sent()))throw Error("User ID not found");return[4,ee.getToken()];case 2:if(!h.sent())throw Error("Token not found");if(o=l({},ee._createUploadMetadata(),{text_post_app_info:{reply_control:s.REPLY_CONTROL_OPTIONS[null!=(a=r.replyControl)?a:"everyone"]},_uid:n,device_id:ee.deviceID,caption:r.text||""}),i=s.POST_URL,!(u=r.attachment)&&("image"in r&&r.image?u={image:r.image}:"url"in r&&r.url&&(u={url:r.url})),!u)return[3,9];if(!u.url)return[3,3];return o.text_post_app_info.link_attachment_url=u.url,[3,9];case 3:if(!u.image)return[3,5];return i=s.POST_WITH_IMAGE_URL,[4,ee.uploadImage(u.image,o.upload_id)];case 4:return h.sent(),o.scene_type=null,o.scene_capture_type="",[3,9];case 5:if(!u.sidecar)return[3,9];i=s.POST_WITH_SIDECAR_URL,o.client_sidecar_id=o.upload_id,o.children_metadata=[],c=d(u.sidecar),h.label=6;case 6:if((p=c()).done)return[3,9];return f=p.value,[4,ee.uploadImage(f)];case 7:g=h.sent().upload_id,o.children_metadata.push(l({},ee._createUploadMetadata(g),{scene_type:null,scene_capture_type:""})),h.label=8;case 8:return[3,6];case 9:return r.parentPostID&&(o.text_post_app_info.reply_id=r.parentPostID.replace(/_\d+$/,"")),r.quotedPostID&&(o.text_post_app_info.quoted_post_id=r.quotedPostID.replace(/_\d+$/,"")),i===s.POST_URL&&(o.publish_mode="text_post"),_="signed_body=SIGNATURE."+encodeURIComponent(JSON.stringify(o)),[4,e.default.post(i,_,{httpAgent:ee.httpAgent,httpsAgent:ee.httpsAgent,headers:ee._getAppHeaders(),timeout:6e4})];case 10:if(v=h.sent(),ee.verbose&&console.debug("[PUBLISH]",v.data),"ok"===v.data.status)return[2,v.data];return[2,void 0]}})});var et=this;this.delete=c(function(t,r){var n,a;return h(this,function(o){switch(o.label){case 0:return n=s.BASE_API_URL+"/api/v1/media/"+t+"/delete/",a="signed_body=SIGNATURE."+encodeURIComponent(JSON.stringify({media_id:t,_uid:et.userID,_uuid:et.deviceID})),[4,e.default.post(n,a,l({httpAgent:et.httpAgent,httpsAgent:et.httpsAgent,headers:et._getAppHeaders(),timeout:6e4},r))];case 1:if("ok"===o.sent().data.status)return[2,!0];return[2,!1]}})});var er=this;this.publishWithImage=c(function(e,t){return h(this,function(r){return[2,er.publish({text:e,image:t})]})});var en=this;if(this.uploadImage=c(function(t,s){var a,o,i,u,c,d,p,f,g,_,v;return h(this,function(h){switch(h.label){case 0:if(void 0===s&&(s=en._nextUploadID()),o="https://www.instagram.com/rupload_igphoto/"+(a=s+"_0_"+Math.floor(9e9*Math.random()+1e9)),!("string"==typeof t||"path"in t))return[3,6];if((c="string"==typeof t?t:t.path).startsWith("http"))return[3,3];return[4,Promise.resolve().then(function(){return require("fs")})];case 1:return[4,h.sent().promises.readFile(c)];case 2:return i=h.sent(),u=r.lookup(c)||"application/octet-stream",[3,5];case 3:return[4,e.default.get(c,{responseType:"arraybuffer"})];case 4:d=h.sent(),i=Buffer.from(d.data,"binary"),u=d.headers["content-type"],h.label=5;case 5:return[3,7];case 6:i=t.data,u=(t.type.includes("/")?t.type:r.lookup(t.type))||"application/octet-stream",h.label=7;case 7:p={upload_id:s,media_type:"1",sticker_burnin_params:JSON.stringify([]),image_compression:JSON.stringify({lib_name:"moz",lib_version:"3.1.m",quality:"80"}),xsharing_user_ids:JSON.stringify([]),retry_context:JSON.stringify({num_step_auto_retry:"0",num_reupload:"0",num_step_manual_retry:"0"}),"IG-FB-Xpost-entry-point-v2":"feed"},f=i.length,g=l({},en._getDefaultHeaders(),{"Content-Type":"application/octet-stream",X_FB_PHOTO_WATERFALL_ID:(0,n.v4)(),"X-Entity-Type":void 0!==u?"image/"+u:"image/jpeg",Offset:"0","X-Instagram-Rupload-Params":JSON.stringify(p),"X-Entity-Name":a,"X-Entity-Length":f.toString(),"Content-Length":f.toString(),"Accept-Encoding":"gzip"}),en.verbose&&console.log("[UPLOAD_IMAGE] Uploading "+f.toLocaleString()+"b as "+s+"..."),h.label=8;case 8:return h.trys.push([8,10,,11]),[4,e.default.post(o,i,{httpAgent:en.httpAgent,headers:g,timeout:6e4})];case 9:return _=h.sent().data,en.verbose&&console.log("[UPLOAD_IMAGE] SUCCESS",_),[2,_];case 10:throw v=h.sent(),en.verbose&&console.log("[UPLOAD_IMAGE] FAILED",v.response.data),Error("Upload image failed");case 11:return[2]}})}),(null==i?void 0:i.token)&&(this.token=i.token),(null==i?void 0:i.fbLSDToken)&&(this.fbLSDToken=i.fbLSDToken),this.noUpdateToken=!!(null==i?void 0:i.noUpdateToken),this.noUpdateLSD=!!(null==i?void 0:i.noUpdateLSD),this.verbose=(null==i?void 0:i.verbose)||!1,this.httpAgent=null==i?void 0:i.httpAgent,this.httpsAgent=null==i?void 0:i.httpsAgent,this.username=null!=(u=null==i?void 0:i.username)?u:process.env.THREADS_USERNAME,this.password=null!=(f=null==i?void 0:i.password)?f:process.env.THREADS_PASSWORD,this.deviceID=null!=(_=null!=(g=null==i?void 0:i.deviceID)?g:process.env.THREADS_DEVICE_ID)?_:"",this.deviceID||(this.deviceID="android-"+(1e24*Math.random()).toString(36),console.warn("⚠️ WARNING: deviceID not provided, automatically generating device id '"+this.deviceID+"'","Please save this device id and use it for future uses to prevent login issues.","You can provide this device id by passing it to the constructor or setting the THREADS_DEVICE_ID environment variable (.env file)")),this.device=null==i?void 0:i.device,this.userID=null==i?void 0:i.userID,null==i?void 0:i.locale)this.locale=i.locale;else{var es=Intl.DateTimeFormat().resolvedOptions().locale;this.locale=es}this.maxRetries=(null==i?void 0:i.maxRetries)||this.maxRetries}return i.prototype.sign=function(e){var r="object"==typeof e?JSON.stringify(e):e;return{ig_sig_key_version:4,signed_body:t.createHmac("sha256",s.SIGNATURE_KEY).update(r).digest("hex")+"."+r}},i}(),g=f;
"use strict";!function(e,t){for(var r in t)Object.defineProperty(e,r,{enumerable:!0,get:t[r]})}(exports,{DEFAULT_DEVICE:function(){return f},ThreadsAPI:function(){return g},default:function(){return _}});var e=require("axios"),t=require("crypto"),r=require("mrmime"),n=require("uuid"),s=require("./constants"),a=require("./dynamic-data"),o=require("./error");function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function u(e,t,r,n,s,a,o){try{var i=e[a](o),u=i.value}catch(e){r(e);return}i.done?t(u):Promise.resolve(u).then(n,s)}function c(e){return function(){var t=this,r=arguments;return new Promise(function(n,s){var a=e.apply(t,r);function o(e){u(a,n,s,o,i,"next",e)}function i(e){u(a,n,s,o,i,"throw",e)}o(void 0)})}}function l(){return(l=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function d(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return i(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);if("Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r)return Array.from(r);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return i(e,t)}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function h(e,t){var r,n,s,a,o={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(a){return function(i){return function(a){if(r)throw TypeError("Generator is already executing.");for(;o;)try{if(r=1,n&&(s=2&a[0]?n.return:a[0]?n.throw||((s=n.return)&&s.call(n),0):n.next)&&!(s=s.call(n,a[1])).done)return s;switch(n=0,s&&(a=[2&a[0],s.value]),a[0]){case 0:case 1:s=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,n=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(s=(s=o.trys).length>0&&s[s.length-1])&&(6===a[0]||2===a[0])){o=0;continue}if(3===a[0]&&(!s||a[1]>s[0]&&a[1]<s[3])){o.label=a[1];break}if(6===a[0]&&o.label<s[1]){o.label=s[1],s=a;break}if(s&&o.label<s[2]){o.label=s[2],o.ops.push(a);break}s[2]&&o.ops.pop(),o.trys.pop();continue}a=t.call(e,o)}catch(e){a=[6,e],n=0}finally{r=s=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,i])}}}var p=function(){var e="android-"+(1e24*Math.random()).toString(36);return console.warn("⚠️ WARNING: deviceID not provided, automatically generating device id '"+e+"'","Please save this device id and use it for future uses to prevent login issues.","You can provide this device id by passing it to the constructor or setting the THREADS_DEVICE_ID environment variable (.env file)"),e},f={manufacturer:"OnePlus",model:"ONEPLUS+A3010",os_version:25,os_release:"7.1.1"},g=function(){"use strict";function i(i){void 0===i&&(i={});var u,g,_,v,b,m,I,A=this,S=this;this.syncLoginExperiments=c(function(){var t,r,a;return h(this,function(o){switch(o.label){case 0:r={id:t=(0,n.v4)(),experiments:s.LOGIN_EXPERIMENTS},o.label=1;case 1:return o.trys.push([1,3,,4]),[4,e.default.post(""+s.BASE_API_URL+"/api/v1/qe/sync/",S.sign(r),{httpAgent:S.httpAgent,httpsAgent:S.httpsAgent,headers:l({},S._getAppHeaders(),{Authorization:void 0,"Sec-Fetch-Site":"same-origin","X-DEVICE-ID":t})})];case 2:return[2,o.sent()];case 3:throw a=o.sent(),S.verbose&&console.log("[SYNC LOGIN EXPERIMENT FAILED]",a.response.data),Error("Sync login experiment failed");case 4:return[2]}})});var w=this;this.encryptPassword=c(function(e){var r,n,s,a,o,i,u,c,l,d,p;return h(this,function(h){switch(h.label){case 0:return r=t.randomBytes(32),n=t.randomBytes(12),[4,w.syncLoginExperiments()];case 1:return s=h.sent().headers,w.verbose&&console.log("[SYNC LOGIN EXPERIMENT HEADERS]",JSON.stringify(s)),a=s["ig-set-password-encryption-key-id"],o=s["ig-set-password-encryption-pub-key"],i=t.publicEncrypt({key:Buffer.from(o||"","base64").toString(),padding:t.constants.RSA_PKCS1_PADDING},r),u=t.createCipheriv("aes-256-gcm",r,n),c=Math.floor(Date.now()/1e3).toString(),u.setAAD(Buffer.from(c)),l=Buffer.concat([u.update(e,"utf8"),u.final()]),(d=Buffer.alloc(2,0)).writeInt16LE(i.byteLength,0),p=u.getAuthTag(),[2,{time:c,password:Buffer.concat([Buffer.from([1,a||0]),n,d,i,p,l]).toString("base64")}]}})});var D=this;this.login=c(function(){var t,r,n,a;return h(this,function(o){switch(o.label){case 0:t=function(){var e;return h(this,function(t){switch(t.label){case 0:return t.trys.push([0,1,,3]),[2,{v:n()}];case 1:return t.sent(),D.verbose&&console.error("[LOGIN] Failed to login, retrying... ("+(r+1)+"/"+D.maxRetries+")"),e=1e3*Math.pow(2,r),[4,new Promise(function(t){return setTimeout(t,e)})];case 2:return t.sent(),r++,[3,3];case 3:return[2]}})},r=0,n=c(function(){var t,r,n,a,o,i,u,c;return h(this,function(l){switch(l.label){case 0:return D.verbose&&console.log("[LOGIN] Logging in..."),[4,D.encryptPassword(D.password)];case 1:return r=encodeURIComponent(JSON.stringify({client_input_params:{password:"#PWD_INSTAGRAM:4:"+(t=l.sent()).time+":"+t.password,contact_point:D.username,device_id:D.deviceID},server_params:{credential_type:"password",device_id:D.deviceID}})),n=encodeURIComponent(JSON.stringify({bloks_version:s.BLOKS_VERSION,styles_id:"instagram"})),a={httpAgent:D.httpAgent,httpsAgent:D.httpsAgent,method:"POST",headers:D._getAppHeaders(),responseType:"text",data:"params="+r+"&bk_client_context="+n+"&bloks_versioning_id="+s.BLOKS_VERSION},[4,(0,e.default)(""+s.BASE_API_URL+"/api/v1/bloks/apps/com.bloks.www.bloks.caa.login.async.send_login_request/",a)];case 2:o=JSON.stringify((o=l.sent().data).replaceAll("\\","")),D.verbose&&console.log("[LOGIN] Cleaned output",o);try{return u=o.split("Bearer IGT:2:")[1].split('"')[0].replaceAll("\\",""),c=null==(i=o.match(/pk_id(.{18})/))?void 0:i[1].replaceAll(/[\\":]/g,""),D.noUpdateToken||(D.verbose&&console.debug("[token] UPDATED",u),D.token=u),D.userID=c,D.verbose&&console.debug("[userID] UPDATED",D.userID),[2,{token:u,userID:c}]}catch(e){throw D.verbose&&console.error("[LOGIN] Failed to login",e),Error("Login Failed")}return[2]}})}),o.label=1;case 1:if(!(r<D.maxRetries))return[3,3];return[5,function(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(t())];case 2:var i;if("object"==((i=a=o.sent())&&"undefined"!=typeof Symbol&&i.constructor===Symbol?"symbol":typeof i))return[2,a.v];return[3,1];case 3:throw Error("[LOGIN] Failed to login after "+D.maxRetries+" retries")}})}),this._getUnAuthenticatedHeaders=function(){return{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.199 Safari/537.36","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}},this._getDefaultUserDataHeaders=function(e){return l({},A._getUnAuthenticatedHeaders(),{Host:"www.threads.net",Accept:"*/*","Accept-Language":A.locale,"cache-control":"no-cache",Origin:"https://www.threads.net",Pragma:"no-cache","Sec-Fetch-Site":"same-origin","X-Asbd-id":"129477","X-FB-Friendly-Name":"BarcelonaProfileRootQuery","X-FB-Lsd":A.fbLSDToken,"X-Ig-App-Id":"238260118697367"},e?{Referer:"https://www.threads.net/@"+e}:void 0)},this._getAppHeaders=function(){return l({"User-Agent":"Barcelona "+a.LATEST_ANDROID_APP_VERSION+" Android","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},A.token&&{Authorization:"Bearer IGT:2:"+A.token})},this._getInstaHeaders=function(){return l({},A._getAppHeaders(),{"X-Bloks-Is-Layout-Rtl":"false","X-Bloks-Version-Id":s.BLOKS_VERSION,"X-Ig-Android-Id":A.deviceID,"X-Ig-App-Id":s.IG_APP_ID,"Accept-Language":A.locale||"en-US"},A.userID&&{"Ig-U-Ds-User-Id":A.userID,"Ig-Intended-User-Id":A.userID},A.locale&&{"X-Ig-App-Locale":A.locale.replace("-","_"),"X-Ig-Device-Locale":A.locale.replace("-","_"),"X-Ig-Mapped-Locale":A.locale.replace("-","_")})},this._getDefaultHeaders=function(e){return l({},A._getAppHeaders(),{authority:"www.threads.net",accept:"*/*","accept-language":A.locale,"cache-control":"no-cache",origin:"https://www.threads.net",pragma:"no-cache","Sec-Fetch-Site":"same-origin","x-asbd-id":"129477","x-fb-lsd":A.fbLSDToken,"x-ig-app-id":"238260118697367"},e?{referer:"https://www.threads.net/@"+e}:void 0)};var y=this;this._getCleanedProfileHTML=c(function(t,r,n){return h(this,function(s){switch(s.label){case 0:return[4,e.default.get(""+t+r,l({},n,{httpAgent:y.httpAgent,httpsAgent:y.httpsAgent,headers:l({},y._getDefaultHeaders(r),{accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","accept-language":"ko,en;q=0.9,ko-KR;q=0.8,ja;q=0.7",Authorization:void 0,referer:"https://www.instagram.com/","sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"cross-site","sec-fetch-user":"?1","upgrade-insecure-requests":"1","x-asbd-id":void 0,"x-fb-lsd":void 0,"x-ig-app-id":void 0})}))];case 1:return[2,(0,s.sent().data).replace(/\s/g,"").replace(/\n/g,"")]}})});var E=this;this.getUserIDfromUsernameWithInstagram=c(function(e,t){var r,n,s,a,o;return h(this,function(i){switch(i.label){case 0:return[4,E._getCleanedProfileHTML("https://www.instagram.com/",e,t)];case 1:return a=null==(r=(s=i.sent()).match(/"user_id":"(\d+)",/))?void 0:r[1],o=null==(n=s.match(/"LSD",\[\],{"token":"(\w+)"},\d+\]/))?void 0:n[1],!E.noUpdateLSD&&o&&(E.fbLSDToken=o,E.verbose&&console.debug("[fbLSDToken] UPDATED",E.fbLSDToken)),[2,a]}})});var U=this;this.getUserIDfromUsername=c(function(e,t){var r,n,s,a,o;return h(this,function(i){switch(i.label){case 0:return[4,U._getCleanedProfileHTML("https://www.threads.net/@",e,t)];case 1:if(a=null==(r=(s=i.sent()).match(/"user_id":"(\d+)"/))?void 0:r[1],o=null==(n=s.match(/"LSD",\[\],{"token":"(\w+)"},\d+\]/))?void 0:n[1],!a)return[2,U.getUserIDfromUsernameWithInstagram(e,t)];return!U.noUpdateLSD&&o&&(U.fbLSDToken=o,U.verbose&&console.debug("[fbLSDToken] UPDATED",U.fbLSDToken)),[2,a]}})});var L=this;this.getCurrentUserID=c(function(e){var t;return h(this,function(r){switch(r.label){case 0:if(L.userID)return L.verbose&&console.debug("[userID] USING",L.userID),[2,L.userID];if(!L.username)throw Error("username is not defined");r.label=1;case 1:return r.trys.push([1,3,,5]),[4,L.getUserIDfromUsername(L.username,e)];case 2:return L.userID=r.sent(),L.verbose&&console.debug("[userID] UPDATED",L.userID),[2,L.userID];case 3:return t=r.sent(),L.verbose&&console.error("[userID] Failed to fetch userID, Fallbacking to login",t),[4,L.login()];case 4:return[2,r.sent().userID];case 5:return[2]}})}),this._requestQuery=function(t,r,n){return Object.keys(r).forEach(function(e){return void 0===r[e]&&delete r[e]}),e.default.post(t,new URLSearchParams(r),l({httpAgent:A.httpAgent,httpsAgent:A.httpsAgent,headers:A._getDefaultHeaders()},n))},this._requestUserDataQuery=function(t,r,n){return Object.keys(r).forEach(function(e){return void 0===r[e]&&delete r[e]}),e.default.post(t,new URLSearchParams(r),l({httpAgent:A.httpAgent,httpsAgent:A.httpsAgent,headers:A._getDefaultUserDataHeaders()},n))},this._destructureFromUserIDQuerier=function(e){var t,r;return"string"==typeof e[0]&&"string"==typeof e[1]?(t=e[1],r=e[2]):(t=e[0],r=e[1]),{userID:t,options:r}};var T=this;this.getUserProfile=c(function(){var e,t,r,n,s,a,o=arguments;return h(this,function(i){switch(i.label){case 0:for(t=Array(e=o.length),r=0;r<e;r++)t[r]=o[r];return s=(n=T._destructureFromUserIDQuerier(t)).userID,a=n.options,T.verbose&&console.debug("[fbLSDToken] USING",T.fbLSDToken),[4,T._requestUserDataQuery("https://www.threads.net/api/graphql",{lsd:T.fbLSDToken,variables:JSON.stringify({userID:s}),doc_id:"23996318473300828"},a)];case 1:return[2,i.sent().data.data.userData.user]}})});var R=this;this.getUserProfileLoggedIn=c(function(e,t){var r,n,a;return h(this,function(i){switch(i.label){case 0:void 0===t&&(t={}),n=void 0,i.label=1;case 1:return i.trys.push([1,3,,4]),[4,R._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/users/"+e+"/info?is_prefetch=false&entry_point=profile&from_module=ProfileViewModel",t)];case 2:return n=i.sent().data,[3,4];case 3:return n=null==(a=i.sent().response)?void 0:a.data,[3,4];case 4:if((null==(r=n)?void 0:r.status)!=="ok")throw R.verbose&&console.log("[USER PROFILE] Failed to fetch",n),new o.ThreadsAPIError("Failed to fetch user profile: "+JSON.stringify(n),n);return[2,n]}})});var P=this;this.getUserProfileThreads=c(function(){var e,t,r,n,s,a,o,i,u=arguments;return h(this,function(c){switch(c.label){case 0:for(t=Array(e=u.length),r=0;r<e;r++)t[r]=u[r];return o=(a=P._destructureFromUserIDQuerier(t)).userID,i=a.options,P.verbose&&console.debug("[fbLSDToken] USING",P.fbLSDToken),[4,P._requestUserDataQuery("https://www.threads.net/api/graphql",{lsd:P.fbLSDToken,variables:JSON.stringify({userID:o}),doc_id:"6232751443445612"},i)];case 1:return[2,(null==(s=c.sent().data.data)?void 0:null==(n=s.mediaData)?void 0:n.threads)||[]]}})});var k=this;this.getUserProfileThreadsLoggedIn=c(function(e,t,r){var n,a,i;return h(this,function(u){switch(u.label){case 0:void 0===t&&(t=""),void 0===r&&(r={}),a=void 0,u.label=1;case 1:return u.trys.push([1,3,,4]),[4,k._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/"+e+"/profile/"+(t?"?max_id="+t:""),r)];case 2:return a=u.sent().data,[3,4];case 3:return a=null==(i=u.sent().response)?void 0:i.data,[3,4];case 4:if((null==(n=a)?void 0:n.status)!=="ok")throw k.verbose&&console.log("[USER THREADS] Failed to fetch",a),new o.ThreadsAPIError("Failed to fetch user threads: "+JSON.stringify(a),a);return[2,a]}})}),this._getDefaultRepliesHeaders=function(e){return l({},A._getUnAuthenticatedHeaders(),{Host:"www.threads.net",Accept:"*/*","Accept-Language":A.locale,"cache-control":"no-cache",Origin:"https://www.threads.net",Pragma:"no-cache","Sec-Fetch-Site":"same-origin","X-Asbd-id":"129477","X-FB-Friendly-Name":"BarcelonaProfileProfileRepliesTabQuery","X-FB-Lsd":A.fbLSDToken,"X-Ig-App-Id":"238260118697367"},e?{Referer:"https://www.threads.net/@"+e+"/replies"}:void 0)},this._requestRepliesQuery=function(t,r,n){return Object.keys(r).forEach(function(e){return void 0===r[e]&&delete r[e]}),e.default.post(t,new URLSearchParams(r),l({httpAgent:A.httpAgent,httpsAgent:A.httpsAgent,headers:A._getDefaultRepliesHeaders()},n))};var O=this;this.getUserProfileReplies=c(function(){var e,t,r,n,s,a,o,i=arguments;return h(this,function(u){switch(u.label){case 0:for(t=Array(e=i.length),r=0;r<e;r++)t[r]=i[r];return a=(s=O._destructureFromUserIDQuerier(t)).userID,o=s.options,O.verbose&&console.debug("[fbLSDToken] USING",O.fbLSDToken),[4,O._requestRepliesQuery("https://www.threads.net/api/graphql",{lsd:O.fbLSDToken,variables:JSON.stringify({userID:a}),doc_id:"6684830921547925"},o)];case 1:return[2,(null==(n=u.sent().data.data.mediaData)?void 0:n.threads)||[]]}})});var N=this;this.getUserProfileRepliesLoggedIn=c(function(e,t,r){var n,a,i;return h(this,function(u){switch(u.label){case 0:if(void 0===t&&(t=""),void 0===r&&(r={}),N.token)return[3,2];return[4,N.getToken()];case 1:u.sent(),u.label=2;case 2:if(!N.token)throw Error("Token not found");a=void 0,u.label=3;case 3:return u.trys.push([3,5,,6]),[4,N._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/"+e+"/profile/replies/"+(t?"?max_id="+t:""),r)];case 4:return a=u.sent().data,[3,6];case 5:return a=null==(i=u.sent().response)?void 0:i.data,[3,6];case 6:if((null==(n=a)?void 0:n.status)!=="ok")throw N.verbose&&console.log("[USER REPLIES] Failed to fetch",a),new o.ThreadsAPIError("Failed to fetch user replies: "+JSON.stringify(a),a);return[2,a]}})});var x=this;this.getUserFollowers=c(function(e,t,r){var n,a,i,u,c,d,p,f;return h(this,function(h){switch(h.label){case 0:a=(n=void 0===t?{}:t).maxID,i=n.query,c=void 0,d=new URLSearchParams(s.BASE_FOLLOW_PARAMS),a&&d.append("max_id",a),i&&d.append("query",i),h.label=1;case 1:return h.trys.push([1,3,,4]),[4,x._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/friendships/"+e+"/followers/?"+d.toString(),l({},r,{headers:l({"X-Ig-Nav-Chain":s.FOLLOW_NAV_CHAIN},null==(p=r)?void 0:p.headers)}))];case 2:return c=h.sent().data,[3,4];case 3:return c=null==(f=h.sent().response)?void 0:f.data,[3,4];case 4:if((null==(u=c)?void 0:u.status)!=="ok")throw x.verbose&&console.log("[USER FOLLOWERS] Failed to fetch",c),new o.ThreadsAPIError("Failed to fetch user followers: "+JSON.stringify(c),c);return[2,c]}})});var F=this;this.getUserFollowings=c(function(e,t,r){var n,a,i,u,c,d,p,f;return h(this,function(h){switch(h.label){case 0:a=(n=void 0===t?{}:t).maxID,i=n.query,c=void 0,d=new URLSearchParams(s.BASE_FOLLOW_PARAMS),a&&d.append("max_id",a),i&&d.append("query",i),h.label=1;case 1:return h.trys.push([1,3,,4]),[4,F._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/friendships/"+e+"/following/?"+d.toString(),l({},r,{headers:l({"X-Ig-Nav-Chain":s.FOLLOW_NAV_CHAIN},null==(p=r)?void 0:p.headers)}))];case 2:return c=h.sent().data,[3,4];case 3:return c=null==(f=h.sent().response)?void 0:f.data,[3,4];case 4:if((null==(u=c)?void 0:u.status)!=="ok")throw F.verbose&&console.log("[USER FOLLOWINGS] Failed to fetch",c),new o.ThreadsAPIError("Failed to fetch user followings: "+JSON.stringify(c),c);return[2,c]}})}),this.getPostIDfromThreadID=function(e){e=(e=(e=e.split("?")[0]).replace(/\s/g,"")).replace(/\//g,"");for(var t,r=0n,n=d(e);!(t=n()).done;){var s=t.value;r=64n*r+BigInt("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".indexOf(s))}return r.toString()},this.getPostIDfromURL=function(e){var t,r,n=null==e?void 0:e.split("?")[0];return(null==(t=n)?void 0:t.endsWith("/"))&&(n=n.slice(0,-1)),n=(null==(r=n)?void 0:r.split("/").pop())||"",A.getPostIDfromThreadID(n||"")};var q=this;this.getThreads=c(function(e,t){return h(this,function(r){switch(r.label){case 0:return q.verbose&&console.debug("[fbLSDToken] USING",q.fbLSDToken),[4,q._requestUserDataQuery("https://www.threads.net/api/graphql",{lsd:q.fbLSDToken,variables:JSON.stringify({postID:e}),doc_id:"5587632691339264"},t)];case 1:return[2,r.sent().data.data.data]}})});var B=this;this.getThreadsLoggedIn=c(function(e,t,r){var n,a,i;return h(this,function(u){switch(u.label){case 0:void 0===t&&(t=""),void 0===r&&(r={}),a=void 0,u.label=1;case 1:return u.trys.push([1,3,,4]),[4,B._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/"+e+"/replies/"+(t?"?paging_token="+t:""),r)];case 2:return a=u.sent().data,[3,4];case 3:return a=null==(i=u.sent().response)?void 0:i.data,[3,4];case 4:if((null==(n=a)?void 0:n.status)!=="ok")throw B.verbose&&console.log("[USER FEED] Failed to fetch",a),new o.ThreadsAPIError("Failed to fetch user feed: "+JSON.stringify(a),a);return[2,a]}})});var G=this;this.getThreadLikers=c(function(e,t){return h(this,function(r){switch(r.label){case 0:return G.verbose&&console.debug("[fbLSDToken] USING",G.fbLSDToken),[4,G._requestUserDataQuery("https://www.threads.net/api/graphql",{lsd:G.fbLSDToken,variables:JSON.stringify({mediaID:e}),doc_id:"9360915773983802"},t)];case 1:return[2,r.sent().data.data.likers]}})});var C=this;this.getTimeline=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:if(void 0===e&&(e=""),!C.token&&(!C.username||!C.password))throw Error("Username or password not set");return[4,C.getToken()];case 1:if(!n.sent())throw Error("Token not found");n.label=2;case 2:return n.trys.push([2,4,,5]),[4,C._requestQuery(""+s.BASE_API_URL+"/api/v1/feed/text_post_app_timeline/",{pagination_source:"text_post_feed_threads",max_id:e||void 0},l({},t,{headers:C._getAppHeaders()}))];case 3:return[2,n.sent().data];case 4:throw r=n.sent(),C.verbose&&console.log("[TIMELINE FETCH FAILED]",r.response.data),Error("Failed to fetch timeline");case 5:return[2]}})});var H=this;this._fetchAuthGetRequest=c(function(t,r){var n;return h(this,function(s){switch(s.label){case 0:return[4,H.getToken()];case 1:if(!s.sent())throw Error("Token not found");return[4,e.default.get(t,l({},r,{headers:l({},H._getInstaHeaders(),null==(n=r)?void 0:n.headers)}))];case 2:return[2,s.sent()]}})});var M=this;this._toggleAuthPostRequest=c(function(t,r,n){return h(this,function(s){switch(s.label){case 0:return[4,M.getToken()];case 1:if(!s.sent())throw Error("Token not found");return[4,e.default.post(t,r?new URLSearchParams(r):void 0,l({},n,{httpAgent:M.httpAgent,httpsAgent:M.httpsAgent,headers:M._getDefaultHeaders()}))];case 2:return[2,s.sent()]}})});var J=this;this.like=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,J.getCurrentUserID()];case 1:return r=n.sent(),[4,J._toggleAuthPostRequest(s.BASE_API_URL+"/api/v1/media/"+e+"_"+r+"/like/",void 0,t)];case 2:return[2,n.sent().data]}})});var X=this;this.unlike=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,X.getCurrentUserID()];case 1:return r=n.sent(),[4,X._toggleAuthPostRequest(s.BASE_API_URL+"/api/v1/media/"+e+"_"+r+"/unlike/",void 0,t)];case 2:return[2,n.sent().data]}})});var W=this;this.follow=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,W._toggleAuthPostRequest(s.BASE_API_URL+"/api/v1/friendships/create/"+e+"/",void 0,t)];case 1:return r=n.sent(),W.verbose&&console.debug("[FOLLOW]",r.data),[2,r.data]}})});var j=this;this.unfollow=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,j._toggleAuthPostRequest(s.BASE_API_URL+"/api/v1/friendships/destroy/"+e+"/",void 0,t)];case 1:return r=n.sent(),j.verbose&&console.debug("[UNFOLLOW]",r.data),[2,r.data]}})});var Q=this;this.repost=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,Q._toggleAuthPostRequest(""+s.BASE_API_URL+"/api/v1/repost/create_repost/",{media_id:e},t)];case 1:return r=n.sent(),Q.verbose&&console.debug("[REPOST]",r.data),[2,r.data]}})});var z=this;this.unrepost=c(function(e,t){var r;return h(this,function(n){switch(n.label){case 0:return[4,z._toggleAuthPostRequest(""+s.BASE_API_URL+"/api/v1/repost/delete_text_app_repost/",{original_media_id:e},t)];case 1:return r=n.sent(),z.verbose&&console.debug("[UNREPOST]",r.data),[2,r.data]}})});var V=this;this.mute=c(function(e,t){var r,n,a,o;return h(this,function(i){switch(i.label){case 0:return r=""+s.BASE_API_URL+"/api/v1/friendships/mute_posts_or_story_from_follow/",n={_uid:V.userID,_uuid:V.deviceID,container_module:"ig_text_feed_timeline"},e.postID&&(n.media_id=String(e.postID)),n.target_posts_author_id=String(e.userID),a={signed_body:"SIGNATURE."+encodeURIComponent(JSON.stringify(n))},[4,V._toggleAuthPostRequest(r,a,t)];case 1:return o=i.sent(),V.verbose&&console.debug("[MUTE]",o.data),[2,o.data]}})});var K=this;this.unmute=c(function(e,t){var r,n,a,o;return h(this,function(i){switch(i.label){case 0:return r=""+s.BASE_API_URL+"/api/v1/friendships/unmute_posts_or_story_from_follow/",n={_uid:K.userID,_uuid:K.deviceID,container_module:"ig_text_feed_timeline"},e.postID&&(n.media_id=String(e.postID)),n.target_posts_author_id=String(e.userID),a={signed_body:"SIGNATURE."+encodeURIComponent(JSON.stringify(n))},[4,K._toggleAuthPostRequest(r,a,t)];case 1:return o=i.sent(),K.verbose&&console.debug("[UNMUTE]",o.data),[2,o.data]}})});var Y=this;this.block=c(function(e,t){var r,n,a;return h(this,function(o){switch(o.label){case 0:return r=s.BASE_API_URL+"/api/v1/friendships/block/"+e+"/",n={signed_body:"SIGNATURE."+encodeURIComponent(JSON.stringify({surface:"ig_text_feed_timeline",is_auto_block_enabled:!0,user_id:e,_uid:Y.userID,_uuid:Y.deviceID}))},[4,Y._toggleAuthPostRequest(r,n,t)];case 1:return a=o.sent(),Y.verbose&&console.debug("[MUTE]",a.data),[2,a.data]}})});var $=this;this.unblock=c(function(e,t){var r,n,a;return h(this,function(o){switch(o.label){case 0:return r=s.BASE_API_URL+"/api/v1/friendships/unblock/"+e+"/",n={signed_body:"SIGNATURE."+encodeURIComponent(JSON.stringify({user_id:e,_uid:$.userID,_uuid:$.deviceID,container_module:"ig_text_feed_timeline"}))},[4,$._toggleAuthPostRequest(r,n,t)];case 1:return a=o.sent(),$.verbose&&console.debug("[MUTE]",a.data),[2,a.data]}})});var Z=this;this.getNotifications=c(function(e,t,r){var n,a,i,u,c;return h(this,function(l){switch(l.label){case 0:void 0===r&&(r={}),a={feed_type:"all",mark_as_seen:!1,timezone_offset:-25200,timezone_name:"America%2FLos_Angeles"},e&&(a.selected_filters=e),t&&(a.max_id=t.maxID,a.pagination_first_record_timestamp=t.firstRecordTimestamp),i=Object.entries(a).map(function(e){return e[0]+"="+e[1]}).join("&"),u=void 0,l.label=1;case 1:return l.trys.push([1,3,,4]),[4,Z._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/text_app_notifications/?"+i,r)];case 2:return u=l.sent().data,[3,4];case 3:return u=null==(c=l.sent().response)?void 0:c.data,[3,4];case 4:if((null==(n=u)?void 0:n.status)!=="ok")throw Z.verbose&&console.log("[NOTIFICATIONS] Failed to fetch",u),new o.ThreadsAPIError("Failed to fetch notifications: "+JSON.stringify(u),u);return[2,u]}})});var ee=this;this.setNotificationsSeen=c(function(e){var t,r,n;return h(this,function(a){switch(a.label){case 0:return t=""+s.BASE_API_URL+"/api/v1/text_feed/text_app_inbox_seen/",r={_uuid:""+ee.userID},[4,ee._toggleAuthPostRequest(t,r,e)];case 1:return n=a.sent(),ee.verbose&&console.debug("[SET_NOTIFICATIONS_SEEN]",n.data),[2,n.data]}})});var et=this;this.searchUsers=c(function(e,t,r){var n,a,i,u;return h(this,function(c){switch(c.label){case 0:void 0===t&&(t=30),void 0===r&&(r={}),a=Object.entries({q:e,count:t}).map(function(e){return e[0]+"="+e[1]}).join("&"),i=void 0,c.label=1;case 1:return c.trys.push([1,3,,4]),[4,et._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/users/search/?"+a,r)];case 2:return i=c.sent().data,[3,4];case 3:return i=null==(u=c.sent().response)?void 0:u.data,[3,4];case 4:if((null==(n=i)?void 0:n.status)!=="ok")throw et.verbose&&console.log("[USER SEARCH] Failed to fetch",i),new o.ThreadsAPIError("Failed to fetch user search results: "+JSON.stringify(i),i);return[2,i]}})});var er=this;this.getRecommendedUsers=c(function(e,t){var r,n,a;return h(this,function(i){switch(i.label){case 0:void 0===e&&(e=""),void 0===t&&(t={}),n=void 0,i.label=1;case 1:return i.trys.push([1,3,,4]),[4,er._fetchAuthGetRequest(s.BASE_API_URL+"/api/v1/text_feed/recommended_users/?"+(e?"?max_id="+e:""),t)];case 2:return n=i.sent().data,[3,4];case 3:return n=null==(a=i.sent().response)?void 0:a.data,[3,4];case 4:if((null==(r=n)?void 0:r.status)!=="ok")throw er.verbose&&console.log("[RECOMMENDED] Failed to fetch",n),new o.ThreadsAPIError("Failed to fetch recommended users: "+JSON.stringify(n),n);return[2,n]}})});var en=this;this.getToken=c(function(){return h(this,function(e){switch(e.label){case 0:if(en.token)return en.verbose&&console.debug("[token] USING",en.token),[2,en.token];if(!en.username||!en.password)throw Error("Username and password are required");return[4,en.login()];case 1:return e.sent(),[2,en.token]}})}),this._lastUploadID=0,this._nextUploadID=function(){var e=Date.now(),t=A._lastUploadID;return(A._lastUploadID=e<t?t+1:e).toString()},this._createUploadMetadata=function(e){var t;return void 0===e&&(e=A._nextUploadID()),{upload_id:e,source_type:"4",timezone_offset:(null!=(t=A._timezoneOffset)?t:A._timezoneOffset=-(60*new Date().getTimezoneOffset())).toString(),device:A.device}};var es=this;this.publish=c(function(t){var r,n,a,o,i,u,c,p,f,g,_,v;return h(this,function(h){switch(h.label){case 0:if(r="string"==typeof t?{text:t}:t,!es.token&&(!es.username||!es.password))throw Error("Username or password not set");return[4,es.getCurrentUserID()];case 1:if(!(n=h.sent()))throw Error("User ID not found");return[4,es.getToken()];case 2:if(!h.sent())throw Error("Token not found");if(o=l({},es._createUploadMetadata(),{text_post_app_info:{reply_control:s.REPLY_CONTROL_OPTIONS[null!=(a=r.replyControl)?a:"everyone"]},_uid:n,device_id:es.deviceID,caption:r.text||""}),i=s.POST_URL,!(u=r.attachment)&&("image"in r&&r.image?u={image:r.image}:"url"in r&&r.url&&(u={url:r.url})),!u)return[3,9];if(!u.url)return[3,3];return o.text_post_app_info.link_attachment_url=u.url,[3,9];case 3:if(!u.image)return[3,5];return i=s.POST_WITH_IMAGE_URL,[4,es.uploadImage(u.image,o.upload_id)];case 4:return h.sent(),o.scene_type=null,o.scene_capture_type="",[3,9];case 5:if(!u.sidecar)return[3,9];i=s.POST_WITH_SIDECAR_URL,o.client_sidecar_id=o.upload_id,o.children_metadata=[],c=d(u.sidecar),h.label=6;case 6:if((p=c()).done)return[3,9];return f=p.value,[4,es.uploadImage(f)];case 7:g=h.sent().upload_id,o.children_metadata.push(l({},es._createUploadMetadata(g),{scene_type:null,scene_capture_type:""})),h.label=8;case 8:return[3,6];case 9:return r.parentPostID&&(o.text_post_app_info.reply_id=r.parentPostID.replace(/_\d+$/,"")),r.quotedPostID&&(o.text_post_app_info.quoted_post_id=r.quotedPostID.replace(/_\d+$/,"")),i===s.POST_URL&&(o.publish_mode="text_post"),_="signed_body=SIGNATURE."+encodeURIComponent(JSON.stringify(o)),[4,e.default.post(i,_,{httpAgent:es.httpAgent,httpsAgent:es.httpsAgent,headers:es._getAppHeaders(),timeout:6e4})];case 10:if(v=h.sent(),es.verbose&&console.debug("[PUBLISH]",v.data),"ok"===v.data.status)return[2,v.data.media.id];return[2,void 0]}})});var ea=this;this.delete=c(function(t,r){var n,a;return h(this,function(o){switch(o.label){case 0:return n=s.BASE_API_URL+"/api/v1/media/"+t+"/delete/",a="signed_body=SIGNATURE."+encodeURIComponent(JSON.stringify({media_id:t,_uid:ea.userID,_uuid:ea.deviceID})),[4,e.default.post(n,a,l({httpAgent:ea.httpAgent,httpsAgent:ea.httpsAgent,headers:ea._getAppHeaders(),timeout:6e4},r))];case 1:if("ok"===o.sent().data.status)return[2,!0];return[2,!1]}})});var eo=this;this.publishWithImage=c(function(e,t){return h(this,function(r){return[2,eo.publish({text:e,image:t})]})});var ei=this;this.uploadImage=c(function(t,s){var a,o,i,u,c,d,p,f,g,_,v;return h(this,function(h){switch(h.label){case 0:if(void 0===s&&(s=ei._nextUploadID()),o="https://www.instagram.com/rupload_igphoto/"+(a=s+"_0_"+Math.floor(9e9*Math.random()+1e9)),!("string"==typeof t||"path"in t))return[3,6];if((c="string"==typeof t?t:t.path).startsWith("http"))return[3,3];return[4,Promise.resolve().then(function(){return require("fs")})];case 1:return[4,h.sent().promises.readFile(c)];case 2:return i=h.sent(),u=r.lookup(c)||"application/octet-stream",[3,5];case 3:return[4,e.default.get(c,{responseType:"arraybuffer"})];case 4:d=h.sent(),i=Buffer.from(d.data,"binary"),u=d.headers["content-type"],h.label=5;case 5:return[3,7];case 6:i=t.data,u=(t.type.includes("/")?t.type:r.lookup(t.type))||"application/octet-stream",h.label=7;case 7:p={upload_id:s,media_type:"1",sticker_burnin_params:JSON.stringify([]),image_compression:JSON.stringify({lib_name:"moz",lib_version:"3.1.m",quality:"80"}),xsharing_user_ids:JSON.stringify([]),retry_context:JSON.stringify({num_step_auto_retry:"0",num_reupload:"0",num_step_manual_retry:"0"}),"IG-FB-Xpost-entry-point-v2":"feed"},f=i.length,g=l({},ei._getDefaultHeaders(),{"Content-Type":"application/octet-stream",X_FB_PHOTO_WATERFALL_ID:(0,n.v4)(),"X-Entity-Type":void 0!==u?"image/"+u:"image/jpeg",Offset:"0","X-Instagram-Rupload-Params":JSON.stringify(p),"X-Entity-Name":a,"X-Entity-Length":f.toString(),"Content-Length":f.toString(),"Accept-Encoding":"gzip"}),ei.verbose&&console.log("[UPLOAD_IMAGE] Uploading "+f.toLocaleString()+"b as "+s+"..."),h.label=8;case 8:return h.trys.push([8,10,,11]),[4,e.default.post(o,i,{httpAgent:ei.httpAgent,headers:g,timeout:6e4})];case 9:return _=h.sent().data,ei.verbose&&console.log("[UPLOAD_IMAGE] SUCCESS",_),[2,_];case 10:throw v=h.sent(),ei.verbose&&console.log("[UPLOAD_IMAGE] FAILED",v.response.data),Error("Upload image failed");case 11:return[2]}})}),this.verbose=!!i.verbose,this.token=i.token,this.fbLSDToken=null!=(u=i.fbLSDToken)?u:s.DEFAULT_LSD_TOKEN,this.noUpdateToken=!!i.noUpdateToken,this.noUpdateLSD=!!i.noUpdateLSD,this.httpAgent=i.httpAgent,this.httpsAgent=i.httpsAgent,this.username=null!=(g=i.username)?g:process.env.THREADS_USERNAME,this.password=null!=(_=i.password)?_:process.env.THREADS_PASSWORD,this.deviceID=(null!=(v=i.deviceID)?v:process.env.THREADS_DEVICE_ID)||p(),this.device=null!=(b=i.device)?b:f,this.userID=i.userID,this.locale=null!=(m=i.locale)?m:Intl.DateTimeFormat().resolvedOptions().locale,this.maxRetries=null!=(I=i.maxRetries)?I:1}return i.prototype.sign=function(e){var r="object"==typeof e?JSON.stringify(e):e;return{ig_sig_key_version:4,signed_body:t.createHmac("sha256",s.SIGNATURE_KEY).update(r).digest("hex")+"."+r}},i}(),_=g;
{
"name": "threads-api",
"version": "1.6.2",
"version": "1.6.3",
"description": "Unofficial, Reverse-Engineered Node.js/TypeScript client for Meta's [Threads](https://threads.net).",

@@ -5,0 +5,0 @@ "author": "Junho Yeo <i@junho.io>",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc