Skip to content

Commit

Permalink
core: use zip.js for reading/writing zip archives
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Jan 4, 2024
1 parent d617fda commit 486c628
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 427 deletions.
116 changes: 115 additions & 1 deletion packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@standardnotes/features": "1.58.0",
"@standardnotes/models": "1.42.0",
"@streamparser/json": "^0.0.10",
"@zip.js/zip.js": "^2.7.32",
"css-select": "^5.1.0",
"dom-serializer": "^2.0.0",
"domhandler": "^5.0.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Tar, TarFile } from "./tar";
import { IStorage } from "@notesnook-importer/storage";
import { unzip } from "./unzip-stream";
import { NoteStream } from "./note-stream";
import { ZipStream } from "./zip-stream";
import { createZipStream } from "./zip-stream";

type Unpacker = (file: IFile) => IFile[] | Promise<IFile[]>;

Expand Down Expand Up @@ -66,7 +66,7 @@ export function pack(
storage: IStorage<Note>,
report: (done: number) => void = () => {}
) {
return new NoteStream(storage, report).pipeThrough(new ZipStream());
return new NoteStream(storage, report).pipeThrough(createZipStream());
}

async function untar(file: IFile): Promise<IFile[]> {
Expand Down
101 changes: 101 additions & 0 deletions packages/core/src/utils/fflate-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Deflate as FflateDeflate, Inflate as FflateInflate } from "fflate";

function initShimAsyncCodec(library, options = {}, registerDataHandler) {
return {
Deflate: createCodecClass(
library.Deflate,
options.deflate,
registerDataHandler
),
Inflate: createCodecClass(
library.Inflate,
options.inflate,
registerDataHandler
)
};
}

function objectHasOwn(object, propertyName) {
// eslint-disable-next-line no-prototype-builtins
return typeof Object.hasOwn === "function"
? Object.hasOwn(object, propertyName)
: // eslint-disable-next-line no-prototype-builtins
object.hasOwnProperty(propertyName);
}

function createCodecClass(
constructor,
constructorOptions,
registerDataHandler
) {
return class {
constructor(options) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const codecAdapter = this;
const onData = (data) => {
if (codecAdapter.pendingData) {
const previousPendingData = codecAdapter.pendingData;
codecAdapter.pendingData = new Uint8Array(
previousPendingData.length + data.length
);
const { pendingData } = codecAdapter;
pendingData.set(previousPendingData, 0);
pendingData.set(data, previousPendingData.length);
} else {
codecAdapter.pendingData = new Uint8Array(data);
}
};
if (objectHasOwn(options, "level") && options.level === undefined) {
delete options.level;
}
codecAdapter.codec = new constructor(
Object.assign({}, constructorOptions, options)
);
registerDataHandler(codecAdapter.codec, onData);
}
append(data) {
this.codec.push(data);
return getResponse(this);
}
flush() {
this.codec.push(new Uint8Array(), true);
return getResponse(this);
}
};

function getResponse(codec) {
if (codec.pendingData) {
const output = codec.pendingData;
codec.pendingData = null;
return output;
} else {
return new Uint8Array();
}
}
}

const { Deflate, Inflate } = initShimAsyncCodec(
{ Deflate: FflateDeflate, Inflate: FflateInflate },
undefined,
(codec, onData) => (codec.ondata = onData)
);
export { Deflate, Inflate };
Loading

0 comments on commit 486c628

Please sign in to comment.