Skip to content

Commit

Permalink
fix: the problem of socket host when use ip:port url (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
easy1090 authored Oct 28, 2024
1 parent b6508b6 commit 5b5355c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
6 changes: 4 additions & 2 deletions packages/cli/src/commands/bundle-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ example: ${bin} ${Commands.BundleDiff} --baseline="x.json" --current="x.json"
const baselineManifestsBuffer = Buffer.from(
JSON.stringify({
__LOCAL__SERVER__: true,
__SOCKET__URL__: baselineSdk.server.socketUrl,
__SOCKET__URL__: baselineSdk.server.socketUrl.socketUrl,
__SOCKET__PORT__: baselineSdk.server.socketUrl.port,
...baselineData,
}),
);
const currentManifestsBuffer = Buffer.from(
JSON.stringify({
__LOCAL__SERVER__: true,
__SOCKET__URL__: currentSdk.server.socketUrl,
__SOCKET__PORT__: currentSdk.server.socketUrl.port,
__SOCKET__URL__: currentSdk.server.socketUrl.socketUrl,
...currentData,
}),
);
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/utils/data/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export class LocalServerDataLoader extends BaseDataLoader {
const [api, body] = args;
// request limitation key
const key = body ? `${api}_${JSON.stringify(body)}` : `${api}`;
const socketUrl = this.get('__SOCKET__URL__') ?? ''
const socketPort = this.get('__SOCKET__PORT__') ?? '';

return this.limit(key, async () => {
return new Promise((resolve) => {
getSocket(socketUrl).emit(
getSocket(socketPort).emit(
api,
body,
(res: SDK.ServerAPI.SocketResponseType<T>) => {
Expand Down
32 changes: 30 additions & 2 deletions packages/components/src/utils/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,35 @@ function ensureSocket(socketUrl: string = defaultSocketUrl) {
}
return map.get(socketUrl)!;
}
export function getSocket(socketUrl?: string): Socket {
const socket = ensureSocket(socketUrl || defaultSocketUrl);

export function getSocket(socketPort?: string): Socket {
const socketUrl = formatURL({
port: socketPort,
hostname: location.hostname,
protocol: location.protocol,
});
const socket = ensureSocket(socketPort ? socketUrl : defaultSocketUrl);
return socket;
}

export function formatURL({
port,
protocol,
hostname,
}: {
port?: string;
protocol: string;
hostname: string;
}) {
if (typeof URL !== 'undefined') {
const url = new URL('http://localhost');
url.port = String(port);
url.hostname = hostname;
url.protocol = location.protocol.includes('https') ? 'wss' : 'ws';
return url.toString();
}

// compatible with IE11
const colon = protocol.indexOf(':') === -1 ? ':' : '';
return `${protocol}${colon}//${hostname}:${port}`;
}
3 changes: 2 additions & 1 deletion packages/sdk/src/sdk/sdk/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ export class RsdoctorWebpackSDK<
return t;
}, {} as Common.PlainObject) as unknown as Manifest.RsdoctorManifestWithShardingFiles['data'],
__LOCAL__SERVER__: true,
__SOCKET__URL__: this.server.socketUrl,
__SOCKET__PORT__: this.server.socketUrl.port.toString(),
__SOCKET__URL__: this.server.socketUrl.socketUrl,
};

return data;
Expand Down
9 changes: 7 additions & 2 deletions packages/sdk/src/sdk/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { getLocalIpAddress } from './utils';
import { isUndefined } from 'lodash';
export * from './utils';

export type ISocketType = { port: number; socketUrl: string };

export class RsdoctorServer implements SDK.RsdoctorServerInstance {
private _server!: Common.PromiseReturnType<typeof Server.createServer>;

Expand Down Expand Up @@ -59,8 +61,11 @@ export class RsdoctorServer implements SDK.RsdoctorServerInstance {
return `http://${this.host}:${this.port}`;
}

public get socketUrl(): string {
return `ws://localhost:${this.port}`;
public get socketUrl(): ISocketType {
return {
port: this.port,
socketUrl: `ws://localhost:${this.port}`,
};
}

public get innerClientPath(): string {
Expand Down
13 changes: 7 additions & 6 deletions packages/types/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface RsdoctorManifestWithShardingFiles
* local server will proxy the manifest content and inject `__LOCAL__SERVER__: true`
*/
__LOCAL__SERVER__?: boolean;
__SOCKET__PORT__?: string;
__SOCKET__URL__?: string;
}

Expand Down Expand Up @@ -73,10 +74,10 @@ export type RsdoctorManifestMappingKeys =
? RsdoctorManifestData[K] extends Array<unknown>
? never
: string extends keyof RsdoctorManifestData[K]
? never
: keyof RsdoctorManifestData[K] extends string
? `${K}.${keyof RsdoctorManifestData[K]}`
: never
? never
: keyof RsdoctorManifestData[K] extends string
? `${K}.${keyof RsdoctorManifestData[K]}`
: never
: never;
}[RsdoctorManifestObjectKeys]
| RsdoctorManifestRootKeys;
Expand All @@ -89,8 +90,8 @@ export type InferManifestDataValue<T> =
: never
: never
: T extends RsdoctorManifestRootKeys
? RsdoctorManifestData[T]
: never;
? RsdoctorManifestData[T]
: never;

export interface ManifestDataLoader {
loadManifest(): Promise<RsdoctorManifest | RsdoctorManifestWithShardingFiles>;
Expand Down

0 comments on commit 5b5355c

Please sign in to comment.