UNPKG

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