@httpland/accept-ranges-parser
Version:
HTTP Accept-Ranges header field parser
32 lines (31 loc) • 1.32 kB
JavaScript
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseAcceptRanges = void 0;
const deps_js_1 = require("./deps.js");
const constants_js_1 = require("./constants.js");
/** Parses string into {@link AcceptRanges}.
*
* @example
* ```ts
* import { parseAcceptRanges } from "https://deno.land/x/accept_ranges_parser@$VERSION/parse.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(parseAcceptRanges(`none`), ["none"]);
* assertEquals(parseAcceptRanges(`bytes, unknown`), ["bytes", "unknown"]);
* ```
* @throws {SyntaxError} If the input is invalid [`<Accept-Ranges>`](https://www.rfc-editor.org/rfc/rfc9110.html#section-14.3-2) syntax.
*/
function parseAcceptRanges(input) {
const acceptableRanges = (0, deps_js_1.parseListFields)(input);
const msg = `${constants_js_1.Msg.InvalidSyntax} "${input}"`;
if (!(0, deps_js_1.isNotEmpty)(acceptableRanges))
throw new SyntaxError(msg);
acceptableRanges.forEach((token) => {
if (!(0, deps_js_1.isToken)(token))
throw new SyntaxError(msg);
});
return acceptableRanges;
}
exports.parseAcceptRanges = parseAcceptRanges;
;