-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1589f17
commit a5d4842
Showing
7 changed files
with
95 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
moduleNameMapper: { | ||
"^uuid$": require.resolve("uuid"), | ||
"^node:crypto$": require.resolve("node:crypto"), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
jest.mock("node-fetch"); | ||
const fetch = require("node-fetch"); | ||
|
||
const initLogger = require("./index.js"); | ||
|
||
describe("logger", () => { | ||
let logger; | ||
|
||
beforeAll(() => { | ||
logger = initLogger(); | ||
}); | ||
|
||
beforeEach(() => { | ||
// @ts-ignore | ||
fetch.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
logger.stop(); | ||
}); | ||
|
||
it("should send logs", async () => { | ||
console.log("Hello, world!"); | ||
|
||
await logger.flushQueue(); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
|
||
// @ts-ignore | ||
const body = JSON.parse(fetch.mock.calls[0][1].body); | ||
expect(body).toHaveLength(1); | ||
expect(body[0].log).toBe('{"0":"Hello, world!"}'); | ||
}); | ||
|
||
it("flushes logs when the queue is full", async () => { | ||
for (let i = 0; i < 120; i++) { | ||
console.log("Hello, world!"); | ||
} | ||
|
||
expect(fetch).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// We use an alternative UUID implementation to prevent bundling issues. | ||
// This one is less secure, but randomness is less of an issue for log IDs. | ||
|
||
const str = () => | ||
( | ||
"00000000000000000" + (Math.random() * 0xffffffffffffffff).toString(16) | ||
).slice(-16); | ||
|
||
const uuid = () => { | ||
const a = str(); | ||
const b = str(); | ||
return ( | ||
a.slice(0, 8) + | ||
"-" + | ||
a.slice(8, 12) + | ||
"-4" + | ||
a.slice(13) + | ||
"-a" + | ||
b.slice(1, 4) + | ||
"-" + | ||
b.slice(4) | ||
); | ||
}; | ||
|
||
module.exports = uuid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters