GeezerVison is showing github.com — lib: move encodeStr function to internal for reusable · nodejs/node@daeb348
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/querystring.js +67Lines changed: 67 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| | 3 | +const { ERR_INVALID_URI } = require('internal/errors').codes; | | 4 | + | 3 | 5 | const hexTable = new Array(256); | 4 | 6 | for (var i = 0; i < 256; ++i) | 5 | 7 | hexTable[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase(); | @@ -23,7 +25,72 @@ const isHexTable = [ | 23 | 25 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 256 | 24 | 26 | ]; | 25 | 27 |
| | 28 | +function encodeStr(str, noEscapeTable, hexTable) { | | 29 | + const len = str.length; | | 30 | + if (len === 0) | | 31 | + return ''; | | 32 | + | | 33 | + var out = ''; | | 34 | + var lastPos = 0; | | 35 | + | | 36 | + for (var i = 0; i < len; i++) { | | 37 | + var c = str.charCodeAt(i); | | 38 | + | | 39 | + // ASCII | | 40 | + if (c < 0x80) { | | 41 | + if (noEscapeTable[c] === 1) | | 42 | + continue; | | 43 | + if (lastPos < i) | | 44 | + out += str.slice(lastPos, i); | | 45 | + lastPos = i + 1; | | 46 | + out += hexTable[c]; | | 47 | + continue; | | 48 | + } | | 49 | + | | 50 | + if (lastPos < i) | | 51 | + out += str.slice(lastPos, i); | | 52 | + | | 53 | + // Multi-byte characters ... | | 54 | + if (c < 0x800) { | | 55 | + lastPos = i + 1; | | 56 | + out += hexTable[0xC0 | (c >> 6)] + | | 57 | + hexTable[0x80 | (c & 0x3F)]; | | 58 | + continue; | | 59 | + } | | 60 | + if (c < 0xD800 || c >= 0xE000) { | | 61 | + lastPos = i + 1; | | 62 | + out += hexTable[0xE0 | (c >> 12)] + | | 63 | + hexTable[0x80 | ((c >> 6) & 0x3F)] + | | 64 | + hexTable[0x80 | (c & 0x3F)]; | | 65 | + continue; | | 66 | + } | | 67 | + // Surrogate pair | | 68 | + ++i; | | 69 | + | | 70 | + // This branch should never happen because all URLSearchParams entries | | 71 | + // should already be converted to USVString. But, included for | | 72 | + // completion's sake anyway. | | 73 | + if (i >= len) | | 74 | + throw new ERR_INVALID_URI(); | | 75 | + | | 76 | + var c2 = str.charCodeAt(i) & 0x3FF; | | 77 | + | | 78 | + lastPos = i + 1; | | 79 | + c = 0x10000 + (((c & 0x3FF) << 10) | c2); | | 80 | + out += hexTable[0xF0 | (c >> 18)] + | | 81 | + hexTable[0x80 | ((c >> 12) & 0x3F)] + | | 82 | + hexTable[0x80 | ((c >> 6) & 0x3F)] + | | 83 | + hexTable[0x80 | (c & 0x3F)]; | | 84 | + } | | 85 | + if (lastPos === 0) | | 86 | + return str; | | 87 | + if (lastPos < len) | | 88 | + return out + str.slice(lastPos); | | 89 | + return out; | | 90 | +} | | 91 | + | 26 | 92 | module.exports = { | | 93 | + encodeStr, | 27 | 94 | hexTable, | 28 | 95 | isHexTable | 29 | 96 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +1-64Lines changed: 1 addition & 64 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const util = require('util'); | 4 | 4 | const { | | 5 | + encodeStr, | 5 | 6 | hexTable, | 6 | 7 | isHexTable | 7 | 8 | } = require('internal/querystring'); | @@ -833,70 +834,6 @@ const noEscape = [ | 833 | 834 | const paramHexTable = hexTable.slice(); | 834 | 835 | paramHexTable[0x20] = '+'; | 835 | 836 |
| 836 | | -function encodeStr(str, noEscapeTable, hexTable) { | 837 | | - const len = str.length; | 838 | | - if (len === 0) | 839 | | - return ''; | 840 | | - | 841 | | - var out = ''; | 842 | | - var lastPos = 0; | 843 | | - | 844 | | - for (var i = 0; i < len; i++) { | 845 | | - var c = str.charCodeAt(i); | 846 | | - | 847 | | - // ASCII | 848 | | - if (c < 0x80) { | 849 | | - if (noEscapeTable[c] === 1) | 850 | | - continue; | 851 | | - if (lastPos < i) | 852 | | - out += str.slice(lastPos, i); | 853 | | - lastPos = i + 1; | 854 | | - out += hexTable[c]; | 855 | | - continue; | 856 | | - } | 857 | | - | 858 | | - if (lastPos < i) | 859 | | - out += str.slice(lastPos, i); | 860 | | - | 861 | | - // Multi-byte characters ... | 862 | | - if (c < 0x800) { | 863 | | - lastPos = i + 1; | 864 | | - out += hexTable[0xC0 | (c >> 6)] + | 865 | | - hexTable[0x80 | (c & 0x3F)]; | 866 | | - continue; | 867 | | - } | 868 | | - if (c < 0xD800 || c >= 0xE000) { | 869 | | - lastPos = i + 1; | 870 | | - out += hexTable[0xE0 | (c >> 12)] + | 871 | | - hexTable[0x80 | ((c >> 6) & 0x3F)] + | 872 | | - hexTable[0x80 | (c & 0x3F)]; | 873 | | - continue; | 874 | | - } | 875 | | - // Surrogate pair | 876 | | - ++i; | 877 | | - var c2; | 878 | | - if (i < len) | 879 | | - c2 = str.charCodeAt(i) & 0x3FF; | 880 | | - else { | 881 | | - // This branch should never happen because all URLSearchParams entries | 882 | | - // should already be converted to USVString. But, included for | 883 | | - // completion's sake anyway. | 884 | | - c2 = 0; | 885 | | - } | 886 | | - lastPos = i + 1; | 887 | | - c = 0x10000 + (((c & 0x3FF) << 10) | c2); | 888 | | - out += hexTable[0xF0 | (c >> 18)] + | 889 | | - hexTable[0x80 | ((c >> 12) & 0x3F)] + | 890 | | - hexTable[0x80 | ((c >> 6) & 0x3F)] + | 891 | | - hexTable[0x80 | (c & 0x3F)]; | 892 | | - } | 893 | | - if (lastPos === 0) | 894 | | - return str; | 895 | | - if (lastPos < len) | 896 | | - return out + str.slice(lastPos); | 897 | | - return out; | 898 | | -} | 899 | | - | 900 | 837 | // application/x-www-form-urlencoded serializer | 901 | 838 | // Ref: https://url.spec.whatwg.org/#concept-urlencoded-serializer | 902 | 839 | function serializeParams(array) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/querystring.js +2-53Lines changed: 2 additions & 53 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | 'use strict'; | 25 | 25 |
| 26 | 26 | const { Buffer } = require('buffer'); | 27 | | -const { ERR_INVALID_URI } = require('internal/errors').codes; | 28 | 27 | const { | | 28 | + encodeStr, | 29 | 29 | hexTable, | 30 | 30 | isHexTable | 31 | 31 | } = require('internal/querystring'); | @@ -140,59 +140,8 @@ function qsEscape(str) { | 140 | 140 | else | 141 | 141 | str += ''; | 142 | 142 | } | 143 | | - var out = ''; | 144 | | - var lastPos = 0; | 145 | | - | 146 | | - for (var i = 0; i < str.length; ++i) { | 147 | | - var c = str.charCodeAt(i); | 148 | | - | 149 | | - // ASCII | 150 | | - if (c < 0x80) { | 151 | | - if (noEscape[c] === 1) | 152 | | - continue; | 153 | | - if (lastPos < i) | 154 | | - out += str.slice(lastPos, i); | 155 | | - lastPos = i + 1; | 156 | | - out += hexTable[c]; | 157 | | - continue; | 158 | | - } | 159 | 143 |
| 160 | | - if (lastPos < i) | 161 | | - out += str.slice(lastPos, i); | 162 | | - | 163 | | - // Multi-byte characters ... | 164 | | - if (c < 0x800) { | 165 | | - lastPos = i + 1; | 166 | | - out += hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]; | 167 | | - continue; | 168 | | - } | 169 | | - if (c < 0xD800 || c >= 0xE000) { | 170 | | - lastPos = i + 1; | 171 | | - out += hexTable[0xE0 | (c >> 12)] + | 172 | | - hexTable[0x80 | ((c >> 6) & 0x3F)] + | 173 | | - hexTable[0x80 | (c & 0x3F)]; | 174 | | - continue; | 175 | | - } | 176 | | - // Surrogate pair | 177 | | - ++i; | 178 | | - | 179 | | - if (i >= str.length) | 180 | | - throw new ERR_INVALID_URI(); | 181 | | - | 182 | | - var c2 = str.charCodeAt(i) & 0x3FF; | 183 | | - | 184 | | - lastPos = i + 1; | 185 | | - c = 0x10000 + (((c & 0x3FF) << 10) | c2); | 186 | | - out += hexTable[0xF0 | (c >> 18)] + | 187 | | - hexTable[0x80 | ((c >> 12) & 0x3F)] + | 188 | | - hexTable[0x80 | ((c >> 6) & 0x3F)] + | 189 | | - hexTable[0x80 | (c & 0x3F)]; | 190 | | - } | 191 | | - if (lastPos === 0) | 192 | | - return str; | 193 | | - if (lastPos < str.length) | 194 | | - return out + str.slice(lastPos); | 195 | | - return out; | | 144 | + return encodeStr(str, noEscape, hexTable); | 196 | 145 | } | 197 | 146 |
| 198 | 147 | function stringifyPrimitive(v) { | |
|