Skip to content

Commit a7b1a32

Browse files
committed
fix usage inside of stackblitz.com online IDE
1 parent 6b422ea commit a7b1a32

File tree

8 files changed

+58
-17
lines changed

8 files changed

+58
-17
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

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { Location } from "./location";
77
import { createPeer, InjectExpress } from "./app";
88
import { AppInfo } from "./appInfo";
99
import { getColorForId } from "./color";
10-
import { Instance as FrontendInstance } from "../types/instance";
10+
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+
}

backend/run.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import process from "process";
2-
import open from "open";
1+
import process from 'process';
2+
import open from 'open';
33

4-
import { createFrontend, InjectExpress } from "./app";
5-
import { Instances, Options } from "./instance";
6-
import { getLocation, Location, LocationError } from "./location";
7-
import { getAppInfo, AppInfo, AppInfoError } from "./appInfo";
4+
import { createFrontend, InjectExpress } from './app';
5+
import { Instances, Options } from './instance';
6+
import { getLocation, Location, LocationError } from './location';
7+
import { getAppInfo, AppInfo, AppInfoError } from './appInfo';
88

99
export type Inject = {
1010
injectFrontend: InjectExpress;
@@ -29,12 +29,12 @@ function actualRun(appInfo: AppInfo, options: Options, inject: Inject): void {
2929
);
3030

3131
frontend.listen(options.basePort, () => {
32-
console.log("Starting webxdc-dev frontend");
32+
console.log('Starting webxdc-dev frontend');
3333
});
3434

3535
instances.start();
3636

37-
open("http://localhost:" + options.basePort);
37+
open('http://localhost:' + options.basePort);
3838
}
3939

4040
export function run(locationStr: string, options: Options, inject: Inject) {
@@ -49,13 +49,13 @@ export function run(locationStr: string, options: Options, inject: Inject) {
4949
throw e;
5050
}
5151

52-
for (const signal in ["SIGINT", "SIGTERM"]) {
52+
for (const signal in ['SIGINT', 'SIGTERM']) {
5353
process.on(signal, () => {
5454
location.dispose();
5555
});
5656
}
5757

58-
console.log("Starting webxdc project in:", locationStr);
58+
console.log('Starting webxdc project in:', locationStr);
5959

6060
getAppInfo(location)
6161
.then((appInfo) => {

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

+20-5
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,9 +130,9 @@ 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) {
119-
return;
120-
}
133+
// if (event.origin.indexOf("localhost:") === -1) {
134+
// return;
135+
// }
121136
if (event.data === "reload") {
122137
window.location.reload();
123138
}

0 commit comments

Comments
 (0)