Skip to content

Commit

Permalink
moved to tsup with checks for CJS & ESM import styles
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed May 2, 2024
1 parent 36fa971 commit a8efa77
Show file tree
Hide file tree
Showing 10 changed files with 1,062 additions and 86 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ jobs:
- run: pnpm run lint
- run: pnpm run coverage
- run: pnpm run build
- run: pnpm run debug-imports
- run: pnpm run bench
- run: pnpm pack
- run: pnpm publint
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![version][deoxysii-version]][deoxysii-npm]
[![size][deoxysii-size]][deoxysii-bundlephobia]
![downloads][deoxysii-downloads]
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

[deoxysii-npm]: https://www.npmjs.com/package/@oasisprotocol/deoxysii
[deoxysii-version]: https://img.shields.io/npm/v/@oasisprotocol/deoxysii
Expand Down Expand Up @@ -71,11 +72,24 @@ try {
> Users that require a more performant and secure implementation are suggested
> to investigate WebAssembly, or (even better) calling native code.
#### Acknowledgements
#### Development

This MIT licensed project utilizes modified code originally developed by Franz X
Antesberger. The original code for `uint32.js` is available at [fxa/uint32.js].
We have adapted this code for TypeScript. We appreciate the contributions of
Franz X Antesberger to the open-source community.
* [Node.js](https://nodejs.org/en/about/previous-releases) - version 18+
* [typescript](https://www.typescriptlang.org/) - version 5.x, for type safety
* [pnpm](https://pnpm.io/) - package manager
* [`gh act`](https://github.com/nektos/act) - run GitHub actions locally
* [vitest](https://vitest.dev/) - tests, benchmarking & coverage
* [biome](https://biomejs.dev/) - lint & formatting
* [tsup](https://tsup.egoist.dev/) - compilation & bundling
* [publint](https://publint.dev/) - packaging checks

#### License & Acknowledgements

This project is released under the MIT License.

This project utilizes modified code originally developed by Franz X Antesberger.
The original code for `uint32.js` is available at [fxa/uint32.js]. We have
adapted this code for TypeScript. We appreciate the contributions of Franz X
Antesberger to the open-source community.

[fxa/uint32.js]: https://github.com/fxa/uint32.js
22 changes: 22 additions & 0 deletions examples/check.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const deoxysii = require("@oasisprotocol/deoxysii");
console.log("cjs", deoxysii.AEAD);

// Define a key (ensure the size matches requirements)
const key = crypto.getRandomValues(new Uint8Array(deoxysii.KeySize));
const aead = new deoxysii.AEAD(key);

// Encryption
const nonce = crypto.getRandomValues(new Uint8Array(deoxysii.NonceSize));
const plaintext = new TextEncoder().encode("Hello World");
const associatedData = new Uint8Array([0x1, 0x2, 0x3]);

const encrypted = aead.encrypt(nonce, plaintext, associatedData);
console.log("Encrypted:", encrypted);

// Decryption
try {
const decrypted = aead.decrypt(nonce, encrypted, associatedData);
console.log("Decrypted:", new TextDecoder().decode(decrypted));
} catch (error) {
console.error("Decryption failed:", error);
}
30 changes: 30 additions & 0 deletions examples/check.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as deoxysii from "@oasisprotocol/deoxysii";
console.log("star", deoxysii.AEAD);

import deoxysiiDefault from "@oasisprotocol/deoxysii";
console.log("default", deoxysiiDefault.AEAD);

import { AEAD } from "@oasisprotocol/deoxysii";
console.log("AEAD", AEAD);

import { KeySize, NonceSize } from "@oasisprotocol/deoxysii";

// Define a key (ensure the size matches requirements)
const key = crypto.getRandomValues(new Uint8Array(KeySize));
const aead = new AEAD(key);

// Encryption
const nonce = crypto.getRandomValues(new Uint8Array(NonceSize));
const plaintext = new TextEncoder().encode("Hello World");
const associatedData = new Uint8Array([0x1, 0x2, 0x3]);

const encrypted = aead.encrypt(nonce, plaintext, associatedData);
console.log("Encrypted:", encrypted);

// Decryption
try {
const decrypted = aead.decrypt(nonce, encrypted, associatedData);
console.log("Decrypted:", new TextDecoder().decode(decrypted));
} catch (error) {
console.error("Decryption failed:", error);
}
56 changes: 34 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,43 @@
"version": "0.0.6",
"description": "Deoxys-II-256-128",
"sideEffects": false,
"files": ["dist", "!dist/*.tsbuildinfo", "src", "!src/*.json"],
"main": "./dist/_cjs/index.js",
"module": "./dist/_esm/index.js",
"types": "./dist/_types/index.d.ts",
"typings": "./dist/_types/index.d.ts",
"files": [
"dist",
"!dist/*.tsbuildinfo",
"LICENSE",
"src",
"!src/*.test.ts",
"!src/*.bench.ts"
],
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"type": "commonjs",
"exports": {
"node": {
"import": "./dist/_esm/index.js",
"require": "./dist/_cjs/index.cjs",
"types": "./dist/_types/index.d.ts"
},
"types": "./dist/_types/index.d.ts",
"default": "./dist/_esm/index.js"
".": {
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
}
}
},
"scripts": {
"bench": "pnpm vitest bench src/*.bench.ts",
"test": "vitest",
"coverage": "vitest run --coverage --coverage.exclude=scripts --coverage.exclude='src/*.bench.ts'",
"coverage": "vitest run --coverage --coverage.exclude=scripts --coverage.exclude='src/*.bench.ts' --coverage.exclude='examples'",
"lint": "biome check .",
"format": "biome format --write .",
"clean": "rm -rf dist",
"build": "npm run build:cjs && npm run build:esm && npm run build:types",
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./dist/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./dist/_cjs/package.json && node scripts/rename-cjs.mjs",
"build:esm": "tsc --project ./tsconfig.build.json --module es2015 --outDir ./dist/_esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/_esm/package.json",
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./dist/_types --emitDeclarationOnly --declaration --declarationMap",
"prepublishOnly": "npm run build"
"clean": "rm -rf dist coverage",
"distclean": "pnpm run clean && rm -rf node_modules *.tgz",
"build": "pnpm tsup src/index.ts --format cjs,esm --dts --clean",
"prepublishOnly": "npm run build",
"debug-imports-cjs": "NODE_DEBUG=module,esm bash -c 'pnpm node --experimental-global-webcrypto examples/check.cjs 2> >(grep deoxysii)'",
"debug-imports-esm": "NODE_DEBUG=module,esm bash -c 'pnpm node --experimental-global-webcrypto examples/check.mjs 2> >(grep deoxysii)'",
"debug-imports": "pnpm run debug-imports-cjs && pnpm run debug-imports-esm"
},
"repository": {
"type": "git",
Expand All @@ -40,9 +51,10 @@
"@biomejs/biome": "^1.7.0",
"@types/node": "18.x",
"@vitest/coverage-v8": "^1.5.3",
"benchmark": "^2.1.4",
"vitest": "^1.5.3",
"typescript": ">=5.0.4"
"publint": "^0.2.7",
"tsup": "^8.0.2",
"typescript": ">=5.0.4",
"vitest": "^1.5.3"
},
"peerDependencies": {
"typescript": ">=5.0.4"
Expand Down
Loading

0 comments on commit a8efa77

Please sign in to comment.