@wechatbot/wechatbot 2.2.0 → 2.3.1
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 +95 -1
- package/dist/auth/authenticator.d.ts +5 -0
- package/dist/auth/authenticator.d.ts.map +1 -1
- package/dist/auth/authenticator.js +15 -2
- package/dist/auth/authenticator.js.map +1 -1
- package/dist/auth/types.d.ts +2 -1
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/core/client.d.ts +55 -3
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/client.js +98 -42
- package/dist/core/client.js.map +1 -1
- package/dist/core/errors.d.ts +6 -0
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +8 -0
- package/dist/core/errors.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/media/downloader.d.ts +3 -1
- package/dist/media/downloader.d.ts.map +1 -1
- package/dist/media/downloader.js +5 -2
- package/dist/media/downloader.js.map +1 -1
- package/dist/media/index.d.ts +1 -1
- package/dist/media/index.d.ts.map +1 -1
- package/dist/media/index.js +1 -1
- package/dist/media/index.js.map +1 -1
- package/dist/media/markdown.d.ts +12 -0
- package/dist/media/markdown.d.ts.map +1 -1
- package/dist/media/markdown.js +17 -0
- package/dist/media/markdown.js.map +1 -1
- package/dist/media/remote.d.ts +3 -0
- package/dist/media/remote.d.ts.map +1 -1
- package/dist/media/remote.js +3 -1
- package/dist/media/remote.js.map +1 -1
- package/dist/media/uploader.d.ts +3 -1
- package/dist/media/uploader.d.ts.map +1 -1
- package/dist/media/uploader.js +5 -2
- package/dist/media/uploader.js.map +1 -1
- package/dist/message/cache.d.ts +30 -0
- package/dist/message/cache.d.ts.map +1 -0
- package/dist/message/cache.js +53 -0
- package/dist/message/cache.js.map +1 -0
- package/dist/message/index.d.ts +2 -0
- package/dist/message/index.d.ts.map +1 -1
- package/dist/message/index.js +2 -0
- package/dist/message/index.js.map +1 -1
- package/dist/message/parser.d.ts.map +1 -1
- package/dist/message/parser.js +10 -2
- package/dist/message/parser.js.map +1 -1
- package/dist/message/partial-quote.d.ts +15 -0
- package/dist/message/partial-quote.d.ts.map +1 -0
- package/dist/message/partial-quote.js +41 -0
- package/dist/message/partial-quote.js.map +1 -0
- package/dist/message/types.d.ts +15 -0
- package/dist/message/types.d.ts.map +1 -1
- package/dist/messaging/sender.d.ts +4 -3
- package/dist/messaging/sender.d.ts.map +1 -1
- package/dist/messaging/sender.js +8 -3
- package/dist/messaging/sender.js.map +1 -1
- package/dist/protocol/api.d.ts +21 -3
- package/dist/protocol/api.d.ts.map +1 -1
- package/dist/protocol/api.js +57 -11
- package/dist/protocol/api.js.map +1 -1
- package/dist/protocol/bot-agent.d.ts.map +1 -1
- package/dist/protocol/bot-agent.js +2 -2
- package/dist/protocol/bot-agent.js.map +1 -1
- package/dist/protocol/headers.d.ts +14 -2
- package/dist/protocol/headers.d.ts.map +1 -1
- package/dist/protocol/headers.js +30 -8
- package/dist/protocol/headers.js.map +1 -1
- package/dist/protocol/json.d.ts +12 -0
- package/dist/protocol/json.d.ts.map +1 -0
- package/dist/protocol/json.js +69 -0
- package/dist/protocol/json.js.map +1 -0
- package/dist/protocol/types.d.ts +29 -2
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js +14 -1
- package/dist/protocol/types.js.map +1 -1
- package/dist/transport/fetch.d.ts +8 -0
- package/dist/transport/fetch.d.ts.map +1 -0
- package/dist/transport/fetch.js +5 -0
- package/dist/transport/fetch.js.map +1 -0
- package/dist/transport/http.d.ts +8 -1
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +14 -7
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/types.d.ts +2 -0
- package/dist/transport/types.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -51,12 +51,40 @@ const bot = new WeChatBot({
|
|
|
51
51
|
storageDir: '~/.wechatbot',
|
|
52
52
|
logLevel: 'info', // 'debug' | 'info' | 'warn' | 'error' | 'silent'
|
|
53
53
|
loginCallbacks: {
|
|
54
|
-
onQrUrl: (url) => renderQrCode(url),
|
|
54
|
+
onQrUrl: (url) => renderQrCode(url), // without it, the QR URL is logged at warn level
|
|
55
55
|
onScanned: () => console.log('Scanned!'),
|
|
56
|
+
onVerifyCode: (isRetry) => askUser(), // digits WeChat shows on the phone
|
|
56
57
|
},
|
|
58
|
+
botAgent: 'MyApp/1.0', // sent as base_info.bot_agent
|
|
59
|
+
messageCacheSize: 1000, // recent messages kept to fill quoted text; 0 disables
|
|
60
|
+
routeTag: undefined, // deployment route tag, only if WeChat gave you one
|
|
61
|
+
verifyPin: false, // ask WeChat to require the phone PIN when binding
|
|
57
62
|
})
|
|
58
63
|
```
|
|
59
64
|
|
|
65
|
+
`loginCallbacks` set here apply to every login, including the automatic
|
|
66
|
+
re-login after a session expires. `bot.login({ callbacks })` overrides them
|
|
67
|
+
field by field. Without `onVerifyCode`, the SDK reads the PIN from stdin, and
|
|
68
|
+
fails with an `AuthError` when stdin is not a terminal.
|
|
69
|
+
|
|
70
|
+
### Proxy
|
|
71
|
+
|
|
72
|
+
Node's built-in `fetch` ignores `HTTPS_PROXY`. To send requests through a proxy:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
// Node 22.21+ / 24.5+: built-in proxy support (process-wide)
|
|
76
|
+
new WeChatBot({ proxy: 'http://127.0.0.1:7890' })
|
|
77
|
+
new WeChatBot({ proxy: true }) // read HTTPS_PROXY / HTTP_PROXY / NO_PROXY
|
|
78
|
+
|
|
79
|
+
// Any Node version: pass your own fetch, e.g. undici with a ProxyAgent
|
|
80
|
+
import { fetch as undiciFetch, ProxyAgent } from 'undici'
|
|
81
|
+
const dispatcher = new ProxyAgent('http://127.0.0.1:7890')
|
|
82
|
+
new WeChatBot({ fetch: (url, init) => undiciFetch(url, { ...init, dispatcher }) })
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
You can also start Node with `NODE_USE_ENV_PROXY=1` and `HTTPS_PROXY` set. The
|
|
86
|
+
custom `fetch` covers the iLink API, the CDN, and `{ url }` downloads.
|
|
87
|
+
|
|
60
88
|
## API Reference
|
|
61
89
|
|
|
62
90
|
### Lifecycle
|
|
@@ -105,6 +133,62 @@ await bot.reply(msg, { url: 'https://example.com/photo.jpg' })
|
|
|
105
133
|
await bot.send(userId, { image: buffer, caption: 'Hi!' })
|
|
106
134
|
```
|
|
107
135
|
|
|
136
|
+
A caption is sent as its own text message before the media. WeChat rejects
|
|
137
|
+
text and media in one message.
|
|
138
|
+
|
|
139
|
+
`reply`, `send` and `sendRaw` return a `SendResult`:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
const { messageId, captionMessageId } = await bot.reply(msg, 'Hello!')
|
|
143
|
+
// messageId: server ID (uint64 as a string). A later quote of this reply refers to it.
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
WeChat lets a bot message a user only within 24 hours of the user's last
|
|
147
|
+
message, and only a few times before the user writes again. A rejected send
|
|
148
|
+
throws an `ApiError` with `isSendRejected === true` (`ret=-2`):
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
try {
|
|
152
|
+
await bot.send(userId, 'Reminder')
|
|
153
|
+
} catch (err) {
|
|
154
|
+
if (err instanceof ApiError && err.isSendRejected) {
|
|
155
|
+
// wait until the user messages the bot again
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Quoted messages
|
|
161
|
+
|
|
162
|
+
Newer WeChat clients often send only the **ID** of a quoted message, not its
|
|
163
|
+
text. The SDK keeps the last `messageCacheSize` received and sent messages in
|
|
164
|
+
memory and fills `quotedMessage.text` when it has seen the quoted message.
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
bot.onMessage((msg) => {
|
|
168
|
+
const q = msg.quotedMessage
|
|
169
|
+
if (q) {
|
|
170
|
+
q.messageId // server ID of the quoted message
|
|
171
|
+
q.text // filled from the cache when known; for a partial quote, the quoted part
|
|
172
|
+
q.fromBot // true when the user quoted one of the bot's messages
|
|
173
|
+
q.partialText // present when only part of the message was quoted
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The cache is in memory only; a restart clears it.
|
|
179
|
+
|
|
180
|
+
### Underscores in text
|
|
181
|
+
|
|
182
|
+
WeChat renders bot text as Markdown, so `file_name.txt` can show as
|
|
183
|
+
`filename.txt`. `protectUnderscores(text)` replaces `_` outside code spans with
|
|
184
|
+
the full-width `_` (U+FF3F), which WeChat displays as is. Text a user copies
|
|
185
|
+
then contains `_`, so the SDK does not do this automatically.
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import { protectUnderscores } from '@wechatbot/wechatbot'
|
|
189
|
+
await bot.reply(msg, protectUnderscores('Saved EPS_manuscript_CN.pdf'))
|
|
190
|
+
```
|
|
191
|
+
|
|
108
192
|
### Typing
|
|
109
193
|
|
|
110
194
|
| Method | Description |
|
|
@@ -153,6 +237,7 @@ bot.on('close', () => { })
|
|
|
153
237
|
|
|
154
238
|
```typescript
|
|
155
239
|
interface IncomingMessage {
|
|
240
|
+
messageId?: string // server ID (uint64 as a string)
|
|
156
241
|
userId: string
|
|
157
242
|
text: string
|
|
158
243
|
type: 'text' | 'image' | 'voice' | 'file' | 'video'
|
|
@@ -164,6 +249,15 @@ interface IncomingMessage {
|
|
|
164
249
|
quotedMessage?: QuotedMessage
|
|
165
250
|
raw: WireMessage
|
|
166
251
|
}
|
|
252
|
+
|
|
253
|
+
interface QuotedMessage {
|
|
254
|
+
messageId?: string
|
|
255
|
+
title?: string
|
|
256
|
+
text?: string
|
|
257
|
+
type?: 'text' | 'image' | 'voice' | 'file' | 'video'
|
|
258
|
+
fromBot?: boolean
|
|
259
|
+
partialText?: PartialText
|
|
260
|
+
}
|
|
167
261
|
```
|
|
168
262
|
|
|
169
263
|
### Storage Interface
|
|
@@ -2,6 +2,11 @@ import type { Logger } from '../logger/types.js';
|
|
|
2
2
|
import type { ILinkApi } from '../protocol/api.js';
|
|
3
3
|
import type { Storage } from '../storage/interface.js';
|
|
4
4
|
import type { Credentials, QrLoginCallbacks } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Any status that contains this marker asks for the digits shown on the phone
|
|
7
|
+
* (`need_verifycode`, `confirmed_but_need_verifycode`, …), as upstream does.
|
|
8
|
+
*/
|
|
9
|
+
export declare function statusNeedsVerifyCode(status: string): boolean;
|
|
5
10
|
/**
|
|
6
11
|
* Handles the entire QR login flow with credential persistence.
|
|
7
12
|
* Separated from the main client so it can be tested / replaced independently.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authenticator.d.ts","sourceRoot":"","sources":["../../src/auth/authenticator.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAElD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAA;AAEtD,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"authenticator.d.ts","sourceRoot":"","sources":["../../src/auth/authenticator.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAElD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAA;AAEtD,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAK/D;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAE7D;AAKD;;;GAGG;AACH,qBAAa,aAAa;IAItB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJ1B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;gBAGZ,GAAG,EAAE,QAAQ,EACb,OAAO,EAAE,OAAO,EACjC,MAAM,EAAE,MAAM;IAKhB;;OAEG;IACG,eAAe,IAAI,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC;IAIzD;;OAEG;IACG,KAAK,CAAC,OAAO,GAAE;QACnB,KAAK,CAAC,EAAE,OAAO,CAAA;QACf,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,SAAS,CAAC,EAAE,gBAAgB,CAAA;KACxB,GAAG,OAAO,CAAC,WAAW,CAAC;IAc7B;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAU/B;;;;;OAKG;YACW,OAAO;IA4HrB;;;OAGG;YACW,gBAAgB;CA0B/B"}
|
|
@@ -6,6 +6,13 @@ import { STORAGE_KEYS } from '../storage/interface.js';
|
|
|
6
6
|
const QR_POLL_INTERVAL_MS = 2_000;
|
|
7
7
|
/** Maximum number of times to refresh the QR code before giving up. */
|
|
8
8
|
const MAX_QR_REFRESH_COUNT = 3;
|
|
9
|
+
/**
|
|
10
|
+
* Any status that contains this marker asks for the digits shown on the phone
|
|
11
|
+
* (`need_verifycode`, `confirmed_but_need_verifycode`, …), as upstream does.
|
|
12
|
+
*/
|
|
13
|
+
export function statusNeedsVerifyCode(status) {
|
|
14
|
+
return status.includes('need_verifycode');
|
|
15
|
+
}
|
|
9
16
|
/** Fixed API base URL for QR code requests (matches npm package). */
|
|
10
17
|
const FIXED_QR_BASE_URL = 'https://ilinkai.weixin.qq.com';
|
|
11
18
|
/**
|
|
@@ -77,7 +84,8 @@ export class Authenticator {
|
|
|
77
84
|
callbacks.onQrUrl(qr.qrcode_img_content);
|
|
78
85
|
}
|
|
79
86
|
else {
|
|
80
|
-
|
|
87
|
+
// Warn level: without a callback this line is the only way to get the QR.
|
|
88
|
+
this.logger.warn(`Scan this QR code in WeChat (pass loginCallbacks.onQrUrl to handle it yourself): ${qr.qrcode_img_content}`);
|
|
81
89
|
}
|
|
82
90
|
let lastStatus;
|
|
83
91
|
// Current polling URL; may be updated on IDC redirect
|
|
@@ -124,7 +132,7 @@ export class Authenticator {
|
|
|
124
132
|
return credentials;
|
|
125
133
|
}
|
|
126
134
|
// Pair-code challenge: ask the user for the digits shown in WeChat
|
|
127
|
-
if (status.status
|
|
135
|
+
if (statusNeedsVerifyCode(status.status)) {
|
|
128
136
|
const isRetry = pendingVerifyCode !== undefined;
|
|
129
137
|
pendingVerifyCode = await this.promptVerifyCode(isRetry, callbacks);
|
|
130
138
|
continue; // Re-poll immediately with the code attached
|
|
@@ -171,6 +179,11 @@ export class Authenticator {
|
|
|
171
179
|
if (callbacks?.onVerifyCode) {
|
|
172
180
|
return callbacks.onVerifyCode(isRetry);
|
|
173
181
|
}
|
|
182
|
+
if (!process.stdin.isTTY) {
|
|
183
|
+
throw new AuthError('WeChat asked for the pairing code shown on the phone, but no ' +
|
|
184
|
+
'loginCallbacks.onVerifyCode was given and stdin is not a terminal. ' +
|
|
185
|
+
'Pass onVerifyCode to supply the code.');
|
|
186
|
+
}
|
|
174
187
|
const prompt = isRetry
|
|
175
188
|
? 'Code mismatch — enter the pairing code shown in WeChat again: '
|
|
176
189
|
: 'Enter the pairing code shown in WeChat on your phone: ';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authenticator.js","sourceRoot":"","sources":["../../src/auth/authenticator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAA;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAG7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CA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+D;gBAC7D,qEAAqE;gBACrE,uCAAuC,CAC1C,CAAA;QACH,CAAC;QAED,MAAM,MAAM,GAAG,OAAO;YACpB,CAAC,CAAC,gEAAgE;YAClE,CAAC,CAAC,wDAAwD,CAAA;QAC5D,MAAM,EAAE,GAAG,eAAe,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;QAC5E,IAAI,CAAC;YACH,OAAO,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;QAC3C,CAAC;gBAAS,CAAC;YACT,EAAE,CAAC,KAAK,EAAE,CAAA;QACZ,CAAC;IACH,CAAC;CACF"}
|
package/dist/auth/types.d.ts
CHANGED
|
@@ -15,7 +15,8 @@ export interface QrLoginCallbacks {
|
|
|
15
15
|
/**
|
|
16
16
|
* Called when the server requires a pairing code (the digits shown in
|
|
17
17
|
* WeChat on the user's phone). `isRetry` is true when a previously
|
|
18
|
-
* submitted code was rejected. Defaults to a stdin prompt
|
|
18
|
+
* submitted code was rejected. Defaults to a stdin prompt when stdin is a
|
|
19
|
+
* terminal; otherwise login fails with an AuthError.
|
|
19
20
|
*/
|
|
20
21
|
onVerifyCode?: (isRetry: boolean) => string | Promise<string>;
|
|
21
22
|
}
|
package/dist/auth/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,8DAA8D;IAC9D,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IAC/B,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;IACtB,2EAA2E;IAC3E,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;IACtB
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,8DAA8D;IAC9D,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IAC/B,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;IACtB,2EAA2E;IAC3E,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;IACtB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;CAC9D"}
|
package/dist/core/client.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { MessageBuilder, type IncomingMessage } from '../message/index.js';
|
|
|
6
6
|
import { type Middleware } from '../middleware/index.js';
|
|
7
7
|
import { type CDNMedia } from '../protocol/types.js';
|
|
8
8
|
import { type Storage } from '../storage/index.js';
|
|
9
|
+
import type { FetchFunction } from '../transport/fetch.js';
|
|
9
10
|
export interface WeChatBotOptions {
|
|
10
11
|
/** Base URL for the iLink API (default: https://ilinkai.weixin.qq.com) */
|
|
11
12
|
baseUrl?: string;
|
|
@@ -25,6 +26,51 @@ export interface WeChatBotOptions {
|
|
|
25
26
|
* "WeChatBot/<version>".
|
|
26
27
|
*/
|
|
27
28
|
botAgent?: string;
|
|
29
|
+
/**
|
|
30
|
+
* How many recent messages (received and sent) to keep in memory, keyed by
|
|
31
|
+
* server message ID. Newer WeChat clients send only the ID of a quoted
|
|
32
|
+
* message; the SDK uses this cache to fill `quotedMessage.text`.
|
|
33
|
+
* Default 1000. Set 0 to disable.
|
|
34
|
+
*/
|
|
35
|
+
messageCacheSize?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Deployment route tag, e.g. "10086". Sent as the `SKRouteTag` header and as
|
|
38
|
+
* `_cradle_hostname=g<routeTag>` on every iLink API URL. Leave unset unless
|
|
39
|
+
* WeChat gave you one.
|
|
40
|
+
*/
|
|
41
|
+
routeTag?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Ask WeChat to require the PIN shown on the phone when binding (sends
|
|
44
|
+
* `bot_spam_business_data` with `verify_pin: 1`). Handle the PIN with
|
|
45
|
+
* `loginCallbacks.onVerifyCode`. Default false.
|
|
46
|
+
*/
|
|
47
|
+
verifyPin?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Custom fetch for every HTTP request: iLink API, CDN, and `{ url }` media
|
|
50
|
+
* downloads. Use it to send traffic through a proxy, for example
|
|
51
|
+
* `fetch: (url, init) => undiciFetch(url, { ...init, dispatcher: new ProxyAgent(proxyUrl) })`.
|
|
52
|
+
* Overrides `proxy`.
|
|
53
|
+
*/
|
|
54
|
+
fetch?: FetchFunction;
|
|
55
|
+
/**
|
|
56
|
+
* Send all requests through an HTTP(S) proxy, e.g. "http://127.0.0.1:7890".
|
|
57
|
+
* `true` reads HTTPS_PROXY / HTTP_PROXY / NO_PROXY from the environment.
|
|
58
|
+
* Uses Node's built-in proxy support (`http.setGlobalProxyFromEnv`,
|
|
59
|
+
* Node 22.21+ / 24.5+), which is process-wide. On older Node, set
|
|
60
|
+
* NODE_USE_ENV_PROXY=1 instead, or pass `fetch`.
|
|
61
|
+
*/
|
|
62
|
+
proxy?: string | boolean;
|
|
63
|
+
}
|
|
64
|
+
/** Result of a send. */
|
|
65
|
+
export interface SendResult {
|
|
66
|
+
/**
|
|
67
|
+
* Server-assigned ID of the message, as a string (uint64). For long text
|
|
68
|
+
* split into chunks, the ID of the last chunk. Undefined when the server
|
|
69
|
+
* returned none.
|
|
70
|
+
*/
|
|
71
|
+
messageId?: string;
|
|
72
|
+
/** ID of the separate caption message, when a caption was sent. */
|
|
73
|
+
captionMessageId?: string;
|
|
28
74
|
}
|
|
29
75
|
/**
|
|
30
76
|
* What to send. Exactly one of text/image/file/video/url must be provided.
|
|
@@ -108,6 +154,7 @@ export declare class WeChatBot extends TypedEmitter<BotEventMap> {
|
|
|
108
154
|
readonly uploader: MediaUploader;
|
|
109
155
|
readonly downloader: MediaDownloader;
|
|
110
156
|
private readonly http;
|
|
157
|
+
private readonly fetchFn?;
|
|
111
158
|
private readonly api;
|
|
112
159
|
private readonly auth;
|
|
113
160
|
private readonly parser;
|
|
@@ -116,13 +163,16 @@ export declare class WeChatBot extends TypedEmitter<BotEventMap> {
|
|
|
116
163
|
private readonly poller;
|
|
117
164
|
private readonly sender;
|
|
118
165
|
private readonly typing;
|
|
166
|
+
private readonly messageCache;
|
|
119
167
|
private baseUrl;
|
|
120
168
|
private credentials?;
|
|
121
169
|
private messageHandlers;
|
|
122
170
|
private runPromise;
|
|
171
|
+
private readonly loginCallbacks?;
|
|
123
172
|
constructor(options?: WeChatBotOptions);
|
|
124
173
|
/**
|
|
125
174
|
* Login to WeChat. Uses stored credentials if available, otherwise starts QR flow.
|
|
175
|
+
* `options.callbacks` override the constructor's `loginCallbacks` field by field.
|
|
126
176
|
*/
|
|
127
177
|
login(options?: {
|
|
128
178
|
force?: boolean;
|
|
@@ -171,7 +221,7 @@ export declare class WeChatBot extends TypedEmitter<BotEventMap> {
|
|
|
171
221
|
* // From URL (auto-download, auto-detect type)
|
|
172
222
|
* await bot.reply(msg, { url: 'https://example.com/image.jpg' })
|
|
173
223
|
*/
|
|
174
|
-
reply(message: IncomingMessage, content: SendContent): Promise<
|
|
224
|
+
reply(message: IncomingMessage, content: SendContent): Promise<SendResult>;
|
|
175
225
|
/**
|
|
176
226
|
* Send content to a user by ID.
|
|
177
227
|
* Requires a prior context_token from that user (i.e., they messaged you first).
|
|
@@ -184,11 +234,11 @@ export declare class WeChatBot extends TypedEmitter<BotEventMap> {
|
|
|
184
234
|
* await bot.send(userId, { file: data, fileName: 'doc.pdf' })
|
|
185
235
|
* await bot.send(userId, { url: 'https://example.com/img.png' })
|
|
186
236
|
*/
|
|
187
|
-
send(userId: string, content: SendContent): Promise<
|
|
237
|
+
send(userId: string, content: SendContent): Promise<SendResult>;
|
|
188
238
|
/**
|
|
189
239
|
* Send a pre-built message payload (advanced use with MessageBuilder).
|
|
190
240
|
*/
|
|
191
|
-
sendRaw(payload: ReturnType<MessageBuilder['build']>): Promise<
|
|
241
|
+
sendRaw(payload: ReturnType<MessageBuilder['build']>): Promise<SendResult>;
|
|
192
242
|
/**
|
|
193
243
|
* Download media from an incoming message.
|
|
194
244
|
*
|
|
@@ -250,6 +300,8 @@ export declare class WeChatBot extends TypedEmitter<BotEventMap> {
|
|
|
250
300
|
* Core send implementation. All public send methods route through here.
|
|
251
301
|
*/
|
|
252
302
|
private sendContent;
|
|
303
|
+
/** Send text and remember it in the message cache under its server ID. */
|
|
304
|
+
private sendTrackedText;
|
|
253
305
|
/**
|
|
254
306
|
* Upload buffer to CDN and send as a message item.
|
|
255
307
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/core/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAA;AAG5D,OAAO,EAAiB,KAAK,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACzF,OAAO,EAAgB,KAAK,MAAM,EAAE,KAAK,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7E,OAAO,EACL,eAAe,EACf,aAAa,EACb,KAAK,aAAa,EAClB,KAAK,YAAY,EAKlB,MAAM,mBAAmB,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G,CAAC,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAC;QAAC,SAAS,CAAC,EAAE,gBAAgB,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAKrF,+BAA+B;IAC/B,IAAI,IAAI,IAAI;IAMZ,yDAAyD;IACzD,IAAI,SAAS,IAAI,OAAO,CAEvB;IAMD;;OAEG;IACH,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc;IAU7C;;OAEG;YACW,WAAW;IAkEzB,0EAA0E;YAC5D,eAAe;IAW7B;;OAEG;YACW,eAAe;IAmC7B,OAAO,CAAC,iBAAiB;YA0DX,kBAAkB;IAahC,OAAO,CAAC,cAAc;IAatB,OAAO,CAAC,kBAAkB;CAM3B"}
|
package/dist/core/client.js
CHANGED
|
@@ -4,12 +4,47 @@ import { Authenticator } from '../auth/index.js';
|
|
|
4
4
|
import { createLogger } from '../logger/index.js';
|
|
5
5
|
import { MediaDownloader, MediaUploader, categorizeByMime, getMimeFromFilename, downloadFromUrl, silkToWav, } from '../media/index.js';
|
|
6
6
|
import { MessageBuilder, MessageParser } from '../message/index.js';
|
|
7
|
+
import { MessageCache } from '../message/cache.js';
|
|
7
8
|
import { MiddlewareEngine } from '../middleware/index.js';
|
|
8
9
|
import { ContextStore, MessagePoller, MessageSender, TypingService } from '../messaging/index.js';
|
|
9
10
|
import { ILinkApi } from '../protocol/api.js';
|
|
10
11
|
import { DEFAULT_BASE_URL, MediaType } from '../protocol/types.js';
|
|
11
12
|
import { FileStorage, MemoryStorage } from '../storage/index.js';
|
|
12
13
|
import { HttpClient } from '../transport/http.js';
|
|
14
|
+
import * as nodeHttp from 'node:http';
|
|
15
|
+
/**
|
|
16
|
+
* Pick the fetch for a client. A custom `fetch` wins. `proxy` turns on
|
|
17
|
+
* Node's built-in proxy support for the global fetch, process-wide.
|
|
18
|
+
*/
|
|
19
|
+
function resolveClientFetch(options, logger) {
|
|
20
|
+
if (options.fetch)
|
|
21
|
+
return options.fetch;
|
|
22
|
+
if (!options.proxy)
|
|
23
|
+
return undefined;
|
|
24
|
+
const setGlobalProxyFromEnv = nodeHttp.setGlobalProxyFromEnv;
|
|
25
|
+
if (typeof setGlobalProxyFromEnv !== 'function') {
|
|
26
|
+
logger.warn(`The proxy option needs Node 22.21+ or 24.5+ (running ${process.versions.node}). ` +
|
|
27
|
+
'Set NODE_USE_ENV_PROXY=1 with HTTPS_PROXY, or pass a custom fetch.');
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
if (options.proxy === true) {
|
|
31
|
+
setGlobalProxyFromEnv();
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
setGlobalProxyFromEnv({
|
|
35
|
+
HTTP_PROXY: options.proxy,
|
|
36
|
+
HTTPS_PROXY: options.proxy,
|
|
37
|
+
NO_PROXY: process.env.NO_PROXY ?? process.env.no_proxy,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
const MEDIA_CONTENT_TYPE = {
|
|
43
|
+
[MediaType.IMAGE]: 'image',
|
|
44
|
+
[MediaType.VIDEO]: 'video',
|
|
45
|
+
[MediaType.FILE]: 'file',
|
|
46
|
+
[MediaType.VOICE]: 'voice',
|
|
47
|
+
};
|
|
13
48
|
// ═══════════════════════════════════════════════════════════════════════
|
|
14
49
|
// Main Client
|
|
15
50
|
// ═══════════════════════════════════════════════════════════════════════
|
|
@@ -51,6 +86,7 @@ export class WeChatBot extends TypedEmitter {
|
|
|
51
86
|
downloader;
|
|
52
87
|
// ── Internal services ───────────────────────────────────────────────
|
|
53
88
|
http;
|
|
89
|
+
fetchFn;
|
|
54
90
|
api;
|
|
55
91
|
auth;
|
|
56
92
|
parser;
|
|
@@ -59,29 +95,37 @@ export class WeChatBot extends TypedEmitter {
|
|
|
59
95
|
poller;
|
|
60
96
|
sender;
|
|
61
97
|
typing;
|
|
98
|
+
messageCache;
|
|
62
99
|
// ── State ───────────────────────────────────────────────────────────
|
|
63
100
|
baseUrl;
|
|
64
101
|
credentials;
|
|
65
102
|
messageHandlers = [];
|
|
66
103
|
runPromise = null;
|
|
104
|
+
loginCallbacks;
|
|
67
105
|
constructor(options = {}) {
|
|
68
106
|
super();
|
|
69
107
|
// Initialize foundational components
|
|
70
108
|
this.baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
|
|
109
|
+
this.loginCallbacks = options.loginCallbacks;
|
|
71
110
|
this.logger = options.logger ?? createLogger({ level: options.logLevel ?? 'info' });
|
|
72
111
|
this.storage = resolveStorage(options);
|
|
73
112
|
// Build the layered architecture
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
113
|
+
this.fetchFn = resolveClientFetch(options, this.logger);
|
|
114
|
+
this.http = new HttpClient({ logger: this.logger, fetch: this.fetchFn });
|
|
115
|
+
this.api = new ILinkApi(this.http, options.botAgent, {
|
|
116
|
+
routeTag: options.routeTag,
|
|
117
|
+
verifyPin: options.verifyPin,
|
|
118
|
+
});
|
|
76
119
|
this.auth = new Authenticator(this.api, this.storage, this.logger);
|
|
77
120
|
this.parser = new MessageParser();
|
|
121
|
+
this.messageCache = new MessageCache(options.messageCacheSize ?? 1000);
|
|
78
122
|
this.middleware = new MiddlewareEngine();
|
|
79
123
|
this.contextStore = new ContextStore(this.storage, this.logger);
|
|
80
124
|
this.poller = new MessagePoller(this.api, this.storage, this.logger);
|
|
81
125
|
this.sender = new MessageSender(this.api, this.contextStore, this.logger);
|
|
82
126
|
this.typing = new TypingService(this.api, this.contextStore, this.logger);
|
|
83
|
-
this.uploader = new MediaUploader(this.api, undefined, this.logger);
|
|
84
|
-
this.downloader = new MediaDownloader(undefined, this.logger);
|
|
127
|
+
this.uploader = new MediaUploader(this.api, undefined, this.logger, this.fetchFn);
|
|
128
|
+
this.downloader = new MediaDownloader(undefined, this.logger, this.fetchFn);
|
|
85
129
|
// Wire up internal events
|
|
86
130
|
this.setupPollerEvents();
|
|
87
131
|
}
|
|
@@ -90,12 +134,13 @@ export class WeChatBot extends TypedEmitter {
|
|
|
90
134
|
// ═══════════════════════════════════════════════════════════════════
|
|
91
135
|
/**
|
|
92
136
|
* Login to WeChat. Uses stored credentials if available, otherwise starts QR flow.
|
|
137
|
+
* `options.callbacks` override the constructor's `loginCallbacks` field by field.
|
|
93
138
|
*/
|
|
94
139
|
async login(options = {}) {
|
|
95
140
|
const creds = await this.auth.login({
|
|
96
141
|
force: options.force,
|
|
97
142
|
baseUrl: this.baseUrl,
|
|
98
|
-
callbacks: options.callbacks,
|
|
143
|
+
callbacks: { ...this.loginCallbacks, ...options.callbacks },
|
|
99
144
|
});
|
|
100
145
|
this.setCredentials(creds);
|
|
101
146
|
this.emit('login', creds);
|
|
@@ -165,8 +210,9 @@ export class WeChatBot extends TypedEmitter {
|
|
|
165
210
|
async reply(message, content) {
|
|
166
211
|
const creds = this.requireCredentials();
|
|
167
212
|
this.contextStore.set(message.userId, message._contextToken);
|
|
168
|
-
await this.sendContent(message.userId, message._contextToken, content);
|
|
213
|
+
const result = await this.sendContent(message.userId, message._contextToken, content);
|
|
169
214
|
this.typing.stopTyping(creds.baseUrl, creds.token, message.userId).catch(() => { });
|
|
215
|
+
return result;
|
|
170
216
|
}
|
|
171
217
|
/**
|
|
172
218
|
* Send content to a user by ID.
|
|
@@ -184,14 +230,14 @@ export class WeChatBot extends TypedEmitter {
|
|
|
184
230
|
const ctx = this.contextStore.get(userId);
|
|
185
231
|
if (!ctx)
|
|
186
232
|
throw new NoContextError(userId);
|
|
187
|
-
|
|
233
|
+
return this.sendContent(userId, ctx, content);
|
|
188
234
|
}
|
|
189
235
|
/**
|
|
190
236
|
* Send a pre-built message payload (advanced use with MessageBuilder).
|
|
191
237
|
*/
|
|
192
238
|
async sendRaw(payload) {
|
|
193
239
|
const creds = this.requireCredentials();
|
|
194
|
-
await this.sender.sendRaw(creds.baseUrl, creds.token, payload);
|
|
240
|
+
return { messageId: await this.sender.sendRaw(creds.baseUrl, creds.token, payload) };
|
|
195
241
|
}
|
|
196
242
|
// ═══════════════════════════════════════════════════════════════════
|
|
197
243
|
// Downloading — one method: download()
|
|
@@ -354,17 +400,15 @@ export class WeChatBot extends TypedEmitter {
|
|
|
354
400
|
const creds = this.requireCredentials();
|
|
355
401
|
// String shorthand → text
|
|
356
402
|
if (typeof content === 'string') {
|
|
357
|
-
await this.
|
|
358
|
-
return;
|
|
403
|
+
return { messageId: await this.sendTrackedText(creds, userId, content, contextToken) };
|
|
359
404
|
}
|
|
360
405
|
// { text }
|
|
361
406
|
if ('text' in content) {
|
|
362
|
-
await this.
|
|
363
|
-
return;
|
|
407
|
+
return { messageId: await this.sendTrackedText(creds, userId, content.text, contextToken) };
|
|
364
408
|
}
|
|
365
409
|
// { url } → download then re-route
|
|
366
410
|
if ('url' in content) {
|
|
367
|
-
const result = await downloadFromUrl(content.url, { logger: this.logger });
|
|
411
|
+
const result = await downloadFromUrl(content.url, { logger: this.logger, fetch: this.fetchFn });
|
|
368
412
|
const fileName = content.fileName ?? result.filename;
|
|
369
413
|
return this.sendContent(userId, contextToken, {
|
|
370
414
|
file: result.data,
|
|
@@ -374,43 +418,41 @@ export class WeChatBot extends TypedEmitter {
|
|
|
374
418
|
}
|
|
375
419
|
// { image }
|
|
376
420
|
if ('image' in content) {
|
|
377
|
-
|
|
421
|
+
return this.sendMediaBuffer(creds, userId, contextToken, content.image, MediaType.IMAGE, (builder, result) => {
|
|
378
422
|
builder.image({ media: result.media, midSize: result.encryptedFileSize });
|
|
379
423
|
}, content.caption);
|
|
380
|
-
return;
|
|
381
424
|
}
|
|
382
425
|
// { video }
|
|
383
426
|
if ('video' in content) {
|
|
384
|
-
|
|
427
|
+
return this.sendMediaBuffer(creds, userId, contextToken, content.video, MediaType.VIDEO, (builder, result) => {
|
|
385
428
|
builder.video({ media: result.media, videoSize: result.encryptedFileSize });
|
|
386
429
|
}, content.caption);
|
|
387
|
-
return;
|
|
388
430
|
}
|
|
389
431
|
// { file, fileName } — auto-route by extension
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
caption: content.caption,
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
if (category === 'video') {
|
|
400
|
-
return this.sendContent(userId, contextToken, {
|
|
401
|
-
video: content.file,
|
|
402
|
-
caption: content.caption,
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
// Generic file
|
|
406
|
-
if (content.caption) {
|
|
407
|
-
await this.sender.sendText(creds.baseUrl, creds.token, userId, content.caption, contextToken);
|
|
408
|
-
}
|
|
409
|
-
await this.sendMediaBuffer(creds, userId, contextToken, content.file, MediaType.FILE, (builder, result) => {
|
|
410
|
-
builder.file({ media: result.media, fileName: content.fileName, size: content.file.length });
|
|
432
|
+
const mime = getMimeFromFilename(content.fileName);
|
|
433
|
+
const category = categorizeByMime(mime);
|
|
434
|
+
if (category === 'image') {
|
|
435
|
+
return this.sendContent(userId, contextToken, {
|
|
436
|
+
image: content.file,
|
|
437
|
+
caption: content.caption,
|
|
411
438
|
});
|
|
412
|
-
return;
|
|
413
439
|
}
|
|
440
|
+
if (category === 'video') {
|
|
441
|
+
return this.sendContent(userId, contextToken, {
|
|
442
|
+
video: content.file,
|
|
443
|
+
caption: content.caption,
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
// Generic file
|
|
447
|
+
return this.sendMediaBuffer(creds, userId, contextToken, content.file, MediaType.FILE, (builder, result) => {
|
|
448
|
+
builder.file({ media: result.media, fileName: content.fileName, size: content.file.length });
|
|
449
|
+
}, content.caption);
|
|
450
|
+
}
|
|
451
|
+
/** Send text and remember it in the message cache under its server ID. */
|
|
452
|
+
async sendTrackedText(creds, userId, text, contextToken) {
|
|
453
|
+
const messageId = await this.sender.sendText(creds.baseUrl, creds.token, userId, text, contextToken);
|
|
454
|
+
this.messageCache.put(messageId, { text, type: 'text', fromBot: true });
|
|
455
|
+
return messageId;
|
|
414
456
|
}
|
|
415
457
|
/**
|
|
416
458
|
* Upload buffer to CDN and send as a message item.
|
|
@@ -421,12 +463,19 @@ export class WeChatBot extends TypedEmitter {
|
|
|
421
463
|
userId,
|
|
422
464
|
mediaType,
|
|
423
465
|
});
|
|
466
|
+
// iLink rejects a text item and a media item in one item_list
|
|
467
|
+
// ("invalid arguments"), so the caption goes first as its own message.
|
|
468
|
+
let captionMessageId;
|
|
469
|
+
if (caption) {
|
|
470
|
+
captionMessageId = await this.sendTrackedText(creds, userId, caption, contextToken);
|
|
471
|
+
}
|
|
424
472
|
const builder = MessageBuilder.to(userId, contextToken);
|
|
425
|
-
if (caption)
|
|
426
|
-
builder.text(caption);
|
|
427
473
|
buildItem(builder, result);
|
|
428
|
-
await this.sender.sendRaw(creds.baseUrl, creds.token, builder.build());
|
|
474
|
+
const messageId = await this.sender.sendRaw(creds.baseUrl, creds.token, builder.build());
|
|
475
|
+
const type = MEDIA_CONTENT_TYPE[mediaType];
|
|
476
|
+
this.messageCache.put(messageId, { text: `[${type}]`, type, fromBot: true });
|
|
429
477
|
this.logger.info('Sent media', { userId, mediaType, size: data.length });
|
|
478
|
+
return { messageId, ...(captionMessageId ? { captionMessageId } : {}) };
|
|
430
479
|
}
|
|
431
480
|
// ═══════════════════════════════════════════════════════════════════
|
|
432
481
|
// Internal wiring
|
|
@@ -441,6 +490,13 @@ export class WeChatBot extends TypedEmitter {
|
|
|
441
490
|
const incoming = this.parser.parse(wire);
|
|
442
491
|
if (!incoming)
|
|
443
492
|
continue;
|
|
493
|
+
// Fill an ID-only quote from the cache, then remember this message
|
|
494
|
+
this.messageCache.resolve(incoming.quotedMessage);
|
|
495
|
+
this.messageCache.put(incoming.messageId, {
|
|
496
|
+
text: incoming.text,
|
|
497
|
+
type: incoming.type,
|
|
498
|
+
fromBot: false,
|
|
499
|
+
});
|
|
444
500
|
// Run through middleware chain
|
|
445
501
|
const ctx = await this.middleware.run(incoming);
|
|
446
502
|
if (ctx.handled)
|