123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- var inherits = require('util').inherits;
- var Transform = require('readable-stream').Transform;
- var crc32 = require('buffer-crc32');
- var util = require('archiver-utils');
- var Json = function(options) {
- if (!(this instanceof Json)) {
- return new Json(options);
- }
- options = this.options = util.defaults(options, {});
- Transform.call(this, options);
- this.supports = {
- directory: true,
- symlink: true
- };
- this.files = [];
- };
- inherits(Json, Transform);
- Json.prototype._transform = function(chunk, encoding, callback) {
- callback(null, chunk);
- };
- Json.prototype._writeStringified = function() {
- var fileString = JSON.stringify(this.files);
- this.write(fileString);
- };
- Json.prototype.append = function(source, data, callback) {
- var self = this;
- data.crc32 = 0;
- function onend(err, sourceBuffer) {
- if (err) {
- callback(err);
- return;
- }
- data.size = sourceBuffer.length || 0;
- data.crc32 = crc32.unsigned(sourceBuffer);
- self.files.push(data);
- callback(null, data);
- }
- if (data.sourceType === 'buffer') {
- onend(null, source);
- } else if (data.sourceType === 'stream') {
- util.collectStream(source, onend);
- }
- };
- Json.prototype.finalize = function() {
- this._writeStringified();
- this.end();
- };
- module.exports = Json;
|