Giter VIP home page Giter VIP logo

fetch-base64's Introduction

Fetch Base64

Build Status

A node package to fetch local or remote files in base64 encoding. Useful for inlining assets (images, web fonts, etc.) into HTML or CSS documents.

Disclaimer: I've only used this for images so far, but there is no reason why it shouldn't work for any other kind of files.

If you find a bug please report it here.

Usage

const fetch = require(fetch-base64);

fetch.local('/path/to/image.jpg').then((data) => {
  // data[0] contains the raw base64-encoded jpg
}).catch((reason) => {});

fetch.remote('http://domain.com/to/image.jpg').then((data) => {
  // data[1] contains base64-encoded jpg with MimeType
}).catch((reason) => {});

fecth.auto('/remote/or/local/path/image.gif').then((data) => {

}).catch((reason) => {});

Install

npm install --save fetch-base64

API

Return value

All the functions return a Promise which when resolved will return an Array with two Strings (data):

  • data[0] will contain the raw base64-encoded file. E.g.: iVBORw0KGgoAAAANSUhEUg...
  • data[1] will contain the same information as data[0] and the mimeType. Useful if you want to embed the file into an HTMl or CSS document. E.g.: data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...

fetch.local(...paths)

Fetch local files and return a promise with the file in base64 format.

Params

  • ...paths <string(s)>: Single or multiple paths which will be combined using node's path.resolve(). You can pass multiple paths to resolve a relative path to an absolute path. Some examples of valid values for this parameter:
    • '/some/absolute/path/image.jpg'
    • '/base/path/to/html', './img/animation.gif'

fetch.remote(url)

Fetch remote file in url and return a promise with the file in base64 format.

User Agent is spoofed to be same as Chrome's to avoid some restrictions, but fetching could still fail for other reasons.

Params

  • url <string>: URL where the file resides. Note that node must have access to the given URL.

fetch.remote(from, to)

Resolve url using node's url.resolve(from, to), fetch remote file and return a promise with the file in base64 format.

User Agent is spoofed to be same as Chrome's to avoid some restrictions, but fetching could still fail for other reasons.

Params

  • from <string>: The Base URL being resolved against.
  • to <string>: The HREF URL being resolved.

See url.resolve() for more information and examples.

fetch.auto(...mixed)

This function will do the best effort to automatically detect the kind of path passed (remote or local) and invoke the correspondent function.

It will use the fetch.isRemote() function to determine if a remote url or a local path has been passed in the first parameter.

Params

  • ...mixed <string(s)>: Accepts the same parameters as fetch.local(...paths), fetch.remote(url) or fetch.remote(from, to) - see above. Examples of valid calls:
    • fetch.auto('/base/path/to/html', './img/animation.gif');
    • fetch.auto('http://some.domain/file.png');
    • fetch.auto('http://some.domain/', 'file.png');

Utility functions

fetch.isLocal(path)

Returns true if the passed path (<string>) is local. Returns false otherwise.

fetch.isRemote(path)

Returns true if the passed path (<string>) is remote. Returns false otherwise.

Examples

Include package

const fetch = require('fetch-base64');

Local

// will fetch image in /Users/bla/src/project/img/logo.jpg
const doFetchLocal = fetch.local('/Users/bla/src/project', './img/logo.jpg');
doFetchLocal.then((data) => {
  console.log(`base64 image raw: ${data[0]}`);
}, (reason) => {
  console.log(`Fetch Failed: ${reason}`)
});

Remote

const doFetchRemote = fetch.remote('https://somedomain.com/image.jpg');
doFetchRemote.then((data) => {
  console.log(`base64 image with mimeType: ${data[1]}`);
}, (reason) => {
  console.log(`Fetch Failed: ${reason}`)
});
const doFetchRemote2 = fetch.remote('https://somedomain.com', '/image.jpg');
doFetchRemote.then((data) => {
  console.log(`base64 image with mimeType: ${data[1]}`);
}, (reason) => {
  console.log(`Fetch Failed: ${reason}`)
});

Auto

const doFetchAuto = fetch.auto('https://somedomain.com/image.jpg');
doFetch.then((data) => {
  console.log(`base64 image: ${data[0]}`);
}, (reason) => {
  console.log(`Fetch Failed: ${reason}`)
});
const doFetchAuto2 = fetch.auto('/some/local/', '/path/', './image.jpg');
doFetch.then((data) => {
  console.log(`base64 image: ${data[0]}`);
}, (reason) => {
  console.log(`Fetch Failed: ${reason}`)
});

fetch-base64's People

Contributors

gamell avatar

Watchers

 avatar

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.