Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 4c8a155

Browse files
alxhubmhevery
authored andcommitted
feat(node): patch most fs methods (#438)
1 parent 100b82b commit 4c8a155

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

Diff for: lib/node/fs.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {bindArguments} from '../common/utils';
2+
3+
let fs;
4+
try {
5+
fs = require('fs');
6+
} catch (err) {}
7+
8+
// TODO(alxhub): Patch `watch` and `unwatchFile`.
9+
const TO_PATCH = [
10+
'access',
11+
'appendFile',
12+
'chmod',
13+
'chown',
14+
'close',
15+
'exists',
16+
'fchmod',
17+
'fchown',
18+
'fdatasync',
19+
'fstat',
20+
'fsync',
21+
'ftruncate',
22+
'futimes',
23+
'lchmod',
24+
'lchown',
25+
'link',
26+
'lstat',
27+
'mkdir',
28+
'mkdtemp',
29+
'open',
30+
'read',
31+
'readdir',
32+
'readFile',
33+
'readlink',
34+
'realpath',
35+
'rename',
36+
'rmdir',
37+
'stat',
38+
'symlink',
39+
'truncate',
40+
'unlink',
41+
'utimes',
42+
'write',
43+
'writeFile',
44+
];
45+
46+
if (fs) {
47+
TO_PATCH
48+
.filter(name => !!fs[name] && typeof fs[name] === 'function')
49+
.forEach(name => {
50+
fs[name] = ((delegate: Function) => {
51+
return function() {
52+
return delegate.apply(this, bindArguments(<any>arguments, 'fs.' + name));
53+
};
54+
})(fs[name]);
55+
});
56+
}

Diff for: lib/node/node.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import '../zone';
22
import {patchTimer} from '../common/timers';
33

44
import './events';
5+
import './fs';
56

67
const set = 'set';
78
const clear = 'clear';

Diff for: test/node/fs.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {exists} from 'fs';
2+
3+
describe('nodejs file system', () => {
4+
it('has patched exists()', (done) => {
5+
const zoneA = Zone.current.fork({ name: 'A' });
6+
zoneA.run(() => {
7+
exists('testfile', (_) => {
8+
expect(Zone.current.name).toBe(zoneA.name);
9+
done();
10+
});
11+
});
12+
});
13+
});

Diff for: test/node_tests.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
import './node/events.spec';
1+
import './node/events.spec';
2+
import './node/fs.spec';

0 commit comments

Comments
 (0)