@azteam/express 1.2.470 → 1.2.472

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.
@@ -1,183 +0,0 @@
1
- import {createAdapter} from '@socket.io/redis-adapter';
2
- import bodyParser from 'body-parser';
3
- import cookieParser from 'cookie-parser';
4
- import express from 'express';
5
- import fs from 'fs';
6
- import http from 'http';
7
- import _ from 'lodash';
8
- import socketIO from 'socket.io';
9
-
10
- const wrap = (middleware) => (socket, next) => middleware(socket.request, {}, next);
11
-
12
- class SocketServer {
13
- constructor(currentDir = '', options = {}) {
14
- this.options = {
15
- redisConfig: null,
16
- ...options,
17
- };
18
-
19
- this.middlewares = [];
20
- this.controllers = [];
21
- this.whiteList = [];
22
- this.debug = process.env.NODE_ENV === 'development';
23
- this.initController(currentDir);
24
- }
25
-
26
- setCallbackError(callback = null) {
27
- this.callbackError = callback;
28
- return this;
29
- }
30
-
31
- setWhiteList(whiteList) {
32
- this.whiteList = whiteList;
33
- return this;
34
- }
35
-
36
- setDebug(debug) {
37
- this.debug = debug;
38
- return this;
39
- }
40
-
41
- addController(name, version, controller) {
42
- this.controllers.push({
43
- name,
44
- version,
45
- controller,
46
- });
47
- return this;
48
- }
49
-
50
- initController(apiDir) {
51
- if (apiDir) {
52
- const controllerDirs = fs.readdirSync(apiDir);
53
-
54
- for (const dirName of controllerDirs) {
55
- if (fs.statSync(`${apiDir}/${dirName}`).isDirectory()) {
56
- const versionDirs = fs.readdirSync(`${apiDir}/${dirName}`);
57
-
58
- for (const versionName of versionDirs) {
59
- // eslint-disable-next-line import/no-dynamic-require,global-require
60
- const controller = require(`${apiDir}/${dirName}/${versionName}/controller`).default;
61
- this.addController(dirName, versionName, controller);
62
- }
63
- }
64
- }
65
- }
66
- return this;
67
- }
68
-
69
- addGlobalMiddleware(middleware) {
70
- this.middlewares.push(middleware);
71
- return this;
72
- }
73
-
74
- startPort(port) {
75
- if (!_.isEmpty(this.controllers)) {
76
- const WHITE_LIST = this.whiteList,
77
- server = http.Server(express()),
78
- io = socketIO(server, {
79
- wsEngine: 'eiows',
80
- perMessageDeflate: {
81
- threshold: 32768,
82
- },
83
- cors: {
84
- credentials: true,
85
- origin(origin, callback) {
86
- if (!origin || !WHITE_LIST.length || WHITE_LIST.some((re) => origin.endsWith(re))) {
87
- callback(null, true);
88
- } else {
89
- callback(new Error(`${origin} Not allowed by CORS`));
90
- }
91
- },
92
- },
93
- });
94
-
95
- if (this.options.redisConfig) {
96
- io.adapter(createAdapter(this.options.redisConfig));
97
- }
98
-
99
- const msg = [];
100
- _.map(this.controllers, (obj) => {
101
- const {controller} = obj;
102
-
103
- _.map(controller, (item, key) => {
104
- item.path = obj.version.startsWith('v') ? `/${obj.version}${item.path}` : item.path;
105
-
106
- const nsp = io.of(item.path),
107
- middlewares = [...this.middlewares, ...(item.middlewares || [])];
108
-
109
- nsp.use(wrap(bodyParser.urlencoded({limit: '5mb', extended: true})));
110
- nsp.use(wrap(bodyParser.json({limit: '5mb'})));
111
- nsp.use(wrap(cookieParser(process.env.SECRET_KEY)));
112
-
113
- _.map(middlewares, (middleware) => {
114
- nsp.use(wrap(middleware));
115
- });
116
-
117
- if (item.schedule) {
118
- item.schedule(io);
119
- }
120
-
121
- if (item.connection) {
122
- nsp.on('connection', (socket) => {
123
- item.connection(io, socket);
124
- });
125
- }
126
-
127
- msg.push({
128
- controller: obj.name,
129
- version: obj.version,
130
- path: item.path,
131
- });
132
- });
133
- });
134
-
135
- console.table(msg);
136
-
137
- server.on('listening', () => {
138
- this._alert('listening', `Server start at http://localhost:${server.address().port}`);
139
- });
140
-
141
- server.on('error', (error) => {
142
- if (error.syscall !== 'listen') {
143
- throw error;
144
- }
145
-
146
- let bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
147
-
148
- switch (error.code) {
149
- case 'EACCES':
150
- this._alert('EACCES', `${bind} requires elevated privileges`);
151
- process.exit(1);
152
- break;
153
- case 'EADDRINUSE':
154
- this._alert('EACCES', `${bind} is already in use`);
155
- process.exit(1);
156
- break;
157
- default:
158
- throw error;
159
- }
160
- });
161
-
162
- server.listen(port);
163
-
164
- return server;
165
- }
166
- throw new Error('No controllers in use');
167
- }
168
-
169
- setAlertCallback(callback) {
170
- this.alertCallback = callback;
171
- return this;
172
- }
173
-
174
- _alert(status, msg) {
175
- if (typeof this.alertCallback === 'function') {
176
- this.alertCallback(status, msg);
177
- } else {
178
- console.log(status, msg);
179
- }
180
- }
181
- }
182
-
183
- export default SocketServer;