Created
June 28, 2020 12:57
-
-
Save d4n5h/dbd7f64c7fc09abf9b4eec99401833d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let app = require('./uApp.js'); | |
const route = require('./route.js'); | |
const port = 9001; | |
app = new app(); | |
app.use((res, req) => { | |
app.req.test = 'test' | |
}) | |
app.get('/api', (res, req) => { | |
console.log(req.test) | |
res.end('asd') | |
}) | |
// Load another route | |
app.use('/routePath', route) | |
app.listen(port, (token) => { | |
if (token) { | |
console.log('Listening to port ' + port); | |
} else { | |
console.log('Failed to listen to port ' + port); | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let app = require('./uApp.js'); | |
const port = 9001; | |
app = new app(); | |
app.get('/api', (res, req) => { | |
console.log(req.test) | |
res.end('Hello from route') | |
}) | |
module.exports = app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { flatten } = require('array-flatten'), | |
uWS = require('uWebSockets.js'), | |
fs = require('fs'), | |
p = require('path'), | |
slice = Array.prototype.slice; | |
function parseQuery (query) { | |
query = query.split('&'); | |
let results = {}; | |
for (let i = 0; i < query.length; i++) { | |
const kv = query[i].split('='); | |
if (kv.length > 1) { | |
results[kv[0]] = kv[1]; | |
} | |
} | |
return results; | |
} | |
const methods = ['get', 'post', 'put', 'del', 'any', 'ws', 'patch', 'listen', 'connect', 'options', 'trace', 'publish', 'head'] | |
const exclude = ['ws', 'listen', 'connect', 'options', 'trace'] | |
const uApp = function (options = {}) { | |
this.uWS = uWS; | |
if (options.ssl) { | |
this.app = this.uWS.SSLApp(options.ssl); | |
} else { | |
this.app = this.uWS.App(); | |
} | |
this.stack = []; | |
this.req = {}; | |
this.set = function (key, val) { | |
this.req[key] = val; | |
return this; | |
} | |
this.use = function use(fn) { | |
let offset = 0; | |
let path = '/'; | |
if (typeof fn !== 'function') { | |
let arg = fn; | |
while (Array.isArray(arg) && arg.length !== 0) { | |
arg = arg[0]; | |
} | |
if (typeof arg !== 'function') { | |
offset = 1; | |
path = fn; | |
} | |
} | |
const callbacks = flatten(slice.call(arguments, offset)); | |
callbacks.forEach((callback) => { | |
if (callback.stack) { | |
req = Object.assign(callback.req, this.req) | |
callback.stack.forEach((cb) => { | |
this.stack.push({ | |
path: path + cb.path, isMw: cb.isMw, method: cb.method, callback: function (res, req) { | |
req = Object.assign(req, this.req) | |
req.query = parseQuery(req.getQuery()); | |
cb.callback(res, req) | |
} | |
}) | |
}) | |
const newReqs = Object.keys(callback.req); | |
for (let i = 0; i < newReqs.length; i++) { | |
const key = newReqs[i]; | |
const val = callback.req[key]; | |
this.req[key] = val; | |
} | |
} else { | |
if (path == '/') { | |
path = '/*' | |
} | |
this.stack.push({ | |
path: path, isMw: true, method: 'any', callback: (res, req) => { | |
req = Object.assign(req, this.req) | |
req.query = parseQuery(req.getQuery()); | |
req.setYield(true); | |
res.onAborted(() => { | |
res.aborted = true; | |
}); | |
callback(res, req); | |
} | |
}) | |
} | |
}) | |
return this; | |
}; | |
const that = this; | |
methods.forEach(method => { | |
uApp.prototype[method] = function (path, callback) { | |
this.stack.push({ | |
path: path, method: method, callback: function (res, req) { | |
req = Object.assign(req, that.req) | |
req.query = parseQuery(req.getQuery()); | |
callback(res, req) | |
} | |
}) | |
return this | |
} | |
}); | |
this.listen = function (port, cb) { | |
for (let i = 0; i < this.stack.length; i++) { | |
const route = this.stack[i]; | |
if (route.path.includes('*') && route.isMw) { | |
const base = route.path.split('*')[0]; | |
for (let x = 0; x < this.stack.length; x++) { | |
if (this.stack[x].path.includes(base) && !this.stack[x].path.includes('*') && !this.stack[x].isMw && !exclude.includes(this.stack[x].method)) { | |
this.app[this.stack[x].method](this.stack[x].path, route.callback) | |
} | |
} | |
} else { | |
this.app[route.method](route.path, route.callback) | |
} | |
} | |
this.app.listen(port, cb); | |
} | |
return this; | |
} | |
module.exports = uApp; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment