-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from mogeko/dev
Implement the encoder by ourselves
- Loading branch information
Showing
19 changed files
with
166 additions
and
227 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+13.7 KB
.yarn/cache/unist-util-visit-parents-npm-6.0.1-29ba152125-645b3cbc5e.zip
Binary file not shown.
Binary file not shown.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,78 @@ | ||
import { type InputType, deflateRawSync } from "node:zlib"; | ||
|
||
export function deflate(buff: InputType) { | ||
return deflateRawSync(buff, { level: 9 }).toString("binary"); | ||
} | ||
|
||
function encode6bit(code: number) { | ||
if (code < 10) return String.fromCharCode(48 + code); // 0-9 | ||
if (code < 36) return String.fromCharCode(55 + code); // A-Z | ||
if (code < 62) return String.fromCharCode(61 + code); // a-z | ||
if (code === 62) return "-"; | ||
if (code === 63) return "_"; | ||
return "?"; | ||
} | ||
|
||
function append3bytes(b1: number, b2: number, b3: number) { | ||
return ( | ||
encode6bit((b1 >> 2) & 0x3f) + | ||
encode6bit(((b1 & 0x3) << 4) | ((b2 >> 4) & 0x3f)) + | ||
encode6bit(((b2 & 0xf) << 2) | ((b3 >> 6) & 0x3f)) + | ||
encode6bit(b3 & 0x3f) | ||
); | ||
} | ||
|
||
function encode64(data: string) { | ||
let r = ""; | ||
for (let i = 0; i < data.length; i += 3) { | ||
if (i + 2 === data.length) { | ||
r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), 0); | ||
} else if (i + 1 === data.length) { | ||
r += append3bytes(data.charCodeAt(i), 0, 0); | ||
} else { | ||
r += append3bytes( | ||
data.charCodeAt(i), | ||
data.charCodeAt(i + 1), | ||
data.charCodeAt(i + 2), | ||
); | ||
} | ||
} | ||
return r; | ||
} | ||
|
||
export function encoder(puml: string) { | ||
return encode64(deflate(puml)); | ||
} | ||
|
||
if (import.meta.vitest) { | ||
const { Buffer } = await import("node:buffer"); | ||
const { expect, it } = await import("vitest"); | ||
|
||
it("encode6bit", () => { | ||
expect( | ||
[ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, | ||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, | ||
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, | ||
56, 57, 58, 59, 60, 61, 62, 63, | ||
] | ||
.map(encode6bit) | ||
.join(""), | ||
).toStrictEqual( | ||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_", | ||
); | ||
expect(encode6bit(64)).toStrictEqual("?"); | ||
}); | ||
|
||
it("append3bytes", () => { | ||
expect(append3bytes(1, 2, 3)).toStrictEqual("0G83"); | ||
expect(append3bytes(1, 2, 0)).toStrictEqual("0G80"); | ||
expect(append3bytes(1, 0, 0)).toStrictEqual("0G00"); | ||
}); | ||
|
||
it("encode64", () => { | ||
expect( | ||
encode64(Buffer.from([75, 76, 74, 6, 0]).toString("binary")), | ||
).toStrictEqual("IqnA1W00"); | ||
}); | ||
} |
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 was deleted.
Oops, something went wrong.
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,14 @@ | ||
import { deflate, encoder } from "@/encoder"; | ||
import { expect, it } from "vitest"; | ||
|
||
it("deflateSync", () => { | ||
expect(deflate("abc")).toStrictEqual( | ||
Buffer.from([75, 76, 74, 6, 0]).toString("binary"), | ||
); | ||
}); | ||
|
||
it("encoderSync", () => { | ||
expect(encoder(`@startuml\nA -> B: Hello / 你好'\n@enduml`)).toMatch( | ||
/SoWkIImgAStDuN9KqBLJSB9Iy4ZDoSbNq5TuidV1qwLxrRaSKlDI/, | ||
); | ||
}); |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import { defineConfig } from "tsup"; | ||
|
||
export default defineConfig({ | ||
entry: ["src/index.ts"], | ||
entry: ["./src/index.ts"], | ||
format: ["cjs"], | ||
clean: true, | ||
dts: true, | ||
minify: true, | ||
|
||
// To delete the in-source testing (comes from vitest) | ||
// See: https://github.com/egoist/tsup/issues/625#issuecomment-1608591913 | ||
define: { "import.meta.vitest": "false" }, | ||
treeshake: true, | ||
}); |
Oops, something went wrong.