@mtproto/core
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "@mtproto/core", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Telegram API for browser", | ||
@@ -36,2 +36,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"dotenv-webpack": "1.7.0", | ||
"webpack": "4.41.2", | ||
@@ -38,0 +39,0 @@ "webpack-cli": "3.3.10", |
@@ -48,41 +48,2 @@ const http = require('../transport'); | ||
function apiDebounce(func, wait) { | ||
let timeout; | ||
let messages = []; | ||
return function(newMessages) { | ||
return new Promise(function(resolve, reject) { | ||
if (!(newMessages instanceof Array)) { | ||
newMessages = [newMessages]; | ||
} | ||
messages = messages.concat(newMessages); | ||
//messages = [...newMessages]; | ||
//messages = newMessages; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(function() { | ||
resolve(func(messages)); | ||
messages = []; | ||
}, wait); | ||
}); | ||
return func(newMessages); | ||
return new Promise(function(resolve, reject) { | ||
if (!(newMessages instanceof Array)) { | ||
newMessages = [newMessages]; | ||
} | ||
const context = this; | ||
messages = messages.concat(newMessages); | ||
const later = function() { | ||
timeout = null; | ||
const useMessages = [...messages]; | ||
messages = []; | ||
resolve(func.call(context, useMessages)); | ||
}; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, wait); | ||
}); | ||
}; | ||
} | ||
function tsNow(seconds) { | ||
@@ -487,3 +448,3 @@ var t = +new Date() + (window.tsOffset || 0); | ||
const urlPath = config.test ? '/apiw_test1' : 'apiw1'; | ||
const urlPath = true ? '/apiw_test1' : '/apiw1'; | ||
@@ -568,3 +529,2 @@ const url = | ||
const sentMessages = {}; | ||
window.sentMessages = sentMessages; | ||
@@ -1278,64 +1238,3 @@ function _sendEncryptedRequest(message) { | ||
function apiCall(method, params = {}, options = {}) { | ||
return new Promise(function(resolve, reject) { | ||
invokeApiCall(method, params, options) | ||
.then(response => { | ||
const { messageDeferred } = response; | ||
messageDeferred.then(resolve); | ||
messageDeferred.catch(reject); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
window.apicalls = []; | ||
window.invokeApiCall = invokeApiCall; | ||
window.getApiCallMessage = getApiCallMessage; | ||
function getApiCallMessage(method, params = {}, options = {}) { | ||
const serializer = new TLSerialization(options); | ||
serializer.storeInt(config.invokeWithLayer, 'invokeWithLayer'); | ||
serializer.storeInt(config.layer, 'layer'); | ||
serializer.storeInt(config.initConnection, 'initConnection'); | ||
serializer.storeInt(config.app.api_id, 'api_id'); | ||
serializer.storeString( | ||
navigator.userAgent || 'Unknown UserAgent', | ||
'device_model' | ||
); | ||
serializer.storeString( | ||
navigator.platform || 'Unknown Platform', | ||
'system_version' | ||
); | ||
serializer.storeString('1.0.0', 'app_version'); | ||
serializer.storeString(navigator.language || 'en', 'system_lang_code'); | ||
serializer.storeString('', 'lang_pack'); | ||
serializer.storeString(navigator.language || 'en', 'lang_code'); | ||
options.resultType = serializer.storeMethod(method, params); | ||
let toAck = []; //msgs_ack | ||
const msg_id = generateMessageID(); | ||
const message = { | ||
msg_id, | ||
seq_no: generateSeqNo(), | ||
body: serializer.getBytes(true), | ||
isAPI: true, | ||
method, | ||
}; | ||
const messageByteLength = | ||
(message.body.byteLength || message.body.length) + 32; | ||
return message; | ||
} | ||
function invokeApiCall(method, params = {}, options = {}) { | ||
const message = getApiCallMessage(method, params, options); | ||
sendAcks(); | ||
return sendEncryptedRequest(message); | ||
} | ||
window.sendEncryptedRequest = sendEncryptedRequest; | ||
function sendEncryptedRequest(messages) { | ||
//const sendEncryptedRequest = apiDebounce(function (messages) { | ||
//console.log('send req', messages.length) | ||
@@ -1407,8 +1306,76 @@ | ||
} | ||
//}, 200); | ||
} | ||
module.exports = { | ||
authorize, | ||
apiCall, | ||
}; | ||
class API { | ||
constructor({ api_id, api_hash }) { | ||
this.api_id = api_id; | ||
this.api_hash = api_hash; | ||
} | ||
getApiCallMessage(method, params = {}, options = {}) { | ||
const serializer = new TLSerialization(options); | ||
serializer.storeInt(config.invokeWithLayer, 'invokeWithLayer'); | ||
serializer.storeInt(config.layer, 'layer'); | ||
serializer.storeInt(config.initConnection, 'initConnection'); | ||
serializer.storeInt(this.api_id, 'api_id'); | ||
serializer.storeString( | ||
navigator.userAgent || 'Unknown UserAgent', | ||
'device_model' | ||
); | ||
serializer.storeString( | ||
navigator.platform || 'Unknown Platform', | ||
'system_version' | ||
); | ||
serializer.storeString('1.0.0', 'app_version'); | ||
serializer.storeString(navigator.language || 'en', 'system_lang_code'); | ||
serializer.storeString('', 'lang_pack'); | ||
serializer.storeString(navigator.language || 'en', 'lang_code'); | ||
options.resultType = serializer.storeMethod(method, params); | ||
let toAck = []; //msgs_ack | ||
const msg_id = generateMessageID(); | ||
const message = { | ||
msg_id, | ||
seq_no: generateSeqNo(), | ||
body: serializer.getBytes(true), | ||
isAPI: true, | ||
method, | ||
}; | ||
const messageByteLength = | ||
(message.body.byteLength || message.body.length) + 32; | ||
return message; | ||
} | ||
invokeApiCall(method, params = {}, options = {}) { | ||
const message = this.getApiCallMessage(method, params, options); | ||
sendAcks(); | ||
return sendEncryptedRequest(message); | ||
} | ||
apiCall(method, params = {}, options = {}) { | ||
return new Promise((resolve, reject) => { | ||
this.invokeApiCall(method, params, options) | ||
.then(response => { | ||
const { messageDeferred } = response; | ||
messageDeferred.then(resolve); | ||
messageDeferred.catch(reject); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
call(method, data) { | ||
return authorize().then(() => { | ||
return this.apiCall(method, { | ||
api_hash: this.api_hash, | ||
api_id: this.api_id, | ||
...data, | ||
}); | ||
}); | ||
} | ||
} | ||
module.exports = API; |
@@ -16,4 +16,2 @@ const apiSchema = require('../scheme/api-74.json'); | ||
app: {}, | ||
test: false, | ||
}; |
@@ -1,22 +0,9 @@ | ||
const config = require('./config'); | ||
const { authorize, apiCall } = require('./api'); | ||
const API = require('./api'); | ||
class MTProto { | ||
constructor({ api_id, api_hash, test = false }) { | ||
config.app.api_id = api_id; | ||
config.app.api_hash = api_hash; | ||
config.test = test; | ||
constructor({ api_id, api_hash }) { | ||
this.api = new API({ api_id, api_hash }); | ||
} | ||
call(method, data) { | ||
return authorize().then(() => { | ||
return apiCall(method, { | ||
api_hash: config.app.api_hash, | ||
api_id: config.app.api_id, | ||
...data, | ||
}); | ||
}); | ||
} | ||
} | ||
module.exports = MTProto; |
@@ -1,34 +0,7 @@ | ||
// const axios = require('axios'); | ||
const axios = require('axios'); | ||
// const http = axios.create(); | ||
// delete http.defaults.headers.post['Content-Type']; | ||
// delete http.defaults.headers.common['Accept']; | ||
const http = axios.create(); | ||
delete http.defaults.headers.post['Content-Type']; | ||
delete http.defaults.headers.common['Accept']; | ||
const http = { | ||
post(url, data, opts) { | ||
return new Promise(function(resolve, reject) { | ||
var request = new XMLHttpRequest(); | ||
Object.keys(opts).forEach(key => (request[key] = opts[key])); | ||
request.addEventListener('load', function() { | ||
resolve({ data: this.response }); | ||
}); | ||
request.addEventListener('error', e => { | ||
reject(e); | ||
}); | ||
request.open('POST', url); | ||
request.setRequestHeader('Content-Type', ''); | ||
request.setRequestHeader('Accept', '*/*'); | ||
request.onreadystatechange = function() { | ||
if (request.readyState === 4) { | ||
if (request.status !== 200) { | ||
//reject(new Error(`API request error ${request.status}`)); | ||
reject(`API request error ${request.status}`); | ||
} | ||
} | ||
}; | ||
request.send(data); | ||
}); | ||
}, | ||
}; | ||
module.exports = http; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15
348093
4
6158
3