Does anyone know how to do local

Does anyone know how to do local development with functions and durable objects? Specifically how do you get binding with durable objects to work on a local environment?
41 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
Thanks for the response, Larry. One questions: Presumably you use .newUniqueId() and it will work for local development too?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
@Larry I wonder if you can help me again here I have created my durable object and I cannot connect to it locally here are some details: Binding name = DECISION_DO Class name = DecisionDO package.json "name" = decision-do wrangler has three environments .. the default "name" is "decision-do-dev" I've tried various command lines: npx wrangler pages dev --do=DECISION_DO=DecisionDO@decision-do or npx wrangler pages dev --do DECISION_DO=DecisionDO@decision-do or DECISION_DO=DecisionDO@decision-do-dev or DECISION_DO=DecisionDO@wrangler-decision-do-dev or DECISION_DO=DecisionDO@wrangler-decision-do or DECISION_DO=DecisionDO@[email protected] In each case DECISION_DO is not defined in context.env any suggestions? Btw .. when I run the npx wrangler pages dev .. it does report that I have access to DecisionDO .. yet my code finds no DecisionDO defined in context.env I wonder if I have to also express the binding in the wrangler.toml file for the worker project
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
ok ... I have no bindings in wrangler.toml .. since I assumed that the binding from --do is what matters so do I need to add that as well
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
yep .. I have a name in there : name = "decision-do-dev" main = "src/index.ts"
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
so the correct command should be npx wrangler pages dev --do=DECISION_DO@decision-do-dev I am also running npx wrangler dev --local from the directory of the durable object worker in another terminal window and I've verified that is working hang on .. it should be npx wrangler pages dev --do DECISION_DO=DecisionDO@decision-do-dev
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
yep .. let me try again .. right now It says: Your worker has access to the following bindings: - Durable Objects: - DECISION_DO: DecisionDO (defined in decision-do-dev)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
yep there is no DECISION_DO defined in context.env at all I console.log the entire context.env
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
FYI .. my complete wrangler command is: npx wrangler pages dev \ --kv=PERSON --kv=TEST --kv=CONFIG \ --do DECISION_DO=DecisionDO@decision-do-dev \ --d1=META_DB --persist \ -- npm start
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
Mine is: interface Env { DECISION_DO: DurableObjectNamespace; } //export async function onRequest( context ) { export const onRequest: PagesFunction<Env> =async (context) => { (using Typescript)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
let me try yep .. tried it with export async function onRequest( context ) { and context.env.DECISION_DO however, I actually console.log all of context.env .. and I'm seeing all the other bindings but there is not DECISION_DO
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
⛅️ wrangler 2.12.2 (update available 2.14.0) I tried upgrading to 2.14.0 but then everything crashed badly
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
o ok
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
let me downgrade to 2.12.0
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
lol .. I downgraded and now everything crashes again .. with a failed to parse URL node_modules/undici/lib/fetch/request.js:81:15 let me see if it recovers if I go back to to 2.12.2
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
lol .. things work with [email protected] .. both versions are the same ok .. no idea what to do at this point I'm sure something will come to us actually by work
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
I mean that there is no nasty crash in undici but the DECISION_DO is not there yep .. agreed
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
I'm on MacOS 13.2.1 going to take a break .. will try npm install the crash happens because undici requires fully qualified URLs .. it does not like /something/something-else .. as it does a new URL(...) and is missing the baseUrl presumably will try wiping node_modules and trying a new install
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hanpolo
hanpoloOP2y ago
Yep .. I'll go fix that on my URLs .. but it also is complaining about the favicon on my front end wow .. 12.4.0 actually takes my fully qualified URL .. an external call .. strips out the domain .. and inserts a local domain instead .. why does it do this .. no idea Also, deleted node_modules and ran npm install. installed 2.12.0 and still got the same results. but this time at least I did not get the crash with the external URL Ok Larry. Final disposition is that I've installed wrangler 2.12.0. The basic code runs but DO_DECISION is missing from context.env wrangler 2.14.0 has a bug. It takes my fully qualified external URL (to some external 3P service) and strips out its domain and makes it localhost:8788 and then it crashes I've opened a bug for that one
hanpolo
hanpoloOP2y ago
GitHub
🐛 BUG: Failed to Parse URL · Issue #2984 · cloudflare/workers-sdk
Which Cloudflare product(s) does this pertain to? Pages, Wrangler What version of Wrangler are you using? 2.14.0 What operating system are you using? macOS 13.2.1 Describe the Bug I installed wrang...
hanpolo
hanpoloOP2y ago
@Larry Any suggestions on a course of action. I'm in a bind without local emulation. Let me know what you need to debug this. Some more steps I've taken: * I created a test project where my only script is functions/test.ts * Tested [email protected] and verified it works * eliminated -- npm start at the end of the command : so npx wrangler pages dev www --do=DECISION_DO=DecisionDO@decision-do-dev Where www is directory for static assets. The command displays: Your worker has access to the following bindings: - Durable Objects: - DECISION_DO: DecisionDO (defined in decision-do-dev) However, when I console.log from function/test.ts I do not have DECISION_DO in context.env. Next, I will attempt to create a plain HTML with basic Javascript (i.e. eliminate my framework .. all in a test project) to see if I get different results Ok .. I ran the whole thing with very plain HTML and things still don't work: index.html: <!DOCTYPE HTML> <html> <body> <p>Before the script...</p> <script src="./script.js"></script> <p>...After the script.</p> </body> </html> script.js: async function fetchDO() { console.log('hello world async'); const response = await fetch('/test'); console.log(response.data); } fetchDO(); functions/test.ts: export async function onRequest( context ) { console.log('test.onRequest'); console.log(context.env); return new Response('hello function'); } Wrangler Command: npx wrangler pages dev wwwp --do=DECISION_DO=DecisionDO@decision-do-dev (wwwp is directory for index.html & script.js) Console.log from test.ts: test.onRequest { ASSETS: Fetcher {} } There is no DECISION_DO in context.env I also ran my durable object from another terminal with npx wrangler dev --local .. I opened a window and verified the worker is sending back hello world
hanpolo
hanpoloOP2y ago
FYI put all this code on https://github.com/safaalai/stencil-playground.git Note that there's a lot of other stuff there but the main directories to bother with are functions & wwwp. Top directory package.json command pages.wwwp will run the simple pages in wwwp folder. In packages/decision-do directory 'npm start' will run wrangler dev --local
GitHub
GitHub - safaalai/stencil-playground
Contribute to safaalai/stencil-playground development by creating an account on GitHub.
hanpolo
hanpoloOP2y ago
Ok .. got it working. You had to add a durable objects section to the wrangler.toml file of the woker script with the durable object: `[durable_objects] bindings = [ { name="DURABLE_DO", class_name="DecisionDO"} ]
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server