cloud-run-functions 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config.schema.json +2 -5
- package/dist/{chunk-NINUPPZA.js → chunk-L3GFRAQX.js} +13 -10
- package/dist/{chunk-PFVLROGP.js → chunk-XHFBJ7D2.js} +7 -11
- package/dist/index.js +2 -2
- package/dist/tools/build.d.ts +1 -1
- package/dist/tools/build.js +1 -1
- package/dist/tools/preview.js +1 -1
- package/package.json +1 -1
- package/readme.md +146 -13
package/config.schema.json
CHANGED
|
@@ -55,10 +55,7 @@
|
|
|
55
55
|
"oneOf": [
|
|
56
56
|
{
|
|
57
57
|
"description": "The adapter wraps your Cloud Run functions at runtime, allowing you to write them with a platform-agnostic HTTP framework, like Hattip.\n\nSet this to \"node\" to skip using an adapter, in which case, your functions should conform to what @google-cloud/functions-framework expects.\n\n@default \"node\"",
|
|
58
|
-
"enum": [
|
|
59
|
-
"hattip",
|
|
60
|
-
"node"
|
|
61
|
-
]
|
|
58
|
+
"enum": ["hattip", "node"]
|
|
62
59
|
},
|
|
63
60
|
{
|
|
64
61
|
"type": "null"
|
|
@@ -91,4 +88,4 @@
|
|
|
91
88
|
}
|
|
92
89
|
},
|
|
93
90
|
"required": []
|
|
94
|
-
}
|
|
91
|
+
}
|
|
@@ -65,6 +65,7 @@ async function build(root, { define, outdir } = {}) {
|
|
|
65
65
|
logOverride: {
|
|
66
66
|
"empty-glob": "silent"
|
|
67
67
|
},
|
|
68
|
+
external: ["@google-cloud/functions-framework"],
|
|
68
69
|
plugins: [
|
|
69
70
|
{
|
|
70
71
|
name: "crf-virtual-functions",
|
|
@@ -76,18 +77,20 @@ async function build(root, { define, outdir } = {}) {
|
|
|
76
77
|
path: virtualModuleId,
|
|
77
78
|
namespace: virtualModuleNamespace
|
|
78
79
|
}));
|
|
79
|
-
build2.onLoad(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
80
|
+
build2.onLoad(
|
|
81
|
+
{ filter: /.*/, namespace: virtualModuleNamespace },
|
|
82
|
+
() => ({
|
|
83
|
+
contents: createVirtualModule({
|
|
84
|
+
routes,
|
|
85
|
+
adapter
|
|
86
|
+
}),
|
|
87
|
+
loader: "js",
|
|
88
|
+
resolveDir: functionsRoot
|
|
89
|
+
})
|
|
90
|
+
);
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
|
-
]
|
|
90
|
-
external: adapter === "hattip" ? [] : ["@hattip/adapter-node"]
|
|
93
|
+
]
|
|
91
94
|
});
|
|
92
95
|
console.log(
|
|
93
96
|
`[esbuild] Bundled ${routes.length} function${routes.length === 1 ? "" : "s"} into ${outfile}`
|
|
@@ -10,18 +10,14 @@ function preview({ outDir, ...options } = {}) {
|
|
|
10
10
|
const binDir = path.resolve(packageDir, "../node_modules/.bin");
|
|
11
11
|
const sourceDir = path.resolve(process.cwd(), outDir ?? "dist");
|
|
12
12
|
const source = path.join(sourceDir, "index.js");
|
|
13
|
-
return $(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
env: {
|
|
20
|
-
...options.env ?? process.env,
|
|
21
|
-
PATH: `${binDir}:${process.env.PATH}`
|
|
22
|
-
}
|
|
13
|
+
return $("functions-framework --target=build --source %s", [source], {
|
|
14
|
+
stdio: "inherit",
|
|
15
|
+
...options,
|
|
16
|
+
env: {
|
|
17
|
+
...options.env ?? process.env,
|
|
18
|
+
PATH: `${binDir}:${process.env.PATH}`
|
|
23
19
|
}
|
|
24
|
-
);
|
|
20
|
+
});
|
|
25
21
|
}
|
|
26
22
|
|
|
27
23
|
export {
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
build
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-L3GFRAQX.js";
|
|
4
4
|
import "./chunk-MOB3GBJW.js";
|
|
5
5
|
import "./chunk-SPF3AMMV.js";
|
|
6
6
|
import {
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
} from "./chunk-DG37B63B.js";
|
|
9
9
|
import {
|
|
10
10
|
preview
|
|
11
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-XHFBJ7D2.js";
|
|
12
12
|
export {
|
|
13
13
|
build,
|
|
14
14
|
dev,
|
package/dist/tools/build.d.ts
CHANGED
|
@@ -36,11 +36,11 @@ declare function build(root?: string, { define, outdir }?: BundleOptions): Promi
|
|
|
36
36
|
logOverride: {
|
|
37
37
|
'empty-glob': "silent";
|
|
38
38
|
};
|
|
39
|
+
external: string[];
|
|
39
40
|
plugins: {
|
|
40
41
|
name: string;
|
|
41
42
|
setup(build: esbuild.PluginBuild): void;
|
|
42
43
|
}[];
|
|
43
|
-
external: string[];
|
|
44
44
|
}>>;
|
|
45
45
|
|
|
46
46
|
export { type BuildOptions, type BundleOptions, build };
|
package/dist/tools/build.js
CHANGED
package/dist/tools/preview.js
CHANGED
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,33 +1,166 @@
|
|
|
1
1
|
# cloud-run-functions
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
File-based routing plus local dev and bundling for `@google-cloud/functions-framework`.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
You point it at a directory of `.ts` or `.js` files. \
|
|
6
|
+
Each file becomes an HTTP route and its default export is treated as the handler.
|
|
6
7
|
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
`@google-cloud/functions-framework` is required because this package runs it under the hood.
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
pnpm add -D cloud-run-functions @google-cloud/functions-framework
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Optional:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
pnpm add -D dotenv
|
|
20
|
+
pnpm add -D @hattip/adapter-node
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick start
|
|
24
|
+
|
|
25
|
+
Create a function file:
|
|
26
|
+
|
|
27
|
+
`functions/hello.ts`
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
export default (req, res) => {
|
|
31
|
+
res.status(200).send('hello')
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Run the dev server:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
pnpx cloud-run-functions dev functions
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Call it:
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
curl http://localhost:8080/hello
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Routing
|
|
48
|
+
|
|
49
|
+
- A file path becomes a URL path
|
|
50
|
+
- `functions/hello.ts` becomes `/hello`
|
|
51
|
+
- `functions/users/create.ts` becomes `/users/create`
|
|
52
|
+
- Your file should default export a function handler
|
|
53
|
+
- If you set `entrySuffix` to `.task`, then `hello.task.ts` becomes `/hello`
|
|
54
|
+
|
|
55
|
+
## CLI
|
|
56
|
+
|
|
57
|
+
The binary name is `cloud-run-functions`.
|
|
58
|
+
|
|
59
|
+
### dev
|
|
60
|
+
|
|
61
|
+
Start the development server with hot reload.
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
npx cloud-run-functions dev [root]
|
|
7
65
|
```
|
|
8
|
-
|
|
66
|
+
|
|
67
|
+
- `root` is the directory to search for function entrypoints. Default is the current working directory
|
|
68
|
+
- `--port, -p <port>` sets the port. Default is `8080`
|
|
69
|
+
- `--define, -d <key:value>` can be repeated. It passes `define` values to esbuild
|
|
70
|
+
|
|
71
|
+
### build
|
|
72
|
+
|
|
73
|
+
Bundle your functions for deployment.
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
npx cloud-run-functions build [root]
|
|
9
77
|
```
|
|
10
78
|
|
|
11
|
-
|
|
79
|
+
- `root` is the directory to search for function entrypoints. Default is the current working directory
|
|
80
|
+
- `--outdir, -o <dir>` sets the output directory. Default is `dist`
|
|
81
|
+
- `--define, -d <key:value>` can be repeated. It passes `define` values to esbuild
|
|
82
|
+
|
|
83
|
+
Output:
|
|
12
84
|
|
|
13
|
-
|
|
85
|
+
- Writes `index.js` and sourcemaps into `outdir`
|
|
86
|
+
- Run it with the Functions Framework using the `build` target
|
|
87
|
+
|
|
88
|
+
Example:
|
|
14
89
|
|
|
15
90
|
```sh
|
|
16
|
-
npx cloud-run-functions
|
|
91
|
+
npx cloud-run-functions build functions
|
|
92
|
+
npx functions-framework --target=build --source dist/index.js
|
|
17
93
|
```
|
|
18
94
|
|
|
19
|
-
|
|
95
|
+
### preview
|
|
20
96
|
|
|
21
|
-
|
|
97
|
+
Preview bundled functions locally.
|
|
22
98
|
|
|
23
|
-
|
|
99
|
+
```sh
|
|
100
|
+
npx cloud-run-functions preview [--outDir <dir>]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
- `--outDir, -o <dir>` sets the directory containing the bundled output. Default is `dist`
|
|
104
|
+
|
|
105
|
+
This runs the Functions Framework using the `build` target and your bundled `index.js`.
|
|
106
|
+
|
|
107
|
+
## Define values
|
|
24
108
|
|
|
25
|
-
|
|
109
|
+
The CLI format is `--define key:value` or `-d key:value`.
|
|
110
|
+
|
|
111
|
+
- If `value` looks like a number, it is used as one
|
|
112
|
+
- Otherwise it is treated as a string literal
|
|
113
|
+
|
|
114
|
+
Examples:
|
|
26
115
|
|
|
27
116
|
```sh
|
|
28
|
-
npx cloud-run-functions
|
|
117
|
+
npx cloud-run-functions dev functions -d process.env.STAGE:dev
|
|
118
|
+
npx cloud-run-functions build functions -d __BUILD_ID__:123
|
|
29
119
|
```
|
|
30
120
|
|
|
31
|
-
|
|
121
|
+
If you need full control over esbuild `define` values, use the programmatic API.
|
|
122
|
+
|
|
123
|
+
## Configuration
|
|
124
|
+
|
|
125
|
+
Create a `crf.config.json` file. \
|
|
126
|
+
It is searched for by walking up from the `root` directory you pass to the CLI.
|
|
127
|
+
|
|
128
|
+
Example:
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"root": "functions",
|
|
133
|
+
"entrySuffix": ".task",
|
|
134
|
+
"adapter": "node",
|
|
135
|
+
"maxInstanceConcurrency": 5
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Options:
|
|
140
|
+
|
|
141
|
+
- `root` string. base directory when searching for entry points. default is the config directory
|
|
142
|
+
- `globs` string array. globs to search within `root`. default is `["**/*"]`
|
|
143
|
+
- `extensions` string array. file extensions to match. default is `[".ts", ".js"]`
|
|
144
|
+
- `entrySuffix` string. require a suffix like `.task` before the extension
|
|
145
|
+
- `adapter` `"node"` or `"hattip"`. default is `"node"`
|
|
146
|
+
- `maxInstanceConcurrency` number or record of `{ [routeName]: number }`. default is `5`. used by `dev` to limit concurrent requests per route
|
|
147
|
+
|
|
148
|
+
Adapter notes:
|
|
149
|
+
|
|
150
|
+
- `adapter: "node"` means your default export should be a Functions Framework handler `(req, res) => ...`
|
|
151
|
+
- `adapter: "hattip"` means your default export should be a Hattip app and it will be wrapped at runtime
|
|
152
|
+
|
|
153
|
+
## Dotenv support
|
|
154
|
+
|
|
155
|
+
If `dotenv` is installed, `dev` will load the closest `.env` file under your functions root.
|
|
156
|
+
Values in `.env` do not override existing `process.env` values.
|
|
157
|
+
|
|
158
|
+
## Programmatic API
|
|
159
|
+
|
|
160
|
+
The package also exports the underlying functions used by the CLI:
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
import { build, dev, preview } from 'cloud-run-functions'
|
|
164
|
+
```
|
|
32
165
|
|
|
33
|
-
|
|
166
|
+
See `src/index.ts` for the current exports and `src/tools/*.ts` for option types.
|