Piik now has one public presentation and a coordinated App/Server release path. This phase unifies the room UI, rewrites the bilingual README and guides, adds a static website with an inline product film, and prepares matching GitHub/Gitee packages and update links. The film uses the current homepage and shared product controls on one seekable timeline. Runtime fixes preserve presentation when the App opens a room, normalize Site origins, and keep capture and media-recovery status in the television indicator. New visits use Chinese for a Chinese system language and English otherwise; explicit language and visual-mode choices remain available. An optional site passphrase controls site entry, while room authorization remains enforced. GitHub is the release authority. Gitee mirrors verified packages and provides update metadata when GitHub is unavailable. Reviewed PR release notes become the GitHub Release description. The separate public demo uses P2P only, with HTTPS and STUN verified. Validation: 756 Web tests, TypeScript and production build; Go core tests, formatting, vet and available target builds; bounded Windows capture and App/Site interoperability; browser film checks, audible playback and narrow layouts; anonymous Gitee Windows-package download, checksum and launcher/update-link checks. Public demo acceptance covers authenticated WebSocket signaling, synthetic P2P video and room cleanup on one network. macOS/Linux physical capture and the wider device/network/endurance matrix remain explicitly deferred. Publication builds every package from the merged source SHA. The repository is public, main protection is active, and Pages is configured for piik.tv. CI/publisher and Pages deployment records own publication results. ## Release notes ### 中文 Piik 是免费开源的屏幕共享工具。开个房间,发出邀请,最多 20 位朋友就能用浏览器加入观看。 - 使用浏览器或 Piik App 分享游戏、窗口和整个屏幕。 - 优先建立直连;自托管站点可开启媒体转发作为备用线路。 - Piik App 支持本地房间、临时公网邀请和已有站点。 - Piik Server 内置网页界面、房间管理和连接服务,方便自行部署。 - 提供中英文界面与使用指南,以及 GitHub、Gitee 两条下载线路。 [官网](https://piik.tv) · [在线体验](https://demo.piik.tv) · [使用指南](https://github.com/TNTcraftHIM/Piik/blob/main/docs/guide/getting-started.zh-CN.md) · [自行部署](https://github.com/TNTcraftHIM/Piik/blob/main/docs/operations/self-hosting.zh-CN.md) · [Gitee 下载](https://gitee.com/TNTcraftHIM/Piik/releases) Windows App 和桌面浏览器是本次主要验证的平台。macOS 和 Linux 尚未经过实机测试,欢迎有设备的朋友测试并反馈。演示站和 App 公网邀请使用 P2P,受限网络下可能无法连接;自托管站点可开启媒体转发。 ### English Piik is free, open-source screen sharing for you and up to 20 viewers. Start a room, send an invitation, and your friends can join in a browser. - Share games, windows or a whole screen from your browser or Piik App. - Direct connections come first, with optional media forwarding on self-hosted sites. - Piik App supports local rooms, temporary public invitations and existing sites. - Piik Server includes the web interface, rooms and connection services for self-hosting. - English and Chinese interfaces and guides are available, with GitHub and Gitee downloads. [Website](https://piik.tv) · [Online demo](https://demo.piik.tv) · [User guide](https://github.com/TNTcraftHIM/Piik/blob/main/docs/guide/getting-started.md) · [Self-hosting](https://github.com/TNTcraftHIM/Piik/blob/main/docs/operations/self-hosting.md) · [Gitee downloads](https://gitee.com/TNTcraftHIM/Piik/releases) Windows App and desktop browsers are the primary validated paths. macOS and Linux have not yet been tested on physical devices; testing and feedback are welcome. The demo and App public invitations rely on P2P and may not connect on restrictive networks. Self-hosted sites can enable media forwarding.
485 lines
16 KiB
Go
485 lines
16 KiB
Go
// Package config owns Hosted and App Local configuration and ICE discovery.
|
|
// Callers provide configuration inputs; loading opens no runtime resources.
|
|
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"net/netip"
|
|
"net/url"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
|
|
)
|
|
|
|
// Environment selects development-only behavior. A stale unit file must fail
|
|
// closed rather than fall back to development.
|
|
type Environment string
|
|
|
|
// The two accepted PIIK_ENV values.
|
|
const (
|
|
EnvironmentDevelopment Environment = "development"
|
|
EnvironmentProduction Environment = "production"
|
|
)
|
|
|
|
// Bounds and defaults for Hosted configuration.
|
|
const (
|
|
minSiteAccessPasswordBytes = 8
|
|
maxSiteAccessPasswordBytes = 128
|
|
defaultMaxViewersPerRoom = 8
|
|
defaultPort = 8787
|
|
maxPort = 65_535
|
|
)
|
|
|
|
// VISIBLE_ASCII_PATTERN.
|
|
var visibleASCIIPattern = regexp.MustCompile(`^[\x21-\x7e]+$`)
|
|
|
|
const removedTurnReason = "ordinary ICE accepts STUN_URLS only"
|
|
|
|
// removedEnvironmentVariables names settings with no current equivalent. An
|
|
// empty reason takes the TURN default.
|
|
// The order is the report order when several removed names are present.
|
|
var removedEnvironmentVariables = []struct{ name, reason string }{
|
|
{"TURN_URLS", ""},
|
|
{"TURN_SHARED_SECRET", ""},
|
|
{"TURN_CREDENTIAL_TTL_SECONDS", ""},
|
|
{"PEER_ICE_TURN_URLS", ""},
|
|
{"PEER_ICE_TURN_SHARED_SECRET", ""},
|
|
{"PEER_ICE_TURN_CREDENTIAL_TTL_SECONDS", ""},
|
|
{"SELECTED_EDGE_TURN_URLS", ""},
|
|
{"SELECTED_EDGE_TURN_SHARED_SECRET", ""},
|
|
{"SELECTED_EDGE_TURN_CREDENTIAL_TTL_SECONDS", ""},
|
|
{"SELECTED_EDGE_TURN_ALLOCATION_CAPACITY", ""},
|
|
{"PEER_ASSISTED_ROOM_IDS", "peer-assisted media applies to every room"},
|
|
{"PEER_ASSISTED_MEDIA", "peer-assisted media is always enabled"},
|
|
{"HOST_ADMISSION_PASSWORD", "use SITE_ACCESS_PASSWORD"},
|
|
{"MAX_PEER_RELAY_DOWNSTREAM_EDGES", "use ENDPOINT_MEDIA_COPY_CAPACITY"},
|
|
{"ROOM_TTL_SECONDS", "rooms do not expire"},
|
|
{"ROOM_LEASE_SECONDS", "rooms do not expire"},
|
|
{"ACCESS_PASSWORD", "use SITE_ACCESS_PASSWORD"},
|
|
{"NODE_ENV", "use PIIK_ENV"},
|
|
}
|
|
|
|
// SFUConfig enables the embedded UDP media listener.
|
|
type SFUConfig struct {
|
|
ListenHost string
|
|
Port int
|
|
PublicIP string
|
|
}
|
|
|
|
// Config describes runtime configuration. Zero values mean open site
|
|
// access, process-memory rooms, and no SFU fallback.
|
|
type Config struct {
|
|
Env Environment
|
|
Port int
|
|
ListenHost string
|
|
PublicBaseURL *url.URL
|
|
// AllowedOrigins is a membership test only; map order is not observable.
|
|
AllowedOrigins map[string]struct{}
|
|
SiteAccessPassword string
|
|
RoomDatabasePath string
|
|
MaxViewersPerRoom int
|
|
EndpointMediaCopyCapacity int
|
|
SFU *SFUConfig
|
|
STUNURLs []string
|
|
// STUNListenAddresses is populated only by Hosted configuration. Local
|
|
// discovery URLs do not make the Client an externally reachable STUN server.
|
|
STUNListenAddresses []string
|
|
NATPredictionEnabled bool
|
|
// NATPredictionSTUNURLs is nil when unset, which IceConfig distinguishes
|
|
// from a configured empty list.
|
|
NATPredictionSTUNURLs []string
|
|
}
|
|
|
|
// Load validates the whole process environment. Presence, not emptiness,
|
|
// distinguishes an explicit value from an unset value where that distinction
|
|
// is part of the contract.
|
|
func Load(env map[string]string) (Config, error) {
|
|
for _, removed := range removedEnvironmentVariables {
|
|
if _, present := env[removed.name]; present {
|
|
reason := removed.reason
|
|
if reason == "" {
|
|
reason = removedTurnReason
|
|
}
|
|
return Config{}, fmt.Errorf("%s is no longer supported; %s", removed.name, reason)
|
|
}
|
|
}
|
|
|
|
environment, err := parseEnvironment(env)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
port, err := parsePositiveInteger(env["PORT"], defaultPort, "PORT")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
if port > maxPort {
|
|
return Config{}, fmt.Errorf("PORT must be at most %d", maxPort)
|
|
}
|
|
listenHost := strings.TrimFunc(env["LISTEN_HOST"], protocol.IsJSWhitespace)
|
|
if listenHost == "" {
|
|
listenHost = "0.0.0.0"
|
|
}
|
|
|
|
// Only an absent variable takes the default; an explicit empty value is
|
|
// invalid below.
|
|
rawPublicBaseURL, present := env["PUBLIC_BASE_URL"]
|
|
if !present {
|
|
rawPublicBaseURL = fmt.Sprintf("http://localhost:%d", port)
|
|
}
|
|
publicBaseURL, err := parseOriginURL(rawPublicBaseURL, "PUBLIC_BASE_URL", "http", "https")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
if environment == EnvironmentProduction && publicBaseURL.Scheme != "https" {
|
|
return Config{}, errors.New("PUBLIC_BASE_URL must use https in production")
|
|
}
|
|
|
|
siteAccessPassword := env["SITE_ACCESS_PASSWORD"]
|
|
roomDatabasePath, err := parseRoomDatabasePath(env["ROOM_DATABASE_PATH"])
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
stunURLs, err := parseStunURLList(env["STUN_URLS"], "STUN_URLS")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
natPredictionEnabled, err := parseBoolean(env["NAT_PREDICTION_ENABLED"], "NAT_PREDICTION_ENABLED")
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
if natPredictionEnabled && len(NATPredictionStunURLs(stunURLs)) == 0 {
|
|
return Config{}, errors.New("NAT_PREDICTION_ENABLED requires a STUN_URLS entry on UDP 3478")
|
|
}
|
|
if natPredictionEnabled &&
|
|
len(stunURLs)+protocol.MaxNatPredictionAuxiliaryStunURLs > protocol.MaxIceServerURLs {
|
|
return Config{}, fmt.Errorf(
|
|
"STUN_URLS must contain at most %d URLs when NAT_PREDICTION_ENABLED=true",
|
|
protocol.MaxIceServerURLs-protocol.MaxNatPredictionAuxiliaryStunURLs)
|
|
}
|
|
var stunListeners []string
|
|
if len(stunURLs) > 0 {
|
|
stunListeners, err = stunListenAddresses(env["STUN_LISTEN_HOST"], natPredictionEnabled)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
}
|
|
maxViewersPerRoom, err := parseBoundedInteger(env["MAX_VIEWERS_PER_ROOM"],
|
|
defaultMaxViewersPerRoom, "MAX_VIEWERS_PER_ROOM", 1, protocol.MaxViewersPerRoomLimit)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
endpointMediaCopyCapacity, err := parseBoundedInteger(env["ENDPOINT_MEDIA_COPY_CAPACITY"],
|
|
protocol.DefaultEndpointMediaCopyCapacity, "ENDPOINT_MEDIA_COPY_CAPACITY",
|
|
1, protocol.MaxEndpointMediaCopyCapacity)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
sfu, err := parseSFU(env)
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
// Buffer.byteLength is the UTF-8 length, which is len() in Go. The pattern
|
|
// already restricts the value to one byte per character.
|
|
if siteAccessPassword != "" &&
|
|
(!visibleASCIIPattern.MatchString(siteAccessPassword) ||
|
|
len(siteAccessPassword) < minSiteAccessPasswordBytes ||
|
|
len(siteAccessPassword) > maxSiteAccessPasswordBytes) {
|
|
return Config{}, fmt.Errorf("SITE_ACCESS_PASSWORD must contain %d to %d visible ASCII bytes",
|
|
minSiteAccessPasswordBytes, maxSiteAccessPasswordBytes)
|
|
}
|
|
if environment == EnvironmentProduction && len(stunURLs) == 0 {
|
|
return Config{}, errors.New("STUN is required in production")
|
|
}
|
|
|
|
allowedOrigins, err := parseOrigins(env["ALLOWED_ORIGINS"], Origin(publicBaseURL))
|
|
if err != nil {
|
|
return Config{}, err
|
|
}
|
|
|
|
return Config{
|
|
Env: environment,
|
|
Port: int(port),
|
|
ListenHost: listenHost,
|
|
PublicBaseURL: publicBaseURL,
|
|
AllowedOrigins: allowedOrigins,
|
|
SiteAccessPassword: siteAccessPassword,
|
|
RoomDatabasePath: roomDatabasePath,
|
|
MaxViewersPerRoom: int(maxViewersPerRoom),
|
|
EndpointMediaCopyCapacity: int(endpointMediaCopyCapacity),
|
|
SFU: sfu,
|
|
STUNURLs: stunURLs,
|
|
STUNListenAddresses: stunListeners,
|
|
NATPredictionEnabled: natPredictionEnabled,
|
|
}, nil
|
|
}
|
|
|
|
// parseEnvironment ports parseEnvironment for PIIK_ENV.
|
|
func parseEnvironment(env map[string]string) (Environment, error) {
|
|
value, present := env["PIIK_ENV"]
|
|
if !present {
|
|
return EnvironmentDevelopment, nil
|
|
}
|
|
switch Environment(value) {
|
|
case EnvironmentDevelopment, EnvironmentProduction:
|
|
return Environment(value), nil
|
|
}
|
|
return "", errors.New("PIIK_ENV must be development or production")
|
|
}
|
|
|
|
// parseBoolean ports parseBoolean; every caller used a false fallback.
|
|
func parseBoolean(value string, name string) (bool, error) {
|
|
switch value {
|
|
case "", "false":
|
|
return false, nil
|
|
case "true":
|
|
return true, nil
|
|
}
|
|
return false, fmt.Errorf("%s must be true or false", name)
|
|
}
|
|
|
|
// parsePositiveInteger ports parsePositiveInteger, including Number.isSafeInteger.
|
|
func parsePositiveInteger(value string, fallback int64, name string) (int64, error) {
|
|
if value == "" {
|
|
return fallback, nil
|
|
}
|
|
number, ok := jsNumber(value)
|
|
if !ok || number != math.Trunc(number) || number <= 0 || number > protocol.MaxSafeInteger {
|
|
return 0, fmt.Errorf("%s must be a positive integer", name)
|
|
}
|
|
return int64(number), nil
|
|
}
|
|
|
|
// parseBoundedInteger ports parseBoundedInteger.
|
|
func parseBoundedInteger(value string, fallback int64, name string, minimum, maximum int64) (int64, error) {
|
|
parsed, err := parsePositiveInteger(value, fallback, name)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if parsed < minimum || parsed > maximum {
|
|
return 0, fmt.Errorf("%s must be between %d and %d", name, minimum, maximum)
|
|
}
|
|
return parsed, nil
|
|
}
|
|
|
|
// Hosted defaults to durable room authority in its working directory. Explicit
|
|
// paths stay absolute so a deployment cannot relocate an existing database
|
|
// when its working directory changes. :memory: selects the in-memory RoomStore.
|
|
func parseRoomDatabasePath(value string) (string, error) {
|
|
path := strings.TrimFunc(value, protocol.IsJSWhitespace)
|
|
if path == "" {
|
|
return filepath.Abs("rooms.sqlite")
|
|
}
|
|
if path == ":memory:" {
|
|
return "", nil
|
|
}
|
|
if strings.ContainsRune(path, 0) || !filepath.IsAbs(path) {
|
|
return "", errors.New("ROOM_DATABASE_PATH must be an absolute file path")
|
|
}
|
|
return path, nil
|
|
}
|
|
|
|
func parseSFU(env map[string]string) (*SFUConfig, error) {
|
|
rawPort := strings.TrimSpace(env["SFU_UDP_PORT"])
|
|
if rawPort == "" {
|
|
return nil, nil
|
|
}
|
|
port, err := parseBoundedInteger(rawPort, 0, "SFU_UDP_PORT", 1, maxPort)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
listenHost := strings.TrimSpace(env["SFU_LISTEN_HOST"])
|
|
if listenHost == "" {
|
|
listenHost = "0.0.0.0"
|
|
}
|
|
if address, err := netip.ParseAddr(listenHost); err != nil || !address.Is4() {
|
|
return nil, errors.New("SFU_LISTEN_HOST must be an IPv4 address")
|
|
}
|
|
publicIP := strings.TrimSpace(env["SFU_PUBLIC_IP"])
|
|
if publicIP != "" {
|
|
if address, err := netip.ParseAddr(publicIP); err != nil || !address.Is4() {
|
|
return nil, errors.New("SFU_PUBLIC_IP must be an IPv4 address")
|
|
}
|
|
}
|
|
return &SFUConfig{
|
|
ListenHost: listenHost,
|
|
Port: int(port),
|
|
PublicIP: publicIP,
|
|
}, nil
|
|
}
|
|
|
|
// parseURLList ports parseUrlList.
|
|
func parseURLList(value string, name string) ([]string, error) {
|
|
if strings.TrimFunc(value, protocol.IsJSWhitespace) == "" {
|
|
return nil, nil
|
|
}
|
|
entries := strings.Split(value, ",")
|
|
list := make([]string, 0, len(entries))
|
|
for _, entry := range entries {
|
|
trimmed := strings.TrimFunc(entry, protocol.IsJSWhitespace)
|
|
if trimmed == "" {
|
|
return nil, fmt.Errorf("%s contains an empty URL", name)
|
|
}
|
|
list = append(list, trimmed)
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// parseStunURLList validates the bounded ordinary-STUN list.
|
|
func parseStunURLList(value string, name string) ([]string, error) {
|
|
values, err := parseURLList(value, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(values) > protocol.MaxIceServerURLs {
|
|
return nil, fmt.Errorf("%s must contain at most %d URLs", name, protocol.MaxIceServerURLs)
|
|
}
|
|
for _, value := range values {
|
|
if !protocol.ValidStunURLValue(value) {
|
|
return nil, fmt.Errorf("%s contains an invalid STUN URL", name)
|
|
}
|
|
}
|
|
return values, nil
|
|
}
|
|
|
|
// parseOrigins ports parseOrigins.
|
|
func parseOrigins(value string, fallback string) (map[string]struct{}, error) {
|
|
origins, err := parseURLList(value, "ALLOWED_ORIGINS")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(origins) == 0 {
|
|
origins = []string{fallback}
|
|
}
|
|
set := make(map[string]struct{}, len(origins))
|
|
for _, entry := range origins {
|
|
origin, err := toOrigin(entry)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
set[origin] = struct{}{}
|
|
}
|
|
return set, nil
|
|
}
|
|
|
|
// toOrigin validates one allowed HTTP(S) origin.
|
|
func toOrigin(value string) (string, error) {
|
|
parsed, err := url.Parse(value)
|
|
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
|
return "", errors.New("Allowed origins must be valid http or https URLs")
|
|
}
|
|
if parsed.Scheme != "http" && parsed.Scheme != "https" {
|
|
return "", errors.New("Allowed origins must use http or https")
|
|
}
|
|
return Origin(parsed), nil
|
|
}
|
|
|
|
// parseOriginURL validates an origin and returns the WHATWG-normalised URL
|
|
// (lowercase host, default port removed, path "/").
|
|
func parseOriginURL(value, name, scheme, alternative string) (*url.URL, error) {
|
|
parsed, err := url.Parse(value)
|
|
// net/url accepts relative references and opaque URLs that the WHATWG
|
|
// parser rejects (or, for "wss:host", repairs); require both parts.
|
|
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
|
return nil, fmt.Errorf("%s must be a valid %s or %s origin", name, scheme, alternative)
|
|
}
|
|
if parsed.Scheme != scheme && parsed.Scheme != alternative {
|
|
return nil, fmt.Errorf("%s must use %s or %s", name, scheme, alternative)
|
|
}
|
|
// WHATWG reports "/" for an empty path, while net/url leaves it "".
|
|
if hasUserinfo(parsed) || (parsed.Path != "" && parsed.Path != "/") ||
|
|
parsed.RawQuery != "" || parsed.Fragment != "" {
|
|
return nil, fmt.Errorf(
|
|
"%s must be an origin without credentials, path, query, or fragment", name)
|
|
}
|
|
return &url.URL{Scheme: parsed.Scheme, Host: normalizedHost(parsed), Path: "/"}, nil
|
|
}
|
|
|
|
// hasUserinfo ports `url.username || url.password`: WHATWG reports empty
|
|
// strings for "https://@host" and "https://:@host", where net/url reports a
|
|
// non-nil (but empty) Userinfo.
|
|
func hasUserinfo(parsed *url.URL) bool {
|
|
if parsed.User == nil {
|
|
return false
|
|
}
|
|
password, _ := parsed.User.Password()
|
|
return parsed.User.Username() != "" || password != ""
|
|
}
|
|
|
|
// Origin ports the WHATWG URL `origin` getter for the http/https/ws/wss schemes
|
|
// this package accepts: new URL("https://Example.com:443/").origin is
|
|
// "https://example.com". It is exported so app and cmd can compare request
|
|
// origins without a second copy of the rule.
|
|
func Origin(parsed *url.URL) string {
|
|
return parsed.Scheme + "://" + normalizedHost(parsed)
|
|
}
|
|
|
|
// normalizedHost lowercases the host, keeps an IPv6 literal bracketed and drops
|
|
// a port equal to the scheme default. Non-ASCII hosts are not IDNA-encoded, so
|
|
// they keep the form they were configured with.
|
|
func normalizedHost(parsed *url.URL) string {
|
|
host := strings.ToLower(parsed.Hostname())
|
|
if strings.Contains(host, ":") {
|
|
host = "[" + host + "]"
|
|
}
|
|
// WHATWG parses the port as a number, so "0443" and "443" are both default.
|
|
if number, err := strconv.Atoi(parsed.Port()); err == nil {
|
|
if port := strconv.Itoa(number); port != defaultSchemePort(parsed.Scheme) {
|
|
host += ":" + port
|
|
}
|
|
}
|
|
return host
|
|
}
|
|
|
|
func defaultSchemePort(scheme string) string {
|
|
switch scheme {
|
|
case "http", "ws":
|
|
return "80"
|
|
case "https", "wss":
|
|
return "443"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// decimalNumberPattern is the StrDecimalLiteral grammar Number(string) accepts.
|
|
var decimalNumberPattern = regexp.MustCompile(
|
|
`^[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$`)
|
|
|
|
// jsNumber ports Number(string). The second result is false where JavaScript
|
|
// yields NaN; values JavaScript renders as Infinity are reported as NaN too,
|
|
// because every caller rejects them through Number.isSafeInteger anyway.
|
|
func jsNumber(value string) (float64, bool) {
|
|
text := strings.TrimFunc(value, protocol.IsJSWhitespace)
|
|
if text == "" {
|
|
return 0, true // Number("") and Number(" ") are 0.
|
|
}
|
|
if len(text) > 2 && text[0] == '0' {
|
|
if base := numericBase(text[1]); base != 0 {
|
|
number, err := strconv.ParseUint(text[2:], base, 64)
|
|
return float64(number), err == nil
|
|
}
|
|
}
|
|
if !decimalNumberPattern.MatchString(text) {
|
|
return 0, false
|
|
}
|
|
number, err := strconv.ParseFloat(text, 64)
|
|
return number, err == nil
|
|
}
|
|
|
|
func numericBase(marker byte) int {
|
|
switch marker {
|
|
case 'x', 'X':
|
|
return 16
|
|
case 'o', 'O':
|
|
return 8
|
|
case 'b', 'B':
|
|
return 2
|
|
}
|
|
return 0
|
|
}
|