-
Notifications
You must be signed in to change notification settings - Fork 9
/
resolver.ts
152 lines (126 loc) · 4.19 KB
/
resolver.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
import { CancellationTokenSource, PackageMainField, Uri } from '@velcro/common';
import { ResolverContext } from './context';
import type { ResolverStrategy } from './strategy';
export class Resolver {
private disposed = false;
readonly rootCtx: ResolverContext;
private readonly settings: Resolver.Settings;
private readonly strategy: ResolverStrategy;
private readonly tokenSource = new CancellationTokenSource();
constructor(strategy: ResolverStrategy, settings: Resolver.Settings) {
this.settings = settings;
this.strategy = strategy;
this.rootCtx = ResolverContext.create(
this,
this.strategy,
this.settings,
this.tokenSource.token,
{ debug: settings.debug }
);
}
decode(buf: BufferSource | string): string {
if (typeof buf === 'string') {
return buf;
}
return this.rootCtx.decoder.decode(buf);
}
dispose() {
this.disposed = true;
return this.rootCtx.dispose();
}
getCanonicalUrl(uri: string | Uri) {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
return this.rootCtx.runInIsolatedContext('Resolver.getCanonicalUrl', uri, (ctx) =>
ctx.getCanonicalUrl(typeof uri === 'string' ? Uri.parse(uri) : uri)
);
}
getResolveRoot(uri: string | Uri) {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
return this.rootCtx.runInIsolatedContext('Resolver.getResolveRoot', uri, (ctx) =>
ctx.getResolveRoot(typeof uri === 'string' ? Uri.parse(uri) : uri)
);
}
getSettings(uri: string | Uri) {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
return this.rootCtx.runInIsolatedContext('Resolver.getSettings', uri, (ctx) =>
ctx.getSettings(typeof uri === 'string' ? Uri.parse(uri) : uri)
);
}
getUrlForBareModule(name: string, spec: string, path: string) {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
return this.rootCtx.runInIsolatedContext(
'Resolver.getUrlForBareModule',
`${name}|${spec}|${path}`,
(ctx) => ctx.getUrlForBareModule(name, spec, path)
);
}
invalidate(uri: string | Uri) {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
return this.rootCtx.runInIsolatedContext('Resolver.invalidate', uri, (ctx) =>
ctx.invalidate(typeof uri === 'string' ? Uri.parse(uri) : uri)
);
}
listEntries(uri: Uri) {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
return this.rootCtx.runInIsolatedContext('Resolver.listEntries', uri, (ctx) =>
ctx.listEntries(typeof uri === 'string' ? Uri.parse(uri) : uri)
);
}
readFileContent(uri: Uri) {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
return this.rootCtx.runInIsolatedContext('Resolver.readFileContent', uri, (ctx) =>
ctx.readFileContent(typeof uri === 'string' ? Uri.parse(uri) : uri)
);
}
readParentPackageJson(uri: Uri) {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
return this.rootCtx.runInIsolatedContext('Resolver.readParentPackageJson', uri, (ctx) =>
ctx.readParentPackageJson(typeof uri === 'string' ? Uri.parse(uri) : uri)
);
}
resolve(spec: Uri): ReturnType<ResolverContext['resolve']>;
resolve(spec: string, fromUri: Uri): ReturnType<ResolverContext['resolve']>;
resolve(spec: string | Uri, fromUri?: Uri): ReturnType<ResolverContext['resolve']> {
if (this.disposed) {
throw new Error('Resolver has been disposed');
}
if (Uri.isUri(spec)) {
return this.rootCtx.runInIsolatedContext('Resolver.resolveUri', spec, (ctx) =>
ctx.resolveUri(spec)
);
}
if (!fromUri) {
throw new Error(
'When calling Resolver.resolve with a string spec, a second "fromUri" argument is required'
);
}
return this.rootCtx.runInIsolatedContext(
'Resolver.resolve',
`${fromUri ? fromUri.toString() : ''}|${spec}`,
(ctx) => ctx.resolve(spec, fromUri)
);
}
}
export namespace Resolver {
export interface Settings {
debug?: boolean;
extensions: string[];
packageMain: PackageMainField[];
}
}