Skip to content

Commit 3c8a002

Browse files
authored
Merge pull request #58 from webxdc/simon/stackblitz-support
fix usage inside of stackblitz.com online IDE
2 parents 6b422ea + 6c3b802 commit 3c8a002

7 files changed

+49
-4
lines changed

.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66

77
# we don't want test fixtures
88
backend/fixtures
9+
10+
example_app.xdc

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ release date when you use `npm version` (see `README.md`).
2121

2222
- Apps being unable to `fetch()` anything or use `blob:` and `data:` resource because of `connect-src` CSP
2323
- `indexedDB` not getting cleared on `clear()`
24+
- fix usage inside of stackblitz.com online IDE
2425

2526
## [0.17.0][] - 2023-06-08
2627

backend/instance.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createPeer, InjectExpress } from "./app";
88
import { AppInfo } from "./appInfo";
99
import { getColorForId } from "./color";
1010
import { Instance as FrontendInstance } from "../types/instance";
11+
import { getInstanceUrl } from "./instance_url";
1112

1213
export type Options = {
1314
basePort: number;
@@ -93,7 +94,7 @@ export class Instances {
9394
throw new Error(`Already have Webxdc instance at port: ${port}`);
9495
}
9596

96-
const instanceUrl = `http://localhost:${port}`;
97+
const instanceUrl = getInstanceUrl(port);
9798

9899
const wsInstance = createPeer({
99100
location: this.location,

backend/instance_url.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { isWebContainer, HostURL } from "@webcontainer/env";
2+
3+
export function getInstanceUrl(port: number) {
4+
if (isWebContainer()) {
5+
// stackblitz / webcontainer uses different url to represent different ports.
6+
// This is why we need to convert it here.
7+
return HostURL.parse(`https://localhost:${port}`).href;
8+
}
9+
return `http://localhost:${port}`;
10+
}

package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"dependencies": {
7676
"@hope-ui/solid": "^0.6.2",
7777
"@stitches/core": "^1.2.8",
78+
"@webcontainer/env": "^1.1.1",
7879
"adm-zip": "^0.5.9",
7980
"body-parser": "^1.20.0",
8081
"commander": "^9.3.0",

sim/webxdc.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,26 @@ export class DevServerTransport implements Transport {
8383
window.location.reload();
8484
}
8585

86+
static port() {
87+
if (location.host.endsWith(".webcontainer.io")) {
88+
// stackblitz / webcontainer uses different url to represent different ports.
89+
// example: `localhost:7002` becomes `https://xoriwypmnngithub-ltqe--7002--f565b097.local-corp.webcontainer.io/`
90+
// in stackblitz environment.
91+
// This regex extracts the port from the url.
92+
return (
93+
/--(\d+)--/.exec(document.location.href)?.[1] ||
94+
"error in webxdc simulator"
95+
);
96+
} else {
97+
return document.location.port;
98+
}
99+
}
100+
86101
address() {
87-
return `instance@${document.location.port}`;
102+
return `instance@${DevServerTransport.port()}`;
88103
}
89104
name() {
90-
return `Instance ${document.location.port}`;
105+
return `Instance ${DevServerTransport.port()}`;
91106
}
92107

93108
setInfo(info: Info): void {
@@ -115,7 +130,11 @@ window.addEventListener("load", () => alterUi(getWebXdc().selfName, transport));
115130

116131
// listen to messages coming into iframe
117132
window.addEventListener("message", (event) => {
118-
if (event.origin.indexOf("localhost:") === -1) {
133+
const isAllowed =
134+
event.origin.includes("localhost:") ||
135+
(location.host.endsWith(".webcontainer.io") &&
136+
event.origin.includes(".webcontainer.io"));
137+
if (!isAllowed) {
119138
return;
120139
}
121140
if (event.data === "reload") {

0 commit comments

Comments
 (0)