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

fix implicit any warnings (#1180)

上级 09c86d49
......@@ -2,9 +2,9 @@
# https://help.github.com/articles/about-codeowners/
#
/docs/ @lijiarui @hczhcz @TingYinHelen @ax4
/examples/ @Gcaufy @hczhcz
/src/puppet-puppeteer/bridge.*.ts @mukaiu @xjchengo
/src/puppet-puppeteer/event.ts @binsee
/src/puppet-puppeteer/firer.*.ts @xinbenlv
/src/puppet-puppeteer/wechaty-bro.js @binsee @mukaiu @hczhcz @cherry-geqi @zhenyong
/docs/ @lijiarui @hczhcz @TingYinHelen @ax4
/examples/ @Gcaufy @hczhcz
/src/puppet-puppeteer/bridge.*.ts @mukaiu @xjchengo
/src/puppet-puppeteer/event.ts @binsee
/src/puppet-puppeteer/firer.*.ts @xinbenlv
/src/puppet-puppeteer/wechaty-bro.js @binsee @mukaiu @hczhcz @cherry-geqi @zhenyong
......@@ -64,7 +64,7 @@ client.init()
client.initWeb()
.catch(onError.bind(client))
function onError(e) {
function onError(e: Error) {
log.error('Client', 'initWeb() fail: %s', e)
this.quit()
process.exit(-1)
......
......@@ -43,6 +43,7 @@ import { EventEmitter } from 'events'
import {
config,
Wechaty,
Message,
} from '../src/'
// log.level = 'verbose'
......@@ -103,11 +104,16 @@ bot.start()
})
class Talker extends EventEmitter {
private thinker
private obj
private timer
private thinker: (text: string) => Promise<string>
private obj: {
text: string[],
time: number[],
}
private timer?: number
constructor(thinker) {
constructor(
thinker: (text: string) => Promise<string>,
) {
log.verbose('Talker()')
super()
this.thinker = thinker
......@@ -115,10 +121,9 @@ class Talker extends EventEmitter {
text: [],
time: [],
}
this.timer = null
}
public save(text) {
public save(text: string) {
log.verbose('Talker', 'save(%s)', text)
this.obj.text.push(text)
this.obj.time.push(Date.now())
......@@ -131,7 +136,7 @@ class Talker extends EventEmitter {
return text
}
public updateTimer(delayTime?) {
public updateTimer(delayTime?: number) {
delayTime = delayTime || this.delayTime()
log.verbose('Talker', 'updateTimer(%s)', delayTime)
......@@ -139,7 +144,7 @@ class Talker extends EventEmitter {
this.timer = setTimeout(this.say.bind(this), delayTime)
}
public hear(text) {
public hear(text: string) {
log.verbose('Talker', `hear(${text})`)
this.save(text)
this.updateTimer()
......@@ -149,7 +154,7 @@ class Talker extends EventEmitter {
const text = this.load()
this.thinker(text)
.then(reply => this.emit('say', reply))
this.timer = null
this.timer = undefined
}
public delayTime() {
......@@ -161,19 +166,22 @@ class Talker extends EventEmitter {
}
/* tslint:disable:variable-name */
const Talkers: Talker[] = []
const Talkers: {
[index: string]: Talker,
} = {}
function talk(m) {
function talk(m: Message) {
const fromId = m.from().id
const roomId = m.room().id
const room = m.room()
const roomId = room && room.id
const content = m.text()
const talkerName = fromId + roomId
if (!Talkers[talkerName]) {
Talkers[talkerName] = new Talker(function(text) {
Talkers[talkerName] = new Talker(function(text: string) {
return new Promise((resolve, reject) => {
brainApiAi.textRequest(text)
.on('response', function(response) {
.on('response', function(response: any) {
console.log(response)
/*
{ id: 'a09381bb-8195-4139-b49c-a2d03ad5e014',
......@@ -188,7 +196,7 @@ function talk(m) {
score: 0 },
status: { code: 200, errorType: 'success' } }
*/
const reply = response.result.fulfillment.speech
const reply: string = response.result.fulfillment.speech
if (!reply) {
log.info('ApiAi', `Talker do not want to talk for "${text}"`)
return reject()
......@@ -196,7 +204,7 @@ function talk(m) {
log.info('ApiAi', 'Talker reply:"%s" for "%s" ', reply, text)
return resolve(reply)
})
.on('error', function(error) {
.on('error', function(error: Error) {
log.error('ApiAi', error)
reject(error)
})
......
......@@ -249,7 +249,7 @@ setInterval(function() {
screen.render()
}, 500)
function setLineData(mockData, line) {
function setLineData(mockData: any, line: any) {
for (let i = 0; i < mockData.length; i++) {
const last = mockData[i].y[mockData[i].y.length - 1]
mockData[i].y.shift()
......@@ -321,7 +321,7 @@ function startBot(bot: Wechaty, logElement: any) {
{
small: true,
},
qrData => logElement.setContent(qrData),
(qrData: string) => logElement.setContent(qrData),
)
}
// logElement.log(`${url}\n[${code}] Scan QR Code above url to log in: `)
......
......@@ -87,7 +87,7 @@ async function saveMediaFile(message: Message) {
const netStream = await message.readyStream()
netStream
.pipe(fileStream)
.on('close', _ => {
.on('close', () => {
const stat = statSync(filename)
console.log('finish readyStream() for ', filename, ' size: ', stat.size)
})
......
......@@ -264,7 +264,7 @@ async function manageDingRoom() {
/**
* Event: Join
*/
room.on('join', function(this, inviteeList, inviter) {
room.on('join', function(inviteeList, inviter) {
log.verbose('Bot', 'Room EVENT: join - %s, %s',
inviteeList.map(c => c.name()).join(', '),
inviter.name(),
......@@ -337,7 +337,7 @@ async function checkRoomJoin(room: Room, inviteeList: Contact[], inviter: Contac
}
async function putInRoom(contact, room) {
async function putInRoom(contact: Contact, room: Room) {
log.info('Bot', 'putInRoom(%s, %s)', contact.name(), room.topic())
try {
......@@ -369,7 +369,7 @@ function getHelperContact() {
return Contact.find({ name: HELPER_CONTACT_NAME })
}
async function createDingRoom(contact): Promise<any> {
async function createDingRoom(contact: Contact): Promise<any> {
log.info('Bot', 'createDingRoom(%s)', contact)
try {
......
......@@ -122,7 +122,7 @@ function mp3ToWav(mp3Stream: Readable): NodeJS.ReadableStream {
// .on('end', function() {
// console.log('Finished processing');
// })
.on('error', function(err, stdout, stderr) {
.on('error', function(err: Error /*, stdout, stderr */) {
console.log('Cannot process video: ' + err.message)
})
......
......@@ -118,6 +118,7 @@
"xml2js": "^0.4.0"
},
"devDependencies": {
"@types/blessed": "^0.1.10",
"@types/blue-tape": "^0.1.0",
"@types/cuid": "^1.3.0",
"@types/express": "^4.0.0",
......@@ -163,7 +164,7 @@
"tslint-eslint-rules": "^5.1.0",
"tslint-jsdoc-rules": "^0.1.0",
"tuling123-client": "^0.0.1",
"typescript": "^2.8.3"
"typescript": "^2.9.0-dev.20180430"
},
"files_comment__whitelist_npm_publish": "http://stackoverflow.com/a/8617868/1123955",
"files": [
......
......@@ -2,7 +2,9 @@
import * as readline from 'readline'
const contributeMap = {}
const contributeMap: {
[contributor: string]: string[],
} = {}
function parseLine(line: string): string[] | null {
// [\#264](https://github.com/Chatie/wechaty/pull/264) ([lijiarui](https://github.com/lijiarui))
......
......@@ -45,7 +45,7 @@ class LicenseTransformer extends Transform {
super(options)
}
public _transform(chunk, encoding, done) {
public _transform(chunk: any, encoding: string, done: Function) {
if (this.updated) {
this.push(chunk)
} else {
......@@ -56,7 +56,7 @@ class LicenseTransformer extends Transform {
done()
}
private updateChunk(chunk): string {
private updateChunk(chunk: any): string {
const buffer = this.lineBuf + chunk.toString()
this.lineBuf = ''
......@@ -106,7 +106,7 @@ class LicenseTransformer extends Transform {
return updatedLineList.join('\n')
}
public _flush(done) {
public _flush(done: Function) {
if (this.lineBuf) {
this.push(this.lineBuf)
this.lineBuf = ''
......@@ -133,7 +133,7 @@ async function updateLicense(file: string): Promise<void> {
await promisify(unlinkCallback)(tmpFile)
}
async function glob(pattern): Promise<string[]> {
async function glob(pattern: string): Promise<string[]> {
return promisify<string, string[]>(globCallback as any)(pattern)
}
......
......@@ -16,7 +16,7 @@
* limitations under the License.
*
*/
import * as express from 'express'
/**
* DO NOT use `require('../')` here!
* because it will casue a LOOP require ERROR
......@@ -24,13 +24,14 @@
// import Brolog from 'brolog'
import StateSwitch from 'state-switch'
import { Message } from './puppet/'
import {
config,
log,
} from './config'
import { Io } from './io'
import { Wechaty } from './wechaty'
export interface IoClientOptions {
token : string,
wechaty? : Wechaty,
......@@ -120,7 +121,7 @@ export class IoClient {
public initWeb(port = config.httpPort) {
// if (process.env.DYNO) {
// }
const app = require('express')()
const app = express()
app.get('/', function (req, res) {
res.send('Wechaty IO Bot Alive!')
......@@ -136,7 +137,7 @@ export class IoClient {
})
}
private onMessage(m) {
private onMessage(m: Message) {
// const from = m.from()
// const to = m.to()
// const content = m.toString()
......
......@@ -20,6 +20,7 @@ import * as WebSocket from 'ws'
import StateSwitch from 'state-switch'
import {
Message,
ScanData,
} from './puppet/'
......@@ -27,7 +28,9 @@ import {
config,
log,
} from './config'
import Wechaty from './wechaty'
import {
Wechaty,
} from './wechaty'
export interface IoOptions {
wechaty: Wechaty,
......@@ -441,7 +444,7 @@ export class Io {
* Prepare to be overwriten by server setting
*
*/
private async ioMessage(m): Promise<void> {
private async ioMessage(m: Message): Promise<void> {
log.silly('Io', 'ioMessage() is a nop function before be overwriten from cloud')
if (typeof this.onMessage === 'function') {
await this.onMessage(m)
......
......@@ -22,6 +22,7 @@ import * as test from 'blue-tape'
// import * as sinon from 'sinon'
// const sinonTest = require('sinon-test')(sinon)
import * as http from 'http'
import * as express from 'express'
import Misc from './misc'
......@@ -121,7 +122,7 @@ test('downloadStream() for media', async t => {
res.end('dong')
})
const server = require('http').createServer(app)
const server = http.createServer(app)
server.on('clientError', (err, socket) => {
t.fail('server on clientError')
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n')
......
......@@ -19,6 +19,7 @@
import * as crypto from 'crypto'
import * as https from 'https'
import * as http from 'http'
import * as net from 'net'
import {
Readable,
} from 'stream'
......@@ -111,22 +112,22 @@ export class Misc {
// const myurl = 'http://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=3080011908135131569&skey=%40crypt_c117402d_53a58f8fbb21978167a3fc7d3be7f8c9'
href = href.replace(/^https/i, 'http') // use http instead of https, because https will only success on the very first request!
const u = url.parse(href)
const protocol = u.protocol as 'https:'|'http:'
const parsedUrl = url.parse(href)
const protocol = parsedUrl.protocol as 'https:'|'http:'
let options
let options: http.RequestOptions | https.RequestOptions
// let request
let get
let get: typeof https.get
if (protocol === 'https:') {
// request = https.request.bind(https)
get = https.get
options = u as any as https.RequestOptions
options = parsedUrl as any as https.RequestOptions
options.agent = https.globalAgent
} else if (protocol === 'http:') {
// request = http.request.bind(http)
get = http.get
options = u as any as http.RequestOptions
options = parsedUrl as any as http.RequestOptions
options.agent = http.globalAgent
} else {
throw new Error('protocol unknown: ' + protocol)
......@@ -200,14 +201,14 @@ export class Misc {
return new Promise((resolve, reject) => {
// https://gist.github.com/mikeal/1840641
function _getPort(cb) {
const server = require('net').createServer()
function _getPort(cb: (port: number) => void) {
const server = net.createServer()
server.on('error', function(err) {
if (err) {/* fail safe */ }
tryPort = nextPort(port)
_getPort(cb)
})
server.listen(tryPort, function(err) {
server.listen(tryPort, function(err: any) {
if (err) {/* fail safe */}
server.once('close', function() {
cb(tryPort)
......
......@@ -6,9 +6,15 @@ import { log } from './config'
import { Puppet } from './puppet/'
// use Symbol to prevent conflicting with the child class properties
export const PUPPET_ACCESSORY_NAME = Symbol('name')
export abstract class PuppetAccessory extends EventEmitter {
// use Symbol to prevent conflicting with the child class properties
private static readonly PUPPET_ACCESSORY_NAME = Symbol('name')
// Not work???
// private static readonly PUPPET_ACCESSORY_NAME = Symbol('name')
private static [PUPPET_ACCESSORY_NAME]: string
private [PUPPET_ACCESSORY_NAME]: string
/**
* 1. Static puppet property
......@@ -17,7 +23,7 @@ export abstract class PuppetAccessory extends EventEmitter {
public static set puppet(puppet: Puppet) {
log.silly('PuppetAccessory', '<%s> static set puppet(%s)',
this[this.PUPPET_ACCESSORY_NAME] || this.name,
this[PUPPET_ACCESSORY_NAME] || this.name,
puppet.constructor.name,
)
this._puppet = puppet
......@@ -25,7 +31,7 @@ export abstract class PuppetAccessory extends EventEmitter {
public static get puppet(): Puppet {
log.silly('PuppetAccessory', '<%s> static get puppet()',
this[this.PUPPET_ACCESSORY_NAME] || this.name,
this[PUPPET_ACCESSORY_NAME] || this.name,
)
if (this._puppet) {
......@@ -42,7 +48,7 @@ export abstract class PuppetAccessory extends EventEmitter {
name?: string,
) {
super()
this[PuppetAccessory.PUPPET_ACCESSORY_NAME] = name || this.constructor.name
this[PUPPET_ACCESSORY_NAME] = name || this.constructor.name
}
/**
......@@ -51,12 +57,12 @@ export abstract class PuppetAccessory extends EventEmitter {
private _puppet?: Puppet
public set puppet(puppet: Puppet) {
log.silly('PuppetAccessory', '<%s> set puppet(%s)', this[PuppetAccessory.PUPPET_ACCESSORY_NAME], puppet.constructor.name)
log.silly('PuppetAccessory', '<%s> set puppet(%s)', this[PUPPET_ACCESSORY_NAME], puppet.constructor.name)
this._puppet = puppet
}
public get puppet(): Puppet {
log.silly('PuppetAccessory', '<%s> get puppet()', this[PuppetAccessory.PUPPET_ACCESSORY_NAME])
log.silly('PuppetAccessory', '<%s> get puppet()', this[PUPPET_ACCESSORY_NAME])
if (this._puppet) {
return this._puppet
......
......@@ -121,11 +121,11 @@ export class MockMessage extends Message {
return this
}
public static async find(query) {
public static async find(query: any) {
return Promise.resolve(new MockMessage({MsgId: '-1'}))
}
public static async findAll(query) {
public static async findAll(query: any) {
return Promise.resolve([
new MockMessage({MsgId: '-2'}),
new MockMessage({MsgId: '-3'}),
......
......@@ -184,7 +184,7 @@ test('retryPromise()', async t => {
const EXPECTED_RESOLVE = 'Okey'
const EXPECTED_REJECT = 'NotTheTime'
function delayedFactory(timeout) {
function delayedFactory(timeout: number) {
const startTime = Date.now()
return function() {
const nowTime = Date.now()
......@@ -202,7 +202,7 @@ test('retryPromise()', async t => {
const delay500 = delayedFactory(500)
await retryPromise({ max: 1, backoff: 1 }, function() {
return delay500()
}).catch(e => {
}).catch((e: any) => {
thenSpy(e)
})
t.true(thenSpy.withArgs(EXPECTED_REJECT).calledOnce, 'should got EXPECTED_REJECT when wait not enough')
......@@ -212,13 +212,12 @@ test('retryPromise()', async t => {
await retryPromise({ max: 6, backoff: 10 }, function() {
return anotherDelay50()
})
.then(r => {
.then((r: string) => {
thenSpy(r)
})
t.true(thenSpy.withArgs(EXPECTED_RESOLVE).calledOnce, 'should got EXPECTED_RESOLVE when wait enough')
})
declare const WechatyBro
test('WechatyBro.ding()', async t => {
const profile = new Profile(Math.random().toString(36).substr(2, 5))
const bridge = new Bridge({
......
......@@ -24,7 +24,7 @@ import {
Browser,
Cookie,
Dialog,
ElementHandle,
// ElementHandle,
launch,
Page,
} from 'puppeteer'
......@@ -54,8 +54,6 @@ export interface BridgeOptions {
profile : Profile,
}
declare const WechatyBro
export class Bridge extends EventEmitter {
private browser : Browser
private page : Page
......@@ -337,7 +335,10 @@ export class Bridge extends EventEmitter {
}
}
public async roomDelMember(roomId, contactId): Promise<number> {
public async roomDelMember(
roomId: string,
contactId: string,
): Promise<number> {
if (!roomId || !contactId) {
throw new Error('no roomId or contactId')
}
......@@ -349,7 +350,10 @@ export class Bridge extends EventEmitter {
}
}
public async roomAddMember(roomId, contactId): Promise<number> {
public async roomAddMember(
roomId: string,
contactId: string,
): Promise<number> {
log.verbose('PuppetPuppeteerBridge', 'roomAddMember(%s, %s)', roomId, contactId)
if (!roomId || !contactId) {
......@@ -363,7 +367,10 @@ export class Bridge extends EventEmitter {
}
}
public async roomModTopic(roomId, topic): Promise<string> {
public async roomModTopic(
roomId: string,
topic: string,
): Promise<string> {
if (!roomId) {
throw new Error('no roomId')
}
......@@ -394,7 +401,10 @@ export class Bridge extends EventEmitter {
}
}
public async verifyUserRequest(contactId, hello): Promise<boolean> {
public async verifyUserRequest(
contactId: string,
hello: string,
): Promise<boolean> {
log.verbose('PuppetPuppeteerBridge', 'verifyUserRequest(%s, %s)', contactId, hello)
if (!contactId) {
......@@ -408,7 +418,10 @@ export class Bridge extends EventEmitter {
}
}
public async verifyUserOk(contactId, ticket): Promise<boolean> {
public async verifyUserOk(
contactId: string,
ticket: string,
): Promise<boolean> {
log.verbose('PuppetPuppeteerBridge', 'verifyUserOk(%s, %s)', contactId, ticket)
if (!contactId || !ticket) {
......@@ -422,7 +435,10 @@ export class Bridge extends EventEmitter {
}
}
public async send(toUserName: string, text: string): Promise<void> {
public async send(
toUserName: string,
text: string,
): Promise<void> {
log.verbose('PuppetPuppeteerBridge', 'send(%s, %s)', toUserName, text)
if (!toUserName) {
......@@ -443,7 +459,7 @@ export class Bridge extends EventEmitter {
}
}
public async getMsgImg(id): Promise<string> {
public async getMsgImg(id: string): Promise<string> {
log.verbose('PuppetPuppeteerBridge', 'getMsgImg(%s)', id)
try {
......@@ -454,7 +470,7 @@ export class Bridge extends EventEmitter {
}
}
public async getMsgEmoticon(id): Promise<string> {
public async getMsgEmoticon(id: string): Promise<string> {
log.verbose('PuppetPuppeteerBridge', 'getMsgEmoticon(%s)', id)
try {
......@@ -465,7 +481,7 @@ export class Bridge extends EventEmitter {
}
}
public async getMsgVideo(id): Promise<string> {
public async getMsgVideo(id: string): Promise<string> {
log.verbose('PuppetPuppeteerBridge', 'getMsgVideo(%s)', id)
try {
......@@ -476,7 +492,7 @@ export class Bridge extends EventEmitter {
}
}
public async getMsgVoice(id): Promise<string> {
public async getMsgVoice(id: string): Promise<string> {
log.verbose('PuppetPuppeteerBridge', 'getMsgVoice(%s)', id)
try {
......@@ -487,7 +503,7 @@ export class Bridge extends EventEmitter {
}
}
public async getMsgPublicLinkImg(id): Promise<string> {
public async getMsgPublicLinkImg(id: string): Promise<string> {
log.verbose('PuppetPuppeteerBridge', 'getMsgPublicLinkImg(%s)', id)
try {
......@@ -514,8 +530,8 @@ export class Bridge extends EventEmitter {
const timeout = max * (backoff * max) / 2
try {
return await retryPromise({ max: max, backoff: backoff }, async attempt => {
log.silly('PuppetPuppeteerBridge', 'getContact() retryPromise: attampt %s/%s time for timeout %s',
return await retryPromise({ max: max, backoff: backoff }, async (attempt: number) => {
log.silly('PuppetPuppeteerBridge', 'getContact() retryPromise: attampt %d/%d time for timeout %d',
attempt, max, timeout)
try {
const r = await this.proxyWechaty('getContact', id)
......@@ -670,7 +686,7 @@ export class Bridge extends EventEmitter {
}
}
public async ding(data): Promise<any> {
public async ding(data: any): Promise<any> {
log.verbose('PuppetPuppeteerBridge', 'ding(%s)', data)
try {
......@@ -757,52 +773,53 @@ export class Bridge extends EventEmitter {
public async clickSwitchAccount(page: Page): Promise<boolean> {
log.verbose('PuppetPuppeteerBridge', 'clickSwitchAccount()')
// TODO: use page.$x() (with puppeteer v1.1 or above) to replace DIY version of listXpath() instead.
// See: https://github.com/GoogleChrome/puppeteer/blob/v1.1.0/docs/api.md#pagexexpression
// https://github.com/GoogleChrome/puppeteer/issues/537#issuecomment-334918553
async function listXpath(thePage: Page, xpath: string): Promise<ElementHandle[]> {
log.verbose('PuppetPuppeteerBridge', 'clickSwitchAccount() listXpath()')
// async function listXpath(thePage: Page, xpath: string): Promise<ElementHandle[]> {
// log.verbose('PuppetPuppeteerBridge', 'clickSwitchAccount() listXpath()')
// try {
// const nodeHandleList = await (thePage as any).evaluateHandle(xpathInner => {
// const nodeList: Node[] = []
// const query = document.evaluate(xpathInner, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
// for (let i = 0, length = query.snapshotLength; i < length; ++i) {
// nodeList.push(query.snapshotItem(i))
// }
// return nodeList
// }, xpath)
// const properties = await nodeHandleList.getProperties()
// const elementHandleList: ElementHandle[] = []
// const releasePromises: Promise<void>[] = []
// for (const property of properties.values()) {
// const element = property.asElement()
// if (element)
// elementHandleList.push(element)
// else
// releasePromises.push(property.dispose())
// }
// await Promise.all(releasePromises)
// return elementHandleList
// } catch (e) {
// log.verbose('PuppetPuppeteerBridge', 'clickSwitchAccount() listXpath() exception: %s', e)
// return []
// }
// }
try {
const nodeHandleList = await (thePage as any).evaluateHandle(xpathInner => {
const nodeList: Node[] = []
const query = document.evaluate(xpathInner, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
for (let i = 0, length = query.snapshotLength; i < length; ++i) {
nodeList.push(query.snapshotItem(i))
}
return nodeList
}, xpath)
const properties = await nodeHandleList.getProperties()
const elementHandleList: ElementHandle[] = []
const releasePromises: Promise<void>[] = []
for (const property of properties.values()) {
const element = property.asElement()
if (element)
elementHandleList.push(element)
else
releasePromises.push(property.dispose())
}
await Promise.all(releasePromises)
return elementHandleList
} catch (e) {
log.verbose('PuppetPuppeteerBridge', 'clickSwitchAccount() listXpath() exception: %s', e)
return []
}
}
// TODO: use page.$x() (with puppeteer v1.1 or above) to replace DIY version of listXpath() instead.
// See: https://github.com/GoogleChrome/puppeteer/blob/v1.1.0/docs/api.md#pagexexpression
const XPATH_SELECTOR = `//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']`
try {
const [button] = await listXpath(page, XPATH_SELECTOR)
// const [button] = await listXpath(page, XPATH_SELECTOR)
const [button] = await page.$x(XPATH_SELECTOR)
if (button) {
await button.click()
log.silly('PuppetPuppeteerBridge', 'clickSwitchAccount() clicked!')
return true
} else {
// log.silly('PuppetPuppeteerBridge', 'clickSwitchAccount() button not found')
log.silly('PuppetPuppeteerBridge', 'clickSwitchAccount() button not found')
return false
}
......@@ -874,7 +891,7 @@ export class Bridge extends EventEmitter {
}
let url
if (/^http/.test(url)) {
if (/^http/.test(domain)) {
url = domain
} else {
// Protocol error (Page.navigate): Cannot navigate to invalid URL undefined
......
......@@ -51,7 +51,10 @@ export const Event = {
}
function onDing(this: PuppetPuppeteer, data): void {
function onDing(
this: PuppetPuppeteer,
data: any,
): void {
log.silly('PuppetPuppeteerEvent', 'onDing(%s)', data)
this.emit('watchdog', { data })
}
......@@ -163,7 +166,10 @@ async function onLogin(this: PuppetPuppeteer, note: string, ttl = 30): Promise<v
return
}
async function onLogout(this: PuppetPuppeteer, data): Promise<void> {
async function onLogout(
this: PuppetPuppeteer,
data: any,
): Promise<void> {
log.verbose('PuppetPuppeteerEvent', 'onLogout(%s)', data)
if (this.logonoff()) {
......
......@@ -17,7 +17,7 @@
*
*/
import * as request from 'request'
import * as bl from 'bl'
import * as bl from 'bl'
import cloneClass from 'clone-class'
import {
......@@ -40,8 +40,6 @@ import {
log,
Raven,
} from '../config'
import {
} from '../message'
import Profile from '../profile'
import Misc from '../misc'
......@@ -355,7 +353,7 @@ export class PuppetPuppeteer extends Puppet {
const readStream = await message.readyStream()
const buffer = <Buffer>await new Promise((resolve, reject) => {
readStream.pipe(bl((err, data) => {
readStream.pipe(bl((err: Error, data: Buffer) => {
if (err) reject(err)
else resolve(data)
}))
......@@ -831,7 +829,7 @@ export class PuppetPuppeteer extends Puppet {
): string {
log.verbose('PuppetPuppeteer', 'contactQueryFilterToFunctionString({ %s })',
Object.keys(query)
.map(k => `${k}: ${query[k]}`)
.map((k: keyof ContactQueryFilter) => `${k}: ${query[k]}`)
.join(', '),
)
......@@ -839,21 +837,21 @@ export class PuppetPuppeteer extends Puppet {
throw new Error('query only support one key. multi key support is not availble now.')
}
let filterKey = Object.keys(query)[0]
let filterValue: string | RegExp = query[filterKey]
const filterKey = Object.keys(query)[0] as keyof ContactQueryFilter
const keyMap = {
name: 'NickName',
alias: 'RemarkName',
let filterValue: string | RegExp | undefined = query[filterKey]
if (!filterValue) {
throw new Error('filterValue not found')
}
filterKey = keyMap[filterKey]
if (!filterKey) {
throw new Error('unsupport filter key')
const protocolKeyMap = {
name: 'NickName',
alias: 'RemarkName',
}
if (!filterValue) {
throw new Error('filterValue not found')
const protocolFilterKey = protocolKeyMap[filterKey]
if (!protocolFilterKey) {
throw new Error('unsupport protocol filter key')
}
/**
......@@ -863,10 +861,10 @@ export class PuppetPuppeteer extends Puppet {
let filterFunction: string
if (filterValue instanceof RegExp) {
filterFunction = `(function (c) { return ${filterValue.toString()}.test(c.${filterKey}) })`
filterFunction = `(function (c) { return ${filterValue.toString()}.test(c.${protocolFilterKey}) })`
} else if (typeof filterValue === 'string') {
filterValue = filterValue.replace(/'/g, '\\\'')
filterFunction = `(function (c) { return c.${filterKey} === '${filterValue}' })`
filterFunction = `(function (c) { return c.${protocolFilterKey} === '${filterValue}' })`
} else {
throw new Error('unsupport name type')
}
......
......@@ -20,7 +20,7 @@
*/
// tslint:disable:no-shadowed-variable
import * as test from 'blue-tape'
// import * as sinon from 'sinon'
import * as sinon from 'sinon'
import cloneClass from 'clone-class'
......@@ -45,7 +45,7 @@ test('Contact smoke testing', async t => {
const NickName = 'NickNameTest'
const RemarkName = 'AliasTest'
MyContact.puppet['getContact'] = function(id) {
sinon.stub((MyContact.puppet as PuppetPuppeteer), 'getContact', function(id: string) {
return new Promise<any>((resolve, reject) => {
if (id !== UserName) return resolve({})
setTimeout(() => {
......@@ -56,15 +56,14 @@ test('Contact smoke testing', async t => {
})
}, 200)
})
}
})
const c = new MyContact(UserName)
t.is(c.id, UserName, 'id/UserName right')
const r = await c.ready()
t.is(r.get('id') , UserName, 'UserName set')
t.is(r.get('name') , NickName, 'NickName set')
t.is(r.name(), NickName, 'should get the right name from Contact')
t.is(r.id , UserName, 'UserName set')
t.is(r.name(), NickName, 'NickName set')
t.is(r.alias(), RemarkName, 'should get the right alias from Contact')
const s = r.toString()
......
......@@ -200,7 +200,7 @@ export class PuppeteerContact extends Contact implements Sayable {
public async say(textOrMessage: string | PuppeteerMessage): Promise<void> {
log.verbose('PuppeteerContact', 'say(%s)', textOrMessage)
const user = this.puppet.userSelf()
const user = this.puppet.userSelf() as PuppeteerContact
if (!user) {
throw new Error('no user')
......@@ -430,10 +430,10 @@ export class PuppeteerContact extends Contact implements Sayable {
}
}
/**
* @private
*/
public get(prop) { return this.obj && this.obj[prop] }
// /**
// * @private
// */
// public get(prop) { return this.obj && this.obj[prop] }
/**
* @private
......@@ -494,7 +494,7 @@ export class PuppeteerContact extends Contact implements Sayable {
*/
public dumpRaw() {
console.error('======= dump raw contact =======')
Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
Object.keys(this.rawObj).forEach((k: keyof PuppeteerContactRawObj) => console.error(`${k}: ${this.rawObj[k]}`))
}
/**
......@@ -505,7 +505,7 @@ export class PuppeteerContact extends Contact implements Sayable {
if (!this.obj) {
throw new Error('no this.obj')
}
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj && this.obj[k]}`))
Object.keys(this.obj).forEach((k: keyof PuppeteerContactObj) => console.error(`${k}: ${this.obj && this.obj[k]}`))
}
/**
......
......@@ -151,7 +151,7 @@ export class PuppeteerFriendRequest extends FriendRequest {
}
throw new Error('FriendRequest.accept() content.ready() not ready')
}).catch( e => {
}).catch((e: Error) => {
log.warn('PuppeteerFriendRequest', 'accept() rejected for contact %s because %s', this.contact, e && e.message || e)
})
......
......@@ -85,7 +85,7 @@ test('ready()', async t => {
const expectedMsgId = '3009511950433684462'
// Mock
function mockGetContact(id) {
function mockGetContact(id: string) {
log.silly('TestMessage', `mocked getContact(${id})`)
return new Promise((resolve, reject) => {
let obj = {}
......@@ -192,7 +192,7 @@ test('mentioned()', async t => {
const ROOM_ID = '@@9cdc696e490bd76c57e7dd54792dc1408e27d65e312178b1943e88579b7939f4'
// Mock
const mockContactGetter = function (id) {
const mockContactGetter = function (id: string) {
return new Promise((resolve, reject) => {
if (id !== ROOM_ID && !(id in CONTACT_LIST)) return resolve({})
if (id === ROOM_ID) {
......
......@@ -102,7 +102,7 @@ export class PuppeteerMessage extends Message {
* @private
*/
// Transform rawObj to local obj
private parse(rawObj): MsgObj {
private parse(rawObj: MsgRawObj): MsgObj {
const obj: MsgObj = {
id: rawObj.MsgId,
type: rawObj.MsgType,
......@@ -497,7 +497,7 @@ export class PuppeteerMessage extends Message {
}
// flatten array, see http://stackoverflow.com/a/10865042/1123955
const mentionList = [].concat.apply([], rawMentionedList)
const mentionList: string[] = [].concat.apply([], rawMentionedList)
log.verbose('PuppeteerMessage', 'mentioned(%s),get mentionList: %s', this.text(), JSON.stringify(mentionList))
contactList = [].concat.apply([],
......@@ -675,7 +675,7 @@ export class PuppeteerMessage extends Message {
*/
public dump() {
console.error('======= dump message =======')
Object.keys(this.obj!).forEach(k => console.error(`${k}: ${this.obj![k]}`))
Object.keys(this.obj!).forEach((k: keyof MsgObj) => console.error(`${k}: ${this.obj![k]}`))
}
/**
......@@ -686,20 +686,20 @@ export class PuppeteerMessage extends Message {
if (!this.rawObj) {
throw new Error('no this.rawObj')
}
Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj && this.rawObj[k]}`))
Object.keys(this.rawObj).forEach((k: keyof MsgRawObj) => console.error(`${k}: ${this.rawObj && this.rawObj[k]}`))
}
/**
* @todo add function
*/
public static async find(query) {
public static async find(query: any) {
return Promise.resolve(new PuppeteerMessage(<MsgRawObj>{MsgId: '-1'}))
}
/**
* @todo add function
*/
public static async findAll(query) {
public static async findAll(query: any) {
return Promise.resolve([
new PuppeteerMessage (<MsgRawObj>{MsgId: '-2'}),
new PuppeteerMessage (<MsgRawObj>{MsgId: '-3'}),
......@@ -901,7 +901,7 @@ export class PuppeteerMessage extends Message {
throw new Error('saveFile() file does exist!')
}
const writeStream = fs.createWriteStream(filePath)
let readStream
let readStream: Readable
try {
readStream = await this.readyStream()
} catch (e) {
......
......@@ -106,7 +106,7 @@ test('Room smoking test', async t => {
}
// Mock
const mockContactGetter = function (id) {
const mockContactGetter = function (id: string) {
return new Promise((resolve, reject) => {
if (id !== EXPECTED.id && !(id in CONTACT_LIST)) return resolve({})
if (id === EXPECTED.id) {
......
......@@ -150,7 +150,7 @@ export interface MsgObj {
content: string,
status: string,
digest: string,
date: string,
date: number,
url?: string, // for MessageMedia class
}
......
......@@ -62,12 +62,16 @@ export abstract class Contact extends PuppetAccessory implements Sayable {
throw new Error('Contact.load(): id not found')
}
if (!(id in this.pool)) {
// when we call `load()`, `this` should already be extend-ed a child class.
// so we force `this as any` at here to make the call.
Contact.pool[id] = new (this as any)(id)
const existingContact = this.pool.get(id)
if (existingContact) {
return existingContact
}
return this.pool[id]
// when we call `load()`, `this` should already be extend-ed a child class.
// so we force `this as any` at here to make the call.
const newContact = new (this as any)(id)
Contact.pool.set(id, newContact)
return newContact
}
/**
......@@ -132,7 +136,7 @@ export abstract class Contact extends PuppetAccessory implements Sayable {
// log.verbose('Cotnact', 'findAll({ name: %s })', query.name)
log.verbose('Cotnact', 'findAll({ %s })',
Object.keys(query)
.map(k => `${k}: ${query[k]}`)
.map((k: keyof ContactQueryFilter) => `${k}: ${query[k]}`)
.join(', '),
)
......
......@@ -20,10 +20,6 @@
import PuppetAccessory from '../puppet-accessory'
import {
// config,
// log,
} from './config'
import Contact from './contact'
/**
......
......@@ -49,9 +49,9 @@ export abstract class Message extends PuppetAccessory implements Sayable {
*/
public static async find<T extends typeof Message>(
this: T,
query,
query: any,
): Promise<T['prototype'] | null> {
return this.findAll(query)[0]
return (await this.findAll(query))[0]
}
/**
......@@ -59,12 +59,12 @@ export abstract class Message extends PuppetAccessory implements Sayable {
*/
public static async findAll<T extends typeof Message>(
this: T,
query,
query: any,
): Promise<T['prototype'][]> {
return Promise.resolve([
return [
new (this as any)(1),
new (this as any)(2),
])
]
}
/**
......
......@@ -159,7 +159,6 @@ export abstract class Room extends PuppetAccessory implements Sayable {
const newRoom = new (this as any)(id)
this.pool.set(id, newRoom)
return newRoom
}
......
declare module 'bl'
declare module 'blessed-contrib'
declare module 'qrcode-terminal'
declare module '*/package.json' {
export const version: string
}
// Extend the `Window` from Browser
interface Window {
emit: Function, // from puppeteer
}
declare const WechatyBro: any
......@@ -205,7 +205,7 @@ test('other demos', async t => {
}
})
page.on('requestfailed', (...args) => {
page.on('requestfailed', (...args: any[]) => {
console.log('requestfailed:args:', args)
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册