This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from xmtp/rygine/primitives
Add content type primitives package
- Loading branch information
Showing
11 changed files
with
364 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ["custom"], | ||
parserOptions: { | ||
project: "./tsconfig.eslint.json", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Content type primitives | ||
|
||
This package provides primitives for building custom XMTP content types. | ||
|
||
## Install the package | ||
|
||
```bash | ||
# npm | ||
npm i @xmtp/content-type-primitives | ||
|
||
# yarn | ||
yarn add @xmtp/content-type-primitives | ||
|
||
# pnpm | ||
pnpm i @xmtp/content-type-primitives | ||
``` | ||
|
||
## Developing | ||
|
||
Run `yarn dev` to build the content type primitives and watch for changes, which will trigger a rebuild. | ||
|
||
## Useful commands | ||
|
||
- `yarn build`: Builds the content type primitives | ||
- `yarn clean`: Removes `node_modules`, `dist`, and `.turbo` folders | ||
- `yarn dev`: Builds the content type and watches for changes, which will trigger a rebuild | ||
- `yarn format`: Runs Prettier format and write changes | ||
- `yarn format:check`: Runs Prettier format check | ||
- `yarn lint`: Runs ESLint | ||
- `yarn test`: Runs all unit tests | ||
- `yarn typecheck`: Runs `tsc` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{ | ||
"name": "@xmtp/content-type-primitives", | ||
"version": "1.0.0", | ||
"description": "Primitives for building custom XMTP content types", | ||
"keywords": [ | ||
"xmtp", | ||
"messaging", | ||
"web3", | ||
"js", | ||
"ts", | ||
"javascript", | ||
"typescript", | ||
"content-types" | ||
], | ||
"homepage": "https://github.com/xmtp/xmtp-js-content-types", | ||
"bugs": { | ||
"url": "https://github.com/xmtp/xmtp-js-content-types/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:xmtp/xmtp-js-content-types.git", | ||
"directory": "packages/content-type-primitives" | ||
}, | ||
"license": "MIT", | ||
"author": "XMTP Labs <[email protected]>", | ||
"sideEffects": false, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"browser": "./dist/browser/index.js", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
} | ||
}, | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.js", | ||
"browser": "dist/browser/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "yarn clean:dist && yarn rollup -c", | ||
"clean": "rimraf .turbo node_modules && yarn clean:dist", | ||
"clean:dist": "rimraf dist", | ||
"dev": "yarn clean:dist && yarn rollup -c --watch", | ||
"format": "yarn format:base -w .", | ||
"format:base": "prettier --ignore-path ../../.gitignore", | ||
"format:check": "yarn format:base -c .", | ||
"lint": "eslint . --ignore-path ../../.gitignore", | ||
"test": "yarn test:node && yarn test:jsdom", | ||
"test:jsdom": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest run --environment happy-dom", | ||
"test:node": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest run --environment node", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 3 chrome versions", | ||
"last 3 firefox versions", | ||
"last 3 safari versions" | ||
] | ||
}, | ||
"dependencies": { | ||
"@xmtp/proto": "^3.61.1" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"@types/node": "^18.19.22", | ||
"eslint": "^8.57.0", | ||
"eslint-config-custom": "workspace:*", | ||
"happy-dom": "^13.7.3", | ||
"prettier": "^3.2.5", | ||
"prettier-plugin-packagejson": "^2.4.12", | ||
"rimraf": "^5.0.5", | ||
"rollup": "^4.12.1", | ||
"rollup-plugin-dts": "^6.1.0", | ||
"rollup-plugin-filesize": "^10.0.0", | ||
"typescript": "^5.4.2", | ||
"vite": "^5.1.6", | ||
"vitest": "^1.3.1" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"provenance": true, | ||
"registry": "https://registry.npmjs.org/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { defineConfig } from "rollup"; | ||
import typescript from "@rollup/plugin-typescript"; | ||
import { dts } from "rollup-plugin-dts"; | ||
import terser from "@rollup/plugin-terser"; | ||
import filesize from "rollup-plugin-filesize"; | ||
|
||
const plugins = [ | ||
typescript({ | ||
declaration: false, | ||
declarationMap: false, | ||
}), | ||
filesize({ | ||
showMinifiedSize: false, | ||
}), | ||
]; | ||
|
||
const external = ["@xmtp/proto"]; | ||
|
||
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/browser/index.js", | ||
format: "es", | ||
sourcemap: true, | ||
}, | ||
plugins: [...plugins, terser()], | ||
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: [dts()], | ||
}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { ContentTypeId } from "."; | ||
|
||
describe("ContentTypeId", () => { | ||
it("creates a new content type", () => { | ||
const contentType = new ContentTypeId({ | ||
authorityId: "foo", | ||
typeId: "bar", | ||
versionMajor: 1, | ||
versionMinor: 0, | ||
}); | ||
expect(contentType.authorityId).toEqual("foo"); | ||
expect(contentType.typeId).toEqual("bar"); | ||
expect(contentType.versionMajor).toEqual(1); | ||
expect(contentType.versionMinor).toEqual(0); | ||
}); | ||
|
||
it("creates a string from a content type", () => { | ||
const contentType = new ContentTypeId({ | ||
authorityId: "foo", | ||
typeId: "bar", | ||
versionMajor: 1, | ||
versionMinor: 0, | ||
}); | ||
expect(contentType.toString()).toEqual("foo/bar:1.0"); | ||
}); | ||
|
||
it("creates a content type from a string", () => { | ||
const contentType = ContentTypeId.fromString("foo/bar:1.0"); | ||
expect(contentType.authorityId).toEqual("foo"); | ||
expect(contentType.typeId).toEqual("bar"); | ||
expect(contentType.versionMajor).toEqual(1); | ||
expect(contentType.versionMinor).toEqual(0); | ||
}); | ||
|
||
it("compares two content types", () => { | ||
const contentType1 = new ContentTypeId({ | ||
authorityId: "foo", | ||
typeId: "bar", | ||
versionMajor: 1, | ||
versionMinor: 0, | ||
}); | ||
const contentType2 = new ContentTypeId({ | ||
authorityId: "baz", | ||
typeId: "qux", | ||
versionMajor: 1, | ||
versionMinor: 0, | ||
}); | ||
expect(contentType1.sameAs(contentType2)).toBe(false); | ||
expect(contentType1.sameAs(contentType1)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { content } from "@xmtp/proto"; | ||
|
||
export class ContentTypeId { | ||
authorityId: string; | ||
|
||
typeId: string; | ||
|
||
versionMajor: number; | ||
|
||
versionMinor: number; | ||
|
||
constructor(obj: content.ContentTypeId) { | ||
this.authorityId = obj.authorityId; | ||
this.typeId = obj.typeId; | ||
this.versionMajor = obj.versionMajor; | ||
this.versionMinor = obj.versionMinor; | ||
} | ||
|
||
toString(): string { | ||
return `${this.authorityId}/${this.typeId}:${this.versionMajor}.${this.versionMinor}`; | ||
} | ||
|
||
static fromString(contentTypeString: string): ContentTypeId { | ||
const [idString, versionString] = contentTypeString.split(":"); | ||
const [authorityId, typeId] = idString.split("/"); | ||
const [major, minor] = versionString.split("."); | ||
return new ContentTypeId({ | ||
authorityId, | ||
typeId, | ||
versionMajor: Number(major), | ||
versionMinor: Number(minor), | ||
}); | ||
} | ||
|
||
sameAs(id: ContentTypeId): boolean { | ||
return this.authorityId === id.authorityId && this.typeId === id.typeId; | ||
} | ||
} | ||
|
||
export type EncodedContent<Parameters = Record<string, string>> = { | ||
type: ContentTypeId; | ||
parameters: Parameters; | ||
fallback?: string; | ||
compression?: number; | ||
content: Uint8Array; | ||
}; | ||
|
||
export type ContentCodec<T> = { | ||
contentType: ContentTypeId; | ||
encode(content: T): EncodedContent; | ||
decode(content: EncodedContent): T; | ||
fallback(content: T): string | undefined; | ||
shouldPush: (content: T) => boolean; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": [".", ".eslintrc.cjs", "rollup.config.js"], | ||
"exclude": ["dist", "node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "tsconfig/build.json", | ||
"include": ["src"], | ||
"exclude": ["dist", "node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters