sotobot 0.3.1 → 0.3.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/lib/cloudflare.d.ts +3 -1
- package/lib/cloudflare.js +16 -1
- package/lib/main.js +0 -6
- package/package.json +1 -1
package/lib/cloudflare.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export interface ServerOptions {
|
|
|
4
4
|
appId: string;
|
|
5
5
|
webhookSecret: string;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
type BotFactory = () => Promise<Bot>;
|
|
8
|
+
export declare function createServer(bot: Bot | BotFactory): {
|
|
8
9
|
fetch(request: Request): Promise<Response>;
|
|
9
10
|
};
|
|
11
|
+
export {};
|
package/lib/cloudflare.js
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
import { WEBHOOK_PATH } from './constants.js';
|
|
2
|
+
const botCache = new WeakMap();
|
|
2
3
|
export function createServer(bot) {
|
|
3
4
|
return {
|
|
4
5
|
async fetch(request) {
|
|
5
6
|
const { pathname } = new URL(request.url);
|
|
7
|
+
let instance;
|
|
8
|
+
if (typeof bot === 'function') {
|
|
9
|
+
const existing = botCache.get(bot);
|
|
10
|
+
if (existing) {
|
|
11
|
+
instance = existing;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
instance = await bot();
|
|
15
|
+
botCache.set(bot, instance);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
instance = bot;
|
|
20
|
+
}
|
|
6
21
|
if (request.method === 'POST' && pathname === WEBHOOK_PATH) {
|
|
7
|
-
return
|
|
22
|
+
return instance.handleRequest(request);
|
|
8
23
|
}
|
|
9
24
|
return new Response('Not found', { status: 404 });
|
|
10
25
|
},
|
package/lib/main.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { App } from '@octokit/app';
|
|
2
2
|
import { Bot } from './bot.js';
|
|
3
|
-
const botCache = new WeakMap();
|
|
4
3
|
export function createBot(options) {
|
|
5
|
-
const cachedBot = botCache.get(options);
|
|
6
|
-
if (cachedBot) {
|
|
7
|
-
return cachedBot;
|
|
8
|
-
}
|
|
9
4
|
const { privateKey, appId, webhookSecret } = options;
|
|
10
5
|
const app = new App({
|
|
11
6
|
appId,
|
|
@@ -15,7 +10,6 @@ export function createBot(options) {
|
|
|
15
10
|
},
|
|
16
11
|
});
|
|
17
12
|
const bot = new Bot(app, options.bot);
|
|
18
|
-
botCache.set(options, bot);
|
|
19
13
|
return bot;
|
|
20
14
|
}
|
|
21
15
|
export * from './commands.js';
|