Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: New Safari does not handle compressed websockets. #55

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export interface Hook {
}

/**
* The DOM management interace. This allows external JS libraries to
* The DOM management interface. This allows external JS libraries to
* interop with Live.
*/
export interface DOM {
Expand Down
11 changes: 3 additions & 8 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ type BaseEngine struct {
}

// NewBaseEngine creates a new base engine.
func NewBaseEngine(h Handler, configs ...EngineConfig) *BaseEngine {
e := &BaseEngine{
func NewBaseEngine(h Handler) *BaseEngine {
const maxUploadSize = 100 * 1024 * 1024
return &BaseEngine{
broadcastLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
broadcastHandler: func(ctx context.Context, h Engine, msg Event) {
h.self(ctx, nil, msg)
Expand All @@ -94,12 +95,6 @@ func NewBaseEngine(h Handler, configs ...EngineConfig) *BaseEngine {
MaxUploadSize: 100 * 1024 * 1024,
handler: h,
}
for _, conf := range configs {
if err := conf(e); err != nil {
log.Println("warning:", fmt.Errorf("could not apply config to engine: %w", err))
}
}
return e
}

func (e *BaseEngine) Handler(hand Handler) {
Expand Down
35 changes: 31 additions & 4 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/gorilla/sessions"
Expand All @@ -35,16 +36,33 @@ type HttpSessionStore interface {

// HttpEngine serves live for net/http.
type HttpEngine struct {
sessionStore HttpSessionStore
acceptOptions *websocket.AcceptOptions
sessionStore HttpSessionStore
*BaseEngine
}

// WithWebsocketAcceptOptions apply websocket accept options to the HTTP engine.
func WithWebsocketAcceptOptions(options *websocket.AcceptOptions) EngineConfig {
return func(e Engine) error {
if httpEngine, ok := e.(*HttpEngine); ok {
httpEngine.acceptOptions = options
}
return nil
}
}

// NewHttpHandler returns the net/http handler for live.
func NewHttpHandler(store HttpSessionStore, handler Handler, configs ...EngineConfig) *HttpEngine {
return &HttpEngine{
e := &HttpEngine{
sessionStore: store,
BaseEngine: NewBaseEngine(handler, configs...),
BaseEngine: NewBaseEngine(handler),
}
for _, conf := range configs {
if err := conf(e); err != nil {
log.Println("warning:", fmt.Errorf("could not apply config to engine: %w", err))
}
}
return e
}

// ServeHTTP serves this handler.
Expand Down Expand Up @@ -267,7 +285,16 @@ func (h *HttpEngine) serveWS(ctx context.Context, w http.ResponseWriter, r *http
return
}

c, err := websocket.Accept(w, r, nil)
// https://github.com/nhooyr/websocket/issues/218
// https://github.com/gorilla/websocket/issues/731
if strings.Contains(r.UserAgent(), "Safari") {
if h.acceptOptions == nil {
h.acceptOptions = &websocket.AcceptOptions{}
}
h.acceptOptions.CompressionMode = websocket.CompressionDisabled
}

c, err := websocket.Accept(w, r, h.acceptOptions)
if err != nil {
h.Error()(ctx, err)
return
Expand Down