@mherod/get-cookie 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils.js DELETED
@@ -1,175 +0,0 @@
1
- // noinspection JSUnusedGlobalSymbols
2
-
3
- const {exec} = require('child_process');
4
- const fs = require("fs");
5
-
6
- async function execSimple(command) {
7
- if (typeof command !== 'string') {
8
- throw new TypeError('execSimple: command must be a string');
9
- }
10
- if (process.env.VERBOSE) {
11
- console.log(command);
12
- }
13
- return await new Promise((resolve, reject) => {
14
- exec(command, {encoding: 'binary', maxBuffer: 5 * 1024}, (error, stdout, stderr) => {
15
- if (error) {
16
- reject(error);
17
- return;
18
- }
19
- if (stderr) {
20
- reject(error);
21
- return;
22
- }
23
- if (stdout) {
24
- resolve(stdout.toString('utf8').trim());
25
- }
26
- });
27
- });
28
- }
29
-
30
- async function execAsBuffer(command) {
31
- if (typeof command !== 'string') {
32
- throw new TypeError('execAsBuffer: command must be a string');
33
- }
34
- if (process.env.VERBOSE) {
35
- console.log(command);
36
- }
37
- return await new Promise((resolve, reject) => {
38
- exec(command, {encoding: 'binary', maxBuffer: 5 * 1024}, (error, stdout, stderr) => {
39
- if (error) {
40
- reject(error);
41
- return;
42
- }
43
- if (stderr) {
44
- reject(error);
45
- return;
46
- }
47
- let stdoutAsBuffer = stdout;
48
- if (typeof stdoutAsBuffer === 'string' && stdoutAsBuffer.length > 0) {
49
- // noinspection JSCheckFunctionSignatures
50
- stdoutAsBuffer = Buffer.from(stdoutAsBuffer, 'binary').slice(0, -1);
51
- }
52
- if (stdoutAsBuffer && stdoutAsBuffer.length > 0) {
53
- resolve(stdoutAsBuffer);
54
- }
55
- });
56
- });
57
- }
58
-
59
- function toStringValue(r) {
60
- if (process.env.VERBOSE) {
61
- console.log("Printing value", r);
62
- }
63
- if (r) {
64
- if (typeof r === 'string') {
65
- return r;
66
- } else if (r.toString) {
67
- // noinspection JSCheckFunctionSignatures
68
- return r.toString('utf8');
69
- }
70
- }
71
- }
72
-
73
- function printStringValue(r) {
74
- if (process.env.VERBOSE) {
75
- console.log("Printing value", r);
76
- }
77
- if (r) {
78
- if (typeof r === 'string') {
79
- console.log(r);
80
- } else if (r.toString) {
81
- // noinspection JSCheckFunctionSignatures
82
- console.log(r.toString('utf8'));
83
- }
84
- }
85
- }
86
-
87
- /**
88
- *
89
- * @param result
90
- * @returns {string|null}
91
- */
92
- function toStringOrNull(result) {
93
- if (result == null) {
94
- return null;
95
- }
96
- if (process.env.VERBOSE) {
97
- console.log('result', result);
98
- }
99
- if (typeof result === 'string' && result.length > 0) {
100
- return result;
101
- }
102
- if (result.slice && result.toString) {
103
- // noinspection JSCheckFunctionSignatures
104
- return result.toString('utf8');
105
- }
106
- return null;
107
- }
108
-
109
- /**
110
- *
111
- * @param {string} file
112
- * @param {string} sql
113
- * @returns {Promise<Buffer[]>}
114
- */
115
- async function doSqliteQuery1(file, sql) {
116
- if (typeof sql !== 'string') {
117
- throw new TypeError('doSqliteQuery1: sql must be a string');
118
- }
119
- if (typeof file !== 'string') {
120
- throw new TypeError('doSqliteQuery1: file must be a string');
121
- }
122
- if (!fs.existsSync(file)) {
123
- throw new Error(`doSqliteQuery1: file ${file} does not exist`);
124
- }
125
- if (process.env.VERBOSE) {
126
- console.log(`doSqliteQuery1: file ${file}`);
127
- console.log(`doSqliteQuery1: sql ${sql}`);
128
- }
129
- const sqlite3 = require('sqlite3');
130
- const db = new sqlite3.Database(file);
131
- return new Promise((resolve, reject) => {
132
- db.all(sql, (err, rows) => {
133
- if (err) {
134
- if (process.env.VERBOSE) {
135
- console.log(`doSqliteQuery1: error ${err}`);
136
- }
137
- reject(err);
138
- return;
139
- }
140
- const rows1 = rows;
141
- if (rows1 == null || rows1.length === 0) {
142
- if (process.env.VERBOSE) {
143
- console.log(`doSqliteQuery1: no rows`);
144
- }
145
- resolve([]);
146
- return;
147
- }
148
- if (Array.isArray(rows1)) {
149
- if (process.env.VERBOSE) {
150
- console.log(`doSqliteQuery1: ${rows1.length} rows`);
151
- }
152
- // noinspection JSCheckFunctionSignatures
153
- const buffers = rows1.flatMap(row => Object.values(row)).map(v => Buffer.from(v, 'binary'));
154
- if (process.env.VERBOSE) {
155
- console.log(`doSqliteQuery1: ${buffers.length} buffers`);
156
- }
157
- resolve(buffers);
158
- return;
159
- }
160
- if (process.env.VERBOSE) {
161
- console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
162
- }
163
- resolve([rows1]);
164
- });
165
- });
166
- }
167
-
168
- module.exports = {
169
- execSimple: execSimple,
170
- execAsBuffer: execAsBuffer,
171
- printStringValue: printStringValue,
172
- toStringValue,
173
- toStringOrNull: toStringOrNull,
174
- doSqliteQuery1
175
- };