-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor dtsHost to
@volar/cdn
(#50)
- Loading branch information
1 parent
eb8f8d9
commit 082f6d5
Showing
18 changed files
with
634 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023-present Johnson Chu | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "@volar/cdn", | ||
"version": "1.7.10", | ||
"main": "out/index.js", | ||
"license": "MIT", | ||
"files": [ | ||
"out/**/*.js", | ||
"out/**/*.d.ts" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/volarjs/volar.js.git", | ||
"directory": "packages/cdn" | ||
}, | ||
"dependencies": { | ||
"@volar/language-service": "1.7.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import type { FileType, FileSystem, FileStat } from '@volar/language-service'; | ||
import { UriResolver } from '../types'; | ||
import { fetchJson, fetchText } from '../utils'; | ||
|
||
export function createGitHubUriResolver(fileNameBase: string, owner: string, repo: string, branch: string): UriResolver { | ||
|
||
const gitHubUriBase = getGitHubUriBase(owner, repo, branch); | ||
|
||
return { | ||
uriToFileName, | ||
fileNameToUri, | ||
}; | ||
|
||
function uriToFileName(uri: string) { | ||
if (uri === gitHubUriBase) { | ||
return fileNameBase; | ||
} | ||
if (uri.startsWith(gitHubUriBase + '/')) { | ||
const path = uri.substring(gitHubUriBase.length); | ||
return `${fileNameBase}${path}`; | ||
} | ||
} | ||
|
||
function fileNameToUri(fileName: string) { | ||
if (fileName === fileNameBase) { | ||
return gitHubUriBase; | ||
} | ||
if (fileName.startsWith(fileNameBase + '/')) { | ||
const path = fileName.substring(fileNameBase.length); | ||
return `${gitHubUriBase}${path}`; | ||
} | ||
} | ||
} | ||
|
||
export function createGitHubFs(owner: string, repo: string, branch: string, onReadFile?: (uri: string, content: string) => void): FileSystem { | ||
|
||
const gitHubUriBase = getGitHubUriBase(owner, repo, branch); | ||
|
||
return { | ||
stat, | ||
readDirectory, | ||
readFile, | ||
}; | ||
|
||
async function stat(uri: string): Promise<FileStat | undefined> { | ||
|
||
if (uri === gitHubUriBase) { | ||
return { | ||
type: 2 satisfies FileType.Directory, | ||
size: -1, | ||
ctime: -1, | ||
mtime: -1, | ||
}; | ||
} | ||
|
||
if (uri.startsWith(gitHubUriBase + '/')) { | ||
|
||
if (uri.endsWith('/')) { | ||
return { | ||
type: 2 satisfies FileType.Directory, | ||
size: -1, | ||
ctime: -1, | ||
mtime: -1, | ||
}; | ||
} | ||
|
||
const path = uri.substring(gitHubUriBase.length); | ||
const dirName = path.substring(0, path.lastIndexOf('/')); | ||
const baseName = path.substring(path.lastIndexOf('/') + 1); | ||
const dirData = await fetchContents(dirName); | ||
const file = dirData.find(entry => entry.name === baseName && entry.type === 'file'); | ||
const dir = dirData.find(entry => entry.name === baseName && entry.type === 'dir'); | ||
if (file) { | ||
return { | ||
type: 1 satisfies FileType.File, | ||
size: file.size, | ||
ctime: -1, | ||
mtime: -1, | ||
}; | ||
} | ||
if (dir) { | ||
return { | ||
type: 2 satisfies FileType.Directory, | ||
size: dir.size, | ||
ctime: -1, | ||
mtime: -1, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
async function readDirectory(uri: string): Promise<[string, FileType][]> { | ||
|
||
if (uri === gitHubUriBase || uri.startsWith(gitHubUriBase + '/')) { | ||
|
||
const path = uri.substring(gitHubUriBase.length); | ||
const dirData = await fetchContents(path); | ||
const result: [string, FileType][] = dirData.map(entry => [ | ||
entry.name, | ||
entry.type === 'file' ? 1 satisfies FileType.File | ||
: entry.type === 'dir' ? 2 satisfies FileType.Directory | ||
: 0 satisfies FileType.Unknown, | ||
]); | ||
return result; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
async function readFile(uri: string): Promise<string | undefined> { | ||
|
||
if (uri.startsWith(gitHubUriBase + '/')) { | ||
|
||
const text = await fetchText(uri); | ||
if (text !== undefined) { | ||
onReadFile?.(uri, text); | ||
} | ||
return text; | ||
} | ||
} | ||
|
||
async function fetchContents(dirName: string) { | ||
return await fetchJson<{ | ||
name: string; | ||
path: string; | ||
sha: string; | ||
size: number; | ||
url: string; | ||
html_url: string; | ||
git_url: string; | ||
download_url: null | string; | ||
type: 'file' | 'dir', | ||
}[]>(`https://api.github.com/repos/${owner}/${repo}/contents${dirName}?ref=${branch}`) ?? []; | ||
} | ||
} | ||
|
||
function getGitHubUriBase(owner: string, repo: string, branch: string) { | ||
return `https://raw.githubusercontent.com/${owner}/${repo}/${branch}`; | ||
} |
Oops, something went wrong.