Skip to content

Commit 7329c9b

Browse files
committed
Added a few more options to the Ngrok script.
1 parent 734aefb commit 7329c9b

File tree

9 files changed

+186
-2503
lines changed

9 files changed

+186
-2503
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,6 @@ app.zip
146146
# SSL files
147147
*.key
148148
*.crt
149+
150+
# Downloaded binary files
151+
.bin

.idea/dictionaries/neonexusdemortis.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/Start_Ngrok__nobuild_.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
# Changelog
2+
## [v4.2.1](https://github.com/neonexus/sails-react-bootstrap-webpack/compare/v4.2.0...v4.2.1) (2023-04-23)
3+
### Features
4+
5+
* Removed optional dependencies from `package.json`, as the setup would remove optional dependencies when installing new packages without use of `--include=optional`...
6+
* Changed use of `NGROK_URL` -> `BASE_URL`.
7+
* Renamed `NGROK_AUTH` -> `NGROK_TOKEN` to avoid confusion with the `ngrok.js` config option `auth`.
8+
* Added some Ngrok config options.
9+
* Updated dependencies.
210

311
## [v4.2.0](https://github.com/neonexus/sails-react-bootstrap-webpack/compare/v4.1.1...v4.2.0) (2023-04-20)
412
### Features

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Gitter: [![Join the chat at https://gitter.im/sails-react-bootstrap-webpack/comm
1414
* [Main Features](#main-features)
1515
* [Branch Warning](#branch-warning)
1616
* [Current Dependencies](#current-dependencies)
17-
* [Optional Dependencies](#optional-dependencies)
1817
* [How to Use](#how-to-use)
1918
* [Configuration](#configuration)
2019
* [Custom Configuration Options](#custom-configuration-options)
@@ -72,12 +71,6 @@ the [`releases section`](https://github.com/neonexus/sails-react-bootstrap-webpa
7271
* [React-Bootstrap](https://react-bootstrap.github.io) **v2**
7372
* [Webpack](https://webpack.js.org) **v5**
7473

75-
### Optional Dependencies
76-
77-
Optional dependencies are not automatically installed in this repo. You must either manually install them with `npm i`, or you can install all optional dependencies: `npm i --include=optional`.
78-
79-
There are currently 2 optional dependencies in this repo: [`ngrok`](#working-with-ngrok) and [`sails-hook-autoreload`](#support-for-sails-hook-autoreload).
80-
8174
See the [`package.json` for more details](package.json).
8275

8376
## How to Use
@@ -282,7 +275,7 @@ and [`config/env/production.js`](config/env/production.js)
282275
| `DB_PORT` | 3306 | The port number for the datastore. |
283276
| `DB_SSL` | true | If the datastore requires SSL, set this to "true". |
284277
| `SESSION_SECRET` | "" (empty string) | Used to sign cookies, and SHOULD be set, especially on PRODUCTION environments. |
285-
| `NGROK_AUTH` | "" (empty string) | Ngrok auth token used in the [`ngrok.js`](#working-with-ngrok) script. |
278+
| `NGROK_TOKEN` | "" (empty string) | Ngrok auth token used in the [`ngrok.js`](#working-with-ngrok) script. |
286279

287280
[//]: # (| DATA_ENCRYPTION_KEY | "" (empty string) | **DATA_ENCRYPTION_KEY** | **"" (empty string)** | **Currently unused; intended for future use.** |)
288281

@@ -368,26 +361,29 @@ This repo has a custom script ([`ngrok.js`](ngrok.js)), which will start an Ngro
368361

369362
You will want to get an auth token (and create an account if you haven't already): https://dashboard.ngrok.com/tunnels/authtokens
370363

371-
You will need to `npm i ngrok --save-dev` (or install optional dependencies) before you can do anything. I've opted to not have it pre-installed, as it does add a bit of bloat, and not everyone is going to use it.
364+
You will need to `npm i ngrok --save-dev` before you can do anything. I've opted to not have it pre-installed, as it does add a bit of bloat, and not everyone is going to use it.
372365

373-
After you have it installed, you can run `ngrok.js`, like this: `node ngrok`.
366+
After you have it installed, you can run `ngrok.js`, with node: `node ngrok` or just directly: `./ngrok.js`.
374367

375368
### Script Options
376369

377-
These are the current configuration flags. Order does not matter. There will likely be more in the future.
370+
These are the current configuration flags. Order does not matter.
378371

379-
| Option | Description |
380-
|-------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
381-
| `nobuild` | Adding this flag will disable asset building. |
382-
| `auth=TOKEN` | Adding this flag (replacing TOKEN with your actual token) will set your Ngrok auth token. In most cases, ngrok will automatically save this token in your home folder, and re-use it later. You can test this out by omitting your token on next run, and go to your [Ngrok dashboard](https://dashboard.ngrok.com/tunnels/agents). |
372+
| Option | Description |
373+
|-----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
374+
| `auth=USER:PASS` | This will protect the Ngrok tunnel with HTTP Basic Auth, using the USER / PASS you supply. |
375+
| `nobuild` | Adding this flag will disable asset building. |
376+
| `domain=MYDOMAIN` | The domain to connect the tunnel from Sails to. |
377+
| `region=MYREGION` | The region to use for connection to the Ngrok services. One of Ngrok regions (`us`, `eu`, `au`, `ap`, `sa`, `jp`, `in`). Defaults to `us`. |
378+
| `token=MY_AUTH_TOKEN` | Adding this flag will set your Ngrok auth token. In most cases, ngrok will automatically save this token in your home folder, and re-use it later. You can test this out by omitting your token on next run, and go to your [Ngrok dashboard](https://dashboard.ngrok.com/tunnels/agents). |
383379

384-
You can also use the environment variable `NGROK_AUTH` to pass your auth token.
380+
You can also use the environment variable `NGROK_TOKEN` to pass your auth token. If both environment variable, and script flag are set, the script flag will take priority.
385381

386-
Full example: `node ngrok nobuild auth=S1T2A3Y4I5N6G7A8L9I0V1E`
382+
An example: `node ngrok nobuild token=S1T2A3Y4I5N6G7A8L9I0V1E region=us`
387383

388384
## Support for `sails-hook-autoreload`
389385

390-
If you would like to use [`sails-hook-autoreload`](https://npmjs.com/package/sails-hook-autoreload), just install it: `npm i sails-hook-autoreload --save-dev` (or install optional dependencies). The config file [`config/autoreload.js`](config/autoreload.js) is already pre-configured for this repo.
386+
If you would like to use [`sails-hook-autoreload`](https://npmjs.com/package/sails-hook-autoreload), just install it: `npm i sails-hook-autoreload --save-dev`. The config file [`config/autoreload.js`](config/autoreload.js) is already pre-configured for this repo.
391387

392388
## What about SEO?
393389

ngrok.js

100644100755
Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
#!/usr/bin/env node
2+
13
/**
24
* This is the file used to lift Sails, build assets, and start Ngrok.
5+
*
6+
* Run it via node: `node ngrok` OR `node ngrok.js`
7+
* Run it directly: `./ngrok.js`
38
*/
49
let ngrok;
510

611
try {
712
ngrok = require('ngrok');
813
} catch (e) { // eslint-disable-line
914
console.log('');
10-
console.log('In order to run this script, you must either install `Ngrok`:');
11-
console.log('npm i ngrok --save-dev');
15+
console.log('In order to run this script, you must install `Ngrok`:');
1216
console.log('');
13-
console.log('Or install all the optional dependencies (will also install sails-hook-autoreload):');
14-
console.log('npm i --include=optional');
17+
console.log('npm i ngrok --save-dev');
1518
console.log('');
1619

1720
return process.exit(1);
@@ -67,23 +70,37 @@ try {
6770
}
6871

6972
// Set our defaults.
70-
config.authtoken = process.env.NGROK_AUTH || null;
73+
config.auth = null;
74+
config.token = process.env.NGROK_TOKEN || null;
7175
config.buildAssets = true;
76+
config.domain = null;
7277
config.port = process.env.PORT || config.port || 1337;
78+
config.region = null;
7379

7480
// Read our console config flags.
7581
for (let i = 2; i < process.argv.length; ++i) {
76-
if (process.argv[i].toLowerCase() === 'nobuild') {
82+
const thisFlag = process.argv[i].toLowerCase();
83+
84+
if (thisFlag === 'nobuild') {
7785
config.buildAssets = false;
78-
} else if (process.argv[i].toLowerCase().startsWith('auth=')) {
79-
config.authtoken = process.argv[i].substring(5);
86+
} else if (thisFlag.startsWith('auth=')) {
87+
config.auth = process.argv[i].substring(5);
88+
} else if (thisFlag.startsWith('domain=')) {
89+
config.domain = thisFlag.substring(7);
90+
} else if (thisFlag.startsWith('region=')) {
91+
config.region = thisFlag.substring(7);
92+
} else if (thisFlag.startsWith('token=')) {
93+
config.token = process.argv[i].substring(6);
8094
}
8195
}
8296

8397
ngrok.connect({
84-
authtoken: config.authtoken,
98+
auth: config.auth, // HTTP Basic auth
99+
authtoken: config.token,
85100
addr: config.port,
86-
scheme: 'https' // Currently, this does nothing, but will force Ngrok to open ONLY an HTTPs tunnel, when v5 of `ngrok` package is released.
101+
subdomain: config.domain,
102+
region: config.region,
103+
scheme: 'https' // Currently, this does nothing, but will force Ngrok to open ONLY an HTTPS tunnel, when v5 of `ngrok` package is released.
87104
}).then((ngrokUrl) => {
88105
// Close our "http" tunnel (leaving our "https" tunnel open). Hacky, but required until v5.
89106
const ngrokApi = ngrok.getApi();
@@ -97,16 +114,17 @@ ngrok.connect({
97114

98115
let origins;
99116

117+
// Smaller helper function, to output the Ngrok URLs.
100118
function sendUrls() {
101119
console.log('');
102120
console.log('Ngrok URL: ' + ngrokUrl);
103121
console.log('Ngrok Dashboard: http://localhost:4040');
104122
console.log('');
105123
}
106124

125+
// Build our assets in the background.
107126
if (config.buildAssets) {
108-
// Build our assets in the background.
109-
const assetBuilderProcess = spawn('npm', ['run', 'build'], {env: {...process.env, NGROK_URL: ngrokUrl}});
127+
const assetBuilderProcess = spawn('npm', ['run', 'build'], {env: {...process.env, BASE_URL: ngrokUrl}});
110128

111129
assetBuilderProcess.stderr.on('data', (data) => {
112130
console.log('Error:');
@@ -129,12 +147,14 @@ ngrok.connect({
129147
console.log('');
130148
}
131149

150+
// Add the Ngrok URL to our allowed origins.
132151
if (config.security && config.security.cors && config.security.cors.allowOrigins) {
133152
origins = [...config.security.cors.allowOrigins, ngrokUrl];
134153
} else {
135154
origins = [ngrokUrl];
136155
}
137156

157+
// Start Sails.
138158
sails.lift({
139159
...rc('sails'),
140160
baseUrl: ngrokUrl,
@@ -150,6 +170,8 @@ ngrok.connect({
150170
return process.exit(1);
151171
}
152172

173+
// Sails tends to be faster at startup than asset building...
174+
// Assume we are still waiting for assets (which will output URLs when finished)...
153175
if (config.buildAssets) {
154176
console.log('');
155177
console.log('Please wait for assets...');

0 commit comments

Comments
 (0)