Skip to content

Commit df4820b

Browse files
committed
Remove dependencies on node:path and mime-types
1 parent 12c7f97 commit df4820b

File tree

3 files changed

+119
-97
lines changed

3 files changed

+119
-97
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@types/jest": "^27.0.2",
5151
"@types/jsbn": "^1.2.30",
5252
"@types/jsonwebtoken": "^8.5.8",
53-
"@types/mime-types": "^2.1.1",
5453
"@types/node": "^16.11.7",
5554
"@types/uuid": "^8.3.3",
5655
"@typescript-eslint/eslint-plugin": "^5.8.1",
@@ -74,7 +73,6 @@
7473
"ip-address": "^8.1.0",
7574
"json5": "^2.2.0",
7675
"jsonwebtoken": "^8.5.1",
77-
"mime-types": "^2.1.34",
7876
"universal-url": "^2.0.0",
7977
"uuid": "^8.3.2"
8078
},
@@ -92,4 +90,4 @@
9290
"pre-commit": "npm run prettier-format && npm run lint"
9391
}
9492
}
95-
}
93+
}

src/formats/hostname.ts

Lines changed: 27 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,33 @@
1-
import path from "path";
2-
3-
const filenameExtensions = [
4-
".7z",
5-
".aac",
6-
".au",
7-
".avi",
8-
".bmp",
9-
".bz2",
10-
".css",
11-
".csv",
12-
".dmg",
13-
".doc",
14-
".docx",
15-
".eot",
16-
".epub",
17-
".flac",
18-
".flv",
19-
".gif",
20-
".gz",
21-
".htm",
22-
".html",
23-
".ico",
24-
".jpeg",
25-
".jpg",
26-
".js",
27-
".json",
28-
".key",
29-
".m2v",
30-
".m4a",
31-
".m4v",
32-
".m4v",
33-
".md",
34-
".mov",
35-
".mp2",
36-
".mp3",
37-
".mp4",
38-
".mpe",
39-
".mpeg",
40-
".mpeg",
41-
".mpg",
42-
".mpg",
43-
".mpv",
44-
".mxf",
45-
".numbers",
46-
".odt",
47-
".ogg",
48-
".ogv",
49-
".pages",
50-
".pdf",
51-
".png",
52-
".ppt",
53-
".pptx",
54-
".psd",
55-
".rar",
56-
".raw",
57-
".rtf",
58-
".svg",
59-
".swf",
60-
".tar",
61-
".tgz",
62-
".tif",
63-
".tiff",
64-
".ts",
65-
".tsv",
66-
".ttf",
67-
".txt",
68-
".wav",
69-
".webp",
70-
".wmv",
71-
".woff",
72-
".woff2",
73-
".xls",
74-
".xlsx",
75-
".xml",
76-
".xz",
77-
".yaml",
78-
".yml",
79-
".z",
80-
".zip",
1+
const tlds = [
2+
"com",
3+
"org",
4+
"net",
5+
"edu",
6+
"gov",
7+
"mil",
8+
"co",
9+
"io",
10+
"ac",
11+
"dev",
12+
"info",
13+
"biz",
14+
"name",
15+
"uk",
16+
"me",
17+
"ca",
18+
"tv",
19+
"ir",
20+
"au",
8121
];
8222

83-
function isNotFilename(value: string): boolean {
84-
const extname = path.extname(value);
23+
function containsTld(value: string): boolean {
24+
const extname = value.split(".").pop();
8525

86-
return extname === "" || !filenameExtensions.includes(extname);
87-
}
88-
89-
function isNotSemver(value: string): boolean {
90-
const regex =
91-
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
26+
if (!extname) {
27+
return false;
28+
}
9229

93-
return !regex.test(value);
30+
return tlds.includes(extname);
9431
}
9532

9633
export type JSONHostnameFormat = {
@@ -137,7 +74,7 @@ function isValidHostname(value: string, allowUnderscore = false): boolean {
13774
return validLabel;
13875
});
13976

140-
return isValid && isNotFilename(value) && isNotSemver(value);
77+
return isValid && containsTld(value);
14178
}
14279

14380
export function inferHostname(value: string): JSONHostnameFormat | undefined {

src/formats/uri.ts

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,106 @@
11
import { URL } from "universal-url";
2-
import mime from "mime-types";
3-
import path from "path";
42

53
export type JSONURIFormat = {
64
name: "uri";
75
contentType?: string;
86
};
97

8+
function lookupMimeType(ext?: string): string | undefined {
9+
if (ext === undefined) {
10+
return undefined;
11+
}
12+
13+
const extensionToMimeType: Record<string, string> = {
14+
json: "application/json",
15+
js: "application/javascript",
16+
html: "text/html",
17+
css: "text/css",
18+
txt: "text/plain",
19+
ts: "text/typescript",
20+
tsx: "text/typescript",
21+
aac: "audio/aac",
22+
abw: "application/x-abiword",
23+
arc: "application/x-freearc",
24+
avi: "video/x-msvideo",
25+
azw: "application/vnd.amazon.ebook",
26+
bin: "application/octet-stream",
27+
bmp: "image/bmp",
28+
bz: "application/x-bzip",
29+
bz2: "application/x-bzip2",
30+
cda: "application/x-cdf",
31+
csh: "application/x-csh",
32+
csv: "text/csv",
33+
doc: "application/msword",
34+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
35+
eot: "application/vnd.ms-fontobject",
36+
epub: "application/epub+zip",
37+
gz: "application/gzip",
38+
gif: "image/gif",
39+
htm: "text/html",
40+
ico: "image/vnd.microsoft.icon",
41+
ics: "text/calendar",
42+
jar: "application/java-archive",
43+
jpeg: "image/jpeg ",
44+
jpg: "image/jpeg",
45+
jsonld: "application/ld+json",
46+
mid: "audio/midi",
47+
midi: "audio/midi",
48+
mjs: "text/javascript",
49+
mp3: "audio/mpeg",
50+
mp4: "video/mp4",
51+
mpeg: "video/mpeg",
52+
mpkg: "application/vnd.apple.installer+xml",
53+
odp: "application/vnd.oasis.opendocument.presentation",
54+
ods: "application/vnd.oasis.opendocument.spreadsheet",
55+
odt: "application/vnd.oasis.opendocument.text",
56+
oga: "audio/ogg",
57+
ogv: "video/ogg",
58+
ogx: "application/ogg",
59+
opus: "audio/opus",
60+
otf: "font/otf",
61+
png: "image/png",
62+
pdf: "application/pdf",
63+
php: "application/x-httpd-php",
64+
ppt: "application/vnd.ms-powerpoint",
65+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
66+
rar: "application/vnd.rar",
67+
rtf: "application/rtf",
68+
sh: "application/x-sh",
69+
svg: "image/svg+xml",
70+
swf: "application/x-shockwave-flash",
71+
tar: "application/x-tar",
72+
tif: "image/tiff",
73+
tiff: "image/tiff",
74+
ttf: "font/ttf",
75+
vsd: "application/vnd.visio",
76+
wav: "audio/wav",
77+
weba: "audio/webm",
78+
webm: "video/webm",
79+
webp: "image/webp",
80+
woff: "font/woff",
81+
woff2: "font/woff2",
82+
xhtml: "application/xhtml+xml",
83+
xls: "application/vnd.ms-excel",
84+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
85+
xml: "application/xml",
86+
xul: "application/vnd.mozilla.xul+xml",
87+
zip: "application/zip",
88+
"3gp": "video/3gpp",
89+
"3g2": "video/3gpp2",
90+
"7z": "application/x-7z-compressed",
91+
md: "text/markdown",
92+
};
93+
94+
return extensionToMimeType[ext];
95+
}
96+
1097
export function inferUri(value: string): JSONURIFormat | undefined {
1198
try {
1299
const url = new URL(value);
13100

14101
// Get mimetype from extension
15-
const ext = path.extname(url.pathname);
16-
const mimeType = mime.lookup(ext);
102+
const ext = url.pathname.split(".").pop();
103+
const mimeType = lookupMimeType(ext);
17104

18105
return {
19106
name: "uri",

0 commit comments

Comments
 (0)