tony
tony
Explore posts from servers
HHono
Created by tony on 4/30/2024 in #help
executionCtx.waitUntil on write service
I have a worker that I want to call an external api on, but I want to return a response without awaiting the the external api call. (the external api call is to just save time writing to the db on another service).
const promise = fetch('http://localhost:3000/impression', {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
});
c.executionCtx.waitUntil(promise.then())

flow.params = { campaignId: campaignId }
return flow;
const promise = fetch('http://localhost:3000/impression', {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
});
c.executionCtx.waitUntil(promise.then())

flow.params = { campaignId: campaignId }
return flow;
however when I am testing it local and it is not hitting the service I am using hono https://hono.dev/api/context#executionctx the worker is on localhost:8787 and the write service for the impression is on localhost:3000 I am able to create the impression successfully calling it directly but the waitUntil is not hitting localhost:3000 when calling the localhost:8787 endpoint
1 replies
CDCloudflare Developers
Created by tony on 4/30/2024 in #workers-help
executionCtx.waitUntil on write service
I have a worker that I want to call an external api on, but I want to return a response without awaiting the the external api call. (the external api call is to just save time writing to the db on another service).
const promise = fetch('http://localhost:3000/impression', {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
});
c.executionCtx.waitUntil(promise.then())

flow.params = { campaignId: campaignId }
return flow;

const promise = fetch('http://localhost:3000/impression', {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
});
c.executionCtx.waitUntil(promise.then())

flow.params = { campaignId: campaignId }
return flow;

however when I am testing it local and it is not hitting the service I am using hono https://hono.dev/api/context#executionctx the worker is on localhost:8787 and the write service for the impression is on localhost:3000 I am able to create the impression successfully calling it directly but the waitUntil is not hitting localhost:3000 when calling the localhost:8787 endpoint
19 replies
CDCloudflare Developers
Created by tony on 3/25/2024 in #workers-help
Cannot change Custom Hostname proxy-fallback ip
I have a custom hostname trk.northwavestudio.com that has active status on both hostname status and certificate status. however I want to point the Custom Hostnames to a different worker (jump.example.com). I have changed the proxy-fallback A record to the ip of my jump.example.com worker however these Custom Hostnames are still being pointed to the api.example.com worker, I am able to observe logs when making calls to trk.northwavestudio.com to the api.example.com worker.
1 replies
CDCloudflare Developers
Created by tony on 3/25/2024 in #general-help
Custom Hostname proxy-fallback not calling correct worker.
I have a custom hostname trk.northwavestudio.com that has active status on both hostname status and certificate status. however I want to point the Custom Hostnames to a different worker (jump.example.com). I have changed the proxy-fallback A record to the ip of my jump.example.com worker however these Custom Hostnames are still being pointed to the api.example.com worker, I am able to observe logs when making calls to trk.northwavestudio.com to the api.example.com worker.
2 replies
CDCloudflare Developers
Created by tony on 2/1/2024 in #general-help
Create Custom Hostname response does not match the documentation
When sending a post request to the create endpoint it seems like the validation cert is not being returned in the response for the post hostname api. I would expect response.data.ssl.validation_records https://developers.cloudflare.com/api/operations/custom-hostname-for-a-zone-create-custom-hostname But the response is:
result: {
id: '85efa926-8179-4c81-926e-eced6009ad63',
hostname: 'test.exampleDomain.com',
ssl: {
id: 'de9b5334-8757-4b5f-85a9-5bcf3a4f0919',
type: 'dv',
method: 'txt',
status: 'initializing',
settings: [Object],
wildcard: false,
certificate_authority: 'google'
},
status: 'pending',
ownership_verification: {
type: 'txt',
name: '_cf-custom-hostname.test.exampleDomain.com',
value: '7bd6536e-dc3d-46ba-974a-9e608090a3aa'
},
ownership_verification_http: {
http_url: 'http://test.exampleDomain.com/.well-known/cf-custom-hostname-challenge/85efa926-8179-4c81-926e-eced6009ad63',
http_body: '7bd6536e-dc3d-46ba-974a-9e608090a3aa'
},
created_at: '2024-01-31T19:55:43.182431Z'
},
success: true,
errors: [],
messages: []
}
result: {
id: '85efa926-8179-4c81-926e-eced6009ad63',
hostname: 'test.exampleDomain.com',
ssl: {
id: 'de9b5334-8757-4b5f-85a9-5bcf3a4f0919',
type: 'dv',
method: 'txt',
status: 'initializing',
settings: [Object],
wildcard: false,
certificate_authority: 'google'
},
status: 'pending',
ownership_verification: {
type: 'txt',
name: '_cf-custom-hostname.test.exampleDomain.com',
value: '7bd6536e-dc3d-46ba-974a-9e608090a3aa'
},
ownership_verification_http: {
http_url: 'http://test.exampleDomain.com/.well-known/cf-custom-hostname-challenge/85efa926-8179-4c81-926e-eced6009ad63',
http_body: '7bd6536e-dc3d-46ba-974a-9e608090a3aa'
},
created_at: '2024-01-31T19:55:43.182431Z'
},
success: true,
errors: [],
messages: []
}
5 replies
CDCloudflare Developers
Created by tony on 1/29/2023 in #workers-help
Prisma in module worker
Anyone have an example of using '@Prisma/client/edge' in a cloudflare module worker? I am getting:
`Invalid
value ${JSON.stringify(e)} for datasource "${r}" provided to PrismaClient constructor.
`Invalid
value ${JSON.stringify(e)} for datasource "${r}" provided to PrismaClient constructor.
when justing doing a simple:
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
console.log(env.DATABASE_URL)
const prisma = new PrismaClient({
datasources: {
db: {
url: env.DATABASE_URL,
}
}
});
const users = await prisma.user.findMany();
return new Response("Hello World!");
},
};
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
console.log(env.DATABASE_URL)
const prisma = new PrismaClient({
datasources: {
db: {
url: env.DATABASE_URL,
}
}
});
const users = await prisma.user.findMany();
return new Response("Hello World!");
},
};
console log on the DATABASE_URL showing correct and I am able to connect to it fine with a nestjs repo
1 replies
CDCloudflare Developers
Created by tony on 1/16/2023 in #workers-help
CORS error with worker to localhost:3000
error
Access to fetch at 'https://users-worker.data-well.workers.dev/users' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Access to fetch at 'https://users-worker.data-well.workers.dev/users' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
worker
import { Router } from 'itty-router'
import { withContent } from 'itty-router-extras'
import { Prisma, PrismaClient, User } from '@prisma/client/edge'
import { createCors } from 'itty-cors'

// create CORS handlers
const { preflight, corsify } = createCors({ origins: ['*'] })

const prisma = new PrismaClient()
const router = Router()
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json'
}

router.get('/users', async () => {
const response: User[] = await prisma.user.findMany()
return new Response(JSON.stringify(response), {
headers
})
})

router.get('/users/:id', async ({ params }) => {
const { id } = params;
const response = await prisma.user.findUnique({
where: {
id
}
})

return new Response(JSON.stringify(response), { headers })
})

router.post('/users', withContent, async ({ content }) => {
const {id, name, email} = content;
const response = await prisma.user.create({
data: {
id,
name,
email
}
})
return new Response(JSON.stringify(response), { headers })
})

router.all('*', preflight)
router.all('*', () => new Response('Not Found.', { status: 404 }))

addEventListener('fetch', event => {
event.respondWith(router.handle(event.request),)
})
import { Router } from 'itty-router'
import { withContent } from 'itty-router-extras'
import { Prisma, PrismaClient, User } from '@prisma/client/edge'
import { createCors } from 'itty-cors'

// create CORS handlers
const { preflight, corsify } = createCors({ origins: ['*'] })

const prisma = new PrismaClient()
const router = Router()
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json'
}

router.get('/users', async () => {
const response: User[] = await prisma.user.findMany()
return new Response(JSON.stringify(response), {
headers
})
})

router.get('/users/:id', async ({ params }) => {
const { id } = params;
const response = await prisma.user.findUnique({
where: {
id
}
})

return new Response(JSON.stringify(response), { headers })
})

router.post('/users', withContent, async ({ content }) => {
const {id, name, email} = content;
const response = await prisma.user.create({
data: {
id,
name,
email
}
})
return new Response(JSON.stringify(response), { headers })
})

router.all('*', preflight)
router.all('*', () => new Response('Not Found.', { status: 404 }))

addEventListener('fetch', event => {
event.respondWith(router.handle(event.request),)
})
client

async getUsers(){
const response = await fetch('https://users-worker.data-well.workers.dev/users', {
method: 'get',
headers: { 'Access-Control-Allow-Origin' : '*' }
});
this.users = response
}

async getUsers(){
const response = await fetch('https://users-worker.data-well.workers.dev/users', {
method: 'get',
headers: { 'Access-Control-Allow-Origin' : '*' }
});
this.users = response
}
responds in postman and when I spin up wrangler dev but when trying to directly call https://users-worker.data-well.workers.dev/users from localhost:3000 it shows a cors error 😦
3 replies
CDCloudflare Developers
Created by tony on 1/15/2023 in #pages-help
worker error when using Building Nitro Server (preset: cloudflare_pages) and Prisma
adlab.pages.dev build log is attached
13:31:53.588 ✨ Success! Uploaded 0 files (38 already uploaded) (0.62 sec)
13:31:53.589
13:31:54.069 ✨ Upload complete!
13:31:55.513 Success: Assets published!
13:31:56.628 Error: Failed to publish your Function. Got error: Uncaught TypeError: Cannot read properties of undefined (reading 'fd')
at worker.mjs:6782:98 in useColors
at worker.mjs:6678:98 in createDebug
at worker.mjs:13054:40 in debugCall
at worker.mjs:13092:16
at worker.mjs:20707:3
13:31:53.588 ✨ Success! Uploaded 0 files (38 already uploaded) (0.62 sec)
13:31:53.589
13:31:54.069 ✨ Upload complete!
13:31:55.513 Success: Assets published!
13:31:56.628 Error: Failed to publish your Function. Got error: Uncaught TypeError: Cannot read properties of undefined (reading 'fd')
at worker.mjs:6782:98 in useColors
at worker.mjs:6678:98 in createDebug
at worker.mjs:13054:40 in debugCall
at worker.mjs:13092:16
at worker.mjs:20707:3
the nitro server builds
13:31:22.277 ℹ Building Nitro Server (preset: cloudflare_pages)
13:31:42.848 ✔ Nitro server built
13:31:42.917 └─ functions/path.js (1.35 MB) (367 kB gzip)
13:31:42.918 Σ Total size: 1.35 MB (367 kB gzip)
13:31:22.277 ℹ Building Nitro Server (preset: cloudflare_pages)
13:31:42.848 ✔ Nitro server built
13:31:42.917 └─ functions/path.js (1.35 MB) (367 kB gzip)
13:31:42.918 Σ Total size: 1.35 MB (367 kB gzip)
30 replies