node: fix test-util-parse-env.js (#17701)

This commit is contained in:
Meghan Denny
2025-02-28 20:15:18 -08:00
committed by GitHub
parent 7882418c5f
commit 2d74c0162a
5 changed files with 104 additions and 5 deletions

View File

@@ -7,6 +7,8 @@ const string = bun.string;
const Output = bun.Output;
const ZigString = JSC.ZigString;
const uv = bun.windows.libuv;
const validators = @import("./util/validators.zig");
const envloader = @import("./../../env_loader.zig");
pub fn internalErrorName(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue {
const arguments = callframe.arguments_old(1).slice();
@@ -212,3 +214,24 @@ pub fn normalizeEncoding(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFr
if (str.inMapCaseInsensitive(JSC.Node.Encoding.map)) |enc| return enc.toJS(globalThis);
return JSC.JSValue.jsUndefined();
}
pub fn parseEnv(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue {
const content = callframe.argument(0);
try validators.validateString(globalThis, content, "content", .{});
var arena = std.heap.ArenaAllocator.init(bun.default_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const str = content.asString().toSlice(globalThis, allocator);
var map = envloader.Map.init(allocator);
var p = envloader.Loader.init(&map, allocator);
p.loadFromString(str.slice(), true, false);
var obj = JSC.JSValue.createEmptyObject(globalThis, map.map.count());
for (map.map.keys(), map.map.values()) |k, v| {
obj.put(globalThis, JSC.ZigString.initUTF8(k), bun.String.createUTF8ForJS(globalThis, v.value));
}
return obj;
}