@joiner-platform/cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -2
- package/bundled-resources/poe-tile-templates/base/CLAUDE.md +72 -0
- package/bundled-resources/poe-tile-templates/base/README.md +7 -0
- package/bundled-resources/poe-tile-templates/base/_bunfig.toml +8 -0
- package/bundled-resources/poe-tile-templates/base/_gitignore +18 -0
- package/bundled-resources/poe-tile-templates/base/client.ts +12 -0
- package/bundled-resources/poe-tile-templates/base/package.json +36 -0
- package/bundled-resources/poe-tile-templates/base/playwright.config.ts +38 -0
- package/bundled-resources/poe-tile-templates/base/scripts/check-source.ts +148 -0
- package/bundled-resources/poe-tile-templates/base/scripts/dev.ts +19 -0
- package/bundled-resources/poe-tile-templates/base/scripts/doctor.sh +29 -0
- package/bundled-resources/poe-tile-templates/base/scripts/visual-check.ts +64 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/backend-config.ts +9 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/client-config.ts +8 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/data/items.ts +16 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/mutators/index.ts +10 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/mutators/remove-todo.ts +5 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/mutators/set-todo.ts +20 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/mutators/types.ts +5 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/mutators.test.ts +138 -0
- package/bundled-resources/poe-tile-templates/base/synced-store/schema.ts +40 -0
- package/bundled-resources/poe-tile-templates/base/tests/e2e.test.playwright.ts +92 -0
- package/bundled-resources/poe-tile-templates/base/tests/mobile-viewport.test.playwright.ts +111 -0
- package/bundled-resources/poe-tile-templates/base/tests/render-check.test.playwright.ts +128 -0
- package/bundled-resources/poe-tile-templates/base/tests/screenshot.test.playwright.ts +86 -0
- package/bundled-resources/poe-tile-templates/base/tests/setup-dom.ts +70 -0
- package/bundled-resources/poe-tile-templates/base/tile/src/backend.ts +3 -0
- package/bundled-resources/poe-tile-templates/base/tile/styles.css +47 -0
- package/bundled-resources/poe-tile-templates/base/tsconfig.json +13 -0
- package/bundled-resources/poe-tile-templates/coverage.config.json +6 -0
- package/bundled-resources/poe-tile-templates/index.ts +27 -0
- package/bundled-resources/poe-tile-templates/package.json +31 -0
- package/bundled-resources/poe-tile-templates/phaserjs/package.partial.json +6 -0
- package/bundled-resources/poe-tile-templates/phaserjs/tile/index.html +14 -0
- package/bundled-resources/poe-tile-templates/phaserjs/tile/src/entry.ts +35 -0
- package/bundled-resources/poe-tile-templates/phaserjs/ui/App.test.happydom.tsx +98 -0
- package/bundled-resources/poe-tile-templates/phaserjs/ui/App.ts +180 -0
- package/bundled-resources/poe-tile-templates/phaserjs/ui/phaser-scene.ts +174 -0
- package/bundled-resources/poe-tile-templates/phaserjs/vite.config.ts +28 -0
- package/bundled-resources/poe-tile-templates/preact/package.partial.json +9 -0
- package/bundled-resources/poe-tile-templates/preact/postcss.config.js +5 -0
- package/bundled-resources/poe-tile-templates/preact/tile/index.html +14 -0
- package/bundled-resources/poe-tile-templates/preact/tile/src/entry.tsx +36 -0
- package/bundled-resources/poe-tile-templates/preact/tile/styles.css +51 -0
- package/bundled-resources/poe-tile-templates/preact/tsconfig.json +15 -0
- package/bundled-resources/poe-tile-templates/preact/ui/App.test.happydom.tsx +69 -0
- package/bundled-resources/poe-tile-templates/preact/ui/App.tsx +116 -0
- package/bundled-resources/poe-tile-templates/preact/vite.config.ts +18 -0
- package/bundled-resources/poe-tile-templates/react/package.partial.json +12 -0
- package/bundled-resources/poe-tile-templates/react/postcss.config.js +5 -0
- package/bundled-resources/poe-tile-templates/react/tile/index.html +14 -0
- package/bundled-resources/poe-tile-templates/react/tile/src/entry.tsx +38 -0
- package/bundled-resources/poe-tile-templates/react/tile/styles.css +51 -0
- package/bundled-resources/poe-tile-templates/react/tsconfig.json +14 -0
- package/bundled-resources/poe-tile-templates/react/ui/App.test.happydom.tsx +71 -0
- package/bundled-resources/poe-tile-templates/react/ui/App.tsx +110 -0
- package/bundled-resources/poe-tile-templates/react/vite.config.ts +21 -0
- package/bundled-resources/poe-tile-templates/scripts/cleanup.ts +10 -0
- package/bundled-resources/poe-tile-templates/scripts/generate.ts +57 -0
- package/bundled-resources/poe-tile-templates/scripts/install-diagnostics.ts +52 -0
- package/bundled-resources/poe-tile-templates/scripts/paths.ts +7 -0
- package/bundled-resources/poe-tile-templates/scripts/sdk-dependency-key.ts +288 -0
- package/bundled-resources/poe-tile-templates/scripts/verify-all-sdks.ts +132 -0
- package/bundled-resources/poe-tile-templates/scripts/verify-local-sdk-command.ts +30 -0
- package/bundled-resources/poe-tile-templates/scripts/verify-local-sdk-tarball.ts +397 -0
- package/bundled-resources/poe-tile-templates/solidjs/_bunfig.toml +14 -0
- package/bundled-resources/poe-tile-templates/solidjs/package.partial.json +13 -0
- package/bundled-resources/poe-tile-templates/solidjs/postcss.config.js +5 -0
- package/bundled-resources/poe-tile-templates/solidjs/tests/solid-plugin.ts +49 -0
- package/bundled-resources/poe-tile-templates/solidjs/tile/index.html +14 -0
- package/bundled-resources/poe-tile-templates/solidjs/tile/src/entry.tsx +36 -0
- package/bundled-resources/poe-tile-templates/solidjs/tile/styles.css +51 -0
- package/bundled-resources/poe-tile-templates/solidjs/tsconfig.json +15 -0
- package/bundled-resources/poe-tile-templates/solidjs/ui/App.test.happydom.tsx +70 -0
- package/bundled-resources/poe-tile-templates/solidjs/ui/App.tsx +129 -0
- package/bundled-resources/poe-tile-templates/solidjs/vite.config.ts +18 -0
- package/bundled-resources/poe-tile-templates/tests/check-source.test.ts +131 -0
- package/bundled-resources/poe-tile-templates/tests/dev-script.test.slow.ts +166 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/customized-manager.ts +67 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/extended-mutators.ts.txt +106 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/multiplayer-verification.test.playwright.ts.txt +103 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/replace-starter-schema.ts +67 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/starter-demo/phaserjs.tsx.txt +85 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/starter-demo/preact.tsx.txt +69 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/starter-demo/react.tsx.txt +73 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/starter-demo/seating.ts.txt +54 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/starter-demo/solidjs.tsx.txt +70 -0
- package/bundled-resources/poe-tile-templates/tests/fixtures/starter-demo/vanilla-js.tsx.txt +85 -0
- package/bundled-resources/poe-tile-templates/tests/generated-templates.test.ts +278 -0
- package/bundled-resources/poe-tile-templates/tests/happy-dom-version.test.ts +27 -0
- package/bundled-resources/poe-tile-templates/tests/install-diagnostics.test.ts +99 -0
- package/bundled-resources/poe-tile-templates/tests/native-gesture-defaults.test.ts +23 -0
- package/bundled-resources/poe-tile-templates/tests/playwright-version.test.ts +84 -0
- package/bundled-resources/poe-tile-templates/tests/render-check-scaffold.test.ts +56 -0
- package/bundled-resources/poe-tile-templates/tests/sdk-dependency-key.test.ts +129 -0
- package/bundled-resources/poe-tile-templates/tests/visual-check.test.ts +90 -0
- package/bundled-resources/poe-tile-templates/vanilla-js/package.partial.json +3 -0
- package/bundled-resources/poe-tile-templates/vanilla-js/tile/index.html +14 -0
- package/bundled-resources/poe-tile-templates/vanilla-js/tile/src/entry.ts +35 -0
- package/bundled-resources/poe-tile-templates/vanilla-js/ui/App.test.happydom.tsx +96 -0
- package/bundled-resources/poe-tile-templates/vanilla-js/ui/App.ts +113 -0
- package/bundled-resources/poe-tile-templates/vanilla-js/vite.config.ts +17 -0
- package/cli.ts +134 -0
- package/index.ts +96 -0
- package/package.json +48 -5
- package/skills/audio-generation/.generated-by-poe-templated-docs +0 -0
- package/skills/audio-generation/SKILL.md +125 -0
- package/skills/finish-prototype/.generated-by-poe-templated-docs +0 -0
- package/skills/finish-prototype/SKILL.md +22 -0
- package/skills/help-me-build/.generated-by-poe-templated-docs +0 -0
- package/skills/help-me-build/SKILL.md +56 -0
- package/skills/image-generation/.generated-by-poe-templated-docs +0 -0
- package/skills/image-generation/SKILL.md +128 -0
- package/skills/one-shot/.generated-by-poe-templated-docs +0 -0
- package/skills/one-shot/SKILL.md +22 -0
- package/skills/platform-prototype/.generated-by-poe-templated-docs +0 -0
- package/skills/platform-prototype/SKILL.md +60 -0
- package/skills/platform-subagent/.generated-by-poe-templated-docs +0 -0
- package/skills/platform-subagent/SKILL.md +16 -0
- package/skills/poe-tiles-cli/.generated-by-poe-templated-docs +0 -0
- package/skills/poe-tiles-cli/SKILL.md +609 -0
- package/skills/poe-tiles-cli/references/device-motion.md +40 -0
- package/skills/poe-tiles-cli/references/player-updates.md +41 -0
- package/skills/prototype/.generated-by-poe-templated-docs +0 -0
- package/skills/prototype/SKILL.md +148 -0
- package/skills/publish/.generated-by-poe-templated-docs +0 -0
- package/skills/publish/SKILL.md +73 -0
- package/skills/synced-store/.generated-by-poe-templated-docs +0 -0
- package/skills/synced-store/SKILL.md +583 -0
- package/skills/synced-store/references/actions.md +85 -0
- package/skills/synced-store/references/api-patterns.md +245 -0
- package/skills/synced-store/references/architecture-scaling-and-performance.md +280 -0
- package/skills/synced-store/references/client-api-reference.md +263 -0
- package/skills/synced-store/references/data-visibility.md +95 -0
- package/skills/synced-store/references/external-stores.md +162 -0
- package/skills/synced-store/references/file-uploads.md +216 -0
- package/skills/synced-store/references/getting-user-info-of-members.md +228 -0
- package/skills/synced-store/references/limitations.md +72 -0
- package/skills/synced-store/references/live-data-limit-hook.md +131 -0
- package/skills/synced-store/references/mutator-rules.md +169 -0
- package/skills/synced-store/references/platform.md +49 -0
- package/skills/synced-store/references/schema-and-bindings.md +85 -0
- package/skills/synced-store/references/schema-libraries.md +57 -0
- package/skills/synced-store/references/schema-migrations.md +119 -0
- package/skills/synced-store/references/searchable-tables.md +27 -0
- package/skills/synced-store/references/server-forking.md +115 -0
- package/skills/synced-store/references/singleton-tables.md +61 -0
- package/skills/synced-store/references/sort-keys.md +253 -0
- package/skills/synced-store/references/ui-patterns.md +95 -0
- package/skills/tile-creator/.generated-by-poe-templated-docs +0 -0
- package/skills/tile-creator/SKILL.md +196 -0
- package/skills/tile-creator/references/assets.md +124 -0
- package/skills/tile-creator/references/backend-api.md +308 -0
- package/skills/tile-creator/references/cli-limitations.md +64 -0
- package/skills/tile-creator/references/cli.md +604 -0
- package/skills/tile-creator/references/client-api.md +2407 -0
- package/skills/tile-creator/references/composing-apps.md +166 -0
- package/skills/tile-creator/references/e2e-tests.md +55 -0
- package/skills/tile-creator/references/game-ux-best-practices.md +57 -0
- package/skills/tile-creator/references/migrating-pre-rename-tiles.md +79 -0
- package/skills/tile-creator/references/multiplayer-api-recipe.md +205 -0
- package/skills/tile-creator/references/native-results.md +100 -0
- package/skills/tile-creator/references/platform-integration.md +201 -0
- package/skills/tile-creator/references/power-report.md +199 -0
- package/skills/tile-creator/references/publish-rollout.md +166 -0
- package/skills/tile-creator/references/publishing.md +33 -0
- package/skills/tile-creator/references/push-notification-guidelines.md +58 -0
- package/skills/tile-creator/references/render-check.md +266 -0
- package/skills/tile-creator/references/running-the-client-api-on-a-server-owned-by-the-user.md +95 -0
- package/skills/tile-creator/references/safe-area-insets.md +79 -0
- package/skills/tile-creator/references/sandbox-limitations.md +32 -0
- package/skills/tile-creator/references/scaffolded-tile-folder-structure.md +48 -0
- package/skills/tile-creator/references/scaffolding-a-new-app.md +202 -0
- package/skills/tile-creator/references/simulations.md +125 -0
- package/skills/tile-creator/references/synced-store-client-reference.md +92 -0
- package/skills/tile-creator/references/troubleshooting.md +22 -0
- package/skills/tile-creator/references/turn-based-mutator.md +43 -0
- package/skills/tile-creator/references/user-erasure.md +14 -0
- package/skills/tile-creator/references/vite-plugin.md +166 -0
- package/skills/tile-creator/references/workspace-retention.md +72 -0
- package/src/admin-conversions-client.ts +28 -0
- package/src/admin-reviews-client.ts +36 -0
- package/src/api.generated.ts +1527 -0
- package/src/auth-token.ts +198 -0
- package/src/bounded-read.ts +35 -0
- package/src/bun-version.ts +33 -0
- package/src/checkout-base.ts +207 -0
- package/src/cli-bun-version.ts +16 -0
- package/src/cli-env.ts +28 -0
- package/src/cli-name.ts +23 -0
- package/src/client.ts +2474 -0
- package/src/collect-files.ts +226 -0
- package/src/commands/accounts.ts +55 -0
- package/src/commands/admin-abandon-conversion.ts +60 -0
- package/src/commands/admin-reviews.ts +66 -0
- package/src/commands/admin.ts +219 -0
- package/src/commands/auth.ts +89 -0
- package/src/commands/checkout-prototype.ts +67 -0
- package/src/commands/creator-source-capability.ts +11 -0
- package/src/commands/doctor-bun.ts +145 -0
- package/src/commands/doctor-command.ts +64 -0
- package/src/commands/doctor-package-json.ts +47 -0
- package/src/commands/doctor-sdk-pin.ts +156 -0
- package/src/commands/doctor-store-keys-lexer.ts +213 -0
- package/src/commands/doctor-store-keys-scan.ts +262 -0
- package/src/commands/doctor-store-keys.ts +196 -0
- package/src/commands/doctor.ts +371 -0
- package/src/commands/init/blank-scaffold-templates.ts +397 -0
- package/src/commands/init/index.ts +170 -0
- package/src/commands/init/internal-toolchain.ts +49 -0
- package/src/commands/init/manager-test-template.ts +107 -0
- package/src/commands/init/multiplayer-test-template.ts +217 -0
- package/src/commands/init/name-input.tsx +167 -0
- package/src/commands/init/no-store-scaffold-templates.ts +281 -0
- package/src/commands/init/no-store-scaffold.ts +134 -0
- package/src/commands/init/package-json-scripts.ts +72 -0
- package/src/commands/init/scaffold.ts +960 -0
- package/src/commands/init/sdk-tarball-url.generated.ts +4 -0
- package/src/commands/init/sdk-tarball-url.ts +31 -0
- package/src/commands/init/template-picker.tsx +147 -0
- package/src/commands/inspect-child-entry.ts +107 -0
- package/src/commands/inspect-contract.ts +87 -0
- package/src/commands/inspect-freshness.ts +217 -0
- package/src/commands/inspect-lock.ts +201 -0
- package/src/commands/inspect-options.ts +360 -0
- package/src/commands/inspect-orchestrate.ts +375 -0
- package/src/commands/inspect-process.ts +172 -0
- package/src/commands/inspect-ready.ts +65 -0
- package/src/commands/inspect-report.ts +235 -0
- package/src/commands/inspect-run-state.ts +296 -0
- package/src/commands/inspect-run.ts +157 -0
- package/src/commands/inspect-watchdog.ts +74 -0
- package/src/commands/install-joiner.ts +208 -0
- package/src/commands/login.ts +308 -0
- package/src/commands/models.ts +40 -0
- package/src/commands/parse-options.ts +36 -0
- package/src/commands/remix-package-json.ts +64 -0
- package/src/commands/remix.ts +379 -0
- package/src/commands/sdk-url.ts +141 -0
- package/src/commands/simulations.ts +133 -0
- package/src/commands/skills.ts +108 -0
- package/src/commands/store-files.ts +92 -0
- package/src/commands/stores.ts +703 -0
- package/src/commands/tiles-channel-commands.ts +115 -0
- package/src/commands/tiles-checkout.ts +351 -0
- package/src/commands/tiles-gallery-command.ts +95 -0
- package/src/commands/tiles-get-helpers.ts +27 -0
- package/src/commands/tiles-inspect.ts +276 -0
- package/src/commands/tiles-power-command.ts +308 -0
- package/src/commands/tiles-power-format.ts +396 -0
- package/src/commands/tiles-power-versions.ts +299 -0
- package/src/commands/tiles-publish-helpers.ts +145 -0
- package/src/commands/tiles-upload-command.ts +277 -0
- package/src/commands/tiles-version-commands.ts +201 -0
- package/src/commands/tiles-visibility-command.ts +103 -0
- package/src/commands/tiles.ts +257 -0
- package/src/commands/upgrade.ts +307 -0
- package/src/commit-in-progress.ts +42 -0
- package/src/creator-source-context.ts +97 -0
- package/src/creator-source-publish.ts +52 -0
- package/src/deferred-assets.ts +125 -0
- package/src/distribution.ts +28 -0
- package/src/errors.ts +61 -0
- package/src/install-command.ts +43 -0
- package/src/output.ts +62 -0
- package/src/poe-tile-config-files.ts +100 -0
- package/src/poe-tile-config-players.ts +168 -0
- package/src/poe-tile-config-remix.ts +166 -0
- package/src/poe-tile-config.ts +831 -0
- package/src/power-report-client.ts +55 -0
- package/src/prototype-startup.ts +21 -0
- package/src/publish-change-summary.ts +55 -0
- package/src/publish-overrides-validation.ts +49 -0
- package/src/publish-pack.ts +167 -0
- package/src/publish-phases.ts +57 -0
- package/src/publish-retry.ts +51 -0
- package/src/publish-tile-from-config.ts +434 -0
- package/src/recorder-stream.ts +169 -0
- package/src/remix-provenance.ts +156 -0
- package/src/resolve-client-credential.ts +71 -0
- package/src/resolve-media-uploads.ts +147 -0
- package/src/sdk-dependency.ts +54 -0
- package/src/sdk-skills.ts +85 -0
- package/src/should-skip-client-setup.ts +20 -0
- package/src/simulation-snapshot.ts +123 -0
- package/src/simulation-types.ts +74 -0
- package/src/simulations-client.ts +112 -0
- package/src/source-bundle.ts +466 -0
- package/src/staged-publish-cleanup.ts +44 -0
- package/src/staged-publish-deferred.ts +160 -0
- package/src/staged-publish.ts +401 -0
- package/src/store-file-upload.ts +268 -0
- package/src/store-request.ts +168 -0
- package/src/tar-canonical.ts +41 -0
- package/src/tar-extensions.ts +159 -0
- package/src/tar-extract.ts +193 -0
- package/src/tar-parse.ts +224 -0
- package/src/tar.ts +231 -0
- package/src/template-check-scripts.ts +18 -0
- package/src/tile-gallery-client.ts +82 -0
- package/src/types.ts +258 -0
- package/src/upload-concurrency.ts +54 -0
- package/src/whole-tile.ts +52 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// owner: jyoung-q
|
|
2
|
+
|
|
3
|
+
import { CliError } from "./errors.js";
|
|
4
|
+
|
|
5
|
+
/** Mirrors the general receiver's 10 MiB request-body cap. */
|
|
6
|
+
export const MAX_STORE_FILE_UPLOAD_BYTES = 10 * 1024 * 1024;
|
|
7
|
+
|
|
8
|
+
const UUID_RE =
|
|
9
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
10
|
+
const FILE_KEY_RE = /^[0-9a-f]{64}$/;
|
|
11
|
+
const DEFAULT_CONTENT_TYPE = "application/octet-stream";
|
|
12
|
+
|
|
13
|
+
export interface StoreFileUploadOptions {
|
|
14
|
+
storeTypeId: string;
|
|
15
|
+
instanceId: string;
|
|
16
|
+
data: Uint8Array;
|
|
17
|
+
/** UUID idempotency key. A fresh UUID is generated when omitted. */
|
|
18
|
+
fileId?: string;
|
|
19
|
+
/** Original display name, without path separators. */
|
|
20
|
+
name?: string;
|
|
21
|
+
/** Receiver hint; the platform inspects the bytes authoritatively. */
|
|
22
|
+
contentType?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface StoreFileUploadResult {
|
|
26
|
+
fileId: string;
|
|
27
|
+
fileKey: string;
|
|
28
|
+
url: string;
|
|
29
|
+
sizeBytes: number;
|
|
30
|
+
contentType: string;
|
|
31
|
+
width: number | null;
|
|
32
|
+
height: number | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type StoreFileUploader = (
|
|
36
|
+
options: StoreFileUploadOptions,
|
|
37
|
+
) => Promise<StoreFileUploadResult>;
|
|
38
|
+
|
|
39
|
+
type FetchLike = (
|
|
40
|
+
input: string | URL | Request,
|
|
41
|
+
init?: RequestInit,
|
|
42
|
+
) => Promise<Response>;
|
|
43
|
+
|
|
44
|
+
interface StoreFileUploaderDependencies {
|
|
45
|
+
serverUrl: string;
|
|
46
|
+
fetchFn: FetchLike;
|
|
47
|
+
authenticate: () => Promise<Record<string, string>>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface MintedTicket {
|
|
51
|
+
ticket: string;
|
|
52
|
+
uploadUrl: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function hasControlCharacter(value: string): boolean {
|
|
56
|
+
for (const character of value) {
|
|
57
|
+
const codePoint = character.codePointAt(0)!;
|
|
58
|
+
if (codePoint <= 0x1f || codePoint === 0x7f) return true;
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function assertStoreFileUploadSize(sizeBytes: number): void {
|
|
64
|
+
if (sizeBytes < 1) {
|
|
65
|
+
throw new CliError("The upload file must not be empty.");
|
|
66
|
+
}
|
|
67
|
+
if (sizeBytes > MAX_STORE_FILE_UPLOAD_BYTES) {
|
|
68
|
+
throw new CliError("The upload file is larger than 10 MiB.");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function validateOptions(options: StoreFileUploadOptions): void {
|
|
73
|
+
assertStoreFileUploadSize(options.data.byteLength);
|
|
74
|
+
if (options.fileId !== undefined && !UUID_RE.test(options.fileId)) {
|
|
75
|
+
throw new CliError("The upload file ID must be a UUID.");
|
|
76
|
+
}
|
|
77
|
+
if (
|
|
78
|
+
options.name !== undefined &&
|
|
79
|
+
(options.name.length < 1 ||
|
|
80
|
+
options.name.length > 255 ||
|
|
81
|
+
options.name.includes("/") ||
|
|
82
|
+
hasControlCharacter(options.name))
|
|
83
|
+
) {
|
|
84
|
+
throw new CliError(
|
|
85
|
+
"The upload file name must be 1-255 characters with no path separators or control characters.",
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (
|
|
89
|
+
options.contentType !== undefined &&
|
|
90
|
+
(options.contentType.length < 1 ||
|
|
91
|
+
options.contentType.length > 255 ||
|
|
92
|
+
hasControlCharacter(options.contentType))
|
|
93
|
+
) {
|
|
94
|
+
throw new CliError("The upload content type is invalid.");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function readJson(
|
|
99
|
+
response: Response,
|
|
100
|
+
operation: string,
|
|
101
|
+
): Promise<unknown> {
|
|
102
|
+
try {
|
|
103
|
+
return await response.json();
|
|
104
|
+
} catch {
|
|
105
|
+
throw new CliError(`${operation} returned an invalid JSON response.`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function responseError(body: unknown): string {
|
|
110
|
+
if (typeof body !== "object" || body === null) return "unknown error";
|
|
111
|
+
const { error, appCode } = body as { error?: unknown; appCode?: unknown };
|
|
112
|
+
const message = typeof error === "string" ? error : "unknown error";
|
|
113
|
+
return typeof appCode === "string" && appCode.length > 0
|
|
114
|
+
? `${message} (${appCode})`
|
|
115
|
+
: message;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function parseMintedTicket(body: unknown): MintedTicket | null {
|
|
119
|
+
if (typeof body !== "object" || body === null) return null;
|
|
120
|
+
const { success, ticket, uploadUrl } = body as {
|
|
121
|
+
success?: unknown;
|
|
122
|
+
ticket?: unknown;
|
|
123
|
+
uploadUrl?: unknown;
|
|
124
|
+
};
|
|
125
|
+
if (
|
|
126
|
+
success !== true ||
|
|
127
|
+
typeof ticket !== "string" ||
|
|
128
|
+
ticket.length < 1 ||
|
|
129
|
+
typeof uploadUrl !== "string"
|
|
130
|
+
) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
const parsedUrl = new URL(uploadUrl);
|
|
135
|
+
if (parsedUrl.protocol !== "https:" && parsedUrl.protocol !== "http:") {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
} catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
return { ticket, uploadUrl };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function mintTicket(
|
|
145
|
+
dependencies: StoreFileUploaderDependencies,
|
|
146
|
+
options: StoreFileUploadOptions,
|
|
147
|
+
fileId: string,
|
|
148
|
+
): Promise<MintedTicket> {
|
|
149
|
+
const authHeaders = await dependencies.authenticate();
|
|
150
|
+
const response = await dependencies.fetchFn(
|
|
151
|
+
`${dependencies.serverUrl}/api/v1/synced-store/files/tickets`,
|
|
152
|
+
{
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: {
|
|
155
|
+
...authHeaders,
|
|
156
|
+
"Content-Type": "application/json",
|
|
157
|
+
"X-Requested-With": "poe-tiles-cli",
|
|
158
|
+
},
|
|
159
|
+
body: JSON.stringify({
|
|
160
|
+
typeId: options.storeTypeId,
|
|
161
|
+
instanceId: options.instanceId,
|
|
162
|
+
fileId,
|
|
163
|
+
sizeBytes: options.data.byteLength,
|
|
164
|
+
...(options.name === undefined ? {} : { name: options.name }),
|
|
165
|
+
}),
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
const body = await readJson(response, "File upload ticket request");
|
|
169
|
+
if (!response.ok) {
|
|
170
|
+
throw new CliError(
|
|
171
|
+
`File upload ticket request failed (${response.status}): ${responseError(body)}`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
const minted = parseMintedTicket(body);
|
|
175
|
+
if (minted === null) {
|
|
176
|
+
throw new CliError("File upload ticket response was invalid.");
|
|
177
|
+
}
|
|
178
|
+
return minted;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function nullableDimension(value: unknown): number | null | undefined {
|
|
182
|
+
if (value === null) return null;
|
|
183
|
+
return typeof value === "number" && Number.isFinite(value)
|
|
184
|
+
? value
|
|
185
|
+
: undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function parsePublishedFile(
|
|
189
|
+
body: unknown,
|
|
190
|
+
fileId: string,
|
|
191
|
+
uploadUrl: string,
|
|
192
|
+
): StoreFileUploadResult | null {
|
|
193
|
+
if (typeof body !== "object" || body === null) return null;
|
|
194
|
+
const { facts, url } = body as { facts?: unknown; url?: unknown };
|
|
195
|
+
if (typeof facts !== "object" || facts === null) return null;
|
|
196
|
+
const { fileKey, sizeBytes, contentType, width, height } = facts as {
|
|
197
|
+
fileKey?: unknown;
|
|
198
|
+
sizeBytes?: unknown;
|
|
199
|
+
contentType?: unknown;
|
|
200
|
+
width?: unknown;
|
|
201
|
+
height?: unknown;
|
|
202
|
+
};
|
|
203
|
+
const parsedWidth = nullableDimension(width);
|
|
204
|
+
const parsedHeight = nullableDimension(height);
|
|
205
|
+
if (
|
|
206
|
+
typeof fileKey !== "string" ||
|
|
207
|
+
!FILE_KEY_RE.test(fileKey) ||
|
|
208
|
+
typeof sizeBytes !== "number" ||
|
|
209
|
+
!Number.isSafeInteger(sizeBytes) ||
|
|
210
|
+
sizeBytes < 1 ||
|
|
211
|
+
typeof contentType !== "string" ||
|
|
212
|
+
parsedWidth === undefined ||
|
|
213
|
+
parsedHeight === undefined
|
|
214
|
+
) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
const servedUrl =
|
|
218
|
+
typeof url === "string" && /^https?:\/\//.test(url)
|
|
219
|
+
? url
|
|
220
|
+
: new URL(`/files/${fileKey}`, uploadUrl).toString();
|
|
221
|
+
return {
|
|
222
|
+
fileId,
|
|
223
|
+
fileKey,
|
|
224
|
+
url: servedUrl,
|
|
225
|
+
sizeBytes,
|
|
226
|
+
contentType,
|
|
227
|
+
width: parsedWidth,
|
|
228
|
+
height: parsedHeight,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async function putFile(
|
|
233
|
+
dependencies: StoreFileUploaderDependencies,
|
|
234
|
+
options: StoreFileUploadOptions,
|
|
235
|
+
fileId: string,
|
|
236
|
+
minted: MintedTicket,
|
|
237
|
+
): Promise<StoreFileUploadResult> {
|
|
238
|
+
const response = await dependencies.fetchFn(minted.uploadUrl, {
|
|
239
|
+
method: "PUT",
|
|
240
|
+
headers: {
|
|
241
|
+
Authorization: `Bearer ${minted.ticket}`,
|
|
242
|
+
"Content-Type": options.contentType ?? DEFAULT_CONTENT_TYPE,
|
|
243
|
+
},
|
|
244
|
+
body: Uint8Array.from(options.data).buffer,
|
|
245
|
+
});
|
|
246
|
+
const body = await readJson(response, "File upload receiver");
|
|
247
|
+
if (response.status !== 201) {
|
|
248
|
+
throw new CliError(
|
|
249
|
+
`File upload receiver failed (${response.status}): ${responseError(body)}`,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
const file = parsePublishedFile(body, fileId, minted.uploadUrl);
|
|
253
|
+
if (file === null) {
|
|
254
|
+
throw new CliError("File upload receiver returned an invalid record.");
|
|
255
|
+
}
|
|
256
|
+
return file;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function createStoreFileUploader(
|
|
260
|
+
dependencies: StoreFileUploaderDependencies,
|
|
261
|
+
): StoreFileUploader {
|
|
262
|
+
return async (options) => {
|
|
263
|
+
validateOptions(options);
|
|
264
|
+
const fileId = options.fileId ?? crypto.randomUUID();
|
|
265
|
+
const minted = await mintTicket(dependencies, options, fileId);
|
|
266
|
+
return putFile(dependencies, options, fileId, minted);
|
|
267
|
+
};
|
|
268
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// owner: klu
|
|
2
|
+
import type { CliWebSocket, JSONValue } from "./client.js";
|
|
3
|
+
import { CliError } from "./errors.js";
|
|
4
|
+
|
|
5
|
+
export async function sendStoreRequest<
|
|
6
|
+
T extends { request_id: string },
|
|
7
|
+
>(options: {
|
|
8
|
+
socket: CliWebSocket;
|
|
9
|
+
storeTypeId: string;
|
|
10
|
+
instanceId: string;
|
|
11
|
+
requestId: string;
|
|
12
|
+
timeoutMs: number;
|
|
13
|
+
request: JSONValue;
|
|
14
|
+
isResponse: (value: unknown) => value is T;
|
|
15
|
+
maxResponseBytes?: number;
|
|
16
|
+
}): Promise<T> {
|
|
17
|
+
const { socket, storeTypeId, instanceId, timeoutMs, request } = options;
|
|
18
|
+
|
|
19
|
+
return new Promise<T>((resolve, reject) => {
|
|
20
|
+
const previousOnMessage = socket.onmessage;
|
|
21
|
+
const previousOnClose = socket.onclose;
|
|
22
|
+
const previousOnError = socket.onerror;
|
|
23
|
+
|
|
24
|
+
const cleanup = () => {
|
|
25
|
+
clearTimeout(timeout);
|
|
26
|
+
socket.onmessage = previousOnMessage;
|
|
27
|
+
socket.onclose = previousOnClose;
|
|
28
|
+
socket.onerror = previousOnError;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// biome-ignore lint: CLI WebSocket operations need a wall-clock response timeout.
|
|
32
|
+
const timeout = setTimeout(() => {
|
|
33
|
+
cleanup();
|
|
34
|
+
reject(
|
|
35
|
+
new CliError(
|
|
36
|
+
`Timed out waiting for store response after ${timeoutMs}ms.`,
|
|
37
|
+
),
|
|
38
|
+
);
|
|
39
|
+
}, timeoutMs);
|
|
40
|
+
|
|
41
|
+
socket.onmessage = (event) => {
|
|
42
|
+
try {
|
|
43
|
+
const response = matchingResponse(event.data, options);
|
|
44
|
+
if (response === undefined) return;
|
|
45
|
+
cleanup();
|
|
46
|
+
resolve(response);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
cleanup();
|
|
49
|
+
reject(error);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
socket.onclose = (event) => {
|
|
53
|
+
cleanup();
|
|
54
|
+
reject(
|
|
55
|
+
new CliError(
|
|
56
|
+
`Store WebSocket closed before response (${event.code}${event.reason ? `: ${event.reason}` : ""}).`,
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
};
|
|
60
|
+
socket.onerror = () => {
|
|
61
|
+
cleanup();
|
|
62
|
+
reject(new CliError("Store WebSocket failed before response."));
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
socket.send(
|
|
67
|
+
serializeStoreEnvelope(
|
|
68
|
+
storeTypeId,
|
|
69
|
+
instanceId,
|
|
70
|
+
JSON.stringify(request),
|
|
71
|
+
),
|
|
72
|
+
);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
cleanup();
|
|
75
|
+
reject(error);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function matchingResponse<T extends { request_id: string }>(
|
|
81
|
+
data: unknown,
|
|
82
|
+
options: {
|
|
83
|
+
storeTypeId: string;
|
|
84
|
+
instanceId: string;
|
|
85
|
+
requestId: string;
|
|
86
|
+
isResponse: (value: unknown) => value is T;
|
|
87
|
+
maxResponseBytes?: number;
|
|
88
|
+
},
|
|
89
|
+
): T | undefined {
|
|
90
|
+
if (typeof data !== "string") return undefined;
|
|
91
|
+
const envelope = parseStoreEnvelope(data);
|
|
92
|
+
if (
|
|
93
|
+
envelope.storeTypeId !== options.storeTypeId ||
|
|
94
|
+
envelope.instanceId !== options.instanceId
|
|
95
|
+
)
|
|
96
|
+
return undefined;
|
|
97
|
+
let response: unknown;
|
|
98
|
+
try {
|
|
99
|
+
response = JSON.parse(envelope.message);
|
|
100
|
+
} catch {
|
|
101
|
+
throw new CliError("Invalid JSON in store response.");
|
|
102
|
+
}
|
|
103
|
+
if (
|
|
104
|
+
!options.isResponse(response) ||
|
|
105
|
+
response.request_id !== options.requestId
|
|
106
|
+
)
|
|
107
|
+
return undefined;
|
|
108
|
+
// The user-router also broadcasts unrelated store messages on this socket.
|
|
109
|
+
if (
|
|
110
|
+
options.maxResponseBytes !== undefined &&
|
|
111
|
+
new TextEncoder().encode(data).byteLength > options.maxResponseBytes
|
|
112
|
+
)
|
|
113
|
+
throw new CliError("Store response exceeds the debug snapshot byte limit.");
|
|
114
|
+
return response;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface ParsedStoreEnvelope {
|
|
118
|
+
storeTypeId: string;
|
|
119
|
+
instanceId: string;
|
|
120
|
+
message: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function serializeStoreEnvelope(
|
|
124
|
+
storeTypeId: string,
|
|
125
|
+
instanceId: string,
|
|
126
|
+
message: string,
|
|
127
|
+
): string {
|
|
128
|
+
for (const field of ["E", storeTypeId, instanceId]) {
|
|
129
|
+
if (field.includes("#")) {
|
|
130
|
+
throw new CliError(`Store envelope field must not contain "#": ${field}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return `3#E#${storeTypeId}#${instanceId}#${message}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function parseStoreEnvelope(raw: string): ParsedStoreEnvelope {
|
|
137
|
+
const firstDelim = raw.indexOf("#");
|
|
138
|
+
if (firstDelim === -1) {
|
|
139
|
+
throw new CliError("Invalid store response envelope: missing field count.");
|
|
140
|
+
}
|
|
141
|
+
const fieldCount = Number.parseInt(raw.slice(0, firstDelim), 10);
|
|
142
|
+
if (fieldCount !== 3) {
|
|
143
|
+
throw new CliError(
|
|
144
|
+
`Invalid store response envelope: expected 3 fields, got ${fieldCount}.`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let offset = firstDelim + 1;
|
|
149
|
+
const fields: string[] = [];
|
|
150
|
+
for (let i = 0; i < fieldCount; i += 1) {
|
|
151
|
+
const next = raw.indexOf("#", offset);
|
|
152
|
+
if (next === -1) {
|
|
153
|
+
throw new CliError("Invalid store response envelope: truncated fields.");
|
|
154
|
+
}
|
|
155
|
+
fields.push(raw.slice(offset, next));
|
|
156
|
+
offset = next + 1;
|
|
157
|
+
}
|
|
158
|
+
if (fields[0] !== "E") {
|
|
159
|
+
throw new CliError(
|
|
160
|
+
`Invalid store response envelope type: expected E, got ${fields[0] ?? ""}.`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
storeTypeId: fields[1] ?? "",
|
|
165
|
+
instanceId: fields[2] ?? "",
|
|
166
|
+
message: raw.slice(offset),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// owner: aaronroe
|
|
2
|
+
|
|
3
|
+
import { Gzip } from "fflate";
|
|
4
|
+
import { buildTar } from "./tar.js";
|
|
5
|
+
|
|
6
|
+
/** Mirrors `CANONICAL_GZIP_BLOCK_BYTES` in `@lib/tar-parser`'s `tar-gz-stream.ts`. */
|
|
7
|
+
const CANONICAL_GZIP_BLOCK_BYTES = 64 * 1024;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The same tar, gzipped the way the server's publish re-encode does
|
|
11
|
+
* (`buildStreamingCanonicalTarGz` in `@lib/tar-parser`): fflate's streaming
|
|
12
|
+
* encoder fed fixed 64 KiB pushes. Its bytes differ from `buildTarGz`'s for
|
|
13
|
+
* larger inputs, and a client bundle's id is the digest of these bytes, so a
|
|
14
|
+
* bundle the server stores as uploaded keeps the id its re-encode gave it.
|
|
15
|
+
*/
|
|
16
|
+
export function buildCanonicalTarGz(
|
|
17
|
+
files: Map<string, Uint8Array>,
|
|
18
|
+
): Uint8Array {
|
|
19
|
+
const tar = buildTar(files);
|
|
20
|
+
const chunks: Uint8Array[] = [];
|
|
21
|
+
const gzip = new Gzip({ mtime: 0 }, (chunk) => chunks.push(chunk));
|
|
22
|
+
for (let offset = 0; ; offset += CANONICAL_GZIP_BLOCK_BYTES) {
|
|
23
|
+
const end = offset + CANONICAL_GZIP_BLOCK_BYTES;
|
|
24
|
+
// A full final block is pushed without the final flag, then an empty one
|
|
25
|
+
// with it, exactly as the server's re-blocking encoder finishes.
|
|
26
|
+
if (end > tar.byteLength) {
|
|
27
|
+
gzip.push(tar.subarray(offset), true);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
gzip.push(tar.subarray(offset, end), false);
|
|
31
|
+
}
|
|
32
|
+
const out = new Uint8Array(
|
|
33
|
+
chunks.reduce((n, chunk) => n + chunk.byteLength, 0),
|
|
34
|
+
);
|
|
35
|
+
let written = 0;
|
|
36
|
+
for (const chunk of chunks) {
|
|
37
|
+
out.set(chunk, written);
|
|
38
|
+
written += chunk.byteLength;
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// owner: aaronroe
|
|
2
|
+
// Mirrored in the standalone CLI (which ships without workspace dependencies).
|
|
3
|
+
// The CLI's archive tests enforce byte-for-byte equality between these copies.
|
|
4
|
+
|
|
5
|
+
export const MAX_TAR_PATH_BYTES = 4096;
|
|
6
|
+
export const MAX_TAR_EXTENSION_BYTES = 8192;
|
|
7
|
+
|
|
8
|
+
export function maxTarExtensionCount(maxEntries: number): number {
|
|
9
|
+
return maxEntries * 2 + 2;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Budget every permitted extension, including its header and padded payload. */
|
|
13
|
+
export function maxTarExtensionArchiveBytes(maxEntries: number): number {
|
|
14
|
+
const blocksPerExtension = 1 + Math.ceil(MAX_TAR_EXTENSION_BYTES / 512);
|
|
15
|
+
return maxTarExtensionCount(maxEntries) * blocksPerExtension * 512;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const encoder = new TextEncoder();
|
|
19
|
+
// ignoreBOM keeps a leading U+FEFF, a valid filename character, in the path.
|
|
20
|
+
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
21
|
+
|
|
22
|
+
type Attributes = { path?: string | null; size?: number | null };
|
|
23
|
+
|
|
24
|
+
function checkedPath(path: string): string {
|
|
25
|
+
if (path.includes("\0")) throw new Error("NUL in tar entry path");
|
|
26
|
+
if (encoder.encode(path).byteLength > MAX_TAR_PATH_BYTES) {
|
|
27
|
+
throw new Error(`Tar entry path exceeds ${MAX_TAR_PATH_BYTES} bytes`);
|
|
28
|
+
}
|
|
29
|
+
return path;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** PAX lengths count UTF-8 bytes, including the length field and newline. */
|
|
33
|
+
export function encodePaxPath(path: string): Uint8Array {
|
|
34
|
+
checkedPath(path);
|
|
35
|
+
const record = encoder.encode(` path=${path}\n`);
|
|
36
|
+
let length = record.byteLength + 1;
|
|
37
|
+
while (String(length).length + record.byteLength !== length) {
|
|
38
|
+
length = String(length).length + record.byteLength;
|
|
39
|
+
}
|
|
40
|
+
return encoder.encode(`${length} path=${path}\n`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function paxRecord(
|
|
44
|
+
data: Uint8Array,
|
|
45
|
+
offset: number,
|
|
46
|
+
): { key: string; value: Uint8Array; end: number } {
|
|
47
|
+
const space = data.indexOf(0x20, offset);
|
|
48
|
+
const digits = decoder.decode(data.subarray(offset, space));
|
|
49
|
+
const length = Number(digits);
|
|
50
|
+
const end = offset + length;
|
|
51
|
+
if (
|
|
52
|
+
space < offset ||
|
|
53
|
+
!/^[1-9][0-9]*$/.test(digits) ||
|
|
54
|
+
!Number.isSafeInteger(length) ||
|
|
55
|
+
end > data.byteLength ||
|
|
56
|
+
end <= space + 2 ||
|
|
57
|
+
data[end - 1] !== 0x0a
|
|
58
|
+
) {
|
|
59
|
+
throw new Error("Malformed PAX record length");
|
|
60
|
+
}
|
|
61
|
+
const record = data.subarray(space + 1, end - 1);
|
|
62
|
+
const equals = record.indexOf(0x3d);
|
|
63
|
+
if (equals < 1) throw new Error("Malformed PAX record");
|
|
64
|
+
// The value is decoded by the consumer that reads it: libarchive stores
|
|
65
|
+
// extended attributes (SCHILY.xattr.*, e.g. the binary
|
|
66
|
+
// `com.apple.provenance` macOS stamps on every new file) as raw bytes.
|
|
67
|
+
return {
|
|
68
|
+
key: decoder.decode(record.subarray(0, equals)),
|
|
69
|
+
value: record.subarray(equals + 1),
|
|
70
|
+
end,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function paxSize(value: string): number | null {
|
|
75
|
+
if (value === "") return null;
|
|
76
|
+
const size = Number(value);
|
|
77
|
+
if (!/^[0-9]+$/.test(value) || !Number.isSafeInteger(size))
|
|
78
|
+
throw new Error("Invalid PAX size");
|
|
79
|
+
return size;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parsePax(data: Uint8Array): Attributes {
|
|
83
|
+
const attributes: Attributes = {};
|
|
84
|
+
let offset = 0;
|
|
85
|
+
while (offset < data.byteLength) {
|
|
86
|
+
const { key, value, end } = paxRecord(data, offset);
|
|
87
|
+
if (key === "path") {
|
|
88
|
+
const path = decoder.decode(value);
|
|
89
|
+
attributes.path = path === "" ? null : checkedPath(path);
|
|
90
|
+
}
|
|
91
|
+
if (key === "size") attributes.size = paxSize(decoder.decode(value));
|
|
92
|
+
if (key.startsWith("GNU.sparse."))
|
|
93
|
+
throw new Error("Unsupported sparse tar entry");
|
|
94
|
+
offset = end;
|
|
95
|
+
}
|
|
96
|
+
return attributes;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function isTarExtension(typeFlag: number): boolean {
|
|
100
|
+
return typeFlag === 0x78 || typeFlag === 0x67 || typeFlag === 0x4c;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Resolve names BEFORE the caller's path validation, duplicate checks and limits. */
|
|
104
|
+
export class TarExtensions {
|
|
105
|
+
private global: Attributes = {};
|
|
106
|
+
private local: Attributes | undefined;
|
|
107
|
+
|
|
108
|
+
consume(typeFlag: number, data: Uint8Array): void {
|
|
109
|
+
if (data.byteLength > MAX_TAR_EXTENSION_BYTES)
|
|
110
|
+
throw new Error("Tar extension is too large");
|
|
111
|
+
if (typeFlag === 0x4c) {
|
|
112
|
+
const nul = data.indexOf(0);
|
|
113
|
+
if (nul < 0 || nul !== data.byteLength - 1)
|
|
114
|
+
throw new Error("Malformed GNU long path");
|
|
115
|
+
this.local = {
|
|
116
|
+
...this.local,
|
|
117
|
+
path: checkedPath(decoder.decode(data.subarray(0, nul))),
|
|
118
|
+
};
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const attributes = parsePax(data);
|
|
122
|
+
if (typeFlag === 0x67) this.global = { ...this.global, ...attributes };
|
|
123
|
+
else this.local = { ...this.local, ...attributes };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
resolve(path: string, size: number): { path: string; size: number } {
|
|
127
|
+
const attributes = { ...this.global, ...this.local };
|
|
128
|
+
this.local = undefined;
|
|
129
|
+
return {
|
|
130
|
+
path: checkedPath(attributes.path ?? path),
|
|
131
|
+
size: attributes.size ?? size,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
finish(): void {
|
|
136
|
+
if (this.local !== undefined)
|
|
137
|
+
throw new Error("Tar extension has no following entry");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type TarHeaderInput = { path: string; size: number; typeFlag?: number };
|
|
142
|
+
|
|
143
|
+
/** One regular-file header, optionally preceded by a standard PAX path header. */
|
|
144
|
+
export function tarFileHeaders(
|
|
145
|
+
path: string,
|
|
146
|
+
size: number,
|
|
147
|
+
buildHeader: (input: TarHeaderInput) => Uint8Array,
|
|
148
|
+
): Uint8Array[] {
|
|
149
|
+
checkedPath(path);
|
|
150
|
+
if (encoder.encode(path).byteLength <= 100)
|
|
151
|
+
return [buildHeader({ path, size })];
|
|
152
|
+
const pax = encodePaxPath(path);
|
|
153
|
+
return [
|
|
154
|
+
buildHeader({ path: "PaxHeader", size: pax.byteLength, typeFlag: 0x78 }),
|
|
155
|
+
pax,
|
|
156
|
+
new Uint8Array((512 - (pax.byteLength % 512)) % 512),
|
|
157
|
+
buildHeader({ path: "PaxFile", size }),
|
|
158
|
+
];
|
|
159
|
+
}
|