UNPKG

5.06 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 {
35 PuppetModuleName,
36 PUPPET_NAME_DEFAULT,
37} from './puppet-config'
38import {
39 VERSION,
40 GIT_COMMIT_HASH,
41} from './version'
42
43const pkg = readPkgUp.sync({ cwd: __dirname })!.packageJson
44
45/**
46 * Raven.io
47 */
48Raven.disableConsoleAlerts()
49
50Raven
51 .config(
52 isProduction()
53 && 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672',
54 {
55 release: VERSION,
56 tags: {
57 git_commit: GIT_COMMIT_HASH,
58 platform: process.env.WECHATY_DOCKER
59 ? 'docker'
60 : os.platform(),
61 },
62 },
63 )
64 .install()
65
66/*
67try {
68 doSomething(a[0])
69} catch (e) {
70 Raven.captureException(e)
71}
72
73Raven.context(function () {
74 doSomething(a[0])
75})
76 */
77
78/**
79 * to handle unhandled exceptions
80 */
81if (log.level() === 'verbose' || log.level() === 'silly') {
82 log.info('Config', 'registering process.on("unhandledRejection") for development/debug')
83
84 /**
85 * Refer to https://nodejs.org/api/process.html#process_event_unhandledrejection
86 * the reason is in type: Error | any
87 */
88 process.on('unhandledRejection', (reason: Error | any, promise) => {
89 log.error('Config', '###########################')
90 log.error('Config', 'unhandledRejection: %s %s', reason.stack || reason, promise)
91 log.error('Config', '###########################')
92 promise.catch(err => {
93 log.error('Config', 'process.on(unhandledRejection) promise.catch(%s)', err.message)
94 console.error('Config', err) // I don't know if log.error has similar full trace print support like console.error
95 })
96 })
97
98 process.on('uncaughtException', function (error) {
99 const origin = arguments[1] // to compatible with node 12 or below version typings
100
101 log.error('Config', '###########################')
102 log.error('Config', 'uncaughtException: %s %s', error.stack, origin)
103 log.error('Config', '###########################')
104 })
105}
106
107export interface DefaultSetting {
108 DEFAULT_PORT : number,
109 DEFAULT_APIHOST : string,
110 DEFAULT_PROTOCOL : string,
111}
112
113const DEFAULT_SETTING = pkg.wechaty as DefaultSetting
114
115export class Config {
116
117 public default = DEFAULT_SETTING
118
119 public apihost = process.env.WECHATY_APIHOST || DEFAULT_SETTING.DEFAULT_APIHOST
120
121 public systemPuppetName (): PuppetModuleName {
122 return (
123 process.env.WECHATY_PUPPET || PUPPET_NAME_DEFAULT
124 ).toLowerCase() as PuppetModuleName
125 }
126
127 public name = process.env.WECHATY_NAME
128
129 // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
130 public token = process.env.WECHATY_TOKEN
131
132 public debug = !!(process.env.WECHATY_DEBUG)
133
134 public httpPort = process.env.PORT || process.env.WECHATY_PORT || DEFAULT_SETTING.DEFAULT_PORT
135 public docker = !!(process.env.WECHATY_DOCKER)
136
137 constructor () {
138 log.verbose('Config', 'constructor()')
139 this.validApiHost(this.apihost)
140 }
141
142 public validApiHost (apihost: string): boolean {
143 if (/^[a-zA-Z0-9.\-_]+:?[0-9]*$/.test(apihost)) {
144 return true
145 }
146 throw new Error('validApiHost() fail for ' + apihost)
147 }
148
149}
150
151export const CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d'
152
153export function qrCodeForChatie (): FileBox {
154 const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
155 return FileBox.fromQRCode(CHATIE_OFFICIAL_ACCOUNT_QRCODE)
156}
157
158// http://jkorpela.fi/chars/spaces.html
159// String.fromCharCode(8197)
160export const FOUR_PER_EM_SPACE = String.fromCharCode(0x2005)
161// mobile: \u2005, PC、mac: \u0020
162export const AT_SEPARATOR_REGEX = /[\u2005\u0020]/
163
164export function qrcodeValueToImageUrl (qrcodeValue: string): string {
165 return [
166 'https://wechaty.github.io/qrcode/',
167 encodeURIComponent(qrcodeValue),
168 ].join('')
169}
170
171export function isProduction (): boolean {
172 return process.env.NODE_ENV === 'production'
173 || process.env.NODE_ENV === 'prod'
174}
175
176export {
177 log,
178 FileBox,
179 MemoryCard,
180 Raven,
181
182 VERSION,
183}
184
185export const config = new Config()