The script will never generate a response

Ok that's not the reason, it's not working in production either. Creating a thread to paste the code. (Sorry for tagging: @kian)
7 Replies
Sam
SamOP12mo ago
index.ts
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const pg = new Client({
connectionString: env.HYPERDRIVE?.connectionString || env.pgConnectionString || ""
});

try {
return apiRouter({ pg }, request, env, ctx);
} catch (e) {
return ServerUnavailable();
}
}
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const pg = new Client({
connectionString: env.HYPERDRIVE?.connectionString || env.pgConnectionString || ""
});

try {
return apiRouter({ pg }, request, env, ctx);
} catch (e) {
return ServerUnavailable();
}
}
}
apiRouter.ts
const Router = (initialPassthrough: InitialPassthrough, request: Request, env: Env, ctx: ExecutionContext) => {
const { pg } = initialPassthrough;

const app = new Hono<HonoRouter>();

app.use('*', async (c, next) => {
const data = await pg.query(`SELECT 1 + 1 AS result`);

console.log(data);

return c.json({});
});

return app.fetch(request, env, ctx);
}

export default Router;
const Router = (initialPassthrough: InitialPassthrough, request: Request, env: Env, ctx: ExecutionContext) => {
const { pg } = initialPassthrough;

const app = new Hono<HonoRouter>();

app.use('*', async (c, next) => {
const data = await pg.query(`SELECT 1 + 1 AS result`);

console.log(data);

return c.json({});
});

return app.fetch(request, env, ctx);
}

export default Router;
This is resulting in the following error:
Error: The script will never generate a response.
Error: The script will never generate a response.
Replacing "Client" with "Pool" does work. But not the recommended way (if I'm reading this message correctly): https://discord.com/channels/595317990191398933/1150557986239021106/1184899420119122011 Without replacing this and using pg.connect(), it works. But this shouldn't be needed in production, since Hyperdrive is pooling for us - and the connection is being destroyed when the Worker is finished.
kian
kian12mo ago
Just seems like you missed out await pg.connect(); before the query.
kian
kian12mo ago
No description
Sam
SamOP12mo ago
Ah I thought you where talking about not needing that. https://discord.com/channels/595317990191398933/1150557986239021106/1184899420119122011 Do I need to close it?
kian
kian12mo ago
In your client example you had await client.connect() You don't necessarily need to close it, Hyperdrive will handle that.
Sam
SamOP12mo ago
Yeah, sorry. Just misunderstanding your reaction then. Thanks for answering.
kian
kian12mo ago
The way that Hyperdrive works is that your Worker connects to Hyperdrive on each request (which is a quick round-trip) and Hyperdrive keeps a connection open to your database.
Want results from more Discord servers?
Add your server