1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
export enum MediaType {
JavaScript,
Jsx,
Mjs,
Cjs,
TypeScript,
Mts,
Cts,
Dts,
Dmts,
Dcts,
Tsx,
Json,
Wasm,
TsBuildInfo,
SourceMap,
Unknown,
}
const split_path = (path: string) => path.match(/^((?:.*\/)?([^\/]*?))(?:\.([^\.\/]*)$|$)/)?.slice(1);
/// Definition files don't have separate content types and so we have to "guess"
/// at what they are meant to be.
function map_typescript_like(
path: string,
base_type: MediaType,
definition_type: MediaType,
): MediaType {
const match = split_path(path);
if (!match) return base_type;
const [file_stem, file_name, file_ext] = match;
// .ts files that contain .d. in the file name are always considered a typescript declaration file.
// See: https://github.com/microsoft/TypeScript/issues/53319#issuecomment-1474174018
if (
file_stem.endsWith(".d")
|| (file_ext === ("ts")
&& file_stem.includes(".d."))
)
{
return definition_type;
}
return base_type;
}
export function as_ts_extension(type: MediaType): string {
return ({
[MediaType.JavaScript]: ".js",
[MediaType.Jsx]: ".jsx",
[MediaType.Mjs]: ".mjs",
[MediaType.Cjs]: ".cjs",
[MediaType.TypeScript]: ".ts",
[MediaType.Mts]: ".mts",
[MediaType.Cts]: ".cts",
[MediaType.Dts]: ".d.ts",
[MediaType.Dmts]: ".d.mts",
[MediaType.Dcts]: ".d.cts",
[MediaType.Tsx]: ".tsx",
[MediaType.Json]: ".json",
// TypeScript doesn't have an "unknown", so we will treat WASM as JS for
// mapping purposes, though in reality, it is unlikely to ever be passed
// to the compiler.
[MediaType.Wasm]: ".js",
[MediaType.TsBuildInfo]: ".tsbuildinfo",
// TypeScript doesn't have an "source map", so we will treat SourceMap as
// JS for mapping purposes, though in reality, it is unlikely to ever be
// passed to the compiler.
[MediaType.SourceMap]: ".js",
// TypeScript doesn't have an "unknown", so we will treat unknowns as JS
// for mapping purposes, though in reality, it is unlikely to ever be
// passed to the compiler.
[MediaType.Unknown]: ".js",
})[type];
}
export function as_content_type(type: MediaType): string | undefined {
return ({
[MediaType.JavaScript]: ("text/javascript"),
[MediaType.Jsx]: ("text/jsx"),
[MediaType.Mjs]: ("text/javascript"),
[MediaType.Cjs]: ("text/javascript"),
[MediaType.TypeScript]: ("text/typescript"),
[MediaType.Mts]: ("text/typescript"),
[MediaType.Cts]: ("text/typescript"),
[MediaType.Dts]: ("text/typescript"),
[MediaType.Dmts]: ("text/typescript"),
[MediaType.Dcts]: ("text/typescript"),
[MediaType.Tsx]: ("text/tsx"),
[MediaType.Json]: ("application/json"),
[MediaType.Wasm]: ("application/wasm"),
[MediaType.TsBuildInfo]: ("application/json"),
[MediaType.SourceMap]: ("application/json"),
[MediaType.Unknown]: undefined,
})[type];
}
export function is_declaration(type: MediaType): type is MediaType.Dts | MediaType.Dcts | MediaType.Dmts {
return type === MediaType.Dts || type === MediaType.Dmts || type === MediaType.Dcts;
}
export function from_response(response: Response) {
return from_url_and_content_type(new URL(response.url), response.headers.get("content-type"));
}
export function from_url_and_content_type(
url: URL,
maybe_content_type?: string,
): MediaType {
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
March 8, 2024