|
| 1 | +"use strict"; |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// Licensed under the MIT License. |
| 4 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 5 | +exports.AcquisitionManager = exports.AcquisitionStatus = void 0; |
| 6 | +class AcquisitionStatus { |
| 7 | + static DeploymentSucceeded = "DeploymentSucceeded"; |
| 8 | + static DeploymentFailed = "DeploymentFailed"; |
| 9 | +} |
| 10 | +exports.AcquisitionStatus = AcquisitionStatus; |
| 11 | +class AcquisitionManager { |
| 12 | + _appVersion; |
| 13 | + _clientUniqueId; |
| 14 | + _deploymentKey; |
| 15 | + _httpRequester; |
| 16 | + _ignoreAppVersion; |
| 17 | + _serverUrl; |
| 18 | + constructor(httpRequester, configuration) { |
| 19 | + this._httpRequester = httpRequester; |
| 20 | + this._serverUrl = configuration.serverUrl; |
| 21 | + if (this._serverUrl.slice(-1) !== "/") { |
| 22 | + this._serverUrl += "/"; |
| 23 | + } |
| 24 | + this._appVersion = configuration.appVersion; |
| 25 | + this._clientUniqueId = configuration.clientUniqueId; |
| 26 | + this._deploymentKey = configuration.deploymentKey; |
| 27 | + this._ignoreAppVersion = configuration.ignoreAppVersion; |
| 28 | + } |
| 29 | + queryUpdateWithCurrentPackage(currentPackage, callback) { |
| 30 | + if (!currentPackage || !currentPackage.appVersion) { |
| 31 | + throw new Error("Calling common acquisition SDK with incorrect package"); // Unexpected; indicates error in our implementation |
| 32 | + } |
| 33 | + const updateRequest = { |
| 34 | + deploymentKey: this._deploymentKey, |
| 35 | + appVersion: currentPackage.appVersion, |
| 36 | + packageHash: currentPackage.packageHash, |
| 37 | + isCompanion: this._ignoreAppVersion, |
| 38 | + label: currentPackage.label, |
| 39 | + clientUniqueId: this._clientUniqueId, |
| 40 | + }; |
| 41 | + const requestUrl = this._serverUrl + "updateCheck?" + queryStringify(updateRequest); |
| 42 | + this._httpRequester.request(0 /* Http.Verb.GET */, requestUrl, (error, response) => { |
| 43 | + if (error) { |
| 44 | + callback(error, /*remotePackage=*/ null); |
| 45 | + return; |
| 46 | + } |
| 47 | + if (response.statusCode !== 200) { |
| 48 | + callback(new Error(response.statusCode + ": " + response.body), /*remotePackage=*/ null); |
| 49 | + return; |
| 50 | + } |
| 51 | + let updateInfo; |
| 52 | + try { |
| 53 | + const responseObject = JSON.parse(response.body); |
| 54 | + updateInfo = responseObject.updateInfo; |
| 55 | + } |
| 56 | + catch (error) { |
| 57 | + callback(error, /*remotePackage=*/ null); |
| 58 | + return; |
| 59 | + } |
| 60 | + if (!updateInfo) { |
| 61 | + callback(error, /*remotePackage=*/ null); |
| 62 | + return; |
| 63 | + } |
| 64 | + else if (updateInfo.updateAppVersion) { |
| 65 | + callback(/*error=*/ null, { |
| 66 | + updateAppVersion: true, |
| 67 | + appVersion: updateInfo.appVersion, |
| 68 | + }); |
| 69 | + return; |
| 70 | + } |
| 71 | + else if (!updateInfo.isAvailable) { |
| 72 | + callback(/*error=*/ null, /*remotePackage=*/ null); |
| 73 | + return; |
| 74 | + } |
| 75 | + const remotePackage = { |
| 76 | + deploymentKey: this._deploymentKey, |
| 77 | + description: updateInfo.description, |
| 78 | + label: updateInfo.label, |
| 79 | + appVersion: updateInfo.appVersion, |
| 80 | + isMandatory: updateInfo.isMandatory, |
| 81 | + packageHash: updateInfo.packageHash, |
| 82 | + packageSize: updateInfo.packageSize, |
| 83 | + downloadUrl: updateInfo.downloadURL, |
| 84 | + }; |
| 85 | + callback(/*error=*/ null, remotePackage); |
| 86 | + }); |
| 87 | + } |
| 88 | + reportStatusDeploy(deployedPackage, status, previousLabelOrAppVersion, previousDeploymentKey, callback) { |
| 89 | + const url = this._serverUrl + "reportStatus/deploy"; |
| 90 | + const body = { |
| 91 | + appVersion: this._appVersion, |
| 92 | + deploymentKey: this._deploymentKey, |
| 93 | + }; |
| 94 | + if (this._clientUniqueId) { |
| 95 | + body.clientUniqueId = this._clientUniqueId; |
| 96 | + } |
| 97 | + if (deployedPackage) { |
| 98 | + body.label = deployedPackage.label; |
| 99 | + body.appVersion = deployedPackage.appVersion; |
| 100 | + switch (status) { |
| 101 | + case AcquisitionStatus.DeploymentSucceeded: |
| 102 | + case AcquisitionStatus.DeploymentFailed: |
| 103 | + body.status = status; |
| 104 | + break; |
| 105 | + default: |
| 106 | + if (callback) { |
| 107 | + if (!status) { |
| 108 | + callback(new Error("Missing status argument."), /*not used*/ null); |
| 109 | + } |
| 110 | + else { |
| 111 | + callback(new Error('Unrecognized status "' + status + '".'), /*not used*/ null); |
| 112 | + } |
| 113 | + } |
| 114 | + return; |
| 115 | + } |
| 116 | + } |
| 117 | + if (previousLabelOrAppVersion) { |
| 118 | + body.previousLabelOrAppVersion = previousLabelOrAppVersion; |
| 119 | + } |
| 120 | + if (previousDeploymentKey) { |
| 121 | + body.previousDeploymentKey = previousDeploymentKey; |
| 122 | + } |
| 123 | + callback = typeof arguments[arguments.length - 1] === "function" && arguments[arguments.length - 1]; |
| 124 | + this._httpRequester.request(2 /* Http.Verb.POST */, url, JSON.stringify(body), (error, response) => { |
| 125 | + if (callback) { |
| 126 | + if (error) { |
| 127 | + callback(error, /*not used*/ null); |
| 128 | + return; |
| 129 | + } |
| 130 | + if (response.statusCode !== 200) { |
| 131 | + callback(new Error(response.statusCode + ": " + response.body), /*not used*/ null); |
| 132 | + return; |
| 133 | + } |
| 134 | + callback(/*error*/ null, /*not used*/ null); |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + reportStatusDownload(downloadedPackage, callback) { |
| 139 | + const url = this._serverUrl + "reportStatus/download"; |
| 140 | + const body = { |
| 141 | + clientUniqueId: this._clientUniqueId, |
| 142 | + deploymentKey: this._deploymentKey, |
| 143 | + label: downloadedPackage.label, |
| 144 | + }; |
| 145 | + this._httpRequester.request(2 /* Http.Verb.POST */, url, JSON.stringify(body), (error, response) => { |
| 146 | + if (callback) { |
| 147 | + if (error) { |
| 148 | + callback(error, /*not used*/ null); |
| 149 | + return; |
| 150 | + } |
| 151 | + if (response.statusCode !== 200) { |
| 152 | + callback(new Error(response.statusCode + ": " + response.body), /*not used*/ null); |
| 153 | + return; |
| 154 | + } |
| 155 | + callback(/*error*/ null, /*not used*/ null); |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | +} |
| 160 | +exports.AcquisitionManager = AcquisitionManager; |
| 161 | +function queryStringify(object) { |
| 162 | + let queryString = ""; |
| 163 | + let isFirst = true; |
| 164 | + for (const property in object) { |
| 165 | + if (object.hasOwnProperty(property)) { |
| 166 | + const value = object[property]; |
| 167 | + if (!isFirst) { |
| 168 | + queryString += "&"; |
| 169 | + } |
| 170 | + queryString += encodeURIComponent(property) + "="; |
| 171 | + if (value !== null && typeof value !== "undefined") { |
| 172 | + queryString += encodeURIComponent(value); |
| 173 | + } |
| 174 | + isFirst = false; |
| 175 | + } |
| 176 | + } |
| 177 | + return queryString; |
| 178 | +} |
0 commit comments