8000 :sparkles: add ability to generate proxy script. · momocow/webpack-userscript@09d313c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 09d313c

Browse files
committed
✨ add ability to generate proxy script.
1 parent ced22ec commit 09d313c

File tree

6 files changed

+130
-16
lines changed

6 files changed

+130
-16
lines changed

lib/plugin.ts

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@ import {
1414
resolveUpdateBaseUrl,
1515
setDefaultMatch,
1616
} from './reducers';
17+
import { processProxyScript } from './reducers/proxy-script';
1718
import {
1819
FileInfo,
1920
HeadersWaterfall,
2021
SSRILock,
2122
UserscriptOptions,
2223
} from './types';
24+
import { interpolate } from './util';
2325

2426
const { ConcatSource, RawSource } = sources;
2527

2628
export class UserscriptPlugin {
2729
public readonly hooks = {
2830
processHeaders: new AsyncSeriesWaterfallHook<HeadersWaterfall>(['headers']),
31+
processProxyHeaders: new AsyncSeriesWaterfallHook<HeadersWaterfall>([
32+
'headers',
33+
]),
2934
};
3035

3136
public constructor(
@@ -73,7 +78,8 @@ export class UserscriptPlugin {
7378
}
7479

7580
protected applyHooks(): void {
76-
const { downloadBaseUrl, updateBaseUrl, ssri, headers } = this.options;
81+
const { downloadBaseUrl, updateBaseUrl, ssri, headers, proxyScript } =
82+
this.options;
7783

7884
if (typeof headers === 'function') {
7985
this.hooks.processHeaders.tapPromise(
@@ -114,6 +120,13 @@ export class UserscriptPlugin {
114120
interpolateValues.name,
115121
wrapHook(interpolateValues),
116122
);
123+
124+
if (proxyScript) {
125+
this.hooks.processProxyHeaders.tap(
126+
processProxyScript.name,
127+
wrapHook(processProxyScript),
128+
);
129+
}
117130
}
118131

119132
protected headersFactory(props: HeadersProps): Readonly<Headers> {
@@ -288,7 +301,8 @@ export class UserscriptPlugin {
288301
data: CompilerData,
289302
fileInfo: FileInfo,
290303
): Promise<void> {
291-
const { prefix, pretty, suffix, whitelist, strict } = this.options;
304+
const { prefix, pretty, suffix, whitelist, strict, proxyScript, metajs } =
305+
this.options;
292306

293307
const { headers: headersProps, ssriLock } =
294308
await this.hooks.processHeaders.promise({
@@ -306,12 +320,52 @@ export class UserscriptPlugin {
306320
headers.validate({ whitelist: whitelist ?? true });
307321
}
308322

323+
let proxyScriptFile: string | undefined;
324+
let proxyHeaders: Readonly<Headers> | undefined;
325+
326+
if (proxyScript) {
327+
const { headers: proxyHeadersProps } =
328+
await this.hooks.processProxyHeaders.promise({
329+
headers: headersProps,
330+
ssriLock,
331+
fileInfo,
332+
compilation,
333+
buildNo: data.buildNo,
334+
options: this.options,
335+
});
336+
337+
proxyHeaders = this.headersFactory(proxyHeadersProps);
338+
339+
if (strict) {
340+
proxyHeaders.validate({ whitelist: whitelist ?? true });
341+
}
342+
343+
if (proxyScript === true || proxyScript.filename === undefined) {
344+
proxyScriptFile = '[basename].proxy.user.js';
345+
} else {
346+
proxyScriptFile = interpolate(proxyScript.filename, {
347+
chunkName: fileInfo.chunk.name,
348+
file: fileInfo.originalFile,
349+
filename: fileInfo.filename,
350+
basename: fileInfo.basename,
351+
query: fileInfo.query,
352+
buildNo: data.buildNo.toString(),
353+
buildTime: Date.now().toString(),
354+
});
355+
}
356+
}
357+
309358
const { originalFile, chunk, metajsFile, userjsFile } = fileInfo;
310359
const headersStr = headers.render({
311360
prefix,
312361
pretty,
313362
suffix,
314363
});
364+
const proxyHeadersStr = proxyHeaders?.render({
365+
prefix,
366+
pretty,
367+
suffix,
368+
});
315369
const sourceAsset = compilation.getAsset(originalFile);
316370
if (!sourceAsset) {
317371
return;
@@ -321,14 +375,25 @@ export class UserscriptPlugin {
321375
userjsFile,
322376
new ConcatSource(headersStr, '\n', sourceAsset.source),
323377
{
324-
related: { metajs: metajsFile },
325378
minimized: true,
326379
},
327380
);
328-
compilation.emitAsset(metajsFile, new RawSource(headersStr), {
329-
related: { userjs: userjsFile },
330-
minimized: true,
331-
});
381+
382+
if (metajs !== false) {
383+
compilation.emitAsset(
384+
metajsFile,
385+
new RawSource(proxyHeadersStr ?? headersStr),
386+
{
387+
minimized: true,
388+
},
389+
);
390+
}
391+
392+
if (proxyHeadersStr !== undefined && proxyScriptFile !== undefined) {
393+
compilation.emitAsset(proxyScriptFile, new RawSource(proxyHeadersStr), {
394+
minimized: true,
395+
});
396+
}
332397

333398
chunk.files.add(userjsFile);
334399
chunk.auxiliaryFiles.add(metajsFile);

lib/reducers/base-urls.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export const resolveUpdateBaseUrl: HeadersReducer = ({
2727
if (headers.updateURL === undefined) {
2828
return {
2929
...headers,
30-
updateURL: !options.metajs
31-
? headers.downloadURL
32-
: new URL(fileInfo.metajsFile, options.updateBaseUrl).toString(),
30+
updateURL:
31+
options.metajs === false
32+
? headers.downloadURL
33+
: new URL(fileInfo.metajsFile, options.updateBaseUrl).toString(),
3334
};
3435
}
3536
return headers;

lib/reducers/interpolate.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { HeadersReducer } from '../types';
2+
import { interpolate } from '../util';
23

34
export const interpolateValues: HeadersReducer = ({
45
fileInfo: { chunk, originalFile, filename, basename, query },
@@ -51,9 +52,3 @@ export const interpolateValues: HeadersReducer = ({
5152

5253
return headers;
5354
};
54-
55-
export function interpolate(str: string, data: Record<string, string>): string {
56-
return Object.entries(data).reduce((value, [dataKey, dataVal]) => {
57-
return value.replace(new RegExp(`\\[${dataKey}\\]`, 'g'), `${dataVal}`);
58-
}, str);
59-
}

lib/reducers/proxy-script.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { URL } from 'url';
2+
3+
import { HeadersReducer } from '../types';
4+
import { interpolate } from '../util';
5+
6+
export const processProxyScript: HeadersReducer = ({
7+
headers,
8+
fileInfo: { chunk, originalFile, filename, basename, query, userjsFile },
9+
buildNo,
10+
options: { proxyScript },
11+
}) => {
12+
if (proxyScript) {
13+
const devBaseUrl =
14+
proxyScript === true || proxyScript.baseUrl === undefined
15+
? 'http://localhost:8080/'
16+
: interpolate(proxyScript.baseUrl, {
17+
chunkName: chunk.name,
18+
file: originalFile,
19+
filename,
20+
basename,
21+
query,
22+
buildNo: buildNo.toString(),
23+
buildTime: Date.now().toString(),
24+
});
25+
26+
const requireTags = Array.isArray(headers.require)
27+
? headers.require
28+
: typeof headers.require === 'string'
29+
? [headers.require]
30+
: [];
31+
32+
headers = {
33+
...headers,
34+
require: [...requireTags, new URL(userjsFile, devBaseUrl).toString()],
35+
downloadURL: undefined,
36+
updateURL: undefined,
37+
installURL: undefined,
38+
};
39+
}
40+
41+
return headers;
42+
};

lib/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface UserscriptOptions {
1616
downloadBaseUrl?: string;
1717
updateBaseUrl?: string;
1818
ssri?: true | SSRIOptions;
19+
proxyScript?: true | ProxyScriptOptions;
1920
}
2021

2122
export type HeadersProvider = HeadersReducer | AsyncHeadersReducer;
@@ -41,6 +42,11 @@ export interface SSRIOptions {
4142
lock?: boolean | string;
4243
}
4344

45+
export interface ProxyScriptOptions {
46+
filename?: string;
47+
baseUrl?: string;
48+
}
49+
4450
export interface FileInfo {
4551
chunk: Chunk;
4652
originalFile: string;

lib/util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function interpolate(str: string, data: Record<string, string>): string {
2+
return Object.entries(data).reduce((value, [dataKey, dataVal]) => {
3+
return value.replace(new RegExp(`\\[${dataKey}\\]`, 'g'), `${dataVal}`);
4+
}, str);
5+
}

0 commit comments

Comments
 (0)
0