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.
@@ -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({ filter: /.*/, namespace: virtualModuleNamespace }, () => ({
80
- contents: createVirtualModule({
81
- routes,
82
- adapter
83
- }),
84
- loader: "js",
85
- resolveDir: functionsRoot
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
- "functions-framework --target=build --source %s",
15
- [source],
16
- {
17
- stdio: "inherit",
18
- ...options,
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-NINUPPZA.js";
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-PFVLROGP.js";
11
+ } from "./chunk-XHFBJ7D2.js";
12
12
  export {
13
13
  build,
14
14
  dev,
@@ -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 };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  build
3
- } from "../chunk-NINUPPZA.js";
3
+ } from "../chunk-L3GFRAQX.js";
4
4
  import "../chunk-MOB3GBJW.js";
5
5
  import "../chunk-SPF3AMMV.js";
6
6
  export {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  preview
3
- } from "../chunk-PFVLROGP.js";
3
+ } from "../chunk-XHFBJ7D2.js";
4
4
  export {
5
5
  preview
6
6
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cloud-run-functions",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.2.1",
5
5
  "bin": "./dist/main.js",
6
6
  "exports": {
7
7
  "types": "./dist/index.d.ts",
package/readme.md CHANGED
@@ -1,33 +1,166 @@
1
1
  # cloud-run-functions
2
2
 
3
- > ⚠️ This project is not ready for production use.
3
+ File-based routing plus local dev and bundling for `@google-cloud/functions-framework`.
4
4
 
5
- Enhances the `@google-cloud/functions-framework` package with "hot reloading" (for local development) and a bundler (for deployment).
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
- pnpm add cloud-run-functions -D
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
- ## Development server
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
- The dev server uses the `@google-cloud/functions-framework` package to run your functions locally. We handle _transpiling_ and _hot reloading_ for you. Just tell the dev server where to find your functions.
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 dev ./path/to/functions/
91
+ npx cloud-run-functions build functions
92
+ npx functions-framework --target=build --source dist/index.js
17
93
  ```
18
94
 
19
- By default, any `.ts` or `.js` module is considered "loadable" by the dev server. This behavior is configurable with a `crf.config.json` file.
95
+ ### preview
20
96
 
21
- The dev server uses filesystem routing. By default, the dev server runs on port 8080. So if you do `http get :8080/hello` from your terminal, the dev server will look for a file called `hello.ts` or `hello.js` in the `./path/to/functions/` directory. If that file exists, its default export will be used as the function handler.
97
+ Preview bundled functions locally.
22
98
 
23
- ## Bundling
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
- When you're ready to deploy, use the `build` command to bundle your functions.
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 build ./path/to/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
- ## Tips
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
- - If you have the [dotenv](https://www.npmjs.com/package/dotenv) package installed, the dev server will import it and automatically load environment variables from the closest `.env` file. Note that environment variables in `.env` won't override existing `process.env` values.
166
+ See `src/index.ts` for the current exports and `src/tools/*.ts` for option types.