Skip to content

Commit

Permalink
refactor: remove dependency on /runtime/internal/* subpaths (#2524)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jun 13, 2024
1 parent d549634 commit 93d002b
Show file tree
Hide file tree
Showing 33 changed files with 88 additions and 57 deletions.
5 changes: 2 additions & 3 deletions src/core/config/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isTest, isDebug } from "std-env";
import type { NitroConfig } from "nitropack/types";
import { resolve } from "pathe";
import { resolve, join } from "pathe";
import { runtimeDir } from "nitropack/runtime/meta";

export const NitroDefaults: NitroConfig = {
Expand Down Expand Up @@ -58,8 +58,7 @@ export const NitroDefaults: NitroConfig = {
baseURL: process.env.NITRO_APP_BASE_URL || "/",
handlers: [],
devHandlers: [],
// errorHandler: "nitropack/runtime/internal/error",
errorHandler: "#internal/nitro/error",
errorHandler: join(runtimeDir, "internal/error"),
routeRules: {},
prerender: {
autoSubfolderIndex: true,
Expand Down
8 changes: 5 additions & 3 deletions src/core/config/resolvers/open-api.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { NitroOptions } from "nitropack/types";
import { runtimeDir } from "nitropack/runtime/meta";
import { join } from "pathe";

export async function resolveOpenAPIOptions(options: NitroOptions) {
if (options.dev && options.experimental.openAPI) {
options.handlers.push({
route: "/_nitro/openapi.json",
handler: "nitropack/runtime/internal/routes/openapi",
handler: join(runtimeDir, "internal/routes/openapi"),
});
options.handlers.push({
route: "/_nitro/scalar",
handler: "nitropack/runtime/internal/routes/scalar",
handler: join(runtimeDir, "internal/routes/scalar"),
});
options.handlers.push({
route: "/_nitro/swagger",
handler: "nitropack/runtime/internal/routes/swagger",
handler: join(runtimeDir, "internal/routes/swagger"),
});
}
}
6 changes: 4 additions & 2 deletions src/core/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { installModules } from "./module";
import { updateNitroConfig } from "./config/update";
import { addNitroTasksVirtualFile } from "./task";
import { scanAndSyncOptions } from "./scan";
import { join } from "pathe";
import { runtimeDir } from "nitropack/runtime/meta";

export async function createNitro(
config: NitroConfig = {},
Expand Down Expand Up @@ -48,10 +50,10 @@ export async function createNitro(
// Debug and timing
if (nitro.options.debug) {
createDebugger(nitro.hooks, { tag: "nitro" });
nitro.options.plugins.push("nitropack/runtime/internal/debug");
nitro.options.plugins.push(join(runtimeDir, "internal/debug"));
}
if (nitro.options.timing) {
nitro.options.plugins.push("nitropack/runtime/internal/timing");
nitro.options.plugins.push(join(runtimeDir, "internal/timing"));
}

// Logger
Expand Down
6 changes: 3 additions & 3 deletions src/presets/_nitro/runtime/nitro-dev.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitropack/runtime";
import { trapUnhandledNodeErrors } from "nitropack/runtime/internal/utils";
import { runTask } from "nitropack/runtime/internal/task";
import { startScheduleRunner } from "nitropack/runtime/internal/task";
import { trapUnhandledNodeErrors } from "nitropack/runtime/internal";
import { runTask } from "nitropack/runtime";
import { startScheduleRunner } from "nitropack/runtime/internal";
import { tasks, scheduledTasks } from "#nitro-internal-virtual/tasks";

import { Server } from "node:http";
Expand Down
2 changes: 1 addition & 1 deletion src/presets/_nitro/runtime/nitro-prerenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitropack/runtime";
import { trapUnhandledNodeErrors } from "nitropack/runtime/internal/utils";
import { trapUnhandledNodeErrors } from "nitropack/runtime/internal";

const nitroApp = useNitroApp();

Expand Down
2 changes: 1 addition & 1 deletion src/presets/aws-lambda/runtime/aws-lambda-streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useNitroApp } from "nitropack/runtime";
import {
normalizeLambdaIncomingHeaders,
normalizeLambdaOutgoingHeaders,
} from "nitropack/runtime/internal/utils.lambda";
} from "nitropack/runtime/internal";

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
Expand Down
4 changes: 2 additions & 2 deletions src/presets/aws-lambda/runtime/aws-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
normalizeLambdaIncomingHeaders,
normalizeLambdaOutgoingBody,
normalizeLambdaOutgoingHeaders,
} from "nitropack/runtime/internal/utils.lambda";
import { normalizeCookieHeader } from "nitropack/runtime/internal/utils";
normalizeCookieHeader,
} from "nitropack/runtime/internal";

const nitroApp = useNitroApp();

Expand Down
6 changes: 4 additions & 2 deletions src/presets/azure/runtime/azure-functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitropack/runtime";
import { getAzureParsedCookiesFromHeaders } from "nitropack/runtime/internal/utils.azure";
import { normalizeLambdaOutgoingHeaders } from "nitropack/runtime/internal/utils.lambda";
import {
getAzureParsedCookiesFromHeaders,
normalizeLambdaOutgoingHeaders,
} from "nitropack/runtime/internal";

import type { HttpRequest, HttpResponse } from "@azure/functions";

Expand Down
6 changes: 4 additions & 2 deletions src/presets/azure/runtime/azure-swa.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitropack/runtime";
import { getAzureParsedCookiesFromHeaders } from "nitropack/runtime/internal/utils.azure";
import { normalizeLambdaOutgoingHeaders } from "nitropack/runtime/internal/utils.lambda";
import {
getAzureParsedCookiesFromHeaders,
normalizeLambdaOutgoingHeaders,
} from "nitropack/runtime/internal";

import type { HttpResponse, HttpRequest } from "@azure/functions";
import { parseURL } from "ufo";
Expand Down
2 changes: 1 addition & 1 deletion src/presets/bun/runtime/bun.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitropack/runtime";
import { startScheduleRunner } from "nitropack/runtime/internal/task";
import { startScheduleRunner } from "nitropack/runtime/internal";

import type {} from "bun";
import wsAdapter from "crossws/adapters/bun";
Expand Down
6 changes: 2 additions & 4 deletions src/presets/cloudflare/runtime/cloudflare-module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import "#nitro-internal-pollyfills";
import { requestHasBody } from "nitropack/runtime/internal/utils";
import { useNitroApp } from "nitropack/runtime";
import { useRuntimeConfig } from "nitropack/runtime";
import { runCronTasks } from "nitropack/runtime/internal/task";
import { requestHasBody, runCronTasks } from "nitropack/runtime/internal";
import { useNitroApp, useRuntimeConfig } from "nitropack/runtime";
import { getPublicAssetMeta } from "#nitro-internal-virtual/public-assets";

import { withoutBase } from "ufo";
Expand Down
3 changes: 1 addition & 2 deletions src/presets/cloudflare/runtime/cloudflare-pages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "#nitro-internal-pollyfills";
import { runCronTasks } from "nitropack/runtime/internal/task";
import { requestHasBody } from "nitropack/runtime/internal/utils";
import { runCronTasks, requestHasBody } from "nitropack/runtime/internal";
import { useNitroApp } from "nitropack/runtime";
import { isPublicAssetURL } from "#nitro-internal-virtual/public-assets";

Expand Down
5 changes: 2 additions & 3 deletions src/presets/cloudflare/runtime/cloudflare-worker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "#nitro-internal-pollyfills";
import { requestHasBody } from "nitropack/runtime/internal/utils";
import { useNitroApp } from "nitropack/runtime";
import { useRuntimeConfig } from "nitropack/runtime";
import { requestHasBody } from "nitropack/runtime/internal";
import { useNitroApp, useRuntimeConfig } from "nitropack/runtime";
import { getPublicAssetMeta } from "#nitro-internal-virtual/public-assets";

import { withoutBase } from "ufo";
Expand Down
3 changes: 0 additions & 3 deletions src/presets/deno/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const denoDeploy = defineNitroPreset(
deploy:
"cd ./ && deployctl deploy --project=<project_name> server/index.ts",
},
unenv: {
polyfill: ["nitropack/runtime/internal/polyfill/deno-env"],
},
rollupConfig: {
preserveEntrySignatures: false,
external: (id) => id.startsWith("https://"),
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/presets/deno/runtime/deno-deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "#nitro-internal-pollyfills";
import "./_deno-env-polyfill";
import { useNitroApp } from "nitropack/runtime";

import wsAdapter from "crossws/adapters/deno";
Expand Down
3 changes: 2 additions & 1 deletion src/presets/deno/runtime/deno-server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import "#nitro-internal-pollyfills";
import "./_deno-env-polyfill";
import { useNitroApp } from "nitropack/runtime";
import { useRuntimeConfig } from "nitropack/runtime";
import { startScheduleRunner } from "nitropack/runtime/internal/task";
import { startScheduleRunner } from "nitropack/runtime/internal";

import destr from "destr";
import wsAdapter from "crossws/adapters/deno";
Expand Down
3 changes: 0 additions & 3 deletions src/presets/netlify/legacy/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ const netlifyEdge = defineNitroPreset(
format: "esm",
},
},
unenv: {
polyfill: ["nitropack/runtime/internal/polyfill/deno-env"],
},
hooks: {
"rollup:before": (nitro: Nitro) => {
deprecateSWR(nitro);
Expand Down
1 change: 1 addition & 0 deletions src/presets/netlify/legacy/runtime/_deno-env-polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(process.env, Deno.env.toObject());
1 change: 1 addition & 0 deletions src/presets/netlify/legacy/runtime/netlify-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "#nitro-internal-pollyfills";
import "./_deno-env-polyfill";

import { builder } from "@netlify/functions";
import { lambda } from "./netlify-lambda";
Expand Down
2 changes: 2 additions & 0 deletions src/presets/netlify/legacy/runtime/netlify-edge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import "#nitro-internal-pollyfills";
import "./_deno-env-polyfill";

import { useNitroApp } from "nitropack/runtime";
import { isPublicAssetURL } from "#nitro-internal-virtual/public-assets";

Expand Down
5 changes: 3 additions & 2 deletions src/presets/netlify/legacy/runtime/netlify-lambda.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import "#nitro-internal-pollyfills";
import "./_deno-env-polyfill";
import { useNitroApp } from "nitropack/runtime";
import { normalizeCookieHeader } from "nitropack/runtime/internal/utils";
import {
normalizeCookieHeader,
normalizeLambdaIncomingHeaders,
normalizeLambdaOutgoingBody,
normalizeLambdaOutgoingHeaders,
} from "nitropack/runtime/internal/utils.lambda";
} from "nitropack/runtime/internal";

import { withQuery } from "ufo";
import type {
Expand Down
4 changes: 2 additions & 2 deletions src/presets/netlify/legacy/runtime/netlify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "#nitro-internal-pollyfills";
import { getRouteRulesForPath } from "nitropack/runtime/internal/route-rules";

import "./_deno-env-polyfill";
import { getRouteRulesForPath } from "nitropack/runtime/internal";
import { withQuery } from "ufo";
import type { Handler } from "@netlify/functions/dist/main";
import { lambda } from "./netlify-lambda";
Expand Down
3 changes: 0 additions & 3 deletions src/presets/netlify/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ const netlifyEdge = defineNitroPreset(
format: "esm",
},
},
unenv: {
polyfill: ["nitropack/runtime/internal/polyfill/deno-env"],
},
hooks: {
async compiled(nitro: Nitro) {
await writeHeaders(nitro);
Expand Down
1 change: 1 addition & 0 deletions src/presets/netlify/runtime/_deno-env-polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(process.env, Deno.env.toObject());
5 changes: 3 additions & 2 deletions src/presets/netlify/runtime/netlify.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import "#nitro-internal-pollyfills";
import "./_deno-env-polyfill";
import { useNitroApp } from "nitropack/runtime";
import { getRouteRulesForPath } from "nitropack/runtime/internal/route-rules";
import {
getRouteRulesForPath,
joinHeaders,
normalizeCookieHeader,
} from "nitropack/runtime/internal/utils";
} from "nitropack/runtime/internal";

const nitroApp = useNitroApp();

Expand Down
6 changes: 4 additions & 2 deletions src/presets/node/runtime/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os from "node:os";
import cluster from "node:cluster";
import { getGracefulShutdownConfig } from "nitropack/runtime/internal/shutdown";
import { trapUnhandledNodeErrors } from "nitropack/runtime/internal/utils";
import {
trapUnhandledNodeErrors,
getGracefulShutdownConfig,
} from "nitropack/runtime/internal";

function runMaster() {
const numberOfWorkers =
Expand Down
6 changes: 4 additions & 2 deletions src/presets/node/runtime/node-listener.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import "#nitro-internal-pollyfills";
import { toNodeListener } from "h3";
import { useNitroApp } from "nitropack/runtime";
import { trapUnhandledNodeErrors } from "nitropack/runtime/internal/utils";
import { startScheduleRunner } from "nitropack/runtime/internal/task";
import {
startScheduleRunner,
trapUnhandledNodeErrors,
} from "nitropack/runtime/internal";

const nitroApp = useNitroApp();

Expand Down
11 changes: 6 additions & 5 deletions src/presets/node/runtime/node-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { Server as HttpsServer } from "node:https";
import destr from "destr";
import { toNodeListener } from "h3";
import wsAdapter from "crossws/adapters/node";
import { useNitroApp } from "nitropack/runtime";
import { setupGracefulShutdown } from "nitropack/runtime/internal/shutdown";
import { trapUnhandledNodeErrors } from "nitropack/runtime/internal/utils";
import { useRuntimeConfig } from "nitropack/runtime";
import { startScheduleRunner } from "nitropack/runtime/internal/task";
import { useNitroApp, useRuntimeConfig } from "nitropack/runtime";
import {
setupGracefulShutdown,
startScheduleRunner,
trapUnhandledNodeErrors,
} from "nitropack/runtime/internal";

const cert = process.env.NITRO_SSL_CERT;
const key = process.env.NITRO_SSL_KEY;
Expand Down
2 changes: 1 addition & 1 deletion src/presets/stormkit/runtime/stormkit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitropack/runtime";
import { normalizeLambdaOutgoingBody } from "nitropack/runtime/internal/utils.lambda";
import { normalizeLambdaOutgoingBody } from "nitropack/runtime/internal";

import type { Handler } from "aws-lambda";

Expand Down
2 changes: 1 addition & 1 deletion src/presets/winterjs/runtime/winterjs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck TODO: Remove after removing polyfills
import "#nitro-internal-pollyfills";
import { toBuffer } from "nitropack/runtime/internal/utils";
import { toBuffer } from "nitropack/runtime/internal";
import { useNitroApp } from "nitropack/runtime";
import { toPlainHandler } from "h3";
import { hasProtocol, joinURL } from "ufo";
Expand Down
4 changes: 3 additions & 1 deletion src/rollup/plugins/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
NitroEventHandler,
} from "nitropack/types";
import { virtual } from "./virtual";
import { join } from "pathe";
import { runtimeDir } from "nitropack/runtime/meta";

export function handlers(nitro: Nitro) {
const getHandlers = () => {
Expand Down Expand Up @@ -36,7 +38,7 @@ export function handlers(nitro: Nitro) {
if (nitro.options.serveStatic) {
handlers.unshift({
middleware: true,
handler: "nitropack/runtime/internal/static",
handler: join(runtimeDir, "internal/static"),
});
}
if (nitro.options.renderer) {
Expand Down
21 changes: 21 additions & 0 deletions src/runtime/internal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Limited INTERNAL exports used by the presets runtime
// Please don't use these in your project code!

export {
trapUnhandledNodeErrors,
normalizeCookieHeader,
requestHasBody,
joinHeaders,
toBuffer,
} from "./utils";

export {
normalizeLambdaIncomingHeaders,
normalizeLambdaOutgoingHeaders,
normalizeLambdaOutgoingBody,
} from "./utils.lambda";

export { startScheduleRunner, runCronTasks } from "./task";
export { getAzureParsedCookiesFromHeaders } from "./utils.azure";
export { getGracefulShutdownConfig, setupGracefulShutdown } from "./shutdown";
export { getRouteRulesForPath } from "./route-rules";

0 comments on commit 93d002b

Please sign in to comment.