Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-base64-file

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-base64-file

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

  • 2.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

methodparamsdescription
loadpath,fileName,callbackloads a local file and converts it to base64. note : path should always end with a slash
loadSyncpath,fileNamesame as load, but it returns the base64 string instead of passing it to a callback. This could be slow on really large files.
loadRemoteurl,fileName,node-fetch optionsloads 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
savedata,path,fileName,callbacksaves the data to the specified path and filename async callback
saveSyncdata,path,fileNamesaves the data to the specified path and filename sync

Class Instance

callbackparamsdescription
loaderr, base64Datagives you the base64 encoded file data
saveerr, datawill 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!

Keywords

FAQs

Package last updated on 24 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc