@webext-core/proxy-service 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +54 -0
- package/lib/index.cjs +119 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +87 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Aaron
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# `@webext-core/proxy-service`
|
|
2
|
+
|
|
3
|
+
Defer service execution to the background service worker (or page).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
// todos-repo.ts
|
|
7
|
+
import idb from 'idb';
|
|
8
|
+
|
|
9
|
+
interface TodosRepo {
|
|
10
|
+
getOne: (id: string) => Promise<Todo>;
|
|
11
|
+
getAll: () => Promise<Todo[]>;
|
|
12
|
+
create: (todo: Todo) => Promise<void>;
|
|
13
|
+
update: (todo: Todo) => Promise<void>;
|
|
14
|
+
delete: (todo: Todo) => Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const [registerTodosRepo, getTodosRepo] = defineProxyService<TodosRepo>(
|
|
18
|
+
"TodosRepo",
|
|
19
|
+
(idb: Promise<IDBDatabase>) => ({
|
|
20
|
+
// Implement the `TodosRepo`
|
|
21
|
+
getOne: (id) => ...,
|
|
22
|
+
getAll: () => ...,
|
|
23
|
+
create: (todo) => ...,
|
|
24
|
+
update: (todo) => ...,
|
|
25
|
+
delete: (todo) => ...,
|
|
26
|
+
})
|
|
27
|
+
);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// background.ts
|
|
32
|
+
import {} from 'idb';
|
|
33
|
+
import { registerTodosRepo, getTodosRepo } from './todos-repo';
|
|
34
|
+
|
|
35
|
+
const idb = IndexedDB.open(...);
|
|
36
|
+
registerTodosRepo(idb);
|
|
37
|
+
|
|
38
|
+
// getTodosRepo will return the "real" service when in the background
|
|
39
|
+
const todosRepo = getTodosRepo();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// popup.ts
|
|
44
|
+
import { getTodosRepo } from 'todos-repo';
|
|
45
|
+
|
|
46
|
+
// getTodosRepo will return a "proxy" service when NOT in the background
|
|
47
|
+
const todosRepo = getTodosRepo();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Behind the scenes, the "proxy" service will message the background asking the registered service to perform all the real logic to execute a function.
|
|
51
|
+
|
|
52
|
+
Here, we're using `IndexedDB` to store Todos. Because standard web storage APIs aren't shared accross all JS contexts of your extension (popup `IndexedDB` does not contain any documents the background's `IndexedDB` contains), we need to always access the database from the background.
|
|
53
|
+
|
|
54
|
+
`@webext-core/proxy-service` is perfect for this!
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
var __async = (__this, __arguments, generator) => {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
var fulfilled = (value) => {
|
|
28
|
+
try {
|
|
29
|
+
step(generator.next(value));
|
|
30
|
+
} catch (e) {
|
|
31
|
+
reject(e);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var rejected = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.throw(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
42
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/index.ts
|
|
47
|
+
var src_exports = {};
|
|
48
|
+
__export(src_exports, {
|
|
49
|
+
defineProxyService: () => defineProxyService
|
|
50
|
+
});
|
|
51
|
+
module.exports = __toCommonJS(src_exports);
|
|
52
|
+
|
|
53
|
+
// src/isBackground.ts
|
|
54
|
+
var import_webextension_polyfill = __toESM(require("webextension-polyfill"), 1);
|
|
55
|
+
function isBackground() {
|
|
56
|
+
if (!canAccessExtensionApi())
|
|
57
|
+
return false;
|
|
58
|
+
const manifest = import_webextension_polyfill.default.runtime.getManifest();
|
|
59
|
+
if (!manifest.background)
|
|
60
|
+
return false;
|
|
61
|
+
return manifest.manifest_version === 3 ? isBackgroundServiceWorker() : isBackgroundPage();
|
|
62
|
+
}
|
|
63
|
+
function canAccessExtensionApi() {
|
|
64
|
+
var _a;
|
|
65
|
+
return !!((_a = import_webextension_polyfill.default.runtime) == null ? void 0 : _a.id);
|
|
66
|
+
}
|
|
67
|
+
var KNOWN_BACKGROUND_PAGE_PATHNAMES = [
|
|
68
|
+
"/_generated_background_page.html"
|
|
69
|
+
];
|
|
70
|
+
function isBackgroundPage() {
|
|
71
|
+
return typeof window !== "undefined" && KNOWN_BACKGROUND_PAGE_PATHNAMES.includes(location.pathname);
|
|
72
|
+
}
|
|
73
|
+
function isBackgroundServiceWorker() {
|
|
74
|
+
return typeof window === "undefined";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/defineProxyService.ts
|
|
78
|
+
var import_messaging = require("@webext-core/messaging");
|
|
79
|
+
function defineProxyService(name, init, config) {
|
|
80
|
+
let service;
|
|
81
|
+
const messageKey = `proxy-service.${name}`;
|
|
82
|
+
const { onMessage, sendMessage } = (0, import_messaging.defineExtensionMessaging)();
|
|
83
|
+
return [
|
|
84
|
+
function registerService(...args) {
|
|
85
|
+
service = init(...args);
|
|
86
|
+
onMessage(messageKey, ({ data }) => {
|
|
87
|
+
const method = service == null ? void 0 : service[data.method];
|
|
88
|
+
if (method)
|
|
89
|
+
return Promise.resolve(method.bind(service)(...data.args));
|
|
90
|
+
});
|
|
91
|
+
return service;
|
|
92
|
+
},
|
|
93
|
+
function getService() {
|
|
94
|
+
if (!isBackground())
|
|
95
|
+
return new Proxy({}, {
|
|
96
|
+
get(_, path) {
|
|
97
|
+
return (...args) => __async(this, null, function* () {
|
|
98
|
+
console.log(`Proxy${name}.${path}`, { args });
|
|
99
|
+
const res = yield sendMessage(messageKey, {
|
|
100
|
+
method: path,
|
|
101
|
+
args
|
|
102
|
+
});
|
|
103
|
+
return res;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
if (service == null) {
|
|
108
|
+
throw Error(
|
|
109
|
+
`Failed to get an instance of ${name}: in background, but registerService has not been called. Did you forget to call registerService?`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return service;
|
|
113
|
+
}
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
117
|
+
0 && (module.exports = {
|
|
118
|
+
defineProxyService
|
|
119
|
+
});
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ExtensionMessagingConfig } from '@webext-core/messaging';
|
|
2
|
+
|
|
3
|
+
declare type Proimsify<T> = T extends Promise<any> ? T : Promise<T>;
|
|
4
|
+
declare type Service = Record<string, any | ((...args: any[]) => any)>;
|
|
5
|
+
/**
|
|
6
|
+
* Make all functions of a Service/interface async.
|
|
7
|
+
*/
|
|
8
|
+
declare type AsyncService<TService> = {
|
|
9
|
+
[fn in keyof TService]: TService[fn] extends (...args: any[]) => any ? (...args: Parameters<TService[fn]>) => Proimsify<ReturnType<TService[fn]>> : TService[fn];
|
|
10
|
+
};
|
|
11
|
+
interface ProxyServiceConfig extends ExtensionMessagingConfig {
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare type RegisterService<TService extends Service, TArgs extends any[]> = (...args: TArgs) => TService;
|
|
15
|
+
declare type GetService<TService extends Service> = () => AsyncService<TService>;
|
|
16
|
+
declare function defineProxyService<TService extends Service, TArgs extends any[]>(name: string, init: (...args: TArgs) => TService, config?: ProxyServiceConfig): [RegisterService<TService, TArgs>, GetService<TService>];
|
|
17
|
+
|
|
18
|
+
export { ProxyServiceConfig, defineProxyService };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/isBackground.ts
|
|
23
|
+
import Browser from "webextension-polyfill";
|
|
24
|
+
function isBackground() {
|
|
25
|
+
if (!canAccessExtensionApi())
|
|
26
|
+
return false;
|
|
27
|
+
const manifest = Browser.runtime.getManifest();
|
|
28
|
+
if (!manifest.background)
|
|
29
|
+
return false;
|
|
30
|
+
return manifest.manifest_version === 3 ? isBackgroundServiceWorker() : isBackgroundPage();
|
|
31
|
+
}
|
|
32
|
+
function canAccessExtensionApi() {
|
|
33
|
+
var _a;
|
|
34
|
+
return !!((_a = Browser.runtime) == null ? void 0 : _a.id);
|
|
35
|
+
}
|
|
36
|
+
var KNOWN_BACKGROUND_PAGE_PATHNAMES = [
|
|
37
|
+
"/_generated_background_page.html"
|
|
38
|
+
];
|
|
39
|
+
function isBackgroundPage() {
|
|
40
|
+
return typeof window !== "undefined" && KNOWN_BACKGROUND_PAGE_PATHNAMES.includes(location.pathname);
|
|
41
|
+
}
|
|
42
|
+
function isBackgroundServiceWorker() {
|
|
43
|
+
return typeof window === "undefined";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/defineProxyService.ts
|
|
47
|
+
import { defineExtensionMessaging } from "@webext-core/messaging";
|
|
48
|
+
function defineProxyService(name, init, config) {
|
|
49
|
+
let service;
|
|
50
|
+
const messageKey = `proxy-service.${name}`;
|
|
51
|
+
const { onMessage, sendMessage } = defineExtensionMessaging();
|
|
52
|
+
return [
|
|
53
|
+
function registerService(...args) {
|
|
54
|
+
service = init(...args);
|
|
55
|
+
onMessage(messageKey, ({ data }) => {
|
|
56
|
+
const method = service == null ? void 0 : service[data.method];
|
|
57
|
+
if (method)
|
|
58
|
+
return Promise.resolve(method.bind(service)(...data.args));
|
|
59
|
+
});
|
|
60
|
+
return service;
|
|
61
|
+
},
|
|
62
|
+
function getService() {
|
|
63
|
+
if (!isBackground())
|
|
64
|
+
return new Proxy({}, {
|
|
65
|
+
get(_, path) {
|
|
66
|
+
return (...args) => __async(this, null, function* () {
|
|
67
|
+
console.log(`Proxy${name}.${path}`, { args });
|
|
68
|
+
const res = yield sendMessage(messageKey, {
|
|
69
|
+
method: path,
|
|
70
|
+
args
|
|
71
|
+
});
|
|
72
|
+
return res;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (service == null) {
|
|
77
|
+
throw Error(
|
|
78
|
+
`Failed to get an instance of ${name}: in background, but registerService has not been called. Did you forget to call registerService?`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return service;
|
|
82
|
+
}
|
|
83
|
+
];
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
defineProxyService
|
|
87
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webext-core/proxy-service",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Defer function execution to the background",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"web-extension",
|
|
8
|
+
"browser-extension",
|
|
9
|
+
"chrome-extension",
|
|
10
|
+
"webext",
|
|
11
|
+
"web-ext",
|
|
12
|
+
"web",
|
|
13
|
+
"chrome",
|
|
14
|
+
"browser",
|
|
15
|
+
"extension",
|
|
16
|
+
"background",
|
|
17
|
+
"proxy",
|
|
18
|
+
"service",
|
|
19
|
+
"webextension-polyfill"
|
|
20
|
+
],
|
|
21
|
+
"homepage": "https://github.com/aklinker1/webext-core/tree/main/packages/proxy-service",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/aklinker1/webext-core",
|
|
25
|
+
"directory": "packages/proxy-service"
|
|
26
|
+
},
|
|
27
|
+
"type": "module",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"lib"
|
|
33
|
+
],
|
|
34
|
+
"main": "lib/index.cjs",
|
|
35
|
+
"module": "lib/index.js",
|
|
36
|
+
"types": "lib/index.d.ts",
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"import": "./lib/index.js",
|
|
40
|
+
"require": "./lib/index.cjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@webext-core/messaging": "1.0.2",
|
|
45
|
+
"webextension-polyfill": "^0.10.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/webextension-polyfill": "^0.9.1",
|
|
49
|
+
"@vitest/coverage-c8": "^0.24.5",
|
|
50
|
+
"@webext-core/fake-browser": "1.0.1",
|
|
51
|
+
"tsconfig": "0.0.0",
|
|
52
|
+
"tsup": "^6.4.0",
|
|
53
|
+
"typescript": "^4.8.4",
|
|
54
|
+
"vitest": "^0.24.5"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"webextension-polyfill": "^0.10.0"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsup src/index.ts --clean --out-dir lib --format esm,cjs --dts",
|
|
61
|
+
"test": "vitest -r src",
|
|
62
|
+
"test:coverage": "vitest run -r src --coverage",
|
|
63
|
+
"compile": "tsc --noEmit"
|
|
64
|
+
}
|
|
65
|
+
}
|