use regex, not Uri.parse, to detect custom scheme

This commit is contained in:
Jan Kretschmer 2021-11-24 22:07:31 +01:00
parent 8779aaf2ae
commit bb89815cfb
2 changed files with 18 additions and 17 deletions

View file

@ -4,7 +4,8 @@
*--------------------------------------------------------------------------------------------*/
import { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode';
import { resolvePath, joinPath } from './requests';
import { resolvePath, joinPath, uriScheme } from './requests';
export function getCustomDataSource(toDispose: Disposable[]) {
let pathsInWorkspace = getCustomDataPathsInAllWorkspaces();
@ -57,8 +58,7 @@ function getCustomDataPathsInAllWorkspaces(): Set<string> {
if (Array.isArray(paths)) {
for (const path of paths) {
if (typeof path === 'string') {
const uri = Uri.parse(path);
if (uri.scheme === 'file') {
if (!uriScheme.test(path)) {
// only resolve file paths relative to extension
dataPaths.add(resolvePath(rootFolder, path).toString());
} else {
@ -94,12 +94,11 @@ function getCustomDataPathsFromAllExtensions(): Set<string> {
const customData = extension.packageJSON?.contributes?.html?.customData;
if (Array.isArray(customData)) {
for (const rp of customData) {
const uri = Uri.parse(rp);
if (uri.scheme === 'file') {
// only resolve file paths relative to extension
if (!uriScheme.test(rp)) {
// no schame -> resolve relative to extension
dataPaths.add(joinPath(extension.extensionUri, rp).toString());
} else {
// others schemes
// actual schemes
dataPaths.add(rp);
}

View file

@ -7,6 +7,8 @@ import { Uri, workspace } from 'vscode';
import { RequestType, CommonLanguageClient } from 'vscode-languageclient';
import { Runtime } from './htmlClient';
export const uriScheme = /^(?<scheme>\w[\w\d+.-]*):/;
export namespace FsContentRequest {
export const type: RequestType<{ uri: string; encoding?: string; }, string, any> = new RequestType('fs/content');
}
@ -20,34 +22,34 @@ export namespace FsReadDirRequest {
export function serveFileSystemRequests(client: CommonLanguageClient, runtime: Runtime, subscriptions: { dispose(): any }[]) {
subscriptions.push(client.onRequest(FsContentRequest.type, (param: { uri: string; encoding?: string; }) => {
const uri = Uri.parse(param.uri);
if (uri.scheme === 'file') {
const uri = param.uri.match(uriScheme);
if (uri?.groups?.scheme === 'file') {
if (runtime.fs) {
return runtime.fs.getContent(param.uri);
} else {
return workspace.fs.readFile(uri).then(buffer => {
return workspace.fs.readFile(Uri.parse(param.uri)).then(buffer => {
return new runtime.TextDecoder(param.encoding).decode(buffer);
});
}
} else {
return workspace.openTextDocument(uri).then(doc => {
return workspace.openTextDocument(Uri.parse(param.uri)).then(doc => {
return doc.getText();
});
}
}));
subscriptions.push(client.onRequest(FsReadDirRequest.type, (uriString: string) => {
const uri = Uri.parse(uriString);
if (uri.scheme === 'file' && runtime.fs) {
const uri = uriString.match(uriScheme);
if (uri?.groups?.scheme === 'file' && runtime.fs) {
return runtime.fs.readDirectory(uriString);
}
return workspace.fs.readDirectory(uri);
return workspace.fs.readDirectory(Uri.parse(uriString));
}));
subscriptions.push(client.onRequest(FsStatRequest.type, (uriString: string) => {
const uri = Uri.parse(uriString);
if (uri.scheme === 'file' && runtime.fs) {
const uri = uriString.match(uriScheme);
if (uri?.groups?.scheme === 'file' && runtime.fs) {
return runtime.fs.stat(uriString);
}
return workspace.fs.stat(uri);
return workspace.fs.stat(Uri.parse(uriString));
}));
}