Skip to content

Commit

Permalink
fix(scripts): remove lowercase headers access (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
gorillamoe authored Oct 5, 2024
1 parent c7d258d commit 757f87c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
13 changes: 13 additions & 0 deletions docs/docs/usage/public-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ Additionally, you can clear a list of global variables by
passing a table of variable names like so:
`require('kulala').scripts_clear_global({'variable_name1', 'variable_name2'})`.

### clear_cached_files

`require('kulala').clear_cached_files()`
clears all cached files.

These files include:

- last response body
- last response headers
- last request data
- global variables set via scripts
- compiled pre- and post-request scripts

### download_graphql_schema

You can download the schema of a GraphQL server with:
Expand Down
2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local M = {}

local plugin_tmp_dir = FS.get_plugin_tmp_dir()

M.VERSION = "4.0.3"
M.VERSION = "4.0.4"
M.UI_ID = "kulala://ui"
M.SCRATCHPAD_ID = "kulala://scratchpad"
M.HEADERS_FILE = plugin_tmp_dir .. "/headers.txt"
Expand Down
7 changes: 7 additions & 0 deletions lua/kulala/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local JUMPS = require("kulala.jumps")
local Graphql = require("kulala.graphql")
local Logger = require("kulala.logger")
local ScriptsUtils = require("kulala.parser.scripts.utils")
local Fs = require("kulala.utils.fs")
local M = {}

M.setup = function(config)
Expand Down Expand Up @@ -101,4 +102,10 @@ M.set_selected_env = function(env)
vim.g.kulala_selected_env = env
end

---Clears all cached files
---Useful when you want to clear all cached files
M.clear_cached_files = function()
Fs.clear_cached_files()
end

return M
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ if (fs.existsSync(_RESPONSE_HEADERS_FILEPATH)) {
continue;
}
const [key] = line.split(delimiter);
const lkey = key.toLowerCase();
headers[lkey] = {
headers[key] = {
name: key,
value: line.slice(key.length + delimiter.length).trim()
}
Expand All @@ -44,16 +43,14 @@ export const Response = {
body,
headers: {
valueOf: (headerName: string): string | null => {
const lkey = headerName.toLowerCase();
if (lkey in headers) {
return headers[lkey].value;
if (headerName in headers) {
return headers[headerName].value;
}
return null;
},
valuesOf: function (headerName: string): HeaderObject | null {
const lkey = headerName.toLowerCase();
if (lkey in headers) {
return headers[lkey];
if (headerName in headers) {
return headers[headerName];
}
return null;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ export const Request = {
},
headers: {
findByName: (headerName: string) => {
const hnl = headerName.toLowerCase();
return getHeaderObject(hnl, req.headers_raw[hnl], req.headers[hnl]);
return getHeaderObject(headerName, req.headers_raw[headerName], req.headers[headerName]);
},
all: function (): HeaderObject[] {
const h = [];
for (const [key, value] of Object.entries(req.headers)) {
const lkey = key.toLowerCase();
const item = getHeaderObject(lkey, req.headers_raw[lkey], value);
const item = getHeaderObject(key, req.headers_raw[key], value);
if (item !== null) {
h.push(item);
}
Expand Down
26 changes: 23 additions & 3 deletions lua/kulala/utils/fs.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Logger = require("kulala.logger")
local M = {}

---Get the OS
Expand Down Expand Up @@ -198,7 +199,11 @@ M.get_request_scripts_dir = function()
return dir
end

M.delete_files_in_directory = function(dir)
---Delete all files in a directory
---@param dir string
---@param verbose boolean|nil
---@usage fs.delete_files_in_directory('tmp', true)
M.delete_files_in_directory = function(dir, verbose)
-- Open the directory for scanning
local scandir = vim.loop.fs_scandir(dir)
if scandir then
Expand All @@ -208,13 +213,16 @@ M.delete_files_in_directory = function(dir)
if not name then
break
end
-- Only delete files, not directories
if type == "file" then
-- Only delete files, not directories except .gitingore
if type == "file" and name:match(".gitignore$") == nil then
local filepath = M.join_paths(dir, name)
local success, err = vim.loop.fs_unlink(filepath)
if not success then
print("Error deleting file:", filepath, err)
end
if verbose and success then
Logger.info("Deleted file: " .. filepath)
end
end
end
else
Expand Down Expand Up @@ -316,4 +324,16 @@ M.read_file_lines = function(filename)
return lines
end

---Clears all cached files
M.clear_cached_files = function()
local tmp_dir = M.get_plugin_tmp_dir()
local scripts_dir = M.get_tmp_scripts_dir()
local request_scripts_dir = M.get_request_scripts_dir()
local compiled_pre_request_scripts = M.join_paths(M.get_scripts_dir(), "engines", "javascript", "lib", "dist")
M.delete_files_in_directory(tmp_dir, true)
M.delete_files_in_directory(scripts_dir, true)
M.delete_files_in_directory(request_scripts_dir, true)
M.delete_files_in_directory(compiled_pre_request_scripts, true)
end

return M

0 comments on commit 757f87c

Please sign in to comment.