UNPKG

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