Skip to content

Commit

Permalink
Remove spoken-word expires
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Sep 28, 2022
1 parent 9a5d901 commit 515690c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 74 deletions.
1 change: 0 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
"kleur": "^4.1.4",
"magic-string": "^0.25.9",
"mime": "^3.0.0",
"ms": "^2.1.3",
"ora": "^6.1.0",
"path-browserify": "^1.0.1",
"path-to-regexp": "^6.2.1",
Expand Down
38 changes: 2 additions & 36 deletions packages/astro/src/core/cookies/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { CookieSerializeOptions } from 'cookie';
import { parse, serialize } from 'cookie';
import ms from 'ms';

interface AstroCookieSetOptions {
domain?: string;
expires?: number | Date | string;
expires?: Date;
httpOnly?: boolean;
maxAge?: number;
path?: string;
Expand Down Expand Up @@ -42,9 +41,6 @@ class AstroCookie implements AstroCookieInterface {
return JSON.parse(this.value);
}
number() {
if(this.value === undefined) {
throw new Error(`Cannot convert undefined to a number.`);
}
return Number(this.value);
}
boolean() {
Expand Down Expand Up @@ -149,39 +145,9 @@ class AstroCookies implements AstroCookiesInterface {
}
}

let expires: Date | undefined = undefined;
if(options?.expires) {
let rawExpires = options.expires;
switch(typeof rawExpires) {
case 'string': {
let numberOfMs = ms(rawExpires);
if(numberOfMs === undefined) {
if(rawExpires.includes('month')) {
throw new Error(`Cannot convert months because there is no fixed duration. Use days instead.`);
} else {
throw new Error(`Unable to convert expires expression [${rawExpires}]`);
}
}
let now = Date.now();
expires = new Date(now + numberOfMs);
break;
}
case 'number': {
expires = new Date(rawExpires);
break;
}
default: {
expires = rawExpires;
break;
}
}
}

const serializeOptions: CookieSerializeOptions = {};
if(options) {
Object.assign(serializeOptions, options, {
expires
});
Object.assign(serializeOptions, options);
}

this.#ensureOutgoingMap().set(key, [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
if(Astro.request.method === 'POST') {
Astro.cookies.set('admin', 'true', {
expires: '30 days'
expires: new Date()
});
}
---
Expand Down
80 changes: 80 additions & 0 deletions packages/astro/test/units/cookies/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,86 @@ describe('astro/src/core/cookies', () => {
expect(value).to.be.an('number');
expect(value).to.equal(22);
});

it('Coerces non-number into NaN', () => {
const req = new Request('http://example.com/', {
headers: {
'cookie': 'foo=bar'
}
});
let cookies = new AstroCookies(req);

const value = cookies.get('foo').number();
expect(value).to.be.an('number');
expect(Number.isNaN(value)).to.equal(true);
});
});

describe('.boolean()', () => {
it('Coerces true into `true`', () => {
const req = new Request('http://example.com/', {
headers: {
'cookie': 'foo=true'
}
});
let cookies = new AstroCookies(req);

const value = cookies.get('foo').boolean();
expect(value).to.be.an('boolean');
expect(value).to.equal(true);
});

it('Coerces false into `false`', () => {
const req = new Request('http://example.com/', {
headers: {
'cookie': 'foo=false'
}
});
let cookies = new AstroCookies(req);

const value = cookies.get('foo').boolean();
expect(value).to.be.an('boolean');
expect(value).to.equal(false);
});

it('Coerces 1 into `true`', () => {
const req = new Request('http://example.com/', {
headers: {
'cookie': 'foo=1'
}
});
let cookies = new AstroCookies(req);

const value = cookies.get('foo').boolean();
expect(value).to.be.an('boolean');
expect(value).to.equal(true);
});

it('Coerces 0 into `false`', () => {
const req = new Request('http://example.com/', {
headers: {
'cookie': 'foo=0'
}
});
let cookies = new AstroCookies(req);

const value = cookies.get('foo').boolean();
expect(value).to.be.an('boolean');
expect(value).to.equal(false);
});

it('Coerces truthy strings into `true`', () => {
const req = new Request('http://example.com/', {
headers: {
'cookie': 'foo=bar'
}
});
let cookies = new AstroCookies(req);

const value = cookies.get('foo').boolean();
expect(value).to.be.an('boolean');
expect(value).to.equal(true);
});
});
});
});
34 changes: 0 additions & 34 deletions packages/astro/test/units/cookies/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,6 @@ describe('astro/src/core/cookies', () => {
expect(headers[0]).to.equal('one=2');
});

it('can pass a string distance from now as expires', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);
cookies.set('one', 2, {
expires: '1 week'
});
let headers = Array.from(cookies.headers());
expect(headers).to.have.a.lengthOf(1);
expect(headers[0]).to.match(/one=2/);
expect(headers[0]).to.match(/Expires/);
});

it('can pass a date as a number to expires', () => {
let req = new Request('http://example.com/');
let expiration = new Date('Fri, 01 Jan 2044 00:00:00 GMT');
let cookies = new AstroCookies(req);
cookies.set('one', 2, {
expires: expiration.valueOf()
});
let headers = Array.from(cookies.headers());
expect(headers).to.have.a.lengthOf(1);
expect(headers[0]).to.equal('one=2; Expires=Fri, 01 Jan 2044 00:00:00 GMT');
});

it('throws if passing a string to expires that ms does not convert', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);
expect(() => {
cookies.set('one', 2, {
expires: 'unknown date'
});
}).to.throw(`Unable to convert expires expression [unknown date]`);
});

it('Can get the value after setting', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);
Expand Down
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 515690c

Please sign in to comment.