From 77e021d82893587d1ab0152476b56ab4b8150c70 Mon Sep 17 00:00:00 2001 From: Tarun Koyalwar Date: Sat, 16 Sep 2023 01:50:47 +0530 Subject: [PATCH] add fs module --- v2/pkg/js/compiler/compiler.go | 1 + v2/pkg/js/generated/go/libfs/fs.go | 33 ++++++++++++++++++ v2/pkg/js/generated/js/libfs/fs.js | 54 ++++++++++++++++++++++++++++++ v2/pkg/js/global/exports.js | 1 + v2/pkg/js/global/js/dump.js | 5 +++ 5 files changed, 94 insertions(+) create mode 100644 v2/pkg/js/generated/go/libfs/fs.go create mode 100644 v2/pkg/js/generated/js/libfs/fs.js diff --git a/v2/pkg/js/compiler/compiler.go b/v2/pkg/js/compiler/compiler.go index 360071e10d..9128b03ffc 100644 --- a/v2/pkg/js/compiler/compiler.go +++ b/v2/pkg/js/compiler/compiler.go @@ -12,6 +12,7 @@ import ( "github.com/projectdiscovery/gologger" _ "github.com/projectdiscovery/nuclei/v2/pkg/js/generated/go/libbytes" + _ "github.com/projectdiscovery/nuclei/v2/pkg/js/generated/go/libfs" _ "github.com/projectdiscovery/nuclei/v2/pkg/js/generated/go/libikev2" _ "github.com/projectdiscovery/nuclei/v2/pkg/js/generated/go/libkerberos" _ "github.com/projectdiscovery/nuclei/v2/pkg/js/generated/go/libldap" diff --git a/v2/pkg/js/generated/go/libfs/fs.go b/v2/pkg/js/generated/go/libfs/fs.go new file mode 100644 index 0000000000..0aac05f7eb --- /dev/null +++ b/v2/pkg/js/generated/go/libfs/fs.go @@ -0,0 +1,33 @@ +package fs + +import ( + lib_fs "github.com/projectdiscovery/nuclei/v2/pkg/js/libs/fs" + + "github.com/dop251/goja" + "github.com/projectdiscovery/nuclei/v2/pkg/js/gojs" +) + +var ( + module = gojs.NewGojaModule("nuclei/fs") +) + +func init() { + module.Set( + gojs.Objects{ + // Functions + "ListDir": lib_fs.ListDir, + "ReadFile": lib_fs.ReadFile, + "ReadFileAsString": lib_fs.ReadFileAsString, + + // Var and consts + + // Types (value type) + + // Types (pointer type) + }, + ).Register() +} + +func Enable(runtime *goja.Runtime) { + module.Enable(runtime) +} diff --git a/v2/pkg/js/generated/js/libfs/fs.js b/v2/pkg/js/generated/js/libfs/fs.js new file mode 100644 index 0000000000..339b3ee696 --- /dev/null +++ b/v2/pkg/js/generated/js/libfs/fs.js @@ -0,0 +1,54 @@ +/** + * @module fs + */ + +/** + * @method + * @name ListDir + * @param {string} path - The path of the directory. + * @param {string} itemType - The type of the item (values => all/file/dir) + * @throws {Error} If an error occurred while listing the directory. + * @example + * // Usage of ListDir + * let m = require('nuclei/fs'); + * let files = ListDir('helpers', 'file'); + */ +function ListDir(path, itemType) { + // implemented in go +} + +/** + * @method + * @name ReadFile + * @param {string} path - The path of the file. + * @throws {Error} If an error occurred while reading the file. + * @return {Buffer} The content of the file. + * @example + * // Usage of ReadFile + * let m = require('nuclei/fs'); + * let content = ReadFile('helpers/usernames.txt'); + */ +function ReadFile(path) { + // implemented in go +} + +/** + * @method + * @name ReadFileAsString + * @param {string} path - The path of the file. + * @throws {Error} If an error occurred while reading the file. + * @return {string} The content of the file as a string. + * @example + * // Usage of ReadFileAsString + * let m = require('nuclei/fs'); + * let content = ReadFileAsString('helpers/usernames.txt'); + */ +function ReadFileAsString(path) { + // implemented in go +} + +module.exports = { + ListDir: ListDir, + ReadFile: ReadFile, + ReadFileAsString: ReadFileAsString, +}; \ No newline at end of file diff --git a/v2/pkg/js/global/exports.js b/v2/pkg/js/global/exports.js index b15a3dd813..8cd80c0b71 100644 --- a/v2/pkg/js/global/exports.js +++ b/v2/pkg/js/global/exports.js @@ -2,6 +2,7 @@ exports = { // General dump_json: dump_json, to_json: to_json, + to_array: to_array, hex_to_ascii: hex_to_ascii, // Active Directory diff --git a/v2/pkg/js/global/js/dump.js b/v2/pkg/js/global/js/dump.js index e5e0a0cedb..141f286782 100644 --- a/v2/pkg/js/global/js/dump.js +++ b/v2/pkg/js/global/js/dump.js @@ -9,6 +9,11 @@ function to_json(data) { return JSON.stringify(data, null, 2); } +// to_array sets object type as array +function to_array(data) { + return Object.setPrototypeOf(data, Array.prototype); +} + // hex_to_ascii converts a hex string to ascii. function hex_to_ascii(str1) { var hex = str1.toString();