1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16interface NativeURLSearchParams{ 17 new(input ?: object | string | Iterable<[]> | null | undefined) : NativeURLSearchParams; 18 append(params1 : string, params2 : string) : void; 19 set(setname : string, setvalues : string) : void; 20 sort() : void; 21 has(hasname : string) : boolean; 22 toString() : string; 23 keys() : Object; 24 values(): Object; 25 getAll(getAllname : string) : string[]; 26 get(getname : string) : string; 27 entries() : Object; 28 delete(deletename : string) : void; 29 updateParams() : void; 30 array : string[] | number[]; 31} 32 33interface NativeURLParams{ 34 new(input ?: object | string | Iterable<[]> | null | undefined) : NativeURLParams; 35 append(params1 : string, params2 : string) : void; 36 set(setname : string, setvalues : string) : void; 37 sort() : void; 38 has(hasname : string) : boolean; 39 toString() : string; 40 keys() : Object; 41 values(): Object; 42 getAll(getAllname : string) : string[]; 43 get(getname : string) : string; 44 entries() : Object; 45 delete(deletename : string) : void; 46 updateParams() : void; 47 array : string[] | number[]; 48} 49 50interface NativeUrl{ 51 new(input : string, base ?: string | NativeUrl) : NativeUrl; 52 new() : NativeUrl; 53 protocol : string; 54 username : string; 55 password : string; 56 hash : string; 57 search : string; 58 hostname : string; 59 host : string; 60 port : string; 61 href(input : string) : void; 62 pathname : string; 63 onOrOff : boolean; 64 GetIsIpv6 : boolean; 65 parseURL(input : string, base ?: string | NativeUrl | URL) : NativeUrl; 66} 67interface UrlInterface{ 68 URLSearchParams1 : NativeURLSearchParams; 69 Url : NativeUrl; 70 URLParams1 : NativeURLParams; 71 stringParmas(input : string) : string[]; 72} 73 74declare function requireInternal(s : string) : UrlInterface; 75const UrlInterface = requireInternal('url'); 76 77 78var seachParamsArr : Array<string> = []; 79const TypeErrorCodeId = 401; 80const SyntaxErrorCodeId = 10200002; 81 82class BusinessError extends Error { 83 code : number; 84 constructor(msg:string) { 85 super(msg) 86 this.name = 'BusinessError'; 87 this.code = TypeErrorCodeId; 88 } 89} 90 91class URLParams { 92 urlcalss:NativeURLParams; 93 constructor(input : object | string | Iterable<[]> | null | undefined) { 94 let out : string[] = parameterProcess(input); 95 this.urlcalss = new UrlInterface.URLParams1(); 96 this.urlcalss.array = out; 97 } 98 99 append(params1 : string, params2 : string) : void { 100 if (arguments.length === 0 || typeof arguments[0] !== 'string') { 101 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 102 throw err; 103 } 104 if (arguments.length === 1 || typeof arguments[1] !== 'string') { 105 let error = new BusinessError(`Parameter error.The type of ${arguments[1]} must be string`); 106 throw error; 107 } 108 this.urlcalss.append(params1, params2); 109 } 110 111 set(setname : string, setvalues : string) : void { 112 if (arguments.length === 0 || typeof arguments[0] !== 'string') { 113 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 114 throw err; 115 } 116 if (arguments.length === 1 || typeof arguments[1] !== 'string') { 117 let error = new BusinessError(`Parameter error.The type of ${arguments[1]} must be string`); 118 throw error; 119 } 120 this.urlcalss.set(setname, setvalues); 121 } 122 123 sort() : void { 124 this.urlcalss.sort(); 125 } 126 127 has(hasname : string) : boolean { 128 if (typeof arguments[0] !== 'string') { 129 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 130 throw err; 131 } 132 return this.urlcalss.has(hasname); 133 } 134 135 toString() : string { 136 return this.urlcalss.toString(); 137 } 138 139 keys() : Object { 140 return this.urlcalss.keys(); 141 } 142 143 values() : Object { 144 return this.urlcalss.values(); 145 } 146 147 getAll() : string[] { 148 if ((arguments.length !== 1) || (typeof arguments[0] !== 'string')) { 149 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 150 throw err; 151 } 152 return this.urlcalss.getAll(arguments[0]); 153 } 154 155 get(getname : string) : string { 156 if (arguments.length === 0 || typeof arguments[0] !== 'string') { 157 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 158 throw err; 159 } 160 return this.urlcalss.get(getname); 161 } 162 163 entries() : Object { 164 return this.urlcalss.entries(); 165 } 166 167 delete(deletename : string) : void { 168 if (arguments.length === 0 || typeof arguments[0] !== 'string') { 169 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 170 throw err; 171 } 172 this.urlcalss.delete(deletename); 173 } 174 175 forEach(objfun : Function, thisArg ?: Object) { 176 if (typeof objfun !== 'function') { 177 let err = new BusinessError(`Parameter error.The type of ${objfun} must be function`); 178 throw err; 179 } 180 let array = this.urlcalss.array; 181 if (array.length == 0) { 182 return; 183 } 184 let size = array.length - 1; 185 for (let i = 0; i < size; i += 2) { // 2:Searching for the number and number of keys and values 2 186 let key = array[i]; 187 let value = array[i + 1]; 188 objfun.call(thisArg, value, key, this); 189 } 190 } 191 192 [Symbol.iterator]() : Object { 193 return this.urlcalss.entries(); 194 } 195 196 updateParams(input : string) : void { 197 let out = []; 198 out = parameterProcess(input); 199 this.urlcalss.array = out; 200 } 201} 202 203class URLSearchParams { 204 urlcalss : NativeURLSearchParams; 205 constructor(input : object | string | Iterable<[]> | null | undefined) { 206 let out : string[] = parameterProcessing(input); 207 this.urlcalss = new UrlInterface.URLSearchParams1(); 208 this.urlcalss.array = out; 209 } 210 append(params1 : string, params2 : string) { 211 if (arguments.length === 0 || typeof arguments[0] !== 'string') { 212 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 213 throw err; 214 } 215 if (arguments.length === 1 || typeof arguments[1] !== 'string') { 216 let error = new BusinessError(`Parameter error.The type of ${arguments[1]} must be string`); 217 throw error; 218 } 219 this.urlcalss.append(params1, params2); 220 } 221 222 set(setname : string, setvalues : string) { 223 if (arguments.length === 0 || typeof arguments[0] !== 'string') { 224 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 225 throw err; 226 } 227 if (arguments.length === 1 || typeof arguments[1] !== 'string') { 228 let error = new BusinessError(`Parameter error.The type of ${arguments[1]} must be string`); 229 throw error; 230 } 231 this.urlcalss.set(setname, setvalues); 232 } 233 234 sort() { 235 this.urlcalss.sort(); 236 } 237 238 has(hasname : string) { 239 if (typeof arguments[0] !== 'string') { 240 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 241 throw err; 242 } 243 return this.urlcalss.has(hasname); 244 } 245 246 toString() { 247 return this.urlcalss.toString(); 248 } 249 250 keys() { 251 return this.urlcalss.keys(); 252 } 253 254 values() { 255 return this.urlcalss.values(); 256 } 257 258 getAll(getAllname : string) { 259 return this.urlcalss.getAll(getAllname); 260 } 261 262 get(getname : string) { 263 return this.urlcalss.get(getname); 264 } 265 266 entries() { 267 268 return this.urlcalss.entries(); 269 } 270 271 delete(deletename : string) { 272 this.urlcalss.delete(deletename); 273 } 274 275 forEach(objfun : Function, thisArg ?: Object) { 276 let array = this.urlcalss.array; 277 if (array.length == 0) { 278 return; 279 } 280 281 let size = array.length - 1; 282 for (let i = 0; i < size; i += 2) { // 2:Searching for the number and number of keys and values 2 283 let key = array[i]; 284 let value = array[i + 1]; 285 objfun.call(thisArg, value, key, this); 286 } 287 } 288 289 [Symbol.iterator]() { 290 return this.urlcalss.entries(); 291 } 292 293 updateParams(input : string) { 294 let out = []; 295 out = parameterProcessing(input); 296 this.urlcalss.array = out; 297 } 298} 299 300function toHleString(arg : string | symbol | number) { 301 return arg.toString(); 302} 303 304function parameterProcess(input : object | string | Iterable<[]>) { 305 if (input === null || typeof input === 'undefined') { 306 seachParamsArr = []; 307 return seachParamsArr; 308 } else if (typeof input === 'object' || typeof input === 'function') { 309 return sysObjectParams(input); 310 } else { 311 return initToStringSeachParams(input); 312 } 313} 314 315function parameterProcessing(input : object | string | Iterable<[]>) { 316 if (input === null || typeof input === 'undefined') { 317 seachParamsArr = []; 318 return seachParamsArr; 319 } else if (typeof input === 'object' || typeof input === 'function') { 320 return initObjectSeachParams(input); 321 } else { 322 return initToStringSeachParams(input); 323 } 324} 325 326function sysObjectParams(input : object | Iterable<[]>) { 327 if (typeof input[Symbol.iterator] === 'function') { 328 return iteratorMethodThrow(input as Iterable<[string]>); 329 } 330 return recordMethod(input); 331} 332 333function initObjectSeachParams(input : object | Iterable<[]>) { 334 if (typeof input[Symbol.iterator] === 'function') { 335 return iteratorMethod(input as Iterable<[string]>); 336 } 337 return recordMethod(input); 338} 339 340function recordMethod(input : object) { 341 const keys = Reflect.ownKeys(input); 342 seachParamsArr = []; 343 for (let i = 0; i <= keys.length; i++) { 344 const key = keys[i]; 345 const desc = Reflect.getOwnPropertyDescriptor(input, key); 346 if (desc !== undefined && desc.enumerable) { 347 const typedKey = toHleString(key); 348 const typedValue = toHleString(input[key]); 349 seachParamsArr.push(typedKey, typedValue); 350 } 351 } 352 return seachParamsArr; 353} 354 355function iteratorMethodThrow(input : Iterable<[string]>) { 356 let pairs = []; 357 seachParamsArr = []; 358 for (const pair of input) { 359 if ((typeof pair !== 'object' && typeof pair !== 'function') || pair === null || typeof pair[Symbol.iterator] !== 'function') { 360 let err = new BusinessError(`Parameter error.The type of ${input} must be string[][]`); 361 throw err; 362 } 363 const convertedPair = []; 364 for (let element of pair) { 365 convertedPair.push(element); 366 } 367 pairs.push(convertedPair); 368 } 369 370 for (const pair of pairs) { 371 if (pair.length !== 2) { // 2:Searching for the number and number of keys and values 2 372 let err = new BusinessError(`Parameter error.The type of ${input} must be string[][]`); 373 throw err; 374 } 375 seachParamsArr.push(pair[0], pair[1]); 376 } 377 return seachParamsArr; 378} 379 380function iteratorMethod(input : Iterable<[string]>) { 381 let pairs = []; 382 seachParamsArr = []; 383 for (const pair of input) { 384 const convertedPair = []; 385 for (let element of pair) { 386 convertedPair.push(element); 387 } 388 pairs.push(convertedPair); 389 } 390 391 for (const pair of pairs) { 392 if (pair.length !== 2) { // 2:Searching for the number and number of keys and values 2 393 console.error('key-value-is-worong'); 394 } 395 seachParamsArr.push(pair[0], pair[1]); 396 } 397 return seachParamsArr; 398} 399 400function initToStringSeachParams(input : string) { 401 if (typeof input !== 'string') { 402 let err = new BusinessError(`Parameter error.The type of ${input} must be string`); 403 throw err; 404 } 405 if (input[0] === '?') { 406 input = input.slice(1); 407 } 408 let strVal = decodeURI(input); 409 seachParamsArr = UrlInterface.stringParmas(strVal); 410 return seachParamsArr; 411} 412 413class URL { 414 href_ : string = ''; 415 search_ : string = ''; 416 origin_ : string = ''; 417 username_ : string = ''; 418 password_ : string = ''; 419 hostname_ : string = ''; 420 host_ : string = ''; 421 hash_ : string = ''; 422 protocol_ : string = ''; 423 pathname_ : string = ''; 424 port_ : string = ''; 425 searchParamsClass_ !: URLSearchParams; 426 URLParamsClass_ !: URLParams; 427 c_info !: NativeUrl; 428 public constructor() 429 public constructor(inputUrl: string, inputBase?: string | URL) 430 public constructor(inputUrl?: string, inputBase?: string | URL) { 431 if (arguments.length === 0) { 432 } 433 let nativeUrl !: NativeUrl; 434 if (arguments.length === 1) { 435 inputUrl = arguments[0]; 436 if (typeof inputUrl === 'string' && inputUrl.length > 0) { 437 nativeUrl = new UrlInterface.Url(inputUrl); 438 } else { 439 console.error('Input parameter error'); 440 } 441 } 442 443 if (arguments.length === 2) { // 2:The number of parameters is 2 444 inputUrl = arguments[0]; 445 inputBase = arguments[1]; 446 if (typeof inputUrl === 'string') { 447 if (typeof inputBase === 'string') { 448 if (inputBase.length > 0) { 449 nativeUrl = new UrlInterface.Url(inputUrl, inputBase); 450 } else { 451 console.error('Input parameter error'); 452 return; 453 } 454 } else if (typeof inputBase === 'object') { 455 let nativeBase = inputBase.getInfo(); 456 nativeUrl = new UrlInterface.Url(inputUrl, nativeBase); 457 } 458 } 459 } 460 if (arguments.length === 1 || arguments.length === 2) { // 2:The number of parameters is 2 461 this.c_info = nativeUrl; 462 if (nativeUrl.onOrOff) { 463 this.search_ = encodeURI(nativeUrl.search); 464 this.username_ = encodeURI(nativeUrl.username); 465 this.password_ = encodeURI(nativeUrl.password); 466 if (nativeUrl.GetIsIpv6) { 467 this.hostname_ = nativeUrl.hostname; 468 this.host_ = nativeUrl.host; 469 } else { 470 this.hostname_ = encodeURI(nativeUrl.hostname); 471 this.host_ = encodeURI(nativeUrl.host); 472 } 473 this.hash_ = encodeURI(nativeUrl.hash); 474 this.protocol_ = encodeURI(nativeUrl.protocol); 475 this.pathname_ = encodeURI(nativeUrl.pathname); 476 this.port_ = nativeUrl.port; 477 this.origin_ = nativeUrl.protocol + '//' + nativeUrl.host; 478 this.searchParamsClass_ = new URLSearchParams(this.search_); 479 this.URLParamsClass_ = new URLParams(this.search_) 480 this.set_href(); 481 } else { 482 console.error('constructor failed'); 483 } 484 } 485 } 486 487 static parseURL(inputUrl : string, inputBase ?: string | NativeUrl | URL) { 488 if (typeof arguments[0] !== 'string') { 489 let err = new BusinessError(`Parameter error.The type of ${arguments[0]} must be string`); 490 throw err; 491 } 492 let nativeUrl !: NativeUrl; 493 if (arguments.length === 1) { 494 inputUrl = arguments[0]; 495 nativeUrl = new UrlInterface.Url(inputUrl); 496 } 497 if (arguments.length === 2) { // 2:The number of parameters is 2 498 if (typeof inputBase === 'string') { 499 if (inputBase.length > 0) { 500 nativeUrl = new UrlInterface.Url(inputUrl, inputBase); 501 } else { 502 let err = new BusinessError(`Parameter error.The type of ${inputBase} must be string`); 503 throw err; 504 } 505 } else if (typeof inputBase === 'object') { 506 let nativeBase = inputBase.getInfo(); 507 nativeUrl = new UrlInterface.Url(inputUrl, nativeBase); 508 } else { 509 let err = new BusinessError(`Parameter error.The type of ${inputBase} must be string or URL`); 510 throw err; 511 } 512 } 513 let urlHelper = new URL(); 514 if (nativeUrl.onOrOff) { 515 urlHelper.search_ = encodeURI(nativeUrl.search); 516 urlHelper.username_ = encodeURI(nativeUrl.username); 517 urlHelper.password_ = encodeURI(nativeUrl.password); 518 if (nativeUrl.GetIsIpv6) { 519 urlHelper.hostname_ = nativeUrl.hostname; 520 urlHelper.host_ = nativeUrl.host; 521 } else { 522 urlHelper.hostname_ = encodeURI(nativeUrl.hostname); 523 urlHelper.host_ = encodeURI(nativeUrl.host); 524 } 525 urlHelper.hash_ = encodeURI(nativeUrl.hash); 526 urlHelper.protocol_ = encodeURI(nativeUrl.protocol); 527 urlHelper.pathname_ = encodeURI(nativeUrl.pathname); 528 urlHelper.port_ = nativeUrl.port; 529 urlHelper.origin_ = nativeUrl.protocol + '//' + nativeUrl.host; 530 urlHelper.searchParamsClass_ = new URLSearchParams(urlHelper.search_); 531 urlHelper.URLParamsClass_ = new URLParams(urlHelper.search_); 532 urlHelper.set_href(); 533 } else { 534 let err = new BusinessError('Syntax Error. Invalid Url string'); 535 err.code = SyntaxErrorCodeId; 536 throw err; 537 } 538 return urlHelper; 539 } 540 541 getInfo() { 542 return this.c_info; 543 } 544 toString() { 545 return this.href_; 546 } 547 548 get protocol() { 549 return this.protocol_; 550 } 551 set protocol(scheme) { 552 if (scheme.length === 0) { 553 return; 554 } 555 if (this.protocol_ === 'file:' 556 && (this.host_ === '' || this.host_ == null)) { 557 return; 558 } 559 this.c_info.protocol = scheme; 560 this.protocol_ = this.c_info.protocol; 561 this.set_href() 562 } 563 get origin() { 564 let kOpaqueOrigin = 'null'; 565 switch (this.protocol_) { 566 case 'ftp:': 567 case 'gopher:': 568 case 'http:': 569 case 'https:': 570 case 'ws:': 571 case 'wss:': 572 return this.origin_; 573 } 574 return kOpaqueOrigin; 575 } 576 get username() { 577 return this.username_; 578 } 579 set username(input) { 580 if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') { 581 return; 582 } 583 const usname_ = encodeURI(input); 584 this.c_info.username = usname_; 585 this.username_ = this.c_info.username; 586 this.set_href(); 587 } 588 get password() { 589 return this.password_; 590 } 591 set password(input) { 592 if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') { 593 return; 594 } 595 const passwd_ = encodeURI(input); 596 this.c_info.password = passwd_; 597 this.password_ = this.c_info.password; 598 this.set_href(); 599 } 600 get hash() { 601 return this.hash_; 602 } 603 set hash(fragment) { 604 const fragment_ = encodeURI(fragment); 605 this.c_info.hash = fragment_; 606 this.hash_ = this.c_info.hash; 607 this.set_href(); 608 } 609 get search() { 610 return this.search_; 611 } 612 set search(query) { 613 const query_ = encodeURI(query); 614 this.c_info.search = query_; 615 this.search_ = this.c_info.search; 616 this.searchParamsClass_.updateParams(this.search_); 617 this.URLParamsClass_.updateParams(this.search_); 618 this.set_href(); 619 } 620 get hostname() { 621 return this.hostname_; 622 } 623 set hostname(hostname) { 624 this.c_info.hostname = hostname; 625 if (this.c_info.GetIsIpv6) { 626 this.hostname_ = this.c_info.hostname; 627 } else { 628 this.hostname_ = encodeURI(this.c_info.hostname); 629 } 630 this.set_href(); 631 } 632 get host() { 633 return this.host_; 634 } 635 set host(host_) { 636 this.c_info.host = host_; 637 if (this.c_info.GetIsIpv6) { 638 this.host_ = this.c_info.host; 639 this.hostname_ = this.c_info.hostname; 640 this.port_ = this.c_info.port; 641 } else { 642 this.host_ = encodeURI(this.c_info.host); 643 this.hostname_ = encodeURI(this.c_info.hostname); 644 this.port_ = this.c_info.port; 645 } 646 this.set_href(); 647 } 648 get port() { 649 return this.port_; 650 } 651 set port(port) { 652 if (this.host_ === '' || this.protocol_ === 'file:' || port === '') { 653 return; 654 } 655 this.c_info.port = port; 656 this.port_ = this.c_info.port; 657 this.set_href(); 658 } 659 get href() { 660 return this.href_; 661 } 662 set href(href_) { 663 this.c_info.href(href_); 664 if (this.c_info.onOrOff) { 665 this.search_ = encodeURI(this.c_info.search); 666 this.username_ = encodeURI(this.c_info.username); 667 this.password_ = encodeURI(this.c_info.password); 668 if (this.c_info.GetIsIpv6) { 669 this.hostname_ = this.c_info.hostname; 670 this.host_ = this.c_info.host; 671 } else { 672 this.hostname_ = encodeURI(this.c_info.hostname); 673 this.host_ = encodeURI(this.c_info.host); 674 } 675 this.hash_ = encodeURI(this.c_info.hash); 676 this.protocol_ = encodeURI(this.c_info.protocol); 677 this.pathname_ = encodeURI(this.c_info.pathname); 678 this.port_ = this.c_info.port; 679 this.origin_ = this.protocol_ + '//' + this.host_; 680 this.searchParamsClass_.updateParams(this.search_); 681 this.URLParamsClass_.updateParams(this.search_); 682 this.set_href(); 683 } 684 } 685 686 get pathname() { 687 return this.pathname_; 688 } 689 set pathname(path) { 690 const path_ = encodeURI(path); 691 this.c_info.pathname = path_; 692 this.pathname_ = this.c_info.pathname; 693 this.set_href(); 694 } 695 696 get searchParams() { 697 return this.searchParamsClass_; 698 } 699 700 get params() { 701 return this.URLParamsClass_; 702 } 703 704 toJSON() { 705 return this.href_; 706 } 707 set_href() { 708 let temp = this.protocol_; 709 if (this.hostname_ !== '') { 710 temp += '//'; 711 if (this.password_ !== '' || this.username_ !== '') { 712 if (this.username_ !== '') { 713 temp += this.username_; 714 } 715 if (this.password_ !== '') { 716 temp += ':'; 717 temp += this.password_; 718 } 719 temp += '@'; 720 } 721 temp += this.hostname_; 722 if (this.port_ !== '') { 723 temp += ':'; 724 temp += this.port_; 725 } 726 } else if (this.protocol_ === 'file:') { 727 temp += '//'; 728 } 729 temp += this.pathname_; 730 if (this.search_) { 731 temp += this.search_; 732 } 733 if (this.hash_) { 734 temp += this.hash_; 735 } 736 this.href_ = temp; 737 } 738} 739 740export default { 741 URLSearchParams: URLSearchParams, 742 URL: URL, 743 URLParams: URLParams, 744}