@astl/server 0.0.0-stage → 0.1.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/CHANGELOG.md +23 -0
- package/LICENSE +202 -0
- package/README.md +41 -2
- package/dist/handler.d.ts +41 -0
- package/dist/handler.d.ts.map +1 -0
- package/dist/handler.js +290 -0
- package/dist/handler.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/store.d.ts +61 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +60 -0
- package/dist/store.js.map +1 -0
- package/package.json +42 -3
- package/src/handler.ts +340 -0
- package/src/index.ts +4 -0
- package/src/store.ts +124 -0
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempt storage for the grading handler. The handler only needs three operations (four with hints), so any database
|
|
3
|
+
* can back it; `createMemoryAttemptStore` is the reference implementation for examples, tests and single-process
|
|
4
|
+
* servers.
|
|
5
|
+
*/
|
|
6
|
+
import { type AnswerKey, type Answers, type AttemptPolicy, type SubmitResponse } from '@astl/core';
|
|
7
|
+
/** One attempt as the grading handler sees it. The key never leaves the server. */
|
|
8
|
+
export interface AttemptRecord {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly key: AnswerKey;
|
|
11
|
+
readonly policy: AttemptPolicy;
|
|
12
|
+
/** Checks counted so far. */
|
|
13
|
+
readonly checksUsed: number;
|
|
14
|
+
readonly submitted: boolean;
|
|
15
|
+
/** ISO 8601. */
|
|
16
|
+
readonly createdAt: string;
|
|
17
|
+
/**
|
|
18
|
+
* Hints given so far, by view element or page id (`recordHint`); absent means none. Grading takes their costs off,
|
|
19
|
+
* so a host that re-grades a submission later passes them on (`engine.grade(key, answers, { hintsUsed })`).
|
|
20
|
+
*/
|
|
21
|
+
readonly hintsUsed?: Readonly<Record<string, number>>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* What a submit stores: the final answers and the response the student got (re-grade the answers to release more
|
|
25
|
+
* later). The answers are those within their slot kind's length limit, open answers (for the teacher) included:
|
|
26
|
+
* `Metadata.open` lists their slot ids.
|
|
27
|
+
*/
|
|
28
|
+
export interface Submission {
|
|
29
|
+
readonly answers: Answers;
|
|
30
|
+
readonly response: SubmitResponse;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Storage the grading handler needs. The writes must be atomic per attempt (for example `UPDATE ... RETURNING` or a
|
|
34
|
+
* conditional update), so parallel requests can neither exceed `maxChecks`, get the same hint twice nor submit twice.
|
|
35
|
+
*/
|
|
36
|
+
export interface AttemptStore {
|
|
37
|
+
/** null for an unknown attempt. */
|
|
38
|
+
get(id: string): Promise<AttemptRecord | null>;
|
|
39
|
+
/** Counts one check; resolves to the record after counting, or null for an unknown attempt. */
|
|
40
|
+
recordCheck(id: string): Promise<AttemptRecord | null>;
|
|
41
|
+
/** Closes the attempt with its submission; resolves false when it was already submitted (or is unknown). */
|
|
42
|
+
recordSubmit(id: string, submission: Submission): Promise<boolean>;
|
|
43
|
+
/**
|
|
44
|
+
* Counts one hint of `element` (a view element or page id) in `hintsUsed`; resolves to the record after counting, or
|
|
45
|
+
* null for an unknown attempt. Optional: a store without it offers no hints (`403 hints_not_allowed`).
|
|
46
|
+
*/
|
|
47
|
+
recordHint?(id: string, element: string): Promise<AttemptRecord | null>;
|
|
48
|
+
}
|
|
49
|
+
export interface NewAttempt {
|
|
50
|
+
readonly key: AnswerKey;
|
|
51
|
+
readonly policy: AttemptPolicy;
|
|
52
|
+
}
|
|
53
|
+
export interface MemoryAttemptStore extends AttemptStore {
|
|
54
|
+
/** Starts an attempt with a random id. Throws a TypeError for an invalid key or policy. */
|
|
55
|
+
create(attempt: NewAttempt): Promise<AttemptRecord>;
|
|
56
|
+
/** The stored submission of an attempt, or null. */
|
|
57
|
+
submission(id: string): Promise<Submission | null>;
|
|
58
|
+
}
|
|
59
|
+
/** An AttemptStore in a Map: fine for one process; attempts are lost on restart. */
|
|
60
|
+
export declare function createMemoryAttemptStore(): MemoryAttemptStore;
|
|
61
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,aAAa,EAGlB,KAAK,cAAc,EACpB,MAAM,YAAY,CAAC;AAEpB,mFAAmF;AACnF,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,6BAA6B;IAC7B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,gBAAgB;IAChB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACvD;AAED;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,mCAAmC;IACnC,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAC;IAC/C,+FAA+F;IAC/F,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAC;IACvD,4GAA4G;IAC5G,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACnE;;;OAGG;IACH,UAAU,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAC;CACzE;AAED,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;CAChC;AAED,MAAM,WAAW,kBAAmB,SAAQ,YAAY;IACtD,2FAA2F;IAC3F,MAAM,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACpD,oDAAoD;IACpD,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;CACpD;AAOD,oFAAoF;AACpF,wBAAgB,wBAAwB,IAAI,kBAAkB,CA8C7D"}
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempt storage for the grading handler. The handler only needs three operations (four with hints), so any database
|
|
3
|
+
* can back it; `createMemoryAttemptStore` is the reference implementation for examples, tests and single-process
|
|
4
|
+
* servers.
|
|
5
|
+
*/
|
|
6
|
+
import { isAnswerKey, isAttemptPolicy, } from '@astl/core';
|
|
7
|
+
/** An AttemptStore in a Map: fine for one process; attempts are lost on restart. */
|
|
8
|
+
export function createMemoryAttemptStore() {
|
|
9
|
+
const entries = new Map();
|
|
10
|
+
return {
|
|
11
|
+
async create({ key, policy }) {
|
|
12
|
+
if (!isAnswerKey(key))
|
|
13
|
+
throw new TypeError('createMemoryAttemptStore: invalid answer key');
|
|
14
|
+
if (!isAttemptPolicy(policy))
|
|
15
|
+
throw new TypeError('createMemoryAttemptStore: invalid attempt policy');
|
|
16
|
+
const record = {
|
|
17
|
+
id: crypto.randomUUID(),
|
|
18
|
+
key,
|
|
19
|
+
policy,
|
|
20
|
+
checksUsed: 0,
|
|
21
|
+
submitted: false,
|
|
22
|
+
createdAt: new Date().toISOString(),
|
|
23
|
+
hintsUsed: {},
|
|
24
|
+
};
|
|
25
|
+
entries.set(record.id, { record, submission: null });
|
|
26
|
+
return record;
|
|
27
|
+
},
|
|
28
|
+
async get(id) {
|
|
29
|
+
return entries.get(id)?.record ?? null;
|
|
30
|
+
},
|
|
31
|
+
async recordCheck(id) {
|
|
32
|
+
const entry = entries.get(id);
|
|
33
|
+
if (entry === undefined)
|
|
34
|
+
return null;
|
|
35
|
+
entry.record = { ...entry.record, checksUsed: entry.record.checksUsed + 1 };
|
|
36
|
+
return entry.record;
|
|
37
|
+
},
|
|
38
|
+
async recordHint(id, element) {
|
|
39
|
+
const entry = entries.get(id);
|
|
40
|
+
if (entry === undefined)
|
|
41
|
+
return null;
|
|
42
|
+
const used = entry.record.hintsUsed ?? {};
|
|
43
|
+
const count = Object.hasOwn(used, element) ? (used[element] ?? 0) : 0;
|
|
44
|
+
entry.record = { ...entry.record, hintsUsed: { ...used, [element]: count + 1 } };
|
|
45
|
+
return entry.record;
|
|
46
|
+
},
|
|
47
|
+
async recordSubmit(id, submission) {
|
|
48
|
+
const entry = entries.get(id);
|
|
49
|
+
if (entry === undefined || entry.record.submitted)
|
|
50
|
+
return false;
|
|
51
|
+
entry.record = { ...entry.record, submitted: true };
|
|
52
|
+
entry.submission = submission;
|
|
53
|
+
return true;
|
|
54
|
+
},
|
|
55
|
+
async submission(id) {
|
|
56
|
+
return entries.get(id)?.submission ?? null;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astl/server",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Fetch-API grading endpoint for Astrolabe (check and submit) that runs on Node, Deno, Bun and Workers.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "The Astrolabe Authors",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/starship1337/astrolabe.git",
|
|
10
|
+
"directory": "packages/server"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://astrolabe.starship.education",
|
|
13
|
+
"bugs": "https://github.com/starship1337/astrolabe/issues",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"@astl/source": "./src/index.ts",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src",
|
|
28
|
+
"CHANGELOG.md",
|
|
29
|
+
"!src/**/*.test.ts",
|
|
30
|
+
"!src/**/testing"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22.12"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@astl/core": "0.1.0",
|
|
40
|
+
"@astl/engine": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@astl/player": "0.1.0"
|
|
44
|
+
}
|
|
6
45
|
}
|
package/src/handler.ts
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The grading endpoint (protocol 1, apps/docs/src/content/docs/reference/protocol.md) on the standard Fetch API, so it
|
|
3
|
+
* runs unchanged on Node 22+, Deno, Bun, Cloudflare Workers and Hono. The rules mirror the engine's `localChecker`, so
|
|
4
|
+
* a player behaves the same against a server as with local grading.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
type AnswerKey,
|
|
8
|
+
type Answers,
|
|
9
|
+
type AttemptPolicy,
|
|
10
|
+
type CheckRequest,
|
|
11
|
+
type Engine,
|
|
12
|
+
fitsAnswer,
|
|
13
|
+
type GradeOptions,
|
|
14
|
+
type GradeResult,
|
|
15
|
+
type HintRequest,
|
|
16
|
+
helpPolicy,
|
|
17
|
+
isAnswerKey,
|
|
18
|
+
isAnswers,
|
|
19
|
+
isAttemptPolicy,
|
|
20
|
+
JSON_MEDIA_TYPE,
|
|
21
|
+
MAX_REQUEST_BYTES,
|
|
22
|
+
PROBLEM_MEDIA_TYPE,
|
|
23
|
+
ProblemCode,
|
|
24
|
+
type ProblemCodeValue,
|
|
25
|
+
type ProblemDetails,
|
|
26
|
+
ProtocolPath,
|
|
27
|
+
type SubmitResponse,
|
|
28
|
+
} from '@astl/core';
|
|
29
|
+
import { helpShown, hintCount, isGradedElement, nextHint, resultHelp } from '@astl/engine';
|
|
30
|
+
import type { AttemptRecord, AttemptStore } from './store.js';
|
|
31
|
+
|
|
32
|
+
export interface GradingHandlerOptions {
|
|
33
|
+
readonly engine: Engine;
|
|
34
|
+
readonly attempts: AttemptStore;
|
|
35
|
+
/** Clock in milliseconds, for deadlines and `submittedAt`. Default `Date.now`. */
|
|
36
|
+
readonly now?: () => number;
|
|
37
|
+
/**
|
|
38
|
+
* Called with every internal failure (a store that throws or returns an inconsistent record, a grading error), for
|
|
39
|
+
* logging. The response stays a generic `500 internal_error` either way. Default: nothing.
|
|
40
|
+
*/
|
|
41
|
+
readonly onError?: (error: unknown, request: Request) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Which attempt a request is for, as the host resolved it after authorising the request. */
|
|
45
|
+
export interface GradingContext {
|
|
46
|
+
readonly attemptId: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Handles `POST {endpoint}/check`, `/submit` and `/hint`. The host passes the attempt: the handler reads only the
|
|
51
|
+
* action (the final path segment) from the URL, so no part of the URL can point it at another attempt.
|
|
52
|
+
*/
|
|
53
|
+
export type GradingHandler = (request: Request, context: GradingContext) => Promise<Response>;
|
|
54
|
+
|
|
55
|
+
/** Attempt ids the handler accepts; anything else is answered `404 not_found`. */
|
|
56
|
+
export const ATTEMPT_ID_PATTERN: RegExp = /^[A-Za-z0-9_-]{1,128}$/;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A submit this late after the deadline still counts: the player hands in when its countdown ends, and that request
|
|
60
|
+
* needs a moment to arrive. Later submits are refused like any submit on a closed attempt.
|
|
61
|
+
*/
|
|
62
|
+
export const DEADLINE_GRACE_MS = 30_000;
|
|
63
|
+
|
|
64
|
+
/** Titles are for developers (RFC 9457); players show their own translated messages per `code`. */
|
|
65
|
+
const PROBLEMS: Readonly<Record<ProblemCodeValue, { readonly status: number; readonly title: string }>> = {
|
|
66
|
+
invalid_request: { status: 400, title: 'The body is not a valid grading request' },
|
|
67
|
+
unauthorized: { status: 401, title: 'The attempt could not be identified' },
|
|
68
|
+
check_not_allowed: { status: 403, title: 'Checking is not allowed in this attempt' },
|
|
69
|
+
checks_exhausted: { status: 403, title: 'No checks are left in this attempt' },
|
|
70
|
+
hints_not_allowed: { status: 403, title: 'Hints are not offered in this attempt' },
|
|
71
|
+
hints_exhausted: { status: 403, title: 'Every hint of this interaction or page is used' },
|
|
72
|
+
not_found: { status: 404, title: 'Unknown attempt, page or element' },
|
|
73
|
+
method_not_allowed: { status: 405, title: 'Only POST is allowed' },
|
|
74
|
+
attempt_closed: { status: 409, title: 'The attempt is already submitted' },
|
|
75
|
+
payload_too_large: { status: 413, title: 'The body is larger than 256 KiB' },
|
|
76
|
+
internal_error: { status: 500, title: 'Internal server error' },
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const HEADERS = { 'cache-control': 'no-store' } as const;
|
|
80
|
+
|
|
81
|
+
function json(body: unknown): Response {
|
|
82
|
+
return new Response(JSON.stringify(body), {
|
|
83
|
+
status: 200,
|
|
84
|
+
headers: { ...HEADERS, 'content-type': JSON_MEDIA_TYPE },
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function problem(code: ProblemCodeValue, headers: Readonly<Record<string, string>> = {}): Response {
|
|
89
|
+
const { status, title } = PROBLEMS[code];
|
|
90
|
+
const body: ProblemDetails = { type: `urn:astrolabe:problem:${code}`, title, status, code };
|
|
91
|
+
return new Response(JSON.stringify(body), {
|
|
92
|
+
status,
|
|
93
|
+
headers: { ...HEADERS, ...headers, 'content-type': PROBLEM_MEDIA_TYPE },
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
type Action = typeof ProtocolPath.check | typeof ProtocolPath.submit | typeof ProtocolPath.hint;
|
|
98
|
+
|
|
99
|
+
/** The final path segment when it is an action; null otherwise (a trailing slash included). */
|
|
100
|
+
function actionOf(url: string): Action | null {
|
|
101
|
+
const last = new URL(url).pathname.split('/').at(-1);
|
|
102
|
+
return last === ProtocolPath.check || last === ProtocolPath.submit || last === ProtocolPath.hint
|
|
103
|
+
? last
|
|
104
|
+
: null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const usedOf = (record: AttemptRecord, id: string): number => {
|
|
108
|
+
const used = record.hintsUsed;
|
|
109
|
+
return used !== undefined && Object.hasOwn(used, id) ? (used[id] ?? 0) : 0;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/** Hints used, what grading takes off: valid counts of a record (the store's own data, checked by `isRecord`). */
|
|
113
|
+
const isHintsUsed = (value: unknown): boolean =>
|
|
114
|
+
typeof value === 'object' &&
|
|
115
|
+
value !== null &&
|
|
116
|
+
!Array.isArray(value) &&
|
|
117
|
+
Object.values(value).every((n) => Number.isInteger(n) && (n as number) >= 0);
|
|
118
|
+
|
|
119
|
+
/** A store record the handler can trust: it is the requested attempt, with a valid key, policy and counters. */
|
|
120
|
+
function isRecord(value: unknown, id: string): value is AttemptRecord {
|
|
121
|
+
if (typeof value !== 'object' || value === null) return false;
|
|
122
|
+
const r = value as Partial<AttemptRecord>;
|
|
123
|
+
return (
|
|
124
|
+
r.id === id &&
|
|
125
|
+
isAnswerKey(r.key) &&
|
|
126
|
+
isAttemptPolicy(r.policy) &&
|
|
127
|
+
Number.isInteger(r.checksUsed) &&
|
|
128
|
+
(r.checksUsed as number) >= 0 &&
|
|
129
|
+
typeof r.submitted === 'boolean' &&
|
|
130
|
+
(r.hintsUsed === undefined || isHintsUsed(r.hintsUsed))
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
class TooLarge extends Error {}
|
|
135
|
+
|
|
136
|
+
/** The body as text, read with a running size check: Content-Length can be missing or wrong. */
|
|
137
|
+
async function readBody(request: Request): Promise<string> {
|
|
138
|
+
const declared = Number(request.headers.get('content-length') ?? '');
|
|
139
|
+
if (declared > MAX_REQUEST_BYTES) throw new TooLarge();
|
|
140
|
+
if (request.body === null) return '';
|
|
141
|
+
const reader = request.body.getReader();
|
|
142
|
+
const chunks: Uint8Array[] = [];
|
|
143
|
+
let size = 0;
|
|
144
|
+
for (;;) {
|
|
145
|
+
const { done, value } = await reader.read();
|
|
146
|
+
if (done) break;
|
|
147
|
+
size += value.byteLength;
|
|
148
|
+
if (size > MAX_REQUEST_BYTES) {
|
|
149
|
+
await reader.cancel().catch(() => undefined);
|
|
150
|
+
throw new TooLarge();
|
|
151
|
+
}
|
|
152
|
+
chunks.push(value);
|
|
153
|
+
}
|
|
154
|
+
const bytes = new Uint8Array(size);
|
|
155
|
+
let offset = 0;
|
|
156
|
+
for (const chunk of chunks) {
|
|
157
|
+
bytes.set(chunk, offset);
|
|
158
|
+
offset += chunk.byteLength;
|
|
159
|
+
}
|
|
160
|
+
// Fatal: invalid UTF-8 is a malformed request, not text to guess at.
|
|
161
|
+
return new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type Parsed =
|
|
165
|
+
| { readonly action: typeof ProtocolPath.check; readonly request: CheckRequest }
|
|
166
|
+
| { readonly action: typeof ProtocolPath.submit; readonly answers: Answers }
|
|
167
|
+
| { readonly action: typeof ProtocolPath.hint; readonly request: HintRequest };
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* CheckRequest, SubmitRequest or HintRequest from untrusted JSON; null when it does not match the schema (a check
|
|
171
|
+
* naming both a page and an element included).
|
|
172
|
+
*/
|
|
173
|
+
function parseRequest(action: Action, value: unknown): Parsed | null {
|
|
174
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return null;
|
|
175
|
+
const { answers, page, element } = value as { answers?: unknown; page?: unknown; element?: unknown };
|
|
176
|
+
const optional = (v: unknown) => v === undefined || typeof v === 'string';
|
|
177
|
+
if (action === ProtocolPath.hint) {
|
|
178
|
+
return typeof element === 'string' ? { action, request: { element } } : null;
|
|
179
|
+
}
|
|
180
|
+
if (!isAnswers(answers)) return null;
|
|
181
|
+
if (action === ProtocolPath.submit) return { action, answers };
|
|
182
|
+
if (!optional(page) || !optional(element) || (page !== undefined && element !== undefined)) return null;
|
|
183
|
+
const request: CheckRequest = {
|
|
184
|
+
answers,
|
|
185
|
+
...(page === undefined ? {} : { page: page as string }),
|
|
186
|
+
...(element === undefined ? {} : { element: element as string }),
|
|
187
|
+
};
|
|
188
|
+
return { action, request };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The answers a submission stores: those within the length limit of their slot's kind (open answers 2000 code points,
|
|
193
|
+
* the others 500). Longer ones count as unanswered, so they are not kept either.
|
|
194
|
+
*/
|
|
195
|
+
function storable(key: AnswerKey, answers: Answers): Answers {
|
|
196
|
+
return Object.fromEntries(
|
|
197
|
+
Object.entries(answers).filter(([id, value]) =>
|
|
198
|
+
fitsAnswer(value, Object.hasOwn(key.slots, id) ? key.slots[id]?.kind : undefined),
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** The hints used of a record as grading options: nothing when there are none, so old records grade as before. */
|
|
204
|
+
function hintsOf(record: AttemptRecord): GradeOptions {
|
|
205
|
+
return record.hintsUsed === undefined ? {} : { hintsUsed: record.hintsUsed };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Same rule and rounding as the engine's `localChecker`. */
|
|
209
|
+
function passes(result: GradeResult, passMark: number): boolean {
|
|
210
|
+
return result.max === 0 || Math.round((result.score / result.max) * 1e6) / 1e6 >= passMark;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** From the deadline (plus `grace`) on. Fails closed: a deadline or clock that is not a number closes the attempt. */
|
|
214
|
+
function pastDeadline(policy: AttemptPolicy, now: number, grace = 0): boolean {
|
|
215
|
+
return policy.deadline !== null && !(now <= Date.parse(policy.deadline) + grace);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Creates the grading endpoint. It answers only protocol-1 requests and never sends key data: check responses carry
|
|
220
|
+
* the GradeResult, hints go out one at a time when the policy offers them, feedback and explanations when the policy
|
|
221
|
+
* shows them (`helpShown`), and solutions with a submit only when the attempt's policy reveals answers.
|
|
222
|
+
*/
|
|
223
|
+
export function createGradingHandler(options: GradingHandlerOptions): GradingHandler {
|
|
224
|
+
const { engine, attempts } = options;
|
|
225
|
+
const now = options.now ?? Date.now;
|
|
226
|
+
|
|
227
|
+
/** A store answer the handler can act on; anything inconsistent throws, which fails the request closed (500). */
|
|
228
|
+
function trusted(record: AttemptRecord | null, id: string, from: string): AttemptRecord | null {
|
|
229
|
+
if (record === null || isRecord(record, id)) return record;
|
|
230
|
+
throw new TypeError(`AttemptStore.${from} returned an invalid record for attempt ${id}`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async function check(id: string, request: CheckRequest): Promise<Response> {
|
|
234
|
+
const record = trusted(await attempts.get(id), id, 'get');
|
|
235
|
+
if (record === null) return problem(ProblemCode.notFound);
|
|
236
|
+
const { policy, key } = record;
|
|
237
|
+
if (policy.mode !== 'practice') return problem(ProblemCode.checkNotAllowed);
|
|
238
|
+
if (record.submitted || pastDeadline(policy, now())) return problem(ProblemCode.attemptClosed);
|
|
239
|
+
const { page, element } = request;
|
|
240
|
+
if (element !== undefined && helpPolicy(policy).checkScope !== 'interaction') {
|
|
241
|
+
return problem(ProblemCode.checkNotAllowed);
|
|
242
|
+
}
|
|
243
|
+
if (page !== undefined && !key.pages.includes(page)) return problem(ProblemCode.notFound);
|
|
244
|
+
if (element !== undefined && !isGradedElement(key, element)) return problem(ProblemCode.notFound);
|
|
245
|
+
const max = policy.maxChecks;
|
|
246
|
+
if (max !== null && record.checksUsed >= max) return problem(ProblemCode.checksExhausted);
|
|
247
|
+
// Count first, then decide on the counted record: a parallel check cannot slip past maxChecks.
|
|
248
|
+
const counted = trusted(await attempts.recordCheck(id), id, 'recordCheck');
|
|
249
|
+
if (counted === null) return problem(ProblemCode.notFound);
|
|
250
|
+
if (counted.submitted) return problem(ProblemCode.attemptClosed);
|
|
251
|
+
if (max !== null && counted.checksUsed > max) return problem(ProblemCode.checksExhausted);
|
|
252
|
+
const scope = page !== undefined ? { page } : element !== undefined ? { element } : {};
|
|
253
|
+
const result = engine.grade(key, request.answers, { ...scope, ...hintsOf(counted) });
|
|
254
|
+
return json({
|
|
255
|
+
result,
|
|
256
|
+
checksLeft: max === null ? null : Math.max(0, max - counted.checksUsed),
|
|
257
|
+
...resultHelp(key, result, helpShown(policy, 'check'), element),
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async function hint(id: string, request: HintRequest): Promise<Response> {
|
|
262
|
+
const record = trusted(await attempts.get(id), id, 'get');
|
|
263
|
+
if (record === null) return problem(ProblemCode.notFound);
|
|
264
|
+
const { policy, key } = record;
|
|
265
|
+
if (!helpPolicy(policy).hints || attempts.recordHint === undefined)
|
|
266
|
+
return problem(ProblemCode.hintsNotAllowed);
|
|
267
|
+
if (record.submitted || pastDeadline(policy, now())) return problem(ProblemCode.attemptClosed);
|
|
268
|
+
const { element } = request;
|
|
269
|
+
const total = hintCount(key, element);
|
|
270
|
+
if (total === 0) return problem(ProblemCode.notFound);
|
|
271
|
+
if (usedOf(record, element) >= total) return problem(ProblemCode.hintsExhausted);
|
|
272
|
+
// Count first, then serve the counted one: parallel requests get a hint each, never the same one twice.
|
|
273
|
+
const counted = trusted(await attempts.recordHint(id, element), id, 'recordHint');
|
|
274
|
+
if (counted === null) return problem(ProblemCode.notFound);
|
|
275
|
+
if (counted.submitted) return problem(ProblemCode.attemptClosed);
|
|
276
|
+
const next = nextHint(key, element, usedOf(counted, element) - 1);
|
|
277
|
+
return next === null ? problem(ProblemCode.hintsExhausted) : json(next);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async function submit(id: string, sent: Answers): Promise<Response> {
|
|
281
|
+
const record = trusted(await attempts.get(id), id, 'get');
|
|
282
|
+
if (record === null) return problem(ProblemCode.notFound);
|
|
283
|
+
const { policy, key } = record;
|
|
284
|
+
if (record.submitted || policy.mode === 'review' || pastDeadline(policy, now(), DEADLINE_GRACE_MS)) {
|
|
285
|
+
return problem(ProblemCode.attemptClosed);
|
|
286
|
+
}
|
|
287
|
+
const answers = storable(key, sent);
|
|
288
|
+
const result = engine.grade(key, answers, hintsOf(record));
|
|
289
|
+
const revealed = policy.reveal !== 'nothing';
|
|
290
|
+
const response: SubmitResponse = {
|
|
291
|
+
result: revealed ? result : null,
|
|
292
|
+
solution: policy.reveal === 'answers' ? engine.solution(key) : null,
|
|
293
|
+
passed: revealed && policy.passMark !== null ? passes(result, policy.passMark) : null,
|
|
294
|
+
submittedAt: new Date(now()).toISOString(),
|
|
295
|
+
...(revealed ? resultHelp(key, result, helpShown(policy, 'submit')) : {}),
|
|
296
|
+
};
|
|
297
|
+
// Only an explicit `true` closes the attempt for this request: anything else is treated as "already submitted".
|
|
298
|
+
if ((await attempts.recordSubmit(id, { answers, response })) !== true)
|
|
299
|
+
return problem(ProblemCode.attemptClosed);
|
|
300
|
+
return json(response);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async function handle(request: Request, context: GradingContext | undefined): Promise<Response> {
|
|
304
|
+
if (request.method !== 'POST') return problem(ProblemCode.methodNotAllowed, { allow: 'POST' });
|
|
305
|
+
const action = actionOf(request.url);
|
|
306
|
+
// JavaScript callers may leave the context out: no attempt, nothing to grade.
|
|
307
|
+
const id = context?.attemptId;
|
|
308
|
+
if (action === null || typeof id !== 'string' || !ATTEMPT_ID_PATTERN.test(id)) {
|
|
309
|
+
return problem(ProblemCode.notFound);
|
|
310
|
+
}
|
|
311
|
+
// JSON only: other media types are not protocol requests, and requiring this one makes cross-site form posts
|
|
312
|
+
// (which cannot set it without a CORS preflight) fail before they touch an attempt.
|
|
313
|
+
const type = (request.headers.get('content-type') ?? '').split(';')[0]?.trim().toLowerCase();
|
|
314
|
+
if (type !== JSON_MEDIA_TYPE) return problem(ProblemCode.invalidRequest);
|
|
315
|
+
let body: unknown;
|
|
316
|
+
try {
|
|
317
|
+
body = JSON.parse(await readBody(request));
|
|
318
|
+
} catch (error) {
|
|
319
|
+
return problem(error instanceof TooLarge ? ProblemCode.payloadTooLarge : ProblemCode.invalidRequest);
|
|
320
|
+
}
|
|
321
|
+
const parsed = parseRequest(action, body);
|
|
322
|
+
if (parsed === null) return problem(ProblemCode.invalidRequest);
|
|
323
|
+
if (parsed.action === ProtocolPath.check) return check(id, parsed.request);
|
|
324
|
+
return parsed.action === ProtocolPath.hint ? hint(id, parsed.request) : submit(id, parsed.answers);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return async (request, context) => {
|
|
328
|
+
try {
|
|
329
|
+
return await handle(request, context);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
try {
|
|
332
|
+
options.onError?.(error, request);
|
|
333
|
+
} catch {
|
|
334
|
+
// A failing logger must not change the answer.
|
|
335
|
+
}
|
|
336
|
+
// Storage or grading failed: say so without details (they may describe the key or the database).
|
|
337
|
+
return problem(ProblemCode.internal);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { GradingContext, GradingHandler, GradingHandlerOptions } from './handler.js';
|
|
2
|
+
export { ATTEMPT_ID_PATTERN, createGradingHandler, DEADLINE_GRACE_MS } from './handler.js';
|
|
3
|
+
export type { AttemptRecord, AttemptStore, MemoryAttemptStore, NewAttempt, Submission } from './store.js';
|
|
4
|
+
export { createMemoryAttemptStore } from './store.js';
|
package/src/store.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempt storage for the grading handler. The handler only needs three operations (four with hints), so any database
|
|
3
|
+
* can back it; `createMemoryAttemptStore` is the reference implementation for examples, tests and single-process
|
|
4
|
+
* servers.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
type AnswerKey,
|
|
8
|
+
type Answers,
|
|
9
|
+
type AttemptPolicy,
|
|
10
|
+
isAnswerKey,
|
|
11
|
+
isAttemptPolicy,
|
|
12
|
+
type SubmitResponse,
|
|
13
|
+
} from '@astl/core';
|
|
14
|
+
|
|
15
|
+
/** One attempt as the grading handler sees it. The key never leaves the server. */
|
|
16
|
+
export interface AttemptRecord {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly key: AnswerKey;
|
|
19
|
+
readonly policy: AttemptPolicy;
|
|
20
|
+
/** Checks counted so far. */
|
|
21
|
+
readonly checksUsed: number;
|
|
22
|
+
readonly submitted: boolean;
|
|
23
|
+
/** ISO 8601. */
|
|
24
|
+
readonly createdAt: string;
|
|
25
|
+
/**
|
|
26
|
+
* Hints given so far, by view element or page id (`recordHint`); absent means none. Grading takes their costs off,
|
|
27
|
+
* so a host that re-grades a submission later passes them on (`engine.grade(key, answers, { hintsUsed })`).
|
|
28
|
+
*/
|
|
29
|
+
readonly hintsUsed?: Readonly<Record<string, number>>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* What a submit stores: the final answers and the response the student got (re-grade the answers to release more
|
|
34
|
+
* later). The answers are those within their slot kind's length limit, open answers (for the teacher) included:
|
|
35
|
+
* `Metadata.open` lists their slot ids.
|
|
36
|
+
*/
|
|
37
|
+
export interface Submission {
|
|
38
|
+
readonly answers: Answers;
|
|
39
|
+
readonly response: SubmitResponse;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Storage the grading handler needs. The writes must be atomic per attempt (for example `UPDATE ... RETURNING` or a
|
|
44
|
+
* conditional update), so parallel requests can neither exceed `maxChecks`, get the same hint twice nor submit twice.
|
|
45
|
+
*/
|
|
46
|
+
export interface AttemptStore {
|
|
47
|
+
/** null for an unknown attempt. */
|
|
48
|
+
get(id: string): Promise<AttemptRecord | null>;
|
|
49
|
+
/** Counts one check; resolves to the record after counting, or null for an unknown attempt. */
|
|
50
|
+
recordCheck(id: string): Promise<AttemptRecord | null>;
|
|
51
|
+
/** Closes the attempt with its submission; resolves false when it was already submitted (or is unknown). */
|
|
52
|
+
recordSubmit(id: string, submission: Submission): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Counts one hint of `element` (a view element or page id) in `hintsUsed`; resolves to the record after counting, or
|
|
55
|
+
* null for an unknown attempt. Optional: a store without it offers no hints (`403 hints_not_allowed`).
|
|
56
|
+
*/
|
|
57
|
+
recordHint?(id: string, element: string): Promise<AttemptRecord | null>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface NewAttempt {
|
|
61
|
+
readonly key: AnswerKey;
|
|
62
|
+
readonly policy: AttemptPolicy;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface MemoryAttemptStore extends AttemptStore {
|
|
66
|
+
/** Starts an attempt with a random id. Throws a TypeError for an invalid key or policy. */
|
|
67
|
+
create(attempt: NewAttempt): Promise<AttemptRecord>;
|
|
68
|
+
/** The stored submission of an attempt, or null. */
|
|
69
|
+
submission(id: string): Promise<Submission | null>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface Entry {
|
|
73
|
+
record: AttemptRecord;
|
|
74
|
+
submission: Submission | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** An AttemptStore in a Map: fine for one process; attempts are lost on restart. */
|
|
78
|
+
export function createMemoryAttemptStore(): MemoryAttemptStore {
|
|
79
|
+
const entries = new Map<string, Entry>();
|
|
80
|
+
return {
|
|
81
|
+
async create({ key, policy }) {
|
|
82
|
+
if (!isAnswerKey(key)) throw new TypeError('createMemoryAttemptStore: invalid answer key');
|
|
83
|
+
if (!isAttemptPolicy(policy)) throw new TypeError('createMemoryAttemptStore: invalid attempt policy');
|
|
84
|
+
const record: AttemptRecord = {
|
|
85
|
+
id: crypto.randomUUID(),
|
|
86
|
+
key,
|
|
87
|
+
policy,
|
|
88
|
+
checksUsed: 0,
|
|
89
|
+
submitted: false,
|
|
90
|
+
createdAt: new Date().toISOString(),
|
|
91
|
+
hintsUsed: {},
|
|
92
|
+
};
|
|
93
|
+
entries.set(record.id, { record, submission: null });
|
|
94
|
+
return record;
|
|
95
|
+
},
|
|
96
|
+
async get(id) {
|
|
97
|
+
return entries.get(id)?.record ?? null;
|
|
98
|
+
},
|
|
99
|
+
async recordCheck(id) {
|
|
100
|
+
const entry = entries.get(id);
|
|
101
|
+
if (entry === undefined) return null;
|
|
102
|
+
entry.record = { ...entry.record, checksUsed: entry.record.checksUsed + 1 };
|
|
103
|
+
return entry.record;
|
|
104
|
+
},
|
|
105
|
+
async recordHint(id, element) {
|
|
106
|
+
const entry = entries.get(id);
|
|
107
|
+
if (entry === undefined) return null;
|
|
108
|
+
const used = entry.record.hintsUsed ?? {};
|
|
109
|
+
const count = Object.hasOwn(used, element) ? (used[element] ?? 0) : 0;
|
|
110
|
+
entry.record = { ...entry.record, hintsUsed: { ...used, [element]: count + 1 } };
|
|
111
|
+
return entry.record;
|
|
112
|
+
},
|
|
113
|
+
async recordSubmit(id, submission) {
|
|
114
|
+
const entry = entries.get(id);
|
|
115
|
+
if (entry === undefined || entry.record.submitted) return false;
|
|
116
|
+
entry.record = { ...entry.record, submitted: true };
|
|
117
|
+
entry.submission = submission;
|
|
118
|
+
return true;
|
|
119
|
+
},
|
|
120
|
+
async submission(id) {
|
|
121
|
+
return entries.get(id)?.submission ?? null;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|