@ddunigma/node 2.0.0 → 2.0.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.
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
- ## ddunigma Node
1
+ ## ddunigma Node
2
+
2
3
  [![npm version](https://badge.fury.io/js/@ddunigma%2Fnode.svg)](https://www.npmjs.com/package/@ddunigma/node)
3
4
 
4
- ## Overview
5
+ ## Overview
5
6
 
6
7
  Node.js implementation of [ddunigma](https://github.com/i3l3/ddunigma) (Python original)
7
8
 
@@ -46,23 +47,23 @@ console.log(decoded); // "Hello World!"
46
47
  import { Ddu64, DduSetSymbol } from "@ddunigma/node";
47
48
 
48
49
  // ONECHARSET (64개 문자)
49
- const encoder1 = new Ddu64(undefined, undefined, {
50
- dduSetSymbol: DduSetSymbol.ONECHARSET
50
+ const encoder1 = new Ddu64(undefined, undefined, {
51
+ dduSetSymbol: DduSetSymbol.ONECHARSET,
51
52
  });
52
53
 
53
54
  // DDU (8개 문자)
54
- const encoder2 = new Ddu64(undefined, undefined, {
55
- dduSetSymbol: DduSetSymbol.DDU
55
+ const encoder2 = new Ddu64(undefined, undefined, {
56
+ dduSetSymbol: DduSetSymbol.DDU,
56
57
  });
57
58
 
58
59
  // TWOCHARSET (1024개 문자)
59
- const encoder3 = new Ddu64(undefined, undefined, {
60
- dduSetSymbol: DduSetSymbol.TWOCHARSET
60
+ const encoder3 = new Ddu64(undefined, undefined, {
61
+ dduSetSymbol: DduSetSymbol.TWOCHARSET,
61
62
  });
62
63
 
63
64
  // THREECHARSET (32768개 문자)
64
- const encoder4 = new Ddu64(undefined, undefined, {
65
- dduSetSymbol: DduSetSymbol.THREECHARSET
65
+ const encoder4 = new Ddu64(undefined, undefined, {
66
+ dduSetSymbol: DduSetSymbol.THREECHARSET,
66
67
  });
67
68
 
68
69
  const text = "안녕하세요";
@@ -77,8 +78,10 @@ const decodedBuffer = encoder1.decodeToBuffer(encoded);
77
78
  import { Ddu64 } from "@ddunigma/node";
78
79
 
79
80
  const koreanChars = [
80
- "뜌", "뜍", "뜎", "뜏", "뜐", "뜑", "뜒", "뜓", "뜔", "뜕", "뜖", "뜗", "뜘", "뜙", "뜚", "뜛",
81
- "", "", "", "", "", "", "", "", "뜤", "뜥", "뜦", "뜧", "뜨", "뜩", "뜪", "뜫",
81
+ "뜌", "뜍", "뜎", "뜏", "뜐", "뜑", "뜒", "뜓",
82
+ "", "", "", "", "", "", "", "",
83
+ "뜜", "뜝", "뜞", "뜟", "뜠", "뜡", "뜢", "뜣",
84
+ "뜨", "뜩", "뜪", "뜫",
82
85
  // ... 256개 문자
83
86
  ];
84
87
 
@@ -90,6 +93,39 @@ const decoded = encoder.decode(encoded);
90
93
  const decodedBuffer = encoder.decodeToBuffer(encoded);
91
94
  ```
92
95
 
96
+ ### 압축 인코딩 사용
97
+
98
+ ```typescript
99
+ import { Ddu64 } from "@ddunigma/node";
100
+
101
+ // 생성자에서 기본 압축 활성화
102
+ const encoder = new Ddu64(undefined, undefined, {
103
+ compress: true,
104
+ });
105
+
106
+ // 또는 encode 호출 시 압축 옵션 지정
107
+ const text = "반복되는 긴 텍스트...".repeat(100);
108
+ const encoded = encoder.encode(text, { compress: true });
109
+ const decoded = encoder.decode(encoded); // 자동으로 압축 해제
110
+ ```
111
+
112
+ ### Zip Bomb 방어 설정
113
+
114
+ ```typescript
115
+ import { Ddu64 } from "@ddunigma/node";
116
+
117
+ // 디코딩/압축해제 크기 제한 설정 (기본값: 64MB)
118
+ const encoder = new Ddu64(undefined, undefined, {
119
+ maxDecodedBytes: 10 * 1024 * 1024, // 10MB
120
+ maxDecompressedBytes: 50 * 1024 * 1024, // 50MB
121
+ });
122
+
123
+ // 또는 decode 호출 시 옵션 지정
124
+ const decoded = encoder.decode(encoded, {
125
+ maxDecodedBytes: 1024 * 1024, // 1MB
126
+ });
127
+ ```
128
+
93
129
  ## API
94
130
 
95
131
  ### `new Ddu64(dduChar?, paddingChar?, options?)`
@@ -97,29 +133,65 @@ const decodedBuffer = encoder.decodeToBuffer(encoded);
97
133
  인코더 인스턴스를 생성합니다.
98
134
 
99
135
  **Parameters:**
100
- - `dduChar` (string | string[]): charset 문자열 또는 배열
101
- - `paddingChar` (string): 패딩 문자
102
- - `options` (DduOptions): 옵션 객체
103
- - `dduSetSymbol`: 미리 정의된 charset 사용
104
- - `encoding`: Buffer encoding (기본값: 'utf-8')
105
- - `usePowerOfTwo`: 2의 제곱수 강제 여부
106
- - `useBuildErrorReturn`: 에러 발생 시 throw 여부
107
136
 
108
- ### `encode(data: string | Buffer): string`
137
+ | Parameter | Type | Description |
138
+ |-----------|------|-------------|
139
+ | `dduChar` | `string \| string[]` | charset 문자열 또는 배열 |
140
+ | `paddingChar` | `string` | 패딩 문자 |
141
+ | `options` | `DduConstructorOptions` | 옵션 객체 |
109
142
 
110
- 데이터를 인코딩
143
+ **DduConstructorOptions:**
111
144
 
112
- ### `decode(encoded: string): string`
145
+ | Option | Type | Default | Description |
146
+ |--------|------|---------|-------------|
147
+ | `dduSetSymbol` | `DduSetSymbol` | `DDU` | 미리 정의된 charset 심볼 |
148
+ | `encoding` | `BufferEncoding` | `'utf-8'` | 문자열 인코딩 |
149
+ | `usePowerOfTwo` | `boolean` | `true` | 2의 제곱수 강제 여부 |
150
+ | `useBuildErrorReturn` | `boolean` | `false` | 에러 발생 시 throw 여부 |
151
+ | `compress` | `boolean` | `false` | 기본 압축 활성화 |
152
+ | `maxDecodedBytes` | `number` | `67108864` | 최대 디코딩 바이트 (64MB) |
153
+ | `maxDecompressedBytes` | `number` | `67108864` | 최대 압축해제 바이트 (64MB) |
113
154
 
114
- 인코딩된 문자열을 디코딩
155
+ ### `encode(data, options?): string`
115
156
 
116
- ### `decodeToBuffer(encoded: string): Buffer`
157
+ 데이터를 인코딩합니다.
117
158
 
118
- 인코딩된 문자열을 Buffer로 직접 디코딩
159
+ **Parameters:**
160
+
161
+ | Parameter | Type | Description |
162
+ |-----------|------|-------------|
163
+ | `data` | `string \| Buffer` | 인코딩할 데이터 |
164
+ | `options.compress` | `boolean` | 압축 사용 여부 |
165
+
166
+ **Returns:** 인코딩된 문자열
167
+
168
+ ### `decode(encoded, options?): string`
169
+
170
+ 인코딩된 문자열을 디코딩합니다.
171
+
172
+ **Parameters:**
173
+
174
+ | Parameter | Type | Description |
175
+ |-----------|------|-------------|
176
+ | `encoded` | `string` | 인코딩된 문자열 |
177
+ | `options.maxDecodedBytes` | `number` | 최대 디코딩 바이트 |
178
+ | `options.maxDecompressedBytes` | `number` | 최대 압축해제 바이트 |
179
+
180
+ **Returns:** 디코딩된 문자열
119
181
 
120
- ### `getCharSetInfo()`
182
+ ### `decodeToBuffer(encoded, options?): Buffer`
121
183
 
122
- 현재 charset 정보를 반환
184
+ 인코딩된 문자열을 Buffer로 직접 디코딩합니다.
185
+
186
+ **Parameters:** `decode`와 동일
187
+
188
+ **Returns:** 디코딩된 Buffer
189
+
190
+ ### `getCharSetInfo(): CharSetInfo`
191
+
192
+ 현재 인코더의 charset 정보를 반환합니다.
193
+
194
+ **Returns:**
123
195
 
124
196
  ```typescript
125
197
  {
@@ -129,5 +201,17 @@ const decodedBuffer = encoder.decodeToBuffer(encoded);
129
201
  bitLength: number;
130
202
  usePowerOfTwo: boolean;
131
203
  encoding: BufferEncoding;
204
+ defaultCompress: boolean;
205
+ defaultMaxDecodedBytes: number;
206
+ defaultMaxDecompressedBytes: number;
132
207
  }
133
- ```
208
+ ```
209
+
210
+ ## DduSetSymbol
211
+
212
+ | Symbol | 문자 수 | 비트 길이 | 설명 |
213
+ |--------|---------|-----------|------|
214
+ | `DDU` | 8 | 3 | 한글 + 특수문자 기본 세트 |
215
+ | `ONECHARSET` | 64 | 6 | 영문 + 숫자 + 특수문자 |
216
+ | `TWOCHARSET` | 1024 | 10 | 2글자 조합 세트 |
217
+ | `THREECHARSET` | 32768 | 15 | 3글자 조합 세트 |
@@ -1,20 +1,46 @@
1
- import { DduOptions, BufferToDduBinaryResult } from "../types";
1
+ import { DduOptions } from "../types";
2
2
  export declare abstract class BaseDdu {
3
3
  protected readonly defaultEncoding: BufferEncoding;
4
- protected readonly binaryLookup: string[];
5
- protected escapeRegExp(str: string): string;
6
- protected splitString(s: string, length: number): Generator<string>;
7
- protected getLargestPowerOfTwo(n: number): number;
4
+ /**
5
+ * 주어진 숫자보다 작거나 같은 가장 큰 2의 제곱수의 지수를 반환합니다.
6
+ * @param n - 대상 숫자
7
+ * @returns 2의 제곱수 지수 (예: n=8 → 3, n=64 → 6)
8
+ * @example getLargestPowerOfTwoExponent(8) // 3
9
+ * @example getLargestPowerOfTwoExponent(100) // 6
10
+ */
8
11
  protected getLargestPowerOfTwoExponent(n: number): number;
12
+ /**
13
+ * charset 크기에 필요한 비트 길이를 계산합니다.
14
+ * @param setLength - charset 문자 수
15
+ * @returns 필요한 비트 수
16
+ * @example getBitLength(64) // 6
17
+ * @example getBitLength(100) // 7
18
+ */
9
19
  protected getBitLength(setLength: number): number;
10
- protected bufferToDduBinary(input: Buffer, bitLength: number): BufferToDduBinaryResult;
11
- protected dduBinaryToBuffer(decodedBin: string, paddingBits: number): Buffer;
20
+ /**
21
+ * 입력 데이터를 인코딩합니다.
22
+ * @param input - 인코딩할 문자열 또는 Buffer
23
+ * @param options - 인코딩 옵션
24
+ * @returns 인코딩된 문자열
25
+ */
12
26
  abstract encode(input: Buffer | string, options?: DduOptions): string;
27
+ /**
28
+ * 인코딩된 문자열을 Buffer로 디코딩합니다.
29
+ * @param input - 인코딩된 문자열
30
+ * @param options - 디코딩 옵션
31
+ * @returns 디코딩된 Buffer
32
+ */
13
33
  abstract decodeToBuffer(input: string, options?: DduOptions): Buffer;
34
+ /**
35
+ * 인코딩된 문자열을 원본 문자열로 디코딩합니다.
36
+ * @param input - 인코딩된 문자열
37
+ * @param options - 디코딩 옵션
38
+ * @returns 디코딩된 문자열
39
+ */
14
40
  abstract decode(input: string, options?: DduOptions): string;
15
41
  /**
16
- * 테스트 디버깅용 추상 메서드
17
- * 구현 클래스의 내부 상태 정보를 반환
42
+ * 현재 인코더의 charset 정보를 반환합니다.
43
+ * @returns charset 설정 정보 객체
18
44
  */
19
45
  abstract getCharSetInfo(): {
20
46
  charSet: string[];
@@ -4,51 +4,26 @@ exports.BaseDdu = void 0;
4
4
  class BaseDdu {
5
5
  constructor() {
6
6
  this.defaultEncoding = "utf-8";
7
- this.binaryLookup = Array.from({ length: 256 }, (_, i) => i.toString(2).padStart(8, "0"));
8
- }
9
- escapeRegExp(str) {
10
- return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
11
- }
12
- *splitString(s, length) {
13
- for (let i = 0; i < s.length; i += length) {
14
- yield s.slice(i, Math.min(i + length, s.length));
15
- }
16
- }
17
- getLargestPowerOfTwo(n) {
18
- return 2 ** Math.floor(Math.log2(n));
19
7
  }
8
+ /**
9
+ * 주어진 숫자보다 작거나 같은 가장 큰 2의 제곱수의 지수를 반환합니다.
10
+ * @param n - 대상 숫자
11
+ * @returns 2의 제곱수 지수 (예: n=8 → 3, n=64 → 6)
12
+ * @example getLargestPowerOfTwoExponent(8) // 3
13
+ * @example getLargestPowerOfTwoExponent(100) // 6
14
+ */
20
15
  getLargestPowerOfTwoExponent(n) {
21
16
  return Math.floor(Math.log2(n));
22
17
  }
18
+ /**
19
+ * charset 크기에 필요한 비트 길이를 계산합니다.
20
+ * @param setLength - charset 문자 수
21
+ * @returns 필요한 비트 수
22
+ * @example getBitLength(64) // 6
23
+ * @example getBitLength(100) // 7
24
+ */
23
25
  getBitLength(setLength) {
24
26
  return Math.ceil(Math.log2(setLength));
25
27
  }
26
- bufferToDduBinary(input, bitLength) {
27
- // 성능 최적화: reduce 대신 for loop + join 사용
28
- if (input.length === 0) {
29
- return { dduBinary: [], padding: 0 };
30
- }
31
- const binaryParts = new Array(input.length);
32
- for (let i = 0; i < input.length; i++) {
33
- binaryParts[i] = this.binaryLookup[input[i]];
34
- }
35
- const encodedBin = binaryParts.join("");
36
- const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
37
- const padding = bitLength - dduBinary[dduBinary.length - 1].length;
38
- if (padding > 0) {
39
- dduBinary[dduBinary.length - 1] += "0".repeat(padding);
40
- }
41
- return { dduBinary, padding };
42
- }
43
- dduBinaryToBuffer(decodedBin, paddingBits) {
44
- if (paddingBits > 0) {
45
- decodedBin = decodedBin.slice(0, -paddingBits);
46
- }
47
- const buffer = [];
48
- for (let i = 0; i < decodedBin.length; i += 8) {
49
- buffer.push(parseInt(decodedBin.slice(i, i + 8), 2));
50
- }
51
- return Buffer.from(buffer);
52
- }
53
28
  }
54
29
  exports.BaseDdu = BaseDdu;
@@ -1,4 +1,18 @@
1
1
  import { CharSetConfig, DduSetSymbol } from "../types";
2
+ /**
3
+ * 심볼에 해당하는 charset 설정을 반환합니다.
4
+ * @param symbol - charset 심볼
5
+ * @returns CharSetConfig 또는 undefined
6
+ */
2
7
  export declare function getCharSet(symbol: DduSetSymbol): CharSetConfig | undefined;
8
+ /**
9
+ * 모든 사용 가능한 charset 심볼 목록을 반환합니다.
10
+ * @returns DduSetSymbol 배열
11
+ */
3
12
  export declare function getAllSymbols(): DduSetSymbol[];
13
+ /**
14
+ * 해당 심볼의 charset이 존재하는지 확인합니다.
15
+ * @param symbol - 확인할 charset 심볼
16
+ * @returns 존재 여부
17
+ */
4
18
  export declare function hasCharSet(symbol: DduSetSymbol): boolean;