lixnet 1.3.3 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/lib/server.js
CHANGED
|
@@ -64,7 +64,13 @@ export default class LixnetServer {
|
|
|
64
64
|
? event.schema.parse(jsonData.input)
|
|
65
65
|
: jsonData.input;
|
|
66
66
|
try {
|
|
67
|
-
const newRequest = wrapLixnetRequest(requestClone
|
|
67
|
+
const newRequest = wrapLixnetRequest(requestClone, {
|
|
68
|
+
header: response.header.bind(response),
|
|
69
|
+
cookie: response.cookie.bind(response),
|
|
70
|
+
deleteCookie: response.deleteCookie.bind(response),
|
|
71
|
+
responseHeaders: response.responseHeaders,
|
|
72
|
+
responseCookies: response.responseCookies,
|
|
73
|
+
});
|
|
68
74
|
const data = await event.handler({
|
|
69
75
|
...validatedInput,
|
|
70
76
|
request: newRequest,
|
|
@@ -10,10 +10,23 @@ export default function (defaultHeaders) {
|
|
|
10
10
|
for (const [cookieName, cookie] of Object.entries(this.responseCookies)) {
|
|
11
11
|
if (cookie.type === "value") {
|
|
12
12
|
const opts = cookie.options ?? {};
|
|
13
|
-
|
|
13
|
+
const parts = [`${cookieName}=${cookie.value ?? ""}`];
|
|
14
|
+
if (opts.domain)
|
|
15
|
+
parts.push(`Domain=${opts.domain}`);
|
|
16
|
+
if (opts.path)
|
|
17
|
+
parts.push(`Path=${opts.path}`);
|
|
18
|
+
if (typeof opts.maxAge === "number")
|
|
19
|
+
parts.push(`Max-Age=${opts.maxAge}`);
|
|
20
|
+
if (opts.httpOnly)
|
|
21
|
+
parts.push("HttpOnly");
|
|
22
|
+
if (opts.secure)
|
|
23
|
+
parts.push("Secure");
|
|
24
|
+
if (opts.sameSite)
|
|
25
|
+
parts.push(`SameSite=${opts.sameSite}`);
|
|
26
|
+
headers.append("Set-Cookie", parts.join("; "));
|
|
14
27
|
}
|
|
15
28
|
else if (cookie.type === "delete") {
|
|
16
|
-
headers.
|
|
29
|
+
headers.append('Set-Cookie', `${cookieName}=; Max-Age=0; Path=/; HttpOnly; Secure; SameSite=Strict`);
|
|
17
30
|
}
|
|
18
31
|
else {
|
|
19
32
|
throw new Error(`Unknown cookie type: ${cookie.type}`);
|
|
@@ -7,12 +7,23 @@ export type RequestCookie = {
|
|
|
7
7
|
*/
|
|
8
8
|
export declare class ReadonlyRequestCookies {
|
|
9
9
|
private readonly map;
|
|
10
|
-
|
|
10
|
+
private readonly mut?;
|
|
11
|
+
constructor(cookieHeader: string | null, mut?: {
|
|
12
|
+
setCookie: (name: string, value: string, options?: unknown) => void;
|
|
13
|
+
deleteCookie: (name: string) => void;
|
|
14
|
+
getStaged: () => Record<string, {
|
|
15
|
+
type: "value" | "delete";
|
|
16
|
+
value?: string;
|
|
17
|
+
options?: unknown;
|
|
18
|
+
}>;
|
|
19
|
+
});
|
|
11
20
|
get size(): number;
|
|
12
21
|
get(name: string): RequestCookie | undefined;
|
|
13
22
|
getAll(): RequestCookie[];
|
|
14
23
|
getAll(name: string): RequestCookie[];
|
|
15
24
|
has(name: string): boolean;
|
|
25
|
+
set(name: string, value: string, options?: unknown): void;
|
|
26
|
+
delete(name: string): void;
|
|
16
27
|
[Symbol.iterator](): IterableIterator<[string, RequestCookie]>;
|
|
17
28
|
}
|
|
18
29
|
/**
|
|
@@ -21,7 +32,13 @@ export declare class ReadonlyRequestCookies {
|
|
|
21
32
|
*/
|
|
22
33
|
export declare class ReadonlyHeaders {
|
|
23
34
|
private readonly h;
|
|
24
|
-
|
|
35
|
+
private readonly mut?;
|
|
36
|
+
constructor(headers: Headers, mut?: {
|
|
37
|
+
setHeader: (name: string, value: string) => void;
|
|
38
|
+
appendHeader: (name: string, value: string) => void;
|
|
39
|
+
deleteHeader: (name: string) => void;
|
|
40
|
+
getStaged: () => Record<string, string>;
|
|
41
|
+
});
|
|
25
42
|
get(name: string): string | null;
|
|
26
43
|
has(name: string): boolean;
|
|
27
44
|
getSetCookie(): string[];
|
|
@@ -30,10 +47,23 @@ export declare class ReadonlyHeaders {
|
|
|
30
47
|
values(): IterableIterator<string>;
|
|
31
48
|
forEach(callbackfn: (value: string, name: string, parent: ReadonlyHeaders) => void, thisArg?: unknown): void;
|
|
32
49
|
[Symbol.iterator](): IterableIterator<[string, string]>;
|
|
50
|
+
set(name: string, value: string): void;
|
|
51
|
+
append(name: string, value: string): void;
|
|
52
|
+
delete(name: string): void;
|
|
33
53
|
}
|
|
34
54
|
export type LixnetRequest = Omit<Request, "headers"> & {
|
|
35
55
|
cookies(): ReadonlyRequestCookies;
|
|
36
56
|
headers(): ReadonlyHeaders;
|
|
37
57
|
};
|
|
38
58
|
/** Used by the server only; not part of the public package API. */
|
|
39
|
-
export declare function wrapLixnetRequest(request: Request
|
|
59
|
+
export declare function wrapLixnetRequest(request: Request, response: {
|
|
60
|
+
header: (name: string, value: string) => void;
|
|
61
|
+
cookie: (name: string, value: string, options?: any) => void;
|
|
62
|
+
deleteCookie: (name: string) => void;
|
|
63
|
+
responseHeaders: Record<string, string>;
|
|
64
|
+
responseCookies: Record<string, {
|
|
65
|
+
type: "value" | "delete";
|
|
66
|
+
value?: string;
|
|
67
|
+
options?: unknown;
|
|
68
|
+
}>;
|
|
69
|
+
}): LixnetRequest;
|
package/dist/lib/util/request.js
CHANGED
|
@@ -32,13 +32,21 @@ function parseCookieHeader(header) {
|
|
|
32
32
|
*/
|
|
33
33
|
export class ReadonlyRequestCookies {
|
|
34
34
|
map;
|
|
35
|
-
|
|
35
|
+
mut;
|
|
36
|
+
constructor(cookieHeader, mut) {
|
|
36
37
|
this.map = parseCookieHeader(cookieHeader);
|
|
38
|
+
this.mut = mut;
|
|
37
39
|
}
|
|
38
40
|
get size() {
|
|
39
41
|
return this.map.size;
|
|
40
42
|
}
|
|
41
43
|
get(name) {
|
|
44
|
+
const staged = this.mut?.getStaged()?.[name];
|
|
45
|
+
if (staged) {
|
|
46
|
+
if (staged.type === "delete")
|
|
47
|
+
return undefined;
|
|
48
|
+
return { name, value: staged.value ?? "" };
|
|
49
|
+
}
|
|
42
50
|
const value = this.map.get(name);
|
|
43
51
|
if (value === undefined)
|
|
44
52
|
return undefined;
|
|
@@ -46,10 +54,27 @@ export class ReadonlyRequestCookies {
|
|
|
46
54
|
}
|
|
47
55
|
getAll(name) {
|
|
48
56
|
if (name === undefined) {
|
|
49
|
-
|
|
57
|
+
const base = [...this.map.entries()].map(([n, value]) => ({
|
|
50
58
|
name: n,
|
|
51
59
|
value,
|
|
52
60
|
}));
|
|
61
|
+
const staged = this.mut?.getStaged?.();
|
|
62
|
+
if (!staged)
|
|
63
|
+
return base;
|
|
64
|
+
const out = new Map(base.map((c) => [c.name, c.value]));
|
|
65
|
+
for (const [n, v] of Object.entries(staged)) {
|
|
66
|
+
if (v.type === "delete")
|
|
67
|
+
out.delete(n);
|
|
68
|
+
else
|
|
69
|
+
out.set(n, v.value ?? "");
|
|
70
|
+
}
|
|
71
|
+
return [...out.entries()].map(([n, value]) => ({ name: n, value }));
|
|
72
|
+
}
|
|
73
|
+
const staged = this.mut?.getStaged()?.[name];
|
|
74
|
+
if (staged) {
|
|
75
|
+
if (staged.type === "delete")
|
|
76
|
+
return [];
|
|
77
|
+
return [{ name, value: staged.value ?? "" }];
|
|
53
78
|
}
|
|
54
79
|
const value = this.map.get(name);
|
|
55
80
|
if (value === undefined)
|
|
@@ -57,10 +82,34 @@ export class ReadonlyRequestCookies {
|
|
|
57
82
|
return [{ name, value }];
|
|
58
83
|
}
|
|
59
84
|
has(name) {
|
|
85
|
+
const staged = this.mut?.getStaged()?.[name];
|
|
86
|
+
if (staged)
|
|
87
|
+
return staged.type !== "delete";
|
|
60
88
|
return this.map.has(name);
|
|
61
89
|
}
|
|
90
|
+
set(name, value, options) {
|
|
91
|
+
if (!this.mut) {
|
|
92
|
+
throw new Error("cookies().set is not available in this context");
|
|
93
|
+
}
|
|
94
|
+
this.mut.setCookie(name, value, options);
|
|
95
|
+
}
|
|
96
|
+
delete(name) {
|
|
97
|
+
if (!this.mut) {
|
|
98
|
+
throw new Error("cookies().delete is not available in this context");
|
|
99
|
+
}
|
|
100
|
+
this.mut.deleteCookie(name);
|
|
101
|
+
}
|
|
62
102
|
[Symbol.iterator]() {
|
|
63
|
-
const map = this.map;
|
|
103
|
+
const map = new Map(this.map);
|
|
104
|
+
const staged = this.mut?.getStaged?.();
|
|
105
|
+
if (staged) {
|
|
106
|
+
for (const [n, v] of Object.entries(staged)) {
|
|
107
|
+
if (v.type === "delete")
|
|
108
|
+
map.delete(n);
|
|
109
|
+
else
|
|
110
|
+
map.set(n, v.value ?? "");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
64
113
|
function* iter() {
|
|
65
114
|
for (const [n, value] of map.entries()) {
|
|
66
115
|
yield [n, { name: n, value }];
|
|
@@ -75,13 +124,21 @@ export class ReadonlyRequestCookies {
|
|
|
75
124
|
*/
|
|
76
125
|
export class ReadonlyHeaders {
|
|
77
126
|
h;
|
|
78
|
-
|
|
127
|
+
mut;
|
|
128
|
+
constructor(headers, mut) {
|
|
79
129
|
this.h = new Headers(headers);
|
|
130
|
+
this.mut = mut;
|
|
80
131
|
}
|
|
81
132
|
get(name) {
|
|
133
|
+
const staged = this.mut?.getStaged?.();
|
|
134
|
+
if (staged && name in staged)
|
|
135
|
+
return staged[name] ?? null;
|
|
82
136
|
return this.h.get(name);
|
|
83
137
|
}
|
|
84
138
|
has(name) {
|
|
139
|
+
const staged = this.mut?.getStaged?.();
|
|
140
|
+
if (staged && name in staged)
|
|
141
|
+
return true;
|
|
85
142
|
return this.h.has(name);
|
|
86
143
|
}
|
|
87
144
|
getSetCookie() {
|
|
@@ -93,13 +150,31 @@ export class ReadonlyHeaders {
|
|
|
93
150
|
return single ? [single] : [];
|
|
94
151
|
}
|
|
95
152
|
entries() {
|
|
96
|
-
|
|
153
|
+
const merged = new Headers(this.h);
|
|
154
|
+
const staged = this.mut?.getStaged?.();
|
|
155
|
+
if (staged) {
|
|
156
|
+
for (const [k, v] of Object.entries(staged))
|
|
157
|
+
merged.set(k, v);
|
|
158
|
+
}
|
|
159
|
+
return merged.entries();
|
|
97
160
|
}
|
|
98
161
|
keys() {
|
|
99
|
-
|
|
162
|
+
const merged = new Headers(this.h);
|
|
163
|
+
const staged = this.mut?.getStaged?.();
|
|
164
|
+
if (staged) {
|
|
165
|
+
for (const [k, v] of Object.entries(staged))
|
|
166
|
+
merged.set(k, v);
|
|
167
|
+
}
|
|
168
|
+
return merged.keys();
|
|
100
169
|
}
|
|
101
170
|
values() {
|
|
102
|
-
|
|
171
|
+
const merged = new Headers(this.h);
|
|
172
|
+
const staged = this.mut?.getStaged?.();
|
|
173
|
+
if (staged) {
|
|
174
|
+
for (const [k, v] of Object.entries(staged))
|
|
175
|
+
merged.set(k, v);
|
|
176
|
+
}
|
|
177
|
+
return merged.values();
|
|
103
178
|
}
|
|
104
179
|
forEach(callbackfn, thisArg) {
|
|
105
180
|
const self = this;
|
|
@@ -108,22 +183,53 @@ export class ReadonlyHeaders {
|
|
|
108
183
|
});
|
|
109
184
|
}
|
|
110
185
|
[Symbol.iterator]() {
|
|
111
|
-
return this.
|
|
186
|
+
return this.entries();
|
|
187
|
+
}
|
|
188
|
+
set(name, value) {
|
|
189
|
+
if (!this.mut)
|
|
190
|
+
throw new Error("headers().set is not available in this context");
|
|
191
|
+
this.mut.setHeader(name, value);
|
|
192
|
+
}
|
|
193
|
+
append(name, value) {
|
|
194
|
+
if (!this.mut)
|
|
195
|
+
throw new Error("headers().append is not available in this context");
|
|
196
|
+
this.mut.appendHeader(name, value);
|
|
197
|
+
}
|
|
198
|
+
delete(name) {
|
|
199
|
+
if (!this.mut)
|
|
200
|
+
throw new Error("headers().delete is not available in this context");
|
|
201
|
+
this.mut.deleteHeader(name);
|
|
112
202
|
}
|
|
113
203
|
}
|
|
114
204
|
/** Used by the server only; not part of the public package API. */
|
|
115
|
-
export function wrapLixnetRequest(request) {
|
|
205
|
+
export function wrapLixnetRequest(request, response) {
|
|
116
206
|
const snapshot = new Headers(request.headers);
|
|
207
|
+
const cookieMut = {
|
|
208
|
+
setCookie: (name, value, options) => response.cookie(name, value, options),
|
|
209
|
+
deleteCookie: (name) => response.deleteCookie(name),
|
|
210
|
+
getStaged: () => response.responseCookies,
|
|
211
|
+
};
|
|
212
|
+
const headerMut = {
|
|
213
|
+
setHeader: (name, value) => response.header(name, value),
|
|
214
|
+
appendHeader: (name, value) => {
|
|
215
|
+
const existing = response.responseHeaders[name] ??
|
|
216
|
+
response.responseHeaders[name.toLowerCase()] ??
|
|
217
|
+
"";
|
|
218
|
+
response.header(name, existing ? `${existing}, ${value}` : value);
|
|
219
|
+
},
|
|
220
|
+
deleteHeader: (name) => response.header(name, ""),
|
|
221
|
+
getStaged: () => response.responseHeaders,
|
|
222
|
+
};
|
|
117
223
|
return new Proxy(request, {
|
|
118
224
|
get(target, prop, receiver) {
|
|
119
225
|
if (prop === "cookies") {
|
|
120
226
|
return function cookies() {
|
|
121
|
-
return new ReadonlyRequestCookies(snapshot.get("cookie"));
|
|
227
|
+
return new ReadonlyRequestCookies(snapshot.get("cookie"), cookieMut);
|
|
122
228
|
};
|
|
123
229
|
}
|
|
124
230
|
if (prop === "headers") {
|
|
125
231
|
return function headers() {
|
|
126
|
-
return new ReadonlyHeaders(snapshot);
|
|
232
|
+
return new ReadonlyHeaders(snapshot, headerMut);
|
|
127
233
|
};
|
|
128
234
|
}
|
|
129
235
|
return Reflect.get(target, prop, receiver);
|
|
@@ -24,7 +24,7 @@ export declare class LixnetResponse {
|
|
|
24
24
|
error(error: string): void;
|
|
25
25
|
code(code: number): void;
|
|
26
26
|
headers(headers: Record<string, string>): void;
|
|
27
|
-
cookie(cookieName: string, cookieValue: string, cookieOptions
|
|
27
|
+
cookie(cookieName: string, cookieValue: string, cookieOptions?: CookieOptions): void;
|
|
28
28
|
deleteCookie(cookieName: string): void;
|
|
29
29
|
format: (this: LixnetResponse) => Response;
|
|
30
30
|
setFormatter(formatter: (this: LixnetResponse) => Response): void;
|