-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmeta.ts
144 lines (138 loc) · 4.16 KB
/
meta.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
import { escape } from './utils.js';
export interface Registry {
[uri: string]: any;
}
export interface Options {
scope: string;
registry?: Registry;
refs?: Set<string>;
}
export interface Meta {
registry: Registry;
refs: Set<string>;
scope: string;
root: any;
parent?: any;
derefd?: boolean;
}
const __meta = Symbol();
export const LII_RE: RegExp = /^[a-zA-Z][a-zA-Z0-9\.\-_:]*$/; // Location-independent identifier, JSON Schema draft 7, par. 8.2.3
export function normalizeUri(input: string, scope?: string): string {
const uri = new URL(input, scope);
const out = uri.toString();
return out + (!uri.hash && out[out.length - 1] !== '#' ? '#' : '');
}
export function isRef(obj: any): boolean {
return obj !== null && typeof obj === 'object' && typeof obj.$ref === 'string';
}
export function isAnnotated(obj: any): boolean {
return obj !== null && typeof obj === 'object' && typeof obj[__meta] === 'object';
}
export function isDerefd(obj: any): boolean {
return isAnnotated(obj) && obj[__meta].derefd === true;
}
export function getMeta(obj: any): Meta {
if (!isAnnotated(obj)) {
throw new Error('Not annotated');
}
return obj[__meta];
}
export function getKey(obj: any): string | number | undefined {
const parent = getMeta(obj).parent;
if (typeof parent === 'undefined') {
return undefined;
} else if (Array.isArray(parent)) {
for (let i = 0; i < parent.length; i++) {
if (parent[i] === obj) {
return i;
}
}
return undefined;
} else {
return Object.keys(parent).find((k) => parent[k] === obj);
}
}
export function getById(obj: any, id: string): any {
if (obj === null || typeof obj !== 'object') {
throw new TypeError('Invalid object');
}
const meta = getMeta(obj);
return meta.registry[normalizeUri(id, meta.scope)];
}
export function annotate(obj: any, options: Options): any {
if (obj === null || typeof obj !== 'object') {
throw new TypeError('Invalid object');
} else if (isAnnotated(obj)) {
throw new Error('Already annotated');
}
obj[__meta] = {
registry: options.registry || {},
refs: options.refs || new Set(),
root: obj,
} as Registry;
obj[__meta].registry[normalizeUri(options.scope)] = obj;
return (function _annotate(obj: any, scope: string): any {
if (isRef(obj)) {
const uri = new URL(obj.$ref, scope);
uri.hash = '';
getMeta(obj).refs.add(uri.toString() + '#');
obj[__meta].scope = normalizeUri(scope);
} else {
if (typeof obj.$id === 'string') {
if (!obj.$id || obj.$id === '#') {
throw new SyntaxError(`Invalid identifier ${obj.$id}`);
}
const id = new URL(obj.$id, scope);
if (id.hash && !id.hash.substr(1).match(LII_RE)) {
throw new SyntaxError(`Invalid identifier ${obj.$id}`);
}
obj[__meta].scope = normalizeUri(obj.$id, scope);
obj[__meta].registry[obj[__meta].scope] = obj;
obj[__meta].root = obj;
} else {
obj[__meta].scope = normalizeUri(scope);
}
const keys = Object.keys(obj);
for (let key of keys) {
const next = obj[key];
if (next !== null && typeof next === 'object' && !isAnnotated(next)) {
const meta = getMeta(obj);
next[__meta] = {
registry: meta.registry,
refs: meta.refs,
parent: obj,
root: meta.root,
};
_annotate(next, `${meta.scope}/${escape(key)}`);
}
}
}
return obj;
})(obj, options.scope);
}
export function missingRefs(obj: any): string[] {
const meta = getMeta(obj);
const known = new Set(Object.keys(meta.registry));
return [...meta.refs].filter((r) => !known.has(r));
}
export function normalize(obj: any): any {
if (!isAnnotated(obj)) {
throw new Error('Not annotated');
}
const meta = getMeta(obj);
if (typeof obj.$id === 'string') {
obj.$id = normalizeUri(obj.$id, meta.scope);
}
const keys = Object.keys(obj);
for (let key of keys) {
const o = obj[key];
if (o !== null && typeof o === 'object') {
if (isRef(o)) {
o.$ref = normalizeUri(o.$ref, meta.scope);
} else {
normalize(o);
}
}
}
return obj;
}