mte-relay-browser-public-client 4.4.5 → 4.5.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.
@@ -0,0 +1,96 @@
1
+ /**
2
+ * This file provides an in-memory caching layer for the MTE Relay client.
3
+ * It manages origin statuses, client IDs, pair queues, and MTE states to
4
+ * optimize performance and maintain session information. It also interacts
5
+ * with the persistent storage interface for durability.
6
+ */
7
+ import { CACHE_KEYS } from "./constants";
8
+ import type { OriginStatus } from "./types";
9
+ /**
10
+ * Generates a standardized cache key.
11
+ * @param {keyof typeof CACHE_KEYS} type The type of cache entry.
12
+ * @param {string} id The unique identifier for the entry.
13
+ * @returns {string} The generated cache key.
14
+ */
15
+ export declare const getCacheKey: (type: keyof typeof CACHE_KEYS, id: string) => string;
16
+ /**
17
+ * Sets a value in the in-memory cache.
18
+ * @param {string} key The cache key.
19
+ * @param {*} value The value to store.
20
+ */
21
+ export declare function setCacheItem(key: string, value: any): void;
22
+ /**
23
+ * Gets a value from the in-memory cache.
24
+ * @param {string} key The cache key.
25
+ * @returns {T | undefined} The cached value.
26
+ */
27
+ export declare function getCacheItem<T>(key: string): T | undefined;
28
+ /**
29
+ * Deletes an item from the in-memory cache.
30
+ * @param {string} key The cache key.
31
+ */
32
+ export declare function deleteCacheItem(key: string): void;
33
+ /**
34
+ * Gets the current status of an origin.
35
+ * @param {string} origin The origin URL.
36
+ * @returns {OriginStatus} The current status.
37
+ */
38
+ export declare function getOriginStatus(origin: string): OriginStatus;
39
+ /**
40
+ * Sets the status for a given origin.
41
+ * @param {string} origin The origin URL.
42
+ * @param {OriginStatus} status The new status.
43
+ */
44
+ export declare function setOriginStatus(origin: string, status: OriginStatus): void;
45
+ /**
46
+ * Retrieves the client ID for a given origin, checking cache and persistent storage.
47
+ * @param {string} origin The origin URL.
48
+ * @returns {Promise<string | undefined>} The client ID.
49
+ */
50
+ export declare function getClientId(origin: string): Promise<string | undefined>;
51
+ /**
52
+ * Sets the client ID for an origin in cache and persistent storage.
53
+ * @param {string} origin The origin URL.
54
+ * @param {string} clientId The client ID.
55
+ */
56
+ export declare function setClientId(origin: string, clientId: string): Promise<void>;
57
+ /**
58
+ * Deletes the client ID for an origin from cache and persistent storage.
59
+ * @param {string} origin The origin URL.
60
+ */
61
+ export declare function deleteClientId(origin: string): Promise<void>;
62
+ /**
63
+ * Adds a new pair ID to an origin's queue.
64
+ * @param {string} origin The origin URL.
65
+ * @param {string} pairId The pair ID to add.
66
+ */
67
+ export declare function addPairIdToQueue(origin: string, pairId: string): void;
68
+ /**
69
+ * Gets the next available pair ID from an origin's queue and rotates it.
70
+ * @param {string} origin The origin URL.
71
+ * @returns {string} The next pair ID.
72
+ */
73
+ export declare function getNextPairIdFromQueue(origin: string): string;
74
+ /**
75
+ * Removes a specific pair ID from an origin's queue.
76
+ * @param {string} origin The origin URL.
77
+ * @param {string} pairId The pair ID to remove.
78
+ */
79
+ export declare function deletePairIdFromQueue(origin: string, pairId: string): void;
80
+ /**
81
+ * Clears all pair IDs for a given origin.
82
+ * @param {string} origin The origin URL.
83
+ */
84
+ export declare function deleteAllPairsFromOrigin(origin: string): void;
85
+ /**
86
+ * Caches the state of an MTE encoder or decoder.
87
+ * @param {string} id The unique identifier for the state.
88
+ * @param {string} state The Base64 encoded state string.
89
+ */
90
+ export declare function setEncDecState(id: string, state: string): void;
91
+ /**
92
+ * Retrieves the cached state of an MTE encoder or decoder.
93
+ * @param {string} id The unique identifier for the state.
94
+ * @returns {string | undefined} The Base64 encoded state string.
95
+ */
96
+ export declare function getEncDecState(id: string): string | undefined;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * This file handles the core logic for encoding outgoing requests and decoding
3
+ * incoming responses. It orchestrates the encryption/decryption of URLs,
4
+ * headers, and bodies.
5
+ */
6
+ /**
7
+ * Encodes a Request object's URL, headers, and body.
8
+ * @param request The original Request object.
9
+ * @param options Configuration for the encoding process.
10
+ * @returns A new, MTE-encoded Request object.
11
+ */
12
+ export declare function encodeRequest(request: Request, options: {
13
+ clientId: string;
14
+ origin: string;
15
+ pairId: string;
16
+ type: "MTE" | "MKE";
17
+ encodeUrl: boolean;
18
+ encodeHeaders: boolean | string[];
19
+ useStreaming: boolean;
20
+ pathPrefix: string;
21
+ }): Promise<Request>;
22
+ /**
23
+ * Decodes an MTE-encoded Response object's headers and body.
24
+ * @param response The MTE-encoded Response object.
25
+ * @param options Configuration for the decoding process.
26
+ * @returns A new, decoded Response object.
27
+ */
28
+ export declare function decodeResponse(response: Response, options: {
29
+ decoderId: string;
30
+ }): Promise<Response>;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * This file manages the global configuration state for the MTE Relay client.
3
+ * It holds default settings, references to the MTE WASM instance, and other
4
+ * shared state that is initialized once and used throughout the library's
5
+ * lifecycle.
6
+ */
7
+ import { MteWasm } from "mte";
8
+ import type { MteRelayStorage } from "./types";
9
+ export declare const config: {
10
+ numberOfPairs: number;
11
+ encodeType: "MTE" | "MKE";
12
+ encodeUrls: boolean;
13
+ encodeHeaders: boolean | string[];
14
+ pathPrefix: string;
15
+ mtePoolSize: number;
16
+ mkePoolSize: number;
17
+ persistentStorage: MteRelayStorage | undefined;
18
+ customFetch: typeof fetch | undefined;
19
+ };
20
+ export declare let mteWasm: MteWasm;
21
+ export declare let finishEncryptBytes: number;
22
+ export declare let isTrial: boolean;
23
+ export declare let _fetch: typeof fetch;
24
+ /**
25
+ * Sets the global MTE WASM instance.
26
+ * @param {MteWasm} wasmInstance The initialized MTE WASM instance.
27
+ */
28
+ export declare function setMteWasm(wasmInstance: MteWasm): void;
29
+ /**
30
+ * Sets the number of bytes required for MKE finishEncrypt operation.
31
+ * @param {number} bytes The number of bytes.
32
+ */
33
+ export declare function setFinishEncryptBytes(bytes: number): void;
34
+ /**
35
+ * Sets the flag indicating if the MTE library is a trial version.
36
+ * @param {boolean} trialStatus The trial status.
37
+ */
38
+ export declare function setIsTrial(trialStatus: boolean): void;
39
+ /**
40
+ * Sets the fetch implementation to be used by the client.
41
+ * @param {typeof fetch} fetchFn The fetch function.
42
+ */
43
+ export declare function setFetch(fetchFn: typeof fetch): void;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * This file contains constants used throughout the MTE Relay client.
3
+ * This includes header names, cache keys, and error message-to-status-code
4
+ * mappings. Centralizing these values makes configuration and maintenance easier.
5
+ */
6
+ export declare const MTE_ENCODED_HEADERS_HEADER = "x-mte-relay-eh";
7
+ export declare const MTE_RELAY_HEADER = "x-mte-relay";
8
+ export declare const CACHE_KEYS: {
9
+ ORIGIN_STATUS: string;
10
+ CLIENT_ID: string;
11
+ PAIR_QUEUE: string;
12
+ MTE_STATE: string;
13
+ INIT_PROMISE: string;
14
+ };
15
+ export declare const MTE_ERRORS: {
16
+ readonly "Repair is required.": 559;
17
+ readonly "State not found.": 560;
18
+ readonly "Failed to encode.": 561;
19
+ readonly "Failed to decode.": 562;
20
+ readonly "Failed to get state from encoder or decoder.": 563;
21
+ readonly "DRBG reseed is required.": 564;
22
+ readonly "MTE Status was not successful.": 565;
23
+ readonly "Invalid Client ID header.": 566;
24
+ readonly "Failed to save decoder stateId.": 567;
25
+ readonly "Failed to save encoder stateId.": 568;
26
+ readonly "Missing required header": 569;
27
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * This file contains the core orchestration logic for the MTE Relay client.
3
+ * The `sendMteRequest` function acts as the central controller, managing the
4
+ * entire lifecycle of an MTE-protected request, including validation, pairing,
5
+ * encoding, decoding, and error handling.
6
+ */
7
+ import type { MteRequestOptions } from "./types";
8
+ /**
9
+ * Orchestrates the MTE fetch process, including pairing, encoding, and decoding.
10
+ * @param url The request URL or Request object.
11
+ * @param options Standard fetch options.
12
+ * @param mteOptions MTE-specific options.
13
+ * @param isRetry A flag to prevent infinite retry loops.
14
+ * @returns A Promise that resolves to a decoded Response.
15
+ */
16
+ export declare function sendMteRequest(url: RequestInfo, options?: RequestInit, mteOptions?: Partial<MteRequestOptions>, isRetry?: boolean): Promise<Response>;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * This file defines a custom error class, MteRelayError, for handling
3
+ * specific errors that can occur during MTE Relay operations. It provides
4
+ * a structured way to manage server-side MTE errors and other client-side
5
+ * exceptions.
6
+ */
7
+ import { MTE_ERRORS } from "./constants";
8
+ type ErrorMessages = keyof typeof MTE_ERRORS;
9
+ /**
10
+ * Custom error class for MTE Relay specific issues.
11
+ */
12
+ export declare class MteRelayError extends Error {
13
+ status: number;
14
+ info?: Record<string, any>;
15
+ constructor(message: ErrorMessages, info?: Record<string, any>);
16
+ /**
17
+ * Checks if a given HTTP status code corresponds to a known MTE Relay error.
18
+ * @param {number} status The HTTP status code.
19
+ * @returns {boolean} True if it is a known MTE error status.
20
+ */
21
+ static isMteErrorStatus(status: number): boolean;
22
+ /**
23
+ * Retrieves the error message associated with a given MTE Relay status code.
24
+ * @param {number} status The HTTP status code.
25
+ * @returns {ErrorMessages | undefined} The corresponding error message.
26
+ */
27
+ static getStatusErrorMessages(status: number): ErrorMessages | undefined;
28
+ }
29
+ export {};
@@ -0,0 +1,23 @@
1
+ /**
2
+ * This file serves as the main entry point for the MTE Relay client library.
3
+ * It exposes the public API, including the `initMteRelayClient` function for
4
+ * configuration and the `mteFetch` function for making MTE-protected requests.
5
+ * It also re-exports key types and classes for external use.
6
+ */
7
+ import { LocalStorageWrapper, MemoryStorage } from "./storage";
8
+ import type { MteRelayClientOptions, MteRequestOptions } from "./types";
9
+ export { LocalStorageWrapper, MemoryStorage };
10
+ export type { MteRelayStorage } from "./types";
11
+ /**
12
+ * Initializes the MTE Relay Client. Must be called before `mteFetch`.
13
+ * @param {MteRelayClientOptions} options Configuration options for the client.
14
+ */
15
+ export declare function initMteRelayClient(options: MteRelayClientOptions): Promise<void>;
16
+ /**
17
+ * Sends an MTE-encoded request using a `fetch`-like interface.
18
+ * @param {RequestInfo} url The request URL or Request object.
19
+ * @param {RequestInit} [options] Standard fetch options.
20
+ * @param {Partial<MteRequestOptions>} [mteOptions] MTE-specific options.
21
+ * @returns {Promise<Response>} A Promise resolving to the decoded Response.
22
+ */
23
+ export declare function mteFetch(url: RequestInfo, options?: RequestInit, mteOptions?: Partial<MteRequestOptions>): Promise<Response>;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * This file contains functions for managing the MTE lifecycle, including WASM
3
+ * initialization, encoder/decoder instantiation, and object pooling. These
4
+ * helpers abstract the low-level details of interacting with the MTE Core library.
5
+ */
6
+ import { MteDec, MteEnc, MteMkeDec, MteMkeEnc } from "mte";
7
+ import type { EncDec } from "./types";
8
+ /**
9
+ * Initializes the MTE WASM module and validates the license.
10
+ * @param options License information.
11
+ */
12
+ export declare function initWasm(options: {
13
+ licenseKey: string;
14
+ companyName: string;
15
+ }): Promise<void>;
16
+ /**
17
+ * Retrieves an encoder or decoder instance from the appropriate pool.
18
+ * @param type The type of instance ("MTE" or "MKE").
19
+ * @param role The role of the instance ("encoder" or "decoder").
20
+ * @returns An MTE instance.
21
+ */
22
+ export declare function getPoolItem(type: "MTE" | "MKE", role: "encoder"): MteEnc | MteMkeEnc;
23
+ export declare function getPoolItem(type: "MTE" | "MKE", role: "decoder"): MteDec | MteMkeDec;
24
+ /**
25
+ * Returns an MTE instance to its pool for reuse.
26
+ * @param {EncDec} item The MTE instance to return.
27
+ */
28
+ export declare function returnPoolItem(item: EncDec): void;
29
+ /**
30
+ * Creates and initializes a new MTE encoder instance.
31
+ * @param options Configuration for the new encoder.
32
+ */
33
+ export declare function instantiateEncoder(options: {
34
+ origin: string;
35
+ pairId: string;
36
+ entropy: Uint8Array;
37
+ nonce: string;
38
+ personalization: string;
39
+ }): Promise<void>;
40
+ /**
41
+ * Creates and initializes a new MTE decoder instance.
42
+ * @param options Configuration for the new decoder.
43
+ */
44
+ export declare function instantiateDecoder(options: {
45
+ origin: string;
46
+ pairId: string;
47
+ entropy: Uint8Array;
48
+ nonce: string;
49
+ personalization: string;
50
+ }): Promise<void>;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * This file contains functions for managing the saving and restoring of MTE
3
+ * encoder and decoder states. This is crucial for stateless operations and
4
+ * for persisting MTE sessions across requests.
5
+ */
6
+ import type { EncDec } from "./types";
7
+ /**
8
+ * Saves the current state of an MTE instance to a Base64 string.
9
+ * @param {EncDec} encdec The MTE instance.
10
+ * @returns {string} The Base64 encoded state.
11
+ */
12
+ export declare function getMteState(encdec: EncDec): string;
13
+ /**
14
+ * Restores an MTE instance's state from a Base64 string.
15
+ * @param {EncDec} encdec The MTE instance.
16
+ * @param {string} state The Base64 encoded state to restore.
17
+ */
18
+ export declare function restoreMteState(encdec: EncDec, state: string): void;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * This file contains the logic for validating and pairing with an MTE Relay server.
3
+ * It handles the initial handshake to verify the server's identity and then
4
+ * establishes the encrypted communication channels (pairs) used for subsequent
5
+ * requests.
6
+ */
7
+ /**
8
+ * Validates that a remote origin is an MTE Relay server.
9
+ * @param {string} origin The origin URL to validate.
10
+ */
11
+ export declare function validateRemoteIsMteRelay(origin: string): Promise<void>;
12
+ /**
13
+ * Establishes MTE encoder/decoder pairs with the MTE Relay server.
14
+ * @param {string} origin The origin URL to pair with.
15
+ * @param {number} [numberOfPairs] The number of pairs to create.
16
+ */
17
+ export declare function pairWithOrigin(origin: string, numberOfPairs?: number): Promise<void>;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * This file provides isomorphic storage solutions for the MTE Relay client.
3
+ * It defines a standard interface and offers default implementations for
4
+ * in-memory and browser localStorage, allowing the client to run in various
5
+ * JavaScript environments.
6
+ */
7
+ import type { MteRelayStorage } from "./types";
8
+ /**
9
+ * A default in-memory storage implementation for server-side or environments
10
+ * without persistent storage.
11
+ */
12
+ export declare class MemoryStorage implements MteRelayStorage {
13
+ private store;
14
+ getItem(key: string): string | null;
15
+ setItem(key: string, value: string): void;
16
+ removeItem(key: string): void;
17
+ }
18
+ /**
19
+ * A wrapper for browser's localStorage to be used as persistent storage.
20
+ */
21
+ export declare class LocalStorageWrapper implements MteRelayStorage {
22
+ getItem(key: string): string | null;
23
+ setItem(key: string, value: string): void;
24
+ removeItem(key: string): void;
25
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * This file contains shared TypeScript types and interfaces used throughout the
3
+ * MTE Relay client library. Centralizing these types helps maintain consistency
4
+ * and improves code clarity.
5
+ */
6
+ import { MteDec, MteEnc, MteMkeDec, MteMkeEnc } from "mte";
7
+ /**
8
+ * Interface for persistent storage. Allows for custom implementations
9
+ * in different environments (e.g., localStorage in browser, file system in Node).
10
+ */
11
+ export interface MteRelayStorage {
12
+ getItem(key: string): Promise<string | null> | string | null;
13
+ setItem(key: string, value: string): Promise<void> | void;
14
+ removeItem(key: string): Promise<void> | void;
15
+ }
16
+ /**
17
+ * Configuration options for initializing the MTE Relay Client.
18
+ */
19
+ export interface MteRelayClientOptions {
20
+ licenseKey: string;
21
+ licenseCompany: string;
22
+ numberOfPairs?: number;
23
+ mtePoolSize?: number;
24
+ mkePoolSize?: number;
25
+ encodeType?: "MTE" | "MKE";
26
+ encodeUrls?: boolean;
27
+ encodeHeaders?: boolean | string[];
28
+ pathPrefix?: string;
29
+ storage?: MteRelayStorage;
30
+ fetch?: typeof fetch;
31
+ }
32
+ /**
33
+ * Per-request options to override the default MTE encoding behavior.
34
+ */
35
+ export interface MteRequestOptions {
36
+ encodeUrl: boolean;
37
+ encodeHeaders: boolean | string[];
38
+ encodeType: "MTE" | "MKE";
39
+ useStreaming: boolean;
40
+ pathPrefix: string;
41
+ }
42
+ /**
43
+ * Represents the parsed data from the x-mte-relay header.
44
+ */
45
+ export interface MteRelayHeader {
46
+ clientId: string;
47
+ pairId: string;
48
+ type: "MTE" | "MKE";
49
+ urlIsEncoded: boolean;
50
+ headersAreEncoded: boolean;
51
+ bodyIsEncoded: boolean;
52
+ useStreaming: boolean;
53
+ }
54
+ /**
55
+ * The possible validation and pairing statuses for a given origin.
56
+ */
57
+ export type OriginStatus = "paired" | "invalid" | "validate" | "pending";
58
+ /**
59
+ * A union type representing any of the possible MTE encoder or decoder instances.
60
+ */
61
+ export type EncDec = MteMkeEnc | MteMkeDec | MteEnc | MteDec;
62
+ /**
63
+ * A union type representing the two supported encoding types.
64
+ */
65
+ export type EncDecTypes = "MTE" | "MKE";
@@ -0,0 +1,51 @@
1
+ /**
2
+ * This file contains utility functions for cryptography, data encoding,
3
+ * header manipulation, and MTE status validation. These helpers are used
4
+ * across different modules to perform common tasks.
5
+ */
6
+ import { MteBase, MteStatus } from "mte";
7
+ import type { EncDec, MteRelayHeader } from "./types";
8
+ /**
9
+ * Creates a Kyber initiator instance for PQC key exchange.
10
+ * @returns An object with the public key and a function to decrypt the secret.
11
+ */
12
+ export declare function getKyberInitiator(): {
13
+ publicKey: string;
14
+ decryptSecret: (encryptedSecretB64: string) => Uint8Array;
15
+ };
16
+ /**
17
+ * Generates a cryptographically random 16-byte hex string.
18
+ * @returns {string} A random hex string.
19
+ */
20
+ export declare function getRandomStr(): string;
21
+ /**
22
+ * Converts a Uint8Array to a Base64 string.
23
+ * @param {Uint8Array} bytes The byte array to convert.
24
+ * @returns {string} The Base64 encoded string.
25
+ */
26
+ export declare function u8ToB64(bytes: Uint8Array): string;
27
+ /**
28
+ * Converts a Base64 string to a Uint8Array.
29
+ * @param {string} base64 The Base64 string to convert.
30
+ * @returns {Uint8Array} The decoded byte array.
31
+ */
32
+ export declare function b64ToU8(base64: string): Uint8Array;
33
+ /**
34
+ * Formats the MTE Relay header object into a comma-separated string.
35
+ * @param {MteRelayHeader} options The header data.
36
+ * @returns {string} The formatted header string.
37
+ */
38
+ export declare function formatMteRelayHeader(options: MteRelayHeader): string;
39
+ /**
40
+ * Parses a comma-separated MTE Relay header string into an object.
41
+ * @param {string} header The header string.
42
+ * @returns {MteRelayHeader} The parsed header data.
43
+ */
44
+ export declare function parseMteRelayHeader(header: string): MteRelayHeader;
45
+ /**
46
+ * Validates that an MTE operation status is successful, throwing an error if not.
47
+ * @param {MteStatus} status The status returned from an MTE operation.
48
+ * @param {MteBase} mteBase The MTE instance used, for error reporting.
49
+ */
50
+ export declare function validateStatusIsSuccess(status: MteStatus, mteBase: MteBase): void;
51
+ export declare function drbgReseedCheck(encdec: EncDec): void;
package/package.json CHANGED
@@ -1,16 +1,31 @@
1
- {
2
- "name": "mte-relay-browser-public-client",
3
- "version": "4.4.5",
4
- "main": "./src/mte-relay-browser.js",
5
- "scripts": {},
6
- "author": "Eclypses Inc.",
7
- "license": "ISC",
8
- "description": "MTE Relay Browser is the Javascript client library for MTE Relay Server. See more at https://public-docs.eclypses.com/docs/mte-relay-server.",
9
- "dependencies": {},
10
- "exports": {
11
- ".": {
12
- "import": "./src/mte-relay-browser.mjs",
13
- "require": "./src/mte-relay-browser.js"
14
- }
15
- }
16
- }
1
+ {
2
+ "name": "mte-relay-browser-public-client",
3
+ "version": "4.5.2",
4
+ "main": "./src/index.ts",
5
+ "scripts": {
6
+ "build": "tsc && vite build"
7
+ },
8
+ "author": "Eclypses Inc.",
9
+ "license": "ISC",
10
+ "description": "MTE Relay Browser is the Javascript client library for MTE Relay Server. See more at https://public-docs.eclypses.com/docs/mte-relay-server.",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js",
15
+ "types": "./dist/types/index.d.ts"
16
+ }
17
+ },
18
+ "types": "./dist/types/index.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "devDependencies": {
23
+ "@rollup/plugin-typescript": "^12.1.4",
24
+ "mte": "npm:@eclypses/eclypses-inc-aws-client-aws-config-with-kyber@^4.2.0",
25
+ "mte-relay-browser": "^4.4.2",
26
+ "ts-node": "^10.9.2",
27
+ "tslib": "^2.8.1",
28
+ "typescript": "^5.9.2",
29
+ "vite": "^7.1.4"
30
+ }
31
+ }