@httpland/compression-middleware 1.0.0-beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +208 -0
  3. package/esm/deps/deno.land/std@0.180.0/http/_negotiation/common.js +40 -0
  4. package/esm/deps/deno.land/std@0.180.0/http/_negotiation/encoding.js +121 -0
  5. package/esm/deps/deno.land/std@0.180.0/http/_negotiation/language.js +110 -0
  6. package/esm/deps/deno.land/std@0.180.0/http/_negotiation/media_type.js +174 -0
  7. package/esm/deps/deno.land/std@0.180.0/http/negotiation.js +36 -0
  8. package/esm/deps/deno.land/std@0.180.0/media_types/_db.js +33 -0
  9. package/esm/deps/deno.land/std@0.180.0/media_types/_util.js +122 -0
  10. package/esm/deps/deno.land/std@0.180.0/media_types/content_type.js +54 -0
  11. package/esm/deps/deno.land/std@0.180.0/media_types/extension.js +25 -0
  12. package/esm/deps/deno.land/std@0.180.0/media_types/extensions_by_type.js +30 -0
  13. package/esm/deps/deno.land/std@0.180.0/media_types/format_media_type.js +60 -0
  14. package/esm/deps/deno.land/std@0.180.0/media_types/get_charset.js +36 -0
  15. package/esm/deps/deno.land/std@0.180.0/media_types/mod.js +22 -0
  16. package/esm/deps/deno.land/std@0.180.0/media_types/parse_media_type.js +121 -0
  17. package/esm/deps/deno.land/std@0.180.0/media_types/type_by_extension.js +23 -0
  18. package/esm/deps/deno.land/std@0.180.0/media_types/vendor/mime-db.v1.52.0.js +8555 -0
  19. package/esm/deps/deno.land/x/vary@1.0.0/mod.js +108 -0
  20. package/esm/deps.js +8 -0
  21. package/esm/encoders/deflate.js +11 -0
  22. package/esm/encoders/gzip.js +11 -0
  23. package/esm/encoders/utils.js +7 -0
  24. package/esm/middleware.js +55 -0
  25. package/esm/mod.js +3 -0
  26. package/esm/package.json +3 -0
  27. package/esm/transform.js +23 -0
  28. package/esm/types.js +3 -0
  29. package/esm/utils.js +19 -0
  30. package/package.json +55 -0
  31. package/script/deps/deno.land/std@0.180.0/http/_negotiation/common.js +45 -0
  32. package/script/deps/deno.land/std@0.180.0/http/_negotiation/encoding.js +125 -0
  33. package/script/deps/deno.land/std@0.180.0/http/_negotiation/language.js +114 -0
  34. package/script/deps/deno.land/std@0.180.0/http/_negotiation/media_type.js +178 -0
  35. package/script/deps/deno.land/std@0.180.0/http/negotiation.js +42 -0
  36. package/script/deps/deno.land/std@0.180.0/media_types/_db.js +39 -0
  37. package/script/deps/deno.land/std@0.180.0/media_types/_util.js +132 -0
  38. package/script/deps/deno.land/std@0.180.0/media_types/content_type.js +58 -0
  39. package/script/deps/deno.land/std@0.180.0/media_types/extension.js +29 -0
  40. package/script/deps/deno.land/std@0.180.0/media_types/extensions_by_type.js +34 -0
  41. package/script/deps/deno.land/std@0.180.0/media_types/format_media_type.js +64 -0
  42. package/script/deps/deno.land/std@0.180.0/media_types/get_charset.js +40 -0
  43. package/script/deps/deno.land/std@0.180.0/media_types/mod.js +38 -0
  44. package/script/deps/deno.land/std@0.180.0/media_types/parse_media_type.js +125 -0
  45. package/script/deps/deno.land/std@0.180.0/media_types/type_by_extension.js +27 -0
  46. package/script/deps/deno.land/std@0.180.0/media_types/vendor/mime-db.v1.52.0.js +8557 -0
  47. package/script/deps/deno.land/x/vary@1.0.0/mod.js +113 -0
  48. package/script/deps.js +22 -0
  49. package/script/encoders/deflate.js +14 -0
  50. package/script/encoders/gzip.js +14 -0
  51. package/script/encoders/utils.js +10 -0
  52. package/script/middleware.js +61 -0
  53. package/script/mod.js +7 -0
  54. package/script/package.json +3 -0
  55. package/script/transform.js +27 -0
  56. package/script/types.js +4 -0
  57. package/script/utils.js +24 -0
  58. package/types/deps/deno.land/std@0.180.0/http/_negotiation/common.d.ts +37 -0
  59. package/types/deps/deno.land/std@0.180.0/http/_negotiation/encoding.d.ts +33 -0
  60. package/types/deps/deno.land/std@0.180.0/http/_negotiation/language.d.ts +30 -0
  61. package/types/deps/deno.land/std@0.180.0/http/_negotiation/media_type.d.ts +30 -0
  62. package/types/deps/deno.land/std@0.180.0/http/negotiation.d.ts +126 -0
  63. package/types/deps/deno.land/std@0.180.0/media_types/_db.d.ts +5 -0
  64. package/types/deps/deno.land/std@0.180.0/media_types/_util.d.ts +21 -0
  65. package/types/deps/deno.land/std@0.180.0/media_types/content_type.d.ts +38 -0
  66. package/types/deps/deno.land/std@0.180.0/media_types/extension.d.ts +17 -0
  67. package/types/deps/deno.land/std@0.180.0/media_types/extensions_by_type.d.ts +20 -0
  68. package/types/deps/deno.land/std@0.180.0/media_types/format_media_type.d.ts +16 -0
  69. package/types/deps/deno.land/std@0.180.0/media_types/get_charset.d.ts +15 -0
  70. package/types/deps/deno.land/std@0.180.0/media_types/mod.d.ts +20 -0
  71. package/types/deps/deno.land/std@0.180.0/media_types/parse_media_type.d.ts +37 -0
  72. package/types/deps/deno.land/std@0.180.0/media_types/type_by_extension.d.ts +17 -0
  73. package/types/deps/deno.land/std@0.180.0/media_types/vendor/mime-db.v1.52.0.d.ts +8518 -0
  74. package/types/deps/deno.land/x/vary@1.0.0/mod.d.ts +23 -0
  75. package/types/deps.d.ts +7 -0
  76. package/types/encoders/deflate.d.ts +3 -0
  77. package/types/encoders/gzip.d.ts +3 -0
  78. package/types/encoders/utils.d.ts +4 -0
  79. package/types/middleware.d.ts +28 -0
  80. package/types/mod.d.ts +2 -0
  81. package/types/transform.d.ts +3 -0
  82. package/types/types.d.ts +15 -0
  83. package/types/utils.d.ts +2 -0
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+ /*!
4
+ * Adapted directly from negotiator at https://github.com/jshttp/negotiator/
5
+ * which is licensed as follows:
6
+ *
7
+ * (The MIT License)
8
+ *
9
+ * Copyright (c) 2012-2014 Federico Romero
10
+ * Copyright (c) 2012-2014 Isaac Z. Schlueter
11
+ * Copyright (c) 2014-2015 Douglas Christopher Wilson
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining
14
+ * a copy of this software and associated documentation files (the
15
+ * 'Software'), to deal in the Software without restriction, including
16
+ * without limitation the rights to use, copy, modify, merge, publish,
17
+ * distribute, sublicense, and/or sell copies of the Software, and to
18
+ * permit persons to whom the Software is furnished to do so, subject to
19
+ * the following conditions:
20
+ *
21
+ * The above copyright notice and this permission notice shall be
22
+ * included in all copies or substantial portions of the Software.
23
+ *
24
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+ */
32
+ Object.defineProperty(exports, "__esModule", { value: true });
33
+ exports.preferredMediaTypes = void 0;
34
+ const common_js_1 = require("./common.js");
35
+ const simpleMediaTypeRegExp = /^\s*([^\s\/;]+)\/([^;\s]+)\s*(?:;(.*))?$/;
36
+ function quoteCount(str) {
37
+ let count = 0;
38
+ let index = 0;
39
+ while ((index = str.indexOf(`"`, index)) !== -1) {
40
+ count++;
41
+ index++;
42
+ }
43
+ return count;
44
+ }
45
+ function splitMediaTypes(accept) {
46
+ const accepts = accept.split(",");
47
+ let j = 0;
48
+ for (let i = 1; i < accepts.length; i++) {
49
+ if (quoteCount(accepts[j]) % 2 === 0) {
50
+ accepts[++j] = accepts[i];
51
+ }
52
+ else {
53
+ accepts[j] += `,${accepts[i]}`;
54
+ }
55
+ }
56
+ accepts.length = j + 1;
57
+ return accepts;
58
+ }
59
+ function splitParameters(str) {
60
+ const parameters = str.split(";");
61
+ let j = 0;
62
+ for (let i = 1; i < parameters.length; i++) {
63
+ if (quoteCount(parameters[j]) % 2 === 0) {
64
+ parameters[++j] = parameters[i];
65
+ }
66
+ else {
67
+ parameters[j] += `;${parameters[i]}`;
68
+ }
69
+ }
70
+ parameters.length = j + 1;
71
+ return parameters.map((p) => p.trim());
72
+ }
73
+ function splitKeyValuePair(str) {
74
+ const [key, value] = str.split("=");
75
+ return [key.toLowerCase(), value];
76
+ }
77
+ function parseMediaType(str, i) {
78
+ const match = simpleMediaTypeRegExp.exec(str);
79
+ if (!match) {
80
+ return;
81
+ }
82
+ const params = Object.create(null);
83
+ let q = 1;
84
+ const [, type, subtype, parameters] = match;
85
+ if (parameters) {
86
+ const kvps = splitParameters(parameters).map(splitKeyValuePair);
87
+ for (const [key, val] of kvps) {
88
+ const value = val && val[0] === `"` && val[val.length - 1] === `"`
89
+ ? val.slice(1, val.length - 1)
90
+ : val;
91
+ if (key === "q" && value) {
92
+ q = parseFloat(value);
93
+ break;
94
+ }
95
+ params[key] = value;
96
+ }
97
+ }
98
+ return { type, subtype, params, q, i };
99
+ }
100
+ function parseAccept(accept) {
101
+ const accepts = splitMediaTypes(accept);
102
+ const mediaTypes = [];
103
+ for (let i = 0; i < accepts.length; i++) {
104
+ const mediaType = parseMediaType(accepts[i].trim(), i);
105
+ if (mediaType) {
106
+ mediaTypes.push(mediaType);
107
+ }
108
+ }
109
+ return mediaTypes;
110
+ }
111
+ function getFullType(spec) {
112
+ return `${spec.type}/${spec.subtype}`;
113
+ }
114
+ function specify(type, spec, index) {
115
+ const p = parseMediaType(type, index);
116
+ if (!p) {
117
+ return;
118
+ }
119
+ let s = 0;
120
+ if (spec.type.toLowerCase() === p.type.toLowerCase()) {
121
+ s |= 4;
122
+ }
123
+ else if (spec.type !== "*") {
124
+ return;
125
+ }
126
+ if (spec.subtype.toLowerCase() === p.subtype.toLowerCase()) {
127
+ s |= 2;
128
+ }
129
+ else if (spec.subtype !== "*") {
130
+ return;
131
+ }
132
+ const keys = Object.keys(spec.params);
133
+ if (keys.length) {
134
+ if (keys.every((key) => (spec.params[key] || "").toLowerCase() ===
135
+ (p.params[key] || "").toLowerCase())) {
136
+ s |= 1;
137
+ }
138
+ else {
139
+ return;
140
+ }
141
+ }
142
+ return {
143
+ i: index,
144
+ o: spec.o,
145
+ q: spec.q,
146
+ s,
147
+ };
148
+ }
149
+ function getMediaTypePriority(type, accepted, index) {
150
+ let priority = { o: -1, q: 0, s: 0, i: index };
151
+ for (const accepts of accepted) {
152
+ const spec = specify(type, accepts, index);
153
+ if (spec &&
154
+ ((priority.s || 0) - (spec.s || 0) ||
155
+ (priority.q || 0) - (spec.q || 0) ||
156
+ (priority.o || 0) - (spec.o || 0)) < 0) {
157
+ priority = spec;
158
+ }
159
+ }
160
+ return priority;
161
+ }
162
+ function preferredMediaTypes(accept, provided) {
163
+ const accepts = parseAccept(accept === undefined ? "*/*" : accept || "");
164
+ if (!provided) {
165
+ return accepts
166
+ .filter(common_js_1.isQuality)
167
+ .sort(common_js_1.compareSpecs)
168
+ .map(getFullType);
169
+ }
170
+ const priorities = provided.map((type, index) => {
171
+ return getMediaTypePriority(type, accepts, index);
172
+ });
173
+ return priorities
174
+ .filter(common_js_1.isQuality)
175
+ .sort(common_js_1.compareSpecs)
176
+ .map((priority) => provided[priorities.indexOf(priority)]);
177
+ }
178
+ exports.preferredMediaTypes = preferredMediaTypes;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.acceptsLanguages = exports.acceptsEncodings = exports.accepts = void 0;
5
+ /**
6
+ * Contains the functions {@linkcode accepts}, {@linkcode acceptsEncodings}, and
7
+ * {@linkcode acceptsLanguages} to provide content negotiation capabilities.
8
+ *
9
+ * @module
10
+ */
11
+ const encoding_js_1 = require("./_negotiation/encoding.js");
12
+ const language_js_1 = require("./_negotiation/language.js");
13
+ const media_type_js_1 = require("./_negotiation/media_type.js");
14
+ function accepts(request, ...types) {
15
+ const accept = request.headers.get("accept");
16
+ return types.length
17
+ ? accept ? (0, media_type_js_1.preferredMediaTypes)(accept, types)[0] : types[0]
18
+ : accept
19
+ ? (0, media_type_js_1.preferredMediaTypes)(accept)
20
+ : ["*/*"];
21
+ }
22
+ exports.accepts = accepts;
23
+ function acceptsEncodings(request, ...encodings) {
24
+ const acceptEncoding = request.headers.get("accept-encoding");
25
+ return encodings.length
26
+ ? acceptEncoding
27
+ ? (0, encoding_js_1.preferredEncodings)(acceptEncoding, encodings)[0]
28
+ : encodings[0]
29
+ : acceptEncoding
30
+ ? (0, encoding_js_1.preferredEncodings)(acceptEncoding)
31
+ : ["*"];
32
+ }
33
+ exports.acceptsEncodings = acceptsEncodings;
34
+ function acceptsLanguages(request, ...langs) {
35
+ const acceptLanguage = request.headers.get("accept-language");
36
+ return langs.length
37
+ ? acceptLanguage ? (0, language_js_1.preferredLanguages)(acceptLanguage, langs)[0] : langs[0]
38
+ : acceptLanguage
39
+ ? (0, language_js_1.preferredLanguages)(acceptLanguage)
40
+ : ["*"];
41
+ }
42
+ exports.acceptsLanguages = acceptsLanguages;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.db = exports.types = void 0;
7
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
8
+ const mime_db_v1_52_0_js_1 = __importDefault(require("./vendor/mime-db.v1.52.0.js"));
9
+ exports.db = mime_db_v1_52_0_js_1.default;
10
+ const _util_js_1 = require("./_util.js");
11
+ /** A map of the media type for a given extension */
12
+ exports.types = new Map();
13
+ /** Internal function to populate the maps based on the Mime DB. */
14
+ (function populateMaps() {
15
+ const preference = ["nginx", "apache", undefined, "iana"];
16
+ for (const type of Object.keys(mime_db_v1_52_0_js_1.default)) {
17
+ const mime = mime_db_v1_52_0_js_1.default[type];
18
+ const exts = mime.extensions;
19
+ if (!exts || !exts.length) {
20
+ continue;
21
+ }
22
+ // @ts-ignore work around denoland/dnt#148
23
+ _util_js_1.extensions.set(type, exts);
24
+ for (const ext of exts) {
25
+ const current = exports.types.get(ext);
26
+ if (current) {
27
+ const from = preference.indexOf(mime_db_v1_52_0_js_1.default[current].source);
28
+ const to = preference.indexOf(mime.source);
29
+ if (current !== "application/octet-stream" &&
30
+ (from > to ||
31
+ // @ts-ignore work around denoland/dnt#148
32
+ (from === to && current.startsWith("application/")))) {
33
+ continue;
34
+ }
35
+ }
36
+ exports.types.set(ext, type);
37
+ }
38
+ }
39
+ })();
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.needsEncoding = exports.isToken = exports.isIterator = exports.decode2331Encoding = exports.consumeMediaParam = exports.consumeValue = exports.consumeToken = exports.extensions = void 0;
5
+ /** A map of extensions for a given media type. */
6
+ exports.extensions = new Map();
7
+ function consumeToken(v) {
8
+ const notPos = indexOf(v, isNotTokenChar);
9
+ if (notPos == -1) {
10
+ return [v, ""];
11
+ }
12
+ if (notPos == 0) {
13
+ return ["", v];
14
+ }
15
+ return [v.slice(0, notPos), v.slice(notPos)];
16
+ }
17
+ exports.consumeToken = consumeToken;
18
+ function consumeValue(v) {
19
+ if (!v) {
20
+ return ["", v];
21
+ }
22
+ if (v[0] !== `"`) {
23
+ return consumeToken(v);
24
+ }
25
+ let value = "";
26
+ for (let i = 1; i < v.length; i++) {
27
+ const r = v[i];
28
+ if (r === `"`) {
29
+ return [value, v.slice(i + 1)];
30
+ }
31
+ if (r === "\\" && i + 1 < v.length && isTSpecial(v[i + 1])) {
32
+ value += v[i + 1];
33
+ i++;
34
+ continue;
35
+ }
36
+ if (r === "\r" || r === "\n") {
37
+ return ["", v];
38
+ }
39
+ value += v[i];
40
+ }
41
+ return ["", v];
42
+ }
43
+ exports.consumeValue = consumeValue;
44
+ function consumeMediaParam(v) {
45
+ let rest = v.trimStart();
46
+ if (!rest.startsWith(";")) {
47
+ return ["", "", v];
48
+ }
49
+ rest = rest.slice(1);
50
+ rest = rest.trimStart();
51
+ let param;
52
+ [param, rest] = consumeToken(rest);
53
+ param = param.toLowerCase();
54
+ if (!param) {
55
+ return ["", "", v];
56
+ }
57
+ rest = rest.slice(1);
58
+ rest = rest.trimStart();
59
+ const [value, rest2] = consumeValue(rest);
60
+ if (value == "" && rest2 === rest) {
61
+ return ["", "", v];
62
+ }
63
+ rest = rest2;
64
+ return [param, value, rest];
65
+ }
66
+ exports.consumeMediaParam = consumeMediaParam;
67
+ function decode2331Encoding(v) {
68
+ const sv = v.split(`'`, 3);
69
+ if (sv.length !== 3) {
70
+ return undefined;
71
+ }
72
+ const charset = sv[0].toLowerCase();
73
+ if (!charset) {
74
+ return undefined;
75
+ }
76
+ if (charset != "us-ascii" && charset != "utf-8") {
77
+ return undefined;
78
+ }
79
+ const encv = decodeURI(sv[2]);
80
+ if (!encv) {
81
+ return undefined;
82
+ }
83
+ return encv;
84
+ }
85
+ exports.decode2331Encoding = decode2331Encoding;
86
+ function indexOf(s, fn) {
87
+ let i = -1;
88
+ for (const v of s) {
89
+ i++;
90
+ if (fn(v)) {
91
+ return i;
92
+ }
93
+ }
94
+ return -1;
95
+ }
96
+ function isIterator(obj) {
97
+ if (obj == null) {
98
+ return false;
99
+ }
100
+ // deno-lint-ignore no-explicit-any
101
+ return typeof obj[Symbol.iterator] === "function";
102
+ }
103
+ exports.isIterator = isIterator;
104
+ function isToken(s) {
105
+ if (!s) {
106
+ return false;
107
+ }
108
+ return indexOf(s, isNotTokenChar) < 0;
109
+ }
110
+ exports.isToken = isToken;
111
+ function isNotTokenChar(r) {
112
+ return !isTokenChar(r);
113
+ }
114
+ function isTokenChar(r) {
115
+ const code = r.charCodeAt(0);
116
+ return code > 0x20 && code < 0x7f && !isTSpecial(r);
117
+ }
118
+ function isTSpecial(r) {
119
+ return `()<>@,;:\\"/[]?=`.includes(r[0]);
120
+ }
121
+ const CHAR_CODE_SPACE = " ".charCodeAt(0);
122
+ const CHAR_CODE_TILDE = "~".charCodeAt(0);
123
+ function needsEncoding(s) {
124
+ for (const b of s) {
125
+ const charCode = b.charCodeAt(0);
126
+ if ((charCode < CHAR_CODE_SPACE || charCode > CHAR_CODE_TILDE) && b !== "\t") {
127
+ return true;
128
+ }
129
+ }
130
+ return false;
131
+ }
132
+ exports.needsEncoding = needsEncoding;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.contentType = void 0;
4
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
5
+ const parse_media_type_js_1 = require("./parse_media_type.js");
6
+ const type_by_extension_js_1 = require("./type_by_extension.js");
7
+ const get_charset_js_1 = require("./get_charset.js");
8
+ const format_media_type_js_1 = require("./format_media_type.js");
9
+ /**
10
+ * Given an extension or media type, return a full `Content-Type` or
11
+ * `Content-Disposition` header value.
12
+ *
13
+ * The function will treat the `extensionOrType` as a media type when it
14
+ * contains a `/`, otherwise it will process it as an extension, with or without
15
+ * the leading `.`.
16
+ *
17
+ * Returns `undefined` if unable to resolve the media type.
18
+ *
19
+ * > Note: a side effect of `deno/x/media_types` was that you could pass a file
20
+ * > name (e.g. `file.json`) and it would return the content type. This behavior
21
+ * > is intentionally not supported here. If you want to get an extension for a
22
+ * > file name, use `extname()` from `std/path/mod.ts` to determine the
23
+ * > extension and pass it here.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { contentType } from "https://deno.land/std@$STD_VERSION/media_types/content_type.ts";
28
+ *
29
+ * contentType(".json"); // `application/json; charset=UTF-8`
30
+ * contentType("text/html"); // `text/html; charset=UTF-8`
31
+ * contentType("text/html; charset=UTF-8"); // `text/html; charset=UTF-8`
32
+ * contentType("txt"); // `text/plain; charset=UTF-8`
33
+ * contentType("foo"); // undefined
34
+ * contentType("file.json"); // undefined
35
+ * ```
36
+ */
37
+ function contentType(extensionOrType) {
38
+ try {
39
+ const [mediaType, params = {}] = extensionOrType.includes("/")
40
+ ? (0, parse_media_type_js_1.parseMediaType)(extensionOrType)
41
+ : [(0, type_by_extension_js_1.typeByExtension)(extensionOrType), undefined];
42
+ if (!mediaType) {
43
+ return undefined;
44
+ }
45
+ if (!("charset" in params)) {
46
+ const charset = (0, get_charset_js_1.getCharset)(mediaType);
47
+ if (charset) {
48
+ params.charset = charset;
49
+ }
50
+ }
51
+ return (0, format_media_type_js_1.formatMediaType)(mediaType, params);
52
+ }
53
+ catch {
54
+ // just swallow returning undefined
55
+ }
56
+ return undefined;
57
+ }
58
+ exports.contentType = contentType;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extension = void 0;
4
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
5
+ const extensions_by_type_js_1 = require("./extensions_by_type.js");
6
+ /**
7
+ * For a given media type, return the most relevant extension, or `undefined`
8
+ * if no extension can be found.
9
+ *
10
+ * Extensions are returned without a leading `.`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { extension } from "https://deno.land/std@$STD_VERSION/media_types/extension.ts";
15
+ *
16
+ * extension("text/plain"); // `txt`
17
+ * extension("application/json"); // `json`
18
+ * extension("text/html; charset=UTF-8"); // `html`
19
+ * extension("application/foo"); // undefined
20
+ * ```
21
+ */
22
+ function extension(type) {
23
+ const exts = (0, extensions_by_type_js_1.extensionsByType)(type);
24
+ if (exts) {
25
+ return exts[0];
26
+ }
27
+ return undefined;
28
+ }
29
+ exports.extension = extension;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extensionsByType = exports.extensions = void 0;
4
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
5
+ const parse_media_type_js_1 = require("./parse_media_type.js");
6
+ const _util_js_1 = require("./_util.js");
7
+ Object.defineProperty(exports, "extensions", { enumerable: true, get: function () { return _util_js_1.extensions; } });
8
+ /**
9
+ * Returns the extensions known to be associated with the media type `type`.
10
+ * The returned extensions will each begin with a leading dot, as in `.html`.
11
+ *
12
+ * When `type` has no associated extensions, the function returns `undefined`.
13
+ *
14
+ * Extensions are returned without a leading `.`.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { extensionsByType } from "https://deno.land/std@$STD_VERSION/media_types/extensions_by_type.ts";
19
+ *
20
+ * extensionsByType("application/json"); // ["json", "map"]
21
+ * extensionsByType("text/html; charset=UTF-8"); // ["html", "htm", "shtml"]
22
+ * extensionsByType("application/foo"); // undefined
23
+ * ```
24
+ */
25
+ function extensionsByType(type) {
26
+ try {
27
+ const [mediaType] = (0, parse_media_type_js_1.parseMediaType)(type);
28
+ return _util_js_1.extensions.get(mediaType);
29
+ }
30
+ catch {
31
+ // just swallow errors, returning undefined
32
+ }
33
+ }
34
+ exports.extensionsByType = extensionsByType;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatMediaType = void 0;
4
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
5
+ const _util_js_1 = require("./_util.js");
6
+ /** Serializes the media type and the optional parameters as a media type
7
+ * conforming to RFC 2045 and RFC 2616.
8
+ *
9
+ * The type and parameter names are written in lower-case.
10
+ *
11
+ * When any of the arguments results in a standard violation then the return
12
+ * value will be an empty string (`""`).
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { formatMediaType } from "https://deno.land/std@$STD_VERSION/media_types/format_media_type.ts";
17
+ *
18
+ * formatMediaType("text/plain", { charset: "UTF-8" }); // `text/plain; charset=UTF-8`
19
+ * ```
20
+ */
21
+ function formatMediaType(type, param) {
22
+ let b = "";
23
+ const [major, sub] = type.split("/");
24
+ if (!sub) {
25
+ if (!(0, _util_js_1.isToken)(type)) {
26
+ return "";
27
+ }
28
+ b += type.toLowerCase();
29
+ }
30
+ else {
31
+ if (!(0, _util_js_1.isToken)(major) || !(0, _util_js_1.isToken)(sub)) {
32
+ return "";
33
+ }
34
+ b += `${major.toLowerCase()}/${sub.toLowerCase()}`;
35
+ }
36
+ if (param) {
37
+ param = (0, _util_js_1.isIterator)(param) ? Object.fromEntries(param) : param;
38
+ const attrs = Object.keys(param);
39
+ attrs.sort();
40
+ for (const attribute of attrs) {
41
+ if (!(0, _util_js_1.isToken)(attribute)) {
42
+ return "";
43
+ }
44
+ const value = param[attribute];
45
+ b += `; ${attribute.toLowerCase()}`;
46
+ const needEnc = (0, _util_js_1.needsEncoding)(value);
47
+ if (needEnc) {
48
+ b += "*";
49
+ }
50
+ b += "=";
51
+ if (needEnc) {
52
+ b += `utf-8''${encodeURIComponent(value)}`;
53
+ continue;
54
+ }
55
+ if ((0, _util_js_1.isToken)(value)) {
56
+ b += value;
57
+ continue;
58
+ }
59
+ b += `"${value.replace(/["\\]/gi, (m) => `\\${m}`)}"`;
60
+ }
61
+ }
62
+ return b;
63
+ }
64
+ exports.formatMediaType = formatMediaType;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCharset = void 0;
4
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
5
+ const parse_media_type_js_1 = require("./parse_media_type.js");
6
+ const _db_js_1 = require("./_db.js");
7
+ /**
8
+ * Given a media type or header value, identify the encoding charset. If the
9
+ * charset cannot be determined, the function returns `undefined`.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { getCharset } from "https://deno.land/std@$STD_VERSION/media_types/get_charset.ts";
14
+ *
15
+ * getCharset("text/plain"); // `UTF-8`
16
+ * getCharset("application/foo"); // undefined
17
+ * getCharset("application/news-checkgroups"); // `US-ASCII`
18
+ * getCharset("application/news-checkgroups; charset=UTF-8"); // `UTF-8`
19
+ * ```
20
+ */
21
+ function getCharset(type) {
22
+ try {
23
+ const [mediaType, params] = (0, parse_media_type_js_1.parseMediaType)(type);
24
+ if (params && params["charset"]) {
25
+ return params["charset"];
26
+ }
27
+ const entry = _db_js_1.db[mediaType];
28
+ if (entry && entry.charset) {
29
+ return entry.charset;
30
+ }
31
+ if (mediaType.startsWith("text/")) {
32
+ return "UTF-8";
33
+ }
34
+ }
35
+ catch {
36
+ // just swallow errors, returning undefined
37
+ }
38
+ return undefined;
39
+ }
40
+ exports.getCharset = getCharset;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ /** Utility functions for media types (MIME types).
20
+ *
21
+ * This API is inspired by the GoLang [`mime`](https://pkg.go.dev/mime) package
22
+ * and [jshttp/mime-types](https://github.com/jshttp/mime-types), and is
23
+ * designed to integrate and improve the APIs from
24
+ * [deno.land/x/media_types](https://deno.land/x/media_types).
25
+ *
26
+ * The `vendor` folder contains copy of the
27
+ * [jshttp/mime-db](https://github.com/jshttp/mime-types) `db.json` file along
28
+ * with its license.
29
+ *
30
+ * @module
31
+ */
32
+ __exportStar(require("./content_type.js"), exports);
33
+ __exportStar(require("./extension.js"), exports);
34
+ __exportStar(require("./extensions_by_type.js"), exports);
35
+ __exportStar(require("./format_media_type.js"), exports);
36
+ __exportStar(require("./get_charset.js"), exports);
37
+ __exportStar(require("./parse_media_type.js"), exports);
38
+ __exportStar(require("./type_by_extension.js"), exports);