Skip to content

Commit

Permalink
File refreshing & short "stat" cache
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Jul 27, 2024
1 parent 8451932 commit cb98d8d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ out/src/*.js.map

.gitignore
tsconfig.json
eslint.config.js
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = [
languageOptions: {
globals: {
fetch: "readonly",
setTimeout: "readonly",
Response: "readonly"
},
parser: require("@typescript-eslint/parser")
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pterodactyl-vsc",
"displayName": "Pterodactyl file system",
"description": "Open a Pterodactyl server folder as a remote workspace.",
"version": "1.0.3",
"version": "1.1.0",
"publisher": "tomatocake",
"license": "MIT",
"main": "./out/src/extension",
Expand All @@ -15,7 +15,7 @@
"url": "https://github.com/DEVTomatoCake/Pterodactyl-vsc.git"
},
"engines": {
"vscode": "^1.88.0"
"vscode": "^1.91.0"
},
"categories": [
"Other"
Expand All @@ -35,12 +35,20 @@
"commands": [
{
"command": "pterodactyl-vsc.init",
"title": "Connect to server",
"title": "Setup/Connect to server",
"shortTitle": "Setup",
"category": "Pterodactyl"
},
{
"command": "pterodactyl-vsc.reset",
"title": "Reset workspace config",
"shortTitle": "Reset",
"category": "Pterodactyl"
},
{
"command": "pterodactyl-vsc.refresh",
"title": "Refresh server files",
"shortTitle": "Refresh",
"category": "Pterodactyl"
}
],
Expand All @@ -53,6 +61,10 @@
{
"command": "pterodactyl-vsc.reset",
"when": "workbenchState == workspace"
},
{
"command": "pterodactyl-vsc.refresh",
"when": "workbenchState == workspace"
}
]
},
Expand Down Expand Up @@ -106,7 +118,9 @@
"watch": "tsc -watch -p ./",
"pack": "vsce pack -o ./out/pterodactyl-vsc.vsix -t web",
"pack-files": "vsce ls",
"--comment-publish": "https://dev.azure.com/tomatocake/_usersSettings/tokens",
"publish": "vsce publish",
"--comment-publish-openvsx": "https://open-vsx.org/user-settings/tokens",
"publish-openvsx": "npx ovsx publish ./out/pterodactyl-vsc.vsix",
"sideload": "npx serve --cors -l 5000 --ssl-cert $HOME/certs/localhost.pem --ssl-key $HOME/certs/localhost-key.pem",
"sideload-win": "npx serve --cors -l 5000 --ssl-cert %userprofile%\\certs\\localhost.pem --ssl-key %userprofile%\\certs\\localhost-key.pem"
Expand Down
39 changes: 35 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ const addPanel = async () => {
})
}

const responseCache = new Map()
const responseCacheHits = new Map()

export class PterodactylFileSystemProvider implements vscode.FileSystemProvider {
private readonly _eventEmitter: vscode.EventEmitter<vscode.FileChangeEvent[]>

Expand Down Expand Up @@ -278,6 +281,11 @@ export class PterodactylFileSystemProvider implements vscode.FileSystemProvider
public async stat(uri: vscode.Uri): Promise<vscode.FileStat> {
if (serverApiUrl == "") throw vscode.FileSystemError.Unavailable("No server API URL set, please init the extension first.")

if (responseCache.has("stat:" + uri.toString())) {
responseCacheHits.set("stat:" + uri.toString(), responseCacheHits.get("stat:" + uri.toString()) + 1)
return responseCache.get("stat:" + uri.toString())
}

if (uri.path == "/") return {
ctime: 0,
mtime: 0,
Expand All @@ -303,7 +311,7 @@ export class PterodactylFileSystemProvider implements vscode.FileSystemProvider
const file = json.data.find((f: any) => f.attributes.name == uri.path.split("/").pop())?.attributes
if (!file) throw vscode.FileSystemError.FileNotFound(uri)

return {
const response = {
ctime: new Date(file.created_at).getTime(),
mtime: new Date(file.modified_at).getTime(),
permissions: file.mode[2] == "w" ? void 0 : vscode.FilePermission.Readonly,
Expand All @@ -312,6 +320,17 @@ export class PterodactylFileSystemProvider implements vscode.FileSystemProvider
? (file.is_symlink ? vscode.FileType.File | vscode.FileType.SymbolicLink : vscode.FileType.File)
: (file.is_symlink ? vscode.FileType.Directory | vscode.FileType.SymbolicLink : vscode.FileType.Directory)
}

responseCache.set("stat:" + uri.toString(), response)
responseCacheHits.set("stat:" + uri.toString(), 0)
setTimeout(() => {
log(responseCacheHits.get("stat:" + uri.toString()) + " cache hits for stat requests on " + uri.toString())

responseCache.delete("stat:" + uri.toString())
responseCacheHits.delete("stat:" + uri.toString())
}, 1000 * 10)

return response
}

public async writeFile(uri: vscode.Uri, content: Uint8Array, options: { create: boolean, overwrite: boolean }): Promise<void> {
Expand Down Expand Up @@ -359,10 +378,13 @@ export const activate = (context: vscode.ExtensionContext) => {
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(event => {
log("Detected configuration change")
if (event.affectsConfiguration("pterodactyl-vsc.panelUrl") || event.affectsConfiguration("pterodactyl-vsc.serverId")) {
const serverId = vscode.workspace.getConfiguration("pterodactyl-vsc").get("serverId")
if (!serverId) return log("-> No server ID set, not updating server API URL")

const parsedUrl = vscode.Uri.parse(vscode.workspace.getConfiguration("pterodactyl-vsc").get("panelUrl") || "")
log("Setting server api URL to " + parsedUrl.scheme + "://" + parsedUrl.authority + "/api/client/servers/" +
vscode.workspace.getConfiguration("pterodactyl-vsc").get("serverId") + "/files")
serverApiUrl = parsedUrl.scheme + "://" + parsedUrl.authority + "/api/client/servers/" + vscode.workspace.getConfiguration("pterodactyl-vsc").get("serverId") + "/files"

log("Setting server api URL to " + parsedUrl.scheme + "://" + parsedUrl.authority + "/api/client/servers/" + serverId + "/files")
serverApiUrl = parsedUrl.scheme + "://" + parsedUrl.authority + "/api/client/servers/" + serverId + "/files"
}

if (event.affectsConfiguration("pterodactyl-vsc.apiKey")) authHeader = "Bearer " + vscode.workspace.getConfiguration("pterodactyl-vsc").get("apiKey")
Expand All @@ -380,4 +402,13 @@ export const activate = (context: vscode.ExtensionContext) => {

log("Reset workspace")
}))

context.subscriptions.push(vscode.commands.registerCommand("pterodactyl-vsc.refresh", () => {
log("Refreshing workspace server files...")

vscode.workspace.updateWorkspaceFolders(0, vscode.workspace.workspaceFolders?.length || 0, {
uri: vscode.Uri.parse("pterodactyl:/"),
name: "Pterodactyl"
})
}))
}

0 comments on commit cb98d8d

Please sign in to comment.