incyclist-services 1.3.52 → 1.3.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134,7 +134,7 @@ let ActivitiesRepository = (() => {
134
134
  return details;
135
135
  }
136
136
  catch (err) {
137
- this.logError(err, 'migrate');
137
+ this.logError(err, 'migrate', { activity });
138
138
  }
139
139
  }
140
140
  migrateDB() {
@@ -8,7 +8,7 @@ class MigrationV1 extends types_1.ActivitiesDBMigrator {
8
8
  let detailsChanged = false;
9
9
  if (details.routeType === 'Video' && details.route) {
10
10
  const route = this.getRouteList().getRouteDescription(details.route.id);
11
- if (route.originalName) {
11
+ if (route === null || route === void 0 ? void 0 : route.originalName) {
12
12
  details.route.name = route.originalName;
13
13
  detailsChanged = true;
14
14
  }
@@ -9,7 +9,7 @@ class MigrationV3 extends types_1.ActivitiesDBMigrator {
9
9
  let detailsChanged = false;
10
10
  if (details.routeType === 'Video' && details.route) {
11
11
  const route = this.getRouteList().getRouteDescription(details.route.id);
12
- if (route.title) {
12
+ if (route === null || route === void 0 ? void 0 : route.title) {
13
13
  details.route.title = route.title;
14
14
  detailsChanged = true;
15
15
  }
@@ -6,6 +6,7 @@ export interface Credentials {
6
6
  export interface VeloHeroAuth extends Credentials {
7
7
  id?: string;
8
8
  authKey?: string;
9
+ version?: string;
9
10
  }
10
11
  export type UploaderInfo = {
11
12
  service: string;
@@ -14,14 +14,16 @@ export declare class VeloHeroUpload extends IncyclistService implements IActivit
14
14
  isConnecting(): boolean;
15
15
  login(username: string, password: string): Promise<boolean>;
16
16
  disconnect(): void;
17
- protected ensureInitialized(): any;
18
17
  upload(activity: ActivityDetails, format?: string): Promise<boolean>;
19
18
  getUrl(id: string): string;
19
+ protected ensureInitialized(): any;
20
20
  protected getUuid(): any;
21
21
  protected getCredentials(): any;
22
22
  protected saveCredentials(): any;
23
- protected encrypt(): VeloHeroAuth;
24
- protected decrypt(auth: VeloHeroAuth): any;
25
- protected getUserSettings(): import("../../settings").UserSettingsService;
26
- protected getApi(): VeloHeroApi;
23
+ protected encrypt(algo: string): VeloHeroAuth;
24
+ protected decrypt(algo: string, auth: VeloHeroAuth): any;
25
+ protected setupEventListeners(): void;
26
+ protected getUserSettings(): any;
27
+ protected getApi(): any;
28
+ protected getCrypto(): any;
27
29
  }
@@ -57,6 +57,7 @@ const settings_1 = require("../../settings");
57
57
  const crypto_1 = __importDefault(require("crypto"));
58
58
  const valid_1 = require("../../utils/valid");
59
59
  const api_1 = require("../../apps/base/api");
60
+ const CRYPT_ALGO = 'aes256';
60
61
  let VeloHeroUpload = (() => {
61
62
  let _classDecorators = [types_1.Singleton];
62
63
  let _classDescriptor;
@@ -83,21 +84,19 @@ let VeloHeroUpload = (() => {
83
84
  this.password = auth.password;
84
85
  }
85
86
  else {
86
- const user = this.decrypt(auth);
87
+ const user = this.decrypt(CRYPT_ALGO, auth);
87
88
  this.username = user.username;
88
89
  this.password = user.password;
89
90
  }
90
- this.logger.logEvent({ message: 'VeloHero init done', hasCredentials: true });
91
91
  }
92
+ this.logEvent({ message: 'VeloHero init done', hasCredentials: ((0, valid_1.valid)(this.username) && (0, valid_1.valid)(this.password)) });
92
93
  this.isInitialized = true;
93
94
  }
94
95
  catch (err) {
95
- this.logger.logEvent({ message: 'error', error: err.message, fn: 'init', stack: err.stack });
96
+ this.logEvent({ message: 'error', error: err.message, fn: 'init', stack: err.stack });
96
97
  this.isInitialized = false;
97
98
  }
98
- this.on('login-start', () => { this._isConnecting = true; });
99
- this.on('login-success', () => { this._isConnecting = false; });
100
- this.on('login-failure', () => { this._isConnecting = false; });
99
+ this.setupEventListeners();
101
100
  return this.isInitialized;
102
101
  }
103
102
  isConnected() {
@@ -110,11 +109,11 @@ let VeloHeroUpload = (() => {
110
109
  login(username, password) {
111
110
  return __awaiter(this, void 0, void 0, function* () {
112
111
  this.ensureInitialized();
113
- this.logger.logEvent({ message: 'VeloHero Login' });
112
+ this.logEvent({ message: 'VeloHero Login' });
114
113
  this.emit('login-start');
115
114
  try {
116
115
  yield this.getApi().login(username, password);
117
- this.logger.logEvent({ message: 'VeloHero Login success' });
116
+ this.logEvent({ message: 'VeloHero Login success' });
118
117
  this.username = username;
119
118
  this.password = password;
120
119
  this.isInitialized = true;
@@ -123,8 +122,8 @@ let VeloHeroUpload = (() => {
123
122
  return true;
124
123
  }
125
124
  catch (err) {
126
- this.logger.logEvent({ message: 'VeloHero Login failed', error: err.message });
127
- this.emit('login-failure');
125
+ this.logEvent({ message: 'VeloHero Login failed', error: err.message });
126
+ this.emit('login-failure', err.message);
128
127
  throw err;
129
128
  }
130
129
  });
@@ -134,29 +133,28 @@ let VeloHeroUpload = (() => {
134
133
  this.password = undefined;
135
134
  this.getUserSettings().set('user.auth.velohero', null);
136
135
  }
137
- ensureInitialized() {
138
- if (!this.isInitialized)
139
- this.init();
140
- return this.isInitialized;
141
- }
142
136
  upload(activity_1) {
143
137
  return __awaiter(this, arguments, void 0, function* (activity, format = 'TCX') {
144
138
  var _a;
145
139
  try {
146
140
  const ok = this.ensureInitialized();
147
141
  if (!ok) {
148
- this.logger.logEvent({ message: 'VeloHero Upload skipped', reason: 'not initialized' });
142
+ this.logEvent({ message: 'VeloHero Upload skipped', reason: 'not initialized' });
143
+ return false;
144
+ }
145
+ if (!this.isConnected()) {
149
146
  return false;
150
147
  }
151
- this.logger.logEvent({ message: 'VeloHero Upload', format });
148
+ const lcFormat = format.toLowerCase();
149
+ const ucFormat = format.toUpperCase();
150
+ this.logEvent({ message: 'VeloHero Upload', format: ucFormat });
152
151
  if (!activity.links)
153
152
  activity.links = {};
154
153
  const username = this.username;
155
154
  const password = this.password;
156
- const fileName = activity[`${format}FileName`];
155
+ const fileName = activity[`${lcFormat}FileName`];
157
156
  const res = yield this.getApi().upload(fileName, { username, password });
158
- this.logger.logEvent(Object.assign({ message: 'VeloHero Upload success' }, res));
159
- console.log(new Date().toISOString(), 'VeloHero Upload success', res);
157
+ this.logEvent(Object.assign({ message: 'VeloHero Upload success' }, res));
160
158
  activity.links.velohero = {
161
159
  activity_id: res.id,
162
160
  url: (_a = res['url-show']) !== null && _a !== void 0 ? _a : this.getUrl(res.id)
@@ -164,7 +162,7 @@ let VeloHeroUpload = (() => {
164
162
  return true;
165
163
  }
166
164
  catch (err) {
167
- this.logger.logEvent({ message: 'VeloHero Upload failure', error: err.message });
165
+ this.logEvent({ message: 'VeloHero Upload failure', error: err.message });
168
166
  activity.links.velohero = {
169
167
  error: err.message
170
168
  };
@@ -175,6 +173,11 @@ let VeloHeroUpload = (() => {
175
173
  getUrl(id) {
176
174
  return `https://app.velohero.com/workouts/show/${id}`;
177
175
  }
176
+ ensureInitialized() {
177
+ if (!this.isInitialized)
178
+ this.init();
179
+ return this.isInitialized;
180
+ }
178
181
  getUuid() {
179
182
  const userSettings = this.getUserSettings();
180
183
  try {
@@ -198,60 +201,79 @@ let VeloHeroUpload = (() => {
198
201
  this.logger.logEvent({ message: 'VeloHero Save Credentials' });
199
202
  let auth;
200
203
  try {
201
- auth = this.encrypt();
204
+ auth = this.encrypt(CRYPT_ALGO);
202
205
  this.getUserSettings().set('user.auth.velohero', auth);
203
206
  }
204
207
  catch (err) {
205
- this.logger.logEvent({ message: 'error', fn: 'saveCredentials', error: err.message, stack: err.stack });
208
+ this.logEvent({ message: 'error', fn: 'saveCredentials', error: err.message, stack: err.stack });
206
209
  }
207
- this.logger.logEvent({ message: 'VeloHero Save Credentials done' });
210
+ this.logEvent({ message: 'VeloHero Save Credentials done' });
208
211
  return auth;
209
212
  }
210
- encrypt() {
213
+ encrypt(algo) {
211
214
  const iv = crypto_1.default.randomBytes(16);
212
215
  const uuid = this.getUuid();
213
216
  const key = `${uuid.substring(0, 32)}`;
214
- const cipher = crypto_1.default.createCipheriv('AES-256-CCM', key, iv);
217
+ const cipher = crypto_1.default.createCipheriv(algo, key, iv);
215
218
  const text = JSON.stringify({ username: this.username, password: this.password });
216
- let ciphered = cipher.update(text, 'utf8', 'hex');
219
+ let ciphered;
220
+ ciphered = cipher.update(text, 'utf8', 'hex');
217
221
  ciphered += cipher.final('hex');
218
222
  const auth = {
219
223
  id: iv.toString('hex'),
220
- authKey: ciphered
224
+ authKey: ciphered,
221
225
  };
222
226
  return auth;
223
227
  }
224
- decrypt(auth) {
225
- const { id, authKey } = auth;
228
+ decrypt(algo, auth) {
229
+ const { id, authKey, version } = auth;
226
230
  const iv = Buffer.from(id, 'hex');
227
231
  const uuid = this.getUuid();
228
232
  const key = `${uuid.substring(0, 32)}`;
229
- const decipher = (em) => {
230
- try {
231
- const cipher = crypto_1.default.createDecipheriv(em, key, iv);
232
- let text = cipher.update(authKey, 'hex', 'utf8');
233
+ try {
234
+ let text;
235
+ if (algo === 'aes-256-gcm') {
236
+ const cipher = crypto_1.default.createDecipheriv(algo, key, iv);
237
+ const raw = Buffer.from(authKey, "hex");
238
+ const authTagBuff = raw.subarray(raw.length - 16);
239
+ const encTextBuff = raw.subarray(0, raw.length - 16);
240
+ cipher.setAuthTag(authTagBuff);
241
+ text = cipher.update(encTextBuff);
233
242
  text += cipher.final('utf8');
234
- const user = JSON.parse(text);
235
- return user;
236
243
  }
237
- catch (err) {
244
+ else {
245
+ const cipher = crypto_1.default.createDecipheriv(algo, key, iv);
246
+ text = cipher.update(authKey, 'hex', 'utf8');
247
+ text += cipher.final('utf8');
238
248
  }
239
- };
240
- let user;
241
- user = decipher('AES-256-CCM');
242
- if (!user) {
243
- user = decipher('aes256');
249
+ const user = JSON.parse(text);
250
+ return user;
251
+ }
252
+ catch (err) {
253
+ this.logError(err, 'decrypt', { algo, id, authKey });
254
+ return null;
244
255
  }
245
- return user;
256
+ }
257
+ setupEventListeners() {
258
+ this.on('login-start', () => { this._isConnecting = true; });
259
+ this.on('login-success', () => { this._isConnecting = false; });
260
+ this.on('login-failure', () => { this._isConnecting = false; });
246
261
  }
247
262
  getUserSettings() {
248
- return (0, settings_1.useUserSettings)();
263
+ var _a;
264
+ return (_a = this.injected['UserSettings']) !== null && _a !== void 0 ? _a : (0, settings_1.useUserSettings)();
249
265
  }
250
266
  getApi() {
267
+ if (this.injected['Api'])
268
+ return this.injected['Api'];
251
269
  if (!this.api)
252
270
  this.api = new api_1.VeloHeroApi();
253
271
  return this.api;
254
272
  }
273
+ getCrypto() {
274
+ var _a;
275
+ return (_a = this.injected['Crypto']) !== null && _a !== void 0 ? _a : crypto_1.default;
276
+ }
255
277
  };
256
278
  __setFunctionName(_classThis, "VeloHeroUpload");
257
279
  (() => {
@@ -9,4 +9,5 @@ export declare class IncyclistService extends EventEmitter {
9
9
  logEvent(event: any): void;
10
10
  setDebug(enabled: boolean): void;
11
11
  logError(err: Error, fn: string, args?: any): void;
12
+ reset(): void;
12
13
  }
@@ -18,7 +18,9 @@ class IncyclistService extends events_1.default {
18
18
  }
19
19
  logEvent(event) {
20
20
  this.logger.logEvent(event);
21
- this.emit('log', event);
21
+ const emitPayload = Object.assign({}, event);
22
+ delete emitPayload.ts;
23
+ this.emit('log', emitPayload);
22
24
  const w = global.window;
23
25
  if (this.debug || (w === null || w === void 0 ? void 0 : w.SERVICE_DEBUG) || process.env.DEBUG)
24
26
  console.log(`~~~ ${this.logger.getName().toUpperCase()}-SVC`, event);
@@ -30,5 +32,8 @@ class IncyclistService extends events_1.default {
30
32
  const logInfo = args || {};
31
33
  this.logEvent(Object.assign(Object.assign({ message: 'Error', fn }, logInfo), { error: err.message, stack: err.stack }));
32
34
  }
35
+ reset() {
36
+ this.removeAllListeners();
37
+ }
33
38
  }
34
39
  exports.IncyclistService = IncyclistService;
@@ -5,11 +5,19 @@ function Singleton(originalConstructor, _context) {
5
5
  let instance;
6
6
  return class {
7
7
  constructor(...args) {
8
+ var _a;
8
9
  if (instance) {
9
10
  return instance;
10
11
  }
11
12
  instance = new originalConstructor(...args);
12
- instance.reset = () => { instance = null; };
13
+ const resetFn = (_a = instance['reset']) === null || _a === void 0 ? void 0 : _a.bind(instance);
14
+ if (resetFn === undefined || typeof resetFn === 'function') {
15
+ instance['reset'] = () => {
16
+ instance = null;
17
+ if (resetFn)
18
+ resetFn();
19
+ };
20
+ }
13
21
  return instance;
14
22
  }
15
23
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-services",
3
- "version": "1.3.52",
3
+ "version": "1.3.54",
4
4
  "peerDependencies": {
5
5
  "gd-eventlog": "^0.1.26"
6
6
  },