-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathmodule_resolver.ts
223 lines (187 loc) · 6.01 KB
/
module_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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { URL } from "url";
import * as path from "path";
import assert from "assert";
import { getDenoDepsDir } from "./deno";
import { CacheModule, DenoCacheModule } from "./deno_cache";
import { ImportMap } from "./import_map";
import { HashMeta } from "./hash_meta";
import { pathExistsSync, isHttpURL, hashURL, normalizeFilepath } from "./util";
import { Logger } from "./logger";
import {
Extension,
getExtensionFromFile,
isValidDenoModuleExtension,
} from "./extension";
export type ResolvedModule = {
origin: string;
filepath: string;
extension: Extension;
};
export interface ModuleResolverInterface {
resolveModules(moduleNames: string[]): (ResolvedModule | void)[];
}
export class ModuleResolver implements ModuleResolverInterface {
private importMaps = ImportMap.create(this.importMapsFile);
private denoCacheFile?: DenoCacheModule;
/**
* Module resolver constructor
* @param containingFile Absolute file path
* @param importMapsFile Absolute file path
*/
private constructor(
private containingFile: string,
private importMapsFile?: string,
private logger?: Logger
) {
containingFile = normalizeFilepath(containingFile);
if (importMapsFile) {
this.importMapsFile = normalizeFilepath(importMapsFile);
}
assert(
path.isAbsolute(containingFile),
`ModuleResolver filepath require absolute but got ${containingFile}`
);
this.denoCacheFile = CacheModule.create(
this.containingFile
) as DenoCacheModule;
}
/**
* Create a module resolver.
* @param containingFile Absolute file path
* @param importMapsFile Absolute file path
*/
static create(
containingFile: string,
importMapsFile?: string,
logger?: Logger
): ModuleResolver {
return new ModuleResolver(containingFile, importMapsFile, logger);
}
private resolveFromRemote(
httpModuleURL: string,
origin: string
): ResolvedModule | undefined {
const url = new URL(httpModuleURL);
const originDir = path.join(
getDenoDepsDir(),
url.protocol.replace(/:$/, ""), // https: -> https
`${url.hostname}${url.port ? `_PORT${url.port}` : ""}` // hostname.xyz:3000 -> hostname.xyz_PORT3000
);
const hash = hashURL(url);
const metaFilepath = path.join(originDir, `${hash}.metadata.json`);
const meta = HashMeta.create(metaFilepath);
if (!meta) {
return;
}
let redirect = meta.headers["location"];
if (redirect) {
redirect = isHttpURL(redirect) // eg: https://redirect.com/path/to/redirect
? redirect
: path.posix.isAbsolute(redirect) // eg: /path/to/redirect
? `${url.protocol}//${url.host}${redirect}`
: // eg: ./path/to/redirect
`${url.protocol}//${url.host}${path.posix.resolve(
url.pathname,
redirect
)}`;
if (!isHttpURL(redirect) || redirect === httpModuleURL) {
return;
}
return this.resolveFromRemote(redirect, origin);
}
const moduleFilepath = path.join(originDir, hash);
const typescriptTypes = meta.headers["x-typescript-types"];
if (typescriptTypes) {
const resolver = ModuleResolver.create(
moduleFilepath,
this.importMapsFile
);
const [typeModule] = resolver.resolveModules([typescriptTypes]);
/* istanbul ignore else */
if (typeModule) {
typeModule.origin = httpModuleURL;
return typeModule;
}
}
if (!meta.extension) {
return;
}
return {
origin: origin,
filepath: moduleFilepath,
extension: meta.extension,
};
}
private resolveFromLocal(moduleName: string): ResolvedModule | undefined {
const originModuleName = moduleName;
moduleName = this.importMaps.resolveModule(moduleName);
if (isHttpURL(moduleName)) {
return this.resolveFromRemote(moduleName, originModuleName);
}
if (moduleName.startsWith("file://")) {
// file protocol is always a unix style path
// eg: file:///Users/deno/project/mod.ts in MacOS
// eg: file:///Home/deno/project/mod.ts in Linux
// eg: file://d:/project/mod.ts in Window
moduleName = moduleName.replace(/^file:\/\//, "");
}
const moduleFilepath = path.resolve(
path.dirname(this.containingFile),
normalizeFilepath(moduleName)
);
if (
!pathExistsSync(moduleFilepath) ||
!isValidDenoModuleExtension(moduleFilepath)
) {
return;
}
return {
origin: originModuleName,
filepath: moduleFilepath,
extension: getExtensionFromFile(moduleFilepath),
};
}
/**
* Find cached modules in the file.
* If cannot found module, returns undefined
* @param moduleNames Module name is always unix style.
* eg `./foo.ts`
* `/std/path/mod.ts`
* `https://deno.land/std/path/mod.ts`
*/
resolveModules(moduleNames: string[]): (ResolvedModule | undefined)[] {
const resolvedModules: (ResolvedModule | undefined)[] = [];
for (const moduleName of moduleNames) {
/* istanbul ignore next */
this.logger?.info(
`resolve module ${moduleName} from ${this.containingFile}`
);
// If the file is in Deno's cache layout
// Then we should look up from the cache
if (this.denoCacheFile) {
const moduleCacheFile = this.denoCacheFile.resolveModule(moduleName);
if (moduleCacheFile) {
resolvedModules.push({
origin: moduleName,
filepath: moduleCacheFile.filepath,
extension: moduleCacheFile.extension,
});
} else {
resolvedModules.push(undefined);
}
continue;
}
// If import from remote
if (isHttpURL(moduleName)) {
resolvedModules.push(this.resolveFromRemote(moduleName, moduleName));
continue;
}
// The rest are importing local modules
// eg.
// `./foo.ts`
// `../foo/bar.ts`
resolvedModules.push(this.resolveFromLocal(moduleName));
}
return resolvedModules;
}
}