deepjson-connector
v1.0.8
Published
Official connector for DeepJSON Server - JSON storage with real-time sync
Maintainers
Readme
DeepJSON Server & Client
A high-performance JSON storage server with real-time synchronization capabilities and secure scripting support.
Features
- JWT & HMAC Authentication
- CRUD Operations for JSON Data
- Binary File Handling (Images, Documents)
- Secure Script Execution (VM2 Sandbox)
- Real-time Synchronization Channels
- Role-based Access Control
Installation
Node.js Client
npm install deepjson-connectorBrowser Client
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js"></script>
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
<script src="path/to/deepjson-client.js"></script>Quick Start
Basic Connection
Node.js
const { Connector } = require('deepjson-client');
// Basic configuration
const dj = new Connector({
baseURL: 'http://localhost:3000', // Server URL
timeout: 15000, // Request timeout (ms)
storage: 'local' // 'local' (persistent) or 'session' (tab-only)
});
// Advanced configuration
const djSecure = new Connector({
baseURL: 'https://api.yourdomain.com',
token: 'eyJhbGci...', // Preload existing JWT
headers: {
'X-Client-Version': '1.4.2'
}
});Browser
// Initialize with default settings
const dj = new DeepJSONConnector({
baseURL: 'http://localhost:3000'
});
// Initialize with existing token (e.g., from localStorage)
const djAuthenticated = new DeepJSONConnector({
baseURL: 'http://localhost:3000',
token: localStorage.getItem('dj_token')
});Authentification
// Basic username/password login
try {
const response = await dj.login('[email protected]', 'securepassword123');
console.log('Authenticated as:', response.user);
console.log('JWT Token:', dj.getToken());
} catch (err) {
console.error('Login failed:', err.message);
}
// Manual token handling (for existing sessions)
dj.token = 'eyJhbGciOiJIUzI1NiIsInR5c...';Example Error Handling:
try {
await dj.get('secured/data');
} catch (err) {
if (err.status === 401) {
console.log('Session expired - redirecting to login');
window.location.href = '/login';
} else {
console.error('API Error:', err.details);
}
}Basic Operations
Create/Update Data:
// Create new entry
await dj.post('users/123', {
name: 'Alice',
roles: ['editor']
});
// Overwrite existing entry
const overwriteConfig = {
overwriteKey: true
};
await dj.post('users/123', {
name: 'Alicia' // Full replacement
}, overwriteConfig);
// Partial update
await dj.put('users/123', {
roles: ['admin'] // Merge with existing data
});Retrieve Data:
// Simple GET
const userData = await dj.get('users/123');
// Get with query parameters
const filteredData = await dj.get('logs', {
params: {
dateFrom: '2024-01-01',
limit: 50
}
});Delete Data:
// Single entry
await dj.delete('users/123');
// Recursive delete (server must implement this)
await dj.delete('projects/old-project/*');Move Data:
// Move key location
await dj.move('users/temp/guest1', 'users/registered/guest1');
// Verify move
try {
const oldData = await dj.get('users/temp/guest1');
} catch (err) {
console.log('Old location cleared'); // Expected 404
}
const newData = await dj.get('users/registered/guest1');2. Script Handling
// Server-side script execution
const script = `javascript:
response = {
modified: data.value.map(item => item * 2)
};
javascript!`;
const result = await dj.post('data.process', script);3. Binary Files
// Upload file (Browser)
const fileInput = document.querySelector('input[type="file"]');
await dj.uploadFile('documents/report.pdf', fileInput.files[0]);
// Download image with processing
const imgBlob = await dj.get('photos/sunset.jpg', {
binary: true,
width: 800,
quality: 75
});4. Real-time Sync
const syncConnector = new Connector.Sync({
baseURL: 'http://localhost:3000'
});
// Join collaboration session
await syncConnector.joinSession('design-session-12');
// Send real-time updates
syncConnector.send('canvas-update', {
x: 120,
y: 45,
color: '#ff0000'
});
// Receive updates
syncConnector.on('canvas-update', (data) => {
renderCanvas(data);
});Advanced Features
Security Configuration
// HMAC Device Auth
const deviceConnector = new Connector({
baseURL: 'http://localhost:3000',
hmacSecret: 'device-secret-123'
});
// Secure script execution
const safeScript = await dj.post('scripts/clean-data', {
script: `// Safe operations only`,
timeout: 5000,
memoryLimit: 50
});API Reference
| Method | Description |
|-----------------|-------------------------------------|
| .get(key) | Retrieve data/File |
| .post(key, value) | Create new entry |
| .put(key, value) | Update existing entry |
| .move(from, to) | Move data between keys |
| .sync() | Real-time operations |
| .listKeys(regEx) | list keys |
License
MIT License - See LICENSE for details
