@fire-spark/payments-clisitef-client 0.11.0 → 0.12.0

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.
@@ -48,7 +48,7 @@ internal class BoundedRunner {
48
48
  Lane.RECOVERY -> recovery
49
49
  }
50
50
  // TODO(debug-clisitef): remove.
51
- Log.d(DEBUG_TAG, "runner.$operation › queued lane=$lane timeoutMs=$timeoutMs")
51
+ CliSiTefDebug.log("runner.$operation › queued lane=$lane timeoutMs=$timeoutMs")
52
52
  val timer = if (timeoutMs > 0) {
53
53
  watchdog.schedule({
54
54
  if (settled.compareAndSet(false, true)) {
@@ -65,7 +65,9 @@ internal class BoundedRunner {
65
65
  try {
66
66
  target.execute {
67
67
  // TODO(debug-clisitef): remove.
68
- Log.d(DEBUG_TAG, "runner.$operation › running thread=${Thread.currentThread().name} waited=${System.currentTimeMillis() - startedAt}ms")
68
+ val thread = Thread.currentThread().name
69
+ val waited = System.currentTimeMillis() - startedAt
70
+ CliSiTefDebug.log("runner.$operation › running thread=$thread waited=${waited}ms")
69
71
  val outcome = runCatching(block)
70
72
  timer?.cancel(false)
71
73
  if (!settled.compareAndSet(false, true)) {
@@ -75,7 +77,10 @@ internal class BoundedRunner {
75
77
  lastCallOperation = operation
76
78
  lastCallDurationMs = System.currentTimeMillis() - startedAt
77
79
  // TODO(debug-clisitef): remove.
78
- Log.d(DEBUG_TAG, "runner.$operation › settled ok=${outcome.isSuccess} value=${outcome.getOrNull()} error=${outcome.exceptionOrNull()?.message} ms=$lastCallDurationMs")
80
+ val ok = outcome.isSuccess
81
+ val value = outcome.getOrNull()
82
+ val error = outcome.exceptionOrNull()?.message
83
+ CliSiTefDebug.log("runner.$operation › settled ok=$ok value=$value error=$error ms=$lastCallDurationMs")
79
84
  outcome
80
85
  .onSuccess {
81
86
  consecutiveTimeouts.set(0)
@@ -128,8 +133,6 @@ internal class BoundedRunner {
128
133
 
129
134
  private companion object {
130
135
  const val TAG = "CliSiTef"
131
- // TODO(debug-clisitef): remove.
132
- const val DEBUG_TAG = "CLISITEF_DEBUG"
133
136
 
134
137
  fun daemon(name: String): ThreadFactory {
135
138
  val sequence = AtomicInteger(0)
@@ -31,7 +31,8 @@ internal class CliSiTefBridge(
31
31
 
32
32
  // TODO(debug-clisitef): remove with every `traced` / `debug` call.
33
33
  private fun debug(scope: String, message: String) {
34
- Log.d(DEBUG_TAG, "$scope › $message [thread=${Thread.currentThread().name}]")
34
+ val thread = Thread.currentThread().name
35
+ CliSiTefDebug.log("$scope › $message [thread=$thread]")
35
36
  }
36
37
 
37
38
  // TODO(debug-clisitef): remove; call `block()` directly.
@@ -118,8 +119,6 @@ internal class CliSiTefBridge(
118
119
 
119
120
  companion object {
120
121
  private const val TAG = "CliSiTef"
121
- // TODO(debug-clisitef): remove.
122
- private const val DEBUG_TAG = "CLISITEF_DEBUG"
123
122
  private const val CLSIT = "CLSIT"
124
123
  private val APP_DIR = Regex("DiretorioApp=[^;\\]]*")
125
124
 
@@ -144,7 +143,10 @@ internal class CliSiTefBridge(
144
143
  try {
145
144
  val shipped = context.assets.open(CLSIT).use { it.readBytes() }
146
145
  // TODO(debug-clisitef): remove.
147
- Log.d(DEBUG_TAG, "syncClsit › shipped=${shipped.size}B target=${target.absolutePath} exists=${target.isFile}")
146
+ val shippedBytes = shipped.size
147
+ val targetPath = target.absolutePath
148
+ val exists = target.isFile
149
+ CliSiTefDebug.log("syncClsit › shipped=${shippedBytes}B target=$targetPath exists=$exists")
148
150
  if (shipped.isEmpty()) return
149
151
  if (target.isFile && target.readBytes().contentEquals(shipped)) return
150
152
  temp.outputStream().use { out ->
@@ -0,0 +1,16 @@
1
+ package dev.firespark.payments.clisitef
2
+
3
+ import android.util.Log
4
+
5
+ /** Process-wide gate for `CLISITEF_DEBUG` logcat lines. Off until `setLogs`. */
6
+ internal object CliSiTefDebug {
7
+ const val TAG = "CLISITEF_DEBUG"
8
+
9
+ @Volatile
10
+ var enabled: Boolean = false
11
+
12
+ fun log(message: String) {
13
+ if (!enabled) return
14
+ Log.d(TAG, message)
15
+ }
16
+ }
@@ -13,7 +13,8 @@ class CliSiTefModule : Module() {
13
13
  bridge?.let { return it }
14
14
  val context = appContext.reactContext?.applicationContext ?: throw Exceptions.ReactContextLost()
15
15
  // TODO(debug-clisitef): remove.
16
- android.util.Log.d("CLISITEF_DEBUG", "module › creating bridge dataDir=${context.applicationInfo.dataDir}")
16
+ val dataDir = context.applicationInfo.dataDir
17
+ CliSiTefDebug.log("module › creating bridge dataDir=$dataDir")
17
18
  CliSiTefBridge(
18
19
  context,
19
20
  onData = { event -> sendEvent("onData", event.toMap()) },
@@ -66,6 +67,10 @@ class CliSiTefModule : Module() {
66
67
  runner.run("disconnectSitef", 10_000, promise, Lane.RECOVERY) { bridge().disconnectSitef() }
67
68
  }
68
69
 
70
+ Function("setLogs") { enabled: Boolean ->
71
+ CliSiTefDebug.enabled = enabled
72
+ }
73
+
69
74
  Function("getHealth") { runner.health(bridge?.appDirectory) }
70
75
 
71
76
  OnDestroy { runner.shutdown() }
package/dist/index.d.mts CHANGED
@@ -68,6 +68,8 @@ type ClisitefNativeModule = {
68
68
  closePinPad: () => Promise<number>;
69
69
  disconnectSitef: () => Promise<number>;
70
70
  getHealth: () => ClisitefHealth;
71
+ /** Enables or disables `CLISITEF_DEBUG` logcat lines. Off until set. */
72
+ setLogs: (enabled: boolean) => void;
71
73
  addListener: <E extends keyof ClisitefEvents>(event: E, listener: ClisitefEvents[E]) => ClisitefSubscription;
72
74
  };
73
75
  //#endregion
@@ -145,6 +147,8 @@ declare const CLISITEF_DEFAULT_TIME_ZONE = "America/Sao_Paulo";
145
147
  declare const CLISITEF_DEFAULT_PINPAD = "ANDROID_USB";
146
148
  //#endregion
147
149
  //#region src/debug.d.ts
150
+ /** Turns console and Android logcat traces on or off. Default is off. */
151
+ declare const setClisitefLogs: (value: boolean) => void;
148
152
  /** Logs one step: `[CLISITEF_DEBUG] scope › event { data }`. */
149
153
  declare const clisitefDebug: (scope: string, event: string, data?: unknown) => void;
150
154
  /** Logs the input, the output or the error, and the duration of a call. */
@@ -203,4 +207,4 @@ declare const parseClisitefMenu: (input: string) => ClisitefMenuOption[];
203
207
  /** Shared client bound to the device's native module; one per app. */
204
208
  declare const clisitef: ClisitefClient;
205
209
  //#endregion
206
- export { CLISITEF_ABORT_FLAG, CLISITEF_CONFIRM, CLISITEF_DEFAULT_PINPAD, CLISITEF_DEFAULT_TIME_ZONE, CLISITEF_JS_GRACE_MS, CLISITEF_MODALITY, CLISITEF_RESULT, CLISITEF_TIMEOUT_CODE, CLISITEF_TIMEOUT_MS, CLISITEF_VOID_FUNCTION, ClisitefAdditionalParamsInput, ClisitefClient, ClisitefClientOptions, ClisitefConfigureParams, ClisitefDataEvent, ClisitefEvents, ClisitefFinishParams, ClisitefFiscalDateTime, ClisitefHealth, ClisitefMenuOption, ClisitefNativeModule, ClisitefOperation, ClisitefResultEvent, ClisitefStartParams, ClisitefSubscription, ClisitefTimeoutError, ClisitefTimeoutInfo, ClisitefUnavailableError, clisitef, clisitefAdditionalParams, clisitefConfigSignature, clisitefDebug, clisitefDebugCall, clisitefFiscalDateTime, createClisitefClient, formatClisitefAmount, parseClisitefMenu, toClisitefTimeout };
210
+ export { CLISITEF_ABORT_FLAG, CLISITEF_CONFIRM, CLISITEF_DEFAULT_PINPAD, CLISITEF_DEFAULT_TIME_ZONE, CLISITEF_JS_GRACE_MS, CLISITEF_MODALITY, CLISITEF_RESULT, CLISITEF_TIMEOUT_CODE, CLISITEF_TIMEOUT_MS, CLISITEF_VOID_FUNCTION, ClisitefAdditionalParamsInput, ClisitefClient, ClisitefClientOptions, ClisitefConfigureParams, ClisitefDataEvent, ClisitefEvents, ClisitefFinishParams, ClisitefFiscalDateTime, ClisitefHealth, ClisitefMenuOption, ClisitefNativeModule, ClisitefOperation, ClisitefResultEvent, ClisitefStartParams, ClisitefSubscription, ClisitefTimeoutError, ClisitefTimeoutInfo, ClisitefUnavailableError, clisitef, clisitefAdditionalParams, clisitefConfigSignature, clisitefDebug, clisitefDebugCall, clisitefFiscalDateTime, createClisitefClient, formatClisitefAmount, parseClisitefMenu, setClisitefLogs, toClisitefTimeout };
package/dist/index.mjs CHANGED
@@ -43,8 +43,13 @@ const CLISITEF_JS_GRACE_MS = 5e3;
43
43
  const CLISITEF_DEFAULT_TIME_ZONE = "America/Sao_Paulo";
44
44
  const CLISITEF_DEFAULT_PINPAD = "ANDROID_USB";
45
45
  //#endregion
46
+ //#region src/native.ts
47
+ /** `null` on iOS, web, or an Android build without the CliSiTef plugin. */
48
+ const clisitefNative = requireOptionalNativeModule("CliSiTef");
49
+ //#endregion
46
50
  //#region src/debug.ts
47
51
  const TAG = "[CLISITEF_DEBUG]";
52
+ let enabled = false;
48
53
  const elapsed = (startedAt) => Date.now() - startedAt;
49
54
  const safe = (value) => {
50
55
  if (value instanceof Map) return Object.fromEntries(value);
@@ -58,8 +63,14 @@ const safe = (value) => {
58
63
  }
59
64
  return value;
60
65
  };
66
+ /** Turns console and Android logcat traces on or off. Default is off. */
67
+ const setClisitefLogs = (value) => {
68
+ enabled = value;
69
+ clisitefNative?.setLogs(value);
70
+ };
61
71
  /** Logs one step: `[CLISITEF_DEBUG] scope › event { data }`. */
62
72
  const clisitefDebug = (scope, event, data) => {
73
+ if (!enabled) return;
63
74
  const time = (/* @__PURE__ */ new Date()).toISOString();
64
75
  if (data === void 0) return console.log(TAG, time, `${scope} › ${event}`);
65
76
  console.log(TAG, time, `${scope} › ${event}`, safe(data));
@@ -314,6 +325,6 @@ const createClisitefClient = (native, options = {}) => {
314
325
  //#endregion
315
326
  //#region src/index.ts
316
327
  /** Shared client bound to the device's native module; one per app. */
317
- const clisitef = createClisitefClient(requireOptionalNativeModule("CliSiTef"));
328
+ const clisitef = createClisitefClient(clisitefNative);
318
329
  //#endregion
319
- export { CLISITEF_ABORT_FLAG, CLISITEF_CONFIRM, CLISITEF_DEFAULT_PINPAD, CLISITEF_DEFAULT_TIME_ZONE, CLISITEF_JS_GRACE_MS, CLISITEF_MODALITY, CLISITEF_RESULT, CLISITEF_TIMEOUT_CODE, CLISITEF_TIMEOUT_MS, CLISITEF_VOID_FUNCTION, ClisitefTimeoutError, ClisitefUnavailableError, clisitef, clisitefAdditionalParams, clisitefConfigSignature, clisitefDebug, clisitefDebugCall, clisitefFiscalDateTime, createClisitefClient, formatClisitefAmount, parseClisitefMenu, toClisitefTimeout };
330
+ export { CLISITEF_ABORT_FLAG, CLISITEF_CONFIRM, CLISITEF_DEFAULT_PINPAD, CLISITEF_DEFAULT_TIME_ZONE, CLISITEF_JS_GRACE_MS, CLISITEF_MODALITY, CLISITEF_RESULT, CLISITEF_TIMEOUT_CODE, CLISITEF_TIMEOUT_MS, CLISITEF_VOID_FUNCTION, ClisitefTimeoutError, ClisitefUnavailableError, clisitef, clisitefAdditionalParams, clisitefConfigSignature, clisitefDebug, clisitefDebugCall, clisitefFiscalDateTime, createClisitefClient, formatClisitefAmount, parseClisitefMenu, setClisitefLogs, toClisitefTimeout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fire-spark/payments-clisitef-client",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Fire Spark native client for CliSiTef (Software Express) on Android: SiTef server and pinpad bridge shared by the CliSiTef adapters",
5
5
  "sideEffects": false,
6
6
  "files": [