@trap_stevo/filetide 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@ class FileMessager {
11
11
  constructor(options = {}) {
12
12
  const serverOptions = FileMessagerConfigManager.getServerOptions(options);
13
13
  this.fileNet = new IoTide(serverOptions.port, serverOptions, true, this.onConnect.bind(this), this.onDisconnect.bind(this));
14
+ this.messagerClients = new Map();
14
15
  this.onlineClients = new Map();
15
16
  return;
16
17
  }
@@ -29,6 +30,9 @@ class FileMessager {
29
30
  this.fileNet.on("transfer-progress", this.handleFileTransferProgress.bind(this));
30
31
  this.fileNet.on("transfer-complete", this.handleFileTransferComplete.bind(this));
31
32
  this.fileNet.on("client-to-client-transfer", this.handleClientToClientTransfer.bind(this));
33
+ this.fileNet.on("client-offline", this.handleClientOffline.bind(this));
34
+ this.fileNet.on("clients-online", this.handleClientsOnline.bind(this));
35
+ this.fileNet.on("client-online", this.handleClientOnline.bind(this));
32
36
  return;
33
37
  }
34
38
 
@@ -39,9 +43,14 @@ class FileMessager {
39
43
  * @param {String} fileName - The name of the file
40
44
  */
41
45
  sendFileToClient(clientId, fileData, fileName, filePath = process.cwd()) {
42
- const client = this.onlineClients.get(clientId);
46
+ if (!this.messagerClients.has(clientId)) {
47
+ console.log(`Client ~ ${clientId} not authorized.`);
48
+ return;
49
+ }
50
+ const connectionID = this.messagerClients.get(clientId);
51
+ const client = this.onlineClients.get(connectionID);
43
52
  if (!client) {
44
- console.log(`Client with ID ${clientId} not found.`);
53
+ console.log(`Client ~ ${clientId} not found.`);
45
54
  return;
46
55
  }
47
56
  client.emit("incoming-file", {
@@ -49,7 +58,12 @@ class FileMessager {
49
58
  fileData,
50
59
  path: filePath
51
60
  });
52
- console.log(`File ${fileName} sent to client ${clientId}!`);
61
+ console.log(`[FileTide ~ File Messager] ~ File ${fileName} sent to client ${clientId}!`);
62
+ client.emit("transfer-status", {
63
+ status: `[FileTide ~ File Messager] ~ File ${fileName} sent to client ${clientId}!`,
64
+ receivingClientID: clientId,
65
+ success: true
66
+ });
53
67
  return;
54
68
  }
55
69
 
@@ -61,9 +75,30 @@ class FileMessager {
61
75
  const {
62
76
  recipientId,
63
77
  fileName,
64
- fileData
78
+ fileData,
79
+ filePath
65
80
  } = transferData;
66
- this.sendFileToClient(recipientId, fileData, fileName);
81
+ this.sendFileToClient(recipientId, fileData, fileName, filePath);
82
+ return;
83
+ }
84
+ handleClientsOnline() {
85
+ this.fileNet.emit("clients-online", {
86
+ currentClients: this.messagerClients
87
+ });
88
+ return;
89
+ }
90
+ handleClientOnline(clientID, connectionID) {
91
+ if (!this.onlineClients.has(connectionID)) {
92
+ return;
93
+ }
94
+ this.messagerClients.set(clientID, connectionID);
95
+ return;
96
+ }
97
+ handleClientOffline(clientID) {
98
+ if (!this.messagerClients.has(clientID)) {
99
+ return;
100
+ }
101
+ this.messagerClients.delete(clientID);
67
102
  return;
68
103
  }
69
104
  handleFileTransferStart(fileData, emitToChannel) {
@@ -19,7 +19,7 @@ class FileMessagerClient {
19
19
  this.clientTide.createIO(clientID, clientOptions.url, {
20
20
  transports: ["websocket"]
21
21
  });
22
- console.log(`[FileTide ~ File Messager] ~ Created client with ID ${clientID} and socket ${clientID}`);
22
+ console.log(`[FileTide ~ File Messager] ~ Created client with ID ${clientID} and socket ${clientID}!`);
23
23
  return clientID;
24
24
  }
25
25
  joinRoom(userID, roomName) {
@@ -27,24 +27,24 @@ class FileMessagerClient {
27
27
  url: this.clientOptions.url
28
28
  });
29
29
  this.clientTide.joinChannel(socketName, roomName, userID, () => {
30
- console.log(`[FileTide ~ File Messager] ~ Client ${userID} joined room ${roomName} successfully.`);
30
+ console.log(`[FileTide ~ File Messager] ~ Client ~ ${userID} joined room ${roomName} successfully!`);
31
31
  this.clients.set(userID, {
32
32
  roomName
33
33
  });
34
34
  });
35
35
  }
36
- sendFile(recipientId, fileName, fileData, path = process.cwd()) {
37
- this.clientTide.emitEvent(recipientId, "client-to-client-transfer", {
36
+ sendFile(clientID, recipientId, fileName, fileData, filePath = process.cwd()) {
37
+ this.clientTide.emitEvent(clientID, "client-to-client-transfer", {
38
38
  recipientId,
39
39
  fileName,
40
40
  fileData,
41
- path
41
+ filePath
42
42
  });
43
- console.log(`[FileTide ~ File Messager] ~ File ${fileName} sent to client ${recipientId}.`);
43
+ return;
44
44
  }
45
45
  onFileChunkReceived(userID) {
46
46
  this.clientTide.onEvent(userID, "transfer-progress", data => {
47
- console.log(`[FileTide ~ File Messager] ~ Receiving file chunk for user ${userID}: `, data.fileChunk);
47
+ console.log(`[FileTide ~ File Messager] ~ Receiving file chunk for client ~ ${userID}: `, data.fileChunk);
48
48
  console.log("[FileTide ~ File Messager] ~ Progress: ", data.progress);
49
49
  FileUtilityManager.saveChunk(userID, data.fileChunk, data.path, data.name);
50
50
  });
@@ -9,14 +9,12 @@ const path = require("path");
9
9
  const fs = require("fs");
10
10
  var _FileTide_brand = /*#__PURE__*/new WeakSet();
11
11
  class FileTide {
12
- constructor(config = {}) {
12
+ constructor() {
13
13
  /**
14
14
  * Set up file transfer event listeners natively.
15
15
  * Automatically checks and creates directories as needed.
16
16
  */
17
17
  _classPrivateMethodInitSpec(this, _FileTide_brand);
18
- this.config = config;
19
- this.fileNet = null;
20
18
  this.clients = new Map();
21
19
  return;
22
20
  }
@@ -33,6 +31,7 @@ class FileTide {
33
31
  if (onLaunch) {
34
32
  onLaunch(this.fileNet);
35
33
  }
34
+ return;
36
35
  }
37
36
 
38
37
  /**
@@ -44,14 +43,14 @@ class FileTide {
44
43
  */
45
44
  launchMessager(clientOptions = {}, roomName, userID, onLaunch, onIncomingFile) {
46
45
  if (this.clients.has(userID)) {
47
- console.log(`[FileTide] ~ Client for user ${userID} already exists.`);
48
- return;
46
+ this.stopMessager(userID);
49
47
  }
50
48
  const newClient = new FileMessagerClient(clientOptions);
49
+ newClient.createClientInstance();
51
50
  newClient.joinRoom(userID, roomName);
52
51
  newClient.handleMultipleClientsTransfer();
53
52
  this.clients.set(userID, newClient);
54
- console.log(`[FileTide] ~ Messager launched successfully for user ${userID}!`);
53
+ console.log(`[FileTide] ~ Messager launched successfully for client ~ ${userID}!`);
55
54
  _assertClassBrand(_FileTide_brand, this, _setupFileEventListeners).call(this, userID, newClient, onIncomingFile);
56
55
  if (onLaunch) {
57
56
  onLaunch(newClient);
@@ -66,12 +65,17 @@ class FileTide {
66
65
  */
67
66
  sendFileToDevice(userID, fileData, filePath, fileName) {
68
67
  if (this.fileNet.onlineClients.has(userID)) {
69
- console.log(`[FileTide] ~ Sending file to user ${userID}...`);
70
- this.fileNet.sendFileToClient(userID, fileData, filePath, fileName);
71
- console.log(`[FileTide] ~ File sent to user ${userID}!`);
68
+ const currentClient = this.clients[userID];
69
+ if (!currentClient) {
70
+ console.log(`[FileTide] ~ Client ~ ${userID} not authorized.`);
71
+ return;
72
+ }
73
+ console.log(`[FileTide] ~ Sending file to client ~ ${userID}...`);
74
+ this.fileNet.sendFileToClient(currentClient.id, fileData, filePath, fileName);
75
+ console.log(`[FileTide] ~ File sent to client ~ ${userID}!`);
72
76
  return;
73
77
  }
74
- console.log(`[FileTide] ~ No active client found for user ${userID}.`);
78
+ console.log(`[FileTide] ~ No active client ~ ${userID} found.`);
75
79
  return;
76
80
  }
77
81
 
@@ -130,17 +134,17 @@ class FileTide {
130
134
  }
131
135
  function _setupFileEventListeners(userID, client, onIncomingFile) {
132
136
  client.clientTide.onEvent(userID, "incoming-file", data => {
133
- console.log(`[Client] Received file: ${data.fileName}`);
137
+ console.log(`[${userID}] Received file: ${data.fileName}`);
134
138
  const saveDir = data.path;
135
139
  if (!fs.existsSync(saveDir)) {
136
140
  fs.mkdirSync(saveDir, {
137
141
  recursive: true
138
142
  });
139
- console.log(`[Client] Created directory: ${saveDir}`);
143
+ console.log(`[${userID}] Created directory: ${saveDir}`);
140
144
  }
141
145
  const savePath = path.join(saveDir, data.fileName);
142
146
  fs.writeFileSync(savePath, Buffer.from(data.fileData));
143
- console.log(`[Client] File saved at: ${savePath}`);
147
+ console.log(`[${userID}] File saved at: ${savePath}`);
144
148
  if (onIncomingFile) {
145
149
  onIncomingFile(data);
146
150
  }
@@ -148,4 +152,8 @@ function _setupFileEventListeners(userID, client, onIncomingFile) {
148
152
  return;
149
153
  }
150
154
  ;
155
+ const fileT = new FileTide();
156
+ fileT.launchFileTide({
157
+ port: 8869
158
+ });
151
159
  module.exports = FileTide;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trap_stevo/filetide",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Revolutionizing real-time file transfer with seamless, instant communication across any device. Deliver files instantly, regardless of platform, and experience unparalleled speed and control in managing transfers. Elevate your file-sharing capabilities with a tool designed for precision, efficiency, and effortless connectivity.",
5
5
  "main": "dist/cjs/FileTide.js",
6
6
  "scripts": {
@@ -28,7 +28,8 @@
28
28
  "license": "ISC",
29
29
  "dependencies": {
30
30
  "@trap_stevo/iotide": "^0.0.36",
31
- "@trap_stevo/iotide-client": "^0.0.14"
31
+ "@trap_stevo/iotide-client": "^0.0.14",
32
+ "readline": "^1.3.0"
32
33
  },
33
34
  "devDependencies": {
34
35
  "@babel/cli": "^7.24.8",
package/test.txt ADDED
@@ -0,0 +1 @@
1
+ oiyutryuihoj