### Start Server Listening in Lua Source: https://luvit.io/api/net Illustrates how to start a server listening on a specific port, with an optional IP address and a callback function that is invoked once the server starts listening. ```lua Server:listen(port, ip, callback) ``` -------------------------------- ### Luvit File System Module Setup Source: https://luvit.io/api/all Initializes the node-style file system module in Luvit. This module leverages libuv for its operations, providing an interface similar to Linux system calls. ```lua local fs = require('fs') ``` -------------------------------- ### Socket:recvStart Source: https://luvit.io/api/all Starts receiving data on a socket. This method initiates the process of listening for incoming data. ```APIDOC ## Socket:recvStart ### Description Start receiving on socket. ### Method Not specified (likely implicit within the socket object's lifecycle) ### Endpoint Not applicable (this is a method on a socket object) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None (operation is side-effect based) #### Response Example None ``` -------------------------------- ### Socket:bind Source: https://luvit.io/api/all Starts listening on a specified port and host for incoming connections or data. ```APIDOC ## Socket:bind ### Description Starts listening on the specified port and host. ### Method Not specified (likely implicit within the socket object's lifecycle) ### Endpoint Not applicable (this is a method on a socket object) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None (operation is side-effect based) #### Response Example None ``` -------------------------------- ### Luvit DNS Module Setup Source: https://luvit.io/api/all Demonstrates how to require and use the node-style DNS module in Luvit. This module facilitates DNS resolution operations. ```lua local dns = require('dns') ``` -------------------------------- ### Luvit HTTP Server Example Source: https://luvit.io/api/synopsis A basic HTTP server written in Luvit that responds with 'Hello World' to all incoming requests. It uses the 'http' module and listens on port 1337. No external dependencies like Apache or Nginx are required. ```lua local http = require('http') http.createServer(function (req, res) local body = "Hello world\n" res:setHeader("Content-Type", "text/plain") res:setHeader("Content-Length", #body) res:finish(body) end):listen(1337, '127.0.0.1') print('Server running at http://127.0.0.1:1337/') ``` -------------------------------- ### Luvit Net Module: Socket and Server Methods Source: https://luvit.io/api/all The 'net' module's Socket class offers methods like 'initialize', 'resume', 'connect', 'destroy', 'listen', and 'getsockname' for managing network connections. The Server class provides a 'listen' method to start a server on a specified port. ```lua local net = require('net') -- Socket methods example local socket = net.Socket:new({handle = 'TCP'}) socket:listen(128) -- Listen with default queueSize local sockname = socket:getsockname() print('Socket name:', sockname) -- Server listen example local server = net.Server:new() server:listen(3000, '127.0.0.1', function(err) if err then print('Server listen error:', err) end print('Server listening on port 3000') end) ``` -------------------------------- ### Initialize Write Stream - Lua Source: https://luvit.io/api/fs Initializes a write stream for a given path and options. Options can include `fd`, `flags`, `mode`, and `start` to control file opening and writing behavior. This method prepares the stream for writing data to a file. ```lua local fs = require('fs') -- Example with options: local writable = fs.WriteStream:new(path, { flags = 'w', mode = 438, -- equivalent to octal 0666 start = 0 }) ``` -------------------------------- ### Buffer Read Signed 8-bit Integer Example Source: https://luvit.io/api/all Demonstrates reading a signed 8-bit integer from a buffer at a given offset. This method is part of the `buffer.Buffer` class and requires the offset as an argument. ```lua local buffer = require('buffer') -- Assuming 'myBuffer' is an instance of buffer.Buffer and 'offset' is a valid number local value = myBuffer:readInt8(offset) print(value) ``` -------------------------------- ### Initialize WriteStream - Node.js fs Source: https://luvit.io/api/all Initializes a file write stream with a specified path and options. The 'options' table can include 'fd' (file descriptor), 'flags' (e.g., 'w' for write), 'mode' (file permissions), and 'start' (position to begin writing). These options control how the file is opened and written to. ```javascript WriteStream:initialize(path, options) ``` -------------------------------- ### Readline: Basic Line Input in Lua Source: https://luvit.io/api/readline This example shows the primary method `readline.readLine` for obtaining user input from the terminal. It takes a prompt string and a callback function that processes the input. ```lua local readline = require('readline') readline.readLine('Enter text: ', function(text) print('You entered: ' .. text) end) ``` -------------------------------- ### Implementing a Custom Transform Stream in Lua Source: https://luvit.io/api/all Demonstrates how to create a custom transform stream in Lua by extending the base Transform class. This example shows a simple passthrough filter that prints 'hello world'. It requires the 'stream' module and defines the `_transform` method to process incoming data. ```lua local Transform = require('stream').Transform local Transformer = Transform:extend() function Transformer:initialize() Transform:initialize(self, {objectMode = true}) end function Transformer:_transform(line, cb) self:push(line) return cb() end local transformer = Transformer:new() transformer:on('data', print) transformer:write('hello world') ``` -------------------------------- ### Open Write Stream Callback - Lua Source: https://luvit.io/api/fs Defines a callback function to be executed when the write stream is opened. An 'open' event is also emitted with the file descriptor upon successful opening. This allows for setup actions immediately after the file is ready for writing. ```lua local fs = require('fs') local writable = fs.WriteStream:new(path, options) writable:open(function() print('Write stream opened') end) ``` -------------------------------- ### Perform HTTPS GET Request with Luvit Source: https://luvit.io/api/https This example shows how to use the `https.get` method in Luvit for making simple HTTPS GET requests. It simplifies the request process by automatically setting the method to GET and handling the response with a provided callback. This is useful for fetching resources securely. ```lua local https = require('https') local options = { host = 'www.example.com', port = 443, path = '/get' } https.get(options, function(res) local data = '' res.on('data', function(chunk) data = data .. chunk end) res.on('end', function() print(data) end) end).on('error', function(e) print('Got error: ' .. e.message) end) ``` -------------------------------- ### Path Module: Get Extension Name Source: https://luvit.io/api/path Extracts the file extension from a given file path. The extension is defined as the string starting from the last '.' character in the path. ```javascript const path = require('path'); const filePath1 = '/usr/local/bin/node.js'; const extName1 = path.extname(filePath1); console.log(extName1); // '.js' const filePath2 = '/home/user/archive.tar.gz'; const extName2 = path.extname(filePath2); console.log(extName2); // '.gz' const filePath3 = 'no_extension'; const extName3 = path.extname(filePath3); console.log(extName3); // '' ``` -------------------------------- ### Path Module: Get Relative Path Source: https://luvit.io/api/path Calculates the relative path from a starting path ('from') to a target path ('to'). If no relative path can be determined, the 'to' path is returned. ```javascript const path = require('path'); const fromPath = '/usr/local/bin'; const toPath = '/usr/local/lib/node_modules'; const relativePath = path.relative(fromPath, toPath); console.log(relativePath); // '../lib/node_modules' ``` -------------------------------- ### Running Luvit Server from Command Line Source: https://luvit.io/api/synopsis Demonstrates how to execute a Luvit script, specifically the 'server.lua' file, from the command line using the 'luvit' executable. This shows the practical application of the Luvit scripting platform. ```shell > luvit server.lua Server running at http://127.0.0.1:1337/ ``` -------------------------------- ### Get Relative Path - Luvit Path Module Source: https://luvit.io/api/all Calculates the relative path from a starting path to a target path. If a relative path cannot be determined, the target path is returned. Dependencies: None. Inputs: 'from' path (string), 'to' path (string). Outputs: Relative path (string). ```javascript path.relative(from, to) ``` -------------------------------- ### HTTP Server and Client Methods Source: https://luvit.io/api/all This section covers the core methods for creating HTTP servers and initiating client requests using the http module. ```APIDOC ## http.createServer() ### Description Creates an HTTP server. ### Method POST ### Endpoint /http/createServer ### Parameters #### Request Body - **onRequest** (function) - Required - The callback function to handle incoming requests. ### Request Example ```json { "onRequest": "function(req, res) { ... }" } ``` ### Response #### Success Response (200) - **server** (object) - An object representing the HTTP server. #### Response Example ```json { "server": "" } ``` --- ## http.request(options, onResponse) ### Description Makes an HTTP request to a given URL. ### Method POST ### Endpoint /http/request ### Parameters #### Request Body - **options** (object) - Required - An object containing request options (e.g., host, port, path, method). - **onResponse** (function) - Required - The callback function to handle the response. ### Request Example ```json { "options": { "host": "example.com", "port": 80, "path": "/", "method": "GET" }, "onResponse": "function(res) { ... }" } ``` ### Response #### Success Response (200) - **request** (object) - An object representing the HTTP client request. #### Response Example ```json { "request": "" } ``` --- ## http.get(options, onResponse) ### Description Performs an HTTP GET request to a given URL. ### Method POST ### Endpoint /http/get ### Parameters #### Request Body - **options** (object) - Required - An object containing request options (e.g., host, port, path). - **onResponse** (function) - Required - The callback function to handle the response. ### Request Example ```json { "options": { "host": "example.com", "port": 80, "path": "/" }, "onResponse": "function(res) { ... }" } ``` ### Response #### Success Response (200) - **request** (object) - An object representing the HTTP client request. #### Response Example ```json { "request": "" } ``` ``` -------------------------------- ### Get File Status Asynchronously (fs.stat) Source: https://luvit.io/api/all Retrieves file status information asynchronously for a given path. The callback function is executed with an error (or nil) and a stat object containing details like modification time, access time, size, and type. The provided example shows the structure of the returned stat object. ```javascript fs.stat(path, function(err, stat) { // Handle err and stat object }) ``` -------------------------------- ### Luvit Core Object Creation and Extension Source: https://luvit.io/api/core Demonstrates how to create and extend classes using the Luvit core.Object. It shows creating a new instance, defining an initializer, and extending the base class to create subclasses. ```lua local Rectangle = Object:extend() function Rectangle:initialize(w, h) self.w = w self.h = h end function Rectangle:getArea() return self.w * self.h end local rect = Rectangle:new(3, 4) p(rect:getArea()) ``` ```lua local Square = Rectangle:extend() function Square:initialize(w) self.w = w self.h = h end ``` ```lua Object:create() ``` -------------------------------- ### Extend WriteStream Class - Node.js fs Source: https://luvit.io/api/all Provides an example of extending the fs.WriteStream class in Lua. This allows for custom implementations of stream behavior, such as overriding the _write method to add custom logic before writing data. The example demonstrates creating a new writable stream and using its methods. ```lua local path, cb, chunk = 'valid/path', validFunc, 'validString' local WritableChild = fs.WriteStream:extend() function WritableChild:_write(data, callback) print('Wrote: '..data) callback() end local writable = WritableChild:new(path, cb) writable:on('open', function() print('file opened') writable:write(chunk) -- optional callback writable:close() ``` -------------------------------- ### Object Creation and Initialization (Lua) Source: https://luvit.io/api/all Demonstrates how to create a new object instance and initialize it using the `Object:new()` method. This method automatically calls an `initialize` method if it exists on the object's class. It requires the `Object` class as a base. ```lua local Rectangle = Object:extend() function Rectangle:initialize(w, h) self.w = w self.h = h end function Rectangle:getArea() return self.w * self.h end local rect = Rectangle:new(3, 4) p(rect:getArea()) ``` -------------------------------- ### HTTPS Module: Making Requests (Lua) Source: https://luvit.io/api/all The HTTPS module in Luvit provides a Node-style client and server for HTTP over TLS/SSL. The 'request' method allows for making custom HTTP requests with specified options and a callback to handle the response. The 'get' method is a convenience wrapper for making GET requests. ```lua local https = require('https') -- Example for https.request local options = { host = 'example.com', port = 443, path = '/test', method = 'GET' } local req = https.request(options, function(res) print('Status Code: ' .. res.statusCode) res:on('data', function(chunk) print(chunk) end) end) req:end() -- Example for https.get https.get(options, function(res) print('Status Code: ' .. res.statusCode) res:on('data', function(chunk) print(chunk) end) end) ``` -------------------------------- ### http.createServer Source: https://luvit.io/api/http Creates an HTTP server that listens for incoming requests. ```APIDOC ## Method: http.createServer(onRequest) ### Description Creates an HTTP server. ### Parameters - **onRequest** (function) - A callback function that handles incoming requests. It receives an `http.IncomingMessage` and an `http.ServerResponse` object. ### Returns - (http.Server) - An instance of the HTTP server. ``` -------------------------------- ### Event Emitter Usage (Lua) Source: https://luvit.io/api/all Shows how to use the `core.Emitter` class for event handling. It covers creating an emitter instance, registering event listeners using `on`, and emitting events with optional data. It also demonstrates subclassing `Emitter` and using `utils.bind` for context preservation. ```lua local emitter = Emitter:new() emitter:on('foo', p) emitter:emit('foo', 1, 2, 3) ``` ```lua local Custom = Emitter:extend() local c = Custom:new() c:on('bar', onBar) ``` ```lua function some_func(self, a, b, c) end emitter:on('end', utils.bind(some_func, emitter)) emitter:emit('end', 'a', 'b', 'c') ``` -------------------------------- ### Buffer:toString Source: https://luvit.io/api/buffer Converts a portion of the buffer to a string. If start and end indices are not provided, the entire buffer is converted. ```APIDOC ## Buffer:toString ### Description Stringify the buffer from the ith to the jth position, or the whole thing if i and j arent specified. ### Method GET (or similar, depending on context) ### Endpoint /websites/luvit_io_api/Buffer/toString ### Parameters #### Query Parameters - **i** (number) - Optional - The starting index for string conversion. - **j** (number) - Optional - The ending index for string conversion. ### Request Example ``` GET /websites/luvit_io_api/Buffer/toString?i=0&j=10 ``` ### Response #### Success Response (200) - **string** (string) - The string representation of the specified buffer portion. #### Response Example ```json { "data": "string_representation" } ``` ``` -------------------------------- ### Object Creation and Extension Source: https://luvit.io/api/all Details on how to create new object instances and extend existing classes using Object:new and Object:extend. ```APIDOC ## Object:new ### Description Creates a new instance and calls `obj:initialize(...)` if it exists. ### Method `Object:new(...) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```lua local Rectangle = Object:extend() function Rectangle:initialize(w, h) self.w = w self.h = h end function Rectangle:getArea() return self.w * self.h end local rect = Rectangle:new(3, 4) -- p(rect:getArea()) ``` ### Response #### Success Response (200) - **instance** (object) - A new instance of the object. #### Response Example ```json { "instance": {} } ``` ## Object:extend ### Description Creates a new sub-class. ### Method `Object:extend() ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```lua local Square = Rectangle:extend() function Square:initialize(w) self.w = w self.h = h end ``` ### Response #### Success Response (200) - **subclass** (function) - A constructor for the new sub-class. #### Response Example ```json { "subclass": "function" } ``` ``` -------------------------------- ### Get Server Address in Lua Source: https://luvit.io/api/net Demonstrates how to retrieve the address information of the server using the 'address' method. ```lua Server:address() ``` -------------------------------- ### Create HTTP Server in Luvit Source: https://luvit.io/api/http Demonstrates the creation of an HTTP server using http.createServer, which takes an 'onRequest' callback function. This function is executed for each incoming HTTP request. ```lua -- Creating an HTTP server -- local server = http.createServer(function(req, res) -- -- Handle request and response here -- end) ``` -------------------------------- ### Get Socket Name in Lua Source: https://luvit.io/api/net Shows how to retrieve the address information of the socket using the 'getsockname' method. ```lua Socket:getsockname() ``` -------------------------------- ### Connect Socket using Options or Port/Host in Lua Source: https://luvit.io/api/net Illustrates two ways to establish a connection using the 'Socket:connect' method: either by providing an options table with 'host' and 'port', or by specifying 'port' and optionally 'host', with a callback function. ```lua local options = { host = ..., port = ... } connect(options, [cb]) ``` ```lua connect(port, [host, cb]) ``` -------------------------------- ### Path Module: Get Root Path Source: https://luvit.io/api/path Retrieves the root path of the filesystem. This function can optionally take a file path as an argument. ```javascript const path = require('path'); // Get the root path const rootPath = path.getRoot(); console.log(rootPath); // Get root path for a specific file (example) const specificRoot = path.getRoot('/users/documents/file.txt'); console.log(specificRoot); ``` -------------------------------- ### core.Emitter API Source: https://luvit.io/api/all Documentation for the core.Emitter class, including methods for managing event listeners and emitting events. ```APIDOC ## core.Emitter ### Description This class can be used directly whenever an event emitter is needed. It can also be easily sub-classed. Unlike EventEmitter in node.js, Emitter class doesn't auto binds `self` reference. This means, if a callback handler is expecting a `self` reference, utils.bind() should be used, and the callback handler should have a `self` at the beginning its parameter list. ### Methods #### Emitter:new() ##### Description Creates a new instance of the Emitter. ##### Request Example ```lua local emitter = Emitter:new() ``` #### Emitter:on(name, callback) ### Description Adds an event listener (`callback`) for the named event `name`. ### Method `Emitter:on(name, callback) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **name** (string) - The name of the event to listen for. - **callback** (function) - The function to execute when the event is triggered. ### Request Example ```lua em = Emitter:new() em:on('data', function(data) print(data) end) ``` ### Response #### Success Response (200) None #### Response Example N/A #### Emitter:once(name, callback) ### Description Same as `Emitter:on` except it de-registers itself after the first event. ### Method `Emitter:once(name, callback) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **name** (string) - The name of the event to listen for. - **callback** (function) - The function to execute when the event is triggered. ### Request Example ```lua em = Emitter:new() em:once('data', function(data) print(data) end) -- Will only be called once ``` ### Response #### Success Response (200) None #### Response Example N/A #### Emitter:listenerCount(name) ### Description Returns the count of the listeners bound to an event with the name `name` ### Method `Emitter:listenerCount(name) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **name** (string) - The name of the event. ### Request Example ```lua local count = em:listenerCount('data') ``` ### Response #### Success Response (200) - **count** (number) - The number of listeners for the specified event. #### Response Example ```json { "count": 5 } ``` #### Emitter:emit(name, ...) ### Description Emit a named event to all listeners with optional data arguments. ### Method `Emitter:emit(name, ...) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **name** (string) - The name of the event to emit. - **...** (any) - Optional data arguments to pass to the listeners. ### Request Example ```lua em:emit('data', 'foo', 1, 2) ``` ### Response #### Success Response (200) None #### Response Example N/A #### Emitter:removeListener(name, callback) ### Description Removes an event listener for the named event. ### Method `Emitter:removeListener(name, callback) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **name** (string) - The name of the event. - **callback** (function) - The specific listener function to remove. ### Request Example ```lua local my_listener = function(data) print(data) end em:on('data', my_listener) em:removeListener('data', my_listener) ``` ### Response #### Success Response (200) None #### Response Example N/A ``` -------------------------------- ### HTTPS Request API Source: https://luvit.io/api/https Provides methods for making HTTPS requests. This includes a general `request` method and a convenience `get` method. ```APIDOC ## https.request(options, callback) ### Description Creates an HTTPS client request. ### Method POST (implied, as it's a request) ### Endpoint Not applicable (client-side method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **options** (table) - Required - An object containing request options such as host, port, path, method, headers, etc. - **callback** (function) - Required - A callback function that will be executed when a response is received. ### Request Example ```lua local https = require('https') local options = { host = 'www.example.com', port = 443, path = '/resource', method = 'GET', headers = { ['User-Agent'] = 'luvit-https-client' } } local req = https.request(options, function(res) print('Status Code: ' .. res.statusCode) res:on('data', function(chunk) print('Response: ' .. chunk) end) end) req:end() ``` ### Response #### Success Response (200) - **res** (table) - The response object from the server, with methods like `on('data', callback)` and properties like `statusCode`. #### Response Example None (handled by callback) ``` ```APIDOC ## https.get(options, onResponse) ### Description A convenience method for making GET requests using `https.request`. ### Method GET ### Endpoint Not applicable (client-side method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **options** (table) - Required - An object containing request options (similar to `https.request`). - **onResponse** (function) - Required - A callback function to handle the server's response. ### Request Example ```lua local https = require('https') local options = { host = 'www.example.com', path = '/resource' } https.get(options, function(res) print('Status Code: ' .. res.statusCode) res:on('data', function(chunk) print('Response: ' .. chunk) end) end) ``` ### Response #### Success Response (200) - **res** (table) - The response object from the server. #### Response Example None (handled by callback) ``` -------------------------------- ### Luvit Core Emitter Event Handling Source: https://luvit.io/api/core Illustrates the usage of the core.Emitter class for event handling in Luvit. It shows how to create an emitter instance, register event listeners, and emit events with arguments. ```lua local emitter = Emitter:new() emitter:on('foo', p) emitter:emit('foo', 1, 2, 3) ``` ```lua local Custom = Emitter:extend() local c = Custom:new() c:on('bar', onBar) ``` ```lua em = Emitter:new() em:on('data', function(data) print(data) end) em:emit('data', 123) ``` ```lua function some_func(self, a, b, c) end emitter:on('end', utils.bind(some_func, emitter)) emitter:emit('end', 'a', 'b', 'c') ``` -------------------------------- ### Stream.Writable Methods Source: https://luvit.io/api/all Documentation for methods associated with the Stream.Writable class, including initialize, write, cork, and uncork. ```APIDOC ## Writable:initialize(options) ### Description Initializes the Writable stream with provided options. You can modify or set the writable state options here. ### Method `initialize` ### Endpoint `Writable:initialize(options)` ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Request Example ```json { "example": "Writable initialize example" } ``` ### Response #### Success Response (200) - None #### Response Example ```json { "example": "Writable initialize response" } ``` ## Writable:write(chunk, cb) ### Description Manually writes a chunk of data to the writable stream. The callback `cb` is invoked after the chunk has been processed. ### Method `write` ### Endpoint `Writable:write(chunk, cb)` ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Request Example ```json { "example": "Writable write example" } ``` ### Response #### Success Response (200) - None #### Response Example ```json { "example": "Writable write response" } ``` ## Writable:cork() ### Description Enqueues data, similar to pausing the stream. Data written after `cork()` is called will be buffered until `uncork()` is called. ### Method `cork` ### Endpoint `Writable:cork()` ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Request Example ```json { "example": "Writable cork example" } ``` ### Response #### Success Response (200) - None #### Response Example ```json { "example": "Writable cork response" } ``` ## Writable:uncork() ### Description Flushes all buffered data to the stream. This should be called after `cork()` to resume writing. ### Method `uncork` ### Endpoint `Writable:uncork()` ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Request Example ```json { "example": "Writable uncork example" } ``` ### Response #### Success Response (200) - None #### Response Example ```json { "example": "Writable uncork response" } ``` ``` -------------------------------- ### Light Operating System Helper (los) Source: https://luvit.io/api/all The 'los' module provides a simple way to get the host operating system name within Luvit. ```APIDOC ## GET /los/type ### Description Returns the host operating system name. ### Method GET ### Endpoint /los/type ### Parameters None ### Request Example None ### Response #### Success Response (200) - **os_type** (string) - The name of the operating system (e.g., "Windows", "Linux", "OSX", "BSD", "POSIX", "Other"). #### Response Example { "os_type": "Linux" } ``` -------------------------------- ### Path Module: Get Path Separator Source: https://luvit.io/api/path Returns the default path separator for the filesystem. This is typically '/' for POSIX systems and '\' for Windows. ```javascript const path = require('path'); const separator = path.getSep(); console.log(`The path separator is: ${separator}`); ``` -------------------------------- ### Stream.WritableState Methods Source: https://luvit.io/api/all Documentation for the initialize method of the Stream.WritableState class. ```APIDOC ## WritableState:initialize(options, stream) ### Description Initializes the WritableState with given options and stream. Options table: - HighWaterMark - Defaults to 16, maxes out at 128MB. 128MB limit cannot be overwritten without modifying luvit/deps/stream/stream_readable - objectMode - If false/nil then the highWaterMark is set to 16 * 1024 ### Method `initialize` ### Endpoint `WritableState:initialize(options, stream)` ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Request Example ```json { "example": "WritableState initialize example" } ``` ### Response #### Success Response (200) - None #### Response Example ```json { "example": "WritableState initialize response" } ``` ``` -------------------------------- ### Buffer Module: toString Method Source: https://luvit.io/api/all Converts a portion or the entirety of a Buffer object into a string. Optionally accepts start (i) and end (j) indices to specify the substring range. ```lua local buf = Buffer.from('hello world') print(buf:toString()) -- Output: hello world print(buf:toString(6, 11)) -- Output: world ``` -------------------------------- ### Filesystem Operations API Source: https://luvit.io/api/all This section covers various filesystem operations provided by the Luvit.io API, including creating symbolic links, reading symbolic links, and changing file ownership. ```APIDOC ## fs.symlink ### Description Creates a symbolic link (soft link) instead of a hard link. ### Method Asynchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **path** (string) - Required - The original path. - **newPath** (string) - Required - The path for the new symbolic link. - **option** (object) - Optional - Options for the symlink creation. - **callback** (function) - Required - The callback function to handle the result. ### Request Example ```javascript fs.symlink(path, newPath, [options], callback); ``` ### Response #### Success Response No direct response, operation is asynchronous. #### Response Example N/A ``` ```APIDOC ## fs.symlinkSync ### Description Synchronously creates a symbolic link (soft link). ### Method Synchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **path** (string) - Required - The original path. - **newPath** (string) - Required - The path for the new symbolic link. - **options** (object) - Optional - Options for the symlink creation. ### Request Example ```javascript fs.symlinkSync(path, newPath, [options]); ``` ### Response #### Success Response No direct response, operation is synchronous. #### Response Example N/A ``` ```APIDOC ## fs.readlink ### Description Asynchronously reads the target of a symbolic link. The callback receives an error and the link string. ### Method Asynchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **path** (string) - Required - The path to the symbolic link. - **callback** (function) - Required - The callback function with arguments (err, linkString). ### Request Example ```javascript fs.readlink(path, callback); ``` ### Response #### Success Response The target path of the symbolic link. #### Response Example ```json "/path/to/original/file" ``` ``` ```APIDOC ## fs.readlinkSync ### Description Synchronously reads the target of a symbolic link. ### Method Synchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **path** (string) - Required - The path to the symbolic link. ### Request Example ```javascript fs.readlinkSync(path); ``` ### Response #### Success Response The target path of the symbolic link. #### Response Example ```json "/path/to/original/file" ``` ``` ```APIDOC ## fs.chown ### Description Asynchronously changes the ownership (UID and GID) of a file. ### Method Asynchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **path** (string) - Required - The path to the file. - **uid** (number) - Required - The new user ID. - **gid** (number) - Required - The new group ID. - **callback** (function) - Required - The callback function to handle the result. ### Request Example ```javascript fs.chown(path, uid, gid, callback); ``` ### Response #### Success Response No direct response, operation is asynchronous. #### Response Example N/A ``` ```APIDOC ## fs.chownSync ### Description Synchronously changes the ownership (UID and GID) of a file. ### Method Synchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **path** (string) - Required - The path to the file. - **uid** (number) - Required - The new user ID. - **gid** (number) - Required - The new group ID. ### Request Example ```javascript fs.chownSync(path, uid, gid); ``` ### Response #### Success Response No direct response, operation is synchronous. #### Response Example N/A ``` ```APIDOC ## fs.fchown ### Description Asynchronously changes the ownership (UID and GID) of a file using a file descriptor. ### Method Asynchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **fd** (number) - Required - The file descriptor. - **uid** (number) - Required - The new user ID. - **gid** (number) - Required - The new group ID. - **callback** (function) - Required - The callback function to handle the result. ### Request Example ```javascript fs.fchown(fd, uid, gid, callback); ``` ### Response #### Success Response No direct response, operation is asynchronous. #### Response Example N/A ``` ```APIDOC ## fs.fchownSync ### Description Synchronously changes the ownership (UID and GID) of a file using a file descriptor. ### Method Synchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **fd** (number) - Required - The file descriptor. - **uid** (number) - Required - The new user ID. - **gid** (number) - Required - The new group ID. ### Request Example ```javascript fs.fchownSync(fd, uid, gid); ``` ### Response #### Success Response No direct response, operation is synchronous. #### Response Example N/A ``` ```APIDOC ## fs.readFile ### Description Reads the content of a file into a string buffer asynchronously. Works with virtual filesystems as well. ### Method Asynchronous ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **path** (string) - Required - The path to the file. - **callback** (function) - Required - The callback function with arguments (err, buffer). ### Request Example ```javascript fs.readFile(path, callback); ``` ### Response #### Success Response A string buffer containing the file content. #### Response Example ```json "File content as a string" ``` ``` -------------------------------- ### File System Operations Source: https://luvit.io/api/all This section covers various asynchronous and synchronous file system operations, including getting file status, renaming files, and data synchronization. ```APIDOC ## GET /fs/fstatSync ### Description Synchronously retrieve file status information. ### Method GET ### Endpoint /fs/fstatSync ### Parameters #### Query Parameters - **fd** (number) - Required - File descriptor for the file. ### Response #### Success Response (200) - **stats** (object) - An object containing file statistics. #### Response Example ```json { "dev": 21, "ino": 54227, "mode": 33188, "nlink": 1, "uid": 84, "gid": 52, "rdev": 0, "size": 1024, "blksize": 1024, "blocks": 8, "atime": "2013-08-17T03:24:04.000Z", "mtime": "2013-07-19T21:37:22.000Z", "ctime": "2013-07-19T21:37:22.000Z" } ``` ## GET /fs/lstat ### Description Retrieves file status information for a given path. If the path is a symbolic link, the link itself is stat-ed, not the file it refers to. This is an asynchronous operation. ### Method GET ### Endpoint /fs/lstat ### Parameters #### Query Parameters - **path** (string) - Required - The path to the file or directory. - **callback** (function) - Required - A callback function to handle the result. ### Response #### Success Response (200) - **stats** (object) - An object containing file statistics. #### Response Example ```json { "dev": 21, "ino": 54227, "mode": 33188, "nlink": 1, "uid": 84, "gid": 52, "rdev": 0, "size": 1024, "blksize": 1024, "atime": "2013-08-17T03:24:04.000Z", "mtime": "2013-07-19T21:37:22.000Z", "ctime": "2013-07-19T21:37:22.000Z" } ``` ## GET /fs/lstatSync ### Description Synchronously retrieves file status information for a given path. If the path is a symbolic link, the link itself is stat-ed, not the file it refers to. ### Method GET ### Endpoint /fs/lstatSync ### Parameters #### Query Parameters - **path** (string) - Required - The path to the file or directory. ### Response #### Success Response (200) - **stats** (object) - An object containing file statistics. #### Response Example ```json { "dev": 21, "ino": 54227, "mode": 33188, "nlink": 1, "uid": 84, "gid": 52, "rdev": 0, "size": 1024, "blksize": 1024, "atime": "2013-08-17T03:24:04.000Z", "mtime": "2013-07-19T21:37:22.000Z", "ctime": "2013-07-19T21:37:22.000Z" } ``` ## POST /fs/rename ### Description Renames a file or directory located at the given path to the new path. The callback is called with either the error or true upon completion. ### Method POST ### Endpoint /fs/rename ### Parameters #### Request Body - **path** (string) - Required - The current path of the file or directory. - **newPath** (string) - Required - The new path for the file or directory. - **callback** (function) - Required - A callback function to handle the result. ### Request Example ```json { "path": "/old/path/file.txt", "newPath": "/new/path/file.txt", "callback": "(err) => console.log(err)" } ``` ### Response #### Success Response (200) - **success** (boolean) - Indicates if the rename operation was successful. #### Response Example ```json { "success": true } ``` ## POST /fs/renameSync ### Description Synchronously renames a file or directory located at the given path to the new path. ### Method POST ### Endpoint /fs/renameSync ### Parameters #### Request Body - **path** (string) - Required - The current path of the file or directory. - **newPath** (string) - Required - The new path for the file or directory. ### Request Example ```json { "path": "/old/path/file.txt", "newPath": "/new/path/file.txt" } ``` ### Response #### Success Response (200) - **success** (boolean) - Indicates if the rename operation was successful. #### Response Example ```json { "success": true } ``` ## POST /fs/fsync ### Description Asynchronously synchronizes file data and metadata to disk. No arguments other than a possible exception are given to the completion callback. ### Method POST ### Endpoint /fs/fsync ### Parameters #### Request Body - **fd** (number) - Required - File descriptor of the file to synchronize. - **callback** (function) - Required - A callback function to handle the result. ### Request Example ```json { "fd": 1, "callback": "(err) => console.log(err)" } ``` ### Response #### Success Response (200) - **success** (boolean) - Indicates if the fsync operation was successful. #### Response Example ```json { "success": true } ``` ## POST /fs/fsyncSync ### Description Synchronously synchronizes file data and metadata to disk. ### Method POST ### Endpoint /fs/fsyncSync ### Parameters #### Request Body - **fd** (number) - Required - File descriptor of the file to synchronize. ### Request Example ```json { "fd": 1 } ``` ### Response #### Success Response (200) - **success** (boolean) - Indicates if the fsync operation was successful. #### Response Example ```json { "success": true } ``` ## POST /fs/fdatasync ### Description Asynchronously synchronizes file data to disk, similar to fsync(), but without flushing all modified metadata unless necessary for subsequent data retrieval. This can reduce disk activity. ### Method POST ### Endpoint /fs/fdatasync ### Parameters #### Request Body - **fd** (number) - Required - File descriptor of the file to synchronize. - **callback** (function) - Required - A callback function to handle the result. ### Request Example ```json { "fd": 1, "callback": "(err) => console.log(err)" } ``` ### Response #### Success Response (200) - **success** (boolean) - Indicates if the fdatasync operation was successful. #### Response Example ```json { "success": true } ``` ``` -------------------------------- ### Control UDP Socket Receiving Source: https://luvit.io/api/dgram Provides methods to start and stop receiving datagrams on a UDP socket. 'recvStart()' enables listening, while 'recvStop()' disables it. ```lua socket:recvStart() socket:recvStop() ```