UNPKG

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