birdpack 1.0.11 → 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
  }
@@ -152,4 +161,97 @@ module.exports = {
152
161
  return use;
153
162
  };
154
163
  },
155
- };
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, {domain:'*'});
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{
@@ -239,7 +252,8 @@ module.exports = class{
239
252
  call = call[ws.path];
240
253
 
241
254
  if(call.next !== false){
242
- this.getCall(call.next)(ws, ()=>{
255
+ this.getCall(call.next)(ws, (data)=>{
256
+ ws.nextData = data;
243
257
  const run = this.getCall(call.callback);
244
258
  ws.update(run);
245
259
  run(ws, 'start');
@@ -273,7 +287,7 @@ module.exports = class{
273
287
  this.logServer('server', `The server is collecting logs via (${this.opt.log})`);
274
288
  port = port || this.opt.port || process.env.PORT || 3000;
275
289
  this.server.listen(port, host, ()=>{
276
- 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})`);
277
291
  });
278
292
  }
279
293
  }
@@ -313,25 +327,9 @@ module.exports = class{
313
327
  }
314
328
 
315
329
  domain(domain){
316
- if(domain == undefined){
317
- domain = '*';
318
- }
330
+ let config = {};
319
331
 
320
- let config = {
321
- next:(n)=>{
322
- let next = {};
323
- methodPlugin.basic(this, next, {domain, next:this.setCall(n)});
324
- next.routes = methodPlugin.routes(next);
325
- next.directory = methodPlugin.directory(next);
326
- next.websocket = methodPlugin.websocket(this, next, {domain, next:this.setCall(n)});
327
-
328
- return next;
329
- }
330
- }
331
- methodPlugin.basic(this, config, {domain});
332
- config.routes = methodPlugin.routes(config);
333
- config.directory = methodPlugin.directory(config);
334
- config.websocket = methodPlugin.websocket(this, config, {domain});
332
+ methodPlugin.maps(this, config, domain);
335
333
 
336
334
  return config;
337
335
  }
@@ -349,11 +347,11 @@ module.exports = class{
349
347
  return false;
350
348
  }
351
349
 
352
- if(typeof next != 'number'){
350
+ if(typeof next != 'string'){
353
351
  next = false;
354
352
  }
355
353
 
356
- if(typeof callback != 'number'){
354
+ if(typeof callback != 'string'){
357
355
  return false;
358
356
  }
359
357
 
@@ -393,11 +391,11 @@ module.exports = class{
393
391
  return false;
394
392
  }
395
393
 
396
- if(typeof callback != 'number'){
394
+ if(typeof callback != 'string'){
397
395
  return false;
398
396
  }
399
397
 
400
- if(typeof next != 'number'){
398
+ if(typeof next != 'string'){
401
399
  next = false;
402
400
  }
403
401
 
@@ -410,9 +408,19 @@ module.exports = class{
410
408
  next,
411
409
  };
412
410
  }
413
- setCall(callback){
414
- this.calls.push(callback);
415
- 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
+ }
416
424
  }
417
425
  getCall(id){
418
426
  if(this.calls[id]){
@@ -420,14 +428,8 @@ module.exports = class{
420
428
  }
421
429
  return ()=>{};
422
430
  }
423
- next(n){
424
- let next = {};
425
- methodPlugin.basic(this, next, {domain:'*', next:this.setCall(n)});
426
- next.routes = methodPlugin.routes(next);
427
- next.directory = methodPlugin.directory(next);
428
- next.websocket = methodPlugin.websocket(this, next, {domain:'*', next:this.setCall(n)});
429
-
430
- return next;
431
- }
432
431
  }
433
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/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.11",
6
+ "version":"1.0.12",
7
7
  "main": "index.js"
8
8
  }