lixnet 1.3.4 → 1.3.6

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/exports.d.ts CHANGED
@@ -3,5 +3,5 @@ import LixnetClient from "./lib/client";
3
3
  import LixnetPeer from "./lib/peer";
4
4
  import { LixnetResponse } from "./lib/util/response";
5
5
  export { LixnetServer, LixnetClient, LixnetPeer, LixnetResponse };
6
- export type { LixnetRequest, ReadonlyHeaders, ReadonlyRequestCookies, RequestCookie, } from "./lib/util/request";
6
+ export type { LixnetCookies, LixnetHeaders, LixnetRequest, ReadonlyHeaders, ReadonlyRequestCookies, RequestCookie, } from "./lib/util/request";
7
7
  export type { LXN_ServerClient_EventType, LXNServerHandler, FunctionInput, LXN_ServerClient_Request, LixnetServerInjections, } from "./lib/types";
@@ -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
- headers.append('Set-Cookie', `${cookieName}=; Max-Age=0; Path=/; HttpOnly; Secure; SameSite=Strict`);
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
  }
@@ -3,14 +3,14 @@ export type RequestCookie = {
3
3
  value: string;
4
4
  };
5
5
  /**
6
- * Read-only view of request cookies (Next.js `ReadonlyRequestCookies`).
6
+ * Cookie API exposed by `LixnetRequest`: read from the request snapshot, write via `LixnetResponse`.
7
7
  */
8
- export declare class ReadonlyRequestCookies {
8
+ export declare class LixnetCookies {
9
9
  private readonly map;
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,14 +23,13 @@ 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
  /**
30
- * Read-only `Headers` view (Next.js `ReadonlyHeaders`).
31
- * Wraps a cloned `Headers` instance so callers cannot mutate the snapshot.
30
+ * Headers API exposed by `LixnetRequest`: read from the request snapshot, write via `LixnetResponse`.
32
31
  */
33
- export declare class ReadonlyHeaders {
32
+ export declare class LixnetHeaders {
34
33
  private readonly h;
35
34
  private readonly mut?;
36
35
  constructor(headers: Headers, mut?: {
@@ -45,25 +44,31 @@ export declare class ReadonlyHeaders {
45
44
  entries(): IterableIterator<[string, string]>;
46
45
  keys(): IterableIterator<string>;
47
46
  values(): IterableIterator<string>;
48
- forEach(callbackfn: (value: string, name: string, parent: ReadonlyHeaders) => void, thisArg?: unknown): void;
47
+ forEach(callbackfn: (value: string, name: string, parent: LixnetHeaders) => void, thisArg?: unknown): void;
49
48
  [Symbol.iterator](): IterableIterator<[string, string]>;
50
49
  set(name: string, value: string): void;
51
50
  append(name: string, value: string): void;
52
51
  delete(name: string): void;
53
52
  }
54
53
  export type LixnetRequest = Omit<Request, "headers"> & {
55
- cookies(): ReadonlyRequestCookies;
56
- headers(): ReadonlyHeaders;
54
+ cookies(): LixnetCookies;
55
+ headers(): LixnetHeaders;
57
56
  };
58
57
  /** Used by the server only; not part of the public package API. */
59
58
  export declare function wrapLixnetRequest(request: Request, response: {
60
59
  header: (name: string, value: string) => void;
60
+ deleteHeader: (name: string) => void;
61
61
  cookie: (name: string, value: string, options?: any) => void;
62
- deleteCookie: (name: string) => void;
62
+ deleteCookie: (name: string, options?: any) => void;
63
63
  responseHeaders: Record<string, string>;
64
+ responseHeaderDeletes: Set<string>;
64
65
  responseCookies: Record<string, {
65
66
  type: "value" | "delete";
66
67
  value?: string;
67
68
  options?: unknown;
68
69
  }>;
69
70
  }): LixnetRequest;
71
+ /** @deprecated Use {@link LixnetCookies}. */
72
+ export type ReadonlyRequestCookies = LixnetCookies;
73
+ /** @deprecated Use {@link LixnetHeaders}. */
74
+ export type ReadonlyHeaders = LixnetHeaders;
@@ -28,9 +28,9 @@ function parseCookieHeader(header) {
28
28
  return map;
29
29
  }
30
30
  /**
31
- * Read-only view of request cookies (Next.js `ReadonlyRequestCookies`).
31
+ * Cookie API exposed by `LixnetRequest`: read from the request snapshot, write via `LixnetResponse`.
32
32
  */
33
- export class ReadonlyRequestCookies {
33
+ export class LixnetCookies {
34
34
  map;
35
35
  mut;
36
36
  constructor(cookieHeader, mut) {
@@ -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);
@@ -119,10 +119,9 @@ export class ReadonlyRequestCookies {
119
119
  }
120
120
  }
121
121
  /**
122
- * Read-only `Headers` view (Next.js `ReadonlyHeaders`).
123
- * Wraps a cloned `Headers` instance so callers cannot mutate the snapshot.
122
+ * Headers API exposed by `LixnetRequest`: read from the request snapshot, write via `LixnetResponse`.
124
123
  */
125
- export class ReadonlyHeaders {
124
+ export class LixnetHeaders {
126
125
  h;
127
126
  mut;
128
127
  constructor(headers, mut) {
@@ -178,7 +177,13 @@ export class ReadonlyHeaders {
178
177
  }
179
178
  forEach(callbackfn, thisArg) {
180
179
  const self = this;
181
- this.h.forEach(function (value, name) {
180
+ const merged = new Headers(this.h);
181
+ const staged = this.mut?.getStaged?.();
182
+ if (staged) {
183
+ for (const [k, v] of Object.entries(staged))
184
+ merged.set(k, v);
185
+ }
186
+ merged.forEach(function (value, name) {
182
187
  callbackfn.call(thisArg ?? self, value, name, self);
183
188
  });
184
189
  }
@@ -206,30 +211,39 @@ export function wrapLixnetRequest(request, response) {
206
211
  const snapshot = new Headers(request.headers);
207
212
  const cookieMut = {
208
213
  setCookie: (name, value, options) => response.cookie(name, value, options),
209
- deleteCookie: (name) => response.deleteCookie(name),
214
+ deleteCookie: (name, options) => response.deleteCookie(name, options),
210
215
  getStaged: () => response.responseCookies,
211
216
  };
212
217
  const headerMut = {
213
218
  setHeader: (name, value) => response.header(name, value),
214
219
  appendHeader: (name, value) => {
220
+ if (response.responseHeaderDeletes.has(name.toLowerCase())) {
221
+ response.responseHeaderDeletes.delete(name.toLowerCase());
222
+ }
215
223
  const existing = response.responseHeaders[name] ??
216
224
  response.responseHeaders[name.toLowerCase()] ??
217
225
  "";
218
226
  response.header(name, existing ? `${existing}, ${value}` : value);
219
227
  },
220
- deleteHeader: (name) => response.header(name, ""),
221
- getStaged: () => response.responseHeaders,
228
+ deleteHeader: (name) => response.deleteHeader(name),
229
+ getStaged: () => {
230
+ const out = { ...response.responseHeaders };
231
+ for (const k of response.responseHeaderDeletes) {
232
+ delete out[k];
233
+ }
234
+ return out;
235
+ },
222
236
  };
223
237
  return new Proxy(request, {
224
238
  get(target, prop, receiver) {
225
239
  if (prop === "cookies") {
226
240
  return function cookies() {
227
- return new ReadonlyRequestCookies(snapshot.get("cookie"), cookieMut);
241
+ return new LixnetCookies(snapshot.get("cookie"), cookieMut);
228
242
  };
229
243
  }
230
244
  if (prop === "headers") {
231
245
  return function headers() {
232
- return new ReadonlyHeaders(snapshot, headerMut);
246
+ return new LixnetHeaders(snapshot, headerMut);
233
247
  };
234
248
  }
235
249
  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
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lixnet",
3
- "version": "1.3.4",
3
+ "version": "1.3.6",
4
4
  "author": "nitlix",
5
5
  "license": "MIT",
6
6
  "repository": {