@mfxhub/sdk 0.2.2 → 0.2.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/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ Every release of `@mfxhub/sdk`, newest first. The format follows
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
5
5
  [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## 0.2.3
8
+
9
+ ### Added
10
+
11
+ - Every HTTP attempt is timed. `new MfxClient(..., { onCall })` is told each one as a `CallTiming`
12
+ (`what`, `status`, `ms`; `status` is `null` when no answer came), and when the environment
13
+ variable `MFX_CALL_LOG` names a file, one line `{"status": 200, "ms": 12.5}` is appended to it
14
+ for each. A read retried after a 429 is two attempts. Nothing else is written: no path, id,
15
+ header or key. A listener that throws, or a file that cannot be written, never fails a call.
16
+ The platform's checks use it to count and time a sample's calls, as they do for the Python SDK.
17
+
7
18
  ## 0.2.2
8
19
 
9
20
  ### Fixed
package/dist/client.d.ts CHANGED
@@ -1,10 +1,3 @@
1
- /**
2
- * `MfxClient`: one signed connection to the MFX Hub API.
3
- *
4
- * Every request is signed with the tenant's Ed25519 key. Reads that are rate limited are retried
5
- * after the platform's own `Retry-After`; writes are never retried, because a retried write is a
6
- * second order. Failures become typed errors from `errors.ts`.
7
- */
8
1
  import { MfxApiError } from "./errors.js";
9
2
  import { RequestSigner, type SigningKey } from "./signing.js";
10
3
  import { Accounts } from "./accounts.js";
@@ -16,6 +9,18 @@ import { Terminals } from "./terminals.js";
16
9
  export declare const DEFAULT_TIMEOUT_MS = 30000;
17
10
  /** How many times a rate-limited read is tried again. */
18
11
  export declare const DEFAULT_READ_RETRIES = 3;
12
+ /** The environment variable naming a file to append one JSON line to for every HTTP attempt. */
13
+ export declare const CALL_LOG_ENV = "MFX_CALL_LOG";
14
+ /**
15
+ * One HTTP attempt the client made: what was asked, the status, and how long it took.
16
+ * Each attempt is a call: a read retried after a 429 is two. `status` is null when no answer came
17
+ * (a connection that failed, a timeout). `ms` is measured on the client's own clock.
18
+ */
19
+ export interface CallTiming {
20
+ readonly what: string;
21
+ readonly status: number | null;
22
+ readonly ms: number;
23
+ }
19
24
  /** What the key had left when MFX Hub last answered. */
20
25
  export interface Budget {
21
26
  readonly remaining: number;
@@ -35,6 +40,10 @@ export interface ClientOptions {
35
40
  readonly clock?: () => number;
36
41
  /** A number in [0, 1) for the backoff's jitter; replaceable in tests. */
37
42
  readonly random?: () => number;
43
+ /** Told each call as it ends ({@link CallTiming}); `MFX_CALL_LOG` is the same, as a file. */
44
+ readonly onCall?: (timing: CallTiming) => void;
45
+ /** A monotonic clock in milliseconds; replaceable in tests. */
46
+ readonly now?: () => number;
38
47
  }
39
48
  /** One request, as the resource modules describe it. */
40
49
  export interface RequestSpec {
package/dist/client.js CHANGED
@@ -5,6 +5,7 @@
5
5
  * after the platform's own `Retry-After`; writes are never retried, because a retried write is a
6
6
  * second order. Failures become typed errors from `errors.ts`.
7
7
  */
8
+ import { appendFileSync } from "node:fs";
8
9
  import { MfxApiError, MfxTimeout, MfxTransportError, errorFor, retryAfterSeconds, } from "./errors.js";
9
10
  import { RequestSigner } from "./signing.js";
10
11
  import { Accounts } from "./accounts.js";
@@ -28,6 +29,8 @@ const NOT_SENT = new Set([
28
29
  "EHOSTUNREACH",
29
30
  "UND_ERR_CONNECT_TIMEOUT",
30
31
  ]);
32
+ /** The environment variable naming a file to append one JSON line to for every HTTP attempt. */
33
+ export const CALL_LOG_ENV = "MFX_CALL_LOG";
31
34
  const wait = (ms) => new Promise((resolve) => {
32
35
  setTimeout(resolve, ms);
33
36
  });
@@ -40,6 +43,9 @@ export class MfxClient {
40
43
  #sleep;
41
44
  #fetch;
42
45
  #random;
46
+ #onCall;
47
+ #now;
48
+ #callLog;
43
49
  #budget = null;
44
50
  /** The terminals this key's tenant owns. */
45
51
  terminals;
@@ -63,6 +69,9 @@ export class MfxClient {
63
69
  this.#sleep = options.sleep ?? wait;
64
70
  this.#fetch = options.fetch ?? fetch;
65
71
  this.#random = options.random ?? Math.random;
72
+ this.#onCall = options.onCall;
73
+ this.#now = options.now ?? (() => performance.now());
74
+ this.#callLog = process.env[CALL_LOG_ENV] || undefined;
66
75
  this.terminals = new Terminals(this);
67
76
  this.execution = new Execution(this);
68
77
  this.catalog = new Catalog(this);
@@ -146,18 +155,29 @@ export class MfxClient {
146
155
  headers["Content-Type"] = "application/json";
147
156
  if (spec.idempotencyKey !== undefined)
148
157
  headers["Idempotency-Key"] = spec.idempotencyKey;
149
- let response;
158
+ const started = this.#now();
159
+ let status = null;
150
160
  try {
151
- response = await this.#fetch(url, {
152
- method: spec.method,
153
- headers,
154
- ...(spec.body === undefined ? {} : { body }),
155
- signal: AbortSignal.timeout(this.#timeoutMs),
156
- });
161
+ let response;
162
+ try {
163
+ response = await this.#fetch(url, {
164
+ method: spec.method,
165
+ headers,
166
+ ...(spec.body === undefined ? {} : { body }),
167
+ signal: AbortSignal.timeout(this.#timeoutMs),
168
+ });
169
+ }
170
+ catch (error) {
171
+ throw transportError(error, spec);
172
+ }
173
+ status = response.status;
174
+ return await this.#answer(response);
157
175
  }
158
- catch (error) {
159
- throw transportError(error, spec);
176
+ finally {
177
+ this.#timed(`${spec.method} ${spec.path}`, status, started);
160
178
  }
179
+ }
180
+ async #answer(response) {
161
181
  const text = await response.text();
162
182
  this.#noteBudget(response.headers);
163
183
  let parsed = null;
@@ -171,6 +191,29 @@ export class MfxClient {
171
191
  }
172
192
  return { status: response.status, headers: response.headers, body: parsed };
173
193
  }
194
+ /**
195
+ * Hand one call's timing on. Never throws: timing is an observation, not a reason for a call
196
+ * that succeeded to fail. Nothing but the status and the time is written: no path, id, header
197
+ * or key.
198
+ */
199
+ #timed(what, status, started) {
200
+ const timing = { what, status, ms: this.#now() - started };
201
+ try {
202
+ this.#onCall?.(timing);
203
+ }
204
+ catch {
205
+ // A listener's bug must not fail the call.
206
+ }
207
+ if (this.#callLog !== undefined) {
208
+ try {
209
+ const ms = Math.round(timing.ms * 1000) / 1000;
210
+ appendFileSync(this.#callLog, JSON.stringify({ status, ms }) + "\n", "utf8");
211
+ }
212
+ catch {
213
+ // An unwritable log is no reason to fail a call.
214
+ }
215
+ }
216
+ }
174
217
  #noteBudget(headers) {
175
218
  const remaining = headers.get("X-MFX-Budget-Remaining");
176
219
  if (remaining !== null && Number.isFinite(Number(remaining))) {
package/dist/index.d.ts CHANGED
@@ -6,8 +6,8 @@
6
6
  * const client = new MfxClient("https://api.dev.mfxhub.com/v1", keyId, privateKey);
7
7
  * ```
8
8
  */
9
- export { MfxClient, DEFAULT_READ_RETRIES, DEFAULT_TIMEOUT_MS } from "./client.js";
10
- export type { Answer, Budget, ClientOptions, RequestSpec } from "./client.js";
9
+ export { MfxClient, CALL_LOG_ENV, DEFAULT_READ_RETRIES, DEFAULT_TIMEOUT_MS } from "./client.js";
10
+ export type { Answer, Budget, CallTiming, ClientOptions, RequestSpec } from "./client.js";
11
11
  export * from "./errors.js";
12
12
  export { Page } from "./pages.js";
13
13
  export { Catalog } from "./catalog.js";
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * const client = new MfxClient("https://api.dev.mfxhub.com/v1", keyId, privateKey);
7
7
  * ```
8
8
  */
9
- export { MfxClient, DEFAULT_READ_RETRIES, DEFAULT_TIMEOUT_MS } from "./client.js";
9
+ export { MfxClient, CALL_LOG_ENV, DEFAULT_READ_RETRIES, DEFAULT_TIMEOUT_MS } from "./client.js";
10
10
  export * from "./errors.js";
11
11
  export { Page } from "./pages.js";
12
12
  export { Catalog } from "./catalog.js";
package/dist/version.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  /** This package's version. */
2
- export declare const VERSION = "0.2.2";
2
+ export declare const VERSION = "0.2.3";
3
3
  /** What every request says it is, as a product token: the SDK's language and its version. */
4
- export declare const USER_AGENT = "mfx-sdk-typescript/0.2.2";
4
+ export declare const USER_AGENT = "mfx-sdk-typescript/0.2.3";
package/dist/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // This package's version, in one place: exported by the package, and named by every request.
2
2
  /** This package's version. */
3
- export const VERSION = "0.2.2";
3
+ export const VERSION = "0.2.3";
4
4
  /** What every request says it is, as a product token: the SDK's language and its version. */
5
5
  export const USER_AGENT = `mfx-sdk-typescript/${VERSION}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mfxhub/sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Typed TypeScript client for the MFX Hub API: hosted trading terminals, market data, execution and a live event stream",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",