Skip to content

Commit b010344

Browse files
author
tcl89
committed
implement --url flag (default is no url)
1 parent a5e4d93 commit b010344

File tree

5 files changed

+64
-26
lines changed

5 files changed

+64
-26
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
![Hyperform Banner](https://github.com/qngapparat/hyperform/blob/master/hyperform-banner.png)
22

33

4-
<p align="center">Deploys JavaScript functions as serverless functions
4+
<p align="center">A next-gen serverless deployer
55
<br>For AWS Lambda & Google Cloud Functions</p>
66

77
## Install
@@ -13,6 +13,11 @@ $ npm install -g hyperform-cli
1313

1414
## Usage
1515

16+
```
17+
$ hf init
18+
$ hf deploy [--url]
19+
```
20+
1621
### ✍️ Write normal JavaScript
1722

1823

@@ -47,12 +52,15 @@ $ hf init
4752
### 🚀 Deploy exports as functions
4853

4954
```sh
50-
$ hf deploy
55+
$ hf deploy --url
5156

5257
🟢 endpointGreet https://ltirihayh9.execute-api.us-east-2.amazonaws.com/endpointGreet
5358
# or
5459
🟢 endpointGreet https://us-central1-myproject.cloudfunctions.net/endpointGreet
5560
```
61+
62+
If you don't want to make your functions public, omit the `--url` flag.
63+
5664
<!--
5765
## Invoke
5866

cli.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ const { maybeShowSurvey, answerSurvey } = require('./surveyor/index')
1010

1111
const args = process.argv.slice(2)
1212

13-
if ((/init|deploy/.test(args[0]) === false) || (args.length > 1)) {
13+
if ((/init|deploy/.test(args[0]) === false) || (args.length === 2 && args[1] !== '--url') || args.length > 2) {
1414
log(`Usage:
1515
$ hf init
16-
$ hf deploy
16+
$ hf deploy [--url]
1717
`)
1818
process.exit(1)
1919
}
2020

2121
const mode = args[0]
22+
const isPublic = (args[1] === '--url')
2223
// $ hf should always be invoked in the desired directory
2324
const absdir = process.cwd()
2425

@@ -76,7 +77,7 @@ try {
7677
// Do not import earlier, it needs to absorb process.env set above
7778
// TODO: make less sloppy
7879
const { main } = require('./index')
79-
main(absdir, fnregex, parsedHyperformJson)
80+
main(absdir, fnregex, parsedHyperformJson, isPublic)
8081
// show anonymous survey question with 1/30 percent probability
8182
.then(() => maybeShowSurvey())
8283
} catch (e) {

deployer/google/index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ async function _allowPublicInvokeGoogle(options) {
186186
* it creates a new GCF with given code ("pathToZip") and "options".
187187
* If GCF exists already, it updates its code with "pathToZip".
188188
* If other options are specified, it can update those too (currently only "runtime").
189-
* Returns URL immediately, but Cloud Function takes another 1-2 minutes to be invokable.
189+
* Returns IAM-protected URL immediately, but Cloud Function takes another 1-2 minutes to be invokable.
190+
* To remove IAM protection to allow anyone with the URL to call it, use publishGoogle afterwards.
190191
* @param {string} pathToZip
191192
* @param {{
192193
* name: string,
@@ -236,23 +237,33 @@ async function deployGoogle(pathToZip, options) {
236237
}
237238
await _updateGoogle(signedUploadUrl, updateParams)
238239
}
239-
240-
// allow anyone to invoke function
241-
const allowPublicInvokeOptions = {
242-
name: options.name,
243-
project: options.project,
244-
region: options.region,
245-
}
246-
await _allowPublicInvokeGoogle(allowPublicInvokeOptions)
247-
240+
248241
// Construct endpoint URL (it's deterministic)
249242
const endpointUrl = `https://${options.region}-${options.project}.cloudfunctions.net/${options.name}`
250243

251244
// Note: GCF likely not ready by the time we return its URL here
252245
return endpointUrl
253246
}
254247

248+
/**
249+
* @description Allows anyone to call function via its HTTP endpoint.
250+
* Does so by turning IAM checking of Google off.
251+
* Unlike publishAmazon, publishgoogle it does not return an URL, deployGoogle does that already.
252+
* @param {*} name
253+
* @param {*} project
254+
* @param {*} region
255+
*/
256+
async function publishGoogle(name, project, region) {
257+
const allowPublicInvokeOptions = {
258+
name: name,
259+
project: project,
260+
region: region,
261+
}
262+
await _allowPublicInvokeGoogle(allowPublicInvokeOptions)
263+
}
264+
255265
module.exports = {
256266
deployGoogle,
267+
publishGoogle,
257268
_only_for_testing_isExistsGoogle: isExistsGoogle,
258269
}

index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { deployAmazon } = require('./deployer/amazon/index')
88
const { publishAmazon } = require('./publisher/amazon/index')
99
const { spinnies, log, logdev } = require('./printers/index')
1010
const { zip } = require('./zipper/index')
11-
const { deployGoogle } = require('./deployer/google/index')
11+
const { deployGoogle, publishGoogle } = require('./deployer/google/index')
1212
const { transpile } = require('./transpiler/index')
1313
const schema = require('./schemas/index').hyperformJsonSchema
1414

@@ -73,9 +73,10 @@ async function bundleTranspileZipGoogle(fpath) {
7373
* @param {string} name
7474
* @param {string} region
7575
* @param {string} zipPath
76-
* @returns {string} URL of the endpoint of the Lambda
76+
* @param {boolean} isPublic whether to publish
77+
* @returns {string?} If isPublic was true, URL of the endpoint of the Lambda
7778
*/
78-
async function deployPublishAmazon(name, region, zipPath) {
79+
async function deployPublishAmazon(name, region, zipPath, isPublic) {
7980
const amazonSpinnieName = `amazon-main-${name}`
8081
try {
8182
spinnies.add(amazonSpinnieName, { text: `Deploying ${name}` })
@@ -86,11 +87,14 @@ async function deployPublishAmazon(name, region, zipPath) {
8687
region: region,
8788
}
8889
const amazonArn = await deployAmazon(zipPath, amazonDeployOptions)
89-
// Publish it
90-
const amazonUrl = await publishAmazon(amazonArn, region)
90+
let amazonUrl
91+
// Publish it if isPpublic
92+
if (isPublic === true) {
93+
amazonUrl = await publishAmazon(amazonArn, region)
94+
}
9195
spinnies.succ(amazonSpinnieName, { text: `🟢 ${name} ${chalk.rgb(255, 255, 255).bgWhite(amazonUrl)}` })
9296

93-
// return url
97+
// (return url)
9498
return amazonUrl
9599
} catch (e) {
96100
spinnies.f(amazonSpinnieName, {
@@ -107,9 +111,10 @@ async function deployPublishAmazon(name, region, zipPath) {
107111
* @param {string} region
108112
* @param {string} project
109113
* @param {string} zipPath
110-
* @returns {string} URL of the Google Cloud Function
114+
* @param {boolean} isPublic whether to publish
115+
* @returns {string?} If isPublic was true, URL of the Google Cloud Function
111116
*/
112-
async function deployPublishGoogle(name, region, project, zipPath) {
117+
async function deployPublishGoogle(name, region, project, zipPath, isPublic) {
113118
const googleSpinnieName = `google-main-${name}`
114119
try {
115120
spinnies.add(googleSpinnieName, { text: `Deploying ${name}` })
@@ -120,6 +125,11 @@ async function deployPublishGoogle(name, region, project, zipPath) {
120125
runtime: 'nodejs12',
121126
}
122127
const googleUrl = await deployGoogle(zipPath, googleOptions)
128+
129+
if (isPublic === true) {
130+
// enables anyone with the URL to call the function
131+
await publishGoogle(name, project, region)
132+
}
123133
spinnies.succ(googleSpinnieName, { text: `🟢 ${name} ${chalk.rgb(255, 255, 255).bgWhite(googleUrl)}` })
124134
console.log('Google takes another 1 - 2m for changes to take effect')
125135

@@ -138,9 +148,10 @@ async function deployPublishGoogle(name, region, project, zipPath) {
138148
* @param {string} dir
139149
* @param {Regex} fnregex
140150
* @param {*} parsedHyperformJson
151+
* @param {boolean} isPublic Controls whether (Amazon) to create URL endpoint and (Google) whether to remove IAM protection on the URL
141152
* @returns {{ urls: string[] }} urls: Mixed, nested Array of endpoint URLs.
142153
*/
143-
async function main(dir, fnregex, parsedHyperformJson) {
154+
async function main(dir, fnregex, parsedHyperformJson, isPublic) {
144155
const infos = await getInfos(dir, fnregex)
145156
/*
146157
[
@@ -203,6 +214,7 @@ async function main(dir, fnregex, parsedHyperformJson) {
203214
exp,
204215
parsedHyperformJson.amazon.aws_default_region,
205216
amazonZipPath,
217+
isPublic,
206218
)
207219
}
208220

@@ -216,6 +228,7 @@ async function main(dir, fnregex, parsedHyperformJson) {
216228
'us-central1',
217229
'firstnodefunc',
218230
googleZipPath,
231+
isPublic,
219232
)
220233
}
221234

index.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ describe('System tests (takes 1-2 minutes)', () => {
7373
},
7474

7575
}
76+
77+
const isPublic = true
7678

7779
let err
7880
try {
79-
amazonMainRes = await main(dir, fnregex, amazonParsedHyperformJson)
81+
amazonMainRes = await main(dir, fnregex, amazonParsedHyperformJson, isPublic)
8082
} catch (e) {
8183
console.log(e)
8284
err = e
@@ -102,10 +104,13 @@ describe('System tests (takes 1-2 minutes)', () => {
102104
},
103105

104106
}
107+
108+
// to test publishing too
109+
const isPublic = true
105110

106111
let err
107112
try {
108-
googleMainRes = await main(dir, fnregex, googleParsedHyperformJson)
113+
googleMainRes = await main(dir, fnregex, googleParsedHyperformJson, isPublic)
109114
} catch (e) {
110115
console.log(e)
111116
err = e

0 commit comments

Comments
 (0)