@orpc/standard-server 0.0.0-next.ca29a36 → 0.0.0-next.d0e429d

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/index.js CHANGED
@@ -10,29 +10,25 @@ var ErrorEvent = class extends Error {
10
10
  this.data = options?.data;
11
11
  }
12
12
  };
13
- var UnknownEvent = class extends ErrorEvent {
14
- };
15
13
 
16
14
  // src/event-source/decoder.ts
17
15
  function decodeEventMessage(encoded) {
18
16
  const lines = encoded.replace(/\n+$/, "").split(/\n/);
19
17
  const message = {
20
- data: "",
18
+ data: void 0,
21
19
  event: void 0,
22
20
  id: void 0,
23
- retry: void 0
21
+ retry: void 0,
22
+ comments: []
24
23
  };
25
24
  for (const line of lines) {
26
- const index = line.indexOf(": ");
27
- if (index === -1) {
28
- throw new EventDecoderError(`Invalid EventSource message line: ${line}`);
29
- }
30
- const key = line.slice(0, index);
31
- const value = line.slice(index + 2);
32
- if (key !== "data" && key in message && message[key] !== void 0) {
33
- throw new EventDecoderError(`Duplicate EventSource message key: ${key}`);
34
- }
35
- if (key === "data") {
25
+ const index = line.indexOf(":");
26
+ const key = index === -1 ? line : line.slice(0, index);
27
+ const value = index === -1 ? "" : line.slice(index + 1).replace(/^\s/, "");
28
+ if (index === 0) {
29
+ message.comments.push(value);
30
+ } else if (key === "data") {
31
+ message.data ??= "";
36
32
  message.data += `${value}
37
33
  `;
38
34
  } else if (key === "event") {
@@ -41,15 +37,12 @@ function decodeEventMessage(encoded) {
41
37
  message.id = value;
42
38
  } else if (key === "retry") {
43
39
  const maybeInteger = Number.parseInt(value);
44
- if (!Number.isInteger(maybeInteger) || maybeInteger < 0 || maybeInteger.toString() !== value) {
45
- throw new EventDecoderError(`Invalid EventSource message retry value: ${value}`);
40
+ if (Number.isInteger(maybeInteger) && maybeInteger >= 0 && maybeInteger.toString() === value) {
41
+ message.retry = maybeInteger;
46
42
  }
47
- message.retry = maybeInteger;
48
- } else {
49
- throw new EventDecoderError(`Unknown EventSource message key: ${key}`);
50
43
  }
51
44
  }
52
- message.data = message.data.replace(/\n$/, "");
45
+ message.data = message.data?.replace(/\n$/, "");
53
46
  return message;
54
47
  }
55
48
  var EventDecoder = class {
@@ -63,12 +56,9 @@ var EventDecoder = class {
63
56
  if (lastCompleteIndex === -1) {
64
57
  return;
65
58
  }
66
- const completes = this.incomplete.slice(0, lastCompleteIndex + 2).split(/\n{2,}/);
59
+ const completes = this.incomplete.slice(0, lastCompleteIndex).split(/\n\n/);
67
60
  this.incomplete = this.incomplete.slice(lastCompleteIndex + 2);
68
61
  for (const encoded of completes) {
69
- if (!encoded) {
70
- continue;
71
- }
72
62
  const message = decodeEventMessage(`${encoded}
73
63
 
74
64
  `);
@@ -79,7 +69,9 @@ var EventDecoder = class {
79
69
  this.incomplete = "";
80
70
  }
81
71
  end() {
82
- this.feed("\n\n");
72
+ if (this.incomplete) {
73
+ throw new EventDecoderError("EventSource ended before complete");
74
+ }
83
75
  }
84
76
  };
85
77
  var EventDecoderStream = class extends TransformStream {
@@ -119,8 +111,13 @@ function assertEventRetry(retry) {
119
111
  throw new EventEncoderError("Event-source retry must be a integer and >= 0");
120
112
  }
121
113
  }
114
+ function assertEventComment(comment) {
115
+ if (comment.includes("\n")) {
116
+ throw new EventEncoderError("Event-source comment must not contain a newline character");
117
+ }
118
+ }
122
119
  function encodeEventData(data) {
123
- const lines = data ? data.split(/\n/) : [""];
120
+ const lines = data?.split(/\n/) ?? [];
124
121
  let output = "";
125
122
  for (const line of lines) {
126
123
  output += `data: ${line}
@@ -128,8 +125,18 @@ function encodeEventData(data) {
128
125
  }
129
126
  return output;
130
127
  }
128
+ function encodeEventComments(comments) {
129
+ let output = "";
130
+ for (const comment of comments ?? []) {
131
+ assertEventComment(comment);
132
+ output += `: ${comment}
133
+ `;
134
+ }
135
+ return output;
136
+ }
131
137
  function encodeEventMessage(message) {
132
138
  let output = "";
139
+ output += encodeEventComments(message.comments);
133
140
  if (message.event !== void 0) {
134
141
  assertEventName(message.event);
135
142
  output += `event: ${message.event}
@@ -160,6 +167,11 @@ function withEventMeta(container, meta) {
160
167
  if (meta.retry !== void 0) {
161
168
  assertEventRetry(meta.retry);
162
169
  }
170
+ if (meta.comments !== void 0) {
171
+ for (const comment of meta.comments) {
172
+ assertEventComment(comment);
173
+ }
174
+ }
163
175
  return new Proxy(container, {
164
176
  get(target, prop, receiver) {
165
177
  if (prop === EVENT_SOURCE_META_SYMBOL) {
@@ -181,12 +193,13 @@ export {
181
193
  EventDecoderError,
182
194
  EventDecoderStream,
183
195
  EventEncoderError,
184
- UnknownEvent,
196
+ assertEventComment,
185
197
  assertEventId,
186
198
  assertEventName,
187
199
  assertEventRetry,
188
200
  contentDisposition,
189
201
  decodeEventMessage,
202
+ encodeEventComments,
190
203
  encodeEventData,
191
204
  encodeEventMessage,
192
205
  getEventMeta,
@@ -2,6 +2,8 @@ import type { EventMessage } from './types';
2
2
  export declare function assertEventId(id: string): void;
3
3
  export declare function assertEventName(event: string): void;
4
4
  export declare function assertEventRetry(retry: number): void;
5
+ export declare function assertEventComment(comment: string): void;
5
6
  export declare function encodeEventData(data: string | undefined): string;
7
+ export declare function encodeEventComments(comments: string[] | undefined): string;
6
8
  export declare function encodeEventMessage(message: Partial<EventMessage>): string;
7
9
  //# sourceMappingURL=encoder.d.ts.map
@@ -10,6 +10,4 @@ export declare class ErrorEvent extends Error {
10
10
  data: unknown;
11
11
  constructor(options?: ErrorEventOptions);
12
12
  }
13
- export declare class UnknownEvent extends ErrorEvent {
14
- }
15
13
  //# sourceMappingURL=errors.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import type { EventMessage } from './types';
2
- export type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id'>>;
2
+ export type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
3
3
  export declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
4
4
  export declare function getEventMeta(container: unknown): EventMeta | undefined;
5
5
  //# sourceMappingURL=meta.d.ts.map
@@ -1,10 +1,11 @@
1
1
  export interface EventMessage {
2
2
  event: string | undefined;
3
3
  id: string | undefined;
4
- data: string;
4
+ data: string | undefined;
5
5
  /**
6
6
  * The number of milliseconds to wait before retrying the event source if error occurs.
7
7
  */
8
8
  retry: number | undefined;
9
+ comments: string[];
9
10
  }
10
11
  //# sourceMappingURL=types.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/standard-server",
3
3
  "type": "module",
4
- "version": "0.0.0-next.ca29a36",
4
+ "version": "0.0.0-next.d0e429d",
5
5
  "license": "MIT",
6
6
  "homepage": "https://unnoq.com",
7
7
  "repository": {
@@ -29,7 +29,7 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@tinyhttp/content-disposition": "^2.2.2",
32
- "@orpc/shared": "0.0.0-next.ca29a36"
32
+ "@orpc/shared": "0.0.0-next.d0e429d"
33
33
  },
34
34
  "scripts": {
35
35
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",