Skip to content

Instantly share code, notes, and snippets.

@choonkeat
Last active June 13, 2019 15:31
Show Gist options
  • Save choonkeat/9476909becf29608844cba328cc5e3a3 to your computer and use it in GitHub Desktop.
Save choonkeat/9476909becf29608844cba328cc5e3a3 to your computer and use it in GitHub Desktop.
Locating worker.js in Google Cloud Function

Locate worker.js

First, prepare an index.js that is valid, but has a runtime error

exports.helloWorld = function helloWorld (req, res) {
    console.log(abc) // `abc` is not defined
}

Next, deploy it, e.g. with gcloud beta functions deploy

Finally, trigger it and look at your Google Cloud Function log. You'll see a stacktrace of your error, e.g.

ReferenceError: abc is not defined at helloWorld (/user_code/index.js:20:14)
  at /var/tmp/worker/worker.js:635:7
  at /var/tmp/worker/worker.js:619:9
  at _combinedTickCallback (internal/process/next_tick.js:73:7)
  at process._tickDomainCallback (internal/process/next_tick.js:128:9)

From that stacktrace, we can see that the wrapper running your Google Cloud Function is located at /var/tmp/worker/worker.js

Content of worker.js

Although fs.readFileSync is not available in the given node js environment (i.e. var fs = require("fs") will fail) but we can use Go runtime to give us the file content

func main() {
	flag.Parse()
	http.HandleFunc(nodego.HTTPTrigger, func(w http.ResponseWriter, r *http.Request) {
		defer r.Body.Close()

		f, err := os.Open("/var/tmp/worker/worker.js")
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		defer f.Close()

		_, err = io.Copy(w, f)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	})

	nodego.TakeOver()
}

If you trigger your Google Cloud Function via http, you will get the content of /var/tmp/worker/worker.js in the http response body

@askdesigners
Copy link

Why would you need to do this? Just curious...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment