8000 :sparkles: add ability to fix ambiguous tag names · momocow/webpack-userscript@9f3e7d0 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 9f3e7d0

Browse files
committed
✨ add ability to fix ambiguous tag names
1 parent 60e8fd1 commit 9f3e7d0

File tree

7 files changed

+128
-127
lines changed

7 files changed

+128
-127
lines changed

lib/headers.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,6 @@ export class HeadersImpl implements StrictHeadersProps {
247247
return instanceToPlain(this, { exposeUnsetFields: false });
248248
}
249249

250-
public update(
251-
props: HeadersProps,
252-
options: HeadersFactoryOptions = {},
253-
): Headers {
254-
return (this.constructor as typeof HeadersImpl).fromJSON(
255-
{
256-
...this.toJSON(),
257-
...props,
258-
},
259-
options,
260-
);
261-
}
262-
263250
public render({
264251
prefix = '// ==UserScript==\n',
265252
suffix = '// ==/UserScript==\n',

lib/hook.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { AsyncHeadersReducer, HeadersReducer, HeadersWaterfall } from './types';
2+
3+
export function wrapHook(
4+
fn: HeadersReducer,
5+
): (data: HeadersWaterfall) => HeadersWaterfall;
6+
export function wrapHook(
7+
fn: AsyncHeadersReducer,
8+
): (data: HeadersWaterfall) => Promise<HeadersWaterfall>;
9+
10+
export function wrapHook(
11+
fn: HeadersReducer | AsyncHeadersReducer,
12+
): (data: HeadersWaterfall) => HeadersWaterfall | Promise<HeadersWaterfall> {
13+
return (data) => {
14+
const originalHeaders = data.headers;
15+
const headers = fn(data);
16+
17+
if (headers instanceof Promise) {
18+
return headers.then((headers) => ({
19+
...data,
20+
headers: headers ?? originalHeaders,
21+
}));
22+
}
23+
24+
return { ...data, headers: headers ?? originalHeaders };
25+
};
26+
}

lib/hooks.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

lib/plugin.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { Compilation, Compiler, sources } from 'webpack';
55

66
import { findPackage, FsReadFile, FsStat, readJSON, writeJSON } from './fs';
77
import { Headers, HeadersImpl, HeadersProps } from './headers';
8+
import { wrapHook } from './hook';
89
import {
9-
applyWhitelist,
10+
fixTagNames,
1011
resolveDownloadBaseUrl,
1112
resolveUpdateBaseUrl,
1213
setDefaultMatch,
13-
wrapHook,
14-
} from './hooks';
14+
} from './reducers';
1515
import { processSSRI } from './ssri';
1616
import {
1717
FileInfo,
@@ -61,8 +61,7 @@ export class UserscriptPlugin {
6161
}
6262

6363
protected applyHooks(): void {
64-
const { downloadBaseUrl, updateBaseUrl, ssri, headers, whitelist, strict } =
65-
this.options;
64+
const { downloadBaseUrl, updateBaseUrl, ssri, headers } = this.options;
6665

6766
if (typeof headers === 'function') {
6867
this.hooks.processHeaders.tapPromise(
@@ -94,12 +93,7 @@ export class UserscriptPlugin {
9493
wrapHook(setDefaultMatch),
9594
);
9695

97-
if (whitelist ?? strict) {
98-
this.hooks.processHeaders.tap(
99-
applyWhitelist.name,
100-
wrapHook(applyWhitelist),
101-
);
102-
}
96+
this.hooks.processHeaders.tap(fixTagNames.name, wrapHook(fixTagNames));
10397
}
10498

10599
protected headersFactory(props: HeadersProps): Headers {

lib/reducers.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { URL } from 'url';
2+
3+
import { StrictHeadersProps } from './headers';
4+
import { HeadersReducer } from './types';
5+
6+
export const setDefaultMatch: HeadersReducer = ({ headers }) => {
7+
if (headers.include === undefined && headers.match === undefined) {
8+
return {
9+
...headers,
10+
match: '*://*/*',
11+
};
12+
}
13+
return headers;
14+
};
15+
16+
export const resolveDownloadBaseUrl: HeadersReducer = ({
17+
headers,
18+
fileInfo,
19+
options,
20+
}) => {
21+
if (headers.downloadURL === undefined) {
22+
return {
23+
...headers,
24+
downloadURL: new URL(
25+
fileInfo.userjsFile,
26+
options.downloadBaseUrl,
27+
).toString(),
28+
};
29+
}
30+
return headers;
31+
};
32+
33+
export const resolveUpdateBaseUrl: HeadersReducer = ({
34+
headers,
35+
fileInfo,
36+
options,
37+
}) => {
38+
if (headers.updateURL === undefined) {
39+
return {
40+
...headers,
41+
updateURL: !options.metajs
42+
? headers.downloadURL
43+
: new URL(fileInfo.metajsFile, options.updateBaseUrl).toString(),
44+
};
45+
}
46+
return headers;
47+
};
48+
49+
export const FIXABLE_TAG_NAMES = new Map<string, keyof StrictHeadersProps>([
50+
['updateUrl', 'updateURL'],
51+
['iconUrl', 'iconURL'],
52+
['icon64Url', 'icon64URL'],
53+
['installUrl', 'installURL'],
54+
['supportUrl', 'supportURL'],
55+
['downloadUrl', 'downloadURL'],
56+
['homepageUrl', 'homepageURL'],
57+
]);
58+
59+
export const fixTagNames: HeadersReducer = ({ headers }) => {
60+
for (const [source, target] of FIXABLE_TAG_NAMES) {
61+
if (headers[source] !== undefined) {
62+
if (headers[target] !== undefined) {
63+
throw new Error(`ambiguous tags: ("${source}", "${target}")`);
64+
}
65+
66+
headers = {
67+
...headers,
68+
[source]: undefined,
69+
[target]: headers[source],
70+
};
71+
}
72+
}
73+
74+
return headers;
75+
};

lib/ssri.ts

Lines changed: 17 additions & 11 deletions
17
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
import { Readable } from 'stream';
1010
import { URL } from 'url';
1111

12-
import { Headers, HeadersProps } from './headers';
12+
import { HeadersProps } from './headers';
1313
import {
14-
ProcessHeadersAsyncHook,
14+
AsyncHeadersReducer,
1515
SSRIAlgorithm,
1616
SSRILock,
17
SSRIMap,
@@ -24,13 +24,17 @@ export const SSRI_MAP: Map<string, Map<SSRIAlgorithm, string>> = new Map();
2424
const concurrency = parseInt(process.env.WPUS_SSRI_CONCURRENCY ?? '');
2525
const limit = pLimit(Number.isNaN(concurrency) ? 6 : concurrency);
2626

27-
export const processSSRI: ProcessHeadersAsyncHook = async (data) => {
27+
export const processSSRI: AsyncHeadersReducer = async (data) => {
2828
const {
2929
headers,
3030
ssriLock,
3131
options: { ssri },
3232
} = data;
3333

34+
if (headers.resource == undefined && headers.require === undefined) {
35+
return headers;
36+
}
37+
3438
const ssriOptions: SSRIOptions =
3539
ssri === true || ssri === undefined ? {} : ssri;
3640

@@ -78,7 +82,10 @@ export const processSSRI: ProcessHeadersAsyncHook = async (data) => {
7882
data.ssriLock = toSSRILock(ssriMap);
7983
}
8084

81-
return updateHeaders(headers, ssriMap);
85+
return {
86+
...headers,
87+
...patchHeaders(headers, ssriMap),
88+
};
8289
};
8390

8491
export function normalizeURL(url: string): string {
@@ -88,7 +95,7 @@ export function normalizeURL(url: string): string {
8895
}
8996

9097
export function getTargetURLs(
91-
headers: Headers,
98+
headers: HeadersProps,
9299
options: Pick<SSRIOptions, 'include' | 'exclude'>,
93100
): string[] {
94101
const urls: string[] = [];
@@ -202,13 +209,12 @@ export function updateURL(url: string, ssriMap: SSRIMap): string {
202209
return urlObj.toString();
203210
}
204211

205-
export function updateHeaders(headers: Headers, ssriMap: SSRIMap): Headers {
212+
export function patchHeaders(
213+
headers: HeadersProps,
214+
ssriMap: SSRIMap,
215+
): HeadersProps {
206216
const headersProps: HeadersProps = {};
207217

208-
if (headers.resource == undefined && headers.require === undefined) {
209-
return headers;
210-
}
211-
212218
if (headers.require !== undefined) {
213219
if (Array.isArray(headers.require)) {
214220
headersProps.require = headers.require.map((url) =>
@@ -228,5 +234,5 @@ export function updateHeaders(headers: Headers, ssriMap: SSRIMap): Headers {
228234
);
229235
}
230236

231-
return headers.update(headersProps);
237+
return headersProps;
232238
}

lib/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IntegrityMap } from 'ssri';
22
import { URL } from 'url';
33
import { Chunk, Compilation } from 'webpack';
44

5-
import { Headers, HeadersProps } from './headers';
5+
import { HeadersProps } from './headers';
66

77
export interface UserscriptOptions {
88
root?: string;
@@ -18,7 +18,7 @@ export interface UserscriptOptions {
1818
ssri?: true | SSRIOptions;
1919
}
2020

21-
export type HeadersProvider = ProcessHeadersHook | ProcessHeadersAsyncHook;
21+
export type HeadersProvider = HeadersReducer | AsyncHeadersReducer;
2222
export type HeadersFile = string;
2323

2424
export type HeadersOption =
@@ -59,8 +59,8 @@ export interface HeadersWaterfall {
5959
ssriLock?: SSRILock;
6060
}
6161

62-
export type ProcessHeadersHook = (data: HeadersWaterfall) => Headers;
62+
export type HeadersReducer = (data: HeadersWaterfall) => HeadersProps;
6363

64-
export type ProcessHeadersAsyncHook = (
64+
export type AsyncHeadersReducer = (
6565
data: HeadersWaterfall,
66-
) => Promise<Headers>;
66+
) => Promise<HeadersProps>;

0 commit comments

Comments
 (0)
0