Skip to content

Commit c8f3f43

Browse files
authored
Merge pull request totaljs#519 from totaljs/v2.7.0
v2.7.0
2 parents 1e2f4ab + e5d956d commit c8f3f43

16 files changed

Lines changed: 590 additions & 579 deletions

File tree

builders.js

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* @module FrameworkBuilders
24-
* @version 2.6.0
24+
* @version 2.7.0
2525
*/
2626

2727
'use strict';
@@ -31,7 +31,7 @@ const DEFAULT_SCHEMA = 'default';
3131
const SKIP = { $$schema: true, $$result: true, $$callback: true, $$async: true, $$index: true, $$repository: true, $$can: true, $$controller: true };
3232
const REGEXP_CLEAN_EMAIL = /\s/g;
3333
const REGEXP_CLEAN_PHONE = /\s|\.|\-|\(|\)/g;
34-
const REGEXP_NEWOPERATION = /^function(\s)?\([a-zA-Z0-9\$]+\)/g;
34+
const REGEXP_NEWOPERATION = /^function(\s)?\([a-zA-Z0-9\$]+\)/;
3535
const hasOwnProperty = Object.prototype.hasOwnProperty;
3636
const Qs = require('querystring');
3737

@@ -180,9 +180,10 @@ SchemaBuilderEntity.prototype.define = function(name, type, required, custom) {
180180

181181
switch (this.schema[name].type) {
182182
case 7:
183-
if (!this.dependencies)
184-
this.dependencies = [];
185-
this.dependencies.push(name);
183+
if (this.dependencies)
184+
this.dependencies.push(name);
185+
else
186+
this.dependencies = [name];
186187
break;
187188
}
188189

@@ -2345,6 +2346,11 @@ SchemaInstance.prototype.$exec = function(name, helper, callback) {
23452346
return this;
23462347
};
23472348

2349+
SchemaInstance.prototype.$controller = function(controller) {
2350+
this.$$controller = controller;
2351+
return this;
2352+
};
2353+
23482354
SchemaInstance.prototype.$save = function(helper, callback) {
23492355
if (this.$$can && this.$$async)
23502356
this.$push('save', helper);
@@ -3538,6 +3544,7 @@ function async_wait(arr, onItem, onCallback, index) {
35383544
}
35393545

35403546
function RESTBuilder(url) {
3547+
35413548
this.$url = url;
35423549
this.$headers = { 'User-Agent': 'Total.js/v' + F.version_header, Accept: 'application/json, text/plain, text/plain, text/xml' };
35433550
this.$method = 'get';
@@ -3546,6 +3553,7 @@ function RESTBuilder(url) {
35463553
this.$schema;
35473554
this.$length = 0;
35483555
this.$transform = transforms['restbuilder_default'];
3556+
this.$files = null;
35493557

35503558
// this.$flags;
35513559
// this.$data = {};
@@ -3591,6 +3599,15 @@ RESTBuilder.prototype.url = function(url) {
35913599
return this;
35923600
};
35933601

3602+
RESTBuilder.prototype.file = function(name, filename, buffer) {
3603+
var obj = { name: name, filename: filename, buffer: buffer };
3604+
if (this.$files)
3605+
this.$files.push(obj);
3606+
else
3607+
this.$files = [obj];
3608+
return this;
3609+
};
3610+
35943611
RESTBuilder.prototype.maketransform = function(obj, data) {
35953612
if (this.$transform) {
35963613
var fn = transforms['restbuilder'][this.$transform];
@@ -3657,6 +3674,22 @@ RESTBuilder.prototype.origin = function(value) {
36573674
return this;
36583675
};
36593676

3677+
RESTBuilder.prototype.robot = function() {
3678+
if (this.$headers['User-Agent'])
3679+
this.$headers['User-Agent'] += ' Bot';
3680+
else
3681+
this.$headers['User-Agent'] = 'Bot';
3682+
return this;
3683+
};
3684+
3685+
RESTBuilder.prototype.mobile = function() {
3686+
if (this.$headers['User-Agent'])
3687+
this.$headers['User-Agent'] += ' iPhone';
3688+
else
3689+
this.$headers['User-Agent'] = 'iPhone';
3690+
return this;
3691+
};
3692+
36603693
RESTBuilder.prototype.put = function(data) {
36613694
if (this.$method !== 'put') {
36623695
this.$flags = null;
@@ -3814,12 +3847,26 @@ RESTBuilder.prototype.stream = function(callback) {
38143847
return U.download(self.$url, flags, self.$data, callback, self.$cookies, self.$headers, undefined, self.$timeout);
38153848
};
38163849

3850+
RESTBuilder.prototype.file = function(name, filename) {
3851+
var self = this;
3852+
var obj = { name: name, filename: filename };
3853+
if (self.$files)
3854+
self.$files.push(obj);
3855+
else
3856+
self.$files = [obj];
3857+
return self;
3858+
};
3859+
38173860
RESTBuilder.prototype.exec = function(callback) {
38183861

38193862
if (!callback)
38203863
callback = NOOP;
38213864

38223865
var self = this;
3866+
3867+
if (self.$files && self.$method === 'get')
3868+
self.$method = 'post';
3869+
38233870
var flags = self.$flags ? self.$flags : [self.$method];
38243871
var key;
38253872

@@ -3829,14 +3876,19 @@ RESTBuilder.prototype.exec = function(callback) {
38293876
self.$length && flags.push('<' + self.$length);
38303877
self.$redirect === false && flags.push('noredirect');
38313878

3832-
switch (self.$type) {
3833-
case 1:
3834-
flags.push('json');
3835-
break;
3836-
case 3:
3837-
flags.push('xml');
3838-
break;
3879+
if (self.$files) {
3880+
flags.push('upload');
3881+
} else {
3882+
switch (self.$type) {
3883+
case 1:
3884+
flags.push('json');
3885+
break;
3886+
case 3:
3887+
flags.push('xml');
3888+
break;
3889+
}
38393890
}
3891+
38403892
self.$flags = flags;
38413893
}
38423894

@@ -3886,7 +3938,7 @@ RESTBuilder.prototype.exec = function(callback) {
38863938
callback(err, self.maketransform(output.value, output), output);
38873939
output.cache = true;
38883940

3889-
}, self.$cookies, self.$headers, undefined, self.$timeout);
3941+
}, self.$cookies, self.$headers, undefined, self.$timeout, self.$files);
38903942
};
38913943

38923944
function exec_removelisteners(evt) {
@@ -3988,7 +4040,9 @@ exports.Pagination = Pagination;
39884040
exports.Page = Page;
39894041
exports.UrlBuilder = UrlBuilder;
39904042
exports.TransformBuilder = TransformBuilder;
4043+
exports.SchemaOptions = SchemaOptions;
39914044
global.RESTBuilder = RESTBuilder;
4045+
global.RESTBuilderResponse = RESTBuilderResponse;
39924046
global.ErrorBuilder = ErrorBuilder;
39934047
global.TransformBuilder = TransformBuilder;
39944048
global.Pagination = Pagination;

changes.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1+
======= 2.7.0
2+
3+
- added: __IMPORTANT__ new unit-testing mechanism
4+
- added: __IMPORTANT__ `F.prototypes(function(proto) {})` for extending all internal prototypes
5+
- added: `HttpFile` is set in `global` for extending of prototype
6+
- added: `file.move()` a new alias for `file.rename()`
7+
- added: `SchemaBuilderEntity.$controller(new_controller)`
8+
- added: `EMPTYCONTROLLER` is a global variable
9+
- added: new alias `NOSQL.set()` and `NOSQL.get()` for `NOSQL.meta()`
10+
- added: `RESTBuilder.file(name, filename, [buffer])` supports uploading files
11+
- added: `RESTBuilder.mobile()` adds `iPhone` phrase into the `User-Agent` header
12+
- added: `RESTBuilder.robot()` adds `Bot` phrase into the `User-Agent` header
13+
- added: a small protection for multipart data
14+
- added: a new global aliases `ROUTE()` --> `F.route()`, `FILE()` --> `F.file()` and `WEBSOCKET()` --> `F.websocket()`
15+
16+
- updated: __IMPORTANT__ components (framework can render css/js from specific group)
17+
- updated: `F.cluster` each operation checks whether cluster is activated
18+
- updated: default IP to `0.0.0.0`
19+
- updated: `Date.prototype.format()` with `ddd` renders name of day with 2 capital letters
20+
21+
- fixed: new schemas with defined callback `function($)`
22+
- fixed: loading of `config-test` file (added rewriting of existing values)
23+
- fixed: Total.js version in `debug.js`
24+
- fixed: cluster initialization
25+
26+
- improved: `cors` in `F.restful()` and `F.restful2()`
27+
- improved: `auto-vendor-prefixes`
28+
- improved: parsing files from multipart data
29+
130
======= 2.6.2 (HOTFIX)
231

332
- fixed: a critical bug with `debug.js`
33+
- fixed: `try/catch` block from parsing of WebSocket message
434

535
======= 2.6.1 (HOTFIX)
636

cluster.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* @module FrameworkCluster
24-
* @version 2.6.0
24+
* @version 2.7.0
2525
*/
2626

2727
const Cluster = require('cluster');
@@ -36,14 +36,19 @@ var OPTIONS = {};
3636
var THREADS = 0;
3737

3838
exports.emit = function(name, data) {
39-
CLUSTER_EMIT.name = name;
40-
CLUSTER_EMIT.data = data;
41-
process.send(CLUSTER_EMIT);
39+
if (F.isCluster) {
40+
CLUSTER_EMIT.name = name;
41+
CLUSTER_EMIT.data = data;
42+
process.send(CLUSTER_EMIT);
43+
}
4244
return F;
4345
};
4446

4547
exports.request = function(name, data, callback, timeout) {
4648

49+
if (!F.isCluster)
50+
return F;
51+
4752
if (typeof(data) === 'function') {
4853
timeout = callback;
4954
callback = data;
@@ -71,6 +76,9 @@ exports.response = function(name, callback) {
7176

7277
exports.req = function(message) {
7378

79+
if (!F.isCluster)
80+
return F;
81+
7482
// message.id
7583
// message.name
7684
// message.data
@@ -114,9 +122,11 @@ exports.restart = function(index) {
114122
exports.restart(i);
115123
} else {
116124
var fork = FORKS[index];
117-
fork.removeAllListeners();
118-
fork.disconnect();
119-
exec(index);
125+
if (fork) {
126+
fork.removeAllListeners();
127+
fork.disconnect();
128+
exec(index);
129+
}
120130
}
121131
};
122132

@@ -168,23 +178,26 @@ function exec(index) {
168178
(function(fork) {
169179
setTimeout(function() {
170180
OPTIONS.options.id = fork.$id;
171-
fork.send({ type: 'init', id: fork.$id, mode: OPTIONS.mode, options: OPTIONS.options, threads: OPTIONS.count, index: index });
181+
fork.send({ TYPE: 'init', id: fork.$id, mode: OPTIONS.mode, options: OPTIONS.options, threads: OPTIONS.count, index: index });
172182
}, fork.$id * 500);
173183
})(fork);
174184
}
175185

176186
function fork() {
177187
require('./index');
178-
F.once('message', function(msg) {
179-
switch (msg.type) {
180-
case 'init':
181-
CLUSTER_EMIT.id = msg.id;
182-
CLUSTER_REQ.id = msg.id;
183-
CLUSTER_RES.id = msg.id;
184-
THREADS = msg.threads;
185-
F.http(msg.mode, msg.options);
186-
F.isCluster = true;
187-
break;
188-
}
189-
});
188+
F.on('message', on_init);
189+
}
190+
191+
function on_init(msg) {
192+
switch (msg.TYPE) {
193+
case 'init':
194+
CLUSTER_EMIT.id = msg.id;
195+
CLUSTER_REQ.id = msg.id;
196+
CLUSTER_RES.id = msg.id;
197+
THREADS = msg.threads;
198+
F.http(msg.mode, msg.options);
199+
F.isCluster = true;
200+
F.removeListener(msg.TYPE, on_init);
201+
break;
202+
}
190203
}

debug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function runwatching() {
5050

5151
const FILENAME = U.getName(process.argv[1]);
5252
const directory = process.cwd();
53-
const VERSION = '2.6.2';
53+
const VERSION = F.version_header;
5454
const TIME = 2000;
5555
const REG_CONFIGS = /configs\//g;
5656
const REG_FILES = /config\-debug|config\-release|config|versions|sitemap|dependencies|\.js|\.resource/i;

0 commit comments

Comments
 (0)