microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.6

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/appcenter/codepush/release-strategy/legacyCodePushServiceClient.ts

111lines · modeblame

bf370babSergey Akhalkov8 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as request from "request";
5import { DefaultApp } from "../../command/commandParams";
6import * as fs from "fs";
7import * as Q from "q";
8import { models } from "../../api/index";
9
10export interface PackageInfo {
11appVersion?: string;
12description?: string;
13isDisabled?: boolean;
14isMandatory?: boolean;
15label?: string;
16packageHash?: string;
17rollout?: number;
18}
19
20export interface Package extends PackageInfo {
21/*generated*/ blobUrl: string;
22/*generated*/ diffPackageMap?: PackageHashToBlobInfoMap;
23/*generated*/ originalLabel?: string; // Set on "Promote" and "Rollback"
24/*generated*/ originalDeployment?: string; // Set on "Promote"
25/*generated*/ releasedBy?: string; // Set by commitPackage
26/*generated*/ releaseMethod?: string; // "Upload", "Promote" or "Rollback". Unknown if unspecified
27/*generated*/ size: number;
28/*generated*/ uploadTime: number;
29}
30
31export interface PackageHashToBlobInfoMap {
32[packageHash: string]: BlobInfo;
33}
34
35export interface BlobInfo {
36size: number;
37url: string;
38}
39
40export type Headers = { [headerName: string]: string };
41
42export default class LegacyCodePushServiceClient {
43private static API_VERSION: number = 2;
44
774e7f5fSergey Akhalkov8 years ago45constructor(private accessKey: string, private app: DefaultApp, private serverUrl: string) {
bf370babSergey Akhalkov8 years ago46if (!accessKey) throw new Error("A token must be specified to execute server calls.");
47if (!serverUrl) throw new Error("A server url must be specified to execute server calls.");
48}
49
50public release(deploymentName: string, filePath: string, updateMetadata: PackageInfo): Q.Promise<models.CodePushRelease> {
51const appName = this.app.identifier;
52return Q.Promise<models.CodePushRelease>((resolve, reject) => {
53const options = {
54url: this.serverUrl + this.urlEncode(`/apps/${this.appNameParam(appName)}/deployments/${deploymentName}/release`),
55headers: {
56"Accept": `application/vnd.code-push.v${LegacyCodePushServiceClient.API_VERSION}+json`,
57"Authorization": `Bearer ${this.accessKey}`,
58},
59formData: {
60"packageInfo": JSON.stringify(updateMetadata),
61"package": fs.createReadStream(filePath),
62},
63};
64
65request.post(options, (err: any, httpResponse: any) => {
66if (err) {
67reject(this.getErrorMessage(err, httpResponse));
68return;
69}
70if (httpResponse.statusCode === 201) {
71resolve(<models.CodePushRelease>JSON.parse(httpResponse.body).package);
72} else {
73reject(this.getErrorMessage(null, httpResponse));
74return;
75}
76});
77});
78}
79
80// A template string tag function that URL encodes the substituted values
81private urlEncode(strings: any, ...values: string[]): string {
82let result = "";
83for (let i = 0; i < strings.length; i++) {
84result += strings[i];
85if (i < values.length) {
86result += encodeURIComponent(values[i]);
87}
88}
89
90return result;
91}
92
93// IIS and Azure web apps have this annoying behavior where %2F (URL encoded slashes) in the URL are URL decoded
94// BEFORE the requests reach node. That essentially means there's no good way to encode a "/" in the app name--
95// URL encodeing will work when running locally but when running on Azure it gets decoded before express sees it,
96// so app names with slashes don't get routed properly. See https://github.com/tjanczuk/iisnode/issues/343 (or other sites
97// that complain about the same) for some more info. I explored some IIS config based workarounds, but the previous
98// link seems to say they won't work, so I eventually gave up on that.
99// Anyway, to workaround this issue, we now allow the client to encode / characters as ~~ (two tildes, URL encoded).
100// The CLI now converts / to ~~ if / appears in an app name, before passing that as part of the URL. This code below
101// does the encoding. It's hack, but seems like the least bad option here.
102// Eventually, this service will go away & we'll all be on Max's new service. That's hosted in docker, no more IIS,
103// so this issue should go away then.
104private appNameParam(appName: string) {
105return appName.replace("/", "~~");
106}
107
108private getErrorMessage(error: Error | null, response: request.RequestResponse): string {
109return response && response.body ? response.body : (error ? error.message : "");
110}
111}