diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 2e6213d..e282172 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -5,13 +5,12 @@ name: Node.js CI on: push: - branches: [ "master" ] + branches: ["master"] pull_request: - branches: [ "master" ] + branches: ["master"] jobs: build: - runs-on: ubuntu-latest strategy: @@ -20,12 +19,12 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - cache: 'npm' - - run: npm ci - - run: npm run build --if-present - - run: npm test + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: "npm" + - run: npm ci + - run: npm run build --if-present + - run: npm test diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..105164e --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +test +lib \ No newline at end of file diff --git a/main.js b/main.js deleted file mode 100644 index 151dbc6..0000000 --- a/main.js +++ /dev/null @@ -1 +0,0 @@ -export { Archive } from "./src/libarchive.js"; diff --git a/package.json b/package.json index de87d7c..d7246a1 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "libarchive.js", "version": "2.0.0", - "description": "extract archive files in browser, libarchive port in wasm", - "main": "main.js", + "description": "extract/create archive files in browser/nodejs, libarchive port in wasm", + "main": "dist/libarchive.js", "repository": { "type": "git", "url": "https://github.com/nika-begiashvili/libarchivejs" @@ -24,7 +24,8 @@ "7z", "javascript", "js", - "browser" + "browser", + "nodejs" ], "author": "Nika Begiashvili", "license": "MIT", diff --git a/src/archive-reader.ts b/src/archive-reader.ts index 12704db..0e6a7a3 100644 --- a/src/archive-reader.ts +++ b/src/archive-reader.ts @@ -3,57 +3,56 @@ import { CompressedFile } from "./compressed-file.js"; import { cloneContent, getObjectPropReference, objectToArray } from "./utils"; export type ArchiveEntry = { - size: number; - path: string; - type: string; - lastModified: number; - fileData: ArrayBuffer; - fileName: string; - }; + size: number; + path: string; + type: string; + lastModified: number; + fileData: ArrayBuffer; + fileName: string; +}; export class ArchiveReader { + private file: File | null; + private client: any; + private worker: any; - private file: File | null; - private client: any; - private worker: any; + private _content: any = {}; + private _processed: number = 0; - private _content: any = {}; - private _processed: number = 0; - - constructor(file: File, client: any, worker: any) { - this.file = file; - this.client = client; - this.worker = worker; - } + constructor(file: File, client: any, worker: any) { + this.file = file; + this.client = client; + this.worker = worker; + } - /** + /** * Prepares file for reading * @returns {Promise} archive instance */ - open(): Promise { - this._content = {}; - this._processed = 0; - return new Promise((resolve, _) => { - this.client.open( - this.file, - Comlink.proxy(() => { - resolve(this); - }), - ); - }); - } + open(): Promise { + this._content = {}; + this._processed = 0; + return new Promise((resolve, _) => { + this.client.open( + this.file, + Comlink.proxy(() => { + resolve(this); + }), + ); + }); + } - /** + /** * Terminate worker to free up memory */ - async close() { - this.worker?.terminate(); - this.worker = null; - this.client = null; - this.file = null; - } + async close() { + this.worker?.terminate(); + this.worker = null; + this.client = null; + this.file = null; + } - /** + /** * detect if archive has encrypted data * @returns {boolean|null} null if could not be determined */ @@ -155,6 +154,4 @@ export class ArchiveReader { this.worker?.terminate(); return cloneContent(this._content); } - - -} \ No newline at end of file +} diff --git a/src/libarchive.ts b/src/libarchive.ts index 954ca2b..204e58d 100644 --- a/src/libarchive.ts +++ b/src/libarchive.ts @@ -80,7 +80,8 @@ export class Archive { } private static async getClient(worker: any, options: ArchiveOptions) { - const Client = options.createClient?.(worker) || (Comlink.wrap(worker as any) as any); + const Client = + options.createClient?.(worker) || (Comlink.wrap(worker as any) as any); // @ts-ignore - Promise.WithResolvers let { promise: clientReady, resolve } = Promise.withResolvers(); @@ -95,5 +96,4 @@ export class Archive { return client; } - } diff --git a/src/webworker/archive-writer.js b/src/webworker/archive-writer.js index 3836f2e..468af5b 100644 --- a/src/webworker/archive-writer.js +++ b/src/webworker/archive-writer.js @@ -62,7 +62,7 @@ export class ArchiveWriter { readNumberFromPointer(ptr) { const ptrSize = this._runCode.sizeOfSizeT(); - const outputSizeBytes = this._wasmModule.HEAPU8.slice(ptr,ptr + ptrSize); + const outputSizeBytes = this._wasmModule.HEAPU8.slice(ptr, ptr + ptrSize); let output = null; if (ptrSize == 4) { @@ -70,7 +70,7 @@ export class ArchiveWriter { } else if (ptrSize == 8) { output = new BigUint64Array(outputSizeBytes)[0]; } else throw Error("Unexpected size of size_t: " + ptrSize); - + return output; } } diff --git a/test/node.test.mjs b/test/node.test.mjs index a76a7d3..cc761d1 100644 --- a/test/node.test.mjs +++ b/test/node.test.mjs @@ -1,4 +1,8 @@ -import { Archive, ArchiveCompression, ArchiveFormat } from "../dist/libarchive-node.mjs"; +import { + Archive, + ArchiveCompression, + ArchiveFormat, +} from "../dist/libarchive-node.mjs"; import fs from "fs"; import { Blob } from "buffer"; import { fileChecksums } from "./checksum-utils"; @@ -17,16 +21,17 @@ describe("Extract file using nodejs", () => { archive.close(); }, 5000); - test("Create new archive", async () => { let buffer = fs.readFileSync("test/files/archives/README.md"); let blob = new Blob([buffer]); const archiveFile = await Archive.write({ - files: [{ - file: blob, - pathname: "README.md", - }], + files: [ + { + file: blob, + pathname: "README.md", + }, + ], outputFileName: "test.tar.gz", compression: ArchiveCompression.GZIP, format: ArchiveFormat.USTAR, @@ -40,5 +45,4 @@ describe("Extract file using nodejs", () => { archive.close(); }, 5000); - }); diff --git a/test/testutils.js b/test/testutils.js index 6acc0ed..52e956f 100644 --- a/test/testutils.js +++ b/test/testutils.js @@ -20,11 +20,13 @@ const startServer = () => module.exports = { setup: async () => { - let browser = isWsl ? await puppeteer.launch({ - headless: "new", - executablePath: "google-chrome", - args: ["--no-sandbox"], - }) : await puppeteer.launch({headless: 'new'}); + let browser = isWsl + ? await puppeteer.launch({ + headless: "new", + executablePath: "google-chrome", + args: ["--no-sandbox"], + }) + : await puppeteer.launch({ headless: "new" }); let page = await browser.newPage(); await page.setViewport({ width, height });