Skip to content

Commit

Permalink
constrained identity function
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Aug 20, 2021
1 parent 5f83e8a commit 66f7915
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
11 changes: 8 additions & 3 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pathToFileURL, resolve, URL } from 'url';
import { mkdirp } from '../../utils/filesystem.js';
import { __fetch_polyfill } from '../../install-fetch.js';
import { SVELTE_KIT } from '../constants.js';
import { get_single_valued_header } from '../../utils/http.js';

/**
* @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler
Expand Down Expand Up @@ -182,10 +183,14 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
mkdirp(dirname(file));

if (response_type === REDIRECT) {
const location = /** @type {string} */ (headers.location);
const location = get_single_valued_header(headers, 'location');

log.warn(`${rendered.status} ${path} -> ${location}`);
writeFileSync(file, `<meta http-equiv="refresh" content="0;url=${encodeURI(location)}">`);
if (location) {
log.warn(`${rendered.status} ${path} -> ${location}`);
writeFileSync(file, `<meta http-equiv="refresh" content="0;url=${encodeURI(location)}">`);
} else {
log.warn(`location header missing on redirect received from ${path}`);
}

return;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { get_single_valued_header } from '../../utils/http.js';
import { lowercase_keys } from './utils.js';

/** @param {string} body */
Expand Down Expand Up @@ -62,7 +63,7 @@ export async function render_endpoint(request, route, match) {
let { status = 200, body, headers = {} } = response;

headers = lowercase_keys(headers);
const type = /** @type {string} */ (headers['content-type']);
const type = get_single_valued_header(headers, 'content-type');

const is_type_textual = is_content_type_textual(type);

Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parse_body } from './parse_body/index.js';
import { lowercase_keys } from './utils.js';
import { coalesce_to_error } from '../utils.js';
import { hash } from '../hash.js';
import { get_single_valued_header } from '../../utils/http.js';

/**
* @param {import('types/internal').Incoming} incoming
Expand Down Expand Up @@ -70,8 +71,8 @@ export async function respond(incoming, options, state = {}) {
if (response) {
// inject ETags for 200 responses
if (response.status === 200) {
const cache_control = /** @type {string} */ (response.headers['cache-control']);
if (!/(no-store|immutable)/.test(cache_control)) {
const cache_control = get_single_valued_header(response.headers, 'cache-control');
if (!cache_control || !/(no-store|immutable)/.test(cache_control)) {
const etag = `"${hash(response.body || '')}"`;

if (request.headers['if-none-match'] === etag) {
Expand Down
20 changes: 20 additions & 0 deletions packages/kit/src/utils/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {Record<string,string|string[]>} headers
* @param {string} key
* @returns {string|undefined}
*/
export function get_single_valued_header(headers, key) {
const value = headers[key];
if (Array.isArray(value)) {
if (value.length === 0) {
return undefined;
}
if (value.length > 1) {
throw new Error(
`Multiple headers provided for ${key}. Multiple may be provided only for set-cookie`
);
}
return value[0];
}
return value;
}

0 comments on commit 66f7915

Please sign in to comment.