From 89098db512db17f0cb0c4d5c7d287c1a1f702b4c Mon Sep 17 00:00:00 2001 From: Huan LI Date: Sat, 26 May 2018 12:40:09 +0800 Subject: [PATCH] enable tsconfig **strict** mode (#1180) --- examples/friend-bot.ts | 2 +- src/contact.ts | 2 +- src/io.ts | 16 +++++++++-- src/profile.ts | 31 +++++++++++--------- src/puppet-puppeteer/bridge.ts | 36 ++++++++++++++++++++++-- src/puppet-puppeteer/puppet-puppeteer.ts | 22 ++++++--------- src/puppet/puppet.ts | 7 +++-- tsconfig.json | 1 + 8 files changed, 81 insertions(+), 36 deletions(-) diff --git a/examples/friend-bot.ts b/examples/friend-bot.ts index 8dd7cf70..d6e3ce18 100644 --- a/examples/friend-bot.ts +++ b/examples/friend-bot.ts @@ -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 { diff --git a/src/contact.ts b/src/contact.ts index 7b7392e5..aba684c6 100644 --- a/src/contact.ts +++ b/src/contact.ts @@ -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(', '), ) diff --git a/src/io.ts b/src/io.ts index f75353d1..38f1a4c3 100644 --- a/src/io.ts +++ b/src/io.ts @@ -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 { + 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[] = [] 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 { + if (!this.ws) { + throw new Error('no ws') + } + this.state.off('pending') // try to send IoEvents in buffer diff --git a/src/profile.ts b/src/profile.ts index 95fa2ab0..67fe00d3 100644 --- a/src/profile.ts +++ b/src/profile.ts @@ -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 { 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(section: ProfileSection): Promise { 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 { 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 { 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 diff --git a/src/puppet-puppeteer/bridge.ts b/src/puppet-puppeteer/bridge.ts index d7bfb4a8..73845a5a 100644 --- a/src/puppet-puppeteer/bridge.ts +++ b/src/puppet-puppeteer/bridge.ts @@ -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 { 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 { 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 public async cookies(cookieList?: Cookie[]): Promise { + 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 { 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 { log.silly('PuppetPuppeteerBridge', 'evaluate()') + + if (!this.page) { + throw new Error('no page') + } + try { return await this.page.evaluate(fn, ...args) } catch (e) { diff --git a/src/puppet-puppeteer/puppet-puppeteer.ts b/src/puppet-puppeteer/puppet-puppeteer.ts index afc5d3a1..c72bc964 100644 --- a/src/puppet-puppeteer/puppet-puppeteer.ts +++ b/src/puppet-puppeteer/puppet-puppeteer.ts @@ -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(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 { + public async initBridge(): Promise { 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(', '), ) diff --git a/src/puppet/puppet.ts b/src/puppet/puppet.ts index c540fb46..ac97de1c 100644 --- a/src/puppet/puppet.ts +++ b/src/puppet/puppet.ts @@ -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' } /** diff --git a/tsconfig.json b/tsconfig.json index cfbcdc7d..9e99f453 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "target": "es6" , "module": "commonjs" , "outDir": "dist" + , "strict": true , "experimentalDecorators": true , "emitDecoratorMetadata": true -- GitLab