-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxhr.js
69 lines (62 loc) · 2.29 KB
/
xhr.js
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
import fs from 'fs';
import path from 'path';
import XHRShim from 'xhr-shim'; // Install xhr-shim: npm install xhr-shim
import mime from 'mime-types';
export default function createXMLHttpRequest(gameDir) {
const resourcePath = fs.existsSync(path.join(gameDir, 'public'))
? path.join(gameDir, 'public')
: gameDir;
return class LocalXMLHttpRequest extends XHRShim {
constructor(...args) {
super(...args);
this.isFSFilePath = false;
this.localFilePath = '';
// console.log('LocalXMLHttpRequest constructor', args);
}
open(method, url, async = true, user = null, password = null) {
const lcUrl = String(url).toLowerCase();
// Determine if it's a local file
if (!lcUrl.startsWith('http') && !lcUrl.startsWith('//')) {
this.isFSFilePath = true;
this.localFilePath = path.join(resourcePath, url);
} else {
this.isFSFilePath = false;
}
// console.log('XHR open', method, url, async, this.isFSFilePath);
super.open(method, url, async, user, password); // Call the parent class's open method
}
get responseText() {
return this._responseText;
}
send(data = null) {
// console.log('XHR send', data, this.isFSFilePath);
if (this.isFSFilePath) {
// Handle local file requests
fs.readFile(this.localFilePath, (err, fileContent) => {
if (err) {
this.status = 404;
this.statusText = 'Not Found';
this.readyState = 4;
this.onreadystatechange && this.onreadystatechange();
return;
}
// Set up response headers and content
this.status = 200;
this.statusText = 'OK';
this._responseText = fileContent;
this.responseType = 'text';
this.response = this.responseText;
this.readyState = 4;
this.getAllResponseHeaders = () =>
`Content-Type: ${mime.lookup(this.localFilePath) || 'application/octet-stream'}`;
this.onreadystatechange && this.onreadystatechange(this, {});
this.onload && this.onload(this, {});
this.onLoad && this.onLoad(this, {});
});
} else {
// Fall back to the parent class's send method for web requests
super.send(data);
}
}
};
}