Skip to content

feat: implement compression stream in c++ #43

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

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
boost: 1dca942403ed9342f98334bf4c3621f011aa7946
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
FastIO: 120a9c95af04ba7671b95aa2b5862f64a59a47bf
FastIO: 58c29380cf3ea0dfc4e65554dfa51a43a1468b9a
FBLazyVector: aa59bef5c46e93168bffcf3dc37ee1e176de799a
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
Expand Down
3 changes: 3 additions & 0 deletions example/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const root = path.resolve(__dirname, '..')
*/
const config = {
watchFolders: [root],
resolver: {
unstable_enablePackageExports: true,
},
}

module.exports = mergeConfig(getDefaultConfig(__dirname), config)
2 changes: 1 addition & 1 deletion example/server/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function createServer(payload: string | Bun.BufferSource, port: number) {
return new Response('No body provided', { status: 400 })
}

const writer = Bun.file('uploaded_file').writer()
const writer = Bun.file('uploaded_file.jpg.gz').writer()

for await (const chunk of req.body) {
// Write each chunk to file
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native-fast-io/FastIO.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/grabbou/react-native-fast-io.git", :tag => "#{s.version}" }

s.source_files = [
"ios/**/*.{h,m,swift}"
"ios/**/*.{h,m,swift}",
"cpp/**/*.{h,hpp,cpp}"
]

s.pod_target_xcconfig = {
Expand Down
8 changes: 7 additions & 1 deletion packages/react-native-fast-io/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ set (CMAKE_CXX_STANDARD 20)
# Define C++ library and add all sources
add_library(${PACKAGE_NAME} SHARED
src/main/cpp/cpp-adapter.cpp
src/main/cpp/HybridStreamFactory.cpp
../cpp/HybridCompressor.cpp
../cpp/HybridCompressorFactory.cpp
)

# Add Nitrogen specs :)
Expand All @@ -16,14 +19,17 @@ include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/FastIO+autolinking.cma
# Set up local includes
include_directories(
"src/main/cpp"
# "../cpp"
"../cpp"
)

find_package(ZLIB REQUIRED)
find_library(LOG_LIB log)

# Link all libraries together
target_link_libraries(
${PACKAGE_NAME}
${LOG_LIB}
${ZLIB_LIBRARIES}
android # <-- Android core
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <jni.h>
#include "Constants.h"

extern "C"
JNIEXPORT jint JNICALL
Java_com_margelo_nitro_fastio_HybridStreamFactory_getBufferSize(
JNIEnv* env,
jclass /* this */
) {
return FASTIO_BUFFER_SIZE;
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import com.margelo.nitro.core.Promise

class HybridStreamFactory : HybridStreamFactorySpec() {
override val bufferSize: Double
get() = throw NotImplementedError("HybridStreamFactory.bufferSize not implemented")
get() = getBufferSize().toDouble()

override fun createInputStream(path: String): HybridInputStreamSpec {
throw NotImplementedError("HybridStreamFactory.createInputStream() not implemented")
}

override val memorySize: Long
get() = 0L

companion object {
@JvmStatic
private external fun getBufferSize(): Int
}
}
11 changes: 11 additions & 0 deletions packages/react-native-fast-io/cpp/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#define FASTIO_BUFFER_SIZE (64 * 1024)

#ifdef __cplusplus
}
#endif
76 changes: 76 additions & 0 deletions packages/react-native-fast-io/cpp/HybridCompressor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "HybridCompressor.hpp"
#include <stdexcept>
#include <iostream>

namespace margelo::nitro::fastio {

HybridCompressor::HybridCompressor(CompressionAlgorithm algorithm) : HybridObject(TAG) {
std::cout << "Initializing compressor with algorithm: " << static_cast<int>(algorithm) << std::endl;

_stream.zalloc = Z_NULL;
_stream.zfree = Z_NULL;
_stream.opaque = Z_NULL;

int windowBits = 15;
switch (algorithm) {
case CompressionAlgorithm::GZIP:
windowBits += 16;
break;
case CompressionAlgorithm::DEFLATE_RAW:
windowBits = -windowBits;
break;
default:
break;
}

if (deflateInit2(&_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
throw std::runtime_error("Failed to initialize compression");
}
}

HybridCompressor::~HybridCompressor() {
deflateEnd(&_stream);
}

std::shared_ptr<ArrayBuffer> HybridCompressor::compressBuffer(const uint8_t* source, size_t sourceSize, bool finalize) {
uint8_t* destBuffer = new uint8_t[64 * 1024];

_stream.next_in = const_cast<Bytef*>(source);
_stream.avail_in = sourceSize;
_stream.next_out = destBuffer;
_stream.avail_out = 64 * 1024;

int status = deflate(&_stream, finalize ? Z_FINISH : Z_NO_FLUSH);

if (status == Z_STREAM_ERROR) {
delete[] destBuffer;
throw std::runtime_error("Compression error");
}

if (_stream.avail_in != 0) {
delete[] destBuffer;
throw std::runtime_error("Unexpected remaining input data");
}

size_t currentOffset = 64 * 1024 - _stream.avail_out;

return ArrayBuffer::makeBuffer(
destBuffer,
currentOffset,
[=] { delete[] destBuffer; }
);
}

std::shared_ptr<ArrayBuffer> HybridCompressor::compress(const std::shared_ptr<ArrayBuffer>& chunk) {
return compressBuffer(
chunk->data(),
chunk->size()
);
}

std::shared_ptr<ArrayBuffer> HybridCompressor::finalize() {
static const uint8_t dummyByte = 0;
return compressBuffer(&dummyByte, 0, true);
}

}
26 changes: 26 additions & 0 deletions packages/react-native-fast-io/cpp/HybridCompressor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <memory>
#include <vector>
#include <zlib.h>

#include "HybridCompressorSpec.hpp"
#include "CompressionAlgorithm.hpp"

namespace margelo::nitro::fastio {

class HybridCompressor : public HybridCompressorSpec {
public:
explicit HybridCompressor(CompressionAlgorithm algorithm);
~HybridCompressor();

std::shared_ptr<ArrayBuffer> compress(const std::shared_ptr<ArrayBuffer>& chunk);
std::shared_ptr<ArrayBuffer> finalize();

private:
std::shared_ptr<ArrayBuffer> compressBuffer(const uint8_t* source, size_t sourceSize, bool finalize = false);

z_stream _stream{};
};

}
11 changes: 11 additions & 0 deletions packages/react-native-fast-io/cpp/HybridCompressorFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "HybridCompressorFactory.hpp"
#include "HybridCompressor.hpp"
#include <stdexcept>

namespace margelo::nitro::fastio {

std::shared_ptr<HybridCompressorSpec> HybridCompressorFactory::create(CompressionAlgorithm algorithm) {
return std::make_shared<HybridCompressor>(algorithm);
}

}
16 changes: 16 additions & 0 deletions packages/react-native-fast-io/cpp/HybridCompressorFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <memory>
#include "HybridCompressorFactorySpec.hpp"
#include "HybridCompressor.hpp"

namespace margelo::nitro::fastio {

class HybridCompressorFactory : public HybridCompressorFactorySpec {
public:
HybridCompressorFactory() : HybridObject(TAG) {}

std::shared_ptr<HybridCompressorSpec> create(CompressionAlgorithm algorithm) override;
};

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module FastIOPrivate {
header "../RCTUtilsWrapper.h"
header "../../cpp/Constants.h"
export *
}
Loading