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 CHANGED
@@ -252,6 +252,9 @@ socket.send({ text: 'Hello!' });
252
252
  ```
253
253
 
254
254
  #### **SSE**
255
+
256
+ Zlient's SSE implementation supports custom HTTP methods, request bodies, and **automatic authentication**.
257
+
255
258
  ```typescript
256
259
  const stream = client.createSSE({
257
260
  path: '/events',
@@ -1,4 +1,5 @@
1
1
  import type { AuthProvider } from '../auth';
2
+ import { LoggerUtil } from '../logger';
2
3
  import { ClientOptions, HTTPMethod, HTTPStatusCodeNumber, RequestOptions, ResponseSchema, SSEEndpointCall, SSEEndpointConfig, SSEResponseSchema, StandardSchemaV1, WSEndpointCall, WSEndpointConfig } from '../types';
3
4
  import { EndpointCall, EndpointConfig } from './http-endpoint';
4
5
  /**
@@ -68,6 +69,12 @@ export declare class HttpClient {
68
69
  * @returns Resolved base URL string
69
70
  */
70
71
  getBaseUrl(key: string): string;
72
+ /** @internal */
73
+ getAuth(): AuthProvider;
74
+ /** @internal */
75
+ getHeaders(): Record<string, string>;
76
+ /** @internal */
77
+ getLogger(): LoggerUtil;
71
78
  /**
72
79
  * Make an HTTP request with automatic retry, authentication, and validation.
73
80
  *
@@ -1 +1 @@
1
- {"version":3,"file":"http-client.d.ts","sourceRoot":"","sources":["../../lib/http/http-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAK5C,OAAO,EAEL,aAAa,EAEb,UAAU,EACV,oBAAoB,EAEpB,cAAc,EACd,cAAc,EAEd,eAAe,EACf,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAEhB,cAAc,EACd,gBAAgB,EACjB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,YAAY,EAAE,cAAc,EAAgB,MAAM,iBAAiB,CAAC;AAE7E;;;;;;;;;;;;;;GAcG;AACH,qBAAa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AAE,cAAc,CAAC;IAK9E;;;;;OAKG;IACH,eAAe,CACb,UAAU,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC3D,aAAa,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC9D,WAAW,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC5D,UAAU,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAE3D,MAAM,EAAE,gBAAgB,CAAC,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,CAAC,GAC3E,cAAc,CAAC,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,CAAC;IAKrE;;;;;OAKG;IACH,SAAS,CACP,SAAS,SAAS,iBAAiB,GAAG,SAAS,GAAG,SAAS,EAC3D,SAAS,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC1D,WAAW,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC5D,UAAU,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAE3D,MAAM,EAAE,iBAAiB,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,CAAC,GACvE,eAAe,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,CAAC;CAIlE"}
package/dist/index.cjs CHANGED
@@ -508,7 +508,8 @@ var SSEConnectionImpl = class {
508
508
  }
509
509
  async start() {
510
510
  try {
511
- const { method = "GET", data, headers = {}, withCredentials, signal } = this.options;
511
+ let { method = "GET", data, headers = {}, withCredentials, signal, auth, logger } = this.options;
512
+ let url = this.url;
512
513
  const init = {
513
514
  method,
514
515
  headers: {
@@ -517,48 +518,107 @@ var SSEConnectionImpl = class {
517
518
  },
518
519
  signal: signal || this.abortController.signal
519
520
  };
521
+ if (auth) {
522
+ await auth.apply({
523
+ url,
524
+ init
525
+ });
526
+ if (init.__urlOverride) url = init.__urlOverride;
527
+ }
520
528
  if (withCredentials) init.credentials = "include";
521
529
  if (data) {
522
530
  init.body = typeof data === "object" ? JSON.stringify(data) : String(data);
523
531
  if (!init.headers || !("Content-Type" in init.headers)) init.headers["Content-Type"] = "application/json";
524
532
  }
525
- const response = await fetch(this.url, init);
526
- if (!response.ok) throw new Error(`SSE request failed with status ${response.status}`);
533
+ if (logger) logger.debug("SSE connection initiated", {
534
+ method,
535
+ url,
536
+ hasData: !!data
537
+ });
538
+ const response = await fetch(url, init);
539
+ if (!response.ok) {
540
+ if (logger) logger.error(`SSE request failed with status ${response.status}`, /* @__PURE__ */ new Error("SSE Error"), {
541
+ url,
542
+ status: response.status
543
+ });
544
+ throw new Error(`SSE request failed with status ${response.status}`);
545
+ }
527
546
  this._readyState = 1;
528
547
  this.emit("open", { type: "open" });
529
548
  const reader = response.body?.getReader();
530
549
  if (!reader) throw new Error("Response body is not readable");
531
550
  const decoder = new TextDecoder();
532
- let buffer = "";
551
+ let lineBuffer = "";
552
+ let eventData = "";
553
+ let eventName = "message";
554
+ let lastCharWasCR = false;
555
+ const processLine = (line) => {
556
+ if (line === "") {
557
+ if (eventData) {
558
+ this.handleEvent(eventName, eventData.endsWith("\n") ? eventData.slice(0, -1) : eventData);
559
+ eventData = "";
560
+ }
561
+ eventName = "message";
562
+ return;
563
+ }
564
+ if (line.startsWith(":")) return;
565
+ const colonIndex = line.indexOf(":");
566
+ let field;
567
+ let value;
568
+ if (colonIndex === -1) {
569
+ field = line;
570
+ value = "";
571
+ } else {
572
+ field = line.slice(0, colonIndex);
573
+ value = line.slice(colonIndex + 1);
574
+ if (value.startsWith(" ")) value = value.slice(1);
575
+ }
576
+ if (field === "event") eventName = value;
577
+ else if (field === "data") eventData += value + "\n";
578
+ };
533
579
  while (true) {
534
580
  const { done, value } = await reader.read();
535
- if (done) break;
536
- buffer += decoder.decode(value, { stream: true });
537
- const parts = buffer.split(/\r\n|\r|\n/);
538
- buffer = parts.pop() || "";
539
- let eventData = "";
540
- let eventName = "message";
541
- for (const line of parts) {
542
- if (line === "") {
543
- if (eventData) {
544
- this.handleEvent(eventName, eventData.trim());
545
- eventData = "";
546
- eventName = "message";
547
- }
548
- continue;
581
+ const chunk = decoder.decode(value, { stream: true });
582
+ for (let i = 0; i < chunk.length; i++) {
583
+ const char = chunk[i];
584
+ if (char === "\n") if (lastCharWasCR) lastCharWasCR = false;
585
+ else {
586
+ processLine(lineBuffer);
587
+ lineBuffer = "";
549
588
  }
550
- if (line.startsWith(":")) continue;
551
- const colonIndex = line.indexOf(":");
552
- if (colonIndex === -1) this.processField(line, "", (name, data) => {
553
- eventName = name || eventName;
554
- eventData += data;
555
- });
589
+ else if (char === "\r") {
590
+ processLine(lineBuffer);
591
+ lineBuffer = "";
592
+ lastCharWasCR = true;
593
+ } else {
594
+ if (lastCharWasCR) lastCharWasCR = false;
595
+ lineBuffer += char;
596
+ }
597
+ }
598
+ if (done) {
599
+ const finalChunk = decoder.decode();
600
+ for (const char of finalChunk) if (char === "\n") if (lastCharWasCR) lastCharWasCR = false;
556
601
  else {
557
- const field = line.slice(0, colonIndex);
558
- const value = line.slice(colonIndex + 1).trim();
559
- if (field === "event") eventName = value;
560
- else if (field === "data") eventData += (eventData ? "\n" : "") + value;
602
+ processLine(lineBuffer);
603
+ lineBuffer = "";
604
+ }
605
+ else if (char === "\r") {
606
+ processLine(lineBuffer);
607
+ lineBuffer = "";
608
+ lastCharWasCR = true;
609
+ } else {
610
+ if (lastCharWasCR) lastCharWasCR = false;
611
+ lineBuffer += char;
612
+ }
613
+ if (lineBuffer) {
614
+ processLine(lineBuffer);
615
+ lineBuffer = "";
561
616
  }
617
+ if (eventData) {
618
+ this.handleEvent(eventName, eventData.endsWith("\n") ? eventData.slice(0, -1) : eventData);
619
+ eventData = "";
620
+ }
621
+ break;
562
622
  }
563
623
  }
564
624
  } catch (error) {
@@ -569,15 +629,11 @@ var SSEConnectionImpl = class {
569
629
  this._readyState = 2;
570
630
  }
571
631
  }
572
- processField(field, value, callback) {
573
- if (field === "event") callback(value, "");
574
- else if (field === "data") callback("", value);
575
- }
576
632
  async handleEvent(event, data) {
577
633
  let parsedData = data;
578
634
  try {
579
- if (typeof data === "string") try {
580
- parsedData = JSON.parse(data);
635
+ if (typeof data === "string" && data.length > 0) try {
636
+ if (data.startsWith("{") && data.endsWith("}") || data.startsWith("[") && data.endsWith("]") || data.startsWith("\"") && data.endsWith("\"")) parsedData = JSON.parse(data);
581
637
  } catch {}
582
638
  const schema = this.getSchema(event);
583
639
  if (!this.options.skipResponseValidation && schema) parsedData = await parseOrThrow(schema, parsedData);
@@ -641,10 +697,13 @@ var SSEEndpointImpl = class {
641
697
  method: this.config.method,
642
698
  data,
643
699
  headers: {
700
+ ...this.client.getHeaders(),
644
701
  ...this.config.advanced?.headers || {},
645
702
  ...headers || {}
646
703
  },
647
- signal
704
+ signal,
705
+ auth: this.config.advanced?.skipAuth ? void 0 : this.client.getAuth(),
706
+ logger: this.client.getLogger()
648
707
  });
649
708
  };
650
709
  }
@@ -865,6 +924,18 @@ var HttpClient = class {
865
924
  getBaseUrl(key) {
866
925
  return this.resolveBaseUrl(key);
867
926
  }
927
+ /** @internal */
928
+ getAuth() {
929
+ return this.auth;
930
+ }
931
+ /** @internal */
932
+ getHeaders() {
933
+ return this.headers;
934
+ }
935
+ /** @internal */
936
+ getLogger() {
937
+ return this.logger;
938
+ }
868
939
  /**
869
940
  * Make an HTTP request with automatic retry, authentication, and validation.
870
941
  *