UNPKG

@remix-run/headers

Version:

A toolkit for working with HTTP headers in JavaScript

43 lines (42 loc) 1.51 kB
export function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); } export function isIterable(value) { return value != null && typeof value[Symbol.iterator] === 'function'; } export function isValidDate(date) { return date instanceof Date && !isNaN(date.getTime()); } export function quoteEtag(tag) { return tag === '*' ? tag : /^(W\/)?".*"$/.test(tag) ? tag : `"${tag}"`; } /** * Removes milliseconds from a timestamp, returning seconds. * HTTP dates only have second precision, so this is useful for date comparisons. */ export function removeMilliseconds(time) { let timestamp = time instanceof Date ? time.getTime() : time; return Math.floor(timestamp / 1000); } const imfFixdatePattern = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{2}):(\d{2}):(\d{2}) GMT$/; /** * Parses an HTTP date header value. * * HTTP dates must follow RFC 7231 IMF-fixdate format: * "Day, DD Mon YYYY HH:MM:SS GMT" (e.g., "Wed, 21 Oct 2015 07:28:00 GMT") * * [RFC 7231 Section 7.1.1.1](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.1.1) * * @param dateString The HTTP date string to parse * @returns The timestamp in milliseconds, or null if invalid */ export function parseHttpDate(dateString) { if (!imfFixdatePattern.test(dateString)) { return null; } let timestamp = Date.parse(dateString); if (isNaN(timestamp)) { return null; } return timestamp; }