@@ -377,144 +377,6 @@ function getPath() {
377377 return _path as typeof import ( 'path' )
378378}
379379
380- /**
381- * Move the "slow cases" to a separate function to make sure this function gets
382- * inlined properly. That prioritizes the common case.
383- *
384- * Based on Node.js internal/util.js normalizeEncoding implementation.
385- * @see https://github.com/nodejs/node/blob/ae62b36d442b7bf987e85ae6e0df0f02cc1bb17f/lib/internal/util.js#L247-L310
386- *
387- * @param enc - Encoding to normalize
388- * @returns Normalized encoding string or undefined if no match
389- * @private
390- */
391- /*@__NO_SIDE_EFFECTS__ */
392- function slowCases ( enc : string ) : BufferEncoding | undefined {
393- switch ( enc . length ) {
394- case 4 :
395- if ( enc === 'UTF8' ) {
396- return 'utf8'
397- }
398- if ( enc === 'ucs2' || enc === 'UCS2' ) {
399- return 'utf16le'
400- }
401- enc = enc . toLowerCase ( )
402- if ( enc === 'utf8' ) {
403- return 'utf8'
404- }
405- if ( enc === 'ucs2' ) {
406- return 'utf16le'
407- }
408- break
409- case 3 :
410- if ( enc === 'hex' || enc === 'HEX' || enc . toLowerCase ( ) === 'hex' ) {
411- return 'hex'
412- }
413- break
414- case 5 :
415- if ( enc === 'ascii' ) {
416- return 'ascii'
417- }
418- if ( enc === 'ucs-2' ) {
419- return 'utf16le'
420- }
421- if ( enc === 'UTF-8' ) {
422- return 'utf8'
423- }
424- if ( enc === 'ASCII' ) {
425- return 'ascii'
426- }
427- if ( enc === 'UCS-2' ) {
428- return 'utf16le'
429- }
430- enc = enc . toLowerCase ( )
431- if ( enc === 'utf-8' ) {
432- return 'utf8'
433- }
434- if ( enc === 'ascii' ) {
435- return 'ascii'
436- }
437- if ( enc === 'ucs-2' ) {
438- return 'utf16le'
439- }
440- break
441- case 6 :
442- if ( enc === 'base64' ) {
443- return 'base64'
444- }
445- if ( enc === 'latin1' || enc === 'binary' ) {
446- return 'latin1'
447- }
448- if ( enc === 'BASE64' ) {
449- return 'base64'
450- }
451- if ( enc === 'LATIN1' || enc === 'BINARY' ) {
452- return 'latin1'
453- }
454- enc = enc . toLowerCase ( )
455- if ( enc === 'base64' ) {
456- return 'base64'
457- }
458- if ( enc === 'latin1' || enc === 'binary' ) {
459- return 'latin1'
460- }
461- break
462- case 7 :
463- if (
464- enc === 'utf16le' ||
465- enc === 'UTF16LE' ||
466- enc . toLowerCase ( ) === 'utf16le'
467- ) {
468- return 'utf16le'
469- }
470- break
471- case 8 :
472- if (
473- enc === 'utf-16le' ||
474- enc === 'UTF-16LE' ||
475- enc . toLowerCase ( ) === 'utf-16le'
476- ) {
477- return 'utf16le'
478- }
479- break
480- case 9 :
481- if (
482- enc === 'base64url' ||
483- enc === 'BASE64URL' ||
484- enc . toLowerCase ( ) === 'base64url'
485- ) {
486- return 'base64url'
487- }
488- break
489- default :
490- if ( enc === '' ) {
491- return 'utf8'
492- }
493- }
494- return undefined
495- }
496-
497- /**
498- * Normalize encoding string to canonical form.
499- * Handles common encodings inline for performance, delegates to slowCases for others.
500- *
501- * Based on Node.js internal/util.js normalizeEncoding implementation.
502- * @see https://github.com/nodejs/node/blob/ae62b36d442b7bf987e85ae6e0df0f02cc1bb17f/lib/internal/util.js#L247-L310
503- *
504- * @param enc - Encoding to normalize (can be null/undefined)
505- * @returns Normalized encoding string, defaults to 'utf8'
506- * @private
507- */
508- /*@__NO_SIDE_EFFECTS__ */
509- function normalizeEncoding (
510- enc : BufferEncoding | string | null | undefined ,
511- ) : BufferEncoding {
512- if ( enc == null || enc === 'utf8' || enc === 'utf-8' ) {
513- return 'utf8'
514- }
515- return slowCases ( enc ) ?? 'utf8'
516- }
517-
518380/**
519381 * Process directory entries and filter for directories.
520382 * Filters entries to include only directories, optionally excluding empty ones.
@@ -855,6 +717,127 @@ export function isSymLinkSync(filepath: PathLike) {
855717 return false
856718}
857719
720+ /**
721+ * Normalize encoding string to canonical form.
722+ * Handles common encodings inline for performance, delegates to slowCases for others.
723+ *
724+ * Based on Node.js internal/util.js normalizeEncoding implementation.
725+ * @see https://github.com/nodejs/node/blob/ae62b36d442b7bf987e85ae6e0df0f02cc1bb17f/lib/internal/util.js#L247-L310
726+ *
727+ * @param enc - Encoding to normalize (can be null/undefined)
728+ * @returns Normalized encoding string, defaults to 'utf8'
729+ *
730+ * @example
731+ * ```ts
732+ * normalizeEncoding('UTF-8') // Returns 'utf8'
733+ * normalizeEncoding('binary') // Returns 'latin1'
734+ * normalizeEncoding('ucs-2') // Returns 'utf16le'
735+ * normalizeEncoding(null) // Returns 'utf8'
736+ * ```
737+ */
738+ /*@__NO_SIDE_EFFECTS__ */
739+ export function normalizeEncoding (
740+ enc : BufferEncoding | string | null | undefined ,
741+ ) : BufferEncoding {
742+ return enc == null || enc === 'utf8' || enc === 'utf-8'
743+ ? 'utf8'
744+ : normalizeEncodingSlow ( enc )
745+ }
746+
747+ /**
748+ * Move the "slow cases" to a separate function to make sure this function gets
749+ * inlined properly. That prioritizes the common case.
750+ *
751+ * Based on Node.js internal/util.js normalizeEncoding implementation.
752+ * @see https://github.com/nodejs/node/blob/ae62b36d442b7bf987e85ae6e0df0f02cc1bb17f/lib/internal/util.js#L247-L310
753+ *
754+ * @param enc - Encoding to normalize
755+ * @returns Normalized encoding string, defaults to 'utf8' for unknown encodings
756+ */
757+ /*@__NO_SIDE_EFFECTS__ */
758+ export function normalizeEncodingSlow ( enc : string ) : BufferEncoding {
759+ const { length } = enc
760+ if ( length === 4 ) {
761+ if ( enc === 'ucs2' || enc === 'UCS2' ) {
762+ return 'utf16le'
763+ }
764+ if ( enc . toLowerCase ( ) === 'ucs2' ) {
765+ return 'utf16le'
766+ }
767+ } else if (
768+ ( length === 3 && enc === 'hex' ) ||
769+ enc === 'HEX' ||
770+ enc . toLowerCase ( ) === 'hex'
771+ ) {
772+ return 'hex'
773+ } else if ( length === 5 ) {
774+ if ( enc === 'ascii' ) {
775+ return 'ascii'
776+ }
777+ if ( enc === 'ucs-2' ) {
778+ return 'utf16le'
779+ }
780+ if ( enc === 'ASCII' ) {
781+ return 'ascii'
782+ }
783+ if ( enc === 'UCS-2' ) {
784+ return 'utf16le'
785+ }
786+ enc = enc . toLowerCase ( )
787+ if ( enc === 'ascii' ) {
788+ return 'ascii'
789+ }
790+ if ( enc === 'ucs-2' ) {
791+ return 'utf16le'
792+ }
793+ } else if ( length === 6 ) {
794+ if ( enc === 'base64' ) {
795+ return 'base64'
796+ }
797+ if ( enc === 'latin1' || enc === 'binary' ) {
798+ return 'latin1'
799+ }
800+ if ( enc === 'BASE64' ) {
801+ return 'base64'
802+ }
803+ if ( enc === 'LATIN1' || enc === 'BINARY' ) {
804+ return 'latin1'
805+ }
806+ enc = enc . toLowerCase ( )
807+ if ( enc === 'base64' ) {
808+ return 'base64'
809+ }
810+ if ( enc === 'latin1' || enc === 'binary' ) {
811+ return 'latin1'
812+ }
813+ } else if ( length === 7 ) {
814+ if (
815+ enc === 'utf16le' ||
816+ enc === 'UTF16LE' ||
817+ enc . toLowerCase ( ) === 'utf16le'
818+ ) {
819+ return 'utf16le'
820+ }
821+ } else if ( length === 8 ) {
822+ if (
823+ enc === 'utf-16le' ||
824+ enc === 'UTF-16LE' ||
825+ enc . toLowerCase ( ) === 'utf-16le'
826+ ) {
827+ return 'utf16le'
828+ }
829+ } else if ( length === 9 ) {
830+ if (
831+ enc === 'base64url' ||
832+ enc === 'BASE64URL' ||
833+ enc . toLowerCase ( ) === 'base64url'
834+ ) {
835+ return 'base64url'
836+ }
837+ }
838+ return 'utf8'
839+ }
840+
858841/**
859842 * Read directory names asynchronously with filtering and sorting.
860843 * Returns only directory names (not files), with optional filtering for empty directories
0 commit comments