This repository was archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
96 lines (86 loc) · 3.52 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
const express = require('express');
const proxy = require('http-proxy-middleware');
const spoLogon = require('./o365-spo-logon');
module.exports = function (deployConf) {
var passthroughCookies = false;
const targetUri = require('url').parse(deployConf.siteUrl);
const targetAuthority = targetUri.protocol + '//' + targetUri.host;
const targetLoginPage = targetAuthority + '/_forms/default.aspx?wa=wsignin1.0';
const httpsTarget = targetUri.protocol === 'https:' || targetUri.protocol === 'https';
const negotiateAuth = deployConf.credentials.type === 'negotiate';
if (negotiateAuth) {
var KAAgent = require('agentkeepalive');
if (httpsTarget) {
KAAgent = KAAgent.HttpsAgent;
}
}
var reqCookies;
var keepaliveAgent;
if (!deployConf.credentials.type || deployConf.credentials.type === 'spo') {
spoLogon(deployConf.credentials.userName, deployConf.credentials.passWord, targetLoginPage)
.then(cookies => reqCookies = cookies);
} else if (negotiateAuth) {
keepaliveAgent = new KAAgent({
maxSockets: 1,
keepAlive: true,
maxFreeSockets: 10,
keepAliveMsecs: 1000,
timeout: 60000,
keepAliveTimeout: 90000 // free socket keepalive for 30 seconds
});
}
const proxyRoute = proxy(['/_api', '/_vti_bin'], {
target: targetAuthority,
pathRewrite: function (path, req) { return (targetUri.path + path).replace('//', '/') },
secure: httpsTarget,
changeOrigin: true,
auth: negotiateAuth ? 'LOGIN:PASS' : undefined,
logLevel: 'debug',
agent: keepaliveAgent,
onProxyRes: function onProxyRes(proxyRes, req, res) {
console.log('RSP', req.url, proxyRes.statusCode);
if (negotiateAuth) {
const key = 'www-authenticate';
proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',');
}
},
onProxyReq: function onProxyReq(proxyReq, req, res) {
if (passthroughCookies && req.headers && req.headers.cookie) {
proxyReq.setHeader('Cookie', req.headers.cookie);
} else if (reqCookies) {
proxyReq.setHeader('Cookie', reqCookies);
//passthroughCookies = true;
}
proxyReq.removeHeader('origin');
proxyReq.removeHeader('access-control-request-headers');
proxyReq.removeHeader('access-control-request-method');
}
}
)
const port = (deployConf.serve && deployConf.serve) || 5430;
var app = express();
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, OData-Version, X-RequestDigest");
if (req.method === 'OPTIONS'){
res.status(204).end();
} else {
next();
}
});
app.use(proxyRoute);
try {
const certs = require('@microsoft/gulp-core-build-serve/lib/CertificateStore')
const https = require('https');
https.createServer({
key: certs.default.instance.keyData,
cert: certs.default.instance.certificateData,
requestCert: false,
rejectUnauthorized: false
}, app).listen(port);
} catch (e) {
//Presuming setup sans sp worbench;
app.listen(port);
}
}