@mtkruto/node 0.1.124 → 0.1.125

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 (97) hide show
  1. package/esm/0_deps.d.ts +1 -0
  2. package/esm/0_deps.js +1 -0
  3. package/esm/1_utilities.d.ts +1 -1
  4. package/esm/1_utilities.js +1 -1
  5. package/esm/4_constants.d.ts +1 -1
  6. package/esm/4_constants.js +1 -1
  7. package/esm/client/1_client_abstract.d.ts +1 -1
  8. package/esm/client/3_types.d.ts +56 -0
  9. package/esm/client/5_client.d.ts +17 -1
  10. package/esm/client/5_client.js +152 -49
  11. package/esm/connection/0_connection.d.ts +4 -0
  12. package/esm/connection/0_connection.js +6 -0
  13. package/esm/deps/deno.land/std@0.208.0/media_types/_db.d.ts +5 -0
  14. package/esm/deps/deno.land/std@0.208.0/media_types/_db.js +33 -0
  15. package/esm/deps/deno.land/std@0.208.0/media_types/_util.d.ts +21 -0
  16. package/esm/deps/deno.land/std@0.208.0/media_types/_util.js +122 -0
  17. package/esm/deps/deno.land/std@0.208.0/media_types/content_type.d.ts +38 -0
  18. package/esm/deps/deno.land/std@0.208.0/media_types/content_type.js +60 -0
  19. package/esm/deps/deno.land/std@0.208.0/media_types/format_media_type.d.ts +16 -0
  20. package/esm/deps/deno.land/std@0.208.0/media_types/format_media_type.js +61 -0
  21. package/esm/deps/deno.land/std@0.208.0/media_types/get_charset.d.ts +15 -0
  22. package/esm/deps/deno.land/std@0.208.0/media_types/get_charset.js +37 -0
  23. package/esm/deps/deno.land/std@0.208.0/media_types/parse_media_type.d.ts +37 -0
  24. package/esm/deps/deno.land/std@0.208.0/media_types/parse_media_type.js +122 -0
  25. package/esm/deps/deno.land/std@0.208.0/media_types/vendor/mime-db.v1.52.0.d.ts +8518 -0
  26. package/esm/deps/deno.land/std@0.208.0/media_types/vendor/mime-db.v1.52.0.js +8555 -0
  27. package/esm/storage/0_storage.d.ts +1 -0
  28. package/esm/storage/1_storage_indexed_db.d.ts +4 -3
  29. package/esm/storage/1_storage_indexed_db.js +17 -8
  30. package/esm/storage/1_storage_local_storage.d.ts +2 -2
  31. package/esm/storage/1_storage_local_storage.js +3 -0
  32. package/esm/storage/1_storage_memory.d.ts +1 -0
  33. package/esm/storage/1_storage_memory.js +3 -0
  34. package/esm/storage/1_storage_session_storage.d.ts +1 -0
  35. package/esm/storage/1_storage_session_storage.js +3 -0
  36. package/esm/transport/0_transport.d.ts +1 -1
  37. package/esm/transport/0_transport.js +0 -6
  38. package/esm/transport/1_transport_abridged.d.ts +1 -0
  39. package/esm/transport/1_transport_abridged.js +9 -5
  40. package/esm/transport/1_transport_intermediate.d.ts +1 -0
  41. package/esm/transport/1_transport_intermediate.js +8 -4
  42. package/esm/types/1_chat.d.ts +2 -15
  43. package/esm/types/1_chat.js +3 -14
  44. package/esm/types/1_user.js +4 -2
  45. package/esm/types/3_message.d.ts +2 -2
  46. package/esm/types/3_message.js +6 -6
  47. package/esm/utilities/{0_base64.js → 1_base64.js} +2 -1
  48. package/package.json +1 -1
  49. package/script/0_deps.d.ts +1 -0
  50. package/script/0_deps.js +3 -1
  51. package/script/1_utilities.d.ts +1 -1
  52. package/script/1_utilities.js +1 -1
  53. package/script/4_constants.d.ts +1 -1
  54. package/script/4_constants.js +1 -1
  55. package/script/client/1_client_abstract.d.ts +1 -1
  56. package/script/client/3_types.d.ts +56 -0
  57. package/script/client/5_client.d.ts +17 -1
  58. package/script/client/5_client.js +151 -48
  59. package/script/connection/0_connection.d.ts +4 -0
  60. package/script/connection/0_connection.js +6 -0
  61. package/script/deps/deno.land/std@0.208.0/media_types/_db.d.ts +5 -0
  62. package/script/deps/deno.land/std@0.208.0/media_types/_db.js +39 -0
  63. package/script/deps/deno.land/std@0.208.0/media_types/_util.d.ts +21 -0
  64. package/script/deps/deno.land/std@0.208.0/media_types/_util.js +132 -0
  65. package/script/deps/deno.land/std@0.208.0/media_types/content_type.d.ts +38 -0
  66. package/script/deps/deno.land/std@0.208.0/media_types/content_type.js +64 -0
  67. package/script/deps/deno.land/std@0.208.0/media_types/format_media_type.d.ts +16 -0
  68. package/script/deps/deno.land/std@0.208.0/media_types/format_media_type.js +65 -0
  69. package/script/deps/deno.land/std@0.208.0/media_types/get_charset.d.ts +15 -0
  70. package/script/deps/deno.land/std@0.208.0/media_types/get_charset.js +41 -0
  71. package/script/deps/deno.land/std@0.208.0/media_types/parse_media_type.d.ts +37 -0
  72. package/script/deps/deno.land/std@0.208.0/media_types/parse_media_type.js +126 -0
  73. package/script/deps/deno.land/std@0.208.0/media_types/vendor/mime-db.v1.52.0.d.ts +8518 -0
  74. package/script/deps/deno.land/std@0.208.0/media_types/vendor/mime-db.v1.52.0.js +8557 -0
  75. package/script/storage/0_storage.d.ts +1 -0
  76. package/script/storage/1_storage_indexed_db.d.ts +4 -3
  77. package/script/storage/1_storage_indexed_db.js +17 -8
  78. package/script/storage/1_storage_local_storage.d.ts +2 -2
  79. package/script/storage/1_storage_local_storage.js +3 -0
  80. package/script/storage/1_storage_memory.d.ts +1 -0
  81. package/script/storage/1_storage_memory.js +3 -0
  82. package/script/storage/1_storage_session_storage.d.ts +1 -0
  83. package/script/storage/1_storage_session_storage.js +3 -0
  84. package/script/transport/0_transport.d.ts +1 -1
  85. package/script/transport/0_transport.js +0 -6
  86. package/script/transport/1_transport_abridged.d.ts +1 -0
  87. package/script/transport/1_transport_abridged.js +9 -5
  88. package/script/transport/1_transport_intermediate.d.ts +1 -0
  89. package/script/transport/1_transport_intermediate.js +8 -4
  90. package/script/types/1_chat.d.ts +2 -15
  91. package/script/types/1_chat.js +3 -14
  92. package/script/types/1_user.js +4 -2
  93. package/script/types/3_message.d.ts +2 -2
  94. package/script/types/3_message.js +6 -6
  95. package/script/utilities/{0_base64.js → 1_base64.js} +2 -1
  96. /package/esm/utilities/{0_base64.d.ts → 1_base64.d.ts} +0 -0
  97. /package/script/utilities/{0_base64.d.ts → 1_base64.d.ts} +0 -0
@@ -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,21 @@
1
+ /** Supporting functions for media_types that do not make part of the public
2
+ * API.
3
+ *
4
+ * @module
5
+ * @private
6
+ */
7
+ export interface DBEntry {
8
+ source: string;
9
+ compressible?: boolean;
10
+ charset?: string;
11
+ extensions?: string[];
12
+ }
13
+ /** A map of extensions for a given media type. */
14
+ export declare const extensions: Map<string, string[]>;
15
+ export declare function consumeToken(v: string): [token: string, rest: string];
16
+ export declare function consumeValue(v: string): [value: string, rest: string];
17
+ export declare function consumeMediaParam(v: string): [key: string, value: string, rest: string];
18
+ export declare function decode2331Encoding(v: string): string | undefined;
19
+ export declare function isIterator<T>(obj: unknown): obj is Iterable<T>;
20
+ export declare function isToken(s: string): boolean;
21
+ export declare function needsEncoding(s: string): boolean;
@@ -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 || obj === undefined) {
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,38 @@
1
+ import type { db } from "./_db.js";
2
+ type DB = typeof db;
3
+ type ContentTypeToExtension = {
4
+ [K in keyof DB]: DB[K] extends {
5
+ "extensions": readonly string[];
6
+ } ? DB[K]["extensions"][number] : never;
7
+ };
8
+ type KnownExtensionOrType = keyof ContentTypeToExtension | ContentTypeToExtension[keyof ContentTypeToExtension] | `.${ContentTypeToExtension[keyof ContentTypeToExtension]}`;
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
+ export declare function contentType<T extends (string & {}) | KnownExtensionOrType>(extensionOrType: T): Lowercase<T> extends KnownExtensionOrType ? string : string | undefined;
38
+ export {};
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.contentType = void 0;
6
+ const parse_media_type_js_1 = require("./parse_media_type.js");
7
+ const get_charset_js_1 = require("./get_charset.js");
8
+ const format_media_type_js_1 = require("./format_media_type.js");
9
+ const _db_js_1 = require("./_db.js");
10
+ /**
11
+ * Given an extension or media type, return a full `Content-Type` or
12
+ * `Content-Disposition` header value.
13
+ *
14
+ * The function will treat the `extensionOrType` as a media type when it
15
+ * contains a `/`, otherwise it will process it as an extension, with or without
16
+ * the leading `.`.
17
+ *
18
+ * Returns `undefined` if unable to resolve the media type.
19
+ *
20
+ * > Note: a side effect of `deno/x/media_types` was that you could pass a file
21
+ * > name (e.g. `file.json`) and it would return the content type. This behavior
22
+ * > is intentionally not supported here. If you want to get an extension for a
23
+ * > file name, use `extname()` from `std/path/mod.ts` to determine the
24
+ * > extension and pass it here.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { contentType } from "https://deno.land/std@$STD_VERSION/media_types/content_type.ts";
29
+ *
30
+ * contentType(".json"); // `application/json; charset=UTF-8`
31
+ * contentType("text/html"); // `text/html; charset=UTF-8`
32
+ * contentType("text/html; charset=UTF-8"); // `text/html; charset=UTF-8`
33
+ * contentType("txt"); // `text/plain; charset=UTF-8`
34
+ * contentType("foo"); // undefined
35
+ * contentType("file.json"); // undefined
36
+ * ```
37
+ */
38
+ function contentType(extensionOrType) {
39
+ try {
40
+ const [mediaType, params = {}] = extensionOrType.includes("/")
41
+ ? (0, parse_media_type_js_1.parseMediaType)(extensionOrType)
42
+ : [typeByExtension(extensionOrType), undefined];
43
+ if (!mediaType) {
44
+ return undefined;
45
+ }
46
+ if (!("charset" in params)) {
47
+ const charset = (0, get_charset_js_1.getCharset)(mediaType);
48
+ if (charset) {
49
+ params.charset = charset;
50
+ }
51
+ }
52
+ return (0, format_media_type_js_1.formatMediaType)(mediaType, params);
53
+ }
54
+ catch {
55
+ // just swallow returning undefined
56
+ }
57
+ return undefined;
58
+ }
59
+ exports.contentType = contentType;
60
+ function typeByExtension(extension) {
61
+ extension = extension.startsWith(".") ? extension.slice(1) : extension;
62
+ // @ts-ignore workaround around denoland/dnt#148
63
+ return _db_js_1.types.get(extension.toLowerCase());
64
+ }
@@ -0,0 +1,16 @@
1
+ /** Serializes the media type and the optional parameters as a media type
2
+ * conforming to RFC 2045 and RFC 2616.
3
+ *
4
+ * The type and parameter names are written in lower-case.
5
+ *
6
+ * When any of the arguments results in a standard violation then the return
7
+ * value will be an empty string (`""`).
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { formatMediaType } from "https://deno.land/std@$STD_VERSION/media_types/format_media_type.ts";
12
+ *
13
+ * formatMediaType("text/plain", { charset: "UTF-8" }); // `text/plain; charset=UTF-8`
14
+ * ```
15
+ */
16
+ export declare function formatMediaType(type: string, param?: Record<string, string> | Iterable<[string, string]>): string;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.formatMediaType = void 0;
6
+ const _util_js_1 = require("./_util.js");
7
+ /** Serializes the media type and the optional parameters as a media type
8
+ * conforming to RFC 2045 and RFC 2616.
9
+ *
10
+ * The type and parameter names are written in lower-case.
11
+ *
12
+ * When any of the arguments results in a standard violation then the return
13
+ * value will be an empty string (`""`).
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { formatMediaType } from "https://deno.land/std@$STD_VERSION/media_types/format_media_type.ts";
18
+ *
19
+ * formatMediaType("text/plain", { charset: "UTF-8" }); // `text/plain; charset=UTF-8`
20
+ * ```
21
+ */
22
+ function formatMediaType(type, param) {
23
+ let b = "";
24
+ const [major, sub] = type.split("/");
25
+ if (!sub) {
26
+ if (!(0, _util_js_1.isToken)(type)) {
27
+ return "";
28
+ }
29
+ b += type.toLowerCase();
30
+ }
31
+ else {
32
+ if (!(0, _util_js_1.isToken)(major) || !(0, _util_js_1.isToken)(sub)) {
33
+ return "";
34
+ }
35
+ b += `${major.toLowerCase()}/${sub.toLowerCase()}`;
36
+ }
37
+ if (param) {
38
+ param = (0, _util_js_1.isIterator)(param) ? Object.fromEntries(param) : param;
39
+ const attrs = Object.keys(param);
40
+ attrs.sort();
41
+ for (const attribute of attrs) {
42
+ if (!(0, _util_js_1.isToken)(attribute)) {
43
+ return "";
44
+ }
45
+ const value = param[attribute];
46
+ b += `; ${attribute.toLowerCase()}`;
47
+ const needEnc = (0, _util_js_1.needsEncoding)(value);
48
+ if (needEnc) {
49
+ b += "*";
50
+ }
51
+ b += "=";
52
+ if (needEnc) {
53
+ b += `utf-8''${encodeURIComponent(value)}`;
54
+ continue;
55
+ }
56
+ if ((0, _util_js_1.isToken)(value)) {
57
+ b += value;
58
+ continue;
59
+ }
60
+ b += `"${value.replace(/["\\]/gi, (m) => `\\${m}`)}"`;
61
+ }
62
+ }
63
+ return b;
64
+ }
65
+ exports.formatMediaType = formatMediaType;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Given a media type or header value, identify the encoding charset. If the
3
+ * charset cannot be determined, the function returns `undefined`.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { getCharset } from "https://deno.land/std@$STD_VERSION/media_types/get_charset.ts";
8
+ *
9
+ * getCharset("text/plain"); // `UTF-8`
10
+ * getCharset("application/foo"); // undefined
11
+ * getCharset("application/news-checkgroups"); // `US-ASCII`
12
+ * getCharset("application/news-checkgroups; charset=UTF-8"); // `UTF-8`
13
+ * ```
14
+ */
15
+ export declare function getCharset(type: string): string | undefined;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.getCharset = void 0;
6
+ const parse_media_type_js_1 = require("./parse_media_type.js");
7
+ const _db_js_1 = require("./_db.js");
8
+ /**
9
+ * Given a media type or header value, identify the encoding charset. If the
10
+ * charset cannot be determined, the function returns `undefined`.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { getCharset } from "https://deno.land/std@$STD_VERSION/media_types/get_charset.ts";
15
+ *
16
+ * getCharset("text/plain"); // `UTF-8`
17
+ * getCharset("application/foo"); // undefined
18
+ * getCharset("application/news-checkgroups"); // `US-ASCII`
19
+ * getCharset("application/news-checkgroups; charset=UTF-8"); // `UTF-8`
20
+ * ```
21
+ */
22
+ function getCharset(type) {
23
+ try {
24
+ const [mediaType, params] = (0, parse_media_type_js_1.parseMediaType)(type);
25
+ if (params && params["charset"]) {
26
+ return params["charset"];
27
+ }
28
+ const entry = _db_js_1.db[mediaType];
29
+ if (entry && entry.charset) {
30
+ return entry.charset;
31
+ }
32
+ if (mediaType.startsWith("text/")) {
33
+ return "UTF-8";
34
+ }
35
+ }
36
+ catch {
37
+ // just swallow errors, returning undefined
38
+ }
39
+ return undefined;
40
+ }
41
+ exports.getCharset = getCharset;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Parses the media type and any optional parameters, per
3
+ * [RFC 1521](https://datatracker.ietf.org/doc/html/rfc1521). Media types are
4
+ * the values in `Content-Type` and `Content-Disposition` headers. On success
5
+ * the function returns a tuple where the first element is the media type and
6
+ * the second element is the optional parameters or `undefined` if there are
7
+ * none.
8
+ *
9
+ * The function will throw if the parsed value is invalid.
10
+ *
11
+ * The returned media type will be normalized to be lower case, and returned
12
+ * params keys will be normalized to lower case, but preserves the casing of
13
+ * the value.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { parseMediaType } from "https://deno.land/std@$STD_VERSION/media_types/parse_media_type.ts";
18
+ * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts";
19
+ *
20
+ * assertEquals(
21
+ * parseMediaType("application/JSON"),
22
+ * [
23
+ * "application/json",
24
+ * undefined
25
+ * ]
26
+ * );
27
+ *
28
+ * assertEquals(
29
+ * parseMediaType("text/html; charset=UTF-8"),
30
+ * [
31
+ * "text/html",
32
+ * { charset: "UTF-8" },
33
+ * ]
34
+ * );
35
+ * ```
36
+ */
37
+ export declare function parseMediaType(v: string): [mediaType: string, params: Record<string, string> | undefined];
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.parseMediaType = void 0;
6
+ const _util_js_1 = require("./_util.js");
7
+ /**
8
+ * Parses the media type and any optional parameters, per
9
+ * [RFC 1521](https://datatracker.ietf.org/doc/html/rfc1521). Media types are
10
+ * the values in `Content-Type` and `Content-Disposition` headers. On success
11
+ * the function returns a tuple where the first element is the media type and
12
+ * the second element is the optional parameters or `undefined` if there are
13
+ * none.
14
+ *
15
+ * The function will throw if the parsed value is invalid.
16
+ *
17
+ * The returned media type will be normalized to be lower case, and returned
18
+ * params keys will be normalized to lower case, but preserves the casing of
19
+ * the value.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { parseMediaType } from "https://deno.land/std@$STD_VERSION/media_types/parse_media_type.ts";
24
+ * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts";
25
+ *
26
+ * assertEquals(
27
+ * parseMediaType("application/JSON"),
28
+ * [
29
+ * "application/json",
30
+ * undefined
31
+ * ]
32
+ * );
33
+ *
34
+ * assertEquals(
35
+ * parseMediaType("text/html; charset=UTF-8"),
36
+ * [
37
+ * "text/html",
38
+ * { charset: "UTF-8" },
39
+ * ]
40
+ * );
41
+ * ```
42
+ */
43
+ function parseMediaType(v) {
44
+ const [base] = v.split(";");
45
+ const mediaType = base.toLowerCase().trim();
46
+ const params = {};
47
+ // Map of base parameter name -> parameter name -> value
48
+ // for parameters containing a '*' character.
49
+ const continuation = new Map();
50
+ v = v.slice(base.length);
51
+ while (v.length) {
52
+ v = v.trimStart();
53
+ if (v.length === 0) {
54
+ break;
55
+ }
56
+ const [key, value, rest] = (0, _util_js_1.consumeMediaParam)(v);
57
+ if (!key) {
58
+ if (rest.trim() === ";") {
59
+ // ignore trailing semicolons
60
+ break;
61
+ }
62
+ throw new TypeError("Invalid media parameter.");
63
+ }
64
+ let pmap = params;
65
+ const [baseName, rest2] = key.split("*");
66
+ if (baseName && rest2 !== undefined) {
67
+ if (!continuation.has(baseName)) {
68
+ continuation.set(baseName, {});
69
+ }
70
+ pmap = continuation.get(baseName);
71
+ }
72
+ if (key in pmap) {
73
+ throw new TypeError("Duplicate key parsed.");
74
+ }
75
+ pmap[key] = value;
76
+ v = rest;
77
+ }
78
+ // Stitch together any continuations or things with stars
79
+ // (i.e. RFC 2231 things with stars: "foo*0" or "foo*")
80
+ let str = "";
81
+ for (const [key, pieceMap] of continuation) {
82
+ const singlePartKey = `${key}*`;
83
+ const v = pieceMap[singlePartKey];
84
+ if (v) {
85
+ const decv = (0, _util_js_1.decode2331Encoding)(v);
86
+ if (decv) {
87
+ params[key] = decv;
88
+ }
89
+ continue;
90
+ }
91
+ str = "";
92
+ let valid = false;
93
+ for (let n = 0;; n++) {
94
+ const simplePart = `${key}*${n}`;
95
+ let v = pieceMap[simplePart];
96
+ if (v) {
97
+ valid = true;
98
+ str += v;
99
+ continue;
100
+ }
101
+ const encodedPart = `${simplePart}*`;
102
+ v = pieceMap[encodedPart];
103
+ if (!v) {
104
+ break;
105
+ }
106
+ valid = true;
107
+ if (n === 0) {
108
+ const decv = (0, _util_js_1.decode2331Encoding)(v);
109
+ if (decv) {
110
+ str += decv;
111
+ }
112
+ }
113
+ else {
114
+ const decv = decodeURI(v);
115
+ str += decv;
116
+ }
117
+ }
118
+ if (valid) {
119
+ params[key] = str;
120
+ }
121
+ }
122
+ return Object.keys(params).length
123
+ ? [mediaType, params]
124
+ : [mediaType, undefined];
125
+ }
126
+ exports.parseMediaType = parseMediaType;