Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@platformatic/config

Package Overview
Dependencies
Maintainers
5
Versions
287
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@platformatic/config - npm Package Compare versions

Comparing version 0.11.0 to 0.12.0

test/update.test.js

77

index.js
'use strict'
const { basename, extname, join, resolve, dirname } = require('path')
const { readFile, writeFile, access } = require('fs/promises')
const { tmpdir } = require('os')
const { readFile, access } = require('fs/promises')
const EventEmitter = require('events')

@@ -19,3 +18,2 @@ const Ajv = require('ajv')

this.pupa = null
this._shouldSave = false
this.envWhitelist = opts.envWhitelist || []

@@ -29,17 +27,21 @@ if (!opts.source) {

this.fullPath = resolve(opts.source)
const allowToWatch = opts.allowToWatch || []
allowToWatch.push(basename(this.fullPath))
this._parser = this._getParser()
this.fileWatcher = new FileWatcher({
path: dirname(this.fullPath),
allowToWatch
})
/* c8 ignore next 3 */
if (opts.watch) {
this.startWatching()
}
this.dirname = dirname(this.fullPath)
} else {
this.fullPath = join(tmpdir(), `platformatic-config-${process.pid}-${Date.now()}.json`)
this.current = opts.source
this._shouldSave = true
this.dirname = opts.dirname || process.cwd()
}
const allowToWatch = opts.allowToWatch || []
allowToWatch.push(basename(this.fullPath))
this.fileWatcher = new FileWatcher({
path: dirname(this.fullPath),
allowToWatch
})
this.serializer = this.getSerializer()
this.schema = opts.schema || {}

@@ -49,6 +51,2 @@ this.schemaOptions = opts.schemaOptions || {}

this.env = this.purgeEnv(this._originalEnv)
/* c8 ignore next 3 */
if (opts.watch) {
this.startWatching()
}
}

@@ -83,20 +81,14 @@

getSerializer () {
_getParser () {
switch (extname(this.fullPath)) {
case '.yaml':
case '.yml':
return YAML
return YAML.parse
case '.json':
return {
parse: (json) => JSON.parse(json),
stringify: (data) => JSON.stringify(data, null, 2)
}
return JSON.parse
case '.json5':
return {
parse: (json) => JSON5.parse(json),
stringify: (data) => JSON5.stringify(data, null, 2)
}
return JSON5.parse
case '.toml':
case '.tml':
return TOML
return TOML.parse
default:

@@ -157,14 +149,8 @@ throw new Error('Invalid config file extension. Only yml, yaml, json, json5, toml, tml are supported.')

_sanitizeConfig () {
return this.current
}
async parse () {
try {
if (this._shouldSave) {
await this.save()
this._shouldSave = false
if (this.fullPath) {
const configString = await this.load()
this.current = this._parser(await this.replaceEnv(configString))
}
const configString = await this.load()
this.current = this.serializer.parse(await this.replaceEnv(configString))
const validationResult = this.validate()

@@ -223,15 +209,8 @@ if (!validationResult) {

this.current = newConfig
if (this.validate()) {
return this.save()
}
this.current = _old
return false
}
async save () {
if (!this.current) {
if (!this.validate()) {
this.current = _old
return false
}
const sanitizedConfig = this._sanitizeConfig()
return await writeFile(this.fullPath, this.serializer.stringify(sanitizedConfig))
this.emit('update', this.current)
return true
}

@@ -238,0 +217,0 @@

@@ -5,48 +5,2 @@ 'use strict'

const { configManager } = opts
const headersSchema = {
type: 'object',
properties: {
'x-platformatic-admin-secret': {
type: 'string',
description: 'The secret defined in authorization.adminSecret property of config file.'
}
},
required: ['x-platformatic-admin-secret']
}
const unauthorizedResponseSchema = {
type: 'object',
properties: {
success: { type: 'boolean', default: false },
message: { type: 'string', default: 'Unauthorized' }
}
}
// TODO: where we implement authorization for this?
// app.addHook('preHandler', async (request, reply) => {
// if (!request.user) {
// return reply.code(401).send({ success: false, message: 'Unauthorized' })
// }
// })
app.post('/config-file', {
schema: {
headers: headersSchema,
response: {
200: {
type: 'object',
properties: {
success: { type: 'boolean' }
}
},
401: unauthorizedResponseSchema
},
body: {
type: 'object'
}
}
}, async (req, reply) => {
await configManager.update(req.body)
return reply
.code(200)
.header('Content-Type', 'application/json')
.send({ success: true })
})

@@ -53,0 +7,0 @@ app.get('/config-file', async (req, reply) => {

{
"name": "@platformatic/config",
"version": "0.11.0",
"version": "0.12.0",
"description": "Platformatic DB Config Manager",

@@ -17,20 +17,19 @@ "main": "index.js",

"devDependencies": {
"fastify": "^4.10.2",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.0.0",
"c8": "^7.11.0",
"fastify": "^4.6.0"
"tap": "^16.3.2"
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"ajv": "^8.11.0",
"dotenv": "^16.0.1",
"json5": "^2.2.1",
"ajv": "^8.11.2",
"dotenv": "^16.0.3",
"json5": "^2.2.2",
"pupa": "^3.1.0",
"yaml": "^2.1.1",
"@platformatic/utils": "0.11.0"
"yaml": "^2.1.3",
"@platformatic/utils": "0.12.0"
},
"scripts": {
"test": "standard | snazzy && c8 --100 tap --no-coverage test/*test.js"
"test": "standard | snazzy && tap test/*test.js"
}
}

@@ -47,2 +47,31 @@ 'use strict'

test('dirname option', async ({ equal, plan }) => {
plan(1)
const cm = new ConfigManager({
source: {
server: {
hostname: '127.0.0.1',
port: '3042'
}
},
dirname: 'foobar'
})
await cm.parse()
equal(cm.dirname, 'foobar')
})
test('dirname as cwd', async ({ equal, plan }) => {
plan(1)
const cm = new ConfigManager({
source: {
server: {
hostname: '127.0.0.1',
port: '3042'
}
}
})
await cm.parse()
equal(cm.dirname, process.cwd())
})
test('should purge env', ({ plan, same, teardown }) => {

@@ -49,0 +78,0 @@ plan(2)

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

const { saveConfigToFile } = require('./helper')
const { readFile, unlink } = require('fs/promises')
const { unlink } = require('fs/promises')

@@ -48,26 +48,2 @@ test('should generate fastify plugin', async ({ teardown, same, equal }) => {

}
{
const newConfig = {
foo: 'bar',
bar: 'baz'
}
// Write config file
const res = await app.inject({
method: 'POST',
url: '/config-file',
headers: {
'x-platformatic-admin-secret': 'secret'
},
body: newConfig
})
equal(res.statusCode, 200)
same(res.json(), {
success: true
})
// check both file and current Config
const newData = JSON.parse(await readFile(file))
same(newData, newConfig)
same(cm.current, newConfig)
}
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc