finish core
This commit is contained in:
54
package-lock.json
generated
54
package-lock.json
generated
@@ -23,6 +23,7 @@
|
|||||||
"react-redux": "^8.0.5",
|
"react-redux": "^8.0.5",
|
||||||
"react-transition-group": "^4.4.5",
|
"react-transition-group": "^4.4.5",
|
||||||
"redux-logger": "^4.0.0",
|
"redux-logger": "^4.0.0",
|
||||||
|
"uuid": "^10.0.0",
|
||||||
"webextension-polyfill": "^0.10.0"
|
"webextension-polyfill": "^0.10.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -49,6 +50,7 @@
|
|||||||
"@types/redux-logger": "^3.0.9",
|
"@types/redux-logger": "^3.0.9",
|
||||||
"@types/shelljs": "^0.8.8",
|
"@types/shelljs": "^0.8.8",
|
||||||
"@types/sinon-chrome": "^2.2.11",
|
"@types/sinon-chrome": "^2.2.11",
|
||||||
|
"@types/uuid": "^10.0.0",
|
||||||
"@types/wait-on": "^5.2.0",
|
"@types/wait-on": "^5.2.0",
|
||||||
"@types/webextension-polyfill": "^0.10.0",
|
"@types/webextension-polyfill": "^0.10.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
||||||
@@ -4096,6 +4098,13 @@
|
|||||||
"resolved": "https://registry.npmjs.org/@types/use-sync-external-store/-/use-sync-external-store-0.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/@types/use-sync-external-store/-/use-sync-external-store-0.0.3.tgz",
|
||||||
"integrity": "sha512-EwmlvuaxPNej9+T4v5AuBPJa2x2UOJVdjCtDHgcDqitUeOtjnJKJ+apYjVcAoBEMjKW1VVFGZLUb5+qqa09XFA=="
|
"integrity": "sha512-EwmlvuaxPNej9+T4v5AuBPJa2x2UOJVdjCtDHgcDqitUeOtjnJKJ+apYjVcAoBEMjKW1VVFGZLUb5+qqa09XFA=="
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/uuid": {
|
||||||
|
"version": "10.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz",
|
||||||
|
"integrity": "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/@types/wait-on": {
|
"node_modules/@types/wait-on": {
|
||||||
"version": "5.3.1",
|
"version": "5.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/@types/wait-on/-/wait-on-5.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/@types/wait-on/-/wait-on-5.3.1.tgz",
|
||||||
@@ -18779,6 +18788,17 @@
|
|||||||
"node": ">=0.8"
|
"node": ">=0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/request/node_modules/uuid": {
|
||||||
|
"version": "3.4.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
|
||||||
|
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
|
||||||
|
"deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"bin": {
|
||||||
|
"uuid": "bin/uuid"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/require-at": {
|
"node_modules/require-at": {
|
||||||
"version": "1.0.6",
|
"version": "1.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/require-at/-/require-at-1.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/require-at/-/require-at-1.0.6.tgz",
|
||||||
@@ -21388,13 +21408,16 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/uuid": {
|
"node_modules/uuid": {
|
||||||
"version": "3.4.0",
|
"version": "10.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz",
|
||||||
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
|
"integrity": "sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==",
|
||||||
"deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.",
|
"funding": [
|
||||||
"dev": true,
|
"https://github.com/sponsors/broofa",
|
||||||
|
"https://github.com/sponsors/ctavan"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
"bin": {
|
"bin": {
|
||||||
"uuid": "bin/uuid"
|
"uuid": "dist/bin/uuid"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/v8-compile-cache": {
|
"node_modules/v8-compile-cache": {
|
||||||
@@ -25578,6 +25601,12 @@
|
|||||||
"resolved": "https://registry.npmjs.org/@types/use-sync-external-store/-/use-sync-external-store-0.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/@types/use-sync-external-store/-/use-sync-external-store-0.0.3.tgz",
|
||||||
"integrity": "sha512-EwmlvuaxPNej9+T4v5AuBPJa2x2UOJVdjCtDHgcDqitUeOtjnJKJ+apYjVcAoBEMjKW1VVFGZLUb5+qqa09XFA=="
|
"integrity": "sha512-EwmlvuaxPNej9+T4v5AuBPJa2x2UOJVdjCtDHgcDqitUeOtjnJKJ+apYjVcAoBEMjKW1VVFGZLUb5+qqa09XFA=="
|
||||||
},
|
},
|
||||||
|
"@types/uuid": {
|
||||||
|
"version": "10.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz",
|
||||||
|
"integrity": "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"@types/wait-on": {
|
"@types/wait-on": {
|
||||||
"version": "5.3.1",
|
"version": "5.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/@types/wait-on/-/wait-on-5.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/@types/wait-on/-/wait-on-5.3.1.tgz",
|
||||||
@@ -36628,6 +36657,12 @@
|
|||||||
"psl": "^1.1.28",
|
"psl": "^1.1.28",
|
||||||
"punycode": "^2.1.1"
|
"punycode": "^2.1.1"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"uuid": {
|
||||||
|
"version": "3.4.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
|
||||||
|
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
|
||||||
|
"dev": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -38625,10 +38660,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"uuid": {
|
"uuid": {
|
||||||
"version": "3.4.0",
|
"version": "10.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz",
|
||||||
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
|
"integrity": "sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"v8-compile-cache": {
|
"v8-compile-cache": {
|
||||||
"version": "2.3.0",
|
"version": "2.3.0",
|
||||||
|
|||||||
@@ -44,6 +44,7 @@
|
|||||||
"react-redux": "^8.0.5",
|
"react-redux": "^8.0.5",
|
||||||
"react-transition-group": "^4.4.5",
|
"react-transition-group": "^4.4.5",
|
||||||
"redux-logger": "^4.0.0",
|
"redux-logger": "^4.0.0",
|
||||||
|
"uuid": "^10.0.0",
|
||||||
"webextension-polyfill": "^0.10.0"
|
"webextension-polyfill": "^0.10.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -70,6 +71,7 @@
|
|||||||
"@types/redux-logger": "^3.0.9",
|
"@types/redux-logger": "^3.0.9",
|
||||||
"@types/shelljs": "^0.8.8",
|
"@types/shelljs": "^0.8.8",
|
||||||
"@types/sinon-chrome": "^2.2.11",
|
"@types/sinon-chrome": "^2.2.11",
|
||||||
|
"@types/uuid": "^10.0.0",
|
||||||
"@types/wait-on": "^5.2.0",
|
"@types/wait-on": "^5.2.0",
|
||||||
"@types/webextension-polyfill": "^0.10.0",
|
"@types/webextension-polyfill": "^0.10.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export default function MainList({
|
|||||||
const disableLogging = async (): Promise<void> => {
|
const disableLogging = async (): Promise<void> => {
|
||||||
dispatch(setLoggingEnabled(false));
|
dispatch(setLoggingEnabled(false));
|
||||||
await browser.storage.sync.set({ loggingEnabled: false });
|
await browser.storage.sync.set({ loggingEnabled: false });
|
||||||
await changeExtensionState('notLogging');
|
await changeExtensionState('trackingDisabled');
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ describe('wakatime config', () => {
|
|||||||
"colors": {
|
"colors": {
|
||||||
"allGood": "",
|
"allGood": "",
|
||||||
"lightTheme": "white",
|
"lightTheme": "white",
|
||||||
"notLogging": "gray",
|
"trackingDisabled": "gray",
|
||||||
"notSignedIn": "red",
|
"notSignedIn": "red",
|
||||||
},
|
},
|
||||||
"currentUserApiEndPoint": "/users/current",
|
"currentUserApiEndPoint": "/users/current",
|
||||||
@@ -46,7 +46,7 @@ describe('wakatime config', () => {
|
|||||||
"udemy.com",
|
"udemy.com",
|
||||||
"w3schools.com",
|
"w3schools.com",
|
||||||
],
|
],
|
||||||
"heartbeatApiEndPoint": "/users/current/heartbeats",
|
"heartbeatApiEndPoint": "/users/current/heartbeats.bulk",
|
||||||
"hostname": "",
|
"hostname": "",
|
||||||
"loggingEnabled": true,
|
"loggingEnabled": true,
|
||||||
"loggingStyle": "deny",
|
"loggingStyle": "deny",
|
||||||
@@ -71,7 +71,7 @@ describe('wakatime config', () => {
|
|||||||
],
|
],
|
||||||
"states": [
|
"states": [
|
||||||
"allGood",
|
"allGood",
|
||||||
"notLogging",
|
"trackingDisabled",
|
||||||
"notSignedIn",
|
"notSignedIn",
|
||||||
"ignored",
|
"ignored",
|
||||||
],
|
],
|
||||||
@@ -80,7 +80,7 @@ describe('wakatime config', () => {
|
|||||||
"tooltips": {
|
"tooltips": {
|
||||||
"allGood": "",
|
"allGood": "",
|
||||||
"ignored": "This URL is ignored",
|
"ignored": "This URL is ignored",
|
||||||
"notLogging": "Not logging",
|
"trackingDisabled": "Not logging",
|
||||||
"notSignedIn": "Not signed In",
|
"notSignedIn": "Not signed In",
|
||||||
},
|
},
|
||||||
"trackSocialMedia": true,
|
"trackSocialMedia": true,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import browser from 'webextension-polyfill';
|
|||||||
/**
|
/**
|
||||||
* Logging
|
* Logging
|
||||||
*/
|
*/
|
||||||
export type ExtensionStatus = 'allGood' | 'notLogging' | 'notSignedIn' | 'ignored';
|
export type ExtensionStatus = 'allGood' | 'trackingDisabled' | 'notSignedIn' | 'ignored';
|
||||||
/**
|
/**
|
||||||
* Supported logging style
|
* Supported logging style
|
||||||
*/
|
*/
|
||||||
@@ -35,7 +35,7 @@ interface SuccessOrFailAlert {
|
|||||||
interface Colors {
|
interface Colors {
|
||||||
allGood: '';
|
allGood: '';
|
||||||
lightTheme: 'white';
|
lightTheme: 'white';
|
||||||
notLogging: 'gray';
|
trackingDisabled: 'gray';
|
||||||
notSignedIn: 'red';
|
notSignedIn: 'red';
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -44,7 +44,7 @@ interface Colors {
|
|||||||
interface Tooltips {
|
interface Tooltips {
|
||||||
allGood: string;
|
allGood: string;
|
||||||
ignored: string;
|
ignored: string;
|
||||||
notLogging: string;
|
trackingDisabled: string;
|
||||||
notSignedIn: string;
|
notSignedIn: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ const config: Config = {
|
|||||||
colors: {
|
colors: {
|
||||||
allGood: '',
|
allGood: '',
|
||||||
lightTheme: 'white',
|
lightTheme: 'white',
|
||||||
notLogging: 'gray',
|
trackingDisabled: 'gray',
|
||||||
notSignedIn: 'red',
|
notSignedIn: 'red',
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -176,7 +176,7 @@ const config: Config = {
|
|||||||
'youtube.com',
|
'youtube.com',
|
||||||
],
|
],
|
||||||
|
|
||||||
states: ['allGood', 'notLogging', 'notSignedIn', 'ignored'],
|
states: ['allGood', 'trackingDisabled', 'notSignedIn', 'ignored'],
|
||||||
|
|
||||||
summariesApiEndPoint: process.env.SUMMARIES_API_URL ?? '/users/current/summaries',
|
summariesApiEndPoint: process.env.SUMMARIES_API_URL ?? '/users/current/summaries',
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ const config: Config = {
|
|||||||
tooltips: {
|
tooltips: {
|
||||||
allGood: '',
|
allGood: '',
|
||||||
ignored: 'This URL is ignored',
|
ignored: 'This URL is ignored',
|
||||||
notLogging: 'Not logging',
|
trackingDisabled: 'Not logging',
|
||||||
notSignedIn: 'Not signed In',
|
notSignedIn: 'Not signed In',
|
||||||
},
|
},
|
||||||
trackSocialMedia: true,
|
trackSocialMedia: true,
|
||||||
|
|||||||
@@ -3,16 +3,15 @@ import browser, { Tabs } from 'webextension-polyfill';
|
|||||||
/* eslint-disable no-fallthrough */
|
/* eslint-disable no-fallthrough */
|
||||||
/* eslint-disable default-case */
|
/* eslint-disable default-case */
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import { OptionalHeartbeat } from 'src/types/sites';
|
import { v4 as uuid4 } from 'uuid';
|
||||||
import { getOperatingSystem } from '../utils';
|
import { OptionalHeartbeat } from '../types/sites';
|
||||||
|
import { getOperatingSystem, IS_EDGE, IS_FIREFOX } from '../utils';
|
||||||
import { changeExtensionStatus } from '../utils/changeExtensionStatus';
|
import { changeExtensionStatus } from '../utils/changeExtensionStatus';
|
||||||
import getDomainFromUrl, { getDomain } from '../utils/getDomainFromUrl';
|
import getDomainFromUrl, { getDomain } from '../utils/getDomainFromUrl';
|
||||||
import { getSettings, Settings } from '../utils/settings';
|
import { getSettings, Settings } from '../utils/settings';
|
||||||
|
|
||||||
import config, { ExtensionStatus } from '../config/config';
|
import config, { ExtensionStatus } from '../config/config';
|
||||||
import { EntityType, Heartbeat } from '../types/heartbeats';
|
import { EntityType, Heartbeat, HeartbeatsBulkResponse } from '../types/heartbeats';
|
||||||
import { getApiKey } from '../utils/apiKey';
|
|
||||||
import { getLoggingType } from '../utils/logging';
|
|
||||||
|
|
||||||
class WakaTimeCore {
|
class WakaTimeCore {
|
||||||
tabsWithDevtoolsOpen: Tabs.Tab[];
|
tabsWithDevtoolsOpen: Tabs.Tab[];
|
||||||
@@ -32,17 +31,15 @@ class WakaTimeCore {
|
|||||||
const dbConnection = await openDB('wakatime', 1, {
|
const dbConnection = await openDB('wakatime', 1, {
|
||||||
upgrade(db, oldVersion) {
|
upgrade(db, oldVersion) {
|
||||||
// Create a store of objects
|
// Create a store of objects
|
||||||
const store = db.createObjectStore('cacheHeartbeats', {
|
const store = db.createObjectStore('heartbeatQueue', {
|
||||||
// The `time` property of the object will be the key, and be incremented automatically
|
keyPath: 'id',
|
||||||
keyPath: 'time',
|
|
||||||
});
|
});
|
||||||
// Switch over the oldVersion, *without breaks*, to allow the database to be incrementally upgraded.
|
// Switch over the oldVersion, *without breaks*, to allow the database to be incrementally upgraded.
|
||||||
switch (oldVersion) {
|
switch (oldVersion) {
|
||||||
case 0:
|
case 0:
|
||||||
// Placeholder to execute when database is created (oldVersion is 0)
|
// Placeholder to execute when database is created (oldVersion is 0)
|
||||||
case 1:
|
case 1:
|
||||||
// Create an index called `type` based on the `type` property of objects in the store
|
store.createIndex('id', 'id');
|
||||||
store.createIndex('time', 'time');
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -95,7 +92,7 @@ class WakaTimeCore {
|
|||||||
async handleActivity(tabId: number) {
|
async handleActivity(tabId: number) {
|
||||||
const settings = await getSettings();
|
const settings = await getSettings();
|
||||||
if (!settings.loggingEnabled) {
|
if (!settings.loggingEnabled) {
|
||||||
await changeExtensionStatus('notLogging');
|
await changeExtensionStatus('trackingDisabled');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +115,9 @@ class WakaTimeCore {
|
|||||||
if (!this.shouldSendHeartbeat(heartbeat)) return;
|
if (!this.shouldSendHeartbeat(heartbeat)) return;
|
||||||
|
|
||||||
// append heartbeat to queue
|
// append heartbeat to queue
|
||||||
await this.db?.add('cacheHeartbeats', heartbeat);
|
await this.db?.add('heartbeatQueue', heartbeat);
|
||||||
|
|
||||||
|
await this.sendHeartbeats();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCurrentTab(tabId: number): Promise<browser.Tabs.Tab | undefined> {
|
async getCurrentTab(tabId: number): Promise<browser.Tabs.Tab | undefined> {
|
||||||
@@ -145,63 +144,112 @@ class WakaTimeCore {
|
|||||||
|
|
||||||
const entity = settings.loggingType === 'domain' ? getDomainFromUrl(url) : url;
|
const entity = settings.loggingType === 'domain' ? getDomainFromUrl(url) : url;
|
||||||
return {
|
return {
|
||||||
branch: heartbeat?.branch,
|
branch: heartbeat?.branch ?? '<<LAST_BRANCH>>',
|
||||||
category: heartbeat?.category,
|
category: heartbeat?.category,
|
||||||
entity: heartbeat?.entity ?? entity,
|
entity: heartbeat?.entity ?? entity,
|
||||||
entityType: heartbeat?.entityType ?? (settings.loggingType as EntityType),
|
id: uuid4(),
|
||||||
language: heartbeat?.language,
|
language: heartbeat?.language,
|
||||||
project: heartbeat?.project,
|
project: heartbeat?.project ?? '<<LAST_PROJECT>>',
|
||||||
|
time: this.getCurrentTime(),
|
||||||
|
type: heartbeat?.entityType ?? (settings.loggingType as EntityType),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
getCurrentTime(): string {
|
||||||
* Given the heartbeat and logging type it creates a payload and
|
const m = moment();
|
||||||
* sends an ajax post request to the API.
|
return `${m.format('x').slice(0, -3)}.${m.format('x').slice(-3)}`;
|
||||||
*
|
}
|
||||||
* @param heartbeat
|
|
||||||
* @param debug
|
|
||||||
*/
|
|
||||||
async sendHeartbeat(
|
|
||||||
heartbeat: Heartbeat,
|
|
||||||
apiKey: string,
|
|
||||||
navigationPayload: Record<string, unknown>,
|
|
||||||
): Promise<void> {
|
|
||||||
console.log('Sending Heartbeat', heartbeat);
|
|
||||||
let payload;
|
|
||||||
|
|
||||||
const loggingType = await getLoggingType();
|
async sendHeartbeats(): Promise<void> {
|
||||||
// Get only the domain from the entity.
|
const settings = await browser.storage.sync.get({
|
||||||
// And send that in heartbeat
|
apiKey: config.apiKey,
|
||||||
if (loggingType == 'domain') {
|
apiUrl: config.apiUrl,
|
||||||
heartbeat.url = getDomainFromUrl(heartbeat.url);
|
heartbeatApiEndPoint: config.heartbeatApiEndPoint,
|
||||||
payload = await this.preparePayload(heartbeat, 'domain');
|
hostname: '',
|
||||||
await this.sendPostRequestToApi(
|
});
|
||||||
{ ...payload, ...navigationPayload },
|
if (!settings.apiKey) {
|
||||||
apiKey,
|
await changeExtensionStatus('notSignedIn');
|
||||||
heartbeat.hostname,
|
return;
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// Send entity in heartbeat
|
|
||||||
else if (loggingType == 'url') {
|
const heartbeats = (await this.db?.getAll('heartbeatQueue', undefined, 50)) as
|
||||||
payload = await this.preparePayload(heartbeat, 'url');
|
| Heartbeat[]
|
||||||
await this.sendPostRequestToApi(
|
| undefined;
|
||||||
{ ...payload, ...navigationPayload },
|
if (!heartbeats || heartbeats.length === 0) return;
|
||||||
apiKey,
|
|
||||||
heartbeat.hostname,
|
await this.db?.delete(
|
||||||
);
|
'heartbeatQueue',
|
||||||
|
heartbeats.map((heartbeat) => heartbeat.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
const userAgent = await this.getUserAgent();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const request: RequestInit = {
|
||||||
|
body: JSON.stringify(
|
||||||
|
heartbeats.map((heartbeat) => {
|
||||||
|
return { ...heartbeat, userAgent };
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
credentials: 'omit',
|
||||||
|
method: 'POST',
|
||||||
|
};
|
||||||
|
if (typeof settings.hostname === 'string' && settings.hostname) {
|
||||||
|
request.headers = {
|
||||||
|
'X-Machine-Name': settings.hostname,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = `${settings.apiUrl}${settings.heartbeatApiEndPoint}?api_key=${settings.apiKey}`;
|
||||||
|
const response = await fetch(url, request);
|
||||||
|
if (response.status === 401) {
|
||||||
|
await this.putHeartbeatsBackInQueue(heartbeats);
|
||||||
|
await changeExtensionStatus('notSignedIn');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = (await response.json()) as HeartbeatsBulkResponse;
|
||||||
|
if (data.error) {
|
||||||
|
await this.putHeartbeatsBackInQueue(heartbeats);
|
||||||
|
console.error(data.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (response.status === 201) {
|
||||||
|
await Promise.all(
|
||||||
|
(data.responses ?? []).map(async (resp, respNumber) => {
|
||||||
|
if (resp[0].error) {
|
||||||
|
await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber));
|
||||||
|
console.error(resp[0].error);
|
||||||
|
} else if (resp[1] === 201 && resp[0].data?.id) {
|
||||||
|
await changeExtensionStatus('allGood');
|
||||||
|
// await this.db?.delete('heartbeatQueue', resp[0].data.id);
|
||||||
|
} else {
|
||||||
|
if (resp[1] !== 400) {
|
||||||
|
await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber));
|
||||||
|
}
|
||||||
|
console.error(
|
||||||
|
`Heartbeat ${resp[0].data?.id ?? respNumber} returned status: ${resp[1]}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await this.putHeartbeatsBackInQueue(heartbeats);
|
||||||
|
console.error(`Heartbeat response status: ${response.status}`);
|
||||||
|
}
|
||||||
|
} catch (err: unknown) {
|
||||||
|
console.error(err);
|
||||||
|
await this.putHeartbeatsBackInQueue(heartbeats);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async putHeartbeatsBackInQueue(heartbeats: Heartbeat[]): Promise<void> {
|
||||||
* Creates payload for the heartbeat and returns it as JSON.
|
await Promise.all(
|
||||||
*
|
heartbeats.map(async (heartbeat) => this.db?.add('heartbeatQueue', heartbeat)),
|
||||||
* @param heartbeat
|
);
|
||||||
* @param type
|
}
|
||||||
* @param debug
|
|
||||||
* @returns {*}
|
async getUserAgent(): Promise<string> {
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
async preparePayload(heartbeat: Heartbeat, type: string): Promise<Record<string, unknown>> {
|
|
||||||
const os = await getOperatingSystem();
|
const os = await getOperatingSystem();
|
||||||
let browserName = 'chrome';
|
let browserName = 'chrome';
|
||||||
let userAgent;
|
let userAgent;
|
||||||
@@ -214,88 +262,7 @@ class WakaTimeCore {
|
|||||||
} else {
|
} else {
|
||||||
userAgent = navigator.userAgent.match(/Chrome\/\S+/g)?.[0];
|
userAgent = navigator.userAgent.match(/Chrome\/\S+/g)?.[0];
|
||||||
}
|
}
|
||||||
const payload: Record<string, unknown> = {
|
return `${userAgent} ${os} ${browserName}-wakatime/${config.version}`;
|
||||||
entity: heartbeat.url,
|
|
||||||
time: moment().format('X'),
|
|
||||||
type: type,
|
|
||||||
user_agent: `${userAgent} ${os} ${browserName}-wakatime/${config.version}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
payload.project = heartbeat.project ?? '<<LAST_PROJECT>>';
|
|
||||||
payload.branch = heartbeat.branch ?? '<<LAST_BRANCH>>';
|
|
||||||
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends AJAX request with payload to the heartbeat API as JSON.
|
|
||||||
*
|
|
||||||
* @param payload
|
|
||||||
* @param method
|
|
||||||
* @returns {*}
|
|
||||||
*/
|
|
||||||
async sendPostRequestToApi(
|
|
||||||
payload: Record<string, unknown>,
|
|
||||||
apiKey = '',
|
|
||||||
hostname = '',
|
|
||||||
): Promise<void> {
|
|
||||||
try {
|
|
||||||
const items = await browser.storage.sync.get({
|
|
||||||
apiUrl: config.apiUrl,
|
|
||||||
heartbeatApiEndPoint: config.heartbeatApiEndPoint,
|
|
||||||
});
|
|
||||||
|
|
||||||
const request: RequestInit = {
|
|
||||||
body: JSON.stringify(payload),
|
|
||||||
credentials: 'omit',
|
|
||||||
method: 'POST',
|
|
||||||
};
|
|
||||||
if (hostname) {
|
|
||||||
request.headers = {
|
|
||||||
'X-Machine-Name': hostname,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const response = await fetch(
|
|
||||||
`${items.apiUrl}${items.heartbeatApiEndPoint}?api_key=${apiKey}`,
|
|
||||||
request,
|
|
||||||
);
|
|
||||||
await response.json();
|
|
||||||
} catch (err: unknown) {
|
|
||||||
if (this.db) {
|
|
||||||
await this.db.add('cacheHeartbeats', payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
await changeExtensionStatus('notSignedIn');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends cached heartbeats request to wakatime api
|
|
||||||
* @param requests
|
|
||||||
*/
|
|
||||||
async sendCachedHeartbeatsRequest(): Promise<void> {
|
|
||||||
const apiKey = await getApiKey();
|
|
||||||
if (!apiKey) {
|
|
||||||
return changeExtensionStatus('notLogging');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.db) {
|
|
||||||
const requests = await this.db.getAll('cacheHeartbeats');
|
|
||||||
await this.db.clear('cacheHeartbeats');
|
|
||||||
const chunkSize = 50; // Create batches of max 50 request
|
|
||||||
for (let i = 0; i < requests.length; i += chunkSize) {
|
|
||||||
const chunk = requests.slice(i, i + chunkSize);
|
|
||||||
const requestsPromises: Promise<void>[] = [];
|
|
||||||
chunk.forEach((request: Record<string, unknown>) =>
|
|
||||||
requestsPromises.push(this.sendPostRequestToApi(request, apiKey)),
|
|
||||||
);
|
|
||||||
try {
|
|
||||||
await Promise.all(requestsPromises);
|
|
||||||
} catch (error: unknown) {
|
|
||||||
console.log('Error sending heartbeats');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ export interface Heartbeat {
|
|||||||
branch?: string | null;
|
branch?: string | null;
|
||||||
category?: Category | null;
|
category?: Category | null;
|
||||||
entity: string;
|
entity: string;
|
||||||
entityType: EntityType;
|
id: string;
|
||||||
language?: string | null;
|
language?: string | null;
|
||||||
project?: string | null;
|
project?: string | null;
|
||||||
|
time: string;
|
||||||
|
type: EntityType;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum Category {
|
export enum Category {
|
||||||
@@ -30,3 +32,7 @@ export interface ProjectDetails {
|
|||||||
language: string;
|
language: string;
|
||||||
project: string;
|
project: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type HeartbeatsBulkResponse = { error?: string; responses?: HeartbeatResponse[] };
|
||||||
|
|
||||||
|
export type HeartbeatResponse = [{ data?: { id: string }; error?: string }, number];
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ export enum KnownSite {
|
|||||||
github = 'github',
|
github = 'github',
|
||||||
gitlab = 'gitlab',
|
gitlab = 'gitlab',
|
||||||
googlemeet = 'googlemeet',
|
googlemeet = 'googlemeet',
|
||||||
|
slack = 'slack',
|
||||||
stackoverflow = 'stackoverflow',
|
stackoverflow = 'stackoverflow',
|
||||||
travisci = 'travisci',
|
travisci = 'travisci',
|
||||||
vercel = 'vercel',
|
vercel = 'vercel',
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
import browser from 'webextension-polyfill';
|
|
||||||
import config from '../config/config';
|
|
||||||
|
|
||||||
export default function apiKeyInvalid(key?: string): string {
|
export default function apiKeyInvalid(key?: string): string {
|
||||||
const err = 'Invalid api key... check https://wakatime.com/settings for your key';
|
const err = 'Invalid api key... check https://wakatime.com/settings for your key';
|
||||||
if (!key) return err;
|
if (!key) return err;
|
||||||
@@ -11,11 +8,3 @@ export default function apiKeyInvalid(key?: string): string {
|
|||||||
if (!re.test(key)) return err;
|
if (!re.test(key)) return err;
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getApiKey(): Promise<string> {
|
|
||||||
const storage = await browser.storage.sync.get({
|
|
||||||
apiKey: config.apiKey,
|
|
||||||
});
|
|
||||||
const apiKey = storage.apiKey as string;
|
|
||||||
return apiKey;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -13,16 +13,16 @@ export async function changeExtensionStatus(status: ExtensionStatus): Promise<vo
|
|||||||
await changeExtensionIcon(config.colors.allGood);
|
await changeExtensionIcon(config.colors.allGood);
|
||||||
await changeExtensionTooltip(config.tooltips.allGood);
|
await changeExtensionTooltip(config.tooltips.allGood);
|
||||||
break;
|
break;
|
||||||
case 'notLogging':
|
case 'trackingDisabled':
|
||||||
await changeExtensionIcon(config.colors.notLogging);
|
await changeExtensionIcon(config.colors.trackingDisabled);
|
||||||
await changeExtensionTooltip(config.tooltips.notLogging);
|
await changeExtensionTooltip(config.tooltips.trackingDisabled);
|
||||||
break;
|
break;
|
||||||
case 'notSignedIn':
|
case 'notSignedIn':
|
||||||
await changeExtensionIcon(config.colors.notSignedIn);
|
await changeExtensionIcon(config.colors.notSignedIn);
|
||||||
await changeExtensionTooltip(config.tooltips.notSignedIn);
|
await changeExtensionTooltip(config.tooltips.notSignedIn);
|
||||||
break;
|
break;
|
||||||
case 'ignored':
|
case 'ignored':
|
||||||
await changeExtensionIcon(config.colors.notLogging);
|
await changeExtensionIcon(config.colors.trackingDisabled);
|
||||||
await changeExtensionTooltip(config.tooltips.ignored);
|
await changeExtensionTooltip(config.tooltips.ignored);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export const logUserIn = async (apiKey: string): Promise<void> => {
|
|||||||
if (items.loggingEnabled === true) {
|
if (items.loggingEnabled === true) {
|
||||||
await changeExtensionState('allGood');
|
await changeExtensionState('allGood');
|
||||||
} else {
|
} else {
|
||||||
await changeExtensionState('notLogging');
|
await changeExtensionState('trackingDisabled');
|
||||||
}
|
}
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
await changeExtensionState('notSignedIn');
|
await changeExtensionState('notSignedIn');
|
||||||
@@ -121,7 +121,7 @@ export const fetchUserData = async (
|
|||||||
if (items.loggingEnabled === true) {
|
if (items.loggingEnabled === true) {
|
||||||
await changeExtensionState('allGood');
|
await changeExtensionState('allGood');
|
||||||
} else {
|
} else {
|
||||||
await changeExtensionState('notLogging');
|
await changeExtensionState('trackingDisabled');
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(setLoggingEnabled(items.loggingEnabled as boolean));
|
dispatch(setLoggingEnabled(items.loggingEnabled as boolean));
|
||||||
|
|||||||
Reference in New Issue
Block a user