@nhtio/encoder 0.1.0-master-ab456424

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/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@nhtio/encoder",
3
+ "version": "0.1.0-master-ab456424",
4
+ "description": "A portable cross-environment encoder / decoder for complex and structured data",
5
+ "keywords": [],
6
+ "author": "Jak Giveon <jak@nht.io>",
7
+ "copyright": "© 2025-present New Horizon Technology LTD",
8
+ "license": "MIT",
9
+ "module": "./index.mjs",
10
+ "main": "./index.cjs",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./index.mjs",
14
+ "require": "./index.cjs",
15
+ "types": "./index.d.ts"
16
+ },
17
+ "./exceptions": {
18
+ "import": "./exceptions.mjs",
19
+ "require": "./exceptions.cjs",
20
+ "types": "./exceptions.d.ts"
21
+ },
22
+ "./types": {
23
+ "import": "./types.mjs",
24
+ "require": "./types.cjs",
25
+ "types": "./types.d.ts"
26
+ }
27
+ },
28
+ "packageManager": "pnpm@10.8.0+sha512.0e82714d1b5b43c74610193cb20734897c1d00de89d0e18420aebc5977fa13d780a9cb05734624e81ebd81cc876cd464794850641c48b9544326b5622ca29971",
29
+ "type": "module",
30
+ "dependencies": {}
31
+ }
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Base exception class that extends the native Error class.
3
+ * Provides a foundation for all custom exceptions with additional metadata
4
+ * like error codes, status codes, and help descriptions.
5
+ */
6
+ export declare class BaseException extends Error {
7
+ /**
8
+ * Define the error metadata as static properties to avoid
9
+ * setting them repeatedly on the error instance
10
+ */
11
+ static help?: string;
12
+ static code?: string;
13
+ static status?: number;
14
+ static message?: string;
15
+ /**
16
+ * Name of the class that raised the exception.
17
+ */
18
+ name: string;
19
+ /**
20
+ * Optional help description for the error. You can use it to define additional
21
+ * human readable information for the error.
22
+ */
23
+ help?: string;
24
+ /**
25
+ * A machine readable error code. This will allow the error handling logic
26
+ * to narrow down exceptions based upon the error code.
27
+ */
28
+ code?: string;
29
+ /**
30
+ * A status code for the error. Usually helpful when converting errors
31
+ * to HTTP responses.
32
+ */
33
+ status: number;
34
+ /**
35
+ * Creates a new BaseException instance.
36
+ *
37
+ * @param message - The error message. If not provided, uses the static message from the constructor.
38
+ * @param options - Additional options including error cause, code, and status.
39
+ */
40
+ constructor(message?: string, options?: ErrorOptions & {
41
+ code?: string;
42
+ status?: number;
43
+ });
44
+ /**
45
+ * Returns the constructor name for the Symbol.toStringTag property.
46
+ *
47
+ * @returns The name of the exception class.
48
+ */
49
+ get [Symbol.toStringTag](): string;
50
+ /**
51
+ * Returns a string representation of the exception.
52
+ * Includes the error code in brackets if available.
53
+ *
54
+ * @returns A formatted string representation of the exception.
55
+ */
56
+ toString(): string;
57
+ }
58
+ /**
59
+ * Exception thrown when a value cannot be encoded.
60
+ * This indicates that the value type is not supported by the encoder.
61
+ */
62
+ export declare class E_UNENCODABLE_VALUE extends BaseException {
63
+ static status: number;
64
+ static code: string;
65
+ /**
66
+ * Creates a new E_UNENCODABLE_VALUE exception.
67
+ *
68
+ * @param value - The value that could not be encoded.
69
+ */
70
+ constructor(value: unknown);
71
+ }
72
+ /**
73
+ * Exception thrown when encoding fails due to an underlying error.
74
+ * This wraps the original error as the cause for better error tracing.
75
+ */
76
+ export declare class E_ENCODING_FAILED extends BaseException {
77
+ static status: number;
78
+ static code: string;
79
+ /**
80
+ * Creates a new E_ENCODING_FAILED exception.
81
+ *
82
+ * @param value - The value that failed to encode.
83
+ * @param cause - The underlying error that caused the encoding to fail.
84
+ */
85
+ constructor(value: unknown, cause: unknown);
86
+ }
87
+ /**
88
+ * Exception thrown when a circular reference is detected during encoding.
89
+ * Circular references cannot be encoded and must be avoided.
90
+ */
91
+ export declare class E_CIRCULAR_REFERENCE extends BaseException {
92
+ static status: number;
93
+ static code: string;
94
+ /**
95
+ * Creates a new E_CIRCULAR_REFERENCE exception.
96
+ */
97
+ constructor();
98
+ }
99
+ /**
100
+ * Exception thrown when a value with an unknown or unsupported type cannot be decoded.
101
+ */
102
+ export declare class E_UNDECODABLE_VALUE extends BaseException {
103
+ static status: number;
104
+ static code: string;
105
+ /**
106
+ * Creates a new E_UNDECODABLE_VALUE exception.
107
+ *
108
+ * @param type - The type string that could not be decoded.
109
+ */
110
+ constructor(type: string);
111
+ }
112
+ /**
113
+ * Exception thrown when attempting to decode a value that is not a valid encoded value.
114
+ * This indicates the input string is not in the expected encoded format.
115
+ */
116
+ export declare class E_NOT_AN_ENCODED_VALUE extends BaseException {
117
+ static status: number;
118
+ static code: string;
119
+ /**
120
+ * Creates a new E_NOT_AN_ENCODED_VALUE exception.
121
+ *
122
+ * @param value - The string value that is not a valid encoded value.
123
+ */
124
+ constructor(value: string);
125
+ }
126
+ /**
127
+ * Exception thrown when an invalid version string is encountered.
128
+ */
129
+ export declare class E_INVALID_VERSION extends BaseException {
130
+ static status: number;
131
+ static code: string;
132
+ /**
133
+ * Creates a new E_INVALID_VERSION exception.
134
+ *
135
+ * @param version - The invalid version string.
136
+ */
137
+ constructor(version: string);
138
+ }
139
+ /**
140
+ * Exception thrown when attempting to decode data encoded with an incompatible version.
141
+ * This typically occurs when the encoded data is from a newer version than the decoder supports.
142
+ */
143
+ export declare class E_INCOMPATIBLE_VERSION extends BaseException {
144
+ static status: number;
145
+ static code: string;
146
+ static help: string;
147
+ /**
148
+ * Creates a new E_INCOMPATIBLE_VERSION exception.
149
+ *
150
+ * @param version - The incompatible version string from the encoded data.
151
+ */
152
+ constructor(version: string);
153
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Type definition for a dehydrated function value
3
+ */
4
+ export type DehydratedFunctionValue = {
5
+ _encodedType: 'function';
6
+ _encodedValueType: 'string';
7
+ _encodedValue: string;
8
+ _namedBindings?: Record<string, any>;
9
+ };
10
+ /**
11
+ * A portable function serializer that can dehydrate and rehydrate functions
12
+ * from strings, with support for named bindings and various function types.
13
+ */
14
+ export declare class FunctionSerializer {
15
+ #private;
16
+ /**
17
+ * Private constructor - use static methods instead
18
+ */
19
+ private constructor();
20
+ /**
21
+ * Check if a value can be serialized as a function
22
+ */
23
+ static canSerialize(value: any): boolean;
24
+ /**
25
+ * Dehydrate a function into a serializable format
26
+ */
27
+ static dehydrate(fn: Function, namedBindings?: Record<string, any>): DehydratedFunctionValue;
28
+ /**
29
+ * Rehydrate a function from its dehydrated format or raw function string
30
+ */
31
+ static rehydrate<T extends (...args: any[]) => any>(input: DehydratedFunctionValue | string, namedBindings?: Record<string, any>): T;
32
+ }
@@ -0,0 +1,27 @@
1
+ export declare const stringSchema: import("@nhtio/validation").StringSchema<string>;
2
+ export declare const numberSchema: import("@nhtio/validation").AlternativesSchema<any>;
3
+ export declare const booleanSchema: import("@nhtio/validation").BooleanSchema<boolean>;
4
+ export declare const bigintSchema: import("@nhtio/validation").AnySchema<any>;
5
+ export declare const nullSchema: import("@nhtio/validation").AnySchema<any>;
6
+ export declare const undefinedSchema: import("@nhtio/validation").AnySchema<any>;
7
+ export declare const primitiveSchema: import("@nhtio/validation").AlternativesSchema<any>;
8
+ export declare const int8ArraySchema: import("@nhtio/validation").AnySchema<any>;
9
+ export declare const uint8ArraySchema: import("@nhtio/validation").AnySchema<any>;
10
+ export declare const uint8ClampedArraySchema: import("@nhtio/validation").AnySchema<any>;
11
+ export declare const int16ArraySchema: import("@nhtio/validation").AnySchema<any>;
12
+ export declare const uint16ArraySchema: import("@nhtio/validation").AnySchema<any>;
13
+ export declare const int32ArraySchema: import("@nhtio/validation").AnySchema<any>;
14
+ export declare const uint32ArraySchema: import("@nhtio/validation").AnySchema<any>;
15
+ export declare const float32ArraySchema: import("@nhtio/validation").AnySchema<any>;
16
+ export declare const float64ArraySchema: import("@nhtio/validation").AnySchema<any>;
17
+ export declare const bigInt64ArraySchema: import("@nhtio/validation").AnySchema<any>;
18
+ export declare const bigUint64ArraySchema: import("@nhtio/validation").AnySchema<any>;
19
+ export declare const typedArraySchema: import("@nhtio/validation").AlternativesSchema<any>;
20
+ export declare const bigIntTypedArraySchema: import("@nhtio/validation").AlternativesSchema<any>;
21
+ export declare const dateSchema: import("@nhtio/validation").AnySchema<any>;
22
+ export declare const regExpSchema: import("@nhtio/validation").AnySchema<any>;
23
+ export declare const arrayBufferSchema: import("@nhtio/validation").AnySchema<any>;
24
+ export declare const dataViewSchema: import("@nhtio/validation").AnySchema<any>;
25
+ export declare const errorSchema: import("@nhtio/validation").AnySchema<any>;
26
+ export declare const uniterableObjectSchema: import("@nhtio/validation").AlternativesSchema<any>;
27
+ export declare const phoneObjectSchema: import("@nhtio/validation").AlternativesSchema<any>;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Converts a string into a compressed base64 string
3
+ * @param data The string to be converted into a compressed base64 string
4
+ * @returns The compressed base64 string
5
+ */
6
+ export declare const utoa: (data: string) => string;
7
+ /**
8
+ * Converts a compressed base64 string back into a normal string
9
+ * @param base64 The compressed base64 string to be converted back into a normal string
10
+ * @returns The normal string
11
+ */
12
+ export declare const atou: (base64: string) => string;
@@ -0,0 +1,10 @@
1
+ import type { Encodable } from './types';
2
+ import type { SerializedRecord } from '@ungap/structured-clone';
3
+ export interface StructuredDataObject {
4
+ _t: string;
5
+ _s: {
6
+ [key: string]: StructuredDataObject;
7
+ } | StructuredDataObject | StructuredDataObject[] | [StructuredDataObject, StructuredDataObject][] | SerializedRecord | string;
8
+ }
9
+ export declare const toStructuredData: (value: Encodable, seen?: WeakSet<any>) => StructuredDataObject;
10
+ export declare const fromStructuredData: <T extends Encodable = Encodable>(data: StructuredDataObject) => T;
@@ -0,0 +1,59 @@
1
+ import { DateTime, Duration, Interval, SystemZone } from 'luxon';
2
+ import type { Phone } from '@nhtio/phone-object';
3
+ /**
4
+ * Type guard to check if a value is a plain object (not null, not array)
5
+ * @param value - The value to check
6
+ * @returns True if the value is a plain object, false otherwise
7
+ */
8
+ export declare const isObject: (value: unknown) => value is {
9
+ [key: string]: unknown;
10
+ };
11
+ /**
12
+ * Type guard to check if a value is an array
13
+ * @param value - The value to check
14
+ * @returns True if the value is an array, false otherwise
15
+ */
16
+ export declare const isArray: (value: unknown) => value is unknown[];
17
+ /**
18
+ * Type guard to check if a value is a Luxon DateTime instance
19
+ * @param value - The value to check
20
+ * @returns True if the value is a Luxon DateTime, false otherwise
21
+ */
22
+ export declare const isLuxonDateTime: (value: unknown) => value is DateTime;
23
+ /**
24
+ * Type guard to check if a value is a Luxon Duration instance
25
+ * @param value - The value to check
26
+ * @returns True if the value is a Luxon Duration, false otherwise
27
+ */
28
+ export declare const isLuxonDuration: (value: unknown) => value is Duration;
29
+ /**
30
+ * Type guard to check if a value is a Luxon Interval instance
31
+ * @param value - The value to check
32
+ * @returns True if the value is a Luxon Interval, false otherwise
33
+ */
34
+ export declare const isLuxonInterval: (value: unknown) => value is Interval;
35
+ /**
36
+ * Type guard to check if a value is a valid Lucid binary value (Uint8Array or Buffer)
37
+ * @param value - The value to check
38
+ * @returns True if the value is a LucidBinaryValue, false otherwise
39
+ */
40
+ export declare const isLucidBinaryValue: (value: unknown) => value is Uint8Array | Buffer;
41
+ /**
42
+ * Checks if a value is an instance of a specific class.
43
+ * @param value - The value to check
44
+ * @param type - The name of the class to check against
45
+ * @returns true if the value is an instance of the specified class, false otherwise
46
+ */
47
+ export declare const isInstanceOf: <T>(value: unknown, type: string, ctor?: new (...args: any[]) => T) => value is T;
48
+ export declare const isMap: <K = any, T = any>(value: unknown) => value is Map<K, T>;
49
+ export declare const isSet: <T = any>(value: unknown) => value is Set<T>;
50
+ export declare const isNumber: (value: unknown) => value is number;
51
+ export declare const isNegativeZero: (value: unknown) => value is -0;
52
+ export declare const isError: (value: unknown) => value is Error;
53
+ export declare const isTypedArray: (value: unknown) => value is Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
54
+ export declare const isBigIntTypedArray: (value: unknown) => value is BigInt64Array | BigUint64Array;
55
+ export declare const isUniterableObject: (value: unknown) => value is Date | RegExp | ArrayBuffer | DataView | Error;
56
+ export declare const isPrimitive: (value: unknown) => value is string | number | boolean | bigint | null;
57
+ export declare const isPhoneObject: (value: unknown) => value is Phone;
58
+ export declare const isLuxonSystemZone: (value: unknown) => value is SystemZone;
59
+ export declare const isBigInt: (value: unknown) => value is bigint;
@@ -0,0 +1,16 @@
1
+ import type { Phone } from '@nhtio/phone-object';
2
+ import type { DateTime, Duration, Interval } from 'luxon';
3
+ /**
4
+ * Defines the union of all encodable primitive types.
5
+ */
6
+ export type EncodablePrimitive = string | number | boolean | bigint | null | undefined;
7
+ /**
8
+ * Defines a union for all the encodable typed arrays.
9
+ */
10
+ export type EncodableTypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
11
+ /**
12
+ * Defines all the types which can be be encoded
13
+ */
14
+ export type Encodable = EncodablePrimitive | Date | RegExp | ArrayBuffer | DataView | DateTime | Duration | Interval | EncodableTypedArray | Encodable[] | {
15
+ [key: string]: Encodable;
16
+ } | Map<Encodable, Encodable> | Set<Encodable> | Error | EvalError | RangeError | ReferenceError | SyntaxError | TypeError | URIError | Phone | Function | ((...args: any[]) => any);
package/types.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=types.cjs.map
package/types.cjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/types.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Types used by the encoder/decoder
3
+ * @module @nhtio/encoder/types
4
+ */
5
+ export type { BaseException } from './private/exceptions';
6
+ export type { E_NOT_AN_ENCODED_VALUE, E_INVALID_VERSION, E_INCOMPATIBLE_VERSION, E_ENCODING_FAILED, E_CIRCULAR_REFERENCE, E_UNENCODABLE_VALUE, E_UNDECODABLE_VALUE, } from './private/exceptions';
7
+ export type { Encodable, EncodablePrimitive, EncodableTypedArray } from './private/types';
8
+ export type { Phone } from '@nhtio/phone-object';
9
+ export type { DateTime, Duration, Interval } from 'luxon';
package/types.mjs ADDED
@@ -0,0 +1,2 @@
1
+
2
+ //# sourceMappingURL=types.mjs.map
package/types.mjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}