@kikiutils/shared 14.0.1 → 14.1.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/dist/buffer.d.ts CHANGED
@@ -4,34 +4,33 @@ import { Buffer } from "node:buffer";
4
4
  //#region src/buffer.d.ts
5
5
 
6
6
  /**
7
- * Converts a Blob, Buffer, or File to a Buffer.
7
+ * Converts various binary data types to a Node.js Buffer.
8
8
  *
9
- * This function provides a unified way to convert various binary data types
10
- * to Node.js Buffer. If the input is already a Buffer, it returns it as-is.
11
- * For Blob or File inputs, it converts them to Buffer via ArrayBuffer.
9
+ * This function provides a unified, efficient way to convert different binary formats
10
+ * (Blob, Buffer, File, ArrayBuffer, or Uint8Array) into a Node.js Buffer.
11
+ * It prioritizes zero-copy conversions for TypedArrays and ArrayBuffers to ensure
12
+ * optimal performance.
12
13
  *
13
- * @param {Blob | Buffer | File} input - The input to convert to Buffer
14
+ * @param {ArrayBuffer | Blob | Buffer | File | Uint8Array} input - The binary data input to convert.
15
+ * Supports Blob, Buffer, File, ArrayBuffer, and Uint8Array.
14
16
  *
15
- * @returns {Promise<Buffer>} A Promise that resolves to a Buffer
17
+ * @returns {Promise<Buffer>} A promise that resolves to a Node.js Buffer.
16
18
  *
17
19
  * @example
18
20
  * ```typescript
19
- * import { toBuffer } from '@kikiutils/shared/general';
21
+ * import { toBuffer } from '@kikiutils/shared/buffer';
20
22
  *
21
- * // Convert a Buffer (returns as-is)
22
- * const buffer = Buffer.from('Hello World');
23
- * const result1 = await toBuffer(buffer);
24
- * console.log(result1); // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
23
+ * // From ArrayBuffer
24
+ * const ab = new ArrayBuffer(8);
25
+ * const bufferFromAB = await toBuffer(ab);
25
26
  *
26
- * // Convert a Blob
27
- * const blob = new Blob(['Hello from Blob'], { type: 'text/plain' });
28
- * const result2 = await toBuffer(blob);
29
- * console.log(result2.toString()); // 'Hello from Blob'
27
+ * // From Uint8Array (Zero-copy)
28
+ * const u8 = new Uint8Array([10, 20, 30]);
29
+ * const bufferFromU8 = await toBuffer(u8);
30
30
  *
31
- * // Convert a File
32
- * const file = new File(['File content'], 'test.txt', { type: 'text/plain' });
33
- * const result3 = await toBuffer(file);
34
- * console.log(result3.toString()); // 'File content'
31
+ * // From Blob or File
32
+ * const blob = new Blob(['data'], { type: 'text/plain' });
33
+ * const bufferFromBlob = await toBuffer(blob);
35
34
  * ```
36
35
  */
37
36
  declare function toBuffer(input: BinaryInput): Promise<Buffer<ArrayBufferLike>>;
@@ -1 +1 @@
1
- {"version":3,"file":"buffer.d.ts","names":[],"sources":["../src/buffer.ts"],"sourcesContent":[],"mappings":";;;;;;;AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAsB,QAAA,QAAgB,cAAW,QAAA,OAAA"}
1
+ {"version":3,"file":"buffer.d.ts","names":[],"sources":["../src/buffer.ts"],"sourcesContent":[],"mappings":";;;;;;;AAkCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAsB,QAAA,QAAgB,cAAW,QAAA,OAAA"}
package/dist/buffer.js CHANGED
@@ -2,39 +2,41 @@ import { Buffer } from "node:buffer";
2
2
 
3
3
  //#region src/buffer.ts
4
4
  /**
5
- * Converts a Blob, Buffer, or File to a Buffer.
5
+ * Converts various binary data types to a Node.js Buffer.
6
6
  *
7
- * This function provides a unified way to convert various binary data types
8
- * to Node.js Buffer. If the input is already a Buffer, it returns it as-is.
9
- * For Blob or File inputs, it converts them to Buffer via ArrayBuffer.
7
+ * This function provides a unified, efficient way to convert different binary formats
8
+ * (Blob, Buffer, File, ArrayBuffer, or Uint8Array) into a Node.js Buffer.
9
+ * It prioritizes zero-copy conversions for TypedArrays and ArrayBuffers to ensure
10
+ * optimal performance.
10
11
  *
11
- * @param {Blob | Buffer | File} input - The input to convert to Buffer
12
+ * @param {ArrayBuffer | Blob | Buffer | File | Uint8Array} input - The binary data input to convert.
13
+ * Supports Blob, Buffer, File, ArrayBuffer, and Uint8Array.
12
14
  *
13
- * @returns {Promise<Buffer>} A Promise that resolves to a Buffer
15
+ * @returns {Promise<Buffer>} A promise that resolves to a Node.js Buffer.
14
16
  *
15
17
  * @example
16
18
  * ```typescript
17
- * import { toBuffer } from '@kikiutils/shared/general';
19
+ * import { toBuffer } from '@kikiutils/shared/buffer';
18
20
  *
19
- * // Convert a Buffer (returns as-is)
20
- * const buffer = Buffer.from('Hello World');
21
- * const result1 = await toBuffer(buffer);
22
- * console.log(result1); // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
21
+ * // From ArrayBuffer
22
+ * const ab = new ArrayBuffer(8);
23
+ * const bufferFromAB = await toBuffer(ab);
23
24
  *
24
- * // Convert a Blob
25
- * const blob = new Blob(['Hello from Blob'], { type: 'text/plain' });
26
- * const result2 = await toBuffer(blob);
27
- * console.log(result2.toString()); // 'Hello from Blob'
25
+ * // From Uint8Array (Zero-copy)
26
+ * const u8 = new Uint8Array([10, 20, 30]);
27
+ * const bufferFromU8 = await toBuffer(u8);
28
28
  *
29
- * // Convert a File
30
- * const file = new File(['File content'], 'test.txt', { type: 'text/plain' });
31
- * const result3 = await toBuffer(file);
32
- * console.log(result3.toString()); // 'File content'
29
+ * // From Blob or File
30
+ * const blob = new Blob(['data'], { type: 'text/plain' });
31
+ * const bufferFromBlob = await toBuffer(blob);
33
32
  * ```
34
33
  */
35
34
  async function toBuffer(input) {
36
35
  if (Buffer.isBuffer(input)) return input;
37
- return Buffer.from(await input.arrayBuffer());
36
+ if (input instanceof ArrayBuffer) return Buffer.from(input);
37
+ if (input instanceof Uint8Array) return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
38
+ if (typeof input.arrayBuffer === "function") return Buffer.from(await input.arrayBuffer());
39
+ throw new TypeError("The provided input is not a supported binary type (Blob, Buffer, File, ArrayBuffer, or Uint8Array).");
38
40
  }
39
41
 
40
42
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"buffer.js","names":[],"sources":["../src/buffer.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\n\nimport type { BinaryInput } from './types';\n\n/**\n * Converts a Blob, Buffer, or File to a Buffer.\n *\n * This function provides a unified way to convert various binary data types\n * to Node.js Buffer. If the input is already a Buffer, it returns it as-is.\n * For Blob or File inputs, it converts them to Buffer via ArrayBuffer.\n *\n * @param {Blob | Buffer | File} input - The input to convert to Buffer\n *\n * @returns {Promise<Buffer>} A Promise that resolves to a Buffer\n *\n * @example\n * ```typescript\n * import { toBuffer } from '@kikiutils/shared/general';\n *\n * // Convert a Buffer (returns as-is)\n * const buffer = Buffer.from('Hello World');\n * const result1 = await toBuffer(buffer);\n * console.log(result1); // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>\n *\n * // Convert a Blob\n * const blob = new Blob(['Hello from Blob'], { type: 'text/plain' });\n * const result2 = await toBuffer(blob);\n * console.log(result2.toString()); // 'Hello from Blob'\n *\n * // Convert a File\n * const file = new File(['File content'], 'test.txt', { type: 'text/plain' });\n * const result3 = await toBuffer(file);\n * console.log(result3.toString()); // 'File content'\n * ```\n */\nexport async function toBuffer(input: BinaryInput) {\n if (Buffer.isBuffer(input)) return input;\n return Buffer.from(await input.arrayBuffer());\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,eAAsB,SAAS,OAAoB;AAC/C,KAAI,OAAO,SAAS,MAAM,CAAE,QAAO;AACnC,QAAO,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"buffer.js","names":[],"sources":["../src/buffer.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\n\nimport type { BinaryInput } from './types';\n\n/**\n * Converts various binary data types to a Node.js Buffer.\n *\n * This function provides a unified, efficient way to convert different binary formats\n * (Blob, Buffer, File, ArrayBuffer, or Uint8Array) into a Node.js Buffer.\n * It prioritizes zero-copy conversions for TypedArrays and ArrayBuffers to ensure\n * optimal performance.\n *\n * @param {ArrayBuffer | Blob | Buffer | File | Uint8Array} input - The binary data input to convert.\n * Supports Blob, Buffer, File, ArrayBuffer, and Uint8Array.\n *\n * @returns {Promise<Buffer>} A promise that resolves to a Node.js Buffer.\n *\n * @example\n * ```typescript\n * import { toBuffer } from '@kikiutils/shared/buffer';\n *\n * // From ArrayBuffer\n * const ab = new ArrayBuffer(8);\n * const bufferFromAB = await toBuffer(ab);\n *\n * // From Uint8Array (Zero-copy)\n * const u8 = new Uint8Array([10, 20, 30]);\n * const bufferFromU8 = await toBuffer(u8);\n *\n * // From Blob or File\n * const blob = new Blob(['data'], { type: 'text/plain' });\n * const bufferFromBlob = await toBuffer(blob);\n * ```\n */\nexport async function toBuffer(input: BinaryInput) {\n if (Buffer.isBuffer(input)) return input;\n if (input instanceof ArrayBuffer) return Buffer.from(input);\n if (input instanceof Uint8Array) return Buffer.from(input.buffer, input.byteOffset, input.byteLength);\n if (typeof input.arrayBuffer === 'function') return Buffer.from(await input.arrayBuffer());\n // eslint-disable-next-line style/max-len\n throw new TypeError('The provided input is not a supported binary type (Blob, Buffer, File, ArrayBuffer, or Uint8Array).');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,eAAsB,SAAS,OAAoB;AAC/C,KAAI,OAAO,SAAS,MAAM,CAAE,QAAO;AACnC,KAAI,iBAAiB,YAAa,QAAO,OAAO,KAAK,MAAM;AAC3D,KAAI,iBAAiB,WAAY,QAAO,OAAO,KAAK,MAAM,QAAQ,MAAM,YAAY,MAAM,WAAW;AACrG,KAAI,OAAO,MAAM,gBAAgB,WAAY,QAAO,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAE1F,OAAM,IAAI,UAAU,sGAAsG"}
@@ -3,7 +3,7 @@ import { Blob as Blob$1, Buffer, File as File$1 } from "node:buffer";
3
3
 
4
4
  //#region src/types/index.d.ts
5
5
  type AnyRecord = Record<string, any>;
6
- type BinaryInput = Blob | Buffer | File | Blob$1 | File$1;
6
+ type BinaryInput = ArrayBuffer | Blob | Buffer | File | Blob$1 | File$1 | Uint8Array;
7
7
  type Booleanish = 'false' | 'true' | boolean;
8
8
  type MaybePartial<T> = Partial<T> | T;
9
9
  type MaybeReadonly<T> = Readonly<T> | T;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types/index.ts"],"sourcesContent":[],"mappings":";;;;KAQY,SAAA,GAAY;KACZ,WAAA,GAAc,OAAO,SAAS,OAAO,SAAW;AADhD,KAEA,UAAA,GAFS,OAAG,GAAM,MAAA,GAAA,OAAA;AAClB,KAEA,YAFW,CAAA,CAAA,CAAA,GAEO,OAFP,CAEe,CAFf,CAAA,GAEoB,CAFpB;AAAG,KAGd,aAHc,CAAA,CAAA,CAAA,GAGK,QAHL,CAGc,CAHd,CAAA,GAGmB,CAHnB;AAAO,KAIrB,QAJqB,CAAA,CAAA,CAAA,GAAA,IAAA,GAIA,CAJA;AAAS,KAK9B,SAAA,GAL8B,MAAA,GAAA,MAAA;AAAO,KAMrC,aANqC,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAMG,OANH,CAMW,MANX,CAMkB,CANlB,EAMqB,CANrB,CAAA,CAAA;AAAW,KAOhD,qBAPgD,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAOA,QAPA,CAOS,aAPT,CAOuB,CAPvB,EAO0B,CAP1B,CAAA,CAAA;AAAQ,KAQxD,cARwD,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAQf,QARe,CAQN,MARM,CAQC,CARD,EAQI,CARJ,CAAA,CAAA"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types/index.ts"],"sourcesContent":[],"mappings":";;;;KAQY,SAAA,GAAY;KACZ,WAAA,GAAc,cAAc,OAAO,SAAS,OAAO,SAAW,SAAW;AADzE,KAEA,UAAA,GAFS,OAAG,GAAM,MAAA,GAAA,OAAA;AAClB,KAEA,YAFW,CAAA,CAAA,CAAA,GAEO,OAFP,CAEe,CAFf,CAAA,GAEoB,CAFpB;AAAG,KAGd,aAHc,CAAA,CAAA,CAAA,GAGK,QAHL,CAGc,CAHd,CAAA,GAGmB,CAHnB;AAAc,KAI5B,QAJ4B,CAAA,CAAA,CAAA,GAAA,IAAA,GAIP,CAJO;AAAO,KAKnC,SAAA,GALmC,MAAA,GAAA,MAAA;AAAS,KAM5C,aAN4C,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAMJ,OANI,CAMI,MANJ,CAMW,CANX,EAMc,CANd,CAAA,CAAA;AAAO,KAOnD,qBAPmD,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAOH,QAPG,CAOM,aAPN,CAOoB,CAPpB,EAOuB,CAPvB,CAAA,CAAA;AAAW,KAQ9D,cAR8D,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAQrB,QARqB,CAQZ,MARY,CAQL,CARK,EAQF,CARE,CAAA,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kikiutils/shared",
3
3
  "type": "module",
4
- "version": "14.0.1",
4
+ "version": "14.1.0",
5
5
  "description": "A lightweight and modular utility library for modern JavaScript and TypeScript — includes secure hashing, flexible logging, datetime tools, Vue/web helpers, storage abstraction, and more.",
6
6
  "author": "kiki-kanri",
7
7
  "license": "MIT",
@@ -170,7 +170,7 @@
170
170
  "depcheck": "^1.4.7",
171
171
  "element-plus": "^2.13.0",
172
172
  "fs-extra": "^11.3.3",
173
- "jsdom": "^27.3.0",
173
+ "jsdom": "^27.4.0",
174
174
  "lru-cache": "^11.2.4",
175
175
  "millify": "^6.1.0",
176
176
  "msgpackr": "^1.11.8",
@@ -179,7 +179,7 @@
179
179
  "pino-pretty": "^13.1.3",
180
180
  "publint": "^0.3.16",
181
181
  "ts-unused-exports": "^11.0.1",
182
- "tsdown": "^0.18.3",
182
+ "tsdown": "^0.18.4",
183
183
  "typescript": "^5.9.3",
184
184
  "unplugin-unused": "^0.5.6",
185
185
  "vitest": "^4.0.16",
package/src/buffer.ts CHANGED
@@ -3,37 +3,40 @@ import { Buffer } from 'node:buffer';
3
3
  import type { BinaryInput } from './types';
4
4
 
5
5
  /**
6
- * Converts a Blob, Buffer, or File to a Buffer.
6
+ * Converts various binary data types to a Node.js Buffer.
7
7
  *
8
- * This function provides a unified way to convert various binary data types
9
- * to Node.js Buffer. If the input is already a Buffer, it returns it as-is.
10
- * For Blob or File inputs, it converts them to Buffer via ArrayBuffer.
8
+ * This function provides a unified, efficient way to convert different binary formats
9
+ * (Blob, Buffer, File, ArrayBuffer, or Uint8Array) into a Node.js Buffer.
10
+ * It prioritizes zero-copy conversions for TypedArrays and ArrayBuffers to ensure
11
+ * optimal performance.
11
12
  *
12
- * @param {Blob | Buffer | File} input - The input to convert to Buffer
13
+ * @param {ArrayBuffer | Blob | Buffer | File | Uint8Array} input - The binary data input to convert.
14
+ * Supports Blob, Buffer, File, ArrayBuffer, and Uint8Array.
13
15
  *
14
- * @returns {Promise<Buffer>} A Promise that resolves to a Buffer
16
+ * @returns {Promise<Buffer>} A promise that resolves to a Node.js Buffer.
15
17
  *
16
18
  * @example
17
19
  * ```typescript
18
- * import { toBuffer } from '@kikiutils/shared/general';
20
+ * import { toBuffer } from '@kikiutils/shared/buffer';
19
21
  *
20
- * // Convert a Buffer (returns as-is)
21
- * const buffer = Buffer.from('Hello World');
22
- * const result1 = await toBuffer(buffer);
23
- * console.log(result1); // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
22
+ * // From ArrayBuffer
23
+ * const ab = new ArrayBuffer(8);
24
+ * const bufferFromAB = await toBuffer(ab);
24
25
  *
25
- * // Convert a Blob
26
- * const blob = new Blob(['Hello from Blob'], { type: 'text/plain' });
27
- * const result2 = await toBuffer(blob);
28
- * console.log(result2.toString()); // 'Hello from Blob'
26
+ * // From Uint8Array (Zero-copy)
27
+ * const u8 = new Uint8Array([10, 20, 30]);
28
+ * const bufferFromU8 = await toBuffer(u8);
29
29
  *
30
- * // Convert a File
31
- * const file = new File(['File content'], 'test.txt', { type: 'text/plain' });
32
- * const result3 = await toBuffer(file);
33
- * console.log(result3.toString()); // 'File content'
30
+ * // From Blob or File
31
+ * const blob = new Blob(['data'], { type: 'text/plain' });
32
+ * const bufferFromBlob = await toBuffer(blob);
34
33
  * ```
35
34
  */
36
35
  export async function toBuffer(input: BinaryInput) {
37
36
  if (Buffer.isBuffer(input)) return input;
38
- return Buffer.from(await input.arrayBuffer());
37
+ if (input instanceof ArrayBuffer) return Buffer.from(input);
38
+ if (input instanceof Uint8Array) return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
39
+ if (typeof input.arrayBuffer === 'function') return Buffer.from(await input.arrayBuffer());
40
+ // eslint-disable-next-line style/max-len
41
+ throw new TypeError('The provided input is not a supported binary type (Blob, Buffer, File, ArrayBuffer, or Uint8Array).');
39
42
  }
@@ -7,7 +7,7 @@ import type {
7
7
  export type { FilteredKeyPath } from './filtered-key-path';
8
8
 
9
9
  export type AnyRecord = Record<string, any>;
10
- export type BinaryInput = Blob | Buffer | File | NodeBlob | NodeFile;
10
+ export type BinaryInput = ArrayBuffer | Blob | Buffer | File | NodeBlob | NodeFile | Uint8Array;
11
11
  export type Booleanish = 'false' | 'true' | boolean;
12
12
  export type MaybePartial<T> = Partial<T> | T;
13
13
  export type MaybeReadonly<T> = Readonly<T> | T;