Skip to content

Commit c044891

Browse files
committed
chore: finish ESM migration
1 parent c080c8a commit c044891

10 files changed

+48
-54
lines changed

.idea/krist.js.iml

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+10-8
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
"license": "GPL-3.0",
1212
"private": false,
1313
"type": "module",
14+
"main": "lib/cjs/src/node.js",
1415
"exports": {
1516
".": {
16-
"import": "./lib/src/node.js",
17-
"default": "./lib/src/node.js",
17+
"import": "./lib/esm/src/node.js",
18+
"default": "./lib/esm/src/node.js",
19+
"require": "./lib/cjs/src/node.js",
1820
"browser": "./lib/browser.js",
19-
"und": "./lib/browser.js"
21+
"umd": "./lib/browser.js"
2022
}
2123
},
22-
"types": "lib/src/node.d.ts",
24+
"types": "lib/esm/src/node.d.ts",
2325
"files": [
2426
"lib/**/*.{ts,js}",
2527
"README.md",
@@ -33,8 +35,9 @@
3335
},
3436
"scripts": {
3537
"clean": "rimraf lib",
36-
"build": "yarn run clean && yarn run build:node && yarn run build:browser",
37-
"build:node": "tsc -p tsconfig.json",
38+
"build": "yarn run clean && yarn run build:node-esm && yarn run build:node-cjs && yarn run build:browser",
39+
"build:node-esm": "tsc -p tsconfig.json",
40+
"build:node-cjs": "tsc -p tsconfig.cjs.json",
3841
"build:browser": "rollup --config rollup.config.js",
3942
"test": "cross-env TSX_TSCONFIG_PATH='./tsconfig.test.json' NODE_ENV=test mocha",
4043
"test:coverage": "nyc yarn run test",
@@ -46,8 +49,7 @@
4649
"@aws-crypto/sha256-js": "^5.2.0",
4750
"axios": "^1.6.7",
4851
"buffer": "^6.0.3",
49-
"limiter": "^2.1.0",
50-
"limiter-es6-compat": "^2.1.2",
52+
"rate-limiter-flexible": "^4.0.1",
5153
"tiny-typed-emitter": "^2.1.0",
5254
"ws": "^8.16.0",
5355
"wspromisify": "^2.4.4"

rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default [
5656
},
5757
{
5858
// Declarations: browser.d.ts
59-
input: "lib/src/browser.d.ts",
59+
input: "lib/esm/src/browser.d.ts",
6060
output: [{ file: "lib/browser.d.ts", format: "es" }],
6161
plugins: [pluginDts()]
6262
},

src/api/KristApi.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
* For more project information, see <https://github.com/tmpim/Krist.js>.
2020
*/
2121

22-
import rateLimiter from "limiter";
22+
import RateLimiterMemory from "rate-limiter-flexible/lib/RateLimiterMemory.js";
2323
import os from "os";
2424

2525
import { KristApiResponse } from "../types/index.js";
2626

27-
import packageJson from "../../package.json" with { "type": "json" };
2827
import { coerceKristError, KristErrorRateLimit } from "../errors/index.js";
2928
import { argObject, argStringNonEmpty } from "../util/argValidation.js";
3029
import { isNil } from "../util/internalUtil.js";
@@ -74,10 +73,10 @@ export const buildBody = (value: any): RequestInit => ({
7473
});
7574

7675
// Global rate limiter so that multiple KristApi instances will share this
77-
const limiter = new rateLimiter.RateLimiter({
76+
const limiter = new RateLimiterMemory({
7877
// Slightly lower than server to account for timing differences
79-
tokensPerInterval: 300,
80-
interval: "minute"
78+
points: 300, // 300 requests per minute
79+
duration: 60
8180
});
8281

8382
/** Client class for the Krist API. {@link KristApiOptions | Options} may be
@@ -97,7 +96,7 @@ export class KristApi {
9796
*/
9897
constructor({
9998
syncNode = "https://krist.dev/",
100-
userAgent = "krist.js/" + packageJson.version,
99+
userAgent = "krist.js",
101100
}: KristApiOptions = {}) {
102101
argStringNonEmpty(syncNode, "syncNode");
103102
argStringNonEmpty(userAgent, "userAgent");
@@ -139,10 +138,10 @@ export class KristApi {
139138
const url = new URL(endpoint.replace(/^\//, "") + qss, this.syncNode);
140139

141140
// Apply rate limiting
142-
await limiter.removeTokens(1);
141+
await limiter.consume("krist-api", 1);
143142

144143
const browserHeaders: any = typeof window !== "undefined" ? {
145-
"Library-Agent": "krist.js/" + packageJson.version,
144+
"Library-Agent": "krist.js",
146145
} : {};
147146
const nodeHeaders: any = typeof window === "undefined" ? {
148147
"Library-Agent": `${this.userAgent} (${os.platform()} ${os.release()}; Node ${process.version})`,
@@ -151,7 +150,7 @@ export class KristApi {
151150
const res = await fetch(url.toString(), {
152151
method,
153152
headers: {
154-
"Library-Agent": "krist.js/" + packageJson.version,
153+
"Library-Agent": "krist.js",
155154
"Accept": "application/json",
156155
...browserHeaders,
157156
...nodeHeaders,

src/api/ws/KristWsClient.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { getWork } from "./routes/wsWork.js";
4040
import NodeWebSocket from "ws";
4141

4242
import { TypedEmitter } from "tiny-typed-emitter";
43-
import rateLimiter from "limiter";
43+
import RateLimiterMemory from "rate-limiter-flexible/lib/RateLimiterMemory.js";
4444

4545
import { KristWalletFormatName } from "../../util/walletFormats.js";
4646
import { Sha256Fn } from "../../util/internalCrypto.js";
@@ -122,9 +122,9 @@ export interface KristWsClientOptions extends KristWsClientOptionsPassword,
122122
}
123123

124124
// Global rate limiter so that multiple KristApi instances will share this
125-
const limiter = new rateLimiter.RateLimiter({
126-
tokensPerInterval: 120,
127-
interval: "minute"
125+
const limiter = new RateLimiterMemory({
126+
points: 120, // 120 requests per minute
127+
duration: 60
128128
});
129129

130130
/**
@@ -415,7 +415,7 @@ export class KristWsClient extends TypedEmitter<KristWsClientEvents> {
415415
if (typeof msg.id !== "number") return;
416416

417417
// Apply rate limiting
418-
await limiter.removeTokens(1);
418+
await limiter.consume("krist-ws", 1);
419419

420420
// TODO: Catch here
421421
this.ws?.send(JSON.stringify(msg));

src/util/walletFormats.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ export type KristWalletFormatName = "kristwallet"
3232
| "jwalelset" | "api";
3333

3434
export const KRIST_WALLET_FORMATS: Record<KristWalletFormatName, KristWalletFormat> = {
35-
"kristwallet": async (sha256Fn = sha256, password) =>
35+
"kristwallet": async (sha256Fn, password) =>
3636
await sha256Fn("KRISTWALLET" + password) + "-000",
3737

38-
"kristwallet_username_appendhashes": async (sha256Fn = sha256, password, username) =>
38+
"kristwallet_username_appendhashes": async (sha256Fn, password, username) =>
3939
await sha256Fn(
4040
"KRISTWALLETEXTENSION"
4141
+ await sha256Fn(
@@ -45,14 +45,14 @@ export const KRIST_WALLET_FORMATS: Record<KristWalletFormatName, KristWalletForm
4545
)
4646
) + "-000",
4747

48-
"kristwallet_username": async (sha256Fn = sha256, password, username) =>
48+
"kristwallet_username": async (sha256Fn, password, username) =>
4949
await sha256Fn(
5050
await sha256Fn(username || "")
5151
+ "^"
5252
+ await sha256Fn(password)
5353
),
5454

55-
"jwalelset": async (sha256Fn = sha256, password) =>
55+
"jwalelset": async (sha256Fn, password) =>
5656
await sha256Fn(await sha256Fn(await sha256Fn(await sha256Fn(
5757
await sha256Fn(await sha256Fn(await sha256Fn(await sha256Fn(
5858
await sha256Fn(await sha256Fn(await sha256Fn(await sha256Fn(

tsconfig.cjs.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"declaration": false,
7+
"outDir": "lib/cjs"
8+
}
9+
}

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"downlevelIteration": true,
1717
"resolveJsonModule": true,
1818
"stripInternal": true,
19-
"outDir": "lib",
19+
"outDir": "lib/esm",
2020
"rootDir": "."
2121
},
2222
"include": [
23-
"src/**/*"
23+
"src/**/*", "typings/**/*"
2424
]
2525
}

tsconfig.test.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"include": [
77
"src/**/*",
8+
"typings/**/*",
89
"test/**/*.ts"
910
]
1011
}

yarn.lock

+5-24
Original file line numberDiff line numberDiff line change
@@ -2248,16 +2248,6 @@ just-extend@^6.2.0:
22482248
resolved "https://registry.yarnpkg.com/just-extend/-/just-extend-6.2.0.tgz#b816abfb3d67ee860482e7401564672558163947"
22492249
integrity sha512-cYofQu2Xpom82S6qD778jBDpwvvy39s1l/hrYij2u9AMdQcGRpaBu6kY4mVhuno5kJVi1DAz4aiphA2WI1/OAw==
22502250

2251-
2252-
version "4.3.2"
2253-
resolved "https://registry.yarnpkg.com/just-performance-es6-compat/-/just-performance-es6-compat-4.3.2.tgz#7877f34cbe25fd994bc2265d0c919bf728dcc42e"
2254-
integrity sha512-bNPuqRaV8PrqYVivdN8FsKi0rmXaQ0Y63oasFUQFJooJPH0r26LdfBAfoodhUQI6I6cek9yNo+tnGiF0R3pIHw==
2255-
2256-
2257-
version "4.3.0"
2258-
resolved "https://registry.yarnpkg.com/just-performance/-/just-performance-4.3.0.tgz#cc2bc8c9227f09e97b6b1df4cd0de2df7ae16db1"
2259-
integrity sha512-L7RjvtJsL0QO8xFs5wEoDDzzJwoiowRw6Rn/GnvldlchS2JQr9wFYPiwZcDfrbbujEKqKN0tvENdbjXdYhDp5Q==
2260-
22612251
keyv@^4.5.3:
22622252
version "4.5.4"
22632253
resolved "https://registry.yarnpkg.com/keyv/-/keyv-4.5.4.tgz#a879a99e29452f942439f2a405e3af8b31d4de93"
@@ -2273,20 +2263,6 @@ levn@^0.4.1:
22732263
prelude-ls "^1.2.1"
22742264
type-check "~0.4.0"
22752265

2276-
limiter-es6-compat@^2.1.2:
2277-
version "2.1.2"
2278-
resolved "https://registry.yarnpkg.com/limiter-es6-compat/-/limiter-es6-compat-2.1.2.tgz#93fdc5fadf5f220f81ffdfc4f8b2b976cc876f98"
2279-
integrity sha512-rAi6LCOubXfnojSePboCylTSJWi9m0hlnZJ6AYsBv0clRFr4PJAZd8DtDVhXSN5Ed5nXa6pULHu9sJxoxpWWww==
2280-
dependencies:
2281-
just-performance-es6-compat "4.3.2"
2282-
2283-
limiter@^2.1.0:
2284-
version "2.1.0"
2285-
resolved "https://registry.yarnpkg.com/limiter/-/limiter-2.1.0.tgz#d38d7c5b63729bb84fb0c4d8594b7e955a5182a2"
2286-
integrity sha512-361TYz6iay6n+9KvUUImqdLuFigK+K79qrUtBsXhJTLdH4rIt/r1y8r1iozwh8KbZNpujbFTSh74mJ7bwbAMOw==
2287-
dependencies:
2288-
just-performance "4.3.0"
2289-
22902266
locate-path@^5.0.0:
22912267
version "5.0.0"
22922268
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"
@@ -2827,6 +2803,11 @@ randombytes@^2.1.0:
28272803
dependencies:
28282804
safe-buffer "^5.1.0"
28292805

2806+
rate-limiter-flexible@^4.0.1:
2807+
version "4.0.1"
2808+
resolved "https://registry.yarnpkg.com/rate-limiter-flexible/-/rate-limiter-flexible-4.0.1.tgz#79b0ce111abe9c5da41d6fddf7cca93cedd3a8fc"
2809+
integrity sha512-2/dGHpDFpeA0+755oUkW+EKyklqLS9lu0go9pDsbhqQjZcxfRyJ6LA4JI0+HAdZ2bemD/oOjUeZQB2lCZqXQfQ==
2810+
28302811
react-is@^16.13.1:
28312812
version "16.13.1"
28322813
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"

0 commit comments

Comments
 (0)