divby0
divby0
Explore posts from servers
CDCloudflare Developers
Created by divby0 on 4/5/2024 in #workers-help
How does the caches.default interact with fetch by default?
No description
1 replies
CDCloudflare Developers
Created by divby0 on 10/9/2023 in #pages-help
Make stack trace use the provided sourcemap (single file, [[path]].js + .map)
I have configured a build that yields a functions/[[path]].js and a functions/[[path]].js.map file. How do I make cloudflare actually use the sourcemap so I can read and unsterand them when an error happens? Currently for my small project I am just hitting a webhook with the stacktrace that sends the stacktrace as a telegram to me but most of the time they are not really readable.
2 replies
DTDrizzle Team
Created by divby0 on 10/3/2023 in #help
Get Raw query string of a relational query in drizzle
I need the raw query string of a relational query to perform an explain command. The docs only explain this process for the non-relational queries :/
7 replies
CDCloudflare Developers
Created by divby0 on 7/3/2023 in #workers-help
Reverse Proxy for blog returns 301 on custom domain, but works correct on preview url?
I've got the domain rank-hub.com added as a custom domain for a worker and a blog lives on ghost.rank-hub.com. The worker script creates a proxy request and returns its result so I can have rank-hub.com/blog proxied to ghost.rank-hub.com/blog. This is very easy with workers and I appreciate it, but I am probably missing a little something as the setup works on the preview url: https://rank-hub-ghost-proxy.divby0.workers.dev/blog but not on the live one https://rank-hub.com/blog Somehow cloudflare returns a 301 to my request when I use the apex domain (so I get redirected to blog.rank-hub.com). When I use the preview url, the content is correctly proxied. What am I missing? Worker script:
export default {
async fetch(request, env, ctx) {
try {

const urlObject = new URL(request.url);

// If the request is to the Ghost subdirectory
if (/^\/blog/.test(urlObject.pathname)) {
// Then Proxy to Ghost
const GHOST_URL = "ghost.rank-hub.com";
const CUSTOM_URL = "rank-hub.com";

let url = new URL(request.url);

url.hostname = GHOST_URL;

let proxyRequest = new Request(url, request);

proxyRequest.headers.set('Host', GHOST_URL);

//Have an X-Forwarded-Host header that matches the custom domain in my.ghost.org.

proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);

//Include the X-Forwarded-Proto header set to https not http.

proxyRequest.headers.set("X-Forwarded-Proto", "https");

//Include the X-Forwarded-For header, populated with the remote IP of the original request.

let ip = proxyRequest.headers.get("CF-Connecting-IP");

proxyRequest.headers.set("X-Forwarded-For", ip);

return await fetch(proxyRequest);
}

} catch (error) {
return await fetch(request);
}

return await fetch(request);
},
};
export default {
async fetch(request, env, ctx) {
try {

const urlObject = new URL(request.url);

// If the request is to the Ghost subdirectory
if (/^\/blog/.test(urlObject.pathname)) {
// Then Proxy to Ghost
const GHOST_URL = "ghost.rank-hub.com";
const CUSTOM_URL = "rank-hub.com";

let url = new URL(request.url);

url.hostname = GHOST_URL;

let proxyRequest = new Request(url, request);

proxyRequest.headers.set('Host', GHOST_URL);

//Have an X-Forwarded-Host header that matches the custom domain in my.ghost.org.

proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);

//Include the X-Forwarded-Proto header set to https not http.

proxyRequest.headers.set("X-Forwarded-Proto", "https");

//Include the X-Forwarded-For header, populated with the remote IP of the original request.

let ip = proxyRequest.headers.get("CF-Connecting-IP");

proxyRequest.headers.set("X-Forwarded-For", ip);

return await fetch(proxyRequest);
}

} catch (error) {
return await fetch(request);
}

return await fetch(request);
},
};
16 replies
CDCloudflare Developers
Created by divby0 on 6/12/2023 in #workers-help
HTMLRewriter treats self closing tags like <img/> as text
I am using HTMLRewriter to get the plain text of a website. Using the following code listening to the text event, it results in uggly <img ....../> or <iframe .../> being included in my result:
let total = "";

const rewriter = new HTMLRewriter()
.on(`article`, {
text(text: Text): void | Promise<void> {
total += text.text;
}
})
;
let total = "";

const rewriter = new HTMLRewriter()
.on(`article`, {
text(text: Text): void | Promise<void> {
total += text.text;
}
})
;
After transforming an html response, this is how response can look like:
<iframe class="hide_iframe_" src="[...]"></iframe><img class="hide_iframe" height="1" width="1" alt="" src="[...]"/> Actual normal text <img src="[...]" alt="..." placeholder="blur" /> ...rest of the article
<iframe class="hide_iframe_" src="[...]"></iframe><img class="hide_iframe" height="1" width="1" alt="" src="[...]"/> Actual normal text <img src="[...]" alt="..." placeholder="blur" /> ...rest of the article
1 replies
DTDrizzle Team
Created by divby0 on 5/9/2023 in #help
Many-to-Many joins results in weird values
Hey, I am new to Prisma, switching here from prisma so I can use a typesafe orm with D1 properly. I am trying to return a many-to-many joined result:
const teamedUser = await db.select().from(usersToTeams)
.where(eq(usersToTeams.user, user.id))
.innerJoin(users, eq(usersToTeams.user, users.id))
.innerJoin(teams, eq(usersToTeams.team, teams.id))
.all();
const teamedUser = await db.select().from(usersToTeams)
.where(eq(usersToTeams.user, user.id))
.innerJoin(users, eq(usersToTeams.user, users.id))
.innerJoin(teams, eq(usersToTeams.team, teams.id))
.all();
This already returns a correct structure:
[
{
usersToTeams: { user: 4, team: 2 },
users: {
id: 2,
email: '<email>',
password: '<password-hash>',
name: '<this field is undefined, but weirdly it puts in the users email>'
},
teams: { id: undefined, name: undefined }
}
]
[
{
usersToTeams: { user: 4, team: 2 },
users: {
id: 2,
email: '<email>',
password: '<password-hash>',
name: '<this field is undefined, but weirdly it puts in the users email>'
},
teams: { id: undefined, name: undefined }
}
]
The weird things are: - the name field of the user is not set (it's undefined in reality, if I use sqlite to look at the row), but my (maybe wrong) query fills it with the user's email?? - the teams object has only undefined values in it even though id as well as name are set - I'd like to not select the password field of the user, how can I do that? I tried looking up partial selects in combination with joined fields, but nothing that I tried worked I'd really appreciate help 🙂 drizzle really looks cool, I just have to learn a few more things, I guess. PS: if that matters, I am using the sqlite / D1 variant of drizzle 🙂
23 replies