]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/graceful-fs/graceful-fs.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / graceful-fs / graceful-fs.js
1 var fs = require('fs')
2 var polyfills = require('./polyfills.js')
3 var legacy = require('./legacy-streams.js')
4 var queue = []
5
6 var util = require('util')
7
8 function noop () {}
9
10 var debug = noop
11 if (util.debuglog)
12   debug = util.debuglog('gfs4')
13 else if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || ''))
14   debug = function() {
15     var m = util.format.apply(util, arguments)
16     m = 'GFS4: ' + m.split(/\n/).join('\nGFS4: ')
17     console.error(m)
18   }
19
20 if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) {
21   process.on('exit', function() {
22     debug(queue)
23     require('assert').equal(queue.length, 0)
24   })
25 }
26
27 module.exports = patch(require('./fs.js'))
28 if (process.env.TEST_GRACEFUL_FS_GLOBAL_PATCH) {
29   module.exports = patch(fs)
30 }
31
32 // Always patch fs.close/closeSync, because we want to
33 // retry() whenever a close happens *anywhere* in the program.
34 // This is essential when multiple graceful-fs instances are
35 // in play at the same time.
36 module.exports.close =
37 fs.close = (function (fs$close) { return function (fd, cb) {
38   return fs$close.call(fs, fd, function (err) {
39     if (!err)
40       retry()
41
42     if (typeof cb === 'function')
43       cb.apply(this, arguments)
44   })
45 }})(fs.close)
46
47 module.exports.closeSync =
48 fs.closeSync = (function (fs$closeSync) { return function (fd) {
49   // Note that graceful-fs also retries when fs.closeSync() fails.
50   // Looks like a bug to me, although it's probably a harmless one.
51   var rval = fs$closeSync.apply(fs, arguments)
52   retry()
53   return rval
54 }})(fs.closeSync)
55
56 function patch (fs) {
57   // Everything that references the open() function needs to be in here
58   polyfills(fs)
59   fs.gracefulify = patch
60   fs.FileReadStream = ReadStream;  // Legacy name.
61   fs.FileWriteStream = WriteStream;  // Legacy name.
62   fs.createReadStream = createReadStream
63   fs.createWriteStream = createWriteStream
64   var fs$readFile = fs.readFile
65   fs.readFile = readFile
66   function readFile (path, options, cb) {
67     if (typeof options === 'function')
68       cb = options, options = null
69
70     return go$readFile(path, options, cb)
71
72     function go$readFile (path, options, cb) {
73       return fs$readFile(path, options, function (err) {
74         if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
75           enqueue([go$readFile, [path, options, cb]])
76         else {
77           if (typeof cb === 'function')
78             cb.apply(this, arguments)
79           retry()
80         }
81       })
82     }
83   }
84
85   var fs$writeFile = fs.writeFile
86   fs.writeFile = writeFile
87   function writeFile (path, data, options, cb) {
88     if (typeof options === 'function')
89       cb = options, options = null
90
91     return go$writeFile(path, data, options, cb)
92
93     function go$writeFile (path, data, options, cb) {
94       return fs$writeFile(path, data, options, function (err) {
95         if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
96           enqueue([go$writeFile, [path, data, options, cb]])
97         else {
98           if (typeof cb === 'function')
99             cb.apply(this, arguments)
100           retry()
101         }
102       })
103     }
104   }
105
106   var fs$appendFile = fs.appendFile
107   if (fs$appendFile)
108     fs.appendFile = appendFile
109   function appendFile (path, data, options, cb) {
110     if (typeof options === 'function')
111       cb = options, options = null
112
113     return go$appendFile(path, data, options, cb)
114
115     function go$appendFile (path, data, options, cb) {
116       return fs$appendFile(path, data, options, function (err) {
117         if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
118           enqueue([go$appendFile, [path, data, options, cb]])
119         else {
120           if (typeof cb === 'function')
121             cb.apply(this, arguments)
122           retry()
123         }
124       })
125     }
126   }
127
128   var fs$readdir = fs.readdir
129   fs.readdir = readdir
130   function readdir (path, cb) {
131     return go$readdir(path, cb)
132
133     function go$readdir () {
134       return fs$readdir(path, function (err, files) {
135         if (files && files.sort)
136           files.sort();  // Backwards compatibility with graceful-fs.
137
138         if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
139           enqueue([go$readdir, [path, cb]])
140         else {
141           if (typeof cb === 'function')
142             cb.apply(this, arguments)
143           retry()
144         }
145       })
146     }
147   }
148
149
150   if (process.version.substr(0, 4) === 'v0.8') {
151     var legStreams = legacy(fs)
152     ReadStream = legStreams.ReadStream
153     WriteStream = legStreams.WriteStream
154   }
155
156   var fs$ReadStream = fs.ReadStream
157   ReadStream.prototype = Object.create(fs$ReadStream.prototype)
158   ReadStream.prototype.open = ReadStream$open
159
160   var fs$WriteStream = fs.WriteStream
161   WriteStream.prototype = Object.create(fs$WriteStream.prototype)
162   WriteStream.prototype.open = WriteStream$open
163
164   fs.ReadStream = ReadStream
165   fs.WriteStream = WriteStream
166
167   function ReadStream (path, options) {
168     if (this instanceof ReadStream)
169       return fs$ReadStream.apply(this, arguments), this
170     else
171       return ReadStream.apply(Object.create(ReadStream.prototype), arguments)
172   }
173
174   function ReadStream$open () {
175     var that = this
176     open(that.path, that.flags, that.mode, function (err, fd) {
177       if (err) {
178         if (that.autoClose)
179           that.destroy()
180
181         that.emit('error', err)
182       } else {
183         that.fd = fd
184         that.emit('open', fd)
185         that.read()
186       }
187     })
188   }
189
190   function WriteStream (path, options) {
191     if (this instanceof WriteStream)
192       return fs$WriteStream.apply(this, arguments), this
193     else
194       return WriteStream.apply(Object.create(WriteStream.prototype), arguments)
195   }
196
197   function WriteStream$open () {
198     var that = this
199     open(that.path, that.flags, that.mode, function (err, fd) {
200       if (err) {
201         that.destroy()
202         that.emit('error', err)
203       } else {
204         that.fd = fd
205         that.emit('open', fd)
206       }
207     })
208   }
209
210   function createReadStream (path, options) {
211     return new ReadStream(path, options)
212   }
213
214   function createWriteStream (path, options) {
215     return new WriteStream(path, options)
216   }
217
218   var fs$open = fs.open
219   fs.open = open
220   function open (path, flags, mode, cb) {
221     if (typeof mode === 'function')
222       cb = mode, mode = null
223
224     return go$open(path, flags, mode, cb)
225
226     function go$open (path, flags, mode, cb) {
227       return fs$open(path, flags, mode, function (err, fd) {
228         if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
229           enqueue([go$open, [path, flags, mode, cb]])
230         else {
231           if (typeof cb === 'function')
232             cb.apply(this, arguments)
233           retry()
234         }
235       })
236     }
237   }
238
239   return fs
240 }
241
242 function enqueue (elem) {
243   debug('ENQUEUE', elem[0].name, elem[1])
244   queue.push(elem)
245 }
246
247 function retry () {
248   var elem = queue.shift()
249   if (elem) {
250     debug('RETRY', elem[0].name, elem[1])
251     elem[0].apply(null, elem[1])
252   }
253 }