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

node-rfc2397

v3.0.1

Published

NodeJS implementation of RFC 2397, both parsing and composing

Downloads

208

Readme

NPM Version Build Status Test Coverage

node-rfc2397

NodeJS implementation of RFC 2397 (The "data" URL scheme), both parsing and composing.

Usage

parse(dataurl, callback)

Parse a RFC 2397 compliant string. callback is a function (err, info) that is called as callback(err) if an error arise and callback(null, info) on success. The info object yielded to callback has the following form:

{
    mime: "mime/type"     // the mime type (a string)
    parameters: {         // an object composed of the given dataurl parameters
        param1: "value1", // a string
        param2: "value2", // a string
        ...
    },
    data: // a Buffer constructed from the data part of the dataurl
}

Example:

var moddataurl = require("node-rfc2397");

// URL encoded
var dataurl = "data:text/plain;charset=cp866;foo=bar;answer=42,%e1%ab%ae%a2%ae";
moddataurl.parse(dataurl, function (err, info) {
    // err is null and info is the following object:
    // {
    //     mime: "text/plain",
    //     parameters: {
    //         charset: "cp866",
    //         foo: "bar",
    //         answer: "42",
    //     },
    //     data: <Buffer e1 ab ae a2 ae>,
    // }
});

// Base64
var dataurl = "data:text/plain;charset=utf-8;base64,SGVsbG8gV29ybGQh";
moddataurl.parse(dataurl, function (err, info) {
    // err is null and info is the following object:
    // {
    //    base64: true,
    //    mime: "text/plain",
    //    parameters: {
    //        charset: "utf-8"
    //    },
    //    data: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64 21>
    // }
    info.data.toString(); // "Hello World!"
});

parseSync(dataurl)

Synchronous version of parse(). This throws if an error occurs. dataurl is a RFC 2397 compliant string. Returns an info object, as described in the documentation of parse().

Example:

var moddataurl = require("node-rfc2397");

try {
    var dataurl = "data:text/plain;charset=utf-8,Hello%20World%21";
    var info = moddataurl.parseSync(dataurl)
    console.log(info);
    // {
    //     mime: 'text/plain',
    //     parameters: {
    //         charset: 'utf-8',
    //     },
    //     data: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64 21>
    // }

} catch (err) {
    console.log(err);
    throw err;
}

compose(info, callback)

Compose a RFC 2397 compliant string from the given object info. callback is a function (err, dataurl) that is called as callback(err) if an error arise and callback(null, dataurl) on success.

Example:

var moddataurl = require("node-rfc2397");

var info = {
    mime:"text/plain",
    parameters: {
        charset:"utf-8"
    },
    base64: true,
    data: Buffer.from("Hello World!")
};

moddataurl.compose(info, function (err, dataurl) {
    // err is null and dataurl is the following string:
    // "data:text/plain;charset=utf-8;base64,SGVsbG8gV29ybGQh"
});

composeSync(info)

Synchronous version of compose(). This throws if an error occurs. info is an object, as described in the documentation of compose(). Returns an RFC 2397 compliant string.

Example:

var moddataurl = require("node-rfc2397");

try {
    var info = {
        mime: "text/plain",
        parameters: {
            charset: "utf-8",
        },
        data: Buffer.from("Hello World!"),
    };
    var dataurl = moddataurl.composeSync(info);
    console.log(dataurl); // data:text/plain;charset=utf-8,Hello%20World%21
} catch (err) {
    console.log(err);
    throw err;
}

Implementation notes

The RFC 2397 is unfortunately vague regarding many details of the syntax of the data URL scheme. This node module does its best to have a solid implementation of the specification of the original RFC. However, some independent choices had to be made where the specification is unclear.

Duplicate parameter attribute handling

The RFC 2397 does not specify what needs to be done when duplicate parameter keys are encountered. The approach retained by this implementation is first given.

Example:

var moddataurl = require("node-rfc2397");
var info = moddataurl.parseSync("data:text/plain;foo=bar;foo=nope,");
console.log(info);
// {
//     mime: 'text/plain',
//     parameters: { foo: 'bar' },
//     data: <Buffer >,
// }

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test

For a test coverage report, run npm test --coverage:

$ npm test --coverage

License

MIT