Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arumi-s committed Jul 30, 2024
0 parents commit 2bd456e
Show file tree
Hide file tree
Showing 19 changed files with 5,232 additions and 0 deletions.
76 changes: 76 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
test:
if: "!contains(github.event.head_commit.message, 'skip-ci')"

strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [18.x, 20.x]

runs-on: ${{ matrix.os }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Cache ~/.pnpm-store
uses: actions/cache@v2
env:
cache-name: cache-pnpm-store
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-${{ matrix.node-version }}-test-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-${{ matrix.node-version }}-test-${{ env.cache-name }}-
${{ runner.os }}-${{ matrix.node-version }}-test-
${{ runner.os }}-
- name: Install pnpm
run: npm i -g pnpm

- name: Install deps
run: pnpm i

# Runs a set of commands using the runners shell
- name: Build and Test
run: pnpm test

release:
runs-on: ubuntu-latest
needs: ['test']
if: "!contains(github.event.head_commit.message, 'skip-release') && !contains(github.event.head_commit.message, 'skip-ci') && github.event_name != 'pull_request'"
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20.x
- name: Cache ~/.pnpm-store
uses: actions/cache@v2
env:
cache-name: cache-pnpm-store
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-${{ matrix.node-version }}-release-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-${{ matrix.node-version }}-release-${{ env.cache-name }}-
${{ runner.os }}-${{ matrix.node-version }}-release-
${{ runner.os }}-
- run: npm i -g pnpm
- run: pnpm i
- run: pnpm dlx semantic-release@20 --branches master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.DS_Store
dist
coverage
*.log
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 140,
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 2,
"useTabs": true,
"endOfLine": "lf"
}
3 changes: 3 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"branches": ["master"]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Arumi Sakura

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.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# zod-pbf-binary-serializer

[![npm version](https://badgen.net/npm/v/zod-pbf-binary-serializer)](https://npm.im/zod-pbf-binary-serializer) [![npm downloads](https://badgen.net/npm/dm/zod-pbf-binary-serializer)](https://npm.im/zod-pbf-binary-serializer)

Serialize and deserialize [zod](https://github.com/colinhacks/zod) schemas to and from a compact binary format.

Bundles the [pbf](https://github.com/mapbox/pbf) package for binary data manipulation.

Requires zod v3 or later.

## Install

```bash
npm i zod-pbf-binary-serializer
```

## Supported Schemas

- z.string
- z.number
- z.boolean
- z.array
- z.object
- z.instanceof(Uint8Array)
- z.discriminatedUnion
- z.literal(string | number | boolean)

## Usage

```typescript
import { fromSchema } from 'zod-pbf-binary-serializer';

const schema = z.object({
a: z.string(),
b: z.number(),
c: z.boolean(),
d: z.array(z.string()),
});

const serializer = fromSchema(schema);

const data = {
a: 'apple',
b: 123,
c: true,
d: ['hello', 'world'],
};

const buffer = serializer.encode(data);
console.log(buffer);
/**
* Uint8Array(28) [
* 5, 97, 112, 112, 108, 101,
* 0, 0, 0, 0, 0, 192, 94, 64,
* 1,
* 2, 5, 104, 101, 108, 108, 111, 5, 119, 111, 114, 108, 100
* ]
*/

const decoded = serializer.decode(buffer);
console.log(decoded);
// { a: 'apple', b: 123, c: true, d: ['hello', 'world'] }
```

## License

[MIT](https://github.com/arumi-s/zod-pbf-binary-serializer/blob/master/LICENSE)
59 changes: 59 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "zod-pbf-binary-serializer",
"version": "1.0.0",
"description": "Serialize and deserialize zod schemas to and from a compact binary format",
"type": "module",
"sideEffects": false,
"author": {
"name": "Arumi Sakura",
"url": "https://github.com/arumi-s"
},
"repository": {
"type": "git",
"url": "git+https://github.com/arumi-s/zod-pbf-binary-serializer.git"
},
"keywords": [
"zod",
"binary",
"serialization"
],
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
"require": "./dist/index.cjs",
"import": "./dist/index.js"
},
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsup --config tsup.config.ts",
"test": "vitest run",
"prepublishOnly": "pnpm run build"
},
"license": "MIT",
"peerDependencies": {
"zod": "^3"
},
"dependencies": {
"pbf": "^4.0.1"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^13.0.0",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^10.1.3",
"@semantic-release/npm": "^12.0.1",
"@semantic-release/release-notes-generator": "^14.0.1",
"@vitest/coverage-v8": "^2.0.4",
"prettier": "3.3.3",
"semantic-release": "^24.0.0",
"tsup": "8.2.3",
"typescript": "5.5.4",
"vitest": "2.0.4"
}
}
Loading

0 comments on commit 2bd456e

Please sign in to comment.