koishi-core
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -19,9 +19,9 @@ /// <reference types="node" /> | ||
} | ||
export declare const selfIds: number[]; | ||
export declare const apps: Record<number, App>; | ||
export declare function createApp(options?: AppOptions): App; | ||
export declare function eachApp(callback: (app: App) => any): void; | ||
export declare const appMap: Record<number, App>; | ||
export declare const appList: App[]; | ||
export declare function onStart(hook: (...app: App[]) => void): void; | ||
export declare function onStop(hook: (...app: App[]) => void): void; | ||
export declare function startAll(): Promise<void>; | ||
export declare function stopAll(): void; | ||
export declare function getSelfIds(): Promise<number[]>; | ||
export declare class App extends Context { | ||
@@ -28,0 +28,0 @@ options: AppOptions; |
@@ -24,15 +24,5 @@ "use strict"; | ||
const showReceiverLog = debug_1.default('koishi:receiver'); | ||
exports.selfIds = []; | ||
exports.apps = {}; | ||
function createApp(options = {}) { | ||
const app = new App(options); | ||
return app; | ||
} | ||
exports.createApp = createApp; | ||
function eachApp(callback) { | ||
for (const id in exports.apps) { | ||
callback(exports.apps[id]); | ||
} | ||
} | ||
exports.eachApp = eachApp; | ||
const selfIds = []; | ||
exports.appMap = {}; | ||
exports.appList = []; | ||
const onStartHooks = new Set(); | ||
@@ -43,11 +33,11 @@ function onStart(hook) { | ||
exports.onStart = onStart; | ||
const onStopHooks = new Set(); | ||
function onStop(hook) { | ||
onStopHooks.add(hook); | ||
} | ||
exports.onStop = onStop; | ||
async function startAll() { | ||
const appList = []; | ||
await Promise.all(Object.keys(exports.apps).map(async (id) => { | ||
const app = exports.apps[id]; | ||
await app.start(); | ||
appList.push(app); | ||
})); | ||
await Promise.all(exports.appList.map(async (app) => app.start())); | ||
for (const hook of onStartHooks) { | ||
hook(...appList); | ||
hook(...exports.appList); | ||
} | ||
@@ -57,7 +47,23 @@ } | ||
function stopAll() { | ||
for (const id in exports.apps) { | ||
exports.apps[id].stop(); | ||
exports.appList.forEach(app => app.stop()); | ||
for (const hook of onStopHooks) { | ||
hook(...exports.appList); | ||
} | ||
} | ||
exports.stopAll = stopAll; | ||
let getSelfIdsPromise; | ||
async function getSelfIds() { | ||
if (!getSelfIdsPromise) { | ||
getSelfIdsPromise = Promise.all(exports.appList.map(async (app) => { | ||
if (app.selfId) | ||
return; | ||
const info = await app.sender.getLoginInfo(); | ||
app.selfId = info.userId; | ||
app._registerSelfId(); | ||
})); | ||
} | ||
await getSelfIdsPromise; | ||
return selfIds; | ||
} | ||
exports.getSelfIds = getSelfIds; | ||
class App extends context_1.Context { | ||
@@ -226,2 +232,3 @@ constructor(options = {}) { | ||
}; | ||
exports.appList.push(this); | ||
if (options.database) | ||
@@ -245,4 +252,4 @@ this.database = database_1.createDatabase(options.database); | ||
_registerSelfId() { | ||
exports.apps[this.options.selfId] = this; | ||
exports.selfIds.push(this.options.selfId); | ||
exports.appMap[this.options.selfId] = this; | ||
selfIds.push(this.options.selfId); | ||
const atMeRE = `\\[CQ:at,qq=${this.options.selfId}\\]`; | ||
@@ -249,0 +256,0 @@ if (this.app.options.name) { |
/// <reference types="node" /> | ||
import WebSocket from 'ws'; | ||
import { Response } from 'express'; | ||
import { Express, Response } from 'express'; | ||
import * as http from 'http'; | ||
import { VersionInfo } from './meta'; | ||
import { App } from './app'; | ||
import { CQResponse } from './sender'; | ||
export declare abstract class Server { | ||
protected _apps: App[]; | ||
apps: App[]; | ||
version: VersionInfo; | ||
readonly type: ServerType; | ||
private _appMap; | ||
@@ -19,4 +22,5 @@ private _isListening; | ||
export declare class HttpServer extends Server { | ||
express: import("express-serve-static-core").Express; | ||
express: Express; | ||
httpServer: http.Server; | ||
readonly type = "http"; | ||
constructor(app: App); | ||
@@ -28,2 +32,3 @@ _listen(): Promise<void>; | ||
socket: WebSocket; | ||
readonly type = "ws"; | ||
private _listeners; | ||
@@ -30,0 +35,0 @@ constructor(app: App); |
@@ -17,3 +17,3 @@ "use strict"; | ||
constructor(app) { | ||
this._apps = []; | ||
this.apps = []; | ||
this._appMap = {}; | ||
@@ -26,3 +26,3 @@ this._isListening = false; | ||
if (!this._appMap[meta.selfId]) { | ||
const index = this._apps.findIndex(app => !app.options.selfId); | ||
const index = this.apps.findIndex(app => !app.options.selfId); | ||
if (index < 0) { | ||
@@ -33,5 +33,5 @@ if (res) | ||
} | ||
this._appMap[meta.selfId] = this._apps[index]; | ||
this._apps[index].options.selfId = meta.selfId; | ||
this._apps[index]._registerSelfId(); | ||
this._appMap[meta.selfId] = this.apps[index]; | ||
this.apps[index].options.selfId = meta.selfId; | ||
this.apps[index]._registerSelfId(); | ||
} | ||
@@ -45,3 +45,3 @@ const app = this._appMap[meta.selfId]; | ||
bind(app) { | ||
this._apps.push(app); | ||
this.apps.push(app); | ||
if (app.options.selfId) { | ||
@@ -57,3 +57,3 @@ this._appMap[app.options.selfId] = app; | ||
await this._listen(); | ||
for (const app of this._apps) { | ||
for (const app of this.apps) { | ||
app.receiver.emit('connected', app); | ||
@@ -67,3 +67,5 @@ } | ||
super(app); | ||
// https://github.com/microsoft/TypeScript/issues/31280 | ||
this.express = express_1.default().use(body_parser_1.json()); | ||
this.type = 'http'; | ||
if (app.options.secret) { | ||
@@ -86,3 +88,3 @@ this.express.use((req, res, next) => { | ||
async _listen() { | ||
const { port } = this._apps[0].options; | ||
const { port } = this.apps[0].options; | ||
this.httpServer = this.express.listen(port); | ||
@@ -102,2 +104,3 @@ showServerLog('listen to port', port); | ||
super(app); | ||
this.type = 'ws'; | ||
this._listeners = {}; | ||
@@ -109,18 +112,2 @@ this.socket = new ws_1.default(app.options.wsServer, { | ||
}); | ||
this.socket.on('message', (data) => { | ||
data = data.toString(); | ||
let parsed; | ||
try { | ||
parsed = JSON.parse(data); | ||
} | ||
catch (error) { | ||
throw new Error(data); | ||
} | ||
if ('post_type' in parsed) { | ||
this._handleData(parsed); | ||
} | ||
else if (parsed.echo in this._listeners) { | ||
this._listeners[parsed.echo](parsed); | ||
} | ||
}); | ||
} | ||
@@ -137,9 +124,43 @@ send(data) { | ||
} | ||
async _listen() { | ||
await new Promise((resolve, reject) => { | ||
this.socket.once('open', resolve); | ||
_listen() { | ||
return new Promise((resolve, reject) => { | ||
this.socket.once('error', reject); | ||
this.socket.once('open', () => { | ||
this.socket.send(JSON.stringify({ | ||
action: 'get_version_info', | ||
echo: -1, | ||
}), (error) => { | ||
if (error) | ||
reject(error); | ||
}); | ||
let resolved = false; | ||
this.socket.on('message', (data) => { | ||
data = data.toString(); | ||
let parsed; | ||
try { | ||
parsed = JSON.parse(data); | ||
} | ||
catch (error) { | ||
throw new Error(data); | ||
} | ||
if (!resolved) { | ||
resolved = true; | ||
const { wsServer } = this.apps[0].options; | ||
showServerLog('connect to ws server:', wsServer); | ||
resolve(); | ||
} | ||
if ('post_type' in parsed) { | ||
this._handleData(parsed); | ||
} | ||
else { | ||
if (parsed.echo === -1) { | ||
this.version = koishi_utils_1.camelCase(parsed.data); | ||
} | ||
if (parsed.echo in this._listeners) { | ||
this._listeners[parsed.echo](parsed); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
const { wsServer } = this._apps[0].options; | ||
showServerLog('connect to ws server:', wsServer); | ||
} | ||
@@ -146,0 +167,0 @@ close() { |
{ | ||
"name": "koishi-core", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"main": "dist/index.js", | ||
@@ -53,3 +53,3 @@ "typings": "dist/index.d.ts", | ||
"fs-extra": "^8.1.0", | ||
"koishi-utils": "^0.1.6", | ||
"koishi-utils": "^0.1.7", | ||
"leven": "^3.1.0", | ||
@@ -56,0 +56,0 @@ "ws": "^7.2.0" |
Sorry, the diff of this file is not supported yet
234065
28
2191
Updatedkoishi-utils@^0.1.7