monocart-reporter 1.7.2 → 1.7.4

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.
@@ -14,23 +14,6 @@ const defaultClientOptions = {
14
14
  timeout: 3000
15
15
  };
16
16
 
17
- const defaultStateOptions = {
18
-
19
- server: defaultServerOptions,
20
-
21
- // onReceive: function(... args) {
22
- // console.log('receive on server', args);
23
- // return ['custom response', ... args];
24
- // },
25
-
26
- // onClose: function(data, config) {
27
- // Object.assign(config.metadata, data);
28
- // },
29
-
30
- // key-value state data
31
- data: {}
32
- };
33
-
34
17
  // ===============================================================================
35
18
 
36
19
  const clientMap = new Map();
@@ -93,34 +76,41 @@ const getActions = (stateData) => {
93
76
  };
94
77
  };
95
78
 
79
+ const getResponseData = async (data, options) => {
80
+ let res;
81
+ // first argument is action name always
82
+ const action = data.shift();
83
+ if (action === 'send') {
84
+ // EC.logCyan('send', data[0]);
85
+ // send handler
86
+ if (typeof options.onReceive === 'function') {
87
+ res = await options.onReceive(... data);
88
+ }
89
+ return res;
90
+ }
91
+
92
+ // get/set/remove handler
93
+ const actions = getActions(options.data);
94
+ const handler = actions[action];
95
+ if (handler) {
96
+ res = handler.apply(options, data);
97
+ }
98
+ return res;
99
+ };
100
+
96
101
  const onMessage = async (ws, buf, options) => {
97
102
  const message = JSON.parse(buf.toString());
98
103
  if (!message) {
104
+ Util.logDebug(EC.red('invalid message buffer'));
99
105
  return;
100
106
  }
101
107
  const { id, data } = message;
102
108
  if (!id || !Array.isArray(data)) {
109
+ Util.logDebug(EC.red('invalid message id or data'));
103
110
  return;
104
111
  }
105
112
 
106
- // first argument is action name always
107
- const action = data.shift();
108
-
109
- let resData;
110
- if (action === 'send') {
111
- // send handler
112
- if (typeof options.onReceive === 'function') {
113
- resData = await options.onReceive.apply(options, data);
114
- }
115
- } else {
116
- // get/set/remove handler
117
- const actions = getActions(options.data);
118
- const handler = actions[action];
119
- if (handler) {
120
- resData = handler.apply(options, data);
121
- }
122
- }
123
-
113
+ const resData = await getResponseData(data, options);
124
114
  const response = JSON.stringify({
125
115
  id,
126
116
  data: resData
@@ -134,13 +124,34 @@ const onMessage = async (ws, buf, options) => {
134
124
 
135
125
  const createStateServer = (stateOptions) => {
136
126
 
127
+ const defaultStateOptions = {
128
+
129
+ // key-value state data
130
+ data: {},
131
+
132
+ server: {}
133
+
134
+ // onReceive: function(... args) {
135
+ // console.log('receive on server', args);
136
+ // return ['custom response', ... args];
137
+ // },
138
+
139
+ // onClose: function(data, config) {
140
+ // Object.assign(config.metadata, data);
141
+ // },
142
+
143
+ };
144
+
137
145
  const options = Util.mergeOption(defaultStateOptions, stateOptions);
138
146
 
139
147
  if (!options.data || typeof options.data !== 'object') {
140
148
  options.data = {};
141
149
  }
142
150
 
143
- const serverOptions = options.server;
151
+ const serverOptions = {
152
+ ... defaultServerOptions,
153
+ ... options.server
154
+ };
144
155
 
145
156
  const wss = new WebSocketServer(serverOptions);
146
157
 
@@ -153,6 +164,8 @@ const createStateServer = (stateOptions) => {
153
164
 
154
165
  wss.on('connection', (ws) => {
155
166
 
167
+ // Util.logDebug('a client connected');
168
+
156
169
  // data {Buffer|ArrayBuffer|Buffer[]}
157
170
  ws.on('message', (data) => {
158
171
  // console.log(data, isBinary);
@@ -169,7 +182,7 @@ const createStateServer = (stateOptions) => {
169
182
  close: async (config) => {
170
183
  wss.close();
171
184
  if (typeof options.onClose === 'function') {
172
- await options.onClose.call(options, options.data, config);
185
+ await options.onClose(options.data, config);
173
186
  }
174
187
  }
175
188
  };