Skip to content

Commit

Permalink
Encode JSON safely
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 21, 2024
1 parent c5d31b8 commit 812e4a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
42 changes: 17 additions & 25 deletions lua/fittencode/key_storage.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
local fn = vim.fn
local uv = vim.uv or vim.loop

local Base = require('fittencode.base')
local FS = require('fittencode.fs')
local Log = require('fittencode.log')

local schedule = Base.schedule

---@class Key
---@field name string|nil
---@field key string|nil
Expand Down Expand Up @@ -33,48 +36,41 @@ function KeyStorage:load(on_success, on_error)
Log.debug('Loading key file: {}', self.path)
if not FS.exists(self.path) then
Log.error('Key file not found')
if on_error then
on_error()
end
schedule(on_error)
return
end
FS.read(self.path, function(data)
local success, result = pcall(fn.json_decode, data)
if success == false then
Log.error('Failed to parse key file; error: {}', result)
if on_error then
on_error()
end
schedule(on_error)
return
end
self.keys = result
Log.debug('Key file loaded successful')
if on_success ~= nil then
on_success(self.keys.name)
end
schedule(on_success, self.keys.name)
end, function(err)
Log.error('Failed to load Key file; error: {}', err)
if on_error then
on_error()
end
schedule(on_error)
end)
end

---@param on_success function|nil
---@param on_error function|nil
function KeyStorage:save(on_success, on_error)
Log.debug('Saving key file: {}', self.path)
local encode_keys = fn.json_encode(self.keys)
local success, encode_keys = pcall(fn.json_encode, self.keys)
if not success then
Log.error('Failed to encode key file; error: {}', encode_keys)
schedule(on_error)
return
end
FS.write_mkdir(encode_keys, self.path, function()
Log.info('Key file saved successful')
if on_success ~= nil then
on_success()
end
schedule(on_success)
end, function(err)
Log.error('Failed to save key file; error: {}', err)
if on_error then
on_error()
end
schedule(on_error)
end)
end

Expand All @@ -86,14 +82,10 @@ function KeyStorage:clear(on_success, on_error)
uv.fs_unlink(self.path, function(err)
if err then
Log.error('Failed to delete key file; error: {}', err)
if on_error then
on_error()
end
schedule(on_error)
else
Log.info('Delete key file successful')
if on_success then
on_success()
end
schedule(on_success)
end
end)
end
Expand Down
7 changes: 6 additions & 1 deletion lua/fittencode/rest/backend/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ local function post_largedata(url, encoded_data, on_success, on_error)
end

function M:post(url, data, on_success, on_error)
local encoded_data = fn.json_encode(data)
local success, encoded_data = pcall(fn.json_encode, data)
if not success then
Log.error('Failed to encode data: {}', data)
schedule(on_error)
return
end
if #encoded_data > 200 then
return post_largedata(url, encoded_data, on_success, on_error)
end
Expand Down

0 comments on commit 812e4a5

Please sign in to comment.