sonikjs / sonik

348 stars 9 forks source link

Setting cookies fails during local development #6

Closed bruceharrison1984 closed 12 months ago

bruceharrison1984 commented 12 months ago

Trying to set a cookie during local development causes the server to terminate with an error. Deploying the same code Cloudflare, the cookie is created and no error is thrown. So this only happens locally.

import { setCookie } from 'hono/cookie';
import { Hono } from 'hono';
import { createApp } from 'sonik/default';

const base = new Hono();
base.get('/', (c) => {
  setCookie(c, 'test', '1234');
  return c.text('cookied');
});

const app = createApp({ app: base });
app.showRoutes();

export default app;

Error:

TypeError: res.headers.getSetCookie is not a function

Manually trying to set the cookie also fails with the same error:

base.get('/', (c) => {
  // any of these cause an error
  c.header('Set-Cookie', 'mycookie=12345');
  c.res.headers.append('Set-Cookie', 'mycookie=12345');
  c.res.headers.set('Set-Cookie', 'mycookie=12345');
  return c.text('cookied');
});

Directly setting a header doesn't cause the app to crash, but only if no cookie is created. So it appears to be directly related to setting cookies.

app.get('/cookie', (c) => {
  c.res.headers.append('myheader', '12345');
  return c.text('not cookied');
});
bruceharrison1984 commented 12 months ago

I finally got to dig into this, and it is simply due to getSetCookie being a somewhat recent addition. Upgrading node immediately resolved the issue.

Per your recommendation @yusukebe

The Hono node adapter does not support v18.6.0. If you want to use "v18.x", use v18.14.1 or higher.

https://github.com/honojs/node-server/issues/58

I really like what you're building here(I had started something very similar until I found yours), and I'll try to stop submitting issues without more due diligence.