Skip to content

Commit

Permalink
feat: add dynamic proxy port if environment variable is not set (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Jul 3, 2023
2 parents 7017e2c + 878a5f2 commit 1784c72
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@opentelemetry/sdk-trace-node": "^1.10.1",
"@opentelemetry/semantic-conventions": "^1.10.1",
"google-protobuf": "3.14.0",
"portfinder": "^1.0.32",
"tslib": "^2.1.0"
},
"license": "Apache-2.0",
Expand Down
19 changes: 16 additions & 3 deletions src/resources/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
// limitations under the License.
import * as faas from '../faas/index';
import { HttpWorkerOptions, http } from '.';

jest.mock('portfinder', () => {
const originalModule = jest.requireActual('portfinder');

return {
__esmodule: true,
...originalModule,
getPort: (callback: (err, port) => void) => {
callback('', 1234);
},
};
});

jest.mock('../faas/index');

describe('HTTP Proxy', () => {
Expand All @@ -36,7 +49,7 @@ describe('HTTP Proxy', () => {

beforeAll(() => {
try {
http(mockApp, 1234);
http(mockApp);
} catch (err) {
error = err;
}
Expand All @@ -55,7 +68,7 @@ describe('HTTP Proxy', () => {
});

beforeAll(async () => {
http(mockApp, 1234, fakeCallback);
http(mockApp, fakeCallback);
});

it('should create a new FaasClient', () => {
Expand All @@ -81,7 +94,7 @@ describe('HTTP Proxy', () => {
});

beforeAll(async () => {
http(fakeFunc, 1234, fakeCallback);
http(fakeFunc, fakeCallback);
});

it('should create a new FaasClient', () => {
Expand Down
20 changes: 19 additions & 1 deletion src/resources/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import portfinder from 'portfinder';
import { Faas } from '../faas';

type ListenerFunction =
Expand Down Expand Up @@ -48,7 +49,6 @@ class HttpWorker {

export const http = (
app: NodeApplication | ListenerFunction,
port: number,
callback?: () => void
) => {
const unknownApp = app as any;
Expand All @@ -58,5 +58,23 @@ export const http = (
? (app as NodeApplication)
: { listen: app as ListenerFunction };

const port = Number.parseInt(process.env.NITRIC_HTTP_PROXY_PORT);

if (Number.isNaN(port)) {
// If port isn't set and the nitric environment is not run or cloud
console.log('NITRIC_HTTP_PROXY_PORT not set. Finding open port...');
portfinder.getPort((err, port) => {
if (err) {
throw new Error(
'Unable to find open port. Try setting the env var `NITRIC_HTTP_PROXY_PORT`'
);
}

new HttpWorker(nodeApp, port, callback);
});

return;
}

new HttpWorker(nodeApp, port, callback);
};
39 changes: 37 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,13 @@ astral-regex@^2.0.0:
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31"
integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==

async@^2.6.4:
version "2.6.4"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.4.tgz#706b7ff6084664cd7eae713f6f965433b5504221"
integrity sha512-mzo5dfJYwAn29PeiJ0zvwTo04zj8HDJj0Mn8TD7sno7q12prdbnasKJHhkm2c1LgrhlJ0teaea8860oxi51mGA==
dependencies:
lodash "^4.17.14"

async@^3.1.0:
version "3.2.4"
resolved "https://registry.yarnpkg.com/async/-/async-3.2.4.tgz#2d22e00f8cddeb5fde5dd33522b56d1cf569a81c"
Expand Down Expand Up @@ -2173,6 +2180,13 @@ debug@^2.2.0, debug@^2.3.3:
dependencies:
ms "2.0.0"

debug@^3.2.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a"
integrity sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==
dependencies:
ms "^2.1.1"

debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
Expand Down Expand Up @@ -4473,7 +4487,7 @@ lodash.truncate@^4.4.2:
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
integrity sha512-jttmRe7bRse52OsWIMDLaXxWqRAmtIUccAQ3garviCqJjafXOfNMO0yMfNpdD6zbGaTU0P5Nz7e7gAT6cKmJRw==

[email protected], lodash@^4.17.15, lodash@^4.7.0:
[email protected], lodash@^4.17.14, lodash@^4.17.15, lodash@^4.7.0:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
Expand Down Expand Up @@ -4653,7 +4667,7 @@ [email protected]:
is-plain-obj "^1.1.0"
kind-of "^6.0.3"

minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5:
minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
Expand Down Expand Up @@ -4691,6 +4705,13 @@ [email protected], mkdirp@^1.0.3:
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

mkdirp@^0.5.6:
version "0.5.6"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.6.tgz#7def03d2432dcae4ba1d611445c48396062255f6"
integrity sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==
dependencies:
minimist "^1.2.6"

modify-values@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand All @@ -4711,6 +4732,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

ms@^2.1.1:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

mz@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32"
Expand Down Expand Up @@ -5152,6 +5178,15 @@ please-upgrade-node@^3.2.0:
dependencies:
semver-compare "^1.0.0"

portfinder@^1.0.32:
version "1.0.32"
resolved "https://registry.yarnpkg.com/portfinder/-/portfinder-1.0.32.tgz#2fe1b9e58389712429dc2bea5beb2146146c7f81"
integrity sha512-on2ZJVVDXRADWE6jnQaX0ioEylzgBpQk8r55NE4wjXW1ZxO+BgDlY6DXwj20i0V8eB4SenDQ00WEaxfiIQPcxg==
dependencies:
async "^2.6.4"
debug "^3.2.7"
mkdirp "^0.5.6"

posix-character-classes@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
Expand Down

0 comments on commit 1784c72

Please sign in to comment.