@synet/encoder 1.0.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.
- package/MANIFESTO.md +220 -0
- package/MANUAL.md +735 -0
- package/README.md +235 -0
- package/biome.json +37 -0
- package/dist/encoder.unit.d.ts +137 -0
- package/dist/encoder.unit.js +517 -0
- package/dist/functions.d.ts +74 -0
- package/dist/functions.js +243 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +38 -0
- package/dist/result.d.ts +23 -0
- package/dist/result.js +69 -0
- package/package.json +55 -0
- package/src/encoder.unit.ts +654 -0
- package/src/functions.ts +252 -0
- package/src/index.ts +49 -0
- package/src/result.ts +81 -0
- package/test/encoder-unit.test.ts +424 -0
- package/test/functions.test.ts +386 -0
- package/tsconfig.json +19 -0
- package/vitest.config.ts +12 -0
package/README.md
ADDED
@@ -0,0 +1,235 @@
|
|
1
|
+
# @synet/encoder
|
2
|
+
|
3
|
+
```bash
|
4
|
+
8"""" 8 8
|
5
|
+
8 eeeee eeee eeeee eeeee eeee eeeee 8 8 eeeee e eeeee
|
6
|
+
8eeee 8 8 8 8 8 88 8 8 8 8 8 8e 8 8 8 8 8
|
7
|
+
88 8e 8 8e 8 8 8e 8 8eee 8eee8e 88 8 8e 8 8e 8e
|
8
|
+
88 88 8 88 8 8 88 8 88 88 8 88 8 88 8 88 88
|
9
|
+
88eee 88 8 88e8 8eee8 88ee8 88ee 88 8 88ee8 88 8 88 88
|
10
|
+
|
11
|
+
version:1.0.0
|
12
|
+
```
|
13
|
+
**Production-ready Unit Architecture compliant encoding/decoding operations**
|
14
|
+
|
15
|
+
A conscious software unit that transforms data between multiple encoding formats with immutable design and comprehensive validation.
|
16
|
+
|
17
|
+
## Features
|
18
|
+
|
19
|
+
- **Unit Architecture Compliant** - Consciousness-based design with teaching/learning capabilities
|
20
|
+
- **Immutable by Design** - No mutable state, purely functional operations
|
21
|
+
- **Multiple Formats** - Base64, Base64URL, Hex, URI, ASCII encoding support
|
22
|
+
- **Auto-detection** - Intelligent format detection with confidence scoring
|
23
|
+
- **Pure Functions** - Serverless-ready stateless operations
|
24
|
+
- **Chainable Operations** - Sequential encoding/decoding workflows
|
25
|
+
- **Zero Dependencies** - Only Node.js/browser native APIs
|
26
|
+
- **Result Pattern** - Error-safe operations with validation
|
27
|
+
- **Teaching Capability** - Other units can learn encoding capabilities
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
```bash
|
32
|
+
npm install @synet/encoder
|
33
|
+
```
|
34
|
+
|
35
|
+
## Quick Start
|
36
|
+
|
37
|
+
### Unit Architecture Pattern
|
38
|
+
|
39
|
+
```typescript
|
40
|
+
import { Encoder } from '@synet/encoder';
|
41
|
+
|
42
|
+
// Create encoder unit
|
43
|
+
const encoder = Encoder.create({
|
44
|
+
defaultFormat: 'base64',
|
45
|
+
autoDetect: true,
|
46
|
+
strictMode: false
|
47
|
+
});
|
48
|
+
|
49
|
+
// Encode data (Result pattern for safety)
|
50
|
+
const encoded = encoder.encode('Hello World', 'base64');
|
51
|
+
if (encoded.isSuccess) {
|
52
|
+
console.log(encoded.value.encoded); // SGVsbG8gV29ybGQ=
|
53
|
+
}
|
54
|
+
|
55
|
+
// Auto-detection decoding
|
56
|
+
const decoded = encoder.decode('48656c6c6f'); // Auto-detects hex
|
57
|
+
if (decoded.isSuccess) {
|
58
|
+
console.log(decoded.value.decoded); // Hello
|
59
|
+
console.log(decoded.value.detectedFormat); // hex
|
60
|
+
}
|
61
|
+
|
62
|
+
// Format detection
|
63
|
+
const detection = encoder.detect('SGVsbG8gV29ybGQh');
|
64
|
+
console.log(detection.format); // base64url
|
65
|
+
console.log(detection.confidence); // 0.9
|
66
|
+
```
|
67
|
+
|
68
|
+
### Pure Functions Pattern
|
69
|
+
|
70
|
+
```typescript
|
71
|
+
import {
|
72
|
+
encode,
|
73
|
+
decode,
|
74
|
+
base64Encode,
|
75
|
+
hexDecode,
|
76
|
+
detectFormat,
|
77
|
+
chainEncode
|
78
|
+
} from '@synet/encoder';
|
79
|
+
|
80
|
+
// Simple encoding
|
81
|
+
const encoded = encode('Hello World', 'base64');
|
82
|
+
console.log(encoded); // SGVsbG8gV29ybGQ=
|
83
|
+
|
84
|
+
// Format-specific functions
|
85
|
+
const hex = hexEncode('Hello');
|
86
|
+
console.log(hex); // 48656c6c6f
|
87
|
+
|
88
|
+
// Auto-detection
|
89
|
+
const format = detectFormat('48656c6c6f');
|
90
|
+
console.log(format); // hex
|
91
|
+
|
92
|
+
// Chain operations
|
93
|
+
const chained = chainEncode('Hello', ['hex', 'base64']);
|
94
|
+
console.log(chained); // NDg2NTZjNmM2Zg==
|
95
|
+
```
|
96
|
+
|
97
|
+
## Supported Formats
|
98
|
+
|
99
|
+
| Format | Description | Example |
|
100
|
+
|--------|-------------|---------|
|
101
|
+
| `base64` | Standard Base64 (RFC 4648) | `SGVsbG8gV29ybGQ=` |
|
102
|
+
| `base64url` | URL-safe Base64 (no padding) | `SGVsbG8gV29ybGQh` |
|
103
|
+
| `hex` | Hexadecimal encoding | `48656c6c6f` |
|
104
|
+
| `uri` | URI component encoding | `Hello%20World` |
|
105
|
+
| `ascii` | ASCII text validation | `Hello World` |
|
106
|
+
|
107
|
+
## Configuration Options
|
108
|
+
|
109
|
+
```typescript
|
110
|
+
const encoder = Encoder.create({
|
111
|
+
defaultFormat: 'base64', // Default encoding format
|
112
|
+
autoDetect: true, // Enable auto-detection for decoding
|
113
|
+
strictMode: false, // Strict validation mode
|
114
|
+
maxInputSize: 10 * 1024 * 1024, // 10MB input limit
|
115
|
+
validateOutput: true // Validate encoded output
|
116
|
+
});
|
117
|
+
```
|
118
|
+
|
119
|
+
## Unit Architecture Integration
|
120
|
+
|
121
|
+
### Teaching Capabilities
|
122
|
+
|
123
|
+
```typescript
|
124
|
+
const encoder = Encoder.create();
|
125
|
+
|
126
|
+
// Teach encoding capabilities to other units
|
127
|
+
const learner = SomeOtherUnit.create();
|
128
|
+
learner.learn([encoder.teach()]);
|
129
|
+
|
130
|
+
// Use learned capabilities
|
131
|
+
const result = learner.execute('encoder.encode', 'Hello', 'base64');
|
132
|
+
```
|
133
|
+
|
134
|
+
### Learning from Others
|
135
|
+
|
136
|
+
```typescript
|
137
|
+
// Encoder can learn from crypto units for advanced operations
|
138
|
+
const cryptoUnit = CryptoUnit.create();
|
139
|
+
encoder.learn([cryptoUnit.teach()]);
|
140
|
+
|
141
|
+
// Now has enhanced capabilities
|
142
|
+
encoder.execute('crypto.sign', data);
|
143
|
+
```
|
144
|
+
|
145
|
+
## Error Handling
|
146
|
+
|
147
|
+
The encoder follows **Doctrine #14: ERROR BOUNDARY CLARITY**:
|
148
|
+
|
149
|
+
- **Simple operations** (detect, validate) throw exceptions
|
150
|
+
- **Complex operations** (encode, decode, chain) return Result objects
|
151
|
+
|
152
|
+
```typescript
|
153
|
+
// Throws on error (simple classification)
|
154
|
+
try {
|
155
|
+
const detection = encoder.detect('invalid-data');
|
156
|
+
} catch (error) {
|
157
|
+
console.log('Detection failed:', error.message);
|
158
|
+
}
|
159
|
+
|
160
|
+
// Result pattern (complex validation)
|
161
|
+
const encoded = encoder.encode('data', 'base64');
|
162
|
+
if (encoded.isFailure) {
|
163
|
+
console.log('Encoding failed:', encoded.error);
|
164
|
+
console.log('Cause:', encoded.errorCause);
|
165
|
+
}
|
166
|
+
```
|
167
|
+
|
168
|
+
## Chain Operations
|
169
|
+
|
170
|
+
```typescript
|
171
|
+
// Sequential encoding
|
172
|
+
const result = encoder.chain('Hello', ['hex', 'base64', 'uri']);
|
173
|
+
if (result.isSuccess) {
|
174
|
+
console.log(result.value.encoded);
|
175
|
+
console.log(result.value.compressionRatio);
|
176
|
+
}
|
177
|
+
|
178
|
+
// Reverse decoding
|
179
|
+
const reversed = encoder.reverse(result.value.encoded, ['hex', 'base64', 'uri']);
|
180
|
+
console.log(reversed.value.decoded); // Hello
|
181
|
+
```
|
182
|
+
|
183
|
+
## Performance Features
|
184
|
+
|
185
|
+
- **Stateless operations** - No side effects, safe for concurrency
|
186
|
+
- **Immutable design** - Thread-safe by default
|
187
|
+
- **Pure functions** - Optimal for serverless environments
|
188
|
+
- **Zero allocations** - Efficient for high-throughput scenarios
|
189
|
+
|
190
|
+
## Browser Compatibility
|
191
|
+
|
192
|
+
Works in both Node.js and browser environments:
|
193
|
+
|
194
|
+
```typescript
|
195
|
+
// Automatically uses Buffer in Node.js, btoa/atob in browsers
|
196
|
+
const encoder = Encoder.create();
|
197
|
+
const encoded = encoder.encode('Hello World', 'base64');
|
198
|
+
```
|
199
|
+
|
200
|
+
## Help System
|
201
|
+
|
202
|
+
```typescript
|
203
|
+
// Get unit help
|
204
|
+
encoder.help();
|
205
|
+
|
206
|
+
// Static help
|
207
|
+
Encoder.help();
|
208
|
+
|
209
|
+
// Capability inspection
|
210
|
+
console.log(encoder.capabilities());
|
211
|
+
console.log(encoder.whoami());
|
212
|
+
```
|
213
|
+
|
214
|
+
## TypeScript Support
|
215
|
+
|
216
|
+
Full TypeScript support with comprehensive type definitions:
|
217
|
+
|
218
|
+
```typescript
|
219
|
+
import type {
|
220
|
+
EncodingFormat,
|
221
|
+
EncodingResult,
|
222
|
+
DecodingResult,
|
223
|
+
DetectionResult
|
224
|
+
} from '@synet/encoder';
|
225
|
+
```
|
226
|
+
|
227
|
+
## License
|
228
|
+
|
229
|
+
MIT
|
230
|
+
|
231
|
+
## Contributing
|
232
|
+
|
233
|
+
Part of the [Unit Architecture](https://github.com/synthetism/unit) ecosystem. See the main repository for contribution guidelines.
|
234
|
+
|
235
|
+
---
|
package/biome.json
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
{
|
2
|
+
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
|
3
|
+
"vcs": {
|
4
|
+
"enabled": false,
|
5
|
+
"clientKind": "git",
|
6
|
+
"useIgnoreFile": false
|
7
|
+
},
|
8
|
+
"files": {
|
9
|
+
"ignoreUnknown": false,
|
10
|
+
"ignore": ["src/services/vault-manager.ts","docs/**", "dist/**", "node_modules/**", "coverage/**", "test/**", "tests/**", "examples/**", "scripts/**"]
|
11
|
+
},
|
12
|
+
"formatter": {
|
13
|
+
"enabled": true,
|
14
|
+
"indentWidth": 2,
|
15
|
+
"lineWidth": 80,
|
16
|
+
"indentStyle": "space"
|
17
|
+
},
|
18
|
+
"organizeImports": {
|
19
|
+
"enabled": true
|
20
|
+
},
|
21
|
+
|
22
|
+
"linter": {
|
23
|
+
"enabled": true,
|
24
|
+
"rules": {
|
25
|
+
"recommended": true,
|
26
|
+
"complexity": {
|
27
|
+
"noStaticOnlyClass": "off"
|
28
|
+
}
|
29
|
+
}
|
30
|
+
},
|
31
|
+
|
32
|
+
"javascript": {
|
33
|
+
"formatter": {
|
34
|
+
"quoteStyle": "double"
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
@@ -0,0 +1,137 @@
|
|
1
|
+
/**
|
2
|
+
* Encoder Unit - Conscious Encoding/Decoding Operations
|
3
|
+
*
|
4
|
+
* SYNET Unit Architecture v1.0.6 Implementation
|
5
|
+
*
|
6
|
+
* Philosophy: One unit, one goal - reliable data transformation
|
7
|
+
*
|
8
|
+
* Native Capabilities:
|
9
|
+
* - encode() - Transform data to specified format
|
10
|
+
* - decode() - Reverse transformation with validation
|
11
|
+
* - detect() - Auto-detect encoding format
|
12
|
+
* - validate() - Verify encoded data integrity
|
13
|
+
* - chain() - Sequential encoding operations
|
14
|
+
*
|
15
|
+
* Supported Formats: Base64, Base64URL, Hex, URI, ASCII
|
16
|
+
*
|
17
|
+
* @author SYNET ALPHA
|
18
|
+
* @version 1.0.0
|
19
|
+
* @follows Unit Architecture Doctrine v1.0.6
|
20
|
+
*/
|
21
|
+
import { Unit, type UnitProps, type TeachingContract } from '@synet/unit';
|
22
|
+
import { Result } from './result.js';
|
23
|
+
/**
|
24
|
+
* External input configuration for static create()
|
25
|
+
*/
|
26
|
+
export interface EncoderConfig {
|
27
|
+
defaultFormat?: EncodingFormat;
|
28
|
+
strictMode?: boolean;
|
29
|
+
autoDetect?: boolean;
|
30
|
+
maxInputSize?: number;
|
31
|
+
validateOutput?: boolean;
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* Internal state after validation (implements UnitProps)
|
35
|
+
*/
|
36
|
+
export interface EncoderProps extends UnitProps {
|
37
|
+
defaultFormat: EncodingFormat;
|
38
|
+
strictMode: boolean;
|
39
|
+
autoDetect: boolean;
|
40
|
+
maxInputSize: number;
|
41
|
+
validateOutput: boolean;
|
42
|
+
readonly created: Date;
|
43
|
+
}
|
44
|
+
/**
|
45
|
+
* Encoding format types
|
46
|
+
*/
|
47
|
+
export type EncodingFormat = 'base64' | 'base64url' | 'hex' | 'uri' | 'ascii';
|
48
|
+
/**
|
49
|
+
* Encoding operation result
|
50
|
+
*/
|
51
|
+
export interface EncodingResult {
|
52
|
+
readonly encoded: string;
|
53
|
+
readonly originalSize: number;
|
54
|
+
readonly encodedSize: number;
|
55
|
+
readonly format: EncodingFormat;
|
56
|
+
readonly compressionRatio: number;
|
57
|
+
readonly timestamp: Date;
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Decoding operation result
|
61
|
+
*/
|
62
|
+
export interface DecodingResult {
|
63
|
+
readonly decoded: string;
|
64
|
+
readonly detectedFormat: EncodingFormat;
|
65
|
+
readonly isValid: boolean;
|
66
|
+
readonly originalSize: number;
|
67
|
+
readonly timestamp: Date;
|
68
|
+
}
|
69
|
+
/**
|
70
|
+
* Format detection result
|
71
|
+
*/
|
72
|
+
export interface DetectionResult {
|
73
|
+
readonly format: EncodingFormat;
|
74
|
+
readonly confidence: number;
|
75
|
+
readonly reasons: string[];
|
76
|
+
readonly timestamp: Date;
|
77
|
+
}
|
78
|
+
/**
|
79
|
+
* Validation result
|
80
|
+
*/
|
81
|
+
export interface ValidationResult {
|
82
|
+
readonly isValid: boolean;
|
83
|
+
readonly format: EncodingFormat;
|
84
|
+
readonly errors: string[];
|
85
|
+
readonly suggestions: string[];
|
86
|
+
}
|
87
|
+
/**
|
88
|
+
* Encoder Implementation
|
89
|
+
*
|
90
|
+
* Doctrine #1: ZERO DEPENDENCY (only Node.js/browser native APIs)
|
91
|
+
* Doctrine #17: VALUE OBJECT FOUNDATION (immutable with identity and capabilities)
|
92
|
+
*/
|
93
|
+
export declare class Encoder extends Unit<EncoderProps> {
|
94
|
+
protected constructor(props: EncoderProps);
|
95
|
+
static create(config?: EncoderConfig): Encoder;
|
96
|
+
help(): void;
|
97
|
+
teach(): TeachingContract;
|
98
|
+
/**
|
99
|
+
* Encode data to specified format (Result - complex validation operation)
|
100
|
+
*/
|
101
|
+
encode(data: string, format?: EncodingFormat): Result<EncodingResult>;
|
102
|
+
/**
|
103
|
+
* Decode data with optional format hint (Result - complex auto-detection operation)
|
104
|
+
*/
|
105
|
+
decode(data: string, format?: EncodingFormat): Result<DecodingResult>;
|
106
|
+
/**
|
107
|
+
* Chain multiple encoding operations (Result - complex multi-step operation)
|
108
|
+
*/
|
109
|
+
chain(data: string, formats: EncodingFormat[]): Result<EncodingResult>;
|
110
|
+
/**
|
111
|
+
* Reverse chain decoding (Result - complex multi-step operation)
|
112
|
+
*/
|
113
|
+
reverse(data: string, formats: EncodingFormat[]): Result<DecodingResult>;
|
114
|
+
/**
|
115
|
+
* Auto-detect encoding format (throw on error - simple classification operation)
|
116
|
+
*/
|
117
|
+
detect(data: string): DetectionResult;
|
118
|
+
/**
|
119
|
+
* Validate encoded data format (throw on error - simple validation operation)
|
120
|
+
*/
|
121
|
+
validate(data: string, format: EncodingFormat): ValidationResult;
|
122
|
+
private performEncode;
|
123
|
+
private performDecode;
|
124
|
+
private encodeBase64;
|
125
|
+
private decodeBase64;
|
126
|
+
private encodeBase64URL;
|
127
|
+
private decodeBase64URL;
|
128
|
+
private encodeHex;
|
129
|
+
private decodeHex;
|
130
|
+
private encodeURI;
|
131
|
+
private decodeURI;
|
132
|
+
private encodeASCII;
|
133
|
+
private decodeASCII;
|
134
|
+
private isValidInput;
|
135
|
+
whoami(): string;
|
136
|
+
toJSON(): Record<string, unknown>;
|
137
|
+
}
|