Skip to content

Commit ea5b059

Browse files
Jake ChampionJakeChampion
authored andcommitted
Add api documentation for the ObjectStore and ObjectStoreEntry implementations
1 parent 8a66c9d commit ea5b059

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

c-dependencies/js-compute-runtime/builtins/object-store.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ bool constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
356356
}
357357

358358
// If the converted string has a length of more than 255 then we throw an Error
359-
// because ObjectStore names have to be less than 1025 characters.
359+
// because ObjectStore names have to be less than 255 characters.
360360
if (name_len > 255) {
361361
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_OBJECT_STORE_NAME_TOO_LONG);
362362
return false;

sdk/js-compute/index.d.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,76 @@ export declare class Env {
306306
get(name: string): string;
307307
}
308308

309+
/**
310+
* Class for accessing a [Fastly Object-store](https://developer.fastly.com/reference/api/object-store/).
311+
*
312+
* An object store is a persistent, globally consistent key-value store.
313+
*
314+
* **Note**: Can only be used when processing requests, not during build-time initialization.
315+
*/
316+
export declare class ObjectStore {
317+
/**
318+
* Creates a new JavaScript ObjectStore object which interacts with the Fastly Object-store named `name`.
319+
*
320+
* @param name Name of the Fastly Object-store to interact with. A name cannot be empty, contain Control characters, or be longer than 255 characters.
321+
*/
322+
constructor(name: string);
323+
/**
324+
* Gets the value associated with the key `key` in the Object-store.
325+
* When the key is present, a resolved Promise containing an ObjectStoreEntry will be returned which contains the associated value.
326+
* When the key is absent, a resolved Promise containing null is returned.
327+
* @param key The key to retrieve from within the Object-store. A key cannot:
328+
* - Be any of the strings "", ".", or ".."
329+
* - Start with the string ".well-known/acme-challenge/""
330+
* - Contain any of the characters "#?*[]\n\r"
331+
* - Be longer than 1024 characters
332+
*/
333+
get(key: string): Promise<ObjectStoreEntry | null>;
334+
335+
/**
336+
* Write the value of `value` into the Object-store under the key `key`.
337+
*
338+
* Note: Object-store is eventually consistent, this means that the updated contents associated with the key `key` may not be available to read from all
339+
* edge locations immediately and some edge locations may continue returning the previous contents associated with the key.
340+
*
341+
* @param key The key to associate with the value. A key cannot:
342+
* - Be any of the strings "", ".", or ".."
343+
* - Start with the string ".well-known/acme-challenge/""
344+
* - Contain any of the characters "#?*[]\n\r"
345+
* - Be longer than 1024 characters
346+
* @param value The value to store within the Object-store.
347+
*/
348+
put(key: string, value: BodyInit): Promise<undefined>;
349+
}
350+
351+
/**
352+
* Class for interacting with a [Fastly Object-store](https://developer.fastly.com/reference/api/object-store/) entry.
353+
*/
354+
export declare interface ObjectStoreEntry {
355+
/**
356+
* A ReadableStream with the contents of the entry.
357+
*/
358+
get body(): ReadableStream;
359+
/**
360+
* A boolean value that indicates whether the body has been read from already.
361+
*/
362+
get bodyUsed(): boolean;
363+
/**
364+
* Reads the body and returns it as a promise that resolves with a string.
365+
* The response is always decoded using UTF-8.
366+
*/
367+
text(): Promise<string>;
368+
/**
369+
* Reads the body and returns it as a promise that resolves with the result of parsing the body text as JSON.
370+
*/
371+
json(): Promise<object>;
372+
/**
373+
* Reads the body and returns it as a promise that resolves with an ArrayBuffer.
374+
*/
375+
arrayBuffer(): Promise<ArrayBuffer>;
376+
// And eventually formData and blob once we support them on Request and Response, too.
377+
}
378+
309379
/**
310380
* The URL class as [specified by WHATWG](https://url.spec.whatwg.org/#url-class)
311381
*

sdk/js-compute/index.test-d.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
import {expectError, expectType} from 'tsd';
2-
import { addEventListener, CacheOverride, CacheOverrideInit, CacheOverrideMode, ClientInfo, CompressionStream, CompressionStreamFormat, Console, console, DecompressionStream, DecompressionStreamFormat, Dictionary, Env, EventMap, fastly, Fastly, FetchEvent, FetchEventListener, Geolocation, Logger, onfetch, ReadableStream, Request, Response, TextDecoder, TextEncoder, URL, URLSearchParams, WritableStream } from ".";
2+
import { ObjectStore, ObjectStoreEntry, addEventListener, CacheOverride, CacheOverrideInit, CacheOverrideMode, ClientInfo, CompressionStream, CompressionStreamFormat, Console, console, DecompressionStream, DecompressionStreamFormat, Dictionary, Env, EventMap, fastly, Fastly, FetchEvent, FetchEventListener, Geolocation, Logger, onfetch, ReadableStream, Request, Response, TextDecoder, TextEncoder, URL, URLSearchParams, WritableStream } from ".";
3+
4+
// ObjectStore
5+
{
6+
expectError(ObjectStore())
7+
expectError(ObjectStore('secrets'))
8+
expectType<ObjectStore>(new ObjectStore("secrets"))
9+
expectError(new ObjectStore('secrets', {}))
10+
const store = new ObjectStore('secrets')
11+
expectError(store.get())
12+
expectError(store.get(1))
13+
expectType<Promise<ObjectStoreEntry|null>>(store.get('cat'))
14+
expectError(store.put())
15+
expectError(store.put('cat'))
16+
expectError(store.put('cat', 1))
17+
expectType<Promise<undefined>>(store.put('cat', 'Aki'))
18+
}
19+
20+
// ObjectStoreEntry
21+
{
22+
const entry = {} as ObjectStoreEntry
23+
expectType<ReadableStream<any>>(entry.body)
24+
expectType<boolean>(entry.bodyUsed)
25+
expectType<Promise<ArrayBuffer>>(entry.arrayBuffer())
26+
expectType<Promise<object>>(entry.json())
27+
expectType<Promise<string>>(entry.text())
28+
}
329

430
// onfetch
531
{

0 commit comments

Comments
 (0)