fix(core): update

This commit is contained in:
Philipp Kunz 2018-07-16 23:39:25 +02:00
parent d77d2b13da
commit b4a9d1aa0c
3 changed files with 93 additions and 63 deletions

View File

@ -23,7 +23,10 @@ tap.test('should post a JSON document over http', async () => {
}); });
tap.test('should deal with unix socks', async () => { tap.test('should deal with unix socks', async () => {
await expect(smartrequest.request('http://unix:/var/run/docker.sock:/containers')) const socketResponse = await smartrequest.request('http://unix:/var/run/docker.sock:/containers');
console.log(socketResponse.body);
}); });
tap.start(); tap.start({
throwOnError: true
});

View File

@ -1,4 +1,4 @@
export { request, extendedIncomingMessage } from './smartrequest.request'; export { request, IExtendedIncomingMessage } from './smartrequest.request';
export { ISmartRequestOptions } from './smartrequest.interfaces'; export { ISmartRequestOptions } from './smartrequest.interfaces';
export * from './smartrequest.jsonrest'; export * from './smartrequest.jsonrest';

View File

@ -1,85 +1,112 @@
import * as https from 'https'; import * as https from "https";
import * as plugins from './smartrequest.plugins'; import * as plugins from "./smartrequest.plugins";
import * as interfaces from './smartrequest.interfaces'; import * as interfaces from "./smartrequest.interfaces";
import { IncomingMessage } from 'http'; import { IncomingMessage } from "http";
export interface extendedIncomingMessage extends IncomingMessage { export interface IExtendedIncomingMessage extends IncomingMessage {
body: any; body: any;
} }
let buildResponse = (incomingMessageArg: IncomingMessage): Promise<extendedIncomingMessage> => { const buildUtf8Response = (
let done = plugins.smartpromise.defer<extendedIncomingMessage>(); incomingMessageArg: IncomingMessage
): Promise<IExtendedIncomingMessage> => {
let done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
// Continuously update stream with data // Continuously update stream with data
let body = ''; let body = "";
incomingMessageArg.on('data', function(chunkArg) { incomingMessageArg.on("data", function(chunkArg) {
body += chunkArg; body += chunkArg;
}); });
incomingMessageArg.on('end', function() { incomingMessageArg.on("end", function() {
try { try {
(incomingMessageArg as extendedIncomingMessage).body = JSON.parse(body); (incomingMessageArg as IExtendedIncomingMessage).body = JSON.parse(body);
} catch (err) { } catch (err) {
(incomingMessageArg as extendedIncomingMessage).body = body; (incomingMessageArg as IExtendedIncomingMessage).body = body;
} }
done.resolve(incomingMessageArg as extendedIncomingMessage); done.resolve(incomingMessageArg as IExtendedIncomingMessage);
}); });
return done.promise; return done.promise;
}; };
/**
* determine wether a url is a unix sock
* @param urlArg
*/
const testForUnixSock = (urlArg: string): boolean => {
const unixRegex = /^(http:\/\/|https:\/\/|)unix:/;
return unixRegex.test(urlArg);
};
/**
* determine socketPath and path for unixsock
*/
const parseSocketPathAndRoute = (stringToParseArg: string) => {
const parseRegex = /(.*):(.*)/;
const result = parseRegex.exec(stringToParseArg);
return {
socketPath: result[1],
path: result[2]
}
}
export let request = async ( export let request = async (
domainArg: string, domainArg: string,
optionsArg: interfaces.ISmartRequestOptions = {}, optionsArg: interfaces.ISmartRequestOptions = {},
streamArg: boolean = false streamArg: boolean = false
): Promise<extendedIncomingMessage> => { ): Promise<IExtendedIncomingMessage> => {
let done = plugins.smartpromise.defer<any>(); let done = plugins.smartpromise.defer<any>();
// parse url
let parsedUrl: plugins.url.Url; let parsedUrl: plugins.url.Url;
if (domainArg) {
parsedUrl = plugins.url.parse(domainArg); parsedUrl = plugins.url.parse(domainArg);
optionsArg.hostname = parsedUrl.hostname; optionsArg.hostname = parsedUrl.hostname;
if (parsedUrl.port) { if (parsedUrl.port) {
optionsArg.port = parseInt(parsedUrl.port); optionsArg.port = parseInt(parsedUrl.port);
} }
optionsArg.path = parsedUrl.path; optionsArg.path = parsedUrl.path;
// determine if unixsock
if(testForUnixSock(domainArg)) {
const detailedUnixPath = parseSocketPathAndRoute(optionsArg.path)
optionsArg.socketPath = detailedUnixPath.socketPath;
optionsArg.path = detailedUnixPath.path;
} }
if (!parsedUrl || parsedUrl.protocol === 'https:') {
let request = plugins.https.request(optionsArg, response => { // lets determine the request module to use
if (streamArg) { const requestModule = (() => {
done.resolve(response); if (parsedUrl.protocol === "https:") {
} else { return plugins.https;
buildResponse(response).then(done.resolve); } else if (parsedUrl.protocol === "http:") {
} return plugins.http;
});
if (optionsArg.requestBody) {
if (typeof optionsArg.requestBody !== 'string') {
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
}
request.write(optionsArg.requestBody);
}
request.on('error', e => {
console.error(e);
});
request.end();
} else if (parsedUrl.protocol === 'http:') {
let request = plugins.http.request(optionsArg, response => {
if (streamArg) {
done.resolve(response);
} else {
buildResponse(response).then(done.resolve);
}
});
if (optionsArg.requestBody) {
if (typeof optionsArg.requestBody !== 'string') {
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
}
request.write(optionsArg.requestBody);
}
request.on('error', e => {
console.error(e);
});
request.end();
} else { } else {
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`); throw new Error(`unsupported protocol: ${parsedUrl.protocol}`);
} }
return done.promise; })() as typeof plugins.https;
// lets perform the actual request
let request = requestModule.request(optionsArg, async response => {
if (streamArg) {
done.resolve(response);
} else {
const builtResponse = await buildUtf8Response(response)
done.resolve(builtResponse);
}
});
// lets write the requestBody
if (optionsArg.requestBody) {
if (typeof optionsArg.requestBody !== "string") {
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
}
request.write(optionsArg.requestBody);
}
request.on("error", e => {
console.error(e);
});
request.end();
const result = await done.promise;
return result;
}; };