UNPKG

http-message-signatures

Version:
93 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.quoteString = exports.parseHeader = exports.Item = exports.List = exports.Dictionary = void 0; const structured_headers_1 = require("structured-headers"); class Dictionary { constructor(input) { this.raw = input; this.parsed = (0, structured_headers_1.parseDictionary)(input); } toString() { return this.serialize(); } serialize() { return (0, structured_headers_1.serializeDictionary)(this.parsed); } has(key) { return this.parsed.has(key); } get(key) { const value = this.parsed.get(key); if (!value) { return value; } if ((0, structured_headers_1.isInnerList)(value)) { return (0, structured_headers_1.serializeInnerList)(value); } return (0, structured_headers_1.serializeItem)(value); } } exports.Dictionary = Dictionary; class List { constructor(input) { this.raw = input; this.parsed = (0, structured_headers_1.parseList)(input); } toString() { return this.serialize(); } serialize() { return (0, structured_headers_1.serializeList)(this.parsed); } } exports.List = List; class Item { constructor(input) { this.raw = input; this.parsed = (0, structured_headers_1.parseItem)(input); } toString() { return this.serialize(); } serialize() { return (0, structured_headers_1.serializeItem)(this.parsed); } } exports.Item = Item; function parseHeader(header) { const classes = [List, Dictionary, Item]; for (let i = 0; i < classes.length; i++) { try { return new classes[i](header); } catch (e) { // noop } } throw new Error('Unable to parse header as structured field'); } exports.parseHeader = parseHeader; /** * This allows consumers of the library to supply field specifications that aren't * strictly "structured fields". Really a string must start with a `"` but that won't * tend to happen in our configs. * * @param {string} input * @returns {string} */ function quoteString(input) { // if it's not quoted, attempt to quote if (!input.startsWith('"')) { // try to split the structured field const [name, ...rest] = input.split(';'); // no params, just quote the whole thing if (!rest.length) { return `"${name}"`; } // quote the first part and put the rest back as it was return `"${name}";${rest.join(';')}`; } return input; } exports.quoteString = quoteString; //# sourceMappingURL=structured-header.js.map