Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve perf of hot path #637

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"]);
Copy link
Contributor

Choose a reason for hiding this comment

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

you changed the nature of this test, can you please revert that? Please add a test where we log with all 4 methods

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

and check that only warn and error was logged

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

});

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