UNPKG

3 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2012 Dmitri Melikyan
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to permit
9 * persons to whom the Software is furnished to do so, subject to the
10 * following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25var nt = require('../nodetime');
26var proxy = require('../proxy');
27var samples = require('../samples');
28
29
30var commands = [
31 'rename',
32 'truncate',
33 'chown',
34 'fchown',
35 'lchown',
36 'chmod',
37 'fchmod',
38 'lchmod',
39 'stat',
40 'lstat',
41 'fstat',
42 'link',
43 'symlink',
44 'readlink',
45 'realpath',
46 'unlink',
47 'rmdir',
48 'mkdir',
49 'readdir',
50 'close',
51 'open',
52 'utimes',
53 'futimes',
54 'fsync',
55 'write',
56 'read',
57 'readFile',
58 'writeFile',
59 'appendFile',
60 'exists'
61];
62
63
64module.exports = function(obj) {
65 commands.forEach(function(command) {
66 proxy.before(obj, command, function(obj, args) {
67 var trace = samples.stackTrace();
68 var params = args;
69 var time = samples.time("File System", command);
70
71 proxy.callback(args, -1, function(obj, args) {
72 if(!time.done()) return;
73 if(nt.paused) return;
74
75 var error = args.length > 0 ? (args[0] ? args[0].message : undefined) : undefined;
76 var context = {'Type': 'File System',
77 'Command': command,
78 'Arguments': samples.truncate(params),
79 'Stack trace': trace,
80 'Error': error};
81
82 samples.add(time, context, 'File System: ' + context['Command']);
83 });
84 });
85
86
87 var commandSync = command + 'Sync';
88 proxy.around(obj, commandSync, function(obj, args, locals) {
89 locals.stackTrace = samples.stackTrace();
90 locals.params = args;
91 locals.time = samples.time("File System", commandSync);
92
93 }, function(obj, args, ret, locals) {
94 if(!locals.time.done()) return;
95 if(nt.paused) return;
96
97 var context = {'Type': 'File System',
98 'Command': commandSync,
99 'Arguments': samples.truncate(locals.params),
100 'Stack trace': locals.stackTrace};
101
102 samples.add(locals.time, context, 'File System: ' + context['Command']);
103 });
104 });
105};
106