Skip to content

Commit

Permalink
support port ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
imcotton committed Nov 23, 2022
1 parent eded36b commit cce2634
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
16 changes: 10 additions & 6 deletions bin.ts
@@ -1,25 +1,29 @@
import { main, safe_int } from './mod.ts'
import { main, safe_int, port_verify, parse_range } from './mod.ts';

import { parse } from 'https://deno.land/std@0.160.0/flags/mod.ts';
import { parse } from 'https://deno.land/std@0.165.0/flags/mod.ts';
import { mapNotNullish } from 'https://deno.land/std@0.165.0/collections/map_not_nullish.ts';





const { port: port_, timeout: timeout_, ...rest } = parse(Deno.args, {
string: [ 'auth', 'crt', 'key' ],
string: [ 'auth', 'crt', 'key', 'port.http', 'port.https' ],
default: {
port: {
http: 0,
https: 0,
http: '',
https: '',
},
timeout: 500, // milliseconds
crt: 'server.crt',
key: 'server.key',
}
});

const port = { http: 0, https: 0, ...port_ ?? {} };
const port = {
http: new Set(mapNotNullish(parse_range(port_.http), port_verify)),
https: new Set(mapNotNullish(parse_range(port_.https), port_verify)),
};

const timeout = safe_int ({ min: 0 }) (Number(timeout_));

Expand Down
4 changes: 2 additions & 2 deletions mod.test.ts
Expand Up @@ -497,7 +497,7 @@ Deno.test('main', { permissions: { net: true, read: [ '.' ] } }, async () => {

const signal = AbortSignal.abort();

await main({ port: { http: 65535 } }, { signal });
await main({ port: { http: [ 65535 ] } }, { signal });

}

Expand Down Expand Up @@ -579,7 +579,7 @@ Deno.test('main', { permissions: { net: true, read: [ '.' ] } }, async () => {
ast.assertExists(port_echo);
ast.assertExists(port_proxy);

main({ port: { http: port_proxy } }, { signal });
main({ port: { http: [ port_proxy ] } }, { signal });

await delay(10);

Expand Down
33 changes: 24 additions & 9 deletions mod.ts
Expand Up @@ -16,32 +16,47 @@ import {



export type Opts = Partial<{
export type MainOpts = Partial<{
auth: string,
port: Partial<{
http: number,
https: number,
http: Iterable<number>,
https: Iterable<number>,
}>,
timeout: number,
crt: string,
key: string,
}>

type OptsWithoutPort = Omit<MainOpts, 'port'>;

export type Opts = OptsWithoutPort & Partial<{
port: Partial<{
http: number,
https: number,
}>,
}>;





export async function main (
opts: Opts,
{ port, ...opts }: MainOpts,
{ signal } = new AbortController() as { readonly signal: AbortSignal },
) {

const handle = await on_request(opts);

const services = await Promise.allSettled([
serve_http(opts),
serve_https(opts),
]);
const services = await Promise.allSettled(

Array.from(port?.http ?? [])
.map(http => serve_http({ ...opts, port: { http } }))

.concat(Array.from(port?.https ?? [])
.map(https => serve_https({ ...opts, port: { https } }))
)

);

if (services.every(settling.rejected)) {

Expand Down Expand Up @@ -147,7 +162,7 @@ export function pre_on_request({
tunnel = tunnel_to,
}) {

return async function ({ auth, timeout }: Opts) {
return async function ({ auth, timeout }: OptsWithoutPort) {

const check = auth && await verify(toFileUrl(toAbsolute(auth)));

Expand Down

0 comments on commit cce2634

Please sign in to comment.