Comparing version 4.1.0 to 4.2.0
@@ -15,4 +15,3 @@ import { Headers } from "./headers"; | ||
$rawBody: RawBody | null | typeof kBodyUsed | typeof kBodyDestroyed; | ||
headers: Headers; | ||
constructor(body: CreateBody, headers: Headers); | ||
constructor(body: CreateBody); | ||
readonly bodyUsed: boolean; | ||
@@ -32,2 +31,3 @@ json(): Promise<any>; | ||
method: string; | ||
headers: Headers; | ||
trailer: Promise<Headers>; | ||
@@ -44,6 +44,7 @@ readonly signal: Signal; | ||
statusText: string; | ||
headers: Headers; | ||
trailer: Promise<Headers>; | ||
readonly ok: boolean; | ||
constructor(body?: CreateBody, opts?: ResponseOptions); | ||
constructor(body?: CreateBody, init?: ResponseOptions); | ||
clone(): Response; | ||
} |
@@ -50,30 +50,5 @@ "use strict"; | ||
class Body { | ||
constructor(body, headers) { | ||
constructor(body) { | ||
const rawBody = body === undefined ? null : body; | ||
this.$rawBody = rawBody; | ||
this.headers = headers; | ||
if (rawBody === null) | ||
return; | ||
if (typeof rawBody === "string") { | ||
if (!headers.has("Content-Type")) { | ||
headers.set("Content-Type", "text/plain"); | ||
} | ||
if (!headers.has("Content-Length")) { | ||
headers.set("Content-Length", byte_length_1.byteLength(rawBody).toString()); | ||
} | ||
return; | ||
} | ||
// Default to "octet stream" for raw bodies. | ||
if (!headers.has("Content-Type")) { | ||
headers.set("Content-Type", "application/octet-stream"); | ||
} | ||
if (rawBody instanceof ArrayBuffer) { | ||
if (!headers.has("Content-Length")) { | ||
headers.set("Content-Length", rawBody.byteLength.toString()); | ||
} | ||
return; | ||
} | ||
if (rawBody instanceof ReadableStream) | ||
return; | ||
throw new TypeError("Unknown body type"); | ||
} | ||
@@ -140,5 +115,5 @@ get bodyUsed() { | ||
this.$rawBody = selfRawBody; | ||
return new Body(clonedRawBody, this.headers.clone()); | ||
return new Body(clonedRawBody); | ||
} | ||
return new Body(rawBody, this.headers.clone()); | ||
return new Body(rawBody); | ||
} | ||
@@ -161,11 +136,16 @@ destroy() { | ||
// Clone request or use passed options object. | ||
const opts = typeof input === "string" ? init : input.clone(); | ||
const headers = new headers_1.Headers(init.headers || opts.headers); | ||
const rawBody = init.body || (opts instanceof Request ? common_1.getRawBody(opts) : null); | ||
super(rawBody, headers); | ||
const req = typeof input === "string" ? undefined : input.clone(); | ||
const rawBody = init.body || (req ? common_1.getRawBody(req) : null); | ||
const headers = req && !init.headers | ||
? req.headers | ||
: getDefaultHeaders(rawBody, init.headers, init.omitDefaultHeaders === true); | ||
super(rawBody); | ||
this.url = typeof input === "string" ? input : input.url; | ||
this.method = init.method || opts.method || "GET"; | ||
this.signal = init.signal || opts.signal || new signal_1.Signal(); | ||
this.method = init.method || (req && req.method) || "GET"; | ||
this.signal = init.signal || (req && req.signal) || new signal_1.Signal(); | ||
this.headers = headers; | ||
this.trailer = Promise.resolve(init.trailer || opts.trailer).then(x => new headers_1.Headers(x)); | ||
this.trailer = | ||
req && !init.trailer | ||
? req.trailer | ||
: Promise.resolve(init.trailer).then(x => new headers_1.Headers(x)); | ||
// Destroy body on abort. | ||
@@ -178,3 +158,4 @@ events_1.once(this.signal, "abort", () => this.destroy()); | ||
body: common_1.getRawBody(cloned), | ||
headers: cloned.headers, | ||
headers: this.headers.clone(), | ||
omitDefaultHeaders: true, | ||
method: this.method, | ||
@@ -194,9 +175,9 @@ signal: this.signal, | ||
} | ||
constructor(body, opts = {}) { | ||
const headers = new headers_1.Headers(opts.headers); | ||
super(body, headers); | ||
this.status = opts.status || 200; | ||
this.statusText = opts.statusText || ""; | ||
constructor(body, init = {}) { | ||
const headers = getDefaultHeaders(body, init.headers, init.omitDefaultHeaders === true); | ||
super(body); | ||
this.status = init.status || 200; | ||
this.statusText = init.statusText || ""; | ||
this.headers = headers; | ||
this.trailer = Promise.resolve(opts.trailer).then(x => new headers_1.Headers(x)); | ||
this.trailer = Promise.resolve(init.trailer).then(x => new headers_1.Headers(x)); | ||
} | ||
@@ -208,3 +189,4 @@ clone() { | ||
statusText: this.statusText, | ||
headers: cloned.headers, | ||
headers: this.headers.clone(), | ||
omitDefaultHeaders: true, | ||
trailer: this.trailer.then(x => x.clone()) | ||
@@ -215,2 +197,32 @@ }); | ||
exports.Response = Response; | ||
/** | ||
* Get default headers for `Request` and `Response` instances. | ||
*/ | ||
function getDefaultHeaders(rawBody, init, omitDefaultHeaders) { | ||
const headers = new headers_1.Headers(init); | ||
if (rawBody === null || rawBody === undefined) | ||
return headers; | ||
if (typeof rawBody === "string") { | ||
if (!omitDefaultHeaders && !headers.has("Content-Type")) { | ||
headers.set("Content-Type", "text/plain"); | ||
} | ||
if (!omitDefaultHeaders && !headers.has("Content-Length")) { | ||
headers.set("Content-Length", byte_length_1.byteLength(rawBody).toString()); | ||
} | ||
return headers; | ||
} | ||
// Default to "octet stream" for raw bodies. | ||
if (!omitDefaultHeaders && !headers.has("Content-Type")) { | ||
headers.set("Content-Type", "application/octet-stream"); | ||
} | ||
if (rawBody instanceof ArrayBuffer) { | ||
if (!omitDefaultHeaders && !headers.has("Content-Length")) { | ||
headers.set("Content-Length", rawBody.byteLength.toString()); | ||
} | ||
return headers; | ||
} | ||
if (rawBody instanceof ReadableStream) | ||
return headers; | ||
throw new TypeError("Unknown body type"); | ||
} | ||
//# sourceMappingURL=browser.js.map |
@@ -34,3 +34,2 @@ import { Headers, HeadersInit } from "./headers"; | ||
$rawBody: T | null | typeof kBodyUsed | typeof kBodyDestroyed; | ||
headers: Headers; | ||
readonly bodyUsed: boolean; | ||
@@ -51,2 +50,3 @@ json(): Promise<any>; | ||
headers?: HeadersInit; | ||
omitDefaultHeaders?: boolean; | ||
trailer?: HeadersInit | Promise<HeadersInit>; | ||
@@ -60,2 +60,3 @@ } | ||
method: string; | ||
headers: Headers; | ||
trailer: Promise<Headers>; | ||
@@ -72,2 +73,3 @@ readonly signal: Signal; | ||
headers?: HeadersInit; | ||
omitDefaultHeaders?: boolean; | ||
trailer?: HeadersInit | Promise<HeadersInit>; | ||
@@ -81,4 +83,5 @@ } | ||
statusText: string; | ||
headers: Headers; | ||
trailer: Promise<Headers>; | ||
clone(): CommonResponse<T>; | ||
} |
@@ -17,4 +17,3 @@ /// <reference types="node" /> | ||
$rawBody: RawBody | null | typeof kBodyUsed | typeof kBodyDestroyed; | ||
headers: Headers; | ||
constructor(body: CreateBody, headers: Headers); | ||
constructor(body: CreateBody); | ||
readonly bodyUsed: boolean; | ||
@@ -35,2 +34,3 @@ json(): Promise<any>; | ||
method: string; | ||
headers: Headers; | ||
trailer: Promise<Headers>; | ||
@@ -47,2 +47,3 @@ readonly signal: Signal; | ||
statusText: string; | ||
headers: Headers; | ||
trailer: Promise<Headers>; | ||
@@ -49,0 +50,0 @@ readonly ok: boolean; |
@@ -37,3 +37,3 @@ "use strict"; | ||
class Body { | ||
constructor(body, headers) { | ||
constructor(body) { | ||
const rawBody = body === undefined | ||
@@ -44,32 +44,3 @@ ? null | ||
: body; | ||
this.headers = headers; | ||
this.$rawBody = rawBody; | ||
if (rawBody === null) | ||
return; | ||
if (typeof rawBody === "string") { | ||
if (!headers.has("Content-Type")) { | ||
headers.set("Content-Type", "text/plain"); | ||
} | ||
if (!headers.has("Content-Length")) { | ||
headers.set("Content-Length", byte_length_1.byteLength(rawBody).toString()); | ||
} | ||
return; | ||
} | ||
// Default to "octet stream" for raw bodies. | ||
if (!headers.has("Content-Type")) { | ||
headers.set("Content-Type", "application/octet-stream"); | ||
} | ||
if (isStream(rawBody)) { | ||
if (typeof rawBody.getHeaders === "function") { | ||
headers.extend(rawBody.getHeaders()); | ||
} | ||
return; | ||
} | ||
if (Buffer.isBuffer(rawBody)) { | ||
if (!headers.has("Content-Length")) { | ||
headers.set("Content-Length", String(rawBody.length)); | ||
} | ||
return; | ||
} | ||
throw new TypeError("Unknown body type"); | ||
} | ||
@@ -128,5 +99,5 @@ get bodyUsed() { | ||
this.$rawBody = rawBody.pipe(new stream_1.PassThrough()); | ||
return new Body(clonedRawBody, this.headers.clone()); | ||
return new Body(clonedRawBody); | ||
} | ||
return new Body(rawBody, this.headers.clone()); | ||
return new Body(rawBody); | ||
} | ||
@@ -149,11 +120,16 @@ destroy() { | ||
// Clone request or use passed options object. | ||
const opts = typeof input === "string" ? init : input.clone(); | ||
const headers = new headers_1.Headers(init.headers || opts.headers); | ||
const rawBody = init.body || (opts instanceof Request ? common_1.getRawBody(opts) : null); | ||
super(rawBody, headers); | ||
const req = typeof input === "string" ? undefined : input.clone(); | ||
const rawBody = init.body || (req ? common_1.getRawBody(req) : null); | ||
const headers = req && !init.headers | ||
? req.headers | ||
: getDefaultHeaders(rawBody, init.headers, init.omitDefaultHeaders === true); | ||
super(rawBody); | ||
this.url = typeof input === "string" ? input : input.url; | ||
this.method = init.method || opts.method || "GET"; | ||
this.signal = init.signal || opts.signal || new signal_1.Signal(); | ||
this.method = init.method || (req && req.method) || "GET"; | ||
this.signal = init.signal || (req && req.signal) || new signal_1.Signal(); | ||
this.headers = headers; | ||
this.trailer = Promise.resolve(init.trailer || opts.trailer).then(x => new headers_1.Headers(x)); | ||
this.trailer = | ||
req && !init.trailer | ||
? req.trailer | ||
: Promise.resolve(init.trailer).then(x => new headers_1.Headers(x)); | ||
// Destroy body on abort. | ||
@@ -166,3 +142,4 @@ events_1.once(this.signal, "abort", () => this.destroy()); | ||
body: common_1.getRawBody(cloned), | ||
headers: cloned.headers, | ||
headers: this.headers.clone(), | ||
omitDefaultHeaders: true, | ||
method: this.method, | ||
@@ -183,4 +160,4 @@ signal: this.signal, | ||
constructor(body, init = {}) { | ||
const headers = new headers_1.Headers(init.headers); | ||
super(body, headers); | ||
const headers = getDefaultHeaders(body, init.headers, init.omitDefaultHeaders === true); | ||
super(body); | ||
this.status = init.status || 200; | ||
@@ -196,3 +173,4 @@ this.statusText = init.statusText || ""; | ||
statusText: this.statusText, | ||
headers: cloned.headers, | ||
headers: this.headers.clone(), | ||
omitDefaultHeaders: true, | ||
trailer: this.trailer.then(x => x.clone()) | ||
@@ -203,2 +181,32 @@ }); | ||
exports.Response = Response; | ||
/** | ||
* Get default headers for `Request` and `Response` instances. | ||
*/ | ||
function getDefaultHeaders(rawBody, init, omitDefaultHeaders) { | ||
const headers = new headers_1.Headers(init); | ||
if (rawBody === null || rawBody === undefined) | ||
return headers; | ||
if (typeof rawBody === "string") { | ||
if (!omitDefaultHeaders && !headers.has("Content-Type")) { | ||
headers.set("Content-Type", "text/plain"); | ||
} | ||
if (!omitDefaultHeaders && !headers.has("Content-Length")) { | ||
headers.set("Content-Length", byte_length_1.byteLength(rawBody).toString()); | ||
} | ||
return headers; | ||
} | ||
// Default to "octet stream" for raw bodies. | ||
if (!omitDefaultHeaders && !headers.has("Content-Type")) { | ||
headers.set("Content-Type", "application/octet-stream"); | ||
} | ||
if (rawBody instanceof ArrayBuffer) { | ||
if (!omitDefaultHeaders && !headers.has("Content-Length")) { | ||
headers.set("Content-Length", rawBody.byteLength.toString()); | ||
} | ||
return headers; | ||
} | ||
if (rawBody instanceof ReadableStream) | ||
return headers; | ||
throw new TypeError("Unknown body type"); | ||
} | ||
//# sourceMappingURL=node.js.map |
@@ -35,2 +35,36 @@ "use strict"; | ||
}); | ||
it("should initialize default headers", () => { | ||
const req = new node_1.Request("", { | ||
body: "test" | ||
}); | ||
expect(req.headers.get("Content-Type")).toEqual("text/plain"); | ||
expect(req.headers.get("Content-Length")).toEqual("4"); | ||
}); | ||
it("should skip default header initialization", () => { | ||
const req = new node_1.Request("/", { | ||
body: "test", | ||
omitDefaultHeaders: true | ||
}); | ||
expect(req.headers.get("Content-Length")).toEqual(null); | ||
const clonedReq = req.clone(); | ||
expect(clonedReq.headers.get("Content-Length")).toEqual(null); | ||
const initReq = new node_1.Request(req); | ||
expect(initReq.headers.get("Content-Length")).toEqual(null); | ||
}); | ||
it("should clone new header instances", () => { | ||
const req = new node_1.Request("/", { | ||
headers: { | ||
"Test": "true" | ||
} | ||
}); | ||
expect(req.headers.get("test")).toEqual("true"); | ||
const clonedReq = req.clone(); | ||
clonedReq.headers.set("Test", "false"); | ||
expect(req.headers.get("test")).toEqual("true"); | ||
expect(clonedReq.headers.get("test")).toEqual("false"); | ||
const initReq = new node_1.Request(req); | ||
initReq.headers.set("Test", "false"); | ||
expect(req.headers.get("test")).toEqual("true"); | ||
expect(initReq.headers.get("test")).toEqual("false"); | ||
}); | ||
}); | ||
@@ -64,4 +98,4 @@ describe("body", () => { | ||
const fn = jest.fn(); | ||
reqClone.signal.on('abort', fn); | ||
req.signal.emit('abort'); | ||
reqClone.signal.on("abort", fn); | ||
req.signal.emit("abort"); | ||
expect(fn).toHaveBeenCalled(); | ||
@@ -68,0 +102,0 @@ }); |
{ | ||
"name": "servie", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "Standard HTTP interfaces for HTTP clients and servers in node.js and browsers", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105083
965