loadJavaScript loads the JavaScript source from an external file or and HTTP/HTTPS endpoint. If the specified path does not qualify for a valid file or an URI, it returns the input path as-is with the assumption that it is an inline JavaScript source. Returns error if there is any failure in reading
(ctx context.Context, path string, insecureSkipVerify bool)
| 555 | // as-is with the assumption that it is an inline JavaScript source. Returns error if there is |
| 556 | // any failure in reading the JavaScript file or URI. |
| 557 | func loadJavaScript(ctx context.Context, path string, insecureSkipVerify bool) (js string, err error) { |
| 558 | rc, err := readFromPath(ctx, path, insecureSkipVerify) |
| 559 | if errors.Is(err, ErrPathNotFound) { |
| 560 | // If rc is nil and readFromPath returns no error, treat the |
| 561 | // the given path as an inline JavaScript and return it as-is. |
| 562 | return path, nil |
| 563 | } |
| 564 | if err != nil { |
| 565 | if !insecureSkipVerify { |
| 566 | var unkAuthErr x509.UnknownAuthorityError |
| 567 | if errors.As(err, &unkAuthErr) { |
| 568 | return "", fmt.Errorf("%w. TLS certificate failed verification. TLS verification "+ |
| 569 | "can be disabled using the unsupported \"remote_config_tls_skip_verify\" option", err) |
| 570 | } |
| 571 | return "", err |
| 572 | } |
| 573 | return "", err |
| 574 | } |
| 575 | defer func() { _ = rc.Close() }() |
| 576 | src, err := io.ReadAll(rc) |
| 577 | if err != nil { |
| 578 | return "", err |
| 579 | } |
| 580 | return string(src), nil |
| 581 | } |
| 582 | |
| 583 | // JSLoadType represents a specific JavaScript load type. |
| 584 | // It is used to uniquely identify any potential errors during JavaScript load. |