Debian Codegraph @

fs2/descriptors-handler.js

raw
1 /* eslint max-lines: off */
2
3 "use strict";
4
5 var last = require("es5-ext/array/#/last")
6 , defineLength = require("es5-ext/function/_define-length")
7 , callable = require("es5-ext/object/valid-callable")
8 , d = require("d")
9 , memoize = require("memoizee")
10 , fs = require("fs");
11
12 var max = Math.max
13 , slice = Array.prototype.slice
14 , limit = Infinity
15 , count = 0
16 , queue = []
17 , debugStats = { fd: 0, unknown: 0 };
18
19 var release = function () {
20 var data, fnCb;
21 // eslint-disable-next-line no-unmodified-loop-condition
22 while (count < limit && (data = queue.shift())) {
23 try {
24 data.fn.apply(data.context, data.args);
25 } catch (e) {
26 fnCb = last.call(data.args);
27 if (typeof fnCb === "function") fnCb(e);
28 }
29 }
30 };
31
32 var wrap = function (asyncFn, type) {
33 var self;
34 debugStats[type] = 0;
35 callable(asyncFn);
36 return (self = defineLength(function () {
37 var openCount, args = arguments, context, cb = last.call(args);
38 if (!exports.initialized || typeof cb !== "function") return asyncFn.apply(this, arguments);
39 if (count >= limit) {
40 queue.push({ fn: self, context: this, args: arguments });
41 return null;
42 }
43 openCount = count++;
44 context = this;
45 args = slice.call(args, 0, -1);
46 args.push(function (err, resultIgnored) {
47 --debugStats[type];
48 --count;
49 if (err && (err.code === "EMFILE" || err.code === "ENFILE")) {
50 if (limit > openCount) limit = openCount;
51 queue.push({ fn: self, context: context, args: args });
52 release();
53 return;
54 }
55 release();
56 if (typeof cb === "function") cb.apply(this, arguments);
57 });
58 ++debugStats[type];
59 return asyncFn.apply(this, args);
60 }, asyncFn.length));
61 };
62
63 module.exports = exports = memoize(function () {
64 var open = fs.open, openSync = fs.openSync, close = fs.close, closeSync = fs.closeSync;
65
66 if (exports.initialized) return;
67
68 fs.open = function (path, flags, mode, fnCb) {
69 var openCount, args;
70 if (count >= limit) {
71 queue.push({ fn: fs.open, context: this, args: arguments });
72 return;
73 }
74 openCount = count++;
75 args = arguments;
76 fnCb = last.call(args);
77 ++debugStats.fd;
78 open(path, flags, mode, function (err, fd) {
79 if (err) {
80 --debugStats.fd;
81 --count;
82 if (err.code === "EMFILE" || err.code === "ENFILE") {
83 if (limit > openCount) limit = openCount;
84 queue.push({ fn: fs.open, context: this, args: args });
85 release();
86 return;
87 }
88 release();
89 }
90 if (typeof fnCb === "function") fnCb(err, fd);
91 });
92 };
93
94 fs.openSync = function (pathIgnored, flagsIgnored, modeIgnored) {
95 var result = openSync.apply(this, arguments);
96 ++debugStats.fd;
97 ++count;
98 return result;
99 };
100
101 fs.close = function (fd, fnCb) {
102 close(fd, function (err) {
103 if (!err) {
104 --debugStats.fd;
105 --count;
106 release();
107 }
108 if (typeof fnCb === "function") fnCb(err);
109 });
110 };
111
112 fs.closeSync = function (fd) {
113 var result;
114 result = closeSync(fd);
115 --debugStats.fd;
116 --count;
117 release();
118 return result;
119 };
120
121 fs.readdir = wrap(fs.readdir, "readdir");
122 // Needed for Node >=1.2 because of commit e65308053c
123 fs.readFile = wrap(fs.readFile, "readFile");
124
125 Object.defineProperty(exports, "initialized", d("e", true));
126 });
127
128 Object.defineProperties(exports, {
129 initialized: d("ce", false),
130 limit: d.gs(
131 function () { return limit; },
132 function (nLimit) { if (limit >= nLimit) limit = max(nLimit, 5); }
133 ),
134 available: d.gs(function () { return max(limit - count, 0); }),
135 taken: d.gs(function () { return count; }),
136 open: d(function () {
137 ++debugStats.unknown;
138 ++count;
139 }),
140 close: d(function () {
141 --debugStats.unknown;
142 --count;
143 if (release) release();
144 }),
145 wrap: d(wrap),
146 debugStats: d(debugStats)
147 });