-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
141 lines (115 loc) · 3.68 KB
/
index.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var Gitana = require("./lib/gitana");
var isNode = function() {
var windowTest;
try {
//if window is declared thorugh JS DOM then window will be defined but will not be equal to this
windowTest = (this === window);
} catch(e) {
return true;
}
try {
return (this===global) && !windowTest;
} catch(e) {
return false;
}
}
var _safeRequire = function(name)
{
var x = null;
try {
x = require(name);
}
catch (e) { }
return x;
}
// default settings so that we connect to Cloud CMS demo sandbox (by default)
Gitana.DEFAULT_CONFIG = {
"baseURL": "https://api.cloudcms.com"
};
if (isNode())
{
var fs = _safeRequire("fs");
var path = _safeRequire("path");
//var request = _safeRequire("request");
var request = require("./request");
var HttpsProxyAgent = _safeRequire("https-proxy-agent");
// support for environment variable "HTTP_PROXY" or "HTTPS_PROXY"
var _httpProxy = process.env.http_proxy || process.env.HTTP_PROXY || process.env.https_proxy || process.env.HTTPS_PROXY;
// support for loading "gitana.json" from optional file on disk
var defaultConfig = null;
Gitana.loadDefaultConfig = function() {
if (!defaultConfig)
{
var configFilePath = path.resolve(path.join(".", "gitana.json"));
if (fs.existsSync(configFilePath)) {
defaultConfig = JSON.parse(fs.readFileSync(configFilePath));
}
}
return defaultConfig;
};
// support node streams for upload and download
Gitana.streamUpload = function(driver, readStream, uploadUri, contentType, callback)
{
var headers = {};
headers["Content-Type"] = contentType;
headers["Authorization"] = driver.getHttpHeaders()["Authorization"];
request({
"method": "POST",
"url": uploadUri,
"data": readStream,
"headers": headers,
"timeout": 120 * 1000 // 2 minutes
}, function (err, httpResponse, body) {
if (err)
{
return callback(err);
}
if (httpResponse.status >= 200 && httpResponse.status <= 204)
{
return callback();
}
callback({
"message": "Status: " + httpResponse.statusCode + ", Message: " + body
});
});
};
Gitana.streamDownload = function(attachment, callback)
{
var driver = attachment.getDriver();
var headers = {};
headers["Authorization"] = driver.getHttpHeaders()["Authorization"];
// download and pipe to stream
request({
"method": "GET",
"url": attachment.getDownloadUri(),
"headers": {
"Authorization": attachment.getDriver().getHttpHeaders()["Authorization"]
},
"timeout": 120 * 1000 // 2 minutes
}, function (err, response) {
if (err) {
return callback(err);
}
callback(null, response.data);
});
};
Gitana.HTTP_XHR_FACTORY = function()
{
var XHR = null;
if (!_httpProxy)
{
// XHR library
var XMLHttpRequest = _safeRequire("xmlhttprequest").XMLHttpRequest;
XHR = new XMLHttpRequest();
}
else
{
// new XHR library
var XMLHttpRequest = _safeRequire("node-http-xhr");
XHR = new XMLHttpRequest();
XHR._reqOpts.agent = new HttpsProxyAgent(_httpProxy);
}
return XHR;
};
}
module.exports = Gitana;