| 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package jsonrpc2 is a minimal implementation of the JSON RPC 2 spec. |
| 6 | // https://www.jsonrpc.org/specification |
| 7 | // It is intended to be compatible with other implementations at the wire level. |
| 8 | package jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ |
| 9 | |
| 10 | import ( |
| 11 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 12 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | // ErrIdleTimeout is returned when serving timed out waiting for new connections. |
| 17 | ErrIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrIdleTimeout. = errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("timed out waiting for new connections") |
| 18 | |
| 19 | // ErrNotHandled is returned from a Handler or Preempter to indicate it did |
| 20 | // not handle the request. |
| 21 | // |
| 22 | // If a Handler returns ErrNotHandled, the server replies with |
| 23 | // ErrMethodNotFound. |
| 24 | ErrNotHandledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrNotHandled. = errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("JSON RPC not handled") |
| 25 | |
| 26 | // ErrAsyncResponse is returned from a handler to indicate it will generate a |
| 27 | // response asynchronously. |
| 28 | // |
| 29 | // ErrAsyncResponse must not be returned for notifications, |
| 30 | // which do not receive responses. |
| 31 | ErrAsyncResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrAsyncResponse. = errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("JSON RPC asynchronous response") |
| 32 | ) |
| 33 | |
| 34 | // Preempter handles messages on a connection before they are queued to the main |
| 35 | // handler. |
| 36 | // Primarily this is used for cancel handlers or notifications for which out of |
| 37 | // order processing is not an issue. |
| 38 | type Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter# interface { |
| 39 | // Preempt is invoked for each incoming request before it is queued for handling. |
| 40 | // |
| 41 | // If Preempt returns ErrNotHandled, the request will be queued, |
| 42 | // and eventually passed to a Handle call. |
| 43 | // |
| 44 | // Otherwise, the result and error are processed as if returned by Handle. |
| 45 | // |
| 46 | // Preempt must not block. (The Context passed to it is for Values only.) |
| 47 | Preemptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter#Preempt.(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, req *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (result any, err error) |
| 48 | } |
| 49 | |
| 50 | // A PreempterFunc implements the Preempter interface for a standalone Preempt function. |
| 51 | type PreempterFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/PreempterFunc# func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, req *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (any, error) |
| 52 | |
| 53 | func (f PreempterFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/PreempterFunc#) Preemptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/PreempterFunc#Preempt().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, req *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (any, error) { |
| 54 | return f(ctx, req) |
| 55 | } |
| 56 | |
| 57 | var _ Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter# = PreempterFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/PreempterFunc#(nil) |
| 58 | |
| 59 | // Handler handles messages on a connection. |
| 60 | type Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Handler# interface { |
| 61 | // Handle is invoked sequentially for each incoming request that has not |
| 62 | // already been handled by a Preempter. |
| 63 | // |
| 64 | // If the Request has a nil ID, Handle must return a nil result, |
| 65 | // and any error may be logged but will not be reported to the caller. |
| 66 | // |
| 67 | // If the Request has a non-nil ID, Handle must return either a |
| 68 | // non-nil, JSON-marshalable result, or a non-nil error. |
| 69 | // |
| 70 | // The Context passed to Handle will be canceled if the |
| 71 | // connection is broken or the request is canceled or completed. |
| 72 | // (If Handle returns ErrAsyncResponse, ctx will remain uncanceled |
| 73 | // until either Cancel or Respond is called for the request's ID.) |
| 74 | Handlescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Handler#Handle.(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, req *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (result any, err error) |
| 75 | } |
| 76 | |
| 77 | type defaultHandlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/defaultHandler# struct{} |
| 78 | |
| 79 | func (defaultHandlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/defaultHandler#) Preemptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/defaultHandler#Preempt().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (any, error) { |
| 80 | return nil, ErrNotHandledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrNotHandled. |
| 81 | } |
| 82 | |
| 83 | func (defaultHandlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/defaultHandler#) Handlescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/defaultHandler#Handle().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (any, error) { |
| 84 | return nil, ErrNotHandledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrNotHandled. |
| 85 | } |
| 86 | |
| 87 | // A HandlerFunc implements the Handler interface for a standalone Handle function. |
| 88 | type HandlerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/HandlerFunc# func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, req *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (any, error) |
| 89 | |
| 90 | func (f HandlerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/HandlerFunc#) Handlescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/HandlerFunc#Handle().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, req *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#) (any, error) { |
| 91 | return f(ctx, req) |
| 92 | } |
| 93 | |
| 94 | var _ Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Handler# = HandlerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/HandlerFunc#(nil) |
| 95 | |
| 96 | // async is a small helper for operations with an asynchronous result that you |
| 97 | // can wait for. |
| 98 | type asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async# struct { |
| 99 | readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#ready. chan struct{} // closed when done |
| 100 | firstErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#firstErr. chan error // 1-buffered; contains either nil or the first non-nil error |
| 101 | } |
| 102 | |
| 103 | func newAsyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/newAsync().() *asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async# { |
| 104 | var a asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async# |
| 105 | a.readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#ready. = make(chan struct{}) |
| 106 | a.firstErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#firstErr. = make(chan error, 1) |
| 107 | a.firstErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#firstErr. <- nil |
| 108 | return &a |
| 109 | } |
| 110 | |
| 111 | func (a *asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#) donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#done().() { |
| 112 | close(a.readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#ready.) |
| 113 | } |
| 114 | |
| 115 | func (a *asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#) waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#wait().() error { |
| 116 | <-a.readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#ready. |
| 117 | err := <-a.firstErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#firstErr. |
| 118 | a.firstErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#firstErr. <- err |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | func (a *asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#) setErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#setError().(err error) { |
| 123 | storedErr := <-a.firstErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#firstErr. |
| 124 | if storedErr == nil { |
| 125 | storedErr = err |
| 126 | } |
| 127 | a.firstErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#firstErr. <- storedErr |
| 128 | } |