Skip to content

Commit

Permalink
fix: remove / from Windows decoded URI path (#244)
Browse files Browse the repository at this point in the history
Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov authored Mar 5, 2024
1 parent e61fd72 commit 4e917fe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}
36 changes: 27 additions & 9 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { expect } from 'chai';

import { isDefined, decodeUriPath } from '../src/utils';
import * as utils from '../src/utils';

describe('Utils tests', () => {

Expand All @@ -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)', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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);
}
});
});

0 comments on commit 4e917fe

Please sign in to comment.