-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.dev-server.config.js
214 lines (172 loc) · 5.87 KB
/
webpack.dev-server.config.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* eslint-disable @typescript-eslint/no-var-requires */
/**
* This webpack config establishes a proxy server to enable a local build of circulation-admin to
* connect to a remote Circulation Manager back-end. Requests for webpack assets are served from
* memory; other requests are proxied to the specified Circulation Manager.
*
* Usage:
* npm run dev-server -- --env=backend=https://gorgon.tpp-qa.lyrasistechnology.org
* or
* npx webpack serve --config webpack.dev-server.config --env=backend=https://gorgon.tpp-qa.lyrasistechnology.org
*
* This makes the circulation-admin webapp available at http://localhost:8080/admin.
*/
const {
createProxyMiddleware,
responseInterceptor,
} = require("http-proxy-middleware");
const { merge } = require("webpack-merge");
const { URL } = require("url");
const dev = require("./webpack.dev.config.js");
/**
* The public path to local webpack assets. This is chosen to have low chance of collision with any
* path on the back-end (e.g., a library ID, or "/admin"). This should start and end with slashes.
*/
const devAssetsPublicPath = "/webpack-dev-assets/";
/**
* Build the webpack configuration.
* See https://webpack.js.org/configuration/configuration-types/#exporting-a-function
*
* @param {object} env The environment.
* @returns The webpack configuration.
*/
module.exports = (env) => {
const { backend } = env;
if (!backend) {
console.error("Please specify the URL of a Circulation Manager back-end.");
console.error(
"Example: npm run dev-server -- --env=backend=https://gorgon.tpp-qa.lyrasistechnology.org"
);
throw "No back-end URL was specified.";
}
console.info(`Using Circulation Manager back-end: ${backend}`);
const backendUrl = new URL(backend);
/**
* Rewrite a location header (as received in a 3xx response). This changes back-end URLs to
* point to the local server instead. The CM may redirect to a URL that contains a "redirect"
* parameter that contains another URL. The "redirect" parameter is also rewritten.
*
* @param res The response.
* @param req The request.
*/
const rewriteLocationHeader = (res, req) => {
const location = res.getHeader("location");
if (!location) {
return;
}
const locationUrl = new URL(location, backendUrl);
if (locationUrl.host !== backendUrl.host) {
return;
}
const requestHost = req.headers.host;
if (!requestHost) {
return;
}
locationUrl.protocol = "http";
locationUrl.host = requestHost;
const redirectParam = locationUrl.searchParams.get("redirect");
if (redirectParam) {
const redirectUrl = new URL(redirectParam);
if (redirectUrl.host == backendUrl.host) {
redirectUrl.protocol = "http";
redirectUrl.host = requestHost;
locationUrl.searchParams.set("redirect", redirectUrl.href);
}
}
res.setHeader("location", locationUrl.href);
};
/**
* Rewrites an OpenSearch description response. This changes back-end URLs in the description to
* point to the local server instead. This is a simple find-and-replace.
*
* @param responseBuffer A buffer containing the response body.
* @param req The request.
* @returns
*/
const rewriteOpenSearch = (responseBuffer, req) => {
const requestHost = req.headers.host;
if (!requestHost) {
return responseBuffer;
}
const osd = responseBuffer.toString("utf8");
return osd.replace(
new RegExp(backendUrl.origin, "g"),
`http://${requestHost}`
);
};
/**
* Rewrites an OPDS response. This changes back-end URLs in the feed to point to the local server
* instead. This is a simple find-and-replace.
*
* @param responseBuffer A buffer containing the response body.
* @param req The request.
* @returns
*/
const rewriteOPDS = (responseBuffer, req) => {
const requestHost = req.headers.host;
if (!requestHost) {
return responseBuffer;
}
const feed = responseBuffer.toString("utf8");
return feed.replace(
new RegExp(backendUrl.origin, "g"),
`http://${requestHost}`
);
};
/**
* Rewrites an HTML response. This changes jsdelivr CDN URLs in the page to point to the webpack
* assets on the local server instead. This is a simple find-and-replace.
*
* @param responseBuffer A buffer containing the response body.
* @param req The request.
* @returns
*/
const rewriteHTML = (responseBuffer, req) => {
const requestHost = req.headers.host;
if (!requestHost) {
return responseBuffer;
}
const page = responseBuffer.toString("utf8");
const packageName = process.env.npm_package_name;
const cdnUrlPattern = `"https://cdn.jsdelivr.net/npm/${packageName}(@.*?)?/dist/(.*?)"`;
return page.replace(
new RegExp(cdnUrlPattern, "g"),
`"http://${requestHost}${devAssetsPublicPath}$2"`
);
};
const proxyMiddleware = createProxyMiddleware({
changeOrigin: true,
onProxyRes: responseInterceptor(
async (responseBuffer, proxyRes, req, res) => {
rewriteLocationHeader(res, req);
const contentType = res.getHeader("content-type");
if (contentType.startsWith("application/atom+xml")) {
return rewriteOPDS(responseBuffer, req);
}
if (contentType.startsWith("text/html")) {
return rewriteHTML(responseBuffer, req);
}
if (contentType.startsWith("application/opensearchdescription+xml")) {
return rewriteOpenSearch(responseBuffer, req);
}
return responseBuffer;
}
),
proxyTimeout: 120000,
secure: false,
selfHandleResponse: true,
target: backend,
timeout: 120000,
});
const config = merge(dev, {
devServer: {
onAfterSetupMiddleware: (devServer) => {
devServer.app.use("/", proxyMiddleware);
},
},
output: {
publicPath: devAssetsPublicPath,
},
});
return config;
};