Skip to content

Commit

Permalink
chore: improve perf of hot path (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Nov 21, 2023
1 parent 81953c6 commit 8e5d7c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
29 changes: 16 additions & 13 deletions src/index.ts
Expand Up @@ -16,6 +16,12 @@ import type {
} from "./types";
import { VERSION } from "./version";

const noop = () => {};
const consoleWarn = console.warn.bind(console);
const consoleError = console.error.bind(console);

const userAgentTrail = `octokit-core.js/${VERSION} ${getUserAgent()}`;

export class Octokit {
static VERSION = VERSION;
static defaults<S extends Constructor<any>>(
Expand Down Expand Up @@ -87,12 +93,9 @@ export class Octokit {
};

// prepend default user agent with `options.userAgent` if set
requestDefaults.headers["user-agent"] = [
options.userAgent,
`octokit-core.js/${VERSION} ${getUserAgent()}`,
]
.filter(Boolean)
.join(" ");
requestDefaults.headers["user-agent"] = options.userAgent
? `${options.userAgent} ${userAgentTrail}`
: userAgentTrail;

if (options.baseUrl) {
requestDefaults.baseUrl = options.baseUrl;
Expand All @@ -110,10 +113,10 @@ export class Octokit {
this.graphql = withCustomRequest(this.request).defaults(requestDefaults);
this.log = Object.assign(
{
debug: () => {},
info: () => {},
warn: console.warn.bind(console),
error: console.error.bind(console),
debug: noop,
info: noop,
warn: consoleWarn,
error: consoleError,
},
options.log,
);
Expand Down Expand Up @@ -163,9 +166,9 @@ export class Octokit {
// apply plugins
// https://stackoverflow.com/a/16345172
const classConstructor = this.constructor as typeof Octokit;
classConstructor.plugins.forEach((plugin) => {
Object.assign(this, plugin(this, options));
});
for (let i = 0; i < classConstructor.plugins.length; ++i) {
Object.assign(this, classConstructor.plugins[i](this, options));
}
}

// assigned during constructor
Expand Down
23 changes: 3 additions & 20 deletions test/log.test.ts
Expand Up @@ -10,30 +10,13 @@ describe("octokit.log", () => {
});

it(".debug() and .info() are no-ops by default", () => {
const originalLogDebug = console.debug;
const originalLogInfo = console.info;
const originalLogWarn = console.warn;
const originalLogError = console.error;

const calls: String[] = [];
console.debug = () => calls.push("debug");
console.info = () => calls.push("info");
console.warn = () => calls.push("warn");
console.error = () => calls.push("error");

const octokit = new Octokit();

expect(octokit.log.debug.name).toBe("noop");
expect(octokit.log.info.name).toBe("noop");

octokit.log.debug("foo");
octokit.log.info("bar");
octokit.log.warn("baz");
octokit.log.error("daz");

console.debug = originalLogDebug;
console.info = originalLogInfo;
console.warn = originalLogWarn;
console.error = originalLogError;

expect(calls).toStrictEqual(["warn", "error"]);
});

it("all .log.*() methods can be overwritten", () => {
Expand Down

0 comments on commit 8e5d7c1

Please sign in to comment.