{"id":13777816,"url":"https://github.com/sjnam/luajit-brotli","last_synced_at":"2025-05-11T11:34:17.146Z","repository":{"id":48988185,"uuid":"81279583","full_name":"sjnam/luajit-brotli","owner":"sjnam","description":"Google brotli ffi binding","archived":true,"fork":false,"pushed_at":"2021-07-01T23:48:02.000Z","size":45,"stargazers_count":20,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-18T20:47:21.562Z","etag":null,"topics":["brotli"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sjnam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-08T02:43:07.000Z","updated_at":"2023-10-20T19:58:22.000Z","dependencies_parsed_at":"2022-09-07T04:03:03.890Z","dependency_job_id":null,"html_url":"https://github.com/sjnam/luajit-brotli","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjnam%2Fluajit-brotli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjnam%2Fluajit-brotli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjnam%2Fluajit-brotli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjnam%2Fluajit-brotli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sjnam","download_url":"https://codeload.github.com/sjnam/luajit-brotli/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253559420,"owners_count":21927583,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["brotli"],"created_at":"2024-08-03T18:00:49.038Z","updated_at":"2025-05-11T11:34:16.867Z","avatar_url":"https://github.com/sjnam.png","language":"Lua","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"Name\n====\nluajit-brotli - Google [Brotli](https://github.com/google/brotli) ffi bounding\n\n\nInstallation\n============\nTo install `luajit-brotli` you need to install\n[Brotli](https://github.com/google/brotli#build-instructions)\nwith shared libraries firtst.\nThen you can install `luajit-brotli` by placing `brotli/{encoder,decoder}.lua` to\nyour lua library path.\n\n\nSynopsis\n========\n\n* Simple usage\n```lua\nlocal brotlienc = require \"brotli.encoder\"\nlocal brotlidec = require \"brotli.decoder\"\n\nlocal encoder = brotlienc:new()\nlocal decoder = brotlidec:new()\n\nlocal txt = string.rep(\"ABCD\", 1000)\nprint(\"Uncompressed size:\", #txt)\nlocal c, err = encoder:compress(txt)\nprint(\"Compressed size:\", #c)\nlocal txt2, err = decoder:decompress(c)\nassert(txt == txt2)\n```\n\n* In nginx with lua-nginx-module\n\n  The document root directory, `html` has only precompressed files with the \".br\" filename\n  extension instead of reqular files. If a brower does not support \"br\",\n  it decompress on-the-fly.\n```nginx\n# static contents\nlocation / {\n    root html;\n    \n    rewrite_by_lua_block {\n       local brotlidec = require \"brotli.decoder\"\n       local brotli_ok = false\n       local header = ngx.var.http_accept_encoding\n       if header then\n          if string.find(header, \"br\") then\n             brotli_ok = true\n          end\n       end\n       if not brotli_ok then\n          ngx.ctx.decoder = brotlidec:new()\n       end\n       ngx.ctx.brotli_ok = brotli_ok\n       ngx.req.set_uri(ngx.var.uri..\".br\")\n    }\n\n    header_filter_by_lua_block {\n       ngx.header[\"Vary\"] = \"Accept-Encoding\"                \n       if not ngx.ctx.brotli_ok then\n          ngx.header.content_length = nil\n       else\n          ngx.header[\"Content-Encoding\"] = \"br\"\n       end\n    }\n    \n    body_filter_by_lua_block {\n       if ngx.ctx.brotli_ok then return end\n       local decoder = ngx.ctx.decoder\n       ngx.arg[1] = decoder:decompressStream(ngx.arg[1])\n       if decoder:isFinished() then\n          decoder:destroy()\n          ngx.arg[2] = true\n       end\n    }\n}\n```\n\n* Compressing on-the-fly for dynamic contents\n```nginx\n# dynamic contents\nlocation /hello {\n    content_by_lua_block {\n        local name = ngx.var.arg_name or \"world\"\n        local msg = string.rep(\"Hello,\"..name..\" \", 100)\n        msg = string.rep(msg..\"\\n\", 10000)\n        ngx.header[\"Content-Length\"] = #msg \n        ngx.print(msg)\n    }\n\n    header_filter_by_lua_block {\n        local brotlienc = require \"brotli.encoder\"\n        local brotli_ok = false\n        local header = ngx.var.http_accept_encoding\n        if header then\n           if string.find(header, \"br\") then\n              brotli_ok = true\n           end\n        end\n        ngx.ctx.brotli_ok = brotli_ok\n        ngx.header[\"Vary\"] = \"Accept-Encoding\"\n        if brotli_ok then\n           ngx.header.content_length = nil\n           ngx.header[\"Content-Encoding\"] = \"br\"\n           ngx.ctx.encoder = brotlienc:new()\n        end\n    }\n\n    body_filter_by_lua_block {\n        if not ngx.ctx.brotli_ok then return end                \n        local encoder = ngx.ctx.encoder\n        ngx.arg[1] = encoder:compressStream(ngx.arg[1])        \n        if encoder:isFinished() then\n           encoder:destroy()\n           ngx.arg[2] = true\n        end\n    }\n}\n```\n\n* Decompressing on-the-fly for the compressed request body\n```nginx\nlocation /reqdecom {\n    content_by_lua_block {\n        local brotlidec = require \"brotli.decoder\"\n\n        local decoder = brotlidec:new()\n        local sock, err = ngx.req.socket()\n        if not sock then\n           print(err)\n           ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)\n        end\n\n        sock:settimeout(1000)\n        while true do\n           local line, err, partial = sock:receive(1024)\n           line = decoder:decompressStream(line or partial)\n           ngx.print(line)\n           if err == \"closed\" then break end\n        end\n        decoder:destroy()\n    }\n}\n```\n\nMethods\n=======\n\nnew\n---\n* `syntax: encoder, err = brotlienc:new(options?)`\n\n* `syntax: decoder, err = brotlidec:new()`\n\nCreate brotli encoder or decoder.\n\nThe `options` argument of the encoder is a lua table holding the following keys:\n\n* `quality`\n\n    Set Brotli quality (compression) level.\n    Acceptable values are in the range from `0` to `11`.\n    (Defaults to 11)\n\n* `lgwin`\n\n    Set Brotli window size. Window size is `(1 \u003c\u003c lgwin) - 16`.\n\n* `mode`\n\n    The compression mode can be\n    \n    * `BROTLI_MODE_GENERIC` (0, default)\n    \n    * `BROTLI_MODE_TEXT` (1, for UTF-8 format text input)\n\n    * `BROTLI_MODE_FONT` (2, for WOFF 2.0)\n\n\ndestroy\n-------\n* `syntax: encoder:destroy()`\n\n    Deinitializes and frees BrotliEncoderState instance.\n\n* `syntax: decoder:destroy()`\n\n    Deinitializes and frees BrotliDecoderState instance.\n\n\ncompress\n--------\n`syntax: encoded_buffer, err = encoder:compress(input_buffer)`\n\nCompresses the data in input_buffer into encoded_buffer.\n\n\ncompressStream\n--------------\n`syntax: buffer = encoder:compressStream(stream)`\n\nCompresses input stream to output buffer.\n\n\ndecompress\n----------\n`syntax: decoded_buffer, err = decoder:decompress(encoded_buffer)`\n\nDecompresses the data in encoded_buffer into decoded_buffer.\n\n\ndecompressStream\n----------------\n`syntax: buffer = decoder:decompressStream(encoded_stream)`\n\nDecompresses the data in encoded_buffer into buffer stream\n\nAuthor\n======\nSoojin Nam jsunam@gmail.com\n\nLicense\n=======\nPublic Domain\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjnam%2Fluajit-brotli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsjnam%2Fluajit-brotli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjnam%2Fluajit-brotli/lists"}