{"id":17136978,"url":"https://github.com/un-def/lua-buffet","last_synced_at":"2025-03-24T06:43:14.718Z","repository":{"id":85223080,"uuid":"270108950","full_name":"un-def/lua-buffet","owner":"un-def","description":"Socket-like buffer objects for Lua","archived":false,"fork":false,"pushed_at":"2020-08-21T08:37:03.000Z","size":106,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T09:11:27.096Z","etag":null,"topics":["lua","mock","nginx-lua","openresty","socket"],"latest_commit_sha":null,"homepage":"","language":"MoonScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/un-def.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-06T21:27:10.000Z","updated_at":"2021-12-22T05:08:41.000Z","dependencies_parsed_at":"2023-04-26T15:46:27.024Z","dependency_job_id":null,"html_url":"https://github.com/un-def/lua-buffet","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/un-def%2Flua-buffet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/un-def%2Flua-buffet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/un-def%2Flua-buffet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/un-def%2Flua-buffet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/un-def","download_url":"https://codeload.github.com/un-def/lua-buffet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245224002,"owners_count":20580362,"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":["lua","mock","nginx-lua","openresty","socket"],"created_at":"2024-10-14T20:05:54.660Z","updated_at":"2025-03-24T06:43:14.700Z","avatar_url":"https://github.com/un-def.png","language":"MoonScript","readme":"# lua-buffet\n\n[![Luarocks](https://img.shields.io/luarocks/v/undef/lua-buffet?color=blue)](https://luarocks.org/modules/undef/lua-buffet)\n[![OPM](https://img.shields.io/opm/v/un-def/lua-buffet?color=blue)](https://opm.openresty.org/package/un-def/lua-buffet/)\n[![Build Status](https://img.shields.io/travis/un-def/lua-buffet)](https://travis-ci.org/un-def/lua-buffet)\n[![License](https://img.shields.io/github/license/un-def/lua-buffet)][license]\n\nSocket-like buffer objects for Lua\n\n## Name\n\nThe word “buffet” is a portmanteau of “**buff**er” and “sock**et**”.\n\n## Description\n\nA _buffet_ is an object that has the same interface as socket objects in the popular Lua libraries [LuaSocket](http://w3.impa.br/~diego/software/luasocket/) and [Lua Nginx Module](https://github.com/openresty/lua-nginx-module) but doesn't do any real network communication. Instead the network stack the _buffet_ receives data from an arbitrary source. The data source can be a string, a table of strings or an iterator function producing strings.\n\nThe _buffet_ works in a streaming fashion. That is, the _buffet_ doesn't try to read and store internally the whole source data at once but reads as little as possible and only when necessary. That means that the _buffet_ can be efficiently used as a proxy for sources of unlimited amounts of data such as _real_ sockets or file I/O readers.\n\nAnother possible use is unit testing where the _buffet_ can be used as a mock object instead of the real socket object.\n\n## Basic usage\n\n```lua\nlocal buffet = require('buffet')\nlocal buffet_resty = require('buffet.resty')\n\n-- Input data is a string.\n-- Read data in chunks of 3 bytes.\ndo\n    local bf = buffet_resty.new('data string')\n    print(buffet.is_closed(bf))   -- false\n    repeat\n        local data, err, partial = bf:receive(3)\n        print(data, err, partial)\n    until err\n    print(buffet.is_closed(bf))   -- true\nend\n\n-- Input data is a table containing data chunks.\n-- Read data line by line.\ndo\n    local bf = buffet_resty.new({'line 1\\nline', ' 2\\nli', 'ne 3\\n'})\n    repeat\n        local data, err, partial = bf:receive('*l')\n        print(data, err, partial)\n    until err\nend\n\n-- Input data is a function producing data chunks.\n-- Read data splitted by the specified pattern, up to 4 bytes at once.\ndo\n    local iterator = coroutine.wrap(function()\n        coroutine.yield('first-==-se')\n        coroutine.yield('cond-==')\n        coroutine.yield('-thi')\n        coroutine.yield('rd')\n        coroutine.yield(nil, 'some error')\n        coroutine.yield('unreachable')\n    end)\n    local bf = buffet_resty.new(iterator)\n    local reader = bf:receiveuntil('-==-')\n    print(buffet.get_iterator_error(bf))   -- nil\n    repeat\n        local data, err, partial = reader(4)\n        print(data, err, partial)\n    until err\n    print(buffet.get_iterator_error(bf))   -- some error\nend\n\n-- Send data.\ndo\n    local bf = buffet_resty.new()\n    for i = 1, 5 do\n        local char = string.char(0x40 + i)\n        bf:send(string.rep(char, i))\n    end\n    local send_buffer = buffet.get_send_buffer(bf)\n    print(#send_buffer)   -- 5\n    for _, chunk in ipairs(send_buffer) do\n        print(chunk)\n    end\n    print(buffet.get_sent_data(bf))   -- ABBCCCDDDDEEEEE\nend\n```\n\nFor more advanded usage see the [examples](https://github.com/un-def/lua-buffet/tree/master/examples) directory.\n\n## Documentation\n\nDocumentation is available at https://undef.im/lua-buffet/.\n\n## Changelog\n\nFor detailed changelog see [CHANGELOG.md](https://github.com/un-def/lua-buffet/blob/master/CHANGELOG.md).\n\n## TODO\n\n### OpenResty\n\n#### `ngx.socket.tcp`\n\n  * [x] constructor (`ngx.socket.tcp` ~ `buffet.resty.new`)\n  * [x] `:connect` (noop)\n  * [x] `:sslhandshake` (noop)\n  * [x] `:send`\n  * [x] `:receive`\n    * [x] `:receive()`\n    * [x] `:receive('*l')`\n    * [x] `:receive('*a')`\n    * [x] `:receive(size)`\n  * [x] `:receiveany`\n  * [x] `:receiveuntil`\n    * [x] `iterator()`\n    * [x] `iterator(size)`\n    * [x] `inclusive` option\n  * [x] `:close`\n  * [x] `:settimeout` (noop)\n  * [x] `:settimeouts` (noop)\n  * [x] `:setoption` (noop)\n  * [x] `:setkeepalive` (equivalent to `:close`)\n  * [x] `:getreusedtimes` (noop)\n\n#### `ngx.socket.udp`\n\n  * [ ] constructor\n  * [ ] `:setpeername`\n  * [ ] `:send`\n  * [ ] `:receive`\n  * [ ] `:close`\n  * [ ] `:settimeout`\n\n### LuaSocket\n\n...\n\n## License\n\nThe [MIT License][license].\n\n\n[license]: https://github.com/un-def/lua-buffet/blob/master/LICENSE\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fun-def%2Flua-buffet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fun-def%2Flua-buffet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fun-def%2Flua-buffet/lists"}