Skip to content

Commit

Permalink
refactor: remove custom inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovyerus committed Oct 31, 2021
1 parent 29899fc commit b6ef7ef
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 69 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@ console.log(me.email); // [email protected]

`new Redite([options])`

| Name | Type | Default | Description |
| ----------------------------- | ------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------- |
| options.client | ioredis.Redis | `new Redis(options.url)` | The Redis connection to piggyback off of. |
| options.url | String | | The Redis URL to use for the automatically created connection. Not used if a client is passed. |
| options.serialise | Function | `JSON.stringify` | Function that takes in a JS object and returns a string that can be sent to Redis. |
| options.parse | Function | `JSON.parse` | Function that takes in a string and returns the JS object that it represents. |
| options.deletedString | String | `@__DELETED__@` | String to use as a temporary placeholder when deleting root indexes in a list. |
| options.unref | Boolean | `true` | Whether to run `.unref` on the Redis client, which allows Node to exit if the connection is idle. |
| options.customInspection | Boolean | `false` | Whether to use a custom inspection for the Redis URL and Redis connection to hide potentially sensitive data. |
| options.ignoreUndefinedValues | Boolean | `false` | Whether to ignore `undefined` as a base value when setting values. |
| Name | Type | Default | Description |
| ----------------------------- | ------------- | ------------------------ | ------------------------------------------------------------------------------------------------- |
| options.client | ioredis.Redis | `new Redis(options.url)` | The Redis connection to piggyback off of. |
| options.url | String | | The Redis URL to use for the automatically created connection. Not used if a client is passed. |
| options.serialise | Function | `JSON.stringify` | Function that takes in a JS object and returns a string that can be sent to Redis. |
| options.parse | Function | `JSON.parse` | Function that takes in a string and returns the JS object that it represents. |
| options.deletedString | String | `@__DELETED__@` | String to use as a temporary placeholder when deleting root indexes in a list. |
| options.unref | Boolean | `true` | Whether to run `.unref` on the Redis client, which allows Node to exit if the connection is idle. |
| options.ignoreUndefinedValues | Boolean | `false` | Whether to ignore `undefined` as a base value when setting values. |

#### **Accessing Objects**

Expand Down
2 changes: 0 additions & 2 deletions redite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ interface RediteOptions {
serialise?: (value: any) => string;
parse?: (value: string) => any;
deletedString?: string;
customInspection?: boolean;
ignoreUndefinedValues?: boolean;
}

Expand Down Expand Up @@ -102,7 +101,6 @@ declare namespace r {
$serialise: (value: any) => string;
$parse: (value: string) => any;
$deletedString: string;
$customInspection: boolean;
$ignoreUndefinedValues: boolean;

constructor(options?: RediteOptions);
Expand Down
22 changes: 0 additions & 22 deletions src/Redite.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const Redis = require("ioredis");

const util = require("util");

const ChildWrapper = require("./ChildWrapper");
const { NonMutatingMethods, SupportedArrayMethods } = require("./Constants");

Expand All @@ -27,7 +25,6 @@ function genTree(stack) {
* @prop {Function} $parse Data parser function.
* @prop {String} $deletedString Temporary string used when deleting items from a list.
* @prop {Boolean} $ignoreUndefinedValues Whether to ignore `undefined` when setting values.
* @prop {Boolean} $customInspection Whether to give a custom object for `util.inspect`.
*/
class Redite {
constructor(options = {}) {
Expand All @@ -36,7 +33,6 @@ class Redite {
this.$parse = options.parse || JSON.parse;
this.$deletedString = options.deletedString || "@__DELETED__@";
this.$ignoreUndefinedValues = options.ignoreUndefinedValues || false;
this.$customInspection = options.customInspection || false;

// (https://stackoverflow.com/a/40714458/8778928)
// eslint-disable-next-line no-constructor-return
Expand Down Expand Up @@ -75,24 +71,6 @@ class Redite {
});
}

[util.inspect.custom]() {
if (!this.$customInspection) return this;
else {
const scope = this;

return new (class Redite {
constructor() {
this.$redis = "<hidden>";
this.$serialise = scope.$serialise;
this.$parse = scope.$parse;
this.$deletedString = scope.$deletedString;
this.$customInspection = scope.$customInspection;
this.$ignoreUndefinedValues = scope.$ignoreUndefinedValues;
}
})();
}
}

/**
* Get an object from Redis.
* You should probably get an object through a simulated object tree.
Expand Down
35 changes: 0 additions & 35 deletions test/other.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

const Redis = require("ioredis");

const {
inspect: { custom },
} = require("util");

const Redite = require("../");

const { redisUrl, TestHash } = require("./lib/consts");

const client = new Redis(redisUrl);
const wrapper = new Redite({
client,
customInspection: true,
ignoreUndefinedValues: true,
});

Expand All @@ -31,7 +26,6 @@ describe("Extra coverage", () => {
expect(db.$serialise).toBe(JSON.stringify);
expect(db.$parse).toBe(JSON.parse);
expect(db.$deletedString).toBe("@__DELETED__@");
expect(db.$customInspection).toBe(false);
expect(db.$ignoreUndefinedValues).toBe(false);

db.$redis.disconnect();
Expand Down Expand Up @@ -103,33 +97,4 @@ describe("Extra coverage", () => {
);
});
});

describe("Custom inspection", () => {
it("should be a function", () => {
expect(wrapper[custom]).toBeInstanceOf(Function);
});

it("should return a new class called `Redite`", () => {
expect(wrapper[custom]().constructor.name).toBe("Redite");
});

it('should have regular values, with `redis` set to "<hidden>"', () => {
const res = wrapper[custom]();

expect(res.$redis).toBe("<hidden>");
expect(res.$serialise).toBe(wrapper.$serialise);
expect(res.$parse).toBe(wrapper.$parse);
expect(res.$deletedString).toBe(wrapper.$deletedString);
expect(res.$customInspection).toBe(wrapper.$customInspection);
expect(res.$ignoreUndefinedValues).toBe(wrapper.$ignoreUndefinedValues);
});

it("should return the regular Redite instance if disabled", () => {
const db = new Redite();

expect(db[custom]()).toBe(db);

db.$redis.disconnect();
});
});
});

0 comments on commit b6ef7ef

Please sign in to comment.