onebots 0.0.13 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +63 -63
  3. package/lib/bin.d.ts +2 -2
  4. package/lib/bin.js +5 -5
  5. package/lib/db.d.ts +19 -19
  6. package/lib/db.js +91 -91
  7. package/lib/index.d.ts +5 -5
  8. package/lib/index.js +21 -21
  9. package/lib/onebot.d.ts +44 -44
  10. package/lib/onebot.js +86 -86
  11. package/lib/server/app.d.ts +52 -52
  12. package/lib/server/app.js +232 -232
  13. package/lib/server/router.d.ts +9 -9
  14. package/lib/server/router.js +32 -32
  15. package/lib/service/V11/action/common.d.ts +65 -66
  16. package/lib/service/V11/action/common.js +142 -142
  17. package/lib/service/V11/action/friend.d.ts +38 -38
  18. package/lib/service/V11/action/friend.js +44 -44
  19. package/lib/service/V11/action/group.d.ts +104 -104
  20. package/lib/service/V11/action/group.js +138 -138
  21. package/lib/service/V11/action/index.d.ts +9 -9
  22. package/lib/service/V11/action/index.js +10 -10
  23. package/lib/service/V11/config.d.ts +10 -10
  24. package/lib/service/V11/config.js +2 -2
  25. package/lib/service/V11/index.d.ts +95 -95
  26. package/lib/service/V11/index.js +499 -488
  27. package/lib/service/V12/action/common.d.ts +33 -33
  28. package/lib/service/V12/action/common.js +108 -108
  29. package/lib/service/V12/action/friend.d.ts +13 -13
  30. package/lib/service/V12/action/friend.js +15 -15
  31. package/lib/service/V12/action/group.d.ts +104 -104
  32. package/lib/service/V12/action/group.js +138 -138
  33. package/lib/service/V12/action/guild.d.ts +2 -2
  34. package/lib/service/V12/action/guild.js +6 -6
  35. package/lib/service/V12/action/index.d.ts +10 -10
  36. package/lib/service/V12/action/index.js +11 -11
  37. package/lib/service/V12/config.d.ts +17 -17
  38. package/lib/service/V12/config.js +2 -2
  39. package/lib/service/V12/index.d.ts +102 -102
  40. package/lib/service/V12/index.js +538 -537
  41. package/lib/types.d.ts +3 -3
  42. package/lib/types.js +2 -2
  43. package/lib/utils.d.ts +14 -14
  44. package/lib/utils.js +150 -150
  45. package/package.json +58 -58
  46. package/lib/config.sample.yaml +0 -31
package/lib/server/app.js CHANGED
@@ -1,232 +1,232 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- var _a;
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.defineConfig = exports.createApp = exports.App = void 0;
31
- const koa_1 = __importDefault(require("koa"));
32
- const os = __importStar(require("os"));
33
- const fs_1 = require("fs");
34
- const log4js_1 = require("log4js");
35
- const path_1 = require("path");
36
- const http_1 = require("http");
37
- const js_yaml_1 = __importDefault(require("js-yaml"));
38
- const koa_bodyparser_1 = __importDefault(require("koa-bodyparser"));
39
- const onebot_1 = require("../onebot");
40
- const mime = require('mime-types');
41
- const utils_1 = require("../utils");
42
- const router_1 = require("./router");
43
- const fs_2 = require("fs");
44
- const V11_1 = require("../service/V11");
45
- const V12_1 = require("../service/V12");
46
- class App extends koa_1.default {
47
- constructor(config = {}) {
48
- super(config);
49
- this.oneBots = [];
50
- this.config = (0, utils_1.deepMerge)((0, utils_1.deepClone)(App.defaultConfig), config);
51
- this.logger = (0, log4js_1.getLogger)('[oicq-oneBot]');
52
- this.logger.level = this.config.log_level;
53
- this.router = new router_1.Router({ prefix: config.path });
54
- this.use((0, koa_bodyparser_1.default)())
55
- .use(this.router.routes())
56
- .use(this.router.allowedMethods());
57
- this.httpServer = (0, http_1.createServer)(this.callback());
58
- this.createOneBots();
59
- }
60
- getLogger(uin, version = '') {
61
- const logger = (0, log4js_1.getLogger)(`[oicq-oneBot${version}:${uin}]`);
62
- logger.level = this.config.log_level;
63
- return logger;
64
- }
65
- getBots() {
66
- const result = new Map();
67
- Object.entries(this.config).forEach(([key, value]) => {
68
- if (parseInt(key).toString() === key && value && typeof value === 'object') {
69
- result.set(parseInt(key), value);
70
- }
71
- });
72
- return result;
73
- }
74
- createOneBots() {
75
- for (const [uin, config] of this.getBots()) {
76
- this.createOneBot(uin, config);
77
- }
78
- }
79
- addAccount(uin, config) {
80
- if (typeof uin !== "number")
81
- uin = Number(uin);
82
- if (Number.isNaN(uin))
83
- throw new Error('无效的账号');
84
- if (this.oneBots.find(oneBot => oneBot.uin === uin))
85
- throw new Error('账户已存在');
86
- this.config[uin] = config;
87
- const oneBot = this.createOneBot(uin, config);
88
- oneBot.startListen();
89
- (0, fs_1.writeFileSync)(App.configPath, js_yaml_1.default.dump((0, utils_1.deepClone)(this.config)));
90
- }
91
- updateAccount(uin, config) {
92
- if (typeof uin !== "number")
93
- uin = Number(uin);
94
- if (Number.isNaN(uin))
95
- throw new Error('无效的账号');
96
- const oneBot = this.oneBots.find(oneBot => oneBot.uin === uin);
97
- if (!oneBot)
98
- return this.addAccount(uin, config);
99
- const newConfig = (0, utils_1.deepMerge)(this.config[uin], config);
100
- this.removeAccount(uin);
101
- this.addAccount(uin, newConfig);
102
- }
103
- removeAccount(uin, force) {
104
- if (typeof uin !== "number")
105
- uin = Number(uin);
106
- if (Number.isNaN(uin))
107
- throw new Error('无效的账号');
108
- const currentIdx = this.oneBots.findIndex(oneBot => oneBot.uin === uin);
109
- if (currentIdx < 0)
110
- throw new Error('账户不存在');
111
- const oneBot = this.oneBots[currentIdx];
112
- oneBot.stop(force);
113
- delete this.config[uin];
114
- this.oneBots.splice(currentIdx, 1);
115
- (0, fs_1.writeFileSync)(App.configPath, js_yaml_1.default.dump(this.config));
116
- }
117
- createOneBot(uin, config) {
118
- const oneBot = new onebot_1.OneBot(this, uin, config);
119
- this.oneBots.push(oneBot);
120
- return oneBot;
121
- }
122
- start() {
123
- for (const oneBot of this.oneBots) {
124
- oneBot.start();
125
- }
126
- this.httpServer.listen(this.config.port);
127
- this.router.get('/list', (ctx) => {
128
- ctx.body = this.oneBots.map(bot => {
129
- return {
130
- uin: bot.uin,
131
- config: bot.config.map(c => (0, utils_1.protectedFields)(c, 'password', "access_token")),
132
- urls: bot.config.map(c => `/${c.version}/${bot.uin}`)
133
- };
134
- });
135
- });
136
- this.router.get('/qrcode', (ctx) => {
137
- const { uin } = ctx.query;
138
- const uinUrl = (0, path_1.join)(App.configDir, 'data', uin);
139
- if (!(0, fs_1.existsSync)(uinUrl)) {
140
- return ctx.res.writeHead(400).end('未登录');
141
- }
142
- const qrcodePath = (0, path_1.join)(App.configDir, 'data', uin, 'qrcode.png');
143
- let file = null;
144
- try {
145
- file = (0, fs_2.readFileSync)(qrcodePath); //读取文件
146
- }
147
- catch (error) {
148
- return ctx.res.writeHead(404).end(error.message);
149
- }
150
- let mimeType = mime.lookup(qrcodePath); //读取图片文件类型
151
- ctx.set('content-type', mimeType); //设置返回类型
152
- ctx.body = file; //返回图片
153
- });
154
- this.router.get('/detail', (ctx) => {
155
- let { uin } = ctx.request.query;
156
- const oneBot = this.oneBots.find(bot => bot.uin === Number(uin));
157
- ctx.body = {
158
- uin,
159
- config: oneBot.config.map(c => (0, utils_1.protectedFields)(c, 'password', "access_token")),
160
- urls: oneBot.config.map(c => `/${uin}/${c.version}`)
161
- };
162
- });
163
- this.router.post('/add', (ctx, next) => {
164
- const { uin, ...config } = ctx.request.body;
165
- try {
166
- this.addAccount(uin, config);
167
- ctx.body = `添加成功`;
168
- }
169
- catch (e) {
170
- ctx.body = e.message;
171
- }
172
- });
173
- this.router.post('/edit', (ctx, next) => {
174
- const { uin, ...config } = ctx.request.body;
175
- try {
176
- this.updateAccount(Number(uin), config);
177
- ctx.body = `修改成功`;
178
- }
179
- catch (e) {
180
- ctx.body = e.message;
181
- }
182
- });
183
- this.router.get('/remove', (ctx, next) => {
184
- const { uin, force } = ctx.request.query;
185
- try {
186
- this.removeAccount(Number(uin), Boolean(force));
187
- ctx.body = `移除成功`;
188
- }
189
- catch (e) {
190
- console.log(e);
191
- ctx.body = e.message;
192
- }
193
- });
194
- this.logger.mark(`server listen at http://0.0.0.0:${this.config.port}/${this.config.path ? this.config.path : ''}`);
195
- }
196
- }
197
- exports.App = App;
198
- _a = App;
199
- App.configDir = (0, path_1.join)(os.homedir(), '.onebots');
200
- App.configPath = (0, path_1.join)(_a.configDir, 'config.yaml');
201
- function createApp(config = 'config.yaml') {
202
- if (typeof config === 'string') {
203
- if (!(0, fs_1.existsSync)(App.configDir)) {
204
- (0, fs_1.mkdirSync)(App.configDir);
205
- }
206
- App.configPath = (0, path_1.join)(App.configDir, config);
207
- if (!(0, fs_1.existsSync)(App.configPath)) {
208
- (0, fs_1.copyFileSync)((0, path_1.resolve)(__dirname, '../config.sample.yaml'), App.configPath);
209
- console.log('未找到对应配置文件,已自动生成默认配置文件,请修改配置文件后重新启动');
210
- console.log(`配置文件在: ${App.configPath}`);
211
- process.exit();
212
- }
213
- config = js_yaml_1.default.load((0, fs_2.readFileSync)(App.configPath, 'utf8'));
214
- }
215
- return new App(config);
216
- }
217
- exports.createApp = createApp;
218
- function defineConfig(config) {
219
- return config;
220
- }
221
- exports.defineConfig = defineConfig;
222
- (function (App) {
223
- App.defaultConfig = {
224
- port: 6727,
225
- platform: 5,
226
- general: {
227
- V11: V11_1.V11.defaultConfig,
228
- V12: V12_1.V12.defaultConfig
229
- },
230
- log_level: 'info',
231
- };
232
- })(App = exports.App || (exports.App = {}));
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ var _a;
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.defineConfig = exports.createApp = exports.App = void 0;
31
+ const koa_1 = __importDefault(require("koa"));
32
+ const os = __importStar(require("os"));
33
+ const fs_1 = require("fs");
34
+ const log4js_1 = require("log4js");
35
+ const path_1 = require("path");
36
+ const http_1 = require("http");
37
+ const js_yaml_1 = __importDefault(require("js-yaml"));
38
+ const koa_bodyparser_1 = __importDefault(require("koa-bodyparser"));
39
+ const onebot_1 = require("../onebot");
40
+ const mime = require('mime-types');
41
+ const utils_1 = require("../utils");
42
+ const router_1 = require("./router");
43
+ const fs_2 = require("fs");
44
+ const V11_1 = require("../service/V11");
45
+ const V12_1 = require("../service/V12");
46
+ class App extends koa_1.default {
47
+ constructor(config = {}) {
48
+ super(config);
49
+ this.oneBots = [];
50
+ this.config = (0, utils_1.deepMerge)((0, utils_1.deepClone)(App.defaultConfig), config);
51
+ this.logger = (0, log4js_1.getLogger)('[oicq-oneBot]');
52
+ this.logger.level = this.config.log_level;
53
+ this.router = new router_1.Router({ prefix: config.path });
54
+ this.use((0, koa_bodyparser_1.default)())
55
+ .use(this.router.routes())
56
+ .use(this.router.allowedMethods());
57
+ this.httpServer = (0, http_1.createServer)(this.callback());
58
+ this.createOneBots();
59
+ }
60
+ getLogger(uin, version = '') {
61
+ const logger = (0, log4js_1.getLogger)(`[oicq-oneBot${version}:${uin}]`);
62
+ logger.level = this.config.log_level;
63
+ return logger;
64
+ }
65
+ getBots() {
66
+ const result = new Map();
67
+ Object.entries(this.config).forEach(([key, value]) => {
68
+ if (parseInt(key).toString() === key && value && typeof value === 'object') {
69
+ result.set(parseInt(key), value);
70
+ }
71
+ });
72
+ return result;
73
+ }
74
+ createOneBots() {
75
+ for (const [uin, config] of this.getBots()) {
76
+ this.createOneBot(uin, config);
77
+ }
78
+ }
79
+ addAccount(uin, config) {
80
+ if (typeof uin !== "number")
81
+ uin = Number(uin);
82
+ if (Number.isNaN(uin))
83
+ throw new Error('无效的账号');
84
+ if (this.oneBots.find(oneBot => oneBot.uin === uin))
85
+ throw new Error('账户已存在');
86
+ this.config[uin] = config;
87
+ const oneBot = this.createOneBot(uin, config);
88
+ oneBot.startListen();
89
+ (0, fs_1.writeFileSync)(App.configPath, js_yaml_1.default.dump((0, utils_1.deepClone)(this.config)));
90
+ }
91
+ updateAccount(uin, config) {
92
+ if (typeof uin !== "number")
93
+ uin = Number(uin);
94
+ if (Number.isNaN(uin))
95
+ throw new Error('无效的账号');
96
+ const oneBot = this.oneBots.find(oneBot => oneBot.uin === uin);
97
+ if (!oneBot)
98
+ return this.addAccount(uin, config);
99
+ const newConfig = (0, utils_1.deepMerge)(this.config[uin], config);
100
+ this.removeAccount(uin);
101
+ this.addAccount(uin, newConfig);
102
+ }
103
+ removeAccount(uin, force) {
104
+ if (typeof uin !== "number")
105
+ uin = Number(uin);
106
+ if (Number.isNaN(uin))
107
+ throw new Error('无效的账号');
108
+ const currentIdx = this.oneBots.findIndex(oneBot => oneBot.uin === uin);
109
+ if (currentIdx < 0)
110
+ throw new Error('账户不存在');
111
+ const oneBot = this.oneBots[currentIdx];
112
+ oneBot.stop(force);
113
+ delete this.config[uin];
114
+ this.oneBots.splice(currentIdx, 1);
115
+ (0, fs_1.writeFileSync)(App.configPath, js_yaml_1.default.dump(this.config));
116
+ }
117
+ createOneBot(uin, config) {
118
+ const oneBot = new onebot_1.OneBot(this, uin, config);
119
+ this.oneBots.push(oneBot);
120
+ return oneBot;
121
+ }
122
+ start() {
123
+ for (const oneBot of this.oneBots) {
124
+ oneBot.start();
125
+ }
126
+ this.httpServer.listen(this.config.port);
127
+ this.router.get('/list', (ctx) => {
128
+ ctx.body = this.oneBots.map(bot => {
129
+ return {
130
+ uin: bot.uin,
131
+ config: bot.config.map(c => (0, utils_1.protectedFields)(c, 'password', "access_token")),
132
+ urls: bot.config.map(c => `/${c.version}/${bot.uin}`)
133
+ };
134
+ });
135
+ });
136
+ this.router.get('/qrcode', (ctx) => {
137
+ const { uin } = ctx.query;
138
+ const uinUrl = (0, path_1.join)(App.configDir, 'data', uin);
139
+ if (!(0, fs_1.existsSync)(uinUrl)) {
140
+ return ctx.res.writeHead(400).end('未登录');
141
+ }
142
+ const qrcodePath = (0, path_1.join)(App.configDir, 'data', uin, 'qrcode.png');
143
+ let file = null;
144
+ try {
145
+ file = (0, fs_2.readFileSync)(qrcodePath); //读取文件
146
+ }
147
+ catch (error) {
148
+ return ctx.res.writeHead(404).end(error.message);
149
+ }
150
+ let mimeType = mime.lookup(qrcodePath); //读取图片文件类型
151
+ ctx.set('content-type', mimeType); //设置返回类型
152
+ ctx.body = file; //返回图片
153
+ });
154
+ this.router.get('/detail', (ctx) => {
155
+ let { uin } = ctx.request.query;
156
+ const oneBot = this.oneBots.find(bot => bot.uin === Number(uin));
157
+ ctx.body = {
158
+ uin,
159
+ config: oneBot.config.map(c => (0, utils_1.protectedFields)(c, 'password', "access_token")),
160
+ urls: oneBot.config.map(c => `/${uin}/${c.version}`)
161
+ };
162
+ });
163
+ this.router.post('/add', (ctx, next) => {
164
+ const { uin, ...config } = (ctx.request.body || {});
165
+ try {
166
+ this.addAccount(uin, config);
167
+ ctx.body = `添加成功`;
168
+ }
169
+ catch (e) {
170
+ ctx.body = e.message;
171
+ }
172
+ });
173
+ this.router.post('/edit', (ctx, next) => {
174
+ const { uin, ...config } = (ctx.request.body || {});
175
+ try {
176
+ this.updateAccount(Number(uin), config);
177
+ ctx.body = `修改成功`;
178
+ }
179
+ catch (e) {
180
+ ctx.body = e.message;
181
+ }
182
+ });
183
+ this.router.get('/remove', (ctx, next) => {
184
+ const { uin, force } = ctx.request.query;
185
+ try {
186
+ this.removeAccount(Number(uin), Boolean(force));
187
+ ctx.body = `移除成功`;
188
+ }
189
+ catch (e) {
190
+ console.log(e);
191
+ ctx.body = e.message;
192
+ }
193
+ });
194
+ this.logger.mark(`server listen at http://0.0.0.0:${this.config.port}/${this.config.path ? this.config.path : ''}`);
195
+ }
196
+ }
197
+ exports.App = App;
198
+ _a = App;
199
+ App.configDir = (0, path_1.join)(os.homedir(), '.onebots');
200
+ App.configPath = (0, path_1.join)(_a.configDir, 'config.yaml');
201
+ function createApp(config = 'config.yaml') {
202
+ if (typeof config === 'string') {
203
+ if (!(0, fs_1.existsSync)(App.configDir)) {
204
+ (0, fs_1.mkdirSync)(App.configDir);
205
+ }
206
+ App.configPath = (0, path_1.join)(App.configDir, config);
207
+ if (!(0, fs_1.existsSync)(App.configPath)) {
208
+ (0, fs_1.copyFileSync)((0, path_1.resolve)(__dirname, '../config.sample.yaml'), App.configPath);
209
+ console.log('未找到对应配置文件,已自动生成默认配置文件,请修改配置文件后重新启动');
210
+ console.log(`配置文件在: ${App.configPath}`);
211
+ process.exit();
212
+ }
213
+ config = js_yaml_1.default.load((0, fs_2.readFileSync)(App.configPath, 'utf8'));
214
+ }
215
+ return new App(config);
216
+ }
217
+ exports.createApp = createApp;
218
+ function defineConfig(config) {
219
+ return config;
220
+ }
221
+ exports.defineConfig = defineConfig;
222
+ (function (App) {
223
+ App.defaultConfig = {
224
+ port: 6727,
225
+ platform: 5,
226
+ general: {
227
+ V11: V11_1.V11.defaultConfig,
228
+ V12: V12_1.V12.defaultConfig
229
+ },
230
+ log_level: 'info',
231
+ };
232
+ })(App = exports.App || (exports.App = {}));
@@ -1,9 +1,9 @@
1
- /// <reference types="node" />
2
- /// <reference types="koa__router" />
3
- import KoaRouter from '@koa/router';
4
- import { WebSocketServer } from "ws";
5
- import http from "http";
6
- export declare class Router extends KoaRouter {
7
- wsStack: WebSocketServer[];
8
- ws(path: string, server: http.Server): import("ws").Server<import("ws").WebSocket>;
9
- }
1
+ /// <reference types="node" />
2
+ /// <reference types="koa__router" />
3
+ import KoaRouter from '@koa/router';
4
+ import { WebSocketServer } from "ws";
5
+ import http from "http";
6
+ export declare class Router extends KoaRouter {
7
+ wsStack: WebSocketServer[];
8
+ ws(path: string, server: http.Server): import("ws").Server<import("ws").WebSocket>;
9
+ }
@@ -1,32 +1,32 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Router = void 0;
7
- const router_1 = __importDefault(require("@koa/router"));
8
- const ws_1 = require("ws");
9
- const url_1 = require("url");
10
- class Router extends router_1.default {
11
- constructor() {
12
- super(...arguments);
13
- this.wsStack = [];
14
- }
15
- ws(path, server) {
16
- const wsServer = new ws_1.WebSocketServer({ noServer: true, path });
17
- this.wsStack.push(wsServer);
18
- server.on('upgrade', (request, socket, head) => {
19
- const { pathname } = (0, url_1.parse)(request.url);
20
- if (this.wsStack.findIndex(wss => wss.options.path === path) === -1) {
21
- socket.destroy();
22
- }
23
- else if (pathname === path) {
24
- wsServer.handleUpgrade(request, socket, head, function done(ws) {
25
- wsServer.emit('connection', ws, request);
26
- });
27
- }
28
- });
29
- return wsServer;
30
- }
31
- }
32
- exports.Router = Router;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Router = void 0;
7
+ const router_1 = __importDefault(require("@koa/router"));
8
+ const ws_1 = require("ws");
9
+ const url_1 = require("url");
10
+ class Router extends router_1.default {
11
+ constructor() {
12
+ super(...arguments);
13
+ this.wsStack = [];
14
+ }
15
+ ws(path, server) {
16
+ const wsServer = new ws_1.WebSocketServer({ noServer: true, path });
17
+ this.wsStack.push(wsServer);
18
+ server.on('upgrade', (request, socket, head) => {
19
+ const { pathname } = (0, url_1.parse)(request.url);
20
+ if (this.wsStack.findIndex(wss => wss.options.path === path) === -1) {
21
+ socket.destroy();
22
+ }
23
+ else if (pathname === path) {
24
+ wsServer.handleUpgrade(request, socket, head, function done(ws) {
25
+ wsServer.emit('connection', ws, request);
26
+ });
27
+ }
28
+ });
29
+ return wsServer;
30
+ }
31
+ }
32
+ exports.Router = Router;