1 /* nodeutil-1.0.2 (c) 2015-2021 Kenji Urushima | kjur.github.io/jsrsasign/license
  2  */
  3 /*
  4  * nodeutil.js - Utilities for Node
  5  *
  6  * Copyright (c) 2015-2021 Kenji Urushima (kenji.urushima@gmail.com)
  7  *
  8  * This software is licensed under the terms of the MIT License.
  9  * https://kjur.github.io/jsrsasign/license/
 10  *
 11  * The above copyright and license notice shall be 
 12  * included in all copies or substantial portions of the Software.
 13  */
 14 
 15 /**
 16  * @fileOverview
 17  * @name nodeutil-1.0.js
 18  * @author Kenji Urushima kenji.urushima@gmail.com
 19  * @version jsrsasign-util 1.0.3 nodeutil 1.0.2 (2021-Feb-15)
 20  * @since jsrsasign 5.0.2
 21  * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a>
 22  */
 23 var fs = require("fs");
 24 var JSONC = require("jsonc-parser");
 25 var rs = require("jsrsasign");
 26 
 27 /**
 28  * read file and return file contents as utf-8 string
 29  * @param {String} utf8File file name to be read
 30  * @return {String} utf-8 string of file contents
 31  * @description
 32  * This function only works in Node.js.
 33  */
 34 function readFileUTF8(utf8File) {
 35     return require('fs').readFileSync(utf8File, 'utf8');
 36 }
 37 
 38 /**
 39  * read binary file and return file contents as hexadecimal string
 40  * @param {String} binFile file name to be read
 41  * @return {String} hexadecimal string of file contents
 42  * @description
 43  * This function only works in Node.js.
 44  */
 45 function readFileHexByBin(binFile) {
 46     return rs.rstrtohex(fs.readFileSync(binFile, 'binary'));
 47 }
 48 
 49 /**
 50  * read file and return file contents
 51  * @param {String} binFile file name to be read
 52  * @return {String} raw string of file contents
 53  * @description
 54  * This function only works in Node.js.
 55  */
 56 function readFile(binFile) {
 57     return fs.readFileSync(binFile, 'binary');
 58 }
 59 
 60 /**
 61  * save raw string to file
 62  * @param {String} binFile file name to save contents.
 63  * @param {String} rawString string contents to be saved.
 64  * @description
 65  * This function only works in Node.js.
 66  */
 67 function saveFile(binFile, rawString) {
 68     var fs = require('fs');
 69     fs.writeFileSync(binFile, rawString, 'binary');
 70 }
 71 
 72 /**
 73  * save UTF-8 string to file
 74  * @param {String} binFile file name to save contents.
 75  * @param {String} utf8String string contents to be saved.
 76  * @description
 77  * This function only works in Node.js.
 78  */
 79 function saveFileUTF8(binFile, utf8String) {
 80     var fs = require('fs');
 81     fs.writeFileSync(binFile, utf8String, 'utf8');
 82 }
 83 
 84 /**
 85  * save data represented by hexadecimal string to file
 86  * @param {String} binFile file name to save contents.
 87  * @param {String} hexString hexadecimal string to be saved.
 88  * @description
 89  * This function only works in Node.js.
 90  */
 91 function saveFileBinByHex(binFile, hexString) {
 92     var rawString = rs.hextorstr(hexString);
 93     fs.writeFileSync(binFile, rawString, 'binary');
 94 }
 95 
 96 /**
 97  * read JSON file and return its JSON object
 98  * @param {String} JSON file name to be read
 99  * @return {Object} JSON object or array of file contents
100  * @since jsrsasign-util 1.0.1 nodeutil 1.0.1
101  *
102  * @description
103  * This function only works in Node.js.
104  * @example
105  * var rsu = require("jsrsasign-util");
106  * rsu.readJSON("aaa.json") → JSON object
107  */
108 function readJSON(jsonFile) {
109     var jsonStr = fs.readFileSync(jsonFile, "utf8");
110     var json = JSON.parse(jsonStr);
111     return json;
112 }
113 
114 /**
115  * read JSONC file and return its JSON object
116  * @param {String} JSONC file name to be read
117  * @return {Object} JSON object or array of file contents
118  * @since jsrsasign-util 1.0.1 nodeutil 1.0.1
119  *
120  * @description
121  * This method read JSONC (i.e. JSON with comments) file
122  * and returns JSON object.
123  * This function only works in Node.js.
124  * 
125  * @example
126  * var rsu = require("jsrsasign-util");
127  * rsu.readJSONC("aaa.jsonc") → JSON object
128  */
129 function readJSONC(jsonFile) {
130     var jsonStr = fs.readFileSync(jsonFile, "utf8");
131     var json = JSONC.parse(jsonStr);
132     return json;
133 }
134 
135 /**
136  * save JSON object as file
137  * @param {Object} jsonFile output JSON file name
138  * @param {Object} json JSON object to save
139  * @since jsrsasign-util 1.0.1 nodeutil 1.0.1
140  *
141  * @description
142  * This method saves JSON object as a file.
143  * This function only works in Node.js.
144  * 
145  * @example
146  * var rsu = require("jsrsasign-util");
147  * rsu.saveFileJSON("aaa.jsonc", json);
148  */
149 function saveFileJSON(jsonFile, json) {
150     var s = JSON.stringify(json, null, "  ");
151     saveFileUTF8(jsonFile, s);
152 }
153 
154 /**
155  * output JSON object to console
156  * @param {Object} json JSON object to print out
157  * @param {Object} prefix prefix string (OPTION)
158  * @since jsrsasign-util 1.0.1 nodeutil 1.0.1
159  *
160  * @description
161  * This method writes JSON object to console.
162  * This function only works in Node.js.
163  * 
164  * @example
165  * var rsu = require("jsrsasign-util");
166  * var obj = {aaa: "bbb", "ccc": 123};
167  * rsu.printJSON(obj, "obj = ") →
168  * obj = {
169  *   "aaa": "bbb",
170  *   "ccc": 123
171  * }
172  */
173 function printJSON(json, prefix) {
174     var s = "";
175     if (prefix != undefined) s = prefix;
176     console.log(s + JSON.stringify(json, null, "  "));
177 }
178 
179