@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
254 lines (252 loc) • 10.2 kB
JavaScript
import { EMPTY, of, throwError } from 'rxjs';
import { catchError, delay, expand, filter, map, mergeMap, switchMap } from 'rxjs/operators';
import { Net } from './net';
/**
* Sharing file mode.
*/
export var DeploymentShareMode;
(function (DeploymentShareMode) {
/**
* Admin share.
*/
DeploymentShareMode["Admin"] = "adminshare";
/**
* User share.
*/
DeploymentShareMode["User"] = "usershare";
})(DeploymentShareMode || (DeploymentShareMode = {}));
/**
* Download states.
*/
export var DownloadState;
(function (DownloadState) {
/**
* Download not started.
*/
DownloadState["NotStarted"] = "NotStarted";
/**
* Download is running.
*/
DownloadState["Running"] = "Running";
/**
* Download is cancelled.
*/
DownloadState["Cancelled"] = "Cancelled";
/**
* Download failed.
*/
DownloadState["Failed"] = "Failed";
/**
* Download completed.
*/
DownloadState["Completed"] = "Completed";
})(DownloadState || (DownloadState = {}));
/**
* Secure deployment share class.
*/
export class DeploymentShare {
appContext;
static deploymentShareUrl = 'deployment/{0}';
static deploymentShareNameUrl = 'deployment/{0}/{1}';
static deploymentShareDownloadUrl = 'deployment/{0}/{1}/download';
static deploymentShareDownloadIdUrl = 'deployment/{0}/{1}/download/{2}';
static deploymentShareDownloadCancelUrl = 'deployment/{0}/{1}/download/{2}/cancel';
static deploymentShareStatusUrl = 'deployment/status';
static deploymentShareFileUrl = 'deployment/{0}/{1}/file';
static deploymentShareFilePathUrl = 'deployment/{0}/{1}/file/{2}';
static deploymentCopyUrl = 'deployment/{0}/{1}/copy';
/**
* Initializes a new instance of the DeploymentShare class.
*
* @param appContext the application context.
*/
constructor(appContext) {
this.appContext = appContext;
}
/**
* Create a new deployment network share. (elevate the desktop gateway if required.)
*
* @param shareMode the sharing mode.
* @param shareName the name of share.
*/
createShare(shareMode, shareName) {
const url = DeploymentShare.deploymentShareNameUrl.format(shareMode, shareName);
return this.appContext.gateway.put(url, '{}')
.pipe(catchError((error, caught) => {
// catch elevation error and retry after elevated.
if (this.appContext.gateway.isElevationRequired(error)) {
return this.appContext.gateway.pollingGatewayElevated()
.pipe(switchMap(elevated => elevated ? caught : throwError(() => error)));
}
return throwError(() => error);
}), map((response) => {
if (!response) {
throw new Error('DeploymentShare createShare() call is failed with null response result.');
}
return response;
}));
}
/**
* Delete the deployment network share. (elevate the desktop gateway if required.)
*
* @param shareMode the sharing mode.
* @param shareName the name of share.
*/
deleteShare(shareMode, shareName) {
const url = DeploymentShare.deploymentShareNameUrl.format(shareMode, shareName);
return this.appContext.gateway.delete(url)
.pipe(catchError((error, caught) => {
// catch elevation error and retry after elevated.
if (this.appContext.gateway.isElevationRequired(error)) {
return this.appContext.gateway.pollingGatewayElevated()
.pipe(switchMap(elevated => elevated ? caught : throwError(() => error)));
}
return throwError(() => error);
}), map(() => null));
}
/**
* List existing deployment network shares.
*
* @param shareMode the sharing mode.
*/
queryShares(shareMode) {
const url = DeploymentShare.deploymentShareUrl.format(shareMode);
return this.appContext.gateway.get(url)
.pipe(map((response) => {
const value = response && response.value;
if (!value) {
throw new Error('DeploymentShare queryShares() call is failed with null response result.');
}
return value;
}));
}
/**
* Download files from specified urls and store to the deployment network share folder.
*
* @param shareMode the sharing mode.
* @param shareName the name of share.
* @param request the request object.
* @param timeout the timeout for the download in milliseconds.
* @param cancelReady (Optional) the cancel ready callback. Retain the supplied ID to call cancelDownload().
* @param polling (Optional) the polling timer milliseconds (default is 2000, and minimum is 1000).
*/
downloadFiles(shareMode, shareName, request, timeout, cancelReady, polling) {
// default polling every 2 seconds, and minimum every 1 second.
polling = polling == null ? 2000 : Math.max(polling, 1000);
const url = DeploymentShare.deploymentShareDownloadUrl.format(shareMode, shareName);
const body = JSON.stringify({ value: request, timeout: timeout });
return this.appContext.gateway.post(url, body)
.pipe(mergeMap((response) => {
if (!response) {
return throwError(() => new Error('DeploymentShare downloadFiles() call is failed with null response or empty Location header.'));
}
if (cancelReady) {
cancelReady(response.id);
}
const statusUrl = DeploymentShare.deploymentShareDownloadIdUrl.format(shareMode, shareName, response.id);
return this.queryStatus(statusUrl)
.pipe(expand((status) => {
if (status.downloadState === DownloadState.Running) {
return of(null)
.pipe(delay(polling), mergeMap(() => this.queryStatus(statusUrl)));
}
return EMPTY;
}), filter(status => status.downloadState !== DownloadState.Running &&
status.downloadState !== DownloadState.NotStarted));
}));
}
/**
* Cancel the download by ID.
*
* @param shareMode the sharing mode.
* @param shareName the name of share.
* @param id the identification of download call responded by cancelReady() call back.
*/
cancelDownload(shareMode, shareName, id) {
const url = DeploymentShare.deploymentShareDownloadCancelUrl.format(shareMode, shareName, id);
return this.appContext.gateway.post(url, '{}')
.pipe(map(() => null));
}
/**
* Query active downloads with filtering conditions.
*
* @param shareMode the sharing mode.
* @param shareName the name of share.
*/
queryDownloads(shareMode, shareName) {
const url = DeploymentShare.deploymentShareStatusUrl;
return this.appContext.gateway.get(url)
.pipe(map((response) => {
if (!response) {
throw new Error('DeploymentShare queryDownloads() call is failed with null response.');
}
return response.filter(item => (!shareMode || item.mode === shareMode) && (!shareName || item.name === shareName));
}));
}
/**
* Query files on the share.
*
* @param shareMode the sharing mode.
* @param shareName the name of share.
*/
queryFiles(shareMode, shareName) {
const url = DeploymentShare.deploymentShareFileUrl.format(shareMode, shareName);
return this.appContext.gateway.get(url)
.pipe(map((response) => {
const files = response && response.files;
if (!files) {
throw new Error('DeploymentShare queryFiles() call is failed with null response.');
}
return files;
}));
}
/**
* Delete a file or a directory on the share.
*
* @param shareMode the sharing mode.
* @param shareName the name of share.
* @param path the path to the file or the directory on the share.
*/
deleteItem(shareMode, shareName, path) {
const path64 = Net.base64urlEncode(path);
const url = DeploymentShare.deploymentShareFilePathUrl.format(shareMode, shareName, path64);
return this.appContext.gateway.delete(url)
.pipe(map(() => null));
}
/**
* Create a text/XML file in network share.
*
* @param shareMode the sharing mode.
* @param shareName the name of share.
* @param path the path to the file or the directory on the share.
* @param content the linese of content for the file to create.
*/
createTextFile(shareMode, shareName, path, content) {
const path64 = Net.base64urlEncode(path);
const url = DeploymentShare.deploymentShareFilePathUrl.format(shareMode, shareName, path64);
const body = JSON.stringify({ content: content });
return this.appContext.gateway.post(url, body).pipe(map(() => null));
}
/**
* Copy file or folder in network share.
* @param shareMode The share mode.
* @param shareName the name of the share.
* @param source The source path of the item to copy relative to the network share.
* @param destination The destination path of the item to copy relative to the network share.
*/
copyItem(shareMode, shareName, source, destination) {
const url = DeploymentShare.deploymentCopyUrl.format(shareMode, shareName);
const body = JSON.stringify({ source: source, destination: destination });
return this.appContext.gateway.post(url, body).pipe(map(() => null));
}
queryStatus(statusUrl) {
return this.appContext.gateway.get(statusUrl)
.pipe(map((response) => {
if (!response) {
throw new Error('DeploymentShare download() call is failed with null response for status.');
}
return response;
}));
}
}
//# sourceMappingURL=deployment-share.js.map