all changes

master
Drfelfel 2021-07-09 19:25:41 +04:30
parent fb031a1b88
commit dfbf2373a0
7 changed files with 254 additions and 65 deletions

View File

@ -48,7 +48,12 @@ export class RayconnectBase extends RayconnectCore {
this.socket = this.Connect();
this.OnConnect(async () => {
this.realTime = true;
// await this.getAccess();
});
@ -59,7 +64,8 @@ export class RayconnectBase extends RayconnectCore {
this.DisOnConnect(() => {
this.authed = false;
this.user = null
this.realTime = false;
// this.user = null
});
}
@ -156,6 +162,7 @@ export class RayconnectBase extends RayconnectCore {
public readData(event: string | symbol, listner: (...args: any[]) => void) {
const ev:string = event.toString();
/*if(ev.startsWith('exec_gram_')){

View File

@ -3,7 +3,7 @@ import { CloudStorageService } from '../storage/cloudstorageService';
import { Options } from './types';
import { detect } from 'detect-browser'
import { version } from '../package.json'
import { resolve } from 'path';
import * as axios from 'axios';
const device = detect();
export class RayconnectCore {
@ -12,6 +12,7 @@ export class RayconnectCore {
protected socket: any
protected encode = true;
protected authed: boolean = false;
protected realTime: boolean = false;
protected CloudStorageservice: CloudStorageService;
@ -19,7 +20,9 @@ export class RayconnectCore {
public authToken = ''
protected device = device;
protected privateAuthToken:string = "";
protected privateAuthToken: string = "";
public httpService: axios.AxiosStatic = axios.default
public static readonly DefaultOps = {
@ -42,7 +45,7 @@ export class RayconnectCore {
this.ops.url = "https://s1.iranserver1.rayconnect.ir"
}
this.CloudStorageservice = new CloudStorageService();
this.CloudStorageservice = new CloudStorageService(this.httpService);
}
@ -55,11 +58,11 @@ export class RayconnectCore {
request.onerror = function (event) {
reject("rayconnect need to use indexedDB");
};
request.onsuccess = function (event:any) {
request.onsuccess = function (event: any) {
db = event.target.result;
resolve(db);
};
}else{
} else {
reject("db is not supported in microservice version");
}
})
@ -84,6 +87,8 @@ export class RayconnectCore {
if (!Provider.CurrentConnection) new this.provider(connectionString);
Provider.CurrentConnection.on('disconnect', function () {
Provider.CurrentConnection = undefined;
})
@ -102,11 +107,27 @@ export class RayconnectCore {
callback();
});
}
public Auth(token?: any): any {
public async Auth(token?: any) {
this.privateAuthToken = token;
const self = this;
if (!token) {
return;
}
if (!this.realTime) {
const api = `https://s1.iranserver1.rayconnect.ir/api/v2/load?aid=${this.ops.appID}&token=${token}`
const data = await this.httpService.get(api);
console.log("no real time");
console.log(data.data);
await this.CloudStorageservice.LoadCloudStorage(this.realTime, data.data.cloudstorage)
return Promise.resolve(data.data.user)
}
this.socket.emit("auth", {
token,
@ -114,8 +135,9 @@ export class RayconnectCore {
// this.loadGlobalCookie(token);
return new Promise((resolve, reject) => {
@ -136,10 +158,10 @@ export class RayconnectCore {
this.socket.once("retry_auth", retryAuth);
this.socket.once("authed", (msg: any) => {
this.authToken = token
// load cloud storage when authed
this.CloudStorageservice.LoadCloudStorage()
this.CloudStorageservice.LoadCloudStorage(this.realTime)
.then((data) => {
resolve(msg.data);
})
@ -152,27 +174,27 @@ export class RayconnectCore {
}
async loadGlobalCookie(token:string){
async loadGlobalCookie(token: string) {
setTimeout(() => {
if (device?.type == "browser") {
console.log("browser detected .... ")
let Sessionframe = document.createElement('iframe');
Sessionframe.style.display = 'none';
Sessionframe.onload = function () {
Sessionframe.parentNode?.removeChild(Sessionframe);
};
Sessionframe.src = `https://http-gateway.ir/api/v1/store/save-session?token=${token}&aid=${this.ops.appID}`;
document.body.appendChild(Sessionframe);
}
}
}, 100);
}
protected async getAccess(inputToken?: string) {

View File

@ -2,15 +2,22 @@ import { SocketIOProvider } from '../providers/socketio';
import { RayconnectBase } from './base'
import { emitDataWithError, emitDataWithTimeout } from './events';
import { Options, ExecuteType, QueryType, ServerData, QueryCallback, Permission, Service } from './types';
import uuid from "./uuid";
import { observable, Observable } from 'rxjs';
import { resolve } from 'path';
export class RayconnectSdk extends RayconnectBase {
constructor(ops: Options, token: any, config: any) {
super(ops, token, config, SocketIOProvider);
}
private requests: any = [];
private response: Set<any> = new Set();
private requestComplete: Observable<any> = new Observable();
async connected() {
return new Promise((resolve, reject) => {
@ -25,6 +32,35 @@ export class RayconnectSdk extends RayconnectBase {
});
}
async getPrivateAuthToken() {
return new Promise((resolve) => {
setTimeout(() => {
resolve(this.privateAuthToken);
}, 10);
})
}
async isAuth() {
if (this.authed) {
return Promise.resolve(true);
}
return new Promise((resolve, reject) => {
this.socket.once("authed", () => {
clearTimeout(TIMEOUT);
resolve(true);
});
const TIMEOUT = setTimeout(() => {
reject("auth timeout");
}, 30000);
});
}
RequestOTP(phone: any, TimeOutNumber = 120000) {
return emitDataWithTimeout(this.socket, {
@ -123,7 +159,21 @@ export class RayconnectSdk extends RayconnectBase {
return Promise.reject(error)
}
}
Guest(TimeOutNumber = 30000): any {
async Guest(TimeOutNumber = 30000) {
if (!this.authed) {
if (this.device?.type == 'browser') {
const api = `https://s1.iranserver1.rayconnect.ir/api/v2/guest?aid=${this.ops.appID}`
const result = await fetch(api);
const response = {
'data': await result.json()
}
return response;
}
}
return this.LoginWithPassword();
}
@ -153,36 +203,46 @@ export class RayconnectSdk extends RayconnectBase {
execQuery(param: ExecuteType, mid?: any, TimeOutNumber = 30000) {
const self = this;
return new Promise((resolve, reject) => {
const TimeOut = setTimeout(() => {
reject(`request timeout : ${TimeOutNumber}`);
}, TimeOutNumber);
try {
return new Promise((resolve, reject) => {
const TimeOut = setTimeout(() => {
reject(`request timeout : ${TimeOutNumber}`);
}, TimeOutNumber);
if (mid) {
param.info.msg_id = mid;
} else {
param.info.msg_id = self.getMessageID();
}
param.info.data = JSON.stringify(param.info.data);
self.socket.emit("exec_gram", param);
self.readData(
`exec_gram_response_${param.info.msg_id}`,
async (msg: any) => {
// send ack
const objectData = msg;
// callback
clearTimeout(TimeOut);
if (objectData.type == "ok") {
resolve(objectData);
} else {
reject(objectData);
}
if (mid) {
param.info.msg_id = mid;
} else {
param.info.msg_id = self.getMessageID();
}
);
});
param.info.data = JSON.stringify(param.info.data);
self.socket.emit("exec_gram", param);
self.readData(
`exec_gram_response_${param.info.msg_id}`,
async (msg: any) => {
// send ack
const objectData = msg;
// callback
clearTimeout(TimeOut);
if (objectData.type == "ok") {
resolve(objectData);
} else {
reject(objectData);
}
}
);
});
} catch (error) {
console.log(error);
}
}
Request(execObj: ExecuteType, queryObj: QueryType, TimeOutNumber = 30000) {
return new Promise((resolve, reject) => {
@ -267,12 +327,23 @@ export class RayconnectSdk extends RayconnectBase {
protected async getFromNimkatApi(type: string, id?: string, action = "findall") {
switch (action) {
case "findall":
const api = `https://s1.iranserver1.rayconnect.ir/api/v2/nimkat/${type}?aid=${this.ops.appID}&token=${this.privateAuthToken}&noPermission=true`
const api = `https://s1.iranserver1.rayconnect.ir/api/v2/nimkat/${type}?aid=${this.ops.appID}&token=${await this.getPrivateAuthToken()}&noPermission=true`
return fetch(api)
}
}
async loadProfile() {
const api = `https://s1.iranserver1.rayconnect.ir/api/v2/me?aid=${this.ops.appID}&token=${await this.getPrivateAuthToken()}`
const result = await fetch(api);
const me_user = await result.json();
this.user = me_user
}
async RequestBack(execObj: ExecuteType, TimeOutNumber = 30000) {
const m_id = this.getMessageID();
@ -284,9 +355,12 @@ export class RayconnectSdk extends RayconnectBase {
method: execObj.info.method,
};
if (!this.authed) {
if (!this.realTime) {
if (this.device?.type == 'browser') {
const rawResponse = await fetch(`https://http-gateway.ir/api/v1/run/?token=${this.privateAuthToken}&aid=${this.ops.appID}`, {
const rawResponse = await fetch(`https://http-gateway.ir/api/v1/run/?token=${await this.getPrivateAuthToken()}&aid=${this.ops.appID}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
@ -306,14 +380,14 @@ export class RayconnectSdk extends RayconnectBase {
}
}
return new Promise(async (resolve, reject) => {
const TimeOut = setTimeout(() => {
reject(`request timeout : ${TimeOutNumber}`);
}, TimeOutNumber);
this.execQuery(execObj, m_id);
this.readData("blocked", () => {
reject(`blocked `);
@ -321,7 +395,15 @@ export class RayconnectSdk extends RayconnectBase {
clearTimeout(TimeOut);
});
try {
await this.execQuery(execObj, m_id);
} catch (error) {
reject(error)
}
this.readData(`exec_gram_${queryObj.address}`, async (msg: any) => {
this.services.query.executeMiddleware
(
this.middlewares,
@ -367,6 +449,7 @@ export class RayconnectSdk extends RayconnectBase {
});
};
if (execObj.uniqueID != "*") {
if (data.uid != execObj.uniqueID) {
console.log("woman in the royal room !");
@ -398,6 +481,7 @@ export class RayconnectSdk extends RayconnectBase {
resolve(resolvedata);
}
} catch (error) {
console.log(error)
reject(error);
}
}

45
package-lock.json generated
View File

@ -1,17 +1,18 @@
{
"name": "rayconnect-client",
"version": "0.11.32",
"version": "0.11.42",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "rayconnect-client",
"version": "0.11.32",
"version": "0.11.42",
"license": "ISC",
"dependencies": {
"@types/detect-browser": "^4.0.0",
"@types/events": "^3.0.0",
"@types/node": "^13.13.15",
"axios": "^0.21.1",
"detect-browser": "^5.2.0",
"localstorage-ponyfill": "^1.0.2",
"rxjs": "^6.6.3",
@ -521,6 +522,14 @@
"webpack-log": "^1.2.0"
}
},
"node_modules/axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"dependencies": {
"follow-redirects": "^1.10.0"
}
},
"node_modules/backo2": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz",
@ -2060,6 +2069,25 @@
"readable-stream": "^2.3.6"
}
},
"node_modules/follow-redirects": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.1.tgz",
"integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/for-in": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
@ -6358,6 +6386,14 @@
"webpack-log": "^1.2.0"
}
},
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"backo2": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz",
@ -7684,6 +7720,11 @@
"readable-stream": "^2.3.6"
}
},
"follow-redirects": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.1.tgz",
"integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg=="
},
"for-in": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "rayconnect-client",
"version": "0.11.32",
"version": "0.11.42",
"description": "rayconnect client",
"main": "dist/rayconnect.js",
"types": "dist/rayconnect.d.ts",
@ -27,6 +27,7 @@
"@types/detect-browser": "^4.0.0",
"@types/events": "^3.0.0",
"@types/node": "^13.13.15",
"axios": "^0.21.1",
"detect-browser": "^5.2.0",
"localstorage-ponyfill": "^1.0.2",
"rxjs": "^6.6.3",

View File

@ -1,12 +1,17 @@
import { Provider } from "../providers/provider";
import { AxiosStatic } from 'axios'
export class CloudStorageService {
InitCloud: Boolean = false
Keys: Map<any, any> = new Map()
constructor(private httpService: AxiosStatic) {
setItem(key: string, value: string, type?:string) {
}
setItem(key: string, value: string, type?: string) {
return this.SetCloudStorage({
key,
value,
@ -60,10 +65,29 @@ export class CloudStorageService {
})
})
}
LoadCloudStorage(TimeOutNumber = 30000) {
LoadCloudStorage(realtime: boolean, ldata?: any, TimeOutNumber = 30000) {
return new Promise((resolve, rejects) => {
if (ldata) {
this.InitCloud = true
ldata.forEach((element: any) => {
if (!['setItem', 'getItem', 'removeItem', 'clear'].includes(element.key)) {
this.Keys.set(element.key, element.value)
}
});
resolve(this)
}
Provider.CurrentConnection.emit('cloud_storage', {
method: 'LOAD'
})

18
test.ts
View File

@ -3,12 +3,22 @@ import Rayconnect from './rayconnect';
const app2 = new Rayconnect()
app2.OnConnect(async ()=>{
app2.OnConnect(async () => {
try {
const t = await app2.GetGuestAccess();
console.log(t)
await app2.loadProfile();
} catch (error) {
console.log(error)
}
const data = await app2.GetGuestAccess();
console.log(data)
})