-
-
Notifications
You must be signed in to change notification settings - Fork 306
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
feat: add support for browser bundle for lightclient #6673
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
7ed55ae
Reorganize the code so it is accessible from one package
nazarhussain c1e138c
Add support for browser build for lightclient
nazarhussain 9407a08
Update the build config
nazarhussain d06c336
Improve the bls vite plugin
nazarhussain 6048592
Restructure the vite and vitest scripts
nazarhussain e447b9e
Simplify vite config
nazarhussain 21e9c50
Remove unused polyfill
nazarhussain 60e36a4
Fix the doc lint error
nazarhussain d803c0e
Add support for bundle test
nazarhussain cae32b2
Update the package json files
nazarhussain b3831a7
Add dist build to default build task
nazarhussain 38bcba0
Fix spelling in the docs
nazarhussain 202cc3c
Fix the lint error
nazarhussain 2557e10
Fix type error
nazarhussain cfc03a7
Disable eslint errors
nazarhussain fa9d5d5
Increase the timeout for bundle test
nazarhussain 3b6a171
Fix eslint bundle
nazarhussain ca35ddb
Fix lint warning
nazarhussain ba5886a
Remove the unused config
nazarhussain 2451dc1
Add the default export to bundle
nazarhussain d800cb6
Enable compression on th build
nazarhussain ebd79e3
Update packages/light-client/README.md
nazarhussain 0929b36
Increase timeout for one test
nazarhussain bf57160
Merge branch 'nh/lightclient-browser-build' of github.com:ChainSafe/l…
nazarhussain 0b74f5d
Merge branch 'unstable' into nh/lightclient-browser-build
nazarhussain 7e539ad
Optimize package build task
nazarhussain 6d5cec7
Update the readme
nazarhussain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This file exists to have proper namespace for the web bundle | ||
export * from "./transport/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This file exists to have proper namespace for the web bundle | ||
export * from "./utils/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {getClient, Api} from "@lodestar/api"; | ||
import {ChainForkConfig, createChainForkConfig} from "@lodestar/config"; | ||
import {NetworkName, networksChainConfig} from "@lodestar/config/networks"; | ||
|
||
export function getApiFromUrl(url: string, network: NetworkName): Api { | ||
if (!(network in networksChainConfig)) { | ||
throw Error(`Invalid network name "${network}". Valid options are: ${Object.keys(networksChainConfig).join()}`); | ||
} | ||
|
||
return getClient({urls: [url]}, {config: createChainForkConfig(networksChainConfig[network])}); | ||
} | ||
|
||
export function getChainForkConfigFromNetwork(network: NetworkName): ChainForkConfig { | ||
if (!(network in networksChainConfig)) { | ||
throw Error(`Invalid network name "${network}". Valid options are: ${Object.keys(networksChainConfig).join()}`); | ||
} | ||
|
||
return createChainForkConfig(networksChainConfig[network]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/light-client/test/unit/webEsmBundle.browser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call */ | ||
import {expect, describe, it, beforeEach, vi} from "vitest"; | ||
import "../../dist/lightclient.min.mjs"; | ||
|
||
describe("web bundle for lightclient", () => { | ||
vi.setConfig({testTimeout: 20_000}); | ||
|
||
let lightclient: any; | ||
|
||
beforeEach(() => { | ||
lightclient = (window as any)["lodestar"]["lightclient"]; | ||
}); | ||
|
||
it("should have a global interface", () => { | ||
expect(lightclient).toBeDefined(); | ||
}); | ||
|
||
it("should have all relevant exports", () => { | ||
expect(lightclient).toHaveProperty("Lightclient"); | ||
expect(lightclient).toHaveProperty("LightclientEvent"); | ||
expect(lightclient).toHaveProperty("RunStatusCode"); | ||
expect(lightclient).toHaveProperty("upgradeLightClientFinalityUpdate"); | ||
expect(lightclient).toHaveProperty("upgradeLightClientOptimisticUpdate"); | ||
expect(lightclient).toHaveProperty("utils"); | ||
expect(lightclient).toHaveProperty("transport"); | ||
expect(lightclient).toHaveProperty("validation"); | ||
|
||
expect(lightclient.Lightclient).toBeTypeOf("function"); | ||
}); | ||
|
||
it("should start the lightclient and sync", async () => { | ||
const {Lightclient, LightclientEvent, transport, utils} = lightclient; | ||
|
||
const logger = utils.getConsoleLogger({logDebug: true}); | ||
const config = utils.getChainForkConfigFromNetwork("mainnet"); | ||
|
||
// TODO: Decide to check which node to use in testing | ||
// We have one node in CI, but that only starts with e2e tests | ||
const api = utils.getApiFromUrl("https://lodestar-mainnet.chainsafe.io", "mainnet"); | ||
|
||
const lc = await Lightclient.initializeFromCheckpointRoot({ | ||
config, | ||
logger, | ||
transport: new transport.LightClientRestTransport(api), | ||
genesisData: await utils.getGenesisData(api), | ||
checkpointRoot: await utils.getFinalizedSyncCheckpoint(api), | ||
opts: { | ||
allowForcedUpdates: true, | ||
updateHeadersOnForcedUpdate: true, | ||
}, | ||
}); | ||
|
||
await expect(lc.start()).resolves.toBeUndefined(); | ||
|
||
await expect( | ||
new Promise((resolve) => { | ||
lc.emitter.on(LightclientEvent.lightClientOptimisticHeader, async (optimisticUpdate: unknown) => { | ||
resolve(optimisticUpdate); | ||
}); | ||
}) | ||
).resolves.toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"exclude": ["src/index.browser.ts"], | ||
"compilerOptions": {} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to find a way to remove the need for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's in the
bls
library. @matthewkeil is working on a newer version. Hope that will solve this problem.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will not. there will always be a top level await in esm because the bindings path is programmatic so its not imported it looked up and then
await import(bindingLocation)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i suppose it would be possible in
bls
to use hard imports and thennull-loader
from weback to not include the other version for web. But for blst if we make it esm it will always have the top-level await for the importThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible avoid top-level-import, but then would have to leave upto user of the library to init where appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is a difference between using top-level await and a ESM module without it. Eg. Node 22 will ship a new feature to
require
ES modules with the exception of the ones that use top-level await (see nodejs/node#51977).In any case, we might wanna avoid top-level await in packages that are used by others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the top-level await go away in
blst
fyi. so might be able to also do something similar inbls
...https://github.com/ChainSafe/blst-ts/blob/37b13881307ee4eb8fb8a4a84f6cafccfde5293c/lib/index.mjs#L7-10