This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
176 lines (150 loc) · 5.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var fs = require('graceful-fs')
var Writable = require('readable-stream').Writable
var util = require('util')
var MurmurHash3 = require('imurmurhash')
var iferr = require('iferr')
var crypto = require('crypto')
function murmurhex () {
var hash = MurmurHash3('')
for (var ii = 0; ii < arguments.length; ++ii) {
hash.hash('' + arguments[ii])
}
return hash.result()
}
var invocations = 0
function getTmpname (filename) {
return filename + '.' + murmurhex(__filename, process.pid, ++invocations)
}
var setImmediate = global.setImmediate || setTimeout
module.exports = WriteStreamAtomic
// Requirements:
// 1. Write everything written to the stream to a temp file.
// 2. If there are no errors:
// a. moves the temp file into its final destination
// b. emits `finish` & `closed` ONLY after the file is
// fully flushed and renamed.
// 3. If there's an error, removes the temp file.
util.inherits(WriteStreamAtomic, Writable)
function WriteStreamAtomic (path, options) {
if (!(this instanceof WriteStreamAtomic)) {
return new WriteStreamAtomic(path, options)
}
Writable.call(this, options)
this.__isWin = options && options.hasOwnProperty('isWin') ? options.isWin : process.platform === 'win32'
this.__atomicTarget = path
this.__atomicTmp = getTmpname(path)
this.__atomicChown = options && options.chown
this.__atomicClosed = false
this.__atomicStream = fs.WriteStream(this.__atomicTmp, options)
this.__atomicStream.once('open', handleOpen(this))
this.__atomicStream.once('close', handleClose(this))
this.__atomicStream.once('error', handleError(this))
}
// We have to suppress default finish emitting, because ordinarily it
// would happen as soon as `end` is called on us and all of the
// data has been written to our target stream. So we suppress
// finish from being emitted here, and only emit it after our
// target stream is closed and we've moved everything around.
WriteStreamAtomic.prototype.emit = function (event) {
if (event === 'finish') return this.__atomicStream.end()
return Writable.prototype.emit.apply(this, arguments)
}
WriteStreamAtomic.prototype._write = function (buffer, encoding, cb) {
var flushed = this.__atomicStream.write(buffer, encoding)
if (flushed) return cb()
this.__atomicStream.once('drain', cb)
}
function handleOpen (writeStream) {
return function (fd) {
writeStream.emit('open', fd)
}
}
function handleClose (writeStream) {
return function () {
if (writeStream.__atomicClosed) return
writeStream.__atomicClosed = true
if (writeStream.__atomicChown) {
var uid = writeStream.__atomicChown.uid
var gid = writeStream.__atomicChown.gid
return fs.chown(writeStream.__atomicTmp, uid, gid, iferr(cleanup, moveIntoPlace))
} else {
moveIntoPlace()
}
}
function moveIntoPlace () {
fs.rename(writeStream.__atomicTmp, writeStream.__atomicTarget, iferr(trapWindowsEPERM, end))
}
function trapWindowsEPERM (err) {
if (writeStream.__isWin &&
err.syscall && err.syscall === 'rename' &&
err.code && err.code === 'EPERM'
) {
checkFileHashes(err)
} else {
cleanup(err)
}
}
function checkFileHashes (eperm) {
var inprocess = 2
var tmpFileHash = crypto.createHash('sha512')
var targetFileHash = crypto.createHash('sha512')
fs.createReadStream(writeStream.__atomicTmp)
.on('data', function (data, enc) { tmpFileHash.update(data, enc) })
.on('error', fileHashError)
.on('end', fileHashComplete)
fs.createReadStream(writeStream.__atomicTarget)
.on('data', function (data, enc) { targetFileHash.update(data, enc) })
.on('error', fileHashError)
.on('end', fileHashComplete)
function fileHashError () {
if (inprocess === 0) return
inprocess = 0
cleanup(eperm)
}
function fileHashComplete () {
if (inprocess === 0) return
if (--inprocess) return
if (tmpFileHash.digest('hex') === targetFileHash.digest('hex')) {
return cleanup()
} else {
return cleanup(eperm)
}
}
}
function cleanup (err) {
fs.unlink(writeStream.__atomicTmp, function () {
if (err) {
writeStream.emit('error', err)
writeStream.emit('close')
} else {
end()
}
})
}
function end () {
// We have to use our parent class directly because we suppress `finish`
// events fired via our own emit method.
Writable.prototype.emit.call(writeStream, 'finish')
// Delay the close to provide the same temporal separation a physical
// file operation would have– that is, the close event is emitted only
// after the async close operation completes.
setImmediate(function () {
writeStream.emit('close')
})
}
}
function handleError (writeStream) {
return function (er) {
cleanupSync()
writeStream.emit('error', er)
writeStream.__atomicClosed = true
writeStream.emit('close')
}
function cleanupSync () {
try {
fs.unlinkSync(writeStream.__atomicTmp)
} finally {
return
}
}
}