You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

rc9

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rc9 - npm Package Compare versions

Comparing version
2.1.1
to
2.1.2
+18
dist/index.d.cts
type RC = Record<string, any>;
interface RCOptions {
name?: string;
dir?: string;
flat?: boolean;
}
declare const defaults: RCOptions;
declare function parse<T extends RC = RC>(contents: string, options?: RCOptions): T;
declare function parseFile<T extends RC = RC>(path: string, options?: RCOptions): T;
declare function read<T extends RC = RC>(options?: RCOptions | string): T;
declare function readUser<T extends RC = RC>(options?: RCOptions | string): T;
declare function serialize<T extends RC = RC>(config: T): string;
declare function write<T extends RC = RC>(config: T, options?: RCOptions | string): void;
declare function writeUser<T extends RC = RC>(config: T, options?: RCOptions | string): void;
declare function update<T extends RC = RC>(config: T, options?: RCOptions | string): T;
declare function updateUser<T extends RC = RC>(config: T, options?: RCOptions | string): T;
export { defaults, parse, parseFile, read, readUser, serialize, update, updateUser, write, writeUser };
type RC = Record<string, any>;
interface RCOptions {
name?: string;
dir?: string;
flat?: boolean;
}
declare const defaults: RCOptions;
declare function parse<T extends RC = RC>(contents: string, options?: RCOptions): T;
declare function parseFile<T extends RC = RC>(path: string, options?: RCOptions): T;
declare function read<T extends RC = RC>(options?: RCOptions | string): T;
declare function readUser<T extends RC = RC>(options?: RCOptions | string): T;
declare function serialize<T extends RC = RC>(config: T): string;
declare function write<T extends RC = RC>(config: T, options?: RCOptions | string): void;
declare function writeUser<T extends RC = RC>(config: T, options?: RCOptions | string): void;
declare function update<T extends RC = RC>(config: T, options?: RCOptions | string): T;
declare function updateUser<T extends RC = RC>(config: T, options?: RCOptions | string): T;
export { defaults, parse, parseFile, read, readUser, serialize, update, updateUser, write, writeUser };
+162
-8

@@ -7,3 +7,2 @@ 'use strict';

const destr = require('destr');
const flat = require('flat');
const defu = require('defu');

@@ -14,4 +13,161 @@

const destr__default = /*#__PURE__*/_interopDefaultCompat(destr);
const flat__default = /*#__PURE__*/_interopDefaultCompat(flat);
function isBuffer (obj) {
return obj &&
obj.constructor &&
(typeof obj.constructor.isBuffer === 'function') &&
obj.constructor.isBuffer(obj)
}
function keyIdentity (key) {
return key
}
function flatten (target, opts) {
opts = opts || {};
const delimiter = opts.delimiter || '.';
const maxDepth = opts.maxDepth;
const transformKey = opts.transformKey || keyIdentity;
const output = {};
function step (object, prev, currentDepth) {
currentDepth = currentDepth || 1;
Object.keys(object).forEach(function (key) {
const value = object[key];
const isarray = opts.safe && Array.isArray(value);
const type = Object.prototype.toString.call(value);
const isbuffer = isBuffer(value);
const isobject = (
type === '[object Object]' ||
type === '[object Array]'
);
const newKey = prev
? prev + delimiter + transformKey(key)
: transformKey(key);
if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
(!opts.maxDepth || currentDepth < maxDepth)) {
return step(value, newKey, currentDepth + 1)
}
output[newKey] = value;
});
}
step(target);
return output
}
function unflatten (target, opts) {
opts = opts || {};
const delimiter = opts.delimiter || '.';
const overwrite = opts.overwrite || false;
const transformKey = opts.transformKey || keyIdentity;
const result = {};
const isbuffer = isBuffer(target);
if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
return target
}
// safely ensure that the key is
// an integer.
function getkey (key) {
const parsedKey = Number(key);
return (
isNaN(parsedKey) ||
key.indexOf('.') !== -1 ||
opts.object
)
? key
: parsedKey
}
function addKeys (keyPrefix, recipient, target) {
return Object.keys(target).reduce(function (result, key) {
result[keyPrefix + delimiter + key] = target[key];
return result
}, recipient)
}
function isEmpty (val) {
const type = Object.prototype.toString.call(val);
const isArray = type === '[object Array]';
const isObject = type === '[object Object]';
if (!val) {
return true
} else if (isArray) {
return !val.length
} else if (isObject) {
return !Object.keys(val).length
}
}
target = Object.keys(target).reduce(function (result, key) {
const type = Object.prototype.toString.call(target[key]);
const isObject = (type === '[object Object]' || type === '[object Array]');
if (!isObject || isEmpty(target[key])) {
result[key] = target[key];
return result
} else {
return addKeys(
key,
result,
flatten(target[key], opts)
)
}
}, {});
Object.keys(target).forEach(function (key) {
const split = key.split(delimiter).map(transformKey);
let key1 = getkey(split.shift());
let key2 = getkey(split[0]);
let recipient = result;
while (key2 !== undefined) {
if (key1 === '__proto__') {
return
}
const type = Object.prototype.toString.call(recipient[key1]);
const isobject = (
type === '[object Object]' ||
type === '[object Array]'
);
// do not write over falsey, non-undefined values if overwrite is false
if (!overwrite && !isobject && typeof recipient[key1] !== 'undefined') {
return
}
if ((overwrite && !isobject) || (!overwrite && recipient[key1] == null)) {
recipient[key1] = (
typeof key2 === 'number' &&
!opts.object
? []
: {}
);
}
recipient = recipient[key1];
if (split.length > 0) {
key1 = getkey(split.shift());
key2 = getkey(split[0]);
}
}
// unflatten again for 'messy objects'
recipient[key1] = unflatten(target[key], opts);
});
return result
}
const RE_KEY_VAL = /^\s*([^\s=]+)\s*=\s*(.*)?\s*$/;

@@ -43,3 +199,3 @@ const RE_LINES = /\n|\r|\r\n/;

const value = destr__default(
match[2].trim()
(match[2] || "").trim()
/* val */

@@ -54,3 +210,3 @@ );

}
return options.flat ? config : flat__default.unflatten(config, { overwrite: true });
return options.flat ? config : unflatten(config, { overwrite: true });
}

@@ -73,5 +229,3 @@ function parseFile(path, options) {

function serialize(config) {
return Object.entries(flat__default.flatten(config)).map(
([key, value]) => `${key}=${typeof value === "string" ? value : JSON.stringify(value)}`
).join("\n");
return Object.entries(flatten(config)).map(([key, value]) => `${key}=${JSON.stringify(value)}`).join("\n");
}

@@ -92,3 +246,3 @@ function write(config, options) {

if (!options.flat) {
config = flat__default.unflatten(config, { overwrite: true });
config = unflatten(config, { overwrite: true });
}

@@ -95,0 +249,0 @@ const newConfig = defu.defu(config, read(options));

@@ -5,5 +5,162 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs';

import destr from 'destr';
import flat from 'flat';
import { defu } from 'defu';
function isBuffer (obj) {
return obj &&
obj.constructor &&
(typeof obj.constructor.isBuffer === 'function') &&
obj.constructor.isBuffer(obj)
}
function keyIdentity (key) {
return key
}
function flatten (target, opts) {
opts = opts || {};
const delimiter = opts.delimiter || '.';
const maxDepth = opts.maxDepth;
const transformKey = opts.transformKey || keyIdentity;
const output = {};
function step (object, prev, currentDepth) {
currentDepth = currentDepth || 1;
Object.keys(object).forEach(function (key) {
const value = object[key];
const isarray = opts.safe && Array.isArray(value);
const type = Object.prototype.toString.call(value);
const isbuffer = isBuffer(value);
const isobject = (
type === '[object Object]' ||
type === '[object Array]'
);
const newKey = prev
? prev + delimiter + transformKey(key)
: transformKey(key);
if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
(!opts.maxDepth || currentDepth < maxDepth)) {
return step(value, newKey, currentDepth + 1)
}
output[newKey] = value;
});
}
step(target);
return output
}
function unflatten (target, opts) {
opts = opts || {};
const delimiter = opts.delimiter || '.';
const overwrite = opts.overwrite || false;
const transformKey = opts.transformKey || keyIdentity;
const result = {};
const isbuffer = isBuffer(target);
if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
return target
}
// safely ensure that the key is
// an integer.
function getkey (key) {
const parsedKey = Number(key);
return (
isNaN(parsedKey) ||
key.indexOf('.') !== -1 ||
opts.object
)
? key
: parsedKey
}
function addKeys (keyPrefix, recipient, target) {
return Object.keys(target).reduce(function (result, key) {
result[keyPrefix + delimiter + key] = target[key];
return result
}, recipient)
}
function isEmpty (val) {
const type = Object.prototype.toString.call(val);
const isArray = type === '[object Array]';
const isObject = type === '[object Object]';
if (!val) {
return true
} else if (isArray) {
return !val.length
} else if (isObject) {
return !Object.keys(val).length
}
}
target = Object.keys(target).reduce(function (result, key) {
const type = Object.prototype.toString.call(target[key]);
const isObject = (type === '[object Object]' || type === '[object Array]');
if (!isObject || isEmpty(target[key])) {
result[key] = target[key];
return result
} else {
return addKeys(
key,
result,
flatten(target[key], opts)
)
}
}, {});
Object.keys(target).forEach(function (key) {
const split = key.split(delimiter).map(transformKey);
let key1 = getkey(split.shift());
let key2 = getkey(split[0]);
let recipient = result;
while (key2 !== undefined) {
if (key1 === '__proto__') {
return
}
const type = Object.prototype.toString.call(recipient[key1]);
const isobject = (
type === '[object Object]' ||
type === '[object Array]'
);
// do not write over falsey, non-undefined values if overwrite is false
if (!overwrite && !isobject && typeof recipient[key1] !== 'undefined') {
return
}
if ((overwrite && !isobject) || (!overwrite && recipient[key1] == null)) {
recipient[key1] = (
typeof key2 === 'number' &&
!opts.object
? []
: {}
);
}
recipient = recipient[key1];
if (split.length > 0) {
key1 = getkey(split.shift());
key2 = getkey(split[0]);
}
}
// unflatten again for 'messy objects'
recipient[key1] = unflatten(target[key], opts);
});
return result
}
const RE_KEY_VAL = /^\s*([^\s=]+)\s*=\s*(.*)?\s*$/;

@@ -35,3 +192,3 @@ const RE_LINES = /\n|\r|\r\n/;

const value = destr(
match[2].trim()
(match[2] || "").trim()
/* val */

@@ -46,3 +203,3 @@ );

}
return options.flat ? config : flat.unflatten(config, { overwrite: true });
return options.flat ? config : unflatten(config, { overwrite: true });
}

@@ -65,5 +222,3 @@ function parseFile(path, options) {

function serialize(config) {
return Object.entries(flat.flatten(config)).map(
([key, value]) => `${key}=${typeof value === "string" ? value : JSON.stringify(value)}`
).join("\n");
return Object.entries(flatten(config)).map(([key, value]) => `${key}=${JSON.stringify(value)}`).join("\n");
}

@@ -84,3 +239,3 @@ function write(config, options) {

if (!options.flat) {
config = flat.unflatten(config, { overwrite: true });
config = unflatten(config, { overwrite: true });
}

@@ -87,0 +242,0 @@ const newConfig = defu(config, read(options));

+18
-0

@@ -22,1 +22,19 @@ MIT License

SOFTWARE.
-----
Bundled with https://github.com/hughsk/flat
Copyright (c) 2014, Hugh Kennedy
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+17
-16
{
"name": "rc9",
"version": "2.1.1",
"version": "2.1.2",
"description": "Read/Write config couldn't be easier!",

@@ -23,3 +23,4 @@ "repository": "unjs/rc9",

"dev": "vitest",
"lint": "eslint --ext .ts . && prettier -c src test",
"lint": "eslint . && prettier -c src test",
"lint:fix": "eslint . --fix && prettier -w src test",
"release": "pnpm test && pnpm build && changelogen --release --push && npm publish",

@@ -29,19 +30,19 @@ "test": "pnpm lint && vitest run --coverage"

"dependencies": {
"defu": "^6.1.2",
"destr": "^2.0.0",
"flat": "^5.0.2"
"defu": "^6.1.4",
"destr": "^2.0.3"
},
"devDependencies": {
"@types/flat": "^5.0.2",
"@types/node": "^20.3.1",
"@vitest/coverage-v8": "^0.32.2",
"changelogen": "^0.5.3",
"eslint": "^8.43.0",
"eslint-config-unjs": "^0.2.1",
"prettier": "^2.8.8",
"typescript": "^5.1.3",
"unbuild": "^1.2.1",
"vitest": "^0.32.2"
"@types/node": "^20.12.6",
"@vitest/coverage-v8": "^1.4.0",
"automd": "^0.3.7",
"changelogen": "^0.5.5",
"eslint": "^9.0.0",
"eslint-config-unjs": "^0.3.0-rc.5",
"flat": "^6.0.1",
"prettier": "^3.2.5",
"typescript": "^5.4.4",
"unbuild": "^2.0.0",
"vitest": "^1.4.0"
},
"packageManager": "pnpm@8.5.1"
"packageManager": "pnpm@8.15.6"
}
+79
-33
# RC9
> Read/Write config couldn't be easier!
<!-- automd:badges color=yellow codecov bundlejs -->
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Github Actions][github-actions-src]][github-actions-href]
[![Codecov][codecov-src]][codecov-href]
[![npm version](https://img.shields.io/npm/v/rc9?color=yellow)](https://npmjs.com/package/rc9)
[![npm downloads](https://img.shields.io/npm/dm/rc9?color=yellow)](https://npmjs.com/package/rc9)
[![bundle size](https://img.shields.io/bundlejs/size/rc9?color=yellow)](https://bundlejs.com/?q=rc9)
[![codecov](https://img.shields.io/codecov/c/gh/unjs/rc9?color=yellow)](https://codecov.io/gh/unjs/rc9)
<!-- /automd -->
Read/Write RC configs couldn't be easier!
## Install
Install using npm or yarn:
Install dependencies:
```bash
npm i rc9
# or
<!-- automd:pm-i -->
```sh
# ✨ Auto-detect
npx nypm install rc9
# npm
npm install rc9
# yarn
yarn add rc9
# pnpm
pnpm install rc9
# bun
bun install rc9
```
Import into your Node.js project:
<!-- /automd -->
Import utils:
<!-- automd:jsimport cjs src="./src/index.ts"-->
**ESM** (Node.js, Bun)
```js
// CommonJS
const { read, write, update } = require('rc9')
import {
defaults,
parse,
parseFile,
read,
readUser,
serialize,
write,
writeUser,
update,
updateUser,
} from "rc9";
```
// ESM
import { read, write, update } from 'rc9'
**CommonJS** (Legacy Node.js)
```js
const {
defaults,
parse,
parseFile,
read,
readUser,
serialize,
write,
writeUser,
update,
updateUser,
} = require("rc9");
```
<!-- /automd -->
## Usage

@@ -34,5 +84,5 @@

```ts
db.username=db username
db.password=db pass
```ini
db.username=username
db.password=multi word password
db.enabled=true

@@ -44,3 +94,3 @@ ```

```ts
update({ 'db.enabled': true }) // or update(..., { name: '.conf' })
update({ 'db.enabled': false }) // or update(..., { name: '.conf' })
```

@@ -61,4 +111,4 @@

// db: {
// username: 'db username',
// password: 'db pass',
// username: 'username',
// password: 'multi word password',
// enabled: true

@@ -131,3 +181,3 @@ // }

```ts
```ini
{

@@ -146,15 +196,11 @@ name: '.conf',

MIT. Made with 💖
<!-- automd:contributors license=MIT -->
<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/rc9?style=flat-square
[npm-version-href]: https://npmjs.com/package/rc9
Published under the [MIT](https://github.com/unjs/rc9/blob/main/LICENSE) license.
Made by [community](https://github.com/unjs/rc9/graphs/contributors) 💛
<br><br>
<a href="https://github.com/unjs/rc9/graphs/contributors">
<img src="https://contrib.rocks/image?repo=unjs/rc9" />
</a>
[npm-downloads-src]: https://img.shields.io/npm/dm/rc9?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/rc9
[github-actions-src]: https://img.shields.io/github/actions/workflow/status/unjs/rc9/ci.yml?branch=main&style=flat-square
[github-actions-href]: https://github.com/unjs/rc9/actions?query=workflow%3Aci
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/rc9/main?style=flat-square
[codecov-href]: https://codecov.io/gh/unjs/rc9
<!-- /automd -->