-
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.
1.3.0 options add compressFormat, default deflate-raw
- Loading branch information
Showing
17 changed files
with
259 additions
and
227 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function KiB(size: number) { | ||
return `${Math.ceil(size / 10.24) / 100} KiB` | ||
} |
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,27 @@ | ||
import base128 from "base128-ascii" | ||
import zlib from 'zlib' | ||
|
||
export type compressFormat = "deflate-raw" | "deflate" | "gzip" | ||
|
||
export function compress(format: compressFormat, buf: zlib.InputType, useBase128: boolean) { | ||
const options = { | ||
level: zlib.constants.Z_BEST_COMPRESSION, | ||
} | ||
let outBuf: Buffer | ||
switch (format) { | ||
case "deflate": | ||
outBuf = zlib.deflateSync(buf, options) | ||
break | ||
case "deflate-raw": | ||
outBuf = zlib.deflateRawSync(buf, options) | ||
break | ||
case "gzip": | ||
outBuf = zlib.gzipSync(buf, options) | ||
break | ||
default: | ||
throw `unknown compress format ${format}` | ||
} | ||
return useBase128 | ||
? base128.encode(Uint8Array.from(outBuf)).toJSTemplateLiterals() | ||
: outBuf.toString('base64') | ||
} |
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,8 @@ | ||
import svgToTinyDataUri from "mini-svg-data-uri"; | ||
import mime from 'mime' | ||
|
||
export function bufferToDataURL(name: string, b: Buffer) { | ||
return name.endsWith('.svg') | ||
? svgToTinyDataUri(b.toString()) | ||
: `data:${mime.getType(name)};base64,${b.toString('base64')}` | ||
} |
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,32 @@ | ||
import path from 'path' | ||
import fs from 'fs' | ||
|
||
const files = { | ||
base64: fs.readFileSync( | ||
path.join(import.meta.dirname, "template/base64.js") | ||
).toString(), | ||
|
||
base128: fs.readFileSync( | ||
path.join(import.meta.dirname, "template/base128.js") | ||
).toString(), | ||
|
||
assets: fs.readFileSync( | ||
path.join(import.meta.dirname, "template/assets.js") | ||
).toString().split('{"":""}', 2), | ||
} | ||
|
||
export const template = { | ||
base(script: string, format: string, useBase128: boolean) { | ||
if (useBase128) { | ||
return files.base128 | ||
.replace("<format>", format) | ||
.split('"<script>"', 2).join(script) | ||
} | ||
return files.base64 | ||
.replace("<format>", format) | ||
.split("<script>", 2).join(script) | ||
}, | ||
assets(assetsJSON: string) { | ||
return files.assets.join(assetsJSON) | ||
}, | ||
} |
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,8 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
export const { version } = JSON.parse( | ||
fs.readFileSync( | ||
path.join(import.meta.dirname, "../package.json") | ||
).toString() | ||
) as { version: string } |
Oops, something went wrong.