From 5fecd716677e8a315ed1cbd954e1c68042f04fd0 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sun, 12 Nov 2023 13:33:43 +1100 Subject: [PATCH 1/7] fix: initialize nitro app before plugins run --- examples/plugins/plugins/fetch.ts | 3 ++ examples/plugins/routes/use-nitro-app.ts | 5 +++ src/runtime/app.ts | 46 ++++++++++++------------ test/fixture/types.ts | 1 - 4 files changed, 30 insertions(+), 25 deletions(-) create mode 100644 examples/plugins/plugins/fetch.ts create mode 100644 examples/plugins/routes/use-nitro-app.ts diff --git a/examples/plugins/plugins/fetch.ts b/examples/plugins/plugins/fetch.ts new file mode 100644 index 0000000000..9497b92da9 --- /dev/null +++ b/examples/plugins/plugins/fetch.ts @@ -0,0 +1,3 @@ +export default defineNitroPlugin((_nitroApp) => { + _nitroApp.localFetch('/') +}); diff --git a/examples/plugins/routes/use-nitro-app.ts b/examples/plugins/routes/use-nitro-app.ts new file mode 100644 index 0000000000..061e7107c3 --- /dev/null +++ b/examples/plugins/routes/use-nitro-app.ts @@ -0,0 +1,5 @@ +import { eventHandler } from "h3"; + +export default eventHandler(() => { + nitroApp: !!useNitroApp() +}); diff --git a/src/runtime/app.ts b/src/runtime/app.ts index edb5b31d2b..37558b77b3 100644 --- a/src/runtime/app.ts +++ b/src/runtime/app.ts @@ -3,31 +3,28 @@ import { createApp, createRouter, eventHandler, - lazyEventHandler, - Router, - toNodeListener, fetchWithEvent, H3Error, - isEvent, H3Event, + isEvent, + lazyEventHandler, + Router, + toNodeListener, } from "h3"; -import { createFetch, Headers } from "ofetch"; +import {createFetch, Headers} from "ofetch"; import destr from "destr"; -import { - createCall, - createFetch as createLocalFetch, -} from "unenv/runtime/fetch/index"; -import { createHooks, Hookable } from "hookable"; -import type { NitroRuntimeHooks, CaptureError } from "./types"; -import { useRuntimeConfig } from "./config"; -import { cachedEventHandler } from "./cache"; -import { normalizeFetchResponse } from "./utils"; -import { createRouteRulesHandler, getRouteRulesForPath } from "./route-rules"; -import { NitroAsyncContext, nitroAsyncContext } from "./context"; -import type { $Fetch, NitroFetchRequest } from "nitropack"; -import { plugins } from "#internal/nitro/virtual/plugins"; +import {createCall, createFetch as createLocalFetch,} from "unenv/runtime/fetch/index"; +import {createHooks, Hookable} from "hookable"; +import type {CaptureError, NitroRuntimeHooks} from "./types"; +import {useRuntimeConfig} from "./config"; +import {cachedEventHandler} from "./cache"; +import {normalizeFetchResponse} from "./utils"; +import {createRouteRulesHandler, getRouteRulesForPath} from "./route-rules"; +import {NitroAsyncContext, nitroAsyncContext} from "./context"; +import type {$Fetch, NitroFetchRequest} from "nitropack"; +import {plugins} from "#internal/nitro/virtual/plugins"; import errorHandler from "#internal/nitro/virtual/error-handler"; -import { handlers } from "#internal/nitro/virtual/server-handlers"; +import {handlers} from "#internal/nitro/virtual/server-handlers"; export interface NitroApp { h3App: H3App; @@ -182,7 +179,7 @@ function createNitroApp(): NitroApp { }; } - const app: NitroApp = { + return { hooks, h3App, router, @@ -190,19 +187,20 @@ function createNitroApp(): NitroApp { localFetch, captureError, }; +} +function runPlugins(app: NitroApp) { for (const plugin of plugins) { try { plugin(app); } catch (err) { - captureError(err, { tags: ["plugin"] }); + app.captureError(err, { tags: ["plugin"] }); throw err; } } - - return app; } export const nitroApp: NitroApp = createNitroApp(); - export const useNitroApp = () => nitroApp; + +runPlugins(nitroApp) diff --git a/test/fixture/types.ts b/test/fixture/types.ts index fc41544e2a..1b98e3b7fa 100644 --- a/test/fixture/types.ts +++ b/test/fixture/types.ts @@ -1,7 +1,6 @@ import { expectTypeOf } from "expect-type"; import { describe, it } from "vitest"; import { EventHandler, EventHandlerRequest, defineEventHandler } from "h3"; -import { $Fetch } from "../.."; import { defineNitroConfig } from "../../src/config"; import { Serialize, Simplify } from "../../src/types"; From 49bb77256b27f8b1f9e14f8f06f2b66ee6191d2b Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sun, 12 Nov 2023 13:36:24 +1100 Subject: [PATCH 2/7] chore: minor fixes --- examples/plugins/routes/use-nitro-app.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/plugins/routes/use-nitro-app.ts b/examples/plugins/routes/use-nitro-app.ts index 061e7107c3..d46846bd82 100644 --- a/examples/plugins/routes/use-nitro-app.ts +++ b/examples/plugins/routes/use-nitro-app.ts @@ -1,5 +1,5 @@ import { eventHandler } from "h3"; -export default eventHandler(() => { +export default eventHandler(() => ({ nitroApp: !!useNitroApp() -}); +})); From fd87b1445883c08ff359f7837dc4ea1b092d6ebf Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sun, 12 Nov 2023 13:39:22 +1100 Subject: [PATCH 3/7] chore: lint --- src/runtime/app.ts | 30 +++++++++++++++++------------- test/fixture/types.ts | 1 + 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/runtime/app.ts b/src/runtime/app.ts index 37558b77b3..98524d594f 100644 --- a/src/runtime/app.ts +++ b/src/runtime/app.ts @@ -11,20 +11,23 @@ import { Router, toNodeListener, } from "h3"; -import {createFetch, Headers} from "ofetch"; +import { createFetch, Headers } from "ofetch"; import destr from "destr"; -import {createCall, createFetch as createLocalFetch,} from "unenv/runtime/fetch/index"; -import {createHooks, Hookable} from "hookable"; -import type {CaptureError, NitroRuntimeHooks} from "./types"; -import {useRuntimeConfig} from "./config"; -import {cachedEventHandler} from "./cache"; -import {normalizeFetchResponse} from "./utils"; -import {createRouteRulesHandler, getRouteRulesForPath} from "./route-rules"; -import {NitroAsyncContext, nitroAsyncContext} from "./context"; -import type {$Fetch, NitroFetchRequest} from "nitropack"; -import {plugins} from "#internal/nitro/virtual/plugins"; +import { + createCall, + createFetch as createLocalFetch, +} from "unenv/runtime/fetch/index"; +import { createHooks, Hookable } from "hookable"; +import type { CaptureError, NitroRuntimeHooks } from "./types"; +import { useRuntimeConfig } from "./config"; +import { cachedEventHandler } from "./cache"; +import { normalizeFetchResponse } from "./utils"; +import { createRouteRulesHandler, getRouteRulesForPath } from "./route-rules"; +import { NitroAsyncContext, nitroAsyncContext } from "./context"; +import type { $Fetch, NitroFetchRequest } from "nitropack"; +import { plugins } from "#internal/nitro/virtual/plugins"; import errorHandler from "#internal/nitro/virtual/error-handler"; -import {handlers} from "#internal/nitro/virtual/server-handlers"; +import { handlers } from "#internal/nitro/virtual/server-handlers"; export interface NitroApp { h3App: H3App; @@ -190,6 +193,7 @@ function createNitroApp(): NitroApp { } function runPlugins(app: NitroApp) { + console.log('calling plugins', plugins) for (const plugin of plugins) { try { plugin(app); @@ -203,4 +207,4 @@ function runPlugins(app: NitroApp) { export const nitroApp: NitroApp = createNitroApp(); export const useNitroApp = () => nitroApp; -runPlugins(nitroApp) +runPlugins(nitroApp); diff --git a/test/fixture/types.ts b/test/fixture/types.ts index 1b98e3b7fa..fc41544e2a 100644 --- a/test/fixture/types.ts +++ b/test/fixture/types.ts @@ -1,6 +1,7 @@ import { expectTypeOf } from "expect-type"; import { describe, it } from "vitest"; import { EventHandler, EventHandlerRequest, defineEventHandler } from "h3"; +import { $Fetch } from "../.."; import { defineNitroConfig } from "../../src/config"; import { Serialize, Simplify } from "../../src/types"; From 3db386619bff241b4439005f3fa69db4a793682d Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Sun, 12 Nov 2023 13:39:59 +1100 Subject: [PATCH 4/7] chore: remove console.log --- src/runtime/app.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/runtime/app.ts b/src/runtime/app.ts index 98524d594f..f933ccf208 100644 --- a/src/runtime/app.ts +++ b/src/runtime/app.ts @@ -193,7 +193,6 @@ function createNitroApp(): NitroApp { } function runPlugins(app: NitroApp) { - console.log('calling plugins', plugins) for (const plugin of plugins) { try { plugin(app); From 76341ca10d3217a866042d0f115c09564be0e85b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 20 Sep 2024 14:02:47 +0200 Subject: [PATCH 5/7] add compat date --- examples/plugins/nitro.config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/plugins/nitro.config.ts b/examples/plugins/nitro.config.ts index 1c68bd7097..d204ad9cde 100644 --- a/examples/plugins/nitro.config.ts +++ b/examples/plugins/nitro.config.ts @@ -1,3 +1,4 @@ export default defineNitroConfig({ plugins: ["~/plugins/test"], -}); + compatibilityDate: "2024-09-20", +}); \ No newline at end of file From fe6f96591098121915e112b5158c79356911f0d3 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 20 Sep 2024 14:05:08 +0200 Subject: [PATCH 6/7] manual rebase --- src/runtime/internal/app.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/runtime/internal/app.ts b/src/runtime/internal/app.ts index adeb3f65eb..b04b08c099 100644 --- a/src/runtime/internal/app.ts +++ b/src/runtime/internal/app.ts @@ -185,18 +185,24 @@ function createNitroApp(): NitroApp { captureError, }; + return app; +} + +function runNitroPlugins(nitroApp: NitroApp) { for (const plugin of plugins) { try { - plugin(app); + plugin(nitroApp); } catch (error: any) { - captureError(error, { tags: ["plugin"] }); + nitroApp.captureError(error, { tags: ["plugin"] }); throw error; } } - - return app; } export const nitroApp: NitroApp = createNitroApp(); -export const useNitroApp = () => nitroApp; +export function useNitroApp() { + return nitroApp; +} + +runNitroPlugins(nitroApp); From 4c63a4ed0b98f80839bdf9f5c1e73acdb7fe79de Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 20 Sep 2024 14:08:51 +0200 Subject: [PATCH 7/7] revert example updates --- examples/plugins/nitro.config.ts | 3 +-- examples/plugins/plugins/fetch.ts | 3 --- examples/plugins/routes/use-nitro-app.ts | 5 ----- 3 files changed, 1 insertion(+), 10 deletions(-) delete mode 100644 examples/plugins/plugins/fetch.ts delete mode 100644 examples/plugins/routes/use-nitro-app.ts diff --git a/examples/plugins/nitro.config.ts b/examples/plugins/nitro.config.ts index d204ad9cde..1c68bd7097 100644 --- a/examples/plugins/nitro.config.ts +++ b/examples/plugins/nitro.config.ts @@ -1,4 +1,3 @@ export default defineNitroConfig({ plugins: ["~/plugins/test"], - compatibilityDate: "2024-09-20", -}); \ No newline at end of file +}); diff --git a/examples/plugins/plugins/fetch.ts b/examples/plugins/plugins/fetch.ts deleted file mode 100644 index 9497b92da9..0000000000 --- a/examples/plugins/plugins/fetch.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default defineNitroPlugin((_nitroApp) => { - _nitroApp.localFetch('/') -}); diff --git a/examples/plugins/routes/use-nitro-app.ts b/examples/plugins/routes/use-nitro-app.ts deleted file mode 100644 index d46846bd82..0000000000 --- a/examples/plugins/routes/use-nitro-app.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { eventHandler } from "h3"; - -export default eventHandler(() => ({ - nitroApp: !!useNitroApp() -}));