New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json-archive

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

json-archive

Simple archive format based on JSON.

latest
Source
npmnpm
Version
2.1.0
Version published
Maintainers
1
Created
Source

JSON Archive

Simple archive format based on JSON.

Features

  • Simple: It converts a directory into a human-inspectable JSON object that maps file paths to their base-64 encoded contents.
  • Tiny: The whole package comes at about ~1kb, with 0 third-party dependencies.
  • Universal: Archives can be visited with the "visit" function in the browser too.

Install

npm install --save json-archive

Usage

Interface

The following interface is provided:

interface JSONArchive {
  pack ( folderPath: string, options?: VisitOptions ): Promise<Archive>;
  unpack ( archivePath: string, options?: VisitOptions ): Promise<Archive>;
  visit ( archive: Archive, options?: VisitOptions ): Promise<Archive>;
};

// Types

type Promisable<T> = T | Promise<T>;

type Filter = ( filePathRelative: string ) => Promisable<boolean>;

type Transform = ( file: File ) => Promisable<File>;

type Visit = ( filePathRelative: string, file: File ) => Promisable<void>;

type Encoding = 'ascii' | 'base64' | 'base64url' | 'binary' | 'hex' | 'latin1' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2';

type File = {
  contents: string,
  encoding: Encoding
};

type Archive = {
  [filePathRelative: string]: File
};

type VisitOptions = {
  filter?: Filter,
  transform?: Transform,
  visit?: Visit
};
Examples

Do this to pack a directory:

import fs from 'fs';
import {pack} from 'json-archive';

const archive = await pack ( 'path/to/dir' );

fs.writeFileSync ( 'archive.json', JSON.stringify ( archive ) );

Do this to unpack an archive:

import fs from 'fs';
import {unpack} from 'json-archive';

const archive = await unpack ( 'archive.json' );

Do this to filter/transform/traverse an archive:

import fs from 'fs';
import {visit} from 'json-archive';

const transformedArchive = await visit ( archive, {
  filter: filePath => {
    return !filePath.startsWith ( '.' );
  },
  visit: ( filePath, file ) => {
    fs.writeFileSync ( filePath, file.contents, { encoding: 'base64' } );
  }
});

License

MIT © Fabio Spampinato

Keywords

json

FAQs

Package last updated on 21 Jul 2023

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