birdpack 1.0.10 → 1.0.12

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.
package/lib/method.js CHANGED
@@ -1,19 +1,28 @@
1
+ const { errorMonitor } = require('events');
1
2
  const tools = require('./tools');
2
- const METHOD = ['get','post','put','patch','delete','head','options'];
3
+ const METHOD = ['get','post','put','patch','delete','options'];
3
4
  const fs = require('fs');
5
+ let crypto = require('crypto');
4
6
 
5
- module.exports = {
7
+ const serviceMethod = {
6
8
  METHOD,
7
9
  basic:(app, use, {domain, next})=>{
8
10
  for(let x of METHOD){
9
11
  use[x] = (url, callback)=>{
10
- app.set({
11
- domain,
12
- method:x,
13
- url,
14
- next,
15
- callback:app.setCall(callback)
16
- });
12
+ let idCallback = app.setCall(callback);
13
+ if(!Array.isArray(url)){
14
+ url = [url];
15
+ }
16
+
17
+ for(let y of url){
18
+ app.set({
19
+ domain,
20
+ method:x,
21
+ url:y,
22
+ next,
23
+ callback:idCallback
24
+ });
25
+ }
17
26
  return use;
18
27
  }
19
28
  }
@@ -64,7 +73,7 @@ module.exports = {
64
73
  return this;
65
74
  }
66
75
  },
67
- websocket:(app, use, domain)=>{
76
+ websocket:(app, use, {domain, next})=>{
68
77
  return (url, a, b, c)=>{
69
78
  let typeA = typeof a == 'function';
70
79
  let typeB = typeof b == 'function';
@@ -95,7 +104,8 @@ module.exports = {
95
104
  };
96
105
 
97
106
  app.setUpgrade({
98
- domain,
107
+ domain,
108
+ next,
99
109
  url,
100
110
  callback: app.setCall(callback)
101
111
  });
@@ -151,4 +161,97 @@ module.exports = {
151
161
  return use;
152
162
  };
153
163
  },
154
- };
164
+
165
+ apply:(app, use, {domain, next})=>{
166
+ use.apply = (pack)=>{
167
+ if(pack.name == 'R938_PACK' && pack.data && Array.isArray(pack.data.upgrade) && Array.isArray(pack.data.method) && Array.isArray(pack.data.callback)){
168
+ for(let x of pack.data.callback){
169
+ app.setCall(x);
170
+ }
171
+
172
+ for(let x of pack.data.upgrade){
173
+ app.setUpgrade({
174
+ domain,
175
+ next,
176
+ url:x.url,
177
+ callback:x.callback
178
+ });
179
+ }
180
+ for(let x of pack.data.method){
181
+ app.set({
182
+ domain,
183
+ next,
184
+ method:x.method,
185
+ url:x.url,
186
+ callback:x.callback
187
+ });
188
+ }
189
+ return use;
190
+ }
191
+ return false;
192
+ };
193
+ },
194
+ pack:()=>{
195
+ let idcal = 0;
196
+ let pack = {
197
+ id:crypto.randomBytes(16).toString('hex'),
198
+ name:'R938_PACK',
199
+ data:{
200
+ upgrade:[],
201
+ method:[],
202
+ callback:[],
203
+ }
204
+ };
205
+
206
+ let vmApp = {
207
+ setCall:(call)=>{
208
+ let id = `r938_${pack.id}_${idcal}`;
209
+ pack.data.callback.push({
210
+ id,
211
+ callback:call
212
+ });
213
+ idcal++;
214
+ return id;
215
+ },
216
+ setUpgrade:({url, callback})=>{
217
+ pack.data.upgrade.push({url, callback});
218
+ },
219
+ set:({method, url, callback})=>{
220
+ pack.data.method.push({method, url, callback});
221
+ },
222
+ };
223
+
224
+ serviceMethod.maps(vmApp, pack);
225
+
226
+ return pack;
227
+ },
228
+ maps:(app, use, domain)=>{
229
+ serviceMethod.basic(app, use, {domain});
230
+ use.routes = serviceMethod.routes(use);
231
+ use.directory = serviceMethod.directory(use);
232
+ use.websocket = serviceMethod.websocket(app, use, {domain});
233
+
234
+ if(use.name !== 'R938_PACK'){
235
+ use.next = (n)=>{
236
+ if(typeof n !== 'function'){
237
+ return false;
238
+ }
239
+
240
+ let next = {
241
+ id:app.setCall(n)
242
+ };
243
+
244
+ serviceMethod.basic(app, next, {domain, next:next.id});
245
+ next.routes = serviceMethod.routes(next);
246
+ next.directory = serviceMethod.directory(next);
247
+ next.websocket = serviceMethod.websocket(app, next, {domain, next:next.id});
248
+ serviceMethod.apply(app, next, {domain, next:next.id});
249
+
250
+ return next;
251
+ }
252
+ serviceMethod.apply(app, use, {domain});
253
+ }
254
+ }
255
+ };
256
+
257
+ module.exports = serviceMethod;
package/lib/pack.js ADDED
@@ -0,0 +1,4 @@
1
+ const methodPlugin = require('./method');
2
+ module.exports = ()=>{
3
+ return methodPlugin.pack();
4
+ }
package/lib/server.js CHANGED
@@ -3,13 +3,16 @@ const core = require('./core');
3
3
  const websocket = require('./websocket');
4
4
  const powered = require('./powered');
5
5
  const methodPlugin = require('./method');
6
+ const packPlugin = require('./pack');
6
7
 
7
8
  module.exports = class{
8
9
  mapws = {}
9
10
  maps = {}
10
- calls = []
11
+ calls = {}
12
+ idcalls = 0;
11
13
  constructor(opt){
12
14
  this.opt = opt || {};
15
+ this.params = {};
13
16
 
14
17
  this.powered();
15
18
  this.dev();
@@ -19,10 +22,6 @@ module.exports = class{
19
22
  this.setupSSL();
20
23
  }
21
24
  dev(){
22
- if(typeof this.opt.log !== 'string'){
23
- this.opt.log = 'console';
24
- }
25
-
26
25
  if(this.opt.log == 'console'){
27
26
  this.log = (status, text)=>{
28
27
  let out = `[${status}] - ${text}`;
@@ -35,12 +34,13 @@ module.exports = class{
35
34
  file.write(`${out}\n`);
36
35
  }
37
36
  }else{
37
+ this.opt.log = 'false';
38
38
  this.log = ()=>{};
39
39
  }
40
40
 
41
41
  let traffic = this.opt.traffic;
42
42
  if(typeof traffic !== 'string'){
43
- traffic = '$time $status $method $path $ip $head(user-agent)';
43
+ traffic = '$time $status $host $method $path $ip';
44
44
  }
45
45
 
46
46
  let useTraffic = traffic.replace(/\$[a-zA-Z0-9\(\)\-]*/g, (a)=>{
@@ -48,6 +48,8 @@ module.exports = class{
48
48
  return '${time}';
49
49
  }else if(a == '$status'){
50
50
  return '${c.status}';
51
+ }else if(a == '$host'){
52
+ return '${c.host}';
51
53
  }else if(a == '$method'){
52
54
  return '${c.method}';
53
55
  }else if(a == '$path'){
@@ -73,21 +75,31 @@ module.exports = class{
73
75
  '-p':'port',
74
76
  };
75
77
  let data = false;
78
+ let id = false;
76
79
  for(let x of process.argv){
77
- if(data !== false){
78
- this.opt[data] = x;
80
+ if(data !== false && id !== false){
81
+ if(data === 'parameter'){
82
+ this.params[id] = x;
83
+ }else if(data === 'server'){
84
+ if(id == 'port'){
85
+ x = parseInt(x);
86
+ }
87
+ this.opt[id] = x;
88
+ }
89
+
90
+ id = false;
79
91
  data = false;
80
92
  }else if(cmd[x]){
81
- data = cmd[x];
82
- this.opt[data] = true;
93
+ id = cmd[x];
94
+ data = 'server';
95
+ }else if(x[0] === '-' && x[1] === '-'){
96
+ id = x.slice(2);
97
+ data = 'parameter';
83
98
  }
84
99
  }
85
100
  }
86
101
  setupMethod(){
87
- methodPlugin.basic(this, this, {domain:'*'});
88
- this.routes = methodPlugin.routes(this);
89
- this.directory = methodPlugin.directory(this);
90
- this.websocket = methodPlugin.websocket(this, this, '*');
102
+ methodPlugin.maps(this, this);
91
103
  }
92
104
  setupSSL(){
93
105
  if(this.opt.use !== 'https'){
@@ -126,15 +138,15 @@ module.exports = class{
126
138
  }
127
139
  }
128
140
  create(){
129
- let use = '';
141
+ let use = 'http';
130
142
  if(this.opt.use == 'http'){
131
143
  use = 'http';
132
144
  }else if(this.opt.use == 'https'){
133
145
  use = 'https';
134
- }else{
135
- return false;
136
146
  }
137
147
 
148
+ this.opt.use = use;
149
+
138
150
  let options = {};
139
151
 
140
152
  for(let x in this.opt){
@@ -213,6 +225,7 @@ module.exports = class{
213
225
  if(call && call.url){
214
226
  if(call.next !== false){
215
227
  return this.getCall(call.next)(cc, (data)=>{
228
+ cc.nextData = data;
216
229
  this.getCall(call.callback)(cc, data);
217
230
  });
218
231
  }else{
@@ -236,13 +249,20 @@ module.exports = class{
236
249
  }
237
250
 
238
251
  if(call && call.hasOwnProperty(ws.path)){
239
- call = call[ws.path].callback;
240
- this.traffic(ws);
241
-
242
- const run = this.getCall(call);
243
-
244
- ws.update(run);
245
- run(ws, 'start');
252
+ call = call[ws.path];
253
+
254
+ if(call.next !== false){
255
+ this.getCall(call.next)(ws, (data)=>{
256
+ ws.nextData = data;
257
+ const run = this.getCall(call.callback);
258
+ ws.update(run);
259
+ run(ws, 'start');
260
+ });
261
+ }else{
262
+ const run = this.getCall(call.callback);
263
+ ws.update(run);
264
+ run(ws, 'start');
265
+ }
246
266
  }else{
247
267
  ws.error(404);
248
268
  }
@@ -267,7 +287,7 @@ module.exports = class{
267
287
  this.logServer('server', `The server is collecting logs via (${this.opt.log})`);
268
288
  port = port || this.opt.port || process.env.PORT || 3000;
269
289
  this.server.listen(port, host, ()=>{
270
- this.logServer('server', `Started on server(${this.opt.use}) port(${port})`);
290
+ this.logServer('server', `Started on server(${this.opt.use}) host(${host || '0.0.0.0'}) port(${port})`);
271
291
  });
272
292
  }
273
293
  }
@@ -307,24 +327,9 @@ module.exports = class{
307
327
  }
308
328
 
309
329
  domain(domain){
310
- if(domain == undefined){
311
- domain = '*';
312
- }
330
+ let config = {};
313
331
 
314
- let config = {
315
- next:(n)=>{
316
- let next = {};
317
- methodPlugin.basic(this, next, {domain, next:this.setCall(n)});
318
- next.routes = methodPlugin.routes(next);
319
- next.directory = methodPlugin.directory(next);
320
-
321
- return next;
322
- }
323
- }
324
- methodPlugin.basic(this, config, {domain});
325
- config.routes = methodPlugin.routes(config);
326
- config.directory = methodPlugin.directory(config);
327
- config.websocket = methodPlugin.websocket(this, config, domain);
332
+ methodPlugin.maps(this, config, domain);
328
333
 
329
334
  return config;
330
335
  }
@@ -342,11 +347,11 @@ module.exports = class{
342
347
  return false;
343
348
  }
344
349
 
345
- if(typeof next != 'number'){
350
+ if(typeof next != 'string'){
346
351
  next = false;
347
352
  }
348
353
 
349
- if(typeof callback != 'number'){
354
+ if(typeof callback != 'string'){
350
355
  return false;
351
356
  }
352
357
 
@@ -377,7 +382,7 @@ module.exports = class{
377
382
  this.maps[domain][method].params[mode == 'params' ? 0 : 1].push(route);
378
383
  }
379
384
  }
380
- setUpgrade({domain, url, callback}){
385
+ setUpgrade({domain, next, url, callback}){
381
386
  if(typeof domain != 'string'){
382
387
  domain = '*';
383
388
  }
@@ -386,21 +391,36 @@ module.exports = class{
386
391
  return false;
387
392
  }
388
393
 
389
- if(typeof callback != 'number'){
394
+ if(typeof callback != 'string'){
390
395
  return false;
391
396
  }
392
397
 
398
+ if(typeof next != 'string'){
399
+ next = false;
400
+ }
401
+
393
402
  if(!this.mapws.hasOwnProperty(domain)){
394
403
  this.mapws[domain] = {};
395
404
  }
396
405
 
397
406
  this.mapws[domain][url] = {
398
- callback:callback
407
+ callback,
408
+ next,
399
409
  };
400
410
  }
401
- setCall(callback){
402
- this.calls.push(callback);
403
- return this.calls.length - 1;
411
+ setCall(a){
412
+ if(typeof a == 'function'){
413
+ let id = `r938_root_${this.idcalls}`;
414
+ this.calls[id] = a;
415
+
416
+ this.idcalls++;
417
+ return id;
418
+ }else if(typeof a == 'object' && typeof a.id == 'string' && typeof a.callback == 'function'){
419
+ if(!this.calls.hasOwnProperty(a.id)){
420
+ this.calls[a.id] = a.callback;
421
+ }
422
+ return a.id;
423
+ }
404
424
  }
405
425
  getCall(id){
406
426
  if(this.calls[id]){
@@ -410,3 +430,6 @@ module.exports = class{
410
430
  }
411
431
  }
412
432
 
433
+ module.exports.pack = ()=>{
434
+ return packPlugin();
435
+ }
package/lib/tools.js CHANGED
@@ -9,18 +9,18 @@ const tools = {
9
9
  let output = {};
10
10
  if(typeof byteData == 'string'){
11
11
  for(let x of byteData.split('&')){
12
- var data = x.search('=');
12
+ const data = x.search('=');
13
13
  let name = '';
14
14
  let value = '';
15
15
  if(data == -1){
16
- name = decodeURIComponent(x.trim());
16
+ name = decodeURIComponent(x);
17
17
  value = '';
18
18
  }else{
19
19
  try{
20
- name = decodeURIComponent(x.substring(0, data).trim());
21
- value = decodeURIComponent(x.substr(data + 1));
20
+ name = decodeURIComponent(x.slice(0, data));
21
+ value = decodeURIComponent(x.slice(data + 1));
22
22
  }catch(e){
23
- this.server.log('error',`${util.format(e)}`);
23
+ return {};
24
24
  }
25
25
  }
26
26
  if(typeof output[name] == 'undefined'){
@@ -67,7 +67,7 @@ const tools = {
67
67
  data = new TextDecoder('utf8').decode(rawData);
68
68
  }
69
69
 
70
- let name=content.indexOf(' name="') + 7;
70
+ let name = content.indexOf(' name="') + 7;
71
71
  name = content.slice(name,content.indexOf('"',name)).toString();
72
72
 
73
73
  if(typeof output[name] == 'undefined'){
package/lib/websocket.js CHANGED
@@ -11,6 +11,7 @@ module.exports = class{
11
11
  query = {}
12
12
  method = ''
13
13
  ip = ''
14
+ isUID = false
14
15
  callback = ()=>{}
15
16
  log = ()=>{}
16
17
  constructor({req, socket, head}){
@@ -170,6 +171,7 @@ module.exports = class{
170
171
  const head = tools.head2line(this.header);
171
172
  const statusLine = `HTTP/1.1 ${this.status} ${tools.statusText(this.status)}`;
172
173
  const headLine = `${statusLine}\r\n${head}\r\n\r\n`;
174
+ this.isUID = true;
173
175
  this.write(Buffer.from(headLine));
174
176
 
175
177
  this.log();
@@ -177,7 +179,9 @@ module.exports = class{
177
179
  return this;
178
180
  }
179
181
  write(data, callback){
180
- if(this.socket.writable){
182
+ if(!this.isUID){
183
+ this.error(400);
184
+ }else if(this.socket.writable){
181
185
  this.socket.write(data, callback);
182
186
  }else if(this.req.destroyed){
183
187
  this.req.destroy();
package/package.json CHANGED
@@ -3,6 +3,6 @@
3
3
  "description": "BirdPack web framework is a tool for web server via TCP HTTP supporting websocket focusing on speed.",
4
4
  "author":"R938",
5
5
  "license":"R938",
6
- "version":"1.0.10",
6
+ "version":"1.0.12",
7
7
  "main": "index.js"
8
8
  }