lixnet 1.2.7 → 1.2.9
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/.gitattributes +1 -1
- package/dist/exports.d.ts +4 -1
- package/dist/exports.js +3 -1
- package/dist/lib/client.js +1 -0
- package/dist/lib/peer.js +57 -59
- package/dist/lib/server.d.ts +4 -3
- package/dist/lib/server.js +39 -36
- package/dist/lib/types.d.ts +2 -3
- package/dist/lib/util/formatter.d.ts +2 -0
- package/dist/lib/util/formatter.js +26 -0
- package/dist/lib/util/request.d.ts +38 -0
- package/dist/lib/util/request.js +134 -0
- package/dist/lib/util/response.d.ts +32 -0
- package/dist/lib/util/response.js +44 -0
- package/package.json +4 -4
package/.gitattributes
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
# Auto detect text files and perform LF normalization
|
|
1
|
+
# Auto detect text files and perform LF normalization
|
|
2
2
|
* text=auto
|
package/dist/exports.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import LixnetServer from "./lib/server";
|
|
2
2
|
import LixnetClient from "./lib/client";
|
|
3
3
|
import LixnetPeer from "./lib/peer";
|
|
4
|
-
|
|
4
|
+
import LixnetResponse from "./lib/util/response";
|
|
5
|
+
export { LixnetServer, LixnetClient, LixnetPeer, LixnetResponse };
|
|
6
|
+
export { default as LixnetRequest } from "./lib/util/request";
|
|
7
|
+
export type { ReadonlyHeaders, ReadonlyRequestCookies, RequestCookie, } from "./lib/util/request";
|
|
5
8
|
export type { LXN_ServerClient_EventType, LXNServerHandler, FunctionInput, LXN_ServerClient_Request, } from "./lib/types";
|
package/dist/exports.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import LixnetServer from "./lib/server";
|
|
2
2
|
import LixnetClient from "./lib/client";
|
|
3
3
|
import LixnetPeer from "./lib/peer";
|
|
4
|
-
|
|
4
|
+
import LixnetResponse from "./lib/util/response";
|
|
5
|
+
export { LixnetServer, LixnetClient, LixnetPeer, LixnetResponse };
|
|
6
|
+
export { default as LixnetRequest } from "./lib/util/request";
|
package/dist/lib/client.js
CHANGED
package/dist/lib/peer.js
CHANGED
|
@@ -2,70 +2,65 @@ function createLixnetId() {
|
|
|
2
2
|
return "LX-" + Math.random().toString(36).substring(2, 15);
|
|
3
3
|
}
|
|
4
4
|
export default class LixnetPeer {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
5
|
+
socket = null;
|
|
6
|
+
rb = {};
|
|
7
|
+
transmissionLimit = -1;
|
|
8
|
+
transmissionChunksLimit = 20;
|
|
9
|
+
setTransmissionChunksLimit(limit) {
|
|
10
|
+
this.transmissionChunksLimit = limit;
|
|
11
|
+
}
|
|
12
|
+
call = async (event, data, { callback } = { callback: null }) => {
|
|
13
|
+
if (!this.socket)
|
|
14
|
+
throw new Error("LIXNET - Socket not set");
|
|
15
|
+
let rf = null;
|
|
16
|
+
if (typeof callback === "function") {
|
|
17
|
+
rf = createLixnetId();
|
|
18
|
+
this.rb[rf] = {
|
|
19
|
+
method: event,
|
|
20
|
+
callback: callback,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
let dataToSend = {
|
|
24
|
+
event,
|
|
25
|
+
data,
|
|
26
|
+
};
|
|
27
|
+
if (rf)
|
|
28
|
+
dataToSend.rf = rf;
|
|
29
|
+
dataToSend = JSON.stringify(dataToSend);
|
|
30
|
+
// No callback needed, just send
|
|
31
|
+
if (this.transmissionLimit !== -1 &&
|
|
32
|
+
dataToSend.length > this.transmissionLimit - 50) {
|
|
33
|
+
// Send in chunks
|
|
34
|
+
let dataString = JSON.stringify(data);
|
|
35
|
+
const tId = createLixnetId();
|
|
36
|
+
const limits = this.getChunkLimits({
|
|
37
|
+
tId,
|
|
38
|
+
dataLength: dataString.length,
|
|
39
|
+
});
|
|
40
|
+
// Send transmission id and rf
|
|
41
|
+
const sendData = {
|
|
42
|
+
tId,
|
|
22
43
|
event,
|
|
23
|
-
|
|
44
|
+
chunkCount: limits.length,
|
|
24
45
|
};
|
|
25
46
|
if (rf)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const tId = createLixnetId();
|
|
34
|
-
const limits = this.getChunkLimits({
|
|
35
|
-
tId,
|
|
36
|
-
dataLength: dataString.length,
|
|
37
|
-
});
|
|
38
|
-
// Send transmission id and rf
|
|
39
|
-
const sendData = {
|
|
40
|
-
tId,
|
|
41
|
-
event,
|
|
42
|
-
chunkCount: limits.length,
|
|
43
|
-
};
|
|
44
|
-
if (rf)
|
|
45
|
-
sendData.rf = rf;
|
|
46
|
-
this.socket.send(JSON.stringify(sendData));
|
|
47
|
-
const chunks = {};
|
|
48
|
-
for (let i = 0; i < limits.length; i++) {
|
|
49
|
-
if (i === limits.length - 1) {
|
|
50
|
-
chunks[i] = `${tId}.${i}.${dataString}`;
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
chunks[i] = `${tId}.${i}.${dataString.slice(0, limits[i])}`;
|
|
54
|
-
dataString = dataString.slice(limits[i]);
|
|
47
|
+
sendData.rf = rf;
|
|
48
|
+
this.socket.send(JSON.stringify(sendData));
|
|
49
|
+
const chunks = {};
|
|
50
|
+
for (let i = 0; i < limits.length; i++) {
|
|
51
|
+
if (i === limits.length - 1) {
|
|
52
|
+
chunks[i] = `${tId}.${i}.${dataString}`;
|
|
53
|
+
break;
|
|
55
54
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return;
|
|
55
|
+
chunks[i] = `${tId}.${i}.${dataString.slice(0, limits[i])}`;
|
|
56
|
+
dataString = dataString.slice(limits[i]);
|
|
59
57
|
}
|
|
60
|
-
this.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
this.
|
|
65
|
-
}
|
|
66
|
-
setTransmissionChunksLimit(limit) {
|
|
67
|
-
this.transmissionChunksLimit = limit;
|
|
68
|
-
}
|
|
58
|
+
this.outgoingTransmissions[tId] = chunks;
|
|
59
|
+
// Chunks sent. Complete.
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.socket.send(dataToSend);
|
|
63
|
+
};
|
|
69
64
|
getChunkLimits({ tId, dataLength, }) {
|
|
70
65
|
const limits = [];
|
|
71
66
|
let chunkIndex = 0;
|
|
@@ -84,6 +79,7 @@ export default class LixnetPeer {
|
|
|
84
79
|
}
|
|
85
80
|
return limits;
|
|
86
81
|
}
|
|
82
|
+
eventHandlers = {};
|
|
87
83
|
setSocket(socket) {
|
|
88
84
|
this.socket = socket;
|
|
89
85
|
}
|
|
@@ -93,6 +89,8 @@ export default class LixnetPeer {
|
|
|
93
89
|
on(event, handler) {
|
|
94
90
|
this.eventHandlers[event] = handler;
|
|
95
91
|
}
|
|
92
|
+
transmissions = {};
|
|
93
|
+
outgoingTransmissions = {};
|
|
96
94
|
transmissionSecurityCheck(tId) {
|
|
97
95
|
const transmission = this.transmissions[tId];
|
|
98
96
|
if (!transmission)
|
package/dist/lib/server.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import type { DebugLogger, FunctionInput, FunctionOutput, LXNServerHandler, LXN_ServerClient_EventType, LXN_ServerClient_Request } from "./types";
|
|
3
|
+
import LixnetResponse from "./util/response";
|
|
3
4
|
type LXNServerEventInput<Events extends LXN_ServerClient_EventType, TName extends keyof Events> = {
|
|
4
5
|
event: TName;
|
|
5
6
|
handler: LXNServerHandler<FunctionInput<Events[TName]> & {
|
|
@@ -11,11 +12,11 @@ export default class LixnetServer<Events extends LXN_ServerClient_EventType> {
|
|
|
11
12
|
private events;
|
|
12
13
|
private logger;
|
|
13
14
|
private debugLog;
|
|
14
|
-
private
|
|
15
|
-
constructor({ debugLog, logger,
|
|
15
|
+
private formatter;
|
|
16
|
+
constructor({ debugLog, logger, formatter, }: {
|
|
16
17
|
debugLog?: boolean;
|
|
17
18
|
logger?: DebugLogger;
|
|
18
|
-
|
|
19
|
+
formatter?: (this: LixnetResponse) => Response;
|
|
19
20
|
});
|
|
20
21
|
on<K extends keyof Events & string>(input: LXNServerEventInput<Events, K> | LXNServerEventInput<Events, K>[]): void;
|
|
21
22
|
handle(request: Request): Promise<Response>;
|
package/dist/lib/server.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import LixnetLog from "./util/log";
|
|
3
|
+
import LixnetResponse from "./util/response";
|
|
4
|
+
import defaultFormatter from "./util/formatter";
|
|
5
|
+
import LixnetRequest from "./util/request";
|
|
3
6
|
export default class LixnetServer {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
events = {};
|
|
8
|
+
logger = LixnetLog;
|
|
9
|
+
debugLog = false;
|
|
10
|
+
formatter;
|
|
11
|
+
constructor({ debugLog = false, logger, formatter, }) {
|
|
8
12
|
this.debugLog = debugLog;
|
|
9
13
|
logger ? (this.logger = logger) : "";
|
|
10
|
-
this.
|
|
14
|
+
this.formatter = formatter ?? defaultFormatter;
|
|
11
15
|
}
|
|
12
16
|
on(input) {
|
|
13
17
|
if (Array.isArray(input)) {
|
|
@@ -21,6 +25,7 @@ export default class LixnetServer {
|
|
|
21
25
|
}
|
|
22
26
|
async handle(request) {
|
|
23
27
|
const requestClone = request.clone();
|
|
28
|
+
const response = new LixnetResponse({ formatter: this.formatter });
|
|
24
29
|
let jsonData;
|
|
25
30
|
try {
|
|
26
31
|
jsonData = (await request.json());
|
|
@@ -32,59 +37,57 @@ export default class LixnetServer {
|
|
|
32
37
|
message: "Invalid JSON",
|
|
33
38
|
})
|
|
34
39
|
: "";
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
response.error("Invalid JSON");
|
|
41
|
+
return response.format();
|
|
42
|
+
}
|
|
43
|
+
if (jsonData === null ||
|
|
44
|
+
typeof jsonData !== "object" ||
|
|
45
|
+
Array.isArray(jsonData)) {
|
|
46
|
+
response.error("Invalid request body");
|
|
47
|
+
return response.format();
|
|
38
48
|
}
|
|
39
49
|
if (!jsonData.event) {
|
|
40
|
-
|
|
50
|
+
response.error("Event not found");
|
|
51
|
+
return response.format();
|
|
41
52
|
}
|
|
42
|
-
if (!jsonData
|
|
43
|
-
|
|
53
|
+
if (!("input" in jsonData)) {
|
|
54
|
+
response.error("Input not found");
|
|
55
|
+
return response.format();
|
|
44
56
|
}
|
|
45
57
|
const event = this.events[jsonData.event];
|
|
46
58
|
if (!event) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}, { status: 404 });
|
|
59
|
+
response.error("Event not found");
|
|
60
|
+
return response.format();
|
|
50
61
|
}
|
|
51
62
|
try {
|
|
52
63
|
const validatedInput = event.schema
|
|
53
64
|
? event.schema.parse(jsonData.input)
|
|
54
65
|
: jsonData.input;
|
|
55
66
|
try {
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
clonedReq.setAdditionalInit = (init) => {
|
|
59
|
-
additionalInit = init;
|
|
60
|
-
};
|
|
61
|
-
const newRequest = clonedReq;
|
|
62
|
-
const result = await event.handler({
|
|
63
|
-
request: newRequest,
|
|
67
|
+
const newRequest = LixnetRequest(requestClone);
|
|
68
|
+
const data = await event.handler({
|
|
64
69
|
...validatedInput,
|
|
70
|
+
request: newRequest,
|
|
71
|
+
response: response,
|
|
65
72
|
});
|
|
66
|
-
|
|
67
|
-
|
|
73
|
+
//check data isnt void/null/undefined
|
|
74
|
+
if (!(data === undefined || data === null)) {
|
|
75
|
+
response.data(data);
|
|
68
76
|
}
|
|
69
|
-
return
|
|
70
|
-
data: result,
|
|
71
|
-
}, additionalInit);
|
|
77
|
+
return response.format();
|
|
72
78
|
}
|
|
73
79
|
catch (error) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
details: error,
|
|
77
|
-
}, { status: 500 });
|
|
80
|
+
response.error("Handler error");
|
|
81
|
+
return response.format();
|
|
78
82
|
}
|
|
79
83
|
}
|
|
80
84
|
catch (error) {
|
|
81
85
|
if (error instanceof z.ZodError) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
details: error.issues,
|
|
85
|
-
}, { status: 400 });
|
|
86
|
+
response.error("Invalid input");
|
|
87
|
+
return response.format();
|
|
86
88
|
}
|
|
87
|
-
|
|
89
|
+
response.error("Invalid input");
|
|
90
|
+
return response.format();
|
|
88
91
|
}
|
|
89
92
|
}
|
|
90
93
|
}
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -10,6 +10,5 @@ export type FunctionOutput<T> = T extends (...args: any) => infer R ? Awaited<R>
|
|
|
10
10
|
};
|
|
11
11
|
export type LXNServerHandler<Input, Output> = (input: Input) => Promise<Output> | Output;
|
|
12
12
|
export type LXN_ServerClient_EventType = object;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
13
|
+
/** The inbound request passed to handlers (Next.js–style `cookies()` / `headers()`). */
|
|
14
|
+
export type { LixnetRequest as LXN_ServerClient_Request } from "./util/request";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export default function jsonFormatter() {
|
|
2
|
+
const headers = new Headers({
|
|
3
|
+
"Content-Type": "application/json",
|
|
4
|
+
});
|
|
5
|
+
for (const [headerName, headerValue] of Object.entries(this.responseHeaders)) {
|
|
6
|
+
headers.set(headerName, headerValue);
|
|
7
|
+
}
|
|
8
|
+
for (const [cookieName, cookie] of Object.entries(this.responseCookies)) {
|
|
9
|
+
if (cookie.type === "value") {
|
|
10
|
+
const opts = cookie.options ?? {};
|
|
11
|
+
headers.set('Set-Cookie', `${cookieName}=${cookie.value}; ${Object.entries(opts).map(([key, value]) => `${key}=${value}`).join('; ')}`);
|
|
12
|
+
}
|
|
13
|
+
else if (cookie.type === "delete") {
|
|
14
|
+
headers.set('Set-Cookie', `${cookieName}=; Max-Age=0; Path=/; HttpOnly; Secure; SameSite=Strict`);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
throw new Error(`Unknown cookie type: ${cookie.type}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return Response.json({
|
|
21
|
+
data: this.responseData,
|
|
22
|
+
}, {
|
|
23
|
+
status: 200,
|
|
24
|
+
headers: headers,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type RequestCookie = {
|
|
2
|
+
name: string;
|
|
3
|
+
value: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Read-only view of request cookies (Next.js `ReadonlyRequestCookies`).
|
|
7
|
+
*/
|
|
8
|
+
export declare class ReadonlyRequestCookies {
|
|
9
|
+
private readonly map;
|
|
10
|
+
constructor(cookieHeader: string | null);
|
|
11
|
+
get size(): number;
|
|
12
|
+
get(name: string): RequestCookie | undefined;
|
|
13
|
+
getAll(): RequestCookie[];
|
|
14
|
+
getAll(name: string): RequestCookie[];
|
|
15
|
+
has(name: string): boolean;
|
|
16
|
+
[Symbol.iterator](): IterableIterator<[string, RequestCookie]>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Read-only `Headers` view (Next.js `ReadonlyHeaders`).
|
|
20
|
+
* Wraps a cloned `Headers` instance so callers cannot mutate the snapshot.
|
|
21
|
+
*/
|
|
22
|
+
export declare class ReadonlyHeaders {
|
|
23
|
+
private readonly h;
|
|
24
|
+
constructor(headers: Headers);
|
|
25
|
+
get(name: string): string | null;
|
|
26
|
+
has(name: string): boolean;
|
|
27
|
+
getSetCookie(): string[];
|
|
28
|
+
entries(): IterableIterator<[string, string]>;
|
|
29
|
+
keys(): IterableIterator<string>;
|
|
30
|
+
values(): IterableIterator<string>;
|
|
31
|
+
forEach(callbackfn: (value: string, name: string, parent: ReadonlyHeaders) => void, thisArg?: unknown): void;
|
|
32
|
+
[Symbol.iterator](): IterableIterator<[string, string]>;
|
|
33
|
+
}
|
|
34
|
+
export type LixnetRequest = Omit<Request, "headers"> & {
|
|
35
|
+
cookies(): ReadonlyRequestCookies;
|
|
36
|
+
headers(): ReadonlyHeaders;
|
|
37
|
+
};
|
|
38
|
+
export default function (request: Request): LixnetRequest;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
function decodeCookieValue(raw) {
|
|
2
|
+
try {
|
|
3
|
+
return decodeURIComponent(raw.replace(/\+/g, " "));
|
|
4
|
+
}
|
|
5
|
+
catch {
|
|
6
|
+
return raw;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function parseCookieHeader(header) {
|
|
10
|
+
const map = new Map();
|
|
11
|
+
if (!header)
|
|
12
|
+
return map;
|
|
13
|
+
const segments = header.split(";");
|
|
14
|
+
for (const segment of segments) {
|
|
15
|
+
const trimmed = segment.trim();
|
|
16
|
+
if (!trimmed)
|
|
17
|
+
continue;
|
|
18
|
+
const eq = trimmed.indexOf("=");
|
|
19
|
+
if (eq === -1) {
|
|
20
|
+
map.set(trimmed, "");
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
const name = trimmed.slice(0, eq).trim();
|
|
24
|
+
const value = decodeCookieValue(trimmed.slice(eq + 1).trim());
|
|
25
|
+
map.set(name, value);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return map;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Read-only view of request cookies (Next.js `ReadonlyRequestCookies`).
|
|
32
|
+
*/
|
|
33
|
+
export class ReadonlyRequestCookies {
|
|
34
|
+
map;
|
|
35
|
+
constructor(cookieHeader) {
|
|
36
|
+
this.map = parseCookieHeader(cookieHeader);
|
|
37
|
+
}
|
|
38
|
+
get size() {
|
|
39
|
+
return this.map.size;
|
|
40
|
+
}
|
|
41
|
+
get(name) {
|
|
42
|
+
const value = this.map.get(name);
|
|
43
|
+
if (value === undefined)
|
|
44
|
+
return undefined;
|
|
45
|
+
return { name, value };
|
|
46
|
+
}
|
|
47
|
+
getAll(name) {
|
|
48
|
+
if (name === undefined) {
|
|
49
|
+
return [...this.map.entries()].map(([n, value]) => ({
|
|
50
|
+
name: n,
|
|
51
|
+
value,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
const value = this.map.get(name);
|
|
55
|
+
if (value === undefined)
|
|
56
|
+
return [];
|
|
57
|
+
return [{ name, value }];
|
|
58
|
+
}
|
|
59
|
+
has(name) {
|
|
60
|
+
return this.map.has(name);
|
|
61
|
+
}
|
|
62
|
+
[Symbol.iterator]() {
|
|
63
|
+
const map = this.map;
|
|
64
|
+
function* iter() {
|
|
65
|
+
for (const [n, value] of map.entries()) {
|
|
66
|
+
yield [n, { name: n, value }];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return iter();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Read-only `Headers` view (Next.js `ReadonlyHeaders`).
|
|
74
|
+
* Wraps a cloned `Headers` instance so callers cannot mutate the snapshot.
|
|
75
|
+
*/
|
|
76
|
+
export class ReadonlyHeaders {
|
|
77
|
+
h;
|
|
78
|
+
constructor(headers) {
|
|
79
|
+
this.h = new Headers(headers);
|
|
80
|
+
}
|
|
81
|
+
get(name) {
|
|
82
|
+
return this.h.get(name);
|
|
83
|
+
}
|
|
84
|
+
has(name) {
|
|
85
|
+
return this.h.has(name);
|
|
86
|
+
}
|
|
87
|
+
getSetCookie() {
|
|
88
|
+
const anyHeaders = this.h;
|
|
89
|
+
if (typeof anyHeaders.getSetCookie === "function") {
|
|
90
|
+
return anyHeaders.getSetCookie();
|
|
91
|
+
}
|
|
92
|
+
const single = this.h.get("set-cookie");
|
|
93
|
+
return single ? [single] : [];
|
|
94
|
+
}
|
|
95
|
+
entries() {
|
|
96
|
+
return this.h.entries();
|
|
97
|
+
}
|
|
98
|
+
keys() {
|
|
99
|
+
return this.h.keys();
|
|
100
|
+
}
|
|
101
|
+
values() {
|
|
102
|
+
return this.h.values();
|
|
103
|
+
}
|
|
104
|
+
forEach(callbackfn, thisArg) {
|
|
105
|
+
const self = this;
|
|
106
|
+
this.h.forEach(function (value, name) {
|
|
107
|
+
callbackfn.call(thisArg ?? self, value, name, self);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
[Symbol.iterator]() {
|
|
111
|
+
return this.h[Symbol.iterator]();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function wrapLixnetRequest(request) {
|
|
115
|
+
const snapshot = new Headers(request.headers);
|
|
116
|
+
return new Proxy(request, {
|
|
117
|
+
get(target, prop, receiver) {
|
|
118
|
+
if (prop === "cookies") {
|
|
119
|
+
return function cookies() {
|
|
120
|
+
return new ReadonlyRequestCookies(snapshot.get("cookie"));
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (prop === "headers") {
|
|
124
|
+
return function headers() {
|
|
125
|
+
return new ReadonlyHeaders(snapshot);
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return Reflect.get(target, prop, receiver);
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
export default function (request) {
|
|
133
|
+
return wrapLixnetRequest(request);
|
|
134
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
type CookieOptions = {
|
|
2
|
+
domain?: string;
|
|
3
|
+
path?: string;
|
|
4
|
+
maxAge?: number;
|
|
5
|
+
httpOnly?: boolean;
|
|
6
|
+
secure?: boolean;
|
|
7
|
+
sameSite?: 'lax' | 'strict' | 'none';
|
|
8
|
+
};
|
|
9
|
+
export default class LixnetResponse {
|
|
10
|
+
responseData: any;
|
|
11
|
+
responseError: string | null;
|
|
12
|
+
responseCode: number;
|
|
13
|
+
responseHeaders: Record<string, string>;
|
|
14
|
+
responseCookies: Record<string, {
|
|
15
|
+
type: "value" | "delete";
|
|
16
|
+
value?: string;
|
|
17
|
+
options?: CookieOptions;
|
|
18
|
+
}>;
|
|
19
|
+
constructor({ formatter }: {
|
|
20
|
+
formatter: (this: LixnetResponse) => Response;
|
|
21
|
+
});
|
|
22
|
+
data(data: any): void;
|
|
23
|
+
header(headerName: string, headerValue: string): void;
|
|
24
|
+
error(error: string): void;
|
|
25
|
+
code(code: number): void;
|
|
26
|
+
headers(headers: Record<string, string>): void;
|
|
27
|
+
cookie(cookieName: string, cookieValue: string, cookieOptions: CookieOptions): void;
|
|
28
|
+
deleteCookie(cookieName: string): void;
|
|
29
|
+
format: (this: LixnetResponse) => Response;
|
|
30
|
+
setFormatter(formatter: (this: LixnetResponse) => Response): void;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export default class LixnetResponse {
|
|
2
|
+
responseData;
|
|
3
|
+
responseError = null;
|
|
4
|
+
responseCode = 200;
|
|
5
|
+
responseHeaders = {};
|
|
6
|
+
responseCookies = {};
|
|
7
|
+
constructor({ formatter }) {
|
|
8
|
+
this.responseData = null;
|
|
9
|
+
this.format = formatter;
|
|
10
|
+
}
|
|
11
|
+
data(data) {
|
|
12
|
+
this.responseData = data;
|
|
13
|
+
}
|
|
14
|
+
header(headerName, headerValue) {
|
|
15
|
+
this.responseHeaders[headerName] = headerValue;
|
|
16
|
+
}
|
|
17
|
+
error(error) {
|
|
18
|
+
this.responseError = error;
|
|
19
|
+
}
|
|
20
|
+
code(code) {
|
|
21
|
+
this.responseCode = code;
|
|
22
|
+
}
|
|
23
|
+
headers(headers) {
|
|
24
|
+
for (const [headerName, headerValue] of Object.entries(headers)) {
|
|
25
|
+
this.header(headerName, headerValue);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
cookie(cookieName, cookieValue, cookieOptions) {
|
|
29
|
+
this.responseCookies[cookieName] = {
|
|
30
|
+
type: "value",
|
|
31
|
+
value: cookieValue,
|
|
32
|
+
options: cookieOptions,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
deleteCookie(cookieName) {
|
|
36
|
+
this.responseCookies[cookieName] = {
|
|
37
|
+
type: "delete",
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
format;
|
|
41
|
+
setFormatter(formatter) {
|
|
42
|
+
this.format = formatter;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lixnet",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"author": "
|
|
3
|
+
"version": "1.2.9",
|
|
4
|
+
"author": "nitlix",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"@types/bun": "latest"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"typescript": "^5.
|
|
19
|
+
"typescript": "^5.9.3"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"zod": "^4.
|
|
22
|
+
"zod": "^4.3.6"
|
|
23
23
|
}
|
|
24
24
|
}
|