Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit 8c0b8b9

Browse files
committed
feat(explorer, lib): some refactoring, add custom namespace in explorer
1 parent bb4cdda commit 8c0b8b9

File tree

7 files changed

+64
-33
lines changed

7 files changed

+64
-33
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@typescript-eslint/eslint-plugin": "5.16.0",
4141
"@typescript-eslint/parser": "5.16.0",
4242
"babel-jest": "27.5.1",
43+
"compression-webpack-plugin": "^9.2.0",
4344
"cypress": "9.5.2",
4445
"eslint": "8.11.0",
4546
"eslint-config-prettier": "8.5.0",
@@ -66,6 +67,7 @@
6667
"ts-jest": "27.1.3",
6768
"typescript": "^4.6.2",
6869
"util": "^0.12.4",
70+
"webpack-bundle-analyzer": "^4.5.0",
6971
"yaml-crypt": "0.7.6",
7072
"zx": "6.0.7"
7173
},

packages/explorer/src/main.tsx

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
11
import React, { useEffect, useState, StrictMode } from 'react'
22
import * as ReactDOM from 'react-dom'
33
import { BrowserRouter } from 'react-router-dom'
4-
import { Stack } from '@dstack-js/lib'
4+
import { Stack, Shard, ShardKind } from '@dstack-js/lib'
5+
6+
declare global {
7+
interface Window {
8+
stack: Stack;
9+
Shard: typeof Shard;
10+
ShardKind: typeof ShardKind;
11+
}
12+
}
513

614
export const App: React.FC = () => {
7-
const namespace = 'explorer'
15+
const namespace: string = localStorage['namespace'] || 'explorer'
816
const [stack, setStack] = useState<Stack | null>()
917

1018
useEffect(() => {
19+
// eslint-disable-next-line @typescript-eslint/no-empty-function
20+
let stop = () => {}
21+
1122
const run = async () => {
1223
const stack = await Stack.create({ namespace })
13-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
14-
// @ts-ignore
1524
window.stack = stack
25+
window.Shard = Shard
26+
window.ShardKind = ShardKind
1627
setStack(stack)
28+
stop = () => stack.stop().catch(console.error)
1729
}
1830

19-
run().catch(console.error)
20-
}, [])
31+
run().catch((err) => {
32+
alert(err)
33+
console.error(err)
34+
})
35+
36+
return () => stop()
37+
}, [namespace])
2138

22-
return stack ? <div>use window.stack</div> : <div>Initializing Stack</div>
39+
return stack
40+
? (
41+
<div>
42+
namespace: <pre>{namespace}</pre> <br /> <code>window.stack</code>,{' '}
43+
<code>window.Shard</code>, <code>window.ShardKind</code>,{' '}
44+
<code>localStorage.namespace</code>
45+
</div>
46+
)
47+
: (
48+
<div>Initializing Stack</div>
49+
)
2350
}
2451

2552
ReactDOM.render(

packages/lib/src/shard.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ export class Shard<TData extends Data = Data> {
9595
stack: Stack,
9696
path: string
9797
): Promise<Shard<TData>> {
98-
// eslint-disable-next-line no-unused-vars
99-
const [_, shard, cid, kind] = path.split('/')
98+
const [shard, cid, kind] = path.split('/').slice(-3)
10099

101100
if (shard !== 'shard') {
102101
throw new InvalidShardPathError()

packages/lib/src/stack.ts

+2-22
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class Stack {
5353
public store: Store
5454

5555
private announceInterval?: ReturnType<typeof setTimeout>
56-
public announce = false
56+
public announce = true
5757

5858
constructor(public namespace: CID, public ipfs: IPFS, storage: Storage) {
5959
this.pubsub = new PubSub(ipfs, namespace.toString())
@@ -92,13 +92,9 @@ export class Stack {
9292
if (this.announceInterval) clearInterval(this.announceInterval)
9393
await this.store.stop()
9494
await this.pubsub.stop()
95+
await this.ipfs.stop()
9596
}
9697

97-
/**
98-
* Create stack
99-
*
100-
* @returns Stack instance
101-
*/
10298
public static async create({
10399
namespace,
104100
ipfs,
@@ -119,11 +115,6 @@ export class Stack {
119115
return stack
120116
}
121117

122-
/**
123-
* Get connected peers
124-
*
125-
* @returns connected peers list
126-
*/
127118
public async peers(): Promise<Peer[]> {
128119
const peers = await this.ipfs.swarm.peers()
129120

@@ -133,13 +124,6 @@ export class Stack {
133124
}))
134125
}
135126

136-
/**
137-
* Connect to peer
138-
*
139-
* By default IPFS will connect to some peers automatically, no need to use it without a reason
140-
*
141-
* @param address MultiAddr to connect
142-
*/
143127
public async connect(address: string): Promise<void> {
144128
await this.ipfs.swarm.connect(address)
145129
}
@@ -150,8 +134,6 @@ export class Stack {
150134
* @param listener Will be called with peer info
151135
*/
152136
public onPeerConnect(listener: (peer: Peer) => void): void {
153-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
154-
// @ts-ignore
155137
this.libp2p.addressManager.on('peer:connect', (event) => {
156138
listener({
157139
id: event.remotePeer.toB58String(),
@@ -166,8 +148,6 @@ export class Stack {
166148
* @param listener Will be called with peer info
167149
*/
168150
public onPeerDisconnected(listener: (peer: Peer) => void): void {
169-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
170-
// @ts-ignore
171151
this.libp2p.addressManager.on('peer:disconnect', (event) => {
172152
listener({
173153
id: event.remotePeer.toB58String(),

packages/relay/project.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"assets": [
1111
"packages/relay/*.md",
1212
"packages/relay/Dockerfile",
13-
"packages/relay/fly.toml",
14-
"yarn.lock"
13+
"packages/relay/fly.toml"
1514
],
1615
"main": "packages/relay/src/index.ts",
1716
"outputPath": "dist/packages/relay",

webpack.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const { join } = require('path');
33

44
const nrwlConfig = require('@nrwl/react/plugins/webpack.js');
55
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
6+
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
7+
const CompressionPlugin = require('compression-webpack-plugin');
68

79
module.exports = (config) => {
810
nrwlConfig(config);
@@ -16,7 +18,15 @@ module.exports = (config) => {
1618
Buffer: ['buffer', 'Buffer'],
1719
process: 'process/browser',
1820
}),
21+
new CompressionPlugin(),
22+
// new BundleAnalyzerPlugin({ analyzerMode: 'static' })
1923
],
24+
optimization: {
25+
mergeDuplicateChunks: true,
26+
minimize: true,
27+
moduleIds: 'size',
28+
nodeEnv: 'production',
29+
},
2030
resolve: {
2131
fallback: {
2232
util: require.resolve('util'),

yarn.lock

+14
Original file line numberDiff line numberDiff line change
@@ -10032,6 +10032,18 @@ __metadata:
1003210032
languageName: node
1003310033
linkType: hard
1003410034

10035+
"compression-webpack-plugin@npm:^9.2.0":
10036+
version: 9.2.0
10037+
resolution: "compression-webpack-plugin@npm:9.2.0"
10038+
dependencies:
10039+
schema-utils: ^4.0.0
10040+
serialize-javascript: ^6.0.0
10041+
peerDependencies:
10042+
webpack: ^5.1.0
10043+
checksum: 50ef78d0973f87ed8d7c30fdb31b009d409c288a77ee842d9b3b5b2348048f55148f510d3a0bd30b5c954e6701ed9fc08c3616ce46ad1eec8e0954b336a5f4b9
10044+
languageName: node
10045+
linkType: hard
10046+
1003510047
"compression@npm:^1.7.4":
1003610048
version: 1.7.4
1003710049
resolution: "compression@npm:1.7.4"
@@ -11941,6 +11953,7 @@ __metadata:
1194111953
"@typescript-eslint/parser": 5.16.0
1194211954
babel-jest: 27.5.1
1194311955
clsx: 1.1.1
11956+
compression-webpack-plugin: ^9.2.0
1194411957
core-js: 3.21.1
1194511958
cypress: 9.5.2
1194611959
eslint: 8.11.0
@@ -11974,6 +11987,7 @@ __metadata:
1197411987
tslib: 2.3.1
1197511988
typescript: ^4.6.2
1197611989
util: ^0.12.4
11990+
webpack-bundle-analyzer: ^4.5.0
1197711991
yaml-crypt: 0.7.6
1197811992
zx: 6.0.7
1197911993
languageName: unknown

0 commit comments

Comments
 (0)