Skip to content

Commit 2c84e0a

Browse files
authored
Bundle sdk for browser (no polyfills required!), and for Node.js (#3358)
### Summary Introduces a Rollup-based bundling system for the SDK, enabling proper platform-specific builds for both Node.js and browser environments with architectural refactoring to support environment-specific code via the `@/` path alias pattern. ### Key Changes - **Rollup bundling pipeline** - Added `rollup.config.mts` producing multiple output formats: - Node.js: ESM (`.js`) and CJS (`.cjs`) - Browser: ESM (`.js`) and CJS (`.cjs`) - UMD: Regular and minified builds for `<script>` tag usage - **Platform-specific code organization** - Introduced `_browser/` and `_nodejs/` directories with `@/` path alias that resolves to the correct platform at build time: - `Persistence`: Browser uses IndexedDB, Node.js uses SQLite - `createRSAKeyPair`: Browser uses WebCrypto, Node.js uses native crypto - **TypeScript configuration refactor** - Split into `tsconfig.node.json` and `tsconfig.browser.json` for separate compilation targets - **Browser compatibility improvements**: - Replaced `crypto.randomBytes` with `@noble/post-quantum/utils` - Added `publicEncrypt` and `privateDecrypt` to `@streamr/utils` - Aliased `stream` → `readable-stream` and `timers` → `timers-browserify` ### Known Limitations - `Persistence.exists()` is not implemented for browser (throws error) - this functionality is Node.js only ### Future Considerations - In the SDK, browser-specific code lives in `src/_browser/` and Node.js-specific code in `src/_nodejs/`. This underscore-prefixed directory pattern could be applied to other packages in the monorepo that need platform-specific implementations.
1 parent 99d3dca commit 2c84e0a

File tree

77 files changed

+1276
-2631
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1276
-2631
lines changed

docs/docs/usage/sdk/how-to-use.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Node.js `20` is the minimum required version, ideally version 22 and later. Node
3838
For usage in the browser include the latest build, e.g. by including a `<script>` tag pointing at a CDN:
3939

4040
```html
41-
<script src="https://unpkg.com/@streamr/sdk/streamr-sdk.web.js"></script>
41+
<script src="https://unpkg.com/@streamr/sdk/exports-umd.js"></script>
4242
```
4343

4444
#### Browser extension

package-lock.json

Lines changed: 421 additions & 2051 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/browser-test-runner/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"rimraf": "^6.1.2",
5151
"rollup": "^4.55.1",
5252
"rollup-plugin-dts": "^6.3.0",
53+
"ts-loader": "^9.5.4",
5354
"tsx": "^4.21.0",
5455
"webpack": "^5.103.0",
5556
"webpack-cli": "^6.0.1"

packages/sdk/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The Streamr SDK is built for the browser and Node.js environments.
4141

4242
**Browser (Website/WebApps)**
4343
- For usage in the browser include the latest build, e.g. by including a `<script>` tag pointing at a CDN:
44-
- `<script src="https://unpkg.com/@streamr/sdk@latest/streamr-sdk.web.min.js"></script>`
44+
- `<script src="https://unpkg.com/@streamr/sdk@latest/exports-umd.min.js"></script>`
4545
- To use within React, please see [streamr-client-react](https://github.com/streamr-dev/streamr-client-react)
4646

4747
**Browser extension**

packages/sdk/bin/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "..",
2+
"extends": "../tsconfig.node.json",
33
"include": [
44
"**/*"
55
]

packages/sdk/createKarmaConfig.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
createKarmaConfig as createKarmaConfigUtil,
3+
createWebpackConfig,
4+
} from '@streamr/browser-test-runner'
5+
import { resolve } from 'node:path'
6+
7+
export function createKarmaConfig(testPaths: string[]): ReturnType<typeof createKarmaConfigUtil> {
8+
return createKarmaConfigUtil(
9+
testPaths,
10+
createWebpackConfig({
11+
libraryName: 'sdk',
12+
alias: {
13+
'@jest/globals': resolve(
14+
__dirname,
15+
'test/test-utils/jestGlobalsMock.ts'
16+
),
17+
'@streamr/dht': resolve(__dirname, '../dht/dist/exports-browser.cjs'),
18+
'@': resolve(__dirname, 'src/_browser'),
19+
},
20+
fallback: {
21+
v8: false,
22+
module: false,
23+
},
24+
externals: {
25+
http: 'HTTP',
26+
ws: 'WebSocket',
27+
'node-datachannel': 'commonjs node-datachannel',
28+
express: 'Express',
29+
'node:stream/web': 'stream/web',
30+
'node:timers/promises': 'timers/promises',
31+
},
32+
}),
33+
__dirname
34+
)
35+
}

packages/sdk/fix-esm.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/sdk/jest.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const config: Config.InitialOptions = {
77
setupFilesAfterEnv: [
88
...defaultConfig.setupFilesAfterEnv,
99
'./src/setupTsyringe.ts',
10-
'./test/test-utils/customMatchers.ts',
10+
'./test/test-utils/setupCustomMatchers.ts',
1111
'@streamr/test-utils/setupCustomMatchers',
1212
],
13+
moduleNameMapper: {
14+
"^@/(.*)$": "<rootDir>/src/_nodejs/$1",
15+
},
1316
}
1417

1518
export default config

packages/sdk/jest.setup.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
// eslint-disable-next-line import/no-extraneous-dependencies
2-
import { GitRevisionPlugin } from 'git-revision-webpack-plugin'
1+
import { execSync } from 'child_process'
32
import pkg from './package.json'
43

4+
const execGitCommand = (command: string): string => {
5+
try {
6+
return execSync(command, { encoding: 'utf8' }).trim()
7+
} catch {
8+
return ''
9+
}
10+
}
11+
512
export default async function setup(): Promise<void> {
613
if (process.env.GIT_VERSION) {
714
return
815
}
916

10-
const gitRevisionPlugin = new GitRevisionPlugin()
11-
12-
const [GIT_VERSION, GIT_COMMITHASH, GIT_BRANCH] = await Promise.all([
13-
gitRevisionPlugin.version(),
14-
gitRevisionPlugin.commithash(),
15-
gitRevisionPlugin.branch(),
16-
])
17+
const GIT_VERSION = execGitCommand('git describe --always --tags')
18+
const GIT_COMMITHASH = execGitCommand('git rev-parse HEAD')
19+
const GIT_BRANCH = execGitCommand('git rev-parse --abbrev-ref HEAD')
1720

1821
Object.assign(process.env, {
1922
version: pkg.version,
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
import webpackConfig from './webpack-karma.config'
2-
import { createKarmaConfig } from '@streamr/browser-test-runner'
3-
4-
export default createKarmaConfig(['test/end-to-end/**/*.ts'], webpackConfig, __dirname)
1+
import { createKarmaConfig } from './createKarmaConfig'
2+
export default createKarmaConfig(['test/end-to-end/**/*.ts'])

0 commit comments

Comments
 (0)