svelte-realtime 0.1.8 → 0.1.9
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/README.md +2 -0
- package/package.json +1 -1
- package/vite.js +48 -2
package/README.md
CHANGED
|
@@ -75,6 +75,8 @@ export function upgrade({ cookies }) {
|
|
|
75
75
|
|
|
76
76
|
`message` is a ready-made hook that routes incoming WebSocket messages to your live functions. `upgrade` decides who can connect and attaches user data to the connection.
|
|
77
77
|
|
|
78
|
+
> **This file is required.** The Vite plugin will warn at startup if it finds live modules in `src/live/` but no `src/hooks.ws.js` (or `.ts`). Without it, WebSocket messages have nothing on the server side to route them, and all RPC calls will silently time out.
|
|
79
|
+
|
|
78
80
|
### Step 5: Write a server function
|
|
79
81
|
|
|
80
82
|
Create the `src/live/` directory. Every `.js` file in this directory becomes a module of callable server functions.
|
package/package.json
CHANGED
package/vite.js
CHANGED
|
@@ -76,8 +76,11 @@ export default function svelteRealtime(options) {
|
|
|
76
76
|
console.warn(
|
|
77
77
|
`[svelte-realtime] Plugin loaded but no live modules found in ${dir}/`
|
|
78
78
|
);
|
|
79
|
-
} else
|
|
80
|
-
|
|
79
|
+
} else {
|
|
80
|
+
if (typedImports) {
|
|
81
|
+
_writeTypeDeclarations(liveDir, dir);
|
|
82
|
+
}
|
|
83
|
+
_checkHooksFile(root, liveDir, dir);
|
|
81
84
|
}
|
|
82
85
|
},
|
|
83
86
|
|
|
@@ -940,6 +943,49 @@ function _findLiveFiles(dir) {
|
|
|
940
943
|
return results;
|
|
941
944
|
}
|
|
942
945
|
|
|
946
|
+
/**
|
|
947
|
+
* Check that src/hooks.ws.{js,ts} exists and exports the `message` handler.
|
|
948
|
+
* Warns at build/dev startup if the file is missing or misconfigured.
|
|
949
|
+
* @param {string} root
|
|
950
|
+
* @param {string} liveDir
|
|
951
|
+
* @param {string} dir
|
|
952
|
+
*/
|
|
953
|
+
function _checkHooksFile(root, liveDir, dir) {
|
|
954
|
+
const files = _findLiveFiles(liveDir);
|
|
955
|
+
if (files.length === 0) return;
|
|
956
|
+
|
|
957
|
+
const hooksPath = resolve(root, 'src/hooks.ws');
|
|
958
|
+
const hooksJs = hooksPath + '.js';
|
|
959
|
+
const hooksTs = hooksPath + '.ts';
|
|
960
|
+
const found = existsSync(hooksJs) ? hooksJs : existsSync(hooksTs) ? hooksTs : null;
|
|
961
|
+
|
|
962
|
+
if (!found) {
|
|
963
|
+
console.warn(
|
|
964
|
+
`[svelte-realtime] Found live modules in ${dir}/ but no src/hooks.ws.js -- ` +
|
|
965
|
+
`WebSocket RPC will not work without it.\n` +
|
|
966
|
+
` Create src/hooks.ws.js with at minimum:\n` +
|
|
967
|
+
` export { message } from 'svelte-realtime/server';\n` +
|
|
968
|
+
` export function upgrade() { return {}; }`
|
|
969
|
+
);
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
let source;
|
|
974
|
+
try { source = readFileSync(found, 'utf-8'); } catch { return; }
|
|
975
|
+
|
|
976
|
+
const hasMessage = /export\s*\{[^}]*\bmessage\b[^}]*\}\s*from\s+['"]svelte-realtime\/server['"]/.test(source)
|
|
977
|
+
|| /export\s+(?:const|function|async\s+function)\s+message\b/.test(source);
|
|
978
|
+
|
|
979
|
+
if (!hasMessage) {
|
|
980
|
+
const name = found.endsWith('.ts') ? 'src/hooks.ws.ts' : 'src/hooks.ws.js';
|
|
981
|
+
console.warn(
|
|
982
|
+
`[svelte-realtime] ${name} exists but does not export a \`message\` handler -- ` +
|
|
983
|
+
`WebSocket RPC calls from ${dir}/ will go unhandled.\n` +
|
|
984
|
+
` Add: export { message } from 'svelte-realtime/server';`
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
|
|
943
989
|
/**
|
|
944
990
|
* Generate and write type declarations for all $live/ modules.
|
|
945
991
|
* Creates `$types.d.ts` in the live directory with ambient module declarations.
|