@influxdata/influxdb-client 1.19.0 → 1.20.0-nightly.1552

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -12,5 +12,5 @@ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12
12
  LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13
13
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14
14
  PERFORMANCE OF THIS SOFTWARE.
15
- ***************************************************************************** */function pt(t,e,r,n){return new(r||(r=Promise))((function(s,i){function o(t){try{a(n.next(t))}catch(t){i(t)}}function l(t){try{a(n.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?s(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(o,l)}a((n=n.apply(t,e||[])).next())}))}class gt{constructor(t){this.options=Object.assign(Object.assign({},j),t),this.success()}nextDelay(t,e){const r=y(t);if(r&&r>0)return r+Math.round(Math.random()*this.options.retryJitter);if(e&&e>0){if(this.options.randomRetry){let t=Math.max(this.options.minRetryDelay,1),r=t*this.options.exponentialBase;for(let n=1;n<e;n++)if(t=r,r*=this.options.exponentialBase,r>=this.options.maxRetryDelay){r=this.options.maxRetryDelay;break}return t+Math.round(Math.random()*(r-t)+Math.random()*this.options.retryJitter)}let t=Math.max(this.options.minRetryDelay,1);for(let r=1;r<e;r++)if(t*=this.options.exponentialBase,t>=this.options.maxRetryDelay){t=this.options.maxRetryDelay;break}return t+Math.round(Math.random()*this.options.retryJitter)}return this.currentDelay?this.currentDelay=Math.min(Math.max(this.currentDelay*this.options.exponentialBase,1)+Math.round(Math.random()*this.options.retryJitter),this.options.maxRetryDelay):this.currentDelay=this.options.minRetryDelay+Math.round(Math.random()*this.options.retryJitter),this.currentDelay}success(){this.currentDelay=void 0}}class mt{constructor(t,e){this.maxLines=t,this.retryLines=e,this.size=0,this.nextRetryTime=0,this.closed=!1,this._timeoutHandle=void 0}addLines(t,e,r,n){if(this.closed)return;if(!t.length)return;let s=Date.now()+r;if(n<s&&(r=n-Date.now(),s=n),s>this.nextRetryTime&&(this.nextRetryTime=s),this.first&&this.size+t.length>this.maxLines){const e=this.size,r=.7*e;do{const t=this.first.next;this.size-=this.first.lines.length,this.first.next=void 0,this.first=t,this.first||(this.last=void 0)}while(this.first&&this.size+t.length>r);G.error(`RetryBuffer: ${e-this.size} oldest lines removed to keep buffer size under the limit of ${this.maxLines} lines`)}const i={lines:t,retryCount:e,expires:n};this.last?(this.last.next=i,this.last=i):(this.first=i,this.last=i,this.scheduleRetry(r)),this.size+=t.length}removeLines(){if(this.first){const t=this.first;return this.first=this.first.next,t.next=void 0,this.size-=t.lines.length,this.first||(this.last=void 0),t}}scheduleRetry(t){this._timeoutHandle=setTimeout(()=>{const t=this.removeLines();t?this.retryLines(t.lines,t.retryCount,t.expires).then(()=>{this.scheduleRetry(1)}).catch(t=>{this.scheduleRetry(this.nextRetryTime-Date.now())}):this._timeoutHandle=void 0},Math.max(t,0))}flush(){return pt(this,void 0,void 0,(function*(){let t;for(;t=this.removeLines();)yield this.retryLines(t.lines,t.retryCount,t.expires)}))}close(){return this._timeoutHandle&&(clearTimeout(this._timeoutHandle),this._timeoutHandle=void 0),this.closed=!0,this.size}}class yt{constructor(t,e,r){this.maxChunkRecords=t,this.flushFn=e,this.scheduleSend=r,this.length=0,this.lines=new Array(t)}add(t){0===this.length&&this.scheduleSend(),this.lines[this.length]=t,this.length++,this.length>=this.maxChunkRecords&&this.flush().catch(t=>{})}flush(){const t=this.reset();return t.length>0?this.flushFn(t):Promise.resolve()}reset(){const t=this.lines.slice(0,this.length);return this.length=0,t}}class wt{constructor(t,e,r,n,s){this.transport=t,this.closed=!1,this._timeoutHandle=void 0,this.httpPath=`/api/v2/write?org=${encodeURIComponent(e)}&bucket=${encodeURIComponent(r)}&precision=${n}`,this.writeOptions=Object.assign(Object.assign({},_),s),this.currentTime=H[n],this.dateToProtocolTimestamp=V[n],this.writeOptions.defaultTags&&this.useDefaultTags(this.writeOptions.defaultTags),this.sendOptions={method:"POST",headers:Object.assign({"content-type":"text/plain; charset=utf-8"},null==s?void 0:s.headers),gzipThreshold:this.writeOptions.gzipThreshold};var i;this.writeBuffer=new yt(this.writeOptions.batchSize,t=>(this._clearFlushTimeout(),this.sendBatch(t,this.writeOptions.maxRetries+1)),()=>{this.writeOptions.flushInterval>0&&(this._clearFlushTimeout(),this.closed||(this._timeoutHandle=setTimeout(()=>this.sendBatch(this.writeBuffer.reset(),this.writeOptions.maxRetries+1).catch(t=>{}),this.writeOptions.flushInterval)))}),this.sendBatch=this.sendBatch.bind(this),this.retryStrategy=(i=this.writeOptions,new gt(i)),this.retryBuffer=new mt(this.writeOptions.maxBufferLines,this.sendBatch)}sendBatch(t,e,r=Date.now()+this.writeOptions.maxRetryTime){const n=this,s=n.writeOptions.maxRetries+2-e;if(!this.closed&&t.length>0){if(r<=Date.now()){const t=new Error("Max retry time exceeded.");return G.error(`Write to InfluxDB failed (attempt: ${s}).`,t),Promise.reject(t)}return new Promise((i,o)=>{let l;const a={responseStarted(t,e){l=e},error(l){const a=n.writeOptions.writeFailed.call(n,l,t,s);if(a)a.then(i,o);else{if(!n.closed&&e>1&&(!(l instanceof p)||l.statusCode>=429))return G.warn(`Write to InfluxDB failed (attempt: ${s}).`,l),n.retryBuffer.addLines(t,e-1,n.retryStrategy.nextDelay(l,s),r),void o(l);G.error("Write to InfluxDB failed.",l),o(l)}},complete(){if(204==l||null==l)n.writeOptions.writeSuccess.call(n,t),n.retryStrategy.success(),i();else{const t=`204 HTTP response status code expected, but ${l} returned`,e=new p(l,t,void 0,"0");e.message=t,a.error(e)}}};this.transport.send(this.httpPath,t.join("\n"),this.sendOptions,a)})}return Promise.resolve()}_clearFlushTimeout(){void 0!==this._timeoutHandle&&(clearTimeout(this._timeoutHandle),this._timeoutHandle=void 0)}writeRecord(t){if(this.closed)throw new Error("writeApi: already closed!");this.writeBuffer.add(t)}writeRecords(t){if(this.closed)throw new Error("writeApi: already closed!");for(let e=0;e<t.length;e++)this.writeBuffer.add(t[e])}writePoint(t){if(this.closed)throw new Error("writeApi: already closed!");const e=t.toLineProtocol(this);e&&this.writeBuffer.add(e)}writePoints(t){if(this.closed)throw new Error("writeApi: already closed!");for(let e=0;e<t.length;e++){const r=t[e].toLineProtocol(this);r&&this.writeBuffer.add(r)}}flush(t){return pt(this,void 0,void 0,(function*(){if(yield this.writeBuffer.flush(),t)return yield this.retryBuffer.flush()}))}close(){return this.writeBuffer.flush().finally(()=>{const t=this.retryBuffer.close();t&&G.error(`Retry buffer closed with ${t} items that were not written to InfluxDB!`,null),this.closed=!0})}dispose(){return this._clearFlushTimeout(),this.closed=!0,this.retryBuffer.close()+this.writeBuffer.length}useDefaultTags(t){return this.defaultTags=void 0,Object.keys(t).forEach(e=>{(this.defaultTags||(this.defaultTags={}))[e]=P.tag(t[e])}),this}convertTime(t){return void 0===t?this.currentTime():"string"==typeof t?t.length>0?t:void 0:t instanceof Date?this.dateToProtocolTimestamp(t):String("number"==typeof t?Math.floor(t):t)}}const bt={concat:(t,e)=>n.concat([t,e]),toUtf8String:(t,e,r)=>t.toString("utf-8",e,r),copy(t,e,r){const s=n.allocUnsafe(r-e);return t.copy(s,0,e,r),s}};const xt={flush:s.constants.Z_SYNC_FLUSH,finishFlush:s.constants.Z_SYNC_FLUSH},vt=n.allocUnsafe(0);class St{constructor(){this.cancelled=!1}cancel(){this.cancelled=!0}isCancelled(){return this.cancelled}}class Ot{constructor(n){var s,i,o,l,a,u,h;this.connectionOptions=n,this.chunkCombiner=bt;const c=t(n.url);if(this.defaultOptions=Object.assign(Object.assign(Object.assign(Object.assign({},$),n),n.transportOptions),{port:c.port,protocol:c.protocol,hostname:c.hostname}),this.contextPath=null!==(s=c.path)&&void 0!==s?s:"",this.contextPath.endsWith("/")&&(this.contextPath=this.contextPath.substring(0,this.contextPath.length-1)),"/api/v2"==this.contextPath&&(G.warn(`Please remove '/api/v2' context path from InfluxDB base url, using ${c.protocol}//${c.hostname}:${c.port} !`),this.contextPath=""),"http:"===c.protocol)this.requestApi=null!==(l=null===(o=null===(i=this.defaultOptions["follow-redirects"])||void 0===i?void 0:i.http)||void 0===o?void 0:o.request)&&void 0!==l?l:e;else{if("https:"!==c.protocol)throw new Error(`Unsupported protocol "${c.protocol} in URL: "${n.url}"`);this.requestApi=null!==(h=null===(u=null===(a=this.defaultOptions["follow-redirects"])||void 0===a?void 0:a.https)||void 0===u?void 0:u.request)&&void 0!==h?h:r}}send(t,e,r,n){const s=new St;n&&n.useCancellable&&n.useCancellable(s),this.createRequestMessage(t,e,r).then(t=>{this._request(t,s,n)},t=>(null==n?void 0:n.error)&&n.error(t))}request(t,e,r,s){e?"string"!=typeof e&&(e=JSON.stringify(e)):e="";let i,o=vt;return new Promise((l,a)=>{this.send(t,e,r,{responseStarted(t,e){s&&s(t,e),i=String(t["content-type"])},next:t=>{o=n.concat([o,t])},complete:()=>{var t,e;const n=null!==(e=null===(t=r.headers)||void 0===t?void 0:t.accept)&&void 0!==e?e:i;try{n.includes("json")?l(JSON.parse(o.toString("utf8"))):n.includes("text")||n.startsWith("application/csv")?l(o.toString("utf8")):l(o)}catch(t){a(t)}},error:t=>{a(t)}})})}createRequestMessage(t,e,r){const i=n.from(e,"utf-8"),o={"content-type":"application/json; charset=utf-8","User-Agent":"influxdb-client-js/1.19.0"};this.connectionOptions.token&&(o.authorization="Token "+this.connectionOptions.token);let l=Promise.resolve(i);const a=Object.assign(Object.assign({},this.defaultOptions),{path:this.contextPath+t,method:r.method,headers:Object.assign(Object.assign({},o),r.headers)});return void 0!==r.gzipThreshold&&r.gzipThreshold<i.length&&(l=l.then(t=>new Promise((e,r)=>{s.gzip(t,(t,n)=>t?r(t):(a.headers["content-encoding"]="gzip",e(n)))}))),l.then(t=>(a.body=t,a.headers["content-length"]=t.length,a))}_request(t,e,r){const n=function(t={}){let e=0;return{next:r=>{0===e&&t.next&&null!=r&&t.next(r)},error:r=>{0===e&&(e=1,t.error&&t.error(r))},complete:()=>{0===e&&(e=2,t.complete&&t.complete())},responseStarted:(e,r)=>{t.responseStarted&&t.responseStarted(e,r)}}}(r);if(e.isCancelled())return void n.complete();const i=this.requestApi(t,t=>{var r;if(e.isCancelled())return t.resume(),void n.complete();t.on("aborted",()=>{n.error(new b)}),n.responseStarted(t.headers,t.statusCode);const i=null!==(r=t.statusCode)&&void 0!==r?r:600;let o;if("gzip"===t.headers["content-encoding"]?(o=s.createGunzip(xt),t.pipe(o)):o=t,o.on("error",n.error),i>=300){let e="";const r=String(t.headers["content-type"]).startsWith("application/json");o.on("data",n=>{e+=n.toString(),!r&&e.length>1e3&&(e=e.slice(0,1e3),t.resume())}),o.on("end",()=>{""===e&&t.headers["x-influxdb-error"]&&(e=t.headers["x-influxdb-error"].toString()),n.error(new p(i,t.statusMessage,e,t.headers["retry-after"],t.headers["content-type"]))})}else o.on("data",r=>{e.isCancelled()?t.resume():n.next(r)}),o.on("end",n.complete)});"function"==typeof i.setTimeout&&i.setTimeout(t.timeout),i.on("timeout",()=>{n.error(new w)}),i.on("error",t=>{n.error(t)}),i.on("close",n.complete),t.body&&i.write(t.body),i.end()}}function Tt(t,e){return e.toObject(t)}class Rt{constructor(t,e){this.isClosed=!1;try{e({next:e=>{t.next(e)},error:e=>{this.isClosed=!0,t.error(e)},complete:()=>{this.isClosed=!0,t.complete()},useCancellable:t=>{this.cancellable=t}})}catch(e){this.isClosed=!0,t.error(e)}}get closed(){return this.isClosed}unsubscribe(){var t;null===(t=this.cancellable)||void 0===t||t.cancel(),this.isClosed=!0}}function Et(){}class Ct{constructor(t,e){this.executor=t,this.decorator=e}subscribe(t,e,r){const n=function(t){const{next:e,error:r,complete:n}=t;return{next:e?e.bind(t):Et,error:r?r.bind(t):Et,complete:n?n.bind(t):Et}}("object"!=typeof t||null===t?{next:t,error:e,complete:r}:t);return new Rt(this.decorator(n),this.executor)}[ft](){return this}}const Dt={header:!0,delimiter:",",quoteChar:'"',commentPrefix:"#",annotations:["datatype","group","default"]},$t=t=>t;class jt{constructor(t,e){this.transport=t,this.options="string"==typeof e?{org:e}:e}with(t){return new jt(this.transport,Object.assign(Object.assign({},this.options),t))}lines(t){return new Ct(this.createExecutor(t),$t)}rows(t){return new Ct(this.createExecutor(t),t=>C({next(e,r){t.next({values:e,tableMeta:r})},error(e){t.error(e)},complete(){t.complete()}}))}queryLines(t,e){this.createExecutor(t)(e)}queryRows(t,e){this.createExecutor(t)(C(e))}collectRows(t,e=Tt){const r=[];return new Promise((n,s)=>{this.queryRows(t,{next(t,n){const s=e.call(this,t,n);void 0!==s&&r.push(s)},error(t){s(t)},complete(){n(r)}})})}collectLines(t){const e=[];return new Promise((r,n)=>{this.queryLines(t,{next(t){e.push(t)},error(t){n(t)},complete(){r(e)}})})}queryRaw(t){const{org:e,type:r,gzip:n,headers:s}=this.options;return this.transport.request("/api/v2/query?org="+encodeURIComponent(e),JSON.stringify(this.decorateRequest({query:t.toString(),dialect:Dt,type:r})),{method:"POST",headers:Object.assign({accept:"text/csv","accept-encoding":n?"gzip":"identity","content-type":"application/json; encoding=utf-8"},s)})}createExecutor(t){const{org:e,type:r,gzip:n,headers:s}=this.options;return i=>{this.transport.send("/api/v2/query?org="+encodeURIComponent(e),JSON.stringify(this.decorateRequest({query:t.toString(),dialect:Dt,type:r})),{method:"POST",headers:Object.assign({"content-type":"application/json; encoding=utf-8","accept-encoding":n?"gzip":"identity"},s)},o(i,this.transport.chunkCombiner))}}decorateRequest(t){var e;return"function"==typeof this.options.now&&(t.now=this.options.now()),t.type=null!==(e=this.options.type)&&void 0!==e?e:"flux",t}}class _t{constructor(t){var e;if("string"==typeof t)this._options={url:t};else{if(null===t||"object"!=typeof t)throw new d("No url or configuration specified!");this._options=t}const r=this._options.url;if("string"!=typeof r)throw new d("No url specified!");r.endsWith("/")&&(this._options.url=r.substring(0,r.length-1)),this.transport=null!==(e=this._options.transport)&&void 0!==e?e:new Ot(this._options)}getWriteApi(t,e,r="ns",n){return new wt(this.transport,t,e,r,null!=n?n:this._options.writeOptions)}getQueryApi(t){return new jt(this.transport,t)}}export{b as AbortError,$ as DEFAULT_ConnectionOptions,j as DEFAULT_RetryDelayStrategyOptions,_ as DEFAULT_WriteOptions,Z as FLUX_VALUE,p as HttpError,d as IllegalArgumentError,_t as InfluxDB,l as LineSplitter,G as Logger,dt as Point,w as RequestTimedOutError,m as canRetryHttpCall,o as chunksToLines,W as consoleLogger,J as convertTimeToNanos,h as createFluxTableColumn,E as createFluxTableMetaData,i as createTextDecoderCombiner,H as currentTime,V as dateToProtocolTimestamp,P as escape,ct as flux,at as fluxBool,st as fluxDateTime,it as fluxDuration,ut as fluxExpression,rt as fluxFloat,nt as fluxInteger,lt as fluxRegExp,tt as fluxString,y as getRetryDelay,f as isStatusCodeRetriable,C as linesToTables,u as newFluxTableColumn,et as sanitizeFloat,S as serializeDateTimeAsDate,O as serializeDateTimeAsNumber,T as serializeDateTimeAsString,X as setLogger,D as stringToLines,ft as symbolObservable,ht as toFluxValue,v as typeSerializers,M as useProcessHrtime};
15
+ ***************************************************************************** */function pt(t,e,r,n){return new(r||(r=Promise))((function(s,i){function o(t){try{a(n.next(t))}catch(t){i(t)}}function l(t){try{a(n.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?s(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(o,l)}a((n=n.apply(t,e||[])).next())}))}class gt{constructor(t){this.options=Object.assign(Object.assign({},j),t),this.success()}nextDelay(t,e){const r=y(t);if(r&&r>0)return r+Math.round(Math.random()*this.options.retryJitter);if(e&&e>0){if(this.options.randomRetry){let t=Math.max(this.options.minRetryDelay,1),r=t*this.options.exponentialBase;for(let n=1;n<e;n++)if(t=r,r*=this.options.exponentialBase,r>=this.options.maxRetryDelay){r=this.options.maxRetryDelay;break}return t+Math.round(Math.random()*(r-t)+Math.random()*this.options.retryJitter)}let t=Math.max(this.options.minRetryDelay,1);for(let r=1;r<e;r++)if(t*=this.options.exponentialBase,t>=this.options.maxRetryDelay){t=this.options.maxRetryDelay;break}return t+Math.round(Math.random()*this.options.retryJitter)}return this.currentDelay?this.currentDelay=Math.min(Math.max(this.currentDelay*this.options.exponentialBase,1)+Math.round(Math.random()*this.options.retryJitter),this.options.maxRetryDelay):this.currentDelay=this.options.minRetryDelay+Math.round(Math.random()*this.options.retryJitter),this.currentDelay}success(){this.currentDelay=void 0}}class mt{constructor(t,e){this.maxLines=t,this.retryLines=e,this.size=0,this.nextRetryTime=0,this.closed=!1,this._timeoutHandle=void 0}addLines(t,e,r,n){if(this.closed)return;if(!t.length)return;let s=Date.now()+r;if(n<s&&(r=n-Date.now(),s=n),s>this.nextRetryTime&&(this.nextRetryTime=s),this.first&&this.size+t.length>this.maxLines){const e=this.size,r=.7*e;do{const t=this.first.next;this.size-=this.first.lines.length,this.first.next=void 0,this.first=t,this.first||(this.last=void 0)}while(this.first&&this.size+t.length>r);G.error(`RetryBuffer: ${e-this.size} oldest lines removed to keep buffer size under the limit of ${this.maxLines} lines`)}const i={lines:t,retryCount:e,expires:n};this.last?(this.last.next=i,this.last=i):(this.first=i,this.last=i,this.scheduleRetry(r)),this.size+=t.length}removeLines(){if(this.first){const t=this.first;return this.first=this.first.next,t.next=void 0,this.size-=t.lines.length,this.first||(this.last=void 0),t}}scheduleRetry(t){this._timeoutHandle=setTimeout(()=>{const t=this.removeLines();t?this.retryLines(t.lines,t.retryCount,t.expires).then(()=>{this.scheduleRetry(1)}).catch(t=>{this.scheduleRetry(this.nextRetryTime-Date.now())}):this._timeoutHandle=void 0},Math.max(t,0))}flush(){return pt(this,void 0,void 0,(function*(){let t;for(;t=this.removeLines();)yield this.retryLines(t.lines,t.retryCount,t.expires)}))}close(){return this._timeoutHandle&&(clearTimeout(this._timeoutHandle),this._timeoutHandle=void 0),this.closed=!0,this.size}}class yt{constructor(t,e,r){this.maxChunkRecords=t,this.flushFn=e,this.scheduleSend=r,this.length=0,this.lines=new Array(t)}add(t){0===this.length&&this.scheduleSend(),this.lines[this.length]=t,this.length++,this.length>=this.maxChunkRecords&&this.flush().catch(t=>{})}flush(){const t=this.reset();return t.length>0?this.flushFn(t):Promise.resolve()}reset(){const t=this.lines.slice(0,this.length);return this.length=0,t}}class wt{constructor(t,e,r,n,s){this.transport=t,this.closed=!1,this._timeoutHandle=void 0,this.httpPath=`/api/v2/write?org=${encodeURIComponent(e)}&bucket=${encodeURIComponent(r)}&precision=${n}`,this.writeOptions=Object.assign(Object.assign({},_),s),this.currentTime=H[n],this.dateToProtocolTimestamp=V[n],this.writeOptions.defaultTags&&this.useDefaultTags(this.writeOptions.defaultTags),this.sendOptions={method:"POST",headers:Object.assign({"content-type":"text/plain; charset=utf-8"},null==s?void 0:s.headers),gzipThreshold:this.writeOptions.gzipThreshold};var i;this.writeBuffer=new yt(this.writeOptions.batchSize,t=>(this._clearFlushTimeout(),this.sendBatch(t,this.writeOptions.maxRetries+1)),()=>{this.writeOptions.flushInterval>0&&(this._clearFlushTimeout(),this.closed||(this._timeoutHandle=setTimeout(()=>this.sendBatch(this.writeBuffer.reset(),this.writeOptions.maxRetries+1).catch(t=>{}),this.writeOptions.flushInterval)))}),this.sendBatch=this.sendBatch.bind(this),this.retryStrategy=(i=this.writeOptions,new gt(i)),this.retryBuffer=new mt(this.writeOptions.maxBufferLines,this.sendBatch)}sendBatch(t,e,r=Date.now()+this.writeOptions.maxRetryTime){const n=this,s=n.writeOptions.maxRetries+2-e;if(!this.closed&&t.length>0){if(r<=Date.now()){const t=new Error("Max retry time exceeded.");return G.error(`Write to InfluxDB failed (attempt: ${s}).`,t),Promise.reject(t)}return new Promise((i,o)=>{let l;const a={responseStarted(t,e){l=e},error(l){const a=n.writeOptions.writeFailed.call(n,l,t,s);if(a)a.then(i,o);else{if(!n.closed&&e>1&&(!(l instanceof p)||l.statusCode>=429))return G.warn(`Write to InfluxDB failed (attempt: ${s}).`,l),n.retryBuffer.addLines(t,e-1,n.retryStrategy.nextDelay(l,s),r),void o(l);G.error("Write to InfluxDB failed.",l),o(l)}},complete(){if(204==l||null==l)n.writeOptions.writeSuccess.call(n,t),n.retryStrategy.success(),i();else{const t=`204 HTTP response status code expected, but ${l} returned`,e=new p(l,t,void 0,"0");e.message=t,a.error(e)}}};this.transport.send(this.httpPath,t.join("\n"),this.sendOptions,a)})}return Promise.resolve()}_clearFlushTimeout(){void 0!==this._timeoutHandle&&(clearTimeout(this._timeoutHandle),this._timeoutHandle=void 0)}writeRecord(t){if(this.closed)throw new Error("writeApi: already closed!");this.writeBuffer.add(t)}writeRecords(t){if(this.closed)throw new Error("writeApi: already closed!");for(let e=0;e<t.length;e++)this.writeBuffer.add(t[e])}writePoint(t){if(this.closed)throw new Error("writeApi: already closed!");const e=t.toLineProtocol(this);e&&this.writeBuffer.add(e)}writePoints(t){if(this.closed)throw new Error("writeApi: already closed!");for(let e=0;e<t.length;e++){const r=t[e].toLineProtocol(this);r&&this.writeBuffer.add(r)}}flush(t){return pt(this,void 0,void 0,(function*(){if(yield this.writeBuffer.flush(),t)return yield this.retryBuffer.flush()}))}close(){return this.writeBuffer.flush().finally(()=>{const t=this.retryBuffer.close();t&&G.error(`Retry buffer closed with ${t} items that were not written to InfluxDB!`,null),this.closed=!0})}dispose(){return this._clearFlushTimeout(),this.closed=!0,this.retryBuffer.close()+this.writeBuffer.length}useDefaultTags(t){return this.defaultTags=void 0,Object.keys(t).forEach(e=>{(this.defaultTags||(this.defaultTags={}))[e]=P.tag(t[e])}),this}convertTime(t){return void 0===t?this.currentTime():"string"==typeof t?t.length>0?t:void 0:t instanceof Date?this.dateToProtocolTimestamp(t):String("number"==typeof t?Math.floor(t):t)}}const bt={concat:(t,e)=>n.concat([t,e]),toUtf8String:(t,e,r)=>t.toString("utf-8",e,r),copy(t,e,r){const s=n.allocUnsafe(r-e);return t.copy(s,0,e,r),s}};const xt={flush:s.constants.Z_SYNC_FLUSH,finishFlush:s.constants.Z_SYNC_FLUSH},vt=n.allocUnsafe(0);class St{constructor(){this.cancelled=!1}cancel(){this.cancelled=!0}isCancelled(){return this.cancelled}}class Ot{constructor(n){var s,i,o,l,a,u,h;this.connectionOptions=n,this.chunkCombiner=bt;const c=t(n.url);if(this.defaultOptions=Object.assign(Object.assign(Object.assign(Object.assign({},$),n),n.transportOptions),{port:c.port,protocol:c.protocol,hostname:c.hostname}),this.contextPath=null!==(s=c.path)&&void 0!==s?s:"",this.contextPath.endsWith("/")&&(this.contextPath=this.contextPath.substring(0,this.contextPath.length-1)),"/api/v2"==this.contextPath&&(G.warn(`Please remove '/api/v2' context path from InfluxDB base url, using ${c.protocol}//${c.hostname}:${c.port} !`),this.contextPath=""),"http:"===c.protocol)this.requestApi=null!==(l=null===(o=null===(i=this.defaultOptions["follow-redirects"])||void 0===i?void 0:i.http)||void 0===o?void 0:o.request)&&void 0!==l?l:e;else{if("https:"!==c.protocol)throw new Error(`Unsupported protocol "${c.protocol} in URL: "${n.url}"`);this.requestApi=null!==(h=null===(u=null===(a=this.defaultOptions["follow-redirects"])||void 0===a?void 0:a.https)||void 0===u?void 0:u.request)&&void 0!==h?h:r}}send(t,e,r,n){const s=new St;n&&n.useCancellable&&n.useCancellable(s),this.createRequestMessage(t,e,r).then(t=>{this._request(t,s,n)},t=>(null==n?void 0:n.error)&&n.error(t))}request(t,e,r,s){e?"string"!=typeof e&&(e=JSON.stringify(e)):e="";let i,o=vt;return new Promise((l,a)=>{this.send(t,e,r,{responseStarted(t,e){s&&s(t,e),i=String(t["content-type"])},next:t=>{o=n.concat([o,t])},complete:()=>{var t,e;const n=null!==(e=null===(t=r.headers)||void 0===t?void 0:t.accept)&&void 0!==e?e:i;try{n.includes("json")?l(JSON.parse(o.toString("utf8"))):n.includes("text")||n.startsWith("application/csv")?l(o.toString("utf8")):l(o)}catch(t){a(t)}},error:t=>{a(t)}})})}createRequestMessage(t,e,r){const i=n.from(e,"utf-8"),o={"content-type":"application/json; charset=utf-8","User-Agent":"influxdb-client-js/1.20.0.nightly"};this.connectionOptions.token&&(o.authorization="Token "+this.connectionOptions.token);let l=Promise.resolve(i);const a=Object.assign(Object.assign({},this.defaultOptions),{path:this.contextPath+t,method:r.method,headers:Object.assign(Object.assign({},o),r.headers)});return void 0!==r.gzipThreshold&&r.gzipThreshold<i.length&&(l=l.then(t=>new Promise((e,r)=>{s.gzip(t,(t,n)=>t?r(t):(a.headers["content-encoding"]="gzip",e(n)))}))),l.then(t=>(a.body=t,a.headers["content-length"]=t.length,a))}_request(t,e,r){const n=function(t={}){let e=0;return{next:r=>{0===e&&t.next&&null!=r&&t.next(r)},error:r=>{0===e&&(e=1,t.error&&t.error(r))},complete:()=>{0===e&&(e=2,t.complete&&t.complete())},responseStarted:(e,r)=>{t.responseStarted&&t.responseStarted(e,r)}}}(r);if(e.isCancelled())return void n.complete();const i=this.requestApi(t,t=>{var r;if(e.isCancelled())return t.resume(),void n.complete();t.on("aborted",()=>{n.error(new b)}),n.responseStarted(t.headers,t.statusCode);const i=null!==(r=t.statusCode)&&void 0!==r?r:600;let o;if("gzip"===t.headers["content-encoding"]?(o=s.createGunzip(xt),t.pipe(o)):o=t,o.on("error",n.error),i>=300){let e="";const r=String(t.headers["content-type"]).startsWith("application/json");o.on("data",n=>{e+=n.toString(),!r&&e.length>1e3&&(e=e.slice(0,1e3),t.resume())}),o.on("end",()=>{""===e&&t.headers["x-influxdb-error"]&&(e=t.headers["x-influxdb-error"].toString()),n.error(new p(i,t.statusMessage,e,t.headers["retry-after"],t.headers["content-type"]))})}else o.on("data",r=>{e.isCancelled()?t.resume():n.next(r)}),o.on("end",n.complete)});"function"==typeof i.setTimeout&&i.setTimeout(t.timeout),i.on("timeout",()=>{n.error(new w)}),i.on("error",t=>{n.error(t)}),i.on("close",n.complete),t.body&&i.write(t.body),i.end()}}function Tt(t,e){return e.toObject(t)}class Rt{constructor(t,e){this.isClosed=!1;try{e({next:e=>{t.next(e)},error:e=>{this.isClosed=!0,t.error(e)},complete:()=>{this.isClosed=!0,t.complete()},useCancellable:t=>{this.cancellable=t}})}catch(e){this.isClosed=!0,t.error(e)}}get closed(){return this.isClosed}unsubscribe(){var t;null===(t=this.cancellable)||void 0===t||t.cancel(),this.isClosed=!0}}function Et(){}class Ct{constructor(t,e){this.executor=t,this.decorator=e}subscribe(t,e,r){const n=function(t){const{next:e,error:r,complete:n}=t;return{next:e?e.bind(t):Et,error:r?r.bind(t):Et,complete:n?n.bind(t):Et}}("object"!=typeof t||null===t?{next:t,error:e,complete:r}:t);return new Rt(this.decorator(n),this.executor)}[ft](){return this}}const Dt={header:!0,delimiter:",",quoteChar:'"',commentPrefix:"#",annotations:["datatype","group","default"]},$t=t=>t;class jt{constructor(t,e){this.transport=t,this.options="string"==typeof e?{org:e}:e}with(t){return new jt(this.transport,Object.assign(Object.assign({},this.options),t))}lines(t){return new Ct(this.createExecutor(t),$t)}rows(t){return new Ct(this.createExecutor(t),t=>C({next(e,r){t.next({values:e,tableMeta:r})},error(e){t.error(e)},complete(){t.complete()}}))}queryLines(t,e){this.createExecutor(t)(e)}queryRows(t,e){this.createExecutor(t)(C(e))}collectRows(t,e=Tt){const r=[];return new Promise((n,s)=>{this.queryRows(t,{next(t,n){const s=e.call(this,t,n);void 0!==s&&r.push(s)},error(t){s(t)},complete(){n(r)}})})}collectLines(t){const e=[];return new Promise((r,n)=>{this.queryLines(t,{next(t){e.push(t)},error(t){n(t)},complete(){r(e)}})})}queryRaw(t){const{org:e,type:r,gzip:n,headers:s}=this.options;return this.transport.request("/api/v2/query?org="+encodeURIComponent(e),JSON.stringify(this.decorateRequest({query:t.toString(),dialect:Dt,type:r})),{method:"POST",headers:Object.assign({accept:"text/csv","accept-encoding":n?"gzip":"identity","content-type":"application/json; encoding=utf-8"},s)})}createExecutor(t){const{org:e,type:r,gzip:n,headers:s}=this.options;return i=>{this.transport.send("/api/v2/query?org="+encodeURIComponent(e),JSON.stringify(this.decorateRequest({query:t.toString(),dialect:Dt,type:r})),{method:"POST",headers:Object.assign({"content-type":"application/json; encoding=utf-8","accept-encoding":n?"gzip":"identity"},s)},o(i,this.transport.chunkCombiner))}}decorateRequest(t){var e;return"function"==typeof this.options.now&&(t.now=this.options.now()),t.type=null!==(e=this.options.type)&&void 0!==e?e:"flux",t}}class _t{constructor(t){var e;if("string"==typeof t)this._options={url:t};else{if(null===t||"object"!=typeof t)throw new d("No url or configuration specified!");this._options=t}const r=this._options.url;if("string"!=typeof r)throw new d("No url specified!");r.endsWith("/")&&(this._options.url=r.substring(0,r.length-1)),this.transport=null!==(e=this._options.transport)&&void 0!==e?e:new Ot(this._options)}getWriteApi(t,e,r="ns",n){return new wt(this.transport,t,e,r,null!=n?n:this._options.writeOptions)}getQueryApi(t){return new jt(this.transport,t)}}export{b as AbortError,$ as DEFAULT_ConnectionOptions,j as DEFAULT_RetryDelayStrategyOptions,_ as DEFAULT_WriteOptions,Z as FLUX_VALUE,p as HttpError,d as IllegalArgumentError,_t as InfluxDB,l as LineSplitter,G as Logger,dt as Point,w as RequestTimedOutError,m as canRetryHttpCall,o as chunksToLines,W as consoleLogger,J as convertTimeToNanos,h as createFluxTableColumn,E as createFluxTableMetaData,i as createTextDecoderCombiner,H as currentTime,V as dateToProtocolTimestamp,P as escape,ct as flux,at as fluxBool,st as fluxDateTime,it as fluxDuration,ut as fluxExpression,rt as fluxFloat,nt as fluxInteger,lt as fluxRegExp,tt as fluxString,y as getRetryDelay,f as isStatusCodeRetriable,C as linesToTables,u as newFluxTableColumn,et as sanitizeFloat,S as serializeDateTimeAsDate,O as serializeDateTimeAsNumber,T as serializeDateTimeAsString,X as setLogger,D as stringToLines,ft as symbolObservable,ht as toFluxValue,v as typeSerializers,M as useProcessHrtime};
16
16
  //# sourceMappingURL=index.mjs.map
package/dist/index.mjs.gz CHANGED
Binary file