Skip to content

Commit

Permalink
Merge branch 'main' into test/vitest-1
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Oct 5, 2023
2 parents 8d264b0 + 6c51546 commit f847478
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 56 deletions.
22 changes: 22 additions & 0 deletions docs/content/2.deploy/providers/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,25 @@ With `cloudflare_module` preset, you need to add the following rule to your `wra
[site]
bucket = ".output/public"
```

### Local Wrangler Dev builds

By default `wrangler dev` requires nitro to be built before it can be served by wrangler.

This can become tiresome if you're making changes to your nitro app and keep rebuilding to test changes in wrangler.

::alert{type="warning"}
This is a temporary workaround until nitro has better support for wrangler dev mode!
::

To instruct wrangler to automatically rebuild nitro when it detects file changes, you need to add the following rule to your `wrangler.toml` file:

```[wrangler.toml]
+ [env.development.build]
+ command = "NITRO_PRESET=cloudflare npm run build" // Replace npm with your packagemanager (npm, pnpm, yarn, bun)
+ cwd = "./"
+ watch_dir = ["./routes", "./nitro.config.ts"]
```

Now you need to run wrangler in development mode using `wrangler dev --env development`
When files change in nitro, wrangler will rebuild and serve the new files
20 changes: 11 additions & 9 deletions docs/content/2.deploy/providers/iis.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ Deploy Nitro apps to IIS.
This is an experimental preset.
::

## Using [IISnode](https://github.com/Azure/iisnode) (recommended)
## Using [IISnode](https://github.com/Azure/iisnode)

**Preset:** `iis_node` ([switch to this preset](/deploy/#changing-the-deployment-preset))

1. Install [IISnode](https://github.com/azure/iisnode/releases), and the [IIS URL Rewrite Module](https://www.iis.net/downloads/microsoft/url-rewrite).
2. In IIS, add `.mjs` as a new mime type and set its content type to `application/javascript`.
3. Deploy the contents of your `.output` folder to your website in IIS.
1. Install the latest LTS version of [Node.js](https://nodejs.org/en/) on your Windows Server.
1. Install [IISnode](https://github.com/azure/iisnode/releases)
2. Install [IIS `URLRewrite` Module](https://www.iis.net/downloads/microsoft/url-rewrite).
3. In IIS, add `.mjs` as a new mime type and set its content type to `application/javascript`.
4. Deploy the contents of your `.output` folder to your website in IIS.

## Using IIS directly
## Using IIS Handler

**Preset:** `iis-handler` ([switch to this preset](/deploy/#changing-the-deployment-preset))
**Preset:** `iis_handler` / `iis` ([switch to this preset](/deploy/#changing-the-deployment-preset))

If you do not wish to use IISnode, you can use IIS directly.
You can use IIS http handler directly.

1. Make sure that [Node.js](https://nodejs.org/en/) is installed on your Windows Server.
2. Make sure [`HttpPlatformHandler` Module](https://www.iis.net/downloads/microsoft/httpplatformhandler) is installed.
1. Install the latest LTS version of [Node.js](https://nodejs.org/en/) on your Windows Server.
2. Install [IIS `HttpPlatformHandler` Module](https://www.iis.net/downloads/microsoft/httpplatformhandler)
3. Copy your `.output` directory into the Windows Server, and create a website on IIS pointing to that exact directory.

## IIS Config options
Expand Down
2 changes: 1 addition & 1 deletion docs/content/3.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Enable WASM support

#### `legacyExternals`

When enabled, lagacy (unstable) experimental rollup externals algorithm will be used.
When enabled, legacy (unstable) experimental rollup externals algorithm will be used.

### `future`

Expand Down
114 changes: 73 additions & 41 deletions src/presets/iis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { resolveFile, writeFile } from "../utils";
import { defineNitroPreset } from "../preset";
import type { Nitro } from "../types";

export const iis = defineNitroPreset({
export const iisHandler = defineNitroPreset({
extends: "node-server",
hooks: {
async compiled(nitro: Nitro) {
Expand All @@ -16,6 +16,8 @@ export const iis = defineNitroPreset({
},
});

export const iis = iisHandler;

export const iisNode = defineNitroPreset({
extends: "node-server",
hooks: {
Expand All @@ -27,7 +29,13 @@ export const iisNode = defineNitroPreset({

await writeFile(
resolve(nitro.options.output.dir, "index.js"),
"import('./server/index.mjs');"
`
if (process.env.PORT.startsWith('\\')) {
process.env.NITRO_UNIX_SOCKET = process.env.PORT
delete process.env.PORT
}
import('./server/index.mjs');
`
);
},
},
Expand All @@ -36,45 +44,69 @@ export const iisNode = defineNitroPreset({
async function iisnodeXmlTemplate(nitro: Nitro) {
const path = resolveFile("web.config", nitro.options.rootDir, ["config"]);
const originalString = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server\\/debug[\\/]?" />
</rule>
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="index.js"/>
</rule>
</rules>
</rewrite>
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
<add segment="node_modules"/>
</hiddenSegments>
</requestFiltering>
</security>
<httpErrors existingResponse="PassThrough" />
<iisnode watchedFiles="web.config;*.js" node_env="production" debuggingEnabled="true" />
</system.webServer>
</configuration>
<!--
This configuration file is required if iisnode is used to run node processes behind
IIS or IIS Express. For more information, visit:
https://github.com/Azure/iisnode/blob/master/src/samples/configuration/web.config
-->
<configuration>
<system.webServer>
<!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
<webSocket enabled="false" />
<handlers>
<!-- Indicates that the index.js file is a Node.js site to be handled by the iisnode module -->
<add name="iisnode" path="index.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<!-- Do not interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^index.js/debug[/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{PATH_INFO}" />
</rule>
<!-- All other URLs are mapped to the Node.js site entrypoint -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
</conditions>
<action type="Rewrite" url="index.js" />
</rule>
</rules>
</rewrite>
<!-- 'bin' directory has no special meaning in Node.js and apps can be placed in it -->
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin" />
</hiddenSegments>
<requestLimits maxAllowedContentLength="4294967295" />
</requestFiltering>
</security>
<!-- Make sure error responses are left untouched -->
<httpErrors existingResponse="PassThrough" />
<!--
You can control how Node is hosted within IIS using the following options:
* watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
* node_env: will be propagated to node as NODE_ENV environment variable
* debuggingEnabled - controls whether the built-in debugger is enabled
See https://github.com/Azure/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
-->
<iisnode
watchedFiles="index.js"
node_env="production"
debuggingEnabled="false"
loggingEnabled="false"
/>
</system.webServer>
</configuration>
`;
if (path !== undefined) {
const fileString = await readFile(path, "utf8");
Expand Down
2 changes: 1 addition & 1 deletion src/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export * from "./cleavr";
export * from "./layer0";
export * from "./flightcontrol";
export * from "./lagon";
export { iis, iisNode } from "./iis";
export { iis, iisHandler, iisNode } from "./iis";
export { _static as static } from "./static";
export * from "./github-pages";
2 changes: 1 addition & 1 deletion src/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = unknown[]>(
// Normalize cache params
const group = opts.group || "nitro/functions";
const name = opts.name || fn.name || "_";
const integrity = hash([opts.integrity, fn, opts]);
const integrity = opts.integrity || hash([fn, opts]);
const validate = opts.validate || (() => true);

async function get(
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/entries/aws-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export async function handler(
headers: normalizeLambdaIncomingHeaders(event.headers),
method,
query,
body: event.body, // TODO: handle event.isBase64Encoded
body: event.isBase64Encoded
? Buffer.from(event.body, "base64").toString("utf8")
: event.body,
});

// ApiGateway v2 https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.v2
Expand Down
3 changes: 2 additions & 1 deletion test/presets/nitro-dev.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, it, expect } from "vitest";
import { isCI } from "std-env";
import { setupTest, testNitro } from "../tests";

describe("nitro:preset:nitro-dev", async () => {
describe.skipIf(isCI)("nitro:preset:nitro-dev", async () => {
const ctx = await setupTest("nitro-dev");
testNitro(
ctx,
Expand Down
3 changes: 2 additions & 1 deletion test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ export function testNitro(
!ctx.nitro.options.node ||
// TODO: Investigate
ctx.preset === "bun" ||
ctx.preset === "deno-server"
ctx.preset === "deno-server" ||
ctx.preset === "nitro-dev"
)("sourcemap works", async () => {
const { data } = await callHandler({ url: "/error-stack" });
expect(data.stack).toMatch("test/fixture/routes/error-stack.ts:4:1");
Expand Down

0 comments on commit f847478

Please sign in to comment.