Skip to content

Commit

Permalink
Merge pull request #10 from fyrejet/experimental
Browse files Browse the repository at this point in the history
3.0
  • Loading branch information
schamberg97 committed Mar 1, 2021
2 parents 1ac5609 + cead96c commit c97ffdf
Show file tree
Hide file tree
Showing 43 changed files with 753 additions and 1,390 deletions.
209 changes: 41 additions & 168 deletions README.md

Large diffs are not rendered by default.

Binary file removed benchmark.png
Binary file not shown.
45 changes: 12 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ var defaultErrorHandler = (err, req, res) => {

function createApplication (options = {}) {
options.errorHandler = options.errorHandler || defaultErrorHandler
if (options.serverType === 'uWebSocket' || process.env.UWS_SERVER_ENABLED_FOR_TEST === 'TRUE') { // legacy or tests
if (options.serverType === 'uWebSocket' || options.serverType === 'uWebSockets' || process.env.UWS_SERVER_ENABLED_FOR_TEST === 'TRUE' || process.env.UWS_SERVER === "TRUE") { // legacy or tests
options.serverType = 'uWebSockets'
options.prioRequestsProcessing = false
options.server = require('low-http-server')()
options.server = require('./lib/uwsCompat').uwsCompat(options)
process.env.UWS_SERVER = "true"
}

var server = options.server || require('http').createServer()
Expand Down Expand Up @@ -151,34 +152,14 @@ function createApplication (options = {}) {
Object.assign(app, appCore(options, server, app))
Object.assign(app, EventEmitter.prototype)

app.request = Object.assign({}, req)

const reqProperties = {}
app.reqPropertiesEssential = reqProperties
const reqPropertiesEssential = {}
app.reqPropertiesEssential = reqPropertiesEssential
Object.keys(app.request.propFn).forEach(name => {
if (name.includes('Setter')) return
let set
if (app.request.propFn[name + 'Setter']) set = app.request.propFn[name + 'Setter']
reqProperties[name] = {
configurable: true,
enumerable: true,
get: app.request.propFn[name],
set: set
}
if (app.request.propFn[name].essential) {
reqPropertiesEssential[name] = {
configurable: true,
enumerable: true,
get: app.request.propFn[name],
set: set
}
}
app.request = Object.create(req, {
app: { configurable: true, enumerable: true, writable: true, value: app }
})

// expose the prototype that will get set on responses
app.response = Object.assign({}, res)
app.response = Object.create(res, {
app: { configurable: true, enumerable: true, writable: true, value: app }
})

app.handler = app.handle
app.callback = () => app.handle
Expand All @@ -189,18 +170,16 @@ function createApplication (options = {}) {
// Init the express-like app abilities
app.init(options)

app.use(initMiddleware(options, reqProperties, reqPropertiesEssential, app))
//app.use(initMiddleware(options, reqProperties, reqPropertiesEssential, app))

app.use(initMiddleware(options, app))

return app
}

exports.initMiddleware = initMiddleware
exports.defaultErrorHandler = defaultErrorHandler // expose defaultErrorHandler for router

exports.application = proto
exports.request = req
exports.response = res

/**
* Expose middleware
*/
Expand All @@ -227,7 +206,7 @@ exports.response = res
// exports.Route = Route;
exports.Router = require('./lib/routing/request-router-constructor')

exports.uwsCompat = require('./lib/uwsCompat')
exports.uwsCompat = require('./lib/uwsCompat').uwsCompat

/**
* Replace Express removed middleware with an appropriate error message. We are not express, but we will imitate it precisely
Expand Down
2 changes: 1 addition & 1 deletion lib/additions/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (root, options) {
var middleware = (req, res, next) => {
const oldNext = next
const originalUrl = req.rData_internal.url
req.rData_internal.url = req.currentUrl
req.rData_internal.url = req.currentUrl()
next = function (err) {
req.rData_internal.url = originalUrl
oldNext(err)
Expand Down
7 changes: 0 additions & 7 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ app.defaultConfiguration = function defaultConfiguration (options) {
}
this.set('env', env)

if (!this.settings['fyrejet mode']) this.set('fyrejet mode', false)

if (!this.settings['query parser']) this.set('query parser', 'extended')
else this.set('query parser', this.settings['query parser'])

Expand Down Expand Up @@ -209,11 +207,6 @@ app.set = function set (setting, val) {

// trigger matched settings
switch (setting) {
case 'fyrejet mode':
this.settings['fyrejet mode'] = val
if (val !== 'api' && val !== 'properties as functions') break
this.getRouter().changeSequentialConfig('specialMode', val)
break
case 'strict routing':
this.settings.strict = val
this.getRouter().modifySetting('strict', val)
Expand Down
94 changes: 45 additions & 49 deletions lib/middleware/init.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
'use strict'

const req = require('../request')
const { route } = require('../application')

const exts = {
request: {},
response: require('./restana-response-extensions')
}

var init = function (options, reqProperties, reqPropertiesEssential, router, slave) {
const reqAdditionsPropsAsFns = Object.assign({}, req)
delete reqAdditionsPropsAsFns.propFn

Object.keys(reqProperties).forEach(item => {
if (!reqPropertiesEssential[item.replace('Setter', '')]) {
reqAdditionsPropsAsFns[item] = req.propFn[item]
}
})
var init = function (options, router, slave) {

function defErrHandlerBuilder (defaultErrHandler, res, req) {
return function (err) {
Expand All @@ -39,56 +25,67 @@ var init = function (options, reqProperties, reqPropertiesEssential, router, sla
const deh = res.defaultErrHandler // backup of defaultErrHandler
res.defaultErrHandler = defErrHandlerBuilder(deh, res, req)
req.activateExpress = function () {
const req = this
const res = this.res

Object.assign(req, req.app.request)
Object.assign(res, req.app.response)

/*
this previously had a ' Object.keys(req.app.response).forEach(key => { ' implementation that sucked and an erroneous claim that it was faster
due to erroneous benchmarks. I am sorry, my dudes
*/

Object.defineProperties(req, reqProperties)
return req
// stub function to break as few apps as possible
return this
}

res.sendLite = exts.response.send(options, req, res) // we still leave the user with restana's res.send implementation, if API mode is on for the route

req.rData_internal.appPrev.push(req.app)
req.app = res.app = router

req.rData_internal.tree.push(router.mountpath)

if (req.rData_internal.initDone) {
req.next = next
return next()

}

const specialMode = req.rData_internal.specialMode

req.next = next
req.rData_internal.initDone = true

if (!specialMode) { // check that the route is not marked as API-only, with disabled Express req & res additions.
req.activateExpress()
return next()
Object.assign(req, req.app.request.__proto__)
Object.assign(req, req.app.request)

Object.assign(res, req.app.response.__proto__)
Object.assign(res, req.app.response)

function methodGetter () {
return this.rData_internal.method
}
if (specialMode === 'properties as functions') {
Object.keys(reqAdditionsPropsAsFns).forEach(key => {
req[key] = reqAdditionsPropsAsFns[key]
})
req.propFn = req.app.request.propFn
Object.keys(req.app.response).forEach(key => {
res[key] = req.app.response[key]
}) // no change here, express response additions are non-problematic

function methodSetter (inp) {
if (this.rData_internal.method !== inp) {
this.rData_internal.reroute = true
this.rData_internal.method = inp
}
return this.rData_internal.method
}

function urlGetter () {
return this.rData_internal.url
}
// if we are here, it means we got into special route mode ;)

if (!res.send) res.send = res.sendLite
function urlSetter (inp) {
if (this.rData_internal.url !== inp) {
this.rData_internal.reroute = true
this.rData_internal.url = inp
}
return this.rData_internal.url
}

Object.defineProperties(req, {
method: {
get: methodGetter,
set: methodSetter,
configurable: true,
enumerable: true
},
url: {
get: urlGetter,
set: urlSetter,
configurable: true,
enumerable: true
}
});

Object.defineProperties(req, reqPropertiesEssential) // enabling bare essentials ;)
return next()
}
normInit.init = true
Expand All @@ -97,7 +94,6 @@ var init = function (options, reqProperties, reqPropertiesEssential, router, sla
if (route.mountpath) req.rData_internal.tree.push(router.mountpath)
const deh = res.defaultErrHandler
res.defaultErrHandler = defErrHandlerBuilder(deh)
req.next = next
return next()
}
slaveInit.init = true
Expand Down
98 changes: 0 additions & 98 deletions lib/middleware/restana-response-extensions.js

This file was deleted.

Loading

0 comments on commit c97ffdf

Please sign in to comment.