lixnet 1.3.4 → 1.3.5
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
|
@@ -66,9 +66,11 @@ export default class LixnetServer {
|
|
|
66
66
|
try {
|
|
67
67
|
const newRequest = wrapLixnetRequest(requestClone, {
|
|
68
68
|
header: response.header.bind(response),
|
|
69
|
+
deleteHeader: response.deleteHeader.bind(response),
|
|
69
70
|
cookie: response.cookie.bind(response),
|
|
70
71
|
deleteCookie: response.deleteCookie.bind(response),
|
|
71
72
|
responseHeaders: response.responseHeaders,
|
|
73
|
+
responseHeaderDeletes: response.responseHeaderDeletes,
|
|
72
74
|
responseCookies: response.responseCookies,
|
|
73
75
|
});
|
|
74
76
|
const data = await event.handler({
|
|
@@ -7,16 +7,24 @@ export default function (defaultHeaders) {
|
|
|
7
7
|
for (const [headerName, headerValue] of Object.entries(this.responseHeaders)) {
|
|
8
8
|
headers.set(headerName, headerValue);
|
|
9
9
|
}
|
|
10
|
+
for (const headerName of this.responseHeaderDeletes) {
|
|
11
|
+
headers.delete(headerName);
|
|
12
|
+
}
|
|
10
13
|
for (const [cookieName, cookie] of Object.entries(this.responseCookies)) {
|
|
11
14
|
if (cookie.type === "value") {
|
|
12
15
|
const opts = cookie.options ?? {};
|
|
16
|
+
//detect value escapes
|
|
17
|
+
if (cookie.value?.includes(";") || cookie.value?.includes("=") || cookie.value?.includes("\n") || cookie.value?.includes("\r")) {
|
|
18
|
+
throw new Error("Cookie value contains invalid characters");
|
|
19
|
+
}
|
|
13
20
|
const parts = [`${cookieName}=${cookie.value ?? ""}`];
|
|
14
21
|
if (opts.domain)
|
|
15
22
|
parts.push(`Domain=${opts.domain}`);
|
|
16
23
|
if (opts.path)
|
|
17
24
|
parts.push(`Path=${opts.path}`);
|
|
18
|
-
if (typeof opts.maxAge === "number")
|
|
25
|
+
if ("maxAge" in opts && typeof opts.maxAge === "number") {
|
|
19
26
|
parts.push(`Max-Age=${opts.maxAge}`);
|
|
27
|
+
}
|
|
20
28
|
if (opts.httpOnly)
|
|
21
29
|
parts.push("HttpOnly");
|
|
22
30
|
if (opts.secure)
|
|
@@ -26,7 +34,18 @@ export default function (defaultHeaders) {
|
|
|
26
34
|
headers.append("Set-Cookie", parts.join("; "));
|
|
27
35
|
}
|
|
28
36
|
else if (cookie.type === "delete") {
|
|
29
|
-
|
|
37
|
+
const opts = (cookie.options ?? {});
|
|
38
|
+
const parts = [`${cookieName}=`, "Max-Age=0"];
|
|
39
|
+
parts.push(`Path=${opts.path ?? "/"}`);
|
|
40
|
+
if (opts.domain)
|
|
41
|
+
parts.push(`Domain=${opts.domain}`);
|
|
42
|
+
if (opts.httpOnly)
|
|
43
|
+
parts.push("HttpOnly");
|
|
44
|
+
if (opts.secure)
|
|
45
|
+
parts.push("Secure");
|
|
46
|
+
if (opts.sameSite)
|
|
47
|
+
parts.push(`SameSite=${opts.sameSite}`);
|
|
48
|
+
headers.append("Set-Cookie", parts.join("; "));
|
|
30
49
|
}
|
|
31
50
|
else {
|
|
32
51
|
throw new Error(`Unknown cookie type: ${cookie.type}`);
|
|
@@ -35,7 +54,7 @@ export default function (defaultHeaders) {
|
|
|
35
54
|
return Response.json({
|
|
36
55
|
data: this.responseData,
|
|
37
56
|
}, {
|
|
38
|
-
status: 200,
|
|
57
|
+
status: this.responseCode ?? 200,
|
|
39
58
|
headers: headers,
|
|
40
59
|
});
|
|
41
60
|
}
|
|
@@ -10,7 +10,7 @@ export declare class ReadonlyRequestCookies {
|
|
|
10
10
|
private readonly mut?;
|
|
11
11
|
constructor(cookieHeader: string | null, mut?: {
|
|
12
12
|
setCookie: (name: string, value: string, options?: unknown) => void;
|
|
13
|
-
deleteCookie: (name: string) => void;
|
|
13
|
+
deleteCookie: (name: string, options?: unknown) => void;
|
|
14
14
|
getStaged: () => Record<string, {
|
|
15
15
|
type: "value" | "delete";
|
|
16
16
|
value?: string;
|
|
@@ -23,7 +23,7 @@ export declare class ReadonlyRequestCookies {
|
|
|
23
23
|
getAll(name: string): RequestCookie[];
|
|
24
24
|
has(name: string): boolean;
|
|
25
25
|
set(name: string, value: string, options?: unknown): void;
|
|
26
|
-
delete(name: string): void;
|
|
26
|
+
delete(name: string, options?: unknown): void;
|
|
27
27
|
[Symbol.iterator](): IterableIterator<[string, RequestCookie]>;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
@@ -58,9 +58,11 @@ export type LixnetRequest = Omit<Request, "headers"> & {
|
|
|
58
58
|
/** Used by the server only; not part of the public package API. */
|
|
59
59
|
export declare function wrapLixnetRequest(request: Request, response: {
|
|
60
60
|
header: (name: string, value: string) => void;
|
|
61
|
+
deleteHeader: (name: string) => void;
|
|
61
62
|
cookie: (name: string, value: string, options?: any) => void;
|
|
62
|
-
deleteCookie: (name: string) => void;
|
|
63
|
+
deleteCookie: (name: string, options?: any) => void;
|
|
63
64
|
responseHeaders: Record<string, string>;
|
|
65
|
+
responseHeaderDeletes: Set<string>;
|
|
64
66
|
responseCookies: Record<string, {
|
|
65
67
|
type: "value" | "delete";
|
|
66
68
|
value?: string;
|
package/dist/lib/util/request.js
CHANGED
|
@@ -93,11 +93,11 @@ export class ReadonlyRequestCookies {
|
|
|
93
93
|
}
|
|
94
94
|
this.mut.setCookie(name, value, options);
|
|
95
95
|
}
|
|
96
|
-
delete(name) {
|
|
96
|
+
delete(name, options) {
|
|
97
97
|
if (!this.mut) {
|
|
98
98
|
throw new Error("cookies().delete is not available in this context");
|
|
99
99
|
}
|
|
100
|
-
this.mut.deleteCookie(name);
|
|
100
|
+
this.mut.deleteCookie(name, options);
|
|
101
101
|
}
|
|
102
102
|
[Symbol.iterator]() {
|
|
103
103
|
const map = new Map(this.map);
|
|
@@ -206,19 +206,28 @@ export function wrapLixnetRequest(request, response) {
|
|
|
206
206
|
const snapshot = new Headers(request.headers);
|
|
207
207
|
const cookieMut = {
|
|
208
208
|
setCookie: (name, value, options) => response.cookie(name, value, options),
|
|
209
|
-
deleteCookie: (name) => response.deleteCookie(name),
|
|
209
|
+
deleteCookie: (name, options) => response.deleteCookie(name, options),
|
|
210
210
|
getStaged: () => response.responseCookies,
|
|
211
211
|
};
|
|
212
212
|
const headerMut = {
|
|
213
213
|
setHeader: (name, value) => response.header(name, value),
|
|
214
214
|
appendHeader: (name, value) => {
|
|
215
|
+
if (response.responseHeaderDeletes.has(name.toLowerCase())) {
|
|
216
|
+
response.responseHeaderDeletes.delete(name.toLowerCase());
|
|
217
|
+
}
|
|
215
218
|
const existing = response.responseHeaders[name] ??
|
|
216
219
|
response.responseHeaders[name.toLowerCase()] ??
|
|
217
220
|
"";
|
|
218
221
|
response.header(name, existing ? `${existing}, ${value}` : value);
|
|
219
222
|
},
|
|
220
|
-
deleteHeader: (name) => response.
|
|
221
|
-
getStaged: () =>
|
|
223
|
+
deleteHeader: (name) => response.deleteHeader(name),
|
|
224
|
+
getStaged: () => {
|
|
225
|
+
const out = { ...response.responseHeaders };
|
|
226
|
+
for (const k of response.responseHeaderDeletes) {
|
|
227
|
+
delete out[k];
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
},
|
|
222
231
|
};
|
|
223
232
|
return new Proxy(request, {
|
|
224
233
|
get(target, prop, receiver) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type CookieOptions = {
|
|
1
|
+
export type CookieOptions = {
|
|
2
2
|
domain?: string;
|
|
3
3
|
path?: string;
|
|
4
4
|
maxAge?: number;
|
|
@@ -6,27 +6,35 @@ type CookieOptions = {
|
|
|
6
6
|
secure?: boolean;
|
|
7
7
|
sameSite?: 'lax' | 'strict' | 'none';
|
|
8
8
|
};
|
|
9
|
+
export type CookieDeleteOptions = {
|
|
10
|
+
domain?: string;
|
|
11
|
+
path?: string;
|
|
12
|
+
httpOnly?: boolean;
|
|
13
|
+
secure?: boolean;
|
|
14
|
+
sameSite?: 'lax' | 'strict' | 'none';
|
|
15
|
+
};
|
|
9
16
|
export declare class LixnetResponse {
|
|
10
17
|
responseData: any;
|
|
11
18
|
responseError: string | null;
|
|
12
19
|
responseCode: number;
|
|
13
20
|
responseHeaders: Record<string, string>;
|
|
21
|
+
responseHeaderDeletes: Set<string>;
|
|
14
22
|
responseCookies: Record<string, {
|
|
15
23
|
type: "value" | "delete";
|
|
16
24
|
value?: string;
|
|
17
|
-
options?: CookieOptions;
|
|
25
|
+
options?: CookieOptions | CookieDeleteOptions;
|
|
18
26
|
}>;
|
|
19
27
|
constructor({ formatter }: {
|
|
20
28
|
formatter: (this: LixnetResponse) => Response;
|
|
21
29
|
});
|
|
22
30
|
data(data: any): void;
|
|
23
31
|
header(headerName: string, headerValue: string): void;
|
|
32
|
+
deleteHeader(headerName: string): void;
|
|
24
33
|
error(error: string): void;
|
|
25
34
|
code(code: number): void;
|
|
26
35
|
headers(headers: Record<string, string>): void;
|
|
27
36
|
cookie(cookieName: string, cookieValue: string, cookieOptions?: CookieOptions): void;
|
|
28
|
-
deleteCookie(cookieName: string): void;
|
|
37
|
+
deleteCookie(cookieName: string, options?: CookieDeleteOptions): void;
|
|
29
38
|
format: (this: LixnetResponse) => Response;
|
|
30
39
|
setFormatter(formatter: (this: LixnetResponse) => Response): void;
|
|
31
40
|
}
|
|
32
|
-
export {};
|
|
@@ -3,6 +3,7 @@ export class LixnetResponse {
|
|
|
3
3
|
responseError = null;
|
|
4
4
|
responseCode = 200;
|
|
5
5
|
responseHeaders = {};
|
|
6
|
+
responseHeaderDeletes = new Set();
|
|
6
7
|
responseCookies = {};
|
|
7
8
|
constructor({ formatter }) {
|
|
8
9
|
this.responseData = null;
|
|
@@ -13,6 +14,12 @@ export class LixnetResponse {
|
|
|
13
14
|
}
|
|
14
15
|
header(headerName, headerValue) {
|
|
15
16
|
this.responseHeaders[headerName] = headerValue;
|
|
17
|
+
this.responseHeaderDeletes.delete(headerName.toLowerCase());
|
|
18
|
+
}
|
|
19
|
+
deleteHeader(headerName) {
|
|
20
|
+
delete this.responseHeaders[headerName];
|
|
21
|
+
delete this.responseHeaders[headerName.toLowerCase()];
|
|
22
|
+
this.responseHeaderDeletes.add(headerName.toLowerCase());
|
|
16
23
|
}
|
|
17
24
|
error(error) {
|
|
18
25
|
this.responseError = error;
|
|
@@ -32,9 +39,10 @@ export class LixnetResponse {
|
|
|
32
39
|
options: cookieOptions,
|
|
33
40
|
};
|
|
34
41
|
}
|
|
35
|
-
deleteCookie(cookieName) {
|
|
42
|
+
deleteCookie(cookieName, options) {
|
|
36
43
|
this.responseCookies[cookieName] = {
|
|
37
44
|
type: "delete",
|
|
45
|
+
options,
|
|
38
46
|
};
|
|
39
47
|
}
|
|
40
48
|
format;
|