@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.
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BlobStoreError = exports.blobOwner = void 0;
4
+ exports.isVersionConflict = isVersionConflict;
5
+ exports.createBlobClient = createBlobClient;
6
+ exports.casUpdate = casUpdate;
7
+ exports.blobOwner = {
8
+ save: (saveFileId) => `save:${saveFileId}`,
9
+ player: (participantId) => `player:${participantId}`,
10
+ world: (worldId) => `world:${worldId}`,
11
+ };
12
+ class BlobStoreError extends Error {
13
+ type;
14
+ status;
15
+ details;
16
+ constructor(type, status, details) {
17
+ super(`${type}: ${String(details.message ?? status)}`);
18
+ this.type = type;
19
+ this.status = status;
20
+ this.details = details;
21
+ this.name = "BlobStoreError";
22
+ }
23
+ }
24
+ exports.BlobStoreError = BlobStoreError;
25
+ function isVersionConflict(e) {
26
+ return e instanceof BlobStoreError && e.type === "VersionConflict";
27
+ }
28
+ const ERROR_TYPES = new Set([
29
+ "VersionConflict", "BlobTooLarge", "BlobQuotaExceeded", "BlobOwnerNotFound", "InvalidBlobRequest", "PluginUnauthorized",
30
+ ]);
31
+ function createBlobClient(options) {
32
+ const doFetch = options.fetchImpl ?? fetch;
33
+ const timeoutMs = options.timeoutMs ?? 5000;
34
+ const root = `${options.platformUrl}/internal/plugins/${encodeURIComponent(options.gameType)}/blobs`;
35
+ async function call(method, path, body) {
36
+ const headers = {};
37
+ if (options.authToken)
38
+ headers["X-Plugin-Auth"] = `Bearer ${options.authToken}`;
39
+ if (body !== undefined)
40
+ headers["Content-Type"] = "application/json";
41
+ const res = await doFetch(`${root}/${path}`, {
42
+ method,
43
+ headers,
44
+ body: body === undefined ? undefined : JSON.stringify(body),
45
+ signal: AbortSignal.timeout(timeoutMs),
46
+ });
47
+ const text = await res.text();
48
+ const parsed = text ? JSON.parse(text) : {};
49
+ if (res.ok)
50
+ return parsed;
51
+ const type = (res.headers.get("X-Amzn-Errortype") ?? parsed.__type ?? "");
52
+ if (ERROR_TYPES.has(type))
53
+ throw new BlobStoreError(type, res.status, parsed);
54
+ throw new Error(`blob store ${method} ${path}: ${res.status} ${text.slice(0, 200)}`);
55
+ }
56
+ const at = (owner, key) => key === undefined ? encodeURIComponent(owner) : `${encodeURIComponent(owner)}/${encodeURIComponent(key)}`;
57
+ return {
58
+ get: (owner, key) => call("GET", at(owner, key)),
59
+ put: (owner, key, value, expectedVersion) => call("PUT", at(owner, key), { value, expectedVersion }),
60
+ delete: (owner, key, expectedVersion) => call("DELETE", at(owner, key) + (expectedVersion === undefined ? "" : `?expectedVersion=${expectedVersion}`)),
61
+ batchGet: async (owner, keys) => (await call("POST", `${at(owner)}/batch-get`, { keys })).items,
62
+ batchPut: async (owner, writes) => (await call("POST", `${at(owner)}/batch-put`, { writes })).items,
63
+ list: (owner) => call("GET", at(owner)),
64
+ };
65
+ }
66
+ async function casUpdate(client, owner, key, change, maxAttempts = 5) {
67
+ for (let attempt = 1;; attempt++) {
68
+ const current = await client.get(owner, key);
69
+ const next = change(current.version === 0 ? undefined : current.value);
70
+ if (next === undefined)
71
+ return { value: current.value, version: current.version };
72
+ try {
73
+ const { version } = await client.put(owner, key, next, current.version);
74
+ return { value: next, version };
75
+ }
76
+ catch (e) {
77
+ if (!isVersionConflict(e) || attempt >= maxAttempts)
78
+ throw e;
79
+ }
80
+ }
81
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runtimeVersion = exports.PLUGIN_PROTOCOL_VERSION = exports.assertConformance = exports.runConformance = exports.createRegistrationLoop = exports.createPluginApp = exports.servePlugin = exports.createGamePluginService = exports.mountSsdkHandler = void 0;
3
+ exports.isVersionConflict = exports.BlobStoreError = exports.blobOwner = exports.casUpdate = exports.createBlobClient = exports.runtimeVersion = exports.PLUGIN_PROTOCOL_VERSION = exports.assertConformance = exports.runConformance = exports.createRegistrationLoop = exports.createPluginApp = exports.servePlugin = exports.createGamePluginService = exports.mountSsdkHandler = void 0;
4
4
  var express_1 = require("./express");
5
5
  Object.defineProperty(exports, "mountSsdkHandler", { enumerable: true, get: function () { return express_1.mountSsdkHandler; } });
6
6
  var defaultService_1 = require("./defaultService");
@@ -16,3 +16,9 @@ Object.defineProperty(exports, "assertConformance", { enumerable: true, get: fun
16
16
  var version_1 = require("./version");
17
17
  Object.defineProperty(exports, "PLUGIN_PROTOCOL_VERSION", { enumerable: true, get: function () { return version_1.PLUGIN_PROTOCOL_VERSION; } });
18
18
  Object.defineProperty(exports, "runtimeVersion", { enumerable: true, get: function () { return version_1.runtimeVersion; } });
19
+ var blobClient_1 = require("./blobClient");
20
+ Object.defineProperty(exports, "createBlobClient", { enumerable: true, get: function () { return blobClient_1.createBlobClient; } });
21
+ Object.defineProperty(exports, "casUpdate", { enumerable: true, get: function () { return blobClient_1.casUpdate; } });
22
+ Object.defineProperty(exports, "blobOwner", { enumerable: true, get: function () { return blobClient_1.blobOwner; } });
23
+ Object.defineProperty(exports, "BlobStoreError", { enumerable: true, get: function () { return blobClient_1.BlobStoreError; } });
24
+ Object.defineProperty(exports, "isVersionConflict", { enumerable: true, get: function () { return blobClient_1.isVersionConflict; } });