0 | module Node.HTTP2.CreateServer
  1 |
  2 | import Node
  3 | import Node.HTTP2.Module
  4 | import Node.Internal.Support
  5 | import public Node.Net.CreateServer
  6 |
  7 | public export
  8 | data PaddingStrategy
  9 |   = None
 10 |   | Max
 11 |   | Aligned
 12 |
 13 | %foreign "node:lambda: (http2) => http2.constants.PADDING_STRATEGY_NONE"
 14 | ffi_paddingStrategy_None : {auto http : HTTP2Module} -> Node PaddingStrategy
 15 |
 16 | %foreign "node:lambda: (http2) => http2.constants.PADDING_STRATEGY_MAX"
 17 | ffi_paddingStrategy_Max : {auto http : HTTP2Module} -> Node PaddingStrategy
 18 |
 19 | %foreign "node:lambda: (http2) => http2.constants.PADDING_STRATEGY_ALIGNED"
 20 | ffi_paddingStrategy_Aligned : {auto http : HTTP2Module} -> Node PaddingStrategy
 21 |
 22 | export
 23 | convertPaddingStrategy : {auto http : HTTP2Module} -> PaddingStrategy -> Node PaddingStrategy
 24 | convertPaddingStrategy = \case
 25 |   None => ffi_paddingStrategy_None
 26 |   Max => ffi_paddingStrategy_Max
 27 |   Aligned => ffi_paddingStrategy_Aligned
 28 |
 29 | public export
 30 | record Settings where
 31 |   constructor MkSettings
 32 |   headerTableSize: Int
 33 |   enablePush: Bool
 34 |   initialWindowSize: Int
 35 |   maxFrameSize: Int
 36 |   maxConcurrentStreams: Double
 37 |   maxHeaderListSize: Int
 38 |   enableConnectProtocol: Bool
 39 |
 40 | export
 41 | defaultSettings : Settings
 42 | defaultSettings = MkSettings
 43 |   { headerTableSize = 4096
 44 |   , enablePush = True
 45 |   , initialWindowSize = 65535
 46 |   , maxFrameSize = 16384
 47 |   , maxConcurrentStreams = 4294967295.0
 48 |   , maxHeaderListSize = 65535
 49 |   , enableConnectProtocol = False
 50 |   }
 51 |
 52 | %foreign """
 53 |   node:lambda:
 54 |   ( headerTableSize
 55 |   , enablePush
 56 |   , initialWindowSize
 57 |   , maxFrameSize
 58 |   , maxConcurrentStreams
 59 |   , maxHeaderListSize
 60 |   , enableConnectProtocol
 61 |   ) => _keepDefined({
 62 |     headerTableSize,
 63 |     enablePush: _bool(enablePush) && undefined, // -- TODO check why value true not accepted and causing HTTP2 session errrors?
 64 |     initialWindowSize,
 65 |     maxFrameSize,
 66 |     maxConcurrentStreams,
 67 |     maxHeaderListSize,
 68 |     enableConnectProtocol: _bool(enableConnectProtocol)
 69 |   })
 70 |   """
 71 | ffi_convertSettings :
 72 |   (headerTableSize: Int) ->
 73 |   (enablePush: Bool) ->
 74 |   (initialWindowSize: Int) ->
 75 |   (maxFrameSize: Int) ->
 76 |   (maxConcurrentStreams: Double) ->
 77 |   (maxHeaderListSize: Int) ->
 78 |   (enableConnectProtocol: Bool) ->
 79 |   Node Settings
 80 |
 81 | export
 82 | convertSettings : Settings -> Node Settings
 83 | convertSettings s = ffi_convertSettings
 84 |   s.headerTableSize
 85 |   s.enablePush
 86 |   s.initialWindowSize
 87 |   s.maxFrameSize
 88 |   s.maxConcurrentStreams
 89 |   s.maxHeaderListSize
 90 |   s.enableConnectProtocol
 91 |
 92 | public export
 93 | record Options where
 94 |   constructor MkOptions
 95 |   maxDeflateDynamicTableSize: Int
 96 |   maxSettings: Int
 97 |   maxSessionMemory: Int
 98 |   maxHeaderListPairs: Int
 99 |   maxOutstandingPings: Int
100 |   maxSendHeaderBlockLength: Maybe Int
101 |   paddingStrategy: PaddingStrategy
102 |   peerMaxConcurrentStreams: Int
103 |   maxSessionInvalidFrames: Int 
104 |   maxSessionRejectedStreams: Int
105 |   settings: Settings
106 |   unknownProtocolTimeout: Int
107 |
108 | export
109 | defaultOptions : HTTP2.CreateServer.Options
110 | defaultOptions = MkOptions
111 |   { maxDeflateDynamicTableSize = 4096
112 |   , maxSettings = 32
113 |   , maxSessionMemory = 10
114 |   , maxHeaderListPairs = 128
115 |   , maxOutstandingPings = 10
116 |   , maxSendHeaderBlockLength = Nothing
117 |   , paddingStrategy = None
118 |   , peerMaxConcurrentStreams = 100
119 |   , maxSessionInvalidFrames = 1000
120 |   , maxSessionRejectedStreams = 100
121 |   , settings = defaultSettings
122 |   , unknownProtocolTimeout = 10000
123 |   }
124 |
125 | %foreign """
126 |   node:lambda:
127 |   ( maxDeflateDynamicTableSize
128 |   , maxSettings
129 |   , maxSessionMemory
130 |   , maxHeaderListPairs
131 |   , maxOutstandingPings
132 |   , maxSendHeaderBlockLength
133 |   , paddingStrategy
134 |   , peerMaxConcurrentStreams
135 |   , maxSessionInvalidFrames
136 |   , maxSessionRejectedStreams
137 |   , settings
138 |   , unknownProtocolTimeout
139 |   ) => _keepDefined({
140 |     maxDeflateDynamicTableSize,
141 |     maxSettings,
142 |     maxSessionMemory,
143 |     maxHeaderListPairs,
144 |     maxOutstandingPings,
145 |     maxSendHeaderBlockLength: _maybe(maxSendHeaderBlockLength),
146 |     paddingStrategy,
147 |     peerMaxConcurrentStreams,
148 |     maxSessionInvalidFrames,
149 |     maxSessionRejectedStreams,
150 |     settings,
151 |     unknownProtocolTimeout
152 |   })
153 |   """
154 | ffi_convertOptions :
155 |   (maxDeflateDynamicTableSize: Int) ->
156 |   (maxSettings: Int) ->
157 |   (maxSessionMemory: Int) ->
158 |   (maxHeaderListPairs: Int) ->
159 |   (maxOutstandingPings: Int) ->
160 |   (maxSendHeaderBlockLength: Maybe Int) ->
161 |   (paddingStrategy: Node PaddingStrategy) ->
162 |   (peerMaxConcurrentStreams: Int) ->
163 |   (maxSessionInvalidFrames: Int ) ->
164 |   (maxSessionRejectedStreams: Int) ->
165 |   (settings: Node Settings) ->
166 |   (unknownProtocolTimeout: Int) ->
167 |   Node HTTP2.CreateServer.Options
168 |
169 | export
170 | convertOptions : {auto http : HTTP2Module} -> HTTP2.CreateServer.Options -> Node HTTP2.CreateServer.Options
171 | convertOptions o = ffi_convertOptions
172 |   o.maxDeflateDynamicTableSize
173 |   o.maxSettings
174 |   o.maxSessionMemory
175 |   o.maxHeaderListPairs
176 |   o.maxOutstandingPings
177 |   o.maxSendHeaderBlockLength
178 |   (convertPaddingStrategy o.paddingStrategy)
179 |   o.peerMaxConcurrentStreams
180 |   o.maxSessionInvalidFrames
181 |   o.maxSessionRejectedStreams
182 |   (convertSettings o.settings)
183 |   o.unknownProtocolTimeout
184 |
185 | namespace Command
186 |
187 |   public export
188 |   record Options where
189 |     constructor MkOptions
190 |     server: Node.HTTP2.CreateServer.Options
191 |     net: Node.Net.CreateServer.Options
192 |
193 |   export
194 |   defaultOptions : Command.Options
195 |   defaultOptions = MkOptions
196 |     { server = defaultOptions
197 |     , net = defaultOptions
198 |     }
199 |
200 |   %foreign "node:lambda: (server, net) => ({...net, ...server})"
201 |   ffi_convertOptions :
202 |     (server : Node HTTP2.CreateServer.Options)
203 |     -> (net : Node Net.CreateServer.Options)
204 |     -> Node Command.Options
205 |
206 |   export
207 |   convertOptions : {auto http : HTTP2Module} -> Command.Options -> Node Command.Options
208 |   convertOptions o = Command.ffi_convertOptions
209 |     (convertOptions o.server)
210 |     (convertOptions o.net)
211 |
212 |