@astram/core 1.5.0 → 1.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/dist/audit/index.d.ts +1 -1
- package/dist/audit/index.d.ts.map +1 -1
- package/dist/audit/index.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/storage/file-ingest.d.ts +90 -0
- package/dist/storage/file-ingest.d.ts.map +1 -0
- package/dist/storage/file-ingest.js +508 -0
- package/dist/storage/file-ingest.js.map +1 -0
- package/dist/storage/index.d.ts +13 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +11 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/ports.d.ts +66 -0
- package/dist/storage/ports.d.ts.map +1 -0
- package/dist/storage/ports.js +30 -0
- package/dist/storage/ports.js.map +1 -0
- package/dist-cjs/index.js +1 -0
- package/dist-cjs/storage/file-ingest.js +512 -0
- package/dist-cjs/storage/index.js +19 -0
- package/dist-cjs/storage/ports.js +34 -0
- package/package.json +1 -1
package/dist/audit/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type { Decision, Action, Resource } from '../authorization/index.js';
|
|
11
11
|
/** Categories of auditable event. */
|
|
12
|
-
export type AuditEventType = 'authentication' | 'session_lifecycle' | 'authorization_decision' | 'permission_change' | 'admin_action';
|
|
12
|
+
export type AuditEventType = 'authentication' | 'session_lifecycle' | 'authorization_decision' | 'permission_change' | 'admin_action' | 'file_access';
|
|
13
13
|
/**
|
|
14
14
|
* One immutable, attributable audit record. Actor is referenced by stable id;
|
|
15
15
|
* the presentation layer resolves it to a display label (never surfaces raw ids
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE5E,qCAAqC;AACrC,MAAM,MAAM,cAAc,GACtB,gBAAgB,GAChB,mBAAmB,GACnB,wBAAwB,GACxB,mBAAmB,GACnB,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE5E,qCAAqC;AACrC,MAAM,MAAM,cAAc,GACtB,gBAAgB,GAChB,mBAAmB,GACnB,wBAAwB,GACxB,mBAAmB,GACnB,cAAc,GACd,aAAa,CAAC;AAElB;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,gEAAgE;IAChE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,mDAAmD;IACnD,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,8CAA8C;IAC9C,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACzD;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,0EAA0E;IAC1E,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1C;AAMD,cAAc,YAAY,CAAC;AAE3B,cAAc,mBAAmB,CAAC;AAElC,cAAc,gBAAgB,CAAC;AAE/B,cAAc,aAAa,CAAC;AAG5B,cAAc,qBAAqB,CAAC"}
|
package/dist/audit/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audit/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA0CH,iFAAiF;AACjF,2EAA2E;AAC3E,yEAAyE;AACzE,0FAA0F;AAC1F,cAAc,YAAY,CAAC;AAC3B,qFAAqF;AACrF,cAAc,mBAAmB,CAAC;AAClC,wEAAwE;AACxE,cAAc,gBAAgB,CAAC;AAC/B,+EAA+E;AAC/E,cAAc,aAAa,CAAC;AAC5B,qFAAqF;AACrF,4DAA4D;AAC5D,cAAc,qBAAqB,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,qBAAqB,CAAC;AAKpC,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,qBAAqB,CAAC;AAKpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -47,4 +47,5 @@ export * from './metadata/index.js';
|
|
|
47
47
|
// prior-layer seam changed): admin ports + query/command types + §11.3 safeguard
|
|
48
48
|
// signatures + the query/command service factories + command-pipeline order.
|
|
49
49
|
export * from './admin/index.js';
|
|
50
|
+
export * from './storage/index.js';
|
|
50
51
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,kEAAkE;AAClE,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAEnC,mFAAmF;AACnF,cAAc,qBAAqB,CAAC;AAEpC,4EAA4E;AAC5E,iFAAiF;AACjF,6EAA6E;AAC7E,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,kEAAkE;AAClE,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AAEnC,mFAAmF;AACnF,cAAc,qBAAqB,CAAC;AAEpC,4EAA4E;AAC5E,iFAAiF;AACjF,6EAA6E;AAC7E,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure file-ingest check (SPEC docs/features/file-storage/SPEC.md §5 D, §10, §13): decides
|
|
3
|
+
* whether one uploaded file is acceptable for a named {@link FileProfile} and, if so, returns the
|
|
4
|
+
* server-built metadata (`storageKey`, `sha256`, canonical `contentType`, `sizeBytes`, sanitised
|
|
5
|
+
* `originalName`) the storage layer writes. R-8 factory: `createFileIngest(deps)` over injected
|
|
6
|
+
* ports (`ByteDigest`, `newId`) and the profile table.
|
|
7
|
+
*
|
|
8
|
+
* ⛔ CLIENT INPUT IS NEVER TRUSTED: the declared MIME type only has to be IN the allowlist; the
|
|
9
|
+
* returned `contentType` is the profile's own spelling. The bytes must pass the profile's
|
|
10
|
+
* `magic` check. No part of the storage key comes from the request: it is
|
|
11
|
+
* `t/<tenantId>/<newId()>`, and `tenantId` (from the session) is re-checked as a canonical uuid.
|
|
12
|
+
* ⛔ NO LEAKAGE: every rejection is a {@link FileRejectedError} with a FIXED reason code; messages
|
|
13
|
+
* never echo the file name, the declared type or the bytes.
|
|
14
|
+
* ⛔ CHEAP CHECKS FIRST: size is checked before any parsing or hashing; an id is drawn only for an
|
|
15
|
+
* accepted file.
|
|
16
|
+
* ⛔ CEILING AT BOOT: a profile above {@link FILE_MAX_BYTES_CEILING} throws at construction.
|
|
17
|
+
*
|
|
18
|
+
* Pure: no IO, no clock, no randomness. ADR-0003: framework-agnostic — no framework / node imports.
|
|
19
|
+
*/
|
|
20
|
+
import type { ByteDigest } from './ports.js';
|
|
21
|
+
/** Hard upper bound for any profile's `maxBytes` (50 MiB). No profile, route or env value can exceed it. */
|
|
22
|
+
export declare const FILE_MAX_BYTES_CEILING: number;
|
|
23
|
+
/**
|
|
24
|
+
* The fixed rejection reasons (SPEC §10). `createFileIngest` emits the first four; the multipart
|
|
25
|
+
* intake (guard `FileIntake`) emits `too_many_files` and `missing_file`.
|
|
26
|
+
*/
|
|
27
|
+
export type FileRejectionReason = 'empty' | 'too_large' | 'type_not_allowed' | 'content_mismatch' | 'too_many_files' | 'missing_file';
|
|
28
|
+
/** A file was refused. The message is fixed per reason: no name, type or bytes are ever included. */
|
|
29
|
+
export declare class FileRejectedError extends Error {
|
|
30
|
+
readonly reason: FileRejectionReason;
|
|
31
|
+
constructor(reason: FileRejectionReason);
|
|
32
|
+
}
|
|
33
|
+
/** What an upload of one kind may be. Every app-added profile MUST supply its own `magic` check. */
|
|
34
|
+
export interface FileProfile {
|
|
35
|
+
/** Per-file byte cap, a positive integer at most {@link FILE_MAX_BYTES_CEILING}. */
|
|
36
|
+
readonly maxBytes: number;
|
|
37
|
+
/** Allowed declared content types (compared case-insensitively). The first match is returned as canonical. */
|
|
38
|
+
readonly contentTypes: readonly string[];
|
|
39
|
+
/** Allowed file-name extensions, with the leading dot (compared case-insensitively). */
|
|
40
|
+
readonly extensions: readonly string[];
|
|
41
|
+
/** Content check over the raw bytes. `false` (or a throw) rejects with `content_mismatch`. */
|
|
42
|
+
readonly magic: (bytes: Uint8Array) => boolean;
|
|
43
|
+
}
|
|
44
|
+
/** Ports and configuration for {@link createFileIngest}. */
|
|
45
|
+
export interface FileIngestDeps {
|
|
46
|
+
readonly digest: ByteDigest;
|
|
47
|
+
/** Server-side id for the storage key's last segment (e.g. a uuid v4). */
|
|
48
|
+
readonly newId: () => string;
|
|
49
|
+
/** Profiles by name; a request names one (e.g. `'xlsx'`). */
|
|
50
|
+
readonly profiles: Readonly<Record<string, FileProfile>>;
|
|
51
|
+
}
|
|
52
|
+
/** One file to check. `tenantId` comes from the session; everything else is client-supplied. */
|
|
53
|
+
export interface FileIngestInput {
|
|
54
|
+
readonly tenantId: string;
|
|
55
|
+
/** Name of a profile in `deps.profiles` (server-chosen by the route). */
|
|
56
|
+
readonly profile: string;
|
|
57
|
+
readonly bytes: Uint8Array;
|
|
58
|
+
readonly declaredContentType: string;
|
|
59
|
+
readonly originalName: string;
|
|
60
|
+
}
|
|
61
|
+
/** The accepted file's server-built metadata. */
|
|
62
|
+
export interface IngestedFile {
|
|
63
|
+
/** `t/<tenantId>/<newId()>`; never contains client input. */
|
|
64
|
+
readonly storageKey: string;
|
|
65
|
+
/** Lower-case hex sha256 of the exact bytes. */
|
|
66
|
+
readonly sha256: string;
|
|
67
|
+
/** Canonical content type from the profile allowlist, never the client header. */
|
|
68
|
+
readonly contentType: string;
|
|
69
|
+
readonly sizeBytes: number;
|
|
70
|
+
/** Sanitised display name (basename, no control characters, NFC, at most 255 code points). */
|
|
71
|
+
readonly originalName: string;
|
|
72
|
+
}
|
|
73
|
+
export interface FileIngest {
|
|
74
|
+
/** Check one file; resolves to its metadata or rejects with {@link FileRejectedError}. */
|
|
75
|
+
ingest(input: FileIngestInput): Promise<IngestedFile>;
|
|
76
|
+
/**
|
|
77
|
+
* The per-file byte cap of a named profile, or `undefined` when the profile is unknown. Lets a
|
|
78
|
+
* transport (guard `FileIntake`) size its parser limit from the configured cap. Optional so a
|
|
79
|
+
* hand-written `FileIngest` stays valid; `createFileIngest` always provides it.
|
|
80
|
+
*/
|
|
81
|
+
maxBytesFor?(profile: string): number | undefined;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Build the ingest check. Validates every profile now (boot): a cap that is not a positive
|
|
85
|
+
* integer or exceeds {@link FILE_MAX_BYTES_CEILING} throws here, never at request time.
|
|
86
|
+
*/
|
|
87
|
+
export declare function createFileIngest(deps: FileIngestDeps): FileIngest;
|
|
88
|
+
/** The `.xlsx` profile: 10 MiB default cap, the spreadsheetml content type, OOXML content check. */
|
|
89
|
+
export declare const XLSX_PROFILE: FileProfile;
|
|
90
|
+
//# sourceMappingURL=file-ingest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-ingest.d.ts","sourceRoot":"","sources":["../../src/storage/file-ingest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,4GAA4G;AAC5G,eAAO,MAAM,sBAAsB,QAAmB,CAAC;AAKvD;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAC3B,OAAO,GACP,WAAW,GACX,kBAAkB,GAClB,kBAAkB,GAClB,gBAAgB,GAChB,cAAc,CAAC;AAEnB,qGAAqG;AACrG,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;gBACzB,MAAM,EAAE,mBAAmB;CAKxC;AAED,oGAAoG;AACpG,MAAM,WAAW,WAAW;IAC1B,oFAAoF;IACpF,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,8GAA8G;IAC9G,QAAQ,CAAC,YAAY,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC,wFAAwF;IACxF,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC,8FAA8F;IAC9F,QAAQ,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,OAAO,CAAC;CAChD;AAED,4DAA4D;AAC5D,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,0EAA0E;IAC1E,QAAQ,CAAC,KAAK,EAAE,MAAM,MAAM,CAAC;IAC7B,6DAA6D;IAC7D,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;CAC1D;AAED,gGAAgG;AAChG,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,yEAAyE;IACzE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,mBAAmB,EAAE,MAAM,CAAC;IACrC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC/B;AAED,iDAAiD;AACjD,MAAM,WAAW,YAAY;IAC3B,6DAA6D;IAC7D,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,gDAAgD;IAChD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,kFAAkF;IAClF,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,8FAA8F;IAC9F,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,UAAU;IACzB,0FAA0F;IAC1F,MAAM,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IACtD;;;;OAIG;IACH,WAAW,CAAC,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACnD;AAiGD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,cAAc,GAAG,UAAU,CAsEjE;AAoHD,oGAAoG;AACpG,eAAO,MAAM,YAAY,EAAE,WAKzB,CAAC"}
|
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure file-ingest check (SPEC docs/features/file-storage/SPEC.md §5 D, §10, §13): decides
|
|
3
|
+
* whether one uploaded file is acceptable for a named {@link FileProfile} and, if so, returns the
|
|
4
|
+
* server-built metadata (`storageKey`, `sha256`, canonical `contentType`, `sizeBytes`, sanitised
|
|
5
|
+
* `originalName`) the storage layer writes. R-8 factory: `createFileIngest(deps)` over injected
|
|
6
|
+
* ports (`ByteDigest`, `newId`) and the profile table.
|
|
7
|
+
*
|
|
8
|
+
* ⛔ CLIENT INPUT IS NEVER TRUSTED: the declared MIME type only has to be IN the allowlist; the
|
|
9
|
+
* returned `contentType` is the profile's own spelling. The bytes must pass the profile's
|
|
10
|
+
* `magic` check. No part of the storage key comes from the request: it is
|
|
11
|
+
* `t/<tenantId>/<newId()>`, and `tenantId` (from the session) is re-checked as a canonical uuid.
|
|
12
|
+
* ⛔ NO LEAKAGE: every rejection is a {@link FileRejectedError} with a FIXED reason code; messages
|
|
13
|
+
* never echo the file name, the declared type or the bytes.
|
|
14
|
+
* ⛔ CHEAP CHECKS FIRST: size is checked before any parsing or hashing; an id is drawn only for an
|
|
15
|
+
* accepted file.
|
|
16
|
+
* ⛔ CEILING AT BOOT: a profile above {@link FILE_MAX_BYTES_CEILING} throws at construction.
|
|
17
|
+
*
|
|
18
|
+
* Pure: no IO, no clock, no randomness. ADR-0003: framework-agnostic — no framework / node imports.
|
|
19
|
+
*/
|
|
20
|
+
/** Hard upper bound for any profile's `maxBytes` (50 MiB). No profile, route or env value can exceed it. */
|
|
21
|
+
export const FILE_MAX_BYTES_CEILING = 50 * 1024 * 1024;
|
|
22
|
+
/** Default per-file cap of the shipped xlsx profile (10 MiB). */
|
|
23
|
+
const DEFAULT_MAX_BYTES = 10 * 1024 * 1024;
|
|
24
|
+
/** A file was refused. The message is fixed per reason: no name, type or bytes are ever included. */
|
|
25
|
+
export class FileRejectedError extends Error {
|
|
26
|
+
reason;
|
|
27
|
+
constructor(reason) {
|
|
28
|
+
super(`file rejected: ${reason}`);
|
|
29
|
+
this.name = 'FileRejectedError';
|
|
30
|
+
this.reason = reason;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// ---------------------------------------------------------------------------------------------
|
|
34
|
+
// Name sanitisation
|
|
35
|
+
// ---------------------------------------------------------------------------------------------
|
|
36
|
+
const MAX_NAME_CODE_POINTS = 255;
|
|
37
|
+
// C0, DEL, C1, soft hyphen, zero-width and line/paragraph separators, BOM, the bidi marks /
|
|
38
|
+
// embedding / override / isolate characters, and the Unicode tag characters U+E0000-U+E007F
|
|
39
|
+
// (invisible or display-spoofing names).
|
|
40
|
+
const NAME_CONTROL_RE =
|
|
41
|
+
// eslint-disable-next-line no-control-regex
|
|
42
|
+
/[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u200b-\u200f\u2028-\u202e\u2066-\u2069\ufeff\u{e0000}-\u{e007f}]/gu;
|
|
43
|
+
// A UTF-16 surrogate without its partner (not well-formed text; would break NFC and storage).
|
|
44
|
+
const LONE_SURROGATE_RE = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g;
|
|
45
|
+
/**
|
|
46
|
+
* Basename only: lone surrogates and control characters removed, NFC, directory parts and `..`
|
|
47
|
+
* dropped, leading/trailing whitespace trimmed (so `'a.xlsx '` keeps its extension). May be ''.
|
|
48
|
+
*/
|
|
49
|
+
function sanitizeName(raw) {
|
|
50
|
+
if (typeof raw !== 'string')
|
|
51
|
+
return '';
|
|
52
|
+
const cleaned = raw.replace(LONE_SURROGATE_RE, '').replace(NAME_CONTROL_RE, '').normalize('NFC');
|
|
53
|
+
const parts = cleaned.split(/[\\/]/);
|
|
54
|
+
const base = (parts[parts.length - 1] ?? '').replace(/\.{2,}/g, '.').trim();
|
|
55
|
+
return base === '.' ? '' : base;
|
|
56
|
+
}
|
|
57
|
+
/** The lower-cased extension (with the dot) of `name`, or '' when it has none or no stem. */
|
|
58
|
+
function extensionOf(name) {
|
|
59
|
+
const dot = name.lastIndexOf('.');
|
|
60
|
+
return dot > 0 ? name.slice(dot).toLowerCase() : '';
|
|
61
|
+
}
|
|
62
|
+
/** Cap at 255 code points, keeping the extension. */
|
|
63
|
+
function capName(name) {
|
|
64
|
+
const cps = Array.from(name);
|
|
65
|
+
if (cps.length <= MAX_NAME_CODE_POINTS)
|
|
66
|
+
return name;
|
|
67
|
+
const dot = name.lastIndexOf('.');
|
|
68
|
+
const ext = dot > 0 ? Array.from(name.slice(dot)) : [];
|
|
69
|
+
const stem = Array.from(dot > 0 ? name.slice(0, dot) : name);
|
|
70
|
+
return stem.slice(0, MAX_NAME_CODE_POINTS - ext.length).join('') + ext.join('');
|
|
71
|
+
}
|
|
72
|
+
// ---------------------------------------------------------------------------------------------
|
|
73
|
+
// Profile validation and the factory
|
|
74
|
+
// ---------------------------------------------------------------------------------------------
|
|
75
|
+
// Canonical (lower-case, 8-4-4-4-12 ASCII hex) uuid only: no trimming, braces or case folding.
|
|
76
|
+
const CANONICAL_UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
77
|
+
const SHA256_HEX_RE = /^[0-9a-f]{64}$/;
|
|
78
|
+
// The id segment of the key: a path-safe token (no '/', '.', whitespace).
|
|
79
|
+
const ID_SEGMENT_RE = /^[A-Za-z0-9_-]{1,128}$/;
|
|
80
|
+
function resolveProfile(name, p) {
|
|
81
|
+
const bad = (what) => new Error(`file profile '${name}': ${what}`);
|
|
82
|
+
if (typeof p !== 'object' || p === null)
|
|
83
|
+
throw bad('not an object');
|
|
84
|
+
if (!Number.isInteger(p.maxBytes) || p.maxBytes <= 0) {
|
|
85
|
+
throw bad('maxBytes must be a positive integer');
|
|
86
|
+
}
|
|
87
|
+
if (p.maxBytes > FILE_MAX_BYTES_CEILING) {
|
|
88
|
+
throw bad(`maxBytes exceeds the ${FILE_MAX_BYTES_CEILING}-byte ceiling`);
|
|
89
|
+
}
|
|
90
|
+
if (!Array.isArray(p.contentTypes) || p.contentTypes.length === 0) {
|
|
91
|
+
throw bad('contentTypes must be a non-empty list');
|
|
92
|
+
}
|
|
93
|
+
if (!Array.isArray(p.extensions) || p.extensions.length === 0) {
|
|
94
|
+
throw bad('extensions must be a non-empty list');
|
|
95
|
+
}
|
|
96
|
+
for (const t of p.contentTypes) {
|
|
97
|
+
if (typeof t !== 'string' || t.length === 0)
|
|
98
|
+
throw bad('invalid content type');
|
|
99
|
+
}
|
|
100
|
+
for (const e of p.extensions) {
|
|
101
|
+
if (typeof e !== 'string' || !/^\.[A-Za-z0-9]+$/.test(e))
|
|
102
|
+
throw bad('invalid extension');
|
|
103
|
+
}
|
|
104
|
+
if (typeof p.magic !== 'function')
|
|
105
|
+
throw bad('magic must be a function');
|
|
106
|
+
return Object.freeze({
|
|
107
|
+
maxBytes: p.maxBytes,
|
|
108
|
+
contentTypes: Object.freeze([...p.contentTypes]),
|
|
109
|
+
contentTypesLower: Object.freeze(p.contentTypes.map((t) => t.toLowerCase())),
|
|
110
|
+
extensionsLower: Object.freeze(p.extensions.map((e) => e.toLowerCase())),
|
|
111
|
+
fallbackName: `upload${p.extensions[0].toLowerCase()}`,
|
|
112
|
+
magic: p.magic,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Build the ingest check. Validates every profile now (boot): a cap that is not a positive
|
|
117
|
+
* integer or exceeds {@link FILE_MAX_BYTES_CEILING} throws here, never at request time.
|
|
118
|
+
*/
|
|
119
|
+
export function createFileIngest(deps) {
|
|
120
|
+
const { digest, newId } = deps;
|
|
121
|
+
if (typeof digest?.sha256 !== 'function')
|
|
122
|
+
throw new Error('createFileIngest: digest port required');
|
|
123
|
+
if (typeof newId !== 'function')
|
|
124
|
+
throw new Error('createFileIngest: newId port required');
|
|
125
|
+
const profiles = new Map();
|
|
126
|
+
for (const [name, p] of Object.entries(deps.profiles ?? {})) {
|
|
127
|
+
profiles.set(name, resolveProfile(name, p));
|
|
128
|
+
}
|
|
129
|
+
async function ingest(input) {
|
|
130
|
+
// Server-side configuration and session facts first: fail closed, not a file rejection.
|
|
131
|
+
const profile = typeof input.profile === 'string' ? profiles.get(input.profile) : undefined;
|
|
132
|
+
if (!profile)
|
|
133
|
+
throw new Error('file ingest: unknown file profile');
|
|
134
|
+
if (typeof input.tenantId !== 'string' || !CANONICAL_UUID_RE.test(input.tenantId)) {
|
|
135
|
+
throw new Error('file ingest: tenant id is not a canonical uuid');
|
|
136
|
+
}
|
|
137
|
+
const tenantId = input.tenantId;
|
|
138
|
+
const bytes = input.bytes;
|
|
139
|
+
if (!(bytes instanceof Uint8Array) || bytes.length === 0) {
|
|
140
|
+
throw new FileRejectedError('empty');
|
|
141
|
+
}
|
|
142
|
+
if (bytes.length > profile.maxBytes)
|
|
143
|
+
throw new FileRejectedError('too_large');
|
|
144
|
+
const sanitized = sanitizeName(input.originalName);
|
|
145
|
+
const name = sanitized === '' ? profile.fallbackName : sanitized;
|
|
146
|
+
if (!profile.extensionsLower.includes(extensionOf(name))) {
|
|
147
|
+
throw new FileRejectedError('type_not_allowed');
|
|
148
|
+
}
|
|
149
|
+
const declared = typeof input.declaredContentType === 'string' ? input.declaredContentType.toLowerCase() : '';
|
|
150
|
+
const typeIndex = profile.contentTypesLower.indexOf(declared);
|
|
151
|
+
if (typeIndex < 0)
|
|
152
|
+
throw new FileRejectedError('type_not_allowed');
|
|
153
|
+
const contentType = profile.contentTypes[typeIndex];
|
|
154
|
+
let magicOk = false;
|
|
155
|
+
try {
|
|
156
|
+
magicOk = profile.magic(bytes) === true;
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
magicOk = false;
|
|
160
|
+
}
|
|
161
|
+
if (!magicOk)
|
|
162
|
+
throw new FileRejectedError('content_mismatch');
|
|
163
|
+
const sha256 = await digest.sha256(bytes);
|
|
164
|
+
if (typeof sha256 !== 'string' || !SHA256_HEX_RE.test(sha256)) {
|
|
165
|
+
throw new Error('file ingest: digest port returned an invalid sha256');
|
|
166
|
+
}
|
|
167
|
+
const id = newId();
|
|
168
|
+
if (typeof id !== 'string' || !ID_SEGMENT_RE.test(id)) {
|
|
169
|
+
throw new Error('file ingest: newId returned an unsafe key segment');
|
|
170
|
+
}
|
|
171
|
+
return Object.freeze({
|
|
172
|
+
storageKey: `t/${tenantId}/${id}`,
|
|
173
|
+
sha256,
|
|
174
|
+
contentType,
|
|
175
|
+
sizeBytes: bytes.length,
|
|
176
|
+
originalName: capName(name),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
function maxBytesFor(profile) {
|
|
180
|
+
return typeof profile === 'string' ? profiles.get(profile)?.maxBytes : undefined;
|
|
181
|
+
}
|
|
182
|
+
return Object.freeze({ ingest, maxBytesFor });
|
|
183
|
+
}
|
|
184
|
+
// ---------------------------------------------------------------------------------------------
|
|
185
|
+
// XLSX content check: ZIP + OOXML [Content_Types].xml naming a spreadsheetml main part.
|
|
186
|
+
// Dependency-free: central-directory walk plus a bounded raw-DEFLATE decoder (RFC 1951).
|
|
187
|
+
// ---------------------------------------------------------------------------------------------
|
|
188
|
+
const XLSX_CONTENT_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
189
|
+
const XLSX_MAIN_PART_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml';
|
|
190
|
+
const CONTENT_TYPES_ENTRY = '[content_types].xml';
|
|
191
|
+
/** `[Content_Types].xml` is small; a larger (decompressed) one is refused (zip-bomb bound). */
|
|
192
|
+
const MAX_CONTENT_TYPES_BYTES = 1024 * 1024;
|
|
193
|
+
const SIG_LOCAL = 0x04034b50;
|
|
194
|
+
const SIG_CENTRAL = 0x02014b50;
|
|
195
|
+
const SIG_EOCD = 0x06054b50;
|
|
196
|
+
function u16(b, o) {
|
|
197
|
+
if (o < 0 || o + 2 > b.length)
|
|
198
|
+
throw new RangeError('zip: out of bounds');
|
|
199
|
+
return b[o] | (b[o + 1] << 8);
|
|
200
|
+
}
|
|
201
|
+
function u32(b, o) {
|
|
202
|
+
if (o < 0 || o + 4 > b.length)
|
|
203
|
+
throw new RangeError('zip: out of bounds');
|
|
204
|
+
return (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0;
|
|
205
|
+
}
|
|
206
|
+
/** ASCII-lower-case of a byte range (non-ASCII bytes become '?'). */
|
|
207
|
+
function asciiLower(b, start, end) {
|
|
208
|
+
let s = '';
|
|
209
|
+
for (let i = start; i < end; i++) {
|
|
210
|
+
const c = b[i];
|
|
211
|
+
s += c < 0x80 ? String.fromCharCode(c >= 0x41 && c <= 0x5a ? c + 32 : c) : '?';
|
|
212
|
+
}
|
|
213
|
+
return s;
|
|
214
|
+
}
|
|
215
|
+
/** The decompressed `[Content_Types].xml`, or `null` when the bytes are not a well-formed ZIP holding exactly one. */
|
|
216
|
+
function readContentTypesEntry(b) {
|
|
217
|
+
if (b.length < 30 || u32(b, 0) !== SIG_LOCAL)
|
|
218
|
+
return null;
|
|
219
|
+
// End of central directory: the last 22 bytes plus at most a 65535-byte comment.
|
|
220
|
+
let eocd = -1;
|
|
221
|
+
for (let o = b.length - 22; o >= Math.max(0, b.length - 22 - 0xffff); o--) {
|
|
222
|
+
if (u32(b, o) === SIG_EOCD) {
|
|
223
|
+
eocd = o;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (eocd < 0)
|
|
228
|
+
return null;
|
|
229
|
+
const count = u16(b, eocd + 10);
|
|
230
|
+
const cdSize = u32(b, eocd + 12);
|
|
231
|
+
const cdOffset = u32(b, eocd + 16);
|
|
232
|
+
if (count === 0xffff || cdSize === 0xffffffff || cdOffset === 0xffffffff)
|
|
233
|
+
return null; // zip64
|
|
234
|
+
if (cdOffset + cdSize > eocd)
|
|
235
|
+
return null;
|
|
236
|
+
let found = null;
|
|
237
|
+
let o = cdOffset;
|
|
238
|
+
for (let i = 0; i < count; i++) {
|
|
239
|
+
if (u32(b, o) !== SIG_CENTRAL)
|
|
240
|
+
return null;
|
|
241
|
+
const flags = u16(b, o + 8);
|
|
242
|
+
const method = u16(b, o + 10);
|
|
243
|
+
const compSize = u32(b, o + 20);
|
|
244
|
+
const uncompSize = u32(b, o + 24);
|
|
245
|
+
const nameLen = u16(b, o + 28);
|
|
246
|
+
const extraLen = u16(b, o + 30);
|
|
247
|
+
const commentLen = u16(b, o + 32);
|
|
248
|
+
const local = u32(b, o + 42);
|
|
249
|
+
if (o + 46 + nameLen > b.length)
|
|
250
|
+
return null;
|
|
251
|
+
if (nameLen === CONTENT_TYPES_ENTRY.length) {
|
|
252
|
+
if (asciiLower(b, o + 46, o + 46 + nameLen) === CONTENT_TYPES_ENTRY) {
|
|
253
|
+
if (found)
|
|
254
|
+
return null; // duplicate entry: ambiguous, refuse
|
|
255
|
+
if (flags & 0x1)
|
|
256
|
+
return null; // encrypted
|
|
257
|
+
found = { method, compSize, uncompSize, local };
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
o += 46 + nameLen + extraLen + commentLen;
|
|
261
|
+
}
|
|
262
|
+
if (!found)
|
|
263
|
+
return null;
|
|
264
|
+
if (u32(b, found.local) !== SIG_LOCAL)
|
|
265
|
+
return null;
|
|
266
|
+
const dataStart = found.local + 30 + u16(b, found.local + 26) + u16(b, found.local + 28);
|
|
267
|
+
const dataEnd = dataStart + found.compSize;
|
|
268
|
+
if (dataEnd > b.length)
|
|
269
|
+
return null;
|
|
270
|
+
if (found.method === 0) {
|
|
271
|
+
if (found.compSize > MAX_CONTENT_TYPES_BYTES)
|
|
272
|
+
return null;
|
|
273
|
+
return b.subarray(dataStart, dataEnd);
|
|
274
|
+
}
|
|
275
|
+
if (found.method === 8)
|
|
276
|
+
return inflateRaw(b, dataStart, dataEnd, MAX_CONTENT_TYPES_BYTES);
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
/** True when `[Content_Types].xml` has an `<Override>` whose ContentType is the spreadsheetml main part. */
|
|
280
|
+
function namesSpreadsheetMainPart(xml) {
|
|
281
|
+
const start = xml.length >= 3 && xml[0] === 0xef && xml[1] === 0xbb && xml[2] === 0xbf ? 3 : 0;
|
|
282
|
+
const text = asciiLower(xml, start, xml.length).replace(/<!--[\s\S]*?-->/g, '');
|
|
283
|
+
const overrides = text.match(/<override\b[^>]*>/g) ?? [];
|
|
284
|
+
for (const el of overrides) {
|
|
285
|
+
const m = /\scontenttype\s*=\s*(?:"([^"]*)"|'([^']*)')/.exec(el);
|
|
286
|
+
const value = (m?.[1] ?? m?.[2] ?? '').trim();
|
|
287
|
+
if (value === XLSX_MAIN_PART_TYPE)
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
/** The shipped xlsx content check: a ZIP whose `[Content_Types].xml` names a spreadsheetml main part. */
|
|
293
|
+
function isXlsx(bytes) {
|
|
294
|
+
try {
|
|
295
|
+
const ct = readContentTypesEntry(bytes);
|
|
296
|
+
return ct !== null && namesSpreadsheetMainPart(ct);
|
|
297
|
+
}
|
|
298
|
+
catch {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/** The `.xlsx` profile: 10 MiB default cap, the spreadsheetml content type, OOXML content check. */
|
|
303
|
+
export const XLSX_PROFILE = Object.freeze({
|
|
304
|
+
maxBytes: DEFAULT_MAX_BYTES,
|
|
305
|
+
contentTypes: Object.freeze([XLSX_CONTENT_TYPE]),
|
|
306
|
+
extensions: Object.freeze(['.xlsx']),
|
|
307
|
+
magic: isXlsx,
|
|
308
|
+
});
|
|
309
|
+
// ---------------------------------------------------------------------------------------------
|
|
310
|
+
// Bounded raw DEFLATE decoder (RFC 1951). Returns null on any malformed input or when the output
|
|
311
|
+
// would exceed `maxOut`. Only used to read `[Content_Types].xml`.
|
|
312
|
+
// ---------------------------------------------------------------------------------------------
|
|
313
|
+
const MAXBITS = 15;
|
|
314
|
+
const LEN_BASE = [
|
|
315
|
+
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131,
|
|
316
|
+
163, 195, 227, 258,
|
|
317
|
+
];
|
|
318
|
+
const LEN_EXTRA = [
|
|
319
|
+
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0,
|
|
320
|
+
];
|
|
321
|
+
const DIST_BASE = [
|
|
322
|
+
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049,
|
|
323
|
+
3073, 4097, 6145, 8193, 12289, 16385, 24577,
|
|
324
|
+
];
|
|
325
|
+
const DIST_EXTRA = [
|
|
326
|
+
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
|
|
327
|
+
];
|
|
328
|
+
const CL_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
|
|
329
|
+
class InflateError extends Error {
|
|
330
|
+
}
|
|
331
|
+
function buildHuffman(lengths, n) {
|
|
332
|
+
const count = new Uint16Array(MAXBITS + 1);
|
|
333
|
+
const symbol = new Uint16Array(n);
|
|
334
|
+
for (let s = 0; s < n; s++)
|
|
335
|
+
count[lengths[s]]++;
|
|
336
|
+
if (count[0] === n)
|
|
337
|
+
return { count, symbol };
|
|
338
|
+
let left = 1;
|
|
339
|
+
for (let len = 1; len <= MAXBITS; len++) {
|
|
340
|
+
left <<= 1;
|
|
341
|
+
left -= count[len];
|
|
342
|
+
if (left < 0)
|
|
343
|
+
throw new InflateError('over-subscribed');
|
|
344
|
+
}
|
|
345
|
+
const offs = new Uint16Array(MAXBITS + 1);
|
|
346
|
+
for (let len = 1; len < MAXBITS; len++)
|
|
347
|
+
offs[len + 1] = offs[len] + count[len];
|
|
348
|
+
for (let s = 0; s < n; s++) {
|
|
349
|
+
const l = lengths[s];
|
|
350
|
+
if (l !== 0)
|
|
351
|
+
symbol[offs[l]++] = s;
|
|
352
|
+
}
|
|
353
|
+
return { count, symbol };
|
|
354
|
+
}
|
|
355
|
+
function inflateRaw(src, start, end, maxOut) {
|
|
356
|
+
let pos = start;
|
|
357
|
+
let bitBuf = 0;
|
|
358
|
+
let bitCnt = 0;
|
|
359
|
+
let out = new Uint8Array(Math.min(maxOut, 4096));
|
|
360
|
+
let outLen = 0;
|
|
361
|
+
const bits = (need) => {
|
|
362
|
+
let val = bitBuf;
|
|
363
|
+
while (bitCnt < need) {
|
|
364
|
+
if (pos >= end)
|
|
365
|
+
throw new InflateError('eof');
|
|
366
|
+
val |= src[pos++] << bitCnt;
|
|
367
|
+
bitCnt += 8;
|
|
368
|
+
}
|
|
369
|
+
bitBuf = val >>> need;
|
|
370
|
+
bitCnt -= need;
|
|
371
|
+
return val & ((1 << need) - 1);
|
|
372
|
+
};
|
|
373
|
+
const emit = (byte) => {
|
|
374
|
+
if (outLen >= maxOut)
|
|
375
|
+
throw new InflateError('output cap');
|
|
376
|
+
if (outLen === out.length) {
|
|
377
|
+
const grown = new Uint8Array(Math.min(maxOut, out.length * 2));
|
|
378
|
+
grown.set(out);
|
|
379
|
+
out = grown;
|
|
380
|
+
}
|
|
381
|
+
out[outLen++] = byte;
|
|
382
|
+
};
|
|
383
|
+
const decode = (h) => {
|
|
384
|
+
let code = 0;
|
|
385
|
+
let first = 0;
|
|
386
|
+
let index = 0;
|
|
387
|
+
for (let len = 1; len <= MAXBITS; len++) {
|
|
388
|
+
code |= bits(1);
|
|
389
|
+
const c = h.count[len];
|
|
390
|
+
if (code - c < first)
|
|
391
|
+
return h.symbol[index + (code - first)];
|
|
392
|
+
index += c;
|
|
393
|
+
first += c;
|
|
394
|
+
first <<= 1;
|
|
395
|
+
code <<= 1;
|
|
396
|
+
}
|
|
397
|
+
throw new InflateError('bad code');
|
|
398
|
+
};
|
|
399
|
+
const codes = (lencode, distcode) => {
|
|
400
|
+
for (;;) {
|
|
401
|
+
let sym = decode(lencode);
|
|
402
|
+
if (sym < 256) {
|
|
403
|
+
emit(sym);
|
|
404
|
+
}
|
|
405
|
+
else if (sym === 256) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
sym -= 257;
|
|
410
|
+
if (sym >= 29)
|
|
411
|
+
throw new InflateError('bad length');
|
|
412
|
+
const len = LEN_BASE[sym] + bits(LEN_EXTRA[sym]);
|
|
413
|
+
const dsym = decode(distcode);
|
|
414
|
+
if (dsym >= 30)
|
|
415
|
+
throw new InflateError('bad distance');
|
|
416
|
+
const dist = DIST_BASE[dsym] + bits(DIST_EXTRA[dsym]);
|
|
417
|
+
if (dist > outLen)
|
|
418
|
+
throw new InflateError('distance too far');
|
|
419
|
+
for (let i = 0; i < len; i++)
|
|
420
|
+
emit(out[outLen - dist]);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
let fixed = null;
|
|
425
|
+
try {
|
|
426
|
+
let last = 0;
|
|
427
|
+
do {
|
|
428
|
+
last = bits(1);
|
|
429
|
+
const type = bits(2);
|
|
430
|
+
if (type === 0) {
|
|
431
|
+
bitBuf = 0;
|
|
432
|
+
bitCnt = 0;
|
|
433
|
+
if (pos + 4 > end)
|
|
434
|
+
throw new InflateError('eof');
|
|
435
|
+
const len = src[pos] | (src[pos + 1] << 8);
|
|
436
|
+
const nlen = src[pos + 2] | (src[pos + 3] << 8);
|
|
437
|
+
pos += 4;
|
|
438
|
+
if (len !== (~nlen & 0xffff))
|
|
439
|
+
throw new InflateError('bad stored length');
|
|
440
|
+
if (pos + len > end)
|
|
441
|
+
throw new InflateError('eof');
|
|
442
|
+
for (let i = 0; i < len; i++)
|
|
443
|
+
emit(src[pos + i]);
|
|
444
|
+
pos += len;
|
|
445
|
+
}
|
|
446
|
+
else if (type === 1) {
|
|
447
|
+
if (!fixed) {
|
|
448
|
+
const l = new Uint8Array(288);
|
|
449
|
+
l.fill(8, 0, 144);
|
|
450
|
+
l.fill(9, 144, 256);
|
|
451
|
+
l.fill(7, 256, 280);
|
|
452
|
+
l.fill(8, 280, 288);
|
|
453
|
+
fixed = { len: buildHuffman(l, 288), dist: buildHuffman(new Uint8Array(30).fill(5), 30) };
|
|
454
|
+
}
|
|
455
|
+
codes(fixed.len, fixed.dist);
|
|
456
|
+
}
|
|
457
|
+
else if (type === 2) {
|
|
458
|
+
const nlen = bits(5) + 257;
|
|
459
|
+
const ndist = bits(5) + 1;
|
|
460
|
+
const ncode = bits(4) + 4;
|
|
461
|
+
if (nlen > 286 || ndist > 30)
|
|
462
|
+
throw new InflateError('bad counts');
|
|
463
|
+
const cl = new Uint8Array(19);
|
|
464
|
+
for (let i = 0; i < ncode; i++)
|
|
465
|
+
cl[CL_ORDER[i]] = bits(3);
|
|
466
|
+
const clcode = buildHuffman(cl, 19);
|
|
467
|
+
const lengths = new Uint8Array(nlen + ndist);
|
|
468
|
+
let i = 0;
|
|
469
|
+
while (i < nlen + ndist) {
|
|
470
|
+
const sym = decode(clcode);
|
|
471
|
+
if (sym < 16) {
|
|
472
|
+
lengths[i++] = sym;
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
let value = 0;
|
|
476
|
+
let rep;
|
|
477
|
+
if (sym === 16) {
|
|
478
|
+
if (i === 0)
|
|
479
|
+
throw new InflateError('repeat with no previous');
|
|
480
|
+
value = lengths[i - 1];
|
|
481
|
+
rep = 3 + bits(2);
|
|
482
|
+
}
|
|
483
|
+
else if (sym === 17) {
|
|
484
|
+
rep = 3 + bits(3);
|
|
485
|
+
}
|
|
486
|
+
else {
|
|
487
|
+
rep = 11 + bits(7);
|
|
488
|
+
}
|
|
489
|
+
if (i + rep > nlen + ndist)
|
|
490
|
+
throw new InflateError('too many lengths');
|
|
491
|
+
while (rep--)
|
|
492
|
+
lengths[i++] = value;
|
|
493
|
+
}
|
|
494
|
+
if (lengths[256] === 0)
|
|
495
|
+
throw new InflateError('no end-of-block code');
|
|
496
|
+
codes(buildHuffman(lengths.subarray(0, nlen), nlen), buildHuffman(lengths.subarray(nlen), ndist));
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
throw new InflateError('bad block type');
|
|
500
|
+
}
|
|
501
|
+
} while (!last);
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
return out.subarray(0, outLen);
|
|
507
|
+
}
|
|
508
|
+
//# sourceMappingURL=file-ingest.js.map
|