Files
bun.sh/src/valkey/valkey_protocol.zig
pfg 05d0475c6c Update to zig 0.15.2 (#24204)
Fixes ENG-21287

Build times, from `bun run build && echo '//' >> src/main.zig && time
bun run build`

|Platform|0.14.1|0.15.2|Speedup|
|-|-|-|-|
|macos debug asan|126.90s|106.27s|1.19x|
|macos debug noasan|60.62s|50.85s|1.19x|
|linux debug asan|292.77s|241.45s|1.21x|
|linux debug noasan|146.58s|130.94s|1.12x|
|linux debug use_llvm=false|n/a|78.27s|1.87x|
|windows debug asan|177.13s|142.55s|1.24x|

Runtime performance:

- next build memory usage may have gone up by 5%. Otherwise seems the
same. Some code with writers may have gotten slower, especially one
instance of a counting writer and a few instances of unbuffered writers
that now have vtable overhead.
- File size reduced by 800kb (from 100.2mb to 99.4mb)

Improvements:

- `@export` hack is no longer needed for watch
- native x86_64 backend for linux builds faster. to use it, set use_llvm
false and no_link_obj false. also set `ASAN_OPTIONS=detect_leaks=0`
otherwise it will spam the output with tens of thousands of lines of
debug info errors. may need to use the zig lldb fork for debugging.
- zig test-obj, which we will be able to use for zig unit tests

Still an issue:

- false 'dependency loop' errors remain in watch mode
- watch mode crashes observed

Follow-up:

- [ ] search `comptime Writer: type` and `comptime W: type` and remove
- [ ] remove format_mode in our zig fork
- [ ] remove deprecated.zig autoFormatLabelFallback
- [ ] remove deprecated.zig autoFormatLabel
- [ ] remove deprecated.BufferedWriter and BufferedReader
- [ ] remove override_no_export_cpp_apis as it is no longer needed
- [ ] css Parser(W) -> Parser, and remove all the comptime writer: type
params
- [ ] remove deprecated writer fully

Files that add lines:

```
649     src/deprecated.zig
167     scripts/pack-codegen-for-zig-team.ts
54      scripts/cleartrace-impl.js
46      scripts/cleartrace.ts
43      src/windows.zig
18      src/fs.zig
17      src/bun.js/ConsoleObject.zig
16      src/output.zig
12      src/bun.js/test/debug.zig
12      src/bun.js/node/node_fs.zig
8       src/env_loader.zig
7       src/css/printer.zig
7       src/cli/init_command.zig
7       src/bun.js/node.zig
6       src/string/escapeRegExp.zig
6       src/install/PnpmMatcher.zig
5       src/bun.js/webcore/Blob.zig
4       src/crash_handler.zig
4       src/bun.zig
3       src/install/lockfile/bun.lock.zig
3       src/cli/update_interactive_command.zig
3       src/cli/pack_command.zig
3       build.zig
2       src/Progress.zig
2       src/install/lockfile/lockfile_json_stringify_for_debugging.zig
2       src/css/small_list.zig
2       src/bun.js/webcore/prompt.zig
1       test/internal/ban-words.test.ts
1       test/internal/ban-limits.json
1       src/watcher/WatcherTrace.zig
1       src/transpiler.zig
1       src/shell/builtin/cp.zig
1       src/js_printer.zig
1       src/io/PipeReader.zig
1       src/install/bin.zig
1       src/css/selectors/selector.zig
1       src/cli/run_command.zig
1       src/bun.js/RuntimeTranspilerStore.zig
1       src/bun.js/bindings/JSRef.zig
1       src/bake/DevServer.zig
```

Files that remove lines:

```
-1      src/test/recover.zig
-1      src/sql/postgres/SocketMonitor.zig
-1      src/sql/mysql/MySQLRequestQueue.zig
-1      src/sourcemap/CodeCoverage.zig
-1      src/css/values/color_js.zig
-1      src/compile_target.zig
-1      src/bundler/linker_context/convertStmtsForChunk.zig
-1      src/bundler/bundle_v2.zig
-1      src/bun.js/webcore/blob/read_file.zig
-1      src/ast/base.zig
-2      src/sql/postgres/protocol/ArrayList.zig
-2      src/shell/builtin/mkdir.zig
-2      src/install/PackageManager/patchPackage.zig
-2      src/install/PackageManager/PackageManagerDirectories.zig
-2      src/fmt.zig
-2      src/css/declaration.zig
-2      src/css/css_parser.zig
-2      src/collections/baby_list.zig
-2      src/bun.js/bindings/ZigStackFrame.zig
-2      src/ast/E.zig
-3      src/StandaloneModuleGraph.zig
-3      src/deps/picohttp.zig
-3      src/deps/libuv.zig
-3      src/btjs.zig
-4      src/threading/Futex.zig
-4      src/shell/builtin/touch.zig
-4      src/meta.zig
-4      src/install/lockfile.zig
-4      src/css/selectors/parser.zig
-5      src/shell/interpreter.zig
-5      src/css/error.zig
-5      src/bun.js/web_worker.zig
-5      src/bun.js.zig
-6      src/cli/test_command.zig
-6      src/bun.js/VirtualMachine.zig
-6      src/bun.js/uuid.zig
-6      src/bun.js/bindings/JSValue.zig
-9      src/bun.js/test/pretty_format.zig
-9      src/bun.js/api/BunObject.zig
-14     src/install/install_binding.zig
-14     src/fd.zig
-14     src/bun.js/node/path.zig
-14     scripts/pack-codegen-for-zig-team.sh
-17     src/bun.js/test/diff_format.zig
```

`git diff --numstat origin/main...HEAD | awk '{ print ($1-$2)"\t"$3 }' |
sort -rn`

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
Co-authored-by: Meghan Denny <meghan@bun.com>
Co-authored-by: tayor.fish <contact@taylor.fish>
2025-11-10 14:38:26 -08:00

677 lines
25 KiB
Zig

pub const RedisError = error{
AuthenticationFailed,
ConnectionClosed,
InvalidArgument,
InvalidArray,
InvalidAttribute,
InvalidBigNumber,
InvalidBlobError,
InvalidBoolean,
InvalidBulkString,
InvalidCommand,
InvalidDouble,
InvalidErrorString,
InvalidInteger,
InvalidMap,
InvalidNull,
InvalidPush,
InvalidResponse,
InvalidResponseType,
InvalidSet,
InvalidSimpleString,
InvalidVerbatimString,
JSError,
OutOfMemory,
JSTerminated,
UnsupportedProtocol,
ConnectionTimeout,
IdleTimeout,
};
pub fn valkeyErrorToJS(globalObject: *jsc.JSGlobalObject, message: ?[]const u8, err: RedisError) jsc.JSValue {
const error_code: jsc.Error = switch (err) {
error.ConnectionClosed => .REDIS_CONNECTION_CLOSED,
error.InvalidResponse => .REDIS_INVALID_RESPONSE,
error.InvalidBulkString => .REDIS_INVALID_BULK_STRING,
error.InvalidArray => .REDIS_INVALID_ARRAY,
error.InvalidInteger => .REDIS_INVALID_INTEGER,
error.InvalidSimpleString => .REDIS_INVALID_SIMPLE_STRING,
error.InvalidErrorString => .REDIS_INVALID_ERROR_STRING,
error.InvalidDouble,
error.InvalidBoolean,
error.InvalidNull,
error.InvalidMap,
error.InvalidSet,
error.InvalidBigNumber,
error.InvalidVerbatimString,
error.InvalidBlobError,
error.InvalidAttribute,
error.InvalidPush,
=> .REDIS_INVALID_RESPONSE,
error.AuthenticationFailed => .REDIS_AUTHENTICATION_FAILED,
error.InvalidCommand => .REDIS_INVALID_COMMAND,
error.InvalidArgument => .REDIS_INVALID_ARGUMENT,
error.UnsupportedProtocol => .REDIS_INVALID_RESPONSE,
error.InvalidResponseType => .REDIS_INVALID_RESPONSE_TYPE,
error.ConnectionTimeout => .REDIS_CONNECTION_TIMEOUT,
error.IdleTimeout => .REDIS_IDLE_TIMEOUT,
error.JSError => return globalObject.takeException(error.JSError),
error.OutOfMemory => globalObject.throwOutOfMemory() catch return globalObject.takeException(error.JSError),
error.JSTerminated => return globalObject.takeException(error.JSTerminated),
};
if (message) |msg| {
return error_code.fmt(globalObject, "{s}", .{msg});
}
return error_code.fmt(globalObject, "Valkey error: {s}", .{@errorName(err)});
}
// RESP protocol types
pub const RESPType = enum(u8) {
// RESP2 types
SimpleString = '+',
Error = '-',
Integer = ':',
BulkString = '$',
Array = '*',
// RESP3 types
Null = '_',
Double = ',',
Boolean = '#',
BlobError = '!',
VerbatimString = '=',
Map = '%',
Set = '~',
Attribute = '|',
Push = '>',
BigNumber = '(',
pub fn fromByte(byte: u8) ?RESPType {
return switch (byte) {
@intFromEnum(RESPType.SimpleString) => .SimpleString,
@intFromEnum(RESPType.Error) => .Error,
@intFromEnum(RESPType.Integer) => .Integer,
@intFromEnum(RESPType.BulkString) => .BulkString,
@intFromEnum(RESPType.Array) => .Array,
@intFromEnum(RESPType.Null) => .Null,
@intFromEnum(RESPType.Double) => .Double,
@intFromEnum(RESPType.Boolean) => .Boolean,
@intFromEnum(RESPType.BlobError) => .BlobError,
@intFromEnum(RESPType.VerbatimString) => .VerbatimString,
@intFromEnum(RESPType.Map) => .Map,
@intFromEnum(RESPType.Set) => .Set,
@intFromEnum(RESPType.Attribute) => .Attribute,
@intFromEnum(RESPType.Push) => .Push,
@intFromEnum(RESPType.BigNumber) => .BigNumber,
else => null,
};
}
};
pub const RESPValue = union(RESPType) {
// RESP2 types
SimpleString: []const u8,
Error: []const u8,
Integer: i64,
BulkString: ?[]const u8,
Array: []RESPValue,
// RESP3 types
Null: void,
Double: f64,
Boolean: bool,
BlobError: []const u8,
VerbatimString: VerbatimString,
Map: []MapEntry,
Set: []RESPValue,
Attribute: Attribute,
Push: Push,
BigNumber: []const u8,
pub fn deinit(self: *RESPValue, allocator: std.mem.Allocator) void {
switch (self.*) {
.SimpleString => |str| allocator.free(str),
.Error => |str| allocator.free(str),
.Integer => {},
.BulkString => |maybe_str| if (maybe_str) |str| allocator.free(str),
.Array => |array| {
for (array) |*value| {
value.deinit(allocator);
}
allocator.free(array);
},
.Null => {},
.Double => {},
.Boolean => {},
.BlobError => |str| allocator.free(str),
.VerbatimString => |*verbatim| {
allocator.free(verbatim.format);
allocator.free(verbatim.content);
},
.Map => |entries| {
for (entries) |*entry| {
entry.deinit(allocator);
}
allocator.free(entries);
},
.Set => |set| {
for (set) |*value| {
value.deinit(allocator);
}
allocator.free(set);
},
.Attribute => |*attribute| {
attribute.deinit(allocator);
},
.Push => |*push| {
push.deinit(allocator);
},
.BigNumber => |str| allocator.free(str),
}
}
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
switch (self) {
.SimpleString => |str| try writer.writeAll(str),
.Error => |str| try writer.writeAll(str),
.Integer => |int| try writer.print("{d}", .{int}),
.BulkString => |maybe_str| {
if (maybe_str) |str| {
try writer.writeAll(str);
} else {
try writer.writeAll("(nil)");
}
},
.Array => |array| {
try writer.writeAll("[");
for (array, 0..) |value, i| {
if (i > 0) try writer.writeAll(", ");
try value.format(writer);
}
try writer.writeAll("]");
},
.Null => try writer.writeAll("(nil)"),
.Double => |d| try writer.print("{d}", .{d}),
.Boolean => |b| try writer.print("{}", .{b}),
.BlobError => |str| try writer.print("Error: {s}", .{str}),
.VerbatimString => |verbatim| try writer.print("{s}:{s}", .{ verbatim.format, verbatim.content }),
.Map => |entries| {
try writer.writeAll("{");
for (entries, 0..) |entry, i| {
if (i > 0) try writer.writeAll(", ");
try entry.key.format(writer);
try writer.writeAll(": ");
try entry.value.format(writer);
}
try writer.writeAll("}");
},
.Set => |set| {
try writer.writeAll("Set{");
for (set, 0..) |value, i| {
if (i > 0) try writer.writeAll(", ");
try value.format(writer);
}
try writer.writeAll("}");
},
.Attribute => |attribute| {
try writer.writeAll("(Attr: ");
try writer.writeAll("{");
for (attribute.attributes, 0..) |entry, i| {
if (i > 0) try writer.writeAll(", ");
try entry.key.format(writer);
try writer.writeAll(": ");
try entry.value.format(writer);
}
try writer.writeAll("} => ");
try attribute.value.format(writer);
try writer.writeAll(")");
},
.Push => |push| {
try writer.print("Push({s}: [", .{push.kind});
for (push.data, 0..) |value, i| {
if (i > 0) try writer.writeAll(", ");
try value.format(writer);
}
try writer.writeAll("])");
},
.BigNumber => |str| try writer.print("BigNumber({s})", .{str}),
}
}
pub fn toJS(self: *RESPValue, globalObject: *jsc.JSGlobalObject) bun.JSError!jsc.JSValue {
return self.toJSWithOptions(globalObject, .{});
}
pub const ToJSOptions = struct {
return_as_buffer: bool = false,
};
fn valkeyStrToJSValue(globalObject: *jsc.JSGlobalObject, str: []const u8, options: *const ToJSOptions) bun.JSError!jsc.JSValue {
if (options.return_as_buffer) {
// TODO: handle values > 4.7 GB
return try jsc.ArrayBuffer.createBuffer(globalObject, str);
} else {
return bun.String.createUTF8ForJS(globalObject, str);
}
}
pub fn toJSWithOptions(self: *RESPValue, globalObject: *jsc.JSGlobalObject, options: ToJSOptions) bun.JSError!jsc.JSValue {
switch (self.*) {
.SimpleString => |str| return valkeyStrToJSValue(globalObject, str, &options),
.Error => |str| return valkeyErrorToJS(globalObject, str, RedisError.InvalidResponse),
.Integer => |int| return jsc.JSValue.jsNumber(int),
.BulkString => |maybe_str| {
if (maybe_str) |str| {
return valkeyStrToJSValue(globalObject, str, &options);
} else {
return jsc.JSValue.jsNull();
}
},
.Array => |array| {
var js_array = try jsc.JSValue.createEmptyArray(globalObject, array.len);
for (array, 0..) |*item, i| {
const js_item = try item.toJSWithOptions(globalObject, options);
try js_array.putIndex(globalObject, @intCast(i), js_item);
}
return js_array;
},
.Null => return jsc.JSValue.jsNull(),
.Double => |d| return jsc.JSValue.jsNumber(d),
.Boolean => |b| return jsc.JSValue.jsBoolean(b),
.BlobError => |str| return valkeyErrorToJS(globalObject, str, RedisError.InvalidBlobError),
.VerbatimString => |verbatim| return valkeyStrToJSValue(globalObject, verbatim.content, &options),
.Map => |entries| {
var js_obj = jsc.JSValue.createEmptyObjectWithNullPrototype(globalObject);
for (entries) |*entry| {
const js_key = try entry.key.toJSWithOptions(globalObject, .{});
var key_str = try js_key.toBunString(globalObject);
defer key_str.deref();
const js_value = try entry.value.toJSWithOptions(globalObject, options);
try js_obj.putMayBeIndex(globalObject, &key_str, js_value);
}
return js_obj;
},
.Set => |set| {
var js_array = try jsc.JSValue.createEmptyArray(globalObject, set.len);
for (set, 0..) |*item, i| {
const js_item = try item.toJSWithOptions(globalObject, options);
try js_array.putIndex(globalObject, @intCast(i), js_item);
}
return js_array;
},
.Attribute => |attribute| {
// For now, we just return the value and ignore attributes
// In the future, we could attach the attributes as a hidden property
return try attribute.value.toJSWithOptions(globalObject, options);
},
.Push => |push| {
var js_obj = jsc.JSValue.createEmptyObjectWithNullPrototype(globalObject);
// Add the push type
const kind_str = try bun.String.createUTF8ForJS(globalObject, push.kind);
js_obj.put(globalObject, "type", kind_str);
// Add the data as an array
var data_array = try jsc.JSValue.createEmptyArray(globalObject, push.data.len);
for (push.data, 0..) |*item, i| {
const js_item = try item.toJSWithOptions(globalObject, options);
try data_array.putIndex(globalObject, @intCast(i), js_item);
}
js_obj.put(globalObject, "data", data_array);
return js_obj;
},
.BigNumber => |str| {
// Try to parse as number if possible
if (std.fmt.parseInt(i64, str, 10)) |int| {
return jsc.JSValue.jsNumber(int);
} else |_| {
// If it doesn't fit in an i64, return as string
return bun.String.createUTF8ForJS(globalObject, str);
}
},
}
}
};
pub const ValkeyReader = struct {
buffer: []const u8,
pos: usize = 0,
pub fn init(buffer: []const u8) ValkeyReader {
return .{
.buffer = buffer,
};
}
pub fn readByte(self: *ValkeyReader) RedisError!u8 {
if (self.pos >= self.buffer.len) return error.InvalidResponse;
const byte = self.buffer[self.pos];
self.pos += 1;
return byte;
}
pub fn readUntilCRLF(self: *ValkeyReader) RedisError![]const u8 {
const buffer = self.buffer[self.pos..];
for (buffer, 0..) |byte, i| {
if (byte == '\r' and buffer.len > i + 1 and buffer[i + 1] == '\n') {
const result = buffer[0..i];
self.pos += i + 2;
return result;
}
}
return error.InvalidResponse;
}
pub fn readInteger(self: *ValkeyReader) RedisError!i64 {
const str = try self.readUntilCRLF();
return std.fmt.parseInt(i64, str, 10) catch return error.InvalidInteger;
}
pub fn readDouble(self: *ValkeyReader) RedisError!f64 {
const str = try self.readUntilCRLF();
// Handle special values
if (std.mem.eql(u8, str, "inf")) return std.math.inf(f64);
if (std.mem.eql(u8, str, "-inf")) return -std.math.inf(f64);
if (std.mem.eql(u8, str, "nan")) return std.math.nan(f64);
// Parse normal double
return std.fmt.parseFloat(f64, str) catch return error.InvalidDouble;
}
pub fn readBoolean(self: *ValkeyReader) RedisError!bool {
const str = try self.readUntilCRLF();
if (str.len != 1) return error.InvalidBoolean;
return switch (str[0]) {
't' => true,
'f' => false,
else => error.InvalidBoolean,
};
}
pub fn readVerbatimString(self: *ValkeyReader, allocator: std.mem.Allocator) RedisError!VerbatimString {
const len = try self.readInteger();
if (len < 0) return error.InvalidVerbatimString;
if (self.pos + @as(usize, @intCast(len)) > self.buffer.len) return error.InvalidVerbatimString;
const content_with_format = self.buffer[self.pos .. self.pos + @as(usize, @intCast(len))];
self.pos += @as(usize, @intCast(len));
// Expect CRLF after content
const crlf = try self.readUntilCRLF();
if (crlf.len != 0) return error.InvalidVerbatimString;
// Format should be "xxx:" followed by content
if (content_with_format.len < 4 or content_with_format[3] != ':') {
return error.InvalidVerbatimString;
}
const format = try allocator.dupe(u8, content_with_format[0..3]);
const content = try allocator.dupe(u8, content_with_format[4..]);
return VerbatimString{
.format = format,
.content = content,
};
}
pub fn readValue(self: *ValkeyReader, allocator: std.mem.Allocator) RedisError!RESPValue {
const type_byte = try self.readByte();
return switch (RESPType.fromByte(type_byte) orelse return error.InvalidResponseType) {
// RESP2 types
.SimpleString => {
const str = try self.readUntilCRLF();
const owned = try allocator.dupe(u8, str);
return RESPValue{ .SimpleString = owned };
},
.Error => {
const str = try self.readUntilCRLF();
const owned = try allocator.dupe(u8, str);
return RESPValue{ .Error = owned };
},
.Integer => {
const int = try self.readInteger();
return RESPValue{ .Integer = int };
},
.BulkString => {
const len = try self.readInteger();
if (len < 0) return RESPValue{ .BulkString = null };
if (self.pos + @as(usize, @intCast(len)) > self.buffer.len) return error.InvalidResponse;
const str = self.buffer[self.pos .. self.pos + @as(usize, @intCast(len))];
self.pos += @as(usize, @intCast(len));
const crlf = try self.readUntilCRLF();
if (crlf.len != 0) return error.InvalidBulkString;
const owned = try allocator.dupe(u8, str);
return RESPValue{ .BulkString = owned };
},
.Array => {
const len = try self.readInteger();
if (len < 0) return RESPValue{ .Array = &[_]RESPValue{} };
const array = try allocator.alloc(RESPValue, @as(usize, @intCast(len)));
errdefer allocator.free(array);
var i: usize = 0;
errdefer {
for (array[0..i]) |*item| {
item.deinit(allocator);
}
}
while (i < len) : (i += 1) {
array[i] = try self.readValue(allocator);
}
return RESPValue{ .Array = array };
},
// RESP3 types
.Null => {
_ = try self.readUntilCRLF(); // Read and discard CRLF
return RESPValue{ .Null = {} };
},
.Double => {
const d = try self.readDouble();
return RESPValue{ .Double = d };
},
.Boolean => {
const b = try self.readBoolean();
return RESPValue{ .Boolean = b };
},
.BlobError => {
const len = try self.readInteger();
if (len < 0) return error.InvalidBlobError;
if (self.pos + @as(usize, @intCast(len)) > self.buffer.len) return error.InvalidBlobError;
const str = self.buffer[self.pos .. self.pos + @as(usize, @intCast(len))];
self.pos += @as(usize, @intCast(len));
const crlf = try self.readUntilCRLF();
if (crlf.len != 0) return error.InvalidBlobError;
const owned = try allocator.dupe(u8, str);
return RESPValue{ .BlobError = owned };
},
.VerbatimString => {
return RESPValue{ .VerbatimString = try self.readVerbatimString(allocator) };
},
.Map => {
const len = try self.readInteger();
if (len < 0) return error.InvalidMap;
const entries = try allocator.alloc(MapEntry, @as(usize, @intCast(len)));
errdefer allocator.free(entries);
var i: usize = 0;
errdefer {
for (entries[0..i]) |*entry| {
entry.deinit(allocator);
}
}
while (i < len) : (i += 1) {
entries[i] = .{ .key = try self.readValue(allocator), .value = try self.readValue(allocator) };
}
return RESPValue{ .Map = entries };
},
.Set => {
const len = try self.readInteger();
if (len < 0) return error.InvalidSet;
var set = try allocator.alloc(RESPValue, @as(usize, @intCast(len)));
errdefer allocator.free(set);
var i: usize = 0;
errdefer {
for (set[0..i]) |*item| {
item.deinit(allocator);
}
}
while (i < len) : (i += 1) {
set[i] = try self.readValue(allocator);
}
return RESPValue{ .Set = set };
},
.Attribute => {
const len = try self.readInteger();
if (len < 0) return error.InvalidAttribute;
var attrs = try allocator.alloc(MapEntry, @as(usize, @intCast(len)));
errdefer allocator.free(attrs);
var i: usize = 0;
errdefer {
for (attrs[0..i]) |*entry| {
entry.deinit(allocator);
}
}
while (i < len) : (i += 1) {
var key = try self.readValue(allocator);
errdefer key.deinit(allocator);
const value = try self.readValue(allocator);
attrs[i] = .{ .key = key, .value = value };
}
// Read the actual value that follows the attributes
const value_ptr = try allocator.create(RESPValue);
errdefer {
allocator.destroy(value_ptr);
}
value_ptr.* = try self.readValue(allocator);
return RESPValue{ .Attribute = .{
.attributes = attrs,
.value = value_ptr,
} };
},
.Push => {
const len = try self.readInteger();
if (len < 0 or len == 0) return error.InvalidPush;
// First element is the push type
const push_type = try self.readValue(allocator);
var push_type_str: []const u8 = "";
switch (push_type) {
.SimpleString => |str| push_type_str = str,
.BulkString => |maybe_str| {
if (maybe_str) |str| {
push_type_str = str;
} else {
return error.InvalidPush;
}
},
else => return error.InvalidPush,
}
// Copy the push type string since the original will be freed
const push_type_dup = try allocator.dupe(u8, push_type_str);
errdefer allocator.free(push_type_dup);
// Read the rest of the data
var data = try allocator.alloc(RESPValue, @as(usize, @intCast(len - 1)));
errdefer allocator.free(data);
var i: usize = 0;
errdefer {
for (data[0..i]) |*item| {
item.deinit(allocator);
}
}
while (i < len - 1) : (i += 1) {
data[i] = try self.readValue(allocator);
}
return RESPValue{ .Push = .{
.kind = push_type_dup,
.data = data,
} };
},
.BigNumber => {
const str = try self.readUntilCRLF();
const owned = try allocator.dupe(u8, str);
return RESPValue{ .BigNumber = owned };
},
};
}
};
pub const MapEntry = struct {
key: RESPValue,
value: RESPValue,
pub fn deinit(self: *MapEntry, allocator: std.mem.Allocator) void {
self.key.deinit(allocator);
self.value.deinit(allocator);
}
};
pub const VerbatimString = struct {
format: []const u8, // e.g. "txt" or "mkd"
content: []const u8,
pub fn deinit(self: *VerbatimString, allocator: std.mem.Allocator) void {
allocator.free(self.format);
allocator.free(self.content);
}
};
pub const Push = struct {
kind: []const u8,
data: []RESPValue,
pub fn deinit(self: *Push, allocator: std.mem.Allocator) void {
allocator.free(self.kind);
for (self.data) |*item| {
item.deinit(allocator);
}
allocator.free(self.data);
}
};
pub const Attribute = struct {
attributes: []MapEntry,
value: *RESPValue,
pub fn deinit(self: *Attribute, allocator: std.mem.Allocator) void {
for (self.attributes) |*entry| {
entry.deinit(allocator);
}
allocator.free(self.attributes);
self.value.deinit(allocator);
allocator.destroy(self.value);
}
};
pub const SubscriptionPushMessage = enum(u2) {
message,
subscribe,
unsubscribe,
pub const map = bun.ComptimeStringMap(SubscriptionPushMessage, .{
.{ "message", .message },
.{ "subscribe", .subscribe },
.{ "unsubscribe", .unsubscribe },
});
};
const std = @import("std");
const bun = @import("bun");
const String = bun.String;
const jsc = bun.jsc;