diff --git a/navigator/src/functions/getDirListing.js b/navigator/src/functions/getDirListing.js deleted file mode 100644 index edf743e..0000000 --- a/navigator/src/functions/getDirListing.js +++ /dev/null @@ -1,70 +0,0 @@ -import { useSpawn, errorString } from "@45drives/cockpit-helpers"; - -/** - * Callback for handling errors during parsing of `dir` output lines - * - * @callback getDirListingFailCallback - * @param {String} message - what went wrong - */ - -/** - * Get list of directory entry names for given path - * - * @param {String} path - Directory path to list - * @param {getDirListingFailCallback} failCallback - Callback function for handling errors, receives {String} message - * @returns {Promise} - */ -async function getDirListing(path, host, failCallback) { - return parseRawDirListing( - ( - await useSpawn([ - 'dir', - '--almost-all', - '--dereference-command-line-symlink-to-dir', - '--quoting-style=c', - '-1', - path - ], { superuser: 'try', host }).promise() - ).stdout, - failCallback - ); -} - -/** - * Parse raw output of `dir` call from {@link getDirListing()} - * - * @param {String} raw - Raw output of `dir` from {@link getDirListing()} - * @param {getDirListingFailCallback} failCallback - Callback function for handling errors, receives {String} message - * @returns {String[]} - */ -function parseRawDirListing(raw, failCallback) { - const decoder = new TextDecoder(); - return raw.split('\n') - .filter(name => name) - .map(escaped => { - try { - return JSON.parse( - escaped.replace( - /(\\\d{1,3})+/g, - octs => decoder.decode( - new Uint8Array( - octs - .split('\\') - .slice(1) - .map(oct => parseInt(oct, 8) - ) - ) - ) - ) - ); - } catch (error) { - failCallback?.(`${errorString(error)}\ncaused by ${escaped}`); - return null; - } - }) - .filter(entry => entry !== null); -} - -export { parseRawDirListing } - -export default getDirListing; diff --git a/navigator/src/functions/getDirListing.test.js b/navigator/src/functions/getDirListing.test.js deleted file mode 100644 index f6f8b25..0000000 --- a/navigator/src/functions/getDirListing.test.js +++ /dev/null @@ -1,79 +0,0 @@ -import { parseRawDirListing } from "./getDirListing"; - -describe('the getDirListing function', () => { - it('can parse output of dir', () => { - // `dir --almost-all --dereference-command-line-symlink-to-dir --quoting-style=c -1` - const input = - `"test" -"sentence test" -"special\\\\characters\\n\\t" -"😀" -`; - const expectedOutput = [ - "test", - "sentence test", - "special\\characters\n\t", - "😀" - ] - const output = parseRawDirListing(input); - expect(output).toEqual(expectedOutput); - expect(output[1]).toBe("sentence test"); - }); - - it('can parse real output of dir', () => { - const input = `"{}" - "\\360\\237\\230\\200" - "~$lock" - "0" - "broken" - "dir" - "dst" - "escape" - "escape\\ndir" - "escapendir" - "hello world" - "hello\\\\world" - "iterate_ips" - "md5" - "messed" - "name" - "pdf_numn" - "src" - "systemd" - "test" - "test\\nescape\\tname"`; - const expectedOutput = [ - "{}", - "😀", - "~$lock", - "0", - "broken", - "dir", - "dst", - "escape", - "escape\ndir", - "escapendir", - "hello world", - "hello\\world", - "iterate_ips", - "md5", - "messed", - "name", - "pdf_numn", - "src", - "systemd", - "test", - "test\nescape\tname", - ] - const errorCallback = jest.fn((message) => console.error(message)); - expect(errorCallback.mock.calls.length).toBe(0); - expect(parseRawDirListing(input, errorCallback)).toEqual(expectedOutput); - }); - - it('can report errors with callback', () => { - const errorCallback = jest.fn(); - const input = "error: example error"; - parseRawDirListing(input, errorCallback); - expect(errorCallback.mock.calls.length).toBe(1); - }) -});