Initial release v1.0.0 — Nextcloud Collabora Node-RED sidecar
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
'use strict';
|
||||
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const { URL } = require('url');
|
||||
|
||||
function requestBuffer(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = new URL(options.url);
|
||||
const transport = url.protocol === 'https:' ? https : http;
|
||||
const reqOptions = {
|
||||
hostname: url.hostname,
|
||||
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
||||
path: url.pathname + url.search,
|
||||
method: options.method || 'GET',
|
||||
headers: options.headers || {},
|
||||
rejectUnauthorized: options.rejectUnauthorized !== false
|
||||
};
|
||||
|
||||
const req = transport.request(reqOptions, (res) => {
|
||||
const chunks = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => {
|
||||
resolve({
|
||||
statusCode: res.statusCode,
|
||||
headers: res.headers,
|
||||
body: Buffer.concat(chunks)
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
if (options.body) {
|
||||
req.write(options.body);
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function requestStream(options, writeBody) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = new URL(options.url);
|
||||
const transport = url.protocol === 'https:' ? https : http;
|
||||
const reqOptions = {
|
||||
hostname: url.hostname,
|
||||
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
||||
path: url.pathname + url.search,
|
||||
method: options.method || 'POST',
|
||||
headers: options.headers || {},
|
||||
rejectUnauthorized: options.rejectUnauthorized !== false
|
||||
};
|
||||
|
||||
const req = transport.request(reqOptions, (res) => {
|
||||
const chunks = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => {
|
||||
resolve({
|
||||
statusCode: res.statusCode,
|
||||
headers: res.headers,
|
||||
body: Buffer.concat(chunks)
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
if (writeBody) {
|
||||
writeBody(req);
|
||||
} else if (options.body) {
|
||||
req.write(options.body);
|
||||
req.end();
|
||||
} else {
|
||||
req.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
requestBuffer,
|
||||
requestStream
|
||||
};
|
||||
Reference in New Issue
Block a user