Skip to content

Commit eb91ba7

Browse files
committed
linting
1 parent c1dbfb4 commit eb91ba7

File tree

8 files changed

+67
-53
lines changed

8 files changed

+67
-53
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function connectToUncPath(uncPathOptions, connectOptions) {
1010
debug(`Connecting to share: ${uncPathOptions.uncPath}`);
1111
let command = `net use "${uncPathOptions.uncPath}"`;
1212
if (uncPathOptionsHaveCredentials(uncPathOptions)) {
13-
command += ` /user:"${uncPathOptions.userName}" "${uncPathOptions.password ?? ''}"`;
13+
command += ` /user:"${uncPathOptions.userName}" "${uncPathOptions.password}"`;
1414
}
1515
try {
1616
const output = execSync(command, { stdio: 'pipe' });

index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair
2+
/* eslint-disable sonarjs/os-command */
3+
14
import { execSync } from 'node:child_process'
25

36
import Debug from 'debug'
@@ -15,9 +18,9 @@ const debug = Debug('windows-unc-path-connect')
1518

1619
/**
1720
* Connects to a given UNC path.
18-
* @param {UncPathOptions} uncPathOptions - UNC path and optional connection credentials.
19-
* @param {Partial<ConnectOptions>} connectOptions - Optional options for the connection.
20-
* @returns {boolean} - True when the connection is made successfully.
21+
* @param uncPathOptions - UNC path and optional connection credentials.
22+
* @param connectOptions - Optional options for the connection.
23+
* @returns True when the connection is made successfully.
2124
*/
2225
export function connectToUncPath(
2326
uncPathOptions: UncPathOptions,
@@ -33,7 +36,7 @@ export function connectToUncPath(
3336

3437
if (uncPathOptionsHaveCredentials(uncPathOptions)) {
3538
command += ` /user:"${uncPathOptions.userName}" "${
36-
uncPathOptions.password ?? ''
39+
uncPathOptions.password
3740
}"`
3841
}
3942

@@ -50,16 +53,16 @@ export function connectToUncPath(
5053

5154
return output.includes('command completed successfully')
5255
} catch (error) {
53-
return error
56+
return (error as Error)
5457
.toString()
5558
.includes('Multiple connections to a server or shared resource')
5659
}
5760
}
5861

5962
/**
6063
* Disconnects a given UNC path.
61-
* @param {UncPath} uncPath - UNC path to disconnect
62-
* @returns {boolean} - True if the path was disconnected.
64+
* @param uncPath - UNC path to disconnect
65+
* @returns True if the path was disconnected.
6366
*/
6467
export function disconnectUncPath(uncPath: UncPath): boolean {
6568
if (!isWindows() || !uncPathIsSafe(uncPath)) {

test/index.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ await describe('windows-unc-path-connect', async () => {
2929
}
3030
});
3131
await describe('windows-unc-path-connect/validators', async () => {
32+
const goodUncPaths = ['\\\\192.168.1.1\\folder'];
3233
await describe('uncPathIsSafe()', async () => {
33-
await it('Returns "true" for good UNC paths', async () => {
34-
const goodUncPaths = ['\\\\192.168.1.1\\folder'];
34+
await it('Returns "true" for good UNC paths', () => {
3535
for (const goodUncPath of goodUncPaths) {
3636
assert.strictEqual(uncPathIsSafe(goodUncPath), true);
3737
}
3838
});
39-
await it('Returns "false" for bad UNC paths', async () => {
39+
await it('Returns "false" for bad UNC paths', () => {
4040
const badUncPaths = [
4141
'192.168.1.1',
4242
'\\192.168.1.1',
@@ -48,36 +48,36 @@ await describe('windows-unc-path-connect/validators', async () => {
4848
});
4949
});
5050
await describe('uncPathOptionsHaveCredentials()', async () => {
51-
await it('Returns "true" for options with credentials', async () => {
51+
await it('Returns "true" for options with credentials', () => {
5252
assert.strictEqual(uncPathOptionsHaveCredentials({
53-
uncPath: '\\\\192.168.1.1\\folder',
53+
uncPath: goodUncPaths[0],
5454
userName: 'user',
5555
password: 'pass'
5656
}), true);
5757
});
58-
await it('Returns "false" for options without credentials', async () => {
58+
await it('Returns "false" for options without credentials', () => {
5959
const optionsWithoutCredentials = [
6060
{
61-
uncPath: '\\\\192.168.1.1\\folder'
61+
uncPath: goodUncPaths[0]
6262
},
6363
{
64-
uncPath: '\\\\192.168.1.1\\folder',
64+
uncPath: goodUncPaths[0],
6565
userName: ''
6666
},
6767
{
68-
uncPath: '\\\\192.168.1.1\\folder',
68+
uncPath: goodUncPaths[0],
6969
userName: 'noPass'
7070
},
7171
{
72-
uncPath: '\\\\192.168.1.1\\folder',
72+
uncPath: goodUncPaths[0],
7373
password: ''
7474
},
7575
{
76-
uncPath: '\\\\192.168.1.1\\folder',
76+
uncPath: goodUncPaths[0],
7777
password: 'noUser'
7878
},
7979
{
80-
uncPath: '\\\\192.168.1.1\\folder',
80+
uncPath: goodUncPaths[0],
8181
userName: '',
8282
password: ''
8383
}

test/index.test.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// eslint-disable-next-line eslint-comments/disable-enable-pair
2-
/* eslint-disable security/detect-non-literal-fs-filename */
1+
// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair
2+
/* eslint-disable @typescript-eslint/no-magic-numbers, security/detect-non-literal-fs-filename, unicorn/prefer-string-raw */
33

44
import assert from 'node:assert'
55
import fs from 'node:fs/promises'
66
import { describe, it } from 'node:test'
77

88
import {
9+
type UncPath,
910
type UncPathOptions,
1011
connectToUncPath,
1112
disconnectUncPath
@@ -45,17 +46,19 @@ await describe('windows-unc-path-connect', async () => {
4546
})
4647

4748
await describe('windows-unc-path-connect/validators', async () => {
48-
await describe('uncPathIsSafe()', async () => {
49-
await it('Returns "true" for good UNC paths', async () => {
50-
const goodUncPaths = ['\\\\192.168.1.1\\folder']
5149

50+
const goodUncPaths: UncPath[] = ['\\\\192.168.1.1\\folder']
51+
52+
await describe('uncPathIsSafe()', async () => {
53+
await it('Returns "true" for good UNC paths', () => {
5254
for (const goodUncPath of goodUncPaths) {
5355
assert.strictEqual(uncPathIsSafe(goodUncPath), true)
5456
}
5557
})
5658

57-
await it('Returns "false" for bad UNC paths', async () => {
59+
await it('Returns "false" for bad UNC paths', () => {
5860
const badUncPaths = [
61+
// eslint-disable-next-line sonarjs/no-hardcoded-ip
5962
'192.168.1.1', // missing slashes
6063
'\\192.168.1.1', // missing double slash beginning
6164
'\\\\192.168.1.1\\folder" /delete' // includes double quote
@@ -68,40 +71,42 @@ await describe('windows-unc-path-connect/validators', async () => {
6871
})
6972

7073
await describe('uncPathOptionsHaveCredentials()', async () => {
71-
await it('Returns "true" for options with credentials', async () => {
74+
await it('Returns "true" for options with credentials', () => {
7275
assert.strictEqual(
7376
uncPathOptionsHaveCredentials({
74-
uncPath: '\\\\192.168.1.1\\folder',
77+
uncPath: goodUncPaths[0],
7578
userName: 'user',
79+
// eslint-disable-next-line sonarjs/no-hardcoded-credentials
7680
password: 'pass'
7781
}),
7882
true
7983
)
8084
})
8185

82-
await it('Returns "false" for options without credentials', async () => {
86+
await it('Returns "false" for options without credentials', () => {
8387
const optionsWithoutCredentials: UncPathOptions[] = [
8488
{
85-
uncPath: '\\\\192.168.1.1\\folder'
89+
uncPath: goodUncPaths[0]
8690
},
8791
{
88-
uncPath: '\\\\192.168.1.1\\folder',
92+
uncPath: goodUncPaths[0],
8993
userName: ''
9094
},
9195
{
92-
uncPath: '\\\\192.168.1.1\\folder',
96+
uncPath: goodUncPaths[0],
9397
userName: 'noPass'
9498
},
9599
{
96-
uncPath: '\\\\192.168.1.1\\folder',
100+
uncPath: goodUncPaths[0],
97101
password: ''
98102
},
99103
{
100-
uncPath: '\\\\192.168.1.1\\folder',
104+
uncPath: goodUncPaths[0],
105+
// eslint-disable-next-line sonarjs/no-hardcoded-credentials
101106
password: 'noUser'
102107
},
103108
{
104-
uncPath: '\\\\192.168.1.1\\folder',
109+
uncPath: goodUncPaths[0],
105110
userName: '',
106111
password: ''
107112
}

types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ export interface UncPathOptionsWithoutCredentials {
44
uncPath: UncPath;
55
}
66
export type UncPathOptionsWithCredentials = UncPathOptionsWithoutCredentials & {
7-
userName: string;
8-
password: string;
7+
userName: Exclude<string, ''>;
8+
password: Exclude<string, ''>;
99
};
1010
export type UncPathOptions = UncPathOptionsWithoutCredentials | UncPathOptionsWithCredentials;
1111
export interface ConnectOptions {

types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export interface UncPathOptionsWithoutCredentials {
77
}
88

99
export type UncPathOptionsWithCredentials = UncPathOptionsWithoutCredentials & {
10-
userName: string
11-
password: string
10+
userName: Exclude<string, ''>
11+
password: Exclude<string, ''>
1212
}
1313

1414
export type UncPathOptions =

validators.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ export function isWindows() {
66
return process.platform === 'win32';
77
}
88
export function uncPathOptionsHaveCredentials(uncPathOptions) {
9-
return ((uncPathOptions.userName ?? '') !== '' &&
10-
(uncPathOptions.password ?? '') !== '');
9+
return ((uncPathOptions.userName ??
10+
'') !== '' &&
11+
(uncPathOptions.password ??
12+
'') !== '');
1113
}
1214
export function uncPathIsSafe(uncPath) {
13-
return uncPath.startsWith(uncPathPrefix) && !stringHasForbiddenCharacters(uncPath);
15+
return (uncPath.startsWith(uncPathPrefix) && !stringHasForbiddenCharacters(uncPath));
1416
}
1517
export function uncPathOptionsAreSafe(uncPathOptions) {
1618
if (!uncPathIsSafe(uncPathOptions.uncPath)) {

validators.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,43 @@ function stringHasForbiddenCharacters(stringToCheck?: string): boolean {
1111

1212
/**
1313
* Checks if the operating system is Windows.
14-
* @returns {boolean} - True if the operating system is Windows.
14+
* @returns True if the operating system is Windows.
1515
*/
1616
export function isWindows(): boolean {
1717
return process.platform === 'win32'
1818
}
1919

2020
/**
2121
* Checks if the options include credentials.
22-
* @param {UncPathOptions} uncPathOptions - UNC path options.
23-
* @returns {boolean} - True when the UNC path options include credentials.
22+
* @param uncPathOptions - UNC path options.
23+
* @returns True when the UNC path options include credentials.
2424
*/
2525
export function uncPathOptionsHaveCredentials(
2626
uncPathOptions: UncPathOptions
2727
): uncPathOptions is UncPathOptionsWithCredentials {
2828
return (
29-
((uncPathOptions as UncPathOptionsWithCredentials).userName ?? '') !== '' &&
30-
((uncPathOptions as UncPathOptionsWithCredentials).password ?? '') !== ''
29+
((uncPathOptions as Partial<UncPathOptionsWithCredentials>).userName ??
30+
'') !== '' &&
31+
((uncPathOptions as Partial<UncPathOptionsWithCredentials>).password ??
32+
'') !== ''
3133
)
3234
}
3335

3436
/**
3537
* Ensures a UNC path is safe.
36-
* @param {string} uncPath - UNC path.
37-
* @returns {boolean} - True if the UNC path is safe for use.
38+
* @param uncPath - UNC path.
39+
* @returns True if the UNC path is safe for use.
3840
*/
3941
export function uncPathIsSafe(uncPath: string): uncPath is UncPath {
40-
return uncPath.startsWith(uncPathPrefix) && !stringHasForbiddenCharacters(uncPath)
42+
return (
43+
uncPath.startsWith(uncPathPrefix) && !stringHasForbiddenCharacters(uncPath)
44+
)
4145
}
4246

4347
/**
4448
* Ensures the options are safe to use.
45-
* @param {UncPathOptions} uncPathOptions - UNC path options.
46-
* @returns {boolean} - True if the options are safe to use.
49+
* @param uncPathOptions - UNC path options.
50+
* @returns True if the options are safe to use.
4751
*/
4852
export function uncPathOptionsAreSafe(uncPathOptions: UncPathOptions): boolean {
4953
if (!uncPathIsSafe(uncPathOptions.uncPath)) {
@@ -54,7 +58,7 @@ export function uncPathOptionsAreSafe(uncPathOptions: UncPathOptions): boolean {
5458
if (
5559
uncPathOptionsHaveCredentials(uncPathOptions) &&
5660
(stringHasForbiddenCharacters(uncPathOptions.userName) ||
57-
stringHasForbiddenCharacters(uncPathOptions.password))
61+
stringHasForbiddenCharacters(uncPathOptions.password))
5862
) {
5963
return false
6064
}

0 commit comments

Comments
 (0)