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