This repository was archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSession.ts
65 lines (56 loc) · 1.85 KB
/
Session.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { URL } from "url";
import { AUTH_TYPE_BASIC, HTTP_PROTOCOL_CHOICES } from "./SessConstants";
import { AbstractSession } from "./AbstractSession";
import { ISession } from "./doc/ISession";
/**
* Non-abstract session class
* @export
* @class Session
* @extends {AbstractSession}
*/
export class Session extends AbstractSession {
/**
* Creates an instance of Session.
* @param {ISession} newSession - contains input for new session
* @memberof Session
*/
constructor(newSession: ISession) {
super(newSession);
}
/**
* Creates an instance of Session from a URL object.
* @param {URL} url - URL object from the Node.js `url` library
* @param {boolean} includePath - Specifies whether session base path should be populated. Default value is true.
* @memberof Session
*/
public static createFromUrl(url: URL, includePath?: boolean): Session {
const sessCfg: ISession = {
hostname: url.hostname,
user: url.username,
password: url.password
};
if (url.protocol === "http:" || url.protocol === "https:") {
sessCfg.protocol = url.protocol.slice(0, -1) as HTTP_PROTOCOL_CHOICES;
}
if (url.port) {
sessCfg.port = parseInt(url.port);
}
if (includePath !== false) {
sessCfg.basePath = url.pathname;
}
if (sessCfg.user != null && sessCfg.password != null) {
sessCfg.type = AUTH_TYPE_BASIC;
}
return new this(sessCfg);
}
}