1 |
|
2 | import { StreamResponse, StreamRequest } from "@worker-tools/stream-response";
|
3 | import { asyncIterToStream } from 'whatwg-stream-to-async-iter';
|
4 | import { JSONStringifyReadable, isAsyncIterable } from './json-stringify.js';
|
5 | const toBody = (x) => x instanceof ReadableStream
|
6 | ? x
|
7 | : isAsyncIterable(x)
|
8 | ? asyncIterToStream(x)
|
9 | : new JSONStringifyReadable(x);
|
10 | export class JSONStreamRequest extends StreamRequest {
|
11 | constructor(input, init) {
|
12 | const { headers: _headers, body: _body, ...rest } = init || {};
|
13 | const body = toBody(_body);
|
14 | const headers = new Headers(_headers);
|
15 | if (!headers.has('Content-Type') && _body != null)
|
16 | headers.set('Content-Type', JSONStreamRequest.contentType);
|
17 | if (!headers.has('Accept'))
|
18 | headers.set('Accept', JSONStreamRequest.accept);
|
19 | super(input instanceof URL ? input.href : input, { headers, body, ...rest });
|
20 | }
|
21 | }
|
22 | Object.defineProperty(JSONStreamRequest, "contentType", {
|
23 | enumerable: true,
|
24 | configurable: true,
|
25 | writable: true,
|
26 | value: 'application/json;charset=UTF-8'
|
27 | });
|
28 | Object.defineProperty(JSONStreamRequest, "accept", {
|
29 | enumerable: true,
|
30 | configurable: true,
|
31 | writable: true,
|
32 | value: 'application/json, text/plain, */*'
|
33 | });
|
34 | export class JSONStreamResponse extends StreamResponse {
|
35 | constructor(body, init) {
|
36 | const { headers: _headers, ...rest } = init || {};
|
37 | const _body = toBody(body);
|
38 | const headers = new Headers(_headers);
|
39 | if (!headers.has('Content-Type') && body != null)
|
40 | headers.set('Content-Type', JSONStreamResponse.contentType);
|
41 | super(_body, { headers, ...rest });
|
42 | }
|
43 | }
|
44 | Object.defineProperty(JSONStreamResponse, "contentType", {
|
45 | enumerable: true,
|
46 | configurable: true,
|
47 | writable: true,
|
48 | value: 'application/json;charset=UTF-8'
|
49 | });
|
50 |
|
\ | No newline at end of file |