├── .github └── workflows │ └── workflow.yml ├── .gitignore ├── README.md ├── ci └── compile.ts ├── commitlint.config.js ├── package-lock.json ├── package.json ├── src ├── fxmanifest.lua └── server │ ├── classes │ ├── HTTP.lua │ ├── Request.lua │ ├── Response.lua │ └── Router.lua │ ├── server.lua │ └── util │ ├── json.lua │ └── pattern.lua └── tsconfig.json /.github/workflows/workflow.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - master 5 | 6 | jobs: 7 | release: 8 | if: github.event_name == 'push' && github.ref == 'refs/heads/master' 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v2 13 | - name: Setup Node.js 14 | uses: actions/setup-node@v1 15 | with: 16 | node-version: 12.x 17 | - name: Release 18 | run: | 19 | npm i semantic-release @semantic-release/exec @semantic-release/release-notes-generator -g 20 | npm i 21 | npm run release-ci 22 | env: 23 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | .vscode 4 | .idea 5 | dist/ 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #FiveM Lua HTTP Wrapper 2 | 3 | ### Create endpoints in your resources with ease! 4 | 5 | ### Features 6 | - Router 7 | - Automatic JSON data handling 8 | - Pattern based routes 9 | 10 | ### Examples 11 | 12 | Simple example 13 | 14 | ```lua 15 | local r = Router.new() 16 | r:Get("/", function(req, res) 17 | res:Send("A Simple response!") 18 | end) 19 | 20 | Server.use("", r) 21 | Server.listen() 22 | ``` 23 | 24 | You can also use return values, an alternative to the above: 25 | ```lua 26 | local r = Router.new() 27 | r:Get("/", function(req, res) 28 | return 200, "A simple response!" 29 | end) 30 | 31 | Server.use("", r) 32 | Server.listen() 33 | ``` 34 | 35 | Pattern Based Routes 36 | ```lua 37 | local r = Router.new() 38 | r:Get("/:steamid", function(req, res) 39 | local steamid = req:Param("steamid") 40 | res.send("The steam id was " .. steamid) 41 | end) 42 | 43 | Server.use("/users", r) 44 | Server.listen() 45 | ``` 46 | 47 | ``` 48 | curl -X GET http://localhost:30120/resource-name/users/steam:9494000210 49 | The steam id was steam:9494000210 50 | ``` 51 | 52 | -------------------------------------------------------------------------------- /ci/compile.ts: -------------------------------------------------------------------------------- 1 | import {Glob} from 'glob' 2 | import * as fs from 'fs' 3 | import * as path from 'path' 4 | 5 | process.env.NEXT_VERSION = process.argv[2] || "0.0.0" 6 | const getFileContents = (filePath: string): Promise => { 7 | return new Promise((resolve, reject) => { 8 | fs.readFile(filePath, (err, contents) => { 9 | if (err) return err; 10 | resolve(contents) 11 | }) 12 | }) 13 | } 14 | 15 | const outDir = process.argv[3] || './dist/http-wrapper' 16 | 17 | type Context = "client" | "server" | "shared" 18 | 19 | class LuaBuilder { 20 | 21 | constructor(public readonly contexts: Context[], public readonly ignores?: string[]) { 22 | if (ignores === undefined) { 23 | this.ignores = [] 24 | } 25 | } 26 | 27 | public compile(): Promise { 28 | return new Promise(async (resolve, reject) => { 29 | for(const context of this.contexts) { 30 | const contextBuff = await this.getContextBuffer(context) 31 | await this.writeContext(context, contextBuff) 32 | } 33 | fs.readFile(`./src/fxmanifest.lua`, 'utf8', (err, manifest) => { 34 | const newManifest = manifest.replace(/^(version )'(.*)'$/gm, `$1 '${process.env.NEXT_VERSION || '0.0.0'}'`) 35 | fs.writeFile(`${outDir}/fxmanifest.lua`, newManifest, () => { 36 | fs.writeFile(`./src/fxmanifest.lua`, newManifest, () => { 37 | resolve() 38 | }) 39 | }) 40 | }) 41 | }) 42 | } 43 | 44 | private isFileIgnored(name: string): boolean { 45 | const base = path.basename(name) 46 | const found = this.ignores.find(v => v.indexOf(base) > -1) 47 | return found !== undefined 48 | } 49 | 50 | private getContextBuffer(context: Context): Promise { 51 | return new Promise((resolve, reject) => { 52 | const glob = new Glob(`./src/${context}/**/*.lua`, {absolute: true}, async (err, matches) => { 53 | let output: Buffer[] = [] 54 | for (const file of matches) { 55 | if(file.indexOf("fxmanifest.lua") > -1 || this.isFileIgnored(file)) continue; 56 | 57 | const contents = await getFileContents(file) 58 | output.push(contents, Buffer.from("\n\n", "utf-8")) 59 | } 60 | resolve(Buffer.concat(output)) 61 | }) 62 | }) 63 | } 64 | 65 | private writeContext(context: string, buff: Buffer): Promise { 66 | return new Promise((resolve, reject) => { 67 | fs.mkdir(outDir, {recursive: true}, () => { 68 | fs.mkdir(`${outDir}/${context}`, {recursive: true}, () => { 69 | fs.writeFile(`${outDir}/${context}/${context}.lua`, buff, (err) => { 70 | resolve() 71 | }) 72 | }) 73 | }) 74 | }) 75 | } 76 | } 77 | 78 | const builder = new LuaBuilder(["server"], ["json.lua"]) 79 | builder.compile().then(() => { 80 | 81 | }) 82 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parserPreset: 'conventional-changelog-conventionalcommits', 3 | rules: { 4 | 'body-leading-blank': [1, 'always'], 5 | 'body-max-line-length': [2, 'always', 100], 6 | 'footer-leading-blank': [1, 'always'], 7 | 'footer-max-line-length': [2, 'always', 100], 8 | 'header-max-length': [2, 'always', 100], 9 | 'scope-case': [2, 'always', 'lower-case'], 10 | 'subject-case': [ 11 | 2, 12 | 'never', 13 | ['sentence-case', 'start-case', 'pascal-case', 'upper-case'] 14 | ], 15 | 'subject-empty': [2, 'never'], 16 | 'subject-full-stop': [2, 'never', '.'], 17 | 'type-case': [2, 'always', 'lower-case'], 18 | 'type-empty': [2, 'never'], 19 | 'type-enum': [ 20 | 2, 21 | 'always', 22 | [ 23 | 'build', 24 | 'chore', 25 | 'ci', 26 | 'docs', 27 | 'feat', 28 | 'fix', 29 | 'perf', 30 | 'refactor', 31 | 'revert', 32 | 'style', 33 | 'test' 34 | ] 35 | ] 36 | } 37 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fivem-http-wrapper", 3 | "scripts": { 4 | "release": "semantic-release --debug true", 5 | "release-ci": "semantic-release", 6 | "release-pre": "semantic-release -d", 7 | "build": "ts-node ci/compile.ts", 8 | "build-fivem": "ts-node ci/compile.ts 0 ../fxl" 9 | }, 10 | "devDependencies": { 11 | "semantic-release": "17.0.8", 12 | "@semantic-release/release-notes-generator": "^9.0.1", 13 | "@semantic-release/exec": "^5.0.0", 14 | "commitlint": "8.3.5", 15 | "@commitlint/cli": "^8.3.5", 16 | "@commitlint/config-conventional": "^8.3.4", 17 | "husky": "^4.2.5", 18 | "glob": "^7.1.6", 19 | "@types/glob": "^7.1.3", 20 | "typescript": "^4.1.3", 21 | "ts-node": "^9.1.1" 22 | }, 23 | "release": { 24 | "plugins": [ 25 | "@semantic-release/commit-analyzer", 26 | "@semantic-release/release-notes-generator", 27 | [ 28 | "@semantic-release/github", 29 | { 30 | "assets": [ 31 | { 32 | "path": "./dist/release.zip", 33 | "label": "Resource" 34 | } 35 | ] 36 | } 37 | ], 38 | [ 39 | "@semantic-release/exec", 40 | { 41 | "generateNotesCmd": "npm run --silent build ${nextRelease.version} && cd ./dist && zip -qq -r release.zip ./" 42 | } 43 | ] 44 | ], 45 | "tagFormat": "${version}" 46 | }, 47 | "husky": { 48 | "hooks": { 49 | "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/fxmanifest.lua: -------------------------------------------------------------------------------- 1 | fx_version 'bodacious' 2 | 3 | version '0.0.0' 4 | 5 | games { 'gta5' } 6 | 7 | server_scripts { 8 | 'server/server.lua' 9 | } 10 | -------------------------------------------------------------------------------- /src/server/classes/HTTP.lua: -------------------------------------------------------------------------------- 1 | 2 | Server = { 3 | Routes = {}, 4 | Middlewares = {} 5 | } 6 | 7 | InvokeHttpRequest = nil 8 | 9 | if SetHttpHandler == nil then 10 | SetHttpHandler = function(cb) 11 | InvokeHttpRequest = function(requestData, responseData) 12 | responseData.send = function(data) 13 | print("Sending", data) 14 | end 15 | responseData.writeHead = function(key, value) 16 | print("Writing to header", "status: " .. key, "headers", json.encode(value)) 17 | end 18 | cb(requestData, responseData) 19 | end 20 | end 21 | end 22 | 23 | --- Starts the server. Call this after all routes have been created 24 | function Server.listen() 25 | SetHttpHandler(function(req, res) 26 | print(req.method .. " => " .. req.path) 27 | ---@type string 28 | local path = req.path 29 | local method = req.method 30 | for k,v in pairs(Server.Routes) do 31 | for b,z in pairs(v.Paths) do 32 | if string.match(path, "^" .. z.path .. "$") then 33 | if z.method == method then 34 | local response = Response.new(res) 35 | local request = Request.new(req) 36 | if z.pathData then 37 | local start = 1 38 | for i,j in path:gmatch(z.path) do 39 | print("checking path", i, start) 40 | if z.pathData.params[start] == nil then print("breaking") break end 41 | z.pathData.params[start].value = i 42 | start = start + 1 43 | end 44 | for i,j in pairs(z.pathData.params) do 45 | print("setting param", j.name, j.value) 46 | request:SetParam(j.name, j.value) 47 | end 48 | end 49 | if z.method == "POST" then 50 | req.setDataHandler(function(data) 51 | request._Body = json.decode(data) or "" 52 | local status, ret = z.handler(request, response) 53 | if status ~= nil then 54 | if type(status) == "number" then 55 | if type(ret) ~= "table" then 56 | response:Send(ret) 57 | else 58 | response:Send(json.encode(ret)) 59 | end 60 | end 61 | end 62 | end) 63 | else 64 | local status, ret = z.handler(request, response) 65 | if status ~= nil then 66 | if type(status) == "number" then 67 | if type(ret) ~= "table" then 68 | response:Send(ret) 69 | else 70 | response:SetHeader("Content-Type", "application/json") 71 | response:Send(json.encode(ret)) 72 | end 73 | end 74 | end 75 | end 76 | return 77 | end 78 | end 79 | end 80 | end 81 | local response = Response.new(res) 82 | local request = Request.new(req) 83 | response:Status(404) 84 | response:Send("Not Found: " .. request:Method() .. " " .. request:Path()) 85 | end) 86 | end 87 | 88 | --- Specifies a handler/middleware for the server to use 89 | ---@param path string The path for this handler 90 | ---@param handler Router The router to handle this path 91 | function Server.use(path, handler) 92 | if type(path) == "string" then 93 | if type(handler) == "function" then 94 | 95 | elseif type(handler) == "table" then 96 | local mt = getmetatable(handler) 97 | if mt == nil then print("^1Error^0: " + "failed to load route " + path + "expected route class") return end 98 | if mt.__call() ~= "Router" then print("^1Error^0: " + "failed to load route " + path + "expected route class") return end 99 | for k,v in pairs(handler.Paths) do 100 | print("Mounting: ", path .. v.path) 101 | v.path = path .. v.path 102 | end 103 | table.insert(Server.Routes, handler) 104 | end 105 | end 106 | end 107 | -------------------------------------------------------------------------------- /src/server/classes/Request.lua: -------------------------------------------------------------------------------- 1 | ---@class Request 2 | Request = setmetatable({}, Request) 3 | 4 | Request.__call = function() 5 | return "Request" 6 | end 7 | 8 | Request.__index = Request 9 | 10 | --- Creates a new Request class instance 11 | ---@param request table Expects a request object from the `SetHttpHandler` callback 12 | function Request.new(request) 13 | 14 | local _Request = { 15 | _Raw = request, 16 | _Params = {} 17 | } 18 | 19 | request.body = request.body or "" 20 | 21 | if json.decode(request.body) then 22 | _Request._Body = json.decode(request.body) 23 | else 24 | _Request._Body = request.body 25 | end 26 | 27 | return setmetatable(_Request, Request) 28 | end 29 | 30 | --- Get the value for a named parameter. i.e. `/users/:id` to fetch "id" use `Request:Param("id")` 31 | ---@param name string Name of the parameter to get 32 | ---@return string 33 | function Request:Param(name) 34 | return self._Params[name] 35 | end 36 | 37 | --- Returns all parameters as a table 38 | ---@return table 39 | function Request:Params() 40 | return self._Params 41 | end 42 | 43 | --- Sets the value of a parameter. (internal) 44 | ---@private 45 | ---@param name string The name of the parameter to set 46 | ---@param val string The value of this parameter 47 | function Request:SetParam(name, val) 48 | self._Params[name] = val 49 | end 50 | 51 | --- Returns the body of this request 52 | ---@return string|table 53 | function Request:Body() 54 | return self._Body 55 | end 56 | 57 | --- Returns the path of this request 58 | ---@return string 59 | function Request:Path() 60 | return self._Raw.path 61 | end 62 | 63 | --- Returns the method of this request 64 | ---@return string 65 | function Request:Method() 66 | return self._Raw.method 67 | end 68 | -------------------------------------------------------------------------------- /src/server/classes/Response.lua: -------------------------------------------------------------------------------- 1 | ---@class Response 2 | Response = setmetatable({}, Response) 3 | 4 | Response.__call = function() 5 | return "Response" 6 | end 7 | 8 | Response.__index = Response 9 | 10 | --- Creates a new instance of the Response class 11 | ---@param response table The response object from the `SetHttpHandler` callback 12 | function Response.new(response) 13 | local _Response = { 14 | _Raw = response, 15 | Headers = { 16 | ["X-POWERED-BY"] = "Cyntaax-FiveM-Express" 17 | }, 18 | _Status = 200 19 | } 20 | 21 | return setmetatable(_Response, Response) 22 | end 23 | 24 | --- Sets a header for the response 25 | ---@param key string Name of the header to set 26 | ---@param value string Value for this header 27 | function Response:SetHeader(key, value) 28 | self.Headers[key] = value 29 | end 30 | 31 | --- Gets or sets the status of the response 32 | ---@param status number The HTTP status code to set 33 | ---@return nil|number 34 | function Response:Status(status) 35 | if status == nil then return self._Status end 36 | self._Status = tonumber(status) or 200 37 | return self 38 | end 39 | 40 | --- Sends the response. If the data type is a table, it will be automatically converted to a JSON string 41 | ---@param data string|table The data to send 42 | ---@param status number The http status of the response 43 | function Response:Send(data, status) 44 | status = tonumber(status) or 200 45 | self:Status(status) 46 | if type(data) ~= "string" then 47 | if type(data) == "number" then 48 | data = tostring(data) 49 | elseif type(data) == "boolean" then 50 | data = tostring(data) 51 | elseif type(data) == "table" then 52 | data = json.encode(data) or "" 53 | if data ~= "" then 54 | self:SetHeader("content-type", "application/json") 55 | end 56 | end 57 | end 58 | 59 | self._Raw.writeHead(self:Status(), self.Headers) 60 | 61 | self._Raw.send(data) 62 | end 63 | 64 | -------------------------------------------------------------------------------- /src/server/classes/Router.lua: -------------------------------------------------------------------------------- 1 | ---@class Router 2 | Router = setmetatable({}, Router) 3 | 4 | Router.__call = function() 5 | return "Router" 6 | end 7 | 8 | Router.__index = Router 9 | 10 | function Router.new() 11 | local _Router = { 12 | Paths = {} 13 | } 14 | 15 | return setmetatable(_Router, Router) 16 | end 17 | 18 | ---@param path string 19 | ---@param handler fun(req: Request, res: Response): void 20 | function Router:Get(path, handler) 21 | local parsed = PatternToRoute(path) 22 | table.insert(self.Paths, { 23 | method = "GET", 24 | path = parsed.route, 25 | handler = handler, 26 | pathData = parsed 27 | }) 28 | end 29 | 30 | ---@param path string 31 | ---@param handler fun(req: Request, res: Response): void 32 | function Router:Post(path, handler) 33 | local parsed = PatternToRoute(path) 34 | table.insert(self.Paths, { 35 | method = "POST", 36 | path = parsed.route, 37 | handler = handler, 38 | pathData = parsed 39 | }) 40 | end 41 | -------------------------------------------------------------------------------- /src/server/server.lua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aethyriion/fivem-http-wrapper/823367da889afad7afefc28ad6bedc7991dda432/src/server/server.lua -------------------------------------------------------------------------------- /src/server/util/json.lua: -------------------------------------------------------------------------------- 1 | -- Module options: 2 | local always_try_using_lpeg = false 3 | local register_global_module_table = true 4 | local global_module_name = 'json' 5 | 6 | --[==[ 7 | 8 | David Kolf's JSON module for Lua 5.1/5.2 9 | 10 | Version 2.5 11 | 12 | 13 | For the documentation see the corresponding readme.txt or visit 14 | . 15 | 16 | You can contact the author by sending an e-mail to 'david' at the 17 | domain 'dkolf.de'. 18 | 19 | 20 | Copyright (C) 2010-2014 David Heiko Kolf 21 | 22 | Permission is hereby granted, free of charge, to any person obtaining 23 | a copy of this software and associated documentation files (the 24 | "Software"), to deal in the Software without restriction, including 25 | without limitation the rights to use, copy, modify, merge, publish, 26 | distribute, sublicense, and/or sell copies of the Software, and to 27 | permit persons to whom the Software is furnished to do so, subject to 28 | the following conditions: 29 | 30 | The above copyright notice and this permission notice shall be 31 | included in all copies or substantial portions of the Software. 32 | 33 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 34 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 35 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 36 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 37 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 38 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 39 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 40 | SOFTWARE. 41 | 42 | --]==] 43 | 44 | -- global dependencies: 45 | local pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset = 46 | pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset 47 | local error, require, pcall, select = error, require, pcall, select 48 | local floor, huge = math.floor, math.huge 49 | local strrep, gsub, strsub, strbyte, strchar, strfind, strlen, strformat = 50 | string.rep, string.gsub, string.sub, string.byte, string.char, 51 | string.find, string.len, string.format 52 | local strmatch = string.match 53 | local concat = table.concat 54 | 55 | local json = { version = "dkjson 2.5" } 56 | 57 | if register_global_module_table then 58 | _G[global_module_name] = json 59 | end 60 | 61 | local _ENV = nil -- blocking globals in Lua 5.2 62 | 63 | pcall (function() 64 | -- Enable access to blocked metatables. 65 | -- Don't worry, this module doesn't change anything in them. 66 | local debmeta = require "debug".getmetatable 67 | if debmeta then getmetatable = debmeta end 68 | end) 69 | 70 | json.null = setmetatable ({}, { 71 | __tojson = function () return "null" end 72 | }) 73 | 74 | local function isarray (tbl) 75 | local max, n, arraylen = 0, 0, 0 76 | for k,v in pairs (tbl) do 77 | if k == 'n' and type(v) == 'number' then 78 | arraylen = v 79 | if v > max then 80 | max = v 81 | end 82 | else 83 | if type(k) ~= 'number' or k < 1 or floor(k) ~= k then 84 | return false 85 | end 86 | if k > max then 87 | max = k 88 | end 89 | n = n + 1 90 | end 91 | end 92 | if max > 10 and max > arraylen and max > n * 2 then 93 | return false -- don't create an array with too many holes 94 | end 95 | return true, max 96 | end 97 | 98 | local escapecodes = { 99 | ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", ["\f"] = "\\f", 100 | ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t" 101 | } 102 | 103 | local function escapeutf8 (uchar) 104 | local value = escapecodes[uchar] 105 | if value then 106 | return value 107 | end 108 | local a, b, c, d = strbyte (uchar, 1, 4) 109 | a, b, c, d = a or 0, b or 0, c or 0, d or 0 110 | if a <= 0x7f then 111 | value = a 112 | elseif 0xc0 <= a and a <= 0xdf and b >= 0x80 then 113 | value = (a - 0xc0) * 0x40 + b - 0x80 114 | elseif 0xe0 <= a and a <= 0xef and b >= 0x80 and c >= 0x80 then 115 | value = ((a - 0xe0) * 0x40 + b - 0x80) * 0x40 + c - 0x80 116 | elseif 0xf0 <= a and a <= 0xf7 and b >= 0x80 and c >= 0x80 and d >= 0x80 then 117 | value = (((a - 0xf0) * 0x40 + b - 0x80) * 0x40 + c - 0x80) * 0x40 + d - 0x80 118 | else 119 | return "" 120 | end 121 | if value <= 0xffff then 122 | return strformat ("\\u%.4x", value) 123 | elseif value <= 0x10ffff then 124 | -- encode as UTF-16 surrogate pair 125 | value = value - 0x10000 126 | local highsur, lowsur = 0xD800 + floor (value/0x400), 0xDC00 + (value % 0x400) 127 | return strformat ("\\u%.4x\\u%.4x", highsur, lowsur) 128 | else 129 | return "" 130 | end 131 | end 132 | 133 | local function fsub (str, pattern, repl) 134 | -- gsub always builds a new string in a buffer, even when no match 135 | -- exists. First using find should be more efficient when most strings 136 | -- don't contain the pattern. 137 | if strfind (str, pattern) then 138 | return gsub (str, pattern, repl) 139 | else 140 | return str 141 | end 142 | end 143 | 144 | local function quotestring (value) 145 | -- based on the regexp "escapable" in https://github.com/douglascrockford/JSON-js 146 | value = fsub (value, "[%z\1-\31\"\\\127]", escapeutf8) 147 | if strfind (value, "[\194\216\220\225\226\239]") then 148 | value = fsub (value, "\194[\128-\159\173]", escapeutf8) 149 | value = fsub (value, "\216[\128-\132]", escapeutf8) 150 | value = fsub (value, "\220\143", escapeutf8) 151 | value = fsub (value, "\225\158[\180\181]", escapeutf8) 152 | value = fsub (value, "\226\128[\140-\143\168-\175]", escapeutf8) 153 | value = fsub (value, "\226\129[\160-\175]", escapeutf8) 154 | value = fsub (value, "\239\187\191", escapeutf8) 155 | value = fsub (value, "\239\191[\176-\191]", escapeutf8) 156 | end 157 | return "\"" .. value .. "\"" 158 | end 159 | json.quotestring = quotestring 160 | 161 | local function replace(str, o, n) 162 | local i, j = strfind (str, o, 1, true) 163 | if i then 164 | return strsub(str, 1, i-1) .. n .. strsub(str, j+1, -1) 165 | else 166 | return str 167 | end 168 | end 169 | 170 | -- locale independent num2str and str2num functions 171 | local decpoint, numfilter 172 | 173 | local function updatedecpoint () 174 | decpoint = strmatch(tostring(0.5), "([^05+])") 175 | -- build a filter that can be used to remove group separators 176 | numfilter = "[^0-9%-%+eE" .. gsub(decpoint, "[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%0") .. "]+" 177 | end 178 | 179 | updatedecpoint() 180 | 181 | local function num2str (num) 182 | return replace(fsub(tostring(num), numfilter, ""), decpoint, ".") 183 | end 184 | 185 | local function str2num (str) 186 | local num = tonumber(replace(str, ".", decpoint)) 187 | if not num then 188 | updatedecpoint() 189 | num = tonumber(replace(str, ".", decpoint)) 190 | end 191 | return num 192 | end 193 | 194 | local function addnewline2 (level, buffer, buflen) 195 | buffer[buflen+1] = "\n" 196 | buffer[buflen+2] = strrep (" ", level) 197 | buflen = buflen + 2 198 | return buflen 199 | end 200 | 201 | function json.addnewline (state) 202 | if state.indent then 203 | state.bufferlen = addnewline2 (state.level or 0, 204 | state.buffer, state.bufferlen or #(state.buffer)) 205 | end 206 | end 207 | 208 | local encode2 -- forward declaration 209 | 210 | local function addpair (key, value, prev, indent, level, buffer, buflen, tables, globalorder, state) 211 | local kt = type (key) 212 | if kt ~= 'string' and kt ~= 'number' then 213 | return nil, "type '" .. kt .. "' is not supported as a key by JSON." 214 | end 215 | if prev then 216 | buflen = buflen + 1 217 | buffer[buflen] = "," 218 | end 219 | if indent then 220 | buflen = addnewline2 (level, buffer, buflen) 221 | end 222 | buffer[buflen+1] = quotestring (key) 223 | buffer[buflen+2] = ":" 224 | return encode2 (value, indent, level, buffer, buflen + 2, tables, globalorder, state) 225 | end 226 | 227 | local function appendcustom(res, buffer, state) 228 | local buflen = state.bufferlen 229 | if type (res) == 'string' then 230 | buflen = buflen + 1 231 | buffer[buflen] = res 232 | end 233 | return buflen 234 | end 235 | 236 | local function exception(reason, value, state, buffer, buflen, defaultmessage) 237 | defaultmessage = defaultmessage or reason 238 | local handler = state.exception 239 | if not handler then 240 | return nil, defaultmessage 241 | else 242 | state.bufferlen = buflen 243 | local ret, msg = handler (reason, value, state, defaultmessage) 244 | if not ret then return nil, msg or defaultmessage end 245 | return appendcustom(ret, buffer, state) 246 | end 247 | end 248 | 249 | function json.encodeexception(reason, value, state, defaultmessage) 250 | return quotestring("<" .. defaultmessage .. ">") 251 | end 252 | 253 | encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, state) 254 | local valtype = type (value) 255 | local valmeta = getmetatable (value) 256 | valmeta = type (valmeta) == 'table' and valmeta -- only tables 257 | local valtojson = valmeta and valmeta.__tojson 258 | if valtojson then 259 | if tables[value] then 260 | return exception('reference cycle', value, state, buffer, buflen) 261 | end 262 | tables[value] = true 263 | state.bufferlen = buflen 264 | local ret, msg = valtojson (value, state) 265 | if not ret then return exception('custom encoder failed', value, state, buffer, buflen, msg) end 266 | tables[value] = nil 267 | buflen = appendcustom(ret, buffer, state) 268 | elseif value == nil then 269 | buflen = buflen + 1 270 | buffer[buflen] = "null" 271 | elseif valtype == 'number' then 272 | local s 273 | if value ~= value or value >= huge or -value >= huge then 274 | -- This is the behaviour of the original JSON implementation. 275 | s = "null" 276 | else 277 | s = num2str (value) 278 | end 279 | buflen = buflen + 1 280 | buffer[buflen] = s 281 | elseif valtype == 'boolean' then 282 | buflen = buflen + 1 283 | buffer[buflen] = value and "true" or "false" 284 | elseif valtype == 'string' then 285 | buflen = buflen + 1 286 | buffer[buflen] = quotestring (value) 287 | elseif valtype == 'table' then 288 | if tables[value] then 289 | return exception('reference cycle', value, state, buffer, buflen) 290 | end 291 | tables[value] = true 292 | level = level + 1 293 | local isa, n = isarray (value) 294 | if n == 0 and valmeta and valmeta.__jsontype == 'object' then 295 | isa = false 296 | end 297 | local msg 298 | if isa then -- JSON array 299 | buflen = buflen + 1 300 | buffer[buflen] = "[" 301 | for i = 1, n do 302 | buflen, msg = encode2 (value[i], indent, level, buffer, buflen, tables, globalorder, state) 303 | if not buflen then return nil, msg end 304 | if i < n then 305 | buflen = buflen + 1 306 | buffer[buflen] = "," 307 | end 308 | end 309 | buflen = buflen + 1 310 | buffer[buflen] = "]" 311 | else -- JSON object 312 | local prev = false 313 | buflen = buflen + 1 314 | buffer[buflen] = "{" 315 | local order = valmeta and valmeta.__jsonorder or globalorder 316 | if order then 317 | local used = {} 318 | n = #order 319 | for i = 1, n do 320 | local k = order[i] 321 | local v = value[k] 322 | if v then 323 | used[k] = true 324 | buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) 325 | prev = true -- add a seperator before the next element 326 | end 327 | end 328 | for k,v in pairs (value) do 329 | if not used[k] then 330 | buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) 331 | if not buflen then return nil, msg end 332 | prev = true -- add a seperator before the next element 333 | end 334 | end 335 | else -- unordered 336 | for k,v in pairs (value) do 337 | buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state) 338 | if not buflen then return nil, msg end 339 | prev = true -- add a seperator before the next element 340 | end 341 | end 342 | if indent then 343 | buflen = addnewline2 (level - 1, buffer, buflen) 344 | end 345 | buflen = buflen + 1 346 | buffer[buflen] = "}" 347 | end 348 | tables[value] = nil 349 | else 350 | return exception ('unsupported type', value, state, buffer, buflen, 351 | "type '" .. valtype .. "' is not supported by JSON.") 352 | end 353 | return buflen 354 | end 355 | 356 | function json.encode (value, state) 357 | state = state or {} 358 | local oldbuffer = state.buffer 359 | local buffer = oldbuffer or {} 360 | state.buffer = buffer 361 | updatedecpoint() 362 | local ret, msg = encode2 (value, state.indent, state.level or 0, 363 | buffer, state.bufferlen or 0, state.tables or {}, state.keyorder, state) 364 | if not ret then 365 | error (msg, 2) 366 | elseif oldbuffer == buffer then 367 | state.bufferlen = ret 368 | return true 369 | else 370 | state.bufferlen = nil 371 | state.buffer = nil 372 | return concat (buffer) 373 | end 374 | end 375 | 376 | local function loc (str, where) 377 | local line, pos, linepos = 1, 1, 0 378 | while true do 379 | pos = strfind (str, "\n", pos, true) 380 | if pos and pos < where then 381 | line = line + 1 382 | linepos = pos 383 | pos = pos + 1 384 | else 385 | break 386 | end 387 | end 388 | return "line " .. line .. ", column " .. (where - linepos) 389 | end 390 | 391 | local function unterminated (str, what, where) 392 | return nil, strlen (str) + 1, "unterminated " .. what .. " at " .. loc (str, where) 393 | end 394 | 395 | local function scanwhite (str, pos) 396 | while true do 397 | pos = strfind (str, "%S", pos) 398 | if not pos then return nil end 399 | local sub2 = strsub (str, pos, pos + 1) 400 | if sub2 == "\239\187" and strsub (str, pos + 2, pos + 2) == "\191" then 401 | -- UTF-8 Byte Order Mark 402 | pos = pos + 3 403 | elseif sub2 == "//" then 404 | pos = strfind (str, "[\n\r]", pos + 2) 405 | if not pos then return nil end 406 | elseif sub2 == "/*" then 407 | pos = strfind (str, "*/", pos + 2) 408 | if not pos then return nil end 409 | pos = pos + 2 410 | else 411 | return pos 412 | end 413 | end 414 | end 415 | 416 | local escapechars = { 417 | ["\""] = "\"", ["\\"] = "\\", ["/"] = "/", ["b"] = "\b", ["f"] = "\f", 418 | ["n"] = "\n", ["r"] = "\r", ["t"] = "\t" 419 | } 420 | 421 | local function unichar (value) 422 | if value < 0 then 423 | return nil 424 | elseif value <= 0x007f then 425 | return strchar (value) 426 | elseif value <= 0x07ff then 427 | return strchar (0xc0 + floor(value/0x40), 428 | 0x80 + (floor(value) % 0x40)) 429 | elseif value <= 0xffff then 430 | return strchar (0xe0 + floor(value/0x1000), 431 | 0x80 + (floor(value/0x40) % 0x40), 432 | 0x80 + (floor(value) % 0x40)) 433 | elseif value <= 0x10ffff then 434 | return strchar (0xf0 + floor(value/0x40000), 435 | 0x80 + (floor(value/0x1000) % 0x40), 436 | 0x80 + (floor(value/0x40) % 0x40), 437 | 0x80 + (floor(value) % 0x40)) 438 | else 439 | return nil 440 | end 441 | end 442 | 443 | local function scanstring (str, pos) 444 | local lastpos = pos + 1 445 | local buffer, n = {}, 0 446 | while true do 447 | local nextpos = strfind (str, "[\"\\]", lastpos) 448 | if not nextpos then 449 | return unterminated (str, "string", pos) 450 | end 451 | if nextpos > lastpos then 452 | n = n + 1 453 | buffer[n] = strsub (str, lastpos, nextpos - 1) 454 | end 455 | if strsub (str, nextpos, nextpos) == "\"" then 456 | lastpos = nextpos + 1 457 | break 458 | else 459 | local escchar = strsub (str, nextpos + 1, nextpos + 1) 460 | local value 461 | if escchar == "u" then 462 | value = tonumber (strsub (str, nextpos + 2, nextpos + 5), 16) 463 | if value then 464 | local value2 465 | if 0xD800 <= value and value <= 0xDBff then 466 | -- we have the high surrogate of UTF-16. Check if there is a 467 | -- low surrogate escaped nearby to combine them. 468 | if strsub (str, nextpos + 6, nextpos + 7) == "\\u" then 469 | value2 = tonumber (strsub (str, nextpos + 8, nextpos + 11), 16) 470 | if value2 and 0xDC00 <= value2 and value2 <= 0xDFFF then 471 | value = (value - 0xD800) * 0x400 + (value2 - 0xDC00) + 0x10000 472 | else 473 | value2 = nil -- in case it was out of range for a low surrogate 474 | end 475 | end 476 | end 477 | value = value and unichar (value) 478 | if value then 479 | if value2 then 480 | lastpos = nextpos + 12 481 | else 482 | lastpos = nextpos + 6 483 | end 484 | end 485 | end 486 | end 487 | if not value then 488 | value = escapechars[escchar] or escchar 489 | lastpos = nextpos + 2 490 | end 491 | n = n + 1 492 | buffer[n] = value 493 | end 494 | end 495 | if n == 1 then 496 | return buffer[1], lastpos 497 | elseif n > 1 then 498 | return concat (buffer), lastpos 499 | else 500 | return "", lastpos 501 | end 502 | end 503 | 504 | local scanvalue -- forward declaration 505 | 506 | local function scantable (what, closechar, str, startpos, nullval, objectmeta, arraymeta) 507 | local len = strlen (str) 508 | local tbl, n = {}, 0 509 | local pos = startpos + 1 510 | if what == 'object' then 511 | setmetatable (tbl, objectmeta) 512 | else 513 | setmetatable (tbl, arraymeta) 514 | end 515 | while true do 516 | pos = scanwhite (str, pos) 517 | if not pos then return unterminated (str, what, startpos) end 518 | local char = strsub (str, pos, pos) 519 | if char == closechar then 520 | return tbl, pos + 1 521 | end 522 | local val1, err 523 | val1, pos, err = scanvalue (str, pos, nullval, objectmeta, arraymeta) 524 | if err then return nil, pos, err end 525 | pos = scanwhite (str, pos) 526 | if not pos then return unterminated (str, what, startpos) end 527 | char = strsub (str, pos, pos) 528 | if char == ":" then 529 | if val1 == nil then 530 | return nil, pos, "cannot use nil as table index (at " .. loc (str, pos) .. ")" 531 | end 532 | pos = scanwhite (str, pos + 1) 533 | if not pos then return unterminated (str, what, startpos) end 534 | local val2 535 | val2, pos, err = scanvalue (str, pos, nullval, objectmeta, arraymeta) 536 | if err then return nil, pos, err end 537 | tbl[val1] = val2 538 | pos = scanwhite (str, pos) 539 | if not pos then return unterminated (str, what, startpos) end 540 | char = strsub (str, pos, pos) 541 | else 542 | n = n + 1 543 | tbl[n] = val1 544 | end 545 | if char == "," then 546 | pos = pos + 1 547 | end 548 | end 549 | end 550 | 551 | scanvalue = function (str, pos, nullval, objectmeta, arraymeta) 552 | pos = pos or 1 553 | pos = scanwhite (str, pos) 554 | if not pos then 555 | return nil, strlen (str) + 1, "no valid JSON value (reached the end)" 556 | end 557 | local char = strsub (str, pos, pos) 558 | if char == "{" then 559 | return scantable ('object', "}", str, pos, nullval, objectmeta, arraymeta) 560 | elseif char == "[" then 561 | return scantable ('array', "]", str, pos, nullval, objectmeta, arraymeta) 562 | elseif char == "\"" then 563 | return scanstring (str, pos) 564 | else 565 | local pstart, pend = strfind (str, "^%-?[%d%.]+[eE]?[%+%-]?%d*", pos) 566 | if pstart then 567 | local number = str2num (strsub (str, pstart, pend)) 568 | if number then 569 | return number, pend + 1 570 | end 571 | end 572 | pstart, pend = strfind (str, "^%a%w*", pos) 573 | if pstart then 574 | local name = strsub (str, pstart, pend) 575 | if name == "true" then 576 | return true, pend + 1 577 | elseif name == "false" then 578 | return false, pend + 1 579 | elseif name == "null" then 580 | return nullval, pend + 1 581 | end 582 | end 583 | return nil, pos, "no valid JSON value at " .. loc (str, pos) 584 | end 585 | end 586 | 587 | local function optionalmetatables(...) 588 | if select("#", ...) > 0 then 589 | return ... 590 | else 591 | return {__jsontype = 'object'}, {__jsontype = 'array'} 592 | end 593 | end 594 | 595 | function json.decode (str, pos, nullval, ...) 596 | local objectmeta, arraymeta = optionalmetatables(...) 597 | return scanvalue (str, pos, nullval, objectmeta, arraymeta) 598 | end 599 | 600 | function json.use_lpeg () 601 | local g = require ("lpeg") 602 | 603 | if g.version() == "0.11" then 604 | error "due to a bug in LPeg 0.11, it cannot be used for JSON matching" 605 | end 606 | 607 | local pegmatch = g.match 608 | local P, S, R = g.P, g.S, g.R 609 | 610 | local function ErrorCall (str, pos, msg, state) 611 | if not state.msg then 612 | state.msg = msg .. " at " .. loc (str, pos) 613 | state.pos = pos 614 | end 615 | return false 616 | end 617 | 618 | local function Err (msg) 619 | return g.Cmt (g.Cc (msg) * g.Carg (2), ErrorCall) 620 | end 621 | 622 | local SingleLineComment = P"//" * (1 - S"\n\r")^0 623 | local MultiLineComment = P"/*" * (1 - P"*/")^0 * P"*/" 624 | local Space = (S" \n\r\t" + P"\239\187\191" + SingleLineComment + MultiLineComment)^0 625 | 626 | local PlainChar = 1 - S"\"\\\n\r" 627 | local EscapeSequence = (P"\\" * g.C (S"\"\\/bfnrt" + Err "unsupported escape sequence")) / escapechars 628 | local HexDigit = R("09", "af", "AF") 629 | local function UTF16Surrogate (match, pos, high, low) 630 | high, low = tonumber (high, 16), tonumber (low, 16) 631 | if 0xD800 <= high and high <= 0xDBff and 0xDC00 <= low and low <= 0xDFFF then 632 | return true, unichar ((high - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000) 633 | else 634 | return false 635 | end 636 | end 637 | local function UTF16BMP (hex) 638 | return unichar (tonumber (hex, 16)) 639 | end 640 | local U16Sequence = (P"\\u" * g.C (HexDigit * HexDigit * HexDigit * HexDigit)) 641 | local UnicodeEscape = g.Cmt (U16Sequence * U16Sequence, UTF16Surrogate) + U16Sequence/UTF16BMP 642 | local Char = UnicodeEscape + EscapeSequence + PlainChar 643 | local String = P"\"" * g.Cs (Char ^ 0) * (P"\"" + Err "unterminated string") 644 | local Integer = P"-"^(-1) * (P"0" + (R"19" * R"09"^0)) 645 | local Fractal = P"." * R"09"^0 646 | local Exponent = (S"eE") * (S"+-")^(-1) * R"09"^1 647 | local Number = (Integer * Fractal^(-1) * Exponent^(-1))/str2num 648 | local Constant = P"true" * g.Cc (true) + P"false" * g.Cc (false) + P"null" * g.Carg (1) 649 | local SimpleValue = Number + String + Constant 650 | local ArrayContent, ObjectContent 651 | 652 | -- The functions parsearray and parseobject parse only a single value/pair 653 | -- at a time and store them directly to avoid hitting the LPeg limits. 654 | local function parsearray (str, pos, nullval, state) 655 | local obj, cont 656 | local npos 657 | local t, nt = {}, 0 658 | repeat 659 | obj, cont, npos = pegmatch (ArrayContent, str, pos, nullval, state) 660 | if not npos then break end 661 | pos = npos 662 | nt = nt + 1 663 | t[nt] = obj 664 | until cont == 'last' 665 | return pos, setmetatable (t, state.arraymeta) 666 | end 667 | 668 | local function parseobject (str, pos, nullval, state) 669 | local obj, key, cont 670 | local npos 671 | local t = {} 672 | repeat 673 | key, obj, cont, npos = pegmatch (ObjectContent, str, pos, nullval, state) 674 | if not npos then break end 675 | pos = npos 676 | t[key] = obj 677 | until cont == 'last' 678 | return pos, setmetatable (t, state.objectmeta) 679 | end 680 | 681 | local Array = P"[" * g.Cmt (g.Carg(1) * g.Carg(2), parsearray) * Space * (P"]" + Err "']' expected") 682 | local Object = P"{" * g.Cmt (g.Carg(1) * g.Carg(2), parseobject) * Space * (P"}" + Err "'}' expected") 683 | local Value = Space * (Array + Object + SimpleValue) 684 | local ExpectedValue = Value + Space * Err "value expected" 685 | ArrayContent = Value * Space * (P"," * g.Cc'cont' + g.Cc'last') * g.Cp() 686 | local Pair = g.Cg (Space * String * Space * (P":" + Err "colon expected") * ExpectedValue) 687 | ObjectContent = Pair * Space * (P"," * g.Cc'cont' + g.Cc'last') * g.Cp() 688 | local DecodeValue = ExpectedValue * g.Cp () 689 | 690 | function json.decode (str, pos, nullval, ...) 691 | local state = {} 692 | state.objectmeta, state.arraymeta = optionalmetatables(...) 693 | local obj, retpos = pegmatch (DecodeValue, str, pos, nullval, state) 694 | if state.msg then 695 | return nil, state.pos, state.msg 696 | else 697 | return obj, retpos 698 | end 699 | end 700 | 701 | -- use this function only once: 702 | json.use_lpeg = function () return json end 703 | 704 | json.using_lpeg = true 705 | 706 | return json -- so you can get the module using json = require "dkjson".use_lpeg() 707 | end 708 | 709 | if always_try_using_lpeg then 710 | pcall (json.use_lpeg) 711 | end 712 | 713 | return json 714 | -------------------------------------------------------------------------------- /src/server/util/pattern.lua: -------------------------------------------------------------------------------- 1 | ---@private 2 | --- Converts a string pattern "`/users/:id`" to an object for the Request class to use 3 | function PatternToRoute(input) 4 | if input == "/" then input = "" end 5 | local routeData = { 6 | route = input, 7 | params = {} 8 | } 9 | local matcher = input 10 | for k,v in input:gmatch "/(:%w+)" do 11 | local rawname = k:gsub(":", "") 12 | table.insert(routeData.params, { 13 | name = rawname 14 | }) 15 | matcher, _ = matcher:gsub(k, "(%%w+)", 1) 16 | end 17 | routeData.route = matcher 18 | return routeData 19 | end 20 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": true, 4 | "baseUrl": "./", 5 | "outDir": "./", 6 | "experimentalDecorators": true, 7 | "module": "commonjs", 8 | "target": "ESNext", 9 | "esModuleInterop": true, 10 | "allowSyntheticDefaultImports": true 11 | } 12 | } 13 | --------------------------------------------------------------------------------