Add session mode: signing in and adding to the cart.
With cart_mode = "session", dibs signs in to the shop's account and puts an item in the cart when it fires. The account's cookies are kept apart from the watcher's and stored encrypted, with the key in the OS keyring. Without a keyring, they're stored in a file only you can read. An alert now goes out right away, without a cart link, and a second one follows once the item is in the cart. If adding fails, the second alert carries the cart link instead. Amazon only accepts additions its page scripts have seen, so plugins can now press a button in headless Chrome with ctx:click. The TUI asks for one-time codes and shows the plugin's warning about account locks in a modal. 'dibs login' signs in from a terminal, or with --browser in a Chrome window, and 'dibs logout' deletes the cookies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e2cbc1a55c
commit
15a34e1f84
39 files changed
+3529
-163
No files matched your search
+38
-16
@@ -115,15 +115,13 @@ func cappedRep(limit int64) lua.LGFunction {
|
||||
|
||||
// run calls fn, which must call into L, under a timeout and the memory
|
||||
// watchdog.
|
||||
func run(L *lua.LState, parent context.Context, timeout time.Duration, memLimit int64, fn func() error) error {
|
||||
func run(L *lua.LState, parent context.Context, timeout time.Duration, m *meter, fn func() error) error {
|
||||
ctx, cancel := context.WithTimeout(parent, timeout)
|
||||
defer cancel()
|
||||
|
||||
var overLimit atomic.Bool
|
||||
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
go watchMemory(ctx, done, memLimit, &overLimit, cancel)
|
||||
go m.watch(ctx, done, cancel)
|
||||
|
||||
L.SetContext(ctx)
|
||||
defer L.RemoveContext()
|
||||
@@ -133,7 +131,7 @@ func run(L *lua.LState, parent context.Context, timeout time.Duration, memLimit
|
||||
switch {
|
||||
case err == nil:
|
||||
return nil
|
||||
case overLimit.Load():
|
||||
case m.overLimit.Load():
|
||||
return ErrMemoryLimit
|
||||
case parent.Err() != nil:
|
||||
return parent.Err()
|
||||
@@ -144,16 +142,33 @@ func run(L *lua.LState, parent context.Context, timeout time.Duration, memLimit
|
||||
}
|
||||
}
|
||||
|
||||
// watchMemory cancels the call once the heap has grown by more than limit
|
||||
// since the call started.
|
||||
func watchMemory(ctx context.Context, done <-chan struct{}, limit int64, overLimit *atomic.Bool, cancel context.CancelFunc) {
|
||||
sample := []metrics.Sample{{Name: "/memory/classes/heap/objects:bytes"}}
|
||||
heap := func() int64 {
|
||||
metrics.Read(sample)
|
||||
return int64(sample[0].Value.Uint64())
|
||||
}
|
||||
// meter is the memory watchdog of one call. It measures the heap growth of
|
||||
// the whole process, so host functions that wait on the network or the user
|
||||
// pause it: what they allocate is the host's, and the measurement starts
|
||||
// over when they return.
|
||||
type meter struct {
|
||||
limit int64
|
||||
base atomic.Int64
|
||||
paused atomic.Bool
|
||||
overLimit atomic.Bool
|
||||
}
|
||||
|
||||
base := heap()
|
||||
func newMeter(limit int64) *meter {
|
||||
m := &meter{limit: limit}
|
||||
m.base.Store(heapBytes())
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *meter) pause() { m.paused.Store(true) }
|
||||
|
||||
func (m *meter) resume() {
|
||||
m.base.Store(heapBytes())
|
||||
m.paused.Store(false)
|
||||
}
|
||||
|
||||
// watch cancels the call once the heap has grown by more than the limit.
|
||||
func (m *meter) watch(ctx context.Context, done <-chan struct{}, cancel context.CancelFunc) {
|
||||
ticker := time.NewTicker(watchInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
@@ -164,8 +179,8 @@ func watchMemory(ctx context.Context, done <-chan struct{}, limit int64, overLim
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if heap()-base > limit {
|
||||
overLimit.Store(true)
|
||||
if !m.paused.Load() && heapBytes()-m.base.Load() > m.limit {
|
||||
m.overLimit.Store(true)
|
||||
cancel()
|
||||
return
|
||||
}
|
||||
@@ -173,6 +188,13 @@ func watchMemory(ctx context.Context, done <-chan struct{}, limit int64, overLim
|
||||
}
|
||||
}
|
||||
|
||||
func heapBytes() int64 {
|
||||
sample := []metrics.Sample{{Name: "/memory/classes/heap/objects:bytes"}}
|
||||
metrics.Read(sample)
|
||||
|
||||
return int64(sample[0].Value.Uint64())
|
||||
}
|
||||
|
||||
// luaError turns a Lua error into a one-line error such as
|
||||
// "amazon.lua:12: attempt to index a nil value".
|
||||
func luaError(err error) error {
|
||||
|
||||
Reference in new issue
Block a user