-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.ts
163 lines (136 loc) · 5.49 KB
/
session.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// deno-lint-ignore-file no-explicit-any
import type { StorageArea } from 'https://ghuc.cc/qwtel/kv-storage-interface/index.d.ts';
import { WebUUID } from 'https://ghuc.cc/qwtel/web-uuid/index.ts';
import { Base64Decoder, Base64Encoder } from 'https://ghuc.cc/qwtel/base64-encoding/index.ts';
import { createDraft, finishDraft, Draft, enableMapSet } from 'https://cdn.skypack.dev/immer@9.0.14?dts';
import { Encoder as BinaryEncoder, Decoder as BinaryDecoder } from 'https://cdn.skypack.dev/msgpackr@1.5.5?dts';
import type { Context } from './context.ts';
import type { Awaitable } from './utils/common-types.ts';
import type { UnsignedCookiesContext, SignedCookiesContext, EncryptedCookiesContext } from './cookies.ts';
enableMapSet();
// enablePatches();
type Rec = Record<PropertyKey, any>;
type CookieContext = Context & (EncryptedCookiesContext | SignedCookiesContext | UnsignedCookiesContext);
interface SessionContext<S extends Rec = Rec> {
session: S
}
export interface CookieSessionContext<S extends Rec = Rec> extends SessionContext<S> {
cookieSession: S
}
export interface StorageSessionContext<S extends Rec = Rec> extends SessionContext<S> {
storageSession: S
}
export interface CookieSessionOptions<S extends Rec = Rec> {
/** The name of the session cookie. Defaults to `sid`. */
cookieName?: string,
/** Session expiration time in seconds. Defaults to five minutes. */
expirationTtl?: number,
/** Provide a record that serves as the default session object. Also used for type inference. */
defaultSession?: S,
}
export interface StorageSessionOptions<S extends Rec = Rec> extends CookieSessionOptions<S> {
/** The storage area where to persist the session objects. */
storage: StorageArea,
}
/**
* Cookie session middleware for worker runtimes.
*
* Requires a cookie store, preferably encrypted or signed.
*
* Important: This will serialize the entire session data and store it in a cookie. It is sent with every request!
* Only applicable for small session objects. Use `storageSession` for a traditional, KV store-backed session.
*/
export function cookieSession<S extends Rec = Rec>(
options: CookieSessionOptions<S> = {}
): <X extends CookieContext>(ax: Awaitable<X>) => Promise<X & CookieSessionContext<S>> {
return async ax => {
const ctx = await ax;
const { cookieStore, cookies } = ctx;
const { defaultSession, cookieName = 'obj', expirationTtl = 5 * 60 } = options
const [session, orig] = getCookieSessionProxy<S>(cookies[cookieName], {
cookieName,
expirationTtl,
defaultSession,
});
const newContext = Object.assign(ctx, { session, cookieSession: session })
ctx.effects.push(response => {
const next: S = finishDraft(session)
if (next !== orig) {
cookieStore.set({
name: cookieName,
value: stringifySessionCookie(next),
expires: new Date(Date.now() + expirationTtl * 1000),
sameSite: 'lax',
httpOnly: true,
});
}
return response;
})
return newContext;
};
}
/**
* Session middleware for worker runtimes.
*
* Need to provide a `StorageArea` to persist the session between requests.
* See `@worker-tools/kv-storage`.
*
*/
// FIXME: Will "block" until session object is retrieved from KV => provide "unyielding" version that returns a promise?
export function storageSession<S extends Rec = Rec>(
options: StorageSessionOptions<S>
): <X extends CookieContext>(ax: Awaitable<X>) => Promise<X & StorageSessionContext<S>> {
return async ax => {
const ctx = await ax;
const { cookies, cookieStore } = ctx;
const { storage, defaultSession, cookieName = 'sid', expirationTtl = 5 * 60 } = options
const [sid, session, orig] = await getStorageSessionProxy<S>(cookies[cookieName], {
storage,
cookieName,
expirationTtl,
defaultSession,
});
const newContext = Object.assign(ctx, { session, storageSession: session })
ctx.waitUntil((async () => {
await ctx.closed;
const next: S = finishDraft(session)
if (next !== orig) {
await storage.set(sid, next, { expirationTtl });
}
})())
ctx.effects.push(response => {
if (!cookies[cookieName]) {
cookieStore.set({
name: cookieName,
value: sid.id,
expires: new Date(Date.now() + expirationTtl * 1000),
sameSite: 'lax',
httpOnly: true,
});
}
return response;
})
return newContext;
};
}
const stringifySessionCookie = <T>(value: T) =>
new Base64Encoder({ url: true }).encode(new BinaryEncoder({ structuredClone: true }).encode(value));
const parseSessionCookie = <T>(value: string) =>
<T>new BinaryDecoder({ structuredClone: true }).decode(new Base64Decoder().decode(value));
function getCookieSessionProxy<S extends Rec = Rec>(
cookieVal: string | null | undefined,
{ defaultSession }: CookieSessionOptions<S>,
): [Draft<S>, S] {
const obj = cookieVal ? parseSessionCookie<S>(cookieVal) : defaultSession ?? <S>{};
const draft = createDraft(obj)
return [draft, obj]
}
async function getStorageSessionProxy<S extends Rec = Rec>(
cookieVal: string | null | undefined,
{ defaultSession, storage }: StorageSessionOptions<S>,
): Promise<[WebUUID, Draft<S>, S]> {
const sessionId = cookieVal ? new WebUUID(cookieVal) : WebUUID.v4()
const obj = (await storage.get<S>(sessionId)) ?? defaultSession ?? <S>{};
const draft = createDraft(obj)
return [sessionId, draft, obj];
}