From f6ea90b824b943109f9861efbc062c87a93e9953 Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Fri, 22 Dec 2023 10:57:00 -0600 Subject: [PATCH 1/3] chore: change `moduleResolution` --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index d030e21a..76de1499 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,7 @@ "noEmit": true, "downlevelIteration": true, "strict": true, - "moduleResolution": "node", + "moduleResolution": "bundler", "resolveJsonModule": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true From 9db009234e180bd2052ce50d5ba8265bcafc2b8b Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Fri, 22 Dec 2023 10:58:37 -0600 Subject: [PATCH 2/3] build: `.web` => `.browser` --- src/crypto/{crypto.web.ts => crypto.browser.ts} | 0 .../{selfEncryption.web.ts => selfEncryption.browser.ts} | 2 -- tsup.config.ts | 6 +++--- 3 files changed, 3 insertions(+), 5 deletions(-) rename src/crypto/{crypto.web.ts => crypto.browser.ts} (100%) rename src/crypto/{selfEncryption.web.ts => selfEncryption.browser.ts} (95%) diff --git a/src/crypto/crypto.web.ts b/src/crypto/crypto.browser.ts similarity index 100% rename from src/crypto/crypto.web.ts rename to src/crypto/crypto.browser.ts diff --git a/src/crypto/selfEncryption.web.ts b/src/crypto/selfEncryption.browser.ts similarity index 95% rename from src/crypto/selfEncryption.web.ts rename to src/crypto/selfEncryption.browser.ts index 5b2c6dac..d4087f70 100644 --- a/src/crypto/selfEncryption.web.ts +++ b/src/crypto/selfEncryption.browser.ts @@ -9,8 +9,6 @@ import init, { user_preferences_decrypt, // eslint-disable-next-line camelcase user_preferences_encrypt, - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore } from '@xmtp/user-preferences-bindings-wasm/web' import { PrivateKey } from './PrivateKey' diff --git a/tsup.config.ts b/tsup.config.ts index 9ff75e74..fd2fffcb 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -6,12 +6,12 @@ export default defineConfig((options) => { const esbuildPlugins: Plugin[] = [] // for browsers, replace imports if there's a file with the same name - // but with a .web extension - // i.e. crypto.ts -> crypto.web.ts + // but with a `.browser` extension + // i.e. crypto.ts -> crypto.browser.ts if (options.platform === 'browser') { esbuildPlugins.push( resolveExtensionsPlugin({ - extensions: ['.web'], + extensions: ['.browser'], }) ) } From 9376bcdc78c4db7c83e209920915bfe086c8ee00 Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Fri, 22 Dec 2023 10:59:14 -0600 Subject: [PATCH 3/3] build: add bundler package, more exports --- package.json | 24 ++++++++++++++++- src/crypto/selfEncryption.bundler.ts | 39 ++++++++++++++++++++++++++++ tsup.bundler.config.ts | 32 +++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/crypto/selfEncryption.bundler.ts create mode 100644 tsup.bundler.config.ts diff --git a/package.json b/package.json index 3727d966..79d096d5 100644 --- a/package.json +++ b/package.json @@ -13,15 +13,37 @@ "browser": "./dist/web/index.js", "import": "./dist/index.js", "require": "./dist/index.cjs" + }, + "./node": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js", + "require": "./dist/index.cjs" + }, + "./node/esm": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "./node/cjs": { + "types": "./dist/index.d.ts", + "default": "./dist/index.cjs" + }, + "./browser": { + "types": "./dist/index.d.ts", + "default": "./dist/web/index.js" + }, + "./browser/bundler": { + "types": "./dist/index.d.ts", + "default": "./dist/bundler/index.js" } }, "scripts": { "prebench": "npm run build:bench", "bench": "node dist/bench/index.cjs", - "build": "npm run clean:dist && npm run build:node && npm run build:web", + "build": "npm run clean:dist && npm run build:node && npm run build:web && npm run build:bundler", "build:bench": "tsup --out-dir dist/bench --entry.0 bench/index.ts --format cjs", "build:node": "tsup", "build:web": "tsup --platform browser --target esnext", + "build:bundler": "tsup --config tsup.bundler.config.ts", "build:docs": "rimraf docs && mkdir -p tmp && cp README.md tmp/ && sed -i.bak '/badge.svg/d' tmp/README.md && typedoc --excludePrivate --readme tmp/README.md src/index.ts", "clean": "npm run clean:dist && npm run clean:proto", "clean:dist": "rimraf dist", diff --git a/src/crypto/selfEncryption.bundler.ts b/src/crypto/selfEncryption.bundler.ts new file mode 100644 index 00000000..8b540532 --- /dev/null +++ b/src/crypto/selfEncryption.bundler.ts @@ -0,0 +1,39 @@ +/*********************************************************************************************** + * DO NOT IMPORT THIS FILE DIRECTLY + ***********************************************************************************************/ + +import { + // eslint-disable-next-line camelcase + generate_private_preferences_topic, + // eslint-disable-next-line camelcase + user_preferences_decrypt, + // eslint-disable-next-line camelcase + user_preferences_encrypt, +} from '@xmtp/user-preferences-bindings-wasm/bundler' +import { PrivateKey } from './PrivateKey' + +export async function userPreferencesEncrypt( + identityKey: PrivateKey, + payload: Uint8Array +) { + const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return user_preferences_encrypt(publicKey, privateKey, payload) +} + +export async function userPreferencesDecrypt( + identityKey: PrivateKey, + payload: Uint8Array +) { + const publicKey = identityKey.publicKey.secp256k1Uncompressed.bytes + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return user_preferences_decrypt(publicKey, privateKey, payload) +} + +export async function generateUserPreferencesTopic(identityKey: PrivateKey) { + const privateKey = identityKey.secp256k1.bytes + // eslint-disable-next-line camelcase + return generate_private_preferences_topic(privateKey) +} diff --git a/tsup.bundler.config.ts b/tsup.bundler.config.ts new file mode 100644 index 00000000..e0d0f359 --- /dev/null +++ b/tsup.bundler.config.ts @@ -0,0 +1,32 @@ +import { defineConfig } from 'tsup' +import { resolveExtensionsPlugin } from './build/esbuild-plugin-resolve-extensions/index.ts' +import { Plugin } from 'esbuild' + +export default defineConfig((options) => { + const esbuildPlugins: Plugin[] = [] + + // replace imports if there's a file with the same name but with a + // `.bundler` or `.browser` extension + // i.e. crypto.ts -> crypto.browser.ts + esbuildPlugins.push( + resolveExtensionsPlugin({ + extensions: ['.bundler', '.browser'], + }) + ) + + return { + entry: ['src/index.ts'], + outDir: 'dist/bundler', + splitting: false, + sourcemap: true, + treeshake: true, + clean: true, + bundle: true, + platform: 'browser', + minify: true, + dts: false, + format: ['esm'], + esbuildPlugins, + target: 'esnext', + } +})