Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'ngrok' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
function ngrokify (server) {
if (typeof server === 'function') server = http.createServer(server)
// let ngroked = Object.create(server)
let ngrokURL
server.once('close', function () {
console.log('Closing time :)')
ngrokURL && ngrok.disconnect(ngrokURL)
})
let addr = server.address()
if (!addr) server.listen(0)
addr = server.address()
console.log('Using Port ' + addr.port)
return ngrok.connect(addr.port).then(function (url) {
ngrokURL = url
return url
})
}
module.exports = ngrokify
value: _asyncToGenerator(function* () {
var _this = this;
if (this._ngrokUrl) {
console.log("Waiting for ngrok to disconnect...");
yield this._stopNgrokAsync();
console.log("Disconnected ngrok; restarting...");
}
this.ngrokReady$ = new _Promise(function (fulfill, reject) {
_this._ngrokReadyFulfill = fulfill;
_this._ngrokReadyReject = reject;
});
this.emit('ngrok-will-start', this.opts.port);
this.ngrokReady$ = ngrok.promise.connect(this.opts.port);
// this._setCombinedPromises();
this._ngrokUrl = yield this.ngrokReady$;
this.emit('ngrok-did-start', this.opts.port, this._ngrokUrl);
this.emit('ngrok-ready', this.opts.port, this._ngrokUrl);
console.log("Connected ngrok to port " + this.opts.port + " via " + this._ngrokUrl);
return this._ngrokUrl;
})
}, {
async _stopNgrokAsync() {
if (this._ngrokUrl) {
this.emit('ngrok-will-disconnect', this._ngrokUrl);
try {
await ngrok.promise.disconnect(this._ngrokUrl);
let oldNgrokUrl = this._ngrokUrl;
this._ngrokUrl = null;
// this._ngrokDisconnectedFulfill(oldNgrokUrl);
// console.log("Disconnected ngrok");
this.emit('ngrok-disconnected', oldNgrokUrl);
} catch (e) {
console.error("Problem disconnecting ngrok:", e);
// this._ngrokDisconnectedReject(e);
this.emit('ngrok-disconnect-err', e);
}
}
}
value: _asyncToGenerator(function* () {
if (this._ngrokUrl) {
this.emit('ngrok-will-disconnect', this._ngrokUrl);
try {
yield ngrok.promise.disconnect(this._ngrokUrl);
var oldNgrokUrl = this._ngrokUrl;
this._ngrokUrl = null;
// this._ngrokDisconnectedFulfill(oldNgrokUrl);
// console.log("Disconnected ngrok");
this.emit('ngrok-disconnected', oldNgrokUrl);
} catch (e) {
console.error("Problem disconnecting ngrok:", e);
// this._ngrokDisconnectedReject(e);
this.emit('ngrok-disconnect-err', e);
}
}
})
}, {
let oldNgrokUrl = this._ngrokUrl;
this._ngrokUrl = null;
// this._ngrokDisconnectedFulfill(oldNgrokUrl);
// console.log("Disconnected ngrok");
this.emit('ngrok-disconnected', oldNgrokUrl);
} catch (e) {
console.error("Problem disconnecting ngrok:", e);
// this._ngrokDisconnectedReject(e);
this.emit('ngrok-disconnect-err', e);
}
}
if (this._packagerNgrokUrl) {
this.emit('packager-ngrok-will-disconnect', this._packagerNgrokUrl);
try {
await ngrok.promise.disconnect(this._packagerNgrokUrl);
let oldNgrokUrl = this._packagerNgrokUrl;
this._packagerNgrokUrl = null;
// this._ngrokDisconnectedFulfill(oldNgrokUrl);
// console.log("Disconnected ngrok");
this.emit('packager-ngrok-disconnected', oldNgrokUrl);
} catch (e) {
console.error("Problem disconnecting packager ngrok:", e);
// this._ngrokDisconnectedReject(e);
this.emit('packager-ngrok-disconnect-err', e);
}
}
}
cp.kill('SIGTERM');
cp = createProcess();
});
const useTypeScript = false; // FIXME
if (useTypeScript) {
// watch js and run cp
// watch ts and compile to dist
} else {
// watch all and run cp
}
if (!isConsole) {
const url = await ngrok.connect(port);
Object.entries(channels || {})
.filter(([, { enabled }]) => enabled)
.forEach(([channel, { path: webhookPath }]) => {
const routePath = webhookPath || `/webhooks/${channel}`;
console.log(`${channel} webhook url: ${url}${routePath}`);
});
}
};
srv.listen(function(){
ngrok(srv.address().port, function(err, url){
if (err) {
console.error('… error setting up reverse tunnel');
console.error(err.stack);
return;
}
console.log('… booting up \u001b[96m'
+ browser + '\u001b[39m (' + (version || 'latest')
+ ') on ' + platform);
spawn(url);
});
// let `error` throw
});
}
console.log("Setting up ngrok", this.argv.ngrokRegion ? `(region: ${this.argv.ngrokRegion})` : "");
}
// 1. start local server on random port
this.ngrokServer = http.createServer(this.ngrokHandler.bind(this));
// turn server.listen() into promise so we can await
const listen = util.promisify( this.ngrokServer.listen.bind(this.ngrokServer) );
await listen(0, '127.0.0.1');
// 2. start ngrok tunnel connected to that port
this.ngrokServerPort = this.ngrokServer.address().port;
// create a unique authorization token that we check on our local instance later
// this adds extra protection on top of the uniquely generated ngrok subdomain (e.g. a01ae275.ngrok.io)
this.ngrokAuth = crypto.randomBytes(32).toString("hex");
const ngrokUrl = await ngrok.connect({
addr: this.ngrokServerPort,
region: this.argv.ngrokRegion
});
// 3. pass on public ngrok url to agent
action.parameters.push({
key: "$ngrokUrl",
value: url.parse(ngrokUrl).host
});
action.parameters.push({
key: "$ngrokAuth",
value: this.ngrokAuth
});
console.log(`Ngrok forwarding: ${ngrokUrl} => http://localhost:${this.ngrokServerPort} (auth: ${this.ngrokAuth})`);
if (this.liveReloadServer.server) {
this.liveReloadServer.close();
} else {
this.liveReloadServer.watcher.close();
}
this.liveReloadServer = null;
});
}
if (this.ngrokServer) {
await this.tryCatch(() => {
this.ngrokServer.close();
this.ngrokServer = null;
});
}
await this.tryCatch(ngrok.kill());
// only log this if we started properly
if (this.ready) {
console.log(`Done`);
}
this.ready = false;
}
async function recreateTunnel(localPort) {
ngrok.disconnect() // disconnect all ngrok service on this computer
const ngrokURL = await createTunnel(localPort)
return ngrokURL
}