Skip to content

Commit b96f140

Browse files
committed
Improve performance + add request behaviour
1 parent 47ed5a8 commit b96f140

5 files changed

Lines changed: 211 additions & 119 deletions

File tree

changes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ BETA ======= 1.9.0
5757
- added: F.snapshop(url, filename, [callback]);
5858
- added: CLEANUP(stream, [callback]) - clean up readable streams
5959
- added: configs directory
60+
- added: F.behaviour(url, flags);
61+
- added: behaviour: disable-upload-dimension (default: false)
6062

6163
- updated: (IMPORTANT) routing: `json` flag is not required for receiving incomming data as JSON
6264
- updated: `F.mail(address, subject, view, [model], [callback], [language])` added language
@@ -95,6 +97,7 @@ BETA ======= 1.9.0
9597
- fixed: F.log(), F.logger() –> problem with objects, reported by Nikita Shmidt
9698
- fixed: Number formatting (problem with negative numbers)
9799

100+
- improved: performance +15%
98101
- improved: code optimalization
99102
- improved: Date.prototype.format()
100103
- improved: String.prototype.format()

index.js

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var REQUEST_COMPRESS_CONTENTTYPE = { 'text/plain': true, 'text/javascript': true
5656
var TEMPORARY_KEY_REGEX = /\//g;
5757
var REG_MOBILE = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|Tablet/i;
5858
var REG_VERSIONS = /(href|src)="[a-zA-Z0-9\/\:\-\.]+\.(jpg|js|css|png|gif|svg|html|ico|json|less|sass|scss|swf|txt|webp|woff|woff2|xls|xlsx|xml|xsl|xslt|zip|rar|csv|doc|docx|eps|gzip|jpe|jpeg|manifest|mov|mp3|mp4|ogg|package|pdf)"/gi;
59+
var REQUEST_PROXY_FLAGS = ['post', 'json'];
5960

6061
var _controller = '';
6162
var _test;
@@ -227,7 +228,7 @@ function Framework() {
227228

228229
this.id = null;
229230
this.version = 1900;
230-
this.version_header = '1.9.0-16';
231+
this.version_header = '1.9.0-17';
231232

232233
var version = process.version.toString().replace('v', '').replace(/\./g, '');
233234
if (version[1] === '0')
@@ -344,6 +345,7 @@ function Framework() {
344345
packages: {},
345346
};
346347

348+
this.behaviours = null;
347349
this.modificators = null;
348350
this.helpers = {};
349351
this.modules = {};
@@ -462,6 +464,30 @@ Framework.prototype = {
462464

463465
Framework.prototype.__proto__ = new events.EventEmitter();
464466

467+
/**
468+
* Adds a new behaviour
469+
* @param {String} url A relative URL address.
470+
* @param {String Array} flags
471+
* @return {Framework}
472+
*/
473+
Framework.prototype.behaviour = function(url, flags) {
474+
var self = this;
475+
476+
if (!self.behaviours)
477+
self.behaviours = {};
478+
479+
if (typeof(flags) === STRING)
480+
flags = [flags];
481+
482+
if (!self.behaviours[url])
483+
self.behaviours[url] = {};
484+
485+
for (var i = 0; i < flags.length; i++)
486+
self.behaviours[url][flags[i]] = true;
487+
488+
return self;
489+
};
490+
465491
/**
466492
* Refersh framework internal informations
467493
* @param {Boolean} clear Clear temporary files, optional
@@ -978,6 +1004,11 @@ Framework.prototype.route = function(url, funcExecute, flags, length, middleware
9781004

9791005
var first = flags[i][0];
9801006

1007+
if (first === '%') {
1008+
self.behaviour(url, flags[i].substring(1));
1009+
continue;
1010+
}
1011+
9811012
if (first === '#') {
9821013
if ((middleware || null) === null)
9831014
middleware = [];
@@ -8245,7 +8276,7 @@ Subscribe.prototype.multipart = function(header) {
82458276
}
82468277

82478278
framework.path.verify('temp');
8248-
framework_internal.parseMULTIPART(req, header, self.route.length, framework.config['directory-temp'], self);
8279+
framework_internal.parseMULTIPART(req, header, self.route, framework.config['directory-temp'], self);
82498280
return self;
82508281
};
82518282

@@ -8330,13 +8361,15 @@ Subscribe.prototype.execute = function(status) {
83308361
}
83318362

83328363
if (route === null) {
8364+
if (!status)
8365+
status = 404;
83338366
if (status === 400 && self.exception instanceof Builders.ErrorBuilder) {
83348367
if (req.$language)
83358368
self.exception.resource(req.$language, framework.config['default-errorbuilder-resource-prefix']);
83368369
framework.responseContent(req, res, 200, self.exception.json(), 'application/json', framework.config['allow-gzip']);
83378370
}
83388371
else
8339-
framework.responseContent(req, res, status || 404, utils.httpStatus(status || 404), CONTENTTYPE_TEXTPLAIN, framework.config['allow-gzip']);
8372+
framework.responseContent(req, res, status, utils.httpStatus(status), CONTENTTYPE_TEXTPLAIN, framework.config['allow-gzip']);
83408373
return self;
83418374
}
83428375

@@ -8447,6 +8480,12 @@ Subscribe.prototype.doExecute = function() {
84478480
if (controller.isCanceled)
84488481
return self;
84498482

8483+
if (!self.route.isASTERIX && !self.route.param.length) {
8484+
// cache internal.routeSplit()
8485+
if (!framework.temporary.other[req.uri.pathname])
8486+
framework.temporary.other[req.uri.pathname] = req.path;
8487+
}
8488+
84508489
if (self.route.isGENERATOR)
84518490
async.call(controller, self.route.execute, true)(controller, framework_internal.routeParam(self.route.param.length ? framework_internal.routeSplit(req.uri.pathname, true) : req.path, self.route));
84528491
else
@@ -8744,8 +8783,6 @@ function Controller(name, req, res, subscribe, currentView) {
87448783
this.req = req;
87458784
this.res = res;
87468785
this.exception = null;
8747-
this.boundary = null;
8748-
87498786

87508787
// Sets the default language
87518788
if (req)
@@ -11585,9 +11622,6 @@ Controller.prototype.close = function(end) {
1158511622
return self;
1158611623
}
1158711624

11588-
if (self.type === 2)
11589-
self.res.write('\r\n\r\n--' + self.boundary + '--');
11590-
1159111625
self.isConnected = false;
1159211626
self.res.success = true;
1159311627

@@ -11628,7 +11662,7 @@ Controller.prototype.proxy = function(url, obj, fnCallback, timeout) {
1162811662
obj = tmp;
1162911663
}
1163011664

11631-
return utils.request(url, ['post', 'json'], obj, function(error, data, code, headers) {
11665+
return utils.request(url, REQUEST_PROXY_FLAGS, obj, function(error, data, code, headers) {
1163211666

1163311667
if (!fnCallback)
1163411668
return;
@@ -12271,7 +12305,7 @@ WebSocket.prototype.proxy = function(url, obj, fnCallback) {
1227112305
obj = tmp;
1227212306
}
1227312307

12274-
return utils.request(url, ['post', 'json'], obj, function(error, data, code, headers) {
12308+
return utils.request(url, REQUEST_PROXY_FLAGS, obj, function(error, data, code, headers) {
1227512309

1227612310
if (!fnCallback)
1227712311
return;
@@ -13481,6 +13515,38 @@ http.IncomingMessage.prototype.noCache = function() {
1348113515
return self;
1348213516
};
1348313517

13518+
http.IncomingMessage.prototype.can = function(type, reverse) {
13519+
13520+
if (!framework.behaviours)
13521+
return reverse ? false : true;
13522+
13523+
var url = this.url;
13524+
13525+
if (!this.isStaticFile && url[url.length - 1] !== '/')
13526+
url += '/';
13527+
13528+
var current = framework.behaviours['*'];
13529+
var value;
13530+
13531+
// global
13532+
if (current !== undefined) {
13533+
current = current[type];
13534+
if (current !== undefined)
13535+
value = reverse ? !current : current;
13536+
}
13537+
13538+
// by specific
13539+
current = framework.behaviours[url];
13540+
if (current === undefined)
13541+
return value; // responds with global
13542+
13543+
current = current[type];
13544+
if (current === undefined)
13545+
return value; // responds with global
13546+
13547+
return reverse ? !current : current;
13548+
};
13549+
1348413550
/**
1348513551
* Read a cookie from current request
1348613552
* @param {String} name Cookie name.
@@ -13667,7 +13733,7 @@ function fsStreamRead(filename, options, callback, next) {
1366713733

1366813734
/**
1366913735
* Prepare URL address to temporary key (for caching)
13670-
* @param {ServerRequest or Strign} req
13736+
* @param {ServerRequest or String} req
1367113737
* @return {String}
1367213738
*/
1367313739
function createTemporaryKey(req) {

0 commit comments

Comments
 (0)