upcore-tcp 0.0.1

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.
@@ -0,0 +1,191 @@
1
+ const fs = require("fs");
2
+ const checkDirectory = require('./checkDirectory');
3
+ const extension = require('./extension');
4
+
5
+ module.exports = routeContext = class{
6
+ setup = ['next', 'all', 'websocket', 'directory', 'routes', 'get','post','put','patch','delete','options', 'head']
7
+ constructor(app, domain, core = []){
8
+ this.app = app;
9
+ this.domain = domain;
10
+ this.core = core;
11
+ }
12
+
13
+ next(...cb){
14
+ let core = [];
15
+
16
+ for(let x of cb){
17
+ if(typeof x === 'function'){
18
+ let id = `__cb_${++this.app.CALLBACK_ID}`;
19
+
20
+ this.app.CALLBACK_TREE[id] = x;
21
+ core.push(id);
22
+
23
+ }else if(typeof x === 'string'){
24
+ core.push(x);
25
+ }
26
+ }
27
+
28
+ return new routeContext(
29
+ this.app,
30
+ this.domain,
31
+ [...this.core, ...core]
32
+ );
33
+ }
34
+
35
+ all(path, ...cb){
36
+ this.app.set(this.domain, '*', path, ...[...this.core, ...cb]);
37
+ return this;
38
+ }
39
+ websocket(path, ...cb){
40
+ let mode = {
41
+ start:cb[0] || (()=>{}),
42
+ data:cb[1] || (()=>{}),
43
+ close:cb[2] || (()=>{}),
44
+ ping:cb[3] || (()=>{}),
45
+ pong:cb[4] || (()=>{}),
46
+ };
47
+
48
+ ['start', 'data', 'close', 'ping', 'pong'];
49
+ this.app.set(this.domain, 'websocket', path, ...[...this.core, cb.length == 1 ? cb : (req, m)=>{
50
+ if(mode[m]){
51
+ mode[m](req);
52
+ }
53
+ }]);
54
+ return this;
55
+ }
56
+ directory(dir, options){
57
+ let {pathname, type, callback, error} = options || {};
58
+
59
+ if(typeof pathname !== 'string'){
60
+ pathname = '/';
61
+ }else if(pathname[pathname.length-1] != '/'){
62
+ pathname += '/';
63
+ }
64
+
65
+ if(dir != '' && dir[dir.length-1] != '/'){
66
+ dir += '/';
67
+ }
68
+
69
+ if(typeof callback !== 'function'){
70
+ callback = (req, {load})=>{
71
+ req.file({
72
+ file:load,
73
+ error:(code)=>{
74
+ error(req, code, '');
75
+ }
76
+ });
77
+ }
78
+ }
79
+
80
+ if(typeof error !== 'function'){
81
+ error = (req, code, text)=>{
82
+ req.code(code).send(text);
83
+ }
84
+ }
85
+
86
+ let checkType = false;
87
+ if(Array.isArray(type)){
88
+ checkType = true;
89
+ }
90
+
91
+ this.app.set(this.domain, 'get', `${pathname}*`, ...[...this.core, (req)=>{
92
+ let file = checkDirectory(req.wildcard);
93
+ let load = `${dir}${file}`;
94
+ let ext = extension(file);
95
+
96
+ if(ext === false || (checkType && !type.includes(ext))){
97
+ error(req, 400, 'The file extension is incorrect.');
98
+ return false;
99
+ }
100
+ if(file !== false && fs.existsSync(load)){
101
+ callback(req, {load, file});
102
+ }else{
103
+ error(req, 404, 'File not found.');
104
+ }
105
+ }]);
106
+
107
+ return this;
108
+ }
109
+ routes(dir){
110
+ if(fs.lstatSync(dir).isDirectory()){
111
+ let path = `${dir}/`;
112
+ let list = {};
113
+ fs.readdirSync(path).forEach((file)=>{
114
+ if(fs.existsSync(path + file)){
115
+ let ext = file.split('.').pop();
116
+ if(ext == 'js'){
117
+ let pathFile = path + file;
118
+ list[file.slice(0,-3)] = require(pathFile);
119
+ }
120
+ }
121
+ });
122
+
123
+ for(let x in list){
124
+ let db = x.split('@');
125
+ let method = "get";
126
+ let path = '';
127
+
128
+ if(db.length == 1){
129
+ path = db[0];
130
+ }else if(db.length == 2){
131
+ method = db[0];
132
+ path = db[1];
133
+ }else{
134
+ break;
135
+ }
136
+
137
+ path = path.replace(/[\-\$\"]/g,(a)=>{
138
+ let list={
139
+ '-':'/',
140
+ '$':':'
141
+ };
142
+ if(list.hasOwnProperty(a)){
143
+ return list[a];
144
+ }else{
145
+ return '';
146
+ }
147
+ });
148
+
149
+ if(this[method]){
150
+ let r = [];
151
+ if(Array.isArray(list[x].next)){
152
+ r.push(...list[x].next);
153
+ }else if(typeof list[x].next == 'string'){
154
+ r.push(list[x].next);
155
+ }
156
+ r.push(list[x]);
157
+ this[method](`/${path}`, ...r);
158
+ }
159
+ }
160
+ }
161
+ return this;
162
+ }
163
+ get(path, ...cb){
164
+ this.app.set(this.domain, 'get', path, ...[...this.core, ...cb]);
165
+ return this;
166
+ }
167
+ post(path, ...cb){
168
+ this.app.set(this.domain, 'post', path, ...[...this.core, ...cb]);
169
+ return this;
170
+ }
171
+ put(path, ...cb){
172
+ this.app.set(this.domain, 'put', path, ...[...this.core, ...cb]);
173
+ return this;
174
+ }
175
+ patch(path, ...cb){
176
+ this.app.set(this.domain, 'patch', path, ...[...this.core, ...cb]);
177
+ return this;
178
+ }
179
+ delete(path, ...cb){
180
+ this.app.set(this.domain, 'delete', path, ...[...this.core, ...cb]);
181
+ return this;
182
+ }
183
+ options(path, ...cb){
184
+ this.app.set(this.domain, 'options', path, ...[...this.core, ...cb]);
185
+ return this;
186
+ }
187
+ head(path, ...cb){
188
+ this.app.set(this.domain, 'head', path, ...[...this.core, ...cb]);
189
+ return this;
190
+ }
191
+ }