es-content-type
Advanced tools
Comparing version 0.0.10 to 0.1.0
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
@@ -3,0 +2,0 @@ |
@@ -1,1 +0,1 @@ | ||
import{IncomingMessage as Request,ServerResponse as Response}from"http";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-]+$/;const QESC_REGEXP=/\\([\u000b\u0020-\u00ff])/g;const QUOTE_REGEXP=/([\\"])/g;const TYPE_REGEXP=/^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;function qstring(val){const str=String(val);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){let header;if(obj instanceof Response&&typeof obj.getHeader==="function"){header=obj.getHeader("content-type")}else if(obj instanceof Request&&typeof obj.headers==="object"){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}class ContentType{constructor(type){this.parameters={};this.type=type}}function format(obj){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;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])}}return string}function parse(string){if(!string){throw new TypeError("argument string is required")}const header=typeof string=="object"?getcontenttype(string):string;if(typeof header!=="string"){throw new TypeError("argument string is required to be a string")}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());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]=='"'){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}export{format,parse}; | ||
import{IncomingMessage as Request,ServerResponse as Response}from"http";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-]+$/;var QESC_REGEXP=/\\([\u000b\u0020-\u00ff])/g;var QUOTE_REGEXP=/([\\"])/g;var TYPE_REGEXP=/^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;function qstring(val){const str=String(val);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){let header;if(obj instanceof Response&&typeof obj.getHeader==="function"){header=obj.getHeader("content-type")}else if(obj instanceof Request&&typeof obj.headers==="object"){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}var ContentType=class{constructor(type){this.parameters={};this.type=type}};function format(obj){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;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])}}return string}function parse(string){if(!string)throw new TypeError("argument string is required");const header=typeof string=="object"?getcontenttype(string):string;if(typeof header!=="string")throw new TypeError("argument string is required to be a string");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());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]=='"'){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}export{format,parse}; |
{ | ||
"name": "es-content-type", | ||
"description": "content-type rewrite in TypeScript with ESM and CommonJS targets", | ||
"version": "0.0.10", | ||
"repository": "https://github.com/talentlessguy/es-content-type.git", | ||
"engines": { | ||
"node": ">=12.x" | ||
}, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"author": "talentlessguy <pilll.PL22@gmail.com>", | ||
"license": "MIT", | ||
"type": "module", | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
}, | ||
"./package.json": "./package.json", | ||
"./": "./" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.6.0", | ||
"tsup": "^3.6.1", | ||
"typescript": "^4.0.2" | ||
}, | ||
"scripts": { | ||
"prepare": "pnpm build", | ||
"build": "tsup src/index.ts --minify-whitespace --format cjs,esm --dts" | ||
} | ||
"name": "es-content-type", | ||
"description": "content-type rewrite in TypeScript with ESM and CommonJS targets", | ||
"version": "0.1.0", | ||
"repository": "https://github.com/talentlessguy/es-content-type.git", | ||
"engines": { | ||
"node": ">=12.x" | ||
}, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"author": "talentlessguy <pilll.PL22@gmail.com>", | ||
"license": "MIT", | ||
"type": "module", | ||
"types": "./dist/index.d.ts", | ||
"exports": "./dist/index.js", | ||
"devDependencies": { | ||
"@types/node": "^15.12.2", | ||
"tsup": "^4.11.2", | ||
"typescript": "^4.3.2" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --minify-whitespace --format esm --dts" | ||
} | ||
} |
# es-content-type | ||
[![Twitter](https://img.shields.io/twitter/follow/v1rtl.svg?label=twitter&style=flat-square)](https://twitter.com/v1rtl) | ||
![Top lang](https://img.shields.io/github/languages/top/talentlessguy/es-content-type.svg?style=flat-square) | ||
![Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/es-content-type.svg?style=flat-square) | ||
[![Version](https://img.shields.io/npm/v/es-content-type.svg?style=flat-square)](https://npm.im/es-content-type) | ||
![Last commit](https://img.shields.io/github/last-commit/talentlessguy/es-content-type.svg?style=flat-square) | ||
> [`content-type`](https://github.com/jshttp/content-type) rewrite in TypeScript with ESM and CommonJS targets. | ||
@@ -79,5 +78,1 @@ | ||
Throws a `TypeError` if the object contains an invalid type or parameter names. | ||
## License | ||
MIT © [v1rtl](https://v1rtl.site) |
@@ -47,9 +47,6 @@ import { IncomingMessage as Request, ServerResponse as Response } from 'http' | ||
// no need to quote tokens | ||
if (TOKEN_REGEXP.test(str)) { | ||
return str | ||
} | ||
if (TOKEN_REGEXP.test(str)) return str | ||
if (str.length > 0 && !TEXT_REGEXP.test(str)) { | ||
throw new TypeError('invalid parameter value') | ||
} | ||
if (str.length > 0 && !TEXT_REGEXP.test(str)) throw new TypeError('invalid parameter value') | ||
@@ -94,11 +91,8 @@ return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"' | ||
export function format(obj: any) { | ||
if (!obj || typeof obj !== 'object') { | ||
throw new TypeError('argument obj is required') | ||
} | ||
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') | ||
} | ||
if (!type || !TYPE_REGEXP.test(type)) throw new TypeError('invalid type') | ||
@@ -112,5 +106,3 @@ let string = type | ||
for (const param of params) { | ||
if (!TOKEN_REGEXP.test(param)) { | ||
throw new TypeError('invalid parameter name') | ||
} | ||
if (!TOKEN_REGEXP.test(param)) throw new TypeError('invalid parameter name') | ||
@@ -128,5 +120,3 @@ string += '; ' + param + '=' + qstring(parameters[param]) | ||
export function parse(string: string | Request | Response) { | ||
if (!string) { | ||
throw new TypeError('argument string is required') | ||
} | ||
if (!string) throw new TypeError('argument string is required') | ||
@@ -136,5 +126,4 @@ // support req/res-like objects as argument | ||
if (typeof header !== 'string') { | ||
throw new TypeError('argument string is required to be a string') | ||
} | ||
if (typeof header !== 'string') throw new TypeError('argument string is required to be a string') | ||
@@ -144,5 +133,4 @@ let index = header.indexOf(';') | ||
if (!TYPE_REGEXP.test(type)) { | ||
throw new TypeError('invalid media type') | ||
} | ||
if (!TYPE_REGEXP.test(type)) throw new TypeError('invalid media type') | ||
@@ -160,5 +148,4 @@ const obj = new ContentType(type.toLowerCase()) | ||
while ((match = PARAM_REGEXP.exec(header))) { | ||
if (match.index !== index) { | ||
throw new TypeError('invalid parameter format') | ||
} | ||
if (match.index !== index) throw new TypeError('invalid parameter format') | ||
@@ -177,5 +164,3 @@ index += match[0].length | ||
if (index != header.length) { | ||
throw new TypeError('invalid parameter format') | ||
} | ||
if (index != header.length) throw new TypeError('invalid parameter format') | ||
} | ||
@@ -182,0 +167,0 @@ |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
2
1
11268
6
149
78