directory-sync
Advanced tools
Comparing version 2.0.4 to 2.0.5
{ | ||
"name": "directory-sync", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"description": "Easily synchronize directories between two machines over the internet.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,4 +7,6 @@ import Stream from 'stream'; | ||
#map; | ||
constructor(map) { | ||
#supress_mutations = true; | ||
constructor(map, supress_mutations = true) { | ||
this.#map = map; | ||
this.#supress_mutations = supress_mutations; | ||
} | ||
@@ -33,6 +35,6 @@ | ||
if (is_directory) { | ||
this.#map.supress(uri, 'directory_create', 1); | ||
if (this.#supress_mutations) this.#map.supress(uri, 'directory_create', 1); | ||
return FileSystem.mkdir(path); | ||
} else { | ||
this.#map.supress(uri, 'file_create', 1); | ||
if (this.#supress_mutations) this.#map.supress(uri, 'file_create', 1); | ||
return FileSystem.writeFile(path, ''); | ||
@@ -70,5 +72,5 @@ } | ||
if (this.#map.get(uri)) { | ||
this.#map.supress(uri, 'file_change', 1); | ||
if (this.#supress_mutations) this.#map.supress(uri, 'file_change', 1); | ||
} else { | ||
this.#map.supress(uri, 'file_create', 1); | ||
if (this.#supress_mutations) this.#map.supress(uri, 'file_create', 1); | ||
} | ||
@@ -96,3 +98,3 @@ | ||
const path = this._absolute_path(uri); | ||
this.#map.supress(uri, is_directory ? 'directory_delete' : 'file_delete', 1); | ||
if (this.#supress_mutations) this.#map.supress(uri, is_directory ? 'directory_delete' : 'file_delete', 1); | ||
return new Promise((resolve, reject) => | ||
@@ -99,0 +101,0 @@ FileSystem.rm(path, { |
@@ -5,3 +5,2 @@ import fetch from 'node-fetch'; | ||
import Websocket from 'ws'; | ||
import JustQueue from 'just-queue'; | ||
import FileSystem from 'fs/promises'; | ||
@@ -41,3 +40,2 @@ import EventEmitter from 'events'; | ||
#manager; | ||
#network_queue; | ||
#destroyed = false; | ||
@@ -54,9 +52,2 @@ #options = { | ||
}, | ||
network_queue: { | ||
concurrent: 1, | ||
throttle: { | ||
rate: 10, | ||
interval: 1000, | ||
}, | ||
}, | ||
}; | ||
@@ -81,13 +72,2 @@ | ||
// Initialize the network queue with provided options | ||
const { concurrent, throttle } = this.#options.network_queue; | ||
const { rate, interval } = throttle; | ||
this.#network_queue = new JustQueue({ | ||
max_concurrent: concurrent, | ||
throttle: { | ||
rate, | ||
interval, | ||
}, | ||
}); | ||
// Initialize local actors | ||
@@ -140,3 +120,3 @@ this._initialize_actors(); | ||
this.#map = new DirectoryMap(options); | ||
this.#manager = new DirectoryManager(this.#map); | ||
this.#manager = new DirectoryManager(this.#map, true); | ||
@@ -386,2 +366,3 @@ // Wait for the map to be ready before performing synchronization | ||
const local_schema = this.#map.schema; | ||
this._log('HARD_SYNC', `START`); | ||
if (schema === undefined) { | ||
@@ -465,3 +446,3 @@ // Retrieve remote host map options and schema | ||
// Emit a completion log | ||
this._log('COMPLETE', `HARD_SYNC - ${Date.now() - start_time}ms`); | ||
this._log('HARD_SYNC', `COMPLETE - ${Date.now() - start_time}ms`); | ||
} | ||
@@ -488,16 +469,14 @@ | ||
// Queue the HTTP request in the Network Queue to prevent extensive backpressure | ||
response = await this.#network_queue.queue(() => | ||
fetch( | ||
`http${ssl ? 's' : ''}://${hostname}:${port}?host=${encodeURIComponent(this.#host)}&actor=${actor}${ | ||
uri ? `&uri=${encodeURIComponent(uri)}` : '' | ||
}`, | ||
{ | ||
method, | ||
headers: { | ||
...headers, | ||
'x-auth-key': auth, | ||
}, | ||
body, | ||
} | ||
) | ||
response = await fetch( | ||
`http${ssl ? 's' : ''}://${hostname}:${port}?host=${encodeURIComponent(this.#host)}&actor=${actor}${ | ||
uri ? `&uri=${encodeURIComponent(uri)}` : '' | ||
}`, | ||
{ | ||
method, | ||
headers: { | ||
...headers, | ||
'x-auth-key': auth, | ||
}, | ||
body, | ||
} | ||
); | ||
@@ -548,4 +527,5 @@ | ||
const { created_at, modified_at } = stats; | ||
this._log('CREATE', `${uri} - REMOTE_DIRECTORY - START`); | ||
await this._http_request('PUT', uri, {}, JSON.stringify([created_at, modified_at])); | ||
this._log('CREATE', `${uri} - DIRECTORY - ${Date.now() - start_time}ms`); | ||
this._log('CREATE', `${uri} - REMOTE_DIRECTORY - ${Date.now() - start_time}ms`); | ||
} | ||
@@ -562,3 +542,4 @@ | ||
const start_time = Date.now(); | ||
const { status, body } = await this._http_request('GET', uri); | ||
this._log('DOWNLOAD', `${uri} - FILE - START`); | ||
const { status, body, headers } = await this._http_request('GET', uri); | ||
@@ -568,5 +549,5 @@ // Ensure the response status code is valid | ||
// Write the file stream to local directory | ||
await this.#manager.write(uri, body); | ||
this._log('DOWNLOAD', `${uri} - FILE - ${Date.now() - start_time}ms`); | ||
// Stream the file to the local file system if we receive some content else empty the file | ||
await this.#manager.write(uri, +headers.get('content-length') === 0 ? '' : body); | ||
this._log('DOWNLOAD', `${uri} - FILE - COMPLETE - ${Date.now() - start_time}ms`); | ||
} | ||
@@ -582,2 +563,3 @@ | ||
const start_time = Date.now(); | ||
this._log('UPLOAD', `${uri} - FILE - START`); | ||
const stream = this.#manager.read(uri, true); | ||
@@ -595,3 +577,3 @@ | ||
); | ||
this._log('UPLOAD', `${uri} - FILE - ${Date.now() - start_time}ms`); | ||
this._log('UPLOAD', `${uri} - FILE - END - ${Date.now() - start_time}ms`); | ||
} | ||
@@ -598,0 +580,0 @@ |
@@ -180,3 +180,3 @@ import Path from 'path'; | ||
descriptor = 'UPLOAD'; | ||
operation = manager.read(uri, true); | ||
operation = record.stats.size > 0 ? manager.read(uri, true) : ''; | ||
break; | ||
@@ -193,3 +193,4 @@ case 'PUT': | ||
descriptor = 'DOWNLOAD'; | ||
operation = manager.write(uri, request.stream); | ||
const content_length = +request.headers['content-length'] || 0; | ||
operation = manager.write(uri, content_length == 0 ? '' : request.stream); | ||
break; | ||
@@ -203,3 +204,3 @@ case 'DELETE': | ||
// Determine if we were able to successfully map the request to an operation | ||
if (operation) { | ||
if (operation !== undefined) { | ||
// Safely retrieve the output from the operation by awaiting if it is a promise | ||
@@ -223,13 +224,18 @@ let output; | ||
// Publish a mutation event if this operation causes a mutation | ||
if (descriptor !== 'UPLOAD') { | ||
/* if (descriptor !== 'UPLOAD') { | ||
const identifier = ascii_to_hex(host); | ||
const mutation = descriptor === 'DOWNLOAD' ? 'MODIFIED' : descriptor; | ||
this._publish_mutation(identifier, actor, mutation, uri, is_directory); | ||
} | ||
setTimeout(() => this._publish_mutation(identifier, actor, mutation, uri, is_directory), 0); | ||
} */ | ||
// If the output of the operation is a readable stream, pipe it as the response | ||
if (output instanceof Stream.Readable) { | ||
return response.stream(output, record.stats.size); | ||
// GET requests are only used to consume data thus we must only send output | ||
if (request.method === 'GET') { | ||
// If the output of the operation is a readable stream, pipe it as the response | ||
if (output instanceof Stream.Readable) { | ||
return response.stream(output, record.stats.size); | ||
} else { | ||
return response.send(output); | ||
} | ||
} else { | ||
// Send a 'SUCCESS' code response with any output as that data | ||
// Send a 'SUCCESS' code response with any output as that data parameter | ||
return response.json({ | ||
@@ -306,3 +312,3 @@ code: 'SUCCESS', | ||
const map = new DirectoryMap(options); | ||
const manager = new DirectoryManager(map); | ||
const manager = new DirectoryManager(map, false); | ||
@@ -309,0 +315,0 @@ // Bind a error handler to pass through any errors |
71532
1579