Fix integer encoding and decoding

This commit is contained in:
luaneko 2025-01-09 04:37:30 +11:00
parent 654c564aff
commit 5dadd7c5a2
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
4 changed files with 52 additions and 50 deletions

8
deno.lock generated
View File

@ -444,6 +444,12 @@
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/events.ts": "c4f2c856cbc7ac5d93b9af9b83d9550db7427cead32514a10424082e492005ae", "https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/events.ts": "c4f2c856cbc7ac5d93b9af9b83d9550db7427cead32514a10424082e492005ae",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/func.ts": "f1935f673365cd68939531d65ef18fe81b5d43dc795b03c34bb5ad821ab1c9ff", "https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/func.ts": "f1935f673365cd68939531d65ef18fe81b5d43dc795b03c34bb5ad821ab1c9ff",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/jit.ts": "260ab418fbc55a5dec594f023c84d36f8d420fd3239e3d27648cba1b9a0e05b1", "https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/jit.ts": "260ab418fbc55a5dec594f023c84d36f8d420fd3239e3d27648cba1b9a0e05b1",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/mod.ts": "dd9271f4e5aae4bfb1ec6b0800697ded12e4178af915acb2b96b97614ae8c8d9" "https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/mod.ts": "dd9271f4e5aae4bfb1ec6b0800697ded12e4178af915acb2b96b97614ae8c8d9",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.5/async.ts": "20bc54c7260c2d2cd27ffcca33b903dde57a3a3635386d8e0c6baca4b253ae4e",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.5/bytes.ts": "b9816e75afa878202529b11431dfd77f5f14f71e67c3f15852fd651f6801d8bb",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.5/events.ts": "c4f2c856cbc7ac5d93b9af9b83d9550db7427cead32514a10424082e492005ae",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.5/func.ts": "f1935f673365cd68939531d65ef18fe81b5d43dc795b03c34bb5ad821ab1c9ff",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.5/jit.ts": "260ab418fbc55a5dec594f023c84d36f8d420fd3239e3d27648cba1b9a0e05b1",
"https://git.lua.re/luaneko/lstd/raw/tag/0.1.5/mod.ts": "95d8b15048a54cb82391825831f695b74e7c8b206317264a99c906ce25c63f13"
} }
} }

View File

@ -1 +1 @@
export * from "https://git.lua.re/luaneko/lstd/raw/tag/0.1.3/mod.ts"; export * from "https://git.lua.re/luaneko/lstd/raw/tag/0.1.5/mod.ts";

36
ser.ts
View File

@ -1,3 +1,9 @@
import { read_i8 } from "./lstd.ts";
import { read_i16_be } from "./lstd.ts";
import { read_i32_be } from "./lstd.ts";
import { write_i32_be } from "./lstd.ts";
import { write_i16_be } from "./lstd.ts";
import { write_i8 } from "./lstd.ts";
import { encode_utf8, from_utf8, jit } from "./lstd.ts"; import { encode_utf8, from_utf8, jit } from "./lstd.ts";
export class EncoderError extends Error { export class EncoderError extends Error {
@ -40,34 +46,30 @@ export function sum_const_size(...ns: (number | null)[]) {
} }
// https://www.postgresql.org/docs/current/protocol-message-types.html#PROTOCOL-MESSAGE-TYPES // https://www.postgresql.org/docs/current/protocol-message-types.html#PROTOCOL-MESSAGE-TYPES
export const u8: Encoder<number> = { export const i8: Encoder<number> = {
const_size: 1, const_size: 1,
allocs() { allocs() {
return 1; return 1;
}, },
encode(buf, cur, n) { encode(buf, cur, n) {
buf[cur.i++] = n & 0xff; write_i8(buf, n, cur.i++);
}, },
decode(buf, cur) { decode(buf, cur) {
return buf[cur.i++]; return read_i8(buf, cur.i++);
}, },
}; };
export const u16: Encoder<number> = { export const i16: Encoder<number> = {
const_size: 2, const_size: 2,
allocs() { allocs() {
return 2; return 2;
}, },
encode(buf, cur, n) { encode(buf, cur, n) {
let { i } = cur; write_i16_be(buf, n, cur.i), (cur.i += 2);
buf[i++] = (n >>> 8) & 0xff;
buf[i++] = n & 0xff;
cur.i = i;
}, },
decode(buf, cur) { decode(buf, cur) {
let { i } = cur; const n = read_i16_be(buf, cur.i);
const n = (buf[i++] << 8) + buf[i++]; return (cur.i += 2), n;
return (cur.i = i), n;
}, },
}; };
@ -77,17 +79,11 @@ export const i32: Encoder<number> = {
return 4; return 4;
}, },
encode(buf, cur, n) { encode(buf, cur, n) {
let { i } = cur; write_i32_be(buf, n, cur.i), (cur.i += 4);
buf[i++] = (n >>> 24) & 0xff;
buf[i++] = (n >>> 16) & 0xff;
buf[i++] = (n >>> 8) & 0xff;
buf[i++] = n & 0xff;
cur.i = i;
}, },
decode(buf, cur) { decode(buf, cur) {
let { i } = cur; const n = read_i32_be(buf, cur.i);
const n = (buf[i++] << 24) + (buf[i++] << 16) + (buf[i++] << 8) + buf[i++]; return (cur.i += 4), n;
return (cur.i = i), n;
}, },
}; };

56
wire.ts
View File

@ -25,9 +25,9 @@ import {
oneof, oneof,
ser_decode, ser_decode,
ser_encode, ser_encode,
u16, i16,
i32, i32,
u8, i8,
sum_const_size, sum_const_size,
} from "./ser.ts"; } from "./ser.ts";
import { import {
@ -119,7 +119,7 @@ function msg<T extends string, S extends ObjectShape>(
shape: S shape: S
): MessageEncoder<T, S> { ): MessageEncoder<T, S> {
const header_size = type !== "" ? 5 : 4; const header_size = type !== "" ? 5 : 4;
const ty = type !== "" ? oneof(char(u8), type) : null; const ty = type !== "" ? oneof(char(i8), type) : null;
const fields = object(shape); const fields = object(shape);
return { return {
@ -160,7 +160,7 @@ function msg_check_err(msg: Uint8Array) {
// https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS // https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS
export const Header = object({ export const Header = object({
type: char(u8), type: char(i8),
length: i32, length: i32,
}); });
@ -237,9 +237,9 @@ export const BackendKeyData = msg("K", {
export const Bind = msg("B", { export const Bind = msg("B", {
portal: cstring, portal: cstring,
statement: cstring, statement: cstring,
param_formats: array(u16, u16), param_formats: array(i16, i16),
param_values: array(u16, byten_lp), param_values: array(i16, byten_lp),
column_formats: array(u16, u16), column_formats: array(i16, i16),
}); });
export const BindComplete = msg("2", {}); export const BindComplete = msg("2", {});
@ -251,7 +251,7 @@ export const CancelRequest = msg("", {
}); });
export const Close = msg("C", { export const Close = msg("C", {
which: oneof(char(u8), "S" as const, "P" as const), which: oneof(char(i8), "S" as const, "P" as const),
name: cstring, name: cstring,
}); });
@ -262,32 +262,32 @@ export const CopyDone = msg("c", {});
export const CopyFail = msg("f", { cause: cstring }); export const CopyFail = msg("f", { cause: cstring });
export const CopyInResponse = msg("G", { export const CopyInResponse = msg("G", {
format: u8, format: i8,
column_formats: array(u16, u16), column_formats: array(i16, i16),
}); });
export const CopyOutResponse = msg("H", { export const CopyOutResponse = msg("H", {
format: u8, format: i8,
column_formats: array(u16, u16), column_formats: array(i16, i16),
}); });
export const CopyBothResponse = msg("W", { export const CopyBothResponse = msg("W", {
format: u8, format: i8,
column_formats: array(u16, u16), column_formats: array(i16, i16),
}); });
export const DataRow = msg("D", { export const DataRow = msg("D", {
column_values: array(u16, byten_lp), column_values: array(i16, byten_lp),
}); });
export const Describe = msg("D", { export const Describe = msg("D", {
which: oneof(char(u8), "S" as const, "P" as const), which: oneof(char(i8), "S" as const, "P" as const),
name: cstring, name: cstring,
}); });
export const EmptyQueryResponse = msg("I", {}); export const EmptyQueryResponse = msg("I", {});
const err_field = char(u8); const err_field = char(i8);
const err_fields: Encoder<Record<string, string>> = { const err_fields: Encoder<Record<string, string>> = {
const_size: null, const_size: null,
allocs(x) { allocs(x) {
@ -325,9 +325,9 @@ export const Flush = msg("H", {});
export const FunctionCall = msg("F", { export const FunctionCall = msg("F", {
oid: i32, oid: i32,
arg_formats: array(u16, u16), arg_formats: array(i16, i16),
arg_values: array(u16, byten_lp), arg_values: array(i16, byten_lp),
result_format: u16, result_format: i16,
}); });
export const FunctionCallResponse = msg("V", { export const FunctionCallResponse = msg("V", {
@ -352,7 +352,7 @@ export const NotificationResponse = msg("A", {
}); });
export const ParameterDescription = msg("t", { export const ParameterDescription = msg("t", {
param_types: array(u16, i32), param_types: array(i16, i32),
}); });
export const ParameterStatus = msg("S", { export const ParameterStatus = msg("S", {
@ -363,7 +363,7 @@ export const ParameterStatus = msg("S", {
export const Parse = msg("P", { export const Parse = msg("P", {
statement: cstring, statement: cstring,
query: cstring, query: cstring,
param_types: array(u16, i32), param_types: array(i16, i32),
}); });
export const ParseComplete = msg("1", {}); export const ParseComplete = msg("1", {});
@ -379,20 +379,20 @@ export const QueryMessage = msg("Q", {
}); });
export const ReadyForQuery = msg("Z", { export const ReadyForQuery = msg("Z", {
tx_status: oneof(char(u8), "I" as const, "T" as const, "E" as const), tx_status: oneof(char(i8), "I" as const, "T" as const, "E" as const),
}); });
export const RowDescription = msg("T", { export const RowDescription = msg("T", {
columns: array( columns: array(
u16, i16,
object({ object({
name: cstring, name: cstring,
table_oid: i32, table_oid: i32,
table_column: u16, table_column: i16,
type_oid: i32, type_oid: i32,
type_size: u16, type_size: i16,
type_modifier: i32, type_modifier: i32,
format: u16, format: i16,
}) })
), ),
}); });
@ -421,7 +421,7 @@ export const StartupMessage = msg("", {
for (const { 0: key, 1: value } of Object.entries(x)) { for (const { 0: key, 1: value } of Object.entries(x)) {
cstring.encode(buf, cur, key), cstring.encode(buf, cur, value); cstring.encode(buf, cur, key), cstring.encode(buf, cur, value);
} }
u8.encode(buf, cur, 0); i8.encode(buf, cur, 0);
}, },
decode(buf, cur) { decode(buf, cur) {
const x: Record<string, string> = {}; const x: Record<string, string> = {};