Skip to content

Commit 8f1b160

Browse files
committed
Adds support for Response objects to be returned from onRequest callbacks, related to #9
1 parent 8c31739 commit 8f1b160

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

server.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,21 +496,36 @@ class EleventyDevServer {
496496
return res.end(result);
497497
}
498498

499-
if(isPlainObject(result)) {
499+
if(isPlainObject(result) || result instanceof Response) {
500500
if(typeof result.status === "number") {
501501
res.statusCode = result.status;
502502
}
503503

504-
if(isPlainObject(result.headers)) {
505-
for(let name in result.headers) {
506-
res.setHeader(name, result.headers[name]);
504+
if(result.headers instanceof Headers) {
505+
for(let [key, value] of result.headers.entries()) {
506+
res.setHeader(key, value);
507507
}
508+
} else if(isPlainObject(result.headers)) {
509+
for(let key of Object.keys(result.headers)) {
510+
res.setHeader(key, result.headers[key]);
511+
}
512+
}
513+
514+
if(result instanceof Response) {
515+
// no gzip/br compression here, uncompressed from fetch https://github.com/w3c/ServiceWorker/issues/339
516+
res.removeHeader("content-encoding");
517+
518+
let arrayBuffer = await result.arrayBuffer();
519+
res.setHeader("content-length", arrayBuffer.byteLength);
520+
521+
let buffer = Buffer.from(arrayBuffer);
522+
return res.end(buffer);
508523
}
509524

510525
return res.end(result.body || "");
511526
}
512527

513-
throw new Error(`Invalid return type from \`onRequest\` pattern for ${urlPatternString}: expected string or object.`);
528+
throw new Error(`Invalid return type from \`onRequest\` pattern for ${urlPatternString}: expected string, object literal, or Response instance.`);
514529
}
515530
}
516531

server/wrapResponse.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ function wrapResponse(resp, transformHtml) {
9696
// Only transform HTML
9797
// Note the “setHeader versus writeHead” note on https://nodejs.org/api/http.html#responsewriteheadstatuscode-statusmessage-headers
9898
let contentType = this._contentType || getContentType(this.getHeaders());
99-
if(contentType && contentType.startsWith("text/html")) {
99+
if(contentType?.startsWith("text/html")) {
100100
if(this._wrappedTransformHtml && typeof this._wrappedTransformHtml === "function") {
101101
result = this._wrappedTransformHtml(result);
102+
// uncompressed size: https://github.com/w3c/ServiceWorker/issues/339
102103
this.setHeader("Content-Length", Buffer.byteLength(result));
103104
}
104105
}
@@ -111,7 +112,7 @@ function wrapResponse(resp, transformHtml) {
111112
this._wrappedOriginalWrite(result, encoding)
112113
return this._wrappedOriginalEnd(callback);
113114
} else {
114-
// Buffers
115+
// Buffer or Uint8Array
115116
for(let headers of this._wrappedHeaders) {
116117
this._wrappedOriginalWriteHead(...headers);
117118
}

0 commit comments

Comments
 (0)