@cyia/chrome-trpc 1.0.2 → 1.0.3
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/package.json +1 -1
- package/readme.md +22 -17
- package/readme.zh-hans.md +14 -11
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -37,18 +37,20 @@ Define your router in a shared `router.ts` file using `@trpc/server` so that bot
|
|
|
37
37
|
|
|
38
38
|
```typescript
|
|
39
39
|
import { initTRPC } from '@trpc/server';
|
|
40
|
-
import
|
|
40
|
+
import * as v from 'valibot';
|
|
41
41
|
|
|
42
42
|
const t = initTRPC.create();
|
|
43
43
|
|
|
44
44
|
export const appRouter = t.router({
|
|
45
|
-
greet: t.procedure.input(
|
|
45
|
+
greet: t.procedure.input(v.string()).query(({ input }) => `Hello ${input}`),
|
|
46
46
|
getData: t.procedure.query(() => ({ payload: new Uint8Array([1, 2, 3]) })),
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
export type AppRouter = typeof appRouter;
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
- If used in a Content Script, you need to set `isServer`: `const t = initTRPC.create({ isServer: true });`
|
|
53
|
+
|
|
52
54
|
### 2. Server (receiving end)
|
|
53
55
|
|
|
54
56
|
In the script that provides the service (e.g., background or content script), use `createChromeHandler` to listen for connections from other parts.
|
|
@@ -90,10 +92,10 @@ console.log(greeting); // "Hello World"
|
|
|
90
92
|
|
|
91
93
|
## 🔌 Three Communication Modes Explained
|
|
92
94
|
|
|
93
|
-
| Direction
|
|
94
|
-
|
|
|
95
|
-
| **Popup → Background** | Popup | Background | `chrome.runtime.connect()`
|
|
96
|
-
| **Content Script → Background** | Content Script | Background | `chrome.runtime.connect()`
|
|
95
|
+
| Direction | Client (caller) | Server (receiver) | How to obtain the `port` |
|
|
96
|
+
| ------------------------------- | --------------- | ----------------- | --------------------------------------------------------------------------------- |
|
|
97
|
+
| **Popup → Background** | Popup | Background | `chrome.runtime.connect()` |
|
|
98
|
+
| **Content Script → Background** | Content Script | Background | `chrome.runtime.connect()` |
|
|
97
99
|
| **Popup → Content Script** | Popup | Content Script | First get the tabId via `chrome.tabs.query`,<br>then `chrome.tabs.connect(tabId)` |
|
|
98
100
|
|
|
99
101
|
### Example: Popup calling Content Script
|
|
@@ -112,24 +114,27 @@ createChromeHandler({ router: appRouter });
|
|
|
112
114
|
import { chromeLink } from '@cyia/chrome-trpc/client';
|
|
113
115
|
import { createTRPCProxyClient } from '@trpc/client';
|
|
114
116
|
import type { AppRouter } from './router';
|
|
115
|
-
|
|
117
|
+
// when popup to Content Script
|
|
116
118
|
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
119
|
+
const port = chrome.tabs.connect(tab.id);
|
|
120
|
+
// when popup/Content Script to Background
|
|
121
|
+
const port = chrome.runtime.connect();
|
|
122
|
+
|
|
123
|
+
const trpc = createTRPCProxyClient<AppRouter>({
|
|
124
|
+
links: [chromeLink({ port })],
|
|
125
|
+
});
|
|
126
|
+
const data = await trpc.getData.query();
|
|
124
127
|
```
|
|
125
128
|
|
|
129
|
+
|
|
130
|
+
|
|
126
131
|
## 📦 Binary Compression
|
|
127
132
|
|
|
128
133
|
The adapter uses the [Snappy](https://github.com/zhipeng-jia/snappyjs) algorithm to compress and decompress `Uint8Array` data, significantly reducing the transfer size.
|
|
129
134
|
|
|
130
135
|
### Response Compression (server configuration)
|
|
131
136
|
|
|
132
|
-
In some scenarios you may want to explicitly specify which response paths should be compressed, or a procedure may return data that is not a plain `Uint8Array` but is still large.
|
|
137
|
+
In some scenarios you may want to explicitly specify which response paths should be compressed, or a procedure may return data that is not a plain `Uint8Array` but is still large.
|
|
133
138
|
In such cases you can pass the `compressPathObj` option to `createChromeHandler`. The keys are the procedure route paths, and a value of `true` enables compression.
|
|
134
139
|
|
|
135
140
|
For example, suppose you extended your router with a `res` sub-router and a `cmp` query:
|
|
@@ -137,7 +142,7 @@ For example, suppose you extended your router with a `res` sub-router and a `cmp
|
|
|
137
142
|
```typescript
|
|
138
143
|
// router.ts extended
|
|
139
144
|
export const appRouter = t.router({
|
|
140
|
-
greet: t.procedure.input(
|
|
145
|
+
greet: t.procedure.input(v.string()).query(({ input }) => `Hello ${input}`),
|
|
141
146
|
getData: t.procedure.query(() => ({ payload: new Uint8Array([1, 2, 3]) })),
|
|
142
147
|
res: t.router({
|
|
143
148
|
cmp: t.procedure.query(() => {
|
|
@@ -145,7 +150,7 @@ export const appRouter = t.router({
|
|
|
145
150
|
}),
|
|
146
151
|
}),
|
|
147
152
|
});
|
|
148
|
-
|
|
153
|
+
|
|
149
154
|
|
|
150
155
|
Server configuration compressing the path `'res.cmp'`:
|
|
151
156
|
|
package/readme.zh-hans.md
CHANGED
|
@@ -37,18 +37,20 @@ npm install @cyia/chrome-trpc @trpc/client @trpc/server
|
|
|
37
37
|
|
|
38
38
|
```typescript
|
|
39
39
|
import { initTRPC } from '@trpc/server';
|
|
40
|
-
import
|
|
40
|
+
import * as v from 'valibot';
|
|
41
41
|
|
|
42
42
|
const t = initTRPC.create();
|
|
43
43
|
|
|
44
44
|
export const appRouter = t.router({
|
|
45
|
-
greet: t.procedure.input(
|
|
45
|
+
greet: t.procedure.input(v.string()).query(({ input }) => `Hello ${input}`),
|
|
46
46
|
getData: t.procedure.query(() => ({ payload: new Uint8Array([1, 2, 3]) })),
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
export type AppRouter = typeof appRouter;
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
- 如果在Content Script中使用,需要设置isServer `const t = initTRPC.create({ isServer: true });`
|
|
53
|
+
|
|
52
54
|
### 2. 服务端(接收端)
|
|
53
55
|
|
|
54
56
|
在需要提供服务的脚本(如 background 或 content script)中,使用 `createChromeHandler` 监听来自其他部分的连接。
|
|
@@ -112,15 +114,16 @@ createChromeHandler({ router: appRouter });
|
|
|
112
114
|
import { chromeLink } from '@cyia/chrome-trpc/client';
|
|
113
115
|
import { createTRPCProxyClient } from '@trpc/client';
|
|
114
116
|
import type { AppRouter } from './router';
|
|
115
|
-
|
|
117
|
+
// popup 到 Content Script 时
|
|
116
118
|
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
119
|
+
const port = chrome.tabs.connect(tab.id);
|
|
120
|
+
// popup/Content Script 到 Background 时
|
|
121
|
+
const port = chrome.runtime.connect();
|
|
122
|
+
|
|
123
|
+
const trpc = createTRPCProxyClient<AppRouter>({
|
|
124
|
+
links: [chromeLink({ port })],
|
|
125
|
+
});
|
|
126
|
+
const data = await trpc.getData.query();
|
|
124
127
|
```
|
|
125
128
|
|
|
126
129
|
## 📦 二进制压缩
|
|
@@ -137,7 +140,7 @@ if (tab.id) {
|
|
|
137
140
|
```typescript
|
|
138
141
|
// router.ts 扩展
|
|
139
142
|
export const appRouter = t.router({
|
|
140
|
-
greet: t.procedure.input(
|
|
143
|
+
greet: t.procedure.input(v.string()).query(({ input }) => `Hello ${input}`),
|
|
141
144
|
getData: t.procedure.query(() => ({ payload: new Uint8Array([1, 2, 3]) })),
|
|
142
145
|
res: t.router({
|
|
143
146
|
cmp: t.procedure.query(() => {
|