• Joined on 2024-08-09

@sek1ro/serialize (0.3.1)

Published 2025-08-09 17:23:18 +03:00 by sek1ro in sek1ro/serialize-js

Installation

@sek1ro:registry=
npm install @sek1ro/serialize@0.3.1
"@sek1ro/serialize": "0.3.1"

About this package

serialize-js

import * as s from '@sek1ro/serialize'

const User = s.Struct({
  id: s.Int(32, 'unsigned'),
  name: String,
  age: Number,
  array: [Array, Boolean],
  raw_data: DataView,
})

const user = {
  id: 1,
  name: 'john',
  age: 20,
  array: [true, false],
  raw_data: s.filledDataView([0x00, 0x00, 0x00, 0xff]),
}

const dv = s.sizedDataView(sizeof(user, User))
s.serialize(dv, user, User)

const user_copy = s.parse(dv, User)

Contents

Types

ES types

  • Boolean
  • Number
  • String (headless)
  • Array (headless)
  • DataView (headless)

Built-in types

  • Int(bits: 8 | 16 | 32, sign: 'signed' | 'unsigned')
  • ConstString(size: number)
  • ConstArray(size: number)
  • ConstDataView(byte_size: number)
  • Struct(type_obj: Record<string, SerializableType | SerializableType[]>)
  • Sequence(types: (SerializableType | SerializableType[])[])

Headless

If type has variable size in bytes, if sizeof type different for specific object, it's called headless.

const array = [true, false]

sizeof(ConstArray(2), Boolean) // 2
sizeof(array, ConstArray(2), Boolean) // 2

sizeof(Array, Boolean) // Error: unknown sizeof Array, Boolean

sizeof(array, Array, Boolean) // 2

Library has function isHeadless(...types: SerializableType[]) to check this. It's used in Structure, if type is headless, in head of structure it's stored as uint32 offset to beginning of type's body, if type isn't headless, it's stored directly in head of structure

Dependencies

Development Dependencies

ID Version
@vitest/coverage-v8 ^3.2.4
esbuild ^0.25.8
prettier ^3.6.2
rollup ^4.46.2
rollup-plugin-dts ^6.2.1
vitest ^3.2.4
Details
npm
2025-08-09 17:23:18 +03:00
1
Vlad Litvinov
LGPL-3.0-only
16 KiB
Assets (1)
Versions (12) View all
0.3.2 2025-08-09
0.3.1 2025-08-09
0.3.0 2025-08-08
0.2.2 2025-08-06
0.2.1 2025-08-05