提交 c6265ca7 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

#40 finally runable with ts-node!

上级 e5a92807
......@@ -121,7 +121,7 @@ Config.puppetInstance = function(instance?: Puppet): Puppet {
type WatchdogFood = {
data: any
, type?: 'HEARTBEAT' | 'POISON'
, type?: 'HEARTBEAT' | 'POISON' | 'SCAN'
, timeout?: number // millisecond
}
......
......@@ -191,12 +191,13 @@ class Bridge {
})
}
public roomModTopic(roomId, topic): Promise<void> {
public roomModTopic(roomId, topic): Promise<string> {
if (!roomId) {
throw new Error('no roomId')
}
return this.proxyWechaty('roomModTopic', roomId, topic)
.then(_ => topic)
.catch(e => {
log.error('PuppetWebBridge', 'roomModTopic(%s, %s) exception: %s', roomId, topic, e.message)
throw e
......
......@@ -204,7 +204,7 @@ class PuppetWeb extends Puppet {
}
}
private initBridge() {
private initBridge(): Promise<Bridge> {
log.verbose('PuppetWeb', 'initBridge()')
const bridge = new Bridge(
this // use puppet instead of browser, is because browser might change(die) duaring run time
......@@ -214,8 +214,9 @@ class PuppetWeb extends Puppet {
this.bridge = bridge
if (this.targetState() !== 'live') {
log.warn('PuppetWeb', 'initBridge() found targetState != live, no init anymore')
return Promise.resolve('skipped')
const errMsg = 'initBridge() found targetState != live, no init anymore'
log.warn('PuppetWeb', errMsg)
return Promise.reject(errMsg)
}
return bridge.init()
......@@ -231,7 +232,7 @@ class PuppetWeb extends Puppet {
})
}
private initServer() {
private initServer(): Promise<Server> {
log.verbose('PuppetWeb', 'initServer()')
const server = new Server(this.port)
......@@ -255,8 +256,9 @@ class PuppetWeb extends Puppet {
this.server = server
if (this.targetState() !== 'live') {
log.warn('PuppetWeb', 'initServer() found targetState != live, no init anymore')
return Promise.resolve('skipped')
const errMsg = 'initServer() found targetState != live, no init anymore'
log.warn('PuppetWeb', errMsg)
return Promise.reject(errMsg)
}
return server.init()
......
......@@ -43,7 +43,7 @@ function onFeed(food: WatchdogFood) {
}
const feed = `${food.type}:[${food.data}]`
log.silly('PuppetWebWatchdog', 'onFeed: %d, %s', timeout, feed)
log.silly('PuppetWebWatchdog', 'onFeed: %d, %s', food.timeout, feed)
if (this.currentState() === 'killing'
) {
......@@ -57,14 +57,14 @@ function onFeed(food: WatchdogFood) {
// return
// }
setWatchDogTimer.call(this, timeout, feed)
setWatchDogTimer.call(this, food.timeout, feed)
this.emit('heartbeat', feed)
monitorScan.call(this, type)
monitorScan.call(this, food.type)
autoSaveSession.call(this)
switch (type) {
switch (food.type) {
case 'POISON':
clearWatchDogTimer.call(this)
break
......@@ -74,7 +74,7 @@ function onFeed(food: WatchdogFood) {
break
default:
throw new Error('Watchdog onFeed: unsupport type ' + type)
throw new Error('Watchdog onFeed: unsupport type ' + food.type)
}
}
......
......@@ -212,7 +212,7 @@ class Room extends EventEmitter {
}
public del(contact: Contact): Promise<number> {
log.verbose('Room', 'del(%s)', contact)
log.verbose('Room', 'del(%s)', contact.name())
if (!contact) {
throw new Error('contact not found')
......@@ -223,12 +223,12 @@ class Room extends EventEmitter {
}
// @private
private delLocal(contact: Contact): boolean {
private delLocal(contact: Contact): number {
log.verbose('Room', 'delLocal(%s)', contact)
const memberList = this.obj.memberList
if (!memberList || memberList.length === 0) {
return true // already in refreshing
return 0 // already in refreshing
}
let i
......@@ -239,9 +239,9 @@ class Room extends EventEmitter {
}
if (i < memberList.length) {
memberList.splice(i, 1)
return true
return 1
}
return false
return 0
}
public quit() {
......
......@@ -2,12 +2,19 @@ import * as http from 'http'
import log from './brolog-env'
/**
* bug compatible with:
* https://github.com/wechaty/wechaty/issues/40#issuecomment-252802084
*/
import * as ws from 'ws'
typeof ws
class UtilLib {
public static stripHtml(html) {
public static stripHtml(html: string): string {
return String(html).replace(/(<([^>]+)>)/ig, '')
}
public static unescapeHtml(str) {
public static unescapeHtml(str: string): string {
return String(str)
.replace(/&apos;/g, "'")
.replace(/&quot;/g, '"')
......@@ -16,7 +23,7 @@ class UtilLib {
.replace(/&amp;/g, '&')
}
public static digestEmoji(html) {
public static digestEmoji(html: string): string {
return String(html)
.replace(/<img class="(\w*?emoji) (\w*?emoji[^"]+?)" text="(.*?)_web" src=[^>]+>/g
, '$3'
......@@ -26,7 +33,7 @@ class UtilLib {
) // '<span class="emoji emoji1f334"></span>'
}
public static plainText(html) {
public static plainText(html: string): string {
return UtilLib.stripHtml(
UtilLib.unescapeHtml(
UtilLib.stripHtml(
......@@ -38,7 +45,7 @@ class UtilLib {
)
}
public static downloadStream(url: string, cookies): Promise<any> {
public static downloadStream(url: string, cookies): Promise<NodeJS.ReadableStream> {
// 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)
......@@ -78,7 +85,7 @@ class UtilLib {
}
// credit - http://stackoverflow.com/a/2117523/1123955
public static guid() {
public static guid(): string {
/* tslint:disable:no-bitwise */
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8)
......@@ -127,7 +134,7 @@ class UtilLib {
})
})
function nextPort(newPort: number) {
function nextPort(newPort: number): number {
const RANDOM_RANGE = 1024
const n = Math.floor(Math.random() * RANDOM_RANGE)
return newPort + n
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册