From 6d19702d26f207de9567cd8a460b258d6187856d Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 10 Jan 2026 19:15:06 +0100 Subject: [PATCH] bugfix --- src/index.ts | 20 ++++++++++++++++++++ src/plugin.ts | 29 ++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2721c0d..f19a2c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,26 @@ /** * MetaMesh Plugin: full-hash + * + * ============================================================================ + * PLUGIN MOUNT ARCHITECTURE - DO NOT MODIFY WITHOUT AUTHORIZATION + * ============================================================================ + * + * Each plugin container has exactly 3 mounts (2 for plugins without output): + * + * 1. /files (READ-ONLY) - Shared media files, read access only + * 2. /cache (READ-WRITE) - Plugin-specific cache folder + * 3. /files/plugin/ (READ-WRITE) - Plugin output folder (if needed) + * + * This plugin (full-hash) only requires mounts 1 and 2: + * - /files (RO) - to read entire files for SHA-256 hashing + * - /cache (RW) - to cache computed hashes + * + * SECURITY: Plugins must NEVER write to /files directly. + * All write operations go to /cache or /files/plugin/ only. + * + * ============================================================================ */ + import Fastify from 'fastify'; import type { HealthResponse, ProcessRequest, ProcessResponse, CallbackPayload, ConfigureRequest, ConfigureResponse } from './types.js'; import { manifest, process as processFile } from './plugin.js'; diff --git a/src/plugin.ts b/src/plugin.ts index c403499..bb4b8ad 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,6 +1,12 @@ /** * Full Hash Plugin - * Computes SHA-256 hash of the entire file + * + * Computes the SHA-256 hash of the entire file. + * This is a background queue plugin - it's slow but provides + * definitive content identification. + * + * Output: + * - cid_sha2-256: SHA-256 hash formatted as CID */ import { createReadStream } from 'fs'; @@ -30,6 +36,9 @@ export const manifest: PluginManifest = { config: {}, }; +/** + * Compute SHA-256 hash of a file using streaming + */ async function computeSHA256(filePath: string): Promise { return new Promise((resolve, reject) => { const hash = createHash('sha256'); @@ -52,6 +61,7 @@ export async function process( // Skip if already computed if (existingMeta?.['cid_sha2-256']) { + console.log(`[full-hash] SHA-256 already computed for ${filePath}, skipping`); await sendCallback({ taskId: request.taskId, status: 'skipped', @@ -61,16 +71,18 @@ export async function process( return; } - console.log(`[full-hash] Computing SHA-256 for ${filePath}`); - const sha256 = await computeSHA256(filePath); + console.log(`[full-hash] Computing SHA-256 hash for ${filePath}`); - // Format as CID-like string - const sha256Cid = `sha256-${sha256}`; + // Compute SHA-256 hash + const sha256Hex = await computeSHA256(filePath); + + // Format as CID-like string (matching old format) + const sha256Cid = `sha256-${sha256Hex}`; await metaCore.setProperty(cid, 'cid_sha2-256', sha256Cid); + console.log(`[full-hash] SHA-256 computed: ${sha256Cid.substring(0, 20)}...`); const duration = Date.now() - startTime; - console.log(`[full-hash] Computed in ${duration}ms: ${sha256Cid.substring(0, 20)}...`); await sendCallback({ taskId: request.taskId, @@ -79,11 +91,14 @@ export async function process( }); } catch (error) { const duration = Date.now() - startTime; + const errorMessage = error instanceof Error ? error.message : String(error); + console.error(`[full-hash] Error computing SHA-256 hash for ${request.filePath}:`, errorMessage); + await sendCallback({ taskId: request.taskId, status: 'failed', duration, - error: error instanceof Error ? error.message : String(error), + error: errorMessage, }); } }