-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to automatically decompress gzip responses returned…
… from `fetch` (#497)
- Loading branch information
1 parent
9043103
commit e08d060
Showing
13 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
integration-tests/js-compute/fixtures/request-auto-decompress/bin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* eslint-env serviceworker */ | ||
import { env } from 'fastly:env'; | ||
import { pass, fail, assert, assertRejects } from "../../../assertions.js"; | ||
|
||
addEventListener("fetch", event => { | ||
event.respondWith(app(event)) | ||
}) | ||
/** | ||
* @param {FetchEvent} event | ||
* @returns {Response} | ||
*/ | ||
function app(event) { | ||
try { | ||
const path = (new URL(event.request.url)).pathname; | ||
console.log(`path: ${path}`) | ||
console.log(`FASTLY_SERVICE_VERSION: ${env('FASTLY_SERVICE_VERSION')}`) | ||
if (routes.has(path)) { | ||
const routeHandler = routes.get(path); | ||
return routeHandler() | ||
} | ||
return fail(`${path} endpoint does not exist`) | ||
} catch (error) { | ||
return fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack) | ||
} | ||
} | ||
|
||
const routes = new Map(); | ||
routes.set('/', () => { | ||
routes.delete('/'); | ||
let test_routes = Array.from(routes.keys()) | ||
return new Response(JSON.stringify(test_routes), { 'headers': { 'content-type': 'application/json' } }); | ||
}); | ||
|
||
// Request.fastly.decompressGzip option -- automatic gzip decompression of responses | ||
{ | ||
routes.set("/request/constructor/fastly/decompressGzip/true", async () => { | ||
const request = new Request('https://httpbin.org/gzip', { | ||
headers: { | ||
accept: 'application/json' | ||
}, | ||
backend: "httpbin", | ||
fastly: { | ||
decompressGzip: true | ||
} | ||
}); | ||
const response = await fetch(request); | ||
// This should work because the response will be decompressed and valid json. | ||
const body = await response.json(); | ||
let error = assert(body.gzipped, true, `body.gzipped`) | ||
if (error) { return error } | ||
return pass() | ||
}); | ||
routes.set("/request/constructor/fastly/decompressGzip/false", async () => { | ||
const request = new Request('https://httpbin.org/gzip', { | ||
headers: { | ||
accept: 'application/json' | ||
}, | ||
backend: "httpbin", | ||
fastly: { | ||
decompressGzip: false | ||
} | ||
}); | ||
const response = await fetch(request); | ||
let error = await assertRejects(async function() { | ||
// This should throw because the response will be gzipped compressed, which we can not parse as json. | ||
await response.json(); | ||
}); | ||
if (error) { return error } | ||
return pass() | ||
}); | ||
|
||
routes.set("/fetch/requestinit/fastly/decompressGzip/true", async () => { | ||
const response = await fetch('https://httpbin.org/gzip', { | ||
headers: { | ||
accept: 'application/json' | ||
}, | ||
backend: "httpbin", | ||
fastly: { | ||
decompressGzip: true | ||
} | ||
}); | ||
// This should work because the response will be decompressed and valid json. | ||
const body = await response.json(); | ||
let error = assert(body.gzipped, true, `body.gzipped`) | ||
if (error) { return error } | ||
return pass() | ||
}); | ||
routes.set("/fetch/requestinit/fastly/decompressGzip/false", async () => { | ||
const response = await fetch('https://httpbin.org/gzip', { | ||
headers: { | ||
accept: 'application/json' | ||
}, | ||
backend: "httpbin", | ||
fastly: { | ||
decompressGzip: false | ||
} | ||
}); | ||
let error = await assertRejects(async function() { | ||
// This should throw because the response will be gzipped compressed, which we can not parse as json. | ||
await response.json(); | ||
}); | ||
if (error) { return error } | ||
return pass() | ||
}); | ||
} |
23 changes: 23 additions & 0 deletions
23
integration-tests/js-compute/fixtures/request-auto-decompress/fastly.toml.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This file describes a Fastly Compute@Edge package. To learn more visit: | ||
# https://developer.fastly.com/reference/fastly-toml/ | ||
|
||
authors = ["[email protected]"] | ||
description = "" | ||
language = "other" | ||
manifest_version = 2 | ||
name = "request-auto-decompress" | ||
service_id = "" | ||
|
||
[scripts] | ||
build = "node ../../../../js-compute-runtime-cli.js" | ||
|
||
[local_server] | ||
[local_server.backends] | ||
[local_server.backends.httpbin] | ||
url = "https://httpbin.org/" | ||
|
||
[setup] | ||
[setup.backends] | ||
[setup.backends.httpbin] | ||
address = "httpbin.org" | ||
port = 443 |
42 changes: 42 additions & 0 deletions
42
integration-tests/js-compute/fixtures/request-auto-decompress/tests.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"GET /request/constructor/fastly/decompressGzip/true": { | ||
"environments": ["viceroy", "c@e"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/request/constructor/fastly/decompressGzip/true" | ||
}, | ||
"downstream_response": { | ||
"status": 200 | ||
} | ||
}, | ||
"GET /request/constructor/fastly/decompressGzip/false": { | ||
"environments": ["viceroy", "c@e"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/request/constructor/fastly/decompressGzip/false" | ||
}, | ||
"downstream_response": { | ||
"status": 200 | ||
} | ||
}, | ||
"GET /fetch/requestinit/fastly/decompressGzip/true": { | ||
"environments": ["viceroy", "c@e"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/fetch/requestinit/fastly/decompressGzip/true" | ||
}, | ||
"downstream_response": { | ||
"status": 200 | ||
} | ||
}, | ||
"GET /fetch/requestinit/fastly/decompressGzip/false": { | ||
"environments": ["viceroy", "c@e"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/fetch/requestinit/fastly/decompressGzip/false" | ||
}, | ||
"downstream_response": { | ||
"status": 200 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters