@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,113 @@
1
+ "use strict";
2
+ /*!
3
+ * Based on https://github.com/jshttp/vary/blob/master/index.js
4
+ * Copyright(c) 2014-2017 Douglas Christopher Wilson
5
+ * Copyright(c) 2020 Henry Zhuang
6
+ * MIT Licensed
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.vary = exports.append = void 0;
10
+ /**
11
+ * RegExp to match field-name in RFC 7230 sec 3.2
12
+ *
13
+ * field-name = token
14
+ * token = 1*tchar
15
+ * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
16
+ * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
17
+ * / DIGIT / ALPHA
18
+ * ; any VCHAR, except delimiters
19
+ */
20
+ const FIELD_NAME_REGEXP = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
21
+ /**
22
+ * Append a field to a vary header.
23
+ *
24
+ * @param {String} header
25
+ * @param {String|Array} field
26
+ * @return {String}
27
+ * @public
28
+ */
29
+ function append(header, field) {
30
+ // if (typeof header !== 'string') {
31
+ // throw new TypeError('header argument is required')
32
+ // }
33
+ // if (!field) {
34
+ // throw new TypeError('field argument is required')
35
+ // }
36
+ // existing, unspecified vary
37
+ if (header === "*") {
38
+ return header;
39
+ }
40
+ // get fields array
41
+ const fields = !Array.isArray(field) ? parse(String(field)) : field;
42
+ // assert on invalid field names
43
+ for (let j = 0; j < fields.length; j++) {
44
+ if (!FIELD_NAME_REGEXP.test(fields[j])) {
45
+ throw new TypeError(`field argument contains an invalid header name \`${fields[j]}\``);
46
+ }
47
+ }
48
+ // enumerate current values
49
+ let val = header;
50
+ const vals = parse(header.toLowerCase());
51
+ // unspecified vary
52
+ if (fields.indexOf("*") !== -1 || vals.indexOf("*") !== -1) {
53
+ return "*";
54
+ }
55
+ for (let i = 0; i < fields.length; i++) {
56
+ const fld = fields[i].toLowerCase();
57
+ // append value (case-preserving)
58
+ if (vals.indexOf(fld) === -1) {
59
+ vals.push(fld);
60
+ val = val ? val + ", " + fields[i] : fields[i];
61
+ }
62
+ }
63
+ return val;
64
+ }
65
+ exports.append = append;
66
+ /**
67
+ * Parse a vary header into an array.
68
+ *
69
+ * @param {String} header
70
+ * @return {Array}
71
+ * @private
72
+ */
73
+ function parse(header) {
74
+ let end = 0;
75
+ const list = [];
76
+ let start = 0;
77
+ // gather tokens
78
+ for (let i = 0, len = header.length; i < len; i++) {
79
+ switch (header.charCodeAt(i)) {
80
+ case 0x20: /* */
81
+ if (start === end) {
82
+ start = end = i + 1;
83
+ }
84
+ break;
85
+ case 0x2c: /* , */
86
+ list.push(header.substring(start, end));
87
+ start = end = i + 1;
88
+ break;
89
+ default:
90
+ end = i + 1;
91
+ break;
92
+ }
93
+ }
94
+ // final token
95
+ list.push(header.substring(start, end));
96
+ return list;
97
+ }
98
+ /**
99
+ * Mark that a request is varied on a header field.
100
+ *
101
+ * @param {Headers} header
102
+ * @param {String|Array} field
103
+ * @public
104
+ */
105
+ function vary(header, field) {
106
+ // get existing header
107
+ let val = header.get("vary") || "";
108
+ // set new header
109
+ if ((val = append(val, field))) {
110
+ header.set("vary", val);
111
+ }
112
+ }
113
+ exports.vary = vary;
package/script/deps.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ var __importDefault = (this && this.__importDefault) || function (mod) {
5
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
+ };
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.vary = exports.compressible = exports.isNull = exports.parseMediaType = exports.acceptsEncodings = exports.RepresentationHeader = exports.ContentNegotiationHeader = exports.CachingHeader = void 0;
9
+ var http_utils_1 = require("@httpland/http-utils");
10
+ Object.defineProperty(exports, "CachingHeader", { enumerable: true, get: function () { return http_utils_1.CachingHeader; } });
11
+ Object.defineProperty(exports, "ContentNegotiationHeader", { enumerable: true, get: function () { return http_utils_1.ContentNegotiationHeader; } });
12
+ Object.defineProperty(exports, "RepresentationHeader", { enumerable: true, get: function () { return http_utils_1.RepresentationHeader; } });
13
+ var negotiation_js_1 = require("./deps/deno.land/std@0.180.0/http/negotiation.js");
14
+ Object.defineProperty(exports, "acceptsEncodings", { enumerable: true, get: function () { return negotiation_js_1.acceptsEncodings; } });
15
+ var mod_js_1 = require("./deps/deno.land/std@0.180.0/media_types/mod.js");
16
+ Object.defineProperty(exports, "parseMediaType", { enumerable: true, get: function () { return mod_js_1.parseMediaType; } });
17
+ var isxx_1 = require("isxx");
18
+ Object.defineProperty(exports, "isNull", { enumerable: true, get: function () { return isxx_1.isNull; } });
19
+ var compressible_1 = require("compressible");
20
+ Object.defineProperty(exports, "compressible", { enumerable: true, get: function () { return __importDefault(compressible_1).default; } });
21
+ var mod_js_2 = require("./deps/deno.land/x/vary@1.0.0/mod.js");
22
+ Object.defineProperty(exports, "vary", { enumerable: true, get: function () { return mod_js_2.vary; } });
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.Deflate = void 0;
6
+ const utils_js_1 = require("./utils.js");
7
+ function encodeDeflate(stream) {
8
+ return stream.pipeThrough(new CompressionStream(utils_js_1.Format.Deflate));
9
+ }
10
+ /** Encoder for `deflate` */
11
+ exports.Deflate = {
12
+ encoding: utils_js_1.Format.Deflate,
13
+ encode: encodeDeflate,
14
+ };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.Gzip = void 0;
6
+ const utils_js_1 = require("./utils.js");
7
+ function encodeGzip(stream) {
8
+ return stream.pipeThrough(new CompressionStream(utils_js_1.Format.Gzip));
9
+ }
10
+ /** Encoder for `gzip` */
11
+ exports.Gzip = {
12
+ encoding: utils_js_1.Format.Gzip,
13
+ encode: encodeGzip,
14
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.Format = void 0;
6
+ var Format;
7
+ (function (Format) {
8
+ Format["Gzip"] = "gzip";
9
+ Format["Deflate"] = "deflate";
10
+ })(Format = exports.Format || (exports.Format = {}));
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.fromEncoders = exports.flat = exports.compression = void 0;
6
+ const deps_js_1 = require("./deps.js");
7
+ const transform_js_1 = require("./transform.js");
8
+ const gzip_js_1 = require("./encoders/gzip.js");
9
+ const deflate_js_1 = require("./encoders/deflate.js");
10
+ const DefaultEncoders = [gzip_js_1.Gzip, deflate_js_1.Deflate];
11
+ /** Create HTTP content compression middleware.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { compression } from "https://deno.land/x/compression_middleware@$VERSION/mod.ts";
16
+ * import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
17
+ *
18
+ * const middleware = compression();
19
+ * const request = new Request("test:", {
20
+ * headers: {
21
+ * "accept-encoding": "deflate;q=0.5, gzip;q=1.0, deflate-raw;q=0.3",
22
+ * },
23
+ * });
24
+ *
25
+ * const response = await middleware(
26
+ * request,
27
+ * () => new Response("<body>"),
28
+ * );
29
+ *
30
+ * assertEquals(await response.text(), "<gzip:body>");
31
+ * assertEquals(response.headers.get("content-encoding"), "gzip");
32
+ * ```
33
+ */
34
+ function compression(encoders) {
35
+ const encodingMap = {
36
+ ...fromEncoders(DefaultEncoders),
37
+ ...Array.isArray(encoders) ? fromEncoders(encoders) : encoders,
38
+ };
39
+ const encodings = Object.keys(encodingMap);
40
+ return async (request, next) => {
41
+ const encoding = (0, deps_js_1.acceptsEncodings)(request, ...encodings, IDENTITY);
42
+ const response = await next(request);
43
+ (0, deps_js_1.vary)(response.headers, deps_js_1.ContentNegotiationHeader.AcceptEncoding);
44
+ if (!encoding || encoding === IDENTITY)
45
+ return response;
46
+ const encode = encodingMap[encoding];
47
+ if (!encode)
48
+ return response;
49
+ return (0, transform_js_1.withContentEncoding)(response, { encode, encoding });
50
+ };
51
+ }
52
+ exports.compression = compression;
53
+ function flat(encoder) {
54
+ return [encoder.encoding, encoder.encode];
55
+ }
56
+ exports.flat = flat;
57
+ function fromEncoders(encoders) {
58
+ return Object.fromEntries(encoders.map(flat));
59
+ }
60
+ exports.fromEncoders = fromEncoders;
61
+ const IDENTITY = "identity";
package/script/mod.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.compression = void 0;
6
+ var middleware_js_1 = require("./middleware.js");
7
+ Object.defineProperty(exports, "compression", { enumerable: true, get: function () { return middleware_js_1.compression; } });
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.withContentEncoding = void 0;
6
+ const deps_js_1 = require("./deps.js");
7
+ const utils_js_1 = require("./utils.js");
8
+ /** Response with `Content-Encoding` header. */
9
+ async function withContentEncoding(response, context) {
10
+ const contentType = response.headers.get(deps_js_1.RepresentationHeader.ContentType);
11
+ const cacheControl = response.headers.get(deps_js_1.CachingHeader.CacheControl);
12
+ if ((0, deps_js_1.isNull)(contentType) ||
13
+ response.headers.has(deps_js_1.RepresentationHeader.ContentEncoding) ||
14
+ !response.body ||
15
+ response.bodyUsed ||
16
+ (!(0, deps_js_1.isNull)(cacheControl) && (0, utils_js_1.isNoTransform)(cacheControl)))
17
+ return response;
18
+ const [mediaType] = (0, deps_js_1.parseMediaType)(contentType);
19
+ const isCompressible = (0, deps_js_1.compressible)(mediaType);
20
+ if (!isCompressible)
21
+ return response;
22
+ const bodyInit = await context.encode(response.body);
23
+ const newResponse = await (0, utils_js_1.reCalcContentLength)(new Response(bodyInit, response));
24
+ newResponse.headers.set(deps_js_1.RepresentationHeader.ContentEncoding, context.encoding);
25
+ return newResponse;
26
+ }
27
+ exports.withContentEncoding = withContentEncoding;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ // Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.reCalcContentLength = exports.isNoTransform = void 0;
6
+ const deps_js_1 = require("./deps.js");
7
+ const ReNoTransform = /(?:^|,)\s*?no-transform\s*?(?:,|$)/;
8
+ function isNoTransform(input) {
9
+ return ReNoTransform.test(input);
10
+ }
11
+ exports.isNoTransform = isNoTransform;
12
+ async function reCalcContentLength(response) {
13
+ if (response.bodyUsed ||
14
+ !response.headers.has(deps_js_1.RepresentationHeader.ContentLength))
15
+ return response;
16
+ const length = await response
17
+ .clone()
18
+ .arrayBuffer()
19
+ .then((buffer) => buffer.byteLength)
20
+ .then(String);
21
+ response.headers.set(deps_js_1.RepresentationHeader.ContentLength, length);
22
+ return response;
23
+ }
24
+ exports.reCalcContentLength = reCalcContentLength;
@@ -0,0 +1,37 @@
1
+ /*!
2
+ * Adapted directly from negotiator at https://github.com/jshttp/negotiator/
3
+ * which is licensed as follows:
4
+ *
5
+ * (The MIT License)
6
+ *
7
+ * Copyright (c) 2012-2014 Federico Romero
8
+ * Copyright (c) 2012-2014 Isaac Z. Schlueter
9
+ * Copyright (c) 2014-2015 Douglas Christopher Wilson
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining
12
+ * a copy of this software and associated documentation files (the
13
+ * 'Software'), to deal in the Software without restriction, including
14
+ * without limitation the rights to use, copy, modify, merge, publish,
15
+ * distribute, sublicense, and/or sell copies of the Software, and to
16
+ * permit persons to whom the Software is furnished to do so, subject to
17
+ * the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be
20
+ * included in all copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
23
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
+ */
30
+ export interface Specificity {
31
+ i: number;
32
+ o?: number;
33
+ q: number;
34
+ s?: number;
35
+ }
36
+ export declare function compareSpecs(a: Specificity, b: Specificity): number;
37
+ export declare function isQuality(spec: Specificity): boolean;
@@ -0,0 +1,33 @@
1
+ /*!
2
+ * Adapted directly from negotiator at https://github.com/jshttp/negotiator/
3
+ * which is licensed as follows:
4
+ *
5
+ * (The MIT License)
6
+ *
7
+ * Copyright (c) 2012-2014 Federico Romero
8
+ * Copyright (c) 2012-2014 Isaac Z. Schlueter
9
+ * Copyright (c) 2014-2015 Douglas Christopher Wilson
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining
12
+ * a copy of this software and associated documentation files (the
13
+ * 'Software'), to deal in the Software without restriction, including
14
+ * without limitation the rights to use, copy, modify, merge, publish,
15
+ * distribute, sublicense, and/or sell copies of the Software, and to
16
+ * permit persons to whom the Software is furnished to do so, subject to
17
+ * the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be
20
+ * included in all copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
23
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
+ */
30
+ /** Given an `Accept-Encoding` string, parse out the encoding returning a
31
+ * negotiated encoding based on the `provided` encodings otherwise just a
32
+ * prioritized array of encodings. */
33
+ export declare function preferredEncodings(accept: string, provided?: string[]): string[];
@@ -0,0 +1,30 @@
1
+ /*!
2
+ * Adapted directly from negotiator at https://github.com/jshttp/negotiator/
3
+ * which is licensed as follows:
4
+ *
5
+ * (The MIT License)
6
+ *
7
+ * Copyright (c) 2012-2014 Federico Romero
8
+ * Copyright (c) 2012-2014 Isaac Z. Schlueter
9
+ * Copyright (c) 2014-2015 Douglas Christopher Wilson
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining
12
+ * a copy of this software and associated documentation files (the
13
+ * 'Software'), to deal in the Software without restriction, including
14
+ * without limitation the rights to use, copy, modify, merge, publish,
15
+ * distribute, sublicense, and/or sell copies of the Software, and to
16
+ * permit persons to whom the Software is furnished to do so, subject to
17
+ * the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be
20
+ * included in all copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
23
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
+ */
30
+ export declare function preferredLanguages(accept?: string, provided?: string[]): string[];
@@ -0,0 +1,30 @@
1
+ /*!
2
+ * Adapted directly from negotiator at https://github.com/jshttp/negotiator/
3
+ * which is licensed as follows:
4
+ *
5
+ * (The MIT License)
6
+ *
7
+ * Copyright (c) 2012-2014 Federico Romero
8
+ * Copyright (c) 2012-2014 Isaac Z. Schlueter
9
+ * Copyright (c) 2014-2015 Douglas Christopher Wilson
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining
12
+ * a copy of this software and associated documentation files (the
13
+ * 'Software'), to deal in the Software without restriction, including
14
+ * without limitation the rights to use, copy, modify, merge, publish,
15
+ * distribute, sublicense, and/or sell copies of the Software, and to
16
+ * permit persons to whom the Software is furnished to do so, subject to
17
+ * the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be
20
+ * included in all copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
23
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
+ */
30
+ export declare function preferredMediaTypes(accept?: string | null, provided?: string[]): string[];
@@ -0,0 +1,126 @@
1
+ export type Request = {
2
+ headers: {
3
+ get(key: string): string | null;
4
+ };
5
+ };
6
+ /**
7
+ * Returns an array of media types accepted by the request, in order of
8
+ * preference. If there are no media types supplied in the request, then any
9
+ * media type selector will be returned.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { accepts } from "https://deno.land/std@$STD_VERSION/http/negotiation.ts";
14
+ *
15
+ * const req = new Request("https://example.com/", {
16
+ * headers: {
17
+ * "accept":
18
+ * "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, *\/*;q=0.8",
19
+ * },
20
+ * });
21
+ *
22
+ * console.log(accepts(req));
23
+ * // [
24
+ * // "text/html",
25
+ * // "application/xhtml+xml",
26
+ * // "image/webp",
27
+ * // "application/xml",
28
+ * // "*\/*",
29
+ * // ]
30
+ * ```
31
+ */
32
+ export declare function accepts(request: Request): string[];
33
+ /**
34
+ * For a given set of media types, return the best match accepted in the
35
+ * request. If no media type matches, then the function returns `undefined`.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { accepts } from "https://deno.land/std@$STD_VERSION/http/negotiation.ts";
40
+ *
41
+ * const req = new Request("https://example.com/", {
42
+ * headers: {
43
+ * "accept":
44
+ * "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, *\/*;q=0.8",
45
+ * },
46
+ * });
47
+ *
48
+ * accepts(req, "text/html", "image/webp"); // "text/html";
49
+ * ```
50
+ */
51
+ export declare function accepts(request: Request, ...types: string[]): string | undefined;
52
+ /**
53
+ * Returns an array of content encodings accepted by the request, in order of
54
+ * preference. If there are no encoding supplied in the request, then `["*"]`
55
+ * is returned, implying any encoding is accepted.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * import { acceptsEncodings } from "https://deno.land/std@$STD_VERSION/http/negotiation.ts";
60
+ *
61
+ * const req = new Request("https://example.com/", {
62
+ * headers: { "accept-encoding": "deflate, gzip;q=1.0, *;q=0.5" },
63
+ * });
64
+ *
65
+ * acceptsEncodings(req); // ["deflate", "gzip", "*"]
66
+ * ```
67
+ */
68
+ export declare function acceptsEncodings(request: Request): string[];
69
+ /**
70
+ * For a given set of content encodings, return the best match accepted in the
71
+ * request. If no content encodings match, then the function returns
72
+ * `undefined`.
73
+ *
74
+ * **NOTE:** You should always supply `identity` as one of the encodings
75
+ * to ensure that there is a match when the `Accept-Encoding` header is part
76
+ * of the request.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * import { acceptsEncodings } from "https://deno.land/std@$STD_VERSION/http/negotiation.ts";
81
+ *
82
+ * const req = new Request("https://example.com/", {
83
+ * headers: { "accept-encoding": "deflate, gzip;q=1.0, *;q=0.5" },
84
+ * });
85
+ *
86
+ * acceptsEncodings(req, "gzip", "identity"); // "gzip"
87
+ * ```
88
+ */
89
+ export declare function acceptsEncodings(request: Request, ...encodings: string[]): string | undefined;
90
+ /**
91
+ * Returns an array of languages accepted by the request, in order of
92
+ * preference. If there are no languages supplied in the request, then `["*"]`
93
+ * is returned, imply any language is accepted.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * import { acceptsLanguages } from "https://deno.land/std@$STD_VERSION/http/negotiation.ts";
98
+ *
99
+ * const req = new Request("https://example.com/", {
100
+ * headers: {
101
+ * "accept-language": "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
102
+ * },
103
+ * });
104
+ *
105
+ * acceptsLanguages(req); // ["fr-CH", "fr", "en", "de", "*"]
106
+ * ```
107
+ */
108
+ export declare function acceptsLanguages(request: Request): string[];
109
+ /**
110
+ * For a given set of languages, return the best match accepted in the request.
111
+ * If no languages match, then the function returns `undefined`.
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * import { acceptsLanguages } from "https://deno.land/std@$STD_VERSION/http/negotiation.ts";
116
+ *
117
+ * const req = new Request("https://example.com/", {
118
+ * headers: {
119
+ * "accept-language": "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
120
+ * },
121
+ * });
122
+ *
123
+ * acceptsLanguages(req, "en-gb", "en-us", "en"); // "en"
124
+ * ```
125
+ */
126
+ export declare function acceptsLanguages(request: Request, ...langs: string[]): string | undefined;