UNPKG

@digitalpersona/devices

Version:
3 lines (2 loc) 10.9 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@digitalpersona/core"),require("WebSdk")):"function"==typeof define&&define.amd?define(["exports","@digitalpersona/core","WebSdk"],t):t(((e=e||self).dp=e.dp||{},e.dp.devices=e.dp.devices||{}),e.dp.core)}(this,function(e,t){"use strict";class n{constructor(e){this.type=e}}class r extends n{constructor(){super("CommunicationFailed")}}class s extends n{constructor(e,t){super(e),this.deviceId=t}}class o extends s{constructor(e){super("DeviceConnected",e)}}class i extends s{constructor(e){super("DeviceDisconnected",e)}}var a,c,d,h,u,l,p,m,f,C,w,D,S,N;(a=e.CardType||(e.CardType={}))[a.Contact=1]="Contact",a[a.Contactless=2]="Contactless",a[a.Proximity=4]="Proximity",(c=e.CardAttributes||(e.CardAttributes={}))[c.SupportsPIN=1]="SupportsPIN",c[c.SupportsUID=2]="SupportsUID",c[c.IsPKI=65536]="IsPKI",c[c.IsPIV=131072]="IsPIV",c[c.IsReadOnly=2147483648]="IsReadOnly";class v extends s{constructor(e,t){super("CardInserted",e),this.cardId=t}}class U extends s{constructor(e,t){super("CardRemoved",e),this.cardId=t}}class T{constructor(){this.handlers={}}_on(e,t){return this.handlers[e]=this.handlers[e]||[],this.handlers[e].push(t),t}_off(e,t){if(e){const n=this.handlers[e];n&&(t?this.handlers[e]=n.filter(e=>e!==t):delete this.handlers[e])}else this.handlers={};return this}emit(e){if(!e)return;const t=e.type,n=this["on"+t];n&&this.invoke(n,e);const r=this.handlers[t];r&&r.forEach(t=>this.invoke(t,e))}invoke(e,t){try{e(t)}catch(e){console.error(e)}}}!function(e){e[e.Response=0]="Response",e[e.Notification=1]="Notification"}(d||(d={}));class g{constructor(e,t){this.pending=[],this.webChannel=new WebSdk.WebChannelClient(e,t),this.webChannel.onConnectionSucceed=this.onConnectionSucceed.bind(this),this.webChannel.onConnectionFailed=this.onConnectionFailed.bind(this),this.webChannel.onDataReceivedTxt=this.onDataReceivedTxt.bind(this)}send(e,t){const n=new Promise((n,r)=>{e.resolve=n,e.reject=r,t&&(e.timer=window.setTimeout(()=>{if(e.timer)try{e.reject(new Error("Timeout"))}catch(e){}},t))});return this.pending.push(e),this.webChannel.isConnected()?this.processRequestQueue():this.webChannel.connect(),n}onConnectionSucceed(){this.processRequestQueue()}onConnectionFailed(){if(this.pending.forEach(e=>e.reject(new Error("Communication failure."))),this.pending=[],this.onCommunicationError)try{this.onCommunicationError()}catch(e){}}onDataReceivedTxt(e){const n=JSON.parse(t.Utf8.fromBase64Url(e));if(n.Type===d.Response){const e=JSON.parse(t.Utf8.fromBase64Url(n.Data||"")),r=this.findRequest(e);if(null!==r){r.timer&&(window.clearTimeout(r.timer),delete r.timer);const t=e.Result>>>0;t>2147483647?r.reject(new Error(`0x${t.toString(16)}`)):r.resolve(e)}else console.log(`Orphaned response: ${n.Type}`)}else if(n.Type===d.Notification){const e=JSON.parse(t.Utf8.fromBase64Url(n.Data||""));if(this.onNotification)try{this.onNotification(e)}catch(e){}}else console.log(`Unknown message type: ${n.Type}`)}processRequestQueue(){this.pending.forEach((e,n,r)=>{e.sent||(this.webChannel.sendDataTxt(t.Base64Url.fromJSON(e.command)),r[n].sent=!0)})}findRequest(e){for(let t=0;t<this.pending.length;t++){const n=this.pending[t];if(n.sent&&n.command.Method===e.Method)return this.pending.splice(t,1),n}return null}}class R{constructor(e,t){this.Method=e,this.Parameters=t}}class I{constructor(e){this.command=e,this.sent=!1}}!function(e){e[e.EnumerateReaders=1]="EnumerateReaders",e[e.EnumerateCards=2]="EnumerateCards",e[e.GetCardInfo=3]="GetCardInfo",e[e.GetCardUID=4]="GetCardUID",e[e.GetDPCardAuthData=5]="GetDPCardAuthData",e[e.GetDPCardEnrollData=6]="GetDPCardEnrollData",e[e.Subscribe=100]="Subscribe",e[e.Unsubscribe=101]="Unsubscribe"}(h||(h={})),function(e){e[e.ReaderConnected=1]="ReaderConnected",e[e.ReaderDisconnected=2]="ReaderDisconnected",e[e.CardInserted=3]="CardInserted",e[e.CardRemoved=4]="CardRemoved"}(u||(u={}));(l=e.DeviceUidType||(e.DeviceUidType={}))[l.Persistent=0]="Persistent",l[l.Volatile=1]="Volatile",(p=e.DeviceModality||(e.DeviceModality={}))[p.Unknown=0]="Unknown",p[p.Swipe=1]="Swipe",p[p.Area=2]="Area",p[p.AreaMultifinger=3]="AreaMultifinger",(m=e.DeviceTechnology||(e.DeviceTechnology={}))[m.Unknown=0]="Unknown",m[m.Optical=1]="Optical",m[m.Capacitive=2]="Capacitive",m[m.Thermal=3]="Thermal",m[m.Pressure=4]="Pressure",(f=e.SampleFormat||(e.SampleFormat={}))[f.Raw=1]="Raw",f[f.Intermediate=2]="Intermediate",f[f.Compressed=3]="Compressed",f[f.PngImage=5]="PngImage",(C=e.QualityCode||(e.QualityCode={}))[C.Good=0]="Good",C[C.NoImage=1]="NoImage",C[C.TooLight=2]="TooLight",C[C.TooDark=3]="TooDark",C[C.TooNoisy=4]="TooNoisy",C[C.LowContrast=5]="LowContrast",C[C.NotEnoughFeatures=6]="NotEnoughFeatures",C[C.NotCentered=7]="NotCentered",C[C.NotAFinger=8]="NotAFinger",C[C.TooHigh=9]="TooHigh",C[C.TooLow=10]="TooLow",C[C.TooLeft=11]="TooLeft",C[C.TooRight=12]="TooRight",C[C.TooStrange=13]="TooStrange",C[C.TooFast=14]="TooFast",C[C.TooSkewed=15]="TooSkewed",C[C.TooShort=16]="TooShort",C[C.TooSlow=17]="TooSlow",C[C.ReverseMotion=18]="ReverseMotion",C[C.PressureTooHard=19]="PressureTooHard",C[C.PressureTooLight=20]="PressureTooLight",C[C.WetFinger=21]="WetFinger",C[C.FakeFinger=22]="FakeFinger",C[C.TooSmall=23]="TooSmall",C[C.RotatedTooMuch=24]="RotatedTooMuch";class y extends s{constructor(e,t,n){super("SamplesAcquired",e),this.sampleFormat=t,this.samples=JSON.parse(n)}}class O extends s{constructor(e,t){super("QualityReported",e),this.quality=t}}class b extends s{constructor(e,t){super("ErrorOccurred",e),this.error=t}}class E extends s{constructor(e){super("AcquisitionStarted",e)}}class J extends s{constructor(e){super("AcquisitionStopped",e)}}!function(e){e[e.EnumerateDevices=1]="EnumerateDevices",e[e.GetDeviceInfo=2]="GetDeviceInfo",e[e.StartAcquisition=3]="StartAcquisition",e[e.StopAcquisition=4]="StopAcquisition"}(w||(w={})),function(e){e[e.Completed=0]="Completed",e[e.Error=1]="Error",e[e.Disconnected=2]="Disconnected",e[e.Connected=3]="Connected",e[e.Quality=4]="Quality",e[e.Stopped=10]="Stopped",e[e.Started=11]="Started"}(D||(D={}));!function(e){e[e.Init=1]="Init",e[e.Continue=2]="Continue",e[e.Term=3]="Term",e[e.Authenticate=4]="Authenticate"}(S||(S={})),function(e){e[e.Response=0]="Response",e[e.Notification=1]="Notification"}(N||(N={}));e.AcquisitionStarted=E,e.AcquisitionStopped=J,e.CardInserted=v,e.CardRemoved=U,e.CardsReader=class extends T{constructor(e){super(),this.channel=new g("smartcards",e),this.channel.onCommunicationError=this.onConnectionFailed.bind(this),this.channel.onNotification=this.processNotification.bind(this)}on(e,t){return this._on(e,t)}off(e,t){return this._off(e,t)}enumerateReaders(){return this.channel.send(new I(new R(h.EnumerateReaders))).then(e=>{const n=JSON.parse(t.Utf8.fromBase64Url(e.Data||"{}"));return JSON.parse(n.Readers||"[]")})}enumerateCards(){return this.channel.send(new I(new R(h.EnumerateCards))).then(e=>{const n=JSON.parse(t.Utf8.fromBase64Url(e.Data||"{}"));return JSON.parse(n.Cards||"[]").map(e=>JSON.parse(t.Utf16.fromBase64Url(e)))})}getCardInfo(e){return this.channel.send(new I(new R(h.GetCardInfo,t.Base64Url.fromJSON({Reader:e})))).then(e=>JSON.parse(t.Utf8.fromBase64Url(e.Data||"null")))}getCardUid(e){return this.channel.send(new I(new R(h.GetCardUID,t.Base64Url.fromJSON({Reader:e})))).then(e=>t.Base64.fromBase64Url(e.Data||""))}getCardAuthData(e,n){return this.channel.send(new I(new R(h.GetDPCardAuthData,t.Base64Url.fromJSON({Reader:e,PIN:n||""})))).then(e=>JSON.parse(t.Utf8.fromBase64Url(e.Data||"")))}getCardEnrollData(e,n){return this.channel.send(new I(new R(h.GetDPCardEnrollData,t.Base64Url.fromJSON({Reader:e,PIN:n||""})))).then(e=>JSON.parse(t.Utf8.fromBase64Url(e.Data||"")))}subscribe(e){return this.channel.send(new I(new R(h.Subscribe,e?t.Base64Url.fromJSON({Reader:e}):""))).then()}unsubscribe(e){return this.channel.send(new I(new R(h.Unsubscribe,e?t.Base64Url.fromJSON({Reader:e}):""))).then()}onConnectionFailed(){this.emit(new r)}processNotification(e){switch(e.Event){case u.ReaderConnected:return this.emit(new o(e.Reader));case u.ReaderDisconnected:return this.emit(new i(e.Reader));case u.CardInserted:return this.emit(new v(e.Reader,e.Card));case u.CardRemoved:return this.emit(new U(e.Reader,e.Card));default:console.log(`Unknown notification: ${e.Event}`)}}},e.CommunicationFailed=r,e.DeviceConnected=o,e.DeviceDisconnected=i,e.DeviceEvent=s,e.ErrorOccurred=b,e.Event=n,e.FingerprintReader=class extends T{constructor(e){super(),this.options=e,this.channel=new g("fingerprints",this.options),this.channel.onCommunicationError=this.onConnectionFailed.bind(this),this.channel.onNotification=this.processNotification.bind(this)}on(e,t){return this._on(e,t)}off(e,t){return this._off(e,t)}enumerateDevices(){return this.channel.send(new I(new R(w.EnumerateDevices))).then(e=>{if(!e)return[];const n=JSON.parse(t.Utf8.fromBase64Url(e.Data||"{}"));return JSON.parse(n.DeviceIDs||"[]")})}getDeviceInfo(e){return this.channel.send(new I(new R(w.GetDeviceInfo,t.Base64Url.fromJSON({DeviceID:e})))).then(e=>JSON.parse(t.Utf8.fromBase64Url(e.Data||"null")))}startAcquisition(e,n){return this.channel.send(new I(new R(w.StartAcquisition,t.Base64Url.fromJSON({DeviceID:n||"00000000-0000-0000-0000-000000000000",SampleType:e})))).then()}stopAcquisition(e){return this.channel.send(new I(new R(w.StopAcquisition,t.Base64Url.fromJSON({DeviceID:e||"00000000-0000-0000-0000-000000000000"})))).then()}onConnectionFailed(){this.emit(new r)}processNotification(e){switch(e.Event){case D.Completed:const n=JSON.parse(t.Utf8.fromBase64Url(e.Data||""));return this.emit(new y(e.Device,n.SampleFormat,n.Samples));case D.Error:const r=JSON.parse(t.Utf8.fromBase64Url(e.Data||""));return this.emit(new b(e.Device,r.uError));case D.Disconnected:return this.emit(new i(e.Device));case D.Connected:return this.emit(new o(e.Device));case D.Quality:const s=JSON.parse(t.Utf8.fromBase64Url(e.Data||""));return this.emit(new O(e.Device,s.Quality));case D.Stopped:return this.emit(new J(e.Device));case D.Started:return this.emit(new E(e.Device));default:console.log(`Unknown notification: ${e.Event}`)}}},e.QualityReported=O,e.SamplesAcquired=y,e.WindowsAuthClient=class extends T{constructor(e){super(),this.channel=new g("wia",e),this.channel.onCommunicationError=this.onConnectionFailed.bind(this)}on(e,t){return this._on(e,t)}off(e,t){return this._off(e,t)}init(){return this.channel.send(new I(new R(S.Init)),3e3).then(e=>{const t=JSON.parse(e.Data||"{}");return{handle:t.Handle,data:t.Data}})}continue(e,t){return this.channel.send(new I(new R(S.Continue,JSON.stringify({Handle:e,Data:t})))).then(e=>JSON.parse(e.Data||"{}").Data)}term(e){return this.channel.send(new I(new R(S.Term,JSON.stringify({Handle:e})))).then()}onConnectionFailed(){this.emit(new r)}},Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=index.umd.min.js.map