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

feat(node): patch most fs methods #438

Merged
merged 1 commit into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';