@liteguard/liteguard-node 0.2.20260314

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/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # @liteguard/liteguard-node
2
+
3
+ [![JavaScript SDK](https://github.com/liteguard/liteguard/actions/workflows/test-js.yml/badge.svg)](https://github.com/liteguard/liteguard/actions/workflows/test-js.yml)
4
+ [![npm](https://img.shields.io/npm/v/@liteguard/liteguard-node)](https://www.npmjs.com/package/@liteguard/liteguard-node)
5
+
6
+ Liteguard for Node.js.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @liteguard/liteguard-node
12
+ ```
13
+
14
+ ## Quick Start
15
+
16
+ ```ts
17
+ import { LiteguardClient } from "@liteguard/liteguard-node";
18
+
19
+ const client = await LiteguardClient.create("pckid-...", {
20
+ environment: "production",
21
+ });
22
+
23
+ const scope = client.createScope({ userId: "user-123", plan: "pro" });
24
+
25
+ if (scope.isOpen("payments.checkout")) {
26
+ // ...
27
+ }
28
+
29
+ await client.shutdown();
30
+ ```
31
+
32
+ ## Primary API
33
+
34
+ - `LiteguardClient.create()` creates and starts a client.
35
+ - `client.createScope()` creates an immutable scope.
36
+ - `scope.isOpen()` evaluates locally.
37
+ - `scope.executeIfOpen()` and `scope.executeIfOpenAsync()` measure guarded work.
38
+ - `scope.withProperties()` and `scope.withProtectedContext()` derive child scopes.
39
+ - `client.flush()` flushes buffered telemetry.
40
+ - `client.shutdown()` flushes and stops background work.
41
+
42
+ ## Notes
43
+
44
+ - Evaluation is local after the initial bundle fetch.
45
+ - Prefer explicit client and scope usage.
46
+ - Unadopted guards default open and emit no signals.
47
+
48
+ ## License
49
+
50
+ Apache 2.0 — see [LICENSE](https://github.com/liteguard/liteguard/blob/main/LICENSE).
@@ -0,0 +1,30 @@
1
+ import { BaseLiteguardClient, ClientOptions } from '@liteguard/core';
2
+ export { ClientOptions, FlushOptions, GetGuardsRequest, GetGuardsResponse, Guard, GuardCheckPerformance, GuardExecutionPerformance, LiteguardChangeListener, LiteguardScope, Operator, Options, Properties, PropertyValue, ProtectedContext, Rule, ScopedOptions, SendUnadoptedGuardsRequest, SendUnadoptedGuardsResponse, Signal, SignalPerformance, TraceContext, evaluateGuard } from '@liteguard/core';
3
+
4
+ /**
5
+ * Liteguard client for Node.js environments.
6
+ *
7
+ * Uses `AsyncLocalStorage` for per-request scope propagation and
8
+ * `process.hrtime` / `process.memoryUsage` for performance measurement.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { LiteguardClient } from '@liteguard/liteguard-node';
13
+ *
14
+ * const client = new LiteguardClient('pk_live_...');
15
+ * await client.start();
16
+ *
17
+ * if (client.isOpen('feature.beta')) {
18
+ * // new code path
19
+ * }
20
+ * ```
21
+ */
22
+ declare class LiteguardClient extends BaseLiteguardClient {
23
+ /**
24
+ * @param projectClientKeyId - Your project client key ID from the Liteguard dashboard.
25
+ * @param options - Optional SDK configuration overrides.
26
+ */
27
+ constructor(projectClientKeyId: string, options?: ClientOptions);
28
+ }
29
+
30
+ export { LiteguardClient };
@@ -0,0 +1,30 @@
1
+ import { BaseLiteguardClient, ClientOptions } from '@liteguard/core';
2
+ export { ClientOptions, FlushOptions, GetGuardsRequest, GetGuardsResponse, Guard, GuardCheckPerformance, GuardExecutionPerformance, LiteguardChangeListener, LiteguardScope, Operator, Options, Properties, PropertyValue, ProtectedContext, Rule, ScopedOptions, SendUnadoptedGuardsRequest, SendUnadoptedGuardsResponse, Signal, SignalPerformance, TraceContext, evaluateGuard } from '@liteguard/core';
3
+
4
+ /**
5
+ * Liteguard client for Node.js environments.
6
+ *
7
+ * Uses `AsyncLocalStorage` for per-request scope propagation and
8
+ * `process.hrtime` / `process.memoryUsage` for performance measurement.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { LiteguardClient } from '@liteguard/liteguard-node';
13
+ *
14
+ * const client = new LiteguardClient('pk_live_...');
15
+ * await client.start();
16
+ *
17
+ * if (client.isOpen('feature.beta')) {
18
+ * // new code path
19
+ * }
20
+ * ```
21
+ */
22
+ declare class LiteguardClient extends BaseLiteguardClient {
23
+ /**
24
+ * @param projectClientKeyId - Your project client key ID from the Liteguard dashboard.
25
+ * @param options - Optional SDK configuration overrides.
26
+ */
27
+ constructor(projectClientKeyId: string, options?: ClientOptions);
28
+ }
29
+
30
+ export { LiteguardClient };
package/dist/index.js ADDED
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ LiteguardClient: () => LiteguardClient,
24
+ evaluateGuard: () => import_core2.evaluateGuard
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_core2 = require("@liteguard/core");
28
+
29
+ // src/client.ts
30
+ var import_core = require("@liteguard/core");
31
+
32
+ // src/runtime.ts
33
+ var import_node_async_hooks = require("async_hooks");
34
+ var executionStorage = new import_node_async_hooks.AsyncLocalStorage();
35
+ var requestScopeStorage = new import_node_async_hooks.AsyncLocalStorage();
36
+ var executionAdapter = {
37
+ /** Return the current execution context for correlated signal emission. */
38
+ getStore() {
39
+ return executionStorage.getStore();
40
+ },
41
+ /** Run a callback inside a new correlated execution context. */
42
+ run(initialState, fn) {
43
+ return executionStorage.run(initialState, fn);
44
+ }
45
+ };
46
+ var requestScopeAdapter = {
47
+ /** Return the current request-scope context for this async call chain. */
48
+ getStore() {
49
+ return requestScopeStorage.getStore();
50
+ },
51
+ /** Run a callback with a request scope bound to the current async chain. */
52
+ run(initialState, fn) {
53
+ return requestScopeStorage.run(initialState, fn);
54
+ }
55
+ };
56
+ var measurementAdapter = {
57
+ /** Capture a high-resolution start time for a guarded execution. */
58
+ beginGuardExecution() {
59
+ return process.hrtime.bigint();
60
+ },
61
+ /** Capture point-in-time runtime metrics for a `guard_check` signal. */
62
+ captureGuardCheck() {
63
+ const memoryUsage = process.memoryUsage();
64
+ const cpuUsage = process.cpuUsage();
65
+ return {
66
+ guardCheck: {
67
+ rssBytes: memoryUsage.rss,
68
+ heapUsedBytes: memoryUsage.heapUsed,
69
+ heapTotalBytes: memoryUsage.heapTotal,
70
+ cpuTimeNs: (cpuUsage.user + cpuUsage.system) * 1e3
71
+ }
72
+ };
73
+ },
74
+ /** Capture completion metrics for a guarded code path on Node.js. */
75
+ captureGuardExecution(startToken, completed, error) {
76
+ const start = typeof startToken === "bigint" ? startToken : process.hrtime.bigint();
77
+ const memoryUsage = process.memoryUsage();
78
+ const cpuUsage = process.cpuUsage();
79
+ const durationNs = Number(process.hrtime.bigint() - start);
80
+ return {
81
+ guardExecution: {
82
+ durationNs,
83
+ rssEndBytes: memoryUsage.rss,
84
+ heapUsedEndBytes: memoryUsage.heapUsed,
85
+ heapTotalEndBytes: memoryUsage.heapTotal,
86
+ cpuTimeEndNs: (cpuUsage.user + cpuUsage.system) * 1e3,
87
+ completed,
88
+ ...error && error instanceof Error && error.name ? { errorClass: error.name } : {}
89
+ }
90
+ };
91
+ }
92
+ };
93
+ var nodeRuntime = {
94
+ name: "node",
95
+ supportsAsyncContextPropagation: true,
96
+ internalStackMarkers: ["/packages/core/src/", "/packages/liteguard-node/src/", "/sdk/js/packages/"],
97
+ execution: executionAdapter,
98
+ requestScope: requestScopeAdapter,
99
+ measurement: measurementAdapter,
100
+ now: () => Date.now(),
101
+ monotonicNow: () => performance.now(),
102
+ fetch: (input, init) => globalThis.fetch(input, init),
103
+ setTimeout: (callback, ms) => globalThis.setTimeout(callback, ms),
104
+ clearTimeout: (handle) => globalThis.clearTimeout(handle),
105
+ setInterval: (callback, ms) => globalThis.setInterval(callback, ms),
106
+ clearInterval: (handle) => globalThis.clearInterval(handle),
107
+ detachTimer: (handle) => {
108
+ const maybeTimer = handle;
109
+ maybeTimer.unref?.();
110
+ }
111
+ };
112
+
113
+ // src/client.ts
114
+ var LiteguardClient = class extends import_core.BaseLiteguardClient {
115
+ /**
116
+ * @param projectClientKeyId - Your project client key ID from the Liteguard dashboard.
117
+ * @param options - Optional SDK configuration overrides.
118
+ */
119
+ constructor(projectClientKeyId, options = {}) {
120
+ super(nodeRuntime, projectClientKeyId, options);
121
+ }
122
+ };
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ LiteguardClient,
126
+ evaluateGuard
127
+ });
128
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/runtime.ts"],"sourcesContent":["export type {\n GetGuardsRequest,\n GetGuardsResponse,\n Guard,\n GuardCheckPerformance,\n GuardExecutionPerformance,\n ClientOptions,\n FlushOptions,\n LiteguardChangeListener,\n LiteguardScope,\n Options,\n Operator,\n Properties,\n PropertyValue,\n ProtectedContext,\n Rule,\n SendUnadoptedGuardsRequest,\n SendUnadoptedGuardsResponse,\n Signal,\n SignalPerformance,\n ScopedOptions,\n TraceContext,\n} from '@liteguard/core';\nexport { evaluateGuard } from '@liteguard/core';\nexport { LiteguardClient } from './client.js';\n","import { BaseLiteguardClient } from '@liteguard/core';\nimport type { ClientOptions } from '@liteguard/core';\nimport { nodeRuntime } from './runtime.js';\n\n/**\n * Liteguard client for Node.js environments.\n *\n * Uses `AsyncLocalStorage` for per-request scope propagation and\n * `process.hrtime` / `process.memoryUsage` for performance measurement.\n *\n * @example\n * ```ts\n * import { LiteguardClient } from '@liteguard/liteguard-node';\n *\n * const client = new LiteguardClient('pk_live_...');\n * await client.start();\n *\n * if (client.isOpen('feature.beta')) {\n * // new code path\n * }\n * ```\n */\nexport class LiteguardClient extends BaseLiteguardClient {\n /**\n * @param projectClientKeyId - Your project client key ID from the Liteguard dashboard.\n * @param options - Optional SDK configuration overrides.\n */\n constructor(projectClientKeyId: string, options: ClientOptions = {}) {\n super(nodeRuntime, projectClientKeyId, options);\n }\n}\n","import { AsyncLocalStorage } from 'node:async_hooks';\nimport type {\n ExecutionAdapter,\n ExecutionState,\n MeasurementAdapter,\n RequestScopeAdapter,\n RequestScopeStore,\n RuntimeAdapter,\n Signal,\n} from '@liteguard/core';\n\nconst executionStorage = new AsyncLocalStorage<ExecutionState>();\nconst requestScopeStorage = new AsyncLocalStorage<RequestScopeStore>();\n\nconst executionAdapter: ExecutionAdapter = {\n /** Return the current execution context for correlated signal emission. */\n getStore(): ExecutionState | undefined {\n return executionStorage.getStore();\n },\n /** Run a callback inside a new correlated execution context. */\n run<T>(initialState: ExecutionState, fn: () => T): T {\n return executionStorage.run(initialState, fn);\n },\n};\n\nconst requestScopeAdapter: RequestScopeAdapter = {\n /** Return the current request-scope context for this async call chain. */\n getStore(): RequestScopeStore | undefined {\n return requestScopeStorage.getStore();\n },\n /** Run a callback with a request scope bound to the current async chain. */\n run<T>(initialState: RequestScopeStore, fn: () => T): T {\n return requestScopeStorage.run(initialState, fn);\n },\n};\n\nconst measurementAdapter: MeasurementAdapter = {\n /** Capture a high-resolution start time for a guarded execution. */\n beginGuardExecution(): bigint {\n return process.hrtime.bigint();\n },\n /** Capture point-in-time runtime metrics for a `guard_check` signal. */\n captureGuardCheck(): NonNullable<Signal['measurement']> {\n const memoryUsage = process.memoryUsage();\n const cpuUsage = process.cpuUsage();\n return {\n guardCheck: {\n rssBytes: memoryUsage.rss,\n heapUsedBytes: memoryUsage.heapUsed,\n heapTotalBytes: memoryUsage.heapTotal,\n cpuTimeNs: (cpuUsage.user + cpuUsage.system) * 1_000,\n },\n };\n },\n /** Capture completion metrics for a guarded code path on Node.js. */\n captureGuardExecution(startToken: unknown, completed: boolean, error?: unknown): NonNullable<Signal['measurement']> {\n const start = typeof startToken === 'bigint' ? startToken : process.hrtime.bigint();\n const memoryUsage = process.memoryUsage();\n const cpuUsage = process.cpuUsage();\n const durationNs = Number(process.hrtime.bigint() - start);\n return {\n guardExecution: {\n durationNs,\n rssEndBytes: memoryUsage.rss,\n heapUsedEndBytes: memoryUsage.heapUsed,\n heapTotalEndBytes: memoryUsage.heapTotal,\n cpuTimeEndNs: (cpuUsage.user + cpuUsage.system) * 1_000,\n completed,\n ...(error && error instanceof Error && error.name ? { errorClass: error.name } : {}),\n },\n };\n },\n};\n\n/** Node.js runtime adapter used by the platform-specific Liteguard client. */\nexport const nodeRuntime: RuntimeAdapter = {\n name: 'node',\n supportsAsyncContextPropagation: true,\n internalStackMarkers: ['/packages/core/src/', '/packages/liteguard-node/src/', '/sdk/js/packages/'],\n execution: executionAdapter,\n requestScope: requestScopeAdapter,\n measurement: measurementAdapter,\n now: () => Date.now(),\n monotonicNow: () => performance.now(),\n fetch: (input, init) => globalThis.fetch(input, init),\n setTimeout: (callback, ms) => globalThis.setTimeout(callback, ms),\n clearTimeout: (handle) => globalThis.clearTimeout(handle as ReturnType<typeof setTimeout>),\n setInterval: (callback, ms) => globalThis.setInterval(callback, ms),\n clearInterval: (handle) => globalThis.clearInterval(handle as ReturnType<typeof setInterval>),\n detachTimer: (handle) => {\n const maybeTimer = handle as { unref?: () => void };\n maybeTimer.unref?.();\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuBA,IAAAA,eAA8B;;;ACvB9B,kBAAoC;;;ACApC,8BAAkC;AAWlC,IAAM,mBAAmB,IAAI,0CAAkC;AAC/D,IAAM,sBAAsB,I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ACpF;AAAA,IACF;AAAA,EACF;AACF;AAGO,IAAM,cAA8B;AAAA,EACzC,MAAM;AAAA,EACN,iCAAiC;AAAA,EACjC,sBAAsB,CAAC,uBAAuB,iCAAiC,mBAAmB;AAAA,EAClG,WAAW;AAAA,EACX,cAAc;AAAA,EACd,aAAa;AAAA,EACb,KAAK,MAAM,KAAK,IAAI;AAAA,EACpB,cAAc,MAAM,YAAY,IAAI;AAAA,EACpC,OAAO,CAAC,OAAO,SAAS,WAAW,MAAM,OAAO,IAAI;AAAA,EACpD,YAAY,CAAC,UAAU,OAAO,WAAW,WAAW,UAAU,EAAE;AAAA,EAChE,cAAc,CAAC,WAAW,WAAW,aAAa,MAAuC;AAAA,EACzF,aAAa,CAAC,UAAU,OAAO,WAAW,YAAY,UAAU,EAAE;AAAA,EAClE,eAAe,CAAC,WAAW,WAAW,cAAc,MAAwC;AAAA,EAC5F,aAAa,CAAC,WAAW;AACvB,UAAM,aAAa;AACnB,eAAW,QAAQ;AAAA,EACrB;AACF;;;ADvEO,IAAM,kBAAN,cAA8B,gCAAoB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKvD,YAAY,oBAA4B,UAAyB,CAAC,GAAG;AACnE,UAAM,aAAa,oBAAoB,OAAO;AAAA,EAChD;AACF;","names":["import_core"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,102 @@
1
+ // src/index.ts
2
+ import { evaluateGuard } from "@liteguard/core";
3
+
4
+ // src/client.ts
5
+ import { BaseLiteguardClient } from "@liteguard/core";
6
+
7
+ // src/runtime.ts
8
+ import { AsyncLocalStorage } from "async_hooks";
9
+ var executionStorage = new AsyncLocalStorage();
10
+ var requestScopeStorage = new AsyncLocalStorage();
11
+ var executionAdapter = {
12
+ /** Return the current execution context for correlated signal emission. */
13
+ getStore() {
14
+ return executionStorage.getStore();
15
+ },
16
+ /** Run a callback inside a new correlated execution context. */
17
+ run(initialState, fn) {
18
+ return executionStorage.run(initialState, fn);
19
+ }
20
+ };
21
+ var requestScopeAdapter = {
22
+ /** Return the current request-scope context for this async call chain. */
23
+ getStore() {
24
+ return requestScopeStorage.getStore();
25
+ },
26
+ /** Run a callback with a request scope bound to the current async chain. */
27
+ run(initialState, fn) {
28
+ return requestScopeStorage.run(initialState, fn);
29
+ }
30
+ };
31
+ var measurementAdapter = {
32
+ /** Capture a high-resolution start time for a guarded execution. */
33
+ beginGuardExecution() {
34
+ return process.hrtime.bigint();
35
+ },
36
+ /** Capture point-in-time runtime metrics for a `guard_check` signal. */
37
+ captureGuardCheck() {
38
+ const memoryUsage = process.memoryUsage();
39
+ const cpuUsage = process.cpuUsage();
40
+ return {
41
+ guardCheck: {
42
+ rssBytes: memoryUsage.rss,
43
+ heapUsedBytes: memoryUsage.heapUsed,
44
+ heapTotalBytes: memoryUsage.heapTotal,
45
+ cpuTimeNs: (cpuUsage.user + cpuUsage.system) * 1e3
46
+ }
47
+ };
48
+ },
49
+ /** Capture completion metrics for a guarded code path on Node.js. */
50
+ captureGuardExecution(startToken, completed, error) {
51
+ const start = typeof startToken === "bigint" ? startToken : process.hrtime.bigint();
52
+ const memoryUsage = process.memoryUsage();
53
+ const cpuUsage = process.cpuUsage();
54
+ const durationNs = Number(process.hrtime.bigint() - start);
55
+ return {
56
+ guardExecution: {
57
+ durationNs,
58
+ rssEndBytes: memoryUsage.rss,
59
+ heapUsedEndBytes: memoryUsage.heapUsed,
60
+ heapTotalEndBytes: memoryUsage.heapTotal,
61
+ cpuTimeEndNs: (cpuUsage.user + cpuUsage.system) * 1e3,
62
+ completed,
63
+ ...error && error instanceof Error && error.name ? { errorClass: error.name } : {}
64
+ }
65
+ };
66
+ }
67
+ };
68
+ var nodeRuntime = {
69
+ name: "node",
70
+ supportsAsyncContextPropagation: true,
71
+ internalStackMarkers: ["/packages/core/src/", "/packages/liteguard-node/src/", "/sdk/js/packages/"],
72
+ execution: executionAdapter,
73
+ requestScope: requestScopeAdapter,
74
+ measurement: measurementAdapter,
75
+ now: () => Date.now(),
76
+ monotonicNow: () => performance.now(),
77
+ fetch: (input, init) => globalThis.fetch(input, init),
78
+ setTimeout: (callback, ms) => globalThis.setTimeout(callback, ms),
79
+ clearTimeout: (handle) => globalThis.clearTimeout(handle),
80
+ setInterval: (callback, ms) => globalThis.setInterval(callback, ms),
81
+ clearInterval: (handle) => globalThis.clearInterval(handle),
82
+ detachTimer: (handle) => {
83
+ const maybeTimer = handle;
84
+ maybeTimer.unref?.();
85
+ }
86
+ };
87
+
88
+ // src/client.ts
89
+ var LiteguardClient = class extends BaseLiteguardClient {
90
+ /**
91
+ * @param projectClientKeyId - Your project client key ID from the Liteguard dashboard.
92
+ * @param options - Optional SDK configuration overrides.
93
+ */
94
+ constructor(projectClientKeyId, options = {}) {
95
+ super(nodeRuntime, projectClientKeyId, options);
96
+ }
97
+ };
98
+ export {
99
+ LiteguardClient,
100
+ evaluateGuard
101
+ };
102
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/runtime.ts"],"sourcesContent":["export type {\n GetGuardsRequest,\n GetGuardsResponse,\n Guard,\n GuardCheckPerformance,\n GuardExecutionPerformance,\n ClientOptions,\n FlushOptions,\n LiteguardChangeListener,\n LiteguardScope,\n Options,\n Operator,\n Properties,\n PropertyValue,\n ProtectedContext,\n Rule,\n SendUnadoptedGuardsRequest,\n SendUnadoptedGuardsResponse,\n Signal,\n SignalPerformance,\n ScopedOptions,\n TraceContext,\n} from '@liteguard/core';\nexport { evaluateGuard } from '@liteguard/core';\nexport { LiteguardClient } from './client.js';\n","import { BaseLiteguardClient } from '@liteguard/core';\nimport type { ClientOptions } from '@liteguard/core';\nimport { nodeRuntime } from './runtime.js';\n\n/**\n * Liteguard client for Node.js environments.\n *\n * Uses `AsyncLocalStorage` for per-request scope propagation and\n * `process.hrtime` / `process.memoryUsage` for performance measurement.\n *\n * @example\n * ```ts\n * import { LiteguardClient } from '@liteguard/liteguard-node';\n *\n * const client = new LiteguardClient('pk_live_...');\n * await client.start();\n *\n * if (client.isOpen('feature.beta')) {\n * // new code path\n * }\n * ```\n */\nexport class LiteguardClient extends BaseLiteguardClient {\n /**\n * @param projectClientKeyId - Your project client key ID from the Liteguard dashboard.\n * @param options - Optional SDK configuration overrides.\n */\n constructor(projectClientKeyId: string, options: ClientOptions = {}) {\n super(nodeRuntime, projectClientKeyId, options);\n }\n}\n","import { AsyncLocalStorage } from 'node:async_hooks';\nimport type {\n ExecutionAdapter,\n ExecutionState,\n MeasurementAdapter,\n RequestScopeAdapter,\n RequestScopeStore,\n RuntimeAdapter,\n Signal,\n} from '@liteguard/core';\n\nconst executionStorage = new AsyncLocalStorage<ExecutionState>();\nconst requestScopeStorage = new AsyncLocalStorage<RequestScopeStore>();\n\nconst executionAdapter: ExecutionAdapter = {\n /** Return the current execution context for correlated signal emission. */\n getStore(): ExecutionState | undefined {\n return executionStorage.getStore();\n },\n /** Run a callback inside a new correlated execution context. */\n run<T>(initialState: ExecutionState, fn: () => T): T {\n return executionStorage.run(initialState, fn);\n },\n};\n\nconst requestScopeAdapter: RequestScopeAdapter = {\n /** Return the current request-scope context for this async call chain. */\n getStore(): RequestScopeStore | undefined {\n return requestScopeStorage.getStore();\n },\n /** Run a callback with a request scope bound to the current async chain. */\n run<T>(initialState: RequestScopeStore, fn: () => T): T {\n return requestScopeStorage.run(initialState, fn);\n },\n};\n\nconst measurementAdapter: MeasurementAdapter = {\n /** Capture a high-resolution start time for a guarded execution. */\n beginGuardExecution(): bigint {\n return process.hrtime.bigint();\n },\n /** Capture point-in-time runtime metrics for a `guard_check` signal. */\n captureGuardCheck(): NonNullable<Signal['measurement']> {\n const memoryUsage = process.memoryUsage();\n const cpuUsage = process.cpuUsage();\n return {\n guardCheck: {\n rssBytes: memoryUsage.rss,\n heapUsedBytes: memoryUsage.heapUsed,\n heapTotalBytes: memoryUsage.heapTotal,\n cpuTimeNs: (cpuUsage.user + cpuUsage.system) * 1_000,\n },\n };\n },\n /** Capture completion metrics for a guarded code path on Node.js. */\n captureGuardExecution(startToken: unknown, completed: boolean, error?: unknown): NonNullable<Signal['measurement']> {\n const start = typeof startToken === 'bigint' ? startToken : process.hrtime.bigint();\n const memoryUsage = process.memoryUsage();\n const cpuUsage = process.cpuUsage();\n const durationNs = Number(process.hrtime.bigint() - start);\n return {\n guardExecution: {\n durationNs,\n rssEndBytes: memoryUsage.rss,\n heapUsedEndBytes: memoryUsage.heapUsed,\n heapTotalEndBytes: memoryUsage.heapTotal,\n cpuTimeEndNs: (cpuUsage.user + cpuUsage.system) * 1_000,\n completed,\n ...(error && error instanceof Error && error.name ? { errorClass: error.name } : {}),\n },\n };\n },\n};\n\n/** Node.js runtime adapter used by the platform-specific Liteguard client. */\nexport const nodeRuntime: RuntimeAdapter = {\n name: 'node',\n supportsAsyncContextPropagation: true,\n internalStackMarkers: ['/packages/core/src/', '/packages/liteguard-node/src/', '/sdk/js/packages/'],\n execution: executionAdapter,\n requestScope: requestScopeAdapter,\n measurement: measurementAdapter,\n now: () => Date.now(),\n monotonicNow: () => performance.now(),\n fetch: (input, init) => globalThis.fetch(input, init),\n setTimeout: (callback, ms) => globalThis.setTimeout(callback, ms),\n clearTimeout: (handle) => globalThis.clearTimeout(handle as ReturnType<typeof setTimeout>),\n setInterval: (callback, ms) => globalThis.setInterval(callback, ms),\n clearInterval: (handle) => globalThis.clearInterval(handle as ReturnType<typeof setInterval>),\n detachTimer: (handle) => {\n const maybeTimer = handle as { unref?: () => void };\n maybeTimer.unref?.();\n },\n};\n"],"mappings":";AAuBA,SAAS,qBAAqB;;;ACvB9B,SAAS,2BAA2B;;;ACApC,SAAS,yBAAyB;AAWlC,IAAM,mBAAmB,IAAI,kBAAkC;AAC/D,IAAM,sBAAsB,IAAI,kBAAqC;AAErE,IAAM,mBAAqC;AAAA;AAAA,EAEzC,WAAuC;AACrC,WAAO,iBAAiB,SAAS;AAAA,EACnC;AAAA;AAAA,EAEA,IAAO,cAA8B,IAAgB;AACnD,WAAO,iBAAiB,IAAI,cAAc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cAAc;AAAA,EACd,aAAa;AAAA,EACb,KAAK,MAAM,KAAK,IAAI;AAAA,EACpB,cAAc,MAAM,YAAY,IAAI;AAAA,EACpC,OAAO,CAAC,OAAO,SAAS,WAAW,MAAM,OAAO,IAAI;AAAA,EACpD,YAAY,CAAC,UAAU,OAAO,WAAW,WAAW,UAAU,EAAE;AAAA,EAChE,cAAc,CAAC,WAAW,WAAW,aAAa,MAAuC;AAAA,EACzF,aAAa,CAAC,UAAU,OAAO,WAAW,YAAY,UAAU,EAAE;AAAA,EAClE,eAAe,CAAC,WAAW,WAAW,cAAc,MAAwC;AAAA,EAC5F,aAAa,CAAC,WAAW;AACvB,UAAM,aAAa;AACnB,eAAW,QAAQ;AAAA,EACrB;AACF;;;ADvEO,IAAM,kBAAN,cAA8B,oBAAoB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKvD,YAAY,oBAA4B,UAAyB,CAAC,GAAG;AACnE,UAAM,aAAa,oBAAoB,OAAO;AAAA,EAChD;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@liteguard/liteguard-node",
3
+ "version": "0.2.20260314",
4
+ "description": "Liteguard SDK for Node.js runtimes — feature guards, observability, and security response evaluated locally with zero network overhead per check",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "license": "Apache-2.0",
9
+ "homepage": "https://liteguard.io",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/liteguard/liteguard.git",
13
+ "directory": "sdk/js/packages/liteguard-node"
14
+ },
15
+ "keywords": [
16
+ "feature-flags",
17
+ "feature-guards",
18
+ "feature-toggles",
19
+ "liteguard",
20
+ "observability",
21
+ "security",
22
+ "node"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.mjs",
28
+ "require": "./dist/index.js"
29
+ }
30
+ },
31
+ "files": [
32
+ "dist",
33
+ "README.md"
34
+ ],
35
+ "sideEffects": false,
36
+ "scripts": {
37
+ "build": "tsup src/index.ts --format cjs,esm --dts --sourcemap --clean"
38
+ },
39
+ "dependencies": {
40
+ "@liteguard/core": "*"
41
+ }
42
+ }