Comments (2)
I have written type definitions that are probably 70% good enough to use but still aren't exactly complete enough to publish to DefinitelyTyped. There are use cases where the types are either too strict or not strict enough compared to how the library actually behaves.
declare module 'restructure' {
export class DecodeStream {
buffer: Buffer;
view: DataView;
pos: number;
constructor(buffer: Buffer);
readString(length: number, encoding = 'ascii'): Buffer;
readBuffer(length: number): Buffer;
// TODO: other read... methods
}
export class EncodeStream {
buffer: Buffer;
view: DataView;
pos: number;
length: number;
constructor(buffer: Buffer);
writeBuffer(buffer: Buffer): void;
writeString(string: string, encoding = 'ascii'): void;
// TODO: other write... methods
fill(val: Buffer, length: number);
}
class Base<T> {
constructor(...args: any);
fromBuffer(buffer: Buffer): T;
toBuffer(value: T): Buffer;
decode(stream: r.DecodeStream): T;
encode(stream: r.EncodeStream, val: T): void;
size(): number;
}
export type BaseType<T> = typeof Base<T>;
class BufferT extends Base<Buffer> {}
export { BufferT as Buffer };
type BaseOf<T> = T extends Base<infer U> ? U : never;
export type LengthArray<T, N, R extends T[] = []> = N extends number
? number extends N
? T[]
: R['length'] extends N
? R
: LengthArray<T, N, [T, ...R]>
: T[];
// TODO: Fix array of structs with computed properties type
export class Array<T, N extends number | string> extends Base<
LengthArray<BaseOf<T>, N>
> {
type: T;
length?: N;
lengthType?: 'count' | 'bytes';
constructor(type: T, length?: N, lengthType?: 'count' | 'bytes');
}
export class Bitfield<T extends Record<string, boolean>> extends Base<T> {}
export class Boolean extends Base<boolean> {}
export class Enum<T extends readonly string[]> extends Base<
T[number] | number
> {
constructor(type: Base<number>, options: T);
}
export class LazyArray<T> extends Base<T[]> {}
class NumberT extends Base<number> {}
export { NumberT as Number };
export class Fixed extends Number<number> {}
export class Optional<T> extends Base<T | undefined> {
constructor(type: Base<T>, condition?: boolean);
}
export class Pointer extends Base<unknown> {}
export class VoidPointer {
constructor(type: any, value: any);
}
export class Reserved extends Base<never> {}
class StringT extends Base<string> {}
export { StringT as String };
export type StructType<T extends Record<string, unknown>> = {
[K in keyof T]: T[K] extends Base<infer U>
? U
: T[K] extends () => infer V
? V
: T[K];
};
export type StructFields<T extends Record<string, unknown>> = {
[K in keyof T]: T[K] extends () => infer V
? (this: StructType<Omit<T, K>>) => V
: T[K];
};
type StructToBuffer<T extends Record<string, unknown>> = StructType<{
[K in keyof T as T[K] extends () => unknown ? never : K]: T[K];
}>;
export class Struct<T extends Record<string, unknown>> extends Base<
StructType<T>
> {
fields: StructFields<T>;
constructor(fields: StructFields<T>);
toBuffer(struct: StructToBuffer<T>): Buffer;
}
export class VersionedStruct extends Base<unknown> {}
export const uint8: NumberT;
export const uint16be: NumberT;
export const uint16 = uint16be;
export const uint16le: NumberT;
export const uint24be: NumberT;
export const uint24 = uint24be;
export const uint24le: NumberT;
export const uint32be: NumberT;
export const uint32 = uint32be;
export const uint32le: NumberT;
export const int8: NumberT;
export const int16be: NumberT;
export const int16 = int16be;
export const int16le: NumberT;
export const int24be: NumberT;
export const int24 = int24be;
export const int24le: NumberT;
export const int32be: NumberT;
export const int32 = int32be;
export const int32le: NumberT;
export const floatbe: NumberT;
export const float = floatbe;
export const floatle: NumberT;
export const doublebe: NumberT;
export const double = doublebe;
export const doublele: NumberT;
}
from restructure.
You should contribute that to https://github.com/DefinitelyTyped/DefinitelyTyped#how-can-i-contribute @Haaxor1689!
from restructure.
Related Issues (20)
- Dependencies: use node or Buffer shim with browsers HOT 8
- Opentype STAT table: stuck on pointer to array of pointers to structs HOT 3
- [idea] offsetOf for Struct
- Unusable with CSP directives HOT 14
- Missing LICENSE file. HOT 1
- Fixed-length types not encoding as expected
- feat: magic byte HOT 1
- feat: parsing stream by frames
- strategy for stream chunk-splitted stuctures HOT 2
- Int of 4 bytes not exists? HOT 1
- Causing unsafe eval issue: is it solved? HOT 1
- Release new version HOT 6
- `int64`/`uint64` support via `BigInt`
- ReferenceError: TextEncoder is not defined HOT 5
- Access field in sub struct of current struct HOT 1
- bug: utf16 strings do not correctly null-terminate
- Issue with npm Buffer dependency in DecodeStream.js HOT 1
- Parcel cache got published on npm
- Optional type, different argument for function in encode and decode
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from restructure.