From f07a3c6264218c10e37aa6e34a9d9d0bac3c9feb Mon Sep 17 00:00:00 2001 From: Zhuohuan LI Date: Mon, 8 Aug 2016 00:58:25 +0800 Subject: [PATCH] add UUID support for io protocol --- src/contact.js | 8 ++--- src/io.js | 15 +++++++-- src/message-media.js | 4 +-- src/message.js | 8 ++--- src/puppet-web/browser.js | 12 +++++-- src/room.js | 6 ++-- src/{web-util.js => util.js} | 43 ++++++++++++++++--------- src/wechaty.js | 4 +++ test/{web-util.spec.js => util.spec.js} | 12 +++---- 9 files changed, 71 insertions(+), 41 deletions(-) rename src/{web-util.js => util.js} (84%) rename test/{web-util.spec.js => util.spec.js} (84%) diff --git a/src/contact.js b/src/contact.js index 2c349bdc..bbc77365 100644 --- a/src/contact.js +++ b/src/contact.js @@ -6,8 +6,8 @@ * https://github.com/zixia/wechaty * */ -const log = require('./npmlog-env') -const webUtil = require('./web-util') +const log = require('./npmlog-env') +const Util = require('./util') class Contact { constructor(id) { @@ -40,8 +40,8 @@ class Contact { , stranger: !!rawObj.stranger // assign by injectio.js } } - - name() { return webUtil.plainText(this.obj.name) } + + name() { return Util.plainText(this.obj.name) } remark() { return this.obj.remark } stranger() { return this.obj.stranger } star() { return this.obj.star } diff --git a/src/io.js b/src/io.js index a749237c..069ca48e 100644 --- a/src/io.js +++ b/src/io.js @@ -30,8 +30,9 @@ class Io { this.wechaty = wechaty this.token = token this.endpoint = endpoint - this.protocol = protocol - log.verbose('Io', 'instantiated with endpoint[%s], token[%s], protocol[%s]', endpoint, token, protocol) + + this.protocol = protocol + '|' + wechaty.uuid + log.verbose('Io', 'instantiated with endpoint[%s], token[%s], protocol[%s], uuid[%s]', endpoint, token, protocol, this.uuid) } toString() { return 'Class Io(' + this.token + ')'} @@ -74,7 +75,7 @@ class Io { const initEvent = { name: 'sys' - , payload: 'Wechaty version ' + this.wechaty.version() + , payload: 'Wechaty version ' + this.wechaty.version() + ` with UUID: ${this.uuid}` } this.send(initEvent) @@ -227,6 +228,13 @@ class Io { ioEvent.payload = data.toString() break + case 'heartbeat': + ioEvent.payload = { + uuid: this.uuid + , data: data + } + break + default: break } @@ -277,6 +285,7 @@ class Io { ioMessage(m) { log.verbose('Io', 'ioMessage() is a nop function before be overwriten from cloud') } + } /** diff --git a/src/message-media.js b/src/message-media.js index b36c1f42..48bab8b9 100644 --- a/src/message-media.js +++ b/src/message-media.js @@ -9,7 +9,7 @@ const co = require('co') const log = require('./npmlog-env') -const webUtil = require('./web-util') +const Util = require('./util') const Message = require('./message') class MediaMessage extends Message { @@ -47,7 +47,7 @@ class MediaMessage extends Message { return Message.puppet.browser.checkSession() }) .then(cookies => { - return webUtil.downloadStream(this.obj.url, cookies) + return Util.downloadStream(this.obj.url, cookies) }) .catch(e => { log.warn('MediaMessage', 'stream() exception: %s', e.message) diff --git a/src/message.js b/src/message.js index d1543f0a..886289e0 100644 --- a/src/message.js +++ b/src/message.js @@ -11,8 +11,8 @@ const co = require('co') const Contact = require('./contact') const Room = require('./room') -const webUtil = require('./web-util') -const log = require('./npmlog-env') +const Util = require('./util') +const log = require('./npmlog-env') class Message { constructor(rawObj) { @@ -52,7 +52,7 @@ class Message { return obj } toString() { - return webUtil.plainText(this.obj.content) + return Util.plainText(this.obj.content) } toStringDigest() { const text = webUtil.digestEmoji(this.obj.digest) @@ -71,7 +71,7 @@ class Message { return '<' + name + (room ? `@${room}` : '') + '>' } getContentString() { - let content = webUtil.plainText(this.obj.content) + let content = Util.plainText(this.obj.content) if (content.length > 20) { content = content.substring(0,17) + '...' } return '{' + this.type() + '}' + content } diff --git a/src/puppet-web/browser.js b/src/puppet-web/browser.js index 81449dd7..2dc4754e 100644 --- a/src/puppet-web/browser.js +++ b/src/puppet-web/browser.js @@ -92,8 +92,9 @@ class Browser extends EventEmitter { // XXX: if no `setTimeout()` here, promise will hang forever! // with a confirmed bug in selenium-webdriver v2.53.2: // https://github.com/SeleniumHQ/selenium/issues/2233 - setTimeout(() => { resolve(this.driver) }, 0) - // resolve(this.driver) + // FIXED: selenium v3 released 20160807 + // setTimeout(() => { resolve(this.driver) }, 0) + resolve(this.driver) }) } @@ -135,9 +136,14 @@ class Browser extends EventEmitter { log.silly('PuppetWebBrowser', 'phantomjs binary: ' + phantomjsExe) log.silly('PuppetWebBrowser', 'phantomjs args: ' + phantomjsArgs.join(' ')) - return new WebDriver.Builder() + const driver = new WebDriver.Builder() .withCapabilities(customPhantom) .build() + + // https://github.com/detro/ghostdriver/blob/f976007a431e634a3ca981eea743a2686ebed38e/src/session.js#L233 + // driver.manage().timeouts().pageLoadTimeout(2000) + + return driver } quit() { diff --git a/src/room.js b/src/room.js index 5d422c84..16fa71ec 100644 --- a/src/room.js +++ b/src/room.js @@ -6,8 +6,8 @@ * https://github.com/zixia/wechaty * */ -const log = require('./npmlog-env') -const webUtil = require('./web-util') +const log = require('./npmlog-env') +const Util = require('./util') class Room { constructor(id) { @@ -46,7 +46,7 @@ class Room { }) } - name() { return webUtil.plainText(this.obj.name) } + name() { return Util.plainText(this.obj.name) } get(prop) { return this.obj[prop] } parse(rawObj) { diff --git a/src/web-util.js b/src/util.js similarity index 84% rename from src/web-util.js rename to src/util.js index 5a69634a..0b770450 100644 --- a/src/web-util.js +++ b/src/util.js @@ -1,13 +1,15 @@ const http = require('http') -const log = require('./npmlog-env') - -const WebUtil = { - stripHtml: stripHtml - , unescapeHtml: unescapeHtml - , digestEmoji: digestEmoji - , plainText: plainText +const log = require('./npmlog-env') - , downloadStream: downloadStream +const Util = { + stripHtml + , unescapeHtml + , digestEmoji + , plainText + + , downloadStream + + , guid } function stripHtml(html) { @@ -44,8 +46,8 @@ function plainText(html) { } function downloadStream(url, cookies) { - // const myurl = 'http://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=3080011908135131569&skey=%40crypt_c117402d_53a58f8fbb21978167a3fc7d3be7f8c9' - url = url.replace(/^https/i, 'http') // use http for better performance + // const myurl = 'http://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=3080011908135131569&skey=%40crypt_c117402d_53a58f8fbb21978167a3fc7d3be7f8c9' + url = url.replace(/^https/i, 'http') // use http for better performance const options = require('url').parse(url) options.headers = { @@ -56,13 +58,13 @@ function downloadStream(url, cookies) { , 'Accept-Language': 'zh-CN,zh;q=0.8' } options.agent = http.globalAgent - - // 'pgv_pvi=6639183872; pgv_si=s8359147520; webwxuvid=747895d9dac5a25dd3a78175a5e931d879e026cacaf3ac06de0bd5f071470e7182fa36f7f1f0477ae5ee9266f741999a; mm_lang=zh_CN; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; wxloadtime=1465928826_expired; wxpluginkey=1465901102; wxuin=1211516682; wxsid=zMT7Gb24aTQzB1rA; webwx_data_ticket=gSeBbuhX+0kFdkXbgeQwr6Ck' - options.headers.Cookie = cookies.map(c => `${c.name}=${c.value}`).join('; ') - // log.verbose('WebUtil', 'Cookie: %s', options.headers.Cookie) + + // 'pgv_pvi=6639183872; pgv_si=s8359147520; webwxuvid=747895d9dac5a25dd3a78175a5e931d879e026cacaf3ac06de0bd5f071470e7182fa36f7f1f0477ae5ee9266f741999a; mm_lang=zh_CN; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; wxloadtime=1465928826_expired; wxpluginkey=1465901102; wxuin=1211516682; wxsid=zMT7Gb24aTQzB1rA; webwx_data_ticket=gSeBbuhX+0kFdkXbgeQwr6Ck' + options.headers.Cookie = cookies.map(c => `${c.name}=${c.value}`).join('; ') + // log.verbose('WebUtil', 'Cookie: %s', options.headers.Cookie) return new Promise((resolve, reject) => { - req = http.request(options, (res) => { + const req = http.request(options, (res) => { // console.log(`STATUS: ${res.statusCode}`); // console.log(`HEADERS: ${JSON.stringify(res.headers)}`); // res.setEncoding('utf8'); @@ -75,4 +77,13 @@ function downloadStream(url, cookies) { req.end() }) } -module.exports = WebUtil + +// credit - http://stackoverflow.com/a/2117523/1123955 +function guid() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8) + return v.toString(16) + }) +} + +module.exports = Util diff --git a/src/wechaty.js b/src/wechaty.js index 4bde63c8..b6488de3 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -12,6 +12,7 @@ const EventEmitter = require('events') const co = require('co') const log = require('./npmlog-env') +const Util = require('./util') class Wechaty extends EventEmitter { @@ -36,6 +37,8 @@ class Wechaty extends EventEmitter { this.npmVersion = require('../package.json').version + this.uuid = Util.guid() + this.inited = false } @@ -226,6 +229,7 @@ class Wechaty extends EventEmitter { getPort(okPort => resolve(okPort)) }) } + } const Message = require('./message') diff --git a/test/web-util.spec.js b/test/util.spec.js similarity index 84% rename from test/web-util.spec.js rename to test/util.spec.js index 4701cbff..7527f1c8 100644 --- a/test/web-util.spec.js +++ b/test/util.spec.js @@ -3,7 +3,7 @@ const test = require('tape') const log = require('../src/npmlog-env') -const webUtil = require('../src/web-util') +const Util = require('../src/util') test('Html smoking test', t => { const HTML_BEFORE_STRIP = 'OuterInner' @@ -24,17 +24,17 @@ test('Html smoking test', t => { const PLAIN_BEFORE = '&&&' const PLAIN_AFTER = '&&&[流汗]' - const strippedHtml = webUtil.stripHtml(HTML_BEFORE_STRIP) + const strippedHtml = Util.stripHtml(HTML_BEFORE_STRIP) t.equal(strippedHtml, HTML_AFTER_STRIP, 'should strip html as expected') - const unescapedHtml = webUtil.unescapeHtml(HTML_BEFORE_UNESCAPE) + const unescapedHtml = Util.unescapeHtml(HTML_BEFORE_UNESCAPE) t.equal(unescapedHtml, HTML_AFTER_UNESCAPE, 'should unescape html as expected') for (let i=0; i { }) server.listen(8000) - webUtil.downloadStream('http://127.0.0.1:8000/ding', [{name: 'life', value: 42}]) + Util.downloadStream('http://127.0.0.1:8000/ding', [{name: 'life', value: 42}]) .then(s => { s.on('data', (chunk) => { // console.log(`BODY: ${chunk}`) -- GitLab