Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preset-iis): parse without explicitArray to allow merging web.config #2457

Merged
merged 2 commits into from
May 21, 2024

Conversation

Ariesly
Copy link
Contributor

@Ariesly Ariesly commented May 21, 2024

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

for test script

const { defu } = require("defu");
const { Builder, Parser } = require("xml2js");

const originalString = /* xml */ `<?xml version="1.0" encoding="utf-8"?>
  <!--
       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 https://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>
`;

const fileString = /* xml */ `<?xml version="1.0" encoding="utf-8"?>
<!--
     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>
  <appSettings>
    <add key="a" value="b" />
    <add key="C" value="d" />
    <add key="e" value="F" />
    <add key="G" value="h" />
  </appSettings>
  <system.webServer>
    <modules>
      <remove name="WebDAVModule"/>
    </modules>
    <handlers>
      <remove name="WebDAV"/>
    </handlers>
  </system.webServer>
</configuration>
`

function parseXmlDoc(xml) {
  if (xml === undefined || !xml) {
    return {};
  }
  const parser = new Parser({ ...{ explicitArray: false } });
  let parsedRecord = {};
  parser.parseString(xml, (_, r) => {
    console.log()
    parsedRecord = r;
  });
  return parsedRecord;
}


const originalWebConfig = parseXmlDoc(originalString);
const fileWebConfig = parseXmlDoc(fileString);

function buildNewXmlDoc(
  xmlObj
)  {
  const builder = new Builder();
  return builder.buildObject(xmlObj);
}

console.log(buildNewXmlDoc(defu(fileWebConfig, originalWebConfig)))

input web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
     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>
  <appSettings>
    <add key="a" value="b" />
    <add key="C" value="d" />
    <add key="e" value="F" />
    <add key="G" value="h" />
  </appSettings>
  <system.webServer>
    <modules>
      <remove name="WebDAVModule"/>
    </modules>
    <handlers>
      <remove name="WebDAV"/>
    </handlers>
  </system.webServer>
</configuration>

output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
  <system.webServer>
    <webSocket enabled="false"/>
    <handlers>
      <add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
      <remove name="WebDAV"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^index.js/debug[/]?"/>
        </rule>
        <rule name="StaticContent">
          <action type="Rewrite" url="public{PATH_INFO}"/>
        </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"/>
        </hiddenSegments>
        <requestLimits maxAllowedContentLength="4294967295"/>
      </requestFiltering>
    </security>
    <httpErrors existingResponse="PassThrough"/>
    <iisnode watchedFiles="index.js" node_env="production" debuggingEnabled="false" loggingEnabled="false"/>
    <modules>
      <remove name="WebDAVModule"/>
    </modules>
  </system.webServer>
  <appSettings>
    <add key="a" value="b"/>
    <add key="C" value="d"/>
    <add key="e" value="F"/>
    <add key="G" value="h"/>
  </appSettings>
</configuration>

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@Ariesly Ariesly marked this pull request as draft May 21, 2024 10:18
@Ariesly Ariesly marked this pull request as ready for review May 21, 2024 10:40
@pi0 pi0 changed the title fix(preset-iis): support merge web.config fix(preset-iis): parse without explicitArray to allow merging web.config May 21, 2024
@pi0 pi0 merged commit 6c3e080 into unjs:main May 21, 2024
3 of 4 checks passed
@pi0 pi0 mentioned this pull request Jun 13, 2024
@pi0 pi0 mentioned this pull request Jun 27, 2024
pi0 pushed a commit that referenced this pull request Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants