![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
properties-reader
Advanced tools
The properties-reader npm package is a utility for reading and manipulating .properties files in Node.js. It allows you to load properties files, access and modify their values, and save changes back to the file.
Load Properties File
This feature allows you to load a .properties file into a PropertiesReader object, making it easy to access and manipulate the properties within the file.
const PropertiesReader = require('properties-reader');
const properties = PropertiesReader('path/to/file.properties');
Get Property Value
This feature allows you to retrieve the value of a specific property key from the loaded properties file.
const value = properties.get('some.property.key');
console.log(value);
Set Property Value
This feature allows you to set or update the value of a specific property key in the loaded properties file.
properties.set('some.property.key', 'new value');
Save Properties File
This feature allows you to save the changes made to the properties back to the file.
properties.save('path/to/file.properties');
Get All Properties
This feature allows you to retrieve all properties as an object, making it easy to work with all the properties at once.
const allProperties = properties.getAllProperties();
console.log(allProperties);
dotenv is a popular package for loading environment variables from a .env file into process.env. While it is primarily used for environment variables, it can also be used to manage configuration properties. Unlike properties-reader, dotenv focuses on environment variables and does not provide methods for manipulating the properties after loading them.
config is a configuration management package for Node.js that allows you to define configuration settings for your application in a variety of formats, including .json, .yaml, and .properties files. It provides a more comprehensive solution for managing application configurations compared to properties-reader, which is focused solely on .properties files.
nconf is a hierarchical configuration manager for Node.js that supports multiple configuration sources such as command-line arguments, environment variables, and files (including .json and .properties). It offers more flexibility and features compared to properties-reader, making it suitable for complex configuration management needs.
An ini file compatible properties reader for Node.JS
The easiest installation is through NPM:
npm install properties-reader
Read properties from a file:
var propertiesReader = require('properties-reader');
var properties = propertiesReader('/path/to/properties.file');
The properties are then accessible either by fully qualified name, or if the property names are in dot-delimited notation, they can be access as an object:
// fully qualified name
var property = properties.get('some.property.name');
// by object path
var property = properties.path().some.property.name;
To read more than one file, chain calls to the .append()
method:
properties.append('/another.file').append('/yet/another.file');
To read properties from a string, use the .read()
method:
properties.read('some.property = Value \n another.property = Another Value');
To set a single property into the properties object, use .set()
:
properties.set('property.name', 'Property Value');
When reading a .ini
file, sections are created by having a line that contains just a section name in square
brackets. The section name is then prefixed to all property names that follow it until another section name is found
to replace the current section.
# contents of properties file
[main]
some.thing = foo
[blah]
some.thing = bar
// reading these back from the properties reader
properties.get('main.some.thing') == 'foo';
properties.get('blah.some.thing') == 'bar';
// looping through the properties reader
properties.each((key, value) => {
// called for each item in the reader,
// first with key=main.some.thing, value=foo
// next with key=blah.some.thing, value=bar
});
// get all properties at once
expect(properties.getAllProperties()).toEqual({
'main.some.thing': 'foo',
'blah.some.thing': 'bar',
})
Checking for the current number of properties that have been read into the reader:
var propertiesCount = properties.length;
The length is calculated on request, so if accessing this in a loop an efficiency would be achieved by caching the value.
When duplicate names are found in the properties, the first one read will be replaced with the later one.
To get the complete set of properties, either loop through them with the .each((key, value) => {})
iterator or
use the convenience method getAllProperties
to return the complete set of flattened properties.
Once a file has been read and changes made, saving those changes to another file is as simple as running:
// async/await ES6
const propertiesReader = require('properties-reader');
const props = propertiesReader(filePath, {writer: { saveSections: true }});
await props.save(filePath);
// ES5 callback styles
props.save(filePath, function then(err, data) { ... });
// ES5 promise style
props.save(filePath).then(onSaved, onSaveError);
To output the properties without any section headings, set the saveSections
option to false
Properties will automatically be converted to their regular data types when they represent true/false or numeric
values. To get the original value without any parsing / type coercion applied, use properties.getRaw('path.to.prop')
.
From version 2.0.0
the default behaviour relating to multiple [section]
blocks with the same name has changed
so combine the items of each same-named section into the one section. This is only visible when saving the items
(via reader.save()
).
To restore the previous behaviour which would allow duplicate [...]
blocks to be created, supply an appender
configuration with the property allowDuplicateSections
set to true
.
const propertiesReader = require('properties-reader');
const props = propertiesReader(filePath, 'utf-8', { allowDuplicateSections: true });
If you find bugs or want to change functionality, feel free to fork and pull request.
2.3.0
__proto__
as a top-level property name.FAQs
Properties file reader for Node.js
The npm package properties-reader receives a total of 0 weekly downloads. As such, properties-reader popularity was classified as not popular.
We found that properties-reader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.