Giter Site home page Giter Site logo

node-rfc2397's Introduction

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

node-rfc2397's People

Contributors

kaworu avatar rolinh avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

sebastian-onlea

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.