Skip to content

Commit

Permalink
V0.3.28 (#89)
Browse files Browse the repository at this point in the history
* fix: request/response interceptor - match all urls

* feat: add testName to session data

* fix(manifest): content script match <all_urls>

* fix: backend API change - SG fields & types naming

* feat(js-coverage-recorder): add url to hash map

* fix: getScriptSource failed console warning

* chore: bump version to 0.3.28
  • Loading branch information
RomanDavlyatshin authored Feb 10, 2021
1 parent 3082cea commit c6ae630
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@drill4j/browser-extension",
"version": "0.3.27",
"version": "0.3.28",
"license": "Apache-2.0",
"scripts": {
"build": "NODE_ENV=production webpack",
Expand Down
30 changes: 16 additions & 14 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ async function init() {

// eslint-disable-next-line no-param-reassign
interceptedDataStore.agents = {};
const agentOrGroupHandler = async (id: string, host: string) => {
const agentOrGroupHandler = async (id: string, url: string) => {
const host = transformHost(url);

// eslint-disable-next-line no-param-reassign
interceptedDataStore.agents[id] = host;

Expand Down Expand Up @@ -244,7 +246,7 @@ async function init() {
if (!adapter) throw new Error('Backend connection unavailable');
// TODO !sessionsData[host] check?
try {
await adapter.stopTest(sessionsData[host].sessionId, sender);
await adapter.stopTest(sessionsData[host].sessionId, sessionsData[host].testName, sender);
sessionsData[host] = {
...sessionsData[host],
status: SessionStatus.STOPPED,
Expand Down Expand Up @@ -371,7 +373,7 @@ function setupResponseInterceptors(interceptedDataStore: Record<string, any>) {

function agentAdaptersReducer(agentsList: any, agentsHosts: Record<string, string>): AdapterInfo[] {
return agentsList
.filter((x: any) => !x.serviceGroup)
.filter((x: any) => !x.group)
.map((x: any) => ({
adapterType: 'agents',
id: x.id,
Expand All @@ -389,14 +391,14 @@ function agentAdaptersReducer(agentsList: any, agentsHosts: Record<string, strin

function sgAdaptersReducer(agentsList: any, agentsHosts: Record<string, string>): AdapterInfo[] {
const sgAdaptersInfoMap: Record<string, AdapterInfo> = agentsList
.filter((x: any) => x.serviceGroup)
.filter((x: any) => x.group)
.reduce((a: any, x: any) => {
if (!a[x.serviceGroup]) {
if (!a[x.group]) {
// eslint-disable-next-line no-param-reassign
a[x.serviceGroup] = {
adapterType: 'service-groups',
id: x.serviceGroup,
host: transformHost(x.systemSettings?.targetHost) || (agentsHosts && agentsHosts[x.serviceGroup]),
a[x.group] = {
adapterType: 'groups',
id: x.group,
host: transformHost(x.systemSettings?.targetHost) || (agentsHosts && agentsHosts[x.group]),
// TODO think what to do with the SG status
status: x.status,
buildVersion: x.buildVersion,
Expand All @@ -406,11 +408,11 @@ function sgAdaptersReducer(agentsList: any, agentsHosts: Record<string, string>)

if (x.agentType.toLowerCase() === AgentType.JAVA_SCRIPT) {
// eslint-disable-next-line no-param-reassign
a[x.serviceGroup].mustRecordJsCoverage = true;
a[x.group].mustRecordJsCoverage = true;
}
if (!a[x.serviceGroup].host) {
if (!a[x.group].host) {
// eslint-disable-next-line no-param-reassign
a[x.serviceGroup].host = transformHost(x.systemSettings?.targetHost);
a[x.group].host = transformHost(x.systemSettings?.targetHost);
}
return a;
}, {});
Expand Down Expand Up @@ -457,10 +459,10 @@ function createAdapter(adapterInfo: AdapterInfo, backend: BackendCreator): Agent
const sessionId = await backendApi.startTest(testName);
return sessionId;
},
stopTest: async (sessionId, sender) => {
stopTest: async (sessionId, testName, sender) => {
if (!sender) throw new Error('STOP_TEST_NO_SENDER');
const data = await jsCoverageRecorder.stop(sender);
await backendApi.addSessionData(sessionId, data);
await backendApi.addSessionData(sessionId, { ...data, testName });
await backendApi.stopTest(sessionId);
},
cancelTest: async (sessionId, sender) => {
Expand Down
23 changes: 17 additions & 6 deletions src/background/js-coverage-recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ async function start(sender: chrome.runtime.MessageSender) {
detailed: true,
});

// FIXME tabId undefined checks
scriptSources[sender?.tab?.id as any] = {
hashToUrl: {},
urlToHash: {},
};
chrome.debugger.onEvent.addListener(async (source, method, params) => {
if (method !== 'Debugger.scriptParsed') {
return;
Expand All @@ -46,17 +51,23 @@ async function start(sender: chrome.runtime.MessageSender) {
return;
}

const rawScriptSource: any = await devToolsApi.sendCommand(target, 'Debugger.getScriptSource', { scriptId });
let rawScriptSource: any;
try {
rawScriptSource = await devToolsApi.sendCommand(target, 'Debugger.getScriptSource', { scriptId });
} catch (e) {
console.log(`%cWARNING%c: scriptId ${scriptId} getScriptSource(...) failed: ${e?.message || JSON.stringify(e)}`,
'background-color: yellow;',
'background-color: unset;');
return;
}

const hash = getHash(unifyLineEndings(rawScriptSource.scriptSource));
// TODO check source hashes agains expected build hashes
// what about filenames?

// FIXME tabId undefined checks
if (!scriptSources[sender?.tab?.id as any]) {
scriptSources[sender?.tab?.id as any] = {};
}
// FIXME #1 either clear scriptSources on start/stop or store those by sender?.tab?.id / host
scriptSources[sender?.tab?.id as any][hash] = url;
scriptSources[sender?.tab?.id as any].hashToUrl[hash] = url;
scriptSources[sender?.tab?.id as any].urlToHash[url] = hash;
});

await devToolsApi.sendCommand(target, 'Debugger.enable', {});
Expand Down
6 changes: 3 additions & 3 deletions src/background/request-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { SessionStatus } from '../common/enums';
import { SessionData } from './types';

export function setupRequestInterceptor(sessionsStorage: Record<string, SessionData>) {
const interceptor = ({ requestHeaders = [], initiator = '' }: WebRequest.OnBeforeSendHeadersDetailsType & { initiator?: string}) => {
const host = transformHost(initiator);
const interceptor = ({ requestHeaders = [], url }: WebRequest.OnBeforeSendHeadersDetailsType) => {
const host = transformHost(url);
if (!host) return { requestHeaders };

const session = sessionsStorage[host];
Expand All @@ -18,7 +18,7 @@ export function setupRequestInterceptor(sessionsStorage: Record<string, SessionD
browser.webRequest.onBeforeSendHeaders.addListener(
interceptor,
{
urls: ['*://*/*'],
urls: ['<all_urls>'],
},
['blocking', 'requestHeaders'],
);
Expand Down
11 changes: 7 additions & 4 deletions src/background/response-interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { browser, WebRequest } from 'webextension-polyfill-ts';

export function setupResponseInterceptor() {
const interceptor = ({ responseHeaders = [], initiator = '' }: WebRequest.OnHeadersReceivedDetailsType & { initiator?: string }) => {
const interceptor = ({ responseHeaders = [], url }: WebRequest.OnHeadersReceivedDetailsType) => {
headerHandlers.forEach(handler => {
const matchedHeader = responseHeaders.find(x => x.name.toLowerCase() === handler.name.toLowerCase());
if (matchedHeader) handler.cb(matchedHeader.value, initiator);
if (matchedHeader) handler.cb(matchedHeader.value, url);
});
};

Expand All @@ -13,7 +13,10 @@ export function setupResponseInterceptor() {
browser.webRequest.onHeadersReceived.addListener(
interceptor,
{
urls: ['*://*/*'], // TODO that could be narrowed with user-defined settings ?
// why not urls: ['*://*/*'],
// <all_urls> - matches all responses, including those that have "Initiator - Other" in DevTools/Network
// reference https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns#%3Call_urls%3E
urls: ['<all_urls>'],
},
['responseHeaders'],
);
Expand All @@ -40,4 +43,4 @@ type HeaderHandler = {
cb: HeaderHandlerCb;
}

type HeaderHandlerCb = (value: any, initiator: string) => any;
type HeaderHandlerCb = (value: any, url: string) => any;
4 changes: 2 additions & 2 deletions src/background/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Routes {

export interface AgentAdapter {
startTest: (testName: string, sender?: chrome.runtime.MessageSender) => Promise<string>;
stopTest: (sessionId: string, sender?: chrome.runtime.MessageSender) => Promise<void>;
stopTest: (sessionId: string, testName?: string, sender?: chrome.runtime.MessageSender) => Promise<void>;
cancelTest: (sessionId: string, sender?: chrome.runtime.MessageSender) => Promise<void>;
}

Expand All @@ -32,7 +32,7 @@ export type SessionData = {
export type ScopeData = Record<string, any>; // TODO type it properly!

export type SubNotifyFunction = (data: unknown) => void;
export type AdapterType = 'agents' | 'service-groups';
export type AdapterType = 'agents' | 'groups';

export interface AdapterInfo {
adapterType: AdapterType;
Expand Down
4 changes: 2 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Drill4J Browser Extension",
"version": "0.3.27",
"version": "0.3.28",
"background": {
"page": "background.html",
"persistent": true
Expand All @@ -24,7 +24,7 @@
},
"content_scripts": [
{
"matches": ["*://*/*"],
"matches": ["<all_urls>"],
"css": ["content.css"],
"js": ["content.bundle.js"]
}
Expand Down

0 comments on commit c6ae630

Please sign in to comment.