Skip to content

Lightpanda: the headless browser designed for AI and automation

Add object store name support + DOMStringList

Karl Seguin91901ca00702
diff --git a/src/browser/webapi/collections.zig b/src/browser/webapi/collections.zig
index 4a5a5384a..eeb884929 100644
--- a/src/browser/webapi/collections.zig
+++ b/src/browser/webapi/collections.zig
@@ -19,6 +19,7 @@
pub const NodeLive = @import("collections/node_live.zig").NodeLive;
pub const ChildNodes = @import("collections/ChildNodes.zig");
pub const DOMTokenList = @import("collections/DOMTokenList.zig");
+pub const DOMStringList = @import("collections/DOMStringList.zig");
pub const RadioNodeList = @import("collections/RadioNodeList.zig");
pub const HTMLCollection = @import("collections/HTMLCollection.zig");
pub const HTMLAllCollection = @import("collections/HTMLAllCollection.zig");
@@ -42,5 +43,9 @@ pub fn registerTypes() []const type {
DOMTokenList.KeyIterator,
DOMTokenList.ValueIterator,
DOMTokenList.EntryIterator,
+ DOMStringList,
+ DOMStringList.KeyIterator,
+ DOMStringList.ValueIterator,
+ DOMStringList.EntryIterator,
};
}
diff --git a/src/browser/webapi/collections/DOMStringList.zig b/src/browser/webapi/collections/DOMStringList.zig
new file mode 100644
index 000000000..c5224a8c5
--- /dev/null
+++ b/src/browser/webapi/collections/DOMStringList.zig
@@ -0,0 +1,127 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const js = @import("../../js/js.zig");
+const Page = @import("../../Page.zig");
+const Frame = @import("../../Frame.zig");
+const GenericIterator = @import("iterator.zig").Entry;
+
+pub const KeyIterator = GenericIterator(Iterator, "0");
+pub const ValueIterator = GenericIterator(Iterator, "1");
+pub const EntryIterator = GenericIterator(Iterator, null);
+
+const Execution = js.Execution;
+const Allocator = std.mem.Allocator;
+
+pub const DOMStringList = @This();
+
+_rc: lp.RC(u8) = .{},
+_arena: Allocator,
+_items: []const []const u8,
+
+pub fn acquireRef(self: *DOMStringList) void {
+ self._rc.acquire();
+}
+
+pub fn deinit(self: *DOMStringList, page: *Page) void {
+ page.releaseArena(self._arena);
+}
+
+pub fn releaseRef(self: *DOMStringList, page: *Page) void {
+ self._rc.release(self, page);
+}
+
+pub fn length(self: *const DOMStringList) u32 {
+ return @intCast(self._items.len);
+}
+
+pub fn item(self: *const DOMStringList, index: usize) ?[]const u8 {
+ if (index >= self._items.len) {
+ return null;
+ }
+ return self._items[index];
+}
+
+pub fn contains(self: *const DOMStringList, string: []const u8) bool {
+ for (self._items) |entry| {
+ if (std.mem.eql(u8, entry, string)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+pub fn keys(self: *const DOMStringList, exec: *Execution) !*KeyIterator {
+ return .init(.{ .items = self._items }, exec);
+}
+
+pub fn values(self: *const DOMStringList, exec: *Execution) !*ValueIterator {
+ return .init(.{ .items = self._items }, exec);
+}
+
+pub fn entries(self: *const DOMStringList, exec: *Execution) !*EntryIterator {
+ return .init(.{ .items = self._items }, exec);
+}
+
+// The iterator borrows the (page-arena) slice rather than the DOMStringList, so
+// it's safe regardless of the wrapper's lifetime.
+const Iterator = struct {
+ index: u32 = 0,
+ items: []const []const u8,
+
+ const Entry = struct { u32, []const u8 };
+
+ pub fn next(self: *Iterator, _: *Execution) ?Entry {
+ const index = self.index;
+ if (index >= self.items.len) {
+ return null;
+ }
+ self.index = index + 1;
+ return .{ index, self.items[index] };
+ }
+};
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(DOMStringList);
+
+ pub const Meta = struct {
+ pub const name = "DOMStringList";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ pub const enumerable = false;
+ };
+
+ pub const length = bridge.accessor(DOMStringList.length, null, .{});
+ pub const contains = bridge.function(DOMStringList.contains, .{});
+ pub const item = bridge.function(_item, .{});
+ fn _item(self: *const DOMStringList, index: i32) ?[]const u8 {
+ if (index < 0) {
+ return null;
+ }
+ return self.item(@intCast(index));
+ }
+
+ pub const keys = bridge.function(DOMStringList.keys, .{});
+ pub const values = bridge.function(DOMStringList.values, .{});
+ pub const entries = bridge.function(DOMStringList.entries, .{});
+ pub const symbol_iterator = bridge.iterator(DOMStringList.values, .{});
+ pub const @"[]" = bridge.indexed(DOMStringList.item, null, .{ .null_as_undefined = true });
+};
diff --git a/src/browser/webapi/storage/idb/Engine.zig b/src/browser/webapi/storage/idb/Engine.zig
index 25bc5867e..8bd7060e8 100644
--- a/src/browser/webapi/storage/idb/Engine.zig
+++ b/src/browser/webapi/storage/idb/Engine.zig
@@ -305,6 +305,20 @@ pub fn indexNames(self: *const Engine, arena: Allocator, object_store_id: i64) !
return list.items;
}
+pub fn objectStoreNames(self: *const Engine, arena: Allocator, database_id: i64) ![]const []const u8 {
+ var rows = try self.conn.rows(
+ "select name from idb_object_stores where database_id = ?1 order by name",
+ .{database_id},
+ );
+ defer rows.deinit();
+
+ var list: std.ArrayList([]const u8) = .empty;
+ while (try rows.next()) |row| {
+ try list.append(arena, try arena.dupe(u8, row.get([]const u8, 0)));
+ }
+ return list.items;
+}
+
pub fn addIndexRecord(self: *Engine, index_id: i64, key: []const u8, primary_key: []const u8, unique: bool) !void {
return self.conn.exec(
"insert into idb_index_records (index_id, key, primary_key, is_unique) values (?1, ?2, ?3, ?4)",
diff --git a/src/browser/webapi/storage/idb/IDBDatabase.zig b/src/browser/webapi/storage/idb/IDBDatabase.zig
index 3bbf4b08f..2a601f48f 100644
--- a/src/browser/webapi/storage/idb/IDBDatabase.zig
+++ b/src/browser/webapi/storage/idb/IDBDatabase.zig
@@ -26,8 +26,10 @@ const EventTarget = @import("../../EventTarget.zig");
const Engine = @import("Engine.zig");
const IDBTransaction = @import("IDBTransaction.zig");
const IDBObjectStore = @import("IDBObjectStore.zig");
+const DOMStringList = @import("../../collections.zig").DOMStringList;
const Execution = js.Execution;
+const Allocator = std.mem.Allocator;
const IDBDatabase = @This();
@@ -114,12 +116,41 @@ pub fn transaction(
options: ?TransactionOptions,
exec: *Execution,
) !*IDBTransaction {
- _ = store_names; // TODO
+ const scope = try normalizeStoreNames(store_names, exec.arena);
const opts = options orelse TransactionOptions{};
return IDBTransaction.init(exec, self, switch (mode orelse .readonly) {
.readonly => .readonly,
.readwrite => .readwrite,
- }, opts.durability);
+ }, opts.durability, scope);
+}
+
+// The transaction's scope: the requested store names, sorted with duplicates
+// removed (per the IndexedDB spec's "transaction scope" steps).
+fn normalizeStoreNames(store_names: StoreNames, arena: Allocator) ![]const []const u8 {
+ var list: std.ArrayList([]const u8) = .empty;
+ switch (store_names) {
+ .name => |name| try list.append(arena, try arena.dupe(u8, name)),
+ .names => |names| {
+ try list.ensureUnusedCapacity(arena, names.len);
+ for (names) |name| list.appendAssumeCapacity(try arena.dupe(u8, name));
+ },
+ }
+
+ std.mem.sort([]const u8, list.items, {}, lessThan);
+
+ // Drop duplicates now that equal names are adjacent.
+ var write: usize = 0;
+ for (list.items, 0..) |name, read| {
+ if (read == 0 or !std.mem.eql(u8, name, list.items[write - 1])) {
+ list.items[write] = name;
+ write += 1;
+ }
+ }
+ return list.items[0..write];
+}
+
+fn lessThan(_: void, a: []const u8, b: []const u8) bool {
+ return std.mem.lessThan(u8, a, b);
}
pub fn close(_: *IDBDatabase) void {
@@ -135,6 +166,16 @@ pub fn getVersion(self: *const IDBDatabase) i64 {
return self._version;
}
+pub fn getObjectStoreNames(self: *IDBDatabase, exec: *Execution) !*DOMStringList {
+ const arena = try exec.getArena(.small, "IDB.getObjectStoreNames");
+ errdefer exec.releaseArena(arena);
+
+ const names = try self._engine.objectStoreNames(arena, self._database_id);
+ const list = try arena.create(DOMStringList);
+ list.* = .{ ._items = names, ._arena = arena };
+ return list;
+}
+
pub const JsApi = struct {
pub const bridge = js.Bridge(IDBDatabase);
@@ -146,6 +187,7 @@ pub const JsApi = struct {
pub const name = bridge.accessor(IDBDatabase.getName, null, .{});
pub const version = bridge.accessor(IDBDatabase.getVersion, null, .{});
+ pub const objectStoreNames = bridge.accessor(IDBDatabase.getObjectStoreNames, null, .{});
pub const createObjectStore = bridge.function(IDBDatabase.createObjectStore, .{});
pub const deleteObjectStore = bridge.function(IDBDatabase.deleteObjectStore, .{});
pub const transaction = bridge.function(IDBDatabase.transaction, .{});
diff --git a/src/browser/webapi/storage/idb/IDBObjectStore.zig b/src/browser/webapi/storage/idb/IDBObjectStore.zig
index e99486d41..34de381af 100644
--- a/src/browser/webapi/storage/idb/IDBObjectStore.zig
+++ b/src/browser/webapi/storage/idb/IDBObjectStore.zig
@@ -28,6 +28,7 @@ const IDBCursor = @import("IDBCursor.zig");
const IDBRequest = @import("IDBRequest.zig");
const IDBKeyRange = @import("IDBKeyRange.zig");
const IDBTransaction = @import("IDBTransaction.zig");
+const DOMStringList = @import("../../collections.zig").DOMStringList;
const log = lp.log;
const Execution = js.Execution;
@@ -455,8 +456,14 @@ pub fn index(self: *IDBObjectStore, name: []const u8, exec: *Execution) !*IDBInd
return IDBIndex.init(self, info, owned_name, exec);
}
-pub fn getIndexNames(self: *IDBObjectStore, exec: *Execution) ![]const []const u8 {
- return self._engine.indexNames(exec.arena, self._store_id);
+pub fn getIndexNames(self: *IDBObjectStore, exec: *Execution) !*DOMStringList {
+ const arena = try exec.getArena(.small, "IDB.getIndexNames");
+ errdefer exec.releaseArena(arena);
+
+ const names = try self._engine.indexNames(arena, self._store_id);
+ const list = try arena.create(DOMStringList);
+ list.* = .{ ._items = names, ._arena = arena };
+ return list;
}
pub const JsApi = struct {
diff --git a/src/browser/webapi/storage/idb/IDBTransaction.zig b/src/browser/webapi/storage/idb/IDBTransaction.zig
index 70219f687..723dc6090 100644
--- a/src/browser/webapi/storage/idb/IDBTransaction.zig
+++ b/src/browser/webapi/storage/idb/IDBTransaction.zig
@@ -29,6 +29,7 @@ const Engine = @import("Engine.zig");
const IDBDatabase = @import("IDBDatabase.zig");
const IDBRequest = @import("IDBRequest.zig");
const IDBObjectStore = @import("IDBObjectStore.zig");
+const DOMStringList = @import("../../collections.zig").DOMStringList;
const log = lp.log;
const Execution = js.Execution;
@@ -41,6 +42,10 @@ _exec: *Execution,
_db: *IDBDatabase,
_engine: *Engine,
_mode: Mode,
+// The transaction's scope: the store names it was opened over, sorted and
+// deduped. Empty for a versionchange transaction, whose scope is "all stores"
+// and is resolved live in getObjectStoreNames.
+_scope: []const []const u8 = &.{},
// Advisory only: we always commit through sqlite. Stored to expose the property.
_durability: Durability = .default,
_requests: std.ArrayList(*IDBRequest) = .empty,
@@ -74,13 +79,14 @@ pub const Durability = enum {
}
};
-pub fn init(exec: *Execution, db: *IDBDatabase, mode: Mode, durability: Durability) !*IDBTransaction {
+pub fn init(exec: *Execution, db: *IDBDatabase, mode: Mode, durability: Durability, scope: []const []const u8) !*IDBTransaction {
const self = try exec._factory.eventTarget(IDBTransaction{
._proto = undefined,
._exec = exec,
._db = db,
._engine = db._engine,
._mode = mode,
+ ._scope = scope,
._durability = durability,
});
@@ -201,6 +207,22 @@ pub fn getDb(self: *IDBTransaction) *IDBDatabase {
return self._db;
}
+pub fn getObjectStoreNames(self: *IDBTransaction, exec: *Execution) !*DOMStringList {
+ const arena = try exec.getArena(.small, "IDB.getObjectStoreNames");
+ errdefer exec.releaseArena(arena);
+
+ // A versionchange transaction spans every store; its set changes as the
+ // upgrade creates/deletes stores, so resolve it live rather than caching.
+ const names = if (self._mode == .versionchange)
+ try self._engine.objectStoreNames(arena, self._db._database_id)
+ else
+ self._scope;
+
+ const list = try arena.create(DOMStringList);
+ list.* = .{ ._items = names, ._arena = arena };
+ return list;
+}
+
pub fn getOnComplete(self: *const IDBTransaction) ?js.Function.Global {
return self._on_complete;
}
@@ -284,6 +306,7 @@ pub const JsApi = struct {
pub const mode = bridge.accessor(IDBTransaction.getMode, null, .{});
pub const durability = bridge.accessor(IDBTransaction.getDurability, null, .{});
pub const db = bridge.accessor(IDBTransaction.getDb, null, .{});
+ pub const objectStoreNames = bridge.accessor(IDBTransaction.getObjectStoreNames, null, .{});
pub const objectStore = bridge.function(IDBTransaction.objectStore, .{});
pub const abort = bridge.function(IDBTransaction.abort, .{});
pub const commit = bridge.function(IDBTransaction.commit, .{});