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

xml-c14n

v0.0.6

Published

XML canonicalisation

Downloads

7,403

Readme

xml-c14n

XML canonicalisation (xml-c14n)

Overview

This module performs XML canonicalisation as specified in xml-c14n.

To operate, a preconstructed DOM object is required. Any object that implements the DOM Level 1 API will suffice. I recommend xmldom if you're working with node, or your browser's native DOM implementation if you're not.

This module was originally adapted from some code in xml-crypto by Yaron Naveh, and as such is also covered by any additional conditions in the license of that codebase (which just so happens to be the MIT license).

Apology

Look, I know the API feels like Java. It pains me as much to work on it as it will pain you to use it. This whole XML thing is a crock of shit, and is so over-engineered that a factory (yeah, you heard me right) was the best way to implement it. So yeah... Sorry.

No Apology

I spell canonicalise with an "s". Deal with it.

Super Quickstart

Also see example.js.

#!/usr/bin/env node

var xmldom = require("xmldom");

var c14n = require("xml-c14n")();

var xmlData = require("fs").readFileSync(process.argv[2], "utf8"),
    document = (new xmldom.DOMParser()).parseFromString(xmlData);

var canonicaliser = c14n.createCanonicaliser("http://www.w3.org/2001/10/xml-exc-c14n#WithComments");

console.log("canonicalising with algorithm: " + canonicaliser.name());
console.log("");

console.log("INPUT");
console.log("");
console.log(xmlData);

console.log("");

canonicaliser.canonicalise(document.documentElement, function(err, res) {
  if (err) {
    return console.warn(err.stack);
  }

  console.log("RESULT");
  console.log("");
  console.log(res);
});
➜  xml-c14n git:(master) ./example.js small.xml
canonicalising with algorithm: http://www.w3.org/2001/10/xml-exc-c14n#WithComments

INPUT

<?xml version="1.0"?>

<?xml-stylesheet   href="doc.xsl"
   type="text/xsl"   ?>

<!DOCTYPE doc SYSTEM "doc.dtd">

<doc>Hello, world!<!-- Comment 1 --></doc>

<?pi-without-data     ?>

<!-- Comment 2 -->

<!-- Comment 3 -->


RESULT

<doc>Hello, world!<!-- Comment 1 --></doc>

Installation

Available via npm:

$ npm install xml-c14n

Or via git:

$ git clone git://github.com/deoxxa/xml-c14n.git node_modules/xml-c14n

API

CanonicalisationFactory

This is what you get when you require("xml-c14n"). It's a factory for getting canonicaliser implementation instances.

CanonicalisationFactory(options)
var c14n = new CanonicalisationFactory();
// OR
var c14n = CanonicalisationFactory();
// THUS
var c14n = require("xml-c14n")();

CanonicalisationFactory.registerAlgorithm

This is how you get a specific canonicalisation algorithm implementation into a factory so that it can be instantiated within and returned to callers. You give it a URI and a factory function (sup dog) and it'll shove it into an object internally so it can be retrieved later on.

c14n.registerAlgorithm(uri, factoryFunction)
c14n.registerAlgorithm("http://herp.derp/", function(options) {
  return new HerpDerp(options);
});

Arguments

  • uri - a URI to identify the algorithm
  • factoryFunction - a function that creates instances of the algorithm's implementation

CanonicalisationFactory.getAlgorithm

This lets you get the factory function for an algorithm. Not incredibly useful, but here for completeness.

c14n.getAlgorithm(uri)
var herpDerpFactory = c14n.getAlgorithm("http://herp.derp/");

Arguments

  • uri - the URI identifying the algorithm to fetch the factory function for

CanonicalisationFactory.createCanonicaliser

Creates an instance of a canonicaliser, referenced by its URI, and optionally passing along something for the new instance.

c14n.createCanonicaliser(uri, [options]);
// creates an xml-exc-c14n canonicaliser
var canonicaliser = c14n.createCanonicaliser("http://www.w3.org/2001/10/xml-exc-c14n#");

Algorithm

This is the abstract "class" that all specific algorithms (should) extend. It provides some stubbed out methods that do nothing useful aside from serving as documentation. These methods should be overridden by specific implementations. What you get back from CanonicalisationFactory.createCanonicaliser will be an extension of this class.

Algorithm.name

This gives you the name (URI) that the algorithm instance goes by.

algorithm.name();
var uri = algorithm.name();

Algorithm.canonicalise

This is what does the meat of the work in most implementations.

algorithm.canonicalise(node, cb);
algorithm.canonicalise(node, function(err, data) {
  if (err) {
    return console.warn(err);
  }

  console.log(data);
});

Included Canonicalisation Algorithms

There are two included algorithms, one of which is a specialisation of the other.

http://www.w3.org/2001/10/xml-exc-c14n#

  • uri - http://www.w3.org/2001/10/xml-exc-c14n#
  • options
    • includeComments
    • inclusiveNamespaces

For a description of this algorithm and its options, see the xml-exc-c14n specification.

http://www.w3.org/2001/10/xml-exc-c14n#WithComments

  • uri - http://www.w3.org/2001/10/xml-exc-c14n#WithComments
  • options
    • inclusiveNamespaces

This is just a special version of http://www.w3.org/2001/10/xml-exc-c14n# with includeComments enabled.

License

3-clause BSD. A copy is included with the source.

Contact