padlocal-client-ts
Version:
Padlocal ts client
170 lines • 7.67 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Log = exports.PadLocalClient = void 0;
const Request_1 = require("./Request");
const padlocal_pb_1 = require("./proto/padlocal_pb");
const WeChatLongLinkProxy_1 = require("./link/WeChatLongLinkProxy");
const events_1 = require("events");
const PadLocalClientApi_1 = require("./PadLocalClientApi");
const version_1 = require("./version");
const GrpcClient_1 = require("./GrpcClient");
const ServerInfo_1 = require("./utils/ServerInfo");
const Host_1 = require("./utils/Host");
const Log_1 = __importDefault(require("./utils/Log"));
exports.Log = Log_1.default;
const LOGPRE = "[PadLocalClient]";
class PadLocalClient extends events_1.EventEmitter {
constructor(serverAddr, token, serverCA, skipPrintVersion = false) {
super();
this.api = new PadLocalClientApi_1.PadLocalClientApi(this);
this.grpcClient = new GrpcClient_1.GrpcClient(serverAddr, token, serverCA);
this.token = token;
this._longLinkProxy = new WeChatLongLinkProxy_1.WeChatLongLinkProxy(this);
this._longLinkProxy.on("message-push", () => __awaiter(this, void 0, void 0, function* () {
try {
const syncEvent = yield this.api.sync(padlocal_pb_1.SyncRequestScene.ON_PUSH);
this._processSyncResponse(syncEvent);
}
catch (e) {
Log_1.default.error(LOGPRE, `error while syncing onpush: ${e.stack}`);
}
}));
this._longLinkProxy.on("status", (detail) => __awaiter(this, void 0, void 0, function* () {
if (detail.newStatus == WeChatLongLinkProxy_1.Status.CONNECTED) {
if (this.isOnline) {
try {
const syncEvent = yield this.api.sync(padlocal_pb_1.SyncRequestScene.LONGLINK_INIT);
this._processSyncResponse(syncEvent);
}
catch (e) {
Log_1.default.error(LOGPRE, `error while syncing after longlink connected: ${e.stack}`);
}
}
}
}));
if (!skipPrintVersion) {
Log_1.default.info(`
============================================================
Welcome to padlocal-client-ts !
version: ${this.version}
============================================================
`);
}
}
emit(event, ...args) {
return super.emit(event, ...args);
}
static create(token, skipPrintVersion = false) {
return __awaiter(this, void 0, void 0, function* () {
let serverInfo;
try {
serverInfo = yield ServerInfo_1.getServerInfo(token);
}
catch (e) {
Log_1.default.error(LOGPRE, `Fail to get server info: ${e}`);
throw e;
}
return new PadLocalClient(`${serverInfo.getHost().getHost()}:${serverInfo.getHost().getPort()}`, serverInfo.getPdltoken(), Buffer.from(serverInfo.getServerca()), skipPrintVersion);
});
}
get isOnline() {
return !!this.selfContact;
}
get version() {
return version_1.VERSION;
}
isSelf(userName) {
var _a;
return ((_a = this.selfContact) === null || _a === void 0 ? void 0 : _a.getUsername()) === userName;
}
createRequest(options) {
const ret = new Request_1.Request(this, options);
ret.onSystemEventCallback = (systemEventRequest) => {
var _a;
if (systemEventRequest.getPayloadCase() === padlocal_pb_1.SystemEventRequest.PayloadCase.KICKOUTEVENT) {
this._reset();
this.emit("kickout", {
errorCode: systemEventRequest.getKickoutevent().getErrorcode(),
errorMessage: systemEventRequest.getKickoutevent().getErrormessage(),
});
this._longLinkProxy.shutdown(true);
}
else if (systemEventRequest.getPayloadCase() === padlocal_pb_1.SystemEventRequest.PayloadCase.LONGLINKUPDATEEVENT) {
const longLinkUpdateEvent = systemEventRequest.getLonglinkupdateevent();
if ((_a = longLinkUpdateEvent.getLonglinkhostlistList()) === null || _a === void 0 ? void 0 : _a.length) {
const hostList = Host_1.HostResolver.hostListFromPBHost(longLinkUpdateEvent.getLonglinkhostlistList());
this._longLinkProxy.updateHostList(hostList);
}
// reconnect only while longlink is not idle or ordered by server
if (!this._longLinkProxy.isIdle() || longLinkUpdateEvent.getReconnectimmediately()) {
Log_1.default.silly(LOGPRE, "reset long link");
this.getLongLinkProxy(true).then();
}
}
else if (systemEventRequest.getPayloadCase() === padlocal_pb_1.SystemEventRequest.PayloadCase.NOTICEEVENT) {
const noticeEvent = systemEventRequest.getNoticeevent();
Log_1.default.warn(LOGPRE, noticeEvent.getNotice());
}
};
return ret;
}
request(request, options) {
return __awaiter(this, void 0, void 0, function* () {
return this.createRequest(options).request(request);
});
}
getLongLinkProxy(reset) {
return __awaiter(this, void 0, void 0, function* () {
if (reset) {
yield this._longLinkProxy.reconnect();
return this._longLinkProxy;
}
else {
yield this._longLinkProxy.makeSureConnected();
return this._longLinkProxy;
}
});
}
getLongLinkProxyDirect() {
return this._longLinkProxy;
}
getLongLinkId() {
return this._longLinkProxy.getId();
}
shutdown() {
return __awaiter(this, void 0, void 0, function* () {
this._reset();
yield this.api
.sendClientStopSignal()
.then()
.catch(() => { });
});
}
_reset() {
this.selfContact = undefined;
this._longLinkProxy.shutdown(true);
}
_processSyncResponse(syncEvent) {
Log_1.default.silly(LOGPRE, `on push notification, contact count:${syncEvent.getContactList().length}, message count:${syncEvent.getMessageList().length}`);
if (syncEvent.getMessageList().length > 0) {
this.emit("message", syncEvent.getMessageList());
}
if (syncEvent.getContactList().length > 0) {
this.emit("contact", syncEvent.getContactList());
}
}
}
exports.PadLocalClient = PadLocalClient;
//# sourceMappingURL=PadLocalClient.js.map