now has possibility to end streaming requests

This commit is contained in:
Philipp Kunz 2016-07-31 21:05:41 +02:00
parent 56612f9ac9
commit 6a8751afd2
10 changed files with 143 additions and 93 deletions

View File

@ -43,6 +43,7 @@ release:
- priv
trigger:
image: hosttoday/ht-docker-node:npmts
stage: trigger
script:
- npmci trigger

View File

@ -4,6 +4,7 @@ import * as plugins from "./dockersock.plugins";
import { Observable } from "rxjs";
export declare class Dockersock {
sockPath: string;
requestObjectmap: plugins.lik.Objectmap;
constructor(pathArg?: string);
auth(userArg: string, passArg: string): plugins.q.Promise<{}>;
listContainers(): plugins.q.Promise<{}>;
@ -23,4 +24,5 @@ export declare class Dockersock {
getChangeObservable(): Observable<{}>;
request(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
requestStream(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
endRequests(): void;
}

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
import "typings-global";
export import beautylog = require("beautylog");
export import lik = require("lik");
export import q = require("q");
export import request = require("request");
export import rxjs = require("rxjs");

View File

@ -1,7 +1,8 @@
"use strict";
require("typings-global");
exports.beautylog = require("beautylog");
exports.lik = require("lik");
exports.q = require("q");
exports.request = require("request");
exports.rxjs = require("rxjs");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG9ja2Vyc29jay5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvZG9ja2Vyc29jay5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxRQUFPLGdCQUFnQixDQUFDLENBQUE7QUFDVixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDO0FBQ2pDLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUNqQixlQUFPLFdBQVcsU0FBUyxDQUFDLENBQUM7QUFDN0IsWUFBSSxXQUFXLE1BQU0sQ0FBQyxDQUFDIn0=
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG9ja2Vyc29jay5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvZG9ja2Vyc29jay5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxRQUFPLGdCQUFnQixDQUFDLENBQUE7QUFDVixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDO0FBQ2pDLFdBQUcsV0FBVyxLQUFLLENBQUMsQ0FBQztBQUNyQixTQUFDLFdBQVcsR0FBRyxDQUFDLENBQUM7QUFDakIsZUFBTyxXQUFXLFNBQVMsQ0FBQyxDQUFDO0FBQzdCLFlBQUksV0FBVyxNQUFNLENBQUMsQ0FBQyJ9

View File

@ -27,15 +27,16 @@
"dependencies": {
"@types/q": "^0.0.27",
"@types/request": "^0.0.27",
"beautylog": "^5.0.14",
"beautylog": "^5.0.18",
"lik": "^1.0.7",
"q": "^1.4.1",
"request": "^2.73.0",
"request": "^2.74.0",
"rxjs": "^5.0.0-beta.10",
"typings-global": "^1.0.6"
},
"devDependencies": {
"npmts-g": "^5.2.6",
"should": "^9.0.2",
"npmts-g": "^5.2.8",
"should": "^10.0.0",
"typings-test": "^1.0.1"
}
}

View File

@ -29,8 +29,16 @@ describe("dockersock", function () {
testDockersock.pullImage("hosttoday/ht-docker-dbase")
.then((dataArg) => {
done();
}, done);
});
});
it("should return a change Objservable", function (done) {
this.timeout(10000);
testDockersock.getChangeObservable();
setTimeout(() => {
testDockersock.endRequests();
done();
}, 5000);
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyx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();
},done);
});
});
it("should return a change Objservable",function(done){
this.timeout(10000);
testDockersock.getChangeObservable();
setTimeout(() => {
testDockersock.endRequests();
done();
},5000);
})
});
});

View File

@ -1,22 +1,23 @@
import "typings-global"
import * as plugins from "./dockersock.plugins";
import {Observable} from "rxjs";
import { Observable } from "rxjs";
export class Dockersock {
sockPath:string;
constructor(pathArg:string = "http://unix:/var/run/docker.sock:"){
sockPath: string;
requestObjectmap: plugins.lik.Objectmap = new plugins.lik.Objectmap();
constructor(pathArg: string = "http://unix:/var/run/docker.sock:") {
this.sockPath = pathArg;
}
// methods
auth(userArg:string,passArg:string){
auth(userArg: string, passArg: string) {
let done = plugins.q.defer();
this.request("POST","");
this.request("POST", "");
return done.promise;
}
listContainers() {
let done = plugins.q.defer();
this.request("GET","/containers")
this.request("GET", "/containers")
.then(done.resolve);
return done.promise;
};
@ -27,8 +28,8 @@ export class Dockersock {
.then((dataArg) => {
let recursiveCounter = 0;
let makeDetailed = () => {
if(typeof dataArg[recursiveCounter] != "undefined"){
this.request("GET","/containers/" + dataArg[recursiveCounter].Id)
if (typeof dataArg[recursiveCounter] != "undefined") {
this.request("GET", "/containers/" + dataArg[recursiveCounter].Id)
.then((dataArg2) => {
detailedDataObject.push(dataArg2);
recursiveCounter++;
@ -52,21 +53,21 @@ export class Dockersock {
return done.promise;
}
listImages() {
return this.request("GET","/images","?all=true");
return this.request("GET", "/images", "?all=true");
}
listImagesDangling(){
return this.request("GET","/images","?dangling=true");
listImagesDangling() {
return this.request("GET", "/images", "?dangling=true");
}
pullImage(imageLabelArg:string){
pullImage(imageLabelArg: string) {
let imageLabel = encodeURI(imageLabelArg);
return this.requestStream("POST","/images/create?fromImage=" + imageLabel);
return this.requestStream("POST", "/images/create?fromImage=" + imageLabel);
};
createContainer(optionsArg,pullFirstArg:boolean = true){
createContainer(optionsArg, pullFirstArg: boolean = true) {
let done = plugins.q.defer();
let create = () => {
return this.request("POST","/containers/create","",optionsArg);
return this.request("POST", "/containers/create", "", optionsArg);
}
if(pullFirstArg){
if (pullFirstArg) {
this.pullImage(optionsArg.Image)
.then(create)
.then(done.resolve);
@ -76,37 +77,37 @@ export class Dockersock {
}
return done.promise;
};
getContainerId(){
getContainerId() {
};
startContainer(containerNameArg){
return this.request("POST","/containers/"+ containerNameArg +"/start");
startContainer(containerNameArg) {
return this.request("POST", "/containers/" + containerNameArg + "/start");
};
stopContainer(containerNameArg){
return this.request("POST","/containers/"+ containerNameArg +"/stop");
stopContainer(containerNameArg) {
return this.request("POST", "/containers/" + containerNameArg + "/stop");
};
removeContainer(containerNameArg){
return this.request("DELETE","/containers/" + containerNameArg + "?v=1");
removeContainer(containerNameArg) {
return this.request("DELETE", "/containers/" + containerNameArg + "?v=1");
};
clean() {
let done = plugins.q.defer();
return done.promise;
};
callOnChange(cb:Function){
callOnChange(cb: Function) {
let cbPromise;
let changeBuffered:boolean = false; // when cb is running then buffer any consequent change
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) => {
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"){
if (typeof cbPromise == "undefined" || cbPromise.state == "pending") {
cbPromise = cb();
} else if (changeBuffered) {
changeBuffered = true;
@ -116,54 +117,56 @@ export class Dockersock {
});
}
});
requestStream.on("end",()=> {
});
requestStream.on("end", () => {
});
};
getChangeObservable(){
getChangeObservable() {
let options = {
method:"GET",
url:this.sockPath + "/events",
headers:{
"Content-Type":"application/json",
"Host":"docker.sock"
method: "GET",
url: this.sockPath + "/events",
headers: {
"Content-Type": "application/json",
"Host": "docker.sock"
}
};
let requestStream = plugins.request(options,(err, res, body) => {
let requestStream = plugins.request(options, (err, res, body) => {
if (!err && res.statusCode == 200) {
} else {
console.log(err);
console.log(res);
};
});
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);
}
});
let changeObservable = Observable.fromEvent(requestStream,"data");
requestStream.on("end",()=> {
requestStream.on("response", (response) => {
this.requestObjectmap.add(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);
}
});
let changeObservable = Observable.fromEvent(requestStream, "data");
requestStream.on("end", () => {
this.requestObjectmap.remove(requestStream);
});
return changeObservable;
}
request(methodArg:string,routeArg:string,queryArg:string = "", dataArg = {}){
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 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"
method: methodArg,
url: this.sockPath + routeArg + suffix + queryArg,
headers: {
"Content-Type": "application/json",
"Host": "docker.sock"
},
body:jsonArg
body: jsonArg
};
//console.log(options);
plugins.request(options,(err, res, body) => {
plugins.request(options, (err, res, body) => {
if (!err && res.statusCode == 200) {
var responseObj = JSON.parse(body);
done.resolve(responseObj);
@ -175,20 +178,20 @@ export class Dockersock {
});
return done.promise;
}
requestStream(methodArg:string,routeArg:string,queryArg:string = "", dataArg = {}){
requestStream(methodArg: string, routeArg: string, queryArg: string = "", dataArg = {}) {
let done = plugins.q.defer();
let jsonArg:string = JSON.stringify(dataArg);
let suffix:string = "";
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"
method: methodArg,
url: this.sockPath + routeArg + suffix + queryArg,
headers: {
"Content-Type": "application/json",
"Host": "docker.sock"
},
body:jsonArg
body: jsonArg
};
let requestStream = plugins.request(options,(err, res, body) => {
let requestStream = plugins.request(options, (err, res, body) => {
if (!err && res.statusCode == 200) {
done.resolve();
} else {
@ -197,19 +200,29 @@ export class Dockersock {
done.reject(err);
};
});
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);
done.reject(response);
}
});
requestStream.on("data",(data:Buffer) => {
requestStream.on("response", (response) => {
this.requestObjectmap.add(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);
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(requestStream);
});
return done.promise;
}
};
endRequests() {
this.requestObjectmap.forEach((itemArg: plugins.request.Request) => {
itemArg.emit("end");
});
this.requestObjectmap.wipe();
};
}

View File

@ -1,5 +1,6 @@
import "typings-global";
export import beautylog = require("beautylog");
export import lik = require("lik");
export import q = require("q");
export import request = require("request");
export import rxjs = require("rxjs");