content-type 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/HISTORY.md +10 -0
  2. package/README.md +21 -19
  3. package/index.js +32 -25
  4. package/package.json +13 -11
package/HISTORY.md CHANGED
@@ -1,3 +1,13 @@
1
+ 1.0.5 / 2023-01-29
2
+ ==================
3
+
4
+ * perf: skip value escaping when unnecessary
5
+
6
+ 1.0.4 / 2017-09-11
7
+ ==================
8
+
9
+ * perf: skip parameter parsing when no parameters
10
+
1
11
  1.0.3 / 2017-09-10
2
12
  ==================
3
13
 
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # content-type
2
2
 
3
- [![NPM Version][npm-image]][npm-url]
4
- [![NPM Downloads][downloads-image]][downloads-url]
5
- [![Node.js Version][node-version-image]][node-version-url]
6
- [![Build Status][travis-image]][travis-url]
7
- [![Test Coverage][coveralls-image]][coveralls-url]
3
+ [![NPM Version][npm-version-image]][npm-url]
4
+ [![NPM Downloads][npm-downloads-image]][npm-url]
5
+ [![Node.js Version][node-image]][node-url]
6
+ [![Build Status][ci-image]][ci-url]
7
+ [![Coverage Status][coveralls-image]][coveralls-url]
8
8
 
9
9
  Create and parse HTTP Content-Type header according to RFC 7231
10
10
 
@@ -26,7 +26,7 @@ var contentType = require('content-type')
26
26
  var obj = contentType.parse('image/svg+xml; charset=utf-8')
27
27
  ```
28
28
 
29
- Parse a content type string. This will return an object with the following
29
+ Parse a `Content-Type` header. This will return an object with the following
30
30
  properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`):
31
31
 
32
32
  - `type`: The media type (the type and subtype, always lower case).
@@ -43,7 +43,7 @@ Throws a `TypeError` if the string is missing or invalid.
43
43
  var obj = contentType.parse(req)
44
44
  ```
45
45
 
46
- Parse the `content-type` header from the given `req`. Short-cut for
46
+ Parse the `Content-Type` header from the given `req`. Short-cut for
47
47
  `contentType.parse(req.headers['content-type'])`.
48
48
 
49
49
  Throws a `TypeError` if the `Content-Type` header is missing or invalid.
@@ -54,7 +54,7 @@ Throws a `TypeError` if the `Content-Type` header is missing or invalid.
54
54
  var obj = contentType.parse(res)
55
55
  ```
56
56
 
57
- Parse the `content-type` header set on the given `res`. Short-cut for
57
+ Parse the `Content-Type` header set on the given `res`. Short-cut for
58
58
  `contentType.parse(res.getHeader('content-type'))`.
59
59
 
60
60
  Throws a `TypeError` if the `Content-Type` header is missing or invalid.
@@ -62,10 +62,13 @@ Throws a `TypeError` if the `Content-Type` header is missing or invalid.
62
62
  ### contentType.format(obj)
63
63
 
64
64
  ```js
65
- var str = contentType.format({type: 'image/svg+xml'})
65
+ var str = contentType.format({
66
+ type: 'image/svg+xml',
67
+ parameters: { charset: 'utf-8' }
68
+ })
66
69
  ```
67
70
 
68
- Format an object into a content type string. This will return a string of the
71
+ Format an object into a `Content-Type` header. This will return a string of the
69
72
  content type for the given object with the following properties (examples are
70
73
  shown that produce the string `'image/svg+xml; charset=utf-8'`):
71
74
 
@@ -80,13 +83,12 @@ Throws a `TypeError` if the object contains an invalid type or parameter names.
80
83
 
81
84
  [MIT](LICENSE)
82
85
 
83
- [npm-image]: https://img.shields.io/npm/v/content-type.svg
86
+ [ci-image]: https://badgen.net/github/checks/jshttp/content-type/master?label=ci
87
+ [ci-url]: https://github.com/jshttp/content-type/actions/workflows/ci.yml
88
+ [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/content-type/master
89
+ [coveralls-url]: https://coveralls.io/r/jshttp/content-type?branch=master
90
+ [node-image]: https://badgen.net/npm/node/content-type
91
+ [node-url]: https://nodejs.org/en/download
92
+ [npm-downloads-image]: https://badgen.net/npm/dm/content-type
84
93
  [npm-url]: https://npmjs.org/package/content-type
85
- [node-version-image]: https://img.shields.io/node/v/content-type.svg
86
- [node-version-url]: http://nodejs.org/download/
87
- [travis-image]: https://img.shields.io/travis/jshttp/content-type/master.svg
88
- [travis-url]: https://travis-ci.org/jshttp/content-type
89
- [coveralls-image]: https://img.shields.io/coveralls/jshttp/content-type/master.svg
90
- [coveralls-url]: https://coveralls.io/r/jshttp/content-type
91
- [downloads-image]: https://img.shields.io/npm/dm/content-type.svg
92
- [downloads-url]: https://npmjs.org/package/content-type
94
+ [npm-version-image]: https://badgen.net/npm/v/content-type
package/index.js CHANGED
@@ -20,8 +20,8 @@
20
20
  * obs-text = %x80-FF
21
21
  * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
22
22
  */
23
- var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g
24
- var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/
23
+ var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g // eslint-disable-line no-control-regex
24
+ var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/ // eslint-disable-line no-control-regex
25
25
  var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
26
26
 
27
27
  /**
@@ -30,7 +30,7 @@ var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
30
30
  * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
31
31
  * obs-text = %x80-FF
32
32
  */
33
- var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g
33
+ var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g // eslint-disable-line no-control-regex
34
34
 
35
35
  /**
36
36
  * RegExp to match chars that must be quoted-pair in RFC 7230 sec 3.2.6
@@ -119,41 +119,48 @@ function parse (string) {
119
119
 
120
120
  var index = header.indexOf(';')
121
121
  var type = index !== -1
122
- ? header.substr(0, index).trim()
122
+ ? header.slice(0, index).trim()
123
123
  : header.trim()
124
124
 
125
125
  if (!TYPE_REGEXP.test(type)) {
126
126
  throw new TypeError('invalid media type')
127
127
  }
128
128
 
129
- var key
130
- var match
131
129
  var obj = new ContentType(type.toLowerCase())
132
- var value
133
130
 
134
- PARAM_REGEXP.lastIndex = index
131
+ // parse parameters
132
+ if (index !== -1) {
133
+ var key
134
+ var match
135
+ var value
135
136
 
136
- while ((match = PARAM_REGEXP.exec(header))) {
137
- if (match.index !== index) {
138
- throw new TypeError('invalid parameter format')
139
- }
137
+ PARAM_REGEXP.lastIndex = index
140
138
 
141
- index += match[0].length
142
- key = match[1].toLowerCase()
143
- value = match[2]
139
+ while ((match = PARAM_REGEXP.exec(header))) {
140
+ if (match.index !== index) {
141
+ throw new TypeError('invalid parameter format')
142
+ }
144
143
 
145
- if (value[0] === '"') {
146
- // remove quotes and escapes
147
- value = value
148
- .substr(1, value.length - 2)
149
- .replace(QESC_REGEXP, '$1')
150
- }
144
+ index += match[0].length
145
+ key = match[1].toLowerCase()
146
+ value = match[2]
151
147
 
152
- obj.parameters[key] = value
153
- }
148
+ if (value.charCodeAt(0) === 0x22 /* " */) {
149
+ // remove quotes
150
+ value = value.slice(1, -1)
154
151
 
155
- if (index !== -1 && index !== header.length) {
156
- throw new TypeError('invalid parameter format')
152
+ // remove escapes
153
+ if (value.indexOf('\\') !== -1) {
154
+ value = value.replace(QESC_REGEXP, '$1')
155
+ }
156
+ }
157
+
158
+ obj.parameters[key] = value
159
+ }
160
+
161
+ if (index !== header.length) {
162
+ throw new TypeError('invalid parameter format')
163
+ }
157
164
  }
158
165
 
159
166
  return obj
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "content-type",
3
3
  "description": "Create and parse HTTP Content-Type header",
4
- "version": "1.0.3",
4
+ "version": "1.0.5",
5
5
  "author": "Douglas Christopher Wilson <doug@somethingdoug.com>",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -13,14 +13,15 @@
13
13
  ],
14
14
  "repository": "jshttp/content-type",
15
15
  "devDependencies": {
16
- "eslint": "3.19.0",
17
- "eslint-config-standard": "10.2.1",
18
- "eslint-plugin-import": "2.7.0",
19
- "eslint-plugin-node": "5.1.1",
20
- "eslint-plugin-promise": "3.5.0",
21
- "eslint-plugin-standard": "3.0.1",
22
- "istanbul": "0.4.5",
23
- "mocha": "~1.21.5"
16
+ "deep-equal": "1.0.1",
17
+ "eslint": "8.32.0",
18
+ "eslint-config-standard": "15.0.1",
19
+ "eslint-plugin-import": "2.27.5",
20
+ "eslint-plugin-node": "11.1.0",
21
+ "eslint-plugin-promise": "6.1.1",
22
+ "eslint-plugin-standard": "4.1.0",
23
+ "mocha": "10.2.0",
24
+ "nyc": "15.1.0"
24
25
  },
25
26
  "files": [
26
27
  "LICENSE",
@@ -34,7 +35,8 @@
34
35
  "scripts": {
35
36
  "lint": "eslint .",
36
37
  "test": "mocha --reporter spec --check-leaks --bail test/",
37
- "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
38
- "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/"
38
+ "test-ci": "nyc --reporter=lcovonly --reporter=text npm test",
39
+ "test-cov": "nyc --reporter=html --reporter=text npm test",
40
+ "version": "node scripts/version-history.js && git add HISTORY.md"
39
41
  }
40
42
  }