febs-browser 1.2.9 → 1.2.11

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.
package/libs/net.fetch.js CHANGED
@@ -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
 
package/libs/net.js CHANGED
@@ -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
  };
package/package.json CHANGED
@@ -88,9 +88,8 @@
88
88
  }
89
89
  ],
90
90
  "scripts": {
91
- "build": "node scripts/webpack.build.js && cp -rf dist/wxmini/index.js wxmini-package/ && node scripts/bundle.js",
92
- "prepublishOnly": "node scripts/webpack.build.js && cp -rf dist/wxmini/index.js wxmini-package/ && node scripts/bundle.js"
91
+ "build": "node --max-old-space-size=4096 scripts/webpack.build.js && cp -rf dist/wxmini/index.js wxmini-package/ && node --max-old-space-size=4096 scripts/bundle.js"
93
92
  },
94
93
  "name": "febs-browser",
95
- "version": "1.2.9"
94
+ "version": "1.2.11"
96
95
  }
package/types/fetch.d.ts CHANGED
@@ -44,7 +44,8 @@ export interface RequestInit {
44
44
  timeout?: number;
45
45
  mode?: RequestMode,
46
46
  credentials?: RequestCredentials,
47
- progress?: (progress:number)=>void,
47
+ progress?: (progress: number) => void,
48
+ signal?: any; // AbortControllerObject.signal
48
49
  }
49
50
 
50
51
  export namespace Response {
@@ -99,3 +100,9 @@ export type Fetch = (
99
100
  url: RequestInfo,
100
101
  init?: RequestInit
101
102
  )=>Promise<Response>;
103
+
104
+ export interface AbortControllerClass {
105
+ new(): AbortControllerClass;
106
+ get signal(): any;
107
+ abort(): void;
108
+ }
package/types/index.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  /// <reference types="node" />
5
5
 
6
- import { Fetch } from './fetch.d';
6
+ import { Fetch, AbortControllerClass } from './fetch.d';
7
7
 
8
8
  declare global {
9
9
  /**
@@ -912,7 +912,16 @@ export namespace net {
912
912
  if (err === 'timeout) // 超时.
913
913
  });
914
914
  */
915
- const fetch : Fetch;
915
+ const fetch: Fetch;
916
+
917
+ /**
918
+ * let abortController = new febs.net.AbortController();
919
+ *
920
+ * febs.net.fetch(url, {signal: abortController.signal}).then(...);
921
+ *
922
+ * abortController.abort();
923
+ */
924
+ const AbortController: AbortControllerClass;
916
925
 
917
926
  /**
918
927
  * @desc: jsonp方式获取数据.