Skip to content

Commit 5d35c22

Browse files
authored
refactor: Add timers polyfill to DHT package for standalone browser usage (#3379)
## Summary Adds the `timers-browserify` polyfill to the DHT package, enabling standalone DHT usage in browsers without requiring consumers to manually polyfill `setImmediate`. The SDK retains its existing polyfill for redundancy. ## Changes - Add `timers-browserify` dependency to `@streamr/dht` package - Add `import 'timers'` polyfill in DHT's `exports.ts` for browser compatibility - Configure `timers` → `timers-browserify` alias in DHT's rollup browser config
1 parent 2a4e5b0 commit 5d35c22

File tree

6 files changed

+18
-21
lines changed

6 files changed

+18
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Changes before Tatum release are not documented in this file.
1414

1515
#### Added
1616

17+
- Add `timers-browserify` polyfill to `@streamr/dht` for standalone browser usage ([#3379])
18+
1719
#### Changed
1820

1921
### @streamr/sdk
@@ -686,3 +688,4 @@ Autostaker changes:
686688
[#3368]: https://github.com/streamr-dev/network/pull/3368
687689
[#3369]: https://github.com/streamr-dev/network/pull/3369
688690
[#3373]: https://github.com/streamr-dev/network/pull/3373
691+
[#3379]: https://github.com/streamr-dev/network/pull/3379

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dht/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"lodash": "^4.17.21",
6464
"lru-cache": "^11.2.2",
6565
"node-datachannel": "^0.27.0",
66+
"timers-browserify": "^2.0.12",
6667
"uuid": "^11.1.0",
6768
"websocket": "^1.0.34",
6869
"ws": "^8.18.3"

packages/dht/rollup.config.mts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const browserAliases: Alias[] = [
2121
new URL('./dist/browser/src/browser/', import.meta.url)
2222
),
2323
},
24+
{ find: 'timers', replacement: 'timers-browserify' },
2425
]
2526

2627
export default defineConfig([
@@ -54,11 +55,7 @@ function nodejs(): RollupOptions {
5455
preferBuiltins: true,
5556
}),
5657
],
57-
external: [
58-
/node_modules/,
59-
/@streamr\//,
60-
],
61-
58+
external: [/node_modules/, /@streamr\//],
6259
}
6360
}
6461

@@ -77,11 +74,7 @@ function nodejsTypes(): RollupOptions {
7774
nodeResolve(),
7875
dts(),
7976
],
80-
external: [
81-
/node_modules/,
82-
/@streamr\//,
83-
],
84-
77+
external: [/node_modules/, /@streamr\//],
8578
}
8679
}
8780

@@ -110,19 +103,14 @@ function browser(): RollupOptions {
110103
browser: true,
111104
}),
112105
],
113-
external: [
114-
/node_modules/,
115-
/@streamr\//,
116-
],
106+
external: [/node_modules/, /@streamr\//],
117107
}
118108
}
119109

120110
function browserTypes(): RollupOptions {
121111
return {
122112
input: './dist/browser/src/exports.d.ts',
123-
output: [
124-
{ file: './dist/exports-browser.d.ts' },
125-
],
113+
output: [{ file: './dist/exports-browser.d.ts' }],
126114
plugins: [
127115
alias({
128116
entries: browserAliases,
@@ -133,9 +121,6 @@ function browserTypes(): RollupOptions {
133121
}),
134122
dts(),
135123
],
136-
external: [
137-
/node_modules/,
138-
/@streamr\//,
139-
],
124+
external: [/node_modules/, /@streamr\//],
140125
}
141126
}

packages/dht/src/exports.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Importing 'timers' ensures `setImmediate` is available in browsers,
3+
* as it's polyfilled by `timers-browserify`. In Node.js, it's already global.
4+
*/
5+
import 'timers'
6+
17
export { DhtNode, type DhtNodeEvents, type DhtNodeOptions } from './dht/DhtNode'
28
export { ListeningRpcCommunicator } from './transport/ListeningRpcCommunicator'
39
export { RoutingRpcCommunicator } from './transport/RoutingRpcCommunicator'

packages/sdk/src/StreamrClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* as it's polyfilled by `timers-browserify`. In Node.js, it's already global.
44
*/
55
import 'timers'
6+
67
import './setupTsyringe'
78
import './utils/PatchTsyringe'
89

0 commit comments

Comments
 (0)