This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support listening on unix domain sockets
This commit refactors the logic for parsing the http2 server's listener options. Fixes: grpc/grpc-node#258
- Loading branch information
Showing
4 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
const { isAbsolute, resolve } = require('path'); | ||
const { URL } = require('url'); | ||
|
||
|
||
function resolveToListenOptions (target, secure) { | ||
if (target.startsWith('unix:')) { | ||
if (target.startsWith('unix://')) { | ||
const path = target.substring(7); | ||
|
||
// The path following 'unix://' must be absolute. | ||
if (!isAbsolute(path)) { | ||
throw new Error(`'${target}' must specify an absolute path`); | ||
} | ||
|
||
return { path }; | ||
} | ||
|
||
// The path following 'unix:' can be relative or absolute. | ||
return { path: resolve(target.substring(5)) }; | ||
} | ||
|
||
if (target.startsWith('dns:')) { | ||
target = target.substring(4); | ||
} | ||
|
||
const url = new URL(`http://${target}`); | ||
const defaultPort = secure === true ? 443 : 80; | ||
const port = String(+url.port) === url.port ? +url.port : defaultPort; | ||
|
||
return { host: url.hostname, port }; | ||
} | ||
|
||
|
||
module.exports = { resolveToListenOptions }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
const Assert = require('assert'); | ||
const Path = require('path'); | ||
const Lab = require('@hapi/lab'); | ||
const { resolveToListenOptions } = require('../lib/server-resolver'); | ||
|
||
// Test shortcuts | ||
const { describe, it } = exports.lab = Lab.script(); | ||
|
||
|
||
// Note(cjihrig): As of @grpc/[email protected], the client claims to support Unix | ||
// domain sockets. However, testing the grpc-js client did not seem to work. | ||
// Testing grpcurl with the flags `-plaintext -unix -authority 'localhost'` did | ||
// work for an insecure server. | ||
describe('Server Resolver', () => { | ||
it('resolveToListenOptions() successfully parses inputs', () => { | ||
[ | ||
[ | ||
resolveToListenOptions('dns:127.0.0.1:9999', true), | ||
{ host: '127.0.0.1', port: 9999 } | ||
], | ||
[ | ||
resolveToListenOptions('dns:foo.bar.com:9999', false), | ||
{ host: 'foo.bar.com', port: 9999 } | ||
], | ||
[ | ||
resolveToListenOptions('localhost:8080', true), | ||
{ host: 'localhost', port: 8080 } | ||
], | ||
[ | ||
resolveToListenOptions('localhost:8080', false), | ||
{ host: 'localhost', port: 8080 } | ||
], | ||
[ | ||
resolveToListenOptions('localhost', true), | ||
{ host: 'localhost', port: 443 } | ||
], | ||
[ | ||
resolveToListenOptions('localhost', false), | ||
{ host: 'localhost', port: 80 } | ||
], | ||
[ | ||
resolveToListenOptions('unix:/foo/bar', false), | ||
{ path: '/foo/bar' } | ||
], | ||
[ | ||
resolveToListenOptions('unix:./foo/../baz/bar', false), | ||
{ path: Path.join(process.cwd(), 'baz', 'bar') } | ||
], | ||
[ | ||
resolveToListenOptions('unix:///foo/bar', false), | ||
{ path: '/foo/bar' } | ||
] | ||
].forEach(([actual, expected]) => { | ||
Assert.deepStrictEqual(actual, expected); | ||
}); | ||
}); | ||
|
||
it('resolveToListenOptions() throws if unix:// path is not absolute', () => { | ||
Assert.throws(() => { | ||
resolveToListenOptions('unix://./foo', false); | ||
}, /^Error: 'unix:\/\/\.\/foo' must specify an absolute path$/); | ||
}); | ||
}); |