ironflock 1.5.2 → 1.5.3
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.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +31 -1
- package/dist/index.mjs +210 -145
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`))),h.push(a)),!1)},T=h=>Object.assign(h,{"~standard":{version:1,vendor:"typia",validate:t=>{const s=h(t);return s.success?{value:s.data}:{issues:s.errors.map(a=>({message:`expected ${a.expected}, got ${a.value}`,path:oe(a.path)}))}}}});var w;(function(h){h[h.Start=0]="Start",h[h.Property=1]="Property",h[h.StringKey=2]="StringKey",h[h.NumberKey=3]="NumberKey"})(w||(w={}));const oe=h=>{if(!h.startsWith("$input"))throw new Error(`Invalid path: ${JSON.stringify(h)}`);const t=[];let s="",a=w.Start,l=5;for(;l<h.length-1;){l++;const i=h[l];if(a===w.Property?i==="."||i==="["?(t.push({key:s}),a=w.Start):l===h.length-1?(s+=i,t.push({key:s}),l++,a=w.Start):s+=i:a===w.StringKey?i==='"'?(t.push({key:JSON.parse(s+i)}),l+=2,a=w.Start):i==="\\"?(s+=h[l],l++,s+=h[l]):s+=i:a===w.NumberKey&&(i==="]"?(t.push({key:Number.parseInt(s)}),l++,a=w.Start):s+=i),a===w.Start&&l<h.length-1){const u=h[l];if(s="",u==="[")h[l+1]==='"'?(a=w.StringKey,l++,s='"'):a=w.NumberKey;else if(u===".")a=w.Property;else throw new Error("Unreachable: pointer points invalid character")}}if(a!==w.Start)throw new Error(`Failed to parse path: ${JSON.stringify(h)}`);return t};var R=(h=>(h.DEVELOPMENT="dev",h.PRODUCTION="prod",h))(R||{});const P=(()=>{const h=e=>typeof e.limit=="number"&&I(e.limit)&&1<=e.limit&&e.limit<=1e4&&(e.offset===void 0||typeof e.offset=="number"&&I(e.offset)&&0<=e.offset)&&(e.timeRange===void 0||typeof e.timeRange=="object"&&e.timeRange!==null&&t(e.timeRange))&&(e.filterAnd===void 0||Array.isArray(e.filterAnd)&&e.filterAnd.every(n=>typeof n=="object"&&n!==null&&s(n))),t=e=>typeof e.start=="string"&&$(e.start)&&typeof e.end=="string"&&$(e.end),s=e=>typeof e.column=="string"&&1<=e.column.length&&typeof e.operator=="string"&&1<=e.operator.length&&e.value!==null&&e.value!==void 0&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"||Array.isArray(e.value)&&e.value.every(n=>typeof n=="string"||typeof n=="number")),a=(e,n,c=!0)=>[typeof e.limit=="number"&&(I(e.limit)||o(c,{path:n+".limit",expected:'number & Type<"uint32">',value:e.limit}))&&(1<=e.limit||o(c,{path:n+".limit",expected:"number & Minimum<1>",value:e.limit}))&&(e.limit<=1e4||o(c,{path:n+".limit",expected:"number & Maximum<10000>",value:e.limit}))||o(c,{path:n+".limit",expected:'(number & Type<"uint32"> & Minimum<1> & Maximum<10000>)',value:e.limit}),e.offset===void 0||typeof e.offset=="number"&&(I(e.offset)||o(c,{path:n+".offset",expected:'number & Type<"uint32">',value:e.offset}))&&(0<=e.offset||o(c,{path:n+".offset",expected:"number & Minimum<0>",value:e.offset}))||o(c,{path:n+".offset",expected:'((number & Type<"uint32"> & Minimum<0>) | undefined)',value:e.offset}),e.timeRange===void 0||(typeof e.timeRange=="object"&&e.timeRange!==null||o(c,{path:n+".timeRange",expected:"(ISOTimeRange | undefined)",value:e.timeRange}))&&l(e.timeRange,n+".timeRange",c)||o(c,{path:n+".timeRange",expected:"(ISOTimeRange | undefined)",value:e.timeRange}),e.filterAnd===void 0||(Array.isArray(e.filterAnd)||o(c,{path:n+".filterAnd",expected:"(Array<SQLFilterAnd> | undefined)",value:e.filterAnd}))&&e.filterAnd.map((d,v)=>(typeof d=="object"&&d!==null||o(c,{path:n+".filterAnd["+v+"]",expected:"SQLFilterAnd",value:d}))&&i(d,n+".filterAnd["+v+"]",c)||o(c,{path:n+".filterAnd["+v+"]",expected:"SQLFilterAnd",value:d})).every(d=>d)||o(c,{path:n+".filterAnd",expected:"(Array<SQLFilterAnd> | undefined)",value:e.filterAnd})].every(d=>d),l=(e,n,c=!0)=>[typeof e.start=="string"&&($(e.start)||o(c,{path:n+".start",expected:'string & Format<"date-time">',value:e.start}))||o(c,{path:n+".start",expected:'(string & Format<"date-time">)',value:e.start}),typeof e.end=="string"&&($(e.end)||o(c,{path:n+".end",expected:'string & Format<"date-time">',value:e.end}))||o(c,{path:n+".end",expected:'(string & Format<"date-time">)',value:e.end})].every(d=>d),i=(e,n,c=!0)=>[typeof e.column=="string"&&(1<=e.column.length||o(c,{path:n+".column",expected:"string & MinLength<1>",value:e.column}))||o(c,{path:n+".column",expected:"(string & MinLength<1>)",value:e.column}),typeof e.operator=="string"&&(1<=e.operator.length||o(c,{path:n+".operator",expected:"string & MinLength<1>",value:e.operator}))||o(c,{path:n+".operator",expected:"(string & MinLength<1>)",value:e.operator}),(e.value!==null||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&(e.value!==void 0||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"||(Array.isArray(e.value)||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&e.value.map((d,v)=>typeof d=="string"||typeof d=="number"||o(c,{path:n+".value["+v+"]",expected:"(number | string)",value:d})).every(d=>d)||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))].every(d=>d),u=e=>typeof e=="object"&&e!==null&&h(e);let r,o;return T(e=>{if(u(e)===!1){r=[],o=_(r),((c,d,v=!0)=>(typeof c=="object"&&c!==null||o(!0,{path:d+"",expected:"TableQueryParams",value:c}))&&a(c,d+"",!0)||o(!0,{path:d+"",expected:"TableQueryParams",value:c}))(e,"$input",!0);const n=r.length===0;return n?{success:n,data:e}:{success:n,errors:r,data:e}}return{success:!0,data:e}})})(),j=(()=>{const h=e=>{const n=e,c=[[d=>d.length===2&&(d[0]===null||typeof d[0]=="number")&&(d[1]===null||typeof d[1]=="number"),d=>d.length===2&&(d[0]===null||typeof d[0]=="number")&&(d[1]===null||typeof d[1]=="number")],[d=>d.length===2&&(d[0]===null||typeof d[0]=="string")&&(d[1]===null||typeof d[1]=="string"),d=>d.length===2&&(d[0]===null||typeof d[0]=="string")&&(d[1]===null||typeof d[1]=="string")]];for(const d of c)if(d[0](n))return d[1](n);return!1},t=(e,n,c=!0)=>{const d=e,v=[[f=>f.length===2&&[f[0]===null||typeof f[0]=="number",f[1]===null||typeof f[1]=="number"].every(g=>g),f=>(f.length===2||o(c,{path:n,expected:"[(null | number), (null | number)]",value:f}))&&[f[0]===null||typeof f[0]=="number"||o(c,{path:n+"[0]",expected:"(null | number)",value:f[0]}),f[1]===null||typeof f[1]=="number"||o(c,{path:n+"[1]",expected:"(null | number)",value:f[1]})].every(g=>g)],[f=>f.length===2&&[f[0]===null||typeof f[0]=="string",f[1]===null||typeof f[1]=="string"].every(g=>g),f=>(f.length===2||o(c,{path:n,expected:"[(null | string), (null | string)]",value:f}))&&[f[0]===null||typeof f[0]=="string"||o(c,{path:n+"[0]",expected:"(null | string)",value:f[0]}),f[1]===null||typeof f[1]=="string"||o(c,{path:n+"[1]",expected:"(null | string)",value:f[1]})].every(g=>g)]];for(const f of v)if(f[0](d))return f[1](d);return o(c,{path:n,expected:"([number | null, number | null] | [string | null, string | null])",value:e})},s=e=>Array.isArray(e.metrics)&&e.metrics.every(n=>typeof n=="string")&&(e.method==="AVG"||e.method==="SUM"||e.method==="COUNT"||e.method==="MIN"||e.method==="MAX"||e.method==="FIRST"||e.method==="LAST")&&typeof e.limit=="number"&&I(e.limit)&&1<=e.limit&&e.limit<=1e4&&Array.isArray(e.timeRange)&&(h(e.timeRange)||!1)&&(e.groupBy===null||e.groupBy===void 0||Array.isArray(e.groupBy)&&e.groupBy.every(n=>typeof n=="string"))&&(e.filterAnd===null||e.filterAnd===void 0||Array.isArray(e.filterAnd)&&e.filterAnd.every(n=>typeof n=="object"&&n!==null&&a(n))),a=e=>typeof e.column=="string"&&1<=e.column.length&&typeof e.operator=="string"&&1<=e.operator.length&&e.value!==null&&e.value!==void 0&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"||Array.isArray(e.value)&&e.value.every(n=>typeof n=="string"||typeof n=="number")),l=(e,n,c=!0)=>[(Array.isArray(e.metrics)||o(c,{path:n+".metrics",expected:"Array<string>",value:e.metrics}))&&e.metrics.map((d,v)=>typeof d=="string"||o(c,{path:n+".metrics["+v+"]",expected:"string",value:d})).every(d=>d)||o(c,{path:n+".metrics",expected:"Array<string>",value:e.metrics}),e.method==="AVG"||e.method==="SUM"||e.method==="COUNT"||e.method==="MIN"||e.method==="MAX"||e.method==="FIRST"||e.method==="LAST"||o(c,{path:n+".method",expected:'("AVG" | "COUNT" | "FIRST" | "LAST" | "MAX" | "MIN" | "SUM")',value:e.method}),typeof e.limit=="number"&&(I(e.limit)||o(c,{path:n+".limit",expected:'number & Type<"uint32">',value:e.limit}))&&(1<=e.limit||o(c,{path:n+".limit",expected:"number & Minimum<1>",value:e.limit}))&&(e.limit<=1e4||o(c,{path:n+".limit",expected:"number & Maximum<10000>",value:e.limit}))||o(c,{path:n+".limit",expected:'(number & Type<"uint32"> & Minimum<1> & Maximum<10000>)',value:e.limit}),(Array.isArray(e.timeRange)||o(c,{path:n+".timeRange",expected:"([number | null, number | null] | [string | null, string | null])",value:e.timeRange}))&&(t(e.timeRange,n+".timeRange",c)||o(c,{path:n+".timeRange",expected:"[number | null, number | null] | [string | null, string | null]",value:e.timeRange}))||o(c,{path:n+".timeRange",expected:"([number | null, number | null] | [string | null, string | null])",value:e.timeRange}),e.groupBy===null||e.groupBy===void 0||(Array.isArray(e.groupBy)||o(c,{path:n+".groupBy",expected:"(Array<string> | null | undefined)",value:e.groupBy}))&&e.groupBy.map((d,v)=>typeof d=="string"||o(c,{path:n+".groupBy["+v+"]",expected:"string",value:d})).every(d=>d)||o(c,{path:n+".groupBy",expected:"(Array<string> | null | undefined)",value:e.groupBy}),e.filterAnd===null||e.filterAnd===void 0||(Array.isArray(e.filterAnd)||o(c,{path:n+".filterAnd",expected:"(Array<SQLFilterAnd> | null | undefined)",value:e.filterAnd}))&&e.filterAnd.map((d,v)=>(typeof d=="object"&&d!==null||o(c,{path:n+".filterAnd["+v+"]",expected:"SQLFilterAnd",value:d}))&&i(d,n+".filterAnd["+v+"]",c)||o(c,{path:n+".filterAnd["+v+"]",expected:"SQLFilterAnd",value:d})).every(d=>d)||o(c,{path:n+".filterAnd",expected:"(Array<SQLFilterAnd> | null | undefined)",value:e.filterAnd})].every(d=>d),i=(e,n,c=!0)=>[typeof e.column=="string"&&(1<=e.column.length||o(c,{path:n+".column",expected:"string & MinLength<1>",value:e.column}))||o(c,{path:n+".column",expected:"(string & MinLength<1>)",value:e.column}),typeof e.operator=="string"&&(1<=e.operator.length||o(c,{path:n+".operator",expected:"string & MinLength<1>",value:e.operator}))||o(c,{path:n+".operator",expected:"(string & MinLength<1>)",value:e.operator}),(e.value!==null||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&(e.value!==void 0||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"||(Array.isArray(e.value)||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&e.value.map((d,v)=>typeof d=="string"||typeof d=="number"||o(c,{path:n+".value["+v+"]",expected:"(number | string)",value:d})).every(d=>d)||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))].every(d=>d),u=e=>typeof e=="object"&&e!==null&&s(e);let r,o;return T(e=>{if(u(e)===!1){r=[],o=_(r),((c,d,v=!0)=>(typeof c=="object"&&c!==null||o(!0,{path:d+"",expected:"SeriesQueryParams",value:c}))&&l(c,d+"",!0)||o(!0,{path:d+"",expected:"SeriesQueryParams",value:c}))(e,"$input",!0);const n=r.length===0;return n?{success:n,data:e}:{success:n,errors:r,data:e}}return{success:!0,data:e}})})(),W=(()=>{const h=i=>typeof i.longitude=="number"&&-180<=i.longitude&&i.longitude<=180&&typeof i.latitude=="number"&&-90<=i.latitude&&i.latitude<=90,t=(i,u,r=!0)=>[typeof i.longitude=="number"&&(-180<=i.longitude||l(r,{path:u+".longitude",expected:"number & Minimum<-180>",value:i.longitude}))&&(i.longitude<=180||l(r,{path:u+".longitude",expected:"number & Maximum<180>",value:i.longitude}))||l(r,{path:u+".longitude",expected:"(number & Minimum<-180> & Maximum<180>)",value:i.longitude}),typeof i.latitude=="number"&&(-90<=i.latitude||l(r,{path:u+".latitude",expected:"number & Minimum<-90>",value:i.latitude}))&&(i.latitude<=90||l(r,{path:u+".latitude",expected:"number & Maximum<90>",value:i.latitude}))||l(r,{path:u+".latitude",expected:"(number & Minimum<-90> & Maximum<90>)",value:i.latitude})].every(o=>o),s=i=>typeof i=="object"&&i!==null&&h(i);let a,l;return T(i=>{if(s(i)===!1){a=[],l=_(a),((r,o,e=!0)=>(typeof r=="object"&&r!==null||l(!0,{path:o+"",expected:"LocationParams",value:r}))&&t(r,o+"",!0)||l(!0,{path:o+"",expected:"LocationParams",value:r}))(i,"$input",!0);const u=a.length===0;return u?{success:u,data:i}:{success:u,errors:a,data:i}}return{success:!0,data:i}})})(),B=(()=>{const h=r=>typeof r.topic=="string"&&1<=r.topic.length&&(r.args===void 0||Array.isArray(r.args))&&(r.kwargs===void 0||typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1&&t(r.kwargs)),t=r=>Object.keys(r).every(o=>(r[o]===void 0,!0)),s=(r,o,e=!0)=>[typeof r.topic=="string"&&(1<=r.topic.length||u(e,{path:o+".topic",expected:"string & MinLength<1>",value:r.topic}))||u(e,{path:o+".topic",expected:"(string & MinLength<1>)",value:r.topic}),r.args===void 0||Array.isArray(r.args)||u(e,{path:o+".args",expected:"(Array<unknown> | undefined)",value:r.args}),r.kwargs===void 0||(typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs}))&&a(r.kwargs,o+".kwargs",e)||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs})].every(n=>n),a=(r,o,e=!0)=>[e===!1||Object.keys(r).map(n=>(r[n]===void 0,!0)).every(n=>n)].every(n=>n),l=r=>typeof r=="object"&&r!==null&&h(r);let i,u;return T(r=>{if(l(r)===!1){i=[],u=_(i),((e,n,c=!0)=>(typeof e=="object"&&e!==null||u(!0,{path:n+"",expected:"PublishParams",value:e}))&&s(e,n+"",!0)||u(!0,{path:n+"",expected:"PublishParams",value:e}))(r,"$input",!0);const o=i.length===0;return o?{success:o,data:r}:{success:o,errors:i,data:r}}return{success:!0,data:r}})})(),ae=(()=>{const h=r=>typeof r.deviceKey=="string"&&1<=r.deviceKey.length&&typeof r.topic=="string"&&1<=r.topic.length&&(r.args===void 0||Array.isArray(r.args))&&(r.kwargs===void 0||typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1&&t(r.kwargs)),t=r=>Object.keys(r).every(o=>(r[o]===void 0,!0)),s=(r,o,e=!0)=>[typeof r.deviceKey=="string"&&(1<=r.deviceKey.length||u(e,{path:o+".deviceKey",expected:"string & MinLength<1>",value:r.deviceKey}))||u(e,{path:o+".deviceKey",expected:"(string & MinLength<1>)",value:r.deviceKey}),typeof r.topic=="string"&&(1<=r.topic.length||u(e,{path:o+".topic",expected:"string & MinLength<1>",value:r.topic}))||u(e,{path:o+".topic",expected:"(string & MinLength<1>)",value:r.topic}),r.args===void 0||Array.isArray(r.args)||u(e,{path:o+".args",expected:"(Array<unknown> | undefined)",value:r.args}),r.kwargs===void 0||(typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs}))&&a(r.kwargs,o+".kwargs",e)||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs})].every(n=>n),a=(r,o,e=!0)=>[e===!1||Object.keys(r).map(n=>(r[n]===void 0,!0)).every(n=>n)].every(n=>n),l=r=>typeof r=="object"&&r!==null&&h(r);let i,u;return T(r=>{if(l(r)===!1){i=[],u=_(i),((e,n,c=!0)=>(typeof e=="object"&&e!==null||u(!0,{path:n+"",expected:"CallParams",value:e}))&&s(e,n+"",!0)||u(!0,{path:n+"",expected:"CallParams",value:e}))(r,"$input",!0);const o=i.length===0;return o?{success:o,data:r}:{success:o,errors:i,data:r}}return{success:!0,data:r}})})(),N=(()=>{const h=r=>typeof r.tablename=="string"&&1<=r.tablename.length&&(r.args===void 0||Array.isArray(r.args))&&(r.kwargs===void 0||typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1&&t(r.kwargs)),t=r=>Object.keys(r).every(o=>(r[o]===void 0,!0)),s=(r,o,e=!0)=>[typeof r.tablename=="string"&&(1<=r.tablename.length||u(e,{path:o+".tablename",expected:"string & MinLength<1>",value:r.tablename}))||u(e,{path:o+".tablename",expected:"(string & MinLength<1>)",value:r.tablename}),r.args===void 0||Array.isArray(r.args)||u(e,{path:o+".args",expected:"(Array<unknown> | undefined)",value:r.args}),r.kwargs===void 0||(typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs}))&&a(r.kwargs,o+".kwargs",e)||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs})].every(n=>n),a=(r,o,e=!0)=>[e===!1||Object.keys(r).map(n=>(r[n]===void 0,!0)).every(n=>n)].every(n=>n),l=r=>typeof r=="object"&&r!==null&&h(r);let i,u;return T(r=>{if(l(r)===!1){i=[],u=_(i),((e,n,c=!0)=>(typeof e=="object"&&e!==null||u(!0,{path:n+"",expected:"TableParams",value:e}))&&s(e,n+"",!0)||u(!0,{path:n+"",expected:"TableParams",value:e}))(r,"$input",!0);const o=i.length===0;return o?{success:o,data:r}:{success:o,errors:i,data:r}}return{success:!0,data:r}})})(),K=(()=>{const h=r=>typeof r.tablename=="string"&&1<=r.tablename.length&&Array.isArray(r.rows)&&1<=r.rows.length&&r.rows.every(o=>typeof o=="object"&&o!==null&&Array.isArray(o)===!1&&t(o))&&(r.kwargs===void 0||typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1&&t(r.kwargs)),t=r=>Object.keys(r).every(o=>(r[o]===void 0,!0)),s=(r,o,e=!0)=>[typeof r.tablename=="string"&&(1<=r.tablename.length||u(e,{path:o+".tablename",expected:"string & MinLength<1>",value:r.tablename}))||u(e,{path:o+".tablename",expected:"(string & MinLength<1>)",value:r.tablename}),(Array.isArray(r.rows)||u(e,{path:o+".rows",expected:"(Array<Record<string, unknown>> & MinItems<1>)",value:r.rows}))&&(1<=r.rows.length||u(e,{path:o+".rows",expected:"Array<> & MinItems<1>",value:r.rows}))&&r.rows.map((n,c)=>(typeof n=="object"&&n!==null&&Array.isArray(n)===!1||u(e,{path:o+".rows["+c+"]",expected:"Record<string, unknown>",value:n}))&&a(n,o+".rows["+c+"]",e)||u(e,{path:o+".rows["+c+"]",expected:"Record<string, unknown>",value:n})).every(n=>n)||u(e,{path:o+".rows",expected:"(Array<Record<string, unknown>> & MinItems<1>)",value:r.rows}),r.kwargs===void 0||(typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs}))&&a(r.kwargs,o+".kwargs",e)||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs})].every(n=>n),a=(r,o,e=!0)=>[e===!1||Object.keys(r).map(n=>(r[n]===void 0,!0)).every(n=>n)].every(n=>n),l=r=>typeof r=="object"&&r!==null&&h(r);let i,u;return T(r=>{if(l(r)===!1){i=[],u=_(i),((e,n,c=!0)=>(typeof e=="object"&&e!==null||u(!0,{path:n+"",expected:"BulkTableParams",value:e}))&&s(e,n+"",!0)||u(!0,{path:n+"",expected:"BulkTableParams",value:e}))(r,"$input",!0);const o=i.length===0;return o?{success:o,data:r}:{success:o,errors:i,data:r}}return{success:!0,data:r}})})(),ie={"sys.appaccess.error.no_grant":"NO_GRANT","sys.appaccess.error.provider_not_installed":"PROVIDER_NOT_INSTALLED","sys.appaccess.error.unknown_app":"UNKNOWN_APP","wamp.error.not_authorized":"NOT_AUTHORIZED","wamp.error.authorization_failed":"NOT_AUTHORIZED","wamp.error.authentication_failed":"NOT_AUTHORIZED"};class E extends Error{constructor(t,s){super(s??t),this.name="CrossAppAccessError",this.code=t}}function M(h){const t=h==null?void 0:h.error;if(typeof t!="string")return null;const s=ie[t];if(!s)return null;const a=h.args,l=Array.isArray(a)&&a.length>0?`: ${JSON.stringify(a[0])}`:"";return new E(s,`${t}${l}`)}class Y{constructor(t,s,a,l,i){this.app=t,this.stage=s,this.tables=a.tables??[],this.transforms=a.transforms??[],this._connection=l,this._onClosed=i}get connection(){return this._connection}get isConnected(){return this._connection.is_open}assertInCatalog(t){if(!t)throw new Error("Tablename must not be empty!");const s=[...this.tables,...this.transforms].map(a=>a.tablename);if(!s.includes(t))throw new E("PRIVATE_TABLE",`'${t}' is not shared by app '${this.app}' (${this.stage}). Available tables/transforms: ${s.length>0?s.join(", "):"none"}`)}async subscribeToTable(t,s,a){this.assertInCatalog(t);const l=await this._connection.subscribe(`transformed.${t}`,s),i=(u,r,o)=>{const e=Array.isArray(u)?u[0]:u,n=Array.isArray(e)?e:e==null?[]:[e];for(const c of n)s([c],r,o)};return await this._connection.subscribe(`transformed.bulk.${t}`,i),l}async getHistory(t,s={limit:10}){this.assertInCatalog(t),s.offset==null&&(s.offset=0);const a=P(s);if(!a.success)throw new Error(`Invalid query parameters: ${a.errors.map(l=>l.path+": "+l.expected).join(", ")}`);try{return await this._connection.call(`history.transformed.${t}`,[s])}catch(l){throw M(l)??l}}async getSeriesHistory(t,s){if(!t)throw new Error("Tablename must not be empty!");const a=this.tables.map(i=>i.tablename);if(!a.includes(t))throw this.transforms.some(i=>i.tablename===t)?new E("PRIVATE_TABLE",`'${t}' is a transform of app '${this.app}' (${this.stage}); series history is available for tables only — use getHistory() instead.`):new E("PRIVATE_TABLE",`'${t}' is not a shared table of app '${this.app}' (${this.stage}). Available tables: ${a.length>0?a.join(", "):"none"}`);const l=j(s);if(!l.success)throw new Error(`Invalid series query parameters: ${l.errors.map(i=>i.path+": "+i.expected).join(", ")}`);try{return await this._connection.call(`history.transformed.series.${t}`,[s])}catch(i){throw M(i)??i}}async close(){var t;this._connection.stop(),(t=this._onClosed)==null||t.call(this),this._onClosed=void 0}}const L="error-logs";function b(h){var t;return typeof process<"u"?(t=process.env)==null?void 0:t[h]:void 0}function ce(h){const t=h??b("DEVICE_SERIAL_NUMBER");if(!t)throw new Error("serialNumber option is required (or set DEVICE_SERIAL_NUMBER env var in Node.js)");return t}class x{constructor(t){this._isConfigured=!1,this._consumedApps=new Map,this._serialNumber=ce(t==null?void 0:t.serialNumber),this._deviceName=(t==null?void 0:t.deviceName)??b("DEVICE_NAME"),this._deviceKey=(t==null?void 0:t.deviceKey)??b("DEVICE_KEY"),this._appName=(t==null?void 0:t.appName)??b("APP_NAME"),this._swarmKey=(t==null?void 0:t.swarmKey)??parseInt(b("SWARM_KEY")??"0",10),this._appKey=(t==null?void 0:t.appKey)??parseInt(b("APP_KEY")??"0",10),this._env=(t==null?void 0:t.env)??b("ENV"),this._reswarmUrl=(t==null?void 0:t.reswarmUrl)??b("RESWARM_URL"),this._cburl=(t==null?void 0:t.ironFlockUrl)??(t==null?void 0:t.cburl),this._connection=new k;const s=[];this._deviceKey||s.push("DEVICE_KEY"),this._appName||s.push("APP_NAME"),this._swarmKey||s.push("SWARM_KEY"),this._appKey||s.push("APP_KEY"),s.length>0&&console.warn(`Warning: The following environment variables must be present: ${s.join(", ")}`)}get connection(){return this._connection}get isConnected(){return this._connection.is_open}configureConnection(t){if(this._isConfigured)return;const s=(this._env??"DEV").toUpperCase(),l={DEV:R.DEVELOPMENT,PROD:R.PRODUCTION}[s]??R.DEVELOPMENT,i=t??this._cburl??k.getWebSocketURI(this._reswarmUrl);this._connection.configure(this._swarmKey,this._appKey,l,this._serialNumber,i),this._isConfigured=!0}async start(t){this.configureConnection(t),await this._connection.start()}async stop(){const t=[...this._consumedApps.values()];this._consumedApps.clear(),await Promise.all(t.map(s=>s.then(a=>a.close()).catch(()=>{}))),this._connection.stop()}async connectToApp(t,s){if(!t)throw new Error("appName must not be empty!");const a=(this._env??"DEV").toUpperCase()==="PROD"?"prod":"dev",l=(s==null?void 0:s.stage)??a,i=`${t.toLowerCase()}:${l}`,u=this._consumedApps.get(i);if(u)return u;let r;const o=()=>{this._consumedApps.get(i)===r&&this._consumedApps.delete(i)};return r=this.openConsumedApp(t,l,o,s==null?void 0:s.onError),this._consumedApps.set(i,r),r.catch(o),r}async listConsumableApps(){try{return await this._connection.call("sys.appaccess.list",[])??[]}catch(t){throw M(t)??t}}async connectToAllApps(t){var e,n;const s=(this._env??"DEV").toUpperCase()==="PROD"?"prod":"dev",a=(t==null?void 0:t.stage)??s,l=(t==null?void 0:t.continueOnError)??!0,i=await this.listConsumableApps(),u=[];for(const c of i)(e=c.stages)!=null&&e[a]&&u.push(this.openCachedFromInfo(c,a,t==null?void 0:t.onError));const r=await Promise.allSettled(u),o=[];for(const c of r)if(c.status==="fulfilled")o.push(c.value);else if(l)(n=t==null?void 0:t.onError)==null||n.call(t,c.reason);else throw c.reason;return o}async openConsumedApp(t,s,a,l){let i;try{i=await this._connection.call("sys.appaccess.resolve",[{app:t}])}catch(u){throw M(u)??u}if(!i)throw new E("PROVIDER_NOT_INSTALLED",`App '${t}' has no ${s} data backend in this project`);return this.openFromInfo(i,s,a,l)}openCachedFromInfo(t,s,a){const l=`${t.app}:${s}`,i=this._consumedApps.get(l);if(i)return i;let u;const r=()=>{this._consumedApps.get(l)===u&&this._consumedApps.delete(l)};return u=this.openFromInfo(t,s,r,a),this._consumedApps.set(l,u),u.catch(r),u}async openFromInfo(t,s,a,l){var n;const i=t.app,u=(n=t.stages)==null?void 0:n[s];if(!u)throw new E("PROVIDER_NOT_INSTALLED",`App '${i}' has no ${s} data backend in this project`);const r=new k;r.failOnAuthError=!0,r.configure(this._swarmKey,t.provider_app_key,s==="prod"?R.PRODUCTION:R.DEVELOPMENT,this._serialNumber,this._connection.socketURI??this._cburl??k.getWebSocketURI(this._reswarmUrl));let o=!1,e;r.on("auth_failure",c=>{e=new E("NOT_AUTHORIZED",`Access to app '${i}' (${s}) denied: ${c}. The grant may have been revoked.`),a(),o&&(l==null||l(e))});try{await r.start()}catch(c){throw e??c}return o=!0,new Y(i,s,u,r,a)}async publish(t,s,a){const l=B({topic:t,args:s,kwargs:a});if(!l.success)throw new Error(`Invalid publish parameters: ${l.errors.map(r=>r.path+": "+r.expected).join(", ")}`);const u={...{DEVICE_SERIAL_NUMBER:this._serialNumber,DEVICE_KEY:this._deviceKey,DEVICE_NAME:this._deviceName},...a??{}};return this._connection.publish(t,s,u,{acknowledge:!0})}async publishToTable(t,s,a){const l=N({tablename:t,args:s,kwargs:a});if(!l.success)throw new Error(`Invalid table parameters: ${l.errors.map(u=>u.path+": "+u.expected).join(", ")}`);if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const i=`${this._swarmKey}.${this._appKey}.${t}`;return this.publish(i,s,a)}async reportError(t,s){const a=t instanceof Error?t.stack??t.message:t,l={tsp:(s==null?void 0:s.tsp)??new Date().toISOString(),msg:a,source:"app",level:(s==null?void 0:s.level)??"error"};return s!=null&&s.append?this.appendToTable(L,[l]):this.publishToTable(L,[l])}async appendToTable(t,s,a){const l=N({tablename:t,args:s,kwargs:a});if(!l.success)throw new Error(`Invalid table parameters: ${l.errors.map(o=>o.path+": "+o.expected).join(", ")}`);if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const i=`append.${this._swarmKey}.${this._appKey}.${t}`,r={...{DEVICE_SERIAL_NUMBER:this._serialNumber,DEVICE_KEY:this._deviceKey,DEVICE_NAME:this._deviceName},...a??{}};return this._connection.call(i,s,r)}async publishRowsToTable(t,s,a){const l=K({tablename:t,rows:s,kwargs:a});if(!l.success)throw new Error(`Invalid bulk table parameters: ${l.errors.map(u=>u.path+": "+u.expected).join(", ")}`);if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const i=`bulk.${this._swarmKey}.${this._appKey}.${t}`;return this.publish(i,[s],a)}async appendRowsToTable(t,s,a){const l=K({tablename:t,rows:s,kwargs:a});if(!l.success)throw new Error(`Invalid bulk table parameters: ${l.errors.map(o=>o.path+": "+o.expected).join(", ")}`);if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const i=`appendBulk.${this._swarmKey}.${this._appKey}.${t}`,r={...{DEVICE_SERIAL_NUMBER:this._serialNumber,DEVICE_KEY:this._deviceKey,DEVICE_NAME:this._deviceName},...a??{}};return this._connection.call(i,[s],r)}async subscribe(t,s,a){return this._connection.subscribe(t,s)}async subscribeToTable(t,s,a){if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const l=`transformed.${t}`,i=await this.subscribe(l,s,a),u=(r,o,e)=>{const n=Array.isArray(r)?r[0]:r,c=Array.isArray(n)?n:n==null?[]:[n];for(const d of c)s([d],o,e)};return await this.subscribe(`transformed.bulk.${t}`,u,a),i}async call(t,s,a,l){return this._connection.call(t,s,a,l)}async callDeviceFunction(t,s,a,l,i){const u=`${this._swarmKey}.${t}.${this._appKey}.${this._env}.${s}`;return this._connection.call(u,a,l,i)}async callFunction(t,s,a,l,i){return console.warn("callFunction() is deprecated and will be removed in a future version. Use callDeviceFunction() instead."),this.callDeviceFunction(t,s,a,l,i)}async registerDeviceFunction(t,s,a){const l=`${this._swarmKey}.${this._deviceKey}.${this._appKey}.${this._env}.${t}`,i=await this._connection.register(l,s,a);return console.log(`Function registered for IronFlock topic '${t}'. (Full WAMP topic: '${l}')`),i}async registerFunction(t,s,a){return console.warn("registerFunction() is deprecated and will be removed in a future version. Use registerDeviceFunction() instead."),this.registerDeviceFunction(t,s,a)}async register(t,s,a){return this.registerDeviceFunction(t,s,a)}async getHistory(t,s={limit:10}){if(!t)throw new Error("Tablename must not be empty!");s.offset==null&&(s.offset=0);const a=P(s);if(!a.success)throw new Error(`Invalid query parameters: ${a.errors.map(i=>i.path+": "+i.expected).join(", ")}`);const l=`history.transformed.${t}`;try{return await this._connection.call(l,[s])}catch(i){const u=String(i);return u.includes("no_such_procedure")||u.includes("no callee registered")?(console.error(`Get history failed: History service procedure '${l}' not registered`),null):(console.error(`Get history failed: ${i}`),null)}}async getSeriesHistory(t,s){if(!t)throw new Error("Tablename must not be empty!");const a=j(s);if(!a.success)throw new Error(`Invalid series query parameters: ${a.errors.map(i=>i.path+": "+i.expected).join(", ")}`);const l=`history.transformed.series.${t}`;try{return await this._connection.call(l,[s])}catch(i){const u=String(i);return u.includes("no_such_procedure")||u.includes("no callee registered")?(console.error(`Get series history failed: History service procedure '${l}' not registered`),null):(console.error(`Get series history failed: ${i}`),null)}}async setDeviceLocation(t,s){const a=W({longitude:t,latitude:s});if(!a.success)throw new Error(`Invalid location parameters: ${a.errors.map(u=>u.path+": "+u.expected).join(", ")}`);const l={long:t,lat:s},i={DEVICE_SERIAL_NUMBER:this._serialNumber,DEVICE_KEY:this._deviceKey,DEVICE_NAME:this._deviceName};try{return await this._connection.call("ironflock.location_service.update",[l],i)}catch(u){return console.error(`Set location failed: ${u}`),null}}getRemoteAccessUrlForPort(t){return this._deviceKey&&this._appName?`https://${this._deviceKey}-${this._appName.toLowerCase()}-${t}.app.ironflock.com`:null}static async fromServer(t){const s=await fetch(t);if(!s.ok)throw new Error(`Failed to fetch IronFlock config from ${t}: ${s.status} ${s.statusText}`);const a=await s.json();return new x(a)}}exports.ConsumedApp=Y;exports.CrossAppAccessError=E;exports.CrossbarConnection=k;exports.ERROR_LOGS_TABLE=L;exports.IronFlock=x;exports.Stage=R;exports.validateBulkTableParams=K;exports.validateCallParams=ae;exports.validateLocationParams=W;exports.validatePublishParams=B;exports.validateSeriesQueryParams=j;exports.validateTableParams=N;exports.validateTableQueryParams=P;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const U=require("autobahn");function G(h){return h&&h.__esModule&&Object.prototype.hasOwnProperty.call(h,"default")?h.default:h}var M={exports:{}},j;function J(){return j||(j=1,(function(h){var t=Object.prototype.hasOwnProperty,s="~";function a(){}Object.create&&(a.prototype=Object.create(null),new a().__proto__||(s=!1));function l(o,e,n){this.fn=o,this.context=e,this.once=n||!1}function i(o,e,n,c,d){if(typeof n!="function")throw new TypeError("The listener must be a function");var v=new l(n,c||o,d),f=s?s+e:e;return o._events[f]?o._events[f].fn?o._events[f]=[o._events[f],v]:o._events[f].push(v):(o._events[f]=v,o._eventsCount++),o}function u(o,e){--o._eventsCount===0?o._events=new a:delete o._events[e]}function r(){this._events=new a,this._eventsCount=0}r.prototype.eventNames=function(){var e=[],n,c;if(this._eventsCount===0)return e;for(c in n=this._events)t.call(n,c)&&e.push(s?c.slice(1):c);return Object.getOwnPropertySymbols?e.concat(Object.getOwnPropertySymbols(n)):e},r.prototype.listeners=function(e){var n=s?s+e:e,c=this._events[n];if(!c)return[];if(c.fn)return[c.fn];for(var d=0,v=c.length,f=new Array(v);d<v;d++)f[d]=c[d].fn;return f},r.prototype.listenerCount=function(e){var n=s?s+e:e,c=this._events[n];return c?c.fn?1:c.length:0},r.prototype.emit=function(e,n,c,d,v,f){var g=s?s+e:e;if(!this._events[g])return!1;var y=this._events[g],b=arguments.length,p,m;if(y.fn){switch(y.once&&this.removeListener(e,y.fn,void 0,!0),b){case 1:return y.fn.call(y.context),!0;case 2:return y.fn.call(y.context,n),!0;case 3:return y.fn.call(y.context,n,c),!0;case 4:return y.fn.call(y.context,n,c,d),!0;case 5:return y.fn.call(y.context,n,c,d,v),!0;case 6:return y.fn.call(y.context,n,c,d,v,f),!0}for(m=1,p=new Array(b-1);m<b;m++)p[m-1]=arguments[m];y.fn.apply(y.context,p)}else{var Q=y.length,I;for(m=0;m<Q;m++)switch(y[m].once&&this.removeListener(e,y[m].fn,void 0,!0),b){case 1:y[m].fn.call(y[m].context);break;case 2:y[m].fn.call(y[m].context,n);break;case 3:y[m].fn.call(y[m].context,n,c);break;case 4:y[m].fn.call(y[m].context,n,c,d);break;default:if(!p)for(I=1,p=new Array(b-1);I<b;I++)p[I-1]=arguments[I];y[m].fn.apply(y[m].context,p)}}return!0},r.prototype.on=function(e,n,c){return i(this,e,n,c,!1)},r.prototype.once=function(e,n,c){return i(this,e,n,c,!0)},r.prototype.removeListener=function(e,n,c,d){var v=s?s+e:e;if(!this._events[v])return this;if(!n)return u(this,v),this;var f=this._events[v];if(f.fn)f.fn===n&&(!d||f.once)&&(!c||f.context===c)&&u(this,v);else{for(var g=0,y=[],b=f.length;g<b;g++)(f[g].fn!==n||d&&!f[g].once||c&&f[g].context!==c)&&y.push(f[g]);y.length?this._events[v]=y.length===1?y[0]:y:u(this,v)}return this},r.prototype.removeAllListeners=function(e){var n;return e?(n=s?s+e:e,this._events[n]&&u(this,n)):(this._events=new a,this._eventsCount=0),this},r.prototype.off=r.prototype.removeListener,r.prototype.addListener=r.prototype.on,r.prefixed=s,r.EventEmitter=r,h.exports=r})(M)),M.exports}var Z=J();const z=G(Z),X="wss://cbw.datapods.io/ws-ua-usr",W="wss://cbw.ironflock.com/ws-ua-usr",q="wss://cbw.ironflock.dev/ws-ua-usr",ee="wss://cbw.record-evolution.com/ws-ua-usr",x="ws://localhost:8080/ws-ua-usr",re="ws://host.docker.internal:8080/ws-ua-usr",F={"https://studio.datapods.io":X,"https://studio.ironflock.dev":q,"https://studio.ironflock.com":W,"https://studio.record-evolution.com":ee,"http://localhost:8085":x,"http://localhost:8086":x,"http://host.docker.internal:8086":re},te=6e3,se=["wamp.error.not_authorized","wamp.error.authorization_failed","wamp.error.authentication_failed","wamp.error.no_auth_method"];class _ extends z{constructor(){super(),this.failOnAuthError=!1,this.subscriptions=[],this.registrations=[],this.firstResolver=()=>{},this.firstRejecter=()=>{},this.shouldReconnect=!1,this.isReconnecting=!1,this.hasOpenedOnce=!1}static getWebSocketURI(t){var l;const s=typeof process<"u"?(l=process.env)==null?void 0:l.DEVICE_ENDPOINT_URL:void 0;if(s)try{const i=new URL(s);return i.pathname="/ws-ua-usr",i.toString().replace(/\/$/,"")}catch{}if(!t)return W;const a=F[t];if(a===void 0)throw new Error(`Cannot resolve WebSocket URI for reswarmUrl=${JSON.stringify(t)}. Set DEVICE_ENDPOINT_URL to the deployment's WAMP router URL (e.g. ws://<host>:18080/ws-re-dev) or pass ironFlockUrl=… to IronFlock(). Known cloud URLs: ${JSON.stringify(Object.keys(F))}`);return a}configure(t,s,a,l,i){this.realm=`realm-${t}-${s}-${a}`,this.serial_number=l,this.socketURI=i??_.getWebSocketURI()}async start(){if(!this.realm||!this.serial_number||!this.socketURI)throw new Error("CrossbarConnection must be configured before starting. Call configure() first.");return this.shouldReconnect=!0,this.firstConnection=new Promise((t,s)=>{this.firstResolver=t,this.firstRejecter=s}),this.connection=new U.Connection({realm:this.realm,url:this.socketURI,authmethods:["wampcra"],authid:this.serial_number,max_retries:-1,max_retry_delay:2,initial_retry_delay:1,autoping_interval:2,autoping_timeout:4,onchallenge:(t,s,a)=>U.auth_cra.sign(this.serial_number,a.challenge)}),this.connection.onopen=this.onOpen.bind(this),this.connection.onclose=this.onClose.bind(this),this.connection.open(),this.firstConnection}onOpen(t){this.session=t,this.session.caller_disclose_me=!0,this.hasOpenedOnce=!0,this.resubscribeAll(),this.reregisterAll(),this.emit("connected",this.serial_number),this.firstResolver(),this.firstResolver=()=>{}}onClose(t,s){this.session=void 0,this.emit("disconnected"),this.firstRejecter(new Error(`Connection failed: ${t} - ${JSON.stringify(s,null,2)}`)),this.firstRejecter=()=>{},console.warn("Connection closed:",{reason:t,details:s,realm:this.realm,url:this.socketURI,authid:this.serial_number});const a=(s==null?void 0:s.reason)??t;return this.failOnAuthError&&typeof a=="string"&&se.includes(a)?(this.shouldReconnect=!1,this.emit("auth_failure",a,s),!0):(this.shouldReconnect&&this.hasOpenedOnce&&(s==null?void 0:s.will_retry)===!1&&this.handleManualReconnect((s==null?void 0:s.reason)??t),!1)}async handleManualReconnect(t){if(!(this.isReconnecting||!this.shouldReconnect)){for(this.isReconnecting=!0;this.shouldReconnect&&!this.session;)if(await new Promise(s=>setTimeout(s,1e3)),this.shouldReconnect&&this.connection&&!this.session){console.log(`Manually retrying connection after ${t}...`);try{this.connection.open(),await new Promise(s=>setTimeout(s,500))}catch(s){console.error("Error during manual reconnection attempt:",s)}}this.isReconnecting=!1}}async sessionWait(){const t=Date.now();for(;!this.session;){if(Date.now()-t>te)throw new Error("Timeout waiting for session");await new Promise(s=>setTimeout(s,200))}}async waitForConnection(){return this.firstConnection}getSession(){return this.session}get is_open(){var t;return!!((t=this.session)!=null&&t.isOpen)}isConnected(){return this.is_open}stop(){var t;this.shouldReconnect=!1,(t=this.connection)==null||t.close("wamp.close.normal","Connection closed by client")}async subscribe(t,s){var l;await this.sessionWait();const a=await((l=this.session)==null?void 0:l.subscribe(t,s));return a&&this.subscriptions.push(a),a}async unsubscribe(t){var a;await this.sessionWait();const s=this.subscriptions.indexOf(t);s!==-1&&(this.subscriptions.splice(s,1),await((a=this.session)==null?void 0:a.unsubscribe(t)))}async unsubscribeTopic(t){const s=this.subscriptions.filter(a=>a.topic===t);for(const a of s)await this.unsubscribe(a)}async resubscribeAll(){const t=[...this.subscriptions];this.subscriptions=[],await Promise.all(t.map(s=>this.subscribe(s.topic,s.handler)))}async reregisterAll(){const t=[...this.registrations];this.registrations=[],await Promise.all(t.map(s=>this.register(s.procedure,s.endpoint,s.options)))}async register(t,s,a){var u;await this.sessionWait();const l={force_reregister:!0,...a??{}},i=await((u=this.session)==null?void 0:u.register(t,s,l));return i&&this.registrations.push(i),i}async unregister(t){var a;await this.sessionWait(),await((a=this.session)==null?void 0:a.unregister(t));const s=this.registrations.indexOf(t);s!==-1&&this.registrations.splice(s,1)}async call(t,s,a,l){var i;return await this.sessionWait(),(i=this.session)==null?void 0:i.call(t,s,a,l)}async publish(t,s,a,l){var i;return await this.sessionWait(),(i=this.session)==null?void 0:i.publish(t,s,a,l)}}const $=h=>Math.floor(h)===h&&ne<=h&&h<=oe,ne=0,oe=2**32-1,O=h=>ae.test(h),ae=/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(T|\s)([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:\.[0-9]{1,9})?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])$/i,k=h=>{const t=s=>{if(h.length===0)return!0;const a=h[h.length-1].path;return s.length>a.length||a.substring(0,s.length)!==s};return(s,a)=>(s&&t(a.path)&&(a.value===void 0&&(a.description??(a.description=["The value at this path is `undefined`.","",`Please fill the \`${a.expected}\` typed value next time.`].join(`
|
|
2
|
+
`))),h.push(a)),!1)},T=h=>Object.assign(h,{"~standard":{version:1,vendor:"typia",validate:t=>{const s=h(t);return s.success?{value:s.data}:{issues:s.errors.map(a=>({message:`expected ${a.expected}, got ${a.value}`,path:ie(a.path)}))}}}});var w;(function(h){h[h.Start=0]="Start",h[h.Property=1]="Property",h[h.StringKey=2]="StringKey",h[h.NumberKey=3]="NumberKey"})(w||(w={}));const ie=h=>{if(!h.startsWith("$input"))throw new Error(`Invalid path: ${JSON.stringify(h)}`);const t=[];let s="",a=w.Start,l=5;for(;l<h.length-1;){l++;const i=h[l];if(a===w.Property?i==="."||i==="["?(t.push({key:s}),a=w.Start):l===h.length-1?(s+=i,t.push({key:s}),l++,a=w.Start):s+=i:a===w.StringKey?i==='"'?(t.push({key:JSON.parse(s+i)}),l+=2,a=w.Start):i==="\\"?(s+=h[l],l++,s+=h[l]):s+=i:a===w.NumberKey&&(i==="]"?(t.push({key:Number.parseInt(s)}),l++,a=w.Start):s+=i),a===w.Start&&l<h.length-1){const u=h[l];if(s="",u==="[")h[l+1]==='"'?(a=w.StringKey,l++,s='"'):a=w.NumberKey;else if(u===".")a=w.Property;else throw new Error("Unreachable: pointer points invalid character")}}if(a!==w.Start)throw new Error(`Failed to parse path: ${JSON.stringify(h)}`);return t};var R=(h=>(h.DEVELOPMENT="dev",h.PRODUCTION="prod",h))(R||{});const K=(()=>{const h=e=>typeof e.limit=="number"&&$(e.limit)&&1<=e.limit&&e.limit<=1e4&&(e.offset===void 0||typeof e.offset=="number"&&$(e.offset)&&0<=e.offset)&&(e.timeRange===void 0||typeof e.timeRange=="object"&&e.timeRange!==null&&t(e.timeRange))&&(e.filterAnd===void 0||Array.isArray(e.filterAnd)&&e.filterAnd.every(n=>typeof n=="object"&&n!==null&&s(n))),t=e=>typeof e.start=="string"&&O(e.start)&&typeof e.end=="string"&&O(e.end),s=e=>typeof e.column=="string"&&1<=e.column.length&&typeof e.operator=="string"&&1<=e.operator.length&&e.value!==null&&e.value!==void 0&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"||Array.isArray(e.value)&&e.value.every(n=>typeof n=="string"||typeof n=="number")),a=(e,n,c=!0)=>[typeof e.limit=="number"&&($(e.limit)||o(c,{path:n+".limit",expected:'number & Type<"uint32">',value:e.limit}))&&(1<=e.limit||o(c,{path:n+".limit",expected:"number & Minimum<1>",value:e.limit}))&&(e.limit<=1e4||o(c,{path:n+".limit",expected:"number & Maximum<10000>",value:e.limit}))||o(c,{path:n+".limit",expected:'(number & Type<"uint32"> & Minimum<1> & Maximum<10000>)',value:e.limit}),e.offset===void 0||typeof e.offset=="number"&&($(e.offset)||o(c,{path:n+".offset",expected:'number & Type<"uint32">',value:e.offset}))&&(0<=e.offset||o(c,{path:n+".offset",expected:"number & Minimum<0>",value:e.offset}))||o(c,{path:n+".offset",expected:'((number & Type<"uint32"> & Minimum<0>) | undefined)',value:e.offset}),e.timeRange===void 0||(typeof e.timeRange=="object"&&e.timeRange!==null||o(c,{path:n+".timeRange",expected:"(ISOTimeRange | undefined)",value:e.timeRange}))&&l(e.timeRange,n+".timeRange",c)||o(c,{path:n+".timeRange",expected:"(ISOTimeRange | undefined)",value:e.timeRange}),e.filterAnd===void 0||(Array.isArray(e.filterAnd)||o(c,{path:n+".filterAnd",expected:"(Array<SQLFilterAnd> | undefined)",value:e.filterAnd}))&&e.filterAnd.map((d,v)=>(typeof d=="object"&&d!==null||o(c,{path:n+".filterAnd["+v+"]",expected:"SQLFilterAnd",value:d}))&&i(d,n+".filterAnd["+v+"]",c)||o(c,{path:n+".filterAnd["+v+"]",expected:"SQLFilterAnd",value:d})).every(d=>d)||o(c,{path:n+".filterAnd",expected:"(Array<SQLFilterAnd> | undefined)",value:e.filterAnd})].every(d=>d),l=(e,n,c=!0)=>[typeof e.start=="string"&&(O(e.start)||o(c,{path:n+".start",expected:'string & Format<"date-time">',value:e.start}))||o(c,{path:n+".start",expected:'(string & Format<"date-time">)',value:e.start}),typeof e.end=="string"&&(O(e.end)||o(c,{path:n+".end",expected:'string & Format<"date-time">',value:e.end}))||o(c,{path:n+".end",expected:'(string & Format<"date-time">)',value:e.end})].every(d=>d),i=(e,n,c=!0)=>[typeof e.column=="string"&&(1<=e.column.length||o(c,{path:n+".column",expected:"string & MinLength<1>",value:e.column}))||o(c,{path:n+".column",expected:"(string & MinLength<1>)",value:e.column}),typeof e.operator=="string"&&(1<=e.operator.length||o(c,{path:n+".operator",expected:"string & MinLength<1>",value:e.operator}))||o(c,{path:n+".operator",expected:"(string & MinLength<1>)",value:e.operator}),(e.value!==null||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&(e.value!==void 0||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"||(Array.isArray(e.value)||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&e.value.map((d,v)=>typeof d=="string"||typeof d=="number"||o(c,{path:n+".value["+v+"]",expected:"(number | string)",value:d})).every(d=>d)||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))].every(d=>d),u=e=>typeof e=="object"&&e!==null&&h(e);let r,o;return T(e=>{if(u(e)===!1){r=[],o=k(r),((c,d,v=!0)=>(typeof c=="object"&&c!==null||o(!0,{path:d+"",expected:"TableQueryParams",value:c}))&&a(c,d+"",!0)||o(!0,{path:d+"",expected:"TableQueryParams",value:c}))(e,"$input",!0);const n=r.length===0;return n?{success:n,data:e}:{success:n,errors:r,data:e}}return{success:!0,data:e}})})(),P=(()=>{const h=e=>{const n=e,c=[[d=>d.length===2&&(d[0]===null||typeof d[0]=="number")&&(d[1]===null||typeof d[1]=="number"),d=>d.length===2&&(d[0]===null||typeof d[0]=="number")&&(d[1]===null||typeof d[1]=="number")],[d=>d.length===2&&(d[0]===null||typeof d[0]=="string")&&(d[1]===null||typeof d[1]=="string"),d=>d.length===2&&(d[0]===null||typeof d[0]=="string")&&(d[1]===null||typeof d[1]=="string")]];for(const d of c)if(d[0](n))return d[1](n);return!1},t=(e,n,c=!0)=>{const d=e,v=[[f=>f.length===2&&[f[0]===null||typeof f[0]=="number",f[1]===null||typeof f[1]=="number"].every(g=>g),f=>(f.length===2||o(c,{path:n,expected:"[(null | number), (null | number)]",value:f}))&&[f[0]===null||typeof f[0]=="number"||o(c,{path:n+"[0]",expected:"(null | number)",value:f[0]}),f[1]===null||typeof f[1]=="number"||o(c,{path:n+"[1]",expected:"(null | number)",value:f[1]})].every(g=>g)],[f=>f.length===2&&[f[0]===null||typeof f[0]=="string",f[1]===null||typeof f[1]=="string"].every(g=>g),f=>(f.length===2||o(c,{path:n,expected:"[(null | string), (null | string)]",value:f}))&&[f[0]===null||typeof f[0]=="string"||o(c,{path:n+"[0]",expected:"(null | string)",value:f[0]}),f[1]===null||typeof f[1]=="string"||o(c,{path:n+"[1]",expected:"(null | string)",value:f[1]})].every(g=>g)]];for(const f of v)if(f[0](d))return f[1](d);return o(c,{path:n,expected:"([number | null, number | null] | [string | null, string | null])",value:e})},s=e=>Array.isArray(e.metrics)&&e.metrics.every(n=>typeof n=="string")&&(e.method==="AVG"||e.method==="SUM"||e.method==="COUNT"||e.method==="MIN"||e.method==="MAX"||e.method==="FIRST"||e.method==="LAST")&&typeof e.limit=="number"&&$(e.limit)&&1<=e.limit&&e.limit<=1e4&&Array.isArray(e.timeRange)&&(h(e.timeRange)||!1)&&(e.groupBy===null||e.groupBy===void 0||Array.isArray(e.groupBy)&&e.groupBy.every(n=>typeof n=="string"))&&(e.filterAnd===null||e.filterAnd===void 0||Array.isArray(e.filterAnd)&&e.filterAnd.every(n=>typeof n=="object"&&n!==null&&a(n))),a=e=>typeof e.column=="string"&&1<=e.column.length&&typeof e.operator=="string"&&1<=e.operator.length&&e.value!==null&&e.value!==void 0&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"||Array.isArray(e.value)&&e.value.every(n=>typeof n=="string"||typeof n=="number")),l=(e,n,c=!0)=>[(Array.isArray(e.metrics)||o(c,{path:n+".metrics",expected:"Array<string>",value:e.metrics}))&&e.metrics.map((d,v)=>typeof d=="string"||o(c,{path:n+".metrics["+v+"]",expected:"string",value:d})).every(d=>d)||o(c,{path:n+".metrics",expected:"Array<string>",value:e.metrics}),e.method==="AVG"||e.method==="SUM"||e.method==="COUNT"||e.method==="MIN"||e.method==="MAX"||e.method==="FIRST"||e.method==="LAST"||o(c,{path:n+".method",expected:'("AVG" | "COUNT" | "FIRST" | "LAST" | "MAX" | "MIN" | "SUM")',value:e.method}),typeof e.limit=="number"&&($(e.limit)||o(c,{path:n+".limit",expected:'number & Type<"uint32">',value:e.limit}))&&(1<=e.limit||o(c,{path:n+".limit",expected:"number & Minimum<1>",value:e.limit}))&&(e.limit<=1e4||o(c,{path:n+".limit",expected:"number & Maximum<10000>",value:e.limit}))||o(c,{path:n+".limit",expected:'(number & Type<"uint32"> & Minimum<1> & Maximum<10000>)',value:e.limit}),(Array.isArray(e.timeRange)||o(c,{path:n+".timeRange",expected:"([number | null, number | null] | [string | null, string | null])",value:e.timeRange}))&&(t(e.timeRange,n+".timeRange",c)||o(c,{path:n+".timeRange",expected:"[number | null, number | null] | [string | null, string | null]",value:e.timeRange}))||o(c,{path:n+".timeRange",expected:"([number | null, number | null] | [string | null, string | null])",value:e.timeRange}),e.groupBy===null||e.groupBy===void 0||(Array.isArray(e.groupBy)||o(c,{path:n+".groupBy",expected:"(Array<string> | null | undefined)",value:e.groupBy}))&&e.groupBy.map((d,v)=>typeof d=="string"||o(c,{path:n+".groupBy["+v+"]",expected:"string",value:d})).every(d=>d)||o(c,{path:n+".groupBy",expected:"(Array<string> | null | undefined)",value:e.groupBy}),e.filterAnd===null||e.filterAnd===void 0||(Array.isArray(e.filterAnd)||o(c,{path:n+".filterAnd",expected:"(Array<SQLFilterAnd> | null | undefined)",value:e.filterAnd}))&&e.filterAnd.map((d,v)=>(typeof d=="object"&&d!==null||o(c,{path:n+".filterAnd["+v+"]",expected:"SQLFilterAnd",value:d}))&&i(d,n+".filterAnd["+v+"]",c)||o(c,{path:n+".filterAnd["+v+"]",expected:"SQLFilterAnd",value:d})).every(d=>d)||o(c,{path:n+".filterAnd",expected:"(Array<SQLFilterAnd> | null | undefined)",value:e.filterAnd})].every(d=>d),i=(e,n,c=!0)=>[typeof e.column=="string"&&(1<=e.column.length||o(c,{path:n+".column",expected:"string & MinLength<1>",value:e.column}))||o(c,{path:n+".column",expected:"(string & MinLength<1>)",value:e.column}),typeof e.operator=="string"&&(1<=e.operator.length||o(c,{path:n+".operator",expected:"string & MinLength<1>",value:e.operator}))||o(c,{path:n+".operator",expected:"(string & MinLength<1>)",value:e.operator}),(e.value!==null||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&(e.value!==void 0||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&(typeof e.value=="string"||typeof e.value=="number"||typeof e.value=="boolean"||(Array.isArray(e.value)||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))&&e.value.map((d,v)=>typeof d=="string"||typeof d=="number"||o(c,{path:n+".value["+v+"]",expected:"(number | string)",value:d})).every(d=>d)||o(c,{path:n+".value",expected:"(Array<string | number> | boolean | number | string)",value:e.value}))].every(d=>d),u=e=>typeof e=="object"&&e!==null&&s(e);let r,o;return T(e=>{if(u(e)===!1){r=[],o=k(r),((c,d,v=!0)=>(typeof c=="object"&&c!==null||o(!0,{path:d+"",expected:"SeriesQueryParams",value:c}))&&l(c,d+"",!0)||o(!0,{path:d+"",expected:"SeriesQueryParams",value:c}))(e,"$input",!0);const n=r.length===0;return n?{success:n,data:e}:{success:n,errors:r,data:e}}return{success:!0,data:e}})})(),B=(()=>{const h=i=>typeof i.longitude=="number"&&-180<=i.longitude&&i.longitude<=180&&typeof i.latitude=="number"&&-90<=i.latitude&&i.latitude<=90,t=(i,u,r=!0)=>[typeof i.longitude=="number"&&(-180<=i.longitude||l(r,{path:u+".longitude",expected:"number & Minimum<-180>",value:i.longitude}))&&(i.longitude<=180||l(r,{path:u+".longitude",expected:"number & Maximum<180>",value:i.longitude}))||l(r,{path:u+".longitude",expected:"(number & Minimum<-180> & Maximum<180>)",value:i.longitude}),typeof i.latitude=="number"&&(-90<=i.latitude||l(r,{path:u+".latitude",expected:"number & Minimum<-90>",value:i.latitude}))&&(i.latitude<=90||l(r,{path:u+".latitude",expected:"number & Maximum<90>",value:i.latitude}))||l(r,{path:u+".latitude",expected:"(number & Minimum<-90> & Maximum<90>)",value:i.latitude})].every(o=>o),s=i=>typeof i=="object"&&i!==null&&h(i);let a,l;return T(i=>{if(s(i)===!1){a=[],l=k(a),((r,o,e=!0)=>(typeof r=="object"&&r!==null||l(!0,{path:o+"",expected:"LocationParams",value:r}))&&t(r,o+"",!0)||l(!0,{path:o+"",expected:"LocationParams",value:r}))(i,"$input",!0);const u=a.length===0;return u?{success:u,data:i}:{success:u,errors:a,data:i}}return{success:!0,data:i}})})(),Y=(()=>{const h=r=>typeof r.topic=="string"&&1<=r.topic.length&&(r.args===void 0||Array.isArray(r.args))&&(r.kwargs===void 0||typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1&&t(r.kwargs)),t=r=>Object.keys(r).every(o=>(r[o]===void 0,!0)),s=(r,o,e=!0)=>[typeof r.topic=="string"&&(1<=r.topic.length||u(e,{path:o+".topic",expected:"string & MinLength<1>",value:r.topic}))||u(e,{path:o+".topic",expected:"(string & MinLength<1>)",value:r.topic}),r.args===void 0||Array.isArray(r.args)||u(e,{path:o+".args",expected:"(Array<unknown> | undefined)",value:r.args}),r.kwargs===void 0||(typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs}))&&a(r.kwargs,o+".kwargs",e)||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs})].every(n=>n),a=(r,o,e=!0)=>[e===!1||Object.keys(r).map(n=>(r[n]===void 0,!0)).every(n=>n)].every(n=>n),l=r=>typeof r=="object"&&r!==null&&h(r);let i,u;return T(r=>{if(l(r)===!1){i=[],u=k(i),((e,n,c=!0)=>(typeof e=="object"&&e!==null||u(!0,{path:n+"",expected:"PublishParams",value:e}))&&s(e,n+"",!0)||u(!0,{path:n+"",expected:"PublishParams",value:e}))(r,"$input",!0);const o=i.length===0;return o?{success:o,data:r}:{success:o,errors:i,data:r}}return{success:!0,data:r}})})(),ce=(()=>{const h=r=>typeof r.deviceKey=="string"&&1<=r.deviceKey.length&&typeof r.topic=="string"&&1<=r.topic.length&&(r.args===void 0||Array.isArray(r.args))&&(r.kwargs===void 0||typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1&&t(r.kwargs)),t=r=>Object.keys(r).every(o=>(r[o]===void 0,!0)),s=(r,o,e=!0)=>[typeof r.deviceKey=="string"&&(1<=r.deviceKey.length||u(e,{path:o+".deviceKey",expected:"string & MinLength<1>",value:r.deviceKey}))||u(e,{path:o+".deviceKey",expected:"(string & MinLength<1>)",value:r.deviceKey}),typeof r.topic=="string"&&(1<=r.topic.length||u(e,{path:o+".topic",expected:"string & MinLength<1>",value:r.topic}))||u(e,{path:o+".topic",expected:"(string & MinLength<1>)",value:r.topic}),r.args===void 0||Array.isArray(r.args)||u(e,{path:o+".args",expected:"(Array<unknown> | undefined)",value:r.args}),r.kwargs===void 0||(typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs}))&&a(r.kwargs,o+".kwargs",e)||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs})].every(n=>n),a=(r,o,e=!0)=>[e===!1||Object.keys(r).map(n=>(r[n]===void 0,!0)).every(n=>n)].every(n=>n),l=r=>typeof r=="object"&&r!==null&&h(r);let i,u;return T(r=>{if(l(r)===!1){i=[],u=k(i),((e,n,c=!0)=>(typeof e=="object"&&e!==null||u(!0,{path:n+"",expected:"CallParams",value:e}))&&s(e,n+"",!0)||u(!0,{path:n+"",expected:"CallParams",value:e}))(r,"$input",!0);const o=i.length===0;return o?{success:o,data:r}:{success:o,errors:i,data:r}}return{success:!0,data:r}})})(),N=(()=>{const h=r=>typeof r.tablename=="string"&&1<=r.tablename.length&&(r.args===void 0||Array.isArray(r.args))&&(r.kwargs===void 0||typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1&&t(r.kwargs)),t=r=>Object.keys(r).every(o=>(r[o]===void 0,!0)),s=(r,o,e=!0)=>[typeof r.tablename=="string"&&(1<=r.tablename.length||u(e,{path:o+".tablename",expected:"string & MinLength<1>",value:r.tablename}))||u(e,{path:o+".tablename",expected:"(string & MinLength<1>)",value:r.tablename}),r.args===void 0||Array.isArray(r.args)||u(e,{path:o+".args",expected:"(Array<unknown> | undefined)",value:r.args}),r.kwargs===void 0||(typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs}))&&a(r.kwargs,o+".kwargs",e)||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs})].every(n=>n),a=(r,o,e=!0)=>[e===!1||Object.keys(r).map(n=>(r[n]===void 0,!0)).every(n=>n)].every(n=>n),l=r=>typeof r=="object"&&r!==null&&h(r);let i,u;return T(r=>{if(l(r)===!1){i=[],u=k(i),((e,n,c=!0)=>(typeof e=="object"&&e!==null||u(!0,{path:n+"",expected:"TableParams",value:e}))&&s(e,n+"",!0)||u(!0,{path:n+"",expected:"TableParams",value:e}))(r,"$input",!0);const o=i.length===0;return o?{success:o,data:r}:{success:o,errors:i,data:r}}return{success:!0,data:r}})})(),C=(()=>{const h=r=>typeof r.tablename=="string"&&1<=r.tablename.length&&Array.isArray(r.rows)&&1<=r.rows.length&&r.rows.every(o=>typeof o=="object"&&o!==null&&Array.isArray(o)===!1&&t(o))&&(r.kwargs===void 0||typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1&&t(r.kwargs)),t=r=>Object.keys(r).every(o=>(r[o]===void 0,!0)),s=(r,o,e=!0)=>[typeof r.tablename=="string"&&(1<=r.tablename.length||u(e,{path:o+".tablename",expected:"string & MinLength<1>",value:r.tablename}))||u(e,{path:o+".tablename",expected:"(string & MinLength<1>)",value:r.tablename}),(Array.isArray(r.rows)||u(e,{path:o+".rows",expected:"(Array<Record<string, unknown>> & MinItems<1>)",value:r.rows}))&&(1<=r.rows.length||u(e,{path:o+".rows",expected:"Array<> & MinItems<1>",value:r.rows}))&&r.rows.map((n,c)=>(typeof n=="object"&&n!==null&&Array.isArray(n)===!1||u(e,{path:o+".rows["+c+"]",expected:"Record<string, unknown>",value:n}))&&a(n,o+".rows["+c+"]",e)||u(e,{path:o+".rows["+c+"]",expected:"Record<string, unknown>",value:n})).every(n=>n)||u(e,{path:o+".rows",expected:"(Array<Record<string, unknown>> & MinItems<1>)",value:r.rows}),r.kwargs===void 0||(typeof r.kwargs=="object"&&r.kwargs!==null&&Array.isArray(r.kwargs)===!1||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs}))&&a(r.kwargs,o+".kwargs",e)||u(e,{path:o+".kwargs",expected:"(Record<string, unknown> | undefined)",value:r.kwargs})].every(n=>n),a=(r,o,e=!0)=>[e===!1||Object.keys(r).map(n=>(r[n]===void 0,!0)).every(n=>n)].every(n=>n),l=r=>typeof r=="object"&&r!==null&&h(r);let i,u;return T(r=>{if(l(r)===!1){i=[],u=k(i),((e,n,c=!0)=>(typeof e=="object"&&e!==null||u(!0,{path:n+"",expected:"BulkTableParams",value:e}))&&s(e,n+"",!0)||u(!0,{path:n+"",expected:"BulkTableParams",value:e}))(r,"$input",!0);const o=i.length===0;return o?{success:o,data:r}:{success:o,errors:i,data:r}}return{success:!0,data:r}})})(),le={"sys.appaccess.error.no_grant":"NO_GRANT","sys.appaccess.error.provider_not_installed":"PROVIDER_NOT_INSTALLED","sys.appaccess.error.unknown_app":"UNKNOWN_APP","wamp.error.not_authorized":"NOT_AUTHORIZED","wamp.error.authorization_failed":"NOT_AUTHORIZED","wamp.error.authentication_failed":"NOT_AUTHORIZED"};class E extends Error{constructor(t,s){super(s??t),this.name="CrossAppAccessError",this.code=t}}function S(h){const t=h==null?void 0:h.error;if(typeof t!="string")return null;const s=le[t];if(!s)return null;const a=h.args,l=Array.isArray(a)&&a.length>0?`: ${JSON.stringify(a[0])}`:"";return new E(s,`${t}${l}`)}class H{constructor(t,s,a,l,i){this.app=t,this.stage=s,this.tables=a.tables??[],this.transforms=a.transforms??[],this._connection=l,this._onClosed=i}get connection(){return this._connection}get isConnected(){return this._connection.is_open}assertInCatalog(t){if(!t)throw new Error("Tablename must not be empty!");const s=[...this.tables,...this.transforms].map(a=>a.tablename);if(!s.includes(t))throw new E("PRIVATE_TABLE",`'${t}' is not shared by app '${this.app}' (${this.stage}). Available tables/transforms: ${s.length>0?s.join(", "):"none"}`)}async subscribeToTable(t,s,a){this.assertInCatalog(t);const l=await this._connection.subscribe(`transformed.${t}`,s),i=(u,r,o)=>{const e=Array.isArray(u)?u[0]:u,n=Array.isArray(e)?e:e==null?[]:[e];for(const c of n)s([c],r,o)};return await this._connection.subscribe(`transformed.bulk.${t}`,i),l}async getHistory(t,s={limit:10}){this.assertInCatalog(t),s.offset==null&&(s.offset=0);const a=K(s);if(!a.success)throw new Error(`Invalid query parameters: ${a.errors.map(l=>l.path+": "+l.expected).join(", ")}`);try{return await this._connection.call(`history.transformed.${t}`,[s])}catch(l){throw S(l)??l}}async getSeriesHistory(t,s){if(!t)throw new Error("Tablename must not be empty!");const a=this.tables.map(i=>i.tablename);if(!a.includes(t))throw this.transforms.some(i=>i.tablename===t)?new E("PRIVATE_TABLE",`'${t}' is a transform of app '${this.app}' (${this.stage}); series history is available for tables only — use getHistory() instead.`):new E("PRIVATE_TABLE",`'${t}' is not a shared table of app '${this.app}' (${this.stage}). Available tables: ${a.length>0?a.join(", "):"none"}`);const l=P(s);if(!l.success)throw new Error(`Invalid series query parameters: ${l.errors.map(i=>i.path+": "+i.expected).join(", ")}`);try{return await this._connection.call(`history.transformed.series.${t}`,[s])}catch(i){throw S(i)??i}}async close(){var t;this._connection.stop(),(t=this._onClosed)==null||t.call(this),this._onClosed=void 0}}const L="error-logs";function A(h){var t;return typeof process<"u"?(t=process.env)==null?void 0:t[h]:void 0}function V(h){var t;if(typeof process<"u")try{const s=(t=process.getBuiltinModule)==null?void 0:t.call(process,"node:fs");if(s){const a=A("IRONFLOCK_ENV_DIR")??"/data/env",l=s.readFileSync(`${a}/${h}.txt`,"utf8").trim();if(l)return l}}catch{}return A(h)}function ue(h){const t=h??A("DEVICE_SERIAL_NUMBER");if(!t)throw new Error("serialNumber option is required (or set DEVICE_SERIAL_NUMBER env var in Node.js)");return t}class D{constructor(t){this._isConfigured=!1,this._consumedApps=new Map,this._serialNumber=ue(t==null?void 0:t.serialNumber),this._deviceName=(t==null?void 0:t.deviceName)??A("DEVICE_NAME"),this._deviceKey=(t==null?void 0:t.deviceKey)??A("DEVICE_KEY"),this._appName=(t==null?void 0:t.appName)??A("APP_NAME"),this._swarmKey=(t==null?void 0:t.swarmKey)??parseInt(A("SWARM_KEY")??"0",10),this._appKey=(t==null?void 0:t.appKey)??parseInt(A("APP_KEY")??"0",10),this._env=(t==null?void 0:t.env)??A("ENV"),this._reswarmUrl=(t==null?void 0:t.reswarmUrl)??A("RESWARM_URL"),this._cburl=(t==null?void 0:t.ironFlockUrl)??(t==null?void 0:t.cburl),this._connection=new _;const s=[];this._deviceKey||s.push("DEVICE_KEY"),this._appName||s.push("APP_NAME"),this._swarmKey||s.push("SWARM_KEY"),this._appKey||s.push("APP_KEY"),s.length>0&&console.warn(`Warning: The following environment variables must be present: ${s.join(", ")}`)}get connection(){return this._connection}get isConnected(){return this._connection.is_open}configureConnection(t){if(this._isConfigured)return;const s=(this._env??"DEV").toUpperCase(),l={DEV:R.DEVELOPMENT,PROD:R.PRODUCTION}[s]??R.DEVELOPMENT,i=t??this._cburl??_.getWebSocketURI(this._reswarmUrl);this._connection.configure(this._swarmKey,this._appKey,l,this._serialNumber,i),this._isConfigured=!0}async start(t){this.configureConnection(t),await this._connection.start()}async stop(){const t=[...this._consumedApps.values()];this._consumedApps.clear(),await Promise.all(t.map(s=>s.then(a=>a.close()).catch(()=>{}))),this._connection.stop()}async connectToApp(t,s){if(!t)throw new Error("appName must not be empty!");const a=(this._env??"DEV").toUpperCase()==="PROD"?"prod":"dev",l=(s==null?void 0:s.stage)??a,i=`${t.toLowerCase()}:${l}`,u=this._consumedApps.get(i);if(u)return u;let r;const o=()=>{this._consumedApps.get(i)===r&&this._consumedApps.delete(i)};return r=this.openConsumedApp(t,l,o,s==null?void 0:s.onError),this._consumedApps.set(i,r),r.catch(o),r}async listConsumableApps(){try{return await this._connection.call("sys.appaccess.list",[])??[]}catch(t){throw S(t)??t}}async connectToAllApps(t){var e,n;const s=(this._env??"DEV").toUpperCase()==="PROD"?"prod":"dev",a=(t==null?void 0:t.stage)??s,l=(t==null?void 0:t.continueOnError)??!0,i=await this.listConsumableApps(),u=[];for(const c of i)(e=c.stages)!=null&&e[a]&&u.push(this.openCachedFromInfo(c,a,t==null?void 0:t.onError));const r=await Promise.allSettled(u),o=[];for(const c of r)if(c.status==="fulfilled")o.push(c.value);else if(l)(n=t==null?void 0:t.onError)==null||n.call(t,c.reason);else throw c.reason;return o}async openConsumedApp(t,s,a,l){let i;try{i=await this._connection.call("sys.appaccess.resolve",[{app:t}])}catch(u){throw S(u)??u}if(!i)throw new E("PROVIDER_NOT_INSTALLED",`App '${t}' has no ${s} data backend in this project`);return this.openFromInfo(i,s,a,l)}openCachedFromInfo(t,s,a){const l=`${t.app}:${s}`,i=this._consumedApps.get(l);if(i)return i;let u;const r=()=>{this._consumedApps.get(l)===u&&this._consumedApps.delete(l)};return u=this.openFromInfo(t,s,r,a),this._consumedApps.set(l,u),u.catch(r),u}async openFromInfo(t,s,a,l){var n;const i=t.app,u=(n=t.stages)==null?void 0:n[s];if(!u)throw new E("PROVIDER_NOT_INSTALLED",`App '${i}' has no ${s} data backend in this project`);const r=new _;r.failOnAuthError=!0,r.configure(this._swarmKey,t.provider_app_key,s==="prod"?R.PRODUCTION:R.DEVELOPMENT,this._serialNumber,this._connection.socketURI??this._cburl??_.getWebSocketURI(this._reswarmUrl));let o=!1,e;r.on("auth_failure",c=>{e=new E("NOT_AUTHORIZED",`Access to app '${i}' (${s}) denied: ${c}. The grant may have been revoked.`),a(),o&&(l==null||l(e))});try{await r.start()}catch(c){throw e??c}return o=!0,new H(i,s,u,r,a)}async publish(t,s,a){const l=Y({topic:t,args:s,kwargs:a});if(!l.success)throw new Error(`Invalid publish parameters: ${l.errors.map(r=>r.path+": "+r.expected).join(", ")}`);const u={...{DEVICE_SERIAL_NUMBER:this._serialNumber,DEVICE_KEY:this._deviceKey,DEVICE_NAME:this._deviceName},...a??{}};return this._connection.publish(t,s,u,{acknowledge:!0})}async publishToTable(t,s,a){const l=N({tablename:t,args:s,kwargs:a});if(!l.success)throw new Error(`Invalid table parameters: ${l.errors.map(u=>u.path+": "+u.expected).join(", ")}`);if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const i=`${this._swarmKey}.${this._appKey}.${t}`;return this.publish(i,s,a)}async reportError(t,s){const a=t instanceof Error?t.stack??t.message:t,l={tsp:(s==null?void 0:s.tsp)??new Date().toISOString(),msg:a,source:"app",level:(s==null?void 0:s.level)??"error"};return s!=null&&s.append?this.appendToTable(L,[l]):this.publishToTable(L,[l])}async appendToTable(t,s,a){const l=N({tablename:t,args:s,kwargs:a});if(!l.success)throw new Error(`Invalid table parameters: ${l.errors.map(o=>o.path+": "+o.expected).join(", ")}`);if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const i=`append.${this._swarmKey}.${this._appKey}.${t}`,r={...{DEVICE_SERIAL_NUMBER:this._serialNumber,DEVICE_KEY:this._deviceKey,DEVICE_NAME:this._deviceName},...a??{}};return this._connection.call(i,s,r)}async publishRowsToTable(t,s,a){const l=C({tablename:t,rows:s,kwargs:a});if(!l.success)throw new Error(`Invalid bulk table parameters: ${l.errors.map(u=>u.path+": "+u.expected).join(", ")}`);if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const i=`bulk.${this._swarmKey}.${this._appKey}.${t}`;return this.publish(i,[s],a)}async appendRowsToTable(t,s,a){const l=C({tablename:t,rows:s,kwargs:a});if(!l.success)throw new Error(`Invalid bulk table parameters: ${l.errors.map(o=>o.path+": "+o.expected).join(", ")}`);if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const i=`appendBulk.${this._swarmKey}.${this._appKey}.${t}`,r={...{DEVICE_SERIAL_NUMBER:this._serialNumber,DEVICE_KEY:this._deviceKey,DEVICE_NAME:this._deviceName},...a??{}};return this._connection.call(i,[s],r)}async subscribe(t,s,a){return this._connection.subscribe(t,s)}async subscribeToTable(t,s,a){if(!this._swarmKey)throw new Error("SWARM_KEY not set in environment variables!");if(!this._appKey)throw new Error("APP_KEY not set in environment variables!");const l=`transformed.${t}`,i=await this.subscribe(l,s,a),u=(r,o,e)=>{const n=Array.isArray(r)?r[0]:r,c=Array.isArray(n)?n:n==null?[]:[n];for(const d of c)s([d],o,e)};return await this.subscribe(`transformed.bulk.${t}`,u,a),i}async call(t,s,a,l){return this._connection.call(t,s,a,l)}async callDeviceFunction(t,s,a,l,i){const u=`${this._swarmKey}.${t}.${this._appKey}.${this._env}.${s}`;return this._connection.call(u,a,l,i)}async callFunction(t,s,a,l,i){return console.warn("callFunction() is deprecated and will be removed in a future version. Use callDeviceFunction() instead."),this.callDeviceFunction(t,s,a,l,i)}async registerDeviceFunction(t,s,a){const l=`${this._swarmKey}.${this._deviceKey}.${this._appKey}.${this._env}.${t}`,i=await this._connection.register(l,s,a);return console.log(`Function registered for IronFlock topic '${t}'. (Full WAMP topic: '${l}')`),i}async registerFunction(t,s,a){return console.warn("registerFunction() is deprecated and will be removed in a future version. Use registerDeviceFunction() instead."),this.registerDeviceFunction(t,s,a)}async register(t,s,a){return this.registerDeviceFunction(t,s,a)}async getHistory(t,s={limit:10}){if(!t)throw new Error("Tablename must not be empty!");s.offset==null&&(s.offset=0);const a=K(s);if(!a.success)throw new Error(`Invalid query parameters: ${a.errors.map(i=>i.path+": "+i.expected).join(", ")}`);const l=`history.transformed.${t}`;try{return await this._connection.call(l,[s])}catch(i){const u=String(i);return u.includes("no_such_procedure")||u.includes("no callee registered")?(console.error(`Get history failed: History service procedure '${l}' not registered`),null):(console.error(`Get history failed: ${i}`),null)}}async getSeriesHistory(t,s){if(!t)throw new Error("Tablename must not be empty!");const a=P(s);if(!a.success)throw new Error(`Invalid series query parameters: ${a.errors.map(i=>i.path+": "+i.expected).join(", ")}`);const l=`history.transformed.series.${t}`;try{return await this._connection.call(l,[s])}catch(i){const u=String(i);return u.includes("no_such_procedure")||u.includes("no callee registered")?(console.error(`Get series history failed: History service procedure '${l}' not registered`),null):(console.error(`Get series history failed: ${i}`),null)}}async setDeviceLocation(t,s){const a=B({longitude:t,latitude:s});if(!a.success)throw new Error(`Invalid location parameters: ${a.errors.map(u=>u.path+": "+u.expected).join(", ")}`);const l={long:t,lat:s},i={DEVICE_SERIAL_NUMBER:this._serialNumber,DEVICE_KEY:this._deviceKey,DEVICE_NAME:this._deviceName};try{return await this._connection.call("ironflock.location_service.update",[l],i)}catch(u){return console.error(`Set location failed: ${u}`),null}}getRemoteAccessUrlForPort(t,s="http",a){const l=A("INSTANCE_KEY");if(s==="tcp"||s==="udp"){if(!a)return null;if(l){const e=V(`${a}_CLOUD`);if(e){const n=A("CLOUD_TUNNEL_DOMAIN")??"app.ironflock.com";return`${s}://${n}:${e}`}}const r=V(a);if(!r)return null;const o=A("TUNNEL_DOMAIN")??"app.ironflock.com";return`${s}://${o}:${r}`}if(s!=="http"&&s!=="https"||!this._deviceKey||!this._appName)return null;let i=`${this._deviceKey}-${this._appName.toLowerCase()}-${t}`;s==="https"&&(i=`secure-${i}`);let u;return l?(i=`i${l}-${i}`,u=A("CLOUD_TUNNEL_DOMAIN")??"app.ironflock.com"):u=A("TUNNEL_DOMAIN")??"app.ironflock.com",i.length>63||i.includes(".")?null:`https://${i}.${u}`}static async fromServer(t){const s=await fetch(t);if(!s.ok)throw new Error(`Failed to fetch IronFlock config from ${t}: ${s.status} ${s.statusText}`);const a=await s.json();return new D(a)}}exports.ConsumedApp=H;exports.CrossAppAccessError=E;exports.CrossbarConnection=_;exports.ERROR_LOGS_TABLE=L;exports.IronFlock=D;exports.Stage=R;exports.validateBulkTableParams=C;exports.validateCallParams=ce;exports.validateLocationParams=B;exports.validatePublishParams=Y;exports.validateSeriesQueryParams=P;exports.validateTableParams=N;exports.validateTableQueryParams=K;
|
|
3
3
|
//# sourceMappingURL=index.cjs.map
|