febs 1.2.9 → 1.3.0

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.
@@ -28,34 +28,36 @@ var net = {};
28
28
  */
29
29
  function ajax( ctx )
30
30
  {
31
- //if (!!Window.ActiveXObject || "ActiveXObject" in Window) // ie11.
31
+ // 不修改调用方传入的 ctx.url, 只在本次请求的 url 上追加标记.
32
+ var url = ctx.url;
33
+ if (url)
32
34
  {
33
- if (ctx.url)
34
- {
35
- if (!Window[Ajaxmark]) Window[Ajaxmark] = 1;
36
- var i = ctx.url.indexOf('?');
37
- if (i < 0) {
38
- ctx.url += "?ajaxmark="+Window[Ajaxmark];
35
+ if (!Window[Ajaxmark]) Window[Ajaxmark] = 1;
36
+ var i = url.indexOf('?');
37
+ if (i < 0) {
38
+ url += "?ajaxmark="+Window[Ajaxmark];
39
+ } else {
40
+ if (i == url.length-1) {
41
+ url += "ajaxmark="+Window[Ajaxmark];
39
42
  } else {
40
- if (i == ctx.url.length-1) {
41
- ctx.url += "ajaxmark="+Window[Ajaxmark];
42
- } else {
43
- ctx.url += "&ajaxmark="+Window[Ajaxmark];
44
- }
43
+ url += "&ajaxmark="+Window[Ajaxmark];
45
44
  }
46
45
  }
47
- Window[Ajaxmark]++;
48
- } // if.
46
+ }
47
+ Window[Ajaxmark]++;
49
48
 
50
49
  var cbError = ctx.error || function(){}
51
50
  var cbSuccess = ctx.success || function(){}
52
51
  var cbComplete = ctx.complete || function(){}
53
52
 
54
- ctx.processData = ctx.hasOwnProperty('processData') ? ctx.processData : true;
53
+ var processData = ctx.hasOwnProperty('processData') ? ctx.processData : true;
55
54
 
56
55
  //
57
56
  // net transfer.
58
57
  var xhr = transfer.transfer(Window);
58
+ if (!xhr) {
59
+ throw new Error('XMLHttpRequest is not supported in current environment');
60
+ }
59
61
 
60
62
  // xhr.onload = function() {
61
63
  // var status = (xhr.status === 1223) ? 204 : xhr.status
@@ -83,7 +85,7 @@ function ajax( ctx )
83
85
  }
84
86
 
85
87
  var body = 'response' in xhr ? xhr.response : xhr.responseText;
86
- if (status == 200)
88
+ if ((status >= 200 && status < 300) || status == 304)
87
89
  cbSuccess(body, xhr.statusText, xhr);
88
90
 
89
91
  cbComplete(xhr, xhr.statusText);
@@ -99,7 +101,7 @@ function ajax( ctx )
99
101
  cbError(xhr, null, new TypeError('Network request failed'));
100
102
  }
101
103
 
102
- if (ctx.progress) {
104
+ if (ctx && ctx.progress) {
103
105
  if(('upload' in xhr) && ('onprogress' in xhr.upload)) {
104
106
  xhr.upload.onprogress = function(event){
105
107
  if(event.lengthComputable){
@@ -111,16 +113,13 @@ function ajax( ctx )
111
113
  }
112
114
  }
113
115
 
114
- xhr.open(ctx.type, ctx.url, ctx.async===false?false:true);
116
+ xhr.open(ctx.type, url, ctx.async===false?false:true);
115
117
  var timeout = (ctx.async===false?false:true)? ctx.timeout: 0;
116
118
  xhr.timeout = (timeout !== undefined && timeout !== null) ? timeout : transfer.DefaultTimeout;
117
119
 
118
120
  if (ctx.hasOwnProperty('withCredentials')) {
119
121
  xhr.withCredentials = ctx.withCredentials;
120
122
  }
121
- else {
122
- xhr.withCredentials = true;
123
- }
124
123
 
125
124
  if (ctx.headers) {
126
125
  if (xhr.setRequestHeader) {
@@ -138,14 +137,14 @@ function ajax( ctx )
138
137
  }
139
138
  }
140
139
 
141
- if (!ctx.headers || !ctx.headers.hasOwnProperty('Content-Type')) {
140
+ if ((!ctx.headers || !ctx.headers.hasOwnProperty('Content-Type')) && xhr.setRequestHeader) {
142
141
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
143
142
  }
144
143
 
145
144
  // auto content-type.
146
145
  var data_content = ctx.data;
147
146
  if (data_content) {
148
- if (ctx.processData && typeof data_content !== 'string') {
147
+ if (processData && typeof data_content !== 'string') {
149
148
  try {
150
149
 
151
150
  if (ctx.headers && ctx.headers['Content-Type'] && ctx.headers['Content-Type'].toLowerCase().indexOf('json') >= 0) {
@@ -27,6 +27,34 @@ else {
27
27
  throw new Error('unsupported Promise')
28
28
  }
29
29
 
30
+ function AbortController() {
31
+ let _this = this;
32
+ Object.defineProperty(this, 'signal', {
33
+ get: function () {
34
+ if (!_this.__signalWrap) {
35
+ _this.__signalWrap = {};
36
+ }
37
+ return _this.__signalWrap;
38
+ },
39
+ enumerable: true,
40
+ configurable: true,
41
+ });
42
+ }
43
+ AbortController.prototype.abort = function () {
44
+ if (this.__signalWrap && this.__signalWrap.__xhr) {
45
+ try {
46
+ this.__signalWrap.__xhr.abort();
47
+ } catch (e) {
48
+ console.log(e);
49
+ }
50
+ this.__signalWrap.__xhr = null;
51
+ }
52
+ }
53
+
54
+ Window._AbortController = AbortController;
55
+ net.AbortController = AbortController;
56
+
57
+
30
58
  // https://github.com/github/fetch
31
59
  febsnet.normalizeName = function(name) {
32
60
  if (typeof name !== 'string') {
@@ -344,7 +372,7 @@ else {
344
372
  Window.Request = febsnet.Request;
345
373
  Window.Response = febsnet.Response;
346
374
 
347
- Window.fetch = febsnet.fetch = function(input, init) {
375
+ Window.fetch = febsnet.fetch = function (input, init) {
348
376
 
349
377
  // other.
350
378
  return new Promise(function(resolve, reject) {
@@ -390,7 +418,7 @@ else {
390
418
  if (xhr.readyState == 4) {
391
419
  var status = (xhr.status === 1223) ? 204 : xhr.status
392
420
  if (status < 100 || status > 599) {
393
- reject(new exception('Network request failed', 'NetworkFailed', __filename, __line, __column))
421
+ reject(new exception('Network request failed', 'NetworkFailed', null, null, null))
394
422
  return
395
423
  }
396
424
  var options = {
@@ -405,20 +433,24 @@ else {
405
433
  }
406
434
 
407
435
  xhr.ontimeout = function() {
408
- reject(new exception('Network timeout', 'NetworkTimeout', __filename, __line, __column))
436
+ reject(new exception('Network timeout', 'NetworkTimeout', null, null, null))
409
437
  }
410
438
  xhr.onerror = function() {
411
- reject(new exception('Network request failed', 'NetworkFailed', __filename, __line, __column))
439
+ reject(new exception('Network request failed', 'NetworkFailed', null, null, null))
412
440
  }
413
441
 
414
- if (init.progress) {
442
+ if (init && init.progress) {
415
443
  xhr.onprogress = function(event){
416
- if(event.lengthComputable){
417
- init.progress(event.position/event.totalSize);
444
+ if (event.lengthComputable) {
445
+ init.progress(((event.position||event.loaded)||0) / ((event.totalSize||event.total)||1));
418
446
  }
419
447
  }
420
448
  }
421
449
 
450
+ if (init && init.signal) {
451
+ init.signal.__xhr = xhr;
452
+ }
453
+
422
454
  xhr.open(request.method, request.url, true)
423
455
  var timeout = init?init.timeout:null;
424
456
  xhr.timeout = (timeout !== undefined && timeout !== null) ? timeout : transfer.DefaultTimeout;
@@ -442,7 +474,11 @@ else {
442
474
  console.log('fetch can\'t set headers');
443
475
  }
444
476
 
445
- xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
477
+ try {
478
+ xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
479
+ } catch (e) {
480
+ reject(new exception('Network request failed', 'NetworkFailed', null, null, null))
481
+ }
446
482
  })
447
483
  }
448
484
 
@@ -14,6 +14,7 @@ var netjsonp = require('./net.jsonp');
14
14
 
15
15
  var net = {
16
16
  ajax: netajax.ajax,
17
+ AbortController: netfetch.AbortController,
17
18
  fetch: netfetch.fetch,
18
19
  jsonp: netjsonp.jsonp
19
20
  };
@@ -6,20 +6,14 @@
6
6
 
7
7
  exports.DefaultTimeout = 5000;
8
8
 
9
+ /**
10
+ * @desc: 创建 XMLHttpRequest 实例.
11
+ * 不再支持 IE8/9 的 ActiveXObject.
12
+ */
9
13
  exports.transfer = function(window) {
10
- var xhr;
11
- if (window.XMLHttpRequest) xhr = new XMLHttpRequest();
12
- else if (window.XDomainRequest) xhr = new XDomainRequest();
13
- else {
14
- var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
15
- for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++) {
16
- try {
17
- xhr = new ActiveXObject(XmlHttpVersions[i]);
18
- }
19
- catch (e) {
20
- }
21
- }
14
+ if (window && window.XMLHttpRequest) {
15
+ return new window.XMLHttpRequest();
22
16
  }
23
-
24
- return xhr;
25
- }
17
+
18
+ return null;
19
+ }
package/package.json CHANGED
@@ -46,15 +46,18 @@
46
46
  "index.js"
47
47
  ],
48
48
  "license": "MIT",
49
+ "scripts": {
50
+ "build": "npm --prefix browser run build"
51
+ },
49
52
  "main": "./index.js",
50
53
  "miniprogram": "./browser/dist/wxmini",
51
54
  "types": "./types/index.d.ts",
52
55
  "maintainers": [
53
56
  {
54
57
  "name": "brainpoint",
55
- "email": "bpoint.lee@gmail.com"
58
+ "email": "bpoint.lee@live.com"
56
59
  }
57
60
  ],
58
61
  "name": "febs",
59
- "version": "1.2.9"
62
+ "version": "1.3.0"
60
63
  }
package/server/crypt.js CHANGED
@@ -60,7 +60,7 @@ function crc32_fileSegment( filename, offset, length ) {
60
60
  }
61
61
 
62
62
  if (stat.size-offset < length) {
63
- length = file.size-offset;
63
+ length = stat.size-offset;
64
64
  }
65
65
 
66
66
  var fd = fs.openSync(filename, 'r');
@@ -68,7 +68,7 @@ function crc32_fileSegment( filename, offset, length ) {
68
68
  return 0;
69
69
 
70
70
  var crc = 0;
71
- var buf = new Buffer(1024*10);
71
+ var buf = Buffer.alloc(1024*10);
72
72
  var size;
73
73
  for (var i = offset, len = offset+length; i < len; i+=buf.length) {
74
74
  size = buf.length > len-i ? len-i : buf.length;
@@ -129,7 +129,7 @@ exports.md5_file = function( filename ) {
129
129
  if (!fd)
130
130
  return 0;
131
131
 
132
- var buf = new Buffer(1024*10);
132
+ var buf = Buffer.alloc(1024*10);
133
133
  var size;
134
134
 
135
135
  var md5 = crypto.createHash('md5');
@@ -191,7 +191,7 @@ exports.sha1_file = function( filename ) {
191
191
  if (!fd)
192
192
  return 0;
193
193
 
194
- var buf = new Buffer(1024*10);
194
+ var buf = Buffer.alloc(1024*10);
195
195
  var size;
196
196
 
197
197
  var sha1 = crypto.createHash('sha1');
package/server/utils.js CHANGED
@@ -276,24 +276,34 @@ exports.execCommand = function execCommand(cmdString, params, optionOrCbFinish,
276
276
  if (reject) reject(e);
277
277
  else cbFinish(e, null, null);
278
278
  });
279
- proc.stdout.on('data', function(data) {
280
- out += data;
279
+ proc.stdout.on('data', function (data) {
280
+ if (resolve) {
281
+ out += data;
282
+ }
283
+ else {
284
+ cbFinish(null, data.toString(), null)
285
+ }
281
286
  });
282
- proc.stderr.on('data', function(data) {
283
- err += data;
287
+ proc.stderr.on('data', function (data) {
288
+ if (resolve) {
289
+ err += data;
290
+ }
291
+ else {
292
+ cbFinish(null, null, data.toString())
293
+ }
284
294
  });
285
295
  proc.on('close', function (code) {
286
- if (code !== 0) {
287
- console.log(code);
288
- } else {
289
- }
296
+ // if (code !== 0) {
297
+ // console.log(code);
298
+ // } else {
299
+ // }
290
300
  if (code !== 0) {
291
301
  if (reject) reject(code);
292
- else cbFinish(code, out, err);
302
+ else cbFinish(code, null, null);
293
303
  }
294
304
  else {
295
305
  if (resolve) resolve({stdout:out, stderr:err});
296
- else cbFinish(code, out, err);
306
+ else cbFinish(code, null, null);
297
307
  }
298
308
  });
299
309
  }
package/types/index.d.ts CHANGED
@@ -742,7 +742,7 @@ export namespace utils {
742
742
  * @desc: 执行cmd.
743
743
  * @param cmd: 指令.
744
744
  * @param params: 输入参数数组.
745
- * @param cbFinish: 完成的回调.
745
+ * @param cbFinish: 有信息输出时的回调. 当stdout与stderr都为null时, 进程结束.
746
746
  */
747
747
  function execCommand(cmd:string, params:string[], options:{cwd?:string}, cbFinish:(err:any, stdout:string, stderr:string)=>void):void;
748
748
  /**