rwsdk 0.1.8-test.20250702232536 → 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.
@@ -1,6 +1,5 @@
1
1
  import { initClient } from "../../client";
2
2
  import { createFromReadableStream } from "react-server-dom-webpack/client.browser";
3
- import { IS_DEV } from "../../constants";
4
3
  import { MESSAGE_TYPE } from "./shared";
5
4
  const DEFAULT_KEY = "default";
6
5
  export const initRealtimeClient = ({ key = DEFAULT_KEY, } = {}) => {
@@ -12,12 +11,13 @@ export const realtimeTransport = ({ key = DEFAULT_KEY }) => (transportContext) =
12
11
  let isConnected = false;
13
12
  const clientId = crypto.randomUUID();
14
13
  const clientUrl = new URL(window.location.href);
14
+ const isHttps = clientUrl.protocol === "https:";
15
15
  clientUrl.protocol = "";
16
16
  clientUrl.host = "";
17
17
  const setupWebSocket = () => {
18
18
  if (ws)
19
19
  return;
20
- const protocol = IS_DEV ? "ws" : "wss";
20
+ const protocol = isHttps ? "wss" : "ws";
21
21
  ws = new WebSocket(`${protocol}://${window.location.host}/__realtime?` +
22
22
  `key=${encodeURIComponent(key)}&` +
23
23
  `url=${encodeURIComponent(clientUrl.toString())}&` +
@@ -4,15 +4,24 @@ import { $ } from "execa";
4
4
  import fs from "node:fs/promises";
5
5
  import { existsSync } from "node:fs";
6
6
  import chokidar from "chokidar";
7
+ import { lock } from "proper-lockfile";
7
8
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
9
  const getPackageManagerInfo = (targetDir) => {
10
+ const pnpmResult = {
11
+ name: "pnpm",
12
+ lockFile: "pnpm-lock.yaml",
13
+ command: "add",
14
+ };
9
15
  if (existsSync(path.join(targetDir, "yarn.lock"))) {
10
16
  return { name: "yarn", lockFile: "yarn.lock", command: "add" };
11
17
  }
12
18
  if (existsSync(path.join(targetDir, "pnpm-lock.yaml"))) {
13
- return { name: "pnpm", lockFile: "pnpm-lock.yaml", command: "add" };
19
+ return pnpmResult;
14
20
  }
15
- return { name: "npm", lockFile: "package-lock.json", command: "install" };
21
+ if (existsSync(path.join(targetDir, "package-lock.json"))) {
22
+ return { name: "npm", lockFile: "package-lock.json", command: "install" };
23
+ }
24
+ return pnpmResult;
16
25
  };
17
26
  const performFullSync = async (sdkDir, targetDir) => {
18
27
  console.log("📦 Packing SDK...");
@@ -34,11 +43,18 @@ const performFullSync = async (sdkDir, targetDir) => {
34
43
  .readFile(lockfilePath, "utf-8")
35
44
  .catch(() => null);
36
45
  try {
37
- let installCommand = `${pm.name} ${pm.command} ${tarballPath}`;
46
+ const cmd = pm.name;
47
+ const args = [pm.command];
38
48
  if (pm.name === "yarn") {
39
- installCommand = `yarn add file:${tarballPath}`;
49
+ args.push(`file:${tarballPath}`);
50
+ }
51
+ else {
52
+ args.push(tarballPath);
40
53
  }
41
- await $ `${installCommand}`;
54
+ await $(cmd, args, {
55
+ cwd: targetDir,
56
+ stdio: "inherit",
57
+ });
42
58
  }
43
59
  finally {
44
60
  if (originalPackageJson) {
@@ -96,50 +112,77 @@ export const debugSync = async (opts) => {
96
112
  console.error("❌ Please provide a target directory as an argument.");
97
113
  process.exit(1);
98
114
  }
99
- // Initial sync
115
+ // If not in watch mode, just do a one-time sync and exit.
116
+ if (!watch) {
117
+ await performSync(sdkDir, targetDir);
118
+ return;
119
+ }
120
+ // --- Watch Mode Logic ---
121
+ const lockfilePath = path.join(targetDir, "node_modules", ".rwsync.lock");
122
+ let release;
123
+ // Ensure the directory for the lockfile exists
124
+ await fs.mkdir(path.dirname(lockfilePath), { recursive: true });
125
+ // "Touch" the file to ensure it exists before locking
126
+ await fs.appendFile(lockfilePath, "").catch(() => { });
127
+ try {
128
+ release = await lock(lockfilePath, { retries: 0 });
129
+ }
130
+ catch (e) {
131
+ if (e.code === "ELOCKED") {
132
+ console.error(`❌ Another rwsync process is already watching ${targetDir}.`);
133
+ console.error(` If this is not correct, please remove the lockfile at ${lockfilePath}`);
134
+ process.exit(1);
135
+ }
136
+ throw e;
137
+ }
138
+ // Initial sync for watch mode. We do it *after* acquiring the lock.
100
139
  await performSync(sdkDir, targetDir);
101
- if (watch) {
102
- const sdkPackageJson = JSON.parse(await fs.readFile(path.join(sdkDir, "package.json"), "utf-8"));
103
- const filesToWatch = (sdkPackageJson.files || []).map((p) => path.join(sdkDir, p));
104
- filesToWatch.push(path.join(sdkDir, "package.json"));
105
- console.log("👀 Watching for changes...");
106
- let childProc = null;
107
- const runWatchedCommand = () => {
108
- if (typeof watch === "string") {
109
- console.log(`\n> ${watch}\n`);
110
- childProc = $({
111
- stdio: "inherit",
112
- shell: true,
113
- cwd: targetDir,
114
- reject: false,
115
- }) `${watch}`;
116
- }
117
- };
118
- const watcher = chokidar.watch(filesToWatch, {
119
- ignoreInitial: true,
120
- cwd: sdkDir,
121
- });
122
- watcher.on("all", async () => {
123
- console.log("\n Detected change, re-syncing...");
124
- if (childProc && !childProc.killed) {
125
- console.log("Stopping running process...");
126
- childProc.kill();
127
- await childProc.catch(() => {
128
- /* ignore kill errors */
129
- });
130
- }
131
- await performSync(sdkDir, targetDir);
132
- runWatchedCommand();
133
- });
134
- const cleanup = () => {
135
- if (childProc && !childProc.killed) {
136
- childProc.kill();
137
- }
138
- };
139
- process.on("SIGINT", cleanup);
140
- process.on("SIGTERM", cleanup);
140
+ const filesToWatch = [
141
+ path.join(sdkDir, "src"),
142
+ path.join(sdkDir, "types"),
143
+ path.join(sdkDir, "bin"),
144
+ path.join(sdkDir, "package.json"),
145
+ ];
146
+ console.log("👀 Watching for changes...");
147
+ let childProc = null;
148
+ const runWatchedCommand = () => {
149
+ if (typeof watch === "string") {
150
+ console.log(`\n> ${watch}\n`);
151
+ childProc = $({
152
+ stdio: "inherit",
153
+ shell: true,
154
+ cwd: targetDir,
155
+ reject: false,
156
+ }) `${watch}`;
157
+ }
158
+ };
159
+ const watcher = chokidar.watch(filesToWatch, {
160
+ ignoreInitial: true,
161
+ cwd: sdkDir,
162
+ });
163
+ watcher.on("all", async () => {
164
+ console.log("\nDetected change, re-syncing...");
165
+ if (childProc && !childProc.killed) {
166
+ console.log("Stopping running process...");
167
+ childProc.kill();
168
+ await childProc.catch(() => {
169
+ /* ignore kill errors */
170
+ });
171
+ }
172
+ await performSync(sdkDir, targetDir);
141
173
  runWatchedCommand();
142
- }
174
+ });
175
+ const cleanup = async () => {
176
+ console.log("\nCleaning up...");
177
+ if (childProc && !childProc.killed) {
178
+ childProc.kill();
179
+ }
180
+ await release();
181
+ process.exit();
182
+ };
183
+ process.on("SIGINT", cleanup);
184
+ process.on("SIGTERM", cleanup);
185
+ runWatchedCommand();
143
186
  };
144
187
  if (import.meta.url === new URL(process.argv[1], import.meta.url).href) {
145
188
  const args = process.argv.slice(2);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.1.8-test.20250702232536",
3
+ "version": "0.1.9",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {
@@ -135,6 +135,7 @@
135
135
  "magic-string": "^0.30.17",
136
136
  "miniflare": "^4.20250405.0",
137
137
  "picocolors": "^1.1.1",
138
+ "proper-lockfile": "^4.1.2",
138
139
  "puppeteer-core": "^22.8.1",
139
140
  "react": "19.2.0-canary-39cad7af-20250411",
140
141
  "react-dom": "19.2.0-canary-39cad7af-20250411",
@@ -155,6 +156,7 @@
155
156
  "@types/debug": "^4.1.12",
156
157
  "@types/lodash": "^4.17.16",
157
158
  "@types/node": "^22.14.0",
159
+ "@types/proper-lockfile": "^4.1.4",
158
160
  "semver": "^7.7.1",
159
161
  "tsx": "^4.19.4",
160
162
  "typescript": "^5.8.3",