UNPKG

docker-decoder-stream

Version:

TS/JS decoder for Docker streams (logs and container attach)

2 lines 7.69 kB
var B=Object.create;var U=Object.defineProperty;var K=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var N=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var $=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports);var M=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of q(e))!W.call(n,s)&&s!==t&&U(n,s,{get:()=>e[s],enumerable:!(r=K(e,s))||r.enumerable});return n};var P=(n,e,t)=>(t=n!=null?B(N(n)):{},M(e||!n||!n.__esModule?U(t,"default",{value:n,enumerable:!0}):t,n));var E=$((G,x)=>{"use strict";var F=Object.prototype.hasOwnProperty,f="~";function m(){}Object.create&&(m.prototype=Object.create(null),new m().__proto__||(f=!1));function z(n,e,t){this.fn=n,this.context=e,this.once=t||!1}function _(n,e,t,r,s){if(typeof t!="function")throw new TypeError("The listener must be a function");var o=new z(t,r||n,s),a=f?f+e:e;return n._events[a]?n._events[a].fn?n._events[a]=[n._events[a],o]:n._events[a].push(o):(n._events[a]=o,n._eventsCount++),n}function v(n,e){--n._eventsCount===0?n._events=new m:delete n._events[e]}function u(){this._events=new m,this._eventsCount=0}u.prototype.eventNames=function(){var e=[],t,r;if(this._eventsCount===0)return e;for(r in t=this._events)F.call(t,r)&&e.push(f?r.slice(1):r);return Object.getOwnPropertySymbols?e.concat(Object.getOwnPropertySymbols(t)):e};u.prototype.listeners=function(e){var t=f?f+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var s=0,o=r.length,a=new Array(o);s<o;s++)a[s]=r[s].fn;return a};u.prototype.listenerCount=function(e){var t=f?f+e:e,r=this._events[t];return r?r.fn?1:r.length:0};u.prototype.emit=function(e,t,r,s,o,a){var h=f?f+e:e;if(!this._events[h])return!1;var i=this._events[h],l=arguments.length,c,d;if(i.fn){switch(i.once&&this.removeListener(e,i.fn,void 0,!0),l){case 1:return i.fn.call(i.context),!0;case 2:return i.fn.call(i.context,t),!0;case 3:return i.fn.call(i.context,t,r),!0;case 4:return i.fn.call(i.context,t,r,s),!0;case 5:return i.fn.call(i.context,t,r,s,o),!0;case 6:return i.fn.call(i.context,t,r,s,o,a),!0}for(d=1,c=new Array(l-1);d<l;d++)c[d-1]=arguments[d];i.fn.apply(i.context,c)}else{var k=i.length,p;for(d=0;d<k;d++)switch(i[d].once&&this.removeListener(e,i[d].fn,void 0,!0),l){case 1:i[d].fn.call(i[d].context);break;case 2:i[d].fn.call(i[d].context,t);break;case 3:i[d].fn.call(i[d].context,t,r);break;case 4:i[d].fn.call(i[d].context,t,r,s);break;default:if(!c)for(p=1,c=new Array(l-1);p<l;p++)c[p-1]=arguments[p];i[d].fn.apply(i[d].context,c)}}return!0};u.prototype.on=function(e,t,r){return _(this,e,t,r,!1)};u.prototype.once=function(e,t,r){return _(this,e,t,r,!0)};u.prototype.removeListener=function(e,t,r,s){var o=f?f+e:e;if(!this._events[o])return this;if(!t)return v(this,o),this;var a=this._events[o];if(a.fn)a.fn===t&&(!s||a.once)&&(!r||a.context===r)&&v(this,o);else{for(var h=0,i=[],l=a.length;h<l;h++)(a[h].fn!==t||s&&!a[h].once||r&&a[h].context!==r)&&i.push(a[h]);i.length?this._events[o]=i.length===1?i[0]:i:v(this,o)}return this};u.prototype.removeAllListeners=function(e){var t;return e?(t=f?f+e:e,this._events[t]&&v(this,t)):(this._events=new m,this._eventsCount=0),this};u.prototype.off=u.prototype.removeListener;u.prototype.addListener=u.prototype.on;u.prefixed=f;u.EventEmitter=u;typeof x<"u"&&(x.exports=u)});var R=P(E(),1);function b(n,e){if(!n)throw new T(e)}var T=class extends Error{name="AssertionError"};function O(n){switch(n[0]){case 0:return"stdin";case 1:return"stdout";case 2:return"stderr";default:throw new Error(`Incorrect docker frame type: ${n[0]}`)}}function I(n){return n[4]<<24|n[5]<<16|n[6]<<8|n[7]}function w(n){let e=n.reduce((s,o)=>s+o.length,0),t=new Uint8Array(e),r=0;for(let s of n)t.set(s,r),r+=s.length;return t}var y={header:0,payload:1},A=8,S=class n extends R.default{static defaultBufferSize=8192;#e=y.header;#t=0;#n=0;#i="stdout";#s=0;#r;get bufferLength(){return this.#r.length}constructor(e=n.defaultBufferSize){if(super(),!Number.isInteger(e))throw new TypeError("Buffer length must be an integer");if(e<=A)throw new RangeError(`Buffer length cannot be <= ${A}`);this.#r=new Uint8Array(e)}decode(e){let t={stdin:[],stdout:[],stderr:[]},r=(s,o)=>{!s||!o||t[s].push(o.slice())};return this.#a(e,r),this.#o(r),{stdin:w(t.stdin),stdout:w(t.stdout),stderr:w(t.stderr)}}push(e){try{this.#a(e,(t,r)=>this.emit("data",t,r))}catch(t){this.emit("error",t)}}close(){this.#o((e,t)=>this.emit("end",e,t))}#o(e){this.#e===y.payload?e(this.#i,this.#r.subarray(0,this.#t)):e(),this.#t=0,this.#n=0,this.#e=y.header}#a(e,t){for(;e.length;){let r=e.length;if(this.#e===y.header){let s=Math.min(A-this.#t,e.length);b(s>0,"Error during header chunk processing"),this.#r.set(e.subarray(0,s),this.#t),e=e.subarray(s),this.#t+=s,this.#t>=A&&(this.#i=O(this.#r),this.#s=I(this.#r),this.#t=0,this.#s&&(this.#e=y.payload))}if(this.#e===y.payload&&e.length){let s=Math.min(this.#s-this.#t,this.#r.length-this.#t,e.length);b(s>0,`DockerStreamDecoder have some data to read (${s} > 0)`),b(this.#t<this.#r.length,`Buffer has enough space to acommodate the data (${this.#t} < ${this.#r.length})`),this.#r.set(e.subarray(0,s),this.#t),e=e.subarray(s),this.#t+=s,this.#n+=s;let o=this.#n>=this.#s;if(o||this.#t>=this.#r.length){let a=this.#r.subarray(0,this.#t);this.#t=0,o&&(this.#e=y.header,this.#n=0),t(this.#i,a)}}b(r>e.length,`Data processed during DockerStreamDecoder iteration (${r} > ${e.length})`)}}};var g=class{#e=[];adopt(e,t){if(typeof t!="function")throw TypeError();return this.#e.push([e,t]),e}defer(e){if(typeof e!="function")throw TypeError();this.#e.push([void 0,e])}dispose(){for(;this.#e.length;){let[e,t]=this.#e.pop();t(e)}}};function D(n){return"byobRequest"in n}var L=class{constructor(e="stdout"){this.defaultStreamType=e;if(e!=="stdin"&&e!=="stdout"&&e!=="stderr")throw new TypeError("Incorrect stream type")}#e=new S;get readable(){switch(this.defaultStreamType){case"stdin":return this.stdin;case"stdout":return this.stdout;case"stderr":return this.stderr;default:throw new TypeError("Incorrect stream type")}}#t;get writable(){return this.#t??=this.#r(),this.#t}#n;get stdin(){return this.#n??=this.#o("stdin"),this.#n}#i;get stdout(){return this.#i??=this.#o("stdout"),this.#i}#s;get stderr(){return this.#s??=this.#o("stderr"),this.#s}#r(){let e=new g;return new WritableStream({start:t=>{this.#e.on("error",e.adopt(r=>{t.error(r)},r=>{this.#e.off("error",r)}))},write:t=>{this.#e.push(t)},close:()=>{this.#e.close(),e.dispose()},abort:()=>{this.#e.close(),e.dispose()}})}#o(e){let t=new g;return new ReadableStream({start:r=>{this.#e.on("data",t.adopt(s,()=>this.#e.off("data",s))).on("end",t.adopt((o,a)=>{s(o,a),r.close(),t.dispose()},o=>this.#e.off("end",o))).on("error",t.adopt(o=>{r.error(o)},o=>this.#e.off("error",o)));function s(o,a){if(o!==e||!a)return;if(!D(r)||!r.byobRequest)return r.enqueue(a.slice());let{view:h}=r.byobRequest;if(!h||h.byteLength-h.byteOffset<a.length)return r.byobRequest.respondWithNewView(a);new Uint8Array(h.buffer,h.byteOffset,h.byteLength).set(a),r.byobRequest.respond(a.length)}},cancel(){t.dispose()},type:"bytes",autoAllocateChunkSize:this.#e.bufferLength})}};async function*ue(n){let e=new Map(Object.keys(n).map(t=>{let r=t;return[r,C(n[r],r)]}));do{let{done:t,value:[r,s]}=await Promise.race(j(e.values()));t?e.delete(r):e.set(r,C(n[r],r)),s!==void 0&&(yield[r,s])}while(e.size)}function C(n,e){return n.read().then(({value:t,done:r})=>({done:r,value:[e,t]}))}function j(n){let e=Array.from(n),t=[];for(;e.length;){let r=Math.floor(Math.random()*e.length),[s]=e.splice(r,1);t.push(s)}return t}export{T as AssertionError,S as DockerDecoder,L as DockerDecoderStream,ue as mixDownReaders}; //# sourceMappingURL=index.mjs.map