Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@axa/bautajs-decorator-cache
Advanced tools
A cache decorator using moize for Bauta.js pipelines.
npm install @axa/bautajs-decorator-cache
Include it on your pipeline as follows:
import { pipe, createContext } from '@axa/bautajs-core';
import { cache } from '@axa/bautajs-decorator-cache';
function createAKey(prev, ctx, bautajs) {
ctx.data.myKey = 'mykey';
}
function doSomethingHeavy(prev, ctx, bautajs) {
let acc = 0;
for(let i=0; i < 1000000000; i++) {
acc += i;
}
return acc;
}
const myPipeline = pipe(
createAKey,
doSomethingHeavy
);
const cacheMyPipeline = cache(myPipeline, (prev, ctx) => ctx.data.myKey, { maxSize:3 });
const result = await cacheMyPipeline(null, createContext({req:{}}), {});
console.log(result);
Normalize functions must return an identifier key in the form of a primitive type that is used to determine if a new value requires cache or not. If you need to use more than one field to generate a key, concatenate or stringify those fields that you need.
There are two main use cases in normalize:
It is straightforward and you can do the following:
const normalizer = (_, ctx) => ctx.whatever_field;
This is trickier because you have to take into account that you will not have the result of the pipeline to be passed as the object to be used as the key. For example, this will not work:
const { pipe } = require('@axa/bautajs-core');
const { cache } = require('@axa/bautajs-decorator-cache');
const { someHeavyOperation } = require('./my-helper');
const myPipeline = pipe( someHeavyOperation, (result) => ({...result, iWantToUseAsKeyThis:1}))
module.exports = resolver((operations)=> {
const normalizer = (value) => value.iWantToUseAsKeyThis;
operations.v1.op1.setup(p =>
p.pipe(
cache(
myPipeline,
normalizer, // When normalizer is called, result from pipeline is not yet there
{ maxSize:5 }
)
)
);
})
This will not work because iWantToUseAsKeyThis is the result of the pipeline that you are trying to cache and it is not executed yet when the cache decorator is being called. Thus, the normalizer instruction will result in an error.
To use the object as a key in the cache normalizer, this object needs to be set in the previous pipeline of the cache, like in the following example:
const normalizer = (prev) => {
return prev.iAmTheKey; // prev may be a primitive
};
const pp = pipe((_, ctx) => {
return 'test';
},
value => ({ a: '123', b: value }),
result => ({ ...result, new: 1 })
)
bautaJS.operations.v1.operation2.setup(p =>
p
.pipe((_, ctx) => {
return { iAmTheKey: 'test' };
},
cache(pp, normalizer, { maxSize: 5 })
);
In here you can see that we have an decorator function that returns an object with a key iAmTheKey that is passed to normalizer previous to the decorator function of the cache.
Copyright (c) AXA Group. All rights reserved. Licensed under the (MIT / Apache 2.0) License.
FAQs
A bautaJS cache decorator
The npm package @axa/bautajs-decorator-cache receives a total of 0 weekly downloads. As such, @axa/bautajs-decorator-cache popularity was classified as not popular.
We found that @axa/bautajs-decorator-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.