ipfrs-interface 0.1.0

HTTP, gRPC, GraphQL and Python interfaces for IPFRS distributed storage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/env node
/**
 * IPFRS JavaScript/Node.js Client Example
 *
 * This example demonstrates how to interact with the IPFRS HTTP Gateway API
 * using JavaScript/Node.js, including:
 * - File upload and download
 * - Batch operations
 * - Tensor operations with Apache Arrow support
 * - Streaming uploads/downloads
 * - WebSocket real-time events
 *
 * Dependencies:
 *   npm install axios form-data apache-arrow ws
 */

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const { tableFromIPC } = require('apache-arrow');
const WebSocket = require('ws');

class IPFRSClient {
    /**
     * Initialize IPFRS client
     * @param {string} baseUrl - Base URL of the IPFRS gateway
     */
    constructor(baseUrl = 'http://localhost:8080') {
        this.baseUrl = baseUrl.replace(/\/$/, '');
        this.client = axios.create({
            baseURL: this.baseUrl,
            timeout: 30000,
        });
    }

    // ========================================================================
    // Kubo v0 API - IPFS Compatibility
    // ========================================================================

    /**
     * Upload a file to IPFRS (Kubo v0 API)
     * @param {string} filePath - Path to file to upload
     * @returns {Promise<{Hash: string, Size: number}>}
     */
    async addFile(filePath) {
        const form = new FormData();
        form.append('file', fs.createReadStream(filePath));

        const response = await this.client.post('/api/v0/add', form, {
            headers: form.getHeaders(),
        });

        return response.data;
    }

    /**
     * Download a file from IPFRS (Kubo v0 API)
     * @param {string} cid - Content Identifier
     * @returns {Promise<Buffer>}
     */
    async cat(cid) {
        const response = await this.client.post(`/api/v0/cat?arg=${cid}`, null, {
            responseType: 'arraybuffer',
        });

        return Buffer.from(response.data);
    }

    /**
     * Get raw block data
     * @param {string} cid - Content Identifier
     * @returns {Promise<Buffer>}
     */
    async getBlock(cid) {
        const response = await this.client.post(`/api/v0/block/get?arg=${cid}`, null, {
            responseType: 'arraybuffer',
        });

        return Buffer.from(response.data);
    }

    /**
     * Store raw block data
     * @param {Buffer} data - Raw block bytes
     * @returns {Promise<{Hash: string, Size: number}>}
     */
    async putBlock(data) {
        const form = new FormData();
        form.append('block', data);

        const response = await this.client.post('/api/v0/block/put', form, {
            headers: form.getHeaders(),
        });

        return response.data;
    }

    // ========================================================================
    // Gateway API - HTTP GET
    // ========================================================================

    /**
     * Get content via HTTP gateway
     * @param {string} cid - Content Identifier
     * @param {[number, number]|null} byteRange - Optional [start, end] for range request
     * @returns {Promise<Buffer>}
     */
    async get(cid, byteRange = null) {
        const headers = {};

        if (byteRange) {
            const [start, end] = byteRange;
            headers['Range'] = `bytes=${start}-${end}`;
        }

        const response = await this.client.get(`/ipfs/${cid}`, {
            headers,
            responseType: 'arraybuffer',
        });

        return Buffer.from(response.data);
    }

    // ========================================================================
    // High-Speed v1 API - Batch Operations
    // ========================================================================

    /**
     * Retrieve multiple blocks in parallel
     * @param {string[]} cids - Array of Content Identifiers
     * @returns {Promise<Array<{cid: string, data: string}>>}
     */
    async batchGetBlocks(cids) {
        const response = await this.client.post('/v1/block/batch/get', { cids });
        return response.data.blocks;
    }

    /**
     * Check existence of multiple blocks
     * @param {string[]} cids - Array of Content Identifiers
     * @returns {Promise<Array<{cid: string, exists: boolean}>>}
     */
    async batchHasBlocks(cids) {
        const response = await this.client.post('/v1/block/batch/has', { cids });
        return response.data.results;
    }

    // ========================================================================
    // Streaming API
    // ========================================================================

    /**
     * Upload large file with streaming
     * @param {string} filePath - Path to file to upload
     * @returns {Promise<{cid: string, size: number, chunks_received: number}>}
     */
    async streamingUpload(filePath) {
        const form = new FormData();
        form.append('file', fs.createReadStream(filePath));

        const response = await this.client.post('/v1/stream/upload', form, {
            headers: form.getHeaders(),
        });

        return response.data;
    }

    /**
     * Download content with streaming
     * @param {string} cid - Content Identifier
     * @param {number} chunkSize - Chunk size in bytes
     * @returns {Promise<Buffer>}
     */
    async streamingDownload(cid, chunkSize = 65536) {
        const response = await this.client.get(`/v1/stream/download/${cid}`, {
            params: { chunk_size: chunkSize },
            responseType: 'stream',
        });

        const chunks = [];
        for await (const chunk of response.data) {
            chunks.push(chunk);
        }

        return Buffer.concat(chunks);
    }

    // ========================================================================
    // Tensor API - Zero-Copy with Arrow Support
    // ========================================================================

    /**
     * Get tensor data (raw format)
     * @param {string} cid - Content Identifier
     * @param {string|null} sliceSpec - Optional slice specification (e.g., "0:10,5:15")
     * @returns {Promise<Buffer>}
     */
    async getTensor(cid, sliceSpec = null) {
        const params = {};
        if (sliceSpec) {
            params.slice = sliceSpec;
        }

        const response = await this.client.get(`/v1/tensor/${cid}`, {
            params,
            responseType: 'arraybuffer',
        });

        return Buffer.from(response.data);
    }

    /**
     * Get tensor metadata only
     * @param {string} cid - Content Identifier
     * @returns {Promise<Object>}
     */
    async getTensorInfo(cid) {
        const response = await this.client.get(`/v1/tensor/${cid}/info`);
        return response.data;
    }

    /**
     * Get tensor as Apache Arrow table
     * @param {string} cid - Content Identifier
     * @param {string|null} sliceSpec - Optional slice specification
     * @returns {Promise<{table: any, metadata: Object}>}
     */
    async getTensorArrow(cid, sliceSpec = null) {
        const params = {};
        if (sliceSpec) {
            params.slice = sliceSpec;
        }

        const response = await this.client.get(`/v1/tensor/${cid}/arrow`, {
            params,
            responseType: 'arraybuffer',
        });

        // Parse Arrow IPC stream
        const table = tableFromIPC(response.data);

        // Extract metadata from headers
        const metadata = {
            tensor_shape: response.headers['x-tensor-shape'] || '[]',
            tensor_dtype: response.headers['x-tensor-dtype'] || 'unknown',
            tensor_elements: response.headers['x-tensor-elements'] || '0',
        };

        return { table, metadata };
    }

    // ========================================================================
    // Node Information
    // ========================================================================

    /**
     * Get node identity information
     * @returns {Promise<Object>}
     */
    async getId() {
        const response = await this.client.post('/api/v0/id');
        return response.data;
    }

    /**
     * Get version information
     * @returns {Promise<Object>}
     */
    async getVersion() {
        const response = await this.client.post('/api/v0/version');
        return response.data;
    }

    /**
     * Get list of connected peers
     * @returns {Promise<Array>}
     */
    async getPeers() {
        const response = await this.client.post('/api/v0/swarm/peers');
        return response.data.Peers || [];
    }

    /**
     * Get bandwidth statistics
     * @returns {Promise<Object>}
     */
    async getBandwidthStats() {
        const response = await this.client.post('/api/v0/stats/bw');
        return response.data;
    }

    // ========================================================================
    // WebSocket - Real-time Events
    // ========================================================================

    /**
     * Connect to WebSocket for real-time events
     * @param {Function} onMessage - Callback for messages
     * @param {Function} onError - Callback for errors
     * @returns {WebSocket}
     */
    connectWebSocket(onMessage, onError) {
        const wsUrl = this.baseUrl.replace(/^http/, 'ws') + '/ws';
        const ws = new WebSocket(wsUrl);

        ws.on('open', () => {
            console.log('WebSocket connected');
        });

        ws.on('message', (data) => {
            const message = JSON.parse(data.toString());
            onMessage(message);
        });

        ws.on('error', (error) => {
            if (onError) {
                onError(error);
            }
        });

        return ws;
    }

    /**
     * Subscribe to a topic via WebSocket
     * @param {WebSocket} ws - WebSocket connection
     * @param {string} topic - Topic to subscribe to (e.g., "blocks", "peers", "dht")
     */
    subscribe(ws, topic) {
        const message = {
            type: 'Subscribe',
            topic: topic,
        };
        ws.send(JSON.stringify(message));
    }
}


// ============================================================================
// Example Usage
// ============================================================================

async function main() {
    console.log('=== IPFRS JavaScript Client Example ===\n');

    // Initialize client
    const client = new IPFRSClient('http://localhost:8080');

    // Example 1: Upload and download a file
    console.log('1. File Upload and Download');
    try {
        // Create a test file
        const testFile = '/tmp/test_ipfrs_js.txt';
        fs.writeFileSync(testFile, 'Hello, IPFRS! This is a test file from JavaScript.');

        // Upload
        const result = await client.addFile(testFile);
        const cid = result.Hash;
        console.log(`   Uploaded file: ${cid} (${result.Size} bytes)`);

        // Download
        const content = await client.cat(cid);
        console.log(`   Downloaded: ${content.toString()}`);
    } catch (error) {
        console.error(`   Error: ${error.message}`);
    }

    // Example 2: Batch operations
    console.log('\n2. Batch Block Operations');
    try {
        // Using CID from previous example (you'd need to have actual CIDs)
        // const cidsToCheck = [cid];
        // const results = await client.batchHasBlocks(cidsToCheck);
        // results.forEach(result => {
        //     console.log(`   ${result.cid}: exists=${result.exists}`);
        // });
        console.log('   (Skipping - would need valid CIDs)');
    } catch (error) {
        console.error(`   Error: ${error.message}`);
    }

    // Example 3: Tensor with Arrow
    console.log('\n3. Tensor Operations with Apache Arrow');
    try {
        // This would work if you have a tensor CID
        // const tensorCid = 'QmYourTensorCID';
        // const { table, metadata } = await client.getTensorArrow(tensorCid);
        // console.log(`   Tensor shape: ${metadata.tensor_shape}`);
        // console.log(`   Tensor dtype: ${metadata.tensor_dtype}`);
        // console.log(`   Table rows: ${table.numRows}`);
        console.log('   (Skipping - no tensor CID available)');
    } catch (error) {
        console.error(`   Error: ${error.message}`);
    }

    // Example 4: Node information
    console.log('\n4. Node Information');
    try {
        const version = await client.getVersion();
        console.log(`   Version: ${version.Version || 'unknown'}`);
        console.log(`   System: ${version.System || 'unknown'}`);
    } catch (error) {
        console.error(`   Error: ${error.message}`);
    }

    // Example 5: WebSocket events (optional)
    console.log('\n5. WebSocket Real-Time Events');
    try {
        // Uncomment to test WebSocket
        /*
        const ws = client.connectWebSocket(
            (message) => {
                console.log('   WebSocket message:', message);
            },
            (error) => {
                console.error('   WebSocket error:', error);
            }
        );

        // Subscribe to block events
        client.subscribe(ws, 'blocks');

        // Keep alive for a few seconds
        setTimeout(() => {
            ws.close();
        }, 5000);
        */
        console.log('   (Uncomment code to test WebSocket)');
    } catch (error) {
        console.error(`   Error: ${error.message}`);
    }

    console.log('\n=== Example Complete ===');
}

// Run if executed directly
if (require.main === module) {
    main().catch(console.error);
}

module.exports = IPFRSClient;