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

js-base64-file

v2.0.3

Published

load fetch convert and save local and remote files and images to base64 in js

Downloads

18,092

Readme

Base64 File loading, converting and saving for node.js

This is an ideal way to load and convert local and remote files to base64 either for use as a string or to save on disk.

This works with all file types!

install npm i js-base64-file


Class Methods

|method |params |description| |------ |------ |-----------| |load |path,fileName,callback |loads a local file and converts it to base64. note : path should always end with a slash | |loadSync |path,fileName |same as load, but it returns the base64 string instead of passing it to a callback. This could be slow on really large files. | |loadRemote |url,fileName,node-fetch options|loads a remote file and converts it to base64. This defaults to a simple GET request, but allows the full options from node-fetch for any type of request even with payloads| |save |data,path,fileName,callback|saves the data to the specified path and filename async callback| |saveSync |data,path,fileName |saves the data to the specified path and filename sync|

Class Instance

|callback|params|description| |--------|------|-----------| |load | err, base64Data |gives you the base64 encoded file data| |save | err, data |will pass any errors back it is unlikely there will ever be data|


Testing & Coverage

We have 98% coverage with the amazing C8 coverage tool. The only things not covered are empty default param functions. You can see the coverage details in the ./coverage/index.html file.

We use the vanilla-test testing module for super light and fast testing, and have integrated with Travis CI.

You can run the tests and explore the coverage yourself by cloning the repo and running npm test. It will automatically set everything up and run coverage for you.

Examples

You can look at and run the files in the ./example dir and run them with node ./example/{example}.js

Below are some quick copy paste examples for you.


LoadSync


    import {Base64File} from 'js-base64-file';

    const image=new Base64File;
    const file='test.png';
    const path='./';


    //this will load and convert if needed synchriouniously
    const data=image.loadSync(path,file);

    console.log(`

    SYNC: you could send this image via ws or http to the browser or save it to disk now : 

    ${data.slice(0,50)} ... ${data.slice(-50)}


    `);

Load Async callback


    import {Base64File} from 'js-base64-file';

    const image=new Base64File;
    const file='test.png';
    const path='./';


    //this will load and convert if needed synchriouniously
    image.load(path,file,asycHandler);

    function asycHandler(err,data){
        if(err){
            console.trace(err);
        }

        console.log(`

        ASYNC: you could send this image via ws or http to the browser or save it to disk now : 

        ${data.slice(0,50)} ... ${data.slice(-50)}


        `);
    }

Load Remote Await


    import {Base64File} from 'js-base64-file';

    const image=new Base64File;

    const remoteURL='https://octodex.github.com/images/';
    const remoteFile='megacat-2.png';
    const data=await image.loadRemote(remoteURL,remoteFile);

    console.log(`

    REMOTE: you could send this image via ws or http to the browser or save it to disk now : 

    ${data.slice(0,50)} ... ${data.slice(-50)}


    `);

SaveSync


    import {Base64File} from '../index.js';
    import {existsSync} from 'fs';

    const image=new Base64File;

    const remoteURL='https://octodex.github.com/images/';
    const remoteFile='megacat-2.png';

    const localPath='./';
    
    //loading an image
    const data=await image.loadRemote(remoteURL,remoteFile);

    //saving the image
    image.saveSync(data,localPath,remoteFile);

    console.log(existsSync(localPath+remoteFile));

Save Async Callback


    import {Base64File} from 'js-base64-file';
    import {existsSync} from 'fs';

    const image=new Base64File;
    const file='test.png';
    const path='./';
    const copyAsyncFile=`copy-async-${file}`;


    //load a file
    const data=image.loadSync(path,file);

    //save the file
    image.save(
        data,
        path,
        copyAsyncFile,
        function(err,data){
            if (err||!existsSync(`${path}${copyAsyncFile}`)){
                test.fail();
            }

            console.log(existsSync(path+copyAsyncFile));

            //call next test here
        }
    );

It's that simple! And it will work with any file type!