Skip to content

Commit

Permalink
Merge pull request #1342 from tediousjs/arthur/do-not-modify-input-ob…
Browse files Browse the repository at this point in the history
…jects

fix: do not modify objects passed to `Request.addOutputParameter` or `Request.addParameter`
  • Loading branch information
arthurschreiber authored Sep 19, 2021
2 parents 51754a6 + f1acd07 commit 4196d85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,8 @@ class Request extends EventEmitter {
* Additional type options. Optional.
*/
// TODO: `type` must be a valid TDS value type
addParameter(name: string, type: DataType, value?: unknown, options?: ParameterOptions | null) {
if (options == null) {
options = {};
}

const { output = false, length, precision, scale } = options;
addParameter(name: string, type: DataType, value?: unknown, options?: Readonly<ParameterOptions> | null) {
const { output = false, length, precision, scale } = options ?? {};

const parameter: Parameter = {
type: type,
Expand All @@ -414,6 +410,7 @@ class Request extends EventEmitter {
precision: precision,
scale: scale
};

this.parameters.push(parameter);
this.parametersByName[name] = parameter;
}
Expand All @@ -433,12 +430,8 @@ class Request extends EventEmitter {
* @param options
* Additional type options. Optional.
*/
addOutputParameter(name: string, type: DataType, value?: unknown, options?: ParameterOptions) {
if (options == null) {
options = {};
}
options.output = true;
this.addParameter(name, type, value, options);
addOutputParameter(name: string, type: DataType, value?: unknown, options?: Readonly<ParameterOptions> | null) {
this.addParameter(name, type, value, { ...options, output: true });
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/unit/request-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { assert } = require('chai');
const { TYPES } = require('../../src/data-type');

const Request = require('../../src/request');

Expand Down Expand Up @@ -36,4 +37,20 @@ describe('Request', function() {
assert.strictEqual(eventEmitted, false);
});
});

describe('#addOutputParameter', function() {
it('does not modify the passed in options object', function() {
const request = new Request('', () => {});

request.addOutputParameter('foo', TYPES.NVarChar, 'test', Object.freeze({ length: 10 }));
});
});

describe('#addParameter', function() {
it('does not modify the passed in options object', function() {
const request = new Request('', () => { });

request.addParameter('foo', TYPES.NVarChar, 'test', Object.freeze({ length: 10 }));
});
});
});

0 comments on commit 4196d85

Please sign in to comment.