@standardserver/core 0.0.19 → 0.0.21
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 +12 -12
- package/dist/index.d.mts +11 -13
- package/dist/index.d.ts +11 -13
- package/dist/index.mjs +12 -12
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -200,7 +200,7 @@ Use Event-Stream Helpers when you need explicit SSE encoding, decoding, or metad
|
|
|
200
200
|
|
|
201
201
|
The event-stream entry point exposes:
|
|
202
202
|
|
|
203
|
-
- `
|
|
203
|
+
- `EventMeta` for `id`, `retry`, and `comments`
|
|
204
204
|
- `EventStreamMessage` for complete SSE messages
|
|
205
205
|
- `encodeEventStreamMessage()` and `decodeEventStreamMessage()` for single-message codec operations
|
|
206
206
|
- `EventStreamDecoder` and `EventStreamDecoderStream` for chunked stream decoding
|
|
@@ -241,29 +241,29 @@ const messages = response.body!
|
|
|
241
241
|
|
|
242
242
|
### Iterator metadata helpers
|
|
243
243
|
|
|
244
|
-
`StandardBody` uses async iterators for event-stream bodies. To attach SSE metadata to a yielded value without changing its visible shape, use `
|
|
244
|
+
`StandardBody` uses async iterators for event-stream bodies. To attach SSE metadata to a yielded value without changing its visible shape, use `withEventMeta()`.
|
|
245
245
|
|
|
246
246
|
```ts
|
|
247
247
|
import type { StandardResponse } from '@standardserver/core'
|
|
248
|
-
import {
|
|
249
|
-
unwrapEventIteratorEvent,
|
|
250
|
-
withEventIteratorEventMeta,
|
|
251
|
-
} from '@standardserver/core'
|
|
248
|
+
import { getEventMeta, unwrapEvent, withEventMeta } from '@standardserver/core'
|
|
252
249
|
|
|
253
|
-
const
|
|
250
|
+
const event = withEventMeta(
|
|
254
251
|
{ message: 'hello' },
|
|
255
252
|
{ id: '1', retry: 3000, comments: ['bootstrap'] },
|
|
256
253
|
)
|
|
257
254
|
|
|
258
|
-
const [data, meta] =
|
|
255
|
+
const [data, meta] = unwrapEvent(event)
|
|
259
256
|
// data => { message: 'hello' }
|
|
260
257
|
// meta => { id: '1', retry: 3000, comments: ['bootstrap'] }
|
|
261
258
|
|
|
259
|
+
const extractedMeta = getEventMeta(event)
|
|
260
|
+
// { id: '1', retry: 3000, comments: ['bootstrap'] }
|
|
261
|
+
|
|
262
262
|
const response: StandardResponse = {
|
|
263
263
|
status: 200,
|
|
264
264
|
headers: {},
|
|
265
265
|
async* body() {
|
|
266
|
-
yield
|
|
266
|
+
yield event
|
|
267
267
|
},
|
|
268
268
|
}
|
|
269
269
|
```
|
|
@@ -277,13 +277,13 @@ The subpath also exports:
|
|
|
277
277
|
|
|
278
278
|
- `EventStreamEncoderError` for invalid outbound SSE messages
|
|
279
279
|
- `EventStreamDecoderError` for incomplete or invalid inbound stream decoding
|
|
280
|
-
- `
|
|
280
|
+
- `ErrorEvent` for wrapping structured event-stream error payloads in an `Error`
|
|
281
281
|
- `assertEventStreamMessageId()`, `assertEventStreamMessageName()`, `assertEventStreamMessageRetry()`, and `assertEventStreamMessageComment()` for low-level validation when building custom SSE tooling
|
|
282
282
|
|
|
283
283
|
```ts
|
|
284
|
-
import {
|
|
284
|
+
import { ErrorEvent } from '@standardserver/core'
|
|
285
285
|
|
|
286
|
-
const error = new
|
|
286
|
+
const error = new ErrorEvent(
|
|
287
287
|
{ code: 'E_STREAM', detail: 'Connection lost' },
|
|
288
288
|
{ message: 'stream error' },
|
|
289
289
|
)
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
interface
|
|
1
|
+
interface EventMeta {
|
|
2
2
|
/**
|
|
3
3
|
* Event identifier, sent back by the client as `lastEventId` for reconnection attempts.
|
|
4
4
|
*
|
|
@@ -16,7 +16,7 @@ interface EventStreamMessageMeta {
|
|
|
16
16
|
*/
|
|
17
17
|
comments?: string[] | undefined;
|
|
18
18
|
}
|
|
19
|
-
interface EventStreamMessage extends
|
|
19
|
+
interface EventStreamMessage extends EventMeta {
|
|
20
20
|
/**
|
|
21
21
|
* Event name (e.g., `message`, `error`).
|
|
22
22
|
*/
|
|
@@ -52,28 +52,26 @@ declare class EventStreamEncoderError extends TypeError {
|
|
|
52
52
|
}
|
|
53
53
|
declare class EventStreamDecoderError extends TypeError {
|
|
54
54
|
}
|
|
55
|
-
interface
|
|
55
|
+
interface ErrorEventOptions extends ErrorOptions {
|
|
56
56
|
message?: string;
|
|
57
57
|
}
|
|
58
|
-
declare class
|
|
59
|
-
|
|
60
|
-
constructor(data: unknown, options?:
|
|
58
|
+
declare class ErrorEvent extends Error {
|
|
59
|
+
data: unknown;
|
|
60
|
+
constructor(data: unknown, options?: ErrorEventOptions);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
declare const EVENT_ITERATOR_EVENT_META_SYMBOL: unique symbol;
|
|
64
|
-
declare const EVENT_ITERATOR_EVENT_SOURCE_SYMBOL: unique symbol;
|
|
65
63
|
/**
|
|
66
64
|
* Returns a new iterator *event value* with attached, validated metadata.
|
|
67
65
|
*/
|
|
68
|
-
declare function
|
|
66
|
+
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
69
67
|
/**
|
|
70
68
|
* Unwraps an iterator event value and extracts its associated metadata.
|
|
71
69
|
*/
|
|
72
|
-
declare function
|
|
70
|
+
declare function unwrapEvent<T>(container: T): [data: T, meta: EventMeta | undefined];
|
|
73
71
|
/**
|
|
74
72
|
* Retrieves metadata attached to a single iterator event value.
|
|
75
73
|
*/
|
|
76
|
-
declare function
|
|
74
|
+
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
77
75
|
|
|
78
76
|
type StandardMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | (string & {});
|
|
79
77
|
type StandardUrl = `/${string}` | `/${string}?${string}` | `/${string}#${string}` | `/${string}?${string}#${string}`;
|
|
@@ -157,5 +155,5 @@ declare function isStandardHeaders(maybe: unknown): maybe is StandardHeaders;
|
|
|
157
155
|
declare function isStandardRequest(maybe: unknown): maybe is StandardRequest;
|
|
158
156
|
declare function isStandardResponse(maybe: unknown): maybe is StandardResponse;
|
|
159
157
|
|
|
160
|
-
export {
|
|
161
|
-
export type {
|
|
158
|
+
export { ErrorEvent, EventStreamDecoder, EventStreamDecoderError, EventStreamDecoderStream, EventStreamEncoderError, assertEventStreamMessageComment, assertEventStreamMessageId, assertEventStreamMessageName, assertEventStreamMessageRetry, decodeEventStreamMessage, encodeEventStreamMessage, encodeEventStreamMessageComments, encodeEventStreamMessageData, flattenStandardHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isStandardHeaders, isStandardMethod, isStandardRequest, isStandardResponse, isStandardUrl, mergeStandardHeaders, parseStandardUrl, unwrapEvent, withEventMeta };
|
|
159
|
+
export type { ErrorEventOptions, EventMeta, EventStreamMessage, StandardBody, StandardBodyHint, StandardHeaders, StandardLazyRequest, StandardLazyResponse, StandardMethod, StandardRequest, StandardResponse, StandardUrl };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
interface
|
|
1
|
+
interface EventMeta {
|
|
2
2
|
/**
|
|
3
3
|
* Event identifier, sent back by the client as `lastEventId` for reconnection attempts.
|
|
4
4
|
*
|
|
@@ -16,7 +16,7 @@ interface EventStreamMessageMeta {
|
|
|
16
16
|
*/
|
|
17
17
|
comments?: string[] | undefined;
|
|
18
18
|
}
|
|
19
|
-
interface EventStreamMessage extends
|
|
19
|
+
interface EventStreamMessage extends EventMeta {
|
|
20
20
|
/**
|
|
21
21
|
* Event name (e.g., `message`, `error`).
|
|
22
22
|
*/
|
|
@@ -52,28 +52,26 @@ declare class EventStreamEncoderError extends TypeError {
|
|
|
52
52
|
}
|
|
53
53
|
declare class EventStreamDecoderError extends TypeError {
|
|
54
54
|
}
|
|
55
|
-
interface
|
|
55
|
+
interface ErrorEventOptions extends ErrorOptions {
|
|
56
56
|
message?: string;
|
|
57
57
|
}
|
|
58
|
-
declare class
|
|
59
|
-
|
|
60
|
-
constructor(data: unknown, options?:
|
|
58
|
+
declare class ErrorEvent extends Error {
|
|
59
|
+
data: unknown;
|
|
60
|
+
constructor(data: unknown, options?: ErrorEventOptions);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
declare const EVENT_ITERATOR_EVENT_META_SYMBOL: unique symbol;
|
|
64
|
-
declare const EVENT_ITERATOR_EVENT_SOURCE_SYMBOL: unique symbol;
|
|
65
63
|
/**
|
|
66
64
|
* Returns a new iterator *event value* with attached, validated metadata.
|
|
67
65
|
*/
|
|
68
|
-
declare function
|
|
66
|
+
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
69
67
|
/**
|
|
70
68
|
* Unwraps an iterator event value and extracts its associated metadata.
|
|
71
69
|
*/
|
|
72
|
-
declare function
|
|
70
|
+
declare function unwrapEvent<T>(container: T): [data: T, meta: EventMeta | undefined];
|
|
73
71
|
/**
|
|
74
72
|
* Retrieves metadata attached to a single iterator event value.
|
|
75
73
|
*/
|
|
76
|
-
declare function
|
|
74
|
+
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
77
75
|
|
|
78
76
|
type StandardMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | (string & {});
|
|
79
77
|
type StandardUrl = `/${string}` | `/${string}?${string}` | `/${string}#${string}` | `/${string}?${string}#${string}`;
|
|
@@ -157,5 +155,5 @@ declare function isStandardHeaders(maybe: unknown): maybe is StandardHeaders;
|
|
|
157
155
|
declare function isStandardRequest(maybe: unknown): maybe is StandardRequest;
|
|
158
156
|
declare function isStandardResponse(maybe: unknown): maybe is StandardResponse;
|
|
159
157
|
|
|
160
|
-
export {
|
|
161
|
-
export type {
|
|
158
|
+
export { ErrorEvent, EventStreamDecoder, EventStreamDecoderError, EventStreamDecoderStream, EventStreamEncoderError, assertEventStreamMessageComment, assertEventStreamMessageId, assertEventStreamMessageName, assertEventStreamMessageRetry, decodeEventStreamMessage, encodeEventStreamMessage, encodeEventStreamMessageComments, encodeEventStreamMessageData, flattenStandardHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isStandardHeaders, isStandardMethod, isStandardRequest, isStandardResponse, isStandardUrl, mergeStandardHeaders, parseStandardUrl, unwrapEvent, withEventMeta };
|
|
159
|
+
export type { ErrorEventOptions, EventMeta, EventStreamMessage, StandardBody, StandardBodyHint, StandardHeaders, StandardLazyRequest, StandardLazyResponse, StandardMethod, StandardRequest, StandardResponse, StandardUrl };
|
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,7 @@ class EventStreamEncoderError extends TypeError {
|
|
|
4
4
|
}
|
|
5
5
|
class EventStreamDecoderError extends TypeError {
|
|
6
6
|
}
|
|
7
|
-
class
|
|
7
|
+
class ErrorEvent extends Error {
|
|
8
8
|
constructor(data, options = {}) {
|
|
9
9
|
super(options?.message ?? "Error Event", options);
|
|
10
10
|
this.data = data;
|
|
@@ -155,9 +155,9 @@ function encodeEventStreamMessage(message) {
|
|
|
155
155
|
return output;
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
function
|
|
158
|
+
const EVENT_META_SYMBOL = Symbol.for("STANDARDSERVER_EVENT_META");
|
|
159
|
+
const EVENT_SOURCE_SYMBOL = Symbol.for("STANDARDSERVER_EVENT_SOURCE");
|
|
160
|
+
function withEventMeta(container, meta) {
|
|
161
161
|
let assertedMeta;
|
|
162
162
|
if (meta.id !== void 0) {
|
|
163
163
|
assertEventStreamMessageId(meta.id);
|
|
@@ -181,29 +181,29 @@ function withEventIteratorEventMeta(container, meta) {
|
|
|
181
181
|
}
|
|
182
182
|
return new Proxy(container, {
|
|
183
183
|
get(target, prop, _receiver) {
|
|
184
|
-
if (prop ===
|
|
184
|
+
if (prop === EVENT_SOURCE_SYMBOL) {
|
|
185
185
|
return target;
|
|
186
186
|
}
|
|
187
|
-
if (prop ===
|
|
187
|
+
if (prop === EVENT_META_SYMBOL) {
|
|
188
188
|
return assertedMeta;
|
|
189
189
|
}
|
|
190
190
|
return getOrBind(target, prop);
|
|
191
191
|
}
|
|
192
192
|
});
|
|
193
193
|
}
|
|
194
|
-
function
|
|
194
|
+
function unwrapEvent(container) {
|
|
195
195
|
if (!isTypescriptObject(container)) {
|
|
196
196
|
return [container, void 0];
|
|
197
197
|
}
|
|
198
|
-
const meta = container[
|
|
199
|
-
const target = container[
|
|
198
|
+
const meta = container[EVENT_META_SYMBOL];
|
|
199
|
+
const target = container[EVENT_SOURCE_SYMBOL] ?? container;
|
|
200
200
|
return [target, meta];
|
|
201
201
|
}
|
|
202
|
-
function
|
|
202
|
+
function getEventMeta(container) {
|
|
203
203
|
if (!isTypescriptObject(container)) {
|
|
204
204
|
return void 0;
|
|
205
205
|
}
|
|
206
|
-
return container[
|
|
206
|
+
return container[EVENT_META_SYMBOL];
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
function generateContentDisposition(filename) {
|
|
@@ -292,4 +292,4 @@ function isStandardResponse(maybe) {
|
|
|
292
292
|
return isStandardHeaders(maybe.headers);
|
|
293
293
|
}
|
|
294
294
|
|
|
295
|
-
export {
|
|
295
|
+
export { ErrorEvent, EventStreamDecoder, EventStreamDecoderError, EventStreamDecoderStream, EventStreamEncoderError, assertEventStreamMessageComment, assertEventStreamMessageId, assertEventStreamMessageName, assertEventStreamMessageRetry, decodeEventStreamMessage, encodeEventStreamMessage, encodeEventStreamMessageComments, encodeEventStreamMessageData, flattenStandardHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isStandardHeaders, isStandardMethod, isStandardRequest, isStandardResponse, isStandardUrl, mergeStandardHeaders, parseStandardUrl, unwrapEvent, withEventMeta };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standardserver/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.21",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://standardserver.dev",
|
|
7
7
|
"repository": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@standardserver/shared": "0.0.
|
|
25
|
+
"@standardserver/shared": "0.0.21"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
28
|
"build": "unbuild",
|