在开发过程中如何在本地运行我的cloudflare worker无服务器功能? [英] How to locally run my cloudflare worker serverless function, during development?

查看:490
本文介绍了在开发过程中如何在本地运行我的cloudflare worker无服务器功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法使用无服务器框架部署了我的第一个cloudflare工作者 https://serverless.com/framework/docs/providers/cloudflare/guide/ 当我碰到云时它正在工作.

在开发过程中,希望能够在 http://localhost:8080/ * 上进行测试>

使用serverless.yml中指定的功能启动本地http服务器并处理我的请求的最简单方法是什么?

我查看了 https://github. com/serverless/examples/tree/master/google-node-simple-http-endpoint 但没有开始"脚本.

https://github.com/serverless/上似乎没有关于cloudflare的示例./p>

解决方案

目前,尚无法在本地运行真正的Cloudflare Workers运行时. Workers团队知道开发人员需要此功能,但是要将核心Workers运行时与Cloudflare其余软件堆栈区分开来要花一些工夫,否则,Cloudflare的软件堆栈太复杂了,无法在本地运行.

同时,您可以尝试以下几种选择:

第三方模拟器

Cloudworker 是Cloudflare Workers的仿真器,它在node.js上本地运行.它是由Dollar Shave Club的工程师建造的,Dollar Shave Club是使用Workers的公司,而不是Cloudflare.由于它是Workers环境的整个独立实现,因此它的行为方式与真实事物"之间可能会存在很小的差异.但是,它足以完成一些工作.

预览服务API

cloudflareworkers.com 上显示的预览可以通过API访问.使用某些curl命令,您可以将代码上传到cloudflareworkers.com并在其上运行测试.这并不是真正的本地",但是如果您始终连接到互联网,那几乎是相同的.您不需要任何特殊的凭据即可使用此API,因此您可以编写一些脚本来使用它来运行单元测试等.

通过将POST脚本命名为https://cloudflareworkers.com/script来上传名为worker.js的脚本:

SCRIPT_ID=$(curl -sX POST https://cloudflareworkers.com/script \
  -H "Content-Type: text/javascript" --data-binary @worker.js | \
  jq -r .id)

现在$SCRIPT_ID将是一个32位十六进制数字,用于标识您的脚本.请注意,该ID基于哈希,因此,如果您上传完全相同的脚本两次,则会获得相同的ID.

接下来,生成一个随机会话ID(32个十六进制数字):

SESSION_ID=$(head -c 16 /dev/urandom | xxd -p)

此会话ID必须是密码随机的,这一点很重要,因为具有该ID的任何人都可以将devtools连接到您的预览版并对其进行调试.

我们还要定义两个配置:

PREVIEW_HOST=example.com
HTTPS=1

这些参数指定当您的工作人员运行时,预览的行为应与在https://example.com上运行的预览相同.传入请求的URL和Host标头将被重写为该协议和主机名.如果URL应该是HTTPS,则设置HTTPS=1;否则,请设置HTTPS=0.

现在,您可以像以下方式向您的工作人员发送请求:

curl https://00000000000000000000000000000000.cloudflareworkers.com \
  -H "Cookie: __ew_fiddle_preview=$SCRIPT_ID$SESSION_ID$HTTPS$PREVIEW_HOST"

(32个零可以是任何十六进制数字.在浏览器中使用预览时,它们是随机生成的,以防止cookie和缓存的内容在整个会话中相互干扰.但是,在使用curl时,这没有关系,因此全零是可以的.)

您可以更改此curl行,以在URL中包含路径,使用其他方法(如-X POST),添加标头等.只要主机名和cookie如下所示,它将转到您的预览工作者.

最后,您可以连接devtools控制台以在Chrome中进行调试(不幸的是,当前仅在Chrome中有效):

google-chrome https://cloudflareworkers.com/devtools/inspector.html?wss=cloudflareworkers.com/inspect/$SESSION_ID&v8only=true

请注意,上述API目前尚未正式记录,并且可能会在将来进行更改,但是通过在浏览器中打开cloudflareworkers.com并查看其发出的请求,应该相对容易找出更改.

I managed to deploy my first cloudflare worker using serverless framework according to https://serverless.com/framework/docs/providers/cloudflare/guide/ and it is working when I hit the cloud.

During development, would like to be able to test on http://localhost:8080/*

What is the simplest way to bring up a local http server and handle my requests using function specified in serverless.yml?

I looked into https://github.com/serverless/examples/tree/master/google-node-simple-http-endpoint but there is no "start" script.

There seem to be no examples for cloudflare on https://github.com/serverless/

解决方案

At present, there is no way to run the real Cloudflare Workers runtime locally. The Workers team knows that developers need this, but it will take some work to separate the core Workers runtime from the rest of Cloudflare's software stack, which is otherwise too complex to run locally.

In the meantime, there are a couple options you can try instead:

Third-party emulator

Cloudworker is an emulator for Cloudflare Workers that runs locally on top of node.js. It was built by engineers at Dollar Shave Club, a company that uses Workers, not by Cloudflare. Since it's an entire independent implementation of the Workers environment, there are likely to be small differences between how it behaves vs. the "real thing". However, it's good enough to get some work done.

Preview Service API

The preview seen on cloudflareworkers.com can be accessed via API. With some curl commands, you can upload your code to cloudflareworkers.com and run tests on it. This isn't really "local", but if you're always connected to the internet anyway, it's almost the same. You don't need any special credentials to use this API, so you can write some scripts that use it to run unit tests, etc.

Upload a script called worker.js by POSTing it to https://cloudflareworkers.com/script:

SCRIPT_ID=$(curl -sX POST https://cloudflareworkers.com/script \
  -H "Content-Type: text/javascript" --data-binary @worker.js | \
  jq -r .id)

Now $SCRIPT_ID will be a 32-digit hex number identifying your script. Note that the ID is based on a hash, so if you upload the exact same script twice, you get the same ID.

Next, generate a random session ID (32 hex digits):

SESSION_ID=$(head -c 16 /dev/urandom | xxd -p)

It's important that this session ID be cryptographically random, because anyone with the ID will be able to connect devtools to your preview and debug it.

Let's also define two pieces of configuration:

PREVIEW_HOST=example.com
HTTPS=1

These specify that when your worker runs, the preview should act like it is running on https://example.com. The URL and Host header of incoming requests will be rewritten to this protocol and hostname. Set HTTPS=1 if the URLs should be HTTPS, or HTTPS=0 if not.

Now you can send a request to your worker like:

curl https://00000000000000000000000000000000.cloudflareworkers.com \
  -H "Cookie: __ew_fiddle_preview=$SCRIPT_ID$SESSION_ID$HTTPS$PREVIEW_HOST"

(The 32 zeros can be any hex digits. When using the preview in the browser, these are randomly-generated to prevent cookies and cached content from interfering across sessions. When using curl, though, this doesn't matter, so all-zero is fine.)

You can change this curl line to include a path in the URL, use a different method (like -X POST), add headers, etc. As long as the hostname and cookie are as shown, it will go to your preview worker.

Finally, you can connect the devtools console for debugging in Chrome (currently only works in Chrome unfortunately):

google-chrome https://cloudflareworkers.com/devtools/inspector.html?wss=cloudflareworkers.com/inspect/$SESSION_ID&v8only=true

Note that the above API is not officially documented at present and could change in the future, but changes should be relatively easy to figure out by opening cloudflareworkers.com in a browser and looking at the requests it makes.

这篇关于在开发过程中如何在本地运行我的cloudflare worker无服务器功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆