initial commit

This commit is contained in:
DecDuck
2024-10-06 13:45:03 +11:00
commit 2b07f487a6
27 changed files with 3543 additions and 0 deletions

2
.cargo/config.toml Normal file
View File

@ -0,0 +1,2 @@
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

208
.github/workflows/CI.yml vendored Normal file
View File

@ -0,0 +1,208 @@
name: CI
env:
DEBUG: napi:*
APP_NAME: droplet
MACOSX_DEPLOYMENT_TARGET: '10.13'
permissions:
contents: write
id-token: write
'on':
push:
branches:
- main
tags-ignore:
- '**'
paths-ignore:
- '**/*.md'
- LICENSE
- '**/*.gitignore'
- .editorconfig
- docs/**
pull_request: null
jobs:
build:
strategy:
fail-fast: false
matrix:
settings:
- host: macos-latest
target: x86_64-apple-darwin
build: yarn build --target x86_64-apple-darwin
- host: windows-latest
build: yarn build --target x86_64-pc-windows-msvc
target: x86_64-pc-windows-msvc
- host: ubuntu-latest
target: x86_64-unknown-linux-gnu
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian
build: yarn build --target x86_64-unknown-linux-gnu
name: stable - ${{ matrix.settings.target }} - node@20
runs-on: ${{ matrix.settings.host }}
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
if: ${{ !matrix.settings.docker }}
with:
node-version: 20
cache: yarn
- name: Install
uses: dtolnay/rust-toolchain@stable
if: ${{ !matrix.settings.docker }}
with:
toolchain: stable
targets: ${{ matrix.settings.target }}
- name: Cache cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
.cargo-cache
target/
key: ${{ matrix.settings.target }}-cargo-${{ matrix.settings.host }}
- uses: goto-bus-stop/setup-zig@v2
if: ${{ matrix.settings.target == 'armv7-unknown-linux-gnueabihf' || matrix.settings.target == 'armv7-unknown-linux-musleabihf' }}
with:
version: 0.13.0
- name: Setup toolchain
run: ${{ matrix.settings.setup }}
if: ${{ matrix.settings.setup }}
shell: bash
- name: Setup node x86
if: matrix.settings.target == 'i686-pc-windows-msvc'
run: yarn config set supportedArchitectures.cpu "ia32"
shell: bash
- name: Install dependencies
run: yarn install
- name: Setup node x86
uses: actions/setup-node@v4
if: matrix.settings.target == 'i686-pc-windows-msvc'
with:
node-version: 20
cache: yarn
architecture: x86
- name: Build in docker
uses: addnab/docker-run-action@v3
if: ${{ matrix.settings.docker }}
with:
image: ${{ matrix.settings.docker }}
options: '--user 0:0 -v ${{ github.workspace }}/.cargo-cache/git/db:/usr/local/cargo/git/db -v ${{ github.workspace }}/.cargo/registry/cache:/usr/local/cargo/registry/cache -v ${{ github.workspace }}/.cargo/registry/index:/usr/local/cargo/registry/index -v ${{ github.workspace }}:/build -w /build'
run: ${{ matrix.settings.build }}
- name: Build
run: ${{ matrix.settings.build }}
if: ${{ !matrix.settings.docker }}
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: bindings-${{ matrix.settings.target }}
path: ${{ env.APP_NAME }}.*.node
if-no-files-found: error
test-macOS-windows-binding:
name: Test bindings on ${{ matrix.settings.target }} - node@${{ matrix.node }}
needs:
- build
strategy:
fail-fast: false
matrix:
settings:
- host: macos-latest
target: x86_64-apple-darwin
- host: windows-latest
target: x86_64-pc-windows-msvc
node:
- '18'
- '20'
runs-on: ${{ matrix.settings.host }}
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn
architecture: x64
- name: Install dependencies
run: yarn install
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: bindings-${{ matrix.settings.target }}
path: .
- name: List packages
run: ls -R .
shell: bash
- name: Test bindings
run: yarn test
test-linux-x64-gnu-binding:
name: Test bindings on Linux-x64-gnu - node@${{ matrix.node }}
needs:
- build
strategy:
fail-fast: false
matrix:
node:
- '18'
- '20'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn
- name: Install dependencies
run: yarn install
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: bindings-x86_64-unknown-linux-gnu
path: .
- name: List packages
run: ls -R .
shell: bash
- name: Test bindings
run: docker run --rm -v $(pwd):/build -w /build node:${{ matrix.node }}-slim yarn test
publish:
name: Publish
runs-on: ubuntu-latest
needs:
- test-macOS-windows-binding
- test-linux-x64-gnu-binding
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn
- name: Install dependencies
run: yarn install
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
- name: Move artifacts
run: yarn artifacts
- name: List packages
run: ls -R ./npm
shell: bash
- name: Publish
run: |
npm config set provenance true
if git log -1 --pretty=%B | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+$";
then
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
npm publish --access public
elif git log -1 --pretty=%B | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+";
then
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
npm publish --tag next --access public
else
echo "Not a release, skipping publish"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

197
.gitignore vendored Normal file
View File

@ -0,0 +1,197 @@
# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# End of https://www.toptal.com/developers/gitignore/api/node
# Created by https://www.toptal.com/developers/gitignore/api/macos
# Edit at https://www.toptal.com/developers/gitignore?templates=macos
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### macOS Patch ###
# iCloud generated files
*.icloud
# End of https://www.toptal.com/developers/gitignore/api/macos
# Created by https://www.toptal.com/developers/gitignore/api/windows
# Edit at https://www.toptal.com/developers/gitignore?templates=windows
### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# End of https://www.toptal.com/developers/gitignore/api/windows
#Added by cargo
/target
Cargo.lock
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
*.node

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

11
.idea/gradle.xml generated Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="corretto-19" />
</GradleProjectSettings>
</option>
</component>
</project>

4
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
</project>

13
.npmignore Normal file
View File

@ -0,0 +1,13 @@
target
Cargo.lock
.cargo
.github
npm
.eslintrc
.prettierignore
rustfmt.toml
yarn.lock
*.node
.yarn
__test__
renovate.json

925
.yarn/releases/yarn-4.5.0.cjs vendored Executable file

File diff suppressed because one or more lines are too long

3
.yarnrc.yml Normal file
View File

@ -0,0 +1,3 @@
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-4.5.0.cjs

31
Cargo.toml Normal file
View File

@ -0,0 +1,31 @@
[package]
edition = "2021"
name = "droplet"
version = "0.0.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.2", default-features = false, features = ["napi4", "async"] }
napi-derive = "2.12.2"
xz2 = "0.1.7"
rayon = "1.10.0"
serde = "1.0.210"
ciborium = "0.2.2"
[dependencies.uuid]
version = "1.10.0"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]
[build-dependencies]
napi-build = "2.0.1"
[profile.release]
lto = true
strip = "symbols"

7
__test__/index.spec.mjs Normal file
View File

@ -0,0 +1,7 @@
import test from 'ava'
import { sum } from '../index.js'
test('sum from native', (t) => {
t.is(sum(1, 2), 3)
})

5
build.rs Normal file
View File

@ -0,0 +1,5 @@
extern crate napi_build;
fn main() {
napi_build::setup();
}

6
index.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
/* tslint:disable */
/* eslint-disable */
/* auto-generated by NAPI-RS */
export declare function repack(source: string, output: string): Promise<void>

315
index.js Normal file
View File

@ -0,0 +1,315 @@
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
/* auto-generated by NAPI-RS */
const { existsSync, readFileSync } = require('fs')
const { join } = require('path')
const { platform, arch } = process
let nativeBinding = null
let localFileExisted = false
let loadError = null
function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
}
} else {
const { glibcVersionRuntime } = process.report.getReport().header
return !glibcVersionRuntime
}
}
switch (platform) {
case 'android':
switch (arch) {
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'droplet.android-arm64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./droplet.android-arm64.node')
} else {
nativeBinding = require('droplet-android-arm64')
}
} catch (e) {
loadError = e
}
break
case 'arm':
localFileExisted = existsSync(join(__dirname, 'droplet.android-arm-eabi.node'))
try {
if (localFileExisted) {
nativeBinding = require('./droplet.android-arm-eabi.node')
} else {
nativeBinding = require('droplet-android-arm-eabi')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Android ${arch}`)
}
break
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(
join(__dirname, 'droplet.win32-x64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.win32-x64-msvc.node')
} else {
nativeBinding = require('droplet-win32-x64-msvc')
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(
join(__dirname, 'droplet.win32-ia32-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.win32-ia32-msvc.node')
} else {
nativeBinding = require('droplet-win32-ia32-msvc')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'droplet.win32-arm64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.win32-arm64-msvc.node')
} else {
nativeBinding = require('droplet-win32-arm64-msvc')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Windows: ${arch}`)
}
break
case 'darwin':
localFileExisted = existsSync(join(__dirname, 'droplet.darwin-universal.node'))
try {
if (localFileExisted) {
nativeBinding = require('./droplet.darwin-universal.node')
} else {
nativeBinding = require('droplet-darwin-universal')
}
break
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'droplet.darwin-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./droplet.darwin-x64.node')
} else {
nativeBinding = require('droplet-darwin-x64')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'droplet.darwin-arm64.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.darwin-arm64.node')
} else {
nativeBinding = require('droplet-darwin-arm64')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on macOS: ${arch}`)
}
break
case 'freebsd':
if (arch !== 'x64') {
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
}
localFileExisted = existsSync(join(__dirname, 'droplet.freebsd-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./droplet.freebsd-x64.node')
} else {
nativeBinding = require('droplet-freebsd-x64')
}
} catch (e) {
loadError = e
}
break
case 'linux':
switch (arch) {
case 'x64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-x64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-x64-musl.node')
} else {
nativeBinding = require('droplet-linux-x64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-x64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-x64-gnu.node')
} else {
nativeBinding = require('droplet-linux-x64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-arm64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-arm64-musl.node')
} else {
nativeBinding = require('droplet-linux-arm64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-arm64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-arm64-gnu.node')
} else {
nativeBinding = require('droplet-linux-arm64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-arm-musleabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-arm-musleabihf.node')
} else {
nativeBinding = require('droplet-linux-arm-musleabihf')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-arm-gnueabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('droplet-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
}
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-riscv64-musl.node')
} else {
nativeBinding = require('droplet-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-riscv64-gnu.node')
} else {
nativeBinding = require('droplet-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
localFileExisted = existsSync(
join(__dirname, 'droplet.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./droplet.linux-s390x-gnu.node')
} else {
nativeBinding = require('droplet-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
break
default:
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}
if (!nativeBinding) {
if (loadError) {
throw loadError
}
throw new Error(`Failed to load native binding`)
}
const { repack } = nativeBinding
module.exports.repack = repack

3
npm/darwin-x64/README.md Normal file
View File

@ -0,0 +1,3 @@
# `droplet-darwin-x64`
This is the **x86_64-apple-darwin** binary for `droplet`

View File

@ -0,0 +1,18 @@
{
"name": "droplet-darwin-x64",
"version": "0.0.0",
"os": [
"darwin"
],
"cpu": [
"x64"
],
"main": "droplet.darwin-x64.node",
"files": [
"droplet.darwin-x64.node"
],
"license": "MIT",
"engines": {
"node": ">= 10"
}
}

View File

@ -0,0 +1,3 @@
# `droplet-linux-x64-gnu`
This is the **x86_64-unknown-linux-gnu** binary for `droplet`

View File

@ -0,0 +1,21 @@
{
"name": "droplet-linux-x64-gnu",
"version": "0.0.0",
"os": [
"linux"
],
"cpu": [
"x64"
],
"main": "droplet.linux-x64-gnu.node",
"files": [
"droplet.linux-x64-gnu.node"
],
"license": "MIT",
"engines": {
"node": ">= 10"
},
"libc": [
"glibc"
]
}

View File

@ -0,0 +1,3 @@
# `droplet-win32-x64-msvc`
This is the **x86_64-pc-windows-msvc** binary for `droplet`

View File

@ -0,0 +1,18 @@
{
"name": "droplet-win32-x64-msvc",
"version": "0.0.0",
"os": [
"win32"
],
"cpu": [
"x64"
],
"main": "droplet.win32-x64-msvc.node",
"files": [
"droplet.win32-x64-msvc.node"
],
"license": "MIT",
"engines": {
"node": ">= 10"
}
}

31
package.json Normal file
View File

@ -0,0 +1,31 @@
{
"name": "droplet",
"version": "0.0.0",
"main": "index.js",
"types": "index.d.ts",
"napi": {
"name": "droplet",
"triples": {}
},
"license": "MIT",
"devDependencies": {
"@napi-rs/cli": "^2.18.4",
"ava": "^6.0.1"
},
"ava": {
"timeout": "3m"
},
"engines": {
"node": ">= 10"
},
"scripts": {
"artifacts": "napi artifacts",
"build": "napi build --platform --release",
"build:debug": "napi build --platform",
"prepublishOnly": "napi prepublish -t npm",
"test": "ava",
"universal": "napi universal",
"version": "napi version"
},
"packageManager": "yarn@4.5.0"
}

2
rustfmt.toml Normal file
View File

@ -0,0 +1,2 @@
tab_spaces = 2
edition = "2021"

21
src/file_utils.rs Normal file
View File

@ -0,0 +1,21 @@
use std::{fs::{self, metadata}, path::{Path, PathBuf}};
pub fn _list_files(vec: &mut Vec<PathBuf>, path: &Path) {
if metadata(&path).unwrap().is_dir() {
let paths = fs::read_dir(&path).unwrap();
for path_result in paths {
let full_path = path_result.unwrap().path();
if metadata(&full_path).unwrap().is_dir() {
_list_files(vec, &full_path);
} else {
vec.push(full_path);
}
}
}
}
pub fn list_files(path: &Path) -> Vec<PathBuf> {
let mut vec = Vec::new();
_list_files(&mut vec, &path);
return vec;
}

121
src/lib.rs Normal file
View File

@ -0,0 +1,121 @@
#![deny(clippy::all)]
const CHUNK_SIZE: usize = 1024 * 1024 * 16;
use file_utils::list_files;
use manifest::{generate_manifest, Manifest, ManifestChunk, ManifestRecord};
use napi::Error;
use std::{
collections::HashMap,
fs::File,
io::{self, BufRead, BufReader},
os::unix::fs::PermissionsExt,
path::Path,
sync::{Arc, Mutex},
};
use uuid::Uuid;
use xz2::bufread::XzEncoder;
pub mod file_utils;
pub mod manifest;
#[macro_use]
extern crate napi_derive;
fn compress(buffer: &[u8], output_path: &Path, chunk_id: Uuid) {
let chunk_path = output_path.join(chunk_id.to_string() + ".bin");
let mut chunk_file = File::create(chunk_path).unwrap();
let mut compressor: XzEncoder<&[u8]> = XzEncoder::new(&buffer[..], 6);
io::copy(&mut compressor, &mut chunk_file).unwrap();
}
#[napi]
pub async fn repack(source: String, output: String) -> Result<(), Error> {
let source_path = Path::new(&source);
let output_path = Path::new(&output);
let files = list_files(source_path);
let num_of_threads: u64 = 8;
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(num_of_threads.try_into().unwrap())
.build()
.unwrap();
let queue_size = Arc::new(Mutex::new(0));
pool.scope(|scope| {
let mut manifest = Manifest {
record: HashMap::new(),
};
for file_path in files {
let file = File::open(file_path.clone()).unwrap();
let permissions = file.try_clone().unwrap().metadata().unwrap().permissions();
let mut reader = BufReader::with_capacity(CHUNK_SIZE, file);
let relative = file_path.strip_prefix(source_path).unwrap();
let mut record = ManifestRecord {
chunks: Vec::new(),
permissions: 0,
};
#[cfg(unix)]
{
record.permissions = permissions.mode();
}
let mut chunk_index = 0;
loop {
let mut buffer: Vec<u8> = Vec::new();
reader.fill_buf().unwrap().clone_into(&mut buffer);
let length = buffer.len();
if length == 0 {
break;
}
{
*queue_size.lock().unwrap() += 1;
}
let chunk_id: Uuid = Uuid::new_v4();
let queue_size_handle = queue_size.clone();
scope.spawn(move |_scope| {
compress(&buffer, output_path, chunk_id);
let mut num = queue_size_handle.lock().unwrap();
*num -= 1;
});
reader.consume(length);
let chunk_record = ManifestChunk {
uuid: chunk_id.to_string(),
index: chunk_index,
};
record.chunks.push(chunk_record);
chunk_index += 1;
loop {
let num = queue_size.lock().unwrap();
if *num < num_of_threads {
break;
}
}
}
manifest
.record
.insert(relative.to_str().unwrap().to_string(), record);
println!("Queued {}", file_path.to_str().unwrap());
}
let manifest_path = output_path.join("manifest.drop");
generate_manifest(manifest, &manifest_path);
});
return Ok(());
}

25
src/manifest.rs Normal file
View File

@ -0,0 +1,25 @@
use std::{collections::HashMap, fs::File, path::Path};
use ciborium::into_writer;
#[derive(serde::Serialize)]
pub struct ManifestChunk {
pub uuid: String,
pub index: i64,
}
#[derive(serde::Serialize)]
pub struct ManifestRecord {
pub chunks: Vec<ManifestChunk>,
pub permissions: u32,
}
#[derive(serde::Serialize)]
pub struct Manifest {
pub record: HashMap<String, ManifestRecord>,
}
pub fn generate_manifest(manifest: Manifest, path: &Path) {
let file = File::create(path).unwrap();
into_writer(&manifest, file).unwrap();
}

6
test.mjs Normal file
View File

@ -0,0 +1,6 @@
import { repack } from './index.js';
const source = "/home/decduck/.steam/steam/steamapps/common/ClusterTruck";
const output = "/home/decduck/Dev/droplet-output";
await repack(source, output);

1536
yarn.lock Normal file

File diff suppressed because it is too large Load Diff