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