es-content-type
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,103 +0,3 @@ | ||
'use strict'; | ||
import { ServerResponse, IncomingMessage } from 'http'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var http = require('http'); | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelper(o, allowArrayLike) { | ||
var it; | ||
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
var F = function () {}; | ||
return { | ||
s: F, | ||
n: function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}, | ||
e: function (e) { | ||
throw e; | ||
}, | ||
f: F | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var normalCompletion = true, | ||
didErr = false, | ||
err; | ||
return { | ||
s: function () { | ||
it = o[Symbol.iterator](); | ||
}, | ||
n: function () { | ||
var step = it.next(); | ||
normalCompletion = step.done; | ||
return step; | ||
}, | ||
e: function (e) { | ||
didErr = true; | ||
err = e; | ||
}, | ||
f: function () { | ||
try { | ||
if (!normalCompletion && it.return != null) it.return(); | ||
} finally { | ||
if (didErr) throw err; | ||
} | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -117,6 +17,5 @@ * RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1 | ||
*/ | ||
var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g; | ||
var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/; | ||
var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/; | ||
const PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g; | ||
const TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/; | ||
const TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/; | ||
/** | ||
@@ -128,9 +27,7 @@ * RegExp to match quoted-pair in RFC 7230 sec 3.2.6 | ||
*/ | ||
var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g; | ||
const QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g; | ||
/** | ||
* RegExp to match chars that must be quoted-pair in RFC 7230 sec 3.2.6 | ||
*/ | ||
var QUOTE_REGEXP = /([\\"])/g; | ||
const QUOTE_REGEXP = /([\\"])/g; | ||
/** | ||
@@ -143,36 +40,29 @@ * RegExp to match type in RFC 7231 sec 3.1.1.1 | ||
*/ | ||
var TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/; | ||
const TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/; | ||
function qstring(val) { | ||
var str = String(val); // no need to quote tokens | ||
if (TOKEN_REGEXP.test(str)) { | ||
return str; | ||
} | ||
if (str.length > 0 && !TEXT_REGEXP.test(str)) { | ||
throw new TypeError('invalid parameter value'); | ||
} | ||
return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'; | ||
const str = String(val); | ||
// no need to quote tokens | ||
if (TOKEN_REGEXP.test(str)) { | ||
return str; | ||
} | ||
if (str.length > 0 && !TEXT_REGEXP.test(str)) { | ||
throw new TypeError('invalid parameter value'); | ||
} | ||
return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'; | ||
} | ||
function getcontenttype(obj) { | ||
var header; | ||
if (obj instanceof http.ServerResponse && typeof obj.getHeader === 'function') { | ||
// res-like | ||
header = obj.getHeader('content-type'); | ||
} else if (obj instanceof http.IncomingMessage && _typeof(obj.headers) === 'object') { | ||
// req-like | ||
var h = obj.headers; | ||
header = h && h['content-type']; | ||
} | ||
if (typeof header !== 'string') { | ||
throw new TypeError('content-type header is missing from object'); | ||
} | ||
return header; | ||
let header; | ||
if (obj instanceof ServerResponse && typeof obj.getHeader === 'function') { | ||
// res-like | ||
header = obj.getHeader('content-type'); | ||
} | ||
else if (obj instanceof IncomingMessage && typeof obj.headers === 'object') { | ||
// req-like | ||
const h = obj.headers; | ||
header = h && h['content-type']; | ||
} | ||
if (typeof header !== 'string') { | ||
throw new TypeError('content-type header is missing from object'); | ||
} | ||
return header; | ||
} | ||
@@ -182,53 +72,31 @@ /** | ||
*/ | ||
var ContentType = function ContentType(type) { | ||
_classCallCheck(this, ContentType); | ||
this.parameters = {}; | ||
this.type = type; | ||
}; | ||
class ContentType { | ||
constructor(type) { | ||
this.parameters = {}; | ||
this.type = type; | ||
} | ||
} | ||
/** | ||
* Format object to media type. | ||
*/ | ||
function format(obj) { | ||
if (!obj || _typeof(obj) !== 'object') { | ||
throw new TypeError('argument obj is required'); | ||
} | ||
var parameters = obj.parameters, | ||
type = obj.type; | ||
if (!type || !TYPE_REGEXP.test(type)) { | ||
throw new TypeError('invalid type'); | ||
} | ||
var string = type; // append parameters | ||
if (parameters && _typeof(parameters) == 'object') { | ||
var params = Object.keys(parameters).sort(); | ||
var _iterator = _createForOfIteratorHelper(params), | ||
_step; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var param = _step.value; | ||
if (!TOKEN_REGEXP.test(param)) { | ||
throw new TypeError('invalid parameter name'); | ||
if (!obj || typeof obj !== 'object') { | ||
throw new TypeError('argument obj is required'); | ||
} | ||
const { parameters, type } = obj; | ||
if (!type || !TYPE_REGEXP.test(type)) { | ||
throw new TypeError('invalid type'); | ||
} | ||
let string = type; | ||
// append parameters | ||
if (parameters && typeof parameters == 'object') { | ||
const params = Object.keys(parameters).sort(); | ||
for (const param of params) { | ||
if (!TOKEN_REGEXP.test(param)) { | ||
throw new TypeError('invalid parameter name'); | ||
} | ||
string += '; ' + param + '=' + qstring(parameters[param]); | ||
} | ||
string += '; ' + param + '=' + qstring(parameters[param]); | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} finally { | ||
_iterator.f(); | ||
} | ||
} | ||
return string; | ||
return string; | ||
} | ||
@@ -238,56 +106,43 @@ /** | ||
*/ | ||
function parse(string) { | ||
if (!string) { | ||
throw new TypeError('argument string is required'); | ||
} // support req/res-like objects as argument | ||
var header = _typeof(string) == 'object' ? getcontenttype(string) : string; | ||
if (typeof header !== 'string') { | ||
throw new TypeError('argument string is required to be a string'); | ||
} | ||
var index = header.indexOf(';'); | ||
var type = index != -1 ? header.substr(0, index).trim() : header.trim(); | ||
if (!TYPE_REGEXP.test(type)) { | ||
throw new TypeError('invalid media type'); | ||
} | ||
var obj = new ContentType(type.toLowerCase()); // parse parameters | ||
if (index != -1) { | ||
var key; | ||
var match; | ||
var value; | ||
PARAM_REGEXP.lastIndex = index; | ||
while (match = PARAM_REGEXP.exec(header)) { | ||
if (match.index !== index) { | ||
throw new TypeError('invalid parameter format'); | ||
} | ||
index += match[0].length; | ||
key = match[1].toLowerCase(); | ||
value = match[2]; | ||
if (value[0] == '"') { | ||
// remove quotes and escapes | ||
value = value.substr(1, value.length - 2).replace(QESC_REGEXP, '$1'); | ||
} | ||
obj.parameters[key] = value; | ||
if (!string) { | ||
throw new TypeError('argument string is required'); | ||
} | ||
if (index != header.length) { | ||
throw new TypeError('invalid parameter format'); | ||
// support req/res-like objects as argument | ||
const header = typeof string == 'object' ? getcontenttype(string) : string; | ||
if (typeof header !== 'string') { | ||
throw new TypeError('argument string is required to be a string'); | ||
} | ||
} | ||
return obj; | ||
let index = header.indexOf(';'); | ||
const type = index != -1 ? header.substr(0, index).trim() : header.trim(); | ||
if (!TYPE_REGEXP.test(type)) { | ||
throw new TypeError('invalid media type'); | ||
} | ||
const obj = new ContentType(type.toLowerCase()); | ||
// parse parameters | ||
if (index != -1) { | ||
let key; | ||
let match; | ||
let value; | ||
PARAM_REGEXP.lastIndex = index; | ||
while ((match = PARAM_REGEXP.exec(header))) { | ||
if (match.index !== index) { | ||
throw new TypeError('invalid parameter format'); | ||
} | ||
index += match[0].length; | ||
key = match[1].toLowerCase(); | ||
value = match[2]; | ||
if (value[0] == '"') { | ||
// remove quotes and escapes | ||
value = value.substr(1, value.length - 2).replace(QESC_REGEXP, '$1'); | ||
} | ||
obj.parameters[key] = value; | ||
} | ||
if (index != header.length) { | ||
throw new TypeError('invalid parameter format'); | ||
} | ||
} | ||
return obj; | ||
} | ||
exports.format = format; | ||
exports.parse = parse; | ||
export { format, parse }; |
{ | ||
"name": "es-content-type", | ||
"description": "content-type rewrite in TypeScript with ESM and CommonJS targets", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"repository": "https://github.com/talentlessguy/es-content-type.git", | ||
@@ -15,9 +15,10 @@ "engines": { | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"module": "dist/index.esm.js", | ||
"type": "module", | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.esm.js", | ||
"require": "./dist/index.js" | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
}, | ||
@@ -29,3 +30,3 @@ "./package.json": "./package.json", | ||
"@types/node": "^14.0.22", | ||
"bili": "^5.0.5", | ||
"rollup": "^2.21.0", | ||
"rollup-plugin-typescript2": "^0.27.1", | ||
@@ -35,4 +36,4 @@ "typescript": "^3.9.6" | ||
"scripts": { | ||
"build": "bili --format esm --format cjs src/index.ts" | ||
"build": "rollup -c" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26252
8
672
Yes
3