Skip to content

Commit 00cb7ae

Browse files
committed
fix: import octokit using dynamic import
1 parent 6b641d3 commit 00cb7ae

3 files changed

Lines changed: 36 additions & 16 deletions

File tree

.lintstagedrc.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export default {
2-
"**/*": "prettier --write --skip-unknown",
2+
"**/*": "prettier --write --ignore-unknown",
33
"*.{ts,tsx}": () => ["npm run check-types", "npm run lint"],
44
};

src/index.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@ import { type SQSEvent, type EventBridgeEvent } from "aws-lambda";
22
import pg, { type QueryResult, type QueryResultRow } from "pg";
33
import Cursor from "pg-cursor";
44
import { SendMessageBatchCommand, SQSClient } from "@aws-sdk/client-sqs";
5-
import { Octokit } from "@octokit/rest";
5+
import type { Octokit } from "@octokit/rest" with { "resolution-mode": "import" };
66

77
type ScheduledEvent = EventBridgeEvent<"Scheduled Event", unknown>;
8-
const client = new Octokit({
9-
auth: process.env.GITHUB_TOKEN,
10-
});
8+
9+
// This mess is required because @octokit/rest is ESM only and AWS Lambda on docker requires CJS.
10+
let _client: Octokit;
11+
async function getGithubClient() {
12+
if (_client) return _client;
13+
14+
const { Octokit } = await import("@octokit/rest");
15+
_client = new Octokit({
16+
auth: process.env.GITHUB_TOKEN,
17+
});
18+
return _client;
19+
}
1120

1221
const connectionString = process.env.DATABASE_URL;
1322
if (!connectionString) {
1423
throw new Error("DATABASE_URL env var missing");
1524
}
16-
const dbClient = new pg.Client({ connectionString });
25+
const dbClient = new pg.Pool({ connectionString, max: 1 });
1726

1827
export async function query<T extends QueryResultRow>(
1928
text: string,
@@ -27,17 +36,22 @@ export async function* queryCursor<T extends QueryResultRow>(
2736
params: any, // eslint-disable-line @typescript-eslint/no-explicit-any
2837
batchSize = 1,
2938
): AsyncGenerator<T> {
30-
const cursor = dbClient.query(new Cursor<T>(text, params));
39+
const client = await dbClient.connect();
40+
try {
41+
const cursor = client.query(new Cursor<T>(text, params));
3142

32-
let size = 0;
33-
do {
34-
const rows = await cursor.read(batchSize);
35-
size = rows.length;
43+
let size = 0;
44+
do {
45+
const rows = await cursor.read(batchSize);
46+
size = rows.length;
3647

37-
for (const row of rows) {
38-
yield row;
39-
}
40-
} while (size > 0);
48+
for (const row of rows) {
49+
yield row;
50+
}
51+
} while (size > 0);
52+
} finally {
53+
client.release();
54+
}
4155
}
4256

4357
export async function handler(event: SQSEvent | ScheduledEvent) {
@@ -193,6 +207,8 @@ async function createBlobForBook(
193207
languageCode: string,
194208
book: Book,
195209
): Promise<TreeItem> {
210+
const client = await getGithubClient();
211+
196212
const result = await client.git.createBlob({
197213
owner: GH_OWNER,
198214
repo: GH_REPO,
@@ -208,6 +224,8 @@ async function createBlobForBook(
208224
}
209225

210226
async function createTree(items: TreeItem[]): Promise<string> {
227+
const client = await getGithubClient();
228+
211229
const result = await client.git.getTree({
212230
owner: GH_OWNER,
213231
repo: GH_REPO,
@@ -224,6 +242,8 @@ async function createTree(items: TreeItem[]): Promise<string> {
224242
}
225243

226244
async function createCommit(code: string, treeSha: string) {
245+
const client = await getGithubClient();
246+
227247
const parentResult = await client.git.getRef({
228248
owner: GH_OWNER,
229249
repo: GH_REPO,

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"resolveJsonModule": true,
1313
"strict": true,
1414
"noImplicitOverride": true,
15-
"module": "commonjs"
15+
"module": "nodenext"
1616
},
1717
"include": ["src"],
1818
"exclude": ["node_modules", "dist"]

0 commit comments

Comments
 (0)