Skip to content

Commit

Permalink
Support for React Native
Browse files Browse the repository at this point in the history
Implementation for the fallback inspect function so that it produces equivalent output to the original inspect behavior.
  • Loading branch information
colincasey committed Dec 2, 2021
1 parent bfc7bc6 commit 59a1b3d
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 204 deletions.
8 changes: 4 additions & 4 deletions lib/cookie.js
Expand Up @@ -31,14 +31,14 @@
"use strict";
const punycode = require("punycode");
const urlParse = require("url-parse");
const nodeUtil = require("./nodeUtil");
const pubsuffix = require("./pubsuffix-psl");
const Store = require("./store").Store;
const MemoryCookieStore = require("./memstore").MemoryCookieStore;
const pathMatch = require("./pathMatch").pathMatch;
const validators = require("./validators.js");
const VERSION = require("./version");
const { fromCallback } = require("universalify");
const { getCustomInspectSymbol } = require("./utilHelper");

// From RFC6265 S4.1.1
// note that it excludes \x3B ";"
Expand Down Expand Up @@ -812,9 +812,9 @@ const cookieDefaults = {

class Cookie {
constructor(options = {}) {
const util = nodeUtil();
if (util.inspect.custom) {
this[util.inspect.custom] = this.inspect;
const customInspectSymbol = getCustomInspectSymbol();
if (customInspectSymbol) {
this[customInspectSymbol] = this.inspect;
}

Object.assign(this, cookieDefaults, options);
Expand Down
58 changes: 53 additions & 5 deletions lib/memstore.js
Expand Up @@ -33,21 +33,21 @@ const { fromCallback } = require("universalify");
const Store = require("./store").Store;
const permuteDomain = require("./permuteDomain").permuteDomain;
const pathMatch = require("./pathMatch").pathMatch;
const nodeUtil = require("./nodeUtil");
const { getCustomInspectSymbol, getUtilInspect } = require("./utilHelper");

class MemoryCookieStore extends Store {
constructor() {
super();
this.synchronous = true;
this.idx = {};
const util = nodeUtil();
if (util.inspect.custom) {
this[util.inspect.custom] = this.inspect;
const customInspectSymbol = getCustomInspectSymbol();
if (customInspectSymbol) {
this[customInspectSymbol] = this.inspect;
}
}

inspect() {
const util = nodeUtil();
const util = { inspect: getUtilInspect(inspectFallback) };
return `{ idx: ${util.inspect(this.idx, false, 2)} }`;
}

Expand Down Expand Up @@ -190,3 +190,51 @@ class MemoryCookieStore extends Store {
});

exports.MemoryCookieStore = MemoryCookieStore;

function inspectFallback(val) {
const domains = Object.keys(val);
if (domains.length === 0) {
return "{}";
}
let result = "{\n";
Object.keys(val).forEach((domain, i) => {
result += formatDomain(domain, val[domain]);
if (i < domains.length - 1) {
result += ",";
}
result += "\n";
});
result += "}";
return result;
}

function formatDomain(domainName, domainValue) {
const indent = " ";
let result = `${indent}'${domainName}': {\n`;
Object.keys(domainValue).forEach((path, i, paths) => {
result += formatPath(path, domainValue[path]);
if (i < paths.length - 1) {
result += ",";
}
result += "\n";
});
result += `${indent}}`;
return result;
}

function formatPath(pathName, pathValue) {
const indent = " ";
let result = `${indent}'${pathName}': {\n`;
Object.keys(pathValue).forEach((cookieName, i, cookieNames) => {
const cookie = pathValue[cookieName];
result += ` ${cookieName}: ${cookie.inspect()}`;
if (i < cookieNames.length - 1) {
result += ",";
}
result += "\n";
});
result += `${indent}}`;
return result;
}

exports.inspectFallback = inspectFallback;
101 changes: 0 additions & 101 deletions lib/nodeUtil.js

This file was deleted.

39 changes: 39 additions & 0 deletions lib/utilHelper.js
@@ -0,0 +1,39 @@
function requireUtil() {
try {
// eslint-disable-next-line no-restricted-modules
return require("util");
} catch (e) {
return null;
}
}

// for v10.12.0+
function lookupCustomInspectSymbol() {
return Symbol.for("nodejs.util.inspect.custom");
}

// for older node environments
function tryReadingCustomSymbolFromUtilInspect(options) {
const _requireUtil = options.requireUtil || requireUtil;
const util = _requireUtil();
return util ? util.inspect.custom : null;
}

exports.getUtilInspect = function getUtilInspect(fallback, options = {}) {
const _requireUtil = options.requireUtil || requireUtil;
const util = _requireUtil();
return function inspect(value, showHidden, depth) {
return util ? util.inspect(value, showHidden, depth) : fallback(value);
};
};

exports.getCustomInspectSymbol = function getCustomInspectSymbol(options = {}) {
const _lookupCustomInspectSymbol =
options.lookupCustomInspectSymbol || lookupCustomInspectSymbol;

// get custom inspect symbol for node environments
return (
_lookupCustomInspectSymbol() ||
tryReadingCustomSymbolFromUtilInspect(options)
);
};

0 comments on commit 59a1b3d

Please sign in to comment.