Skip to content

Commit f1a50e4

Browse files
authored
Docs Updates (#70)
* Added new section in README.md to clarify how to do local testing of event functions. * Updated wording * fix: Fix docs gitignore * docs: Fix and test sample code for Pub/Sub emulation.
1 parent 299f892 commit f1a50e4

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
This directory contains advanced docs around the Functions Framework.
44

5+
- [Testing events and Pub/Sub](events.md)
6+
- [Debugging Functions](debugging.md)
57
- [Running and Deploying Docker Containers](docker.md)
68

79
## TODO Docs

docs/debugging.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Debugging Functions
2+
3+
The Functions Framework works with standard tooling that you might use when writing a function for a Node.js environment. You can attach a debugger to your function by following these steps.
4+
5+
1. Write an `index.js` file containing your Node.js function:
6+
7+
```js
8+
exports.helloWorld = (req, res) => {
9+
res.send('Hello, World');
10+
};
11+
```
12+
13+
2. Install the Functions Framework:
14+
15+
```sh
16+
npm install @google-cloud/functions-framework
17+
```
18+
19+
3. Run `node`, enable the inspector and run the Functions Framework:
20+
21+
```sh
22+
node --inspect node_modules/@google-cloud/functions-framework --target=helloWorld
23+
...
24+
Debugger listening on ws://127.0.0.1:9229/5f57f5e9-ea4b-43ce-be1d-6e9b838ade4a
25+
For help see https://nodejs.org/en/docs/inspector
26+
Serving function...
27+
Function: helloWorld
28+
URL: http://localhost:8080/
29+
```
30+
31+
You can now use an IDE or other tooling to add breakpoints, step through your code and debug your function.

docs/events.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## Local Testing of Cloud Events
2+
3+
The setup for cloud functions that accept events is very similar to the instructions in the quickstart, with the following adjustments:
4+
5+
In your `package.json`, add a signature type (in bold) to your start command:
6+
7+
<pre>
8+
"scripts": {
9+
"start": "functions-framework --target=helloWorld <b>--signature-type=event"</b>
10+
}
11+
</pre>
12+
13+
Upon running ```sh npm start ```, you'll see the function is still being served at `http://localhost:8080/`. However, it is no longer accessible via GET requests from the browser. Instead, send a POST request where the request body conforms to the API defined by [push subscriptions](https://cloud.google.com/pubsub/docs/push).
14+
15+
### Submitting POST Request to Simulate Pub/Sub messages
16+
17+
Create a `mockPubsub.json` file with the following contents:
18+
19+
```json
20+
{
21+
"message": {
22+
"attributes": {
23+
"key": "value"
24+
},
25+
"data": "SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==",
26+
"messageId": "136969346945"
27+
},
28+
"subscription": "projects/myproject/subscriptions/mysubscription"
29+
}
30+
```
31+
32+
The file can be in any folder on your computer. From the terminal, go to the directory where `mockPubsub.json` is located, and run the following command (assuming your cloud function is hosted locally on port 8080):
33+
34+
```sh
35+
curl -d "@mockPubsub.json" \
36+
-X POST \
37+
-H "Ce-Type: true" \
38+
-H "Ce-Specversion: true" \
39+
-H "Ce-Source: true" \
40+
-H "Ce-Id: true" \
41+
http://localhost:8080
42+
```
43+
44+
### Using the Pub/Sub emulator
45+
46+
Another way to test your cloud function Pub/Sub endpoint is to use the [Pub/Sub Emulator](https://cloud.google.com/pubsub/docs/emulator). This allows you to use the Pub/Sub notification from another service to trigger your cloud function.
47+
48+
The high level approach is to:
49+
1. Start the Pub/Sub Emulator
50+
2. Use the Pub/Sub client library to create a subscription and set the `pushEndpoint` to `http://localhost:8080`.
51+
52+
After setup, all notifications to the subscription topic will be pushed to your cloud function.
53+
54+
Here is a sample script for creating subscription with a `pushEndpoint`:
55+
56+
```js
57+
const { PubSub } = require('@google-cloud/pubsub');
58+
59+
async function main() {
60+
const apiEndpoint = 'localhost:8085';
61+
console.log(`Listening to the Pub/Sub emulator event at: ${apiEndpoint}`);
62+
const pubsub = new PubSub({
63+
apiEndpoint, // Pub/Sub emulator endpoint
64+
projectId: 'myproject',
65+
});
66+
const topic = await pubsub.topic('my-topic');
67+
const [topicExists] = await topic.exists();
68+
if (!topicExists) {
69+
await topic.create();
70+
}
71+
const createSubscriptionResponse = await topic.createSubscription('my_subscription', {
72+
pushEndpoint: 'https://localhost:8080',
73+
});
74+
}
75+
76+
main();
77+
```

0 commit comments

Comments
 (0)