Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic types for sub-encoder #68390

Merged
merged 7 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions types/sub-encoder/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!**/*.d.ts
!**/*.d.cts
!**/*.d.mts
!**/*.d.*.ts
49 changes: 49 additions & 0 deletions types/sub-encoder/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference types="node" />

type JsonPrimitive = string | number | boolean | null;
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved
type JsonArray = SubEncoder.JsonValue[];
interface JsonObject {
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved
[key: string]: undefined | SubEncoder.JsonValue;
}

interface Encoding<TInput, TOutput = TInput> {
input: TInput;
output: TOutput;
}

interface EncodingLookup {
ascii: Encoding<string>;
base64: Encoding<string>;
binary: Encoding<string | Buffer, Buffer>;
hex: Encoding<string>;
json: Encoding<SubEncoder.JsonValue>;
ndjson: Encoding<SubEncoder.JsonValue>;
ucs2: Encoding<string>;
"utf-8": Encoding<string>;
utf16le: Encoding<string>;
}

declare class SubEncoder<T extends keyof EncodingLookup = "binary"> {
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved
/**
* Create a new SubEncoder. Optionally set the initial prefix and encoding.
*
* `prefix` can be string or Buffer.
*/
constructor(prefix?: null | string | Buffer, encoding?: T);

/**
* Encode a key.
*/
encode(key: EncodingLookup[T]["input"]): Buffer;

/**
* Decode a key.
*/
decode(buf: Buffer): EncodingLookup[T]["output"];
}

declare namespace SubEncoder {
type JsonValue = JsonPrimitive | JsonArray | JsonObject;
}

export = SubEncoder;
20 changes: 20 additions & 0 deletions types/sub-encoder/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"private": true,
"name": "@types/sub-encoder",
"version": "2.1.9999",
"projects": [
"https://github.com/holepunchto/sub-encoder#readme"
],
"dependencies": {
"@types/node": "*"
},
"devDependencies": {
"@types/sub-encoder": "workspace:."
},
"owners": [
{
"name": "Evan Hahn",
"githubUsername": "EvanHahn"
}
]
}
49 changes: 49 additions & 0 deletions types/sub-encoder/sub-encoder-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference types="node" />

import SubEncoder = require("sub-encoder");
import type { JsonValue } from "sub-encoder";

{
const enc = new SubEncoder();

enc.encode("str"); // $ExpectType Buffer
enc.encode(Buffer.alloc(0)); // $ExpectType Buffer
enc.decode(Buffer.alloc(0)); // $ExpectType Buffer

// @ts-expect-error
enc.encode(123);
}

{
const enc = new SubEncoder(null, "binary");

enc.encode(""); // $ExpectType Buffer
enc.encode(Buffer.alloc(0)); // $ExpectType Buffer
enc.decode(Buffer.alloc(0)); // $ExpectType Buffer

// @ts-expect-error
enc.encode(123);
}

{
const STRING_ENCODINGS = ["ascii", "base64", "utf-8", "hex", "ucs2", "utf16le"] as const;

for (const encoding of STRING_ENCODINGS) {
const enc = new SubEncoder(null, encoding);
enc.encode(""); // $ExpectType Buffer
enc.decode(Buffer.alloc(0)); // $ExpectType string
}

// @ts-expect-error
enc.encode(Buffer.alloc(0));
}

{
const JSON_ENCODING = ["json", "ndjson"] as const;

for (const encoding of JSON_ENCODING) {
const enc = new SubEncoder(null, encoding);
enc.encode({ foo: "bar" }); // $ExpectType Buffer
enc.decode(Buffer.alloc(0)); // $ExpectType JsonValue
}
}
14 changes: 14 additions & 0 deletions types/sub-encoder/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "sub-encoder-tests.ts"]
}