Skip to content
Open
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
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Start with Go image to build the binary
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS builder
FROM --platform=$BUILDPLATFORM golang:1.23-alpine AS builder

# Install build dependencies including bash
RUN apk add --no-cache make git bash

# Set working directory
WORKDIR /app

# Clone the repository
RUN git clone https://github.com/akave-ai/akavesdk .
# Add cache busting
ADD https://api.github.com/repos/akave-ai/akavesdk/git/refs/heads/main /tmp/version.json
RUN git clone -b main https://github.com/akave-ai/akavesdk .

# Set the target platform for the build
ARG TARGETOS
Expand Down
6 changes: 6 additions & 0 deletions jest.config.integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
testTimeout: 30000,
testEnvironment: "node",
testMatch: ["**/tests/integration/**/*.test.js"],
setupFiles: ["dotenv/config"],
};
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest",
"test:integration": "jest --config=jest.config.integration.js"
},
"files": [
"index.js",
"server.js"
],
"dependencies": {
"axios": "^1.7.9",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"express-rate-limit": "^7.5.0",
"multer": "^1.4.5-lts.1",
"viem": "^2.21.42",
"cors": "^2.8.5"
"uuid": "^11.0.5",
"viem": "^2.21.42"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"form-data": "^4.0.0",
"jest": "^29.7.0",
"nodemon": "^3.0.3"
},
"engines": {
Expand Down
17 changes: 17 additions & 0 deletions rate-limiting-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const rateLimit = require('express-rate-limit');
const logger = require('./logger');

const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: "Too many requests from this IP, please try again later.",
statusCode: 429,
onLimit: (req, res) => {
logger.error(req.ip, 'Rate limit exceeded', {
path: req.path,
method: req.method
});
}
});

module.exports = apiLimiter;
55 changes: 32 additions & 23 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const path = require("path");
const os = require("os");
const dotenv = require("dotenv");
const cors = require("cors");
const apiLimiter = require("./rate-limiting-middleware");

dotenv.config();

Expand All @@ -15,14 +16,17 @@ const app = express();

// Configure CORS
const corsOptions = {
origin: process.env.CORS_ORIGIN || '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
origin: process.env.CORS_ORIGIN || "*",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
optionsSuccessStatus: 204,
};

app.use(cors(corsOptions));

// Rate limiter middleware
app.use(apiLimiter);

// Middleware to parse JSON bodies
app.use(express.json());

Expand Down Expand Up @@ -53,13 +57,15 @@ const logger = {
},
warn: (id, message, data = {}) => {
console.warn(`[${id}] 🟡 ${message}`, data);
}
},
};

// After client initialization
logger.info('INIT', 'Initializing client', {
logger.info("INIT", "Initializing client", {
nodeAddress: process.env.NODE_ADDRESS,
privateKeyLength: process.env.PRIVATE_KEY ? process.env.PRIVATE_KEY.length : 0,
privateKeyLength: process.env.PRIVATE_KEY
? process.env.PRIVATE_KEY.length
: 0,
});

// Health check endpoint
Expand Down Expand Up @@ -131,24 +137,21 @@ app.get("/buckets/:bucketName/files/:fileName", async (req, res) => {
app.post("/buckets/:bucketName/files", upload, async (req, res) => {
const requestId = Math.random().toString(36).substring(7);
try {
logger.info(requestId, 'Processing file upload request', {
bucket: req.params.bucketName
logger.info(requestId, "Processing file upload request", {
bucket: req.params.bucketName,
});

let result;
const uploadedFile = req.files?.file?.[0] || req.files?.file1?.[0];

if (uploadedFile) {
logger.info(requestId, 'Handling buffer upload', {
filename: uploadedFile.originalname
logger.info(requestId, "Handling buffer upload", {
filename: uploadedFile.originalname,
});
// Handle buffer upload
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "akave-"));
// Sanitize filename by replacing spaces and special chars with underscore
const sanitizedFileName = uploadedFile.originalname.replace(
/[^a-zA-Z0-9.]/g,
"_"
);
const sanitizedFileName = normalizeFileName(uploadedFile.originalname);
const tempFilePath = path.join(tempDir, sanitizedFileName);
try {
// Write buffer to temporary file
Expand All @@ -164,8 +167,8 @@ app.post("/buckets/:bucketName/files", upload, async (req, res) => {
await fs.rm(tempDir, { recursive: true, force: true });
}
} else if (req.body.filePath) {
logger.info(requestId, 'Handling file path upload', {
path: req.body.filePath
logger.info(requestId, "Handling file path upload", {
path: req.body.filePath,
});
// Handle file path upload
result = await client.uploadFile(
Expand All @@ -176,27 +179,28 @@ app.post("/buckets/:bucketName/files", upload, async (req, res) => {
throw new Error("No file or filePath provided");
}

logger.info(requestId, 'File upload completed', { result });
logger.info(requestId, "File upload completed", { result });
res.json({ success: true, data: result });
} catch (error) {
logger.error(requestId, 'File upload failed', error);
logger.error(requestId, "File upload failed", error);
res.status(500).json({ success: false, error: error.message });
}
});

app.get("/buckets/:bucketName/files/:fileName/download", async (req, res) => {
const requestId = Math.random().toString(36).substring(7);
try {
logger.info(requestId, 'Processing download request', {
logger.info(requestId, "Processing download request", {
bucket: req.params.bucketName,
file: req.params.fileName
file: req.params.fileName,
});

// Create downloads directory if it doesn't exist
const downloadDir = path.join(process.cwd(), "downloads");
await fs.mkdir(downloadDir, { recursive: true });

const destinationPath = path.join(downloadDir, req.params.fileName);
const normalizedFileName = normalizeFileName(req.params.fileName);
const destinationPath = path.join(downloadDir, normalizedFileName);

// Download the file
await client.downloadFile(
Expand Down Expand Up @@ -228,16 +232,16 @@ app.get("/buckets/:bucketName/files/:fileName/download", async (req, res) => {

// Handle stream errors
fileStream.on("error", (err) => {
logger.error(requestId, 'Stream error occurred', err);
logger.error(requestId, "Stream error occurred", err);
if (!res.headersSent) {
res.status(500).json({ success: false, error: err.message });
}
});

logger.info(requestId, 'Starting file stream');
logger.info(requestId, "Starting file stream");
fileStream.pipe(res);
} catch (error) {
logger.error(requestId, 'Download failed', error);
logger.error(requestId, "Download failed", error);
res.status(500).json({ success: false, error: error.message });
}
});
Expand All @@ -247,3 +251,8 @@ const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

// Add at the top of server.js with other utilities
function normalizeFileName(fileName) {
return fileName.replace(/[^a-zA-Z0-9.-]/g, "_");
}
96 changes: 96 additions & 0 deletions tests/integration/bucket.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const axios = require("axios");
const fs = require("fs");
const crypto = require("crypto");
const { v4: uuidv4 } = require("uuid");
const FormData = require("form-data");
const path = require("path");

const API_BASE_URL = process.env.API_URL || "http://localhost:8000";
const TEST_TIMEOUT = 30000; // 30 seconds

describe("Bucket Operations", () => {
let bucketName;
let tempFileName;
let tempFilePath;

beforeAll(() => {
// Create test directory if it doesn't exist
if (!fs.existsSync("./test-files")) {
fs.mkdirSync("./test-files");
}
bucketName = `test-${Math.random().toString(36).substring(7)}`;
});

test(
"should create a new bucket",
async () => {
const response = await axios.post(`${API_BASE_URL}/buckets`, {
bucketName,
});
expect(response.data.success).toBe(true);
expect(response.data.data.Name).toBe(bucketName);
},
TEST_TIMEOUT
);

test(
"should list buckets",
async () => {
const response = await axios.get(`${API_BASE_URL}/buckets`);
expect(response.data.success).toBe(true);
expect(Array.isArray(response.data.data)).toBe(true);
expect(
response.data.data.some((bucket) => bucket.Name === bucketName)
).toBe(true);
},
TEST_TIMEOUT
);

test(
"should upload and download file",
async () => {
// Create test file
tempFileName = `test_${Date.now()}_${uuidv4().replace(/-/g, "_")}.bin`;
tempFilePath = path.join("./test-files", tempFileName);

// Create test file with random data
fs.writeFileSync(tempFilePath, crypto.randomBytes(1024));

// Upload
const form = new FormData();
form.append("file", fs.createReadStream(tempFilePath));

const uploadResponse = await axios.post(
`${API_BASE_URL}/buckets/${bucketName}/files`,
form,
{
headers: form.getHeaders(),
maxContentLength: Infinity,
maxBodyLength: Infinity,
}
);
expect(uploadResponse.data.success).toBe(true);

// Wait a bit for the file to be available
await new Promise((resolve) => setTimeout(resolve, 1000));

// Download and verify
const downloadResponse = await axios.get(
`${API_BASE_URL}/buckets/${bucketName}/files/${tempFileName}/download`,
{ responseType: "arraybuffer" }
);

const downloadedContent = Buffer.from(downloadResponse.data);
const originalContent = fs.readFileSync(tempFilePath);
expect(downloadedContent).toEqual(originalContent);
},
TEST_TIMEOUT
);

afterAll(async () => {
// Cleanup
if (fs.existsSync("./test-files")) {
fs.rmSync("./test-files", { recursive: true, force: true });
}
});
});
8 changes: 4 additions & 4 deletions web3-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ const { privateKeyToAccount } = require('viem/accounts');

// Define Akave Fuji chain
const akaveFuji = {
id: 78963,
name: "Akave Fuji",
id: 78964,
name: "Akave Testnet",
nativeCurrency: {
decimals: 18,
name: "AKAVE",
symbol: "AKVF",
symbol: "AKVT",
},
rpcUrls: {
default: {
http: ["https://node1-asia.ava.akave.ai/ext/bc/tLqcnkJkZ1DgyLyWmborZK9d7NmMj6YCzCFmf9d9oQEd2fHon/rpc"],
http: ["https://n1-us.akave.ai/ext/bc/2JMWNmZbYvWcJRPPy1siaDBZaDGTDAaqXoY5UBKh4YrhNFzEce/rpc"],
},
},
};
Expand Down