mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-05-05 19:19:35 +00:00
code: lua,script,sandboxed (2 files)
This commit is contained in:
@@ -7,107 +7,46 @@
|
||||
|
||||
import type { DBALAdapter } from '../../adapters/adapter'
|
||||
import type { LuaScript, ListOptions, ListResult } from '../types'
|
||||
import { DBALError } from '../errors'
|
||||
import { validateLuaScriptCreate, validateLuaScriptUpdate, validateId } from '../validation'
|
||||
import { createLuaScript } from './lua-script/create-lua-script'
|
||||
import { deleteLuaScript } from './lua-script/delete-lua-script'
|
||||
import { getLuaScript } from './lua-script/get-lua-script'
|
||||
import { listLuaScripts } from './lua-script/list-lua-scripts'
|
||||
import { updateLuaScript } from './lua-script/update-lua-script'
|
||||
|
||||
export interface LuaScriptOperations {
|
||||
create: (data: Omit<LuaScript, 'id' | 'createdAt' | 'updatedAt'>) => Promise<LuaScript>
|
||||
read: (id: string) => Promise<LuaScript | null>
|
||||
update: (id: string, data: Partial<LuaScript>) => Promise<LuaScript>
|
||||
delete: (id: string) => Promise<boolean>
|
||||
list: (options?: ListOptions) => Promise<ListResult<LuaScript>>
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Lua script operations object for the DBAL client
|
||||
*/
|
||||
export const createLuaScriptOperations = (adapter: DBALAdapter) => ({
|
||||
export const createLuaScriptOperations = (adapter: DBALAdapter): LuaScriptOperations => ({
|
||||
/**
|
||||
* Create a new Lua script
|
||||
*/
|
||||
create: async (data: Omit<LuaScript, 'id' | 'createdAt' | 'updatedAt'>): Promise<LuaScript> => {
|
||||
const validationErrors = validateLuaScriptCreate(data)
|
||||
if (validationErrors.length > 0) {
|
||||
throw DBALError.validationError(
|
||||
'Invalid Lua script data',
|
||||
validationErrors.map(error => ({ field: 'luaScript', error }))
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
return adapter.create('LuaScript', data) as Promise<LuaScript>
|
||||
} catch (error) {
|
||||
if (error instanceof DBALError && error.code === 409) {
|
||||
throw DBALError.conflict(`Lua script with name '${data.name}' already exists`)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
},
|
||||
create: (data) => createLuaScript(adapter, data),
|
||||
|
||||
/**
|
||||
* Read a Lua script by ID
|
||||
*/
|
||||
read: async (id: string): Promise<LuaScript | null> => {
|
||||
const validationErrors = validateId(id)
|
||||
if (validationErrors.length > 0) {
|
||||
throw DBALError.validationError(
|
||||
'Invalid Lua script ID',
|
||||
validationErrors.map(error => ({ field: 'id', error }))
|
||||
)
|
||||
}
|
||||
|
||||
const result = await adapter.read('LuaScript', id) as LuaScript | null
|
||||
if (!result) {
|
||||
throw DBALError.notFound(`Lua script not found: ${id}`)
|
||||
}
|
||||
return result
|
||||
},
|
||||
read: (id) => getLuaScript(adapter, id),
|
||||
|
||||
/**
|
||||
* Update an existing Lua script
|
||||
*/
|
||||
update: async (id: string, data: Partial<LuaScript>): Promise<LuaScript> => {
|
||||
const idErrors = validateId(id)
|
||||
if (idErrors.length > 0) {
|
||||
throw DBALError.validationError(
|
||||
'Invalid Lua script ID',
|
||||
idErrors.map(error => ({ field: 'id', error }))
|
||||
)
|
||||
}
|
||||
|
||||
const validationErrors = validateLuaScriptUpdate(data)
|
||||
if (validationErrors.length > 0) {
|
||||
throw DBALError.validationError(
|
||||
'Invalid Lua script update data',
|
||||
validationErrors.map(error => ({ field: 'luaScript', error }))
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
return adapter.update('LuaScript', id, data) as Promise<LuaScript>
|
||||
} catch (error) {
|
||||
if (error instanceof DBALError && error.code === 409) {
|
||||
throw DBALError.conflict('Lua script name already exists')
|
||||
}
|
||||
throw error
|
||||
}
|
||||
},
|
||||
update: (id, data) => updateLuaScript(adapter, id, data),
|
||||
|
||||
/**
|
||||
* Delete a Lua script by ID
|
||||
*/
|
||||
delete: async (id: string): Promise<boolean> => {
|
||||
const validationErrors = validateId(id)
|
||||
if (validationErrors.length > 0) {
|
||||
throw DBALError.validationError(
|
||||
'Invalid Lua script ID',
|
||||
validationErrors.map(error => ({ field: 'id', error }))
|
||||
)
|
||||
}
|
||||
|
||||
const result = await adapter.delete('LuaScript', id)
|
||||
if (!result) {
|
||||
throw DBALError.notFound(`Lua script not found: ${id}`)
|
||||
}
|
||||
return result
|
||||
},
|
||||
delete: (id) => deleteLuaScript(adapter, id),
|
||||
|
||||
/**
|
||||
* List Lua scripts with filtering and pagination
|
||||
*/
|
||||
list: async (options?: ListOptions): Promise<ListResult<LuaScript>> => {
|
||||
return adapter.list('LuaScript', options) as Promise<ListResult<LuaScript>>
|
||||
},
|
||||
list: (options) => listLuaScripts(adapter, options),
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import { setupSandboxedEnvironment } from './functions/sandbox/setup-sandboxed-e
|
||||
import { executeWithTimeout } from './functions/sandbox/execute-with-timeout'
|
||||
import { getLuaMemoryUsageBytes } from './functions/sandbox/get-lua-memory-usage-bytes'
|
||||
import { enforceMaxMemory } from './functions/sandbox/enforce-max-memory'
|
||||
import { setAllowedGlobals } from './functions/sandbox/set-allowed-globals'
|
||||
import { setExecutionTimeout } from './functions/sandbox/set-execution-timeout'
|
||||
import { destroy } from './functions/sandbox/destroy'
|
||||
|
||||
@@ -15,11 +16,13 @@ export class SandboxedLuaEngine {
|
||||
engine: LuaEngine | null = null
|
||||
executionTimeout = 5000
|
||||
maxMemory = 10 * 1024 * 1024
|
||||
allowedGlobals: string[] | undefined
|
||||
|
||||
constructor(timeout: number = 5000) {
|
||||
this.executionTimeout = timeout
|
||||
}
|
||||
|
||||
setAllowedGlobals = setAllowedGlobals
|
||||
executeWithSandbox = executeWithSandbox
|
||||
disableDangerousFunctions = disableDangerousFunctions
|
||||
setupSandboxedEnvironment = setupSandboxedEnvironment
|
||||
|
||||
Reference in New Issue
Block a user