Skip to content

Commit

Permalink
fix(iis): merge web.config (#1658)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
Hardaker587 and pi0 authored Sep 7, 2023
1 parent 86d05f2 commit 5639f5b
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 76 deletions.
37 changes: 34 additions & 3 deletions docs/content/2.deploy/providers/iis.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ This is an experimental 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.

3. Deploy the contents of your `.output` folder to your website in IIS.

## Using IIS directly

Expand All @@ -23,4 +22,36 @@ If you do not wish to use IISnode, you can use IIS 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.
3. Copy your `.output` directory into the Windows Server, and create a website on IIS pointing to that exact directory.
3. Copy your `.output` directory into the Windows Server, and create a website on IIS pointing to that exact directory.

## IIS Config options

::code-group

```ts [nitro.config.ts]
export default defineNitroConfig({
// IIS options default
iis: {
// merges in a pre-exisiting web.config file to the nitro default file
mergeConfig: true,
// overrides the default nitro web.config file all together
overrideConfig: false,
},
});
```

```ts [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
// IIS options default
iis: {
// merges in a pre-exisiting web.config file to the nitro default file
mergeConfig: true,
// overrides the default nitro web.config file all together
overrideConfig: false,
},
},
});
```

::
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,16 @@
"typescript": "^5.2.2",
"unbuild": "^2.0.0",
"undici": "^5.23.0",
"vitest": "^0.34.3"
"vitest": "^0.34.3",
"xml2js": "^0.6.2"
},
"peerDependencies": {
"xml2js": "^0.6.2"
},
"peerDependenciesMeta": {
"xml2js": {
"optional": true
}
},
"packageManager": "pnpm@8.7.0",
"engines": {
Expand All @@ -163,4 +172,4 @@
]
}
}
}
}
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 0 additions & 65 deletions src/presets/iis-node.ts

This file was deleted.

125 changes: 121 additions & 4 deletions src/presets/iis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFile } from "node:fs/promises";
import { resolve } from "pathe";
import { writeFile } from "../utils";
import { resolveFile, writeFile } from "../utils";
import { defineNitroPreset } from "../preset";
import type { Nitro } from "../types";

Expand All @@ -9,14 +10,91 @@ export const iis = defineNitroPreset({
async compiled(nitro: Nitro) {
await writeFile(
resolve(nitro.options.output.dir, "web.config"),
iisXmlTemplate()
await iisXmlTemplate(nitro)
);
},
},
});

function iisXmlTemplate() {
return `<?xml version="1.0" encoding="UTF-8"?>
export const iisNode = defineNitroPreset({
extends: "node-server",
hooks: {
async compiled(nitro: Nitro) {
await writeFile(
resolve(nitro.options.output.dir, "web.config"),
await iisnodeXmlTemplate(nitro)
);

await writeFile(
resolve(nitro.options.output.dir, "index.js"),
"import('./server/index.mjs');"
);
},
},
});

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>
`;
if (path !== undefined) {
const fileString = await readFile(path, "utf8");
const originalWebConfig: Record<string, unknown> =
await parseXmlDoc(originalString);
const fileWebConfig: Record<string, unknown> =
await parseXmlDoc(fileString);

if (nitro.options.iis.mergeConfig && !nitro.options.iis.overrideConfig) {
return buildNewXmlDoc({ ...originalWebConfig, ...fileWebConfig });
} else if (nitro.options.iis.overrideConfig) {
return buildNewXmlDoc({ ...fileWebConfig });
}
}
return originalString;
}

async function iisXmlTemplate(nitro: Nitro) {
const path = resolveFile("web.config", nitro.options.rootDir, ["config"]);
const originalString = `<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
Expand All @@ -31,4 +109,43 @@ function iisXmlTemplate() {
</system.webServer>
</configuration>
`;
if (path !== undefined) {
const fileString = await readFile(path, "utf8");
const originalWebConfig: Record<string, unknown> =
await parseXmlDoc(originalString);
const fileWebConfig: Record<string, unknown> =
await parseXmlDoc(fileString);

if (nitro.options.iis.mergeConfig && !nitro.options.iis.overrideConfig) {
return buildNewXmlDoc({ ...originalWebConfig, ...fileWebConfig });
} else if (nitro.options.iis.overrideConfig) {
return buildNewXmlDoc({ ...fileWebConfig });
}
}
return originalString;
}

// XML Helpers
export async function parseXmlDoc(
xml: string
): Promise<Record<string, unknown>> {
const { Parser } = await import("xml2js");

if (xml === undefined || !xml) {
return {};
}
const parser = new Parser();
let parsedRecord: Record<string, unknown>;
parser.parseString(xml, function (_, r) {
parsedRecord = r;
});
return parsedRecord;
}

export async function buildNewXmlDoc(
xmlObj: Record<string, unknown>
): Promise<string> {
const { Builder } = await import("xml2js");
const builder = new Builder();
return builder.buildObject({ ...xmlObj });
}
3 changes: 1 addition & 2 deletions src/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export * from "./cleavr";
export * from "./layer0";
export * from "./flightcontrol";
export * from "./lagon";
export * from "./iis-node";
export * from "./iis";
export { iis, iisNode } from "./iis";
export { _static as static } from "./static";
export * from "./github-pages";
6 changes: 6 additions & 0 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ export interface NitroOptions extends PresetOptions {
preview: string;
deploy: string;
};

// IIS
iis?: {
mergeConfig?: boolean;
overrideConfig?: boolean;
};
}

declare global {
Expand Down

0 comments on commit 5639f5b

Please sign in to comment.