-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPopAssetServer.js
More file actions
188 lines (162 loc) · 4.85 KB
/
PopAssetServer.js
File metadata and controls
188 lines (162 loc) · 4.85 KB
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
Pop.AssetServer = class
{
constructor(WebsocketPorts)
{
this.CurrentPort = null;
this.Ports = Array.isArray(WebsocketPorts) ? WebsocketPorts : [WebsocketPorts];
this.ListenDirectories = [];
this.FileMonitor = new Pop.FileMonitor();
this.ChangedQueue = new Pop.PromiseQueue(); // queue of filename (or filenames) everytime a file changes
this.WebsocketLoop().then(Pop.Debug).catch(Pop.Warning);
this.FileWatchLoop().then(Pop.Debug).catch(Pop.Warning);
}
FilterFilename(Filename)
{
// todo: Pop.GetOsFilenames (I think there's been a need for this before)
if ( Filename.endsWith('.DS_Store') )
return false;
if (Filename.startsWith('.git'))
return false;
if (Filename.includes('/.git'))
return false;
return true;
}
ListenToDirectory(Directory)
{
this.ListenDirectories.push(Directory);
this.FileMonitor.Add(Directory);
}
OnRequestListFiles()
{
// get all files in directories we're watching
function ListFiles(Directory)
{
// lets send a big group of the filenames
let Filenames = Pop.GetFilenames(Directory);
Filenames = Filenames.filter(this.FilterFilename);
Pop.Debug(`Filenames in ${Directory}; ${Filenames}`);
this.ChangedQueue.Push(Filenames);
}
this.ListenDirectories.forEach(ListFiles.bind(this));
}
async FileWatchLoop()
{
while (true)
{
const ChangedFile = await this.FileMonitor.WaitForChange();
if ( !this.FilterFilename(ChangedFile) )
{
Pop.Debug(`ChangedFile ${ChangedFile} ignored`);
continue;
}
Pop.Debug(`ChangedFile ${ChangedFile}`);
this.ChangedQueue.Push(ChangedFile);
}
}
GetNextPort()
{
if (this.CurrentPort === null)
this.CurrentPort = 0;
else
this.CurrentPort++;
const Port = this.Ports[this.CurrentPort % this.Ports.length];
return Port;
}
OnMessage(Packet,SendReply)
{
// check for meta or file requests and send stuff back
Pop.Debug(`Got Message ${Packet.Data}`);
try
{
const Message = JSON.parse(Packet.Data);
if ( Message.Command == 'RequestList' )
this.OnRequestListFiles();
else if ( Message.Command == 'RequestFile' )
this.OnRequestFile(Message,SendReply);
else
throw `Unhandled Command ${Message.Command}`;
}
catch(e)
{
const Reply = {};
Reply.Error = e;
SendReply( JSON.stringify(Reply) );
Pop.Debug(`Error with incoming message ${JSON.stringify(Packet)}; ${e}`);
}
}
OnRequestFile(Message,SendReply)
{
// client is requesting a file from one of our listening directories!
function GetListenFilename(ListenDirectory)
{
return ListenDirectory + '/' + Message.Filename;
}
const ListenFilenames = this.ListenDirectories.map(GetListenFilename);
// todo: find last-modified in case there's a clash
for ( let Path of ListenFilenames )
{
if ( !Pop.FileExists(Path) )
continue;
// get the file as binary, then insert simple meta json at the start
// this way, binary packets are always independent files and independent
// meta so OOO packets are okay (client can handle encoding)
const FileContents = Pop.LoadFileAsArrayBuffer(Path);
const Meta = {};
Meta.Filename = Message.Filename;
const MetaJsonBin = Pop.StringToBytes(JSON.stringify(Meta));
// concat
const Contents = new Uint8Array( MetaJsonBin.length + FileContents.length );
Contents.set( MetaJsonBin, 0 );
Contents.set( FileContents, MetaJsonBin.length );
//const ContentsStr = Pop.BytesToString(Contents);
//Pop.Debug(`New contents ${ContentsStr}`);
SendReply(Contents);
return;
}
// todo: specifically throw a "file not found Message.Filename" response so caller can forget about the file?
throw `Requested file (${Message.Filename}) failed to load from options; ${ListenFilenames}`;
}
async WebsocketLoop()
{
while (true)
{
try
{
const Port = this.GetNextPort();
let Socket = new Pop.Websocket.Server(Port);
function SendToPeers(Packet)
{
const Peers = Socket.GetPeers();
function Send(Peer)
{
Socket.Send(Peer,Packet);
}
Peers.forEach(Send);
}
async function SendLoop()
{
while (Socket)// is open? some flag to say this is still active
{
const ChangedFile = await this.ChangedQueue.WaitForNext();
let ChangedMeta = {};
ChangedMeta.Command = 'FileChanged';
ChangedMeta.Filename = ChangedFile;
ChangedMeta = JSON.stringify(ChangedMeta);
SendToPeers(ChangedMeta);
}
}
SendLoop.bind(this)().then(Pop.Debug).catch(Pop.Warning);
while (Socket)
{
const Message = await Socket.WaitForMessage();
this.OnMessage(Message,SendToPeers);
}
}
catch (e)
{
Pop.Debug(`Pop.AssetServer error ${e}`);
await Pop.Yield(200);
}
}
}
}