-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathabnf.ts
More file actions
54 lines (49 loc) · 1.39 KB
/
abnf.ts
File metadata and controls
54 lines (49 loc) · 1.39 KB
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
import 'apg-js/dist/apg-api-bundle';
import { odataAbnfCache } from '../cache/odataAbnfRules.cache';
interface ValidationResult {
inputLength: number;
length: number;
matched: number;
maxMatched: number;
maxTreeDepth: number;
nodeHits: number;
state: number;
subBegin: number;
subEnd: number;
subLength: number;
success: boolean;
}
const { apgLib } = globalThis as any;
export class ValidatedUrl {
private static grammar: any;
private static parser = new apgLib.parser();
public static getGrammar() {
if (!ValidatedUrl.grammar) {
this.generateGrammarObject().then(grammar=>{ ValidatedUrl.grammar = grammar});
}
return ValidatedUrl.grammar;
}
private static async generateGrammarObject() {
const grammar = await odataAbnfCache.readGrammar();
return grammar;
}
public validate(graphUrl: string): ValidationResult {
let decodedGraphUrl = graphUrl;
try { decodedGraphUrl = decodeURI(graphUrl); } catch (error) { /* empty */ }
const grammar = ValidatedUrl.getGrammar()
let result = ValidatedUrl.parser.parse(
grammar,
'odataUri',
decodedGraphUrl
);
if (!result.success) {
const pathname = new URL(decodedGraphUrl).pathname.replace('/v1.0/','').replace('/beta/', '');
result = ValidatedUrl.parser.parse(
grammar,
'odataRelativeUri',
pathname
);
}
return result;
}
}