| 1 | package protocol |
| 2 | |
| 3 | // Frozen wire limits. These constants are part of the protocol contract: |
| 4 | // they are frozen into the generated schema document and changing one changes |
| 5 | // the schema hash. |
| 6 | const ( |
| 7 | // FrameBytes caps one JSON-RPC frame on the extension transport. |
| 8 | FrameBytes = 8 << 20 |
| 9 | // ExternalizeFieldBytes is the threshold above which an externalizable |
| 10 | // payload must move into a content ref instead of traveling inline. |
| 11 | ExternalizeFieldBytes = 64 << 10 |
| 12 | // ContentRefChunkBytes caps one host/content/read chunk. |
| 13 | ContentRefChunkBytes = 256 << 10 |
| 14 | // ContentRefObjectBytes caps one externalized object. |
| 15 | ContentRefObjectBytes = 8 << 20 |
| 16 | ) |
| 17 | |
| 18 | // Limits is the JSON-serializable form of the frozen constants, embedded in |
| 19 | // the generated schema document. |
| 20 | type Limits struct { |
| 21 | FrameBytes int `json:"frameBytes"` |
| 22 | ExternalizeFieldBytes int `json:"externalizeFieldBytes"` |
| 23 | ContentRefChunkBytes int `json:"contentRefChunkBytes"` |
| 24 | ContentRefObjectBytes int `json:"contentRefObjectBytes"` |
| 25 | } |
| 26 | |
| 27 | // FrozenLimits returns the immutable limit set for this protocol build. |
| 28 | func FrozenLimits() Limits { |
| 29 | return Limits{ |
| 30 | FrameBytes: FrameBytes, |
| 31 | ExternalizeFieldBytes: ExternalizeFieldBytes, |
| 32 | ContentRefChunkBytes: ContentRefChunkBytes, |
| 33 | ContentRefObjectBytes: ContentRefObjectBytes, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // ValidateFrameSize rejects a frame that exceeds the frozen transport limit. |
| 38 | // The error is the frozen frame_too_large *ProtocolError so transports answer |
| 39 | // with the structured wire reason instead of an ad-hoc string. |
| 40 | func ValidateFrameSize(bytes int) error { |
| 41 | if bytes < 0 || bytes > FrameBytes { |
| 42 | return MustProtocolError(ErrFrameTooLarge) |
| 43 | } |
| 44 | return nil |
| 45 | } |
| 46 | |
| 47 | // RequiresExternalization reports whether an inline externalizable payload |
| 48 | // exceeds the frozen threshold and must move into a content ref. |
| 49 | func RequiresExternalization(payloadBytes int) bool { |
| 50 | return payloadBytes > ExternalizeFieldBytes |
| 51 | } |
| 52 |