Skip to content

Commit d4e0545

Browse files
committed
convert to TS
1 parent f4241db commit d4e0545

28 files changed

+858
-798
lines changed

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ LICENSE
99
.browserslistrc
1010
fixtures/files
1111
example/files.js
12-
pnpm-lock.yaml
12+
pnpm-lock.yaml
13+
test/fixtures.json

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
22
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
33

4-
## Table of Contents
5-
64
- [Conflux](#conflux)
75
- [Blazing Fast](#blazing-fast)
86
- [Compatibility](#compatibility)
97
- [Examples](#examples)
108
- [Usage](#usage)
119
- [Importing Conflux](#importing-conflux)
1210
- [Creating a ZIP](#creating-a-zip)
13-
- [Example using `ReadableStream#pipeThrough`](#example-using-readablestreampipethrough)
14-
- [Example using `writer.write`](#example-using-writerwrite)
15-
- [Incorporating other streams](#incorporating-other-streams)
1611
- [Reading ZIP files](#reading-zip-files)
1712
- [Supporting Legacy Browsers](#supporting-legacy-browsers)
1813
- [License](#license)

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const eslintConfig = tseslint.config(
3131
],
3232
'unicorn/no-null': 'off',
3333
'unicorn/no-nested-ternary': 'off',
34+
'unicorn/number-literal-case': 'off',
3435
'@typescript-eslint/no-unnecessary-condition': [
3536
'error',
3637
{

knip.config.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
import type { KnipConfig } from 'knip';
22

33
const config: KnipConfig = {
4-
entry: [
5-
'src/index.ts!',
6-
'src/api.ts!', // knip needs a little help getting past the actual entrypoint
7-
'src/worker.penumbra.ts!',
8-
'tests/*.test.ts',
9-
'web-test-runner.config.js',
10-
],
11-
project: ['src/**', 'fixtures/**', 'tests/**'],
12-
ignore: [
13-
// Vendored third-party library has some unused exports
14-
'src/utils/brand.ts',
15-
'src/**/*.d.ts',
16-
],
17-
ignoreDependencies: ['@web/test-runner-playwright'],
4+
entry: ['src/index.ts!', 'test/**/*.test.ts', 'web-test-runner.config.js'],
5+
project: ['src/**/*.ts', 'test/**/*.ts'],
6+
ignoreDependencies: ['@web/test-runner-playwright', 'tsx'],
187
// Bypass unlisted binaries "scripts/postbuild.ts package.json"]
198
exclude: ['binaries'],
209
};

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
"unpkg": "dist/conflux.umd.min.js",
99
"browser": "dist/conflux.esm.js",
1010
"module": "dist/conflux.esm.js",
11+
"types": "dist/types/index.d.ts",
1112
"scripts": {
12-
"build": "rollup -c --bundleConfigAsCjs",
13-
"test": "scripts/run-tests.sh",
14-
"test:chromium": "scripts/run-tests.sh --browsers chromium",
13+
"build": "rollup --config rollup.config.ts --configPlugin typescript",
14+
"test": "pnpm exec web-test-runner --files test/**/*.test.ts --node-resolve --playwright --coverage --browsers chromium firefox webkit",
1515
"lint": "pnpm run \"/^lint:.*/\"",
1616
"lint:prettier": "prettier --check . --log-level warn",
1717
"lint:eslint": "eslint .",
1818
"lint:types": "tsc --build",
1919
"lint:knip": "knip",
2020
"format": "prettier --write . --log-level warn && doctoc README.md --maxlevel 3",
21-
"rebuild-fixtures": "tsx test/build.js",
21+
"rebuild-fixtures": "tsx scripts/rebuild-fixtures.ts",
2222
"prepublishOnly": "pnpm lint && pnpm run build"
2323
},
2424
"repository": {
@@ -69,9 +69,11 @@
6969
"@eslint/js": "^9.32.0",
7070
"@esm-bundle/chai": "4.3.4-fix.0",
7171
"@rollup/plugin-node-resolve": "^15.2.3",
72+
"@rollup/plugin-typescript": "^12.1.4",
7273
"@tsconfig/strictest": "^2.0.5",
7374
"@types/mocha": "^10.0.10",
7475
"@types/node": "^22.17.1",
76+
"@types/pako": "^2.0.3",
7577
"@web/dev-server-esbuild": "^1.0.4",
7678
"@web/test-runner": "^0.20.2",
7779
"@web/test-runner-playwright": "^0.11.1",

pnpm-lock.yaml

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import resolve from '@rollup/plugin-node-resolve';
2-
import pkg from './package.json' with { type: 'json' };
1+
import { nodeResolve } from '@rollup/plugin-node-resolve';
2+
import typescript from '@rollup/plugin-typescript';
3+
import packageJson from './package.json' with { type: 'json' };
34

45
export default [
56
// browser-friendly UMD build
67
{
7-
input: 'src/index.js',
8+
input: 'src/index.ts',
89
output: [
910
{
1011
name: 'conflux',
11-
file: pkg.main,
12+
file: packageJson.main,
1213
format: 'umd',
1314
},
1415
],
1516
plugins: [
16-
resolve(), // so Rollup can find package dependencies
17+
nodeResolve(), // so Rollup can find package dependencies
18+
typescript(),
1719
],
1820
},
1921

@@ -24,11 +26,12 @@ export default [
2426
// an array for the `output` option, where we can specify
2527
// `file` and `format` for each target)
2628
{
27-
input: 'src/index.js',
29+
input: 'src/index.ts',
2830
external: [/pako/],
2931
output: [
3032
// { file: pkg.main, format: 'cjs' }, // don't need a Node import yet
31-
{ file: pkg.module, format: 'es' },
33+
{ file: packageJson.module, format: 'es' },
3234
],
35+
plugins: [typescript()],
3336
},
3437
];

scripts/rebuild-fixtures.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
3+
import jsonBinaryBlobCodec from '../test/json-binary-blob-codec.js';
4+
5+
const { replacer } = jsonBinaryBlobCodec;
6+
7+
const pathname = new URL(import.meta.url).pathname;
8+
const __dirname = path.dirname(pathname);
9+
10+
const files = await fs.readdir(path.resolve(__dirname, '../test/fixture'));
11+
const fixtures: Record<string, Uint8Array> = {};
12+
for (const file of files) {
13+
if (file.endsWith('.zip') || file.endsWith('.gif')) {
14+
fixtures[file] = new Uint8Array(
15+
await fs.readFile(path.resolve(__dirname, '../test/fixture', file)),
16+
);
17+
}
18+
}
19+
const replaced = JSON.stringify(fixtures, replacer, 2);
20+
await fs.writeFile(path.resolve(__dirname, '../test/fixtures.json'), replaced);

src/bigint.js

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

src/bigint.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const jsbiShim = {
2+
// constructor
3+
BigInt: BigInt,
4+
5+
// note: JSBI toString is already the same: a.toString()
6+
toNumber: Number,
7+
8+
// binary functions to expressions
9+
add: (a: bigint, b: bigint) => a + b,
10+
subtract: (a: bigint, b: bigint) => a - b,
11+
multiply: (a: bigint, b: bigint) => a * b,
12+
divide: (a: bigint, b: bigint) => a / b,
13+
remainder: (a: bigint, b: bigint) => a % b,
14+
exponentiate: (a: bigint, b: bigint) => a ** b,
15+
leftShift: (a: bigint, b: bigint) => a << b,
16+
signedRightShift: (a: bigint, b: bigint) => a >> b,
17+
bitwiseAnd: (a: bigint, b: bigint) => a & b,
18+
bitwiseOr: (a: bigint, b: bigint) => a | b,
19+
bitwiseXor: (a: bigint, b: bigint) => a ^ b,
20+
equal: (a: bigint, b: bigint) => a === b,
21+
notEqual: (a: bigint, b: bigint) => a !== b,
22+
lessThan: (a: bigint, b: bigint) => a < b,
23+
lessThanOrEqual: (a: bigint, b: bigint) => a <= b,
24+
greaterThan: (a: bigint, b: bigint) => a > b,
25+
greaterThanOrEqual: (a: bigint, b: bigint) => a >= b,
26+
EQ: (a: bigint, b: bigint) => a == b,
27+
NE: (a: bigint, b: bigint) => a != b,
28+
LT: (a: bigint, b: bigint) => a < b,
29+
LE: (a: bigint, b: bigint) => a <= b,
30+
GT: (a: bigint, b: bigint) => a > b,
31+
GE: (a: bigint, b: bigint) => a >= b,
32+
ADD: (a: bigint, b: bigint) => a + b,
33+
34+
// unary functions to expressions
35+
unaryMinus: (a: bigint) => -a,
36+
bitwiseNot: (a: bigint) => ~a,
37+
38+
// static methods
39+
asIntN: (a: number, b: bigint) => BigInt.asIntN(a, b),
40+
asUintN: (a: number, b: bigint) => BigInt.asUintN(a, b),
41+
};
42+
43+
type JSBI = typeof jsbiShim;
44+
45+
declare global {
46+
interface Window {
47+
JSBI?: JSBI;
48+
}
49+
}
50+
51+
if (!(self.BigInt as unknown) && !self.JSBI) {
52+
throw new Error(
53+
'BigInt is not supported in this browser.' +
54+
' Conflux is unable to create zip files without BigInt support, or a JSBI polyfill.',
55+
);
56+
}
57+
58+
/**
59+
* Use JSBI syntax for BigInt operations, instead of calling BigInt directly.
60+
*
61+
* This is NOT a polyfill. It uses native BigInt by default. Using this syntax simply makes it _possible_ to polyfill BigInt.
62+
* If BigInt is not natively supported (ES2020+), library consumer MUST expose globalThis.JSBI before using Conflux.
63+
*
64+
* @see https://github.com/GoogleChromeLabs/jsbi - JSBI library for why BigInt
65+
* @see https://github.com/GoogleChromeLabs/jsbi/blob/master/jsbi.d.ts - types
66+
*/
67+
const jsbi: JSBI =
68+
!(self.BigInt as unknown) && self.JSBI ? self.JSBI : jsbiShim;
69+
70+
export default jsbi;

0 commit comments

Comments
 (0)