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

Commit

Permalink
feat(node): patch most fs methods (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxhub authored and mhevery committed Sep 12, 2016
1 parent 100b82b commit 4c8a155
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
56 changes: 56 additions & 0 deletions lib/node/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {bindArguments} from '../common/utils';

let fs;
try {
fs = require('fs');
} catch (err) {}

// TODO(alxhub): Patch `watch` and `unwatchFile`.
const TO_PATCH = [
'access',
'appendFile',
'chmod',
'chown',
'close',
'exists',
'fchmod',
'fchown',
'fdatasync',
'fstat',
'fsync',
'ftruncate',
'futimes',
'lchmod',
'lchown',
'link',
'lstat',
'mkdir',
'mkdtemp',
'open',
'read',
'readdir',
'readFile',
'readlink',
'realpath',
'rename',
'rmdir',
'stat',
'symlink',
'truncate',
'unlink',
'utimes',
'write',
'writeFile',
];

if (fs) {
TO_PATCH
.filter(name => !!fs[name] && typeof fs[name] === 'function')
.forEach(name => {
fs[name] = ((delegate: Function) => {
return function() {
return delegate.apply(this, bindArguments(<any>arguments, 'fs.' + name));
};
})(fs[name]);
});
}
1 change: 1 addition & 0 deletions lib/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '../zone';
import {patchTimer} from '../common/timers';

import './events';
import './fs';

const set = 'set';
const clear = 'clear';
Expand Down
13 changes: 13 additions & 0 deletions test/node/fs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {exists} from 'fs';

describe('nodejs file system', () => {
it('has patched exists()', (done) => {
const zoneA = Zone.current.fork({ name: 'A' });
zoneA.run(() => {
exists('testfile', (_) => {
expect(Zone.current.name).toBe(zoneA.name);
done();
});
});
});
});
3 changes: 2 additions & 1 deletion test/node_tests.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './node/events.spec';
import './node/events.spec';
import './node/fs.spec';

0 comments on commit 4c8a155

Please sign in to comment.