11'use strict'
22
33const assert = require ( 'node:assert' )
4+ const { forgivingBase64, collectASequenceOfCodePoints, collectASequenceOfCodePointsFast, isomorphicDecode, removeASCIIWhitespace, removeChars } = require ( '../infra' )
45
56const encoder = new TextEncoder ( )
67
@@ -9,7 +10,6 @@ const encoder = new TextEncoder()
910 */
1011const HTTP_TOKEN_CODEPOINTS = / ^ [ ! # $ % & ' * + \- . ^ _ | ~ A - Z a - z 0 - 9 ] + $ /
1112const HTTP_WHITESPACE_REGEX = / [ \u000A \u000D \u0009 \u0020 ] / // eslint-disable-line
12- const ASCII_WHITESPACE_REPLACE_REGEX = / [ \u0009 \u000A \u000C \u000D \u0020 ] / g // eslint-disable-line
1313/**
1414 * @see https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
1515 */
@@ -136,49 +136,6 @@ function URLSerializer (url, excludeFragment = false) {
136136 return serialized
137137}
138138
139- // https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
140- /**
141- * @param {(char: string) => boolean } condition
142- * @param {string } input
143- * @param {{ position: number } } position
144- */
145- function collectASequenceOfCodePoints ( condition , input , position ) {
146- // 1. Let result be the empty string.
147- let result = ''
148-
149- // 2. While position doesn’t point past the end of input and the
150- // code point at position within input meets the condition condition:
151- while ( position . position < input . length && condition ( input [ position . position ] ) ) {
152- // 1. Append that code point to the end of result.
153- result += input [ position . position ]
154-
155- // 2. Advance position by 1.
156- position . position ++
157- }
158-
159- // 3. Return result.
160- return result
161- }
162-
163- /**
164- * A faster collectASequenceOfCodePoints that only works when comparing a single character.
165- * @param {string } char
166- * @param {string } input
167- * @param {{ position: number } } position
168- */
169- function collectASequenceOfCodePointsFast ( char , input , position ) {
170- const idx = input . indexOf ( char , position . position )
171- const start = position . position
172-
173- if ( idx === - 1 ) {
174- position . position = input . length
175- return input . slice ( start )
176- }
177-
178- position . position = idx
179- return input . slice ( start , position . position )
180- }
181-
182139// https://url.spec.whatwg.org/#string-percent-decode
183140/** @param {string } input */
184141function stringPercentDecode ( input ) {
@@ -427,45 +384,6 @@ function parseMIMEType (input) {
427384 return mimeType
428385}
429386
430- // https://infra.spec.whatwg.org/#forgiving-base64-decode
431- /** @param {string } data */
432- function forgivingBase64 ( data ) {
433- // 1. Remove all ASCII whitespace from data.
434- data = data . replace ( ASCII_WHITESPACE_REPLACE_REGEX , '' )
435-
436- let dataLength = data . length
437- // 2. If data’s code point length divides by 4 leaving
438- // no remainder, then:
439- if ( dataLength % 4 === 0 ) {
440- // 1. If data ends with one or two U+003D (=) code points,
441- // then remove them from data.
442- if ( data . charCodeAt ( dataLength - 1 ) === 0x003D ) {
443- -- dataLength
444- if ( data . charCodeAt ( dataLength - 1 ) === 0x003D ) {
445- -- dataLength
446- }
447- }
448- }
449-
450- // 3. If data’s code point length divides by 4 leaving
451- // a remainder of 1, then return failure.
452- if ( dataLength % 4 === 1 ) {
453- return 'failure'
454- }
455-
456- // 4. If data contains a code point that is not one of
457- // U+002B (+)
458- // U+002F (/)
459- // ASCII alphanumeric
460- // then return failure.
461- if ( / [ ^ + / 0 - 9 A - Z a - z ] / . test ( data . length === dataLength ? data : data . substring ( 0 , dataLength ) ) ) {
462- return 'failure'
463- }
464-
465- const buffer = Buffer . from ( data , 'base64' )
466- return new Uint8Array ( buffer . buffer , buffer . byteOffset , buffer . byteLength )
467- }
468-
469387// https://fetch.spec.whatwg.org/#collect-an-http-quoted-string
470388// tests: https://fetch.spec.whatwg.org/#example-http-quoted-string
471389/**
@@ -608,71 +526,6 @@ function removeHTTPWhitespace (str, leading = true, trailing = true) {
608526 return removeChars ( str , leading , trailing , isHTTPWhiteSpace )
609527}
610528
611- /**
612- * @see https://infra.spec.whatwg.org/#ascii-whitespace
613- * @param {number } char
614- */
615- function isASCIIWhitespace ( char ) {
616- // "\r\n\t\f "
617- return char === 0x00d || char === 0x00a || char === 0x009 || char === 0x00c || char === 0x020
618- }
619-
620- /**
621- * @see https://infra.spec.whatwg.org/#strip-leading-and-trailing-ascii-whitespace
622- * @param {string } str
623- * @param {boolean } [leading=true]
624- * @param {boolean } [trailing=true]
625- */
626- function removeASCIIWhitespace ( str , leading = true , trailing = true ) {
627- return removeChars ( str , leading , trailing , isASCIIWhitespace )
628- }
629-
630- /**
631- * @param {string } str
632- * @param {boolean } leading
633- * @param {boolean } trailing
634- * @param {(charCode: number) => boolean } predicate
635- * @returns
636- */
637- function removeChars ( str , leading , trailing , predicate ) {
638- let lead = 0
639- let trail = str . length - 1
640-
641- if ( leading ) {
642- while ( lead < str . length && predicate ( str . charCodeAt ( lead ) ) ) lead ++
643- }
644-
645- if ( trailing ) {
646- while ( trail > 0 && predicate ( str . charCodeAt ( trail ) ) ) trail --
647- }
648-
649- return lead === 0 && trail === str . length - 1 ? str : str . slice ( lead , trail + 1 )
650- }
651-
652- /**
653- * @see https://infra.spec.whatwg.org/#isomorphic-decode
654- * @param {Uint8Array } input
655- * @returns {string }
656- */
657- function isomorphicDecode ( input ) {
658- // 1. To isomorphic decode a byte sequence input, return a string whose code point
659- // length is equal to input’s length and whose code points have the same values
660- // as the values of input’s bytes, in the same order.
661- const length = input . length
662- if ( ( 2 << 15 ) - 1 > length ) {
663- return String . fromCharCode . apply ( null , input )
664- }
665- let result = '' ; let i = 0
666- let addition = ( 2 << 15 ) - 1
667- while ( i < length ) {
668- if ( i + addition > length ) {
669- addition = length - i
670- }
671- result += String . fromCharCode . apply ( null , input . subarray ( i , i += addition ) )
672- }
673- return result
674- }
675-
676529/**
677530 * @see https://mimesniff.spec.whatwg.org/#minimize-a-supported-mime-type
678531 * @param {Exclude<ReturnType<typeof parseMIMEType>, 'failure'> } mimeType
@@ -730,15 +583,11 @@ function minimizeSupportedMimeType (mimeType) {
730583module . exports = {
731584 dataURLProcessor,
732585 URLSerializer,
733- collectASequenceOfCodePoints,
734- collectASequenceOfCodePointsFast,
735586 stringPercentDecode,
736587 parseMIMEType,
737588 collectAnHTTPQuotedString,
738589 serializeAMimeType,
739- removeChars,
740590 removeHTTPWhitespace,
741591 minimizeSupportedMimeType,
742- HTTP_TOKEN_CODEPOINTS ,
743- isomorphicDecode
592+ HTTP_TOKEN_CODEPOINTS
744593}
0 commit comments