forked from ubiquity-os/ubiquity-os-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ubiquity-os#64 from ubiquity/sdk
SDK
- Loading branch information
Showing
28 changed files
with
648 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import type { KnipConfig } from "knip"; | ||
|
||
const config: KnipConfig = { | ||
entry: ["src/worker.ts"], | ||
entry: ["src/worker.ts", "deploy/setup-kv-namespace.ts"], | ||
project: ["src/**/*.ts"], | ||
ignore: ["jest.config.ts"], | ||
ignoreBinaries: ["i"], | ||
ignoreExportsUsedInFile: true, | ||
ignoreDependencies: ["@mswjs/data", "esbuild", "eslint-config-prettier", "eslint-plugin-prettier", "msw"], | ||
ignoreDependencies: ["@mswjs/data", "esbuild", "eslint-config-prettier", "eslint-plugin-prettier", "msw", "ts-node"], | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { Config } from "jest"; | ||
|
||
const config: Config = { | ||
testEnvironment: "node", | ||
roots: ["./tests"], | ||
coveragePathIgnorePatterns: ["node_modules", "mocks"], | ||
collectCoverage: true, | ||
coverageReporters: ["json", "lcov", "text", "clover", "json-summary"], | ||
reporters: ["default", "jest-junit"], | ||
coverageDirectory: "coverage", | ||
verbose: true, | ||
transformIgnorePatterns: [], | ||
transform: { | ||
"^.+\\.[j|t]s$": "@swc/jest", | ||
}, | ||
moduleNameMapper: { | ||
"@octokit/webhooks-methods": "<rootDir>/node_modules/@octokit/webhooks-methods/dist-node/index.js", | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const KERNEL_PUBLIC_KEY = ""; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { EmitterWebhookEvent as WebhookEvent, EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; | ||
import { customOctokit } from "./octokit"; | ||
import { Logs } from "@ubiquity-dao/ubiquibot-logger"; | ||
|
||
export interface Context<TConfig = unknown, TEnv = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName> { | ||
eventName: TSupportedEvents; | ||
payload: { | ||
[K in TSupportedEvents]: K extends WebhookEventName ? WebhookEvent<K> : never; | ||
}[TSupportedEvents]["payload"]; | ||
octokit: InstanceType<typeof customOctokit>; | ||
config: TConfig; | ||
env: TEnv; | ||
logger: Logs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { createPlugin } from "./server"; | ||
export type { Context } from "./context"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Octokit } from "@octokit/core"; | ||
import { RequestOptions } from "@octokit/types"; | ||
import { paginateRest } from "@octokit/plugin-paginate-rest"; | ||
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods"; | ||
import { retry } from "@octokit/plugin-retry"; | ||
import { throttling } from "@octokit/plugin-throttling"; | ||
|
||
const defaultOptions = { | ||
throttle: { | ||
onAbuseLimit: (retryAfter: number, options: RequestOptions, octokit: Octokit) => { | ||
octokit.log.warn(`Abuse limit hit with "${options.method} ${options.url}", retrying in ${retryAfter} seconds.`); | ||
return true; | ||
}, | ||
onRateLimit: (retryAfter: number, options: RequestOptions, octokit: Octokit) => { | ||
octokit.log.warn(`Rate limit hit with "${options.method} ${options.url}", retrying in ${retryAfter} seconds.`); | ||
return true; | ||
}, | ||
onSecondaryRateLimit: (retryAfter: number, options: RequestOptions, octokit: Octokit) => { | ||
octokit.log.warn(`Secondary rate limit hit with "${options.method} ${options.url}", retrying in ${retryAfter} seconds.`); | ||
return true; | ||
}, | ||
}, | ||
}; | ||
|
||
export const customOctokit = Octokit.plugin(throttling, retry, paginateRest, restEndpointMethods).defaults((instanceOptions: object) => { | ||
return Object.assign({}, defaultOptions, instanceOptions); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Hono } from "hono"; | ||
import { HTTPException } from "hono/http-exception"; | ||
import { Context } from "./context"; | ||
import { customOctokit } from "./octokit"; | ||
import { EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; | ||
import { verifySignature } from "./signature"; | ||
import { KERNEL_PUBLIC_KEY } from "./constants"; | ||
import { Logs, LogLevel, LOG_LEVEL } from "@ubiquity-dao/ubiquibot-logger"; | ||
import { Manifest } from "../types/manifest"; | ||
|
||
interface Options { | ||
kernelPublicKey?: string; | ||
logLevel?: LogLevel; | ||
} | ||
|
||
export async function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName>( | ||
handler: (context: Context<TConfig, TEnv, TSupportedEvents>) => Promise<Record<string, unknown> | undefined>, | ||
manifest: Manifest, | ||
options?: Options | ||
) { | ||
const app = new Hono(); | ||
|
||
app.get("/manifest.json", (ctx) => { | ||
return ctx.json(manifest); | ||
}); | ||
|
||
app.post("/", async (ctx) => { | ||
if (ctx.req.header("content-type") !== "application/json") { | ||
throw new HTTPException(400, { message: "Content-Type must be application/json" }); | ||
} | ||
|
||
const payload = await ctx.req.json(); | ||
const signature = payload.signature; | ||
delete payload.signature; | ||
if (!(await verifySignature(options?.kernelPublicKey || KERNEL_PUBLIC_KEY, payload, signature))) { | ||
throw new HTTPException(400, { message: "Invalid signature" }); | ||
} | ||
|
||
try { | ||
new customOctokit({ auth: payload.authToken }); | ||
} catch (error) { | ||
console.error("SDK ERROR", error); | ||
throw new HTTPException(500, { message: "Unexpected error" }); | ||
} | ||
|
||
const context: Context<TConfig, TEnv, TSupportedEvents> = { | ||
eventName: payload.eventName, | ||
payload: payload.payload, | ||
octokit: new customOctokit({ auth: payload.authToken }), | ||
config: payload.settings as TConfig, | ||
env: ctx.env as TEnv, | ||
logger: new Logs(options?.logLevel || LOG_LEVEL.INFO), | ||
}; | ||
|
||
try { | ||
const result = await handler(context); | ||
return ctx.json({ stateId: payload.stateId, output: result }); | ||
} catch (error) { | ||
console.error(error); | ||
throw new HTTPException(500, { message: "Unexpected error" }); | ||
} | ||
}); | ||
|
||
return app; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export async function verifySignature(publicKeyPem: string, payload: unknown, signature: string) { | ||
const pemContents = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").trim(); | ||
const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0)); | ||
|
||
const publicKey = await crypto.subtle.importKey( | ||
"spki", | ||
binaryDer, | ||
{ | ||
name: "RSASSA-PKCS1-v1_5", | ||
hash: "SHA-256", | ||
}, | ||
true, | ||
["verify"] | ||
); | ||
|
||
const signatureArray = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0)); | ||
const dataArray = new TextEncoder().encode(JSON.stringify(payload)); | ||
|
||
return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", publicKey, signatureArray, dataArray); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.