script

package
v0.17.44 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2023 License: Apache-2.0, MIT Imports: 18 Imported by: 2

Documentation

Overview

Package script provides lua scripting for skipper

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// InitialPoolSize is the number of lua states created initially per route
	InitialPoolSize int = 3

	// MaxPoolSize is the number of lua states stored per route - there may be more parallel
	// requests, but only this number is cached.
	MaxPoolSize int = 10
)

Functions

func NewLuaScript

func NewLuaScript() filters.Spec

NewLuaScript creates a new filter spec

func NewLuaScriptWithOptions added in v0.14.0

func NewLuaScriptWithOptions(opts LuaOptions) (filters.Spec, error)

NewLuaScriptWithOptions creates a new filter spec with options

Types

type LuaOptions added in v0.14.0

type LuaOptions struct {
	// Modules configures enabled standard and additional (preloaded) Lua modules.
	// For standard Lua modules (see https://www.lua.org/manual/5.1/manual.html)
	// use "<module>.<symbol>" (e.g. "base.print") to selectively enable module symbols.
	// Additional modules are preloaded with all symbols.
	// Empty value enables all modules.
	Modules []string
	// Sources that are allowed as input sources. Valid sources
	// are "none", "file" and "inline". Empty slice will enable
	// both as default. To disable the use of lua filters use
	// "none".
	Sources []string
}
Example (Default)
runExampleWithOptions(
	LuaOptions{},
	testScript(`
			local base64 = require("base64")

			function request(ctx, params)
				print(table.concat({"Hello", "World"}, " "))
				print(string.lower("Hello World"))
				print(math.abs(-1))
				print(base64.encode("Hello World"))
			end
		`),
)
Output:

Hello World
hello world
1
SGVsbG8gV29ybGQ=
Example (DisableAll)
runExampleWithOptions(
	LuaOptions{
		// use non-existing module
		Modules: []string{"none"},
	},
	testScript(`function request() print("test") end`),
)
Output:

Error calling request from function request() print("test") end: <script>:1: attempt to call a non-function object
stack traceback:
	<script>:1: in main chunk
	[G]: ?
Example (EnableModules)
runExampleWithOptions(
	LuaOptions{
		Modules: []string{
			"base._G", "base.pairs", "base.ipairs", "base.print", "base.require",
			"table.sort", "table.insert",
			// enable all symbols from "package" module as
			// additional preloaded modules require it
			"package",
			// preload additional module
			"base64",
		},
	},
	testScript(printTable+`
			local base64 = require("base64")

			function request()
				printTable("", _G)
				printTable("table.", table)
				printTable("package.", package)
				printTable("package.preload.", package.preload)
			end
		`),
)
Output:

_G
ipairs
package
pairs
print
printTable
request
require
table
table.insert
table.sort
package.config
package.cpath
package.loaded
package.loaders
package.loadlib
package.path
package.preload
package.seeall
package.preload.base64
Example (PrintGlobals)
runExampleWithOptions(
	LuaOptions{},
	testScript(printTable+`
			function request()
				printTable("", _G)
			end
		`),
)
Output:

_G
_GOPHER_LUA_VERSION
_VERSION
_printregs
assert
channel
collectgarbage
coroutine
debug
dofile
error
getfenv
getmetatable
io
ipairs
load
loadfile
loadstring
math
module
newproxy
next
os
package
pairs
pcall
print
printTable
rawequal
rawget
rawset
request
require
select
setfenv
setmetatable
sleep
string
table
tonumber
tostring
type
unpack
xpcall

Directories

Path Synopsis
Package base64 provides an easy way to encode and decode base64
Package base64 provides an easy way to encode and decode base64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL