Skip to content

Commit de3ec31

Browse files
feat: nitro async plugin patch & prod build testing (#61)
1 parent c0d4a1a commit de3ec31

File tree

15 files changed

+228
-53
lines changed

15 files changed

+228
-53
lines changed

lib/db/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export interface CacheKeysTable {
1919
accessed_at: string
2020
}
2121

22-
async function initializeDatabase() {
22+
let db: Kysely<Database>
23+
24+
export async function initializeDatabase() {
2325
const driverName = ENV.DB_DRIVER
2426
const driverSetup = getDatabaseDriver(driverName)
2527
if (!driverSetup) {
@@ -31,7 +33,7 @@ async function initializeDatabase() {
3133

3234
const driver = await driverSetup()
3335

34-
const db = new Kysely<Database>({
36+
db = new Kysely<Database>({
3537
dialect: driver,
3638
})
3739

@@ -52,12 +54,8 @@ async function initializeDatabase() {
5254
}
5355
logger.debug('Migration results', results)
5456
logger.success('Database migrated')
55-
56-
return db
5757
}
5858

59-
const db = await initializeDatabase()
60-
6159
/**
6260
* @see https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key
6361
*/

lib/storage/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ await fs.mkdir(bufferDir, {
2020
recursive: true,
2121
})
2222

23-
async function initializeStorageDriver() {
23+
let storageAdapter: StorageAdapter
24+
25+
export async function initializeStorageAdapter() {
2426
try {
2527
const driverName = ENV.STORAGE_DRIVER
2628
const driverSetup = getStorageDriver(driverName)
@@ -38,7 +40,7 @@ async function initializeStorageDriver() {
3840
const commitLocks = new Set<number>()
3941
const uploadChunkLocks = new Map<string, Promise<any>>()
4042

41-
return <StorageAdapter>{
43+
storageAdapter = {
4244
async reserveCache(key, version, cacheSize) {
4345
logger.debug('Reserve: Reserving cache for', key, version, cacheSize ?? '[no cache size]')
4446
const bufferKey = `${key}:${version}`
@@ -199,4 +201,6 @@ async function initializeStorageDriver() {
199201
}
200202
}
201203

202-
export const storageAdapter = await initializeStorageDriver()
204+
export function useStorageAdapter() {
205+
return storageAdapter
206+
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"better-sqlite3": "^11.1.2",
3131
"consola": "^3.2.3",
3232
"croner": "^8.1.0",
33+
"execa": "^9.3.1",
3334
"h3": "^1.12.0",
3435
"kysely": "^0.27.4",
3536
"minio": "^8.0.1",
@@ -66,5 +67,10 @@
6667
"ts-pattern": "^5.2.0",
6768
"vitest": "^2.0.4",
6869
"wait-on": "^8.0.0"
70+
},
71+
"pnpm": {
72+
"patchedDependencies": {
73+
"nitropack@2.9.7": "patches/nitropack@2.9.7.patch"
74+
}
6975
}
7076
}

patches/nitropack@2.9.7.patch

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
diff --git a/dist/runtime/app.mjs b/dist/runtime/app.mjs
2+
index 6e968f59e719851b0fc166bc02a30e41ebbb8e63..10d19fed1cf71f82d912be1210dc9ec906fe9d6f 100644
3+
--- a/dist/runtime/app.mjs
4+
+++ b/dist/runtime/app.mjs
5+
@@ -22,7 +22,7 @@ import { nitroAsyncContext } from "./context.mjs";
6+
import { plugins } from "#internal/nitro/virtual/plugins";
7+
import errorHandler from "#internal/nitro/virtual/error-handler";
8+
import { handlers } from "#internal/nitro/virtual/server-handlers";
9+
-function createNitroApp() {
10+
+async function createNitroApp() {
11+
const config = useRuntimeConfig();
12+
const hooks = createHooks();
13+
const captureError = (error, context = {}) => {
14+
@@ -140,7 +140,7 @@ function createNitroApp() {
15+
};
16+
for (const plugin of plugins) {
17+
try {
18+
- plugin(app);
19+
+ await plugin(app);
20+
} catch (err) {
21+
captureError(err, { tags: ["plugin"] });
22+
throw err;
23+
@@ -148,5 +148,5 @@ function createNitroApp() {
24+
}
25+
return app;
26+
}
27+
-export const nitroApp = createNitroApp();
28+
+export const nitroApp = await createNitroApp();
29+
export const useNitroApp = () => nitroApp;
30+
diff --git a/dist/runtime/plugin.d.ts b/dist/runtime/plugin.d.ts
31+
index a83e51dc732331862e8eac011e97ae784ee525b8..7907875fe883e837e9305acccdd4cd55a1bf3353 100644
32+
--- a/dist/runtime/plugin.d.ts
33+
+++ b/dist/runtime/plugin.d.ts
34+
@@ -1,6 +1,6 @@
35+
import type { NitroApp } from "./app";
36+
export interface NitroAppPlugin {
37+
- (nitro: NitroApp): void;
38+
+ (nitro: NitroApp): Promise<void> | void;
39+
}
40+
export declare function defineNitroPlugin(def: NitroAppPlugin): NitroAppPlugin;
41+
export declare const nitroPlugin: typeof defineNitroPlugin;

plugins/cleanup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { colorize } from 'consola/utils'
22
import { Cron } from 'croner'
33

4+
import { useStorageAdapter } from '~/lib/storage'
5+
46
import { ENV } from '@/lib/env'
57
import { logger } from '@/lib/logger'
6-
import { storageAdapter } from '@/lib/storage'
78

89
export default defineNitroPlugin(() => {
910
if (ENV.CLEANUP_OLDER_THAN_DAYS === 0) return
@@ -14,6 +15,6 @@ export default defineNitroPlugin(() => {
1415
`Cleaning up cache entries older than ${colorize('blue', `${ENV.CLEANUP_OLDER_THAN_DAYS}d`)} with schedule ${colorize('blue', job.getPattern() ?? '')}${nextRun ? ` (next run: ${nextRun.toLocaleString()})` : ''}`,
1516
)
1617
job.schedule(async () => {
17-
await storageAdapter.pruneCaches(ENV.CLEANUP_OLDER_THAN_DAYS)
18+
await useStorageAdapter().pruneCaches(ENV.CLEANUP_OLDER_THAN_DAYS)
1819
})
1920
})

plugins/setup.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { H3Error } from 'h3'
22

3+
import { initializeDatabase } from '~/lib/db'
34
import { ENV } from '~/lib/env'
45
import { logger } from '~/lib/logger'
6+
import { initializeStorageAdapter } from '~/lib/storage'
57

68
export default defineNitroPlugin(async (nitro) => {
79
logger.info(`🚀 Starting GitHub Actions Cache Server (${useRuntimeConfig().version})`)
810

9-
await import('~/lib/env')
10-
await import('~/lib/db')
11+
await initializeDatabase()
12+
await initializeStorageAdapter()
1113

1214
nitro.hooks.hook('error', (error, { event }) => {
1315
if (!event) {
@@ -31,6 +33,8 @@ export default defineNitroPlugin(async (nitro) => {
3133
)
3234
})
3335
}
36+
37+
if (process.send) process.send('nitro:ready')
3438
})
3539

3640
function obfuscateTokenFromPath(path: string) {

0 commit comments

Comments
 (0)