Skip to content

Commit d4f4285

Browse files
authored
fix: New Safari does not handle compressed websockets. (#55)
Refs: - tilt-dev/tilt#4746 - gorilla/websocket#731 - coder/websocket#218
1 parent d21cd89 commit d4f4285

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ export interface Hook {
397397
}
398398

399399
/**
400-
* The DOM management interace. This allows external JS libraries to
400+
* The DOM management interface. This allows external JS libraries to
401401
* interop with Live.
402402
*/
403403
export interface DOM {

engine.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ type BaseEngine struct {
8383
}
8484

8585
// NewBaseEngine creates a new base engine.
86-
func NewBaseEngine(h Handler, configs ...EngineConfig) *BaseEngine {
87-
e := &BaseEngine{
86+
func NewBaseEngine(h Handler) *BaseEngine {
87+
const maxUploadSize = 100 * 1024 * 1024
88+
return &BaseEngine{
8889
broadcastLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
8990
broadcastHandler: func(ctx context.Context, h Engine, msg Event) {
9091
h.self(ctx, nil, msg)
@@ -94,12 +95,6 @@ func NewBaseEngine(h Handler, configs ...EngineConfig) *BaseEngine {
9495
MaxUploadSize: 100 * 1024 * 1024,
9596
handler: h,
9697
}
97-
for _, conf := range configs {
98-
if err := conf(e); err != nil {
99-
log.Println("warning:", fmt.Errorf("could not apply config to engine: %w", err))
100-
}
101-
}
102-
return e
10398
}
10499

105100
func (e *BaseEngine) Handler(hand Handler) {

http.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"os"
1414
"path/filepath"
15+
"strings"
1516
"time"
1617

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

3637
// HttpEngine serves live for net/http.
3738
type HttpEngine struct {
38-
sessionStore HttpSessionStore
39+
acceptOptions *websocket.AcceptOptions
40+
sessionStore HttpSessionStore
3941
*BaseEngine
4042
}
4143

44+
// WithWebsocketAcceptOptions apply websocket accept options to the HTTP engine.
45+
func WithWebsocketAcceptOptions(options *websocket.AcceptOptions) EngineConfig {
46+
return func(e Engine) error {
47+
if httpEngine, ok := e.(*HttpEngine); ok {
48+
httpEngine.acceptOptions = options
49+
}
50+
return nil
51+
}
52+
}
53+
4254
// NewHttpHandler returns the net/http handler for live.
4355
func NewHttpHandler(store HttpSessionStore, handler Handler, configs ...EngineConfig) *HttpEngine {
44-
return &HttpEngine{
56+
e := &HttpEngine{
4557
sessionStore: store,
46-
BaseEngine: NewBaseEngine(handler, configs...),
58+
BaseEngine: NewBaseEngine(handler),
4759
}
60+
for _, conf := range configs {
61+
if err := conf(e); err != nil {
62+
log.Println("warning:", fmt.Errorf("could not apply config to engine: %w", err))
63+
}
64+
}
65+
return e
4866
}
4967

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

270-
c, err := websocket.Accept(w, r, nil)
288+
// https://github.com/nhooyr/websocket/issues/218
289+
// https://github.com/gorilla/websocket/issues/731
290+
if strings.Contains(r.UserAgent(), "Safari") {
291+
if h.acceptOptions == nil {
292+
h.acceptOptions = &websocket.AcceptOptions{}
293+
}
294+
h.acceptOptions.CompressionMode = websocket.CompressionDisabled
295+
}
296+
297+
c, err := websocket.Accept(w, r, h.acceptOptions)
271298
if err != nil {
272299
h.Error()(ctx, err)
273300
return

0 commit comments

Comments
 (0)