pauldijou / elm-server

Low level util fonctions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

elm-server

⚠️ This is probably not what you should be using except if you want to write you own server implementation in Elm. Those are low level types and functions to help writing server libs.

If you need to run Elm on server-side, check one of the following implementations:

Tradeoff

To ease syntax and boilerplate and improve user experience, this lib can only run one server at a time per Elm worker. This is why you can only start once or again after stopping (start task will fail if you call it again) and you do not have to specify which server you are listening to when subscribing.

Implement your own server

Provide a server creator

module YouCustomServer exposing (..)

import Json.Decode exposing (Decoder)
import Server

type alias Options = {
  -- You should put here any options needed to create a raw server
}

create: Options -> Task Error Server.Server
create options =
  { implementation = Native.YouCustomServer.create options
  , request =
    { decoder = requestDecoder
    }
  -- other stuff...
  }

requestDecoder: Decoder Server.Request
requestDecoder =
  -- you custom request decoder

ServerImplementation

You can put whatever you want in your

function start({
  onRequest: function (replier, request) { /* elm-server internals */ }
}) {
  // return Promise<ServerImplementation>
}

function stop() {
  // return Promise
}

Important concerns

This will be fixed on Elm 0.19 but as far as 0.18 go, if a Json.Decode.Decoder fails, it will call JSON.stringify on the value it tried to decode. Since it is JavaScript, the value might be recursive and it will crash at runtime. To prevent that, you need to add a toJSON function on any opaque type (ServerImplementation, Replier) and your request object before parsing it.

License

This software is licensed under the Apache 2 license, quoted below.

Copyright Paul Dijou (http://pauldijou.fr).

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Low level util fonctions

License:Apache License 2.0


Languages

Language:Elm 87.9%Language:JavaScript 12.1%