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

tar-async

Package Overview
Dependencies
Maintainers
0
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tar-async - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

examples/addDirectory.js

8

lib/header.js

@@ -6,3 +6,3 @@ /*

(function (){
(function () {
"use strict";

@@ -33,3 +33,5 @@

var utils = require("./utils"),
headerFormat = [
headerFormat;
headerFormat = [
{

@@ -105,3 +107,3 @@ 'field': 'fileName',

headerFormat.forEach(function(value){
headerFormat.forEach(function (value) {
buffer.write(data[value.field] || "", offset);

@@ -108,0 +110,0 @@ offset += value.length;

@@ -12,2 +12,3 @@ /*

var fs = require('fs'),
path = require('path'),
Stream = require('stream').Stream,

@@ -17,7 +18,13 @@ header = require("./header"),

function Tar() {
function Tar(opt) {
var tape;
Stream.apply(this, arguments);
// I know, it's kinda ugly, but I really don't want an if-else...
this.addHidden = (opt && 'addHidden' in opt) ? opt.addHidden : false;
this.noDirs = (opt && 'noDirs' in opt) ? opt.noDirs : false;
this.normalize = (opt && 'normalize' in opt) ? opt.normalize : true;
this.recurse = (opt && 'recurse' in opt) ? opt.recurse : true;
this.written = 0;

@@ -33,2 +40,7 @@

});
// this seems to need to be done after the listeners are added
if (opt && opt.output) {
this.pipe(opt.output);
}
}

@@ -44,14 +56,13 @@

Tar.prototype.append = function (path, o, callback) {
Tar.prototype.append = function (filepath, callback) {
var tape = this;
fs.lstat(path, function (err, stat) {
fs.lstat(filepath, function (err, stat) {
var type, data = {}, mtime, filesize;
function finish(cb) {
var checksum = 0, written, input;
Object.keys(data).forEach(function(key){
var i, l,
value = data[key];
Object.keys(data).forEach(function (key) {
var i, value = data[key];
for (i = 0; i < value.length; i++) {
for (i = 0; i < value.length; i += 1) {
checksum += value.charCodeAt(i);

@@ -65,3 +76,3 @@ }

written = 0;
input = fs.createReadStream(path);
input = fs.createReadStream(filepath);
input.on('data', function (chunk) {

@@ -90,21 +101,21 @@ written += chunk.length;

try {
if(stat.isDirectory()){
path += "/";
if (stat.isDirectory()) {
filepath += "/";
type = "5";
}else if(stat.isSymbolicLink()){
} else if (stat.isSymbolicLink()) {
type = "2";
}else if(stat.isFile()){
} else if (stat.isFile()) {
type = "0";
}else{
} else {
type = new Error();
}
mtime = o.mtime || (stat.mtime.getTime() / 1000);
filesize = o.fileSize || (stat.isFile() ? stat.size : 0);
mtime = stat.mtime.getTime() / 1000;
filesize = stat.isFile() ? stat.size : 0;
data = {
fileName: o.fileName || path,
fileMode: utils.pad((o.fileMode || (stat.mode & 0xfff)), 7),
uid: utils.pad(o.uid || (stat.uid), 7),
gid: utils.pad(o.gid || (stat.gid), 7),
fileName: tape.noDirs ? path.basename(filepath) : filepath,
fileMode: utils.pad((stat.mode & 0xfff), 7),
uid: utils.pad(stat.uid, 7),
gid: utils.pad(stat.gid, 7),
fileSize: utils.pad(filesize, 11),

@@ -115,8 +126,12 @@ mtime: utils.pad(mtime, 11),

ustar: "ustar ",
owner: o.owner || "",
group: o.group || ""
owner: "",
group: ""
};
if (tape.normalize && !tape.noDirs) {
data.fileName = path.normalize(data.fileName);
}
if (stat.isSymbolicLink()) {
fs.readlink(path, function (err, resolvedPath) {
fs.readlink(filepath, function (err, resolvedPath) {
if (err) {

@@ -139,7 +154,8 @@ callback(err);

Tar.prototype.addFiles = function (files, overrides, callback) {
Tar.prototype.addFiles = function (files, callback) {
var error, tape = this;
files.forEachAsync(function (next, filename) {
// make sure we get through all cases
if(error){
if (error) {
next();

@@ -151,7 +167,16 @@ }

error = err;
next();
return next();
}
tape.append(filename, overrides, function(tErr){
if(tErr){
if (!tape.addHidden && path.basename(filename).charAt(0) == '.') {
return next();
}
// we won't even append the folders if we don't want to recurse
if (!tape.recurse && stat.isDirectory()) {
return next();
}
tape.append(filename, function (tErr) {
if (tErr) {
error = tErr;

@@ -162,3 +187,3 @@ return next();

if (stat.isDirectory()) {
tape.addDirectory(filename, overrides, function (tErr2) {
tape.addDirectory(filename, function (tErr2) {
if (tErr2) {

@@ -171,3 +196,3 @@ error = tErr2;

});
}else{
} else {
next();

@@ -177,3 +202,3 @@ }

});
}).then(function(){
}).then(function () {
callback(error);

@@ -183,5 +208,5 @@ });

Tar.prototype.addDirectory = function (path, overrides, callback) {
Tar.prototype.addDirectory = function (filepath, callback) {
var tape = this;
fs.readdir(path, function (err, filenames) {
fs.readdir(filepath, function (err, filenames) {
if (err) {

@@ -191,7 +216,11 @@ return callback(err);

filenames.forEach(function(filename, i, arr){
arr[i] = path + "/" + filename;
filenames.forEach(function (filename, i, arr) {
if (tape.normalize) {
arr[i] = path.join(filepath, filename);
} else {
arr[i] = filepath + "/" + filename;
}
});
return tape.addFiles.apply(tape, [filenames, overrides, callback]);
return tape.addFiles.apply(tape, [filenames, callback]);
});

@@ -198,0 +227,0 @@ };

@@ -6,3 +6,3 @@ /*

(function (){
(function () {
"use strict";

@@ -12,3 +12,3 @@

var i, buffer = new Buffer(length);
for (i = 0; i < length; i++) {
for (i = 0; i < length; i += 1) {
buffer[i] = 0;

@@ -15,0 +15,0 @@ }

{
"name": "tar-async",
"description": "Creates tar files asynchronously in chunks",
"version": "0.1.0",
"homepage": "http://github.com/beatgammit/node-tar",
"version": "0.2.0",
"homepage": "http://github.com/beatgammit/node-tar-async",
"repository": {
"type": "git",
"url": "git://github.com/beatgammit/node-tar.git"
"url": "git://github.com/beatgammit/node-tar-async.git"
},

@@ -10,0 +10,0 @@ "author": "T. Jameson Little <t.jameson.little@gmail.com>",

@@ -6,34 +6,74 @@ Intro

* Evented IO with a 'data' event
* Evented IO with events emitted per chunk
* Friendly function calls
* Write to file or write to memory
* Customizable through options
* Full JSLint complience and runs in Strict Mode
* MIT Licensed
The Tar object is actually a NodeJS stream, and each chunk is written to a stream. The Tar object supports the usual events: data, error, end. This module can either be used stricly like the normal tar utility would be by piping the output to standard out, or the chunks can be used as they come.
The Tar object is actually a NodeJS stream, so it supports the usual events: data, error, and end. Since it is a stream, this module can either be simply piped to standard out or to a file, or the chunks can be processed as they come. This makes it really convenient when working with webservers.
No compression is used, so an external compression library is necessary. This is by design and not likely to be implemented.
Install
-------
`npm install tar-async`
Dependencies
------------
The only external module that Tar uses is futures, and only the forEachAsync method at that. This module will add to the Array prototype, so stay away if that bothers you. This module is used to allow for graceful handling of asynchronous calls in a forEach loop. This module can be installed from npm:
The only external module that Tar uses is futures, and only the forEachAsync method at that. This module will add to the Array prototype, so any for..in loops will need to be converted to forEach loops. This module is used to allow for graceful handling of asynchronous calls in a forEach loop.
This module can be installed from npm:
`npm install futures`
Tar also requires the fs and stream modules.
Tar also requires the built-in fs and stream modules.
Usage Guide
===========
This tar utility inherits from stream, so any methods that work with stream will work with this utility. Tar archives are processed in 10KiB chunks. No compression is applied because the libraries for compression are not stable enough for my liking, but that is easily implemented.
The constructor takes an array of options as its only parameter:
* `addHidden`- add hidden files to archive (default false)
* a 'hidden' file/directory is defined as being prefixed by a '.'
* no other paradigm is currently supported
* `noDirs`- lump all files together into a single directory (default false)
* tar by default preserves the directory structure
* often, only raw files are wanted, so this makes it easier to extract
* `normalize`- normalize each file before archiving (default true)
* `recurse`- recursively add files to archive (default true)
* `output`- output stream (default undefined)
* must be a writable stream object
* will throw an error if given an invalid stream object, so be careful
Files can be added to the archive until the close function is called. There are three functions that add data to the archive:
* append- Appends a file to the archive. Takes two parameters
* filepath- Path to the file (can be relative or absolute)
* callback- Has at most one parameter: the error if one occurred
* addFiles- Adds an array of files to the archive. Takes two parameters
* files- Array of files. Files must be absolute or relative paths.
* callback- Has at most one parameter: the error if one occurred
* addDirectory- Adds an entire directory to the archive. Takes two parameters:
* filepath- Path to the directory to add (adds the directory path to each file)
* callback- Has at most one parameter: the error if one occurred
Examples
--------
There are a few examples in the examples directory, but if you are lazy, here are a couple brief examples.
To tar all of the files in a directory:
var Tar = require('./tar'),
overrides = {
owner: "jameson",
group: "jameson"
var Tar = require('tar'),
options = {
output: process.stdout
},
tape = new Tar();
tape = new Tar(options);
tape.addFiles("./", overrides, function (err) {
tape.addDirectory("./", function (err) {
if (err) {

@@ -45,10 +85,7 @@ throw err;

tape.pipe(process.stdout);
To tar a bunch of random files together:
var Tar = require('./tar'),
overrides = {
owner: "jameson",
group: "jameson"
options = {
output: process.stdout
},

@@ -59,3 +96,2 @@ tape = new Tar(),

files = [
"./test.js",
"./tar.js",

@@ -65,3 +101,3 @@ "./header.js"

tape.addFiles(files, overrides, function (err) {
tape.addFiles(files, function (err) {
if (err) {

@@ -73,6 +109,4 @@ throw err;

tape.pipe(process.stdout);
All files that are tarred will preserve the directory structure of the path of each file that was given unless otherwise specified in the options. The addFiles method requires the full path on the file.
Note, the "./" is not really necessary, but I do it for clarity. All files that are tarred will preserve the directory structure of the path of each file that was given. The addFiles method requires the full path on the file.
Source structure

@@ -84,4 +118,9 @@ ================

Stores the header format and a method for formatting the header. The header is an array with two properties in each element:
Stores the header format and a method for formatting the header:
* headerFormat- Header structure, only used in formatHeader()
* formatHeader- writes the data from the parameter to a 512 byte header
The header is an array with two properties in each element:
* field- Corresponds to a property of an object passed in to formatHeader

@@ -98,2 +137,3 @@ * length- Length of this property in bytes

* constructor- Inherits from Stream and all arguments are passed to the Stream constructor
* options- configuration for the tar utility (discussed in 'Usage Guide')
* addFiles- adds a bunch of files to the tar recursively

@@ -110,4 +150,4 @@ * append- Appends a file, nothing too exciting here...

* pad- Stringifies the number and adds ASCII zeroes to the end (up to 12 zeroes)
** num- the number to stringify
** bytes- how long the resulting string should be
** base- default is base-8
* num- the number to stringify
* bytes- how long the resulting string should be
* base- default is base-8
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