-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(macro): hash based id for js macro
- Loading branch information
1 parent
44f8360
commit 9faba10
Showing
15 changed files
with
498 additions
and
274 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,24 @@ | ||
import { generateMessageId } from "./generateMessageId" | ||
|
||
describe("generateMessageId", () => { | ||
it("Should generate an id for a message", () => { | ||
expect(generateMessageId("my message")).toMatchInlineSnapshot(`vQhkQx`) | ||
}) | ||
|
||
it("Should generate different id when context is provided", () => { | ||
const withContext = generateMessageId("my message", "custom context") | ||
expect(withContext).toMatchInlineSnapshot(`gGUeZH`) | ||
|
||
expect(withContext != generateMessageId("my message")).toBeTruthy() | ||
}) | ||
|
||
it("Message + context should not clash with message with suffix or prefix", () => { | ||
const context = "custom context" | ||
const withContext = generateMessageId("my message", context) | ||
const withSuffix = generateMessageId("my message" + context) | ||
const withPrefix = generateMessageId(context, "my message") | ||
|
||
expect(withContext != withSuffix).toBeTruthy() | ||
expect(withContext != withPrefix).toBeTruthy() | ||
}) | ||
}) |
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,11 @@ | ||
import crypto from "crypto" | ||
|
||
const UNIT_SEPARATOR = "\u001F" | ||
|
||
export function generateMessageId(msg: string, context = "") { | ||
return crypto | ||
.createHash("sha256") | ||
.update(msg + UNIT_SEPARATOR + context) | ||
.digest("base64") | ||
.slice(0, 6) | ||
} |
Oops, something went wrong.