-
Notifications
You must be signed in to change notification settings - Fork 22
Add: zip packer #399
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
Add: zip packer #399
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,12 @@ import {createDialog} from "./lib/popup-dialog.js"; | |||||||||||
| import {compileStringTemplate} from "./lib/string-template.js"; | ||||||||||||
| import {expandEnv, expandDate} from "./lib/expand-env.mjs"; | ||||||||||||
| import {getTarPacker} from "./lib/tar-packer.js"; | ||||||||||||
| import {getZipPacker} from "./lib/zip-packer.js"; | ||||||||||||
|
|
||||||||||||
| const PACKERS = { | ||||||||||||
| tar: getTarPacker, | ||||||||||||
| zip: getZipPacker | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const MENU_ACTIONS = { | ||||||||||||
| PICK_FROM_CURRENT_TAB: { | ||||||||||||
|
|
@@ -532,12 +538,11 @@ function getRawPacker() { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| function getPacker() { | ||||||||||||
| if (pref.get("packer") === "tar") { | ||||||||||||
| try { | ||||||||||||
| return getTarPacker(); | ||||||||||||
| } catch (err) { | ||||||||||||
| console.warn(err); | ||||||||||||
| } | ||||||||||||
| try { | ||||||||||||
| const getPacker = PACKERS[pref.get("packer")]; | ||||||||||||
|
||||||||||||
| const getPacker = PACKERS[pref.get("packer")]; | |
| const getPacker = PACKERS[pref.get("packer")]; | |
| if (!getPacker) { | |
| return getRawPacker(); | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||||||||||||||
| import {makeZip} from "client-zip" | ||||||||||||||||||
| import {defer} from "./defer.js"; | ||||||||||||||||||
|
|
||||||||||||||||||
| const EXT = "zip"; | ||||||||||||||||||
|
|
||||||||||||||||||
| export function getZipPacker() { | ||||||||||||||||||
| if (typeof navigator.storage?.getDirectory !== 'function') { | ||||||||||||||||||
| throw new Error('File System Access API is not supported in this browser.'); | ||||||||||||||||||
| } | ||||||||||||||||||
| const tempName = `temp-${Date.now()}-${Math.random().toString(16).slice(2)}.${EXT}`; | ||||||||||||||||||
| let pendingPipe = null; | ||||||||||||||||||
| let itemReady = defer(); | ||||||||||||||||||
| let zipReady = defer(); | ||||||||||||||||||
| return { | ||||||||||||||||||
| prepare, | ||||||||||||||||||
| pack, | ||||||||||||||||||
| save, | ||||||||||||||||||
| waitResponse: true, | ||||||||||||||||||
| singleThread: true, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| async function prepare() { | ||||||||||||||||||
| const root = await navigator.storage.getDirectory(); | ||||||||||||||||||
| const handle = await root.getFileHandle(tempName, {create: true}); | ||||||||||||||||||
| const writable = await handle.createWritable(); | ||||||||||||||||||
| const zipStream = makeZip(async function*() { | ||||||||||||||||||
| let item; | ||||||||||||||||||
| while ((item = await itemReady.promise)) { | ||||||||||||||||||
| yield item; | ||||||||||||||||||
| zipReady.resolve(); | ||||||||||||||||||
| zipReady = defer(); | ||||||||||||||||||
| } | ||||||||||||||||||
| }()); | ||||||||||||||||||
| pendingPipe = zipStream.pipeTo(writable); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| async function pack({blob, filename}) { | ||||||||||||||||||
| itemReady.resolve({ | ||||||||||||||||||
| name: filename, | ||||||||||||||||||
| input: blob, | ||||||||||||||||||
|
Comment on lines
+38
to
+40
|
||||||||||||||||||
| itemReady.resolve({ | |
| name: filename, | |
| input: blob, | |
| // Pass a ReadableStream to client-zip so that blob data is consumed | |
| // via the browser's streaming backpressure rather than as a whole Blob. | |
| itemReady.resolve({ | |
| name: filename, | |
| input: blob.stream(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid automated semicolon insertion (94% of all statements in the enclosing script have an explicit semicolon).