publicsuffixlist.js (21051B) - View raw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637/******************************************************************************* publicsuffixlist.js - an efficient javascript implementation to deal with Mozilla Foundation's Public Suffix List <http://publicsuffix.org/list/> Copyright (C) 2013-present Raymond Hill License: pick the one which suits you: GPL v3 see <https://www.gnu.org/licenses/gpl.html> APL v2 see <http://www.apache.org/licenses/LICENSE-2.0> */ /*! Home: https://github.com/gorhill/publicsuffixlist.js -- GPLv3 APLv2 */ 'use strict'; /******************************************************************************* Reference: https://publicsuffix.org/list/ Excerpt: > Algorithm > > 1. Match domain against all rules and take note of the matching ones. > 2. If no rules match, the prevailing rule is "*". > 3. If more than one rule matches, the prevailing rule is the one which is an exception rule. > 4. If there is no matching exception rule, the prevailing rule is the one with the most labels. > 5. If the prevailing rule is a exception rule, modify it by removing the leftmost label. > 6. The public suffix is the set of labels from the domain which match the labels of the prevailing rule, using the matching algorithm above. > 7. The registered or registrable domain is the public suffix plus one additional label. */ /******************************************************************************/ export default (function() { // >>>>>>>> start of anonymous namespace /******************************************************************************* Tree encoding in array buffer: Node: + u8: length of char data + u8: flags => bit 0: is_publicsuffix, bit 1: is_exception + u16: length of array of children + u32: char data or offset to char data + u32: offset to array of children = 12 bytes More bits in flags could be used; for example: - to distinguish private suffixes */ // i32 / i8 const HOSTNAME_SLOT = 0; // jshint ignore:line const LABEL_INDICES_SLOT = 256; // -- / 256 (256/2 => 128 labels max) const RULES_PTR_SLOT = 100; // 100 / 400 (400-256=144 => 144>128) const SUFFIX_NOT_FOUND_SLOT = 399; // -- / 399 (safe, see above) const CHARDATA_PTR_SLOT = 101; // 101 / 404 const EMPTY_STRING = ''; const SELFIE_MAGIC = 3; let wasmMemory; let pslBuffer32; let pslBuffer8; let pslByteLength = 0; let hostnameArg = EMPTY_STRING; /******************************************************************************/ const fireChangedEvent = function() { if ( typeof window !== 'object' ) { return; } if ( window instanceof Object === false ) { return; } if ( window.dispatchEvent instanceof Function === false ) { return; } if ( window.CustomEvent instanceof Function === false ) { return; } window.dispatchEvent(new CustomEvent('publicSuffixListChanged')); }; /******************************************************************************/ const allocateBuffers = function(byteLength) { pslByteLength = byteLength + 3 & ~3; if ( pslBuffer32 !== undefined && pslBuffer32.byteLength >= pslByteLength ) { return; } if ( wasmMemory !== undefined ) { const newPageCount = pslByteLength + 0xFFFF >>> 16; const curPageCount = wasmMemory.buffer.byteLength >>> 16; const delta = newPageCount - curPageCount; if ( delta > 0 ) { wasmMemory.grow(delta); pslBuffer32 = new Uint32Array(wasmMemory.buffer); pslBuffer8 = new Uint8Array(wasmMemory.buffer); } } else { pslBuffer8 = new Uint8Array(pslByteLength); pslBuffer32 = new Uint32Array(pslBuffer8.buffer); } hostnameArg = EMPTY_STRING; pslBuffer8[LABEL_INDICES_SLOT] = 0; }; /******************************************************************************/ // Parse and set a UTF-8 text-based suffix list. Format is same as found at: // http://publicsuffix.org/list/ // // `toAscii` is a converter from unicode to punycode. Required since the // Public Suffix List contains unicode characters. // Suggestion: use <https://github.com/bestiejs/punycode.js> const parse = function(text, toAscii) { // Use short property names for better minifying results const rootRule = { l: EMPTY_STRING, // l => label f: 0, // f => flags c: undefined // c => children }; // Tree building { const compareLabels = function(a, b) { let n = a.length; let d = n - b.length; if ( d !== 0 ) { return d; } for ( let i = 0; i < n; i++ ) { d = a.charCodeAt(i) - b.charCodeAt(i); if ( d !== 0 ) { return d; } } return 0; }; const addToTree = function(rule, exception) { let node = rootRule; let end = rule.length; while ( end > 0 ) { const beg = rule.lastIndexOf('.', end - 1); const label = rule.slice(beg + 1, end); end = beg; if ( Array.isArray(node.c) === false ) { const child = { l: label, f: 0, c: undefined }; node.c = [ child ]; node = child; continue; } let left = 0; let right = node.c.length; while ( left < right ) { const i = left + right >>> 1; const d = compareLabels(label, node.c[i].l); if ( d < 0 ) { right = i; if ( right === left ) { const child = { l: label, f: 0, c: undefined }; node.c.splice(left, 0, child); node = child; break; } continue; } if ( d > 0 ) { left = i + 1; if ( left === right ) { const child = { l: label, f: 0, c: undefined }; node.c.splice(right, 0, child); node = child; break; } continue; } /* d === 0 */ node = node.c[i]; break; } } node.f |= 0b01; if ( exception ) { node.f |= 0b10; } }; // 2. If no rules match, the prevailing rule is "*". addToTree('*', false); const mustPunycode = /[^a-z0-9.-]/; const textEnd = text.length; let lineBeg = 0; while ( lineBeg < textEnd ) { let lineEnd = text.indexOf('\n', lineBeg); if ( lineEnd === -1 ) { lineEnd = text.indexOf('\r', lineBeg); if ( lineEnd === -1 ) { lineEnd = textEnd; } } let line = text.slice(lineBeg, lineEnd).trim(); lineBeg = lineEnd + 1; // Ignore comments const pos = line.indexOf('//'); if ( pos !== -1 ) { line = line.slice(0, pos); } // Ignore surrounding whitespaces line = line.trim(); if ( line.length === 0 ) { continue; } const exception = line.charCodeAt(0) === 0x21 /* '!' */; if ( exception ) { line = line.slice(1); } if ( mustPunycode.test(line) ) { line = toAscii(line.toLowerCase()); } addToTree(line, exception); } } { const labelToOffsetMap = new Map(); const treeData = []; const charData = []; const allocate = function(n) { const ibuf = treeData.length; for ( let i = 0; i < n; i++ ) { treeData.push(0); } return ibuf; }; const storeNode = function(ibuf, node) { const nChars = node.l.length; const nChildren = node.c !== undefined ? node.c.length : 0; treeData[ibuf+0] = nChildren << 16 | node.f << 8 | nChars; // char data if ( nChars <= 4 ) { let v = 0; if ( nChars > 0 ) { v |= node.l.charCodeAt(0); if ( nChars > 1 ) { v |= node.l.charCodeAt(1) << 8; if ( nChars > 2 ) { v |= node.l.charCodeAt(2) << 16; if ( nChars > 3 ) { v |= node.l.charCodeAt(3) << 24; } } } } treeData[ibuf+1] = v; } else { let offset = labelToOffsetMap.get(node.l); if ( offset === undefined ) { offset = charData.length; for ( let i = 0; i < nChars; i++ ) { charData.push(node.l.charCodeAt(i)); } labelToOffsetMap.set(node.l, offset); } treeData[ibuf+1] = offset; } // child nodes if ( Array.isArray(node.c) === false ) { treeData[ibuf+2] = 0; return; } const iarray = allocate(nChildren * 3); treeData[ibuf+2] = iarray; for ( let i = 0; i < nChildren; i++ ) { storeNode(iarray + i * 3, node.c[i]); } }; // First 512 bytes are reserved for internal use allocate(512 >> 2); const iRootRule = allocate(3); storeNode(iRootRule, rootRule); treeData[RULES_PTR_SLOT] = iRootRule; const iCharData = treeData.length << 2; treeData[CHARDATA_PTR_SLOT] = iCharData; const byteLength = (treeData.length << 2) + (charData.length + 3 & ~3); allocateBuffers(byteLength); pslBuffer32.set(treeData); pslBuffer8.set(charData, treeData.length << 2); } fireChangedEvent(); }; /******************************************************************************/ const setHostnameArg = function(hostname) { const buf = pslBuffer8; if ( hostname === hostnameArg ) { return buf[LABEL_INDICES_SLOT]; } if ( hostname === null || hostname.length === 0 ) { hostnameArg = EMPTY_STRING; return (buf[LABEL_INDICES_SLOT] = 0); } hostname = hostname.toLowerCase(); hostnameArg = hostname; let n = hostname.length; if ( n > 255 ) { n = 255; } buf[LABEL_INDICES_SLOT] = n; let i = n; let j = LABEL_INDICES_SLOT + 1; while ( i-- ) { const c = hostname.charCodeAt(i); if ( c === 0x2E /* '.' */ ) { buf[j+0] = i + 1; buf[j+1] = i; j += 2; } buf[i] = c; } buf[j] = 0; return n; }; /******************************************************************************/ // Returns an offset to the start of the public suffix. // // WASM-able, because no information outside the buffer content is required. const getPublicSuffixPosJS = function() { const buf8 = pslBuffer8; const buf32 = pslBuffer32; const iCharData = buf32[CHARDATA_PTR_SLOT]; let iNode = pslBuffer32[RULES_PTR_SLOT]; let cursorPos = -1; let iLabel = LABEL_INDICES_SLOT; // Label-lookup loop for (;;) { // Extract label indices const labelBeg = buf8[iLabel+1]; const labelLen = buf8[iLabel+0] - labelBeg; // Match-lookup loop: binary search let r = buf32[iNode+0] >>> 16; if ( r === 0 ) { break; } const iCandidates = buf32[iNode+2]; let l = 0; let iFound = 0; while ( l < r ) { const iCandidate = l + r >>> 1; const iCandidateNode = iCandidates + iCandidate + (iCandidate << 1); const candidateLen = buf32[iCandidateNode+0] & 0x000000FF; let d = labelLen - candidateLen; if ( d === 0 ) { const iCandidateChar = candidateLen <= 4 ? iCandidateNode + 1 << 2 : iCharData + buf32[iCandidateNode+1]; for ( let i = 0; i < labelLen; i++ ) { d = buf8[labelBeg+i] - buf8[iCandidateChar+i]; if ( d !== 0 ) { break; } } } if ( d < 0 ) { r = iCandidate; } else if ( d > 0 ) { l = iCandidate + 1; } else /* if ( d === 0 ) */ { iFound = iCandidateNode; break; } } // 2. If no rules match, the prevailing rule is "*". if ( iFound === 0 ) { if ( buf32[iCandidates + 1] !== 0x2A /* '*' */ ) { break; } buf8[SUFFIX_NOT_FOUND_SLOT] = 1; iFound = iCandidates; } iNode = iFound; // 5. If the prevailing rule is a exception rule, modify it by // removing the leftmost label. if ( (buf32[iNode+0] & 0x00000200) !== 0 ) { if ( iLabel > LABEL_INDICES_SLOT ) { return iLabel - 2; } break; } if ( (buf32[iNode+0] & 0x00000100) !== 0 ) { cursorPos = iLabel; } if ( labelBeg === 0 ) { break; } iLabel += 2; } return cursorPos; }; let getPublicSuffixPosWASM; let getPublicSuffixPos = getPublicSuffixPosJS; /******************************************************************************/ const getPublicSuffix = function(hostname) { if ( pslBuffer32 === undefined ) { return EMPTY_STRING; } const hostnameLen = setHostnameArg(hostname); const buf8 = pslBuffer8; if ( hostnameLen === 0 || buf8[0] === 0x2E /* '.' */ ) { return EMPTY_STRING; } const cursorPos = getPublicSuffixPos(); if ( cursorPos === -1 ) { return EMPTY_STRING; } const beg = buf8[cursorPos + 1]; return beg === 0 ? hostnameArg : hostnameArg.slice(beg); }; /******************************************************************************/ const getDomain = function(hostname) { if ( pslBuffer32 === undefined ) { return EMPTY_STRING; } const hostnameLen = setHostnameArg(hostname); const buf8 = pslBuffer8; if ( hostnameLen === 0 || buf8[0] === 0x2E /* '.' */ ) { return EMPTY_STRING; } const cursorPos = getPublicSuffixPos(); if ( cursorPos === -1 || buf8[cursorPos + 1] === 0 ) { return EMPTY_STRING; } // 7. The registered or registrable domain is the public suffix plus one // additional label. const beg = buf8[cursorPos + 3]; return beg === 0 ? hostnameArg : hostnameArg.slice(beg); }; /******************************************************************************/ const suffixInPSL = function(hostname) { if ( pslBuffer32 === undefined ) { return false; } const hostnameLen = setHostnameArg(hostname); const buf8 = pslBuffer8; if ( hostnameLen === 0 || buf8[0] === 0x2E /* '.' */ ) { return false; } buf8[SUFFIX_NOT_FOUND_SLOT] = 0; const cursorPos = getPublicSuffixPos(); return cursorPos !== -1 && buf8[cursorPos + 1] === 0 && buf8[SUFFIX_NOT_FOUND_SLOT] !== 1; }; /******************************************************************************/ const toSelfie = function(encoder) { if ( pslBuffer8 === undefined ) { return ''; } if ( encoder instanceof Object ) { const bufferStr = encoder.encode(pslBuffer8.buffer, pslByteLength); return `${SELFIE_MAGIC}\t${bufferStr}`; } return { magic: SELFIE_MAGIC, buf32: pslBuffer32.subarray(0, pslByteLength >> 2), }; }; const fromSelfie = function(selfie, decoder) { let byteLength = 0; if ( typeof selfie === 'string' && selfie.length !== 0 && decoder instanceof Object ) { const pos = selfie.indexOf('\t'); if ( pos === -1 || selfie.slice(0, pos) !== `${SELFIE_MAGIC}` ) { return false; } const bufferStr = selfie.slice(pos + 1); byteLength = decoder.decodeSize(bufferStr); if ( byteLength === 0 ) { return false; } allocateBuffers(byteLength); decoder.decode(bufferStr, pslBuffer8.buffer); } else if ( selfie instanceof Object && selfie.magic === SELFIE_MAGIC && selfie.buf32 instanceof Uint32Array ) { byteLength = selfie.buf32.length << 2; allocateBuffers(byteLength); pslBuffer32.set(selfie.buf32); } else { return false; } // Important! hostnameArg = EMPTY_STRING; pslBuffer8[LABEL_INDICES_SLOT] = 0; fireChangedEvent(); return true; }; /******************************************************************************/ // The WASM module is entirely optional, the JS implementation will be // used should the WASM module be unavailable for whatever reason. const enableWASM = (( ) => { let wasmPromise; const getWasmInstance = async function(wasmModuleFetcher, path) { if ( typeof WebAssembly !== 'object' ) { return false; } // The wasm code will work only if CPU is natively little-endian, // as we use native uint32 array in our js code. const uint32s = new Uint32Array(1); const uint8s = new Uint8Array(uint32s.buffer); uint32s[0] = 1; if ( uint8s[0] !== 1 ) { return false; } try { const module = await wasmModuleFetcher(`${path}publicsuffixlist`); if ( module instanceof WebAssembly.Module === false ) { return false; } const pageCount = pslBuffer8 !== undefined ? pslBuffer8.byteLength + 0xFFFF >>> 16 : 1; const memory = new WebAssembly.Memory({ initial: pageCount }); const instance = await WebAssembly.instantiate(module, { imports: { memory } }); if ( instance instanceof WebAssembly.Instance === false ) { return false; } const curPageCount = memory.buffer.byteLength >>> 16; const newPageCount = pslBuffer8 !== undefined ? pslBuffer8.byteLength + 0xFFFF >>> 16 : 0; if ( newPageCount > curPageCount ) { memory.grow(newPageCount - curPageCount); } if ( pslBuffer32 !== undefined ) { const buf8 = new Uint8Array(memory.buffer); const buf32 = new Uint32Array(memory.buffer); buf32.set(pslBuffer32); pslBuffer8 = buf8; pslBuffer32 = buf32; } wasmMemory = memory; getPublicSuffixPosWASM = instance.exports.getPublicSuffixPos; getPublicSuffixPos = getPublicSuffixPosWASM; return true; } catch(reason) { console.info(reason); } return false; }; return async function(wasmModuleFetcher, path) { if ( getPublicSuffixPosWASM instanceof Function ) { return true; } if ( wasmPromise instanceof Promise === false ) { wasmPromise = getWasmInstance(wasmModuleFetcher, path); } return wasmPromise; }; })(); const disableWASM = function() { if ( getPublicSuffixPosWASM instanceof Function ) { getPublicSuffixPos = getPublicSuffixPosJS; getPublicSuffixPosWASM = undefined; } if ( wasmMemory === undefined ) { return; } if ( pslBuffer32 !== undefined ) { const buf8 = new Uint8Array(pslByteLength); const buf32 = new Uint32Array(buf8.buffer); buf32.set(pslBuffer32); pslBuffer8 = buf8; pslBuffer32 = buf32; } wasmMemory = undefined; }; /******************************************************************************/ return ({ version: '2.0', parse, getDomain, suffixInPSL, getPublicSuffix, toSelfie, fromSelfie, disableWASM, enableWASM, }); /******************************************************************************/ // <<<<<<<< end of anonymous namespace })();