-
Notifications
You must be signed in to change notification settings - Fork 648
/
parser.ts
193 lines (145 loc) · 5.88 KB
/
parser.ts
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
import {
ElementFound,
FetchEnd,
NetworkData,
Parser
} from 'hint/dist/src/lib/types';
import { normalizeString } from '@hint/utils-string';
import { isHTTP, isHTTPS } from '@hint/utils-network';
import { ManifestEvents } from './types';
import { Engine } from 'hint/dist/src/lib/engine';
import { IJSONResult, parseJSON, SchemaValidationResult, validate } from '@hint/utils-json';
export * from './types';
// Using `require` instead of `loadJSONFile` so this can be bundled with `extension-browser`.
const schema = require('./schema.json');
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
export default class ManifestParser extends Parser<ManifestEvents> {
// Event names.
private readonly fetchEndEventName = 'fetch::end::manifest';
private readonly fetchErrorEventName = 'fetch::error::manifest';
private readonly fetchStartEventName = 'fetch::start::manifest';
private readonly parseEndEventName = 'parse::end::manifest';
private readonly parseErrorSchemaEventName = 'parse::error::manifest::schema';
private readonly parseJSONErrorEventName = 'parse::error::manifest::json';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private fetchedManifests = new Set<string>();
public constructor(engine: Engine<ManifestEvents>) {
super(engine, 'manifest');
engine.on('element::link', this.fetchManifest.bind(this));
engine.on('fetch::end::manifest', this.validateManifest.bind(this));
engine.on('scan::end', this.onScanEnd.bind(this));
}
private onScanEnd() {
// Clear cached manifests so multiple runs work (e.g. in local connector).
this.fetchedManifests.clear();
}
private async fetchManifest(elementFound: ElementFound) {
const { element, resource } = elementFound;
/*
* Check if the target is a local file, and if it is,
* don't try to fetch the web app manifest file as it's
* `href` value will most probably not map to anything
* on disk and the request will fail.
*
* TODO: Remove this once things work as expected.
*/
if (!isHTTP(resource) && !isHTTPS(resource)) {
return;
}
// Check if the `link` tag is for the web app manifest.
if (normalizeString(element.getAttribute('rel')) !== 'manifest') {
return;
}
// If so, check if a non-empty `href` was specified.
const hrefValue: string | null = normalizeString(element.getAttribute('href'));
if (!hrefValue) {
return;
}
// Try to fetch the web app manifest.
const manifestURL: string = element.resolveUrl(hrefValue);
/*
* Don't fetch the manifest if it has already been fetched (as some
* connectors fetch automatically).
*
* Note: This expects manifest fetches by a connector to occur first.
* In theory a redundant fetch can still occur if the connector fetch
* occurs second. This does not happen in practice as this check runs
* after the page has been loaded (as part of the DOM traversal phase).
*/
if (this.fetchedManifests.has(manifestURL)) {
return;
}
await this.engine.emitAsync(this.fetchStartEventName, { resource });
let manifestNetworkData: NetworkData;
try {
manifestNetworkData = await this.engine.fetchContent(manifestURL);
} catch (error) {
await this.engine.emitAsync(this.fetchErrorEventName, {
element,
error,
hops: [manifestURL],
resource: manifestURL
});
return;
}
// If the web app manifest was fetched successfully and hasn't already been seen.
if (this.fetchedManifests.has(manifestURL)) {
return;
}
await this.engine.emitAsync(this.fetchEndEventName, {
element,
request: manifestNetworkData.request,
resource: manifestURL,
response: manifestNetworkData.response
});
}
private async validateManifest(fetchEnd: FetchEnd) {
const { resource, response } = fetchEnd;
if (this.fetchedManifests.has(resource)) {
return;
}
this.fetchedManifests.add(resource);
if (response.statusCode >= 400) {
return;
}
await this.engine.emitAsync(`parse::start::manifest`, { resource });
let result: IJSONResult;
/*
* Try to see if the content of the web app manifest file
* is a valid JSON.
*/
try {
result = parseJSON(response.body.content);
} catch (e) {
await this.engine.emitAsync(this.parseJSONErrorEventName, {
error: e,
resource
});
return;
}
/*
* Try to see if the content of the web app manifest file
* is a valid acording to the schema.
*/
const validationResult: SchemaValidationResult = validate(schema, result.data, result.getLocation);
if (!validationResult.valid) {
await this.engine.emitAsync(this.parseErrorSchemaEventName, {
error: new Error('Invalid manifest'),
errors: validationResult.errors,
groupedErrors: validationResult.groupedErrors,
prettifiedErrors: validationResult.prettifiedErrors,
resource
});
return;
}
/*
* If it is, return the parsed content among with
* other useful information about the manifest.
*/
await this.engine.emitAsync(this.parseEndEventName, {
getLocation: result.getLocation,
parsedContent: validationResult.data,
resource
});
}
}