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

enable tsconfig **strict** mode (#1180)

上级 82d50e9f
......@@ -95,7 +95,7 @@ bot
*/
case FriendRequest.Type.Receive:
if (request.hello() === 'ding') {
logMsg = 'accepted because verify messsage is "ding"'
logMsg = 'accepted automatically because verify messsage is "ding"'
request.accept()
} else {
......
......@@ -193,7 +193,7 @@ export class Contact extends PuppetAccessory implements Sayable {
// log.verbose('Cotnact', 'findAll({ name: %s })', query.name)
log.verbose('Cotnact', 'findAll({ %s })',
Object.keys(query)
.map((k: keyof ContactQueryFilter) => `${k}: ${query[k]}`)
.map(k => `${k}: ${query[k as keyof ContactQueryFilter]}`)
.join(', '),
)
......
......@@ -68,14 +68,14 @@ export class Io {
private readonly cuid : string
private readonly protocol : string
private eventBuffer : IoEvent[] = []
private ws : WebSocket
private ws : undefined | WebSocket
private readonly state = new StateSwitch('Io', log)
private reconnectTimer? : NodeJS.Timer
private reconnectTimeout? : number
private onMessage: Function
private onMessage: undefined | Function
private scanData: ScanData
......@@ -392,6 +392,12 @@ export class Io {
}
private async send(ioEvent?: IoEvent): Promise<void> {
if (!this.ws) {
throw new Error('no ws')
}
const ws = this.ws
if (ioEvent) {
log.silly('Io', 'send(%s: %s)', ioEvent.name, ioEvent.payload)
this.eventBuffer.push(ioEvent)
......@@ -404,7 +410,7 @@ export class Io {
const list: Promise<any>[] = []
while (this.eventBuffer.length) {
const p = new Promise((resolve, reject) => this.ws.send(
const p = new Promise((resolve, reject) => ws.send(
JSON.stringify(
this.eventBuffer.shift(),
),
......@@ -425,6 +431,10 @@ export class Io {
}
public async quit(): Promise<void> {
if (!this.ws) {
throw new Error('no ws')
}
this.state.off('pending')
// try to send IoEvents in buffer
......
......@@ -25,14 +25,15 @@ import {
} from './config'
export interface ProfileSchema {
cookies?: any[]
cookies? : any[]
[idx: string] : any
}
export type ProfileSection = keyof ProfileSchema
export class Profile {
private obj : ProfileSchema
private file? : string
private payload : ProfileSchema
private file? : string
constructor(
public name = config.profile,
......@@ -44,7 +45,7 @@ export class Profile {
} else {
this.file = path.isAbsolute(name)
? name
: path.join(
: path.resolve(
process.cwd(),
name,
)
......@@ -52,6 +53,8 @@ export class Profile {
this.file += '.wechaty.json'
}
}
this.payload = {}
}
public toString() {
......@@ -60,7 +63,7 @@ export class Profile {
public async load(): Promise<void> {
log.verbose('Profile', 'load() file: %s', this.file)
this.obj = {}
this.payload = {}
if (!this.file) {
log.verbose('Profile', 'load() no file, NOOP')
......@@ -74,7 +77,7 @@ export class Profile {
const text = fs.readFileSync(this.file).toString()
try {
this.obj = JSON.parse(text)
this.payload = JSON.parse(text)
} catch (e) {
log.error('Profile', 'load() exception: %s', e)
}
......@@ -86,13 +89,13 @@ export class Profile {
log.verbose('Profile', 'save() no file, NOOP')
return
}
if (!this.obj) {
if (!this.payload) {
log.verbose('Profile', 'save() no obj, NOOP')
return
}
try {
const text = JSON.stringify(this.obj)
const text = JSON.stringify(this.payload)
fs.writeFileSync(this.file, text)
} catch (e) {
log.error('Profile', 'save() exception: %s', e)
......@@ -102,23 +105,23 @@ export class Profile {
public async get<T = any>(section: ProfileSection): Promise<null | T> {
log.verbose('Profile', 'get(%s)', section)
if (!this.obj) {
if (!this.payload) {
return null
}
return this.obj[section] as any as T
return this.payload[section] as any as T
}
public async set(section: ProfileSection, data: any): Promise<void> {
log.verbose('Profile', 'set(%s, %s)', section, data)
if (!this.obj) {
this.obj = {}
if (!this.payload) {
this.payload = {}
}
this.obj[section] = data
this.payload[section] = data
}
public async destroy(): Promise<void> {
log.verbose('Profile', 'destroy() file: %s', this.file)
this.obj = {}
this.payload = {}
if (this.file && fs.existsSync(this.file)) {
fs.unlinkSync(this.file)
this.file = undefined
......
......@@ -57,8 +57,8 @@ export interface BridgeOptions {
}
export class Bridge extends EventEmitter {
private browser : Browser
private page : Page
private browser : undefined | Browser
private page : undefined | Page
private state : StateSwitch
constructor(
......@@ -276,6 +276,14 @@ export class Bridge extends EventEmitter {
public async quit(): Promise<void> {
log.verbose('PuppetPuppeteerBridge', 'quit()')
if (!this.page) {
throw new Error('no page')
}
if (!this.browser) {
throw new Error('no browser')
}
this.state.off('pending')
try {
......@@ -644,6 +652,11 @@ export class Bridge extends EventEmitter {
? ''
: ', ' + args.join(', '),
)
if (!this.page) {
throw new Error('no page')
}
try {
const noWechaty = await this.page.evaluate(() => {
return typeof WechatyBro === 'undefined'
......@@ -832,6 +845,11 @@ export class Bridge extends EventEmitter {
public async hostname(): Promise<string | null> {
log.verbose('PuppetPuppeteerBridge', 'hostname()')
if (!this.page) {
throw new Error('no page')
}
try {
const hostname = await this.page.evaluate(() => location.hostname) as string
log.silly('PuppetPuppeteerBridge', 'hostname() got %s', hostname)
......@@ -847,6 +865,10 @@ export class Bridge extends EventEmitter {
public async cookies(): Promise<Cookie[]>
public async cookies(cookieList?: Cookie[]): Promise<void | Cookie[]> {
if (!this.page) {
throw new Error('no page')
}
if (cookieList) {
try {
await this.page.setCookie(...cookieList)
......@@ -905,12 +927,22 @@ export class Bridge extends EventEmitter {
public async reload(): Promise<void> {
log.verbose('PuppetPuppeteerBridge', 'reload()')
if (!this.page) {
throw new Error('no page')
}
await this.page.reload()
return
}
public async evaluate(fn: () => any, ...args: any[]): Promise<any> {
log.silly('PuppetPuppeteerBridge', 'evaluate()')
if (!this.page) {
throw new Error('no page')
}
try {
return await this.page.evaluate(fn, ...args)
} catch (e) {
......
......@@ -47,7 +47,7 @@ import {
log,
Raven,
} from '../config'
import Profile from '../profile'
// import Profile from '../profile'
import Misc from '../misc'
import {
......@@ -104,7 +104,11 @@ export class PuppetPuppeteer extends Puppet {
) {
super(options)
// this.fileId = 0
this.fileId = 0
this.bridge = new Bridge({
head : config.head,
profile : this.options.profile,
})
const SCAN_TIMEOUT = 2 * 60 * 1000 // 2 minutes
this.scanWatchdog = new Watchdog<ScanFoodType>(SCAN_TIMEOUT, 'Scan')
......@@ -119,7 +123,7 @@ export class PuppetPuppeteer extends Puppet {
this.initWatchdog()
this.initWatchdogForScan()
this.bridge = await this.initBridge(this.options.profile)
this.bridge = await this.initBridge()
log.verbose('PuppetPuppeteer', 'initBridge() done')
/**
......@@ -275,7 +279,7 @@ export class PuppetPuppeteer extends Puppet {
}
}
public async initBridge(profile: Profile): Promise<Bridge> {
public async initBridge(): Promise<Bridge> {
log.verbose('PuppetPuppeteer', 'initBridge()')
if (this.state.off()) {
......@@ -284,14 +288,6 @@ export class PuppetPuppeteer extends Puppet {
throw e
}
const head = config.head
// we have to set this.bridge right now,
// because the Event.onXXX might arrive while we are initializing.
this.bridge = new Bridge({
head,
profile,
})
this.bridge.on('ding' , Event.onDing.bind(this))
this.bridge.on('error' , e => this.emit('error', e))
this.bridge.on('log' , Event.onLog.bind(this))
......@@ -749,7 +745,7 @@ export class PuppetPuppeteer extends Puppet {
): string {
log.verbose('PuppetPuppeteer', 'contactQueryFilterToFunctionString({ %s })',
Object.keys(query)
.map((k: keyof ContactQueryFilter) => `${k}: ${query[k]}`)
.map(k => `${k}: ${query[k as keyof ContactQueryFilter]}`)
.join(', '),
)
......
......@@ -116,7 +116,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
/**
* childPkg stores the `package.json` that the NPM module who extends the `Puppet`
*/
private readonly childPkg: normalize.Package
private readonly childPkg: undefined | normalize.Package
constructor(
public options: PuppetOptions,
......@@ -209,7 +209,10 @@ export abstract class Puppet extends EventEmitter implements Sayable {
}
public version(): string {
return this.childPkg.version
if (this.childPkg) {
return this.childPkg.version
}
return '0.0.0'
}
/**
......
......@@ -3,6 +3,7 @@
"target": "es6"
, "module": "commonjs"
, "outDir": "dist"
, "strict": true
, "experimentalDecorators": true
, "emitDecoratorMetadata": true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册