@anweather/game-plugin-ssdk 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-cjs/runtime/blobClient.js +81 -0
- package/dist-cjs/runtime/index.js +7 -1
- package/dist-cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist-es/runtime/blobClient.js +74 -0
- package/dist-es/runtime/index.js +1 -0
- package/dist-es/tsconfig.es.tsbuildinfo +1 -1
- package/dist-types/runtime/blobClient.d.ts +84 -0
- package/dist-types/runtime/index.d.ts +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export const blobOwner = {
|
|
2
|
+
save: (saveFileId) => `save:${saveFileId}`,
|
|
3
|
+
player: (participantId) => `player:${participantId}`,
|
|
4
|
+
world: (worldId) => `world:${worldId}`,
|
|
5
|
+
};
|
|
6
|
+
export class BlobStoreError extends Error {
|
|
7
|
+
type;
|
|
8
|
+
status;
|
|
9
|
+
details;
|
|
10
|
+
constructor(type, status, details) {
|
|
11
|
+
super(`${type}: ${String(details.message ?? status)}`);
|
|
12
|
+
this.type = type;
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.details = details;
|
|
15
|
+
this.name = "BlobStoreError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function isVersionConflict(e) {
|
|
19
|
+
return e instanceof BlobStoreError && e.type === "VersionConflict";
|
|
20
|
+
}
|
|
21
|
+
const ERROR_TYPES = new Set([
|
|
22
|
+
"VersionConflict", "BlobTooLarge", "BlobQuotaExceeded", "BlobOwnerNotFound", "InvalidBlobRequest", "PluginUnauthorized",
|
|
23
|
+
]);
|
|
24
|
+
export function createBlobClient(options) {
|
|
25
|
+
const doFetch = options.fetchImpl ?? fetch;
|
|
26
|
+
const timeoutMs = options.timeoutMs ?? 5000;
|
|
27
|
+
const root = `${options.platformUrl}/internal/plugins/${encodeURIComponent(options.gameType)}/blobs`;
|
|
28
|
+
async function call(method, path, body) {
|
|
29
|
+
const headers = {};
|
|
30
|
+
if (options.authToken)
|
|
31
|
+
headers["X-Plugin-Auth"] = `Bearer ${options.authToken}`;
|
|
32
|
+
if (body !== undefined)
|
|
33
|
+
headers["Content-Type"] = "application/json";
|
|
34
|
+
const res = await doFetch(`${root}/${path}`, {
|
|
35
|
+
method,
|
|
36
|
+
headers,
|
|
37
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
38
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
39
|
+
});
|
|
40
|
+
const text = await res.text();
|
|
41
|
+
const parsed = text ? JSON.parse(text) : {};
|
|
42
|
+
if (res.ok)
|
|
43
|
+
return parsed;
|
|
44
|
+
const type = (res.headers.get("X-Amzn-Errortype") ?? parsed.__type ?? "");
|
|
45
|
+
if (ERROR_TYPES.has(type))
|
|
46
|
+
throw new BlobStoreError(type, res.status, parsed);
|
|
47
|
+
throw new Error(`blob store ${method} ${path}: ${res.status} ${text.slice(0, 200)}`);
|
|
48
|
+
}
|
|
49
|
+
const at = (owner, key) => key === undefined ? encodeURIComponent(owner) : `${encodeURIComponent(owner)}/${encodeURIComponent(key)}`;
|
|
50
|
+
return {
|
|
51
|
+
get: (owner, key) => call("GET", at(owner, key)),
|
|
52
|
+
put: (owner, key, value, expectedVersion) => call("PUT", at(owner, key), { value, expectedVersion }),
|
|
53
|
+
delete: (owner, key, expectedVersion) => call("DELETE", at(owner, key) + (expectedVersion === undefined ? "" : `?expectedVersion=${expectedVersion}`)),
|
|
54
|
+
batchGet: async (owner, keys) => (await call("POST", `${at(owner)}/batch-get`, { keys })).items,
|
|
55
|
+
batchPut: async (owner, writes) => (await call("POST", `${at(owner)}/batch-put`, { writes })).items,
|
|
56
|
+
list: (owner) => call("GET", at(owner)),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export async function casUpdate(client, owner, key, change, maxAttempts = 5) {
|
|
60
|
+
for (let attempt = 1;; attempt++) {
|
|
61
|
+
const current = await client.get(owner, key);
|
|
62
|
+
const next = change(current.version === 0 ? undefined : current.value);
|
|
63
|
+
if (next === undefined)
|
|
64
|
+
return { value: current.value, version: current.version };
|
|
65
|
+
try {
|
|
66
|
+
const { version } = await client.put(owner, key, next, current.version);
|
|
67
|
+
return { value: next, version };
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
if (!isVersionConflict(e) || attempt >= maxAttempts)
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
package/dist-es/runtime/index.js
CHANGED
|
@@ -4,3 +4,4 @@ export { servePlugin, createPluginApp } from "./servePlugin";
|
|
|
4
4
|
export { createRegistrationLoop } from "./registration";
|
|
5
5
|
export { runConformance, assertConformance } from "./conformance";
|
|
6
6
|
export { PLUGIN_PROTOCOL_VERSION, runtimeVersion } from "./version";
|
|
7
|
+
export { createBlobClient, casUpdate, blobOwner, BlobStoreError, isVersionConflict } from "./blobClient";
|