lixnet 1.3.3 → 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
|
@@ -64,7 +64,15 @@ 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
|
+
deleteHeader: response.deleteHeader.bind(response),
|
|
70
|
+
cookie: response.cookie.bind(response),
|
|
71
|
+
deleteCookie: response.deleteCookie.bind(response),
|
|
72
|
+
responseHeaders: response.responseHeaders,
|
|
73
|
+
responseHeaderDeletes: response.responseHeaderDeletes,
|
|
74
|
+
responseCookies: response.responseCookies,
|
|
75
|
+
});
|
|
68
76
|
const data = await event.handler({
|
|
69
77
|
...validatedInput,
|
|
70
78
|
request: newRequest,
|
|
@@ -7,13 +7,45 @@ 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 ?? {};
|
|
13
|
-
|
|
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
|
+
}
|
|
20
|
+
const parts = [`${cookieName}=${cookie.value ?? ""}`];
|
|
21
|
+
if (opts.domain)
|
|
22
|
+
parts.push(`Domain=${opts.domain}`);
|
|
23
|
+
if (opts.path)
|
|
24
|
+
parts.push(`Path=${opts.path}`);
|
|
25
|
+
if ("maxAge" in opts && typeof opts.maxAge === "number") {
|
|
26
|
+
parts.push(`Max-Age=${opts.maxAge}`);
|
|
27
|
+
}
|
|
28
|
+
if (opts.httpOnly)
|
|
29
|
+
parts.push("HttpOnly");
|
|
30
|
+
if (opts.secure)
|
|
31
|
+
parts.push("Secure");
|
|
32
|
+
if (opts.sameSite)
|
|
33
|
+
parts.push(`SameSite=${opts.sameSite}`);
|
|
34
|
+
headers.append("Set-Cookie", parts.join("; "));
|
|
14
35
|
}
|
|
15
36
|
else if (cookie.type === "delete") {
|
|
16
|
-
|
|
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("; "));
|
|
17
49
|
}
|
|
18
50
|
else {
|
|
19
51
|
throw new Error(`Unknown cookie type: ${cookie.type}`);
|
|
@@ -22,7 +54,7 @@ export default function (defaultHeaders) {
|
|
|
22
54
|
return Response.json({
|
|
23
55
|
data: this.responseData,
|
|
24
56
|
}, {
|
|
25
|
-
status: 200,
|
|
57
|
+
status: this.responseCode ?? 200,
|
|
26
58
|
headers: headers,
|
|
27
59
|
});
|
|
28
60
|
}
|
|
@@ -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, options?: unknown) => 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, options?: unknown): 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,25 @@ 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
|
+
deleteHeader: (name: string) => void;
|
|
62
|
+
cookie: (name: string, value: string, options?: any) => void;
|
|
63
|
+
deleteCookie: (name: string, options?: any) => void;
|
|
64
|
+
responseHeaders: Record<string, string>;
|
|
65
|
+
responseHeaderDeletes: Set<string>;
|
|
66
|
+
responseCookies: Record<string, {
|
|
67
|
+
type: "value" | "delete";
|
|
68
|
+
value?: string;
|
|
69
|
+
options?: unknown;
|
|
70
|
+
}>;
|
|
71
|
+
}): 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, options) {
|
|
97
|
+
if (!this.mut) {
|
|
98
|
+
throw new Error("cookies().delete is not available in this context");
|
|
99
|
+
}
|
|
100
|
+
this.mut.deleteCookie(name, options);
|
|
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,62 @@ 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, options) => response.deleteCookie(name, options),
|
|
210
|
+
getStaged: () => response.responseCookies,
|
|
211
|
+
};
|
|
212
|
+
const headerMut = {
|
|
213
|
+
setHeader: (name, value) => response.header(name, value),
|
|
214
|
+
appendHeader: (name, value) => {
|
|
215
|
+
if (response.responseHeaderDeletes.has(name.toLowerCase())) {
|
|
216
|
+
response.responseHeaderDeletes.delete(name.toLowerCase());
|
|
217
|
+
}
|
|
218
|
+
const existing = response.responseHeaders[name] ??
|
|
219
|
+
response.responseHeaders[name.toLowerCase()] ??
|
|
220
|
+
"";
|
|
221
|
+
response.header(name, existing ? `${existing}, ${value}` : value);
|
|
222
|
+
},
|
|
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
|
+
},
|
|
231
|
+
};
|
|
117
232
|
return new Proxy(request, {
|
|
118
233
|
get(target, prop, receiver) {
|
|
119
234
|
if (prop === "cookies") {
|
|
120
235
|
return function cookies() {
|
|
121
|
-
return new ReadonlyRequestCookies(snapshot.get("cookie"));
|
|
236
|
+
return new ReadonlyRequestCookies(snapshot.get("cookie"), cookieMut);
|
|
122
237
|
};
|
|
123
238
|
}
|
|
124
239
|
if (prop === "headers") {
|
|
125
240
|
return function headers() {
|
|
126
|
-
return new ReadonlyHeaders(snapshot);
|
|
241
|
+
return new ReadonlyHeaders(snapshot, headerMut);
|
|
127
242
|
};
|
|
128
243
|
}
|
|
129
244
|
return Reflect.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
|
-
cookie(cookieName: string, cookieValue: string, cookieOptions
|
|
28
|
-
deleteCookie(cookieName: string): void;
|
|
36
|
+
cookie(cookieName: string, cookieValue: string, cookieOptions?: CookieOptions): 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;
|