Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make all jetstream configurations strict on tests #56

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion test_helpers/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function jsopts() {
jetstream: {
max_file_store: 1024 * 1024,
max_mem_store: 1024 * 1024,
strict: true,
store_dir,
},
};
Expand Down Expand Up @@ -85,10 +86,64 @@ export function jetstreamExportServerConf(
return jetstreamServerConf(conf);
}


/**
* Checks if the given item is a plain object, excluding arrays and dates.
*
* @param {*} item - The item to check.
* @returns {boolean} True if the item is a plain object, false otherwise.
*/
function isObject(item: unknown) {
return (item && typeof item === 'object' && !Array.isArray(item) && !(item instanceof Date))
}

/**
* Recursively merges properties from source objects into a target object. If a property at the current level is an object,
* and both target and source have it, the property is merged. Otherwise, the source property overwrites the target property.
* This function does not modify the source objects and prevents prototype pollution by not allowing __proto__, constructor,
* and prototype property names.
*
* @param {Object} target - The target object to merge properties into.
* @param {...Object} sources - One or more source objects from which to merge properties.
* @returns {Object} The target object after merging properties from sources.
*/
// deno-lint-ignore no-explicit-any
export function deepMerge(target: any, ...sources: any[]) {
if (!sources.length) return target

// Iterate through each source object without modifying the sources array
sources.forEach(source => {
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
continue // Skip potentially dangerous keys to prevent prototype pollution.
}

if (!target[key] || !isObject(target[key])) {
target[key] = {}
}

deepMerge(target[key], source[key])
} else {
target[key] = source[key]
}
}
}
})

return target
}

export function jetstreamServerConf(
opts: unknown = {},
): Record<string, unknown> {
const conf = Object.assign(jsopts(), opts);
const strict = {
jetstream: {
strict: true
}
}
const conf = deepMerge(jsopts(), opts, strict);
if (typeof conf.jetstream.store_dir !== "string") {
conf.jetstream.store_dir = Deno.makeTempDirSync({ prefix: "jetstream" });
}
Expand Down
Loading