From 24ef473fb75e670a037f3ce2e3c781053b6a3a46 Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Tue, 6 Aug 2024 13:41:51 -0400 Subject: [PATCH] Proper url hashing --- package-lock.json | 32 +++++++++++++++++++ package.json | 1 + src/LanguageServerManager.spec.ts | 19 ++++++++++- src/LanguageServerManager.ts | 4 ++- .../LanguageServerInfoCommand.spec.ts | 3 +- 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8e0a9881..c57adbea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,6 +28,7 @@ "iconv-lite": "0.4.24", "jszip": "^3.10.1", "just-throttle": "^4.0.1", + "md5": "^2.3.0", "net": "^1.0.2", "node-cache": "^4.2.0", "node-ssdp": "^4.0.0", @@ -3018,6 +3019,14 @@ "changelog-parser": "bin/cli.js" } }, + "node_modules/charenc": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz", + "integrity": "sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==", + "engines": { + "node": "*" + } + }, "node_modules/check-error": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", @@ -3471,6 +3480,14 @@ "node": ">= 8" } }, + "node_modules/crypt": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz", + "integrity": "sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==", + "engines": { + "node": "*" + } + }, "node_modules/css-select": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/css-select/-/css-select-5.1.0.tgz", @@ -6090,6 +6107,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-buffer": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", + "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==" + }, "node_modules/is-callable": { "version": "1.2.7", "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.7.tgz", @@ -7006,6 +7028,16 @@ "node": ">= 12" } }, + "node_modules/md5": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/md5/-/md5-2.3.0.tgz", + "integrity": "sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==", + "dependencies": { + "charenc": "0.0.2", + "crypt": "0.0.2", + "is-buffer": "~1.1.6" + } + }, "node_modules/mdurl": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz", diff --git a/package.json b/package.json index ca14ff5d..6c10184d 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "iconv-lite": "0.4.24", "jszip": "^3.10.1", "just-throttle": "^4.0.1", + "md5": "^2.3.0", "net": "^1.0.2", "node-cache": "^4.2.0", "node-ssdp": "^4.0.0", diff --git a/src/LanguageServerManager.spec.ts b/src/LanguageServerManager.spec.ts index 679eee1c..aed4316f 100644 --- a/src/LanguageServerManager.spec.ts +++ b/src/LanguageServerManager.spec.ts @@ -65,7 +65,10 @@ describe('LanguageServerManager', () => { }); } - afterEach(() => { + afterEach(function() { + //deleting certain directories take a while + this.timeout(30_000); + sinon.restore(); fsExtra.removeSync(tempDir); }); @@ -358,6 +361,20 @@ describe('LanguageServerManager', () => { expect(spy.called).to.be.false; }); + it('installs from url', async () => { + fsExtra.ensureDirSync( + s`${storageDir}/packages/brighterscript-0.65.0/node_modules/brighterscript/dist/index.js` + ); + expect( + await languageServerManager['ensureBscVersionInstalled']( + 'https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-0.67.5-lsp-refactor.20240806164122.tgz' + ) + ).to.eql(s`${storageDir}/packages/brighterscript-028738851c072bf844c10c260d6d2c65/node_modules/brighterscript`); + expect( + fsExtra.pathExistsSync(s`${storageDir}/packages/brighterscript-028738851c072bf844c10c260d6d2c65/node_modules/brighterscript`) + ).to.be.true; + }); + it('repairs a broken bsc version', async () => { let stub = sinon.stub(fsExtra, 'remove'); fsExtra.ensureDirSync( diff --git a/src/LanguageServerManager.ts b/src/LanguageServerManager.ts index 291a44be..973a0b64 100644 --- a/src/LanguageServerManager.ts +++ b/src/LanguageServerManager.ts @@ -32,6 +32,7 @@ import * as fsExtra from 'fs-extra'; import { EventEmitter } from 'eventemitter3'; import * as childProcess from 'child_process'; import * as semver from 'semver'; +import * as md5 from 'md5'; /** * Tracks the running/stopped state of the language server. When the lsp crashes, vscode will restart it. After the 5th crash, they'll leave it permanently crashed. @@ -482,7 +483,8 @@ export class LanguageServerManager { let packageJsonEntry: string; //if this is a URL if (/^(http|https):\/\//.test(bsdkEntry)) { - folderName = `brighterscript-${btoa(bsdkEntry.trim())}`.substring(0, 30); + //hash the URL to create a unique folder name. There is next to zero possibility these will clash, so the hash should be fine. + folderName = `brighterscript-${md5(bsdkEntry.trim())}`; packageJsonEntry = bsdkEntry.trim(); //this is a valid semantic version diff --git a/src/commands/LanguageServerInfoCommand.spec.ts b/src/commands/LanguageServerInfoCommand.spec.ts index ddd0a36e..5e389d8f 100644 --- a/src/commands/LanguageServerInfoCommand.spec.ts +++ b/src/commands/LanguageServerInfoCommand.spec.ts @@ -35,7 +35,8 @@ describe('LanguageServerInfoCommand', () => { fsExtra.removeSync(tempDir); }); - describe('getBscVersionsFromNpm', () => { + describe('getBscVersionsFromNpm', function() { + this.timeout(20_000); it('returns a list of versions', async () => { const results = await command['getBscVersionsFromNpm'](); // `results` is entire list of all bsc versions, live from npm. so we obviously can't make a test that ensure they're all correct.