sotobot 0.3.1 → 0.3.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/lib/bot.js CHANGED
@@ -29,7 +29,7 @@ export class Bot {
29
29
  handleRequest(request) {
30
30
  return this.#middleware(request);
31
31
  }
32
- #onIssueComment = async ({ octokit, payload, }) => {
32
+ #onIssueComment = async ({ octokit, payload }) => {
33
33
  if (!payload.issue.pull_request) {
34
34
  return;
35
35
  }
@@ -4,6 +4,8 @@ export interface ServerOptions {
4
4
  appId: string;
5
5
  webhookSecret: string;
6
6
  }
7
- export declare function createServer(bot: Bot): {
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 bot.handleRequest(request);
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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sotobot",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "A serverless-focused GitHub bot framework.",
5
5
  "keywords": [
6
6
  "github",