Skip to content

Commit

Permalink
Merging updates from v7 back into v6
Browse files Browse the repository at this point in the history
Fixing mem leak, uncaught exceptions, etc
  • Loading branch information
czirker committed Feb 1, 2024
1 parent 99705a4 commit 536a8f6
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 37 deletions.
4 changes: 3 additions & 1 deletion lib/aws-util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/aws-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export function error(err, options) {
if (typeof err.message === 'string' && err.message !== '') {
if (typeof options === 'string' || (options && options.message)) {
originalError = copy(err);
originalError.message = err.message;
if (originalError != null) {
originalError.message = err.message;
}
}
}
err.message = err.message || null;
Expand Down
22 changes: 14 additions & 8 deletions lib/configuration-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export class ConfigurationBuilder<T> {
g.rstreams_project_config_cache = {};
}

if (g.rsf_config_opts) {
Object.assign(options, g.rsf_config_opts);
} else if (process.env.RSF_CONFIG_OPTS) {
Object.assign(options, JSON.parse(process.env.RSF_CONFIG_OPTS));
}

if (this.data == null || this.data == "") {
if (process.env.RSF_CONFIG) {
this.data = process.env.RSF_CONFIG;
Expand Down Expand Up @@ -109,7 +115,7 @@ export class ConfigurationBuilder<T> {
// Allow extra env vars to be defined as RSF_CONFIG_some.new.field=my_value
Object.entries(process.env).forEach(([key, value]) => {
const a = (key.match(/^RSF_CONFIG_(.*)$/) || [])[1];
if (a) {
if (a && key != "RSF_CONFIG_OPTS") {
let parts = a.split(".");
let lastPart = parts.pop();
let parent = parts.reduce((a, b) => {
Expand Down Expand Up @@ -144,10 +150,10 @@ export class ConfigurationBuilder<T> {
Object.getOwnPropertyNames(root).forEach(key => {
let value = root[key];
let origKey = key;

// convert string shorthand to full ResourceReference
if (typeof value === "string" && value.match(/^.+?::/)) {
let [service, key, type, opts] = value.split('::');
let [service, key, type, opts] = value.split(/(?<!AWS)::/);
type = type || "dynamic";
value = {
service,
Expand Down Expand Up @@ -217,10 +223,10 @@ export class ConfigurationBuilder<T> {
}
}
private isResourceReference(value: any): boolean {
return value != null && typeof value === "object" && value.service && value.key && value.type && ConfigurationBuilder.Resolvers[value.service];
return value != null && typeof value === "object" && value.service && value.key && value.type && ConfigurationBuilder.Resolvers[value.service] != null;
}

static Resolvers = {
static Resolvers: Record<string, (ref: ResourceReference, cache: any) => any> = {
// ssm: (ref: ResourceReference) => {
// return process.env[`RS_ssm::${resolveKeywords(ref.key, ref.options)}`];
// },
Expand Down Expand Up @@ -262,7 +268,7 @@ export class ConfigurationBuilder<T> {
};
}

function resolveKeywords(template: string, data: any) {
export function resolveKeywords(template: string, data: any) {
const name = template.replace(/\${(.*?)}/g, function (match, field) {
let value = getDataSafe(data, field);
if (value != null && typeof value === "object") {
Expand All @@ -272,7 +278,7 @@ function resolveKeywords(template: string, data: any) {
}).replace(/[_-]{2,}/g, "");
return name;
}
function getDataSafe(data = {}, path = "") {
export function getDataSafe(data = {}, path = "") {
const pathArray = path.split(".").filter(a => a !== "");
if (pathArray.length === 0) {
return data;
Expand All @@ -287,7 +293,7 @@ const nullRegex = /^null$/;
const undefinedRegex = /^undefined$/;
const jsonRegex = /^{(.|\n)*}$/;

function inferTypes(node) {
export function inferTypes(node) {
let type = typeof node;
if (Array.isArray(node)) {
for (let i = 0; i < node.length; i++) {
Expand Down
55 changes: 54 additions & 1 deletion lib/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,60 @@ export interface ReadOptions<T = any> {
/**
* Praser to convert string event objects into json event objects.
*
* @default: JSON.parse
* @default JSON.parse
* If the value is as string not found it will try to find a named parser
* otherwise the value is assumed to be the path to a custom parser module
* @example
* eg. { parser: "./my-parser.js"}
* "my-parser.js" file content
*
* // Custom parser options
* interface MatchParserOpts {
* matches: {
* regex: string;
* field: string;
* type?: string;
* }[];
* }
*
*
* // Function that returns the parser function
* // This allows you process settings to then use in your parser
* //
* // This parser takes a list of regex matches to extract data
* export function matchParserFactory(settings: MatchParserOpts) {
* let types = {
* number: (v) => Number(v),
* default: (v) => v
* };
* let matches = settings.matches.map(r => {
* const [, pattern, flags] = r.regex.match(/^\/(.*)\/(.*)?$/);
* let path = r.field.split(".");
* let field = path.pop();
* let type = types[r.type] || types.default;
* return {
* regex: new RegExp(pattern, flags),
* set: (root: any, value: any) => {
* let container = path.reduce((cur, field) => (cur[field] = cur[field] || {}), root);
* container[field] = type(value);
* }
* };
* });
* return (jsonString: string) => {
* let result = {
* __unparsed_value__: jsonString
* };
* matches.forEach(r => {
* let v = (jsonString.match(r.regex) || [])[1];
* if (v !== undefined) {
* r.set(result, v);
* }
* });
* return result;
* };
* }
*
* export default matchParserFactory;
*/
parser?: ((stringEvent: string) => ReadEvent<T>) | ParserName | string,
parserOpts?: any;
Expand Down
Loading

0 comments on commit 536a8f6

Please sign in to comment.