-
-
Notifications
You must be signed in to change notification settings - Fork 840
Expand file tree
/
Copy pathdev-server-utils.ts
More file actions
152 lines (120 loc) · 4.22 KB
/
dev-server-utils.ts
File metadata and controls
152 lines (120 loc) · 4.22 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type * as d from '../declarations';
import { version } from '../version';
import contentTypes from './content-types-db.json';
import * as c from './dev-server-constants';
export function responseHeaders(headers: d.DevResponseHeaders, httpCache = false): any {
headers = { ...DEFAULT_HEADERS, ...headers };
if (httpCache) {
headers['cache-control'] = 'max-age=3600';
delete headers['date'];
delete headers['expires'];
}
return headers;
}
const DEFAULT_HEADERS: d.DevResponseHeaders = {
'cache-control': 'no-cache, no-store, must-revalidate, max-age=0',
expires: '0',
date: 'Wed, 1 Jan 2000 00:00:00 GMT',
server: 'Stencil Dev Server ' + version,
'access-control-allow-origin': '*',
'access-control-expose-headers': '*',
};
export function getBrowserUrl(protocol: string, address: string, port: number, basePath: string, pathname: string) {
address = address === `0.0.0.0` ? `localhost` : address;
const portSuffix = !port || port === 80 || port === 443 ? '' : ':' + port;
let path = basePath;
if (pathname.startsWith('/')) {
pathname = pathname.substring(1);
}
path += pathname;
protocol = protocol.replace(/\:/g, '');
return `${protocol}://${address}${portSuffix}${path}`;
}
export function getDevServerClientUrl(devServerConfig: d.DevServerConfig, host: string, protocol: string) {
let address = devServerConfig.address;
let port = devServerConfig.port;
if (host) {
address = host;
port = null;
}
return getBrowserUrl(protocol ?? devServerConfig.protocol, address, port, devServerConfig.basePath, c.DEV_SERVER_URL);
}
export function getContentType(filePath: string) {
const last = filePath.replace(/^.*[/\\]/, '').toLowerCase();
const ext = last.replace(/^.*\./, '').toLowerCase();
const hasPath = last.length < filePath.length;
const hasDot = ext.length < last.length - 1;
return ((hasDot || !hasPath) && (contentTypes as any)[ext]) || 'application/octet-stream';
}
export function isHtmlFile(filePath: string) {
filePath = filePath.toLowerCase().trim();
return filePath.endsWith('.html') || filePath.endsWith('.htm');
}
export function isCssFile(filePath: string) {
filePath = filePath.toLowerCase().trim();
return filePath.endsWith('.css');
}
const TXT_EXT = ['css', 'html', 'htm', 'js', 'json', 'svg', 'xml'];
export function isSimpleText(filePath: string) {
const ext = filePath.toLowerCase().trim().split('.').pop();
return TXT_EXT.includes(ext);
}
export function isExtensionLessPath(pathname: string) {
const parts = pathname.split('/');
const lastPart = parts[parts.length - 1];
return !lastPart.includes('.');
}
export function isSsrStaticDataPath(pathname: string) {
const parts = pathname.split('/');
const fileName = parts[parts.length - 1].split('?')[0];
return fileName === 'page.state.json';
}
export function getSsrStaticDataPath(req: d.HttpRequest) {
const parts = req.url.href.split('/');
const fileName = parts[parts.length - 1];
const fileNameParts = fileName.split('?');
parts.pop();
let ssrPath = new URL(parts.join('/')).href;
if (!ssrPath.endsWith('/') && req.headers) {
const h = new Headers(req.headers);
if (h.get('referer').endsWith('/')) {
ssrPath += '/';
}
}
return {
ssrPath,
fileName: fileNameParts[0],
hasQueryString: typeof fileNameParts[1] === 'string' && fileNameParts[1].length > 0,
};
}
export function isDevClient(pathname: string) {
return pathname.startsWith(c.DEV_SERVER_URL);
}
export function isDevModule(pathname: string) {
return pathname.includes(c.DEV_MODULE_URL);
}
export function isOpenInEditor(pathname: string) {
return pathname === c.OPEN_IN_EDITOR_URL;
}
export function isInitialDevServerLoad(pathname: string) {
return pathname === c.DEV_SERVER_INIT_URL;
}
export function isDevServerClient(pathname: string) {
return pathname === c.DEV_SERVER_URL;
}
export function shouldCompress(devServerConfig: d.DevServerConfig, req: d.HttpRequest) {
if (!devServerConfig.gzip) {
return false;
}
if (req.method !== 'GET') {
return false;
}
const acceptEncoding = req.headers && req.headers['accept-encoding'];
if (typeof acceptEncoding !== 'string') {
return false;
}
if (!acceptEncoding.includes('gzip')) {
return false;
}
return true;
}