@webext-core/proxy-service 1.0.0 → 1.0.2

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/README.md CHANGED
@@ -1,12 +1,66 @@
1
1
  # `@webext-core/proxy-service`
2
2
 
3
- Defer service execution to the background service worker (or page).
3
+ Defer function execution to the background service worker (or page).
4
+
5
+ ```bash
6
+ pnpm i @webext-core/proxy-service
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ 1. Define the service that will be executed in the background:
12
+
13
+ ###### MathService.ts
14
+
15
+ ```ts
16
+ class MathService {
17
+ factorial(x) {
18
+ return x === 1 ? x : this.factorial(x - 1);
19
+ }
20
+ }
21
+
22
+ export const [registerMathService, getMathService] = defineProxyService(
23
+ 'MathService',
24
+ () => new MathService(),
25
+ );
26
+ ```
27
+
28
+ 2. Register your new service at the beginning of your background script:
29
+
30
+ ###### background.ts
31
+
32
+ ```ts
33
+ import { registerMathService } from './MathService';
34
+
35
+ registerMathService();
36
+ ```
37
+
38
+ 3. Use `getService` to get a proxied instance of your service, and call methods on it like you would with the real service.
39
+
40
+ ###### popup.ts
41
+
42
+ ```ts
43
+ import { getMathService } from './MathService';
44
+
45
+ // getMathService will return a "proxy" service when NOT in the background
46
+ const mathService = getMathService();
47
+
48
+ // Use the proxy just like you would the real service!
49
+ await mathService.factorial(100);
50
+ ```
51
+
52
+ ### Another Example: IndexedDB
53
+
54
+ Since the same IndexedDB database is not available in every JS context of an extension, it's common to use the IndexedDB instance in the background script as a database for browser extensions.
55
+
56
+ `@webext-core/proxy-service` is perfect for this! Here we're using [`idb`](https://www.npmjs.com/package/idb) to simplify it's usage.
57
+
58
+ ###### TodoRepo.ts
4
59
 
5
60
  ```ts
6
- // todos-repo.ts
7
- import idb from 'idb';
61
+ import { IDBPDatabase } from 'idb';
8
62
 
9
- interface TodosRepo {
63
+ interface ITodoRepo {
10
64
  getOne: (id: string) => Promise<Todo>;
11
65
  getAll: () => Promise<Todo[]>;
12
66
  create: (todo: Todo) => Promise<void>;
@@ -14,41 +68,40 @@ interface TodosRepo {
14
68
  delete: (todo: Todo) => Promise<void>;
15
69
  }
16
70
 
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
- );
71
+ function createIdbTodoRepo(idb: Promise<IDBPDatabase>): ITodoRepo {
72
+ return {
73
+ getOne: async id => (await idb).get('todos', id),
74
+ getAll: async () => (await idb).getAll('todos'),
75
+ create: async todo => (await idb).add('todos', todo),
76
+ update: async todo => (await idb).put('todos', todo),
77
+ delete: async todo => (await idb).delete('todos', todo.id),
78
+ };
79
+ }
80
+
81
+ const [registerTodoRepo, getTodoRepo] = defineProxyService('TodoRepo', createIdbTodoRepo);
28
82
  ```
29
83
 
84
+ When registering a service with a dependency, you need to pass that dependency into the `registerService` method:
85
+
86
+ ###### background.ts
87
+
30
88
  ```ts
31
- // background.ts
32
- import {} from 'idb';
33
- import { registerTodosRepo, getTodosRepo } from './todos-repo';
89
+ import { registerTodoRepo } from './TodoRepo';
90
+ import { openDB } from 'idb';
34
91
 
35
- const idb = IndexedDB.open(...);
36
- registerTodosRepo(idb);
92
+ const idb = openDB(...);
37
93
 
38
- // getTodosRepo will return the "real" service when in the background
39
- const todosRepo = getTodosRepo();
94
+ registerTodoRepo(idb);
40
95
  ```
41
96
 
42
- ```ts
43
- // popup.ts
44
- import { getTodosRepo } from 'todos-repo';
97
+ Then you can use `getService` to get a instance of the service anywhere thoughout your extension!
45
98
 
46
- // getTodosRepo will return a "proxy" service when NOT in the background
47
- const todosRepo = getTodosRepo();
48
- ```
99
+ ###### popup.ts
49
100
 
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.
101
+ ```ts
102
+ import { getTodoRepo } from './TodoRepo';
51
103
 
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.
104
+ const todoRepo = getTodoRepo();
53
105
 
54
- `@webext-core/proxy-service` is perfect for this!
106
+ const todos = await todoRepo.getAll();
107
+ ```
package/lib/index.cjs CHANGED
@@ -79,7 +79,7 @@ var import_messaging = require("@webext-core/messaging");
79
79
  function defineProxyService(name, init, config) {
80
80
  let service;
81
81
  const messageKey = `proxy-service.${name}`;
82
- const { onMessage, sendMessage } = (0, import_messaging.defineExtensionMessaging)();
82
+ const { onMessage, sendMessage } = (0, import_messaging.defineExtensionMessaging)(config);
83
83
  return [
84
84
  function registerService(...args) {
85
85
  service = init(...args);
@@ -95,7 +95,6 @@ function defineProxyService(name, init, config) {
95
95
  return new Proxy({}, {
96
96
  get(_, path) {
97
97
  return (...args) => __async(this, null, function* () {
98
- console.log(`Proxy${name}.${path}`, { args });
99
98
  const res = yield sendMessage(messageKey, {
100
99
  method: path,
101
100
  args
package/lib/index.d.ts CHANGED
@@ -13,6 +13,6 @@ interface ProxyServiceConfig extends ExtensionMessagingConfig {
13
13
 
14
14
  declare type RegisterService<TService extends Service, TArgs extends any[]> = (...args: TArgs) => TService;
15
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>];
16
+ declare function defineProxyService<TService extends Service, TArgs extends any[]>(name: string, init: (...args: TArgs) => TService, config?: ProxyServiceConfig): [registerService: RegisterService<TService, TArgs>, getService: GetService<TService>];
17
17
 
18
18
  export { ProxyServiceConfig, defineProxyService };
package/lib/index.js CHANGED
@@ -48,7 +48,7 @@ import { defineExtensionMessaging } from "@webext-core/messaging";
48
48
  function defineProxyService(name, init, config) {
49
49
  let service;
50
50
  const messageKey = `proxy-service.${name}`;
51
- const { onMessage, sendMessage } = defineExtensionMessaging();
51
+ const { onMessage, sendMessage } = defineExtensionMessaging(config);
52
52
  return [
53
53
  function registerService(...args) {
54
54
  service = init(...args);
@@ -64,7 +64,6 @@ function defineProxyService(name, init, config) {
64
64
  return new Proxy({}, {
65
65
  get(_, path) {
66
66
  return (...args) => __async(this, null, function* () {
67
- console.log(`Proxy${name}.${path}`, { args });
68
67
  const res = yield sendMessage(messageKey, {
69
68
  method: path,
70
69
  args
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webext-core/proxy-service",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Defer function execution to the background",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -41,23 +41,24 @@
41
41
  }
42
42
  },
43
43
  "dependencies": {
44
- "@webext-core/messaging": "1.0.2",
45
- "webextension-polyfill": "^0.10.0"
44
+ "webextension-polyfill": "^0.10.0",
45
+ "@webext-core/messaging": "1.1.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/webextension-polyfill": "^0.9.1",
49
49
  "@vitest/coverage-c8": "^0.24.5",
50
- "@webext-core/fake-browser": "1.0.1",
51
- "tsconfig": "0.0.0",
52
50
  "tsup": "^6.4.0",
53
51
  "typescript": "^4.8.4",
54
- "vitest": "^0.24.5"
52
+ "vitest": "^0.24.5",
53
+ "@webext-core/fake-browser": "1.0.2",
54
+ "tsconfig": "0.0.0"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "webextension-polyfill": "^0.10.0"
58
58
  },
59
59
  "scripts": {
60
60
  "build": "tsup src/index.ts --clean --out-dir lib --format esm,cjs --dts",
61
+ "build:dependencies": "cd ../.. && turbo run build --filter=@webext-core/proxy-service^...",
61
62
  "test": "vitest -r src",
62
63
  "test:coverage": "vitest run -r src --coverage",
63
64
  "compile": "tsc --noEmit"