@rpgjs/vite 5.0.0-alpha.10 → 5.0.0-alpha.3
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.
- package/CHANGELOG.md +7 -0
- package/dist/index.d.ts +0 -4
- package/dist/index.js +1 -411
- package/dist/index.js.map +1 -1
- package/package.json +9 -13
- package/src/index.ts +0 -4
- package/vite.config.ts +1 -2
- package/dist/entry-point-plugin.d.ts +0 -48
- package/dist/replace-config-import.d.ts +0 -4
- package/dist/rpgjs-plugin.d.ts +0 -23
- package/dist/server-plugin.d.ts +0 -155
- package/src/entry-point-plugin.ts +0 -99
- package/src/replace-config-import.ts +0 -24
- package/src/rpgjs-plugin.ts +0 -29
- package/src/server-plugin.ts +0 -495
- package/tests/entry-point-plugin.spec.ts +0 -172
package/CHANGELOG.md
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,3 @@ export { tiledMapFolderPlugin, type DataFolderPluginOptions } from './tiled-map-
|
|
|
2
2
|
export { rpgjsModuleViteConfig } from './module-config';
|
|
3
3
|
export { directivePlugin, type DirectivePluginOptions } from './directive-plugin';
|
|
4
4
|
export { removeImportsPlugin, type RemoveImportsPluginOptions } from './remove-imports-plugin';
|
|
5
|
-
export { replaceConfigImport } from './replace-config-import';
|
|
6
|
-
export { rpgjs } from './rpgjs-plugin';
|
|
7
|
-
export { serverPlugin } from './server-plugin';
|
|
8
|
-
export { entryPointPlugin, type EntryPointPluginOptions } from './entry-point-plugin';
|
package/dist/index.js
CHANGED
|
@@ -8497,415 +8497,5 @@ function removeImportsPlugin(options) {
|
|
|
8497
8497
|
};
|
|
8498
8498
|
}
|
|
8499
8499
|
|
|
8500
|
-
|
|
8501
|
-
return {
|
|
8502
|
-
name: "replace-config-import",
|
|
8503
|
-
transform(code, id) {
|
|
8504
|
-
if (!id.endsWith("src/index.ts")) {
|
|
8505
|
-
return;
|
|
8506
|
-
}
|
|
8507
|
-
const baseDir = dirname(id);
|
|
8508
|
-
const isProd = process.env.NODE_ENV === "production";
|
|
8509
|
-
const configFile = isProd ? "config.prod.ts" : "config.dev.ts";
|
|
8510
|
-
const configPath = join(baseDir, configFile);
|
|
8511
|
-
if (existsSync(configPath)) {
|
|
8512
|
-
const newImport = isProd ? "./config.prod" : "./config.dev";
|
|
8513
|
-
return code.replace(/['"]\.\/(config)['"]/g, `"${newImport}"`);
|
|
8514
|
-
}
|
|
8515
|
-
}
|
|
8516
|
-
};
|
|
8517
|
-
}
|
|
8518
|
-
|
|
8519
|
-
class PartyConnection {
|
|
8520
|
-
constructor(ws, id, uri) {
|
|
8521
|
-
this.ws = ws;
|
|
8522
|
-
this._state = {};
|
|
8523
|
-
this.id = id || this.generateId();
|
|
8524
|
-
this.uri = uri || "";
|
|
8525
|
-
}
|
|
8526
|
-
/**
|
|
8527
|
-
* Generates a unique identifier for the connection
|
|
8528
|
-
*
|
|
8529
|
-
* @returns {string} Unique identifier based on timestamp and random number
|
|
8530
|
-
*/
|
|
8531
|
-
generateId() {
|
|
8532
|
-
return `conn_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
8533
|
-
}
|
|
8534
|
-
/**
|
|
8535
|
-
* Sends data to the client via WebSocket
|
|
8536
|
-
*
|
|
8537
|
-
* @param {any} data - Data to send (automatically serialized to JSON if not string)
|
|
8538
|
-
*/
|
|
8539
|
-
send(data) {
|
|
8540
|
-
if (this.ws.readyState === 1) {
|
|
8541
|
-
const message = typeof data === "string" ? data : JSON.stringify(data);
|
|
8542
|
-
this.ws.send(message);
|
|
8543
|
-
}
|
|
8544
|
-
}
|
|
8545
|
-
/**
|
|
8546
|
-
* Closes the WebSocket connection
|
|
8547
|
-
*/
|
|
8548
|
-
close() {
|
|
8549
|
-
if (this.ws.readyState === 1) {
|
|
8550
|
-
this.ws.close();
|
|
8551
|
-
}
|
|
8552
|
-
}
|
|
8553
|
-
/**
|
|
8554
|
-
* Sets state data for this connection
|
|
8555
|
-
*
|
|
8556
|
-
* @param {any} value - State data to store (max 2KB as per PartyKit spec)
|
|
8557
|
-
*/
|
|
8558
|
-
setState(value) {
|
|
8559
|
-
this._state = value;
|
|
8560
|
-
}
|
|
8561
|
-
/**
|
|
8562
|
-
* Gets the current state of this connection
|
|
8563
|
-
*
|
|
8564
|
-
* @returns {any} Current connection state
|
|
8565
|
-
*/
|
|
8566
|
-
get state() {
|
|
8567
|
-
return this._state;
|
|
8568
|
-
}
|
|
8569
|
-
}
|
|
8570
|
-
class Room {
|
|
8571
|
-
constructor(id) {
|
|
8572
|
-
this.env = {};
|
|
8573
|
-
this.context = {};
|
|
8574
|
-
this.connections = /* @__PURE__ */ new Map();
|
|
8575
|
-
this.storageData = /* @__PURE__ */ new Map();
|
|
8576
|
-
this.id = id;
|
|
8577
|
-
this.internalID = `internal_${id}_${Date.now()}`;
|
|
8578
|
-
}
|
|
8579
|
-
/**
|
|
8580
|
-
* Broadcasts a message to all connected clients
|
|
8581
|
-
*
|
|
8582
|
-
* @param {any} message - Message to broadcast
|
|
8583
|
-
* @param {string[]} except - Array of connection IDs to exclude from broadcast
|
|
8584
|
-
*/
|
|
8585
|
-
broadcast(message, except = []) {
|
|
8586
|
-
const data = typeof message === "string" ? message : JSON.stringify(message);
|
|
8587
|
-
for (const [connectionId, connection] of this.connections) {
|
|
8588
|
-
if (!except.includes(connectionId)) {
|
|
8589
|
-
connection.send(data);
|
|
8590
|
-
}
|
|
8591
|
-
}
|
|
8592
|
-
}
|
|
8593
|
-
/**
|
|
8594
|
-
* Gets a connection by its ID
|
|
8595
|
-
*
|
|
8596
|
-
* @param {string} id - Connection ID
|
|
8597
|
-
* @returns {PartyConnection | undefined} The connection or undefined if not found
|
|
8598
|
-
*/
|
|
8599
|
-
getConnection(id) {
|
|
8600
|
-
return this.connections.get(id);
|
|
8601
|
-
}
|
|
8602
|
-
/**
|
|
8603
|
-
* Gets all currently connected clients
|
|
8604
|
-
*
|
|
8605
|
-
* @param {string} tag - Optional tag to filter connections (not implemented yet)
|
|
8606
|
-
* @returns {IterableIterator<PartyConnection>} Iterator of all connections
|
|
8607
|
-
*/
|
|
8608
|
-
getConnections(tag) {
|
|
8609
|
-
return this.connections.values();
|
|
8610
|
-
}
|
|
8611
|
-
/**
|
|
8612
|
-
* Adds a connection to this room
|
|
8613
|
-
*
|
|
8614
|
-
* @param {PartyConnection} connection - Connection to add
|
|
8615
|
-
*/
|
|
8616
|
-
addConnection(connection) {
|
|
8617
|
-
this.connections.set(connection.id, connection);
|
|
8618
|
-
}
|
|
8619
|
-
/**
|
|
8620
|
-
* Removes a connection from this room
|
|
8621
|
-
*
|
|
8622
|
-
* @param {string} connectionId - ID of connection to remove
|
|
8623
|
-
*/
|
|
8624
|
-
removeConnection(connectionId) {
|
|
8625
|
-
this.connections.delete(connectionId);
|
|
8626
|
-
}
|
|
8627
|
-
/**
|
|
8628
|
-
* Simple key-value storage for the room
|
|
8629
|
-
*/
|
|
8630
|
-
get storage() {
|
|
8631
|
-
return {
|
|
8632
|
-
put: async (key, value) => {
|
|
8633
|
-
this.storageData.set(key, value);
|
|
8634
|
-
},
|
|
8635
|
-
get: async (key) => {
|
|
8636
|
-
return this.storageData.get(key);
|
|
8637
|
-
},
|
|
8638
|
-
delete: async (key) => {
|
|
8639
|
-
this.storageData.delete(key);
|
|
8640
|
-
},
|
|
8641
|
-
list: async () => {
|
|
8642
|
-
return Array.from(this.storageData.keys());
|
|
8643
|
-
}
|
|
8644
|
-
};
|
|
8645
|
-
}
|
|
8646
|
-
}
|
|
8647
|
-
async function importWebSocketServer() {
|
|
8648
|
-
if (typeof process === "undefined" || !process.versions?.node) {
|
|
8649
|
-
console.warn("Not in Node.js environment, WebSocket server not available");
|
|
8650
|
-
return null;
|
|
8651
|
-
}
|
|
8652
|
-
try {
|
|
8653
|
-
const { createRequire } = await import('module');
|
|
8654
|
-
const require = createRequire(import.meta.url);
|
|
8655
|
-
const ws = require("ws");
|
|
8656
|
-
return ws.WebSocketServer || ws.default?.WebSocketServer || ws;
|
|
8657
|
-
} catch (error) {
|
|
8658
|
-
console.warn("Failed to load ws module:", error);
|
|
8659
|
-
return null;
|
|
8660
|
-
}
|
|
8661
|
-
}
|
|
8662
|
-
function serverPlugin(serverModule) {
|
|
8663
|
-
let wsServer = null;
|
|
8664
|
-
let rooms = /* @__PURE__ */ new Map();
|
|
8665
|
-
let servers = /* @__PURE__ */ new Map();
|
|
8666
|
-
return {
|
|
8667
|
-
name: "server-plugin",
|
|
8668
|
-
async configureServer(server) {
|
|
8669
|
-
try {
|
|
8670
|
-
const WebSocketServerClass = await importWebSocketServer();
|
|
8671
|
-
if (WebSocketServerClass) {
|
|
8672
|
-
wsServer = new WebSocketServerClass({
|
|
8673
|
-
noServer: true
|
|
8674
|
-
});
|
|
8675
|
-
console.log("WebSocket server initialized successfully");
|
|
8676
|
-
} else {
|
|
8677
|
-
console.log("WebSocket server not available in this environment");
|
|
8678
|
-
}
|
|
8679
|
-
} catch (error) {
|
|
8680
|
-
console.warn("WebSocket server not available:", error);
|
|
8681
|
-
wsServer = null;
|
|
8682
|
-
}
|
|
8683
|
-
console.log("RPG-JS server plugin initialized");
|
|
8684
|
-
server.middlewares.use("/parties", async (req, res, next) => {
|
|
8685
|
-
try {
|
|
8686
|
-
console.log(`RPG-JS HTTP request: ${req.method} ${req.url}`);
|
|
8687
|
-
if (req.url?.includes("/test")) {
|
|
8688
|
-
res.statusCode = 200;
|
|
8689
|
-
res.setHeader("Content-Type", "application/json");
|
|
8690
|
-
res.end(
|
|
8691
|
-
JSON.stringify({
|
|
8692
|
-
message: "RPG-JS server is running",
|
|
8693
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
8694
|
-
})
|
|
8695
|
-
);
|
|
8696
|
-
return;
|
|
8697
|
-
}
|
|
8698
|
-
next();
|
|
8699
|
-
} catch (error) {
|
|
8700
|
-
console.error("Error handling RPG-JS request:", error);
|
|
8701
|
-
res.statusCode = 500;
|
|
8702
|
-
res.end(JSON.stringify({ error: "Internal server error" }));
|
|
8703
|
-
}
|
|
8704
|
-
});
|
|
8705
|
-
if (wsServer) {
|
|
8706
|
-
server.httpServer?.on(
|
|
8707
|
-
"upgrade",
|
|
8708
|
-
(request, socket, head) => {
|
|
8709
|
-
const url = new URL(request.url, `http://${request.headers.host}`);
|
|
8710
|
-
if (url.pathname.startsWith("/parties/")) {
|
|
8711
|
-
console.log(`WebSocket upgrade request: ${url.pathname}`);
|
|
8712
|
-
wsServer.handleUpgrade(
|
|
8713
|
-
request,
|
|
8714
|
-
socket,
|
|
8715
|
-
head,
|
|
8716
|
-
async (ws) => {
|
|
8717
|
-
try {
|
|
8718
|
-
const pathParts = url.pathname.split("/");
|
|
8719
|
-
const roomName = pathParts[pathParts.length - 1];
|
|
8720
|
-
const queryParams = Object.fromEntries(
|
|
8721
|
-
url.searchParams.entries()
|
|
8722
|
-
);
|
|
8723
|
-
console.log(
|
|
8724
|
-
`Room: ${roomName}, Query params:`,
|
|
8725
|
-
queryParams
|
|
8726
|
-
);
|
|
8727
|
-
let room = rooms.get(roomName);
|
|
8728
|
-
if (!room) {
|
|
8729
|
-
room = new Room(roomName);
|
|
8730
|
-
rooms.set(roomName, room);
|
|
8731
|
-
console.log(`Created new room: ${roomName}`);
|
|
8732
|
-
}
|
|
8733
|
-
let rpgServer = servers.get(roomName);
|
|
8734
|
-
if (!rpgServer) {
|
|
8735
|
-
rpgServer = new serverModule(room);
|
|
8736
|
-
servers.set(roomName, rpgServer);
|
|
8737
|
-
console.log(`Created new server instance for room: ${roomName}`);
|
|
8738
|
-
if (typeof rpgServer.onStart === "function") {
|
|
8739
|
-
try {
|
|
8740
|
-
await rpgServer.onStart();
|
|
8741
|
-
console.log(`Server started for room: ${roomName}`);
|
|
8742
|
-
} catch (error) {
|
|
8743
|
-
console.error(`Error starting server for room ${roomName}:`, error);
|
|
8744
|
-
}
|
|
8745
|
-
}
|
|
8746
|
-
}
|
|
8747
|
-
const connection = new PartyConnection(
|
|
8748
|
-
ws,
|
|
8749
|
-
void 0,
|
|
8750
|
-
request.url
|
|
8751
|
-
);
|
|
8752
|
-
room.addConnection(connection);
|
|
8753
|
-
console.log(
|
|
8754
|
-
`WebSocket connection established: ${connection.id} in room: ${roomName}`
|
|
8755
|
-
);
|
|
8756
|
-
ws.on("message", async (data) => {
|
|
8757
|
-
try {
|
|
8758
|
-
const message = data.toString();
|
|
8759
|
-
if (typeof rpgServer.onMessage === "function") {
|
|
8760
|
-
await rpgServer.onMessage(message, connection);
|
|
8761
|
-
}
|
|
8762
|
-
} catch (error) {
|
|
8763
|
-
console.error(
|
|
8764
|
-
"Error processing WebSocket message:",
|
|
8765
|
-
error
|
|
8766
|
-
);
|
|
8767
|
-
}
|
|
8768
|
-
});
|
|
8769
|
-
ws.on("close", async () => {
|
|
8770
|
-
console.log(
|
|
8771
|
-
`WebSocket connection closed: ${connection.id} from room: ${roomName}`
|
|
8772
|
-
);
|
|
8773
|
-
room.removeConnection(connection.id);
|
|
8774
|
-
if (typeof rpgServer.onClose === "function") {
|
|
8775
|
-
await rpgServer.onClose(connection);
|
|
8776
|
-
}
|
|
8777
|
-
});
|
|
8778
|
-
ws.on("error", async (error) => {
|
|
8779
|
-
console.error("WebSocket error:", error);
|
|
8780
|
-
room.removeConnection(connection.id);
|
|
8781
|
-
if (typeof rpgServer.onClose === "function") {
|
|
8782
|
-
await rpgServer.onClose(connection);
|
|
8783
|
-
}
|
|
8784
|
-
});
|
|
8785
|
-
if (typeof rpgServer.onConnect === "function") {
|
|
8786
|
-
const headers = /* @__PURE__ */ new Map();
|
|
8787
|
-
if (request.headers) {
|
|
8788
|
-
Object.entries(request.headers).forEach(
|
|
8789
|
-
([key, value]) => {
|
|
8790
|
-
headers.set(
|
|
8791
|
-
key.toLowerCase(),
|
|
8792
|
-
Array.isArray(value) ? value[0] : value
|
|
8793
|
-
);
|
|
8794
|
-
}
|
|
8795
|
-
);
|
|
8796
|
-
}
|
|
8797
|
-
const connectionContext = {
|
|
8798
|
-
request: {
|
|
8799
|
-
headers: {
|
|
8800
|
-
has: (name) => headers.has(name.toLowerCase()),
|
|
8801
|
-
get: (name) => headers.get(name.toLowerCase()),
|
|
8802
|
-
entries: () => headers.entries(),
|
|
8803
|
-
keys: () => headers.keys(),
|
|
8804
|
-
values: () => headers.values()
|
|
8805
|
-
},
|
|
8806
|
-
url: request.url,
|
|
8807
|
-
method: request.method
|
|
8808
|
-
},
|
|
8809
|
-
url
|
|
8810
|
-
};
|
|
8811
|
-
await rpgServer.onConnect(
|
|
8812
|
-
connection,
|
|
8813
|
-
connectionContext
|
|
8814
|
-
);
|
|
8815
|
-
}
|
|
8816
|
-
connection.send({
|
|
8817
|
-
type: "connected",
|
|
8818
|
-
id: connection.id,
|
|
8819
|
-
message: "Connected to RPG-JS server"
|
|
8820
|
-
});
|
|
8821
|
-
} catch (error) {
|
|
8822
|
-
console.error(
|
|
8823
|
-
"Error establishing WebSocket connection:",
|
|
8824
|
-
error
|
|
8825
|
-
);
|
|
8826
|
-
ws.close();
|
|
8827
|
-
}
|
|
8828
|
-
}
|
|
8829
|
-
);
|
|
8830
|
-
}
|
|
8831
|
-
}
|
|
8832
|
-
);
|
|
8833
|
-
}
|
|
8834
|
-
console.log(
|
|
8835
|
-
"RPG-JS server plugin configured with HTTP and WebSocket forwarding"
|
|
8836
|
-
);
|
|
8837
|
-
},
|
|
8838
|
-
buildStart() {
|
|
8839
|
-
console.log("RPG-JS server starting...");
|
|
8840
|
-
},
|
|
8841
|
-
buildEnd() {
|
|
8842
|
-
if (wsServer) {
|
|
8843
|
-
wsServer.close();
|
|
8844
|
-
}
|
|
8845
|
-
console.log("RPG-JS server stopped");
|
|
8846
|
-
}
|
|
8847
|
-
};
|
|
8848
|
-
}
|
|
8849
|
-
|
|
8850
|
-
function entryPointPlugin(options = {}) {
|
|
8851
|
-
const {
|
|
8852
|
-
entryPoints = {
|
|
8853
|
-
rpg: "./src/standalone.ts",
|
|
8854
|
-
mmorpg: "./src/client.ts"
|
|
8855
|
-
},
|
|
8856
|
-
rpgType = process.env.RPG_TYPE || "rpg"
|
|
8857
|
-
} = options;
|
|
8858
|
-
return {
|
|
8859
|
-
name: "rpgjs:entry-point",
|
|
8860
|
-
transformIndexHtml: {
|
|
8861
|
-
order: "pre",
|
|
8862
|
-
handler(html) {
|
|
8863
|
-
const currentEntryPoint = entryPoints[rpgType];
|
|
8864
|
-
if (!currentEntryPoint) {
|
|
8865
|
-
console.warn(`[rpgjs:entry-point] Unknown RPG_TYPE: ${rpgType}. Using default 'rpg' type.`);
|
|
8866
|
-
const fallbackEntryPoint = entryPoints.rpg || "./src/standalone.ts";
|
|
8867
|
-
return html.replace(
|
|
8868
|
-
/<script\s+type="module"\s+src="[^"]*"[^>]*><\/script>/gi,
|
|
8869
|
-
`<script type="module" src="${fallbackEntryPoint}"><\/script>`
|
|
8870
|
-
);
|
|
8871
|
-
}
|
|
8872
|
-
const transformedHtml = html.replace(
|
|
8873
|
-
/<script\s+type="module"\s+src="[^"]*"[^>]*><\/script>/gi,
|
|
8874
|
-
`<script type="module" src="${currentEntryPoint}"><\/script>`
|
|
8875
|
-
);
|
|
8876
|
-
if (transformedHtml === html && !html.includes('<script type="module"')) {
|
|
8877
|
-
return html.replace(
|
|
8878
|
-
/<\/head>/i,
|
|
8879
|
-
` <script type="module" src="${currentEntryPoint}"><\/script>
|
|
8880
|
-
</head>`
|
|
8881
|
-
);
|
|
8882
|
-
}
|
|
8883
|
-
return transformedHtml;
|
|
8884
|
-
}
|
|
8885
|
-
},
|
|
8886
|
-
configResolved(config) {
|
|
8887
|
-
console.log(`[rpgjs:entry-point] Using RPG_TYPE: ${rpgType}`);
|
|
8888
|
-
console.log(`[rpgjs:entry-point] Entry point: ${entryPoints[rpgType]}`);
|
|
8889
|
-
}
|
|
8890
|
-
};
|
|
8891
|
-
}
|
|
8892
|
-
|
|
8893
|
-
function rpgjs({
|
|
8894
|
-
server,
|
|
8895
|
-
entryPoints
|
|
8896
|
-
}) {
|
|
8897
|
-
return [
|
|
8898
|
-
canvasengine(),
|
|
8899
|
-
replaceConfigImport(),
|
|
8900
|
-
serverPlugin(server),
|
|
8901
|
-
entryPointPlugin({
|
|
8902
|
-
entryPoints: {
|
|
8903
|
-
rpg: entryPoints?.rpg ?? "./src/standalone.ts",
|
|
8904
|
-
mmorpg: entryPoints?.mmorpg ?? "./src/client.ts"
|
|
8905
|
-
}
|
|
8906
|
-
})
|
|
8907
|
-
];
|
|
8908
|
-
}
|
|
8909
|
-
|
|
8910
|
-
export { directivePlugin, entryPointPlugin, removeImportsPlugin, replaceConfigImport, rpgjs, rpgjsModuleViteConfig, serverPlugin, tiledMapFolderPlugin };
|
|
8500
|
+
export { directivePlugin, removeImportsPlugin, rpgjsModuleViteConfig, tiledMapFolderPlugin };
|
|
8911
8501
|
//# sourceMappingURL=index.js.map
|