Skip to content

Commit

Permalink
Add browser tests (#811)
Browse files Browse the repository at this point in the history
* Add browser tests

* Fix windows build

* Update doc

* Update changelog
  • Loading branch information
kigawas authored Nov 5, 2024
1 parent c7ea88f commit a2eaacb
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 123 deletions.
35 changes: 26 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Set .npmrc for windows
run: cp .npmrc.windows ./tests-browser/.npmrc
if: matrix.os == 'windows-latest'

- name: Set up browser tests
working-directory: tests-browser
run: pnpm install && pnpm exec playwright install chromium firefox

- name: Install webkit browser on macos
working-directory: tests-browser
if: matrix.os == 'macos-latest'
run: pnpm exec playwright install

- name: Run browser tests (chromium, firefox)
run: |
pnpm test:browser
pnpm test:browser -- --browser=firefox
- name: Run browser tests (webkit)
if: matrix.os == 'macos-latest'
run: |
pnpm test:browser -- --browser=webkit
check-runtimes:
runs-on: ubuntu-latest
steps:
Expand All @@ -54,18 +77,12 @@ jobs:

- name: check main.js
run: |
bun run example/runtime/main.js
deno run --allow-read example/runtime/main.js
node example/runtime/main.js
./scripts/check-runtime.sh example/runtime/main.js
- name: check import.js
run: |
bun run example/runtime/import.js
deno run --allow-read example/runtime/import.js
node example/runtime/import.js
./scripts/check-runtime.sh example/runtime/import.js
- name: check require.cjs
run: |
bun run example/runtime/require.cjs
deno run --allow-read example/runtime/require.cjs
node example/runtime/require.cjs
./scripts/check-runtime.sh example/runtime/require.cjs
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ bun.lockb
deno.lock
example/browser/pnpm-lock.yaml
example/runtime/pnpm-lock.yaml

# browser tests
tests-browser/pnpm-lock.yaml
tests-browser/**/*.test.ts
3 changes: 3 additions & 0 deletions .npmrc.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# symlink does not work on windows for vite in browsers
# see: https://github.com/vitejs/vite/issues/10802
node-linker=hoisted
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Revamp encapsulate/decapsulate
- Revamp symmetric encryption/decryption
- Revamp elliptic utils
- Add browser tests

## 0.4.10

Expand Down Expand Up @@ -40,14 +41,16 @@
- Bump dependencies
- Drop Node 14 support

## 0.3.1 ~ 0.3.18
## 0.3.1 ~ 0.3.21

- Revamp tests
- Support Node 18, 20
- Drop Node 10, 12 support
- Bump dependencies
- Update documentation
- Extract constant variables and rename some parameters
- Revamp CI
- Migrate to vitest

## 0.3.0

Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,25 @@ hello world🌍

See [Configuration](#configuration) to control with more granularity.

## Browser Support
## Multi-platform Support

### Browser

This library is browser-friendly, check the [`example/browser`](./example/browser) directory for details. Currently it's necessary to polyfill `Buffer` for backward compatibility. From v0.5.0, it can run in browsers as is.

If you want a WASM version to run directly in modern browsers or on some blockchains, you can also try [`ecies-wasm`](https://github.com/ecies/rs-wasm).

### Bun/Deno

For bun/deno, see [`example/runtime`](./example/runtime). There are some limitations currently:

- `xchacha20` does not work on bun
- Only `aes-256-gcm` with 12 bytes nonce works on deno

### React Native

See the [React Native demo](https://github.com/ecies/js-rn-demo).

## API

### `encrypt(receiverRawPK: string | Uint8Array, msg: Uint8Array): Buffer`
Expand Down
4 changes: 4 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Make sure `pnpm build` under the parent directory is run before.

Run `cd browser && pnpm install && pnpm dev`

> [!NOTE]
>
> You need to copy `.npmrc.windows` to `example/browser/.npmrc` on Windows before the command above
For production, run `pnpm build && pnpm preview`

## Runtime example (Node/Bun/Deno)
Expand Down
1 change: 1 addition & 0 deletions example/browser/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { bytesToHex } from "@noble/ciphers/utils";
import { Buffer } from "buffer";
import { ECIES_CONFIG, PrivateKey, decrypt, encrypt } from "eciesjs";

import "./style.css";

globalThis.Buffer = Buffer; // polyfill manually
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,21 @@
},
"scripts": {
"build": "npx tsc",
"test": "vitest"
"test": "vitest",
"test:browser": "node ./scripts/gen-browser-tests.mjs && cd tests-browser && pnpm test"
},
"dependencies": {
"@ecies/ciphers": "^0.2.0",
"@ecies/ciphers": "^0.2.1",
"@noble/ciphers": "^1.0.0",
"@noble/curves": "^1.6.0",
"@noble/hashes": "^1.5.0"
},
"devDependencies": {
"@types/node": "^22.8.2",
"@types/node": "^22.9.0",
"@vitest/coverage-v8": "^2.1.4",
"typescript": "^5.6.3",
"undici": "^6.20.1",
"vitest": "^2.1.4"
},
"packageManager": "pnpm@9.12.2+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228"
"packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
}
Loading

0 comments on commit a2eaacb

Please sign in to comment.