@standardserver/core 0.0.19 → 0.0.20
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 +8 -8
- package/dist/index.d.mts +4 -6
- package/dist/index.d.ts +4 -6
- package/dist/index.mjs +11 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -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
|
```
|
package/dist/index.d.mts
CHANGED
|
@@ -60,20 +60,18 @@ declare class EventIteratorErrorEvent extends Error {
|
|
|
60
60
|
constructor(data: unknown, options?: EventIteratorErrorEventOptions);
|
|
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: EventStreamMessageMeta): 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: EventStreamMessageMeta | 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): EventStreamMessageMeta | 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 {
|
|
158
|
+
export { EventIteratorErrorEvent, 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 };
|
|
161
159
|
export type { EventIteratorErrorEventOptions, EventStreamMessage, EventStreamMessageMeta, StandardBody, StandardBodyHint, StandardHeaders, StandardLazyRequest, StandardLazyResponse, StandardMethod, StandardRequest, StandardResponse, StandardUrl };
|
package/dist/index.d.ts
CHANGED
|
@@ -60,20 +60,18 @@ declare class EventIteratorErrorEvent extends Error {
|
|
|
60
60
|
constructor(data: unknown, options?: EventIteratorErrorEventOptions);
|
|
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: EventStreamMessageMeta): 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: EventStreamMessageMeta | 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): EventStreamMessageMeta | 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 {
|
|
158
|
+
export { EventIteratorErrorEvent, 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 };
|
|
161
159
|
export type { EventIteratorErrorEventOptions, EventStreamMessage, EventStreamMessageMeta, StandardBody, StandardBodyHint, StandardHeaders, StandardLazyRequest, StandardLazyResponse, StandardMethod, StandardRequest, StandardResponse, StandardUrl };
|
package/dist/index.mjs
CHANGED
|
@@ -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 { EventIteratorErrorEvent, 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.20",
|
|
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.20"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
28
|
"build": "unbuild",
|