workers
workers copied to clipboard
Write Durable Objects in Go
Hi,
First of all, I'd like to express my thanks and my encouragement for the folks behind this project. I think this deserve full support from Cloudflare.
Now, I seem to have noticed that the Durable Objects can only be written in JS. I'd like to know if there is a plan to support writing actual DurableObject classes using Go itself, and not just the ability to call the stub from Go.
Thank you.
Hi, @eliezedeck !
Thank you for your feedback. In conclusion, I believe it is possible. However, the implementation will require one of the following two patterns.
- write a small amount of JavaScript code.
- generate JavaScript code.
In either case, due to the nature of Durable Objects, JavaScript code is required.
Durable Objects must be declared and exported as a JavaScript class. https://developers.cloudflare.com/workers/learning/using-durable-objects/#configuring-durable-object-bindings
The problem with this is that it relies on the ES module.
In the case of Pattern 1, it is likely that the following short code would need to be added to the JavaScript: (The following example assumes that the class name is Counter)
import { createDurableObjectClass } from "./utils.mjs";
export const Counter = createDurableObjectClass("Counter");
For pattern 2, I would provide the workers command as a CLI tool to automatically generate the current worker.mjs file.
$ workers generate .
Pattern 1 needs to be investigated to see if it can be implemented. Hopefully, this will be a relatively lightweight implementation since there is no need to provide a CLI tool. However, some additional work will be required to wrap the functionality provided by Durable Objects.
Pattern 2 can certainly be implemented. However, the hurdle for implementing a "worker" may be somewhat higher because of the code generation required; wrapping the functionality provided by Durable Objects would require the same amount of extra work as Pattern 1.
In any case, it seems difficult to achieve this immediately. I'll put it on the wish list! Thank you.
cc: @akarasz
Thank you for the details explanation @syumai. I hope it'll be possible, hopefully one day.