Skip to content

Commit

Permalink
fs/promises
Browse files Browse the repository at this point in the history
  • Loading branch information
SH committed May 22, 2024
1 parent c1ef376 commit e935adb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions endpoint.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// Socket address parser/formatter and server binding helper

const fs = require('node:fs');
const fs = require('node:fs/promises');
const sockaddr = require('sockaddr');

module.exports = function endpoint (addr, defaultPort) {
Expand Down Expand Up @@ -52,16 +52,16 @@ class Endpoint {
const mode = this.mode ? parseInt(this.mode, 8) : false;
if (this.path) {
opts.path = this.path;
if (fs.existsSync(opts.path)) fs.unlinkSync(opts.path);
if (await fs.exists(opts.path)) await fs.unlink(opts.path);
} else {
opts.host = this.host;
opts.port = this.port;
}

return await new Promise((resolve, reject) => {
server.listen(opts, (err) => {
return new Promise((resolve, reject) => {
server.listen(opts, async (err) => {
if(err) return reject(err);
if (mode) fs.chmodSync(opts.path, mode);
if (mode) await fs.chmod(opts.path, mode);
resolve()
});
});
Expand Down
26 changes: 13 additions & 13 deletions test/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ describe('endpoint', () => {
}

this.mockfs = {
existsSync (path, ...args) {
log.push(['existsSync', path, ...args]);
exists (path, ...args) {
log.push(['exists', path, ...args]);
return ('undefined' !== typeof modes[path]);
},
chmodSync (path, mode, ...args) {
log.push(['chmodSync', path, mode, ...args]);
chmod (path, mode, ...args) {
log.push(['chmod', path, mode, ...args]);
modes[path] = mode;
},
unlinkSync (path, ...args) {
log.push(['unlinkSync', path, ...args]);
unlink (path, ...args) {
log.push(['unlink', path, ...args]);
if ('undefined' !== typeof modes[path]) {
delete modes[path];
}
Expand All @@ -64,13 +64,13 @@ describe('endpoint', () => {
},
};

mock('node:fs', this.mockfs);
mock('node:fs/promises', this.mockfs);
this.endpoint = mock.reRequire('../endpoint');
done();
})

afterEach((done) => {
mock.stop('node:fs');
mock.stop('node:fs/promises');
done();
})

Expand All @@ -86,7 +86,7 @@ describe('endpoint', () => {
await this.endpoint('/foo/bar.sock').bind(this.server, {readableAll:true});
assert.deepEqual(
this.log, [
['existsSync', '/foo/bar.sock'],
['exists', '/foo/bar.sock'],
['listen', {path: '/foo/bar.sock', readableAll: true}],
]);
})
Expand All @@ -96,8 +96,8 @@ describe('endpoint', () => {
await this.endpoint('/foo/bar.sock').bind(this.server);
assert.deepEqual(
this.log, [
['existsSync', '/foo/bar.sock'],
['unlinkSync', '/foo/bar.sock'],
['exists', '/foo/bar.sock'],
['unlink', '/foo/bar.sock'],
['listen', {path: '/foo/bar.sock'}],
]);
})
Expand All @@ -106,9 +106,9 @@ describe('endpoint', () => {
await this.endpoint('/foo/bar.sock:764').bind(this.server);
assert.deepEqual(
this.log, [
['existsSync', '/foo/bar.sock'],
['exists', '/foo/bar.sock'],
['listen', {path: '/foo/bar.sock'}],
['chmodSync', '/foo/bar.sock', 0o764],
['chmod', '/foo/bar.sock', 0o764],
]);
})
})
Expand Down

0 comments on commit e935adb

Please sign in to comment.