// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. //go:build script // +build script package js import ( teaconst "github.com/TeaOSLab/EdgeNode/internal/const" "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" "net/url" ) func init() { if !teaconst.IsMain { return } SharedLibraryManager.Register(&JSURLLibrary{}) } type JSURLLibrary struct { JSBaseLibrary } func (this *JSURLLibrary) JSNamespace() string { return "gojs.net.JSURLLibrary" } func (this *JSURLLibrary) JSPrototype() string { return `gojs.net.URL = class { #urlString constructor(urlString) { this.urlString = urlString let url = $this.Parse(urlString) gojs.copyAttrs(this, url) } toString () { return this.urlString } }` } func (this *JSURLLibrary) Parse(arguments *FunctionArguments) (maps.Map, error) { var urlString = "" arg0, ok := arguments.ArgAt(0) if ok { urlString = arg0.String() } u, err := url.Parse(urlString) if err != nil { return maps.Map{}, err } var user = map[string]string{} if u.User != nil { password, _ := u.User.Password() user = map[string]string{ "username": u.User.Username(), "password": password, } } return maps.Map{ "host": u.Host, "query": u.RawQuery, "port": types.Int(u.Port()), "path": u.Path, "hash": u.Fragment, "scheme": u.Scheme, "opaque": u.Opaque, "user": user, }, nil }