Skip to content

Conversation

thomasarbona
Copy link
Contributor

@thomasarbona thomasarbona commented May 17, 2019

What does this PR do?

proxify helper

Create a helper that can throw error when get/set undeclared properties.

const obj = proxify({ foo: 42 });

console.log(obj.foo);
console.log(obj.bar); // throw

Some configuration:

proxify({ foo: 42 }, {
  name: 'object',
  seal: true,
  sealGet: false,
  deprecated: [],
  exposeApi: false,
  apiNamespace: '__proxy__',
});
  • name : the name of the proxified object for warnings
  • seal: does proxify throw error on set undeclared properties?
  • sealGet: does proxify throw error on get undeclared properties? this options is separated from seal because getting undefined properties can be usefull for type checking for example
  • deprecated: array of property names which produce a deprecate warning on get/set
  • warnDepreciationOnce: only warn once per deprecated property
  • exposeApi: expose an api in the object to manipulate properties (described below)
  • apiNamespace: in which namespace api is exposed
proxyfy API
const obj = proxify({ foo: 42 }, { exposeApi: true });

console.log(obj.foo); // OK
console.log(obj.bar); // throw error

obj.__proxy__.registerProp('bar');

console.log(obj.bar); // OK

const hasProp = obj.__proxy__.hasProp('baz'); // check WITHOUT warning

obj.__proxy__.unregisterProp('bar');

console.log(obj.bar); // throw error

usage on kuzzle instance

kuzzle instance use proxify like this in the constructor:

return proxify(this, {
  seal: true,
  name: 'kuzzle',
  exposeApi: true
});

How should this be manually tested?

  • Step 1 : instantiate Kuzzle class
  • Step 2 : define an undeclared property on this instance, like foo

Copy link
Contributor

@scottinet scottinet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one.

A remark though: the documentation you put in your PR description is very nice, but it should also be in a comment of the proxify file

src/proxify.js Outdated
...options
});

const getPropertyNames = (obj) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nitpicking) coding-style consistency

Suggested change
const getPropertyNames = (obj) => {
const getPropertyNames = obj => {

src/proxify.js Outdated
throw new Error(`${options.name}.${name} is not defined`);
}
if (options.deprecated.includes(name)) {
console.warn(`Warning: ${options.name}.${name} is deprecated`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you must add some kind of flag to trigger that warning only once per deprecated property accessed, otherwise this can clog the console up pretty quickly

src/proxify.js Outdated

const handler = {
get: (target, name) => {
if (options.sealGet && typeof name === 'string' && !properties.includes(name)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should remove the typecheck made on name:

  • it can only be a string or a symbol anyway, since this is a getter trap
  • if I pass a non-existing symbol to your handler, I won't get any error even with sealGet active, which does not seem to be the intended behavior

src/proxify.js Outdated
throw new Error(`setting a not defined '${name}' properties in '${options.name}' object`)
}
if (options.deprecated.includes(name)) {
console.warn(`Warning: ${options.name}.${name} is deprecated`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@thomasarbona thomasarbona requested a review from scottinet May 17, 2019 14:33
thomasarbona and others added 5 commits May 17, 2019 16:51
Co-Authored-By: Sébastien Cottinet <[email protected]>
Co-Authored-By: Sébastien Cottinet <[email protected]>
Co-Authored-By: Sébastien Cottinet <[email protected]>
Co-Authored-By: Sébastien Cottinet <[email protected]>
Co-Authored-By: Sébastien Cottinet <[email protected]>
@thomasarbona thomasarbona requested a review from scottinet May 17, 2019 15:20
@codecov
Copy link

codecov bot commented May 17, 2019

Codecov Report

Merging #395 into 6-dev will decrease coverage by 0.02%.
The diff coverage is 95.91%.

Impacted file tree graph

@@            Coverage Diff             @@
##            6-dev     #395      +/-   ##
==========================================
- Coverage   96.46%   96.44%   -0.03%     
==========================================
  Files          30       31       +1     
  Lines        1442     1489      +47     
==========================================
+ Hits         1391     1436      +45     
- Misses         51       53       +2
Impacted Files Coverage Δ
src/Kuzzle.js 95.13% <100%> (+0.11%) ⬆️
src/proxify.js 95.23% <95.23%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cd684cc...f6012da. Read the comment docs.

src/proxify.js Outdated
const options = getOptions(opts);
const properties = ['inspect'];

const warnedDepreciation = new Set();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

src/proxify.js Outdated
}

const options = getOptions(opts);
const properties = ['inspect'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a Set too

thomasarbona and others added 2 commits May 20, 2019 10:52
@thomasarbona thomasarbona requested review from Aschen and scottinet May 20, 2019 09:33
@Aschen Aschen merged commit 3a77f57 into 6-dev May 20, 2019
@Aschen Aschen deleted the KZL-1098-proxify-kuzzle branch May 20, 2019 15:09
@Aschen Aschen mentioned this pull request Jun 14, 2019
Aschen added a commit that referenced this pull request Jun 14, 2019
Release 6.1.2

Bug fixes

    [ #398 ] Fix bulk return (Aschen)
    [ #394 ] Add default values for from/size to document:search (Aschen)
    [ #384 ] Fix search API: "sort" and "search_after" must be in the requests body (scottinet)

Enhancements

    [ #390 ] Add authenticated property on Kuzzle object (Aschen)
    [ #395 ] Proxify kuzzle to avoid mistyping error (thomasarbona)
    [ #389 ] Remove usage of _meta (Aschen)
    [ #391 ] Add isConnected (Aschen)
    [ #388 ] Use BaseController class for controllers (Aschen)
    [ #385 ] Add Security.createRestrictedUser method (Aschen)

Others

    [ #400 ] Fix large document search using scroll (stafyniaksacha)
    [ #387 ] SearchResult.next returns a new instance (Aschen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants