Skip to content

Commit

Permalink
add fs module
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunKoyalwar committed Sep 15, 2023
1 parent 4fd320a commit 77e021d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions v2/pkg/js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 33 additions & 0 deletions v2/pkg/js/generated/go/libfs/fs.go
Original file line number Diff line number Diff line change
@@ -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)
}
54 changes: 54 additions & 0 deletions v2/pkg/js/generated/js/libfs/fs.js
Original file line number Diff line number Diff line change
@@ -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,
};
1 change: 1 addition & 0 deletions v2/pkg/js/global/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions v2/pkg/js/global/js/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 77e021d

Please sign in to comment.