UNPKG

5.58 kBPlain TextView Raw
1/**
2 * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
3 *
4 * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
5 * Wechaty Contributors <https://github.com/wechaty>.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 */
20/// <reference path="./typings.d.ts" />
21/// <reference path="./io-peer/json-rpc-peer.d.ts" />
22
23import os from 'os'
24
25import Raven from 'raven'
26import readPkgUp from 'read-pkg-up'
27
28import {
29 FileBox,
30 MemoryCard,
31 log,
32} from 'wechaty-puppet'
33
34import { looseInstanceOfClass } from './helper-functions/mod'
35
36import {
37 PuppetModuleName,
38 PUPPET_NAME_DEFAULT,
39} from './puppet-config'
40import {
41 VERSION,
42 GIT_COMMIT_HASH,
43} from './version'
44
45const pkg = readPkgUp.sync({ cwd: __dirname })!.packageJson
46
47/**
48 * Raven.io
49 */
50Raven.disableConsoleAlerts()
51
52Raven
53 .config(
54 isProduction()
55 && 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672',
56 {
57 release: VERSION,
58 tags: {
59 git_commit: GIT_COMMIT_HASH,
60 platform: process.env['WECHATY_DOCKER']
61 ? 'docker'
62 : os.platform(),
63 },
64 },
65 )
66 .install()
67
68/*
69try {
70 doSomething(a[0])
71} catch (e) {
72 Raven.captureException(e)
73}
74
75Raven.context(function () {
76 doSomething(a[0])
77})
78 */
79
80/**
81 * to handle unhandled exceptions
82 */
83if (log.level() === 'verbose' || log.level() === 'silly') {
84 log.info('Config', 'registering process.on("unhandledRejection") for development/debug')
85
86 /**
87 * Refer to https://nodejs.org/api/process.html#process_event_unhandledrejection
88 * the reason is in type: Error | any
89 */
90 process.on('unhandledRejection', (reason: Error | any, promise) => {
91 log.error('Config', '###########################')
92 log.error('Config', 'unhandledRejection: %s %s', reason.stack || reason, promise)
93 log.error('Config', '###########################')
94 promise.catch(err => {
95 log.error('Config', 'process.on(unhandledRejection) promise.catch(%s)', err.message)
96 console.error('Config', err) // I don't know if log.error has similar full trace print support like console.error
97 })
98 })
99
100 process.on('uncaughtException', function (error) {
101 const origin = arguments[1] // to compatible with node 12 or below version typings
102
103 log.error('Config', '###########################')
104 log.error('Config', 'uncaughtException: %s %s', error.stack, origin)
105 log.error('Config', '###########################')
106 })
107}
108
109export interface DefaultSetting {
110 DEFAULT_PORT : number,
111 DEFAULT_APIHOST : string,
112 DEFAULT_PROTOCOL : string,
113}
114
115const DEFAULT_SETTING = pkg['wechaty'] as DefaultSetting
116
117export class Config {
118
119 public default = DEFAULT_SETTING
120
121 public apihost = process.env['WECHATY_APIHOST'] || DEFAULT_SETTING.DEFAULT_APIHOST
122 public serviceIp = process.env['WECHATY_PUPPET_SERVICE_IP'] || ''
123
124 public systemPuppetName (): PuppetModuleName {
125 return (
126 process.env['WECHATY_PUPPET'] || PUPPET_NAME_DEFAULT
127 ).toLowerCase() as PuppetModuleName
128 }
129
130 public name = process.env['WECHATY_NAME']
131
132 // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
133 public token = process.env['WECHATY_TOKEN']
134
135 public debug = !!(process.env['WECHATY_DEBUG'])
136
137 public httpPort = process.env['PORT']
138 || process.env['WECHATY_PORT']
139 || DEFAULT_SETTING.DEFAULT_PORT
140
141 public docker = !!(process.env['WECHATY_DOCKER'])
142
143 constructor () {
144 log.verbose('Config', 'constructor()')
145 this.validApiHost(this.apihost)
146 }
147
148 public validApiHost (apihost: string): boolean {
149 if (/^[a-zA-Z0-9.\-_]+:?[0-9]*$/.test(apihost)) {
150 return true
151 }
152 throw new Error('validApiHost() fail for ' + apihost)
153 }
154
155}
156
157export const CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d'
158
159export function qrCodeForChatie (): FileBox {
160 const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
161 return FileBox.fromQRCode(CHATIE_OFFICIAL_ACCOUNT_QRCODE)
162}
163
164// http://jkorpela.fi/chars/spaces.html
165// String.fromCharCode(8197)
166export const FOUR_PER_EM_SPACE = String.fromCharCode(0x2005)
167// mobile: \u2005, PC、mac: \u0020
168export const AT_SEPARATOR_REGEX = /[\u2005\u0020]/
169
170export function qrcodeValueToImageUrl (qrcodeValue: string): string {
171 return [
172 'https://wechaty.js.org/qrcode/',
173 encodeURIComponent(qrcodeValue),
174 ].join('')
175}
176
177export function isProduction (): boolean {
178 return process.env['NODE_ENV'] === 'production'
179 || process.env['NODE_ENV'] === 'prod'
180}
181
182/**
183 * Huan(202011):
184 * Create a `looseInstanceOfClass` to check `FileBox` and `Puppet` instances #2090
185 * https://github.com/wechaty/wechaty/issues/2090
186 */
187type FileBoxClass = FileBox & {
188 new (...args: any): FileBox
189}
190const looseInstanceOfFileBox = looseInstanceOfClass(
191 FileBox as any as FileBoxClass
192)
193
194export {
195 log,
196 FileBox,
197 MemoryCard,
198 Raven,
199 looseInstanceOfFileBox,
200
201 VERSION,
202}
203
204export const config = new Config()