feat: init

This commit is contained in:
orion 2024-04-03 18:40:34 -05:00
parent 41b649db58
commit 1b43507f25
Signed by: orion
GPG Key ID: 6D4165AE4C928719
9 changed files with 1105 additions and 27 deletions

View File

@ -1,3 +1,2 @@
bun 1.0.11 bun 1.1.0
purescript 0.15.12 purescript 0.15.16-0
nodejs 20.9.0

BIN
bun.lockb

Binary file not shown.

1017
spago.lock Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,24 +1,16 @@
package: package:
name: bun-serve
build: build:
strict: true strict: true
pedantic_packages: true pedanticPackages: true
dependencies: dependencies:
- prelude
- aff - aff
- aff-promise
- effect - effect
- either - exceptions
- maybe - foreign
- foldable-traversable - prelude
- console
- newtype
- strings
- stringutils
- transformers
- tuples
- typelevel-prelude
name: project
workspace: workspace:
extra_packages: {} extraPackages: {}
package_set: packageSet:
url: https://raw.githubusercontent.com/purescript/package-sets/psc-0.15.10-20230930/packages.json registry: 50.5.0
hash: sha256-nTsd44o7/hrTdk0c6dh0wyBqhFFDJJIeKdQU6L1zv/A=

View File

@ -0,0 +1,3 @@
module Bun.Serve.Request where
foreign import data Request :: Type

View File

@ -0,0 +1,3 @@
module Bun.Serve.Response where
foreign import data Response :: Type

21
src/Bun.Serve.js Normal file
View File

@ -0,0 +1,21 @@
import Bun from 'bun'
/**
* @typedef
* {{
* error?: (_: Error) => unknown,
* }}
* ServeOptionsExtra
*/
/** @type {(mapErr: (_a: ServeOptionsExtra['error']) => Bun.ServeOptions['error']) => (opts: Bun.ServeOptions & ServeOptionsExtra) => Bun.ServeOptions} */
export const __mapOptError = mapErr => opts => {
if ('error' in opts && opts['error']) {
opts.error = mapErr(opts.error)
}
return opts
}
/** @type {(opts: Bun.ServeOptions) => (fetch: (r: Request) => (s: Bun.Server) => () => Promise<Response>) => () => Bun.Server} */
export const __serve = opts => fetch => () =>
Bun.serve({ ...opts, fetch: (req, server) => fetch(req)(server)() })

50
src/Bun.Serve.purs Normal file
View File

@ -0,0 +1,50 @@
module Bun.Serve where
import Prelude
import Bun.Serve.Request (Request)
import Bun.Serve.Response (Response)
import Control.Promise (Promise)
import Control.Promise as Promise
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Exception (Error)
import Effect.Unsafe (unsafePerformEffect)
import Foreign (Foreign, unsafeToForeign)
import Prim.Row (class Union)
foreign import data Server :: Type
foreign import __serve :: Foreign -> (Request -> Server -> Promise Unit) -> Effect Server
foreign import __mapOptError :: ((Error -> Aff Response) -> Error -> Promise Response) -> Foreign -> Foreign
type TLSOptionsRequired r = (key :: String, cert :: String | r)
type TLSOptionsExtra r = (ca :: String, passphrase :: String | r)
type Options tls =
( hostname :: String
, port :: Number
, development :: Boolean
, error :: Error -> Aff Response
, maxRequestBodySize :: Int
, lowMemoryMode :: Boolean
, tls :: Record tls
)
class IsTLSOptions :: Row Type -> Row Type -> Row Type -> Constraint
class (Union r () (TLSOptionsRequired notrequired), Union notrequired missing (TLSOptionsExtra ())) <= IsTLSOptions r missing notrequired | r -> missing notrequired
instance (Union r () (TLSOptionsRequired notrequired), Union notrequired missing (TLSOptionsExtra ())) => IsTLSOptions r missing notrequired
serve
:: forall opts optsmissing tls tlsm tlsnr
. Union opts optsmissing (Options tls)
=> IsTLSOptions tls tlsm tlsnr
=> Record opts
-> (Request -> Server -> Aff Unit)
-> Effect Server
serve opts fetch = do
let
fetch' r s = unsafePerformEffect $ Promise.fromAff $ fetch r s
mapErr f = unsafePerformEffect <<< Promise.fromAff <<< f
opts' = __mapOptError mapErr $ unsafeToForeign opts
__serve opts' fetch'

View File

@ -1,7 +0,0 @@
module Main where
import Prelude
import Effect (Effect)
main :: Effect Unit
main = pure unit