Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat: use PartySocket when connecting via room.parties #395

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/rare-llamas-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"partykit": patch
---

feat: use `PartySocket` when connecting via `room.parties`

This uses `PartySocket` when connecting why `room.parties`, which gives you reconnection/buffering logic without any extra effort. Non-breaking change, existing code should still just work.
5 changes: 5 additions & 0 deletions .changeset/selfish-yaks-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"partykit": patch
---

rewrite workspace dependencies with version numbers before publish
3 changes: 3 additions & 0 deletions .github/changeset-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ const { execSync } = require("node:child_process");
// See https://github.com/changesets/changesets/issues/421.
execSync("npx changeset version");
execSync("npm install");
execSync("node -r esbuild-register .github/rewrite-local-deps.ts", {
shell: true,
});
69 changes: 69 additions & 0 deletions .github/rewrite-local-deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { execaSync } from "execa";
import * as fs from "fs";
import * as path from "path";

process.chdir(process.cwd());

// get list of all workspaces

type WorkSpace = {
resolved: string;
version: string;
overriden: boolean;
dependencies?: Record<string, WorkSpace>;
};

const { stdout: workspacesJSON } = execaSync(
"npm ls --workspaces --json --depth 0",
{
shell: true,
}
);

const workspaces = JSON.parse(workspacesJSON).dependencies as Record<
string,
WorkSpace
>;

for (const [_name, workspace] of Object.entries(workspaces)) {
const pathToPackage = path.join(
"./node_modules",
workspace.resolved.replace("file:", "")
);
// skip anything that's not a package
if (!pathToPackage.startsWith("packages/")) {
continue;
}
const pathToPackageJson = path.join(pathToPackage, "package.json");
const packageDetails = JSON.parse(fs.readFileSync(pathToPackageJson, "utf8"));

let modified = false;
// for every dependency in the package.json
// if it's a workspace, replace the version with the workspace version
// if it's not a workspace, leave it alone
for (const packageKey of [
"dependencies",
"devDependencies",
"peerDependencies",
"optionalDependencies",
]) {
const knownDependencies = packageDetails[packageKey] || {};

for (const [depName, _depVersion] of Object.entries(knownDependencies)) {
if (depName in workspaces) {
modified = true;
packageDetails[packageKey][depName] = workspaces[
depName
].version.startsWith("0.0.0-")
? workspaces[depName].version
: `^${workspaces[depName].version}`;
}
}
}
if (modified) {
fs.writeFileSync(
pathToPackageJson,
JSON.stringify(packageDetails, null, 2) + "\n"
);
}
}
3 changes: 3 additions & 0 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
- name: Modify package.json version
run: node .github/version-script.js

- name: rewrite local deps
run: node -r esbuild-register .github/rewrite-local-deps.ts

- run: npm run build
env:
PARTYKIT_API_BASE: https://beta-api.partykit.dev
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ jobs:

- run: npm ci

- run: node -r esbuild-register .github/rewrite-local-deps.ts

- run: npm run build

- run: npm run check
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"scripts": {
"start": "npm start -w partykit",
"build": "npm run build -w partykit && concurrently \"npm run build -w y-partykit\" \"npm run build -w partysocket\" \"npm run build -w create-partykit\" \"npm run build -w partymix\" --kill-others-on-fail",
"build": "npm run build -w partysocket && npm run build -w partykit && concurrently \"npm run build -w y-partykit\" \"npm run build -w create-partykit\" \"npm run build -w partymix\" --kill-others-on-fail",
"check": "concurrently \"npm run lint\" \"npm run repocheck\" \"npm run typecheck\" \"npx vitest --watch=false\" --kill-others-on-fail",
"lint": "npx eslint \"**/*.[tj]s?(x)\" --max-warnings=0 --report-unused-disable-directives",
"test": "vitest",
Expand Down
11 changes: 6 additions & 5 deletions packages/partykit/facade/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createLazyConnection,
} from "./connection";
import { type PartyServerAPI, ClassWorker, ModuleWorker } from "./worker";
import PartySocket from "partysocket";

// @ts-expect-error We'll be replacing __WORKER__
// with the path to the input worker
Expand Down Expand Up @@ -102,11 +103,11 @@ function createMultiParties(
},
connect: () => {
// wish there was a way to create a websocket from a durable object
return new WebSocket(
key === "main"
? `ws://${options.host}/party/${name}`
: `ws://${options.host}/parties/${key}/${name}`
);
return new PartySocket({
host: options.host,
room: name,
party: key,
});
},
};
},
Expand Down
1 change: 1 addition & 0 deletions packages/partykit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"clipboardy": "3.0.0",
"esbuild": "0.19.3",
"miniflare": "3.20230918.0",
"partysocket": "file:../partysocket",
"yoga-wasm-web": "0.3.3"
},
"optionalDependencies": {
Expand Down
9 changes: 5 additions & 4 deletions packages/partykit/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ esbuild.buildSync({
outfile: "facade/generated.js",
platform: "neutral",
bundle: true,
external: ["__WORKER__", "__STATIC_ASSETS_MANIFEST__"],
external: ["__WORKER__", "__STATIC_ASSETS_MANIFEST__", "partysocket"],
});

// generate bin/index.js
Expand All @@ -39,12 +39,13 @@ esbuild.buildSync({
outfile: "dist/bin.mjs",
platform: "node",
external: [
"react-devtools-core",
"yoga-wasm-web",
"clipboardy",
"esbuild",
"fsevents",
"miniflare",
"clipboardy",
"partysocket",
"react-devtools-core",
"yoga-wasm-web",
],
banner: isProd
? { js: "#!/usr/bin/env node" + createRequireSnippet }
Expand Down
4 changes: 3 additions & 1 deletion packages/partykit/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
Request as CFRequest,
} from "@cloudflare/workers-types";

import type PartySocket from "partysocket";

export type StaticAssetsManifestType = {
devServer: string;
browserTTL: number | undefined;
Expand Down Expand Up @@ -37,7 +39,7 @@ export interface Storage extends DurableObjectStorage {}
export type ConnectionContext = { request: CFRequest };

export type Stub = {
connect: () => WebSocket;
connect: () => PartySocket;
fetch: (init?: RequestInit) => Promise<Response>;
};

Expand Down