-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 690bca7
Showing
7 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2011 T. Jameson Little | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Intro | ||
===== | ||
|
||
Have you ever wanted to tar something, but you didn't want to push it to your server first? | ||
|
||
Tar-js is here to the rescue!! | ||
|
||
With tar-js, you can construct a tar archive in the browser. This is basically a port of tar-async for Nodejs for the browser, with a couple differences. | ||
|
||
Here's what it supports: | ||
* Add strings to a tar archive as files | ||
* Customizable uid, gid, mtime, and permissions (defaults work well though too) | ||
* Add files in a directory heirarchy | ||
|
||
Dependencies | ||
------------ | ||
|
||
Tar needs an HTML5 compliant browser. More specifically it needs `Uint8Array` to work. | ||
|
||
The only external module is require-kiss, which makes browser JS much more Node-like. | ||
|
||
This module can be installed from npm (`npm install require-kiss`) or directly downloaded from github (https://github.com/coolaj86/require-kiss-js). | ||
|
||
Usage Guide | ||
=========== | ||
|
||
In your HTML file, make sure that require-kiss is included first. Then, to use it, do something like this: | ||
|
||
var Tar = require('tar-js'), | ||
tape = new Tar(); | ||
|
||
Then all you got to do is call `tape.append` with your params and it'll be added to the archive. That's it! | ||
|
||
Here's the api for append: `append(filepath, content, [opts], [callback])` | ||
|
||
* filepath- string path (can include directories and such) | ||
* content- string or Uint8Array | ||
* opts- options: | ||
* mode- permissions of resulting file (octet) [default: 777] | ||
* mtime- modification time in seconds (integer) [default: current time] | ||
* uid- user id (integer) [default: 0] | ||
* gid- group id (integer) [default: 0] | ||
* callback- callback when done (takes a Uint8Array as it's only parameter) | ||
* This is a reference to the tar so far | ||
* Copy it if you want to use it, because subsequent adds may break stuff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<html> | ||
|
||
<head> | ||
<title>Browser Tar</title> | ||
|
||
<script src='../node_modules/require-kiss/require-kiss.js'></script> | ||
<script src='../lib/utils.js'></script> | ||
<script src='../lib/header.js'></script> | ||
<script src='../lib/tar.js'></script> | ||
|
||
<script> | ||
(function () { | ||
"use strict"; | ||
|
||
var Tar = require('tar'), | ||
utils = require('utils'), | ||
tape = new Tar(), | ||
out, | ||
url, | ||
base64; | ||
|
||
out = tape.append('output.txt', 'This is test1!'); | ||
out = tape.append('dir/out.txt', 'This is test2! I changed up the directory'); | ||
out = tape.append('arr.txt', utils.stringToUint8('This is a Uint8Array!')); | ||
|
||
base64 = utils.uint8ToBase64(out); | ||
|
||
url = "data:application/tar;base64," + base64; | ||
window.open(url); | ||
}()); | ||
</script> | ||
</head> | ||
|
||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* tar-js | ||
* MIT (c) 2011 T. Jameson Little | ||
*/ | ||
|
||
(function () { | ||
"use strict"; | ||
|
||
require('require-kiss'); | ||
|
||
/* | ||
struct posix_header { // byte offset | ||
char name[100]; // 0 | ||
char mode[8]; // 100 | ||
char uid[8]; // 108 | ||
char gid[8]; // 116 | ||
char size[12]; // 124 | ||
char mtime[12]; // 136 | ||
char chksum[8]; // 148 | ||
char typeflag; // 156 | ||
char linkname[100]; // 157 | ||
char magic[6]; // 257 | ||
char version[2]; // 263 | ||
char uname[32]; // 265 | ||
char gname[32]; // 297 | ||
char devmajor[8]; // 329 | ||
char devminor[8]; // 337 | ||
char prefix[155]; // 345 | ||
// 500 | ||
}; | ||
*/ | ||
|
||
var utils = require("./utils"), | ||
headerFormat; | ||
|
||
headerFormat = [ | ||
{ | ||
'field': 'fileName', | ||
'length': 100 | ||
}, | ||
{ | ||
'field': 'fileMode', | ||
'length': 8 | ||
}, | ||
{ | ||
'field': 'uid', | ||
'length': 8 | ||
}, | ||
{ | ||
'field': 'gid', | ||
'length': 8 | ||
}, | ||
{ | ||
'field': 'fileSize', | ||
'length': 12 | ||
}, | ||
{ | ||
'field': 'mtime', | ||
'length': 12 | ||
}, | ||
{ | ||
'field': 'checksum', | ||
'length': 8 | ||
}, | ||
{ | ||
'field': 'type', | ||
'length': 1 | ||
}, | ||
{ | ||
'field': 'linkName', | ||
'length': 100 | ||
}, | ||
{ | ||
'field': 'ustar', | ||
'length': 8 | ||
}, | ||
{ | ||
'field': 'owner', | ||
'length': 32 | ||
}, | ||
{ | ||
'field': 'group', | ||
'length': 32 | ||
}, | ||
{ | ||
'field': 'majorNumber', | ||
'length': 8 | ||
}, | ||
{ | ||
'field': 'minorNumber', | ||
'length': 8 | ||
}, | ||
{ | ||
'field': 'filenamePrefix', | ||
'length': 155 | ||
}, | ||
{ | ||
'field': 'padding', | ||
'length': 12 | ||
} | ||
]; | ||
|
||
function formatHeader(data, cb) { | ||
var buffer = utils.clean(512), | ||
offset = 0; | ||
|
||
headerFormat.forEach(function (value) { | ||
var str = data[value.field] || "", | ||
i, length; | ||
|
||
for (i = 0, length = str.length; i < length; i += 1) { | ||
buffer[offset] = str.charCodeAt(i); | ||
offset += 1; | ||
} | ||
|
||
offset += value.length - i; // space it out with nulls | ||
}); | ||
|
||
if (typeof cb === 'function') { | ||
return cb(buffer, offset); | ||
} | ||
return buffer; | ||
} | ||
|
||
module.exports.structure = headerFormat; | ||
module.exports.format = formatHeader; | ||
|
||
provide('header', module.exports); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* tar-js | ||
* MIT (c) 2011 T. Jameson Little | ||
*/ | ||
|
||
(function () { | ||
"use strict"; | ||
|
||
require('require-kiss'); | ||
|
||
var header = require("./header"), | ||
utils = require("./utils"), | ||
recordSize = 512, | ||
blockSize; | ||
|
||
function Tar(recordsPerBlock) { | ||
this.written = 0; | ||
blockSize = (recordsPerBlock || 20) * recordSize; | ||
this.out = utils.clean(blockSize); | ||
} | ||
|
||
Tar.prototype.append = function (filepath, input, opts, callback) { | ||
var data, | ||
checksum, | ||
mode, | ||
mtime, | ||
uid, | ||
gid, | ||
headerArr; | ||
|
||
if (typeof input === 'string') { | ||
input = utils.stringToUint8(input); | ||
} else if (input.constructor !== Uint8Array.prototype.constructor) { | ||
throw 'Invalid input type. You gave me: ' + input.constructor.toString().match(/function\s*([$A-Za-z_][0-9A-Za-z_]*)\s*\(/)[1]; | ||
} | ||
|
||
if (typeof opts === 'function') { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
|
||
opts = opts || {}; | ||
|
||
mode = opts.mode || parseInt('777', 8) & 0xfff; | ||
mtime = opts.mtime || Math.floor(+new Date() / 1000); | ||
uid = opts.uid || 0; | ||
gid = opts.gid || 0; | ||
|
||
data = { | ||
fileName: filepath, | ||
fileMode: utils.pad(mode, 7), | ||
uid: utils.pad(uid, 7), | ||
gid: utils.pad(gid, 7), | ||
fileSize: utils.pad(input.length, 11), | ||
mtime: utils.pad(mtime, 11), | ||
checksum: ' ', | ||
type: '0', // just a file | ||
ustar: 'ustar ', | ||
owner: '', | ||
group: '' | ||
}; | ||
|
||
// calculate the checksum | ||
checksum = 0; | ||
Object.keys(data).forEach(function (key) { | ||
var i, value = data[key], length; | ||
|
||
for (i = 0, length = value.length; i < length; i += 1) { | ||
checksum += value.charCodeAt(i); | ||
} | ||
}); | ||
|
||
data.checksum = utils.pad(checksum, 6) + "\u0000 "; | ||
|
||
headerArr = header.format(data); | ||
|
||
var i, offset, length; | ||
|
||
this.out.set(headerArr, this.written); | ||
|
||
this.written += headerArr.length; | ||
|
||
// this makes sense if the input is greater than 512 bytes | ||
if (headerArr.length + input.length > this.out.length) { | ||
this.out = utils.extend(out, headerArr.length, input.length, blockSize); | ||
} | ||
|
||
this.out.set(input, this.written); | ||
|
||
// to the nearest multiple of recordSize | ||
this.written += input.length + (recordSize - (input.length % recordSize)); | ||
|
||
// make sure there's at least 2 empty records worth of extra space | ||
if (this.out.length - this.written < recordSize * 2) { | ||
this.out = utils.extend(out, this.written, recordSize * 2, blockSize); | ||
} | ||
|
||
if (typeof callback === 'function') { | ||
callback(this.out); | ||
} | ||
|
||
return this.out; | ||
}; | ||
|
||
Tar.prototype.clear = function () { | ||
this.written = 0; | ||
this.out = utils.clean(blockSize); | ||
}; | ||
|
||
module.exports = Tar; | ||
|
||
provide('tar', module.exports); | ||
}()); |
Oops, something went wrong.