Skip to content

Commit

Permalink
fix: tabs.sendMessage error handling (#38)
Browse files Browse the repository at this point in the history
fixed sendMessage error handling to properly display error notification when active tab is not aws console

* add permissions and logic to properly match aws tab url
* add outmatch lib to parse url globs
* use typescript manifests

closes #34
  • Loading branch information
janstuemmel authored Nov 21, 2022
1 parent c53c939 commit 8d7b61c
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 217 deletions.
25 changes: 18 additions & 7 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
"js-ini": "^1.5.1",
"marked": "^4.0.17",
"normalize.css": "^8.0.1",
"outmatch": "^0.7.0",
"pako": "^2.0.4",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@faker-js/faker": "^7.3.0",
"@types/chrome": "^0.0.193",
"@types/chrome": "^0.0.202",
"@types/jest": "^28.1.4",
"@types/lodash": "^4.14.182",
"@types/marked": "^4.0.3",
Expand Down
15 changes: 5 additions & 10 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import { build } from 'esbuild';

import { mergeManifestWithVersion } from '../src/build/plugins';
import { writeManifest } from '../src/build/plugins';
import {
pagesConfig,
scriptsConfig,
} from '../src/build/config';
import fs from 'fs';
import { version } from '../package.json';
import manifestFirefox from '../src/manifest.firefox';
import manifestChrome from '../src/manifest.chrome';

const watch = !!process.argv.includes('--watch');
const minify = !!process.argv.includes('--minify');
Expand All @@ -19,10 +20,7 @@ const result = build({
minify,
plugins: [
...pagesConfig.plugins || [],
mergeManifestWithVersion(version, [
'src/manifest.json',
'src/manifest.firefox.json'
]),
writeManifest(manifestFirefox),
],
outdir: 'dist/firefox',
});
Expand All @@ -39,10 +37,7 @@ build({
minify,
plugins: [
...pagesConfig.plugins || [],
mergeManifestWithVersion(version, [
'src/manifest.json',
'src/manifest.chrome.json'
]),
writeManifest(manifestChrome),
],
outdir: 'dist/chrome',
});
Expand Down
2 changes: 1 addition & 1 deletion src/build/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { mergeManifestWithVersion } from './mergeManifestsWithVersion';
export { writeManifest } from './writeManifest';
57 changes: 0 additions & 57 deletions src/build/plugins/mergeManifestsWithVersion.spec.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/build/plugins/mergeManifestsWithVersion.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/build/plugins/writeManifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Plugin } from 'esbuild';

import path from 'path';
import fs from 'fs';

export const writeManifest = (manifest: object): Plugin => ({
name: 'writeManifest',
setup: (build) => {
build.onEnd(async () => {
const json = JSON.stringify(manifest, null, 2);
await fs.promises.writeFile(
path.join(
process.cwd(),
build.initialOptions.outdir || '',
'manifest.json'
),
json,
);
});
},
});
4 changes: 2 additions & 2 deletions src/common/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export {
createTab,
getCurrentTabId,
sendToCurrentTab,
getCurrentTab,
sendToCurrentAwsConsoleTab,
} from './tabs';

export {
Expand Down
15 changes: 15 additions & 0 deletions src/common/browser/matchAwsConsoleUrl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import matchAwsConsoleUrl from './matchAwsConsoleUrl';

test.each([
[undefined, false],
['', false],
['https://console.aws.amazon.com/console/lambda', false],
['https://us-east-1.console.aws.amazon.com/console/lambda', true],
['https://us-east-1.console.aws.amazon.com/console/home?region=us-east-1', true],
['https://eu-central-1.console.aws.amazon.com/console/home?region=eu-central-1', true],
['https://phd.console.aws.amazon.com/console/home?region=eu-central-1', true],
['https://us-east-1.console.amazonaws-us-gov.com/console/home?region=ueu-central-1', true],
['https://us-east-1.console.amazonaws.cn/console/home', true],
])('test matchAwsConsoleUrl "%s"', (url, expected) => {
expect(matchAwsConsoleUrl(url)).toEqual(expected);
});
7 changes: 7 additions & 0 deletions src/common/browser/matchAwsConsoleUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import outmatch from 'outmatch';

import awsConsoleUrls from '../const/awsConsoleUrls';

export default (url: string | undefined) => {
return outmatch(awsConsoleUrls, { separator: '.' })(url || '');
};
27 changes: 14 additions & 13 deletions src/common/browser/tabs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import matchAwsConsoleUrl from "./matchAwsConsoleUrl";

type Callback = () => void;

export const createTab = async (
Expand All @@ -6,22 +8,21 @@ export const createTab = async (
cb: Callback = () => {}
) => chrome.tabs.create({ active, url }, cb);

export const getCurrentTabId = async () => {
const tab = await new Promise((res: (tabs: chrome.tabs.Tab[]) => void) =>
export const getCurrentTab = async () => {
const tabs = await new Promise((res: (tabs: chrome.tabs.Tab[]) => void) =>
chrome.tabs.query({ active: true, currentWindow: true }, res));

if (tab[0] && tab[0].id) {
return tab[0].id;
}
throw new Error('could not get current tab id');
return tabs[0];
};

export const sendToCurrentTab = async (
message: Message,
cb: Callback = () => {}
) => {
const id = await getCurrentTabId();
return chrome.tabs.sendMessage(id, message, cb);
export const sendToCurrentAwsConsoleTab = async (message: Message) => {
const tab = await getCurrentTab();

// only send to aws console tabs
if (tab.id && matchAwsConsoleUrl(tab.url)) {
return chrome.tabs.sendMessage(tab.id, message);
}

throw new Error('active tab not an aws console');
};

export const updateTabUrl = async (url: string) => {
Expand Down
6 changes: 6 additions & 0 deletions src/common/const/awsConsoleUrls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default [
'https://*.console.aws.amazon.com/*',
'https://phd.aws.amazon.com/*',
'https://*.console.amazonaws-us-gov.com/*',
'https://*.console.amazonaws.cn/*'
];
22 changes: 0 additions & 22 deletions src/manifest.chrome.json

This file was deleted.

32 changes: 32 additions & 0 deletions src/manifest.chrome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import common from './manifest.common';
import awsConsoleUrls from './common/const/awsConsoleUrls';

export default {
...common,
manifest_version: 3,
action: {
default_title: "AWS role switch",
default_popup: "popup/popup.html",
browser_style: true,
default_icon: {
128: "assets/icon128.png"
}
},
permissions: [
"storage"
],
host_permissions: [
...awsConsoleUrls,
],
commands: {
_execute_action: {
description: "Open the popup window",
suggested_key: {
default: "Ctrl+Shift+L"
}
}
},
background: {
service_worker: "background/background.js"
},
};
Loading

0 comments on commit 8d7b61c

Please sign in to comment.