Skip to content

Commit

Permalink
Merge pull request #613 from xmtp/rygine/mls-client
Browse files Browse the repository at this point in the history
Add MLS client
  • Loading branch information
rygine authored Jun 5, 2024
2 parents da4703e + e04b09b commit 0acb628
Show file tree
Hide file tree
Showing 21 changed files with 1,228 additions and 2 deletions.
44 changes: 44 additions & 0 deletions packages/mls-client/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = {
parser: '@typescript-eslint/parser',
extends: [
'eslint:recommended',
'standard',
'prettier',
'plugin:@typescript-eslint/recommended',
'eslint-config-prettier',
],
parserOptions: {
sourceType: 'module',
warnOnUnsupportedTypeScriptVersion: false,
project: 'tsconfig.json',
},
rules: {
'@typescript-eslint/consistent-type-exports': [
'error',
{
fixMixedExportsWithInlineTypeSpecifier: false,
},
],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
destructuredArrayIgnorePattern: '^_',
ignoreRestSiblings: true,
varsIgnorePattern: '^_',
},
],
'prettier/prettier': 'error',
'no-restricted-syntax': [
'error',
{
selector: 'ImportDeclaration[source.value=/^\\.\\./]',
message:
'Relative parent imports are not allowed, use path aliases instead.',
},
],
},
plugins: ['@typescript-eslint', 'prettier'],
}
21 changes: 21 additions & 0 deletions packages/mls-client/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 XMTP (xmtp.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions packages/mls-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# XMTP MLS Client

> **Important**
> This package is currently in **Alpha** status. Do not use in production as the API is not final and certain functionality may not work as intended. This package only works in Node.
97 changes: 97 additions & 0 deletions packages/mls-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "@xmtp/mls-client",
"version": "0.0.1",
"description": "XMTP MLS client for interacting with XMTP networks",
"keywords": [
"xmtp",
"messaging",
"web3",
"js",
"javascript",
"node",
"nodejs"
],
"homepage": "https://github.com/xmtp/xmtp-js",
"bugs": {
"url": "https://github.com/xmtp/xmtp-js/issues"
},
"repository": {
"type": "git",
"url": "https:[email protected]:xmtp/xmtp-js.git",
"directory": "packages/mls-client"
},
"license": "MIT",
"author": "XMTP Labs <[email protected]>",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "yarn clean:dist && rollup -c",
"clean": "yarn clean:dbs && yarn clean:dist && yarn clean:deps && yarn clean:tests",
"clean:dbs": "rm -rf *.db3*",
"clean:deps": "rm -rf node_modules",
"clean:dist": "rm -rf dist",
"clean:tests": "rm -rf test/*.db3*",
"format": "yarn format:base -w .",
"format:base": "prettier --ignore-path ../../.gitignore",
"format:check": "yarn format:base -c .",
"lint": "eslint . --ignore-path ../../.gitignore",
"test": "vitest run",
"test:cov": "vitest run --coverage",
"typecheck": "tsc"
},
"dependencies": {
"@xmtp/mls-client-bindings-node": "^0.0.1",
"@xmtp/proto": "^3.61.1",
"@xmtp/xmtp-js": "workspace:*"
},
"devDependencies": {
"@ianvs/prettier-plugin-sort-imports": "^4.2.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^18.19.23",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@vitest/coverage-v8": "^1.6.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^17.5.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.1.1",
"fast-glob": "^3.3.2",
"prettier": "^3.2.5",
"prettier-plugin-packagejson": "^2.5.0",
"rollup": "^4.17.2",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-filesize": "^10.0.0",
"rollup-plugin-tsconfig-paths": "^1.5.2",
"typescript": "^5.4.5",
"viem": "^2.13.6",
"vite": "5.2.11",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.6.0"
},
"packageManager": "[email protected]",
"engines": {
"node": ">=18"
},
"publishConfig": {
"access": "public",
"provenance": true,
"registry": "https://registry.npmjs.org/"
}
}
59 changes: 59 additions & 0 deletions packages/mls-client/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import json from '@rollup/plugin-json'
import typescript from '@rollup/plugin-typescript'
import { defineConfig } from 'rollup'
import { dts } from 'rollup-plugin-dts'
import filesize from 'rollup-plugin-filesize'
import tsConfigPaths from 'rollup-plugin-tsconfig-paths'

const external = [
'node:path',
'node:process',
'@xmtp/mls-client-bindings-node',
'@xmtp/proto',
'@xmtp/xmtp-js',
]

const plugins = [
tsConfigPaths(),
typescript({
declaration: false,
declarationMap: false,
}),
filesize({
showMinifiedSize: false,
}),
json({
preferConst: true,
}),
]

export default defineConfig([
{
input: 'src/index.ts',
output: {
file: 'dist/index.js',
format: 'es',
sourcemap: true,
},
plugins,
external,
},
{
input: 'src/index.ts',
output: {
file: 'dist/index.cjs',
format: 'cjs',
sourcemap: true,
},
plugins,
external,
},
{
input: 'src/index.ts',
output: {
file: 'dist/index.d.ts',
format: 'es',
},
plugins: [tsConfigPaths(), dts()],
},
])
77 changes: 77 additions & 0 deletions packages/mls-client/src/AsyncStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
type Value<T, V> = V extends undefined ? T : V

type ResolveValue<T, V> = {
value: Value<T, V> | undefined
done: boolean
}

type ResolveNext<T, V> = (resolveValue: ResolveValue<T, V>) => void

type TransformValue<T, V> = (value: T) => Value<T, V>

export class AsyncStream<T, V = undefined> {
#done = false
#resolveNext: ResolveNext<T, V> | null
#queue: Value<T, V>[]
#transformValue?: TransformValue<T, V>

stopCallback: (() => void) | undefined = undefined

constructor(
transformValue: V extends undefined ? undefined : TransformValue<T, V>
) {
this.#queue = []
this.#resolveNext = null
this.#done = false
this.#transformValue = transformValue
}

callback = (err: Error | null, value: T) => {
if (err) {
console.error('stream error', err)
this.stop()
return
}

if (this.#done) {
return
}

const newValue = this.#transformValue
? this.#transformValue(value)
: // must assert type because TypeScript can't infer that T is assignable
// to Value<T, V> when this.#transformValue is undefined
(value as unknown as Value<T, V>)

if (this.#resolveNext) {
this.#resolveNext({ value: newValue, done: false })
this.#resolveNext = null
} else {
this.#queue.push(newValue)
}
}

stop = () => {
this.#done = true
if (this.#resolveNext) {
this.#resolveNext({ value: undefined, done: true })
}
this.stopCallback?.()
}

next = (): Promise<ResolveValue<T, V>> => {
if (this.#queue.length > 0) {
return Promise.resolve({ value: this.#queue.shift(), done: false })
} else if (this.#done) {
return Promise.resolve({ value: undefined, done: true })
} else {
return new Promise((resolve) => {
this.#resolveNext = resolve
})
}
};

[Symbol.asyncIterator]() {
return this
}
}
Loading

0 comments on commit 0acb628

Please sign in to comment.