@invonetwork/web-sdk 3.28.0 → 3.29.1
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/CHANGELOG.md +19 -0
- package/README.md +24 -1
- package/dist/chunk-VOGZZ6QS.js +6 -0
- package/dist/{chunk-4YKTKXC5.js → chunk-YONO4JE5.js} +22 -5
- package/dist/game-server.cjs +184 -19
- package/dist/game-server.d.cts +85 -6
- package/dist/game-server.d.ts +85 -6
- package/dist/game-server.js +165 -22
- package/dist/game.cjs +4 -1
- package/dist/game.d.cts +3 -3
- package/dist/game.d.ts +3 -3
- package/dist/game.js +4 -1
- package/dist/server-BYY6tM6X.d.ts +2165 -0
- package/dist/server-Bxo_wgHx.d.cts +2165 -0
- package/dist/server.cjs +16 -2
- package/dist/server.d.cts +3 -2159
- package/dist/server.d.ts +3 -2159
- package/dist/server.js +2 -2
- package/dist/{types-CoW9LopI.d.cts → types-CLyJDuXd.d.cts} +12 -0
- package/dist/{types-CoW9LopI.d.ts → types-CLyJDuXd.d.ts} +12 -0
- package/dist/{version-iWmcxjYu.d.cts → version-DOaYbHu7.d.cts} +1 -1
- package/dist/{version-iWmcxjYu.d.ts → version-DOaYbHu7.d.ts} +1 -1
- package/package.json +91 -91
- package/dist/chunk-3DRWLDDF.js +0 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 3.29.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Remix detection on Replit now uses `REPL_ID`: the same Repl keeps its project id when it moves from `*.replit.dev` (building) to `*.replit.app` (deployed), and `registerProject` re-registers the new webhook URL with the saved registration token. A different `REPL_ID` is a remix and gets a fresh project id. Outside Replit a changed public host still counts as a remix. The project file now records `replId`.
|
|
8
|
+
- `GET /catalog` adds `demo` (true on the public demo key) and `playerFields.phone` (`"hidden"` on the demo key, `"first_sign_in"` otherwise); `/health` and `invoGameServerFromEnv` also report `demo`. The UI kit hides the phone field when the catalog says so.
|
|
9
|
+
- Nothing removed or renamed.
|
|
10
|
+
|
|
11
|
+
## 3.29.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- Public demo sandbox (INVO game templates): on the demo key, `invoGameServerFromEnv` maps every sign-in to the demo sandbox's player email format for the fork's project (`toDemoEmail`) and gives the player a fictional 555-01XX phone (`demoPhoneFor`), so no real address or number reaches INVO. The project id comes from `INVO_PROJECT_ID`, else `.invo/project.json` (option `projectFile`), else a new one written there; a copied file on a different public host (a remix) gets a fresh id.
|
|
16
|
+
- New `registerProject({ gameSecret, webhookUrl, file? })` (game-server entry): registers the fork on every start, stores the webhook signing secret and the registration token in the project file the first time, and handles a remix's `409 PROJECT_REGISTERED` by taking a fresh project id. `invoGameServerFromEnv` reads the webhook secret from that file when `INVO_WEBHOOK_SECRET` is not set, or from the new `webhookSecret` option.
|
|
17
|
+
- `verifyInvoSignature(rawBody, header, secrets)`: the INVO webhook signature check under the name the template guide uses (same as `verifyWebhook`).
|
|
18
|
+
- The key-environment lookup is retried on each call until it succeeds (a failure at boot no longer wedges the server), and a production error falls through to the sandbox lookup.
|
|
19
|
+
- `InvoGameServerSetup.environment` is `"sandbox"`, `"production"` or `"resolving"`; `sandbox` is no longer `true` for a live key whose environment is still being looked up.
|
|
20
|
+
- Nothing removed or renamed.
|
|
21
|
+
|
|
3
22
|
## 3.28.0
|
|
4
23
|
|
|
5
24
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -2398,7 +2398,7 @@ const economy = {
|
|
|
2398
2398
|
],
|
|
2399
2399
|
};
|
|
2400
2400
|
|
|
2401
|
-
// INVO_GAME_SECRET, INVO_WEBHOOK_SECRET,
|
|
2401
|
+
// INVO_GAME_SECRET, INVO_WEBHOOK_SECRET, PUBLIC_URL (the environment follows the key)
|
|
2402
2402
|
const { server, missing } = invoGameServerFromEnv({
|
|
2403
2403
|
economy,
|
|
2404
2404
|
env: process.env,
|
|
@@ -2409,6 +2409,29 @@ http.createServer(toNodeListener(server, serveYourStaticFiles)).listen(3000);
|
|
|
2409
2409
|
|
|
2410
2410
|
Point your INVO webhook at `https://<your-host>/api/invo/webhook`.
|
|
2411
2411
|
|
|
2412
|
+
**On the public demo key (INVO's game templates).** A fresh fork runs on the public demo key
|
|
2413
|
+
(`ivsdk_demo_...`, sandbox only, no signup). Register the fork on every start; the first time,
|
|
2414
|
+
the webhook signing secret and a registration token are stored in `.invo/project.json` (add it to
|
|
2415
|
+
`.gitignore`; a remix that copied it gets a fresh project id automatically):
|
|
2416
|
+
|
|
2417
|
+
```ts
|
|
2418
|
+
import { registerProject, invoGameServerFromEnv } from "@invonetwork/web-sdk/game-server";
|
|
2419
|
+
|
|
2420
|
+
if (process.env.INVO_GAME_SECRET?.startsWith("ivsdk_demo_")) {
|
|
2421
|
+
await registerProject({
|
|
2422
|
+
gameSecret: process.env.INVO_GAME_SECRET,
|
|
2423
|
+
webhookUrl: `${process.env.PUBLIC_URL}/api/invo/webhook`,
|
|
2424
|
+
});
|
|
2425
|
+
}
|
|
2426
|
+
const { server } = invoGameServerFromEnv({ economy, env: process.env, store });
|
|
2427
|
+
```
|
|
2428
|
+
|
|
2429
|
+
On the demo key every sign-in is mapped to a demo-sandbox player email for this project and a
|
|
2430
|
+
fictional 555-01XX phone, so no real address or number reaches INVO. Going live is replacing
|
|
2431
|
+
`INVO_GAME_SECRET` with your own key (and `INVO_WEBHOOK_SECRET` with the secret from your INVO
|
|
2432
|
+
console): no code changes. `verifyInvoSignature(rawBody, header, secrets)` checks an INVO webhook
|
|
2433
|
+
signature anywhere else you need it.
|
|
2434
|
+
|
|
2412
2435
|
**Browser.**
|
|
2413
2436
|
|
|
2414
2437
|
```ts
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { assertSecureBaseUrl, Http, toRequestPhoneChangeResult, toPhoneChangeRequest, receiptFields, creditFields, toLoyaltyCardsResult, toDestinationsResult } from './chunk-6CN236QH.js';
|
|
2
|
-
import { SDK_VERSION } from './chunk-
|
|
2
|
+
import { SDK_VERSION } from './chunk-VOGZZ6QS.js';
|
|
3
3
|
import { InvoError, buildGameEventBody, toReportGameEventResult } from './chunk-VP3OK4J4.js';
|
|
4
4
|
import { createHmac, timingSafeEqual } from 'crypto';
|
|
5
5
|
|
|
@@ -294,6 +294,9 @@ function verifyWebhook(rawBody, signatureHeader, secret, opts = {}) {
|
|
|
294
294
|
}
|
|
295
295
|
return finalizeEvent(bodyBytes);
|
|
296
296
|
}
|
|
297
|
+
function verifyInvoSignature(rawBody, signatureHeader, secrets, opts = {}) {
|
|
298
|
+
return verifyWebhook(rawBody, signatureHeader, secrets, opts);
|
|
299
|
+
}
|
|
297
300
|
async function verifyWebhookAsync(rawBody, signatureHeader, secret, opts = {}) {
|
|
298
301
|
const { secrets, sigs, message, bodyBytes } = prepareVerification(
|
|
299
302
|
rawBody,
|
|
@@ -401,7 +404,21 @@ function resolveEnvironment(gameSecret, opts = {}) {
|
|
|
401
404
|
if (hit) return hit;
|
|
402
405
|
const fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
|
|
403
406
|
const p = (async () => {
|
|
404
|
-
|
|
407
|
+
let prodError = null;
|
|
408
|
+
let env = null;
|
|
409
|
+
try {
|
|
410
|
+
env = await ask(PRODUCTION_BASE_URL, gameSecret, fetchImpl);
|
|
411
|
+
} catch (err) {
|
|
412
|
+
prodError = err;
|
|
413
|
+
}
|
|
414
|
+
if (!env) {
|
|
415
|
+
try {
|
|
416
|
+
env = await ask(SANDBOX_BASE_URL, gameSecret, fetchImpl);
|
|
417
|
+
} catch (err) {
|
|
418
|
+
throw prodError ?? err;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (!env && prodError) throw prodError;
|
|
405
422
|
if (!env) {
|
|
406
423
|
throw new EnvironmentUnknownError(
|
|
407
424
|
"Neither INVO production nor the INVO sandbox recognises this key. Check INVO_GAME_SECRET in your secrets."
|
|
@@ -3730,6 +3747,6 @@ var InvoServer = class {
|
|
|
3730
3747
|
}
|
|
3731
3748
|
};
|
|
3732
3749
|
|
|
3733
|
-
export { CardsServer, DEMO_KEY_PREFIX, EnvironmentUnknownError, InvoServer, PRODUCTION_BASE_URL, PayoutsServer, PlatformCommerceServer, SANDBOX_BASE_URL, SandboxServer, SandboxSubscriptionsServer, SubscriptionsServer, baseUrlForKey, createWebhookHandler, isDemoKey, isRefundPendingApproval, resolveEnvironment, verifyWebhook, verifyWebhookAsync };
|
|
3734
|
-
//# sourceMappingURL=chunk-
|
|
3735
|
-
//# sourceMappingURL=chunk-
|
|
3750
|
+
export { CardsServer, DEMO_KEY_PREFIX, EnvironmentUnknownError, InvoServer, PRODUCTION_BASE_URL, PayoutsServer, PlatformCommerceServer, SANDBOX_BASE_URL, SandboxServer, SandboxSubscriptionsServer, SubscriptionsServer, baseUrlForKey, createWebhookHandler, isDemoKey, isRefundPendingApproval, resolveEnvironment, verifyInvoSignature, verifyWebhook, verifyWebhookAsync };
|
|
3751
|
+
//# sourceMappingURL=chunk-YONO4JE5.js.map
|
|
3752
|
+
//# sourceMappingURL=chunk-YONO4JE5.js.map
|
package/dist/game-server.cjs
CHANGED
|
@@ -64,7 +64,21 @@ function resolveEnvironment(gameSecret, opts = {}) {
|
|
|
64
64
|
if (hit) return hit;
|
|
65
65
|
const fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
|
|
66
66
|
const p = (async () => {
|
|
67
|
-
|
|
67
|
+
let prodError = null;
|
|
68
|
+
let env = null;
|
|
69
|
+
try {
|
|
70
|
+
env = await ask(PRODUCTION_BASE_URL, gameSecret, fetchImpl);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
prodError = err;
|
|
73
|
+
}
|
|
74
|
+
if (!env) {
|
|
75
|
+
try {
|
|
76
|
+
env = await ask(SANDBOX_BASE_URL, gameSecret, fetchImpl);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
throw prodError ?? err;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!env && prodError) throw prodError;
|
|
68
82
|
if (!env) {
|
|
69
83
|
throw new EnvironmentUnknownError(
|
|
70
84
|
"Neither INVO production nor the INVO sandbox recognises this key. Check INVO_GAME_SECRET in your secrets."
|
|
@@ -1671,7 +1685,7 @@ function toPhoneChangeRequest(raw) {
|
|
|
1671
1685
|
}
|
|
1672
1686
|
|
|
1673
1687
|
// src/version.ts
|
|
1674
|
-
var SDK_VERSION = "3.
|
|
1688
|
+
var SDK_VERSION = "3.29.1";
|
|
1675
1689
|
|
|
1676
1690
|
// src/payouts.ts
|
|
1677
1691
|
var AMOUNT = /^\d+(\.\d{1,2})?$/;
|
|
@@ -1953,6 +1967,9 @@ function verifyWebhook(rawBody, signatureHeader, secret, opts = {}) {
|
|
|
1953
1967
|
}
|
|
1954
1968
|
return finalizeEvent(bodyBytes);
|
|
1955
1969
|
}
|
|
1970
|
+
function verifyInvoSignature(rawBody, signatureHeader, secrets, opts = {}) {
|
|
1971
|
+
return verifyWebhook(rawBody, signatureHeader, secrets, opts);
|
|
1972
|
+
}
|
|
1956
1973
|
|
|
1957
1974
|
// src/server.ts
|
|
1958
1975
|
var DEFAULT_UA = `invonetwork-web-sdk/${SDK_VERSION} (+https://invo.network)`;
|
|
@@ -5266,6 +5283,130 @@ var InvoServer = class {
|
|
|
5266
5283
|
};
|
|
5267
5284
|
}
|
|
5268
5285
|
};
|
|
5286
|
+
var DEMO_EMAIL_DOMAIN = "demo.invo.network";
|
|
5287
|
+
var DEFAULT_PROJECT_FILE = ".invo/project.json";
|
|
5288
|
+
var PROJECT_ID = /^proj_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
5289
|
+
function newDemoProjectId(now = Date.now()) {
|
|
5290
|
+
const A = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
5291
|
+
let t = now;
|
|
5292
|
+
let time = "";
|
|
5293
|
+
for (let i = 0; i < 10; i++) {
|
|
5294
|
+
time = A[t % 32] + time;
|
|
5295
|
+
t = Math.floor(t / 32);
|
|
5296
|
+
}
|
|
5297
|
+
const bytes = crypto.randomBytes(16);
|
|
5298
|
+
let rand = "";
|
|
5299
|
+
for (let i = 0; i < 16; i++) rand += A[bytes[i] % 32];
|
|
5300
|
+
return "proj_" + time + rand;
|
|
5301
|
+
}
|
|
5302
|
+
function isValidProjectId(value) {
|
|
5303
|
+
return typeof value === "string" && PROJECT_ID.test(value);
|
|
5304
|
+
}
|
|
5305
|
+
function toDemoEmail(email, projectId) {
|
|
5306
|
+
if (!isValidProjectId(projectId)) throw new Error("A valid template project id is required.");
|
|
5307
|
+
const lower = email.trim().toLowerCase();
|
|
5308
|
+
if (lower.endsWith("@" + DEMO_EMAIL_DOMAIN)) return lower;
|
|
5309
|
+
const local = (lower.split("@")[0] ?? "player").replace(/[^a-z0-9._-]/g, "").slice(0, 24) || "player";
|
|
5310
|
+
const tag = crypto.createHash("sha256").update(lower).digest("hex").slice(0, 8);
|
|
5311
|
+
return `${local}-${tag}+${projectId.slice(5).toLowerCase()}@${DEMO_EMAIL_DOMAIN}`;
|
|
5312
|
+
}
|
|
5313
|
+
function demoPhoneFor(email) {
|
|
5314
|
+
const n = BigInt("0x" + crypto.createHash("sha256").update(email.trim().toLowerCase()).digest("hex")) % BigInt(100);
|
|
5315
|
+
return "+120255501" + n.toString().padStart(2, "0");
|
|
5316
|
+
}
|
|
5317
|
+
function hostOf(url) {
|
|
5318
|
+
if (!url) return void 0;
|
|
5319
|
+
try {
|
|
5320
|
+
return new URL(url).host.toLowerCase();
|
|
5321
|
+
} catch {
|
|
5322
|
+
return void 0;
|
|
5323
|
+
}
|
|
5324
|
+
}
|
|
5325
|
+
function readFile(file) {
|
|
5326
|
+
try {
|
|
5327
|
+
const data = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
5328
|
+
return isValidProjectId(data.projectId) ? data : null;
|
|
5329
|
+
} catch {
|
|
5330
|
+
return null;
|
|
5331
|
+
}
|
|
5332
|
+
}
|
|
5333
|
+
function writeFile(file, data) {
|
|
5334
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
5335
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2) + "\n", { encoding: "utf8", mode: 384 });
|
|
5336
|
+
}
|
|
5337
|
+
function currentReplId(override) {
|
|
5338
|
+
const v = override ?? (typeof process !== "undefined" ? process.env.REPL_ID : void 0);
|
|
5339
|
+
return v && v.trim() ? v.trim() : void 0;
|
|
5340
|
+
}
|
|
5341
|
+
function sameFork(saved, host, replId) {
|
|
5342
|
+
if (replId && saved.replId) return saved.replId === replId;
|
|
5343
|
+
return !host || !saved.host || saved.host === host;
|
|
5344
|
+
}
|
|
5345
|
+
function loadProject(opts = {}) {
|
|
5346
|
+
const file = opts.file ?? DEFAULT_PROJECT_FILE;
|
|
5347
|
+
const replId = currentReplId(opts.replId);
|
|
5348
|
+
if (opts.projectId) {
|
|
5349
|
+
if (!isValidProjectId(opts.projectId)) throw new Error('INVO_PROJECT_ID must be "proj_" + a 26-character ULID.');
|
|
5350
|
+
const saved2 = readFile(file);
|
|
5351
|
+
return saved2 && saved2.projectId === opts.projectId ? saved2 : { projectId: opts.projectId, replId };
|
|
5352
|
+
}
|
|
5353
|
+
const host = hostOf(opts.publicUrl);
|
|
5354
|
+
const saved = readFile(file);
|
|
5355
|
+
if (saved && sameFork(saved, host, replId)) return replId && !saved.replId ? { ...saved, replId } : saved;
|
|
5356
|
+
const fresh = { projectId: newDemoProjectId(), host, replId };
|
|
5357
|
+
try {
|
|
5358
|
+
writeFile(file, fresh);
|
|
5359
|
+
} catch {
|
|
5360
|
+
}
|
|
5361
|
+
return fresh;
|
|
5362
|
+
}
|
|
5363
|
+
async function registerProject(opts) {
|
|
5364
|
+
if (!isDemoKey(opts.gameSecret)) throw new Error("registerProject is for the public demo key only.");
|
|
5365
|
+
const file = opts.file ?? DEFAULT_PROJECT_FILE;
|
|
5366
|
+
const fetchImpl = opts.fetch ?? ((input, init) => fetch(input, init));
|
|
5367
|
+
const base = (opts.baseUrl ?? SANDBOX_BASE_URL).replace(/\/+$/, "");
|
|
5368
|
+
const replId = currentReplId(opts.replId);
|
|
5369
|
+
let project = loadProject({ file, publicUrl: opts.webhookUrl, projectId: opts.projectId, replId });
|
|
5370
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
5371
|
+
const res = await fetchImpl(base + "/api/demo/projects/register", {
|
|
5372
|
+
method: "POST",
|
|
5373
|
+
redirect: "error",
|
|
5374
|
+
headers: {
|
|
5375
|
+
"Content-Type": "application/json",
|
|
5376
|
+
Accept: "application/json",
|
|
5377
|
+
"X-Game-Secret-Key": opts.gameSecret,
|
|
5378
|
+
"X-Invo-Project-Id": project.projectId,
|
|
5379
|
+
"X-Invo-Project-Host": hostOf(opts.webhookUrl) ?? ""
|
|
5380
|
+
},
|
|
5381
|
+
body: JSON.stringify({ webhook_url: opts.webhookUrl, registration_token: project.registrationToken })
|
|
5382
|
+
});
|
|
5383
|
+
const body = await res.json().catch(() => ({}));
|
|
5384
|
+
if (res.status === 409 && body.error_code === "PROJECT_REGISTERED" && attempt === 0 && !opts.projectId) {
|
|
5385
|
+
project = { projectId: newDemoProjectId(), host: hostOf(opts.webhookUrl), replId };
|
|
5386
|
+
continue;
|
|
5387
|
+
}
|
|
5388
|
+
if (!res.ok) throw new Error(`INVO demo registration failed (${res.status} ${body.error_code ?? ""}): ${body.error ?? ""}`.trim());
|
|
5389
|
+
const created = res.status === 201;
|
|
5390
|
+
if (created) {
|
|
5391
|
+
project = {
|
|
5392
|
+
projectId: project.projectId,
|
|
5393
|
+
host: hostOf(opts.webhookUrl),
|
|
5394
|
+
replId,
|
|
5395
|
+
webhookUrl: opts.webhookUrl,
|
|
5396
|
+
webhookSecret: body.webhook?.signing_secret ?? project.webhookSecret,
|
|
5397
|
+
registrationToken: body.registration_token ?? project.registrationToken
|
|
5398
|
+
};
|
|
5399
|
+
} else {
|
|
5400
|
+
project = { ...project, host: hostOf(opts.webhookUrl) ?? project.host, replId: replId ?? project.replId, webhookUrl: opts.webhookUrl };
|
|
5401
|
+
}
|
|
5402
|
+
try {
|
|
5403
|
+
writeFile(file, project);
|
|
5404
|
+
} catch {
|
|
5405
|
+
}
|
|
5406
|
+
return { projectId: project.projectId, webhookSecret: project.webhookSecret ?? null, created };
|
|
5407
|
+
}
|
|
5408
|
+
throw new Error("INVO demo registration failed.");
|
|
5409
|
+
}
|
|
5269
5410
|
|
|
5270
5411
|
// src/game/server.ts
|
|
5271
5412
|
var MAX_BODY_BYTES = 1e6;
|
|
@@ -5443,7 +5584,9 @@ function createInvoGameServer(options) {
|
|
|
5443
5584
|
premium: { name: economy.premium.name, packs: economy.premium.packs },
|
|
5444
5585
|
soft: { name: economy.soft.name },
|
|
5445
5586
|
items: economy.items,
|
|
5446
|
-
sandbox: options.sandbox
|
|
5587
|
+
sandbox: options.sandbox,
|
|
5588
|
+
demo: !!options.demoProjectId,
|
|
5589
|
+
playerFields: { phone: options.demoProjectId ? "hidden" : "first_sign_in" }
|
|
5447
5590
|
};
|
|
5448
5591
|
return json(200, body);
|
|
5449
5592
|
}
|
|
@@ -5459,9 +5602,12 @@ function createInvoGameServer(options) {
|
|
|
5459
5602
|
const name = (rawName || email.split("@")[0] || "Player").slice(0, 64);
|
|
5460
5603
|
const id = playerIdFor(email);
|
|
5461
5604
|
const known = await store.read((s) => s.players[id] ?? null);
|
|
5462
|
-
const
|
|
5605
|
+
const demoPid = options.demoProjectId;
|
|
5606
|
+
const invoEmail = demoPid ? toDemoEmail(email, demoPid) : email;
|
|
5607
|
+
const phoneForInvo = demoPid ? demoPhoneFor(invoEmail) : phone ?? known?.phone ?? void 0;
|
|
5608
|
+
const storedPhone = demoPid ? null : phone;
|
|
5463
5609
|
try {
|
|
5464
|
-
await invo.mintPlayerToken({ playerEmail:
|
|
5610
|
+
await invo.mintPlayerToken({ playerEmail: invoEmail, playerPhone: phoneForInvo, playerName: name });
|
|
5465
5611
|
} catch (err) {
|
|
5466
5612
|
if (err instanceof InvoError && err.status === 404) {
|
|
5467
5613
|
throw new GameServerError(400, "PHONE_REQUIRED", "First sign-in needs a phone number so INVO can create your account.");
|
|
@@ -5473,7 +5619,7 @@ function createInvoGameServer(options) {
|
|
|
5473
5619
|
const expiresAt = new Date(now().getTime() + sessionTtlMs).toISOString();
|
|
5474
5620
|
const player = await store.transact((s) => {
|
|
5475
5621
|
const existing = s.players[id];
|
|
5476
|
-
const record = existing ? { ...existing, name, phone:
|
|
5622
|
+
const record = existing ? { ...existing, email: invoEmail, name, phone: storedPhone ?? existing.phone } : { id, email: invoEmail, phone: storedPhone, name, createdAt: iso() };
|
|
5477
5623
|
s.players[id] = record;
|
|
5478
5624
|
if (soft.starting > 0) {
|
|
5479
5625
|
addLedger(s, { playerId: id, currency: "soft", amount: soft.starting, reason: "starting balance", ref: `start:${id}`, at: iso() });
|
|
@@ -5771,7 +5917,7 @@ function createInvoGameServer(options) {
|
|
|
5771
5917
|
}
|
|
5772
5918
|
}
|
|
5773
5919
|
async function route(request, sub, method) {
|
|
5774
|
-
if (method === "GET" && sub === "/health") return json(200, { ok: true, setupRequired: false, sandbox: options.sandbox });
|
|
5920
|
+
if (method === "GET" && sub === "/health") return json(200, { ok: true, setupRequired: false, sandbox: options.sandbox, demo: !!options.demoProjectId });
|
|
5775
5921
|
if (method === "GET" && sub === "/catalog") return catalog();
|
|
5776
5922
|
if (method === "POST" && sub === "/session") return signIn(request);
|
|
5777
5923
|
if (method === "POST" && sub === "/session/end") return signOut(request);
|
|
@@ -5812,23 +5958,33 @@ function invoGameServerFromEnv(options) {
|
|
|
5812
5958
|
const forced = env["INVO_ENV"]?.trim().toLowerCase();
|
|
5813
5959
|
const known = forced ? forced === "production" ? "production" : "sandbox" : env["INVO_BASE_URL"] || !secret || isDemoKey(secret) ? "sandbox" : null;
|
|
5814
5960
|
const state = { environment: known };
|
|
5815
|
-
const sandboxNow = () => state.environment
|
|
5816
|
-
const
|
|
5961
|
+
const sandboxNow = () => state.environment === "sandbox";
|
|
5962
|
+
const envNow = () => state.environment ?? "resolving";
|
|
5963
|
+
const demo = isDemoKey(secret);
|
|
5964
|
+
const demoProject = demo ? loadProject({ file: options.projectFile ?? DEFAULT_PROJECT_FILE, publicUrl: env["PUBLIC_URL"], projectId: (options.projectId ?? env["INVO_PROJECT_ID"])?.trim() || void 0 }) : null;
|
|
5965
|
+
const webhookSecrets = (() => {
|
|
5966
|
+
const raw = options.webhookSecret ?? env["INVO_WEBHOOK_SECRET"] ?? demoProject?.webhookSecret ?? "";
|
|
5967
|
+
return (Array.isArray(raw) ? raw : raw.split(",")).map((s) => s.trim()).filter(Boolean);
|
|
5968
|
+
})();
|
|
5969
|
+
const missing = [
|
|
5970
|
+
...secret ? [] : ["INVO_GAME_SECRET"],
|
|
5971
|
+
...webhookSecrets.length ? [] : ["INVO_WEBHOOK_SECRET"]
|
|
5972
|
+
];
|
|
5817
5973
|
if (missing.length) {
|
|
5818
|
-
return { server: setupRequiredServer(options.economy, missing, sandboxNow(), options.basePath), missing, sandbox: sandboxNow() };
|
|
5974
|
+
return { server: setupRequiredServer(options.economy, missing, sandboxNow(), options.basePath, demo), missing, sandbox: sandboxNow(), environment: envNow(), demo };
|
|
5819
5975
|
}
|
|
5820
5976
|
const baseFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
5821
|
-
const projectId = (options.projectId ?? env["INVO_PROJECT_ID"])?.trim() || void 0;
|
|
5977
|
+
const projectId = demoProject?.projectId ?? ((options.projectId ?? env["INVO_PROJECT_ID"])?.trim() || void 0);
|
|
5822
5978
|
const explicitBase = env["INVO_BASE_URL"] || (forced ? state.environment === "production" ? PRODUCTION_BASE_URL : SANDBOX_BASE_URL : "");
|
|
5823
|
-
const
|
|
5979
|
+
const resolveNow = () => resolveEnvironment(secret, { fetch: baseFetch }).then((e) => {
|
|
5824
5980
|
state.environment = e;
|
|
5825
5981
|
return e;
|
|
5826
5982
|
});
|
|
5827
|
-
|
|
5983
|
+
if (!explicitBase && !known) resolveNow().catch(() => void 0);
|
|
5828
5984
|
const invoFetch = async (input, init) => {
|
|
5829
5985
|
let url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
5830
5986
|
if (!explicitBase) {
|
|
5831
|
-
const e = known ?? await
|
|
5987
|
+
const e = known ?? state.environment ?? await resolveNow();
|
|
5832
5988
|
if (e === "sandbox" && url.startsWith(PRODUCTION_BASE_URL + "/")) url = SANDBOX_BASE_URL + url.slice(PRODUCTION_BASE_URL.length);
|
|
5833
5989
|
}
|
|
5834
5990
|
const headers = new Headers(init?.headers);
|
|
@@ -5838,7 +5994,8 @@ function invoGameServerFromEnv(options) {
|
|
|
5838
5994
|
const invo = new InvoServer({ gameSecret: secret, baseUrl: explicitBase || PRODUCTION_BASE_URL, fetch: invoFetch });
|
|
5839
5995
|
const serverOptions = {
|
|
5840
5996
|
invo,
|
|
5841
|
-
webhookSecret:
|
|
5997
|
+
webhookSecret: webhookSecrets,
|
|
5998
|
+
demoProjectId: demo ? projectId : void 0,
|
|
5842
5999
|
economy: options.economy,
|
|
5843
6000
|
store: options.store,
|
|
5844
6001
|
get sandbox() {
|
|
@@ -5849,7 +6006,7 @@ function invoGameServerFromEnv(options) {
|
|
|
5849
6006
|
log: options.log
|
|
5850
6007
|
};
|
|
5851
6008
|
const server = createInvoGameServer(serverOptions);
|
|
5852
|
-
return { server, missing, sandbox: sandboxNow() };
|
|
6009
|
+
return { server, missing, sandbox: sandboxNow(), environment: envNow(), demo };
|
|
5853
6010
|
}
|
|
5854
6011
|
function newProjectId(now = Date.now()) {
|
|
5855
6012
|
const A = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
@@ -5864,7 +6021,7 @@ function newProjectId(now = Date.now()) {
|
|
|
5864
6021
|
for (let i = 0; i < 16; i++) rand += A[bytes[i] % 32];
|
|
5865
6022
|
return "proj_" + time + rand;
|
|
5866
6023
|
}
|
|
5867
|
-
function setupRequiredServer(economy, missing, sandbox, basePathOpt) {
|
|
6024
|
+
function setupRequiredServer(economy, missing, sandbox, basePathOpt, demo = false) {
|
|
5868
6025
|
const basePath = (basePathOpt ?? "/api/invo").replace(/\/+$/, "");
|
|
5869
6026
|
const message = `Add ${missing.join(" and ")} to your environment (on Replit: Tools \u2192 Secrets), then restart.`;
|
|
5870
6027
|
return {
|
|
@@ -5873,9 +6030,9 @@ function setupRequiredServer(economy, missing, sandbox, basePathOpt) {
|
|
|
5873
6030
|
const url = new URL(request.url);
|
|
5874
6031
|
if (url.pathname !== basePath && !url.pathname.startsWith(basePath + "/")) return null;
|
|
5875
6032
|
const sub = url.pathname.slice(basePath.length) || "/";
|
|
5876
|
-
if (request.method === "GET" && sub === "/health") return json(200, { ok: true, setupRequired: true, missing, sandbox });
|
|
6033
|
+
if (request.method === "GET" && sub === "/health") return json(200, { ok: true, setupRequired: true, missing, sandbox, demo });
|
|
5877
6034
|
if (request.method === "GET" && sub === "/catalog") {
|
|
5878
|
-
const body = { premium: { name: economy.premium.name, packs: economy.premium.packs }, soft: { name: economy.soft.name }, items: economy.items, sandbox };
|
|
6035
|
+
const body = { premium: { name: economy.premium.name, packs: economy.premium.packs }, soft: { name: economy.soft.name }, items: economy.items, sandbox, demo, playerFields: { phone: demo ? "hidden" : "first_sign_in" } };
|
|
5879
6036
|
return json(200, body);
|
|
5880
6037
|
}
|
|
5881
6038
|
return json(503, { error: { code: "INVO_SETUP_REQUIRED", message } });
|
|
@@ -6001,6 +6158,8 @@ function createFileGameStore(options) {
|
|
|
6001
6158
|
};
|
|
6002
6159
|
}
|
|
6003
6160
|
|
|
6161
|
+
exports.DEFAULT_PROJECT_FILE = DEFAULT_PROJECT_FILE;
|
|
6162
|
+
exports.DEMO_EMAIL_DOMAIN = DEMO_EMAIL_DOMAIN;
|
|
6004
6163
|
exports.DEMO_KEY_PREFIX = DEMO_KEY_PREFIX;
|
|
6005
6164
|
exports.EnvironmentUnknownError = EnvironmentUnknownError;
|
|
6006
6165
|
exports.GameServerError = GameServerError;
|
|
@@ -6013,13 +6172,19 @@ exports.baseUrlForKey = baseUrlForKey;
|
|
|
6013
6172
|
exports.createFileGameStore = createFileGameStore;
|
|
6014
6173
|
exports.createInvoGameServer = createInvoGameServer;
|
|
6015
6174
|
exports.createMemoryGameStore = createMemoryGameStore;
|
|
6175
|
+
exports.demoPhoneFor = demoPhoneFor;
|
|
6016
6176
|
exports.emptyGameState = emptyGameState;
|
|
6017
6177
|
exports.invoGameServerFromEnv = invoGameServerFromEnv;
|
|
6018
6178
|
exports.isDemoKey = isDemoKey;
|
|
6179
|
+
exports.isValidProjectId = isValidProjectId;
|
|
6180
|
+
exports.loadProject = loadProject;
|
|
6019
6181
|
exports.newProjectId = newProjectId;
|
|
6020
6182
|
exports.playerIdFor = playerIdFor;
|
|
6183
|
+
exports.registerProject = registerProject;
|
|
6021
6184
|
exports.resolveEnvironment = resolveEnvironment;
|
|
6185
|
+
exports.toDemoEmail = toDemoEmail;
|
|
6022
6186
|
exports.toNodeListener = toNodeListener;
|
|
6023
6187
|
exports.validateEconomy = validateEconomy;
|
|
6188
|
+
exports.verifyInvoSignature = verifyInvoSignature;
|
|
6024
6189
|
//# sourceMappingURL=game-server.cjs.map
|
|
6025
6190
|
//# sourceMappingURL=game-server.cjs.map
|
package/dist/game-server.d.cts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { InvoServer } from './server.cjs';
|
|
2
|
-
export { DEMO_KEY_PREFIX, EnvironmentUnknownError,
|
|
3
|
-
import { G as GameCurrencyKind, a as GamePurchaseStatus, b as GameEconomyConfig } from './types-
|
|
4
|
-
export { c as GameCatalog, d as GameCatalogItem, e as GameCheckout, f as GameCurrencyBalance, g as GameErrorBody, h as GameItemPurchase, i as GamePlayer, j as GamePremiumPack, k as GamePurchase, l as GameSession, m as GameWallet } from './types-
|
|
1
|
+
import { I as InvoServer, a as InvoEnvironment } from './server-Bxo_wgHx.cjs';
|
|
2
|
+
export { D as DEMO_KEY_PREFIX, E as EnvironmentUnknownError, P as PRODUCTION_BASE_URL, S as SANDBOX_BASE_URL, b as baseUrlForKey, i as isDemoKey, r as resolveEnvironment, v as verifyInvoSignature } from './server-Bxo_wgHx.cjs';
|
|
3
|
+
import { G as GameCurrencyKind, a as GamePurchaseStatus, b as GameEconomyConfig } from './types-CLyJDuXd.cjs';
|
|
4
|
+
export { c as GameCatalog, d as GameCatalogItem, e as GameCheckout, f as GameCurrencyBalance, g as GameErrorBody, h as GameItemPurchase, i as GamePlayer, j as GamePremiumPack, k as GamePurchase, l as GameSession, m as GameWallet } from './types-CLyJDuXd.cjs';
|
|
5
5
|
export { I as InvoError } from './types-_lIm74kd.cjs';
|
|
6
|
-
export { S as SDK_VERSION } from './version-
|
|
6
|
+
export { S as SDK_VERSION } from './version-DOaYbHu7.cjs';
|
|
7
7
|
|
|
8
8
|
interface StoredPlayer {
|
|
9
9
|
id: string;
|
|
@@ -109,6 +109,12 @@ interface InvoGameServerOptions {
|
|
|
109
109
|
now?: () => Date;
|
|
110
110
|
/** Structured log sink. Never receives secrets, tokens or full phone numbers. */
|
|
111
111
|
log?: (level: GameServerLogLevel, message: string, fields?: Record<string, unknown>) => void;
|
|
112
|
+
/**
|
|
113
|
+
* Set when `invo` uses the public demo key (3.29.0): sign-ins are mapped to the demo
|
|
114
|
+
* sandbox's player email format for this project and given a fictional phone, so no real
|
|
115
|
+
* address or number ever reaches INVO. {@link invoGameServerFromEnv} sets it for you.
|
|
116
|
+
*/
|
|
117
|
+
demoProjectId?: string;
|
|
112
118
|
}
|
|
113
119
|
interface InvoGameServer {
|
|
114
120
|
readonly basePath: string;
|
|
@@ -143,6 +149,16 @@ interface InvoGameServerFromEnvOptions {
|
|
|
143
149
|
* secret. `INVO_PROJECT_ID` in `env` is used when this is not given.
|
|
144
150
|
*/
|
|
145
151
|
projectId?: string;
|
|
152
|
+
/**
|
|
153
|
+
* The webhook signing secret, when it does not come from `INVO_WEBHOOK_SECRET` (3.29.0):
|
|
154
|
+
* for example the one {@link registerProject} stored for a template fork.
|
|
155
|
+
*/
|
|
156
|
+
webhookSecret?: string | string[];
|
|
157
|
+
/**
|
|
158
|
+
* Public demo key only: where the fork's project id, webhook secret and registration token
|
|
159
|
+
* are kept. Default `.invo/project.json`. Keep it out of version control.
|
|
160
|
+
*/
|
|
161
|
+
projectFile?: string;
|
|
146
162
|
/** Override `fetch` for the calls to INVO (tests, proxies). */
|
|
147
163
|
fetch?: typeof fetch;
|
|
148
164
|
}
|
|
@@ -151,7 +167,12 @@ interface InvoGameServerSetup {
|
|
|
151
167
|
server: InvoGameServer;
|
|
152
168
|
/** Names of required environment variables that are not set. Empty when ready. */
|
|
153
169
|
missing: string[];
|
|
170
|
+
/** True once the key is known to be a sandbox key. False while a live key is being looked up. */
|
|
154
171
|
sandbox: boolean;
|
|
172
|
+
/** `"sandbox"`, `"production"`, or `"resolving"` while a key's environment is being looked up. */
|
|
173
|
+
environment: InvoEnvironment | "resolving";
|
|
174
|
+
/** True when the key is INVO's public demo key (3.29.1). */
|
|
175
|
+
demo: boolean;
|
|
155
176
|
}
|
|
156
177
|
/**
|
|
157
178
|
* Build the game server from environment variables (Replit Secrets, `.env`, your host's
|
|
@@ -190,4 +211,62 @@ interface NodeResponseLike {
|
|
|
190
211
|
*/
|
|
191
212
|
declare function toNodeListener(server: InvoGameServer, fallback?: (req: NodeRequestLike, res: NodeResponseLike) => unknown): (req: NodeRequestLike, res: NodeResponseLike) => Promise<void>;
|
|
192
213
|
|
|
193
|
-
|
|
214
|
+
declare const DEMO_EMAIL_DOMAIN = "demo.invo.network";
|
|
215
|
+
declare const DEFAULT_PROJECT_FILE = ".invo/project.json";
|
|
216
|
+
/** What the project file holds. Secrets: keep the file out of version control. */
|
|
217
|
+
interface DemoProjectFile {
|
|
218
|
+
projectId: string;
|
|
219
|
+
/** The public host the project was registered from (remix detection outside Replit). */
|
|
220
|
+
host?: string;
|
|
221
|
+
/** Replit's REPL_ID when the file was written. A different REPL_ID means a remix. */
|
|
222
|
+
replId?: string;
|
|
223
|
+
webhookUrl?: string;
|
|
224
|
+
webhookSecret?: string;
|
|
225
|
+
registrationToken?: string;
|
|
226
|
+
}
|
|
227
|
+
declare function isValidProjectId(value: unknown): value is string;
|
|
228
|
+
/**
|
|
229
|
+
* The demo-sandbox email for a player, deterministic per (email, project):
|
|
230
|
+
* `<local part, cleaned>-<8 hex of sha256(email)>+<project ulid lowercase>@demo.invo.network`.
|
|
231
|
+
*/
|
|
232
|
+
declare function toDemoEmail(email: string, projectId: string): string;
|
|
233
|
+
/** The fictional phone INVO's demo sandbox accepts for a player: `+1202555 01XX`. */
|
|
234
|
+
declare function demoPhoneFor(email: string): string;
|
|
235
|
+
/**
|
|
236
|
+
* This fork's project, from `INVO_PROJECT_ID`, else the project file, else a new id written to
|
|
237
|
+
* the file. A project file copied by a remix (see `sameFork`) is replaced by a fresh project id.
|
|
238
|
+
*/
|
|
239
|
+
declare function loadProject(opts?: {
|
|
240
|
+
file?: string;
|
|
241
|
+
publicUrl?: string;
|
|
242
|
+
projectId?: string;
|
|
243
|
+
replId?: string;
|
|
244
|
+
}): DemoProjectFile;
|
|
245
|
+
interface RegisterProjectOptions {
|
|
246
|
+
/** The public demo key (`ivsdk_demo_...`). */
|
|
247
|
+
gameSecret: string;
|
|
248
|
+
/** This fork's public webhook URL, e.g. `https://my-game.replit.app/api/invo/webhook`. */
|
|
249
|
+
webhookUrl: string;
|
|
250
|
+
file?: string;
|
|
251
|
+
projectId?: string;
|
|
252
|
+
baseUrl?: string;
|
|
253
|
+
fetch?: typeof fetch;
|
|
254
|
+
/** Overrides Replit's REPL_ID (tests). */
|
|
255
|
+
replId?: string;
|
|
256
|
+
}
|
|
257
|
+
interface RegisteredProject {
|
|
258
|
+
projectId: string;
|
|
259
|
+
/** The signing secret to verify INVO webhooks with, or null if INVO never returned one. */
|
|
260
|
+
webhookSecret: string | null;
|
|
261
|
+
/** True when this call created the registration (a new fork). */
|
|
262
|
+
created: boolean;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Register this fork with the demo sandbox (call on every server start). The first call
|
|
266
|
+
* stores the webhook signing secret and the registration token in the project file; later
|
|
267
|
+
* calls change nothing unless the webhook URL moved. A `409 PROJECT_REGISTERED` (a remix that
|
|
268
|
+
* copied another fork's project id) gets a fresh project id, once.
|
|
269
|
+
*/
|
|
270
|
+
declare function registerProject(opts: RegisterProjectOptions): Promise<RegisteredProject>;
|
|
271
|
+
|
|
272
|
+
export { DEFAULT_PROJECT_FILE, DEMO_EMAIL_DOMAIN, type DemoProjectFile, type FileGameStoreOptions, GameCurrencyKind, GameEconomyConfig, GamePurchaseStatus, GameServerError, type GameServerLogLevel, type GameStore, type GameStoreState, InvoEnvironment, type InvoGameServer, type InvoGameServerFromEnvOptions, type InvoGameServerOptions, type InvoGameServerSetup, InvoServer, type LedgerEntry, type NodeRequestLike, type NodeResponseLike, type RegisterProjectOptions, type RegisteredProject, type StoredIdempotentResult, type StoredPlayer, type StoredPurchase, type StoredSession, createFileGameStore, createInvoGameServer, createMemoryGameStore, demoPhoneFor, emptyGameState, invoGameServerFromEnv, isValidProjectId, loadProject, newProjectId, playerIdFor, registerProject, toDemoEmail, toNodeListener, validateEconomy };
|