@tbsoft-gmbh/signature-sdk 1.0.1 → 1.0.2

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.
@@ -353,7 +353,7 @@ var UsbTransportDevice = class extends import_events.EventEmitter {
353
353
  this.iface = null;
354
354
  }
355
355
  };
356
- function listUsbDevices(log) {
356
+ async function listUsbDevices(log) {
357
357
  let usb;
358
358
  try {
359
359
  usb = require("usb");
@@ -367,10 +367,34 @@ function listUsbDevices(log) {
367
367
  for (const device of deviceList) {
368
368
  const desc = device.deviceDescriptor;
369
369
  if (desc.idVendor === SIGNOTEC_VID && desc.idProduct === SIGNOTEC_PID) {
370
+ let serialNumber = "";
371
+ let product = "Signotec Pad";
372
+ if (desc.iSerialNumber || desc.iProduct) {
373
+ try {
374
+ device.open();
375
+ if (desc.iSerialNumber) {
376
+ serialNumber = await new Promise((resolve) => {
377
+ device.getStringDescriptor(desc.iSerialNumber, (err, val) => {
378
+ resolve(err ? "" : (val || "").trim());
379
+ });
380
+ });
381
+ }
382
+ if (desc.iProduct) {
383
+ product = await new Promise((resolve) => {
384
+ device.getStringDescriptor(desc.iProduct, (err, val) => {
385
+ resolve(err ? "Signotec Pad" : (val || "Signotec Pad").trim());
386
+ });
387
+ });
388
+ }
389
+ device.close();
390
+ } catch (err) {
391
+ log.debug?.("Could not read USB string descriptors:", err.message);
392
+ }
393
+ }
370
394
  results.push({
371
395
  path: `usb:${desc.idVendor}:${desc.idProduct}:${device.busNumber}:${device.deviceAddress}`,
372
- serialNumber: "",
373
- product: "Signotec Pad",
396
+ serialNumber,
397
+ product,
374
398
  _usbDevice: device
375
399
  });
376
400
  }
@@ -488,7 +512,7 @@ var HidProtocol = class {
488
512
  * HID devices are found (e.g., Windows without HID class driver).
489
513
  * @returns Array of device descriptors (never throws).
490
514
  */
491
- listDevices() {
515
+ async listDevices() {
492
516
  this.usbDeviceMap.clear();
493
517
  try {
494
518
  const hidDevices = this.HID.devices().filter((d) => d.vendorId === SIGNOTEC_VID2 && d.productId === SIGNOTEC_PID2).map((d) => ({
@@ -505,7 +529,7 @@ var HidProtocol = class {
505
529
  }
506
530
  this.log.info("No Signotec pads found via HID, trying raw USB...");
507
531
  try {
508
- const usbDevices = listUsbDevices(this.log);
532
+ const usbDevices = await listUsbDevices(this.log);
509
533
  if (usbDevices.length > 0) {
510
534
  this.log.info(`Found ${usbDevices.length} Signotec pad(s) via raw USB`);
511
535
  for (const d of usbDevices) {
@@ -1786,7 +1810,7 @@ function createHidDriver(config) {
1786
1810
  const maxAttempts = config?.reconnectAttempts ?? 10;
1787
1811
  const interval = config?.reconnectIntervalMs ?? 2e3;
1788
1812
  let attempts = 0;
1789
- const tryReconnect = () => {
1813
+ const tryReconnect = async () => {
1790
1814
  attempts++;
1791
1815
  if (attempts > maxAttempts) {
1792
1816
  log.error(`Signotec pad reconnection failed after ${maxAttempts} attempts`);
@@ -1797,7 +1821,7 @@ function createHidDriver(config) {
1797
1821
  log.info(`Signotec reconnect attempt ${attempts}/${maxAttempts}...`);
1798
1822
  try {
1799
1823
  const prot = new HidProtocol(config);
1800
- const devices = prot.listDevices();
1824
+ const devices = await prot.listDevices();
1801
1825
  if (devices.length > 0) {
1802
1826
  log.info("Signotec pad reconnected - cancelling session (state lost)");
1803
1827
  currentHandlers?.onUserEvent("cancel", { serialNo: currentSerialNo || "unknown" });
@@ -1861,7 +1885,7 @@ function createHidDriver(config) {
1861
1885
  await cleanup();
1862
1886
  }
1863
1887
  protocol = new HidProtocol(config);
1864
- const devices = protocol.listDevices();
1888
+ const devices = await protocol.listDevices();
1865
1889
  if (devices.length === 0) {
1866
1890
  protocol = null;
1867
1891
  throw new Error(`No device with serialNo.: ${serialNo} was found.`);
@@ -1920,14 +1944,44 @@ function createHidDriver(config) {
1920
1944
  }
1921
1945
  emit("info", "Signing session started", "session", { serialNo: currentSerialNo });
1922
1946
  }
1947
+ function emergencyClose() {
1948
+ const prot = protocol;
1949
+ if (!prot) return;
1950
+ protocol = null;
1951
+ try {
1952
+ prot.close();
1953
+ } catch {
1954
+ }
1955
+ }
1956
+ let exitHandlersInstalled = false;
1957
+ function installExitHandlers() {
1958
+ if (exitHandlersInstalled) return;
1959
+ exitHandlersInstalled = true;
1960
+ process.on("exit", emergencyClose);
1961
+ const gracefulExit = async (signal) => {
1962
+ log.info(`Received ${signal}, closing Signotec device...`);
1963
+ try {
1964
+ await cleanup();
1965
+ } catch {
1966
+ }
1967
+ process.removeListener(signal, gracefulExit);
1968
+ process.kill(process.pid, signal);
1969
+ };
1970
+ process.on("SIGINT", gracefulExit);
1971
+ process.on("SIGTERM", gracefulExit);
1972
+ if (process.platform === "win32") {
1973
+ process.on("SIGBREAK", gracefulExit);
1974
+ }
1975
+ }
1976
+ installExitHandlers();
1923
1977
  const driver = {
1924
- getSignotecPads() {
1978
+ async getSignotecPads() {
1925
1979
  if (cachedDeviceList && Date.now() - cacheTimestamp < CACHE_TTL) {
1926
1980
  return cachedDeviceList;
1927
1981
  }
1928
1982
  try {
1929
1983
  const prot = new HidProtocol(config);
1930
- const devices = prot.listDevices();
1984
+ const devices = await prot.listDevices();
1931
1985
  cachedDeviceList = devices.map((d, i) => ({
1932
1986
  serialNo: d.serialNumber || `signotec-${i}`,
1933
1987
  type: 1,
@@ -2026,7 +2080,7 @@ function createHidDriver(config) {
2026
2080
  }
2027
2081
  try {
2028
2082
  const prot = new HidProtocol(config);
2029
- const devices = prot.listDevices();
2083
+ const devices = await prot.listDevices();
2030
2084
  if (devices.length === 0) {
2031
2085
  log.error("No Signotec device connected for standby image upload");
2032
2086
  return false;
@@ -2061,7 +2115,7 @@ function createHidDriver(config) {
2061
2115
  const start = Date.now();
2062
2116
  const interval = 1e3;
2063
2117
  while (Date.now() - start < timeoutMs) {
2064
- const pads = driver.getSignotecPads();
2118
+ const pads = await driver.getSignotecPads();
2065
2119
  if (pads.length > 0) return pads[0];
2066
2120
  await new Promise((r) => setTimeout(r, interval));
2067
2121
  }