@streamr/dht 102.2.0-rc.0 → 102.2.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamr/dht",
3
- "version": "102.2.0-rc.0",
3
+ "version": "102.2.0-rc.2",
4
4
  "description": "Streamr Network DHT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,15 +9,20 @@
9
9
  },
10
10
  "license": "STREAMR NETWORK OPEN SOURCE LICENSE",
11
11
  "author": "Streamr Network AG <contact@streamr.network>",
12
+ "files": [
13
+ "dist",
14
+ "!*.tsbuildinfo",
15
+ "README.md"
16
+ ],
12
17
  "dependencies": {
13
18
  "@js-sdsl/ordered-map": "^4.4.2",
14
19
  "@protobuf-ts/runtime": "^2.8.2",
15
20
  "@protobuf-ts/runtime-rpc": "^2.8.2",
16
- "@streamr/autocertifier-client": "102.2.0-rc.0",
17
- "@streamr/cdn-location": "102.2.0-rc.0",
18
- "@streamr/geoip-location": "102.2.0-rc.0",
19
- "@streamr/proto-rpc": "102.2.0-rc.0",
20
- "@streamr/utils": "102.2.0-rc.0",
21
+ "@streamr/autocertifier-client": "102.2.0-rc.2",
22
+ "@streamr/cdn-location": "102.2.0-rc.2",
23
+ "@streamr/geoip-location": "102.2.0-rc.2",
24
+ "@streamr/proto-rpc": "102.2.0-rc.2",
25
+ "@streamr/utils": "102.2.0-rc.2",
21
26
  "eventemitter3": "^5.0.0",
22
27
  "heap": "^0.2.6",
23
28
  "ipaddr.js": "^2.0.1",
@@ -31,7 +36,7 @@
31
36
  },
32
37
  "devDependencies": {
33
38
  "@streamr/browser-test-runner": "^0.0.1",
34
- "@streamr/test-utils": "102.2.0-rc.0",
39
+ "@streamr/test-utils": "102.2.0-rc.2",
35
40
  "@types/heap": "^0.2.34",
36
41
  "@types/k-bucket": "^5.0.1",
37
42
  "@types/lodash": "^4.17.15",
@@ -40,7 +45,7 @@
40
45
  "jest-leak-detector": "^27.3.1",
41
46
  "jest-matcher-utils": "^29.7.0",
42
47
  "ts-essentials": "^10.0.4",
43
- "tsx": "^4.19.3"
48
+ "ts-node": "^10.9.2"
44
49
  },
45
50
  "optionalDependencies": {
46
51
  "bufferutil": "^4.0.9",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamr/dht",
3
- "version": "102.2.0-rc.0",
3
+ "version": "102.2.0-rc.2",
4
4
  "description": "Streamr Network DHT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,6 +16,11 @@
16
16
  },
17
17
  "license": "STREAMR NETWORK OPEN SOURCE LICENSE",
18
18
  "author": "Streamr Network AG <contact@streamr.network>",
19
+ "files": [
20
+ "dist",
21
+ "!*.tsbuildinfo",
22
+ "README.md"
23
+ ],
19
24
  "scripts": {
20
25
  "prebuild": "./proto.sh",
21
26
  "postbuild": "./scripts/postbuild.sh",
@@ -35,11 +40,11 @@
35
40
  "@js-sdsl/ordered-map": "^4.4.2",
36
41
  "@protobuf-ts/runtime": "^2.8.2",
37
42
  "@protobuf-ts/runtime-rpc": "^2.8.2",
38
- "@streamr/autocertifier-client": "102.2.0-rc.0",
39
- "@streamr/cdn-location": "102.2.0-rc.0",
40
- "@streamr/geoip-location": "102.2.0-rc.0",
41
- "@streamr/proto-rpc": "102.2.0-rc.0",
42
- "@streamr/utils": "102.2.0-rc.0",
43
+ "@streamr/autocertifier-client": "102.2.0-rc.2",
44
+ "@streamr/cdn-location": "102.2.0-rc.2",
45
+ "@streamr/geoip-location": "102.2.0-rc.2",
46
+ "@streamr/proto-rpc": "102.2.0-rc.2",
47
+ "@streamr/utils": "102.2.0-rc.2",
43
48
  "eventemitter3": "^5.0.0",
44
49
  "heap": "^0.2.6",
45
50
  "ipaddr.js": "^2.0.1",
@@ -53,7 +58,7 @@
53
58
  },
54
59
  "devDependencies": {
55
60
  "@streamr/browser-test-runner": "^0.0.1",
56
- "@streamr/test-utils": "102.2.0-rc.0",
61
+ "@streamr/test-utils": "102.2.0-rc.2",
57
62
  "@types/heap": "^0.2.34",
58
63
  "@types/k-bucket": "^5.0.1",
59
64
  "@types/lodash": "^4.17.15",
@@ -62,7 +67,7 @@
62
67
  "jest-leak-detector": "^27.3.1",
63
68
  "jest-matcher-utils": "^29.7.0",
64
69
  "ts-essentials": "^10.0.4",
65
- "tsx": "^4.19.3"
70
+ "ts-node": "^10.9.2"
66
71
  },
67
72
  "optionalDependencies": {
68
73
  "bufferutil": "^4.0.9",
package/dist/README.md DELETED
@@ -1,120 +0,0 @@
1
- # @streamr/dht
2
-
3
- A connectionful Kademlia based P2P distributed hash table (DHT). Implements an in-memory key-value data store where multiple creators of data store values behind a single key. The library is entirely browser compatible.
4
-
5
- All communication between peers utilizes [protobuf](https://protobuf.dev/), transported with the help of the custom `@streamr/proto-rpc` library.
6
-
7
- Connections in the DHT are established using WebSocket or WebRTC connections. The nodes will decide internally based on peers' connectivity information which connection types to use.
8
-
9
- The DHT also provides an interface for sending messages from peer to peer with the `DhtNode#send` function. The sent message is routed over the network to the target node.
10
-
11
- ## Running a node
12
-
13
- Running a bare DhtNode in the Streamr Network is heavily discouraged as it requires advanced configuration. However, if you do wish to run a node here are some recommended configurations.
14
-
15
- (the following values are examples. For working production or test values check the @streamr/sdk default network configuration)
16
-
17
- ```js
18
- const DhtNode = new DhtNode({
19
- // Add a list of known entry points to the network
20
- entryPoints: [{
21
- kademliaId: new Uint8Array([1, 2, 3])
22
- }],
23
- // a list of STUN and TURN servers. Critically important when opening webrtc conenctions behind NATs.
24
- iceServers: [{
25
- url: "stun.l.google.com",
26
- port: 19302
27
- }],
28
- // A range of ports that the node will attempt to start a WebSocket server in. If you wish to use an exact port give equal values to min and max. If left unspecified the node will start without a server.
29
- websocketPortRange: {
30
- min: 30000,
31
- max: 30500
32
- },
33
- ...
34
- })
35
- ```
36
-
37
- ## WebSocket Server TLS configuration
38
-
39
- Setting up TLS is important if you wish to allow nodes running in the browser to connect to your node over websocket. If left unconfigured webrtc connections will be used in such cases.
40
-
41
- There two ways to configure TLS for a DhtNode's WS server.
42
-
43
- ### Auto-certification
44
-
45
- When starting a node configure it to automatically fetch TLS certificates for a randomly generated domain name as such:
46
-
47
- ```js
48
- const DhtNode = new DhtNode({
49
- ...
50
- websocketServerEnableTls: true,
51
- autoCertifierUrl: 'http://example-autocertifier-url:30000/',
52
- autoCertifierConfigFile: '~/.streamr/certificate.json'
53
- ...
54
- })
55
- ```
56
-
57
- ### Custom TLS configuration
58
-
59
- If you have your own domain name and/or TLS certificates you can configure the node to use them as follows:
60
-
61
- ```js
62
- const DhtNode = new DhtNode({
63
- tlsCertificate: {
64
- certFileName: 'path/to/file'
65
- privateKeyFileName: 'path/to/file'
66
- },
67
- websocketHost: 'custom-domain.com' // If using a custom domain name give it here to ensure that connectivity checking is correctly pointed to the server
68
- })
69
- ```
70
-
71
- ### Setting up a custom DHT network
72
-
73
- To setup a custom DHT network outside the Streamr Networks Amoy or Polygon environments, you simply need to set up one or multiple entry point DhtNodes to the network and point all newly joining DhtNodes to them. It is also a good idea to configure the entry points to know each other. This makes it possible to restart them without causing network partitioning.
74
-
75
- ## Development
76
-
77
- ### local testing and develpment setup
78
-
79
- When running a node for testing or develpment a few configurations are important.
80
-
81
- ```js
82
- const DhtNode = new DhtNode({
83
- entryPoints: [], // Point this to your local dev entry point!
84
- iceServers: [], // Keep empty to ensure webrtc connections are local
85
- webrtcAllowPrivateAddresses: true, // Make sure that the value is true in local develpment
86
- websocketServerEnableTls: false, // Keep as false to ensure that auto-certification is not attempted
87
- websocketHost: '127.0.0.1', // Use 127.0.0.1 instead of localhost during develpment!
88
- })
89
- ```
90
-
91
- ### Generating Protobuf code
92
-
93
- After making changes to the protobuf definitions you should regenerate the protobuf code with the following command.
94
-
95
- ```bash
96
- npm run generate-protoc-code
97
- # Or
98
- ./proto.sh
99
- ```
100
-
101
- ## Running DHT simulations
102
-
103
- Generate test data
104
-
105
- ```bash
106
- npm run prepare-dht-simulation
107
- ```
108
-
109
- Run simulation
110
-
111
- ```bash
112
- npm run run-dht-simulation
113
- ```
114
-
115
- In order to change number of nodes, or other simulation settings,
116
-
117
- * Edit the chages to the file 'test/simulation/data/generatedhtids.ts'.
118
- * Then generate new test data by running 'npm run prepare-dht-simulation'
119
- * Edit the same changes to file 'src/simulation/DhtSimulation.ts'
120
- * Run the simulation with new settings using 'npm run run-dht-simulation '
package/jest.config.ts DELETED
@@ -1,12 +0,0 @@
1
- import type { Config } from '@jest/types'
2
- import defaultConfig from '../../jest.config'
3
-
4
- const config: Config.InitialOptions = {
5
- ...defaultConfig,
6
- setupFilesAfterEnv: [
7
- ...defaultConfig.setupFilesAfterEnv,
8
- './test/utils/customMatchers.ts',
9
- ],
10
- }
11
-
12
- export default config
package/karma-setup.js DELETED
@@ -1,2 +0,0 @@
1
- import * as customMatchers from './test/utils/customMatchers'
2
- window.expect.extend(customMatchers)
package/karma.config.js DELETED
@@ -1,35 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-require-imports */
2
- const path = require('path')
3
- const { createKarmaConfig, createWebpackConfig } = require('@streamr/browser-test-runner')
4
-
5
- const TEST_PATHS = [
6
- 'test/unit/**/!(connectivityRequestHandler*).ts',
7
- './test/integration/**/!(DhtWith*|ReplicateData*|GeoIpConnectivityChecking*).ts/',
8
- './test/end-to-end/**/!(RecoveryFromFailedAutoCertification*|memory-leak*|GeoIpLayer0*).ts'
9
- ]
10
-
11
- const NodeWebrtcConnection = path.resolve(__dirname, 'src/connection/webrtc/NodeWebrtcConnection.ts')
12
- const BrowserWebrtcConnection = path.resolve(__dirname, 'src/connection/webrtc/BrowserWebrtcConnection.ts')
13
- const NodeWebsocketClientConnection = path.resolve(__dirname, 'src/connection/websocket/NodeWebsocketClientConnection.ts')
14
- const BrowserWebsocketClientConnection = path.resolve(__dirname, 'src/connection/websocket/BrowserWebsocketClientConnection.ts')
15
-
16
- module.exports = createKarmaConfig(
17
- TEST_PATHS,
18
- createWebpackConfig({
19
- entry: './src/exports.ts',
20
- libraryName: 'dht',
21
- alias: {
22
- [NodeWebrtcConnection]: BrowserWebrtcConnection,
23
- [NodeWebsocketClientConnection]: BrowserWebsocketClientConnection
24
- },
25
- fallback: {
26
- module: false
27
- },
28
- externals: {
29
- http: 'HTTP',
30
- ws: 'WebSocket',
31
- 'node-datachannel': 'commonjs node-datachannel',
32
- }
33
- }),
34
- __dirname
35
- )
package/proto.sh DELETED
@@ -1,2 +0,0 @@
1
- mkdir -p ./generated
2
- npx protoc --ts_out ./generated --ts_opt server_generic,generate_dependencies,long_type_number --proto_path ../.. packages/dht/protos/DhtRpc.proto
@@ -1,317 +0,0 @@
1
- syntax = "proto3";
2
- option optimize_for = CODE_SIZE;
3
-
4
- package dht;
5
-
6
- import "google/protobuf/empty.proto";
7
- import "google/protobuf/any.proto";
8
- import "google/protobuf/timestamp.proto";
9
- import "packages/proto-rpc/protos/ProtoRpc.proto";
10
-
11
- service DhtNodeRpc {
12
- // TODO rename to getClosestNeighbors (breaking change)
13
- rpc getClosestPeers (ClosestPeersRequest) returns (ClosestPeersResponse);
14
- // TODO rename to getClosestRingContacts (breaking change)
15
- rpc getClosestRingPeers (ClosestRingPeersRequest) returns (ClosestRingPeersResponse);
16
- rpc ping (PingRequest) returns (PingResponse);
17
- rpc leaveNotice (LeaveNotice) returns (google.protobuf.Empty);
18
- }
19
-
20
- service RouterRpc {
21
- rpc routeMessage (RouteMessageWrapper) returns (RouteMessageAck);
22
- rpc forwardMessage (RouteMessageWrapper) returns (RouteMessageAck);
23
- }
24
-
25
- service RecursiveOperationRpc {
26
- rpc routeRequest (RouteMessageWrapper) returns (RouteMessageAck);
27
- }
28
-
29
- service StoreRpc {
30
- rpc storeData (StoreDataRequest) returns (StoreDataResponse);
31
- rpc replicateData (ReplicateDataRequest) returns (google.protobuf.Empty);
32
- }
33
-
34
- service RecursiveOperationSessionRpc {
35
- rpc sendResponse(RecursiveOperationResponse) returns (google.protobuf.Empty);
36
- }
37
-
38
- service WebsocketClientConnectorRpc {
39
- rpc requestConnection (WebsocketConnectionRequest) returns (google.protobuf.Empty);
40
- }
41
-
42
- service WebrtcConnectorRpc {
43
- rpc requestConnection (WebrtcConnectionRequest) returns (google.protobuf.Empty);
44
- rpc rtcOffer (RtcOffer) returns (google.protobuf.Empty);
45
- rpc rtcAnswer (RtcAnswer) returns (google.protobuf.Empty);
46
- rpc iceCandidate (IceCandidate) returns (google.protobuf.Empty);
47
- }
48
-
49
- service ConnectionLockRpc {
50
- rpc lockRequest (LockRequest) returns (LockResponse);
51
- rpc unlockRequest (UnlockRequest) returns (google.protobuf.Empty);
52
- rpc gracefulDisconnect (DisconnectNotice) returns (google.protobuf.Empty);
53
- rpc setPrivate (SetPrivateRequest) returns (google.protobuf.Empty);
54
- }
55
-
56
- service ExternalApiRpc {
57
- rpc externalFetchData (ExternalFetchDataRequest) returns (ExternalFetchDataResponse);
58
- rpc externalStoreData (ExternalStoreDataRequest) returns (ExternalStoreDataResponse);
59
- }
60
-
61
- // Used inside RpcMessage
62
-
63
- message StoreDataRequest {
64
- bytes key = 1;
65
- google.protobuf.Any data = 2;
66
- bytes creator = 3;
67
- google.protobuf.Timestamp createdAt = 4;
68
- uint32 ttl = 5;
69
- }
70
-
71
- message StoreDataResponse {
72
- }
73
-
74
- message ExternalStoreDataRequest {
75
- bytes key = 1;
76
- google.protobuf.Any data = 2;
77
- }
78
-
79
- message ExternalStoreDataResponse {
80
- repeated PeerDescriptor storers = 1;
81
- }
82
-
83
- message ReplicateDataRequest {
84
- DataEntry entry = 1;
85
- }
86
-
87
- message DataEntry {
88
- bytes key = 1;
89
- google.protobuf.Any data = 2;
90
- bytes creator = 3;
91
- google.protobuf.Timestamp createdAt = 4;
92
- google.protobuf.Timestamp storedAt = 5;
93
- uint32 ttl = 6; // milliseconds
94
- bool stale = 7;
95
- bool deleted = 8;
96
- }
97
-
98
- // TODO rename to ClosestNeighborsRequest
99
- message ClosestPeersRequest {
100
- bytes nodeId = 1;
101
- string requestId = 2;
102
- }
103
-
104
- // TODO rename to ClosestNeighborsResponse
105
- message ClosestPeersResponse {
106
- repeated PeerDescriptor peers = 1;
107
- string requestId = 2;
108
- }
109
-
110
- // TODO rename to ClosestRingContactsRequest
111
- message ClosestRingPeersRequest {
112
- bytes ringId = 1;
113
- string requestId = 2;
114
- }
115
-
116
- // TODO rename to ClosestRingContactsResponse
117
- message ClosestRingPeersResponse {
118
- repeated PeerDescriptor leftPeers = 1;
119
- repeated PeerDescriptor rightPeers = 2;
120
- string requestId = 3;
121
- }
122
-
123
- message RecursiveOperationRequest {
124
- string sessionId = 1;
125
- RecursiveOperation operation = 2;
126
- }
127
-
128
- enum RecursiveOperation {
129
- FIND_CLOSEST_NODES = 0;
130
- FETCH_DATA = 1;
131
- DELETE_DATA = 2;
132
- }
133
-
134
- message RecursiveOperationResponse {
135
- repeated PeerDescriptor closestConnectedNodes = 1;
136
- repeated DataEntry dataEntries = 2;
137
- bool noCloserNodesFound = 3;
138
- repeated PeerDescriptor routingPath = 4;
139
- }
140
-
141
- message PingRequest {
142
- string requestId = 1;
143
- }
144
-
145
- message PingResponse {
146
- string requestId = 1;
147
- }
148
-
149
- message LeaveNotice {
150
- }
151
-
152
- message PeerDescriptor {
153
- bytes nodeId = 1;
154
- NodeType type = 2;
155
- ConnectivityMethod udp = 3;
156
- ConnectivityMethod tcp = 4;
157
- ConnectivityMethod websocket = 5;
158
- optional uint32 region = 6;
159
- optional uint32 ipAddress = 7;
160
- optional bytes publicKey = 8;
161
- // signature of fields 2-8
162
- optional bytes signature = 9;
163
- }
164
-
165
- message ConnectivityMethod {
166
- uint32 port = 1;
167
- string host = 2;
168
- bool tls = 3;
169
- }
170
-
171
- enum NodeType {
172
- NODEJS = 0;
173
- BROWSER = 1;
174
- }
175
-
176
- enum RpcResponseError {
177
- SERVER_TIMOUT = 0;
178
- CLIENT_TIMEOUT = 1;
179
- SERVER_ERROR = 2;
180
- UNKNOWN_RPC_METHOD = 3;
181
- }
182
-
183
- message RouteMessageWrapper {
184
- string requestId = 1;
185
- PeerDescriptor sourcePeer = 2;
186
- bytes target = 3;
187
- Message message = 4;
188
- repeated PeerDescriptor reachableThrough = 5;
189
- repeated PeerDescriptor routingPath = 6;
190
- repeated string parallelRootNodeIds = 7;
191
- }
192
-
193
- message RouteMessageAck {
194
- string requestId = 1;
195
- optional RouteMessageError error = 2;
196
- }
197
-
198
- enum RouteMessageError {
199
- NO_TARGETS = 0;
200
- DUPLICATE = 1;
201
- // TODO: can this be removed? If DhtNode is already stopped the server side requests
202
- // should not be processed
203
- STOPPED = 2;
204
- }
205
-
206
- message ConnectivityRequest {
207
- uint32 port = 1;
208
- bool tls = 2;
209
- optional string host = 3;
210
- bool allowSelfSignedCertificate = 4;
211
- }
212
-
213
- message ConnectivityResponse {
214
- string host = 1;
215
- string natType = 2;
216
- ConnectivityMethod websocket = 3;
217
- uint32 ipAddress = 4;
218
- string protocolVersion = 5;
219
- optional double latitude = 6;
220
- optional double longitude = 7;
221
- }
222
-
223
- message HandshakeRequest {
224
- PeerDescriptor sourcePeerDescriptor = 1;
225
- optional PeerDescriptor targetPeerDescriptor = 2;
226
- string protocolVersion = 3;
227
- string applicationVersion = 4;
228
- }
229
-
230
- message HandshakeResponse {
231
- PeerDescriptor sourcePeerDescriptor = 1;
232
- optional HandshakeError error = 2;
233
- string protocolVersion = 3;
234
- string applicationVersion = 4;
235
- }
236
-
237
- enum HandshakeError {
238
- DUPLICATE_CONNECTION = 0;
239
- INVALID_TARGET_PEER_DESCRIPTOR = 1;
240
- UNSUPPORTED_PROTOCOL_VERSION = 2;
241
- }
242
-
243
- // Wraps all messages
244
-
245
- message Message {
246
- string messageId = 1;
247
- PeerDescriptor sourceDescriptor = 2;
248
- PeerDescriptor targetDescriptor = 3;
249
- string serviceId = 4; // id of the RPC service
250
- oneof body {
251
- protorpc.RpcMessage rpcMessage = 5;
252
- ConnectivityRequest connectivityRequest = 6;
253
- ConnectivityResponse connectivityResponse = 7;
254
- HandshakeRequest handshakeRequest = 8;
255
- HandshakeResponse handshakeResponse = 9;
256
- RecursiveOperationRequest recursiveOperationRequest = 10;
257
- };
258
- }
259
-
260
- // Connector Messages
261
-
262
- // WebSocket
263
- message WebsocketConnectionRequest {
264
- }
265
-
266
- // WebRTC
267
- message WebrtcConnectionRequest {
268
- }
269
-
270
- message RtcOffer {
271
- string description = 1;
272
- string connectionId = 2;
273
- }
274
-
275
- message RtcAnswer {
276
- string description = 1;
277
- string connectionId = 2;
278
- }
279
-
280
- message IceCandidate {
281
- string candidate = 1;
282
- string mid = 2;
283
- string connectionId = 3;
284
- }
285
-
286
- message LockRequest {
287
- string lockId = 1;
288
- }
289
-
290
- message UnlockRequest {
291
- string lockId = 1;
292
- }
293
-
294
- message LockResponse {
295
- bool accepted = 1;
296
- }
297
-
298
- enum DisconnectMode {
299
- NORMAL = 0;
300
- LEAVING = 1;
301
- }
302
-
303
- message DisconnectNotice {
304
- DisconnectMode disconnectMode = 1;
305
- }
306
-
307
- message SetPrivateRequest {
308
- bool isPrivate = 1;
309
- }
310
-
311
- message ExternalFetchDataRequest {
312
- bytes key = 1;
313
- }
314
-
315
- message ExternalFetchDataResponse {
316
- repeated DataEntry entries = 1;
317
- }
@@ -1,16 +0,0 @@
1
-
2
- syntax = "proto3";
3
- option optimize_for = CODE_SIZE;
4
-
5
- package tests;
6
-
7
- import "google/protobuf/any.proto";
8
-
9
- message TestMessage {
10
- string messageId = 1;
11
- google.protobuf.Any body = 2;
12
- }
13
-
14
- message SomeMessage {
15
- string juttu = 1;
16
- }
@@ -1,13 +0,0 @@
1
- #!/bin/bash
2
-
3
- mkdir -p dist
4
-
5
- SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
6
-
7
- cd "${SCRIPT_DIR}/.."
8
-
9
- # Sanitize the final package.json
10
- npx tsx scripts/rewrite-package.ts
11
-
12
- # Copy assets
13
- cp -f README.md dist
@@ -1,45 +0,0 @@
1
- /**
2
- * This script moves the package.json file into the dist folder while adjusting
3
- * relative pathnames for exports. Since the compiled output is inside "dist",
4
- * but the original package.json references paths as if it's in the root, we
5
- * need to strip "dist" from all export fields (main, types, …).
6
- *
7
- * This ensures that consumers of the package import the correct files
8
- * without referencing "dist" in their paths, maintaining proper module resolution.
9
- */
10
-
11
- import pkg from '../package.json' assert { type: 'json' }
12
- import * as fs from 'node:fs'
13
- import path, { dirname } from 'node:path'
14
- import { fileURLToPath } from 'url'
15
-
16
- function fixPathname(pathname: string): string {
17
- return pathname.startsWith('./dist')
18
- ? `./${path.relative('./dist', pathname)}`
19
- : pathname
20
- }
21
-
22
- const { main, types, browser, scripts: _scripts, ...rest } = pkg
23
-
24
- const newPkg = {
25
- ...rest,
26
- main: fixPathname(main),
27
- types: fixPathname(types),
28
- browser: Object.entries(browser).reduce(
29
- (memo, [fromPathname, toPathname]) => ({
30
- ...memo,
31
- [fixPathname(fromPathname)]:
32
- typeof toPathname === 'string'
33
- ? fixPathname(toPathname)
34
- : toPathname,
35
- }),
36
- {}
37
- ),
38
- }
39
-
40
- const dist = path.resolve(
41
- dirname(fileURLToPath(import.meta.url)),
42
- '../dist/package.json'
43
- )
44
-
45
- fs.writeFileSync(dist, JSON.stringify(newPkg, null, 2))