@volar/monaco 2.4.0-alpha.2 → 2.4.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/ata.js DELETED
@@ -1,228 +0,0 @@
1
- export function activateAutomaticTypeAcquisition(env, uriConverter, onFetch) {
2
- const textCache = new Map();
3
- const jsonCache = new Map();
4
- const npmFs = createJsDelivrNpmFileSystem();
5
- const _fs = env.fs;
6
- env.fs = {
7
- async stat(uri) {
8
- return await npmFs.stat(uri) ?? await _fs?.stat(uri);
9
- },
10
- async readDirectory(uri) {
11
- return [
12
- ...await npmFs.readDirectory(uri),
13
- ...await _fs?.readDirectory(uri) ?? [],
14
- ];
15
- },
16
- async readFile(uri) {
17
- return await npmFs.readFile(uri) ?? await _fs?.readFile(uri);
18
- },
19
- };
20
- function createJsDelivrNpmFileSystem() {
21
- const fetchResults = new Map();
22
- const flatResults = new Map();
23
- return {
24
- async stat(uri) {
25
- const fileName = uriConverter.asFileName(uri);
26
- if (fileName === '/node_modules') {
27
- return {
28
- type: 2,
29
- size: -1,
30
- ctime: -1,
31
- mtime: -1,
32
- };
33
- }
34
- if (fileName.startsWith('/node_modules/')) {
35
- const path = fileName.substring('/node_modules/'.length);
36
- return await _stat(path);
37
- }
38
- },
39
- async readFile(uri) {
40
- const fileName = uriConverter.asFileName(uri);
41
- if (fileName.startsWith('/node_modules/')) {
42
- const path = fileName.substring('/node_modules/'.length);
43
- return await _readFile(path);
44
- }
45
- },
46
- readDirectory(uri) {
47
- const fileName = uriConverter.asFileName(uri);
48
- if (fileName.startsWith('/node_modules/')) {
49
- const path = fileName.substring('/node_modules/'.length);
50
- return _readDirectory(path);
51
- }
52
- return [];
53
- },
54
- };
55
- async function _stat(path) {
56
- const pkgName = getPackageName(path);
57
- if (!pkgName || !await isValidPackageName(pkgName)) {
58
- return;
59
- }
60
- if (!flatResults.has(pkgName)) {
61
- flatResults.set(pkgName, flat(pkgName));
62
- }
63
- const flatResult = await flatResults.get(pkgName);
64
- const filePath = path.slice(pkgName.length);
65
- const file = flatResult.find(file => file.name === filePath);
66
- if (file) {
67
- return {
68
- type: 1,
69
- ctime: new Date(file.time).valueOf(),
70
- mtime: new Date(file.time).valueOf(),
71
- size: file.size,
72
- };
73
- }
74
- else if (flatResult.some(file => file.name.startsWith(filePath + '/'))) {
75
- return {
76
- type: 2,
77
- ctime: -1,
78
- mtime: -1,
79
- size: -1,
80
- };
81
- }
82
- }
83
- async function _readDirectory(path) {
84
- const pkgName = getPackageName(path);
85
- if (!pkgName || !await isValidPackageName(pkgName)) {
86
- return [];
87
- }
88
- if (!flatResults.has(pkgName)) {
89
- flatResults.set(pkgName, flat(pkgName));
90
- }
91
- const flatResult = await flatResults.get(pkgName);
92
- const dirPath = path.slice(pkgName.length);
93
- const files = flatResult
94
- .filter(f => f.name.substring(0, f.name.lastIndexOf('/')) === dirPath)
95
- .map(f => f.name.slice(dirPath.length + 1));
96
- const dirs = flatResult
97
- .filter(f => f.name.startsWith(dirPath + '/') && f.name.substring(dirPath.length + 1).split('/').length >= 2)
98
- .map(f => f.name.slice(dirPath.length + 1).split('/')[0]);
99
- return [
100
- ...files.map(f => [f, 1]),
101
- ...[...new Set(dirs)].map(f => [f, 2]),
102
- ];
103
- }
104
- async function _readFile(path) {
105
- const pkgName = getPackageName(path);
106
- if (!pkgName || !await isValidPackageName(pkgName)) {
107
- return;
108
- }
109
- if (!fetchResults.has(path)) {
110
- fetchResults.set(path, (async () => {
111
- if ((await _stat(path))?.type !== 1) {
112
- return;
113
- }
114
- const text = await fetchText(`https://cdn.jsdelivr.net/npm/${path}`);
115
- if (text !== undefined) {
116
- onFetch?.(path, text);
117
- }
118
- return text;
119
- })());
120
- }
121
- return await fetchResults.get(path);
122
- }
123
- async function flat(pkgNameWithVersion) {
124
- let pkgName = pkgNameWithVersion;
125
- let version = 'latest';
126
- if (pkgNameWithVersion.substring(1).includes('@')) {
127
- pkgName = pkgNameWithVersion.substring(0, pkgNameWithVersion.lastIndexOf('@'));
128
- version = pkgNameWithVersion.substring(pkgNameWithVersion.lastIndexOf('@') + 1);
129
- }
130
- // resolve tag version
131
- if (version === 'latest') {
132
- const data = await fetchJson(`https://data.jsdelivr.com/v1/package/resolve/npm/${pkgName}@latest`);
133
- if (!data?.version) {
134
- return [];
135
- }
136
- version = data.version;
137
- }
138
- const flat = await fetchJson(`https://data.jsdelivr.com/v1/package/npm/${pkgName}@${version}/flat`);
139
- if (!flat) {
140
- return [];
141
- }
142
- return flat.files;
143
- }
144
- async function isValidPackageName(pkgName) {
145
- // @aaa/bbb@latest -> @aaa/bbb
146
- if (pkgName.substring(1).includes('@')) {
147
- pkgName = pkgName.substring(0, pkgName.lastIndexOf('@'));
148
- }
149
- // ignore @aaa/node_modules
150
- if (pkgName.endsWith('/node_modules')) {
151
- return false;
152
- }
153
- // hard code for known invalid package
154
- if (pkgName.startsWith('@typescript/') || pkgName.startsWith('@types/typescript__')) {
155
- return false;
156
- }
157
- // don't check @types if original package already having types
158
- if (pkgName.startsWith('@types/')) {
159
- let originalPkgName = pkgName.slice('@types/'.length);
160
- if (originalPkgName.indexOf('__') >= 0) {
161
- originalPkgName = '@' + originalPkgName.replace('__', '/');
162
- }
163
- const packageJson = await _readFile(`${originalPkgName}/package.json`);
164
- if (packageJson) {
165
- const packageJsonObj = JSON.parse(packageJson);
166
- if (packageJsonObj.types || packageJsonObj.typings) {
167
- return false;
168
- }
169
- const indexDts = await _stat(`${originalPkgName}/index.d.ts`);
170
- if (indexDts?.type === 1) {
171
- return false;
172
- }
173
- }
174
- }
175
- return true;
176
- }
177
- }
178
- /**
179
- * @example
180
- * "a/b/c" -> "a"
181
- * "@a/b/c" -> "@a/b"
182
- * "@a/b@1.2.3/c" -> "@a/b@1.2.3"
183
- */
184
- function getPackageName(path) {
185
- const parts = path.split('/');
186
- let pkgName = parts[0];
187
- if (pkgName.startsWith('@')) {
188
- if (!parts[1]) {
189
- return undefined;
190
- }
191
- pkgName += '/' + parts[1];
192
- }
193
- return pkgName;
194
- }
195
- async function fetchText(url) {
196
- if (!textCache.has(url)) {
197
- textCache.set(url, (async () => {
198
- try {
199
- const res = await fetch(url);
200
- if (res.status === 200) {
201
- return await res.text();
202
- }
203
- }
204
- catch {
205
- // ignore
206
- }
207
- })());
208
- }
209
- return await textCache.get(url);
210
- }
211
- async function fetchJson(url) {
212
- if (!jsonCache.has(url)) {
213
- jsonCache.set(url, (async () => {
214
- try {
215
- const res = await fetch(url);
216
- if (res.status === 200) {
217
- return await res.json();
218
- }
219
- }
220
- catch {
221
- // ignore
222
- }
223
- })());
224
- }
225
- return await jsonCache.get(url);
226
- }
227
- }
228
- //# sourceMappingURL=ata.js.map