npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-xml-parser

v0.1.0

Published

Simple XML parsing middleware for Koa 2.x

Downloads

204

Readme

Koa XML Parser

Installation

$ yarn add koa-xml-parser

Example

const Koa = require('koa');
const xmlParser = require('koa-xml-parser');

// Configuration object can be omitted, in this case default properties are used
const parseXML = xmlParser({
  limit: '1MB',           // Reject payloads larger than 1 MB
  encoding: 'UTF-8',      // Explicitly set UTF-8 encoding
  xml: {
    normalize: true,      // Trim whitespace inside text nodes
    normalizeTags: true,  // Transform tags to lowercase
    explicitArray: false  // Only put nodes in array if >1
  }
});

const app = new Koa();
app.use(parseXML);

This will parse any XML-based request and place it as an object on context.req.body and context.request.body the same way most body parsers do.

An XML-based request is determined by the value of the Content-Type header. By default, any Content-Type header ending in /xml or +xml is assumed to be XML and is parsed. For example, the following headers will all match:

  • text/xml
  • application/xml
  • application/rss+xml

If you need to match against a custom Content-Type, you can pass in the type property to a configuration object.

Configuration

As shown above, instead of relying on library defaults, you can pass custom configuration options to the parser building function. config object has the following parameters:

  • type - either a string or an array of strings representing content type(s) indicating that body should be parsed. Types must conform to a format of type-is library.
  • encoding - a string representing explicit request encoding. UTF-8 by default.
  • limit - a number or a string representing the request size limit. 1mb by default.
  • xml - an object containing configuration object for xml2js library that is used to parse XML internally. You can refer to the library's documentation for a full list of options with their corresponding possible values.

Test

$ yarn test

License

3-Clause BSD