zlient 3.3.2 → 3.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/README.md +3 -0
- package/dist/http/http-client.d.ts +7 -0
- package/dist/http/http-client.d.ts.map +1 -1
- package/dist/index.cjs +106 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +106 -35
- package/dist/index.js.map +1 -1
- package/dist/sse/sse-client.d.ts +5 -2
- package/dist/sse/sse-client.d.ts.map +1 -1
- package/dist/sse/sse-endpoint.d.ts +1 -1
- package/dist/sse/sse-endpoint.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -507,7 +507,8 @@ var SSEConnectionImpl = class {
|
|
|
507
507
|
}
|
|
508
508
|
async start() {
|
|
509
509
|
try {
|
|
510
|
-
|
|
510
|
+
let { method = "GET", data, headers = {}, withCredentials, signal, auth, logger } = this.options;
|
|
511
|
+
let url = this.url;
|
|
511
512
|
const init = {
|
|
512
513
|
method,
|
|
513
514
|
headers: {
|
|
@@ -516,48 +517,107 @@ var SSEConnectionImpl = class {
|
|
|
516
517
|
},
|
|
517
518
|
signal: signal || this.abortController.signal
|
|
518
519
|
};
|
|
520
|
+
if (auth) {
|
|
521
|
+
await auth.apply({
|
|
522
|
+
url,
|
|
523
|
+
init
|
|
524
|
+
});
|
|
525
|
+
if (init.__urlOverride) url = init.__urlOverride;
|
|
526
|
+
}
|
|
519
527
|
if (withCredentials) init.credentials = "include";
|
|
520
528
|
if (data) {
|
|
521
529
|
init.body = typeof data === "object" ? JSON.stringify(data) : String(data);
|
|
522
530
|
if (!init.headers || !("Content-Type" in init.headers)) init.headers["Content-Type"] = "application/json";
|
|
523
531
|
}
|
|
524
|
-
|
|
525
|
-
|
|
532
|
+
if (logger) logger.debug("SSE connection initiated", {
|
|
533
|
+
method,
|
|
534
|
+
url,
|
|
535
|
+
hasData: !!data
|
|
536
|
+
});
|
|
537
|
+
const response = await fetch(url, init);
|
|
538
|
+
if (!response.ok) {
|
|
539
|
+
if (logger) logger.error(`SSE request failed with status ${response.status}`, /* @__PURE__ */ new Error("SSE Error"), {
|
|
540
|
+
url,
|
|
541
|
+
status: response.status
|
|
542
|
+
});
|
|
543
|
+
throw new Error(`SSE request failed with status ${response.status}`);
|
|
544
|
+
}
|
|
526
545
|
this._readyState = 1;
|
|
527
546
|
this.emit("open", { type: "open" });
|
|
528
547
|
const reader = response.body?.getReader();
|
|
529
548
|
if (!reader) throw new Error("Response body is not readable");
|
|
530
549
|
const decoder = new TextDecoder();
|
|
531
|
-
let
|
|
550
|
+
let lineBuffer = "";
|
|
551
|
+
let eventData = "";
|
|
552
|
+
let eventName = "message";
|
|
553
|
+
let lastCharWasCR = false;
|
|
554
|
+
const processLine = (line) => {
|
|
555
|
+
if (line === "") {
|
|
556
|
+
if (eventData) {
|
|
557
|
+
this.handleEvent(eventName, eventData.endsWith("\n") ? eventData.slice(0, -1) : eventData);
|
|
558
|
+
eventData = "";
|
|
559
|
+
}
|
|
560
|
+
eventName = "message";
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
if (line.startsWith(":")) return;
|
|
564
|
+
const colonIndex = line.indexOf(":");
|
|
565
|
+
let field;
|
|
566
|
+
let value;
|
|
567
|
+
if (colonIndex === -1) {
|
|
568
|
+
field = line;
|
|
569
|
+
value = "";
|
|
570
|
+
} else {
|
|
571
|
+
field = line.slice(0, colonIndex);
|
|
572
|
+
value = line.slice(colonIndex + 1);
|
|
573
|
+
if (value.startsWith(" ")) value = value.slice(1);
|
|
574
|
+
}
|
|
575
|
+
if (field === "event") eventName = value;
|
|
576
|
+
else if (field === "data") eventData += value + "\n";
|
|
577
|
+
};
|
|
532
578
|
while (true) {
|
|
533
579
|
const { done, value } = await reader.read();
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
if (line === "") {
|
|
542
|
-
if (eventData) {
|
|
543
|
-
this.handleEvent(eventName, eventData.trim());
|
|
544
|
-
eventData = "";
|
|
545
|
-
eventName = "message";
|
|
546
|
-
}
|
|
547
|
-
continue;
|
|
580
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
581
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
582
|
+
const char = chunk[i];
|
|
583
|
+
if (char === "\n") if (lastCharWasCR) lastCharWasCR = false;
|
|
584
|
+
else {
|
|
585
|
+
processLine(lineBuffer);
|
|
586
|
+
lineBuffer = "";
|
|
548
587
|
}
|
|
549
|
-
if (
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
588
|
+
else if (char === "\r") {
|
|
589
|
+
processLine(lineBuffer);
|
|
590
|
+
lineBuffer = "";
|
|
591
|
+
lastCharWasCR = true;
|
|
592
|
+
} else {
|
|
593
|
+
if (lastCharWasCR) lastCharWasCR = false;
|
|
594
|
+
lineBuffer += char;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (done) {
|
|
598
|
+
const finalChunk = decoder.decode();
|
|
599
|
+
for (const char of finalChunk) if (char === "\n") if (lastCharWasCR) lastCharWasCR = false;
|
|
555
600
|
else {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
601
|
+
processLine(lineBuffer);
|
|
602
|
+
lineBuffer = "";
|
|
603
|
+
}
|
|
604
|
+
else if (char === "\r") {
|
|
605
|
+
processLine(lineBuffer);
|
|
606
|
+
lineBuffer = "";
|
|
607
|
+
lastCharWasCR = true;
|
|
608
|
+
} else {
|
|
609
|
+
if (lastCharWasCR) lastCharWasCR = false;
|
|
610
|
+
lineBuffer += char;
|
|
611
|
+
}
|
|
612
|
+
if (lineBuffer) {
|
|
613
|
+
processLine(lineBuffer);
|
|
614
|
+
lineBuffer = "";
|
|
560
615
|
}
|
|
616
|
+
if (eventData) {
|
|
617
|
+
this.handleEvent(eventName, eventData.endsWith("\n") ? eventData.slice(0, -1) : eventData);
|
|
618
|
+
eventData = "";
|
|
619
|
+
}
|
|
620
|
+
break;
|
|
561
621
|
}
|
|
562
622
|
}
|
|
563
623
|
} catch (error) {
|
|
@@ -568,15 +628,11 @@ var SSEConnectionImpl = class {
|
|
|
568
628
|
this._readyState = 2;
|
|
569
629
|
}
|
|
570
630
|
}
|
|
571
|
-
processField(field, value, callback) {
|
|
572
|
-
if (field === "event") callback(value, "");
|
|
573
|
-
else if (field === "data") callback("", value);
|
|
574
|
-
}
|
|
575
631
|
async handleEvent(event, data) {
|
|
576
632
|
let parsedData = data;
|
|
577
633
|
try {
|
|
578
|
-
if (typeof data === "string") try {
|
|
579
|
-
parsedData = JSON.parse(data);
|
|
634
|
+
if (typeof data === "string" && data.length > 0) try {
|
|
635
|
+
if (data.startsWith("{") && data.endsWith("}") || data.startsWith("[") && data.endsWith("]") || data.startsWith("\"") && data.endsWith("\"")) parsedData = JSON.parse(data);
|
|
580
636
|
} catch {}
|
|
581
637
|
const schema = this.getSchema(event);
|
|
582
638
|
if (!this.options.skipResponseValidation && schema) parsedData = await parseOrThrow(schema, parsedData);
|
|
@@ -640,10 +696,13 @@ var SSEEndpointImpl = class {
|
|
|
640
696
|
method: this.config.method,
|
|
641
697
|
data,
|
|
642
698
|
headers: {
|
|
699
|
+
...this.client.getHeaders(),
|
|
643
700
|
...this.config.advanced?.headers || {},
|
|
644
701
|
...headers || {}
|
|
645
702
|
},
|
|
646
|
-
signal
|
|
703
|
+
signal,
|
|
704
|
+
auth: this.config.advanced?.skipAuth ? void 0 : this.client.getAuth(),
|
|
705
|
+
logger: this.client.getLogger()
|
|
647
706
|
});
|
|
648
707
|
};
|
|
649
708
|
}
|
|
@@ -864,6 +923,18 @@ var HttpClient = class {
|
|
|
864
923
|
getBaseUrl(key) {
|
|
865
924
|
return this.resolveBaseUrl(key);
|
|
866
925
|
}
|
|
926
|
+
/** @internal */
|
|
927
|
+
getAuth() {
|
|
928
|
+
return this.auth;
|
|
929
|
+
}
|
|
930
|
+
/** @internal */
|
|
931
|
+
getHeaders() {
|
|
932
|
+
return this.headers;
|
|
933
|
+
}
|
|
934
|
+
/** @internal */
|
|
935
|
+
getLogger() {
|
|
936
|
+
return this.logger;
|
|
937
|
+
}
|
|
867
938
|
/**
|
|
868
939
|
* Make an HTTP request with automatic retry, authentication, and validation.
|
|
869
940
|
*
|