Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/kvstore/fastly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ name = "kvstore"
service_id = ""

[local_server]
backends.httpme.url = "https://http-me.fastly.dev"

[local_server.kv_stores]

Expand Down
26 changes: 26 additions & 0 deletions integration_tests/kvstore/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package main

import (
"context"
"maps"
"sort"
"strconv"
"strings"
"testing"

"github.com/fastly/compute-sdk-go/fsthttp"
"github.com/fastly/compute-sdk-go/kvstore"
)

Expand Down Expand Up @@ -102,6 +104,30 @@ func TestKVStore(t *testing.T) {
if !maps.Equal(wantListKeys, gotListKeys) {
t.Errorf("Expected got/want keys mismatch: want=%v, got=%v", mapKeys(wantListKeys), mapKeys(gotListKeys))
}

uri := "https://http-me.fastly.dev/echo/?body=hello,+world"
req, err := fsthttp.NewRequest("GET", uri, nil)
if err != nil {
t.Errorf("error during NewRequest: uri=%v err=%v", uri, err)
return
}

ctx := context.Background()
resp, err := req.Send(ctx, "httpme")

if err := store.Insert("hello", resp.Body); err != nil {
t.Errorf("error during HTTPBody Insert: err=%v", err)
}

hello, err = store.Lookup("hello")
if err != nil {
t.Errorf("error during HTTPBody Lookup: err=%v", err)
return
}

if got, want := hello.String(), "hello, world"; got != want {
t.Errorf("HTTPBody Lookup: got %q, want %q", got, want)
}
}

func mapKeys(m map[string]bool) []string {
Expand Down
13 changes: 1 addition & 12 deletions internal/abi/fastly/kvstore_guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package fastly

import (
"io"

"github.com/fastly/compute-sdk-go/internal/abi/prim"
)

Expand Down Expand Up @@ -186,16 +184,7 @@ func fastlyKVStoreInsert(
) FastlyStatus

// Insert returns a handle to a pending key/value pair insertion.
func (k *KVStore) Insert(key string, value io.Reader, config *KVInsertConfig) (kvstoreInsertHandle, error) {
body, err := NewHTTPBody()
if err != nil {
return 0, err
}

if _, err := io.Copy(body, value); err != nil {
return 0, err
}

func (k *KVStore) Insert(key string, body *HTTPBody, config *KVInsertConfig) (kvstoreInsertHandle, error) {
if config == nil {
config = &KVInsertConfig{}
}
Expand Down
14 changes: 13 additions & 1 deletion kvstore/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,19 @@ func (s *Store) InsertWithConfig(key string, value io.Reader, config *InsertConf
}
}

h, err := s.kvstore.Insert(key, value, &abiConf)
var body *fastly.HTTPBody
if abiBody, ok := value.(*fastly.HTTPBody); ok {
body = abiBody
} else {
var err error
body, err = fastly.NewHTTPBody()
if err != nil {
return err
}
io.Copy(body, value)
}

h, err := s.kvstore.Insert(key, body, &abiConf)
if err != nil {
return mapFastlyErr(err)
}
Expand Down