scorezilla 0.3.0-next.1 → 0.3.0-next.2
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/API.md +77 -7
- package/CHANGELOG.md +58 -0
- package/README.md +77 -0
- package/RECIPES.md +186 -0
- package/dist/{errors-B7hyC-C5.d.cts → errors-CtXMAHtJ.d.cts} +1 -1
- package/dist/{errors-B7hyC-C5.d.ts → errors-CtXMAHtJ.d.ts} +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/server.cjs +215 -1
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +249 -3
- package/dist/server.d.ts +249 -3
- package/dist/server.js +210 -2
- package/dist/server.js.map +1 -1
- package/package.json +11 -1
package/dist/server.cjs
CHANGED
|
@@ -624,13 +624,97 @@ function defaultUserAgent(version, runtime = detectRuntime()) {
|
|
|
624
624
|
return `scorezilla-js/${version} (${runtime})`;
|
|
625
625
|
}
|
|
626
626
|
|
|
627
|
+
// src/verifiers.ts
|
|
628
|
+
async function loadJose() {
|
|
629
|
+
try {
|
|
630
|
+
return await import('jose');
|
|
631
|
+
} catch (cause) {
|
|
632
|
+
throw new Error(
|
|
633
|
+
"scorezilla/server: the optional peer dependency 'jose' is required for verifyJwt() / verifySupabaseJwt(). Install it with `npm i jose` (or your package manager).",
|
|
634
|
+
{ cause }
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
function extractBearerToken(req) {
|
|
639
|
+
const header = req.headers.get("authorization");
|
|
640
|
+
if (!header) return null;
|
|
641
|
+
const trimmed = header.trim();
|
|
642
|
+
if (!/^bearer\s+/i.test(trimmed)) return null;
|
|
643
|
+
return trimmed.replace(/^bearer\s+/i, "").trim() || null;
|
|
644
|
+
}
|
|
645
|
+
function verifyJwt(options) {
|
|
646
|
+
const claim = options.claim ?? "sub";
|
|
647
|
+
let keySet = null;
|
|
648
|
+
return async (req) => {
|
|
649
|
+
const token = extractBearerToken(req);
|
|
650
|
+
if (token === null) return null;
|
|
651
|
+
const jose = await loadJose();
|
|
652
|
+
if (keySet === null) {
|
|
653
|
+
keySet = jose.createRemoteJWKSet(
|
|
654
|
+
new URL(options.jwksUrl),
|
|
655
|
+
options.fetch ? { [jose.customFetch]: options.fetch } : void 0
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
try {
|
|
659
|
+
const { payload } = await jose.jwtVerify(token, keySet, {
|
|
660
|
+
...options.issuer !== void 0 ? { issuer: options.issuer } : {},
|
|
661
|
+
...options.audience !== void 0 ? { audience: options.audience } : {}
|
|
662
|
+
});
|
|
663
|
+
const id = payload[claim];
|
|
664
|
+
return typeof id === "string" && id.length > 0 ? { playerId: id } : null;
|
|
665
|
+
} catch {
|
|
666
|
+
return null;
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
function verifySupabaseJwt(options) {
|
|
671
|
+
const base = options.supabaseUrl.replace(/\/+$/, "");
|
|
672
|
+
return verifyJwt({
|
|
673
|
+
jwksUrl: `${base}/auth/v1/.well-known/jwks.json`,
|
|
674
|
+
issuer: `${base}/auth/v1`,
|
|
675
|
+
audience: "authenticated",
|
|
676
|
+
...options.claim !== void 0 ? { claim: options.claim } : {},
|
|
677
|
+
...options.fetch !== void 0 ? { fetch: options.fetch } : {}
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
function verifyClerkJwt(options) {
|
|
681
|
+
const issuer = options.issuer.replace(/\/+$/, "");
|
|
682
|
+
return verifyJwt({
|
|
683
|
+
jwksUrl: `${issuer}/.well-known/jwks.json`,
|
|
684
|
+
issuer,
|
|
685
|
+
...options.audience !== void 0 ? { audience: options.audience } : {},
|
|
686
|
+
...options.claim !== void 0 ? { claim: options.claim } : {},
|
|
687
|
+
...options.fetch !== void 0 ? { fetch: options.fetch } : {}
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
function verifyAuth0Jwt(options) {
|
|
691
|
+
const host = options.domain.replace(/^https?:\/\//, "").replace(/\/+$/, "");
|
|
692
|
+
return verifyJwt({
|
|
693
|
+
jwksUrl: `https://${host}/.well-known/jwks.json`,
|
|
694
|
+
issuer: `https://${host}/`,
|
|
695
|
+
audience: options.audience,
|
|
696
|
+
...options.claim !== void 0 ? { claim: options.claim } : {},
|
|
697
|
+
...options.fetch !== void 0 ? { fetch: options.fetch } : {}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
var FIREBASE_JWKS_URL = "https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com";
|
|
701
|
+
function verifyFirebaseIdToken(options) {
|
|
702
|
+
return verifyJwt({
|
|
703
|
+
jwksUrl: FIREBASE_JWKS_URL,
|
|
704
|
+
issuer: `https://securetoken.google.com/${options.projectId}`,
|
|
705
|
+
audience: options.projectId,
|
|
706
|
+
...options.claim !== void 0 ? { claim: options.claim } : {},
|
|
707
|
+
...options.fetch !== void 0 ? { fetch: options.fetch } : {}
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
|
|
627
711
|
// src/server.ts
|
|
628
712
|
var Scorezilla = class _Scorezilla {
|
|
629
713
|
/** The package version, injected at build time from `package.json`.
|
|
630
714
|
* Mirrors the static on the public-key client so consumers can log
|
|
631
715
|
* the running SDK build the same way regardless of which surface
|
|
632
716
|
* they imported. */
|
|
633
|
-
static version = "0.3.0-next.
|
|
717
|
+
static version = "0.3.0-next.2";
|
|
634
718
|
#keyId;
|
|
635
719
|
#secret;
|
|
636
720
|
#baseUrl;
|
|
@@ -772,8 +856,138 @@ function isRealBrowserEnvironment() {
|
|
|
772
856
|
const hasNodeLikeHost = Boolean(g.process?.versions?.node) || typeof g.Deno !== "undefined" || typeof g.Bun !== "undefined";
|
|
773
857
|
return !hasNodeLikeHost;
|
|
774
858
|
}
|
|
859
|
+
function createScoreSubmitHandler(config) {
|
|
860
|
+
const sz = new Scorezilla({
|
|
861
|
+
secretKey: config.secretKey,
|
|
862
|
+
...config.baseUrl !== void 0 ? { baseUrl: config.baseUrl } : {},
|
|
863
|
+
...config.fetch !== void 0 ? { fetch: config.fetch } : {},
|
|
864
|
+
...config.maxRetries !== void 0 ? { maxRetries: config.maxRetries } : {},
|
|
865
|
+
...config.timeoutMs !== void 0 ? { timeoutMs: config.timeoutMs } : {}
|
|
866
|
+
});
|
|
867
|
+
const parse = config.parseSubmission ?? defaultParseSubmission;
|
|
868
|
+
return async (req) => {
|
|
869
|
+
const cors = config.cors ? buildCorsHeaders(config.cors, req.headers.get("Origin")) : {};
|
|
870
|
+
if (req.method === "OPTIONS" && config.cors) {
|
|
871
|
+
return new Response(null, { status: 204, headers: cors });
|
|
872
|
+
}
|
|
873
|
+
if (req.method !== "POST") {
|
|
874
|
+
return jsonResponse({ ok: false, error: "method_not_allowed" }, 405, cors);
|
|
875
|
+
}
|
|
876
|
+
if (config.rateLimit) {
|
|
877
|
+
const decision = await config.rateLimit(req);
|
|
878
|
+
if (!decision.ok) {
|
|
879
|
+
const headers = { ...cors };
|
|
880
|
+
if (decision.retryAfterSeconds !== void 0) {
|
|
881
|
+
headers["Retry-After"] = String(decision.retryAfterSeconds);
|
|
882
|
+
}
|
|
883
|
+
return jsonResponse({ ok: false, error: "rate_limited" }, 429, headers);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
let identity;
|
|
887
|
+
try {
|
|
888
|
+
identity = await config.verify(req);
|
|
889
|
+
} catch {
|
|
890
|
+
identity = null;
|
|
891
|
+
}
|
|
892
|
+
if (!identity || typeof identity.playerId !== "string" || identity.playerId.length === 0) {
|
|
893
|
+
return jsonResponse({ ok: false, error: "unauthorized" }, 401, cors);
|
|
894
|
+
}
|
|
895
|
+
let submission;
|
|
896
|
+
try {
|
|
897
|
+
submission = await parse(req);
|
|
898
|
+
} catch {
|
|
899
|
+
submission = null;
|
|
900
|
+
}
|
|
901
|
+
if (!submission || typeof submission.score !== "number" || !Number.isFinite(submission.score)) {
|
|
902
|
+
return jsonResponse(
|
|
903
|
+
{ ok: false, error: "bad_request", message: "invalid score submission" },
|
|
904
|
+
400,
|
|
905
|
+
cors
|
|
906
|
+
);
|
|
907
|
+
}
|
|
908
|
+
const metadata = mergeMetadata(submission.metadata, identity.metadata);
|
|
909
|
+
try {
|
|
910
|
+
const result = await sz.submitScore({
|
|
911
|
+
boardId: config.boardId,
|
|
912
|
+
playerId: identity.playerId,
|
|
913
|
+
score: submission.score,
|
|
914
|
+
...metadata !== void 0 ? { metadata } : {}
|
|
915
|
+
});
|
|
916
|
+
return jsonResponse(
|
|
917
|
+
{
|
|
918
|
+
ok: true,
|
|
919
|
+
rank: result.rank,
|
|
920
|
+
totalEntries: result.totalEntries,
|
|
921
|
+
isPersonalBest: result.isPersonalBest
|
|
922
|
+
},
|
|
923
|
+
200,
|
|
924
|
+
cors
|
|
925
|
+
);
|
|
926
|
+
} catch (err) {
|
|
927
|
+
return mapSubmitError(err, cors);
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
async function defaultParseSubmission(req) {
|
|
932
|
+
const raw = await req.json().catch(() => null);
|
|
933
|
+
if (!isPlainObject(raw)) return null;
|
|
934
|
+
const score = raw.score;
|
|
935
|
+
if (typeof score !== "number" || !Number.isFinite(score)) return null;
|
|
936
|
+
return { score, metadata: isPlainObject(raw.metadata) ? raw.metadata : void 0 };
|
|
937
|
+
}
|
|
938
|
+
function isPlainObject(value) {
|
|
939
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
940
|
+
}
|
|
941
|
+
function mergeMetadata(client, verified) {
|
|
942
|
+
if (!client && !verified) return void 0;
|
|
943
|
+
return { ...client ?? {}, ...verified ?? {} };
|
|
944
|
+
}
|
|
945
|
+
function jsonResponse(body, status, extra) {
|
|
946
|
+
return new Response(JSON.stringify(body), {
|
|
947
|
+
status,
|
|
948
|
+
headers: { "Content-Type": "application/json; charset=utf-8", ...extra }
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
function mapSubmitError(err, cors) {
|
|
952
|
+
if (err instanceof ScorezillaError) {
|
|
953
|
+
if (err.isRateLimited()) {
|
|
954
|
+
const headers = { ...cors };
|
|
955
|
+
if (err.retryAfter !== void 0) headers["Retry-After"] = String(err.retryAfter);
|
|
956
|
+
return jsonResponse({ ok: false, error: "rate_limited" }, 429, headers);
|
|
957
|
+
}
|
|
958
|
+
if (err.status >= 400 && err.status < 500) {
|
|
959
|
+
return jsonResponse({ ok: false, error: err.code, message: err.message }, err.status, cors);
|
|
960
|
+
}
|
|
961
|
+
return jsonResponse({ ok: false, error: "upstream_error" }, 502, cors);
|
|
962
|
+
}
|
|
963
|
+
return jsonResponse({ ok: false, error: "server_error" }, 500, cors);
|
|
964
|
+
}
|
|
965
|
+
function buildCorsHeaders(cors, origin) {
|
|
966
|
+
const headers = {
|
|
967
|
+
"Access-Control-Allow-Methods": (cors.methods ?? ["POST", "OPTIONS"]).join(", "),
|
|
968
|
+
"Access-Control-Allow-Headers": (cors.headers ?? ["content-type", "authorization"]).join(", "),
|
|
969
|
+
"Access-Control-Max-Age": String(cors.maxAgeSeconds ?? 600),
|
|
970
|
+
Vary: "Origin"
|
|
971
|
+
};
|
|
972
|
+
if (origin !== null && isCorsOriginAllowed(cors.origin, origin)) {
|
|
973
|
+
headers["Access-Control-Allow-Origin"] = origin;
|
|
974
|
+
}
|
|
975
|
+
return headers;
|
|
976
|
+
}
|
|
977
|
+
function isCorsOriginAllowed(rule, origin) {
|
|
978
|
+
if (typeof rule === "boolean") return rule;
|
|
979
|
+
if (typeof rule === "string") return rule === origin;
|
|
980
|
+
if (typeof rule === "function") return rule(origin);
|
|
981
|
+
return rule.includes(origin);
|
|
982
|
+
}
|
|
775
983
|
|
|
776
984
|
exports.Scorezilla = Scorezilla;
|
|
777
985
|
exports.ScorezillaError = ScorezillaError;
|
|
986
|
+
exports.createScoreSubmitHandler = createScoreSubmitHandler;
|
|
987
|
+
exports.verifyAuth0Jwt = verifyAuth0Jwt;
|
|
988
|
+
exports.verifyClerkJwt = verifyClerkJwt;
|
|
989
|
+
exports.verifyFirebaseIdToken = verifyFirebaseIdToken;
|
|
990
|
+
exports.verifyJwt = verifyJwt;
|
|
991
|
+
exports.verifySupabaseJwt = verifySupabaseJwt;
|
|
778
992
|
//# sourceMappingURL=server.cjs.map
|
|
779
993
|
//# sourceMappingURL=server.cjs.map
|