-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (31 loc) · 1.11 KB
/
index.js
File metadata and controls
38 lines (31 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const { tunnel } = require("cloudflared");
class CloudflareTunnelPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.stop = null;
this.port = this.serverless.service.custom["serverless-offline"].httpPort || 3000;
this.hooks = {
'before:offline:start:init': this.setupTunnel.bind(this),
'before:offline:start:end': this.teardownTunnel.bind(this)
};
}
async setupTunnel() {
const tunnelUrl = await this.createTunnel();
this.serverless.service.provider.environment.CLOUDFLARE_TUNNEL_URL = tunnelUrl;
this.serverless.cli.log(`Cloudflare Tunnel URL set: ${tunnelUrl}`);
}
async teardownTunnel() {
await this.deleteTunnel();
this.serverless.cli.log('Cloudflare Tunnel has been closed.');
}
async createTunnel() {
const { url, stop } = tunnel({ "--url": `http://localhost:${this.port}` });
this.stop = stop;
return await url;
}
async deleteTunnel() {
this.stop();
}
}
module.exports = CloudflareTunnelPlugin;