@deveye/types 0.20.3 → 0.21.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/package.json +1 -1
- package/src/domain/admission.ts +37 -0
- package/src/domain/device.ts +7 -3
- package/src/domain/featureRegistry.ts +3 -2
- package/src/domain/maintenance.ts +7 -1
- package/src/domain/notifications.ts +16 -0
- package/src/domain/packages.ts +34 -2
- package/src/domain/report.ts +12 -3
- package/src/domain/secrecy.ts +13 -0
- package/src/domain/user.ts +2 -0
- package/src/features/admin.ts +59 -1
- package/src/features/debug.ts +372 -0
- package/src/features/registry.ts +2 -0
- package/src/features/user.ts +46 -0
- package/src/http/device.ts +33 -15
- package/src/http/status.ts +6 -0
- package/src/index.ts +102 -6
- package/src/protocol/agent.ts +27 -2
- package/src/protocol/error.ts +4 -1
- package/src/sdk/accountExport.test.ts +115 -0
- package/src/sdk/accountExport.ts +254 -0
- package/src/sdk/client-ambient.d.ts +86 -0
- package/src/sdk/client.ts +44 -0
- package/src/sdk/devb.test.ts +148 -0
- package/src/sdk/devb.ts +146 -0
- package/src/sdk/download.test.ts +40 -0
- package/src/sdk/download.ts +48 -0
- package/src/sdk/manifest.test.ts +17 -1
- package/src/sdk/manifest.ts +51 -11
- package/src/sdk/net.test.ts +42 -1
- package/src/sdk/net.ts +100 -2
- package/src/sdk/pool.test.ts +36 -0
- package/src/sdk/pool.ts +26 -0
- package/src/sdk/providers.ts +107 -11
- package/src/sdk/server.ts +388 -13
- package/src/sdk/testing.test.ts +66 -0
- package/src/sdk/testing.ts +211 -2
|
@@ -13,6 +13,7 @@ declare module 'deveye-sdk-client' {
|
|
|
13
13
|
CSSProperties,
|
|
14
14
|
ChangeEvent,
|
|
15
15
|
ComponentType,
|
|
16
|
+
DragEvent,
|
|
16
17
|
InputHTMLAttributes,
|
|
17
18
|
ReactNode,
|
|
18
19
|
RefObject,
|
|
@@ -463,6 +464,81 @@ declare module 'deveye-sdk-client' {
|
|
|
463
464
|
draggingId: string | number | null;
|
|
464
465
|
};
|
|
465
466
|
|
|
467
|
+
// ── Files ──────────────────────────────────────────────────────────────
|
|
468
|
+
/** A file picked or dropped, with its relative path, name included (`photos/a.jpg`; `a.jpg` outside a folder). */
|
|
469
|
+
export interface PickedFile {
|
|
470
|
+
file: File;
|
|
471
|
+
path: string;
|
|
472
|
+
}
|
|
473
|
+
export interface UploadHandle {
|
|
474
|
+
done: Promise<void>;
|
|
475
|
+
abort(): void;
|
|
476
|
+
}
|
|
477
|
+
/** What `UploadHandle.done` rejects with: the server's message, or why the connection ended. */
|
|
478
|
+
export class UploadError extends Error {
|
|
479
|
+
readonly aborted: boolean;
|
|
480
|
+
constructor(message: string, aborted?: boolean);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* POSTs the file as is (`application/octet-stream`) to a route of yours,
|
|
484
|
+
* typically a `postStream` URL carrying a ticket. `onProgress` gets 0 to 1;
|
|
485
|
+
* at 1 the server is still writing.
|
|
486
|
+
*/
|
|
487
|
+
export function uploadFile(
|
|
488
|
+
url: string,
|
|
489
|
+
file: File,
|
|
490
|
+
onProgress: (ratio: number) => void
|
|
491
|
+
): UploadHandle;
|
|
492
|
+
/** Downloads a URL served as an attachment, without leaving the page. */
|
|
493
|
+
export function saveFrom(url: string): void;
|
|
494
|
+
/** Opens the system picker; `folder` takes a whole folder, relative paths included. Cancelled: empty. */
|
|
495
|
+
export function pickFiles(opts?: {
|
|
496
|
+
multiple?: boolean;
|
|
497
|
+
folder?: boolean;
|
|
498
|
+
accept?: string;
|
|
499
|
+
}): Promise<PickedFile[]>;
|
|
500
|
+
/** Reads a drop: its files and, recursively, the content of the folders dropped. */
|
|
501
|
+
export function filesOfDrop(data: DataTransfer): Promise<PickedFile[]>;
|
|
502
|
+
/** Makes any element a drop target: spread `props` on it, `over` while files hover. */
|
|
503
|
+
export function useFileDrop(
|
|
504
|
+
onFiles: (files: PickedFile[]) => void,
|
|
505
|
+
disabled?: boolean
|
|
506
|
+
): {
|
|
507
|
+
over: boolean;
|
|
508
|
+
props: {
|
|
509
|
+
onDragOver: (event: DragEvent) => void;
|
|
510
|
+
onDragLeave: (event: DragEvent) => void;
|
|
511
|
+
onDrop: (event: DragEvent) => void;
|
|
512
|
+
};
|
|
513
|
+
};
|
|
514
|
+
/**
|
|
515
|
+
* The drop area: a real button that opens the picker, dropping is a
|
|
516
|
+
* shortcut. `multiple` takes several files and whole folders; otherwise
|
|
517
|
+
* only the first file.
|
|
518
|
+
*/
|
|
519
|
+
export const Dropzone: ComponentType<{
|
|
520
|
+
onFiles: (files: PickedFile[]) => void;
|
|
521
|
+
title: string;
|
|
522
|
+
hint?: string;
|
|
523
|
+
accept?: string;
|
|
524
|
+
multiple?: boolean;
|
|
525
|
+
disabled?: boolean;
|
|
526
|
+
className?: string;
|
|
527
|
+
}>;
|
|
528
|
+
/**
|
|
529
|
+
* The quiet gauge of a quota in a feature's header: the same count as the
|
|
530
|
+
* refusal. It warns at 80 %, alarms at 95 %, and leads the workspace owner
|
|
531
|
+
* to the plans. `limit: null` shows the usage alone. Bytes by default.
|
|
532
|
+
*/
|
|
533
|
+
export const UsageMeter: ComponentType<{
|
|
534
|
+
used: number;
|
|
535
|
+
limit: number | null;
|
|
536
|
+
label: string;
|
|
537
|
+
explain?: string;
|
|
538
|
+
format?: (value: number) => string;
|
|
539
|
+
usedWord?: string;
|
|
540
|
+
}>;
|
|
541
|
+
|
|
466
542
|
// ── Server push events ─────────────────────────────────────────────────
|
|
467
543
|
/** Typed push subscription: filters `event`, safeParses, drops mismatches. */
|
|
468
544
|
export function onServerEvent<T>(
|
|
@@ -475,6 +551,16 @@ declare module 'deveye-sdk-client' {
|
|
|
475
551
|
/** Whether the socket is open right now (to tell a real error from an outage). */
|
|
476
552
|
export function isSocketOpen(): boolean;
|
|
477
553
|
|
|
554
|
+
// ── Usage ──────────────────────────────────────────────────────────────
|
|
555
|
+
/**
|
|
556
|
+
* Names the screen shown inside your feature's view (`'history'`,
|
|
557
|
+
* `'site/traffic'`), for the instance's usage figures and bug reports.
|
|
558
|
+
* Static segments only (lowercase letters, digits, dashes, slashes): never
|
|
559
|
+
* an id, a name or anything typed. Counted only while your view is the one
|
|
560
|
+
* open; the deepest screen mounted wins. `null` withdraws it.
|
|
561
|
+
*/
|
|
562
|
+
export function useSubView(segment: string | null): void;
|
|
563
|
+
|
|
478
564
|
// ── Data ───────────────────────────────────────────────────────────────
|
|
479
565
|
/**
|
|
480
566
|
* A resource key an EXTERNAL module may own: `x-<slug>.<name>`, as listed
|
package/src/sdk/client.ts
CHANGED
|
@@ -51,6 +51,11 @@ export interface AccountViewProps {
|
|
|
51
51
|
hint?: string;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
/** Props of {@link FeatureClient.AdminView}. */
|
|
55
|
+
export interface AdminViewProps {
|
|
56
|
+
close(): void;
|
|
57
|
+
}
|
|
58
|
+
|
|
54
59
|
/**
|
|
55
60
|
* Your package's `./client` export.
|
|
56
61
|
*/
|
|
@@ -65,6 +70,8 @@ export interface FeatureClient {
|
|
|
65
70
|
Full?: ComponentType<FeatureViewProps>;
|
|
66
71
|
/** The view `manifest.accountEntry` opens from the user menu. */
|
|
67
72
|
AccountView?: ComponentType<AccountViewProps>;
|
|
73
|
+
/** The system page `manifest.adminEntry` opens, for a global administrator only. */
|
|
74
|
+
AdminView?: ComponentType<AdminViewProps>;
|
|
68
75
|
/**
|
|
69
76
|
* The vignette shown on your card in the home grid's add market and on
|
|
70
77
|
* your "About" sheet. Without one, the host draws a neutral module mark:
|
|
@@ -418,6 +425,43 @@ export interface AudienceClientProvider {
|
|
|
418
425
|
}>;
|
|
419
426
|
}
|
|
420
427
|
|
|
428
|
+
/** A folder of the workspace, as the Hosting client provider lists it for a project's picker. */
|
|
429
|
+
export interface HostingLinkedCandidate {
|
|
430
|
+
id: number;
|
|
431
|
+
name: string;
|
|
432
|
+
/** Projected here from another workspace. */
|
|
433
|
+
foreign?: boolean;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* What the Hosting module offers the host's screens under
|
|
438
|
+
* `HOSTING_CLIENT_PROVIDER`: the workspace's folders, a linked folder shown
|
|
439
|
+
* inside a project's tab, and the feature's own folder dialog.
|
|
440
|
+
*/
|
|
441
|
+
export interface HostingClientProvider {
|
|
442
|
+
/** The workspace's folders, in the module's own order. */
|
|
443
|
+
listPacks(): Promise<readonly HostingLinkedCandidate[]>;
|
|
444
|
+
/**
|
|
445
|
+
* The given folders, summarised for a project's dashboard tile, in ONE
|
|
446
|
+
* round trip. An id the caller cannot read, or that is gone, comes back
|
|
447
|
+
* `unavailable`, never as a throw.
|
|
448
|
+
*/
|
|
449
|
+
summarize(packIds: readonly number[]): Promise<readonly SdkTileSummary[]>;
|
|
450
|
+
/**
|
|
451
|
+
* A folder linked to a project: its heading and status, its addresses,
|
|
452
|
+
* its latest files with their preview. Loads itself, follows the
|
|
453
|
+
* feature's invalidations, and renders the host's "unlink" as its
|
|
454
|
+
* trailing action.
|
|
455
|
+
*/
|
|
456
|
+
LinkedPack: ComponentType<{ packId: number; canWrite: boolean; onUnlink: () => void }>;
|
|
457
|
+
/** The feature's folder form, to create a folder from a project. */
|
|
458
|
+
PackDialog: ComponentType<{
|
|
459
|
+
open: boolean;
|
|
460
|
+
onClose: () => void;
|
|
461
|
+
onSaved: (packId: number) => void;
|
|
462
|
+
}>;
|
|
463
|
+
}
|
|
464
|
+
|
|
421
465
|
/**
|
|
422
466
|
* What the Invoicing module offers other screens under
|
|
423
467
|
* `INVOICING_CLIENT_PROVIDER`. It sends Invoicing's own command under the
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import crypto from 'node:crypto';
|
|
3
|
+
import { describe, it } from 'node:test';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
BLOB_CHUNK_BYTES,
|
|
7
|
+
openSealedRange,
|
|
8
|
+
openSealedStream,
|
|
9
|
+
sealedSize,
|
|
10
|
+
sealStream
|
|
11
|
+
} from './devb';
|
|
12
|
+
|
|
13
|
+
const KEY = crypto.createHash('sha256').update('test key').digest();
|
|
14
|
+
|
|
15
|
+
async function* once(data: Buffer): AsyncGenerator<Buffer> {
|
|
16
|
+
yield data;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Hands a slice back in small pieces, as a network read would. */
|
|
20
|
+
async function* sliced(data: Buffer, start: number, end: number): AsyncGenerator<Buffer> {
|
|
21
|
+
for (let at = start; at <= end; at += 70_001) {
|
|
22
|
+
yield data.subarray(at, Math.min(at + 70_001, end + 1));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function collect(source: AsyncIterable<Buffer>): Promise<Buffer> {
|
|
27
|
+
const parts: Buffer[] = [];
|
|
28
|
+
for await (const chunk of source) parts.push(chunk);
|
|
29
|
+
return Buffer.concat(parts);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const seal = (data: Buffer, source?: AsyncIterable<Buffer>) =>
|
|
33
|
+
collect(sealStream(KEY, source ?? once(data)));
|
|
34
|
+
|
|
35
|
+
describe('sealStream / openSealedStream', () => {
|
|
36
|
+
it('gives back exactly what it was given, empty included, at every chunk boundary', async () => {
|
|
37
|
+
for (const size of [
|
|
38
|
+
0,
|
|
39
|
+
1,
|
|
40
|
+
1024,
|
|
41
|
+
BLOB_CHUNK_BYTES - 1,
|
|
42
|
+
BLOB_CHUNK_BYTES,
|
|
43
|
+
BLOB_CHUNK_BYTES + 1
|
|
44
|
+
]) {
|
|
45
|
+
const data = crypto.randomBytes(size);
|
|
46
|
+
const sealed = await seal(data);
|
|
47
|
+
assert.equal(sealed.length, sealedSize(size), `sealed size of ${size}`);
|
|
48
|
+
assert.deepEqual(
|
|
49
|
+
await collect(openSealedStream(KEY, once(sealed))),
|
|
50
|
+
data,
|
|
51
|
+
`size ${size}`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('ignores how the source is cut', async () => {
|
|
57
|
+
const data = crypto.randomBytes(BLOB_CHUNK_BYTES + 9999);
|
|
58
|
+
async function* ragged(): AsyncGenerator<Buffer> {
|
|
59
|
+
let offset = 0;
|
|
60
|
+
for (const size of [1, 7, 100_000, 3, BLOB_CHUNK_BYTES, 512]) {
|
|
61
|
+
if (offset >= data.length) break;
|
|
62
|
+
yield data.subarray(offset, Math.min(offset + size, data.length));
|
|
63
|
+
offset += size;
|
|
64
|
+
}
|
|
65
|
+
if (offset < data.length) yield data.subarray(offset);
|
|
66
|
+
}
|
|
67
|
+
assert.deepEqual(
|
|
68
|
+
await collect(openSealedStream(KEY, once(await seal(data, ragged())))),
|
|
69
|
+
data
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('refuses a truncated blob, a tampered one, and another key', async () => {
|
|
74
|
+
const sealed = await seal(crypto.randomBytes(BLOB_CHUNK_BYTES * 2));
|
|
75
|
+
await assert.rejects(() =>
|
|
76
|
+
collect(openSealedStream(KEY, once(sealed.subarray(0, sealed.length - 64))))
|
|
77
|
+
);
|
|
78
|
+
const tampered = Buffer.from(sealed);
|
|
79
|
+
tampered[tampered.length - 40] ^= 0xff;
|
|
80
|
+
await assert.rejects(() => collect(openSealedStream(KEY, once(tampered))));
|
|
81
|
+
const other = crypto.createHash('sha256').update('other key').digest();
|
|
82
|
+
await assert.rejects(() => collect(openSealedStream(other, once(sealed))));
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('openSealedRange', () => {
|
|
87
|
+
const C = BLOB_CHUNK_BYTES;
|
|
88
|
+
|
|
89
|
+
it('reads any inclusive range, within a chunk or across several, up to the last byte', async () => {
|
|
90
|
+
for (const size of [1, 5000, C, C + 1, 3 * C + 17]) {
|
|
91
|
+
const data = crypto.randomBytes(size);
|
|
92
|
+
const sealed = await seal(data);
|
|
93
|
+
const read = (r: { start: number; end: number }) => sliced(sealed, r.start, r.end);
|
|
94
|
+
const clamp = (n: number) => Math.min(size - 1, Math.max(0, n));
|
|
95
|
+
const ranges = [
|
|
96
|
+
[0, 0],
|
|
97
|
+
[0, size - 1],
|
|
98
|
+
[size - 1, size - 1],
|
|
99
|
+
[Math.floor(size / 2), size - 1],
|
|
100
|
+
[clamp(C - 3), clamp(C + 2)],
|
|
101
|
+
[clamp(C), clamp(2 * C + 5)]
|
|
102
|
+
];
|
|
103
|
+
for (const [start, end] of ranges) {
|
|
104
|
+
const got = await collect(openSealedRange(KEY, read, size, { start, end }));
|
|
105
|
+
assert.deepEqual(
|
|
106
|
+
got,
|
|
107
|
+
data.subarray(start, end + 1),
|
|
108
|
+
`size ${size}, ${start}-${end}`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('reads only the header and the chunks that hold the range', async () => {
|
|
115
|
+
const size = 3 * C + 10;
|
|
116
|
+
const sealed = await seal(crypto.randomBytes(size));
|
|
117
|
+
const reads: { start: number; end: number }[] = [];
|
|
118
|
+
const read = (r: { start: number; end: number }) => {
|
|
119
|
+
reads.push(r);
|
|
120
|
+
return sliced(sealed, r.start, r.end);
|
|
121
|
+
};
|
|
122
|
+
await collect(openSealedRange(KEY, read, size, { start: C + 5, end: C + 10 }));
|
|
123
|
+
assert.equal(reads.length, 2);
|
|
124
|
+
assert.equal(reads[1].end - reads[1].start + 1, C + 16);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('refuses a range outside the blob, a tampered chunk, and a short read', async () => {
|
|
128
|
+
const size = 2 * C;
|
|
129
|
+
const sealed = await seal(crypto.randomBytes(size));
|
|
130
|
+
const read = (r: { start: number; end: number }) => sliced(sealed, r.start, r.end);
|
|
131
|
+
await assert.rejects(() =>
|
|
132
|
+
collect(openSealedRange(KEY, read, size, { start: 0, end: size }))
|
|
133
|
+
);
|
|
134
|
+
await assert.rejects(() => collect(openSealedRange(KEY, read, size, { start: 5, end: 4 })));
|
|
135
|
+
const tampered = Buffer.from(sealed);
|
|
136
|
+
tampered[C + 100] ^= 0xff;
|
|
137
|
+
const readTampered = (r: { start: number; end: number }) =>
|
|
138
|
+
sliced(tampered, r.start, r.end);
|
|
139
|
+
await assert.rejects(() =>
|
|
140
|
+
collect(openSealedRange(KEY, readTampered, size, { start: C - 10, end: C + 10 }))
|
|
141
|
+
);
|
|
142
|
+
const short = (r: { start: number; end: number }) =>
|
|
143
|
+
sliced(sealed, r.start, Math.min(r.end, r.start + 100));
|
|
144
|
+
await assert.rejects(() =>
|
|
145
|
+
collect(openSealedRange(KEY, short, size, { start: 0, end: 10 }))
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
});
|
package/src/sdk/devb.ts
CHANGED
|
@@ -118,3 +118,149 @@ export function openChunk(
|
|
|
118
118
|
export function openStreamDecipher(key: Buffer, nonce: Buffer): crypto.DecipherGCM {
|
|
119
119
|
return crypto.createDecipheriv('aes-256-gcm', key, nonce);
|
|
120
120
|
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Scelle un clair en v2. Ré-agrège en blocs de {@link BLOB_CHUNK_BYTES} quel
|
|
124
|
+
* que soit le découpage d'entrée : l'ouvreur compte les blocs pour retrouver
|
|
125
|
+
* leur rang.
|
|
126
|
+
*/
|
|
127
|
+
export async function* sealStream(
|
|
128
|
+
key: Buffer,
|
|
129
|
+
source: AsyncIterable<Uint8Array>
|
|
130
|
+
): AsyncGenerator<Buffer> {
|
|
131
|
+
const header = createBlobHeader();
|
|
132
|
+
const nonce = header.subarray(5, 17);
|
|
133
|
+
yield header;
|
|
134
|
+
|
|
135
|
+
let pending: Buffer[] = [];
|
|
136
|
+
let pendingLen = 0;
|
|
137
|
+
let index = 0;
|
|
138
|
+
|
|
139
|
+
for await (const part of source) {
|
|
140
|
+
pending.push(Buffer.from(part.buffer, part.byteOffset, part.byteLength));
|
|
141
|
+
pendingLen += part.byteLength;
|
|
142
|
+
while (pendingLen >= BLOB_CHUNK_BYTES) {
|
|
143
|
+
const joined = Buffer.concat(pending, pendingLen);
|
|
144
|
+
yield sealChunk(key, nonce, index, joined.subarray(0, BLOB_CHUNK_BYTES), false);
|
|
145
|
+
index += 1;
|
|
146
|
+
const rest = joined.subarray(BLOB_CHUNK_BYTES);
|
|
147
|
+
pending = rest.length > 0 ? [rest] : [];
|
|
148
|
+
pendingLen = rest.length;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Toujours un dernier bloc, même vide : c'est lui qui porte le marqueur de
|
|
153
|
+
// fin dans l'AAD, et donc ce qui rend un blob tronqué détectable.
|
|
154
|
+
yield sealChunk(key, nonce, index, Buffer.concat(pending, pendingLen), true);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Ouvre un flux v2. Lève dès qu'un bloc ne s'authentifie pas : jamais de lecture tolérante. */
|
|
158
|
+
export async function* openSealedStream(
|
|
159
|
+
key: Buffer,
|
|
160
|
+
source: AsyncIterable<Buffer>
|
|
161
|
+
): AsyncGenerator<Buffer> {
|
|
162
|
+
let buffer: Buffer = Buffer.alloc(0);
|
|
163
|
+
let nonce: Buffer | null = null;
|
|
164
|
+
let index = 0;
|
|
165
|
+
|
|
166
|
+
const take = (n: number): Buffer | null => {
|
|
167
|
+
if (buffer.length < n) return null;
|
|
168
|
+
const out = buffer.subarray(0, n);
|
|
169
|
+
buffer = buffer.subarray(n);
|
|
170
|
+
return out;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
for await (const part of source) {
|
|
174
|
+
buffer = buffer.length === 0 ? part : Buffer.concat([buffer, part]);
|
|
175
|
+
|
|
176
|
+
if (nonce === null) {
|
|
177
|
+
const header = take(BLOB_HEADER_LEN);
|
|
178
|
+
if (header === null) continue;
|
|
179
|
+
const parsed = parseBlobHeader(header);
|
|
180
|
+
if (parsed.version !== BLOB_V2) throw new Error('DEVB : format de bloc inattendu');
|
|
181
|
+
nonce = parsed.nonce;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Garder de quoi former un dernier bloc : à exactement `BLOB_CHUNK_SEALED`
|
|
185
|
+
// octets, on ne sait pas s'il est intermédiaire ou final, et l'AAD diffère.
|
|
186
|
+
while (buffer.length > BLOB_CHUNK_SEALED) {
|
|
187
|
+
const sealed = take(BLOB_CHUNK_SEALED);
|
|
188
|
+
if (sealed === null) break;
|
|
189
|
+
yield openChunk(key, nonce, index, sealed, false);
|
|
190
|
+
index += 1;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (nonce === null) throw new Error('DEVB : en-tête absent ou tronqué');
|
|
195
|
+
yield openChunk(key, nonce, index, buffer, true);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Taille sur le disque d'un clair de `plainSize` octets scellé en v2. */
|
|
199
|
+
export function sealedSize(plainSize: number): number {
|
|
200
|
+
const full = Math.floor(plainSize / BLOB_CHUNK_BYTES);
|
|
201
|
+
return (
|
|
202
|
+
BLOB_HEADER_LEN +
|
|
203
|
+
full * BLOB_CHUNK_SEALED +
|
|
204
|
+
(plainSize - full * BLOB_CHUNK_BYTES) +
|
|
205
|
+
BLOB_TAG_LEN
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Les octets `[start, end]` (inclus) du clair d'un blob v2 de `plainSize`
|
|
211
|
+
* octets, sans lire le reste : l'en-tête, puis les seuls blocs qui les
|
|
212
|
+
* portent, chacun authentifié avec son rang et son statut de dernier bloc.
|
|
213
|
+
* `read` lit une étendue inclusive du blob scellé, `SdkObjectStore.get` tel
|
|
214
|
+
* quel. Une troncature hors de l'étendue n'est pas vue : c'est une lecture
|
|
215
|
+
* partielle.
|
|
216
|
+
*/
|
|
217
|
+
export async function* openSealedRange(
|
|
218
|
+
key: Buffer,
|
|
219
|
+
read: (range: { start: number; end: number }) => AsyncIterable<Buffer>,
|
|
220
|
+
plainSize: number,
|
|
221
|
+
range: { start: number; end: number }
|
|
222
|
+
): AsyncGenerator<Buffer> {
|
|
223
|
+
const { start, end } = range;
|
|
224
|
+
if (
|
|
225
|
+
!Number.isInteger(start) ||
|
|
226
|
+
!Number.isInteger(end) ||
|
|
227
|
+
start < 0 ||
|
|
228
|
+
end < start ||
|
|
229
|
+
end >= plainSize
|
|
230
|
+
) {
|
|
231
|
+
throw new Error('DEVB : étendue hors du blob');
|
|
232
|
+
}
|
|
233
|
+
const parts: Buffer[] = [];
|
|
234
|
+
for await (const part of read({ start: 0, end: BLOB_HEADER_LEN - 1 })) parts.push(part);
|
|
235
|
+
const { version, nonce } = parseBlobHeader(Buffer.concat(parts));
|
|
236
|
+
if (version !== BLOB_V2) throw new Error('DEVB : format de bloc inattendu');
|
|
237
|
+
|
|
238
|
+
const finalIndex = Math.floor(plainSize / BLOB_CHUNK_BYTES);
|
|
239
|
+
const finalPlain = plainSize - finalIndex * BLOB_CHUNK_BYTES;
|
|
240
|
+
const sealedLength = (i: number) =>
|
|
241
|
+
i === finalIndex ? finalPlain + BLOB_TAG_LEN : BLOB_CHUNK_SEALED;
|
|
242
|
+
const first = Math.floor(start / BLOB_CHUNK_BYTES);
|
|
243
|
+
const last = Math.floor(end / BLOB_CHUNK_BYTES);
|
|
244
|
+
const from = BLOB_HEADER_LEN + first * BLOB_CHUNK_SEALED;
|
|
245
|
+
const to = BLOB_HEADER_LEN + last * BLOB_CHUNK_SEALED + sealedLength(last) - 1;
|
|
246
|
+
|
|
247
|
+
let buffer: Buffer = Buffer.alloc(0);
|
|
248
|
+
let index = first;
|
|
249
|
+
const emit = (plain: Buffer): Buffer => {
|
|
250
|
+
const offset = index * BLOB_CHUNK_BYTES;
|
|
251
|
+
const lo = index === first ? start - offset : 0;
|
|
252
|
+
const hi = index === last ? end - offset + 1 : plain.length;
|
|
253
|
+
return plain.subarray(lo, hi);
|
|
254
|
+
};
|
|
255
|
+
for await (const part of read({ start: from, end: to })) {
|
|
256
|
+
buffer = buffer.length === 0 ? part : Buffer.concat([buffer, part]);
|
|
257
|
+
while (index <= last && buffer.length >= sealedLength(index)) {
|
|
258
|
+
const n = sealedLength(index);
|
|
259
|
+
const plain = openChunk(key, nonce, index, buffer.subarray(0, n), index === finalIndex);
|
|
260
|
+
buffer = buffer.subarray(n);
|
|
261
|
+
yield emit(plain);
|
|
262
|
+
index += 1;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (index <= last) throw new Error('DEVB : blob tronqué');
|
|
266
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
|
|
4
|
+
import { contentDisposition, parseByteRange } from './download';
|
|
5
|
+
|
|
6
|
+
describe('contentDisposition', () => {
|
|
7
|
+
it('keeps a safe ASCII fallback and the real name in filename*', () => {
|
|
8
|
+
assert.equal(
|
|
9
|
+
contentDisposition('Été "final"\r\n.pdf'),
|
|
10
|
+
`attachment; filename="_t_ final__.pdf"; filename*=UTF-8''${encodeURIComponent('Été "final"\r\n.pdf')}`
|
|
11
|
+
);
|
|
12
|
+
assert.match(contentDisposition('é', 'inline'), /^inline; filename="_"/);
|
|
13
|
+
assert.match(contentDisposition(' '), /filename="fichier"/);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('parseByteRange', () => {
|
|
18
|
+
it('reads the three forms, clamped to the body', () => {
|
|
19
|
+
assert.deepEqual(parseByteRange('bytes=0-99', 1000), { start: 0, end: 99 });
|
|
20
|
+
assert.deepEqual(parseByteRange('bytes=900-', 1000), { start: 900, end: 999 });
|
|
21
|
+
assert.deepEqual(parseByteRange('bytes=-100', 1000), { start: 900, end: 999 });
|
|
22
|
+
assert.deepEqual(parseByteRange('bytes=-5000', 1000), { start: 0, end: 999 });
|
|
23
|
+
assert.deepEqual(parseByteRange('bytes=10-5000', 1000), { start: 10, end: 999 });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('serves the whole body when there is nothing it can honour', () => {
|
|
27
|
+
assert.equal(parseByteRange(undefined, 1000), null);
|
|
28
|
+
assert.equal(parseByteRange('bytes=0-1,5-9', 1000), null);
|
|
29
|
+
assert.equal(parseByteRange('items=0-1', 1000), null);
|
|
30
|
+
assert.equal(parseByteRange('bytes=-', 1000), null);
|
|
31
|
+
assert.equal(parseByteRange('bytes=9-5', 1000), null);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('calls out what lies past the end', () => {
|
|
35
|
+
assert.equal(parseByteRange('bytes=1000-', 1000), 'unsatisfiable');
|
|
36
|
+
assert.equal(parseByteRange('bytes=-0', 1000), 'unsatisfiable');
|
|
37
|
+
assert.equal(parseByteRange('bytes=0-', 0), 'unsatisfiable');
|
|
38
|
+
assert.equal(parseByteRange('bytes=-10', 0), 'unsatisfiable');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serving a stored file over HTTP: the two headers every download route
|
|
3
|
+
* writes, the same way everywhere.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `Content-Disposition` for a name that came from a user. The plain
|
|
8
|
+
* `filename=` form is reduced to safe ASCII (a newline, which Node rejects
|
|
9
|
+
* outright, would turn the download into a 500); the real name rides in the
|
|
10
|
+
* RFC 5987 `filename*` form, which browsers prefer when both are present.
|
|
11
|
+
* `inline` only for types that cannot run script on your origin.
|
|
12
|
+
*/
|
|
13
|
+
export function contentDisposition(
|
|
14
|
+
filename: string,
|
|
15
|
+
disposition: 'attachment' | 'inline' = 'attachment'
|
|
16
|
+
): string {
|
|
17
|
+
const ascii = filename.replace(/[^\x20-\x7e]/g, '_').replace(/["\\]/g, '');
|
|
18
|
+
const fallback = ascii.trim() || 'fichier';
|
|
19
|
+
return `${disposition}; filename="${fallback}"; filename*=UTF-8''${encodeURIComponent(filename)}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reads a `Range` request header against a body of `size` bytes (RFC 9110
|
|
24
|
+
* §14.1.2). One range only: `null` means serve the whole body with a 200 (no
|
|
25
|
+
* header, a malformed one, or several ranges, which a server may ignore);
|
|
26
|
+
* `'unsatisfiable'` calls for a 416 whose `Content-Range` states the size
|
|
27
|
+
* after an asterisk. The range is inclusive, as `SdkObjectStore.get` takes it.
|
|
28
|
+
*/
|
|
29
|
+
export function parseByteRange(
|
|
30
|
+
header: string | undefined,
|
|
31
|
+
size: number
|
|
32
|
+
): { start: number; end: number } | 'unsatisfiable' | null {
|
|
33
|
+
if (header === undefined) return null;
|
|
34
|
+
const match = /^\s*bytes\s*=\s*(\d*)\s*-\s*(\d*)\s*$/i.exec(header);
|
|
35
|
+
if (!match) return null;
|
|
36
|
+
const [, first, last] = match;
|
|
37
|
+
if (first === '' && last === '') return null;
|
|
38
|
+
if (first === '') {
|
|
39
|
+
const suffix = Number(last);
|
|
40
|
+
if (suffix === 0 || size === 0) return 'unsatisfiable';
|
|
41
|
+
return { start: Math.max(0, size - suffix), end: size - 1 };
|
|
42
|
+
}
|
|
43
|
+
const start = Number(first);
|
|
44
|
+
const end = last === '' ? size - 1 : Math.min(Number(last), size - 1);
|
|
45
|
+
if (last !== '' && Number(last) < start) return null;
|
|
46
|
+
if (start >= size) return 'unsatisfiable';
|
|
47
|
+
return { start, end };
|
|
48
|
+
}
|
package/src/sdk/manifest.test.ts
CHANGED
|
@@ -26,12 +26,18 @@ test('validateManifest accepts a minimal external manifest', () => {
|
|
|
26
26
|
assert.doesNotThrow(() => validateManifest(base));
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
+
test('validateManifest lets an external module with items share them', () => {
|
|
30
|
+
assert.doesNotThrow(() =>
|
|
31
|
+
validateManifest({ ...base, hasItems: true, itemNoun: 'pack', shareTier: 'open' })
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
29
35
|
test('validateManifest rejects the classic mistakes', () => {
|
|
30
36
|
const rejects = (patch: Partial<FeatureManifest>, fragment: string) =>
|
|
31
37
|
assert.throws(() => validateManifest({ ...base, ...patch }), new RegExp(fragment));
|
|
32
38
|
rejects({ label: ' ' }, 'empty label');
|
|
33
39
|
rejects({ hasItems: true }, 'itemNoun');
|
|
34
|
-
rejects({ shareTier: 'open' },
|
|
40
|
+
rejects({ shareTier: 'open' }, 'requires hasItems');
|
|
35
41
|
rejects({ commandPrefix: 'x-demo.' }, 'commandPrefix');
|
|
36
42
|
rejects(
|
|
37
43
|
{ commands: [{ command: 'other.get', input: z.object({}), output: z.object({}) }] },
|
|
@@ -166,6 +172,16 @@ test('validateManifest checks quotas and the account entry', () => {
|
|
|
166
172
|
{ quotas: [{ key: 'storage', label: 'of storage', unit: 'bytes', stock: true }] },
|
|
167
173
|
'a stock counts things'
|
|
168
174
|
);
|
|
175
|
+
assert.doesNotThrow(() =>
|
|
176
|
+
validateManifest({
|
|
177
|
+
...base,
|
|
178
|
+
quotas: [{ key: 'fileBytes', label: 'm', unit: 'bytes', perOperation: true }]
|
|
179
|
+
})
|
|
180
|
+
);
|
|
181
|
+
rejects(
|
|
182
|
+
{ quotas: [{ key: 'monitors', label: 'm', stock: true, perOperation: true }] },
|
|
183
|
+
'a per-operation limit is no stock'
|
|
184
|
+
);
|
|
169
185
|
rejects({ accountOnly: true }, 'accountOnly requires accountEntry');
|
|
170
186
|
rejects({ accountOnly: true, accountEntry: { label: 'S' }, tile: {} }, 'no tile');
|
|
171
187
|
rejects({ accountEntry: { label: ' ' } }, 'requires a label');
|