@goa/content-type
Version:
[fork] Create and parse HTTP Content-Type header according to RFC 7231 Written In ES6 And Optimised With JavaScript Compiler.
88 lines (86 loc) • 2.54 kB
JavaScript
;
let DEPACK_EXPORT;
const http = require('http');'use strict';
/*
MIT content-type
2015 Douglas Christopher Wilson
*/
const g = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g, h = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/, k = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/, l = /\\([\u000b\u0020-\u00ff])/g, m = /([\\"])/g, n = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
class p {
constructor(a) {
this.parameters = {};
this.type = a;
}
}
;DEPACK_EXPORT = {format:function(a) {
if (!a || "object" !== typeof a) {
throw new TypeError("argument obj is required");
}
const {parameters:b, type:e} = a;
if (!e || !n.test(e)) {
throw new TypeError("invalid type");
}
var c = e;
if (b && "object" == typeof b) {
a = Object.keys(b).sort();
for (let f = 0; f < a.length; f++) {
var d = a[f];
if (!k.test(d)) {
throw new TypeError("invalid parameter name");
}
var q = "; " + d + "=";
d = String(b[d]);
if (!k.test(d)) {
if (0 < d.length && !h.test(d)) {
throw new TypeError("invalid parameter value");
}
d = '"' + d.replace(m, "\\$1") + '"';
}
c += q + d;
}
}
return c;
}, parse:function(a) {
if (!a) {
throw new TypeError("argument string is required");
}
if ("object" == typeof a) {
if ("function" == typeof a.getHeader) {
var b = a.getHeader("content-type");
} else {
"object" == typeof a.headers && (b = (a = a.headers) && a["content-type"]);
}
if ("string" != typeof b) {
throw new TypeError("content-type header is missing from object");
}
a = b;
}
if ("string" != typeof a) {
throw new TypeError("argument string is required to be a string");
}
b = a.indexOf(";");
var e = -1 != b ? a.substr(0, b).trim() : a.trim();
if (!n.test(e)) {
throw new TypeError("invalid media type");
}
e = new p(e.toLowerCase());
if (-1 != b) {
let d;
var c;
for (g.lastIndex = b; c = g.exec(a);) {
if (c.index !== b) {
throw new TypeError("invalid parameter format");
}
b += c[0].length;
d = c[1].toLowerCase();
c = c[2];
'"' == c[0] && (c = c.substr(1, c.length - 2).replace(l, "$1"));
e.parameters[d] = c;
}
if (b != a.length) {
throw new TypeError("invalid parameter format");
}
}
return e;
}};
module.exports = DEPACK_EXPORT