paperlint 18.4.1 → 18.6.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/CLAUDE.md +6 -2
- package/dist/adapters/hotcrp/portal.io.d.ts.map +1 -1
- package/dist/adapters/hotcrp/portal.io.js +1 -0
- package/dist/adapters/hotcrp/portal.io.js.map +1 -1
- package/dist/adapters/hotcrp/response.d.ts.map +1 -1
- package/dist/adapters/hotcrp/response.js +15 -0
- package/dist/adapters/hotcrp/response.js.map +1 -1
- package/dist/adapters/latex/abstract.d.ts +11 -0
- package/dist/adapters/latex/abstract.d.ts.map +1 -1
- package/dist/adapters/latex/abstract.js +1 -1
- package/dist/adapters/latex/abstract.js.map +1 -1
- package/dist/adapters/latex/index.d.ts.map +1 -1
- package/dist/adapters/latex/index.js +3 -0
- package/dist/adapters/latex/index.js.map +1 -1
- package/dist/adapters/latex/macros.d.ts +6 -0
- package/dist/adapters/latex/macros.d.ts.map +1 -0
- package/dist/adapters/latex/macros.js +27 -0
- package/dist/adapters/latex/macros.js.map +1 -0
- package/dist/adapters/osf/host.io.d.ts +12 -0
- package/dist/adapters/osf/host.io.d.ts.map +1 -0
- package/dist/adapters/osf/host.io.js +119 -0
- package/dist/adapters/osf/host.io.js.map +1 -0
- package/dist/adapters/osf/index.d.ts +10 -0
- package/dist/adapters/osf/index.d.ts.map +1 -0
- package/dist/adapters/osf/index.js +10 -0
- package/dist/adapters/osf/index.js.map +1 -0
- package/dist/adapters/osf/response.d.ts +11 -0
- package/dist/adapters/osf/response.d.ts.map +1 -0
- package/dist/adapters/osf/response.js +87 -0
- package/dist/adapters/osf/response.js.map +1 -0
- package/dist/artifact-upload-rule.d.ts +66 -0
- package/dist/artifact-upload-rule.d.ts.map +1 -0
- package/dist/artifact-upload-rule.js +151 -0
- package/dist/artifact-upload-rule.js.map +1 -0
- package/dist/artifact.d.ts +35 -0
- package/dist/artifact.d.ts.map +1 -0
- package/dist/artifact.js +251 -0
- package/dist/artifact.js.map +1 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +99 -7
- package/dist/cli.js.map +1 -1
- package/dist/domain/artifact-upload.d.ts +44 -0
- package/dist/domain/artifact-upload.d.ts.map +1 -0
- package/dist/domain/artifact-upload.js +80 -0
- package/dist/domain/artifact-upload.js.map +1 -0
- package/dist/domain/submission.d.ts +4 -0
- package/dist/domain/submission.d.ts.map +1 -1
- package/dist/domain/submission.js.map +1 -1
- package/dist/eslint-rules/CLAUDE.md.spec.d.ts.map +1 -1
- package/dist/eslint-rules/CLAUDE.md.spec.js +1 -0
- package/dist/eslint-rules/CLAUDE.md.spec.js.map +1 -1
- package/dist/ports/artifact-host.d.ts +54 -0
- package/dist/ports/artifact-host.d.ts.map +1 -0
- package/dist/ports/artifact-host.js +2 -0
- package/dist/ports/artifact-host.js.map +1 -0
- package/dist/ports/latex.d.ts +9 -0
- package/dist/ports/latex.d.ts.map +1 -1
- package/dist/submission-confirm-hook.d.ts +11 -1
- package/dist/submission-confirm-hook.d.ts.map +1 -1
- package/dist/submission-confirm-hook.js +83 -21
- package/dist/submission-confirm-hook.js.map +1 -1
- package/dist/submission.d.ts +19 -2
- package/dist/submission.d.ts.map +1 -1
- package/dist/submission.js +121 -17
- package/dist/submission.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/docs/artifact-upload.md +124 -0
- package/docs/optional-rules.md +1 -0
- package/docs/osf-artifact.md +4 -1
- package/docs/rules/artifact/upload-current.md +85 -0
- package/docs/rules.md +1 -0
- package/docs/submission.md +19 -4
- package/eslint-rules/CLAUDE.md +5 -1
- package/eslint-rules/CLAUDE.md.spec.ts +3 -0
- package/package.json +1 -1
- package/src/adapters/hotcrp/portal.io.ts +1 -0
- package/src/adapters/hotcrp/portal.test.ts +43 -2
- package/src/adapters/hotcrp/response.ts +21 -0
- package/src/adapters/latex/abstract.ts +3 -1
- package/src/adapters/latex/index.ts +3 -0
- package/src/adapters/latex/macros.test.ts +31 -0
- package/src/adapters/latex/macros.ts +43 -0
- package/src/adapters/osf/cassette/README.md +20 -0
- package/src/adapters/osf/cassette/get-file.json +108 -0
- package/src/adapters/osf/cassette/get-listing.json +111 -0
- package/src/adapters/osf/cassette/get-missing.json +1 -0
- package/src/adapters/osf/cassette/put-new.json +31 -0
- package/src/adapters/osf/host.io.ts +156 -0
- package/src/adapters/osf/host.test.ts +274 -0
- package/src/adapters/osf/index.ts +14 -0
- package/src/adapters/osf/response.ts +105 -0
- package/src/artifact-upload-rule.test.ts +213 -0
- package/src/artifact-upload-rule.ts +213 -0
- package/src/artifact.test.ts +504 -0
- package/src/artifact.ts +402 -0
- package/src/cli.test.ts +149 -1
- package/src/cli.ts +121 -8
- package/src/domain/artifact-upload.test.ts +80 -0
- package/src/domain/artifact-upload.ts +111 -0
- package/src/domain/submission.ts +4 -0
- package/src/ports/artifact-host.ts +65 -0
- package/src/ports/latex.ts +10 -0
- package/src/rule-cases.test.ts +44 -0
- package/src/submission-confirm-hook.test.ts +113 -1
- package/src/submission-confirm-hook.ts +120 -27
- package/src/submission-fields-run.test.ts +1 -0
- package/src/submission-prior-reviews.test.ts +9 -1
- package/src/submission-show.test.ts +81 -0
- package/src/submission-update.test.ts +164 -6
- package/src/submission.ts +208 -24
- package/src/types.ts +6 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"data": [
|
|
3
|
+
{
|
|
4
|
+
"id": "0123456789abcdef01234567",
|
|
5
|
+
"type": "files",
|
|
6
|
+
"attributes": {
|
|
7
|
+
"guid": null,
|
|
8
|
+
"checkout": null,
|
|
9
|
+
"name": "artifact.zip",
|
|
10
|
+
"kind": "file",
|
|
11
|
+
"path": "/0123456789abcdef01234567",
|
|
12
|
+
"size": 15,
|
|
13
|
+
"provider": "osfstorage",
|
|
14
|
+
"materialized_path": "/artifact.zip",
|
|
15
|
+
"last_touched": null,
|
|
16
|
+
"date_modified": "2026-11-01T10:00:00.000000",
|
|
17
|
+
"date_created": "2026-11-01T10:00:00.000000Z",
|
|
18
|
+
"extra": {
|
|
19
|
+
"hashes": {
|
|
20
|
+
"md5": "3f395fbbb68716f38f5ff7fa9d740e3b",
|
|
21
|
+
"sha256": "59bd16dfc39e768f82bb8ec74467e571e80ed284683b586f077dbf1aa2483ecd"
|
|
22
|
+
},
|
|
23
|
+
"downloads": 0
|
|
24
|
+
},
|
|
25
|
+
"tags": [],
|
|
26
|
+
"current_user_can_comment": false,
|
|
27
|
+
"current_version": 1,
|
|
28
|
+
"show_as_unviewed": false
|
|
29
|
+
},
|
|
30
|
+
"relationships": {
|
|
31
|
+
"parent_folder": {
|
|
32
|
+
"links": {
|
|
33
|
+
"related": {
|
|
34
|
+
"href": "https://api.osf.io/v2/files/0123456789abcdef00000000/",
|
|
35
|
+
"meta": {}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"data": {
|
|
39
|
+
"id": "0123456789abcdef00000000",
|
|
40
|
+
"type": "files"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"versions": {
|
|
44
|
+
"links": {
|
|
45
|
+
"related": {
|
|
46
|
+
"href": "https://api.osf.io/v2/files/0123456789abcdef01234567/versions/",
|
|
47
|
+
"meta": {}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"node": {
|
|
52
|
+
"links": {
|
|
53
|
+
"related": {
|
|
54
|
+
"href": "https://api.osf.io/v2/nodes/abcde/",
|
|
55
|
+
"meta": {}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"data": {
|
|
59
|
+
"id": "abcde",
|
|
60
|
+
"type": "nodes"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"target": {
|
|
64
|
+
"links": {
|
|
65
|
+
"related": {
|
|
66
|
+
"href": "https://api.osf.io/v2/nodes/abcde/",
|
|
67
|
+
"meta": {
|
|
68
|
+
"type": "nodes"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"data": {
|
|
73
|
+
"type": "nodes",
|
|
74
|
+
"id": "abcde"
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
"cedar_metadata_records": {
|
|
78
|
+
"links": {
|
|
79
|
+
"related": {
|
|
80
|
+
"href": "https://api.osf.io/v2/files/0123456789abcdef01234567/cedar_metadata_records/",
|
|
81
|
+
"meta": {}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"links": {
|
|
87
|
+
"info": "https://api.osf.io/v2/files/0123456789abcdef01234567/",
|
|
88
|
+
"move": "https://files.osf.io/v1/resources/abcde/providers/osfstorage/0123456789abcdef01234567",
|
|
89
|
+
"upload": "https://files.osf.io/v1/resources/abcde/providers/osfstorage/0123456789abcdef01234567",
|
|
90
|
+
"delete": "https://files.osf.io/v1/resources/abcde/providers/osfstorage/0123456789abcdef01234567",
|
|
91
|
+
"download": "https://osf.io/download/0123456789abcdef01234567/",
|
|
92
|
+
"render": "https://mfr.osf.io/render?url=https%3A%2F%2Fosf.io%2Fdownload%2F0123456789abcdef01234567%2F%3Fdirect%26mode%3Drender",
|
|
93
|
+
"html": "https://osf.io/abcde/files/osfstorage/0123456789abcdef01234567",
|
|
94
|
+
"self": "https://api.osf.io/v2/files/0123456789abcdef01234567/"
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
],
|
|
98
|
+
"links": {
|
|
99
|
+
"first": null,
|
|
100
|
+
"last": null,
|
|
101
|
+
"prev": null,
|
|
102
|
+
"next": null,
|
|
103
|
+
"meta": {
|
|
104
|
+
"total": 1,
|
|
105
|
+
"per_page": 10
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
"meta": {
|
|
109
|
+
"version": "2.0"
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "errors": [{ "detail": "Not found." }], "meta": { "version": "2.0" } }
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"data": {
|
|
3
|
+
"id": "osfstorage/0123456789abcdef01234567",
|
|
4
|
+
"type": "files",
|
|
5
|
+
"attributes": {
|
|
6
|
+
"extra": {
|
|
7
|
+
"guid": null,
|
|
8
|
+
"version": 1,
|
|
9
|
+
"downloads": 0,
|
|
10
|
+
"checkout": null,
|
|
11
|
+
"latestVersionSeen": null,
|
|
12
|
+
"hashes": {
|
|
13
|
+
"md5": "3f395fbbb68716f38f5ff7fa9d740e3b",
|
|
14
|
+
"sha256": "59bd16dfc39e768f82bb8ec74467e571e80ed284683b586f077dbf1aa2483ecd"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"kind": "file",
|
|
18
|
+
"name": "artifact.zip",
|
|
19
|
+
"path": "/0123456789abcdef01234567",
|
|
20
|
+
"provider": "osfstorage",
|
|
21
|
+
"materialized": "/artifact.zip",
|
|
22
|
+
"size": 15
|
|
23
|
+
},
|
|
24
|
+
"links": {
|
|
25
|
+
"move": "https://files.osf.io/v1/resources/abcde/providers/osfstorage/0123456789abcdef01234567",
|
|
26
|
+
"upload": "https://files.osf.io/v1/resources/abcde/providers/osfstorage/0123456789abcdef01234567?kind=file",
|
|
27
|
+
"delete": "https://files.osf.io/v1/resources/abcde/providers/osfstorage/0123456789abcdef01234567",
|
|
28
|
+
"download": "https://files.osf.io/v1/resources/abcde/providers/osfstorage/0123456789abcdef01234567"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OSF as the `ArtifactHost` port, over Node's `fetch`.
|
|
3
|
+
*
|
|
4
|
+
* Read: `GET <api>/files/<id>/` when the record has the file's id; else the project's listing
|
|
5
|
+
* filtered by name, `GET <api>/nodes/<guid>/files/osfstorage/?filter[name]=<file>` (the project's
|
|
6
|
+
* guid is the first segment of the record's `osf.io` url). Both answer `extra.hashes` with `md5` and
|
|
7
|
+
* `sha256` (recorded 2026-11-21, `cassette/`).
|
|
8
|
+
*
|
|
9
|
+
* Upload, through WaterButler (waterbutler.readthedocs.io, "Upload a file"): a new version of a file
|
|
10
|
+
* of the same name at the project's top level, `PUT <files>/resources/<guid>/providers/osfstorage/<id>?kind=file`;
|
|
11
|
+
* else a new file, `PUT <files>/resources/<guid>/providers/osfstorage/?kind=file&name=<name>`. The
|
|
12
|
+
* body is the bytes, not a form. Then the hashes are read back from `GET <api>/files/<id>/`, so the
|
|
13
|
+
* record holds what OSF says it holds, not what was sent.
|
|
14
|
+
*
|
|
15
|
+
* 🔴 THE TOKEN goes in the `Authorization` header and nowhere else: not into a URL, a message, or an
|
|
16
|
+
* error.
|
|
17
|
+
*/
|
|
18
|
+
import { err, ok, type Result } from "../../domain/result.ts";
|
|
19
|
+
import { messageOf } from "../../domain/text.ts";
|
|
20
|
+
import type {
|
|
21
|
+
ArtifactHost,
|
|
22
|
+
HostedFile,
|
|
23
|
+
HostFailure,
|
|
24
|
+
Upload,
|
|
25
|
+
Where,
|
|
26
|
+
} from "../../ports/artifact-host.ts";
|
|
27
|
+
import { parseFile, parseListing, parseUploaded } from "./response.ts";
|
|
28
|
+
|
|
29
|
+
/** Where OSF answers: its API and WaterButler, no trailing slash. A test points them at itself. */
|
|
30
|
+
export interface OsfUrls {
|
|
31
|
+
readonly api: string;
|
|
32
|
+
readonly files: string;
|
|
33
|
+
/** The site whose urls name a project: `https://osf.io/<guid>/…`. */
|
|
34
|
+
readonly site: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const OSF: OsfUrls = {
|
|
38
|
+
api: "https://api.osf.io/v2",
|
|
39
|
+
files: "https://files.osf.io/v1",
|
|
40
|
+
site: "https://osf.io",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const READ_TIMEOUT_MS = 60_000;
|
|
44
|
+
const WRITE_TIMEOUT_MS = 600_000;
|
|
45
|
+
|
|
46
|
+
type Answer = Result<
|
|
47
|
+
{ readonly status: number; readonly body: unknown },
|
|
48
|
+
HostFailure
|
|
49
|
+
>;
|
|
50
|
+
|
|
51
|
+
/** One request → HTTP status and parsed JSON body, or why there is neither. */
|
|
52
|
+
async function call(
|
|
53
|
+
url: string,
|
|
54
|
+
token: string | null,
|
|
55
|
+
init: { readonly method: string; readonly body?: Uint8Array },
|
|
56
|
+
): Promise<Answer> {
|
|
57
|
+
const auth = token === null ? {} : { Authorization: `Bearer ${token}` };
|
|
58
|
+
try {
|
|
59
|
+
const r = await fetch(url, {
|
|
60
|
+
method: init.method,
|
|
61
|
+
...(init.body === undefined ? {} : { body: new Uint8Array(init.body) }),
|
|
62
|
+
headers: { ...auth, Accept: "application/json" },
|
|
63
|
+
signal: AbortSignal.timeout(
|
|
64
|
+
init.method === "GET" ? READ_TIMEOUT_MS : WRITE_TIMEOUT_MS,
|
|
65
|
+
),
|
|
66
|
+
});
|
|
67
|
+
const text = await r.text();
|
|
68
|
+
try {
|
|
69
|
+
return { ok: true, value: { status: r.status, body: JSON.parse(text) } };
|
|
70
|
+
} catch {
|
|
71
|
+
return err({
|
|
72
|
+
kind: "malformed",
|
|
73
|
+
detail: `HTTP ${String(r.status)}: the answer is not JSON`,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
} catch (e) {
|
|
77
|
+
return err({ kind: "unreachable", detail: messageOf(e) });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The project's guid in an `osf.io` url: its first path segment, five letters and digits. */
|
|
82
|
+
const guidOf = (site: string, url: string): string | null =>
|
|
83
|
+
URL.canParse(url) && new URL(url).host === new URL(site).host
|
|
84
|
+
? (/^\/([a-z0-9]{5})(?:\/|$)/u.exec(new URL(url).pathname)?.[1] ?? null)
|
|
85
|
+
: null;
|
|
86
|
+
|
|
87
|
+
type Found = Promise<Result<HostedFile, HostFailure>>;
|
|
88
|
+
|
|
89
|
+
/** The file OSF holds under `id`. */
|
|
90
|
+
async function fileById(u: OsfUrls, id: string, token: string | null): Found {
|
|
91
|
+
const a = await call(`${u.api}/files/${encodeURIComponent(id)}/`, token, {
|
|
92
|
+
method: "GET",
|
|
93
|
+
});
|
|
94
|
+
return a.ok ? parseFile(a.value.status, a.value.body) : a;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The file called `name` at the top level of the project `guid`, or null when there is none. */
|
|
98
|
+
async function byName(
|
|
99
|
+
u: OsfUrls,
|
|
100
|
+
guid: string,
|
|
101
|
+
name: string,
|
|
102
|
+
token: string | null,
|
|
103
|
+
): Promise<Result<HostedFile | null, HostFailure>> {
|
|
104
|
+
const q = new URLSearchParams({ "filter[name]": name });
|
|
105
|
+
const url = `${u.api}/nodes/${guid}/files/osfstorage/?${q.toString()}`;
|
|
106
|
+
const a = await call(url, token, { method: "GET" });
|
|
107
|
+
return a.ok ? parseListing(a.value.status, a.value.body, name) : a;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The file a record names: by its id, else by its name in the project its url names. */
|
|
111
|
+
async function read(u: OsfUrls, where: Where, token: string | null): Found {
|
|
112
|
+
if (where.id !== null) return fileById(u, where.id, token);
|
|
113
|
+
const guid = guidOf(u.site, where.url);
|
|
114
|
+
if (guid === null)
|
|
115
|
+
return err({
|
|
116
|
+
kind: "not-found",
|
|
117
|
+
detail: `the record has no file id and its url names no ${u.site} project`,
|
|
118
|
+
});
|
|
119
|
+
const f = await byName(u, guid, where.file, token);
|
|
120
|
+
if (!f.ok) return f;
|
|
121
|
+
return f.value === null
|
|
122
|
+
? err({
|
|
123
|
+
kind: "not-found",
|
|
124
|
+
detail: `the project ${guid} holds no file ${where.file} at its top level`,
|
|
125
|
+
})
|
|
126
|
+
: ok(f.value);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Put the file: a new version of the one of the same name, else a new file; then read it back. */
|
|
130
|
+
async function upload(u: OsfUrls, up: Upload, token: string): Found {
|
|
131
|
+
const existing = await byName(u, up.target, up.name, token);
|
|
132
|
+
if (!existing.ok) return existing;
|
|
133
|
+
const base = `${u.files}/resources/${up.target}/providers/osfstorage/`;
|
|
134
|
+
const query = new URLSearchParams({ kind: "file", name: up.name });
|
|
135
|
+
const url =
|
|
136
|
+
existing.value === null
|
|
137
|
+
? `${base}?${query.toString()}`
|
|
138
|
+
: `${base}${existing.value.id}?kind=file`;
|
|
139
|
+
const put = await call(url, token, { method: "PUT", body: up.bytes });
|
|
140
|
+
if (!put.ok) return put;
|
|
141
|
+
const id = parseUploaded(put.value.status, put.value.body);
|
|
142
|
+
return id.ok ? fileById(u, id.value, token) : id;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** OSF at `u`: the project's files read, and a file put there. */
|
|
146
|
+
export const osfHost = (u: OsfUrls = OSF): ArtifactHost => ({
|
|
147
|
+
can: { upload: true, hashes: ["sha256", "md5"] },
|
|
148
|
+
token: {
|
|
149
|
+
variable: "OSF_TOKEN_OSF_IO",
|
|
150
|
+
siteLess: "OSF_TOKEN",
|
|
151
|
+
help: `${u.site}/settings/tokens (Settings → Personal access tokens), scope osf.full_write to upload; osf.full_read is enough for show on a project that is not public`,
|
|
152
|
+
},
|
|
153
|
+
targetOf: (url) => guidOf(u.site, url),
|
|
154
|
+
read: (where, token) => read(u, where, token),
|
|
155
|
+
upload: (up, token) => upload(u, up, token),
|
|
156
|
+
});
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OSF adapter against a local HTTP server on 127.0.0.1 — no real OSF, no network. The server
|
|
3
|
+
* answers with the recorded answers in `cassette/` and records each request whole (method, path,
|
|
4
|
+
* query, the Authorization header, the body), so the tests assert what OSF would receive.
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
|
+
import { createServer, type IncomingMessage } from "node:http";
|
|
8
|
+
import type { AddressInfo } from "node:net";
|
|
9
|
+
import { afterAll, beforeAll, beforeEach, expect, test } from "vitest";
|
|
10
|
+
import type { ArtifactHost } from "../../ports/artifact-host.ts";
|
|
11
|
+
import { osfHost, parseFile, parseListing, parseUploaded } from "./index.ts";
|
|
12
|
+
|
|
13
|
+
const cassette = (name: string): string =>
|
|
14
|
+
readFileSync(new URL(`cassette/${name}`, import.meta.url), "utf8");
|
|
15
|
+
|
|
16
|
+
interface Seen {
|
|
17
|
+
readonly request: string;
|
|
18
|
+
readonly auth: string | null;
|
|
19
|
+
readonly body: string;
|
|
20
|
+
}
|
|
21
|
+
interface Reply {
|
|
22
|
+
readonly status: number;
|
|
23
|
+
readonly body: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const seen: Seen[] = [];
|
|
27
|
+
let routes: Readonly<Record<string, Reply>> = {};
|
|
28
|
+
const server = createServer((req: IncomingMessage, res) => {
|
|
29
|
+
const chunks: Buffer[] = [];
|
|
30
|
+
req.on("data", (c: Buffer) => chunks.push(c));
|
|
31
|
+
req.on("end", () => {
|
|
32
|
+
const request = `${req.method ?? ""} ${req.url ?? ""}`;
|
|
33
|
+
seen.push({
|
|
34
|
+
request,
|
|
35
|
+
auth: req.headers.authorization ?? null,
|
|
36
|
+
body: Buffer.concat(chunks).toString(),
|
|
37
|
+
});
|
|
38
|
+
const r = routes[request] ?? { status: 599, body: "{}" };
|
|
39
|
+
res.writeHead(r.status, { "content-type": "application/json" });
|
|
40
|
+
res.end(r.body);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
let host: ArtifactHost;
|
|
45
|
+
let base = "";
|
|
46
|
+
beforeAll(async () => {
|
|
47
|
+
await new Promise<void>((done) => server.listen(0, "127.0.0.1", done));
|
|
48
|
+
const a: AddressInfo | string | null = server.address();
|
|
49
|
+
base = `http://127.0.0.1:${typeof a === "object" && a !== null ? String(a.port) : ""}`;
|
|
50
|
+
host = osfHost({
|
|
51
|
+
api: `${base}/v2`,
|
|
52
|
+
files: `${base}/v1`,
|
|
53
|
+
site: "https://osf.io",
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
afterAll(
|
|
57
|
+
() =>
|
|
58
|
+
new Promise<void>((done) => {
|
|
59
|
+
server.close(() => {
|
|
60
|
+
done();
|
|
61
|
+
});
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
seen.length = 0;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const ID = "0123456789abcdef01234567";
|
|
69
|
+
const FILE = `/v2/files/${ID}/`;
|
|
70
|
+
const LISTING =
|
|
71
|
+
"/v2/nodes/abcde/files/osfstorage/?filter%5Bname%5D=artifact.zip";
|
|
72
|
+
const HOSTED = {
|
|
73
|
+
id: ID,
|
|
74
|
+
url: `https://osf.io/abcde/files/osfstorage/${ID}`,
|
|
75
|
+
sha256: "59bd16dfc39e768f82bb8ec74467e571e80ed284683b586f077dbf1aa2483ecd",
|
|
76
|
+
md5: "3f395fbbb68716f38f5ff7fa9d740e3b",
|
|
77
|
+
};
|
|
78
|
+
const EMPTY = JSON.stringify({ data: [] });
|
|
79
|
+
const ok200 = (name: string): Reply => ({ status: 200, body: cassette(name) });
|
|
80
|
+
|
|
81
|
+
test("read: by the record's file id, without a token on a public file", async () => {
|
|
82
|
+
routes = { [`GET ${FILE}`]: ok200("get-file.json") };
|
|
83
|
+
const r = await host.read(
|
|
84
|
+
{ url: "https://osf.io/abcde/", file: "x", id: ID },
|
|
85
|
+
null,
|
|
86
|
+
);
|
|
87
|
+
// Guards: the file's hashes as OSF holds them, and no Authorization header without a token.
|
|
88
|
+
expect(r).toEqual({ ok: true, value: HOSTED });
|
|
89
|
+
expect(seen).toEqual([{ request: `GET ${FILE}`, auth: null, body: "" }]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("read: by name in the project the url names, with the token in its header only", async () => {
|
|
93
|
+
routes = { [`GET ${LISTING}`]: ok200("get-listing.json") };
|
|
94
|
+
const where = {
|
|
95
|
+
url: "https://osf.io/abcde/?view_only=0f",
|
|
96
|
+
file: "artifact.zip",
|
|
97
|
+
id: null,
|
|
98
|
+
};
|
|
99
|
+
// Guards: the listing filtered by the record's file name; the token travels as a bearer header.
|
|
100
|
+
expect(await host.read(where, "tok")).toEqual({ ok: true, value: HOSTED });
|
|
101
|
+
expect(seen).toEqual([
|
|
102
|
+
{ request: `GET ${LISTING}`, auth: "Bearer tok", body: "" },
|
|
103
|
+
]);
|
|
104
|
+
// Guards: a project without that file is not found, naming both.
|
|
105
|
+
routes = { [`GET ${LISTING}`]: { status: 200, body: EMPTY } };
|
|
106
|
+
expect(await host.read(where, null)).toEqual({
|
|
107
|
+
ok: false,
|
|
108
|
+
error: {
|
|
109
|
+
kind: "not-found",
|
|
110
|
+
detail: "the project abcde holds no file artifact.zip at its top level",
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("read: what OSF or the record cannot answer is a failure that says which", async () => {
|
|
116
|
+
routes = {
|
|
117
|
+
[`GET ${FILE}`]: { status: 404, body: cassette("get-missing.json") },
|
|
118
|
+
};
|
|
119
|
+
const where = { url: "https://example.org/a", file: "artifact.zip", id: ID };
|
|
120
|
+
// Guards: 404 → not found.
|
|
121
|
+
expect(await host.read(where, null)).toEqual({
|
|
122
|
+
ok: false,
|
|
123
|
+
error: {
|
|
124
|
+
kind: "not-found",
|
|
125
|
+
detail: "HTTP 404: OSF has no such file or project",
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
// Guards: a url that is not OSF's, and no id → not found, before any request.
|
|
129
|
+
expect(await host.read({ ...where, id: null }, null)).toEqual({
|
|
130
|
+
ok: false,
|
|
131
|
+
error: {
|
|
132
|
+
kind: "not-found",
|
|
133
|
+
detail:
|
|
134
|
+
"the record has no file id and its url names no https://osf.io project",
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
routes = { [`GET ${FILE}`]: { status: 401, body: "{}" } };
|
|
138
|
+
// Guards: 401 → refused, with no token in the words.
|
|
139
|
+
expect(await host.read(where, "secret-token")).toEqual({
|
|
140
|
+
ok: false,
|
|
141
|
+
error: {
|
|
142
|
+
kind: "refused",
|
|
143
|
+
detail:
|
|
144
|
+
"HTTP 401: OSF refused the token, or the file is not public and no token was given",
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
routes = { [`GET ${FILE}`]: { status: 500, body: "<html>" } };
|
|
148
|
+
// Guards: an answer that is not JSON is malformed.
|
|
149
|
+
expect(await host.read(where, null)).toEqual({
|
|
150
|
+
ok: false,
|
|
151
|
+
error: { kind: "malformed", detail: "HTTP 500: the answer is not JSON" },
|
|
152
|
+
});
|
|
153
|
+
const gone = osfHost({
|
|
154
|
+
api: "http://127.0.0.1:1/v2",
|
|
155
|
+
files: "",
|
|
156
|
+
site: "https://osf.io",
|
|
157
|
+
});
|
|
158
|
+
const r = await gone.read(where, null);
|
|
159
|
+
// Guards: a host that does not answer is unreachable — for a read by id, by name, and an upload.
|
|
160
|
+
expect(r.ok ? null : r.error.kind).toBe("unreachable");
|
|
161
|
+
const byName = await gone.read(
|
|
162
|
+
{ ...where, url: "https://osf.io/abcde/", id: null },
|
|
163
|
+
null,
|
|
164
|
+
);
|
|
165
|
+
expect(byName.ok ? null : byName.error.kind).toBe("unreachable");
|
|
166
|
+
const up = await gone.upload(
|
|
167
|
+
{ target: "abcde", name: "a", bytes: new Uint8Array() },
|
|
168
|
+
"t",
|
|
169
|
+
);
|
|
170
|
+
expect(up.ok ? null : up.error.kind).toBe("unreachable");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("upload: a new file, then its hashes read back from OSF", async () => {
|
|
174
|
+
const put =
|
|
175
|
+
"/v1/resources/abcde/providers/osfstorage/?kind=file&name=artifact.zip";
|
|
176
|
+
routes = {
|
|
177
|
+
[`GET ${LISTING}`]: { status: 200, body: EMPTY },
|
|
178
|
+
[`PUT ${put}`]: { status: 201, body: cassette("put-new.json") },
|
|
179
|
+
[`GET ${FILE}`]: ok200("get-file.json"),
|
|
180
|
+
};
|
|
181
|
+
const bytes = new TextEncoder().encode("artifact bytes\n");
|
|
182
|
+
const r = await host.upload(
|
|
183
|
+
{ target: "abcde", name: "artifact.zip", bytes },
|
|
184
|
+
"tok",
|
|
185
|
+
);
|
|
186
|
+
// Guards: look for the name, PUT the bytes as a new file, read the hashes back — each with the token.
|
|
187
|
+
expect(r).toEqual({ ok: true, value: HOSTED });
|
|
188
|
+
expect(seen).toEqual([
|
|
189
|
+
{ request: `GET ${LISTING}`, auth: "Bearer tok", body: "" },
|
|
190
|
+
{ request: `PUT ${put}`, auth: "Bearer tok", body: "artifact bytes\n" },
|
|
191
|
+
{ request: `GET ${FILE}`, auth: "Bearer tok", body: "" },
|
|
192
|
+
]);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("upload: a file of the same name gets a new version; a failing step stops it", async () => {
|
|
196
|
+
const put = `/v1/resources/abcde/providers/osfstorage/${ID}?kind=file`;
|
|
197
|
+
routes = {
|
|
198
|
+
[`GET ${LISTING}`]: ok200("get-listing.json"),
|
|
199
|
+
[`PUT ${put}`]: ok200("put-new.json"),
|
|
200
|
+
[`GET ${FILE}`]: ok200("get-file.json"),
|
|
201
|
+
};
|
|
202
|
+
const u = {
|
|
203
|
+
target: "abcde",
|
|
204
|
+
name: "artifact.zip",
|
|
205
|
+
bytes: new Uint8Array([1]),
|
|
206
|
+
};
|
|
207
|
+
// Guards: the version goes to the existing file's id.
|
|
208
|
+
expect(await host.upload(u, "tok")).toEqual({ ok: true, value: HOSTED });
|
|
209
|
+
expect(seen.map((s) => s.request)).toEqual([
|
|
210
|
+
`GET ${LISTING}`,
|
|
211
|
+
`PUT ${put}`,
|
|
212
|
+
`GET ${FILE}`,
|
|
213
|
+
]);
|
|
214
|
+
routes = { [`GET ${LISTING}`]: { status: 403, body: "{}" } };
|
|
215
|
+
// Guards: a refused listing uploads nothing.
|
|
216
|
+
expect((await host.upload(u, "tok")).ok).toBe(false);
|
|
217
|
+
routes = {
|
|
218
|
+
[`GET ${LISTING}`]: ok200("get-listing.json"),
|
|
219
|
+
[`PUT ${put}`]: { status: 200, body: "{}" },
|
|
220
|
+
};
|
|
221
|
+
// Guards: an upload answer without the file's path is malformed.
|
|
222
|
+
expect(await host.upload(u, "tok")).toEqual({
|
|
223
|
+
ok: false,
|
|
224
|
+
error: {
|
|
225
|
+
kind: "malformed",
|
|
226
|
+
detail: "HTTP 200: the answer is not an uploaded file",
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
routes = {
|
|
230
|
+
[`GET ${LISTING}`]: ok200("get-listing.json"),
|
|
231
|
+
[`PUT ${put}`]: { status: 502, body: "<html>" },
|
|
232
|
+
};
|
|
233
|
+
// Guards: a PUT whose answer is not JSON stops before the read-back.
|
|
234
|
+
expect(await host.upload(u, "tok")).toEqual({
|
|
235
|
+
ok: false,
|
|
236
|
+
error: { kind: "malformed", detail: "HTTP 502: the answer is not JSON" },
|
|
237
|
+
});
|
|
238
|
+
expect(seen.at(-1)?.request).toBe(`PUT ${put}`);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("the parsers: shapes that are not OSF's are malformed; a file without hashes has none", () => {
|
|
242
|
+
// Guards: each parser refuses a body of the wrong shape, by what it expected.
|
|
243
|
+
expect(parseFile(200, {})).toEqual({
|
|
244
|
+
ok: false,
|
|
245
|
+
error: { kind: "malformed", detail: "HTTP 200: the answer is not a file" },
|
|
246
|
+
});
|
|
247
|
+
expect(parseListing(200, {}, "a")).toEqual({
|
|
248
|
+
ok: false,
|
|
249
|
+
error: {
|
|
250
|
+
kind: "malformed",
|
|
251
|
+
detail: "HTTP 200: the answer is not a list of files",
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
expect(parseListing(404, {}, "a").ok).toBe(false);
|
|
255
|
+
expect(parseUploaded(500, {}).ok).toBe(false);
|
|
256
|
+
const bare = {
|
|
257
|
+
id: "i",
|
|
258
|
+
attributes: { name: "a", kind: "file", extra: {} },
|
|
259
|
+
links: { html: "h" },
|
|
260
|
+
};
|
|
261
|
+
// Guards: a file OSF gives no hashes for reads as none, not as an error.
|
|
262
|
+
expect(parseFile(200, { data: bare })).toEqual({
|
|
263
|
+
ok: true,
|
|
264
|
+
value: { id: "i", url: "h", sha256: null, md5: null },
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test("targetOf: the project's guid in an osf.io url, else none", () => {
|
|
269
|
+
// Guards: a project url, with or without a view-only link; anything else is none.
|
|
270
|
+
expect(host.targetOf("https://osf.io/abcde/?view_only=1")).toBe("abcde");
|
|
271
|
+
expect(host.targetOf("https://osf.io/settings/")).toBe(null);
|
|
272
|
+
expect(host.targetOf("https://zenodo.org/records/1")).toBe(null);
|
|
273
|
+
expect(host.targetOf("not a url")).toBe(null);
|
|
274
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OSF (osf.io), as the `ArtifactHost` port: its API read and WaterButler written over `fetch`, its
|
|
3
|
+
* answers parsed here, its token variable and page. Everything that changes if OSF's API does lives
|
|
4
|
+
* in this folder; `cassette/README.md` says which answers are recorded and which are written from
|
|
5
|
+
* OSF's source. A contributor adding a host copies this folder's shape: `host.io.ts`, `response.ts`,
|
|
6
|
+
* a cassette and its test, and one line in `ARTIFACT_HOSTS` (src/cli.ts).
|
|
7
|
+
*/
|
|
8
|
+
export { OSF, osfHost, type OsfUrls } from "./host.io.ts";
|
|
9
|
+
export {
|
|
10
|
+
parseFile,
|
|
11
|
+
parseListing,
|
|
12
|
+
parseUploaded,
|
|
13
|
+
statusFailure,
|
|
14
|
+
} from "./response.ts";
|