From 7e1e66128504fbec52d71537d4824f33f75f0db0 Mon Sep 17 00:00:00 2001 From: Lee Yeh Date: Fri, 6 Mar 2020 21:49:59 +0800 Subject: [PATCH] fix: polish Adaptor remove the core-weapp bundle as it is no longer required. --- package-lock.json | 114 +++++++++++++++++++++++-------- package.json | 6 +- rollup/rollup.config.realtime.js | 1 - src/builtin-adaptors.js | 4 +- src/core.js | 1 - src/index.js | 8 +-- src/utils/request.js | 9 ++- 7 files changed, 98 insertions(+), 45 deletions(-) diff --git a/package-lock.json b/package-lock.json index 174e2d356..43d060415 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2856,7 +2856,8 @@ "async-limiter": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", - "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" + "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==", + "dev": true }, "asynckit": { "version": "0.4.0", @@ -3236,6 +3237,7 @@ "resolved": "https://registry.npmjs.org/boom/-/boom-2.10.1.tgz", "integrity": "sha1-OciRjO/1eZ+D+UkqhI9iWt0Mdm8=", "dev": true, + "optional": true, "requires": { "hoek": "2.x.x" } @@ -3343,7 +3345,8 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/buffer-more-ints/-/buffer-more-ints-1.0.0.tgz", "integrity": "sha512-EMetuGFz5SLsT0QTnXzINh4Ksr+oo4i+UGTXEshiGCQWnsgSs7ZhJ8fzlwQ+OzEMs0MpDAMr1hxnblp5a4vcHg==", - "dev": true + "dev": true, + "optional": true }, "buildmail": { "version": "4.0.1", @@ -5766,7 +5769,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -5787,12 +5791,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -5807,17 +5813,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -5934,7 +5943,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -5946,6 +5956,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -5960,6 +5971,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -5967,12 +5979,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -5991,6 +6005,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -6071,7 +6086,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -6083,6 +6099,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -6168,7 +6185,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -6204,6 +6222,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -6223,6 +6242,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -6266,12 +6286,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -6919,7 +6941,8 @@ "version": "2.16.3", "resolved": "https://registry.npmjs.org/hoek/-/hoek-2.16.3.tgz", "integrity": "sha1-ILt0A9POo5jpHcRxCo/xuCdKJe0=", - "dev": true + "dev": true, + "optional": true }, "hosted-git-info": { "version": "2.7.1", @@ -6982,6 +7005,7 @@ "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-2.1.0.tgz", "integrity": "sha512-qwHbBLV7WviBl0rQsOzH6o5lwyOIvwp/BdFnvVxXORldu5TmjFfjzBcWUWS5kWAZhmv+JtiDhSuQCp4sBfbIgg==", "dev": true, + "optional": true, "requires": { "agent-base": "4", "debug": "3.1.0" @@ -6992,6 +7016,7 @@ "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", "dev": true, + "optional": true, "requires": { "ms": "2.0.0" } @@ -7014,6 +7039,7 @@ "resolved": "https://registry.npmjs.org/httpntlm/-/httpntlm-1.6.1.tgz", "integrity": "sha1-rQFScUOi6Hc8+uapb1hla7UqNLI=", "dev": true, + "optional": true, "requires": { "httpreq": ">=0.4.22", "underscore": "~1.7.0" @@ -7023,7 +7049,8 @@ "version": "1.7.0", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.7.0.tgz", "integrity": "sha1-a7rwh3UA02vjTsqlhODbn+8DUgk=", - "dev": true + "dev": true, + "optional": true } } }, @@ -7031,7 +7058,8 @@ "version": "0.4.24", "resolved": "https://registry.npmjs.org/httpreq/-/httpreq-0.4.24.tgz", "integrity": "sha1-QzX/2CzZaWaKOUZckprGHWOTYn8=", - "dev": true + "dev": true, + "optional": true }, "https-proxy-agent": { "version": "2.2.1", @@ -7242,7 +7270,8 @@ "version": "1.1.5", "resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz", "integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=", - "dev": true + "dev": true, + "optional": true }, "is-accessor-descriptor": { "version": "0.1.6", @@ -7467,7 +7496,8 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=", - "dev": true + "dev": true, + "optional": true }, "is-regex": { "version": "1.0.4", @@ -7904,6 +7934,17 @@ "protobufjs": "^5.0.1", "uuid": "^3.0.0", "ws": "^5.2.1" + }, + "dependencies": { + "ws": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-5.2.2.tgz", + "integrity": "sha512-jaHFD6PFv6UgoIVda6qZllptQsMlDEJkTQcybzzXDYM1XO9Y8em691FGMPmM46WGyLU4z9KMgQN+qrux/nhlHA==", + "dev": true, + "requires": { + "async-limiter": "~1.0.0" + } + } } }, "leancloud-realtime-plugin-live-query": { @@ -7976,13 +8017,15 @@ "version": "0.1.0", "resolved": "https://registry.npmjs.org/libbase64/-/libbase64-0.1.0.tgz", "integrity": "sha1-YjUag5VjrF/1vSbxL2Dpgwu3UeY=", - "dev": true + "dev": true, + "optional": true }, "libmime": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/libmime/-/libmime-3.0.0.tgz", "integrity": "sha1-UaGp50SOy9Ms2lRCFnW7IbwJPaY=", "dev": true, + "optional": true, "requires": { "iconv-lite": "0.4.15", "libbase64": "0.1.0", @@ -7993,7 +8036,8 @@ "version": "0.4.15", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.15.tgz", "integrity": "sha1-/iZaIYrGpXz+hUkn6dBMGYJe3es=", - "dev": true + "dev": true, + "optional": true } } }, @@ -8001,7 +8045,8 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/libqp/-/libqp-1.1.0.tgz", "integrity": "sha1-9ebgatdLeU+1tbZpiL9yjvHe2+g=", - "dev": true + "dev": true, + "optional": true }, "load-json-file": { "version": "4.0.0", @@ -8946,13 +8991,15 @@ "version": "1.6.0", "resolved": "https://registry.npmjs.org/nodemailer-fetch/-/nodemailer-fetch-1.6.0.tgz", "integrity": "sha1-ecSQihwPXzdbc/6IjamCj23JY6Q=", - "dev": true + "dev": true, + "optional": true }, "nodemailer-shared": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/nodemailer-shared/-/nodemailer-shared-1.1.0.tgz", "integrity": "sha1-z1mU4v0mjQD1zw+nZ6CBae2wfsA=", "dev": true, + "optional": true, "requires": { "nodemailer-fetch": "1.6.0" } @@ -8985,7 +9032,8 @@ "version": "0.1.10", "resolved": "https://registry.npmjs.org/nodemailer-wellknown/-/nodemailer-wellknown-0.1.10.tgz", "integrity": "sha1-WG24EB2zDLRDjrVGc3pBqtDPE9U=", - "dev": true + "dev": true, + "optional": true }, "normalize-package-data": { "version": "2.5.0", @@ -10651,6 +10699,11 @@ "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==", "dev": true }, + "promise-timeout": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/promise-timeout/-/promise-timeout-1.3.0.tgz", + "integrity": "sha512-5yANTE0tmi5++POym6OgtFmwfDvOXABD9oj/jLQr5GPEyuNEb7jH4wbbANJceJid49jwhi1RddxnhnEAb/doqg==" + }, "promisify-call": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/promisify-call/-/promisify-call-2.0.4.tgz", @@ -11446,13 +11499,15 @@ "version": "4.0.2", "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.0.2.tgz", "integrity": "sha512-JDhEpTKzXusOqXZ0BUIdH+CjFdO/CR3tLlf5CN34IypI+xMmXW1uB16OOY8z3cICbJlDAVJzNbwBhNO0wt9OAw==", - "dev": true + "dev": true, + "optional": true }, "smtp-connection": { "version": "2.12.0", "resolved": "https://registry.npmjs.org/smtp-connection/-/smtp-connection-2.12.0.tgz", "integrity": "sha1-1275EnyyPCJZ7bHoNJwujV4tdME=", "dev": true, + "optional": true, "requires": { "httpntlm": "1.6.1", "nodemailer-shared": "1.1.0" @@ -11680,6 +11735,7 @@ "resolved": "https://registry.npmjs.org/socks/-/socks-2.2.3.tgz", "integrity": "sha512-+2r83WaRT3PXYoO/1z+RDEBE7Z2f9YcdQnJ0K/ncXXbV5gJ6wYfNAebYFYiiUjM6E4JyXnPY8cimwyvFYHVUUA==", "dev": true, + "optional": true, "requires": { "ip": "^1.1.5", "smart-buffer": "4.0.2" @@ -11690,6 +11746,7 @@ "resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-4.0.1.tgz", "integrity": "sha512-Kezx6/VBguXOsEe5oU3lXYyKMi4+gva72TwJ7pQY5JfqUx2nMk7NXA6z/mpNqIlfQjWYVfeuNvQjexiTaTn6Nw==", "dev": true, + "optional": true, "requires": { "agent-base": "~4.2.0", "socks": "~2.2.0" @@ -12792,12 +12849,9 @@ } }, "ws": { - "version": "5.2.2", - "resolved": "https://registry.npmjs.org/ws/-/ws-5.2.2.tgz", - "integrity": "sha512-jaHFD6PFv6UgoIVda6qZllptQsMlDEJkTQcybzzXDYM1XO9Y8em691FGMPmM46WGyLU4z9KMgQN+qrux/nhlHA==", - "requires": { - "async-limiter": "~1.0.0" - } + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.1.tgz", + "integrity": "sha512-sucePNSafamSKoOqoNfBd8V0StlkzJKL2ZAhGQinCfNQ+oacw+Pk7lcdAElecBF2VkLNZRiIb5Oi1Q5lVUVt2A==" }, "xmlhttprequest-ssl": { "version": "1.5.5", diff --git a/package.json b/package.json index fa907d416..2aa37d6c6 100644 --- a/package.json +++ b/package.json @@ -89,10 +89,11 @@ "eventemitter3": "^3.0.0", "javascript-state-machine": "^2.3.5", "lodash": "^4.17.10", + "promise-timeout": "^1.3.0", "protobufjs": "^5.0.1", "superagent": "^5.1.0", "uuid": "^3.0.0", - "ws": "^5.2.1" + "ws": "^7.2.1" }, "browser": { "ws": "./src/polyfills/ws-browser.js", @@ -102,8 +103,7 @@ "weapp": { "ws": "weapp-polyfill/websocket.js", "./src/polyfills/polyfills.js": "./src/polyfills/polyfills-weapp.js", - "./dist/realtime.js": "./dist/realtime-weapp.js", - "./dist/realtime-core.js": "./dist/realtime-core-weapp.js" + "./dist/realtime.js": "./dist/realtime-weapp.js" }, "nyc": { "require": [ diff --git a/rollup/rollup.config.realtime.js b/rollup/rollup.config.realtime.js index 8f617e3ba..ca0b778af 100644 --- a/rollup/rollup.config.realtime.js +++ b/rollup/rollup.config.realtime.js @@ -18,5 +18,4 @@ export default [ core(browser), weapp, minify(weapp), - core(weapp), ]; diff --git a/src/builtin-adaptors.js b/src/builtin-adaptors.js index dfd921d21..2788e642e 100644 --- a/src/builtin-adaptors.js +++ b/src/builtin-adaptors.js @@ -1,8 +1,8 @@ import superagent from 'superagent'; import WebSocket from 'ws'; -export function request(url, { method, data, headers, timeout }) { - const req = superagent(method, url).timeout(timeout); +export function request(url, { method, data, headers }) { + const req = superagent(method, url); if (headers) { req.set(headers); } diff --git a/src/core.js b/src/core.js index 66b0e3a8a..4e5a3c014 100644 --- a/src/core.js +++ b/src/core.js @@ -1,4 +1,3 @@ -import './polyfills/polyfills'; import * as Protocols from '../proto/message'; export { diff --git a/src/index.js b/src/index.js index 64aee952a..8eb39b37b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,15 +1,13 @@ /** @module leancloud-realtime */ +import './polyfills/polyfills'; import { Realtime, setAdaptors } from './core'; -import { request, WebSocket } from './builtin-adaptors'; +import * as adaptors from './builtin-adaptors'; import { IMPlugin, defineConversationProperty } from './plugin-im'; import MessageParser from './message-parser'; import * as CoreEvent from './events/core'; import * as IMEvent from './events/im'; -setAdaptors({ - request, - WebSocket, -}); +setAdaptors(adaptors); Realtime.defineConversationProperty = defineConversationProperty; Realtime.__preRegisteredPlugins = [IMPlugin]; diff --git a/src/utils/request.js b/src/utils/request.js index dbd0610c5..3b5e6bbed 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,5 +1,6 @@ import d from 'debug'; import isPlainObject from 'lodash/isPlainObject'; +import { timeout } from 'promise-timeout'; import { getAdaptor } from '../adaptor'; import { createError } from '../error'; @@ -11,7 +12,7 @@ export default ({ query, headers, data, - timeout, + timeout: time, }) => { let url = _url; if (query) { @@ -26,8 +27,9 @@ export default ({ .join('&'); url = `${url}?${queryString}`; } - debug('Req: %O %O %O', method, url, { query, headers, data }); - return getAdaptor('request')(url, { method, query, headers, data, timeout }) + debug('Req: %O %O %O', method, url, { headers, data }); + const request = getAdaptor('request'); + const promise = request(url, { method, headers, data }) .then(response => { if (response.ok === false) { const error = createError(response.data); @@ -48,4 +50,5 @@ export default ({ } throw error; }); + return time ? timeout(promise, time) : promise; };