bind.js
This file
Path
lib/methods/bind.js
25 lines · 973 bytes · JavaScript
Last changed by
Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
History of this fileTake it
Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
lines 1–4why, one step back1import isPlainObject from 'is-plain-obj';2import {FD_SPECIFIC_OPTIONS} from '../arguments/specific.js';34// Deep merge specific options like `env`. Shallow merge the other ones.Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 5why, one step back5// Use spread (which only copies own properties) to safely read from boundOptions without prototype pollutionRefactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 6why, one step back6export const mergeOptions = (boundOptions, options) => {Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
lines 7–8why, one step back7 const safeBoundOptions = {__proto__: null, ...boundOptions};8 const mergedOptions = Object.fromEntries(Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
lines 9–10why, one step back9 Object.entries(options).map(([optionName, optionValue]) => [10 optionName,Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 11why, one step back11 mergeOption(optionName, safeBoundOptions[optionName], optionValue),Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
lines 12–13why, one step back12 ]),13 );Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 14why, one step back14 return {...safeBoundOptions, ...mergedOptions};Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
lines 15–25why, one step back15};1617const mergeOption = (optionName, boundOptionValue, optionValue) => {18 if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) {19 return {...boundOptionValue, ...optionValue};20 }2122 return optionValue;23};2425const DEEP_OPTIONS = new Set(['env', ...FD_SPECIFIC_OPTIONS]);