8000 :sparkles: add additional tags · momocow/webpack-userscript@469daf6 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 469daf6

Browse files
committed
✨ add additional tags
close #87
1 parent 92ce8bd commit 469daf6

File tree

4 files changed

+192
-13
lines changed

4 files changed

+192
-13
lines changed

lib/features/validate-headers/headers.ts

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Expose } from 'class-transformer';
1+
import { Expose, Type } from 'class-transformer';
22
import {
33
IsBoolean,
44
IsEnum,
@@ -7,18 +7,50 @@ import {
77
IsSemVer,
88
IsString,
99
IsUrl,
10+
ValidateNested,
1011
} from 'class-validator';
1112

12-
// import { getBorderCharacters, table } from 'table';
1313
import {
14+
CompatibilityValue,
15+
InjectInto,
1416
MultiValue,
1517
NamedValue,
16-
RunAtValue,
18+
RunAt,
19+
Sandbox,
1720
SingleValue,
1821
StrictHeadersProps,
1922
SwitchValue,
2023
} from '../../types';
2124

25+
class Compatibility implements CompatibilityValue {
26+
[x: string]: SingleValue;
27+
28+
@Expose()
29+
@IsOptional()
30+
@IsString()
31+
public readonly firefox?: string;
32+
33+
@Expose()
34+
@IsOptional()
35+
@IsString()
36+
public readonly chrome?: string;
37+
38+
@Expose()
39+
@IsOptional()
40+
@IsString()
41+
public readonly opera?: string;
42+
43+
@Expose()
44+
@IsOptional()
45+
@IsString()
46+
public readonly safari?: string;
47+
48+
@Expose()
49+
@IsOptional()
50+
@IsString()
51+
public readonly edge?: string;
52+
}
53+
2254
export class Headers implements StrictHeadersProps {
2355
@Expose()
2456
@IsString()
@@ -119,6 +151,11 @@ export class Headers implements StrictHeadersProps {
119151
@IsString({ each: true })
120152
public readonly match?: MultiValue;
121153

154+
@Expose()
155+
@IsOptional()
156+
@IsString({ each: true })
157+
public readonly 'exclude-match'?: MultiValue;
158+
122159
@Expose()
123160
@IsOptional()
124161
@IsString({ each: true })
@@ -166,6 +203,48 @@ export class Headers implements StrictHeadersProps {
166203

167204
@Expose()
168205
@IsOptional()
169-
@IsEnum(RunAtValue)
170-
public readonly ['run-at']?: RunAtValue;
206+
@IsEnum(RunAt)
207+
public readonly 'run-at'?: RunAt;
208+
209+
@Expose()
210+
@IsOptional()
211+
@IsString()
212+
public readonly copyright?: SingleValue;
213+
214+
@Expose()
215+
@IsOptional()
216+
@IsEnum(Sandbox)
217+
public readonly sandbox?: Sandbox;
218+
219+
@Expose()
220+
@IsOptional()
221+
@IsEnum(InjectInto)
222+
public readonly 'inject-into'?: InjectInto;
223+
224+
@Expose()
225+
@IsOptional()
226+
@IsString()
227+
public readonly license?: SingleValue;
228+
229+
@Expose()
230+
@IsOptional()
231+
@IsUrl()
232+
public readonly contributionURL?: SingleValue;
233+
234+
@Expose()
235+
@IsOptional()
236+
@IsString()
237+
public readonly contributionAmount?: SingleValue;
238+
239+
@Expose()
240+
@Type(() => Compatibility)
241+
@IsOptional()
242+
@ValidateNested()
243+
public readonly compatible?: Compatibility;
244+
245+
@Expose()
246+
@Type(() => Compatibility)
247+
@IsOptional()
248+
@ValidateNested()
249+
public readonly incompatible?: Compatibility;
171250
}

lib/types.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,36 @@ export type ValueType =
2727
| SwitchValue
2828
| undefined;
2929

30-
export enum RunAtValue {
30+
export type EnumValue<T extends string> = T | `${T}`;
31+
32+
export enum RunAt {
3133
DocumentStart = 'document-start',
3234
DocumentBody = 'document-body',
3335
DocumentEnd = 'document-end',
3436
DocumentIdle = 'document-idle',
3537
ContextMenu = 'context-menu',
3638
}
3739

40+
export enum Sandbox {
41+
Raw = 'raw',
42+
JavaScript = 'JavaScript',
43+
DOM = 'DOM',
44+
}
45+
46+
export enum InjectInto {
47+
Page = 'page',
48+
Content = 'content',
49+
Auto = 'auto',
50+
}
51+
52+
export interface CompatibilityValue extends NamedValue {
53+
firefox?: string;
54+
chrome?: string;
55+
opera?: string;
56+
safari?: string;
57+
edge?: string;
58+
}
59+
3860
export interface StrictHeadersProps {
3961
name?: SingleValue;
4062
version?: SingleValue;
@@ -56,6 +78,7 @@ export interface StrictHeadersProps {
5678
supportURL?: SingleValue;
5779
include?: MultiValue;
5880
match?: MultiValue;
81+
'exclude-match'?: MultiValue;
5982
exclude?: MultiValue;
6083
require?: MultiValue;
6184
resource?: NamedValue;
@@ -65,7 +88,15 @@ export interface StrictHeadersProps {
6588
noframes?: SwitchValue;
6689
unwrap?: SwitchValue;
6790
antifeature?: NamedValue;
68-
['run-at']?: RunAtValue;
91+
'run-at'?: EnumValue<RunAt>;
92+
copyright?: SingleValue;
93+
sandbox?: EnumValue<Sandbox>;
94+
'inject-into'?: EnumValue<InjectInto>;
95+
license?: SingleValue;
96+
contributionURL?: SingleValue;
97+
contributionAmount?: SingleValue;
98+
compatible?: CompatibilityValue;
99+
incompatible?: CompatibilityValue;
69100
}
70101

71102
export interface HeadersProps extends StrictHeadersProps {

test/integration/headers/fixtures.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { File, GlobalFixtures } from '../fixtures';
44

55
interface TagSample {
66
value: ValueType;
7-
expect: string;
7+
expect: string | string[];
88
}
99

1010
interface TagCase {
@@ -23,8 +23,72 @@ export class Fixtures extends GlobalFixtures {
2323

2424
public static readonly tagSamples: Record<string, TagCase> = {
2525
'run-at': {
26-
validValues: [{ value: 'document-body', expect: 'document-body' }],
26+
validValues: [
27+
{ value: 'document-start', expect: 'document-start' },
28+
{ value: 'document-body', expect: 'document-body' },
29+
{ value: 'document-end', expect: 'document-end' },
30+
{ value: 'document-idle', expect: 'document-idle' },
31+
{ value: 'context-menu', expect: 'context-menu' },
32+
],
2733
invalidValues: [{ value: 'a' }],
2834
},
35+
sandbox: {
36+
validValues: [
37+
{ value: 'raw', expect: 'raw' },
38+
{ value: 'JavaScript', expect: 'JavaScript' },
39+
{ value: 'DOM', expect: 'DOM' },
40+
],
41+
invalidValues: [{ value: 'a' }],
42+
},
43+
'inject-into': {
44+
validValues: [
45+
{ value: 'page', expect: 'page' },
46+
{ value: 'content', expect: 'content' },
47+
{ value: 'auto', expect: 'auto' },
48+
],
49+
invalidValues: [{ value: 'a' }],
50+
},
51+
compatible: {
52+
validValues: [
53+
{
54+
value: {
55+
firefox: 'compatible string',
56+
chrome: 'compatible string',
57+
opera: 'compatible string',
58+
safari: 'compatible string',
59+
edge: 'compatible string',
60+
},
61+
expect: [
62+
'firefox compatible string',
63+
'chrome compatible string',
64+
'opera compatible string',
65+
'safari compatible string',
66+
'edge compatible string',
67+
],
68+
},
69+
],
70+
invalidValues: [{ value: { unknownBrowser: 'compatible string' } }],
71+
},
72+
incompatible: {
73+
validValues: [
74+
{
75+
value: {
76+
firefox: 'compatible string',
77+
chrome: 'compatible string',
78+
opera: 'compatible string',
79+
safari: 'compatible string',
80+
edge: 'compatible string',
81+
},
82+
expect: [
83+
'firefox compatible string',
84+
'chrome compatible string',
85+
'opera compatible string',
86+
'safari compatible string',
87+
'edge compatible string',
88+
],
89+
},
90+
],
91+
invalidValues: [{ value: { unknownBrowser: 'incompatible string' } }],
92+
},
2993
};
3094
}

test/integration/headers/index.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('headers', () => {
114114
)) {
115115
describe(tag, () => {
116116
for (const { value, expect: expectedOutput } of validValues) {
117-
it('valid', async () => {
117+
it('valid case: ' + JSON.stringify(value), async () => {
118118
const output = await compile(input, {
119119
...Fixtures.webpackConfig,
120120
plugins: [
@@ -133,13 +133,18 @@ describe('headers', () => {
133133
.readFileSync('/dist/output.meta.js')
134134
.toString('utf-8');
135135

136-
expect(findTags(tag, expectedOutput, userJs)).toHaveLength(1);
137-
expect(findTags(tag, expectedOutput, metaJs)).toHaveLength(1);
136+
const expectedOutputList = !Array.isArray(expectedOutput)
137+
? [expectedOutput]
138+
: expectedOutput;
139+
for (const outputValue of expectedOutputList) {
140+
expect(findTags(tag, outputValue, userJs)).toHaveLength(1);
141+
expect(findTags(tag, outputValue, metaJs)).toHaveLength(1);
142+
}
138143
});
139144
}
140145

141146
for (const { value } of invalidValues) {
142-
it('invalid', () => {
147+
it('invalid case: ' + JSON.stringify(value), () => {
143148
const promise = compile(input, {
144149
...Fixtures.webpackConfig,
145150
plugins: [

0 commit comments

Comments
 (0)
0