sp-rest-proxy
Version:
SharePoint REST API Proxy for Node.js and Express local serve
118 lines • 5.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Server = void 0;
var SocketIO = require("socket.io");
var http = require("http");
var bodyParser = require("body-parser");
var cors = require("cors");
var misc_1 = require("../utils/misc");
var logger_1 = require("../utils/logger");
var Server = (function () {
function Server(settings, proxy, app) {
var _this = this;
this.settings = settings;
this.proxy = proxy;
this.app = app;
this.init = function () {
_this.server.listen(_this.settings.port || _this.proxy.port);
_this.io.on('connection', function (socket) {
_this.socket = socket;
var bodyParserRaw = bodyParser.raw({
type: '*/*',
limit: _this.proxy.rawBodyLimitSize,
verify: function (req, _res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8');
req.buffer = buf;
}
}
});
_this.app.get('*/_api/*', _this.getTransmitter);
_this.app.post('*/_api/*(/attachmentfiles/add|/files/add)*', bodyParserRaw, _this.postTransmitter);
_this.app.post('*/_api/[$]batch', bodyParserRaw, _this.postTransmitter);
_this.app.post('*/_api/*', bodyParser.json({ limit: _this.proxy.jsonPayloadLimitSize }), _this.postTransmitter);
_this.app.post('*/_vti_bin/*', _this.postTransmitter);
_this.app.get('*', _this.getTransmitter);
_this.app.use(bodyParser.urlencoded({ extended: true }));
_this.app.use(cors());
});
};
this.getTransmitter = function (req, res) {
var transaction = (0, misc_1.generateGuid)();
_this.logger.info('\nGET: ' + req.originalUrl);
var responseCallback = function (data) {
if (data.transaction === transaction) {
var statusCode = data.response.statusCode;
var body = data.response.body;
try {
body = JSON.parse(body);
}
catch (ex) { }
res.status(statusCode);
res.contentType(data.response.headers['content-type']);
res.send(body);
_this.socket.removeListener('RESPONSE', responseCallback);
}
};
_this.socket.on('RESPONSE', responseCallback);
var request = {
url: req.originalUrl,
method: 'GET',
headers: req.headers,
transaction: transaction
};
_this.io.emit('REQUEST', request);
};
this.postTransmitter = function (req, res) {
var transaction = (0, misc_1.generateGuid)();
_this.logger.info('\nPOST: ' + req.originalUrl);
var responseCallback = function (data) {
if (data.transaction === transaction) {
var statusCode = data.response.statusCode;
var body = data.response.body;
try {
body = JSON.parse(body);
}
catch (ex) { }
res.status(statusCode);
res.json(body);
_this.socket.removeListener('RESPONSE', responseCallback);
}
};
_this.socket.on('RESPONSE', responseCallback);
var extractPostRequestBody = function (request, callback) {
var reqBody = '';
if (request.body) {
reqBody = request.body;
if (callback && typeof callback === 'function') {
callback(reqBody);
}
}
else {
request.on('data', function (chunk) { return reqBody += chunk; });
request.on('end', function () {
if (callback && typeof callback === 'function') {
callback(reqBody);
}
});
}
};
extractPostRequestBody(req, function (body) {
var request = {
url: req.originalUrl,
method: 'POST',
headers: req.headers,
body: body,
transaction: transaction
};
_this.io.emit('REQUEST', request);
});
};
this.server = http.createServer(this.app);
this.io = new SocketIO.Server(this.server);
this.logger = new logger_1.Logger(proxy.logLevel);
}
return Server;
}());
exports.Server = Server;
//# sourceMappingURL=server.js.map