From 4e917fe9b1ea35a7dded4d192672a69640c06e3e Mon Sep 17 00:00:00 2001 From: Ilona Shishov Date: Tue, 5 Mar 2024 16:56:24 +0200 Subject: [PATCH] fix: remove / from Windows decoded URI path (#244) Signed-off-by: Ilona Shishov --- src/utils.ts | 12 +++++++++++- test/utils.test.ts | 36 +++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 50db1839..01556ca0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,6 +4,8 @@ * ------------------------------------------------------------------------------------------ */ 'use strict'; +import * as os from 'os'; + /** * Checks if the specified keys are defined within the provided object. * @param obj - The object to check for key definitions. @@ -28,5 +30,13 @@ export function isDefined(obj: any, ...keys: string[]): boolean { export function decodeUriPath(uri: string): string { const url = new URL(uri); const decodedUri = decodeURIComponent(url.pathname); - return decodedUri; + + const osPlatform = os.platform(); + const isWindows = osPlatform.toLowerCase().startsWith('win'); + + if (isWindows && decodedUri.startsWith('/')) { + return decodedUri.substring(1); + } else { + return decodedUri; + } } \ No newline at end of file diff --git a/test/utils.test.ts b/test/utils.test.ts index cba60766..dd841baa 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; -import { isDefined, decodeUriPath } from '../src/utils'; +import * as utils from '../src/utils'; describe('Utils tests', () => { @@ -14,7 +14,7 @@ describe('Utils tests', () => { }, }, }; - expect(isDefined(obj, 'a', 'b', 'c')).to.be.true; + expect(utils.isDefined(obj, 'a', 'b', 'c')).to.be.true; }); it('should return true when all keys are defined in the object (without key requests)', () => { @@ -25,7 +25,7 @@ describe('Utils tests', () => { }, }, }; - expect(isDefined(obj)).to.be.true; + expect(utils.isDefined(obj)).to.be.true; }); it('should return false if any key is not defined in the object', () => { @@ -36,12 +36,12 @@ describe('Utils tests', () => { }, }, }; - expect(isDefined(obj, 'a', 'b', 'd')).to.be.false; + expect(utils.isDefined(obj, 'a', 'b', 'd')).to.be.false; }); it('should return false if the object itself is not defined', () => { const obj = null; - expect(isDefined(obj, 'a', 'b', 'c')).to.be.false; + expect(utils.isDefined(obj, 'a', 'b', 'c')).to.be.false; }); it('should return false if any intermediate key in the object chain is not defined', () => { @@ -50,7 +50,7 @@ describe('Utils tests', () => { b: null }, }; - expect(isDefined(obj, 'a', 'b', 'c')).to.be.false; + expect(utils.isDefined(obj, 'a', 'b', 'c')).to.be.false; }); it('should return false if any intermediate key in the object chain is undefined', () => { @@ -59,14 +59,32 @@ describe('Utils tests', () => { b: undefined }, }; - expect(isDefined(obj, 'a', 'b', 'c')).to.be.false; + expect(utils.isDefined(obj, 'a', 'b', 'c')).to.be.false; }); - it('should decode the URI path correctly', () => { + it('should decode the URI path correctly for non Windows OS', () => { const uriString = 'file:///mock/path%20with%20spaces'; - const decodedPath = decodeUriPath(uriString); + const decodedPath = utils.decodeUriPath(uriString); expect(decodedPath).to.equal('/mock/path with spaces'); }); + + it('should decode the URI path correctly for Windows OS', () => { + const uriString = 'file:///mock/path%20with%20spaces'; + + const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform'); + + Object.defineProperty(process, 'platform', { + value: 'win32' + }); + + const decodedPath = utils.decodeUriPath(uriString); + + expect(decodedPath).to.equal('mock/path with spaces'); + + if (originalPlatform) { + Object.defineProperty(process, 'platform', originalPlatform); + } + }); }); \ No newline at end of file