@worker-tools/json-stream
Version:
Utilities for working with streaming JSON in Worker Runtimes such as Cloudflare Workers, Deno Deploy and Service Workers.
55 lines • 2.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSONStreamResponse = exports.JSONStreamRequest = void 0;
// deno-lint-ignore-file no-explicit-any
const stream_response_1 = require("@worker-tools/stream-response");
const whatwg_stream_to_async_iter_1 = require("whatwg-stream-to-async-iter");
const json_stringify_js_1 = require("./json-stringify.js");
const toBody = (x) => x instanceof ReadableStream
? x
: (0, json_stringify_js_1.isAsyncIterable)(x)
? (0, whatwg_stream_to_async_iter_1.asyncIterToStream)(x)
: new json_stringify_js_1.JSONStringifyReadable(x);
class JSONStreamRequest extends stream_response_1.StreamRequest {
constructor(input, init) {
const { headers: _headers, body: _body, ...rest } = init || {};
const body = toBody(_body);
const headers = new Headers(_headers);
if (!headers.has('Content-Type') && _body != null)
headers.set('Content-Type', JSONStreamRequest.contentType);
if (!headers.has('Accept'))
headers.set('Accept', JSONStreamRequest.accept);
super(input instanceof URL ? input.href : input, { headers, body, ...rest });
}
}
exports.JSONStreamRequest = JSONStreamRequest;
Object.defineProperty(JSONStreamRequest, "contentType", {
enumerable: true,
configurable: true,
writable: true,
value: 'application/json;charset=UTF-8'
});
Object.defineProperty(JSONStreamRequest, "accept", {
enumerable: true,
configurable: true,
writable: true,
value: 'application/json, text/plain, */*'
});
class JSONStreamResponse extends stream_response_1.StreamResponse {
constructor(body, init) {
const { headers: _headers, ...rest } = init || {};
const _body = toBody(body);
const headers = new Headers(_headers);
if (!headers.has('Content-Type') && body != null)
headers.set('Content-Type', JSONStreamResponse.contentType);
super(_body, { headers, ...rest });
}
}
exports.JSONStreamResponse = JSONStreamResponse;
Object.defineProperty(JSONStreamResponse, "contentType", {
enumerable: true,
configurable: true,
writable: true,
value: 'application/json;charset=UTF-8'
});
//# sourceMappingURL=json-fetch.js.map
;