Skip to content

Commit

Permalink
Replace rollup with esbuild (#53)
Browse files Browse the repository at this point in the history
* Replace rollup with esbuild

* Downgrade jest and fix tests

* Add extensions to all imports 😴

* Missed a few extensions
  • Loading branch information
icidasset authored Sep 14, 2021
1 parent c3b1904 commit 7b9baf1
Show file tree
Hide file tree
Showing 30 changed files with 1,864 additions and 2,242 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
### v0.15.0

- Renamed read key to exchange key.
- Switched out `Buffer` usage with `uint8arrays` library
- Switched out `Buffer` usage with `uint8arrays` library.
- Built with esbuild instead of rollup.


### v0.14.0
Expand Down
6 changes: 5 additions & 1 deletion jest.config.js → jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = { // eslint-disable-line
module.exports = {
resolver: "jest-ts-webcompat-resolver",
transform: {
".(ts|tsx)": "ts-jest"
},
Expand All @@ -9,6 +10,9 @@ module.exports = { // eslint-disable-line
"tsx",
"js"
],
collectCoverage: true,
coverageDirectory: "coverage",
coverageProvider: "v8",
coveragePathIgnorePatterns: [
"/node_modules/",
"/test/",
Expand Down
89 changes: 48 additions & 41 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,34 @@
"version": "0.15.0",
"description": "In-browser key management with IndexedDB and the Web Crypto API",
"keywords": [],
"main": "index.cjs.js",
"module": "index.es5.js",
"browser": "index.umd.js",
"typings": "types/index.d.ts",
"type": "module",
"main": "lib/index.js",
"exports": {
".": "./lib/index.js",
"./*": "./lib/*",
"./package.json": "./package.json"
},
"types": "lib/index.d.ts",
"typesVersions": {
"*": {
"lib/index.d.ts": [
"lib/index.d.ts"
],
"*": [
"lib/*"
]
}
},
"files": [
"lib",
"dist",
"README.md",
"CHANGELOG.md",
"LICENSE",
"package.json",
"!*.test.ts",
"docs"
],
"author": "Daniel Holmgren <[email protected]>",
"repository": {
"type": "git",
Expand All @@ -19,52 +43,35 @@
"scripts": {
"lint": "yarn eslint src/**/*.ts test/**/*.ts",
"prebuild": "rimraf dist",
"build": "tsc && rollup -c rollup.config.ts",
"build": "tsc && yarn run build:minified",
"build:minified": "node scripts/build-minified.js",
"start": "tsc -w",
"test": "jest --coverage",
"test:watch": "jest --coverage --watch",
"test:prod": "npm run lint && npm run test -- --no-cache",
"do-publish": "yarn build && cp package.json LICENSE README.md dist/ && cd dist && npm publish",
"precommit": "lint-staged"
},
"lint-staged": {
"{src,test}/**/*.ts": [
"prettier --write",
"git add"
]
},
"prettier": {
"semi": false,
"singleQuote": true
"prepare": "yarn build",
"publish-dry": "npm publish --dry-run",
"publish-alpha": "npm publish --tag alpha",
"publish-latest": "npm publish --tag latest"
},
"devDependencies": {
"@types/jest": "^25.1.3",
"@types/node": "^13.7.4",
"@typescript-eslint/eslint-plugin": "^2.21.0",
"@typescript-eslint/parser": "^2.21.0",
"braces": "^3.0.2",
"eslint": "^6.8.0",
"jest": "^25.1.0",
"jest-config": "^25.1.0",
"lint-staged": "^10.0.7",
"prettier": "^1.19.1",
"prompt": "^1.0.0",
"replace-in-file": "^5.0.2",
"@types/jest": "^26.0.0",
"@types/node": "^16.9.1",
"@typescript-eslint/eslint-plugin": "^4.31.0",
"@typescript-eslint/parser": "^4.31.0",
"esbuild": "^0.12.27",
"eslint": "^7.32.0",
"jest": "^26.0.0",
"jest-config": "^26.0.0",
"jest-ts-webcompat-resolver": "^1.0.0",
"rimraf": "^3.0.2",
"rollup": "^1.31.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-typescript2": "^0.26.0",
"ts-jest": "^25.2.1",
"ts-node": "^8.6.2",
"typescript": "^3.8.2"
"ts-jest": "^26.0.0",
"ts-node": "^10.2.1",
"typescript": "^4.4.2"
},
"dependencies": {
"@ungap/global-this": "^0.4.3",
"localforage": "^1.7.3",
"@ungap/global-this": "^0.4.4",
"localforage": "^1.10.0",
"uint8arrays": "^3.0.0"
}
}
6 changes: 0 additions & 6 deletions publish.sh

This file was deleted.

87 changes: 0 additions & 87 deletions rollup.config.ts

This file was deleted.

62 changes: 62 additions & 0 deletions scripts/build-minified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import esbuild from "esbuild"
import fs from "fs"
import zlib from "zlib"


const globalName = "keystore"
const outfile = "dist/index.umd.min.js"
const outfileGz = `${outfile}.gz`

// From https://github.com/umdjs/umd/blob/36fd1135ba44e758c7371e7af72295acdebce010/templates/returnExports.js
const umd = {
banner:
`(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.${globalName} = factory();
}
}(typeof self !== 'undefined' ? self : this, function () { `,
footer:
`return ${globalName};
}));`
}

console.log("📦 bundling & minifying...")

esbuild.buildSync({
entryPoints: ["src/index.ts"],
outfile,
bundle: true,
minify: true,
sourcemap: true,
platform: "browser",
format: "iife",
target: "es2020",
globalName,
banner: {
js: umd.banner,
},
footer: {
js: umd.footer,
},
})

console.log(`📝 Wrote ${outfile} and ${outfile}.map`)

console.log("💎 compressing into .gz")

const fileContents = fs.createReadStream(outfile)
const writeStream = fs.createWriteStream(outfileGz)
const gzip = zlib.createGzip()

fileContents.pipe(gzip).pipe(writeStream)

console.log(`📝 Wrote ${outfileGz}`)
8 changes: 4 additions & 4 deletions src/aes/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import keys from './keys'
import operations from './operations'
import keys from './keys.js'
import operations from './operations.js'

export * from './keys'
export * from './operations'
export * from './keys.js'
export * from './operations.js'

export default {
...keys,
Expand Down
10 changes: 5 additions & 5 deletions src/aes/keys.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import utils from '../utils'
import { DEFAULT_SYMM_ALG, DEFAULT_SYMM_LEN } from '../constants'
import { SymmKey, SymmKeyOpts } from '../types'
import utils from '../utils.js'
import { DEFAULT_SYMM_ALG, DEFAULT_SYMM_LEN } from '../constants.js'
import { SymmKey, SymmKeyOpts } from '../types.js'

export async function makeKey(opts?: Partial<SymmKeyOpts>): Promise<SymmKey> {
return globalThis.crypto.subtle.generateKey(
{
{
name: opts?.alg || DEFAULT_SYMM_ALG,
length: opts?.length || DEFAULT_SYMM_LEN,
},
Expand All @@ -18,7 +18,7 @@ export async function importKey(base64key: string, opts?: Partial<SymmKeyOpts>):
return globalThis.crypto.subtle.importKey(
'raw',
buf,
{
{
name: opts?.alg || DEFAULT_SYMM_ALG,
length: opts?.length || DEFAULT_SYMM_LEN,
},
Expand Down
10 changes: 5 additions & 5 deletions src/aes/operations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import keys from './keys'
import utils from '../utils'
import { DEFAULT_SYMM_ALG, DEFAULT_CTR_LEN } from '../constants'
import { SymmKey, SymmKeyOpts, SymmAlg, CipherText, Msg } from '../types'
import keys from './keys.js'
import utils from '../utils.js'
import { DEFAULT_SYMM_ALG, DEFAULT_CTR_LEN } from '../constants.js'
import { SymmKey, SymmKeyOpts, SymmAlg, CipherText, Msg } from '../types.js'

export async function encryptBytes(
msg: Msg,
Expand All @@ -13,7 +13,7 @@ export async function encryptBytes(
const alg = opts?.alg || DEFAULT_SYMM_ALG
const iv = opts?.iv || utils.randomBuf(16)
const cipherBuf = await globalThis.crypto.subtle.encrypt(
{
{
name: alg,
// AES-CTR uses a counter, AES-GCM/AES-CBC use an initialization vector
iv: alg === SymmAlg.AES_CTR ? undefined : iv,
Expand Down
8 changes: 4 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ecc from './ecc/keys'
import ecc from './ecc/keys.js'
import {
DEFAULT_CRYPTOSYSTEM,
DEFAULT_ECC_CURVE,
Expand All @@ -10,9 +10,9 @@ import {
DEFAULT_STORE_NAME,
DEFAULT_EXCHANGE_KEY_NAME,
DEFAULT_WRITE_KEY_NAME
} from './constants'
import { Config, KeyUse, CryptoSystem, SymmKeyOpts } from './types'
import utils from './utils'
} from './constants.js'
import { Config, KeyUse, CryptoSystem, SymmKeyOpts } from './types.js'
import utils from './utils.js'

export const defaultConfig = {
type: DEFAULT_CRYPTOSYSTEM,
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EccCurve, RsaSize, SymmAlg, SymmKeyLength, HashAlg, CharSize } from './types'
import { EccCurve, RsaSize, SymmAlg, SymmKeyLength, HashAlg, CharSize } from './types.js'

export const ECC_EXCHANGE_ALG = 'ECDH'
export const ECC_WRITE_ALG = 'ECDSA'
Expand Down
Loading

0 comments on commit 7b9baf1

Please sign in to comment.