Skip to content

Commit

Permalink
fix: allow loading gzipped fixtures (#644)
Browse files Browse the repository at this point in the history
When loading a file that ends in `.gz`, `sirv` will set the
`Content-Encoding` header to `gzip` which means browsers will
unzip the content before handing it back to `fetch` or `XMLHttpRequest`.

This PR adds a workaround to the asset server that sets the header
to a garbage value if a file ending in `.gz` has been requested.

It's necessary to use a garbage value because `sirv` will only set
the header if it's not been set already, so we can't simply delete it.

Refs: lukeed/sirv#158
Refs: ipfs/aegir#1462
  • Loading branch information
achingbrain committed Feb 8, 2024
1 parent fd652e1 commit 2764b63
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mocks/fixtures.mocha.js
@@ -0,0 +1,12 @@
const { equal } = require('uvu/assert')

describe('fixtures', () => {
it('should load a gzipped tar file', async () => {
const resp = await fetch('mocks/fixtures/file.tar.gz')
const buf = await resp.arrayBuffer()
const ui8 = new Uint8Array(buf)
const base64 = btoa(String.fromCodePoint(...ui8))

equal(base64, 'H4sICIlTHVIACw==')
})
})
Binary file added mocks/fixtures/file.tar.gz
Binary file not shown.
13 changes: 13 additions & 0 deletions src/utils/index.js
Expand Up @@ -620,6 +620,19 @@ export async function createPolka(dir, cwd, assets) {
// @ts-ignore
sirv(path.join(cwd, assets), {
dev: true,
setHeaders: (
/** @type {{ setHeader: (arg0: string, arg1: string) => void; }} */ rsp,
/** @type {string} */ pathname
) => {
// workaround for https://github.com/lukeed/sirv/issues/158 - we
// can't unset the `Content-Encoding` header because sirv sets it
// after this function is invoked and will only set it if it's not
// already set, so we need to set it to a garbage value that will be
// ignored by browsers
if (pathname.endsWith('.gz')) {
rsp.setHeader('Content-Encoding', 'unsupported-encoding')
}
},
})
)
.listen(port, host, (/** @type {Error} */ err) => {
Expand Down
11 changes: 11 additions & 0 deletions test.js
Expand Up @@ -15,6 +15,17 @@ describe('mocha', function () {
ok(proc.stdout.includes('passing'), 'process stdout')
})

it('fixtures', async () => {
const proc = await execa('./cli.js', [
'mocks/fixtures.mocha.js',
'--runner',
'mocha',
])

is(proc.exitCode, 0, 'exit code')
ok(proc.stdout.includes('passing'), 'process stdout')
})

it('node', async () => {
const proc = await execa('./cli.js', [
'mocks/test.mocha.js',
Expand Down

0 comments on commit 2764b63

Please sign in to comment.