Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General Refresh: rewrite as TypeScript with both CommonJS and ESM + tree shaking support #35

Merged
merged 8 commits into from
May 8, 2024

Conversation

CedarMist
Copy link
Member

@CedarMist CedarMist commented May 1, 2024

Closes #12
Closes #27
Closes #31
Closes #34

  • bumps version to 0.0.6
  • improves README with links to authors, example code etc.
  • includes the uint32 library
  • This now includes only the ct32 implementation of AES from the bsaes library
    • The vartime implementation has been removed from AES, it was not enabled by default and thus is unused.
  • Moved to using https://vitest.dev/ for tests & benchmarks
  • Linting uses biome
  • Everything is ESM & CommonJS compatibile, with ESM tree-shaking support.
    • With a minified build this resolves to 10kb of code
  • API remains compatible, with full type annotations via TypeScript
  • CI runs code coverage, we're at 99.8%
  • Using pnpm instead of npm

@CedarMist
Copy link
Member Author

CedarMist commented May 1, 2024

Oops, ran into one problem: "default" is not exported by

import deoxysii from '@oasisprotocol/deoxysii';

Fixed by exporting a namespace, this doesn't affect tree-shakability.

@CedarMist CedarMist force-pushed the CedarMist/remove-uint32-dependency branch from 0253c58 to 32e18d0 Compare May 1, 2024 16:38
@CedarMist CedarMist self-assigned this May 1, 2024
@CedarMist CedarMist added p:2 Priority: desired feature s:needs review Status: needs review labels May 1, 2024
@CedarMist CedarMist marked this pull request as ready for review May 1, 2024 16:40
@CedarMist CedarMist requested a review from lukaw3d May 1, 2024 16:41
Copy link

@pro-wh pro-wh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lotta new stuff here. haven't looked through the moved code yet

so we're vendoring in uint32 and bsaes? is it that those libraries didn't have good tree shaking?

scripts/rename-cjs.mjs Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
scripts/tsconfig.json Outdated Show resolved Hide resolved
scripts/rename-cjs.mjs Outdated Show resolved Hide resolved
"useDefineForClassFields": true, // Not enabled by default in `strict` mode unless we bump `target` to ES2022.
"noFallthroughCasesInSwitch": true, // Not enabled by default in `strict` mode.
"noImplicitReturns": true, // Not enabled by default in `strict` mode.
"useUnknownInCatchVariables": true, // TODO: This would normally be enabled in `strict` mode but would require some adjustments to the codebase.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would normally be enabled in strict mode but would require some adjustments to the codebase.

yet this line doesn't disable it? so are the adjustments made?

tsconfig.base.json Show resolved Hide resolved
// Language and environment
"moduleResolution": "NodeNext",
"module": "NodeNext",
"target": "ES2021", // Setting this to `ES2021` enables native support for `Node v16+`: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we still supporting node 16?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Node 18+ only - https://endoflife.date/nodejs

Security updates stopped for Node 16 last year, active support ended 1.5 years ago.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"type": "git",
"url": "https://github.com/oasisprotocol/deoxysii-js.git"
},
"license": "MIT",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contains a lot of differently licensed code now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deoxysii.ts & aes.ts are still MIT licensed

The uint32 package doesn't really have a license (and is explicitly noted by the author as something you can do whatever tf you want with it), so that's been noted in an appropriate SPDX-License-Identifier. I believe it would be entirely kosher to strip the license header, add my own copyright and re-license it as MIT if I wanted.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o ok

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strip the license header

sounds like that's allowed

add my own copyright

I don't think the license controls whether you can do that, and I think you can't do that

re-license it as MIT

I think you have enough permission to redistribute it under MIT, which is more restrictive

Copy link
Member Author

@CedarMist CedarMist May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be fair to add a copyright notice to it given that the changes I made to it are, while trivial, about as much effort as writing it in the first place.

Either way I'm not fussed, please advise on what to do with uint32.ts and the associated uint32.test.ts files.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it was done by changing the uint32.js albeit extensively, that sounds like it's a derivative work. I've read that it's usually preferred to contribute to a project under its original license, which for the uint32.js library was a custom license. so overall, this deoxysii-js package would have a custom license, of what would be expressed in pseudo-SPDX as (MIT AND uint32.js's license)

@CedarMist CedarMist force-pushed the CedarMist/remove-uint32-dependency branch from 513f19d to 65b1f14 Compare May 1, 2024 22:38
@CedarMist CedarMist requested a review from pro-wh May 1, 2024 22:41
biome.json Show resolved Hide resolved
src/deoxysii.ts Outdated Show resolved Hide resolved
src/TestVectors.json Outdated Show resolved Hide resolved
src/uint32.ts Outdated
@@ -0,0 +1,267 @@
// SPDX-License-Identifier: WTFPL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split commits so that changes from the original are visible in a commit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split. Diff from original files is now visible in 36fa971

package.json Show resolved Hide resolved
@CedarMist CedarMist changed the title repackaged as zero-dependency typescript library General Refresh: rewrite as TypeScript with both CommonJS and ESM + tree shaking support, May 1, 2024
src/uint32.ts Outdated
@@ -0,0 +1,267 @@
// SPDX-License-Identifier: WTFPL
Copy link

@pro-wh pro-wh May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear, this is a relicensing or new work/new license

uint32.js license (https://github.com/fxa/uint32.js?tab=readme-ov-file#license) is:

Do, what You want. This library was designed to be a copy/paste template. It would be nice to hear from You, if You used this library or if You just copy/pasted some source. It would be nice, if you added a "contains code of Franz X Antesberger" or something like that or a ref to the github project to your license information or elsewhere.

WTFPL (https://spdx.org/licenses/WTFPL.html) is:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar [email protected]

Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  1. You just DO WHAT THE FUCK YOU WANT TO.

some sentiment in common though

src/deoxysii.ts Outdated Show resolved Hide resolved
@CedarMist CedarMist changed the title General Refresh: rewrite as TypeScript with both CommonJS and ESM + tree shaking support, General Refresh: rewrite as TypeScript with both CommonJS and ESM + tree shaking support May 2, 2024
@CedarMist CedarMist force-pushed the CedarMist/remove-uint32-dependency branch 3 times, most recently from 1cae564 to 58cc004 Compare May 2, 2024 00:50
@CedarMist
Copy link
Member Author

Ok @lukaw3d and @pro-wh

I have incorporated your comments to the best that I'm able to, thank you for the feedback as I'd missed or not considered a few things that you pointed out.

I've also updated the README so it's pretty and includes an example.

@CedarMist CedarMist force-pushed the CedarMist/remove-uint32-dependency branch 2 times, most recently from a24f397 to 361ddbd Compare May 2, 2024 01:06
Comment on lines 1 to 15
/** Renames `.js` files in the /lib/cjs folder to `.cjs` to appease ESM clients. */

import { promises as fs } from "node:fs";
import url from "node:url";

process.chdir(url.fileURLToPath(`${import.meta.url}/../../dist/_cjs`));
for (const filename of await fs.readdir(".")) {
if (!/\.js$/.test(filename)) continue;
const cjs = await fs.readFile(filename, "utf-8");
await fs.writeFile(
filename.replace(/\.js$/, ".cjs"),
cjs.replaceAll(/require\("(.\/[\w\-.]+)\.js"\)/g, 'require("$1.cjs")'),
);
await fs.unlink(filename);
}
Copy link
Member

@lukaw3d lukaw3d May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried comparing your previous version + without namespace + using tsup (https://antfu.me/posts/publish-esm-and-cjs#tsup)

32e7271

might be useful some day. The changed code is inside each tgz/src

Copy link
Member Author

@CedarMist CedarMist May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsup & unbuild look really good, and I really don't like the rename-cjs hack. I wish there was a simpler way that only needed a single set of files.

Unfortunately there seems to be no safe way to drop CommonJS support

Copy link
Member Author

@CedarMist CedarMist May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ pnpm tsup src/index.ts  --format cjs,esm --dts --clean --minify --sourcemap
CLI Building entry: src/index.ts
CLI Using tsconfig: tsconfig.json
CLI tsup v8.0.2
CLI Target: es2021
CLI Cleaning output folder
CJS Build start
ESM Build start
ESM dist/index.mjs     10.17 KB
ESM dist/index.mjs.map 68.06 KB
ESM ⚡️ Build success in 21ms
CJS dist/index.js     10.69 KB
CJS dist/index.js.map 68.32 KB
CJS ⚡️ Build success in 22ms
DTS Build start
DTS ⚡️ Build success in 723ms
DTS dist/index.d.ts  1.17 KB
DTS dist/index.d.mts 1.17 KB

tsup is great, so much less hassle, so the project is about 3.7kb gzipped + minified and has no production dependencies, easy to build both CJS & ESM, this is exactly what I wanted.

Just trying to follow the recommendations on https://publint.dev/ (can be installed locally via publint) for due ESM & CJS packaging.

@CedarMist CedarMist requested a review from lukaw3d May 2, 2024 17:00
"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)'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this. Is it just
NODE_DEBUG=module,esm node --experimental-global-webcrypto examples/check.cjs | grep deoxysii
minus the errors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It greps stderr for logs from module or esm to show only ones related to deoxysii.

Essentially this double checks that check.cjs loads the .js file via the normal module loader, and check.mjs loads the .mjs file via ESM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


> @oasisprotocol/[email protected] debug-imports /home/runner/work/deoxysii-js/deoxysii-js
> pnpm run debug-imports-cjs && pnpm run debug-imports-esm


> @oasisprotocol/[email protected] debug-imports-cjs /home/runner/work/deoxysii-js/deoxysii-js
> NODE_DEBUG=module,esm bash -c 'pnpm node --experimental-global-webcrypto examples/check.cjs 2> >(grep deoxysii)'

MODULE 2161: Module._load REQUEST /home/runner/work/deoxysii-js/deoxysii-js/.pnpmfile.cjs parent: /home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/dist/pnpm.cjs
MODULE 2161: looking for "/home/runner/work/deoxysii-js/deoxysii-js/.pnpmfile.cjs" in ["/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/dist/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules","/home/runner/node_modules","/home/node_modules","/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/.node_modules","/home/runner/.node_libraries","/opt/hostedtoolcache/node/18.20.2/x64/lib/node"]
MODULE 2179: looking for "/home/runner/work/deoxysii-js/deoxysii-js/examples/check.cjs" in ["/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/.node_modules","/home/runner/.node_libraries","/opt/hostedtoolcache/node/18.20.2/x64/lib/node"]
MODULE 2179: load "/home/runner/work/deoxysii-js/deoxysii-js/examples/check.cjs" for module "."
MODULE 2179: Module._load REQUEST @oasisprotocol/deoxysii parent: .
MODULE 2179: looking for "@oasisprotocol/deoxysii" in ["/home/runner/work/deoxysii-js/deoxysii-js/examples/node_modules","/home/runner/work/deoxysii-js/deoxysii-js/node_modules","/home/runner/work/deoxysii-js/node_modules","/home/runner/work/node_modules","/home/runner/node_modules","/home/node_modules","/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/nocjs [class AEAD]
Encrypted: Uint8Array(27) [
  233,  24, 247,  21,  97, 209, 159,
  253,   5, 151, 186,  13, 204,  42,
  227, 216, 204, 121,  32, 176, 232,
   24,  16, 216, 130, 102, 206
]
Decrypted: Hello World
de_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/.node_modules","/home/runner/.node_libraries","/opt/hostedtoolcache/node/18.20.2/x64/lib/node"]
MODULE 2179: load "/home/runner/work/deoxysii-js/deoxysii-js/dist/index.js" for module "/home/runner/work/deoxysii-js/deoxysii-js/dist/index.js"

> @oasisprotocol/[email protected] debug-imports-esm /home/runner/work/deoxysii-js/deoxysii-js
> NODE_DEBUG=module,esm bash -c 'pnpm node --experimental-global-webcrypto examples/check.mjs 2> >(grep deoxysii)'

star [class AEAD]
default [class AEAD]
AEAD [class AEAD]
Encrypted: Uint8Array(27) [
  175, 189, 113,  76, 235, 191, 199,
   63, 217, 208,  56, 109,  32, 214,
  207, 137, 242,   9,   9, 141,   6,
  220, 222,  46, 100, 147, 174
]
Decrypted: Hello World
MODULE 2204: Module._load REQUEST /home/runner/work/deoxysii-js/deoxysii-js/.pnpmfile.cjs parent: /home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/dist/pnpm.cjs
MODULE 2204: looking for "/home/runner/work/deoxysii-js/deoxysii-js/.pnpmfile.cjs" in ["/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/dist/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules","/home/runner/node_modules","/home/node_modules","/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/bin/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules/pnpm/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/[email protected]/node_modules","/home/runner/setup-pnpm/node_modules/.pnpm/node_modules","/home/runner/.node_modules","/home/runner/.node_libraries","/opt/hostedtoolcache/node/18.20.2/x64/lib/node"]
ESM 2222: Storing file:///home/runner/work/deoxysii-js/deoxysii-js/examples/check.mjs (implicit type) in ModuleLoadMap
ESM 2222: Translating StandardModule file:///home/runner/work/deoxysii-js/deoxysii-js/examples/check.mjs
ESM 2222: Storing file:///home/runner/work/deoxysii-js/deoxysii-js/dist/index.mjs (implicit type) in ModuleLoadMap
ESM 2222: Translating StandardModule file:///home/runner/work/deoxysii-js/deoxysii-js/dist/index.mjs

@lukaw3d lukaw3d force-pushed the CedarMist/remove-uint32-dependency branch from 0fbf0b9 to a8efa77 Compare May 2, 2024 23:26
@CedarMist CedarMist force-pushed the CedarMist/remove-uint32-dependency branch from a8efa77 to 98028ea Compare May 3, 2024 10:29
@CedarMist CedarMist added s:ready Status: ready to work/merge and removed s:needs review Status: needs review labels May 3, 2024
@CedarMist CedarMist merged commit 7018d83 into master May 8, 2024
1 check passed
@CedarMist CedarMist deleted the CedarMist/remove-uint32-dependency branch May 8, 2024 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p:2 Priority: desired feature s:ready Status: ready to work/merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rewrite in typescript
4 participants