index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. 'use strict'
  2. const { Minipass } = require('minipass')
  3. const EE = require('events').EventEmitter
  4. const fs = require('fs')
  5. const writev = fs.writev
  6. const _autoClose = Symbol('_autoClose')
  7. const _close = Symbol('_close')
  8. const _ended = Symbol('_ended')
  9. const _fd = Symbol('_fd')
  10. const _finished = Symbol('_finished')
  11. const _flags = Symbol('_flags')
  12. const _flush = Symbol('_flush')
  13. const _handleChunk = Symbol('_handleChunk')
  14. const _makeBuf = Symbol('_makeBuf')
  15. const _mode = Symbol('_mode')
  16. const _needDrain = Symbol('_needDrain')
  17. const _onerror = Symbol('_onerror')
  18. const _onopen = Symbol('_onopen')
  19. const _onread = Symbol('_onread')
  20. const _onwrite = Symbol('_onwrite')
  21. const _open = Symbol('_open')
  22. const _path = Symbol('_path')
  23. const _pos = Symbol('_pos')
  24. const _queue = Symbol('_queue')
  25. const _read = Symbol('_read')
  26. const _readSize = Symbol('_readSize')
  27. const _reading = Symbol('_reading')
  28. const _remain = Symbol('_remain')
  29. const _size = Symbol('_size')
  30. const _write = Symbol('_write')
  31. const _writing = Symbol('_writing')
  32. const _defaultFlag = Symbol('_defaultFlag')
  33. const _errored = Symbol('_errored')
  34. class ReadStream extends Minipass {
  35. constructor (path, opt) {
  36. opt = opt || {}
  37. super(opt)
  38. this.readable = true
  39. this.writable = false
  40. if (typeof path !== 'string') {
  41. throw new TypeError('path must be a string')
  42. }
  43. this[_errored] = false
  44. this[_fd] = typeof opt.fd === 'number' ? opt.fd : null
  45. this[_path] = path
  46. this[_readSize] = opt.readSize || 16 * 1024 * 1024
  47. this[_reading] = false
  48. this[_size] = typeof opt.size === 'number' ? opt.size : Infinity
  49. this[_remain] = this[_size]
  50. this[_autoClose] = typeof opt.autoClose === 'boolean' ?
  51. opt.autoClose : true
  52. if (typeof this[_fd] === 'number') {
  53. this[_read]()
  54. } else {
  55. this[_open]()
  56. }
  57. }
  58. get fd () {
  59. return this[_fd]
  60. }
  61. get path () {
  62. return this[_path]
  63. }
  64. write () {
  65. throw new TypeError('this is a readable stream')
  66. }
  67. end () {
  68. throw new TypeError('this is a readable stream')
  69. }
  70. [_open] () {
  71. fs.open(this[_path], 'r', (er, fd) => this[_onopen](er, fd))
  72. }
  73. [_onopen] (er, fd) {
  74. if (er) {
  75. this[_onerror](er)
  76. } else {
  77. this[_fd] = fd
  78. this.emit('open', fd)
  79. this[_read]()
  80. }
  81. }
  82. [_makeBuf] () {
  83. return Buffer.allocUnsafe(Math.min(this[_readSize], this[_remain]))
  84. }
  85. [_read] () {
  86. if (!this[_reading]) {
  87. this[_reading] = true
  88. const buf = this[_makeBuf]()
  89. /* istanbul ignore if */
  90. if (buf.length === 0) {
  91. return process.nextTick(() => this[_onread](null, 0, buf))
  92. }
  93. fs.read(this[_fd], buf, 0, buf.length, null, (er, br, b) =>
  94. this[_onread](er, br, b))
  95. }
  96. }
  97. [_onread] (er, br, buf) {
  98. this[_reading] = false
  99. if (er) {
  100. this[_onerror](er)
  101. } else if (this[_handleChunk](br, buf)) {
  102. this[_read]()
  103. }
  104. }
  105. [_close] () {
  106. if (this[_autoClose] && typeof this[_fd] === 'number') {
  107. const fd = this[_fd]
  108. this[_fd] = null
  109. fs.close(fd, er => er ? this.emit('error', er) : this.emit('close'))
  110. }
  111. }
  112. [_onerror] (er) {
  113. this[_reading] = true
  114. this[_close]()
  115. this.emit('error', er)
  116. }
  117. [_handleChunk] (br, buf) {
  118. let ret = false
  119. // no effect if infinite
  120. this[_remain] -= br
  121. if (br > 0) {
  122. ret = super.write(br < buf.length ? buf.slice(0, br) : buf)
  123. }
  124. if (br === 0 || this[_remain] <= 0) {
  125. ret = false
  126. this[_close]()
  127. super.end()
  128. }
  129. return ret
  130. }
  131. emit (ev, data) {
  132. switch (ev) {
  133. case 'prefinish':
  134. case 'finish':
  135. break
  136. case 'drain':
  137. if (typeof this[_fd] === 'number') {
  138. this[_read]()
  139. }
  140. break
  141. case 'error':
  142. if (this[_errored]) {
  143. return
  144. }
  145. this[_errored] = true
  146. return super.emit(ev, data)
  147. default:
  148. return super.emit(ev, data)
  149. }
  150. }
  151. }
  152. class ReadStreamSync extends ReadStream {
  153. [_open] () {
  154. let threw = true
  155. try {
  156. this[_onopen](null, fs.openSync(this[_path], 'r'))
  157. threw = false
  158. } finally {
  159. if (threw) {
  160. this[_close]()
  161. }
  162. }
  163. }
  164. [_read] () {
  165. let threw = true
  166. try {
  167. if (!this[_reading]) {
  168. this[_reading] = true
  169. do {
  170. const buf = this[_makeBuf]()
  171. /* istanbul ignore next */
  172. const br = buf.length === 0 ? 0
  173. : fs.readSync(this[_fd], buf, 0, buf.length, null)
  174. if (!this[_handleChunk](br, buf)) {
  175. break
  176. }
  177. } while (true)
  178. this[_reading] = false
  179. }
  180. threw = false
  181. } finally {
  182. if (threw) {
  183. this[_close]()
  184. }
  185. }
  186. }
  187. [_close] () {
  188. if (this[_autoClose] && typeof this[_fd] === 'number') {
  189. const fd = this[_fd]
  190. this[_fd] = null
  191. fs.closeSync(fd)
  192. this.emit('close')
  193. }
  194. }
  195. }
  196. class WriteStream extends EE {
  197. constructor (path, opt) {
  198. opt = opt || {}
  199. super(opt)
  200. this.readable = false
  201. this.writable = true
  202. this[_errored] = false
  203. this[_writing] = false
  204. this[_ended] = false
  205. this[_needDrain] = false
  206. this[_queue] = []
  207. this[_path] = path
  208. this[_fd] = typeof opt.fd === 'number' ? opt.fd : null
  209. this[_mode] = opt.mode === undefined ? 0o666 : opt.mode
  210. this[_pos] = typeof opt.start === 'number' ? opt.start : null
  211. this[_autoClose] = typeof opt.autoClose === 'boolean' ?
  212. opt.autoClose : true
  213. // truncating makes no sense when writing into the middle
  214. const defaultFlag = this[_pos] !== null ? 'r+' : 'w'
  215. this[_defaultFlag] = opt.flags === undefined
  216. this[_flags] = this[_defaultFlag] ? defaultFlag : opt.flags
  217. if (this[_fd] === null) {
  218. this[_open]()
  219. }
  220. }
  221. emit (ev, data) {
  222. if (ev === 'error') {
  223. if (this[_errored]) {
  224. return
  225. }
  226. this[_errored] = true
  227. }
  228. return super.emit(ev, data)
  229. }
  230. get fd () {
  231. return this[_fd]
  232. }
  233. get path () {
  234. return this[_path]
  235. }
  236. [_onerror] (er) {
  237. this[_close]()
  238. this[_writing] = true
  239. this.emit('error', er)
  240. }
  241. [_open] () {
  242. fs.open(this[_path], this[_flags], this[_mode],
  243. (er, fd) => this[_onopen](er, fd))
  244. }
  245. [_onopen] (er, fd) {
  246. if (this[_defaultFlag] &&
  247. this[_flags] === 'r+' &&
  248. er && er.code === 'ENOENT') {
  249. this[_flags] = 'w'
  250. this[_open]()
  251. } else if (er) {
  252. this[_onerror](er)
  253. } else {
  254. this[_fd] = fd
  255. this.emit('open', fd)
  256. if (!this[_writing]) {
  257. this[_flush]()
  258. }
  259. }
  260. }
  261. end (buf, enc) {
  262. if (buf) {
  263. this.write(buf, enc)
  264. }
  265. this[_ended] = true
  266. // synthetic after-write logic, where drain/finish live
  267. if (!this[_writing] && !this[_queue].length &&
  268. typeof this[_fd] === 'number') {
  269. this[_onwrite](null, 0)
  270. }
  271. return this
  272. }
  273. write (buf, enc) {
  274. if (typeof buf === 'string') {
  275. buf = Buffer.from(buf, enc)
  276. }
  277. if (this[_ended]) {
  278. this.emit('error', new Error('write() after end()'))
  279. return false
  280. }
  281. if (this[_fd] === null || this[_writing] || this[_queue].length) {
  282. this[_queue].push(buf)
  283. this[_needDrain] = true
  284. return false
  285. }
  286. this[_writing] = true
  287. this[_write](buf)
  288. return true
  289. }
  290. [_write] (buf) {
  291. fs.write(this[_fd], buf, 0, buf.length, this[_pos], (er, bw) =>
  292. this[_onwrite](er, bw))
  293. }
  294. [_onwrite] (er, bw) {
  295. if (er) {
  296. this[_onerror](er)
  297. } else {
  298. if (this[_pos] !== null) {
  299. this[_pos] += bw
  300. }
  301. if (this[_queue].length) {
  302. this[_flush]()
  303. } else {
  304. this[_writing] = false
  305. if (this[_ended] && !this[_finished]) {
  306. this[_finished] = true
  307. this[_close]()
  308. this.emit('finish')
  309. } else if (this[_needDrain]) {
  310. this[_needDrain] = false
  311. this.emit('drain')
  312. }
  313. }
  314. }
  315. }
  316. [_flush] () {
  317. if (this[_queue].length === 0) {
  318. if (this[_ended]) {
  319. this[_onwrite](null, 0)
  320. }
  321. } else if (this[_queue].length === 1) {
  322. this[_write](this[_queue].pop())
  323. } else {
  324. const iovec = this[_queue]
  325. this[_queue] = []
  326. writev(this[_fd], iovec, this[_pos],
  327. (er, bw) => this[_onwrite](er, bw))
  328. }
  329. }
  330. [_close] () {
  331. if (this[_autoClose] && typeof this[_fd] === 'number') {
  332. const fd = this[_fd]
  333. this[_fd] = null
  334. fs.close(fd, er => er ? this.emit('error', er) : this.emit('close'))
  335. }
  336. }
  337. }
  338. class WriteStreamSync extends WriteStream {
  339. [_open] () {
  340. let fd
  341. // only wrap in a try{} block if we know we'll retry, to avoid
  342. // the rethrow obscuring the error's source frame in most cases.
  343. if (this[_defaultFlag] && this[_flags] === 'r+') {
  344. try {
  345. fd = fs.openSync(this[_path], this[_flags], this[_mode])
  346. } catch (er) {
  347. if (er.code === 'ENOENT') {
  348. this[_flags] = 'w'
  349. return this[_open]()
  350. } else {
  351. throw er
  352. }
  353. }
  354. } else {
  355. fd = fs.openSync(this[_path], this[_flags], this[_mode])
  356. }
  357. this[_onopen](null, fd)
  358. }
  359. [_close] () {
  360. if (this[_autoClose] && typeof this[_fd] === 'number') {
  361. const fd = this[_fd]
  362. this[_fd] = null
  363. fs.closeSync(fd)
  364. this.emit('close')
  365. }
  366. }
  367. [_write] (buf) {
  368. // throw the original, but try to close if it fails
  369. let threw = true
  370. try {
  371. this[_onwrite](null,
  372. fs.writeSync(this[_fd], buf, 0, buf.length, this[_pos]))
  373. threw = false
  374. } finally {
  375. if (threw) {
  376. try {
  377. this[_close]()
  378. } catch {
  379. // ok error
  380. }
  381. }
  382. }
  383. }
  384. }
  385. exports.ReadStream = ReadStream
  386. exports.ReadStreamSync = ReadStreamSync
  387. exports.WriteStream = WriteStream
  388. exports.WriteStreamSync = WriteStreamSync