@civitai/app-sdk 0.92.0 → 0.94.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/README.md +3 -1
- package/dist/blocks/types.d.ts +27 -6
- package/dist/index.d.ts +4 -2
- package/dist/index.js +4 -2
- package/dist/manifest/defineBlock.d.ts +1 -0
- package/dist/manifest/defineBlock.js +10 -2
- package/dist/save-model3d/index.d.ts +54 -0
- package/dist/save-model3d/index.js +216 -0
- package/dist/save-zip/index.d.ts +5 -1
- package/dist/save-zip/index.js +7 -1
- package/package.json +5 -1
- package/schemas/app-block/v1.json +26 -2
package/README.md
CHANGED
|
@@ -34,12 +34,13 @@ pnpm add @civitai/app-sdk
|
|
|
34
34
|
| `manifest/*` — `defineBlock`, `SCHEMA_DIVERGENCES`, `KNOWN_GAPS` (**Node only**) | Build-time manifest validation. `defineBlock(config)` compiles the vendored canonical schema with [Ajv](https://ajv.js.org) and validates a `BlockManifestV1` against it, so authoring mistakes surface in `pnpm dev` instead of at `civitai app validate`/submit. It is **derived from the schema, not a hand-written mirror of it** — that mirror is what produced [#330](https://github.com/civitai/civitai-app-starters/issues/330). Needs `node:fs` and the optional peer `ajv`, which is why it is not on the browser-facing `./blocks` surface. |
|
|
35
35
|
| `vite/*` — `blockManifestPlugin` (**Node only**) | A Vite plugin wrapping `defineBlock`, firing from `configResolved` — the one hook Vite calls on both the dev-server and the build path. Every block scaffold registers it, so `pnpm dev`, `pnpm dev:harness` and `pnpm build` all fail on a bad manifest with the offending field path. On the dev server only, it also exposes the manifest's `scopes` + `storage` to `@civitai/sdk` for a dev-tunnel local preview. Optional peers: `ajv` (runtime) and `vite` (types only). |
|
|
36
36
|
| `save-zip/*` — `validateSaveZip`, `verifySaveZipInflate`, `looksLikeSaveZip`, `SAVE_ZIP_MAX_BYTES`, `SAVE_ZIP_LIMITS`, `SAVE_ZIP_ALLOWED_EXTENSIONS`, the `SAVE_ZIP_*_ERROR` strings | The `.zip` validator behind `SAVE_IMAGE { bytes }` — the ONE copy: the civitai.com host runs it before it starts a download, and `@civitai/blocks-react`'s `createMockHost` imports it, so a block's tests accept and refuse the zips production does. Structure (local headers, central directory and EOCD laid end to end, cross-checked), entry names and types, declared sizes, then an inflate check (`DecompressionStream`). The rules an app author needs are in `@civitai/blocks-react`'s README under `useSaveImage()`. Dependency-free, browser and Node. |
|
|
37
|
+
| `save-model3d/*` — `looksLikeSaveModel3d`, `isValidSaveModel3d`, `SAVE_MODEL3D_MAX_BYTES`, `SAVE_MODEL3D_MAX_JSON_CHUNK_BYTES`, `SAVE_MODEL3D_EXTENSION`, `SaveModel3dType` | The binary glTF (`.glb`) / binary FBX (`.fbx`) check behind `SAVE_IMAGE { bytes }` — the ONE copy, shared by the civitai.com host and `createMockHost` like `save-zip`. A prefix test that picks the per-file cap (`SAVE_MODEL3D_MAX_BYTES`), then a strict structural check (a GLB's declared length and chunks fill the buffer exactly; an FBX's top-level records reach the null record with at most a footer after it). The rules an app author needs are in `@civitai/blocks-react`'s README under `useSaveImage()`. Dependency-free, browser and Node. |
|
|
37
38
|
|
|
38
39
|
## Entry points, and what the root barrel is
|
|
39
40
|
|
|
40
41
|
The root — `import … from '@civitai/app-sdk'` — is **exactly the union of four subpaths**: `@civitai/app-sdk/oauth`, `/scopes`, `/cookies` and `/orchestrator`. All of each, and nothing else. Import from the root when you want the OAuth-app surface in one specifier; import a subpath when you want only that slice. The two are the same symbols either way, which is asserted in both directions by `test/export-surface.test.ts` — the root cannot quietly gain a symbol none of those four exports, or miss one they do.
|
|
41
42
|
|
|
42
|
-
The other
|
|
43
|
+
The other seven subpaths are **deliberately not on the root**, each for a concrete cost it would push onto every consumer:
|
|
43
44
|
|
|
44
45
|
| Subpath | Why it is not on the root |
|
|
45
46
|
|---|---|
|
|
@@ -49,6 +50,7 @@ The other six subpaths are **deliberately not on the root**, each for a concrete
|
|
|
49
50
|
| `@civitai/app-sdk/manifest` | **Node only** — needs `node:fs` and the optional peer `ajv`. |
|
|
50
51
|
| `@civitai/app-sdk/vite` | **Node only** — optional peers `ajv` (runtime) and `vite` (types). |
|
|
51
52
|
| `@civitai/app-sdk/save-zip` | The host's `SAVE_IMAGE` zip validator, shared by the civitai.com host and `@civitai/blocks-react`'s `createMockHost`. An OAuth app never needs it. |
|
|
53
|
+
| `@civitai/app-sdk/save-model3d` | The host's `SAVE_IMAGE` binary glTF / FBX check, shared the same way. An OAuth app never needs it. |
|
|
52
54
|
|
|
53
55
|
> One name is declared twice on purpose: `BuzzAccountType` is the full set of Civitai Buzz pools on the root and `/oauth`, and a narrower three-pool union on `/blocks` — a block can neither prefer nor read the others. Same name, two subpaths, two types; the divergence is pinned rather than merged.
|
|
54
56
|
|
package/dist/blocks/types.d.ts
CHANGED
|
@@ -3418,8 +3418,8 @@ export type BlockStorageMigrationOp = {
|
|
|
3418
3418
|
keyword: BlockStorageMigrationKeyword;
|
|
3419
3419
|
};
|
|
3420
3420
|
/**
|
|
3421
|
-
*
|
|
3422
|
-
*
|
|
3421
|
+
* In a limited rollout — civitai.com accepts a new or changed hook only from
|
|
3422
|
+
* authors the rollout includes, and refuses it otherwise. A collection's permission hook: one ES module of the
|
|
3423
3423
|
* bundle whose default export decides, for every write to the collection,
|
|
3424
3424
|
* whether the write is allowed. It runs after the collection `schema` check,
|
|
3425
3425
|
* never instead of it. Mirrors the canonical schema's
|
|
@@ -3454,7 +3454,7 @@ export interface BlockStorageCollectionHook {
|
|
|
3454
3454
|
* A PRIVATE collection — a separate key space per viewer, read and written
|
|
3455
3455
|
* only by that viewer. Carries none of the shared-only fields (`write`,
|
|
3456
3456
|
* `counters`, `marks`, `showAuthor`, `dailyWritesPerAuthor`,
|
|
3457
|
-
* `ownedModelVersions`, `registry`); the canonical
|
|
3457
|
+
* `ownedModelVersions`, `registry`, `keyedBy`); the canonical
|
|
3458
3458
|
* forbids them here. See {@link BlockStorageSharedCollectionDeclaration} for
|
|
3459
3459
|
* the fields both kinds share.
|
|
3460
3460
|
*/
|
|
@@ -3475,7 +3475,7 @@ export interface BlockStoragePrivateCollectionDeclaration {
|
|
|
3475
3475
|
schema?: BlockStorageCollectionSchema;
|
|
3476
3476
|
/** See {@link BlockStorageSharedCollectionDeclaration.migrations}. */
|
|
3477
3477
|
migrations?: BlockStorageMigrationOp[];
|
|
3478
|
-
/**
|
|
3478
|
+
/** Limited rollout — see {@link BlockStorageSharedCollectionDeclaration.hook}. */
|
|
3479
3479
|
hook?: BlockStorageCollectionHook;
|
|
3480
3480
|
}
|
|
3481
3481
|
/**
|
|
@@ -3587,6 +3587,27 @@ export interface BlockStorageSharedCollectionDeclaration {
|
|
|
3587
3587
|
* sees the change at review.
|
|
3588
3588
|
*/
|
|
3589
3589
|
registry?: 'public';
|
|
3590
|
+
/**
|
|
3591
|
+
* Viewer-keyed documents. `write` `'own'` collections only, never with
|
|
3592
|
+
* `kind` `'log'`, and never beside `counters` or `marks` — the canonical
|
|
3593
|
+
* refuses it under `'anyone'`, `'owner'` and `'none'`, on log and private
|
|
3594
|
+
* collections, and with either of those fields.
|
|
3595
|
+
*
|
|
3596
|
+
* The server derives each document's key from the signed-in viewer, so each
|
|
3597
|
+
* viewer has at most one document (`'viewer'`) or at most one per parent key
|
|
3598
|
+
* the app names (`'parent+viewer'`), and no viewer can write another's. The
|
|
3599
|
+
* app reads and writes it at `/:collection/@me` or `/:collection/@me/:parent`,
|
|
3600
|
+
* where a parent is 1 to 96 characters of `A-Z`, `a-z`, `0-9`, `.`, `_` or
|
|
3601
|
+
* `-` (not only dots); a `'parent+viewer'` key is `<parent>:<digest>`, so
|
|
3602
|
+
* listing with `keyPrefix=<parent>:` returns one parent's documents. Writing
|
|
3603
|
+
* such a collection by key, creating in it and publishing into it are
|
|
3604
|
+
* refused.
|
|
3605
|
+
*
|
|
3606
|
+
* Cannot be added, removed or changed on a collection any approved version
|
|
3607
|
+
* declared: declare a new collection instead. That is checked at submit, not
|
|
3608
|
+
* by `defineBlock` — see `KNOWN_GAPS['storage-keyedBy-immutable']`.
|
|
3609
|
+
*/
|
|
3610
|
+
keyedBy?: 'viewer' | 'parent+viewer';
|
|
3590
3611
|
/**
|
|
3591
3612
|
* The schema every document's `data` must match — see {@link BlockStorageCollectionSchema}.
|
|
3592
3613
|
* Not allowed with `write` `'none'` (no document bodies are written there).
|
|
@@ -3599,8 +3620,8 @@ export interface BlockStorageSharedCollectionDeclaration {
|
|
|
3599
3620
|
*/
|
|
3600
3621
|
migrations?: BlockStorageMigrationOp[];
|
|
3601
3622
|
/**
|
|
3602
|
-
*
|
|
3603
|
-
*
|
|
3623
|
+
* In a limited rollout — civitai.com accepts a new or changed hook only from
|
|
3624
|
+
* authors the rollout includes, and refuses it otherwise. A permission hook that decides, for every
|
|
3604
3625
|
* write to this collection, whether it is allowed — see
|
|
3605
3626
|
* {@link BlockStorageCollectionHook}. Allowed on every collection kind,
|
|
3606
3627
|
* private and shared alike.
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* accident rather than by decision. `./oauth` now re-exports `types.ts`, which
|
|
18
18
|
* fixes it additively: the root's surface did not change.
|
|
19
19
|
*
|
|
20
|
-
* ## The
|
|
20
|
+
* ## The seven subpaths deliberately NOT in the root, and why
|
|
21
21
|
*
|
|
22
22
|
* "Reachable two ways" is a cost, but an absent export is a worse one, so the
|
|
23
23
|
* bar for staying out is a concrete harm to the importer. Each of these clears
|
|
@@ -48,9 +48,11 @@
|
|
|
48
48
|
* - **`./save-zip`** — the host's `SAVE_IMAGE` zip validator, the one copy the
|
|
49
49
|
* civitai.com host and `@civitai/blocks-react`'s `createMockHost` share. An
|
|
50
50
|
* OAuth app never saves a zip through a block host.
|
|
51
|
+
* - **`./save-model3d`** — the host's `SAVE_IMAGE` binary glTF / FBX check,
|
|
52
|
+
* shared the same way and for the same reason.
|
|
51
53
|
*
|
|
52
54
|
* The ledger in `test/export-surface.test.ts` carries one of these reasons per
|
|
53
|
-
* `exports` key and fails when the key set GROWS or SHRINKS — so
|
|
55
|
+
* `exports` key and fails when the key set GROWS or SHRINKS — so an eighth
|
|
54
56
|
* subpath cannot arrive without a stated reason to exist, and a deleted one
|
|
55
57
|
* cannot leave a stale reason behind.
|
|
56
58
|
*
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* accident rather than by decision. `./oauth` now re-exports `types.ts`, which
|
|
18
18
|
* fixes it additively: the root's surface did not change.
|
|
19
19
|
*
|
|
20
|
-
* ## The
|
|
20
|
+
* ## The seven subpaths deliberately NOT in the root, and why
|
|
21
21
|
*
|
|
22
22
|
* "Reachable two ways" is a cost, but an absent export is a worse one, so the
|
|
23
23
|
* bar for staying out is a concrete harm to the importer. Each of these clears
|
|
@@ -48,9 +48,11 @@
|
|
|
48
48
|
* - **`./save-zip`** — the host's `SAVE_IMAGE` zip validator, the one copy the
|
|
49
49
|
* civitai.com host and `@civitai/blocks-react`'s `createMockHost` share. An
|
|
50
50
|
* OAuth app never saves a zip through a block host.
|
|
51
|
+
* - **`./save-model3d`** — the host's `SAVE_IMAGE` binary glTF / FBX check,
|
|
52
|
+
* shared the same way and for the same reason.
|
|
51
53
|
*
|
|
52
54
|
* The ledger in `test/export-surface.test.ts` carries one of these reasons per
|
|
53
|
-
* `exports` key and fails when the key set GROWS or SHRINKS — so
|
|
55
|
+
* `exports` key and fails when the key set GROWS or SHRINKS — so an eighth
|
|
54
56
|
* subpath cannot arrive without a stated reason to exist, and a deleted one
|
|
55
57
|
* cannot leave a stale reason behind.
|
|
56
58
|
*
|
|
@@ -99,6 +99,7 @@ export declare const KNOWN_GAPS: {
|
|
|
99
99
|
readonly 'storage-server-only-rules': string;
|
|
100
100
|
readonly 'storage-collection-schema-server-only-rules': string;
|
|
101
101
|
readonly 'storage-migrations-server-only-rules': string;
|
|
102
|
+
readonly 'storage-keyedBy-immutable': string;
|
|
102
103
|
readonly 'format-uri-not-enforced': string;
|
|
103
104
|
};
|
|
104
105
|
/** The canonical schema object, parsed once. Exported for tests and tooling. */
|
|
@@ -149,8 +149,10 @@ export const KNOWN_GAPS = {
|
|
|
149
149
|
'`apps:storage:import` (and `apps:storage`), requesting any of these scopes with nothing for ' +
|
|
150
150
|
'it to reach is rejected, an import never names this app itself and names each source at ' +
|
|
151
151
|
'most once, a counter may not share a name with a mark, and the ' +
|
|
152
|
-
'RESERVED features (`kind` "log", `ttlSeconds`, `dailyWritesPerAuthor`, `registry`,
|
|
153
|
-
'`write` "anyone" / "owner") validate offline and are refused at submit. A
|
|
152
|
+
'RESERVED features (`kind` "log", `ttlSeconds`, `dailyWritesPerAuthor`, `registry`, ' +
|
|
153
|
+
'`write` "anyone" / "owner") validate offline and are refused at submit. A new or changed ' +
|
|
154
|
+
'`hook` is in a limited rollout: it validates offline and is refused at submit for an author ' +
|
|
155
|
+
'the rollout does not include. A `hook`\'s `file` ' +
|
|
154
156
|
'being outside `outputDir`, present in the bundle with its `sha256`, its size, and one file ' +
|
|
155
157
|
'pinned with one digest across collections are checked by the server too. Ajv enforces the ' +
|
|
156
158
|
'structural rules exactly; these are server-side.',
|
|
@@ -171,6 +173,12 @@ export const KNOWN_GAPS = {
|
|
|
171
173
|
'decided there. So are the canonical\'s prose-only rules: a `default` value must match the new ' +
|
|
172
174
|
'schema at its path and be at most 4096 bytes of JSON, and the ops must touch disjoint ' +
|
|
173
175
|
'fields. All of those validate here.',
|
|
176
|
+
'storage-keyedBy-immutable': 'A collection\'s `keyedBy` (viewer-keyed documents): the canonical encodes its values ' +
|
|
177
|
+
'("viewer" / "parent+viewer") and where it may appear — shared `write` "own" collections ' +
|
|
178
|
+
'only, never `kind` "log", never beside `counters` or `marks` — and Ajv enforces those ' +
|
|
179
|
+
'exactly. That it cannot be added, removed or changed on a collection ANY previously ' +
|
|
180
|
+
'approved version declared needs those approved manifests, so it is decided by the server at ' +
|
|
181
|
+
'submit. A change to it validates here.',
|
|
174
182
|
'format-uri-not-enforced': '`assetBundleUrl` carries `format: "uri"` in addition to `pattern: "^https://"`. Ajv ignores ' +
|
|
175
183
|
'unknown formats unless `ajv-formats` is installed, which would be a second optional peer for ' +
|
|
176
184
|
'one field whose pattern is already enforced. The pattern IS enforced; the `uri` format is ' +
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@civitai/app-sdk/save-model3d` — the App Blocks `SAVE_IMAGE { bytes }` 3D model check: binary
|
|
3
|
+
* glTF (`.glb`) and binary FBX (`.fbx`).
|
|
4
|
+
*
|
|
5
|
+
* 🔴 THE ONE COPY, like `./save-zip`. The civitai.com host and `@civitai/blocks-react`'s
|
|
6
|
+
* `createMockHost` both import this module, so the mock accepts and refuses exactly the models
|
|
7
|
+
* production does. Change the rules here, release, and bump the host's `@civitai/app-sdk`.
|
|
8
|
+
*
|
|
9
|
+
* glTF-JSON (`.gltf`), ASCII FBX and OBJ are NOT model types here: they are text, so the host saves
|
|
10
|
+
* them as `.txt` (and a `.gltf` usually references sidecar files a single save could not carry).
|
|
11
|
+
*
|
|
12
|
+
* Two steps, the same split as the zip rules:
|
|
13
|
+
* 1. {@link looksLikeSaveModel3d} — does the buffer START like one of these? Decided on the first
|
|
14
|
+
* 8 (GLB) or 21 (FBX) bytes, so a caller can pick the per-file cap
|
|
15
|
+
* ({@link SAVE_MODEL3D_MAX_BYTES}) before decoding anything. Both prefixes contain a NUL byte,
|
|
16
|
+
* so no text payload can ever be a candidate: refusing a candidate that fails step 2 takes
|
|
17
|
+
* nothing away from a text classifier that refuses every NUL-bearing buffer.
|
|
18
|
+
* 2. {@link isValidSaveModel3d} — the strict structural check. A GLB must account for EVERY byte
|
|
19
|
+
* of the buffer in its declared chunks (nothing can ride along after it); an FBX must have a
|
|
20
|
+
* well-formed run of top-level node records ending in the null record, with no more than a
|
|
21
|
+
* footer's worth of bytes after it.
|
|
22
|
+
*
|
|
23
|
+
* Dependency-free, browser and Node.
|
|
24
|
+
*/
|
|
25
|
+
/** The two 3D model types, each with the one extension (no dot) it saves under. */
|
|
26
|
+
export declare const SAVE_MODEL3D_EXTENSION: {
|
|
27
|
+
readonly 'model/gltf-binary': "glb";
|
|
28
|
+
readonly 'model/x-fbx': "fbx";
|
|
29
|
+
};
|
|
30
|
+
export type SaveModel3dType = keyof typeof SAVE_MODEL3D_EXTENSION;
|
|
31
|
+
/**
|
|
32
|
+
* The per-file cap on a buffer that starts like a model: 100 MiB. Generated meshes with embedded
|
|
33
|
+
* textures outgrow the 50 MiB cap of the other bytes types; the host also checks a model against
|
|
34
|
+
* its 100 MiB window budget, so a model at the cap is the only save that window admits.
|
|
35
|
+
*/
|
|
36
|
+
export declare const SAVE_MODEL3D_MAX_BYTES: number;
|
|
37
|
+
/**
|
|
38
|
+
* The largest JSON chunk a GLB may carry: 8 MiB. The check decodes and parses that chunk on the
|
|
39
|
+
* caller's thread (the host's is the viewer's main thread), and parse cost grows with it: a 49.5 MB
|
|
40
|
+
* chunk measured 2.6 s and 1.8 GB on node 24, a 99 MB one 7 s and 3.4 GB. Real GLB JSON is the
|
|
41
|
+
* scene description only — node, mesh, accessor and material records; the geometry and textures
|
|
42
|
+
* live in the BIN chunk — so it runs from a few KB to a few MB even for large scenes. 8 MiB leaves
|
|
43
|
+
* room above that while bounding the parse to a fraction of a second. Over it, the file is refused
|
|
44
|
+
* like any other GLB that fails the check, before anything is decoded.
|
|
45
|
+
*/
|
|
46
|
+
export declare const SAVE_MODEL3D_MAX_JSON_CHUNK_BYTES: number;
|
|
47
|
+
/**
|
|
48
|
+
* Which model type the buffer STARTS like (`glTF` + version 2, or the binary FBX magic), or `null`.
|
|
49
|
+
* Reads at most the first 21 bytes. Not a validation: see {@link isValidSaveModel3d}.
|
|
50
|
+
*/
|
|
51
|
+
export declare function looksLikeSaveModel3d(view: Uint8Array): SaveModel3dType | null;
|
|
52
|
+
/** Strict structural check of a buffer that {@link looksLikeSaveModel3d} called `type`. */
|
|
53
|
+
export declare function isValidSaveModel3d(view: Uint8Array, type: SaveModel3dType): boolean;
|
|
54
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@civitai/app-sdk/save-model3d` — the App Blocks `SAVE_IMAGE { bytes }` 3D model check: binary
|
|
3
|
+
* glTF (`.glb`) and binary FBX (`.fbx`).
|
|
4
|
+
*
|
|
5
|
+
* 🔴 THE ONE COPY, like `./save-zip`. The civitai.com host and `@civitai/blocks-react`'s
|
|
6
|
+
* `createMockHost` both import this module, so the mock accepts and refuses exactly the models
|
|
7
|
+
* production does. Change the rules here, release, and bump the host's `@civitai/app-sdk`.
|
|
8
|
+
*
|
|
9
|
+
* glTF-JSON (`.gltf`), ASCII FBX and OBJ are NOT model types here: they are text, so the host saves
|
|
10
|
+
* them as `.txt` (and a `.gltf` usually references sidecar files a single save could not carry).
|
|
11
|
+
*
|
|
12
|
+
* Two steps, the same split as the zip rules:
|
|
13
|
+
* 1. {@link looksLikeSaveModel3d} — does the buffer START like one of these? Decided on the first
|
|
14
|
+
* 8 (GLB) or 21 (FBX) bytes, so a caller can pick the per-file cap
|
|
15
|
+
* ({@link SAVE_MODEL3D_MAX_BYTES}) before decoding anything. Both prefixes contain a NUL byte,
|
|
16
|
+
* so no text payload can ever be a candidate: refusing a candidate that fails step 2 takes
|
|
17
|
+
* nothing away from a text classifier that refuses every NUL-bearing buffer.
|
|
18
|
+
* 2. {@link isValidSaveModel3d} — the strict structural check. A GLB must account for EVERY byte
|
|
19
|
+
* of the buffer in its declared chunks (nothing can ride along after it); an FBX must have a
|
|
20
|
+
* well-formed run of top-level node records ending in the null record, with no more than a
|
|
21
|
+
* footer's worth of bytes after it.
|
|
22
|
+
*
|
|
23
|
+
* Dependency-free, browser and Node.
|
|
24
|
+
*/
|
|
25
|
+
/** The two 3D model types, each with the one extension (no dot) it saves under. */
|
|
26
|
+
export const SAVE_MODEL3D_EXTENSION = {
|
|
27
|
+
// IANA-registered.
|
|
28
|
+
'model/gltf-binary': 'glb',
|
|
29
|
+
// FBX has no registered media type; this names it for the Blob without claiming a registration.
|
|
30
|
+
'model/x-fbx': 'fbx',
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The per-file cap on a buffer that starts like a model: 100 MiB. Generated meshes with embedded
|
|
34
|
+
* textures outgrow the 50 MiB cap of the other bytes types; the host also checks a model against
|
|
35
|
+
* its 100 MiB window budget, so a model at the cap is the only save that window admits.
|
|
36
|
+
*/
|
|
37
|
+
export const SAVE_MODEL3D_MAX_BYTES = 100 * 1024 * 1024;
|
|
38
|
+
/**
|
|
39
|
+
* The largest JSON chunk a GLB may carry: 8 MiB. The check decodes and parses that chunk on the
|
|
40
|
+
* caller's thread (the host's is the viewer's main thread), and parse cost grows with it: a 49.5 MB
|
|
41
|
+
* chunk measured 2.6 s and 1.8 GB on node 24, a 99 MB one 7 s and 3.4 GB. Real GLB JSON is the
|
|
42
|
+
* scene description only — node, mesh, accessor and material records; the geometry and textures
|
|
43
|
+
* live in the BIN chunk — so it runs from a few KB to a few MB even for large scenes. 8 MiB leaves
|
|
44
|
+
* room above that while bounding the parse to a fraction of a second. Over it, the file is refused
|
|
45
|
+
* like any other GLB that fails the check, before anything is decoded.
|
|
46
|
+
*/
|
|
47
|
+
export const SAVE_MODEL3D_MAX_JSON_CHUNK_BYTES = 8 * 1024 * 1024;
|
|
48
|
+
/** `glTF` + container version 2 (little-endian u32). */
|
|
49
|
+
const GLB_PREFIX = [0x67, 0x6c, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00];
|
|
50
|
+
const GLB_HEADER_BYTES = 12;
|
|
51
|
+
const GLB_CHUNK_HEADER_BYTES = 8;
|
|
52
|
+
const GLB_CHUNK_JSON = 0x4e4f534a; // "JSON" read as a little-endian u32
|
|
53
|
+
const GLB_CHUNK_BIN = 0x004e4942; // "BIN\0" read as a little-endian u32
|
|
54
|
+
/** `Kaydara FBX Binary` + two spaces + NUL: the binary FBX magic. ASCII FBX starts with `; FBX`. */
|
|
55
|
+
const FBX_MAGIC = [
|
|
56
|
+
0x4b, 0x61, 0x79, 0x64, 0x61, 0x72, 0x61, 0x20, 0x46, 0x42, 0x58, 0x20, 0x42, 0x69, 0x6e, 0x61,
|
|
57
|
+
0x72, 0x79, 0x20, 0x20, 0x00,
|
|
58
|
+
];
|
|
59
|
+
/** Magic, then `0x1A 0x00`, then the u32 version. */
|
|
60
|
+
const FBX_HEADER_BYTES = 27;
|
|
61
|
+
/**
|
|
62
|
+
* The version range accepted: from 6100 (FBX 2006, the oldest binary writers emit) to 7999. The
|
|
63
|
+
* newest version in use is 7700 (FBX 2020); the bound admits any later 7.x so a newer writer of the
|
|
64
|
+
* same record format is not refused. An 8.x would be a new format and is refused.
|
|
65
|
+
*/
|
|
66
|
+
const FBX_MIN_VERSION = 6100;
|
|
67
|
+
const FBX_MAX_VERSION = 7999;
|
|
68
|
+
/** From 7500 a node record's three leading fields are u64 instead of u32. */
|
|
69
|
+
const FBX_WIDE_RECORD_VERSION = 7500;
|
|
70
|
+
/**
|
|
71
|
+
* The most bytes allowed after the top-level null record. The footer writers emit is 160 bytes
|
|
72
|
+
* plus up to 15 of alignment padding; 256 leaves room for a writer that differs, and still means
|
|
73
|
+
* no second file of any size can follow the node data.
|
|
74
|
+
*/
|
|
75
|
+
const FBX_MAX_TAIL_BYTES = 256;
|
|
76
|
+
function startsWith(u8, sig) {
|
|
77
|
+
if (u8.length < sig.length)
|
|
78
|
+
return false;
|
|
79
|
+
for (let i = 0; i < sig.length; i++)
|
|
80
|
+
if (u8[i] !== sig[i])
|
|
81
|
+
return false;
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Which model type the buffer STARTS like (`glTF` + version 2, or the binary FBX magic), or `null`.
|
|
86
|
+
* Reads at most the first 21 bytes. Not a validation: see {@link isValidSaveModel3d}.
|
|
87
|
+
*/
|
|
88
|
+
export function looksLikeSaveModel3d(view) {
|
|
89
|
+
if (startsWith(view, GLB_PREFIX))
|
|
90
|
+
return 'model/gltf-binary';
|
|
91
|
+
if (startsWith(view, FBX_MAGIC))
|
|
92
|
+
return 'model/x-fbx';
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
/** Strict structural check of a buffer that {@link looksLikeSaveModel3d} called `type`. */
|
|
96
|
+
export function isValidSaveModel3d(view, type) {
|
|
97
|
+
return type === 'model/gltf-binary' ? isValidGlb(view) : isValidFbx(view);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* glTF 2.0 binary container (spec §4.4): a 12-byte header whose declared length equals the
|
|
101
|
+
* buffer's, then chunks. The JSON chunk comes first and only first; a BIN chunk, if any, only
|
|
102
|
+
* second; chunks of any other type are allowed after the JSON chunk, because the spec says
|
|
103
|
+
* "Client implementations MUST ignore chunks with unknown types to enable glTF extensions to
|
|
104
|
+
* reference additional chunks with new types following the first two chunks." Every chunk length
|
|
105
|
+
* is a multiple of 4 (the spec's alignment rule), every chunk lies inside the declared length, and
|
|
106
|
+
* the chunks end exactly at it. The JSON chunk must be at most
|
|
107
|
+
* {@link SAVE_MODEL3D_MAX_JSON_CHUNK_BYTES} (checked before it is decoded) and UTF-8 that parses to
|
|
108
|
+
* an object carrying `asset.version`, the one property glTF requires.
|
|
109
|
+
*/
|
|
110
|
+
function isValidGlb(u8) {
|
|
111
|
+
if (u8.length < GLB_HEADER_BYTES + GLB_CHUNK_HEADER_BYTES)
|
|
112
|
+
return false;
|
|
113
|
+
if (!startsWith(u8, GLB_PREFIX))
|
|
114
|
+
return false;
|
|
115
|
+
const view = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
116
|
+
const length = view.getUint32(8, true);
|
|
117
|
+
if (length !== u8.length || length % 4 !== 0)
|
|
118
|
+
return false;
|
|
119
|
+
let offset = GLB_HEADER_BYTES;
|
|
120
|
+
let index = 0;
|
|
121
|
+
while (offset < length) {
|
|
122
|
+
if (length - offset < GLB_CHUNK_HEADER_BYTES)
|
|
123
|
+
return false;
|
|
124
|
+
const chunkLength = view.getUint32(offset, true);
|
|
125
|
+
const chunkType = view.getUint32(offset + 4, true);
|
|
126
|
+
const dataStart = offset + GLB_CHUNK_HEADER_BYTES;
|
|
127
|
+
if (chunkLength % 4 !== 0 || chunkLength > length - dataStart)
|
|
128
|
+
return false;
|
|
129
|
+
if (index === 0) {
|
|
130
|
+
if (chunkType !== GLB_CHUNK_JSON || chunkLength === 0)
|
|
131
|
+
return false;
|
|
132
|
+
if (chunkLength > SAVE_MODEL3D_MAX_JSON_CHUNK_BYTES)
|
|
133
|
+
return false;
|
|
134
|
+
if (!isGltfJson(u8.subarray(dataStart, dataStart + chunkLength)))
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
else if (chunkType === GLB_CHUNK_JSON || (chunkType === GLB_CHUNK_BIN && index !== 1)) {
|
|
138
|
+
// A second JSON chunk, or a BIN chunk anywhere but second: out of the spec's order.
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
// Every chunk header is 8 bytes, so each pass moves forward and the walk terminates.
|
|
142
|
+
offset = dataStart + chunkLength;
|
|
143
|
+
index += 1;
|
|
144
|
+
}
|
|
145
|
+
return index >= 1 && offset === length;
|
|
146
|
+
}
|
|
147
|
+
function isGltfJson(bytes) {
|
|
148
|
+
let doc;
|
|
149
|
+
try {
|
|
150
|
+
doc = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (!doc || typeof doc !== 'object' || Array.isArray(doc))
|
|
156
|
+
return false;
|
|
157
|
+
const asset = doc.asset;
|
|
158
|
+
if (!asset || typeof asset !== 'object' || Array.isArray(asset))
|
|
159
|
+
return false;
|
|
160
|
+
return typeof asset.version === 'string';
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Binary FBX: the magic, `0x1A 0x00`, a version from 6100 to 7999, then a walk of the TOP-LEVEL
|
|
164
|
+
* node records. Each record's end offset must lie at or past the end of its own header and name
|
|
165
|
+
* (so the walk moves strictly forward) and stay inside the buffer, its name must be printable
|
|
166
|
+
* ASCII, and the walk must reach the all-zero null record with at least one node before it. At most
|
|
167
|
+
* {@link FBX_MAX_TAIL_BYTES} may follow the null record (the footer). Nested records are not
|
|
168
|
+
* walked: they lie inside a top-level record's span, which the walk has already bounded.
|
|
169
|
+
*/
|
|
170
|
+
function isValidFbx(u8) {
|
|
171
|
+
if (u8.length < FBX_HEADER_BYTES)
|
|
172
|
+
return false;
|
|
173
|
+
if (!startsWith(u8, FBX_MAGIC) || u8[21] !== 0x1a || u8[22] !== 0x00)
|
|
174
|
+
return false;
|
|
175
|
+
const view = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
176
|
+
const version = view.getUint32(23, true);
|
|
177
|
+
if (version < FBX_MIN_VERSION || version > FBX_MAX_VERSION)
|
|
178
|
+
return false;
|
|
179
|
+
const wide = version >= FBX_WIDE_RECORD_VERSION;
|
|
180
|
+
const fieldBytes = wide ? 8 : 4;
|
|
181
|
+
const recordHeaderBytes = fieldBytes * 3 + 1;
|
|
182
|
+
const readField = (at) => wide ? Number(view.getBigUint64(at, true)) : view.getUint32(at, true);
|
|
183
|
+
let offset = FBX_HEADER_BYTES;
|
|
184
|
+
let nodes = 0;
|
|
185
|
+
for (;;) {
|
|
186
|
+
if (u8.length - offset < recordHeaderBytes)
|
|
187
|
+
return false;
|
|
188
|
+
const endOffset = readField(offset);
|
|
189
|
+
const numProperties = readField(offset + fieldBytes);
|
|
190
|
+
const propertyListLength = readField(offset + fieldBytes * 2);
|
|
191
|
+
const nameLength = u8[offset + fieldBytes * 3];
|
|
192
|
+
if (endOffset === 0) {
|
|
193
|
+
// The null record: every field zero. Ends the top-level list.
|
|
194
|
+
if (numProperties !== 0 || propertyListLength !== 0 || nameLength !== 0)
|
|
195
|
+
return false;
|
|
196
|
+
const tail = u8.length - (offset + recordHeaderBytes);
|
|
197
|
+
return nodes > 0 && tail <= FBX_MAX_TAIL_BYTES;
|
|
198
|
+
}
|
|
199
|
+
const nameStart = offset + recordHeaderBytes;
|
|
200
|
+
const bodyStart = nameStart + nameLength;
|
|
201
|
+
// A node with no properties and no children ends right after its name; nameLength >= 1 keeps
|
|
202
|
+
// every step strictly forward, so the walk terminates.
|
|
203
|
+
if (nameLength === 0 || endOffset < bodyStart || endOffset > u8.length)
|
|
204
|
+
return false;
|
|
205
|
+
if (propertyListLength > endOffset - bodyStart)
|
|
206
|
+
return false;
|
|
207
|
+
for (let i = nameStart; i < bodyStart; i++) {
|
|
208
|
+
const c = u8[i];
|
|
209
|
+
if (c < 0x20 || c > 0x7e)
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
offset = endOffset;
|
|
213
|
+
nodes += 1;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
//# sourceMappingURL=index.js.map
|
package/dist/save-zip/index.d.ts
CHANGED
|
@@ -57,7 +57,11 @@ export declare const SAVE_ZIP_LIMITS: {
|
|
|
57
57
|
readonly maxEntryRatio: 100;
|
|
58
58
|
readonly ratioFloorBytes: number;
|
|
59
59
|
};
|
|
60
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* Entry extensions a zip may contain (lowercase, after the last `.`). Directories need none. `glb`
|
|
62
|
+
* (binary glTF) and `fbx` are 3D model files, so a block can bundle a generated model with its
|
|
63
|
+
* renders; like every entry, only the name is checked, not the content.
|
|
64
|
+
*/
|
|
61
65
|
export declare const SAVE_ZIP_ALLOWED_EXTENSIONS: ReadonlySet<string>;
|
|
62
66
|
export declare const SAVE_ZIP_NO_EOCD_ERROR = "zip end of central directory not found (archive comments are not allowed)";
|
|
63
67
|
export declare const SAVE_ZIP_MULTIDISK_ERROR = "zip spans multiple disks";
|
package/dist/save-zip/index.js
CHANGED
|
@@ -57,7 +57,11 @@ export const SAVE_ZIP_LIMITS = {
|
|
|
57
57
|
maxEntryRatio: 100,
|
|
58
58
|
ratioFloorBytes: 1024 * 1024,
|
|
59
59
|
};
|
|
60
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* Entry extensions a zip may contain (lowercase, after the last `.`). Directories need none. `glb`
|
|
62
|
+
* (binary glTF) and `fbx` are 3D model files, so a block can bundle a generated model with its
|
|
63
|
+
* renders; like every entry, only the name is checked, not the content.
|
|
64
|
+
*/
|
|
61
65
|
export const SAVE_ZIP_ALLOWED_EXTENSIONS = new Set([
|
|
62
66
|
'png',
|
|
63
67
|
'jpg',
|
|
@@ -66,6 +70,8 @@ export const SAVE_ZIP_ALLOWED_EXTENSIONS = new Set([
|
|
|
66
70
|
'txt',
|
|
67
71
|
'json',
|
|
68
72
|
'csv',
|
|
73
|
+
'glb',
|
|
74
|
+
'fbx',
|
|
69
75
|
]);
|
|
70
76
|
export const SAVE_ZIP_NO_EOCD_ERROR = 'zip end of central directory not found (archive comments are not allowed)';
|
|
71
77
|
export const SAVE_ZIP_MULTIDISK_ERROR = 'zip spans multiple disks';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@civitai/app-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.94.0",
|
|
4
4
|
"description": "OAuth + PKCE, encrypted-cookie sessions, scopes, and orchestrator helpers for building third-party Civitai apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -55,6 +55,10 @@
|
|
|
55
55
|
"types": "./dist/save-zip/index.d.ts",
|
|
56
56
|
"import": "./dist/save-zip/index.js"
|
|
57
57
|
},
|
|
58
|
+
"./save-model3d": {
|
|
59
|
+
"types": "./dist/save-model3d/index.d.ts",
|
|
60
|
+
"import": "./dist/save-model3d/index.js"
|
|
61
|
+
},
|
|
58
62
|
"./schemas/app-block/v1.json": "./schemas/app-block/v1.json"
|
|
59
63
|
},
|
|
60
64
|
"comment-packaging": "`!dist/**/*.map` keeps dangling sourcemaps out of the tarball (#376). Rationale: scripts/check-shipped-sourcemaps.mjs.",
|
|
@@ -536,8 +536,13 @@
|
|
|
536
536
|
"maxItems": 32,
|
|
537
537
|
"items": { "$ref": "#/$defs/storageMigrationOp" }
|
|
538
538
|
},
|
|
539
|
+
"keyedBy": {
|
|
540
|
+
"type": "string",
|
|
541
|
+
"description": "Shared collections with `write` \"own\" only (not kind \"log\"). The server derives each document's key from the signed-in viewer, so each viewer has at most one document (\"viewer\") or at most one per parent key the app names (\"parent+viewer\"), and no viewer can write another's. The app reads and writes it at `/:collection/@me` or `/:collection/@me/:parent`, where a parent is 1 to 96 characters of A-Z, a-z, 0-9, `.`, `_` or `-` (not only dots); a \"parent+viewer\" key is `<parent>:<digest>`, so listing with `keyPrefix=<parent>:` returns one parent's documents. Writing such a collection by key, creating in it, publishing into it and counters or marks on it are refused (it declares none). Cannot be added, removed or changed on a collection any approved version declared: declare a new collection instead.",
|
|
542
|
+
"enum": ["viewer", "parent+viewer"]
|
|
543
|
+
},
|
|
539
544
|
"hook": {
|
|
540
|
-
"description": "
|
|
545
|
+
"description": "Optional, in a limited rollout: civitai.com accepts a new or changed hook only from authors the rollout includes, and refuses it otherwise. A permission hook: one ES module of the bundle whose default export decides, for every write to this collection, whether the write is allowed. It runs after the collection `schema` check, never instead of it. `file` is the hook's path in the submitted bundle, shipped verbatim — a committed source file, not a build output, so it must not be inside `outputDir` or `node_modules`; one self-contained module with no imports, at most 65536 bytes of UTF-8 text. `sha256` is the SHA-256 of that file's bytes as 64 lowercase hex characters: submit, the git-push path and approve each refuse a version whose file is missing or does not match it. `timeoutMs` is the most time one call may take, from 1 to 500 (omitted means 50). One file used by several collections must be pinned with the same sha256 in each.",
|
|
541
546
|
"$ref": "#/$defs/storageCollectionHook"
|
|
542
547
|
}
|
|
543
548
|
},
|
|
@@ -565,7 +570,8 @@
|
|
|
565
570
|
"showAuthor": false,
|
|
566
571
|
"dailyWritesPerAuthor": false,
|
|
567
572
|
"ownedModelVersions": false,
|
|
568
|
-
"registry": false
|
|
573
|
+
"registry": false,
|
|
574
|
+
"keyedBy": false
|
|
569
575
|
}
|
|
570
576
|
}
|
|
571
577
|
},
|
|
@@ -593,6 +599,24 @@
|
|
|
593
599
|
},
|
|
594
600
|
"then": { "properties": { "ownedModelVersions": false } }
|
|
595
601
|
},
|
|
602
|
+
{
|
|
603
|
+
"$comment": "A viewer-keyed collection's viewer is the author of every key they address, so it is declared only under write:\"own\" (private collections are refused above); log entries have server keys.",
|
|
604
|
+
"if": {
|
|
605
|
+
"anyOf": [
|
|
606
|
+
{
|
|
607
|
+
"required": ["write"],
|
|
608
|
+
"properties": { "write": { "enum": ["anyone", "owner", "none"] } }
|
|
609
|
+
},
|
|
610
|
+
{ "required": ["kind"], "properties": { "kind": { "const": "log" } } }
|
|
611
|
+
]
|
|
612
|
+
},
|
|
613
|
+
"then": { "properties": { "keyedBy": false } }
|
|
614
|
+
},
|
|
615
|
+
{
|
|
616
|
+
"$comment": "A viewer-keyed document is itself the viewer's vote or entry, so a viewer-keyed collection declares no counters or marks.",
|
|
617
|
+
"if": { "required": ["keyedBy"] },
|
|
618
|
+
"then": { "properties": { "counters": false, "marks": false } }
|
|
619
|
+
},
|
|
596
620
|
{
|
|
597
621
|
"$comment": "A write:\"none\" collection is written only through counters and marks, so it must declare at least one.",
|
|
598
622
|
"if": {
|