ep_bounded_timeslider_api 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/LICENSE +189 -0
- package/README.md +76 -0
- package/ep.json +13 -0
- package/index.js +158 -0
- package/lib/auth.js +14 -0
- package/lib/report.js +104 -0
- package/lib/snapshot.js +29 -0
- package/package.json +47 -0
- package/static/js/history-authors.js +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including
|
|
48
|
+
the original version of the Work and any modifications or additions
|
|
49
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
50
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
51
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
52
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
53
|
+
means any form of electronic, verbal, or written communication sent
|
|
54
|
+
to the Licensor or its representatives, including but not limited to
|
|
55
|
+
communication on electronic mailing lists, source code control systems,
|
|
56
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
57
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
58
|
+
excluding communication that is conspicuously marked or otherwise
|
|
59
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
60
|
+
|
|
61
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
62
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
63
|
+
subsequently incorporated within the Work.
|
|
64
|
+
|
|
65
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
66
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
67
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
68
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
69
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
70
|
+
Work and such Derivative Works in Source or Object form.
|
|
71
|
+
|
|
72
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
73
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
74
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
75
|
+
(except as stated in this section) patent license to make, have made,
|
|
76
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
77
|
+
where such license applies only to those patent claims licensable
|
|
78
|
+
by such Contributor that are necessarily infringed by their
|
|
79
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
80
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
81
|
+
institute patent litigation against any entity (including a
|
|
82
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
83
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
84
|
+
or contributory patent infringement, then any patent licenses
|
|
85
|
+
granted to You under this License for that Work shall terminate
|
|
86
|
+
as of the date such litigation is filed.
|
|
87
|
+
|
|
88
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
89
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
90
|
+
modifications, and in Source or Object form, provided that You
|
|
91
|
+
meet the following conditions:
|
|
92
|
+
|
|
93
|
+
(a) You must give any other recipients of the Work or
|
|
94
|
+
Derivative Works a copy of this License; and
|
|
95
|
+
|
|
96
|
+
(b) You must cause any modified files to carry prominent notices
|
|
97
|
+
stating that You changed the files; and
|
|
98
|
+
|
|
99
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
100
|
+
that You distribute, all copyright, patent, trademark, and
|
|
101
|
+
attribution notices from the Source form of the Work,
|
|
102
|
+
excluding those notices that do not pertain to any part of
|
|
103
|
+
the Derivative Works; and
|
|
104
|
+
|
|
105
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
106
|
+
distribution, then any Derivative Works that You distribute must
|
|
107
|
+
include a readable copy of the attribution notices contained
|
|
108
|
+
within such NOTICE file, excluding those notices that do not
|
|
109
|
+
pertain to any part of the Derivative Works, in at least one
|
|
110
|
+
of the following places: within a NOTICE text file distributed
|
|
111
|
+
as part of the Derivative Works; within the Source form or
|
|
112
|
+
documentation, if provided along with the Derivative Works; or,
|
|
113
|
+
within a display generated by the Derivative Works, if and
|
|
114
|
+
wherever such third-party notices normally appear. The contents
|
|
115
|
+
of the NOTICE file are for informational purposes only and
|
|
116
|
+
do not modify the License. You may add Your own attribution
|
|
117
|
+
notices within Derivative Works that You distribute, alongside
|
|
118
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
119
|
+
that such additional attribution notices cannot be construed
|
|
120
|
+
as modifying the License.
|
|
121
|
+
|
|
122
|
+
You may add Your own copyright statement to Your modifications and
|
|
123
|
+
may provide additional or different license terms and conditions
|
|
124
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
125
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
126
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
127
|
+
the conditions stated in this License.
|
|
128
|
+
|
|
129
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
130
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
131
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
132
|
+
this License, without any additional terms or conditions.
|
|
133
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
134
|
+
the terms of any separate license agreement you may have executed
|
|
135
|
+
with Licensor regarding such Contributions.
|
|
136
|
+
|
|
137
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
138
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
139
|
+
except as required for reasonable and customary use in describing the
|
|
140
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
141
|
+
|
|
142
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
143
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
144
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
145
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
146
|
+
implied, including, without limitation, any warranties or conditions
|
|
147
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
148
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
149
|
+
appropriateness of using or redistributing the Work and assume any
|
|
150
|
+
risks associated with Your exercise of permissions under this License.
|
|
151
|
+
|
|
152
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
153
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
154
|
+
unless required by applicable law (such as deliberate and grossly
|
|
155
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
156
|
+
liable to You for damages, including any direct, indirect, special,
|
|
157
|
+
incidental, or consequential damages of any character arising as a
|
|
158
|
+
result of this License or out of the use or inability to use the
|
|
159
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
160
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
161
|
+
other commercial damages or losses), even if such Contributor
|
|
162
|
+
has been advised of the possibility of such damages.
|
|
163
|
+
|
|
164
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
165
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
166
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
167
|
+
or other liability obligations and/or rights consistent with this
|
|
168
|
+
License. However, in accepting such obligations, You may act only
|
|
169
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
170
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
171
|
+
defend, and hold each Contributor harmless for any liability
|
|
172
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
173
|
+
of your accepting any such warranty or additional liability.
|
|
174
|
+
|
|
175
|
+
END OF TERMS AND CONDITIONS
|
|
176
|
+
|
|
177
|
+
Copyright 2026 Daniel Castelone
|
|
178
|
+
|
|
179
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
180
|
+
you may not use this file except in compliance with the License.
|
|
181
|
+
You may obtain a copy of the License at
|
|
182
|
+
|
|
183
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
184
|
+
|
|
185
|
+
Unless required by applicable law or agreed to in writing, software
|
|
186
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
187
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
188
|
+
See the License for the specific language governing permissions and
|
|
189
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# ep_bounded_timeslider_api
|
|
2
|
+
|
|
3
|
+
Bounded, server-side Etherpad timeslider API endpoints for assessment workflows. The plugin reads exact revision ranges, preserves Etherpad attribution, and can return a complete attributed diff snapshot for downstream rendering.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- API-key-authenticated, read-only HTTP endpoints
|
|
8
|
+
- Inclusive, bounded revision ranges
|
|
9
|
+
- Exact additions grouped by Etherpad author
|
|
10
|
+
- Attributed text segments for safe downstream rendering
|
|
11
|
+
- Complete before, after, and diff snapshots
|
|
12
|
+
- Visible author list in Etherpad's embedded timeslider
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
From the Etherpad directory:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
pnpm run plugins i ep_bounded_timeslider_api
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Restart Etherpad after installation. This release supports Etherpad 3.3.2 and later 3.x releases.
|
|
23
|
+
|
|
24
|
+
## Authentication
|
|
25
|
+
|
|
26
|
+
Both endpoints require Etherpad's API key. Prefer either request header:
|
|
27
|
+
|
|
28
|
+
```text
|
|
29
|
+
apikey: <etherpad-api-key>
|
|
30
|
+
x-api-key: <etherpad-api-key>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Query-string API keys are accepted for Etherpad API compatibility but are discouraged because URLs can be retained in logs and browser history. Expose these endpoints only on trusted server-to-server ingress.
|
|
34
|
+
|
|
35
|
+
## Authored revision report
|
|
36
|
+
|
|
37
|
+
`GET /pluginfw/ep_bounded_timeslider_api/revisions`
|
|
38
|
+
|
|
39
|
+
| Parameter | Required | Description |
|
|
40
|
+
| --- | --- | --- |
|
|
41
|
+
| `padID` | Yes | Etherpad pad ID |
|
|
42
|
+
| `startRev` | No | First included revision |
|
|
43
|
+
| `endRev` | No | Last included revision |
|
|
44
|
+
| `authorIDs` | No | Comma-separated Etherpad author IDs, up to 50 |
|
|
45
|
+
| `excerptChars` | No | Text excerpt length; defaults to 500 and is capped at 2,000 |
|
|
46
|
+
|
|
47
|
+
The response contains revision timestamps, revision authors, and exact additions. Insert operations without an operation-level author attribute fall back to the stored revision author.
|
|
48
|
+
|
|
49
|
+
## Attributed snapshot report
|
|
50
|
+
|
|
51
|
+
`GET /pluginfw/ep_bounded_timeslider_api/snapshot`
|
|
52
|
+
|
|
53
|
+
This endpoint accepts `padID`, `startRev`, and `endRev`. It returns Etherpad's attributed diff lines plus `beforeLines` and `afterLines`, allowing a consumer to reconstruct semantic structures when a plugin rewrites a complete line for a smaller edit.
|
|
54
|
+
|
|
55
|
+
## Limits
|
|
56
|
+
|
|
57
|
+
- Default maximum range: 1,200 revisions
|
|
58
|
+
- Hard maximum range: 5,000 revisions
|
|
59
|
+
- Default read batch: 50 revisions
|
|
60
|
+
- Maximum read batch: 100 revisions
|
|
61
|
+
|
|
62
|
+
The deployment can set:
|
|
63
|
+
|
|
64
|
+
```text
|
|
65
|
+
EP_BOUNDED_TIMESLIDER_API_MAX_REVISIONS=1200
|
|
66
|
+
EP_BOUNDED_TIMESLIDER_API_BATCH_SIZE=50
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Development
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
pnpm install --frozen-lockfile
|
|
73
|
+
pnpm test
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Licensed under the Apache License 2.0.
|
package/ep.json
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const log4js = require('log4js');
|
|
4
|
+
const padManager = require('ep_etherpad-lite/node/db/PadManager');
|
|
5
|
+
const apiKeyHandler = require('ep_etherpad-lite/node/handler/APIKeyHandler');
|
|
6
|
+
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
|
|
7
|
+
const attributes = require('ep_etherpad-lite/static/js/attributes');
|
|
8
|
+
const PadDiff = require('ep_etherpad-lite/node/utils/padDiff');
|
|
9
|
+
const {isValidApiKey} = require('./lib/auth');
|
|
10
|
+
const {normalizeRange, readRevisionReport} = require('./lib/report');
|
|
11
|
+
const {serializeAText} = require('./lib/snapshot');
|
|
12
|
+
|
|
13
|
+
const logger = log4js.getLogger('ep_bounded_timeslider_api');
|
|
14
|
+
const DEFAULT_MAX_REVISIONS = 1200;
|
|
15
|
+
const HARD_MAX_REVISIONS = 5000;
|
|
16
|
+
const DEFAULT_BATCH_SIZE = 50;
|
|
17
|
+
const DEFAULT_EXCERPT_CHARS = 500;
|
|
18
|
+
|
|
19
|
+
const positiveInteger = (value, fallback) => {
|
|
20
|
+
const parsed = Number(value);
|
|
21
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const parseOptionalInteger = (value, name) => {
|
|
25
|
+
if (value == null || value === '') return undefined;
|
|
26
|
+
const parsed = Number(value);
|
|
27
|
+
if (!Number.isInteger(parsed)) throw new TypeError(`${name} must be an integer`);
|
|
28
|
+
return parsed;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const parseAuthorIds = (value) => {
|
|
32
|
+
if (value == null || value === '') return null;
|
|
33
|
+
const authorIds = String(value).split(',').map((id) => id.trim()).filter(Boolean);
|
|
34
|
+
if (authorIds.length > 50) throw new RangeError('authorIDs accepts at most 50 authors');
|
|
35
|
+
if (authorIds.some((id) => id.length > 100 || !/^a\.[A-Za-z0-9_-]+$/.test(id))) {
|
|
36
|
+
throw new TypeError('authorIDs contains an invalid Etherpad author ID');
|
|
37
|
+
}
|
|
38
|
+
return new Set(authorIds);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const providedApiKey = (req) =>
|
|
42
|
+
req.get('apikey') || req.get('x-api-key') || req.query.apikey || req.query.api_key || '';
|
|
43
|
+
|
|
44
|
+
exports.expressPreSession = async (_hookName, {app}) => {
|
|
45
|
+
app.get('/pluginfw/ep_bounded_timeslider_api/revisions', async (req, res) => {
|
|
46
|
+
const startedAt = Date.now();
|
|
47
|
+
try {
|
|
48
|
+
if (!isValidApiKey(providedApiKey(req), apiKeyHandler.apikey)) {
|
|
49
|
+
return res.status(401).json({code: 4, message: 'no or wrong API Key', data: null});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const padID = String(req.query.padID || '');
|
|
53
|
+
if (!padManager.isValidPadId(padID)) {
|
|
54
|
+
return res.status(400).json({code: 1, message: 'invalid padID', data: null});
|
|
55
|
+
}
|
|
56
|
+
if (!await padManager.doesPadExist(padID)) {
|
|
57
|
+
return res.status(404).json({code: 1, message: 'padID does not exist', data: null});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const configuredMax = Math.min(
|
|
61
|
+
positiveInteger(process.env.EP_BOUNDED_TIMESLIDER_API_MAX_REVISIONS, DEFAULT_MAX_REVISIONS),
|
|
62
|
+
HARD_MAX_REVISIONS);
|
|
63
|
+
const batchSize = Math.min(
|
|
64
|
+
positiveInteger(process.env.EP_BOUNDED_TIMESLIDER_API_BATCH_SIZE, DEFAULT_BATCH_SIZE),
|
|
65
|
+
100);
|
|
66
|
+
const excerptChars = Math.min(
|
|
67
|
+
positiveInteger(req.query.excerptChars, DEFAULT_EXCERPT_CHARS),
|
|
68
|
+
2000);
|
|
69
|
+
const authorIds = parseAuthorIds(req.query.authorIDs);
|
|
70
|
+
const pad = await padManager.getPad(padID);
|
|
71
|
+
const range = normalizeRange(
|
|
72
|
+
pad.getHeadRevisionNumber(),
|
|
73
|
+
parseOptionalInteger(req.query.startRev, 'startRev'),
|
|
74
|
+
parseOptionalInteger(req.query.endRev, 'endRev'),
|
|
75
|
+
configuredMax);
|
|
76
|
+
|
|
77
|
+
const report = await readRevisionReport({
|
|
78
|
+
pad,
|
|
79
|
+
range,
|
|
80
|
+
batchSize,
|
|
81
|
+
excerptChars,
|
|
82
|
+
authorIds,
|
|
83
|
+
changeset: Changeset,
|
|
84
|
+
attributes,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
logger.info('bounded timeslider revision report generated', {
|
|
88
|
+
padID,
|
|
89
|
+
startRev: range.startRev,
|
|
90
|
+
endRev: range.endRev,
|
|
91
|
+
revisions: report.revisions.length,
|
|
92
|
+
durationMs: Date.now() - startedAt,
|
|
93
|
+
});
|
|
94
|
+
return res.json({code: 0, message: 'ok', data: {padID, ...report}});
|
|
95
|
+
} catch (err) {
|
|
96
|
+
const status = err instanceof RangeError ? 413 :
|
|
97
|
+
err instanceof TypeError ? 400 : 500;
|
|
98
|
+
if (status === 500) logger.error('bounded timeslider revision report failed', err);
|
|
99
|
+
return res.status(status).json({
|
|
100
|
+
code: 1,
|
|
101
|
+
message: status === 500 ? 'failed to generate bounded timeslider report' : err.message,
|
|
102
|
+
data: null,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
app.get('/pluginfw/ep_bounded_timeslider_api/snapshot', async (req, res) => {
|
|
108
|
+
const startedAt = Date.now();
|
|
109
|
+
try {
|
|
110
|
+
if (!isValidApiKey(providedApiKey(req), apiKeyHandler.apikey)) {
|
|
111
|
+
return res.status(401).json({code: 4, message: 'no or wrong API Key', data: null});
|
|
112
|
+
}
|
|
113
|
+
const padID = String(req.query.padID || '');
|
|
114
|
+
if (!padManager.isValidPadId(padID)) {
|
|
115
|
+
return res.status(400).json({code: 1, message: 'invalid padID', data: null});
|
|
116
|
+
}
|
|
117
|
+
if (!await padManager.doesPadExist(padID)) {
|
|
118
|
+
return res.status(404).json({code: 1, message: 'padID does not exist', data: null});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const pad = await padManager.getPad(padID);
|
|
122
|
+
const configuredMax = Math.min(
|
|
123
|
+
positiveInteger(process.env.EP_BOUNDED_TIMESLIDER_API_MAX_REVISIONS, DEFAULT_MAX_REVISIONS),
|
|
124
|
+
HARD_MAX_REVISIONS);
|
|
125
|
+
const range = normalizeRange(
|
|
126
|
+
pad.getHeadRevisionNumber(),
|
|
127
|
+
parseOptionalInteger(req.query.startRev, 'startRev'),
|
|
128
|
+
parseOptionalInteger(req.query.endRev, 'endRev'),
|
|
129
|
+
configuredMax);
|
|
130
|
+
const diff = new PadDiff(pad, String(range.startRev), String(range.endRev));
|
|
131
|
+
const [atext, beforeAText, afterAText] = await Promise.all([
|
|
132
|
+
diff._createDiffAtext(),
|
|
133
|
+
pad.getInternalRevisionAText(range.startRev),
|
|
134
|
+
pad.getInternalRevisionAText(range.endRev),
|
|
135
|
+
]);
|
|
136
|
+
const serialize = (value) => serializeAText({
|
|
137
|
+
atext: value, pool: pad.apool(), changeset: Changeset, attributes,
|
|
138
|
+
});
|
|
139
|
+
const lines = serialize(atext);
|
|
140
|
+
const beforeLines = serialize(beforeAText);
|
|
141
|
+
const afterLines = serialize(afterAText);
|
|
142
|
+
|
|
143
|
+
logger.info('snapshot generated', {
|
|
144
|
+
padID, startRev: range.startRev, endRev: range.endRev,
|
|
145
|
+
lines: lines.length, durationMs: Date.now() - startedAt,
|
|
146
|
+
});
|
|
147
|
+
return res.json({code: 0, message: 'ok', data: {padID, ...range, lines, beforeLines, afterLines}});
|
|
148
|
+
} catch (err) {
|
|
149
|
+
const status = err instanceof RangeError ? 413 : err instanceof TypeError ? 400 : 500;
|
|
150
|
+
if (status === 500) logger.error('snapshot failed', err);
|
|
151
|
+
return res.status(status).json({
|
|
152
|
+
code: 1,
|
|
153
|
+
message: status === 500 ? 'failed to generate contribution snapshot' : err.message,
|
|
154
|
+
data: null,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
};
|
package/lib/auth.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('node:crypto');
|
|
4
|
+
|
|
5
|
+
const isValidApiKey = (provided, expected) => {
|
|
6
|
+
const wanted = String(expected || '').trim();
|
|
7
|
+
if (!wanted) return false;
|
|
8
|
+
const actualBuffer = Buffer.from(String(provided || ''), 'utf8');
|
|
9
|
+
const wantedBuffer = Buffer.from(wanted, 'utf8');
|
|
10
|
+
return actualBuffer.length === wantedBuffer.length &&
|
|
11
|
+
crypto.timingSafeEqual(actualBuffer, wantedBuffer);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
module.exports = {isValidApiKey};
|
package/lib/report.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const normalizeRange = (headRevision, requestedStart, requestedEnd, maxRevisions) => {
|
|
4
|
+
if (!Number.isInteger(headRevision) || headRevision < 0) {
|
|
5
|
+
throw new RangeError('pad has no reportable revisions');
|
|
6
|
+
}
|
|
7
|
+
const endRev = requestedEnd == null ? headRevision : requestedEnd;
|
|
8
|
+
const startRev = requestedStart == null ? Math.max(0, endRev - maxRevisions + 1) : requestedStart;
|
|
9
|
+
if (!Number.isInteger(startRev) || startRev < 0) throw new TypeError('startRev must be a non-negative integer');
|
|
10
|
+
if (!Number.isInteger(endRev) || endRev < startRev) throw new TypeError('endRev must be at least startRev');
|
|
11
|
+
if (endRev > headRevision) throw new RangeError('endRev is higher than the pad head revision');
|
|
12
|
+
if (endRev - startRev + 1 > maxRevisions) {
|
|
13
|
+
throw new RangeError(`requested revision range exceeds the ${maxRevisions} revision limit`);
|
|
14
|
+
}
|
|
15
|
+
return {startRev, endRev, headRevision, revisionCount: headRevision + 1};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const readOperationAttributes = (attribString, pool, attributes) => {
|
|
19
|
+
const operationAttributes = [];
|
|
20
|
+
for (const attributeNumber of attributes.decodeAttribString(attribString)) {
|
|
21
|
+
const pair = pool.getAttrib(attributeNumber);
|
|
22
|
+
if (pair) operationAttributes.push(pair);
|
|
23
|
+
}
|
|
24
|
+
return operationAttributes;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const summarizeRevision = ({rev, record, pool, authorIds, excerptChars, changeset, attributes}) => {
|
|
28
|
+
const timestamp = Number(record?.meta?.timestamp);
|
|
29
|
+
if (!Number.isFinite(timestamp) || timestamp <= 0) {
|
|
30
|
+
throw new Error(`revision ${rev} has an invalid timestamp`);
|
|
31
|
+
}
|
|
32
|
+
const revisionAuthorId = String(record?.meta?.author || '');
|
|
33
|
+
const unpacked = changeset.unpack(record.changeset);
|
|
34
|
+
const additions = new Map();
|
|
35
|
+
let bankOffset = 0;
|
|
36
|
+
|
|
37
|
+
for (const op of changeset.deserializeOps(unpacked.ops)) {
|
|
38
|
+
if (op.opcode !== '+') continue;
|
|
39
|
+
const text = unpacked.charBank.slice(bankOffset, bankOffset + op.chars);
|
|
40
|
+
bankOffset += op.chars;
|
|
41
|
+
const operationAttributes = readOperationAttributes(op.attribs, pool, attributes);
|
|
42
|
+
const authorAttribute = operationAttributes.find(([key]) => key === 'author');
|
|
43
|
+
const authorId = authorAttribute?.[1] || revisionAuthorId;
|
|
44
|
+
if (!authorId || (authorIds && !authorIds.has(authorId))) continue;
|
|
45
|
+
|
|
46
|
+
let item = additions.get(authorId);
|
|
47
|
+
if (!item) {
|
|
48
|
+
item = {authorId, chars: 0, text: '', textTruncated: false, segments: []};
|
|
49
|
+
additions.set(authorId, item);
|
|
50
|
+
}
|
|
51
|
+
item.chars += op.chars;
|
|
52
|
+
const remaining = Math.max(0, excerptChars - item.text.length);
|
|
53
|
+
const excerpt = text.slice(0, remaining);
|
|
54
|
+
item.text += excerpt;
|
|
55
|
+
if (excerpt) {
|
|
56
|
+
item.segments.push({
|
|
57
|
+
text: excerpt,
|
|
58
|
+
attributes: operationAttributes.filter(([key]) => key !== 'author'),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
if (text.length > remaining) item.textTruncated = true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
rev,
|
|
66
|
+
timestamp,
|
|
67
|
+
revisionAuthorId,
|
|
68
|
+
additions: Array.from(additions.values()),
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const readRevisionReport = async ({
|
|
73
|
+
pad,
|
|
74
|
+
range,
|
|
75
|
+
batchSize,
|
|
76
|
+
excerptChars,
|
|
77
|
+
authorIds,
|
|
78
|
+
changeset,
|
|
79
|
+
attributes,
|
|
80
|
+
}) => {
|
|
81
|
+
const revisions = [];
|
|
82
|
+
const pool = pad.apool();
|
|
83
|
+
for (let batchStart = range.startRev; batchStart <= range.endRev; batchStart += batchSize) {
|
|
84
|
+
const batchEnd = Math.min(range.endRev, batchStart + batchSize - 1);
|
|
85
|
+
const revisionNumbers = [];
|
|
86
|
+
for (let rev = batchStart; rev <= batchEnd; rev += 1) revisionNumbers.push(rev);
|
|
87
|
+
const records = await Promise.all(revisionNumbers.map((rev) => pad.getRevision(rev)));
|
|
88
|
+
for (let i = 0; i < revisionNumbers.length; i += 1) {
|
|
89
|
+
revisions.push(summarizeRevision({
|
|
90
|
+
rev: revisionNumbers[i],
|
|
91
|
+
record: records[i],
|
|
92
|
+
pool,
|
|
93
|
+
authorIds,
|
|
94
|
+
excerptChars,
|
|
95
|
+
changeset,
|
|
96
|
+
attributes,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {...range, revisions};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
module.exports = {normalizeRange, readRevisionReport, summarizeRevision};
|
package/lib/snapshot.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const readAttributes = (attribString, pool, attributes) => {
|
|
4
|
+
const result = [];
|
|
5
|
+
for (const number of attributes.decodeAttribString(attribString)) {
|
|
6
|
+
const pair = pool.getAttrib(number);
|
|
7
|
+
if (pair) result.push(pair);
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const serializeAText = ({atext, pool, changeset, attributes}) => {
|
|
13
|
+
const lines = [[]];
|
|
14
|
+
let offset = 0;
|
|
15
|
+
for (const op of changeset.deserializeOps(atext.attribs)) {
|
|
16
|
+
const text = atext.text.slice(offset, offset + op.chars);
|
|
17
|
+
offset += op.chars;
|
|
18
|
+
const attrs = readAttributes(op.attribs, pool, attributes);
|
|
19
|
+
const parts = text.split('\n');
|
|
20
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
21
|
+
if (parts[index]) lines[lines.length - 1].push({text: parts[index], attributes: attrs});
|
|
22
|
+
if (index < parts.length - 1) lines.push([]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (lines.length && lines[lines.length - 1].length === 0) lines.pop();
|
|
26
|
+
return lines.map((segments) => ({segments}));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
module.exports = {serializeAText};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ep_bounded_timeslider_api",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"packageManager": "pnpm@11.13.0+sha512.88d94724d8f2e6c186744a5584c6e59ecac869ec7ba15e9cb4cd628e8dc7066820b2481d8ee3b51ea8da323a7378068aa58c556a3720d32b7c20a051d088363a",
|
|
5
|
+
"description": "Authenticated Etherpad revision analysis and attributed snapshots for assessment workflows.",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Daniel Castelone",
|
|
8
|
+
"url": "https://github.com/dcastelone"
|
|
9
|
+
},
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/dcastelone/ep_bounded_timeslider_api.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/dcastelone/ep_bounded_timeslider_api/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/dcastelone/ep_bounded_timeslider_api#readme",
|
|
19
|
+
"keywords": [
|
|
20
|
+
"etherpad",
|
|
21
|
+
"plugin",
|
|
22
|
+
"assessment",
|
|
23
|
+
"contributions",
|
|
24
|
+
"revisions"
|
|
25
|
+
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"ep_etherpad-lite": ">=3.3.2 <4"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=18.0.0"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"ep.json",
|
|
34
|
+
"index.js",
|
|
35
|
+
"lib/",
|
|
36
|
+
"static/"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"socket": "1.1.143"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"test": "node --test"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const AUTHORS_ID = 'bounded-timeslider-api-history-authors';
|
|
4
|
+
|
|
5
|
+
const authorLabel = () => {
|
|
6
|
+
const html10n = window.html10n;
|
|
7
|
+
if (html10n && typeof html10n.get === 'function') {
|
|
8
|
+
return html10n.get('timeslider.toolbar.authors') || 'Authors:';
|
|
9
|
+
}
|
|
10
|
+
return 'Authors:';
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const ensureAuthorRow = () => {
|
|
14
|
+
const banner = document.getElementById('history-banner');
|
|
15
|
+
if (!banner) return null;
|
|
16
|
+
|
|
17
|
+
let row = document.getElementById(AUTHORS_ID);
|
|
18
|
+
if (row) return row;
|
|
19
|
+
|
|
20
|
+
row = document.createElement('span');
|
|
21
|
+
row.id = AUTHORS_ID;
|
|
22
|
+
row.className = 'timeslider-subtitle';
|
|
23
|
+
row.setAttribute('aria-live', 'polite');
|
|
24
|
+
|
|
25
|
+
const returnButton = document.getElementById('history-banner-return');
|
|
26
|
+
banner.insertBefore(row, returnButton || null);
|
|
27
|
+
return row;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const mirrorAuthors = (source) => {
|
|
31
|
+
const row = ensureAuthorRow();
|
|
32
|
+
if (!row) return;
|
|
33
|
+
|
|
34
|
+
row.replaceChildren();
|
|
35
|
+
const label = document.createElement('span');
|
|
36
|
+
label.className = 'authors-label';
|
|
37
|
+
label.textContent = `${authorLabel()} `;
|
|
38
|
+
row.appendChild(label);
|
|
39
|
+
for (const child of source.childNodes) row.appendChild(child.cloneNode(true));
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const watchTimeslider = (iframe) => {
|
|
43
|
+
if (iframe.dataset.boundedTimesliderApiAuthorsBound === 'true') return;
|
|
44
|
+
iframe.dataset.boundedTimesliderApiAuthorsBound = 'true';
|
|
45
|
+
|
|
46
|
+
const bind = () => {
|
|
47
|
+
let source;
|
|
48
|
+
try {
|
|
49
|
+
source = iframe.contentDocument && iframe.contentDocument.getElementById('authorsList');
|
|
50
|
+
} catch (_err) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (!source) return;
|
|
54
|
+
mirrorAuthors(source);
|
|
55
|
+
new MutationObserver(() => mirrorAuthors(source))
|
|
56
|
+
.observe(source, {childList: true, subtree: true, characterData: true});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
iframe.addEventListener('load', bind);
|
|
60
|
+
bind();
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const findTimeslider = () => {
|
|
64
|
+
const iframe = document.querySelector('#history-frame-mount iframe');
|
|
65
|
+
if (iframe) watchTimeslider(iframe);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
exports.postAceInit = () => {
|
|
69
|
+
ensureAuthorRow();
|
|
70
|
+
findTimeslider();
|
|
71
|
+
|
|
72
|
+
const mount = document.getElementById('history-frame-mount');
|
|
73
|
+
if (mount) new MutationObserver(findTimeslider).observe(mount, {childList: true});
|
|
74
|
+
|
|
75
|
+
if (window.html10n && typeof window.html10n.bind === 'function') {
|
|
76
|
+
window.html10n.bind('localized', findTimeslider);
|
|
77
|
+
}
|
|
78
|
+
};
|