@digitalpersona/fingerprint
Version:
HID DigitalPersona Fingerprint API Library
440 lines • 20.3 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Fingerprint;
(function (Fingerprint) {
function b64UrlTo64(a) {
if (a.length % 4 == 2) {
a = a + "==";
}
else {
if (a.length % 4 == 3) {
a = a + "=";
}
}
a = a.replace(/-/g, "+");
a = a.replace(/_/g, "/");
return a;
}
Fingerprint.b64UrlTo64 = b64UrlTo64;
function b64To64Url(a) {
a = a.replace(/\=/g, "");
a = a.replace(/\+/g, "-");
a = a.replace(/\//g, "_");
return a;
}
Fingerprint.b64To64Url = b64To64Url;
function b64UrlToUtf8(str) {
return window.atob(b64UrlTo64(str));
}
Fingerprint.b64UrlToUtf8 = b64UrlToUtf8;
function strToB64Url(str) {
return b64To64Url(window.btoa(str));
}
Fingerprint.strToB64Url = strToB64Url;
var DeviceUidType;
(function (DeviceUidType) {
DeviceUidType[DeviceUidType["Persistent"] = 0] = "Persistent";
DeviceUidType[DeviceUidType["Volatile"] = 1] = "Volatile";
})(DeviceUidType = Fingerprint.DeviceUidType || (Fingerprint.DeviceUidType = {}));
var DeviceModality;
(function (DeviceModality) {
DeviceModality[DeviceModality["Unknown"] = 0] = "Unknown";
DeviceModality[DeviceModality["Swipe"] = 1] = "Swipe";
DeviceModality[DeviceModality["Area"] = 2] = "Area";
DeviceModality[DeviceModality["AreaMultifinger"] = 3] = "AreaMultifinger";
})(DeviceModality = Fingerprint.DeviceModality || (Fingerprint.DeviceModality = {}));
var DeviceTechnology;
(function (DeviceTechnology) {
DeviceTechnology[DeviceTechnology["Unknown"] = 0] = "Unknown";
DeviceTechnology[DeviceTechnology["Optical"] = 1] = "Optical";
DeviceTechnology[DeviceTechnology["Capacitive"] = 2] = "Capacitive";
DeviceTechnology[DeviceTechnology["Thermal"] = 3] = "Thermal";
DeviceTechnology[DeviceTechnology["Pressure"] = 4] = "Pressure";
})(DeviceTechnology = Fingerprint.DeviceTechnology || (Fingerprint.DeviceTechnology = {}));
var SampleFormat;
(function (SampleFormat) {
SampleFormat[SampleFormat["Raw"] = 1] = "Raw";
SampleFormat[SampleFormat["Intermediate"] = 2] = "Intermediate";
SampleFormat[SampleFormat["Compressed"] = 3] = "Compressed";
SampleFormat[SampleFormat["PngImage"] = 5] = "PngImage";
})(SampleFormat = Fingerprint.SampleFormat || (Fingerprint.SampleFormat = {}));
var QualityCode;
(function (QualityCode) {
QualityCode[QualityCode["Good"] = 0] = "Good";
QualityCode[QualityCode["NoImage"] = 1] = "NoImage";
QualityCode[QualityCode["TooLight"] = 2] = "TooLight";
QualityCode[QualityCode["TooDark"] = 3] = "TooDark";
QualityCode[QualityCode["TooNoisy"] = 4] = "TooNoisy";
QualityCode[QualityCode["LowContrast"] = 5] = "LowContrast";
QualityCode[QualityCode["NotEnoughFeatures"] = 6] = "NotEnoughFeatures";
QualityCode[QualityCode["NotCentered"] = 7] = "NotCentered";
QualityCode[QualityCode["NotAFinger"] = 8] = "NotAFinger";
QualityCode[QualityCode["TooHigh"] = 9] = "TooHigh";
QualityCode[QualityCode["TooLow"] = 10] = "TooLow";
QualityCode[QualityCode["TooLeft"] = 11] = "TooLeft";
QualityCode[QualityCode["TooRight"] = 12] = "TooRight";
QualityCode[QualityCode["TooStrange"] = 13] = "TooStrange";
QualityCode[QualityCode["TooFast"] = 14] = "TooFast";
QualityCode[QualityCode["TooSkewed"] = 15] = "TooSkewed";
QualityCode[QualityCode["TooShort"] = 16] = "TooShort";
QualityCode[QualityCode["TooSlow"] = 17] = "TooSlow";
QualityCode[QualityCode["ReverseMotion"] = 18] = "ReverseMotion";
QualityCode[QualityCode["PressureTooHard"] = 19] = "PressureTooHard";
QualityCode[QualityCode["PressureTooLight"] = 20] = "PressureTooLight";
QualityCode[QualityCode["WetFinger"] = 21] = "WetFinger";
QualityCode[QualityCode["FakeFinger"] = 22] = "FakeFinger";
QualityCode[QualityCode["TooSmall"] = 23] = "TooSmall";
QualityCode[QualityCode["RotatedTooMuch"] = 24] = "RotatedTooMuch";
})(QualityCode = Fingerprint.QualityCode || (Fingerprint.QualityCode = {}));
var Event = (function () {
function Event(type) {
this.type = type;
}
return Event;
}());
Fingerprint.Event = Event;
var CommunicationEvent = (function (_super) {
__extends(CommunicationEvent, _super);
function CommunicationEvent(type) {
return _super.call(this, type) || this;
}
return CommunicationEvent;
}(Event));
Fingerprint.CommunicationEvent = CommunicationEvent;
var CommunicationFailed = (function (_super) {
__extends(CommunicationFailed, _super);
function CommunicationFailed() {
return _super.call(this, "CommunicationFailed") || this;
}
return CommunicationFailed;
}(CommunicationEvent));
Fingerprint.CommunicationFailed = CommunicationFailed;
var AcquisitionEvent = (function (_super) {
__extends(AcquisitionEvent, _super);
function AcquisitionEvent(type, deviceUid) {
var _this = _super.call(this, type) || this;
_this.deviceUid = deviceUid;
return _this;
}
return AcquisitionEvent;
}(Event));
Fingerprint.AcquisitionEvent = AcquisitionEvent;
var DeviceConnected = (function (_super) {
__extends(DeviceConnected, _super);
function DeviceConnected(deviceUid) {
return _super.call(this, "DeviceConnected", deviceUid) || this;
}
return DeviceConnected;
}(AcquisitionEvent));
Fingerprint.DeviceConnected = DeviceConnected;
var DeviceDisconnected = (function (_super) {
__extends(DeviceDisconnected, _super);
function DeviceDisconnected(deviceUid) {
return _super.call(this, "DeviceDisconnected", deviceUid) || this;
}
return DeviceDisconnected;
}(AcquisitionEvent));
Fingerprint.DeviceDisconnected = DeviceDisconnected;
var SamplesAcquired = (function (_super) {
__extends(SamplesAcquired, _super);
function SamplesAcquired(deviceUid, sampleFormat, samples) {
var _this = _super.call(this, "SamplesAcquired", deviceUid) || this;
_this.sampleFormat = sampleFormat;
_this.samples = samples;
return _this;
}
return SamplesAcquired;
}(AcquisitionEvent));
Fingerprint.SamplesAcquired = SamplesAcquired;
var QualityReported = (function (_super) {
__extends(QualityReported, _super);
function QualityReported(deviceUid, quality) {
var _this = _super.call(this, "QualityReported", deviceUid) || this;
_this.quality = quality;
return _this;
}
return QualityReported;
}(AcquisitionEvent));
Fingerprint.QualityReported = QualityReported;
var ErrorOccurred = (function (_super) {
__extends(ErrorOccurred, _super);
function ErrorOccurred(deviceUid, error) {
var _this = _super.call(this, "ErrorOccurred", deviceUid) || this;
_this.error = error;
return _this;
}
return ErrorOccurred;
}(AcquisitionEvent));
Fingerprint.ErrorOccurred = ErrorOccurred;
var AcquisitionStarted = (function (_super) {
__extends(AcquisitionStarted, _super);
function AcquisitionStarted(deviceUid) {
return _super.call(this, "AcquisitionStarted", deviceUid) || this;
}
return AcquisitionStarted;
}(AcquisitionEvent));
Fingerprint.AcquisitionStarted = AcquisitionStarted;
var AcquisitionStopped = (function (_super) {
__extends(AcquisitionStopped, _super);
function AcquisitionStopped(deviceUid) {
return _super.call(this, "AcquisitionStopped", deviceUid) || this;
}
return AcquisitionStopped;
}(AcquisitionEvent));
Fingerprint.AcquisitionStopped = AcquisitionStopped;
var Method;
(function (Method) {
Method[Method["EnumerateDevices"] = 1] = "EnumerateDevices";
Method[Method["GetDeviceInfo"] = 2] = "GetDeviceInfo";
Method[Method["StartAcquisition"] = 3] = "StartAcquisition";
Method[Method["StopAcquisition"] = 4] = "StopAcquisition";
})(Method || (Method = {}));
var NotificationType;
(function (NotificationType) {
NotificationType[NotificationType["Completed"] = 0] = "Completed";
NotificationType[NotificationType["Error"] = 1] = "Error";
NotificationType[NotificationType["Disconnected"] = 2] = "Disconnected";
NotificationType[NotificationType["Connected"] = 3] = "Connected";
NotificationType[NotificationType["Quality"] = 4] = "Quality";
NotificationType[NotificationType["Stopped"] = 10] = "Stopped";
NotificationType[NotificationType["Started"] = 11] = "Started";
})(NotificationType || (NotificationType = {}));
var MessageType;
(function (MessageType) {
MessageType[MessageType["Response"] = 0] = "Response";
MessageType[MessageType["Notification"] = 1] = "Notification";
})(MessageType || (MessageType = {}));
var Command = (function () {
function Command(method, parameters) {
this.Method = method;
if (parameters)
this.Parameters = parameters;
}
return Command;
}());
var Request = (function () {
function Request(command, resolve, reject) {
this.command = command;
this.resolve = resolve;
this.reject = reject;
this.sent = false;
}
return Request;
}());
var WebApi = (function () {
function WebApi(options) {
this.requests = [];
this.handlers = {};
var _instance = this;
this.webChannel = new WebSdk.WebChannelClient("fingerprints", options);
this.webChannel.onConnectionSucceed = function () { _instance.onConnectionSucceed(); };
this.webChannel.onConnectionFailed = function () { _instance.onConnectionFailed(); };
this.webChannel.onDataReceivedTxt = function (data) { _instance.onDataReceivedTxt(data); };
}
WebApi.prototype.enumerateDevices = function () {
var _instance = this;
return new Promise(function (resolve, reject) {
var command = new Command(Method.EnumerateDevices);
var request = new Request(command, resolve, reject);
_instance.requests.push(request);
if (_instance.webChannel.isConnected())
_instance.processQueue();
else
_instance.webChannel.connect();
});
};
WebApi.prototype.getDeviceInfo = function (deviceUid) {
var _instance = this;
return new Promise(function (resolve, reject) {
var deviceParams = { DeviceID: deviceUid };
var command = new Command(Method.GetDeviceInfo, strToB64Url(JSON.stringify(deviceParams)));
var request = new Request(command, resolve, reject);
_instance.requests.push(request);
if (_instance.webChannel.isConnected())
_instance.processQueue();
else
_instance.webChannel.connect();
});
};
WebApi.prototype.startAcquisition = function (sampleFormat, deviceUid) {
var _instance = this;
return new Promise(function (resolve, reject) {
var acquisitionParams = { DeviceID: deviceUid ? deviceUid : "00000000-0000-0000-0000-000000000000", SampleType: sampleFormat };
var command = new Command(Method.StartAcquisition, strToB64Url(JSON.stringify(acquisitionParams)));
var request = new Request(command, resolve, reject);
_instance.requests.push(request);
if (_instance.webChannel.isConnected())
_instance.processQueue();
else
_instance.webChannel.connect();
});
};
WebApi.prototype.stopAcquisition = function (deviceUid) {
var _instance = this;
return new Promise(function (resolve, reject) {
var acquisitionParams = { DeviceID: deviceUid ? deviceUid : "00000000-0000-0000-0000-000000000000" };
var command = new Command(Method.StopAcquisition, strToB64Url(JSON.stringify(acquisitionParams)));
var request = new Request(command, resolve, reject);
_instance.requests.push(request);
if (_instance.webChannel.isConnected())
_instance.processQueue();
else
_instance.webChannel.connect();
});
};
WebApi.prototype.onConnectionSucceed = function () {
this.processQueue();
};
WebApi.prototype.onConnectionFailed = function () {
for (var i = 0; i < this.requests.length; i++) {
this.requests[i].reject(new Error("Communication failure."));
}
this.requests = [];
this.emit(new CommunicationFailed());
};
WebApi.prototype.onDataReceivedTxt = function (data) {
var message = JSON.parse(b64UrlToUtf8(data));
if (message.Type === MessageType.Response) {
var response = JSON.parse(b64UrlToUtf8(message.Data));
this.processResponse(response);
}
else if (message.Type === MessageType.Notification) {
var notification = JSON.parse(b64UrlToUtf8(message.Data));
this.processNotification(notification);
}
};
WebApi.prototype.processQueue = function () {
for (var i = 0; i < this.requests.length; i++) {
if (this.requests[i].sent)
continue;
this.webChannel.sendDataTxt(strToB64Url(JSON.stringify(this.requests[i].command)));
this.requests[i].sent = true;
}
};
WebApi.prototype.processResponse = function (response) {
var request;
for (var i = 0; i < this.requests.length; i++) {
if (!this.requests[i].sent)
continue;
if (this.requests[i].command.Method === response.Method) {
request = this.requests[i];
this.requests.splice(i, 1);
break;
}
}
if (request) {
if (response.Method === Method.EnumerateDevices) {
if (response.Result < 0 || response.Result > 2147483647)
request.reject(new Error("EnumerateDevices: " + (response.Result >>> 0).toString(16)));
else {
var enumerateDevicesResponse = JSON.parse(b64UrlToUtf8(response.Data));
request.resolve(JSON.parse(enumerateDevicesResponse.DeviceIDs));
}
}
else if (response.Method === Method.GetDeviceInfo) {
if (response.Result < 0 || response.Result > 2147483647)
request.reject(new Error("GetDeviceInfo: " + (response.Result >>> 0).toString(16)));
else {
var deviceInfo = JSON.parse(b64UrlToUtf8(response.Data));
request.resolve(deviceInfo);
}
}
else if (response.Method === Method.StartAcquisition) {
if (response.Result < 0 || response.Result > 2147483647)
request.reject(new Error("StartAcquisition: " + (response.Result >>> 0).toString(16)));
else
request.resolve();
}
else if (response.Method === Method.StopAcquisition) {
if (response.Result < 0 || response.Result > 2147483647)
request.reject(new Error("StopAcquisition: " + (response.Result >>> 0).toString(16)));
else
request.resolve();
}
}
};
WebApi.prototype.processNotification = function (notification) {
if (notification.Event === NotificationType.Completed) {
var completed = JSON.parse(b64UrlToUtf8(notification.Data));
this.emit(new SamplesAcquired(notification.Device, completed.SampleFormat, completed.Samples));
}
else if (notification.Event === NotificationType.Connected) {
this.emit(new DeviceConnected(notification.Device));
}
else if (notification.Event === NotificationType.Disconnected) {
this.emit(new DeviceDisconnected(notification.Device));
}
else if (notification.Event === NotificationType.Error) {
var error = JSON.parse(b64UrlToUtf8(notification.Data));
this.emit(new ErrorOccurred(notification.Device, error.uError));
}
else if (notification.Event === NotificationType.Quality) {
var quality = JSON.parse(b64UrlToUtf8(notification.Data));
this.emit(new QualityReported(notification.Device, quality.Quality));
}
else if (notification.Event === NotificationType.Started) {
this.emit(new AcquisitionStarted(notification.Device));
}
else if (notification.Event === NotificationType.Stopped) {
this.emit(new AcquisitionStopped(notification.Device));
}
};
WebApi.prototype.on = function (event, handler) {
if (!this.handlers[event])
this.handlers[event] = [];
this.handlers[event].push(handler);
return this;
};
WebApi.prototype.off = function (event, handler) {
if (event) {
var hh = this.handlers[event];
if (hh) {
if (handler)
this.handlers[event] = hh.filter(function (h) { return h !== handler; });
else
delete this.handlers[event];
}
}
else
this.handlers = {};
return this;
};
WebApi.prototype.emit = function (event) {
var _this = this;
if (!event)
return;
var eventName = event.type;
var unicast = this["on" + eventName];
if (unicast)
this.invoke(unicast, event);
var multicast = this.handlers[eventName];
if (multicast)
multicast.forEach(function (h) { return _this.invoke(h, event); });
};
WebApi.prototype.invoke = function (handler, event) {
try {
handler(event);
}
catch (e) {
console.error(e);
}
};
return WebApi;
}());
Fingerprint.WebApi = WebApi;
})(Fingerprint || (Fingerprint = {}));
//# sourceMappingURL=fingerprint.sdk.js.map