1 module.exports = EntryWriter
3 var tar = require("../tar.js")
4 , TarHeader = require("./header.js")
5 , Entry = require("./entry.js")
6 , inherits = require("inherits")
7 , BlockStream = require("block-stream")
9 , Stream = require("stream").Stream
12 inherits(EntryWriter, Stream)
14 function EntryWriter (props) {
17 if (!(me instanceof EntryWriter)) {
18 return new EntryWriter(props)
26 me._stream = new BlockStream(512)
28 me._stream.on("data", function (c) {
32 me._stream.on("drain", function () {
36 me._stream.on("end", function () {
42 if (props.type === "Directory") {
45 props.ustar = "ustar\0"
53 me.on("pipe", function () {
58 EntryWriter.prototype.write = function (c) {
59 // console.error(".. ew write")
60 if (this._ended) return this.emit("error", new Error("write after end"))
63 this._needDrain = this._buffer.length > 0
64 return !this._needDrain
67 EntryWriter.prototype.end = function (c) {
68 // console.error(".. ew end")
69 if (c) this._buffer.push(c)
70 this._buffer.push(EOF)
73 this._needDrain = this._buffer.length > 0
76 EntryWriter.prototype.pause = function () {
77 // console.error(".. ew pause")
82 EntryWriter.prototype.resume = function () {
83 // console.error(".. ew resume")
89 EntryWriter.prototype.add = function (entry) {
90 // console.error(".. ew add")
91 if (!this.parent) return this.emit("error", new Error("no parent"))
93 // make sure that the _header and such is emitted, and clear out
94 // the _currentEntry link on the parent.
95 if (!this._ended) this.end()
97 return this.parent.add(entry)
100 EntryWriter.prototype._header = function () {
101 // console.error(".. ew header")
102 if (this._didHeader) return
103 this._didHeader = true
105 var headerBlock = TarHeader.encode(this.props)
107 if (this.props.needExtended && !this._meta) {
110 ExtendedHeaderWriter = ExtendedHeaderWriter ||
111 require("./extended-header-writer.js")
113 ExtendedHeaderWriter(this.props)
114 .on("data", function (c) {
117 .on("error", function (er) {
123 // console.error(".. .. ew headerBlock emitting")
124 this.emit("data", headerBlock)
128 EntryWriter.prototype._process = function () {
129 // console.error(".. .. ew process")
130 if (!this._didHeader && !this._meta) {
134 if (this._paused || this._processing) {
135 // console.error(".. .. .. paused=%j, processing=%j", this._paused, this._processing)
139 this._processing = true
141 var buf = this._buffer
142 for (var i = 0; i < buf.length; i ++) {
143 // console.error(".. .. .. i=%d", i)
147 if (c === EOF) this._stream.end()
148 else this._stream.write(c)
151 // console.error(".. .. .. paused mid-emission")
152 this._processing = false
153 if (i < buf.length) {
154 this._needDrain = true
155 this._buffer = buf.slice(i + 1)
161 // console.error(".. .. .. emitted")
162 this._buffer.length = 0
163 this._processing = false
165 // console.error(".. .. .. emitting drain")
169 EntryWriter.prototype.destroy = function () {}