pack.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. var constants = require('fs-constants')
  2. var eos = require('end-of-stream')
  3. var inherits = require('inherits')
  4. var alloc = Buffer.alloc
  5. var Readable = require('readable-stream').Readable
  6. var Writable = require('readable-stream').Writable
  7. var StringDecoder = require('string_decoder').StringDecoder
  8. var headers = require('./headers')
  9. var DMODE = parseInt('755', 8)
  10. var FMODE = parseInt('644', 8)
  11. var END_OF_TAR = alloc(1024)
  12. var noop = function () {}
  13. var overflow = function (self, size) {
  14. size &= 511
  15. if (size) self.push(END_OF_TAR.slice(0, 512 - size))
  16. }
  17. function modeToType (mode) {
  18. switch (mode & constants.S_IFMT) {
  19. case constants.S_IFBLK: return 'block-device'
  20. case constants.S_IFCHR: return 'character-device'
  21. case constants.S_IFDIR: return 'directory'
  22. case constants.S_IFIFO: return 'fifo'
  23. case constants.S_IFLNK: return 'symlink'
  24. }
  25. return 'file'
  26. }
  27. var Sink = function (to) {
  28. Writable.call(this)
  29. this.written = 0
  30. this._to = to
  31. this._destroyed = false
  32. }
  33. inherits(Sink, Writable)
  34. Sink.prototype._write = function (data, enc, cb) {
  35. this.written += data.length
  36. if (this._to.push(data)) return cb()
  37. this._to._drain = cb
  38. }
  39. Sink.prototype.destroy = function () {
  40. if (this._destroyed) return
  41. this._destroyed = true
  42. this.emit('close')
  43. }
  44. var LinkSink = function () {
  45. Writable.call(this)
  46. this.linkname = ''
  47. this._decoder = new StringDecoder('utf-8')
  48. this._destroyed = false
  49. }
  50. inherits(LinkSink, Writable)
  51. LinkSink.prototype._write = function (data, enc, cb) {
  52. this.linkname += this._decoder.write(data)
  53. cb()
  54. }
  55. LinkSink.prototype.destroy = function () {
  56. if (this._destroyed) return
  57. this._destroyed = true
  58. this.emit('close')
  59. }
  60. var Void = function () {
  61. Writable.call(this)
  62. this._destroyed = false
  63. }
  64. inherits(Void, Writable)
  65. Void.prototype._write = function (data, enc, cb) {
  66. cb(new Error('No body allowed for this entry'))
  67. }
  68. Void.prototype.destroy = function () {
  69. if (this._destroyed) return
  70. this._destroyed = true
  71. this.emit('close')
  72. }
  73. var Pack = function (opts) {
  74. if (!(this instanceof Pack)) return new Pack(opts)
  75. Readable.call(this, opts)
  76. this._drain = noop
  77. this._finalized = false
  78. this._finalizing = false
  79. this._destroyed = false
  80. this._stream = null
  81. }
  82. inherits(Pack, Readable)
  83. Pack.prototype.entry = function (header, buffer, callback) {
  84. if (this._stream) throw new Error('already piping an entry')
  85. if (this._finalized || this._destroyed) return
  86. if (typeof buffer === 'function') {
  87. callback = buffer
  88. buffer = null
  89. }
  90. if (!callback) callback = noop
  91. var self = this
  92. if (!header.size || header.type === 'symlink') header.size = 0
  93. if (!header.type) header.type = modeToType(header.mode)
  94. if (!header.mode) header.mode = header.type === 'directory' ? DMODE : FMODE
  95. if (!header.uid) header.uid = 0
  96. if (!header.gid) header.gid = 0
  97. if (!header.mtime) header.mtime = new Date()
  98. if (typeof buffer === 'string') buffer = Buffer.from(buffer)
  99. if (Buffer.isBuffer(buffer)) {
  100. header.size = buffer.length
  101. this._encode(header)
  102. var ok = this.push(buffer)
  103. overflow(self, header.size)
  104. if (ok) process.nextTick(callback)
  105. else this._drain = callback
  106. return new Void()
  107. }
  108. if (header.type === 'symlink' && !header.linkname) {
  109. var linkSink = new LinkSink()
  110. eos(linkSink, function (err) {
  111. if (err) { // stream was closed
  112. self.destroy()
  113. return callback(err)
  114. }
  115. header.linkname = linkSink.linkname
  116. self._encode(header)
  117. callback()
  118. })
  119. return linkSink
  120. }
  121. this._encode(header)
  122. if (header.type !== 'file' && header.type !== 'contiguous-file') {
  123. process.nextTick(callback)
  124. return new Void()
  125. }
  126. var sink = new Sink(this)
  127. this._stream = sink
  128. eos(sink, function (err) {
  129. self._stream = null
  130. if (err) { // stream was closed
  131. self.destroy()
  132. return callback(err)
  133. }
  134. if (sink.written !== header.size) { // corrupting tar
  135. self.destroy()
  136. return callback(new Error('size mismatch'))
  137. }
  138. overflow(self, header.size)
  139. if (self._finalizing) self.finalize()
  140. callback()
  141. })
  142. return sink
  143. }
  144. Pack.prototype.finalize = function () {
  145. if (this._stream) {
  146. this._finalizing = true
  147. return
  148. }
  149. if (this._finalized) return
  150. this._finalized = true
  151. this.push(END_OF_TAR)
  152. this.push(null)
  153. }
  154. Pack.prototype.destroy = function (err) {
  155. if (this._destroyed) return
  156. this._destroyed = true
  157. if (err) this.emit('error', err)
  158. this.emit('close')
  159. if (this._stream && this._stream.destroy) this._stream.destroy()
  160. }
  161. Pack.prototype._encode = function (header) {
  162. if (!header.pax) {
  163. var buf = headers.encode(header)
  164. if (buf) {
  165. this.push(buf)
  166. return
  167. }
  168. }
  169. this._encodePax(header)
  170. }
  171. Pack.prototype._encodePax = function (header) {
  172. var paxHeader = headers.encodePax({
  173. name: header.name,
  174. linkname: header.linkname,
  175. pax: header.pax
  176. })
  177. var newHeader = {
  178. name: 'PaxHeader',
  179. mode: header.mode,
  180. uid: header.uid,
  181. gid: header.gid,
  182. size: paxHeader.length,
  183. mtime: header.mtime,
  184. type: 'pax-header',
  185. linkname: header.linkname && 'PaxHeader',
  186. uname: header.uname,
  187. gname: header.gname,
  188. devmajor: header.devmajor,
  189. devminor: header.devminor
  190. }
  191. this.push(headers.encode(newHeader))
  192. this.push(paxHeader)
  193. overflow(this, paxHeader.length)
  194. newHeader.size = header.size
  195. newHeader.type = header.type
  196. this.push(headers.encode(newHeader))
  197. }
  198. Pack.prototype._read = function (n) {
  199. var drain = this._drain
  200. this._drain = noop
  201. drain()
  202. }
  203. module.exports = Pack