update
This commit is contained in:
parent
91d7f583e2
commit
dd35e5d7d2
20
dist/dockersock.classes.dockersock.d.ts
vendored
20
dist/dockersock.classes.dockersock.d.ts
vendored
@ -1,4 +1,3 @@
|
|||||||
/// <reference types="request" />
|
|
||||||
/// <reference types="q" />
|
/// <reference types="q" />
|
||||||
import "typings-global";
|
import "typings-global";
|
||||||
import * as plugins from "./dockersock.plugins";
|
import * as plugins from "./dockersock.plugins";
|
||||||
@ -6,7 +5,7 @@ import { Observable } from "rxjs";
|
|||||||
import { Objectmap } from 'lik';
|
import { Objectmap } from 'lik';
|
||||||
export declare class Dockersock {
|
export declare class Dockersock {
|
||||||
sockPath: string;
|
sockPath: string;
|
||||||
requestObjectmap: Objectmap<plugins.request.Request>;
|
requestObjectmap: Objectmap<any>;
|
||||||
constructor(pathArg?: string);
|
constructor(pathArg?: string);
|
||||||
auth(userArg: string, passArg: string): plugins.q.Promise<{}>;
|
auth(userArg: string, passArg: string): plugins.q.Promise<{}>;
|
||||||
listContainers(): plugins.q.Promise<{}>;
|
listContainers(): plugins.q.Promise<{}>;
|
||||||
@ -27,7 +26,24 @@ export declare class Dockersock {
|
|||||||
* gets you an observable that reports changes in the docker infrastructure
|
* gets you an observable that reports changes in the docker infrastructure
|
||||||
*/
|
*/
|
||||||
getChangeObservable(): Observable<{}>;
|
getChangeObservable(): Observable<{}>;
|
||||||
|
/**
|
||||||
|
* fire a request
|
||||||
|
* @param methodArg
|
||||||
|
* @param routeArg
|
||||||
|
* @param queryArg
|
||||||
|
* @param dataArg
|
||||||
|
*/
|
||||||
request(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
|
request(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
|
||||||
|
/**
|
||||||
|
* fire a streaming request
|
||||||
|
* @param methodArg
|
||||||
|
* @param routeArg
|
||||||
|
* @param queryArg
|
||||||
|
* @param dataArg
|
||||||
|
*/
|
||||||
requestStream(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
|
requestStream(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
|
||||||
|
/**
|
||||||
|
* end all currently streaming requests
|
||||||
|
*/
|
||||||
endRequests(): void;
|
endRequests(): void;
|
||||||
}
|
}
|
||||||
|
19
dist/dockersock.classes.dockersock.js
vendored
19
dist/dockersock.classes.dockersock.js
vendored
File diff suppressed because one or more lines are too long
@ -6,7 +6,7 @@
|
|||||||
"npmdocker":{
|
"npmdocker":{
|
||||||
"baseImage":"hosttoday/ht-docker-node:npmci",
|
"baseImage":"hosttoday/ht-docker-node:npmci",
|
||||||
"command":"(npmci command yarn global add npmts) && npmts",
|
"command":"(npmci command yarn global add npmts) && npmts",
|
||||||
"dockerSock":"true"
|
"dockerSock": true
|
||||||
},
|
},
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"globalNpmTools": [
|
"globalNpmTools": [
|
||||||
|
13
package.json
13
package.json
@ -5,7 +5,7 @@
|
|||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "npmdocker"
|
"test": "(npmts --notest && npmdocker)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -26,16 +26,15 @@
|
|||||||
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/q": "1.x.x",
|
"@types/q": "1.x.x",
|
||||||
"@types/request": "0.x.x",
|
"beautylog": "^6.1.10",
|
||||||
"beautylog": "^6.1.5",
|
"lik": "^1.0.38",
|
||||||
"lik": "^1.0.27",
|
|
||||||
"q": "^1.5.0",
|
"q": "^1.5.0",
|
||||||
"request": "^2.81.0",
|
"request": "^2.81.0",
|
||||||
"rxjs": "^5.2.0",
|
"rxjs": "^5.4.2",
|
||||||
"typings-global": "^1.0.14"
|
"typings-global": "^1.0.19"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"observable-to-promise": "^0.5.0",
|
"observable-to-promise": "^0.5.0",
|
||||||
"tapbundle": "^1.0.5"
|
"tapbundle": "^1.1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
test/test.ts
26
test/test.ts
@ -8,29 +8,27 @@ let testDockersock: Dockersock;
|
|||||||
tap.test("should create a new Dockersock instance", async () => {
|
tap.test("should create a new Dockersock instance", async () => {
|
||||||
testDockersock = new Dockersock();
|
testDockersock = new Dockersock();
|
||||||
return expect(testDockersock).to.be.instanceof(Dockersock);
|
return expect(testDockersock).to.be.instanceof(Dockersock);
|
||||||
}).catch(tap.threw);
|
})
|
||||||
|
|
||||||
tap.test("should list containers", async () => {
|
tap.test("should list containers", async () => {
|
||||||
await testDockersock.listContainers()
|
let data = await testDockersock.listContainers()
|
||||||
.then(async (dataArg) => {
|
console.log(data)
|
||||||
console.log(dataArg);
|
})
|
||||||
});
|
|
||||||
}).catch(tap.threw);
|
|
||||||
|
|
||||||
tap.test("should list detailed containers", async () => {
|
tap.test("should list detailed containers", async () => {
|
||||||
await testDockersock.listContainersDetailed()
|
let data = await testDockersock.listContainersDetailed()
|
||||||
.then(async (dataArg) => {
|
console.log(data)
|
||||||
console.log(dataArg);
|
})
|
||||||
});
|
|
||||||
}).catch(tap.threw);
|
|
||||||
|
|
||||||
tap.test("should pull an image from imagetag", async () => {
|
tap.test("should pull an image from imagetag", async () => {
|
||||||
await testDockersock.pullImage("hosttoday/ht-docker-node:npmci")
|
await testDockersock.pullImage("hosttoday/ht-docker-node:npmci")
|
||||||
}).catch(tap.threw);
|
})
|
||||||
|
|
||||||
/*tap.test("should return a change Objservable", async () => {
|
tap.test("should return a change Objservable", async () => {
|
||||||
let myObservable = testDockersock.getChangeObservable();
|
let myObservable = testDockersock.getChangeObservable();
|
||||||
testDockersock.endRequests();
|
testDockersock.endRequests();
|
||||||
let testPromise = observableToPromise(myObservable)
|
let testPromise = observableToPromise(myObservable)
|
||||||
return await expect(testPromise).to.eventually.be.fulfilled
|
return await expect(testPromise).to.eventually.be.fulfilled
|
||||||
}).catch(tap.threw);*/
|
})
|
||||||
|
|
||||||
|
tap.start()
|
||||||
|
@ -3,241 +3,260 @@ import * as plugins from "./dockersock.plugins";
|
|||||||
import { Observable } from "rxjs";
|
import { Observable } from "rxjs";
|
||||||
|
|
||||||
// interfaces
|
// interfaces
|
||||||
import { Objectmap } from 'lik'
|
import { Objectmap } from 'lik'
|
||||||
|
|
||||||
export class Dockersock {
|
export class Dockersock {
|
||||||
sockPath: string;
|
sockPath: string;
|
||||||
requestObjectmap = new plugins.lik.Objectmap<plugins.request.Request>();
|
requestObjectmap = new plugins.lik.Objectmap<plugins.request.Request>();
|
||||||
constructor(pathArg: string = "http://unix:/var/run/docker.sock:") {
|
constructor(pathArg: string = "http://unix:/var/run/docker.sock:") {
|
||||||
this.sockPath = pathArg;
|
this.sockPath = pathArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
auth(userArg: string, passArg: string) {
|
auth (userArg: string, passArg: string) {
|
||||||
let done = plugins.q.defer();
|
let done = plugins.q.defer();
|
||||||
this.request("POST", "");
|
this.request("POST", "");
|
||||||
return done.promise;
|
return done.promise;
|
||||||
}
|
}
|
||||||
listContainers() {
|
listContainers () {
|
||||||
let done = plugins.q.defer();
|
let done = plugins.q.defer();
|
||||||
this.request("GET", "/containers")
|
this.request("GET", "/containers")
|
||||||
.then(done.resolve);
|
.then(done.resolve);
|
||||||
return done.promise;
|
return done.promise;
|
||||||
};
|
};
|
||||||
listContainersDetailed() {
|
listContainersDetailed () {
|
||||||
let done = plugins.q.defer();
|
let done = plugins.q.defer();
|
||||||
let detailedDataObject = [];
|
let detailedDataObject = [];
|
||||||
this.listContainers()
|
this.listContainers()
|
||||||
.then((dataArg) => {
|
.then((dataArg) => {
|
||||||
let recursiveCounter = 0;
|
let recursiveCounter = 0;
|
||||||
let makeDetailed = () => {
|
let makeDetailed = () => {
|
||||||
if (typeof dataArg[recursiveCounter] != "undefined") {
|
if (typeof dataArg[ recursiveCounter ] != "undefined") {
|
||||||
this.request("GET", "/containers/" + dataArg[recursiveCounter].Id)
|
this.request("GET", "/containers/" + dataArg[ recursiveCounter ].Id)
|
||||||
.then((dataArg2) => {
|
.then((dataArg2) => {
|
||||||
detailedDataObject.push(dataArg2);
|
detailedDataObject.push(dataArg2);
|
||||||
recursiveCounter++;
|
recursiveCounter++;
|
||||||
// recursive call
|
// recursive call
|
||||||
makeDetailed();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
done.resolve(detailedDataObject);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
makeDetailed();
|
makeDetailed();
|
||||||
});
|
});
|
||||||
return done.promise;
|
} else {
|
||||||
};
|
done.resolve(detailedDataObject);
|
||||||
listContainersRunning() {
|
}
|
||||||
let done = plugins.q.defer();
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
listContainersStopped() {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
listImages() {
|
|
||||||
return this.request("GET", "/images", "?all=true");
|
|
||||||
}
|
|
||||||
listImagesDangling() {
|
|
||||||
return this.request("GET", "/images", "?dangling=true");
|
|
||||||
}
|
|
||||||
pullImage(imageLabelArg: string) {
|
|
||||||
let imageLabel = encodeURI(imageLabelArg);
|
|
||||||
return this.requestStream("POST", "/images/create?fromImage=" + imageLabel);
|
|
||||||
};
|
|
||||||
createContainer(optionsArg, pullFirstArg: boolean = true) {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
let create = () => {
|
|
||||||
return this.request("POST", "/containers/create", "", optionsArg);
|
|
||||||
}
|
|
||||||
if (pullFirstArg) {
|
|
||||||
this.pullImage(optionsArg.Image)
|
|
||||||
.then(create)
|
|
||||||
.then(done.resolve);
|
|
||||||
} else {
|
|
||||||
create()
|
|
||||||
.then(done.resolve)
|
|
||||||
}
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
||||||
getContainerId() {
|
|
||||||
|
|
||||||
};
|
|
||||||
startContainer(containerNameArg) {
|
|
||||||
return this.request("POST", "/containers/" + containerNameArg + "/start");
|
|
||||||
};
|
|
||||||
stopContainer(containerNameArg) {
|
|
||||||
return this.request("POST", "/containers/" + containerNameArg + "/stop");
|
|
||||||
};
|
|
||||||
removeContainer(containerNameArg) {
|
|
||||||
return this.request("DELETE", "/containers/" + containerNameArg + "?v=1");
|
|
||||||
};
|
|
||||||
clean() {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
||||||
callOnChange(cb: Function) {
|
|
||||||
let cbPromise;
|
|
||||||
let changeBuffered: boolean = false; // when cb is running then buffer any consequent change
|
|
||||||
let requestStream = plugins.request.get(this.sockPath + "/events");
|
|
||||||
requestStream.on("response", (response) => {
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
plugins.beautylog.ok("request returned status 200, so we are good!");
|
|
||||||
} else {
|
|
||||||
plugins.beautylog.error("request returned error: " + response.statusCode);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
requestStream.on("data", (data: Buffer) => {
|
|
||||||
let status = JSON.parse(data.toString()).status;
|
|
||||||
plugins.beautylog.logReduced(status);
|
|
||||||
if (typeof cbPromise == "undefined" || cbPromise.state == "pending") {
|
|
||||||
cbPromise = cb();
|
|
||||||
} else if (changeBuffered) {
|
|
||||||
changeBuffered = true;
|
|
||||||
cbPromise.then(() => {
|
|
||||||
changeBuffered = false;
|
|
||||||
cbPromise = cb();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
requestStream.on("end", () => {
|
|
||||||
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* gets you an observable that reports changes in the docker infrastructure
|
|
||||||
*/
|
|
||||||
getChangeObservable() {
|
|
||||||
let options = {
|
|
||||||
method: "GET",
|
|
||||||
url: this.sockPath + "/events",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Host": "docker.sock"
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let requestStream = plugins.request(options, (err, res, body) => {
|
makeDetailed();
|
||||||
if (!err && res.statusCode == 200) {
|
});
|
||||||
} else {
|
return done.promise;
|
||||||
console.log(err);
|
};
|
||||||
console.log(res);
|
listContainersRunning () {
|
||||||
};
|
let done = plugins.q.defer();
|
||||||
});
|
return done.promise;
|
||||||
let incomingMessage
|
}
|
||||||
requestStream.on("response", (response) => {
|
listContainersStopped () {
|
||||||
incomingMessage = response
|
let done = plugins.q.defer();
|
||||||
this.requestObjectmap.add(incomingMessage);
|
return done.promise;
|
||||||
if (response.statusCode == 200) {
|
}
|
||||||
plugins.beautylog.ok("request returned status 200, so we are good!");
|
listImages () {
|
||||||
} else {
|
return this.request("GET", "/images", "?all=true");
|
||||||
plugins.beautylog.error("request returned error: " + response.statusCode);
|
}
|
||||||
}
|
listImagesDangling () {
|
||||||
});
|
return this.request("GET", "/images", "?dangling=true");
|
||||||
let changeObservable = Observable.fromEvent(requestStream, "data");
|
}
|
||||||
requestStream.on("end", () => {
|
pullImage (imageLabelArg: string) {
|
||||||
this.requestObjectmap.remove(incomingMessage);
|
let imageLabel = encodeURI(imageLabelArg);
|
||||||
});
|
return this.requestStream("POST", "/images/create?fromImage=" + imageLabel);
|
||||||
return changeObservable;
|
};
|
||||||
|
createContainer (optionsArg, pullFirstArg: boolean = true) {
|
||||||
|
let done = plugins.q.defer();
|
||||||
|
let create = () => {
|
||||||
|
return this.request("POST", "/containers/create", "", optionsArg);
|
||||||
}
|
}
|
||||||
request(methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
if (pullFirstArg) {
|
||||||
let done = plugins.q.defer();
|
this.pullImage(optionsArg.Image)
|
||||||
let jsonArg: string = JSON.stringify(dataArg);
|
.then(create)
|
||||||
let suffix: string = "";
|
.then(done.resolve);
|
||||||
if (methodArg == "GET") suffix = "/json";
|
} else {
|
||||||
let options = {
|
create()
|
||||||
method: methodArg,
|
.then(done.resolve)
|
||||||
url: this.sockPath + routeArg + suffix + queryArg,
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Host": "docker.sock"
|
|
||||||
},
|
|
||||||
body: jsonArg
|
|
||||||
};
|
|
||||||
//console.log(options);
|
|
||||||
plugins.request(options, (err, res, body) => {
|
|
||||||
if (!err && res.statusCode == 200) {
|
|
||||||
var responseObj = JSON.parse(body);
|
|
||||||
done.resolve(responseObj);
|
|
||||||
} else {
|
|
||||||
console.log(err);
|
|
||||||
console.log(res);
|
|
||||||
done.reject(err);
|
|
||||||
};
|
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
}
|
}
|
||||||
requestStream(methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
return done.promise;
|
||||||
let done = plugins.q.defer();
|
};
|
||||||
let jsonArg: string = JSON.stringify(dataArg);
|
getContainerId () {
|
||||||
let suffix: string = "";
|
|
||||||
let options = {
|
};
|
||||||
method: methodArg,
|
startContainer (containerNameArg) {
|
||||||
url: this.sockPath + routeArg + suffix + queryArg,
|
return this.request("POST", "/containers/" + containerNameArg + "/start");
|
||||||
headers: {
|
};
|
||||||
"Content-Type": "application/json",
|
stopContainer (containerNameArg) {
|
||||||
"Host": "docker.sock"
|
return this.request("POST", "/containers/" + containerNameArg + "/stop");
|
||||||
},
|
};
|
||||||
body: jsonArg
|
removeContainer (containerNameArg) {
|
||||||
};
|
return this.request("DELETE", "/containers/" + containerNameArg + "?v=1");
|
||||||
let requestStream = plugins.request(options, (err, res, body) => {
|
};
|
||||||
if (!err && res.statusCode == 200) {
|
clean () {
|
||||||
done.resolve();
|
let done = plugins.q.defer();
|
||||||
} else {
|
return done.promise;
|
||||||
console.log(err);
|
};
|
||||||
console.log(res);
|
callOnChange (cb: Function) {
|
||||||
done.reject(err);
|
let cbPromise;
|
||||||
};
|
let changeBuffered: boolean = false; // when cb is running then buffer any consequent change
|
||||||
|
let requestStream = plugins.request.get(this.sockPath + "/events");
|
||||||
|
requestStream.on("response", (response) => {
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
plugins.beautylog.ok("request returned status 200, so we are good!");
|
||||||
|
} else {
|
||||||
|
plugins.beautylog.error("request returned error: " + response.statusCode);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
requestStream.on("data", (data: Buffer) => {
|
||||||
|
let status = JSON.parse(data.toString()).status;
|
||||||
|
plugins.beautylog.logReduced(status);
|
||||||
|
if (typeof cbPromise == "undefined" || cbPromise.state == "pending") {
|
||||||
|
cbPromise = cb();
|
||||||
|
} else if (changeBuffered) {
|
||||||
|
changeBuffered = true;
|
||||||
|
cbPromise.then(() => {
|
||||||
|
changeBuffered = false;
|
||||||
|
cbPromise = cb();
|
||||||
});
|
});
|
||||||
let incomingMessage
|
}
|
||||||
requestStream.on("response", (response) => {
|
});
|
||||||
incomingMessage = response
|
requestStream.on("end", () => {
|
||||||
this.requestObjectmap.add(incomingMessage);
|
|
||||||
if (response.statusCode == 200) {
|
});
|
||||||
plugins.beautylog.ok("request returned status 200, so we are good!");
|
};
|
||||||
} else {
|
|
||||||
plugins.beautylog.error("request returned error: " + response.statusCode);
|
/**
|
||||||
done.reject(response);
|
* gets you an observable that reports changes in the docker infrastructure
|
||||||
}
|
*/
|
||||||
});
|
getChangeObservable () {
|
||||||
requestStream.on("data", (data: Buffer) => {
|
let options = {
|
||||||
let status;
|
method: "GET",
|
||||||
status = JSON.parse(data.toString()).status;
|
url: this.sockPath + "/events",
|
||||||
plugins.beautylog.logReduced(status);
|
headers: {
|
||||||
});
|
"Content-Type": "application/json",
|
||||||
requestStream.on("end", () => {
|
"Host": "docker.sock"
|
||||||
this.requestObjectmap.remove(incomingMessage);
|
}
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
};
|
};
|
||||||
|
let requestStream = plugins.request(options, (err, res, body) => {
|
||||||
|
if (!err && res.statusCode == 200) {
|
||||||
|
} else {
|
||||||
|
console.log(err);
|
||||||
|
console.log(res);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
let incomingMessage
|
||||||
|
requestStream.on("response", (response) => {
|
||||||
|
incomingMessage = response
|
||||||
|
this.requestObjectmap.add(incomingMessage);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
plugins.beautylog.ok("request returned status 200, so we are good!");
|
||||||
|
} else {
|
||||||
|
plugins.beautylog.error("request returned error: " + response.statusCode);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let changeObservable = Observable.fromEvent(requestStream, "data");
|
||||||
|
requestStream.on("end", () => {
|
||||||
|
this.requestObjectmap.remove(incomingMessage);
|
||||||
|
});
|
||||||
|
return changeObservable;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
endRequests() {
|
/**
|
||||||
setTimeout(() => {
|
* fire a request
|
||||||
this.requestObjectmap.forEach((itemArg: plugins.request.Request) => {
|
* @param methodArg
|
||||||
itemArg.emit("end");
|
* @param routeArg
|
||||||
});
|
* @param queryArg
|
||||||
this.requestObjectmap.wipe();
|
* @param dataArg
|
||||||
}, 5000);
|
*/
|
||||||
|
request (methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
||||||
|
let done = plugins.q.defer();
|
||||||
|
let jsonArg: string = JSON.stringify(dataArg);
|
||||||
|
let suffix: string = "";
|
||||||
|
if (methodArg == "GET") suffix = "/json";
|
||||||
|
let options = {
|
||||||
|
method: methodArg,
|
||||||
|
url: this.sockPath + routeArg + suffix + queryArg,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Host": "docker.sock"
|
||||||
|
},
|
||||||
|
body: jsonArg
|
||||||
};
|
};
|
||||||
|
//console.log(options);
|
||||||
|
plugins.request(options, (err, res, body) => {
|
||||||
|
if (!err && res.statusCode == 200) {
|
||||||
|
var responseObj = JSON.parse(body);
|
||||||
|
done.resolve(responseObj);
|
||||||
|
} else {
|
||||||
|
console.log(err);
|
||||||
|
console.log(res);
|
||||||
|
done.reject(err);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fire a streaming request
|
||||||
|
* @param methodArg
|
||||||
|
* @param routeArg
|
||||||
|
* @param queryArg
|
||||||
|
* @param dataArg
|
||||||
|
*/
|
||||||
|
requestStream (methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
|
||||||
|
let done = plugins.q.defer();
|
||||||
|
let jsonArg: string = JSON.stringify(dataArg);
|
||||||
|
let suffix: string = "";
|
||||||
|
let options = {
|
||||||
|
method: methodArg,
|
||||||
|
url: this.sockPath + routeArg + suffix + queryArg,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Host": "docker.sock"
|
||||||
|
},
|
||||||
|
body: jsonArg
|
||||||
|
};
|
||||||
|
let requestStream = plugins.request(options, (err, res, body) => {
|
||||||
|
if (!err && res.statusCode == 200) {
|
||||||
|
done.resolve();
|
||||||
|
} else {
|
||||||
|
console.log(err);
|
||||||
|
console.log(res);
|
||||||
|
done.reject(err);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
let incomingMessage
|
||||||
|
requestStream.on("response", (response) => {
|
||||||
|
incomingMessage = response
|
||||||
|
this.requestObjectmap.add(incomingMessage);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
plugins.beautylog.ok("request returned status 200, so we are good!");
|
||||||
|
} else {
|
||||||
|
plugins.beautylog.error("request returned error: " + response.statusCode);
|
||||||
|
done.reject(response);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
requestStream.on("data", (data: Buffer) => {
|
||||||
|
let status;
|
||||||
|
status = JSON.parse(data.toString()).status;
|
||||||
|
plugins.beautylog.logReduced(status);
|
||||||
|
});
|
||||||
|
requestStream.on("end", () => {
|
||||||
|
this.requestObjectmap.remove(incomingMessage);
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* end all currently streaming requests
|
||||||
|
*/
|
||||||
|
endRequests () {
|
||||||
|
setTimeout(() => {
|
||||||
|
this.requestObjectmap.forEach((itemArg: plugins.request.Request) => {
|
||||||
|
itemArg.emit("end");
|
||||||
|
});
|
||||||
|
this.requestObjectmap.wipe();
|
||||||
|
}, 5000);
|
||||||
|
};
|
||||||
}
|
}
|
@ -3,4 +3,4 @@ export import beautylog = require("beautylog");
|
|||||||
export import lik = require("lik");
|
export import lik = require("lik");
|
||||||
export import q = require("q");
|
export import q = require("q");
|
||||||
export import request = require("request");
|
export import request = require("request");
|
||||||
export import rxjs = require("rxjs");
|
export import rxjs = require("rxjs");
|
||||||
|
Loading…
Reference in New Issue
Block a user