@pwfabric/pack 0.0.0-stage → 1.0.0-rc.1
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/COMPAT.md +136 -0
- package/LICENSE +202 -0
- package/README.md +34 -2
- package/dist/archive.d.ts +56 -0
- package/dist/archive.js +151 -0
- package/dist/archive.js.map +1 -0
- package/dist/content.d.ts +34 -0
- package/dist/content.js +49 -0
- package/dist/content.js.map +1 -0
- package/dist/folder.d.ts +27 -0
- package/dist/folder.js +85 -0
- package/dist/folder.js.map +1 -0
- package/dist/hash.d.ts +21 -0
- package/dist/hash.js +41 -0
- package/dist/hash.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.d.ts +53 -0
- package/dist/manifest.js +94 -0
- package/dist/manifest.js.map +1 -0
- package/dist/media.d.ts +77 -0
- package/dist/media.js +135 -0
- package/dist/media.js.map +1 -0
- package/dist/paths.d.ts +44 -0
- package/dist/paths.js +126 -0
- package/dist/paths.js.map +1 -0
- package/dist/tar.d.ts +39 -0
- package/dist/tar.js +277 -0
- package/dist/tar.js.map +1 -0
- package/dist/types.d.ts +59 -0
- package/dist/types.js +18 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +63 -0
- package/dist/validate.js +267 -0
- package/dist/validate.js.map +1 -0
- package/package.json +55 -3
package/COMPAT.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# pwpack archive compatibility
|
|
2
|
+
|
|
3
|
+
Five independent pieces of code write or read
|
|
4
|
+
pwpack archives. This note records how each one frames the archive, where
|
|
5
|
+
they agree, where they disagree, and what this package does about it. The
|
|
6
|
+
test file `src/__tests__/compat.test.ts` re-creates each producer's framing
|
|
7
|
+
(`src/__fixtures__/producers.ts`) and proves the verdicts below.
|
|
8
|
+
|
|
9
|
+
The producers:
|
|
10
|
+
|
|
11
|
+
| | Producer | Role |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| P1 | Authoring plugin packer | Packs a developer's source folder for upload |
|
|
14
|
+
| P2 | First-party pack exporter (`export-tgz`) | Builds the source tgz of the first-party packs |
|
|
15
|
+
| P3 | App generator delivery (`.pwpack` of a built app) | Delivers a generated app as the canonical folder |
|
|
16
|
+
| P4 | A hosting platform's pack build service | Rebuilds an uploaded source tree into the published tgz |
|
|
17
|
+
| P5 | A hosting platform's project export and its extractor | Exports a project as a pack; opens every incoming tgz |
|
|
18
|
+
|
|
19
|
+
## Framing, side by side
|
|
20
|
+
|
|
21
|
+
| | P1 plugin | P2 exporter | P3 generator | P4 build service | P5 export / extractor | **this package** |
|
|
22
|
+
|---|---|---|---|---|---|---|
|
|
23
|
+
| Tar implementation | hand-written | hand-written (shared copy with P3) | hand-written | hand-written | hand-written | hand-written |
|
|
24
|
+
| Header format | pre-ustar (no magic) | ustar | ustar | ustar | ustar | ustar |
|
|
25
|
+
| Wrapper dir | none | none | none | `package/` | none | none (reads `package/`) |
|
|
26
|
+
| Layout on the wire | rootless: `atoms/` stripped, `app/surfaces/*` → `surfaces/*` | rootless, same as P1 | **canonical** (`pwpack.json`, `app/`, `atoms/`) | as the source tree (either) | rootless + app envelope at root | as the folder |
|
|
27
|
+
| Entry order | `readdir` order (unsorted) | sorted families, then `pwpack.json`, `package.json` appended | sorted (UTF-16) | all dirs (sorted), then files (sorted) | insertion order, `pwfabric.manifest.json` first | byte order, dirs interleaved |
|
|
28
|
+
| Directory entries | no | no | no | yes, 0755 | no | yes, 0755 |
|
|
29
|
+
| File mode | 0644 | 0644 | 0644 | 0644 | 0644 | 0644 |
|
|
30
|
+
| uid/gid, uname/gname | 0, empty | 0, empty | 0, empty | 0, empty | 0, empty | 0, empty |
|
|
31
|
+
| mtime | **wall clock** | 0 | 0 | 0 | **wall clock** | 0 |
|
|
32
|
+
| Paths > 100 bytes | **silently cut to 100 bytes** | throws | ustar prefix (shortest), else throws | ustar prefix (by char count), else throws | ustar prefix (longest), else PAX `path` | ustar prefix (longest), else PAX `path` |
|
|
33
|
+
| Text bytes | as-is | as-is | **CRLF → LF** | as-is (block/capability manifests rewritten) | as-is | as-is |
|
|
34
|
+
| gzip level | default (6) | 9 | 9 | 9 | default (6) | 9 |
|
|
35
|
+
| gzip OS byte | platform's | platform's | **forced 3** | platform's | platform's | forced 3 |
|
|
36
|
+
| `contentHash` | sha256 of tgz | prints sha256 of tgz | sha256 of tgz (bare hex in its ledger) | sha256 of tgz | sha256 of tgz | `hashPack` (content) + `legacyContentHash` (tgz) |
|
|
37
|
+
|
|
38
|
+
## Where they agree
|
|
39
|
+
|
|
40
|
+
- Rootless archives, POSIX `/` separators, no leading `/`.
|
|
41
|
+
- Regular files 0644, uid/gid 0, empty owner names.
|
|
42
|
+
- A `contentHash` of `sha256:` + hex of the compressed archive, and a
|
|
43
|
+
`storageRef` of `pwpacks/<id without @, / → ->/<version>/<12 hex>.tgz`
|
|
44
|
+
(P1 and P4 agree for `@pwpack/...` ids). `deriveManifest` reproduces both.
|
|
45
|
+
- Atom ids come from the contracts' `classifyAtom`; when two files classify
|
|
46
|
+
as one atom the first in sorted order wins (P1, P4).
|
|
47
|
+
- The extractor (P5) refuses `..` segments, absolute and drive paths, NUL,
|
|
48
|
+
symlink and hardlink members, and caps entries (5 000), per-file size
|
|
49
|
+
(20 MiB), total size (50 MiB) and decompressed size (128 MiB). This
|
|
50
|
+
package's defaults are the same numbers, plus a 64 MiB cap on the
|
|
51
|
+
compressed input.
|
|
52
|
+
|
|
53
|
+
## Where they disagree — and what matters
|
|
54
|
+
|
|
55
|
+
1. **Determinism is not real for P1 and P5.** Both stamp the current time
|
|
56
|
+
into every header, so the same folder never produces the same archive
|
|
57
|
+
twice and their `contentHash` identifies a moment, not content.
|
|
58
|
+
2. **Cross-platform determinism is not real for P2 and P4.** zlib stamps the
|
|
59
|
+
OS it was built for into gzip byte 9 (measured here: 10 on Windows, 3 on
|
|
60
|
+
Linux). P4's header comment claims identical bytes across OSes; it does
|
|
61
|
+
not pin the byte. P3 does. This package pins it to 3, so a Linux-written
|
|
62
|
+
archive keeps its hash. Remaining assumption: the same Node zlib build
|
|
63
|
+
compresses identically on every platform (P3 relies on this too). The
|
|
64
|
+
framing-independent identity is `hashPack`, which does not depend on it.
|
|
65
|
+
3. **P1 corrupts long paths.** Names over 100 bytes are truncated in the
|
|
66
|
+
header with no prefix and no error. Such an archive reads, but the path
|
|
67
|
+
read back is the first 100 bytes of the real one. Nothing can recover it.
|
|
68
|
+
4. **P3 rewrites text.** CRLF becomes LF before packing, so its archive of a
|
|
69
|
+
folder with CRLF files has a different content hash than the folder on
|
|
70
|
+
disk. This package never transforms bytes.
|
|
71
|
+
5. **The rootless wire cannot carry an app.** P1 and P2 move
|
|
72
|
+
`app/surfaces/<key>.receipt.json` to `surfaces/<key>.receipt.json`, where
|
|
73
|
+
they classify as shelf `surface` atoms; `pwpack.json` still names
|
|
74
|
+
`app.entry`, which `validatePack` then reports as `APP_SURFACE_MISSING`.
|
|
75
|
+
P3 is the only producer that ships the canonical folder.
|
|
76
|
+
6. **`pwpack.json` `requires` has three shapes.** The contract schema says
|
|
77
|
+
an array of strings. The first-party packs (P2) write an array of
|
|
78
|
+
`{ id, range }` objects — every first-party pack with a dependency fails
|
|
79
|
+
the contract schema (`IDENTITY_INVALID`). P4 reads `requires` only when it
|
|
80
|
+
is an object (a record) and otherwise falls back to `package.json`.
|
|
81
|
+
**Resolved:** the contract schema now accepts `{ id, range }` entries
|
|
82
|
+
(the form producers should write) and bare names (older files). P4 must
|
|
83
|
+
learn to read the array form when it adopts this package.
|
|
84
|
+
7. **P3 writes `pwpack.json` values the schema refuses:** a pin with
|
|
85
|
+
`hash: null` when the catalog had no hash, and `app.entry: null` when a
|
|
86
|
+
project has no surfaces. It also writes `app.title`, `app.description`
|
|
87
|
+
and `app.icon` (now part of the schema) (they are stripped, not
|
|
88
|
+
refused).
|
|
89
|
+
8. **Identity location differs.** P4 also looks for `src/pwpack.json` and
|
|
90
|
+
falls back to `package.json` for id/version/requires; P1 synthesises a
|
|
91
|
+
`package.json` when there is none; P5's export carries no `pwpack.json` at
|
|
92
|
+
all, only `pwfabric.manifest.json` plus `project.json`,
|
|
93
|
+
`navigation.json`, `connections.json`, `appearance.json` at the root.
|
|
94
|
+
9. **Sorting.** P3 and P4 sort with JavaScript's default (UTF-16 code
|
|
95
|
+
units), P2 with `<`, and P4 sorts manifest atoms with `localeCompare`.
|
|
96
|
+
This package uses UTF-8 byte order everywhere; it differs from the
|
|
97
|
+
others only for non-ASCII or mixed-case names (`localeCompare` puts `a`
|
|
98
|
+
before `B`).
|
|
99
|
+
10. **Ignored files differ.** P4 skips every dot-entry and `build/`, `out/`;
|
|
100
|
+
P1 skips `node_modules`, `.git`, `.DS_Store`, `dist`, `*.tgz`; P2 skips
|
|
101
|
+
tests and logs. `readFolder` skips `node_modules`, `dist`, `.git` and OS
|
|
102
|
+
junk at any depth and `*.pwpack` at the root; anything else is the
|
|
103
|
+
caller's decision (`ignore` option).
|
|
104
|
+
|
|
105
|
+
## What `readPwpack` accepts from each
|
|
106
|
+
|
|
107
|
+
| Producer | Reads | `validatePack` verdict |
|
|
108
|
+
|---|---|---|
|
|
109
|
+
| P1 plugin, a pack | yes | ok; `LAYOUT_ROOTLESS`, `LEGACY_PACKAGE_JSON` |
|
|
110
|
+
| P1 plugin, an app | yes | **error** `APP_SURFACE_MISSING` (point 5) |
|
|
111
|
+
| P1 plugin, path > 100 bytes | yes, truncated path | depends; the data is already lost (point 3) |
|
|
112
|
+
| P2 exporter, pack without dependencies | yes | ok; `LAYOUT_ROOTLESS`, `LEGACY_PACKAGE_JSON` |
|
|
113
|
+
| P2 exporter, pack with dependencies | yes | **error** `IDENTITY_INVALID` (point 6) |
|
|
114
|
+
| P3 generator delivery | yes, identical folder | ok, no issues; **error** `IDENTITY_INVALID` for null pins / entry (point 7) |
|
|
115
|
+
| P4 build service | yes, `package/` removed | as its source folder |
|
|
116
|
+
| P5 project export | yes, PAX names included | **error** `IDENTITY_MISSING`; `UNKNOWN_TOP_LEVEL` for the envelope files |
|
|
117
|
+
|
|
118
|
+
Verified against the real first-party packs: the exporter's archives of two
|
|
119
|
+
of them read, round-trip with an unchanged `hashPack`, and validate as
|
|
120
|
+
described (98 and 6 atoms).
|
|
121
|
+
|
|
122
|
+
Tolerated legacy framing: `package/` on every member, `./` prefixes,
|
|
123
|
+
directory entries, pre-ustar headers, the old GNU magic, signed-byte header
|
|
124
|
+
checksums, PAX `path` records, GNU `L` long names, PAX global headers (ignored)
|
|
125
|
+
and bare uncompressed tar. Refused: everything P5 refuses, plus devices and
|
|
126
|
+
FIFOs, a path appearing twice, malformed headers and base-256 sizes.
|
|
127
|
+
|
|
128
|
+
## What this package does not do
|
|
129
|
+
|
|
130
|
+
- It does not translate between layouts. A rootless archive is read as
|
|
131
|
+
rootless; `validatePack` reports the layout (`canonical`, `rootless`,
|
|
132
|
+
`mixed`) so a caller can decide.
|
|
133
|
+
- It does not rewrite block or capability manifests (P4 derives
|
|
134
|
+
`definition.props` and `config.fields` from source while packing). That is
|
|
135
|
+
a build step, not packaging.
|
|
136
|
+
- It does not preserve executable bits or timestamps.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @pwfabric/pack
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Reads, validates, hashes and writes the pwpack folder and its `.pwpack`
|
|
4
|
+
archive. Node only, no runtime dependencies besides the contracts package.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { readFolder, packContent, validatePack, writePwpack, readPwpack, hashPack, deriveManifest } from '@pwfabric/pack'
|
|
8
|
+
|
|
9
|
+
const source = await readFolder('./my-pack') // folder on disk -> memory (tooling included)
|
|
10
|
+
const report = validatePack(source) // { ok, issues, identity, layout, atoms }
|
|
11
|
+
const pack = packContent(source) // only what belongs to the pack
|
|
12
|
+
const archive = await writePwpack(pack) // deterministic gzipped tar
|
|
13
|
+
const again = await readPwpack(archive) // archive -> memory (limits apply)
|
|
14
|
+
hashPack(again) === hashPack(pack) // content hash, framing-independent
|
|
15
|
+
const manifest = deriveManifest(pack, { archive }) // PwpackManifest
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
- **Folder**: `pwpack.json`, `app/`, `atoms/`, optional `media/` with
|
|
19
|
+
`media/provenance.json`. `packContent` keeps those, root `LICENSE` /
|
|
20
|
+
`NOTICE` / `README.md` and the legacy rootless atom families, and drops an
|
|
21
|
+
author's tooling (`package.json`, `tsconfig.json`, `scripts/`, root
|
|
22
|
+
`test/` …). `hashPack` hashes the pack content only, so a source folder
|
|
23
|
+
and its archive have the same content hash.
|
|
24
|
+
- **Archive**: rootless ustar, byte-ordered names, mode 0644/0755, uid/gid 0,
|
|
25
|
+
mtime 0, PAX `path` records for paths no ustar split fits; gzip level 9,
|
|
26
|
+
header mtime 0, OS byte 3. Bytes are never transformed (CRLF stays CRLF).
|
|
27
|
+
- **Errors**: I/O and archive problems throw `PackError` with a `code`;
|
|
28
|
+
content problems are returned by `validatePack` as issues.
|
|
29
|
+
|
|
30
|
+
See [COMPAT.md](./COMPAT.md) for how this matches the archives other tools
|
|
31
|
+
produce.
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
Apache-2.0
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type PackFolder } from './types.js';
|
|
2
|
+
/** zlib level used for every archive. Part of the byte-identity contract. */
|
|
3
|
+
export declare const GZIP_LEVEL = 9;
|
|
4
|
+
/** RFC 1952 OS byte written into every archive (3 = Unix), whatever platform wrote it. */
|
|
5
|
+
export declare const GZIP_OS_UNIX = 3;
|
|
6
|
+
/** Ceilings `readPwpack` enforces. Every field is optional when passed. */
|
|
7
|
+
export interface PwpackLimits {
|
|
8
|
+
/** Size of the archive as given (compressed). */
|
|
9
|
+
readonly maxArchiveBytes: number;
|
|
10
|
+
/** Size of the tar stream after gunzip — the decompression-bomb ceiling. */
|
|
11
|
+
readonly maxTarBytes: number;
|
|
12
|
+
/** Number of tar members (files, directories, links). */
|
|
13
|
+
readonly maxEntries: number;
|
|
14
|
+
/** Size of one file. */
|
|
15
|
+
readonly maxEntryBytes: number;
|
|
16
|
+
/** Sum of all file sizes. */
|
|
17
|
+
readonly maxTotalBytes: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Defaults, chosen to match the most generous limits an existing extractor
|
|
21
|
+
* enforces (5 000 entries, 20 MiB per file, 50 MiB of files, 128 MiB of tar)
|
|
22
|
+
* so nothing that opens today stops opening here.
|
|
23
|
+
*/
|
|
24
|
+
export declare const DEFAULT_LIMITS: PwpackLimits;
|
|
25
|
+
/**
|
|
26
|
+
* The deterministic `.pwpack` bytes of a folder.
|
|
27
|
+
*
|
|
28
|
+
* Tar: ustar, one entry per file plus one per implied directory, names in
|
|
29
|
+
* byte order, mode 0644 / 0755, uid/gid 0, empty uname/gname, mtime 0; a
|
|
30
|
+
* path that fits no ustar name/prefix split travels in a PAX `path` record.
|
|
31
|
+
* Gzip: level 9, header mtime 0, no file name, OS byte fixed to Unix (zlib
|
|
32
|
+
* otherwise stamps the platform it was built for, so the same folder hashed
|
|
33
|
+
* differently on Windows and Linux). File bytes are written exactly as given.
|
|
34
|
+
*
|
|
35
|
+
* Throws {@link PackError} `UNSAFE_PATH` if any path is unsafe and
|
|
36
|
+
* `PATH_CONFLICT` if a path is both a file and a directory — the writer
|
|
37
|
+
* never produces an archive a reader would have to refuse.
|
|
38
|
+
*/
|
|
39
|
+
export declare function writePwpack(pack: PackFolder): Promise<Uint8Array>;
|
|
40
|
+
/**
|
|
41
|
+
* Open a `.pwpack` (or any pwpack tgz) into memory.
|
|
42
|
+
*
|
|
43
|
+
* Accepted: gzip-compressed tar (the normal case) and a bare uncompressed
|
|
44
|
+
* tar. Tolerated, from layouts existing producers emit: a `package/` wrapper
|
|
45
|
+
* on EVERY file (removed; a partial wrapper is kept as a plain directory),
|
|
46
|
+
* `./` prefixes, explicit directory entries (ignored), ustar `prefix` split
|
|
47
|
+
* names, PAX `path` records and GNU long names, header-less mtimes and any
|
|
48
|
+
* mode. Refused with {@link PackError}: unsafe paths (`UNSAFE_PATH`), link
|
|
49
|
+
* members (`LINK_ENTRY`), devices / FIFOs (`UNSUPPORTED_ENTRY`), a path
|
|
50
|
+
* present twice (`DUPLICATE_ENTRY`), a path that is both a file and a
|
|
51
|
+
* directory — of another file or of a directory member (`PATH_CONFLICT`),
|
|
52
|
+
* malformed streams (`GZIP_INVALID`,
|
|
53
|
+
* `TAR_INVALID`) and every crossed limit (`PACK_TOO_LARGE`,
|
|
54
|
+
* `ENTRY_TOO_LARGE`, `TOO_MANY_ENTRIES`).
|
|
55
|
+
*/
|
|
56
|
+
export declare function readPwpack(bytes: Uint8Array, limits?: Partial<PwpackLimits>): Promise<PackFolder>;
|
package/dist/archive.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `.pwpack` archive: a deterministic gzipped tar of the folder, rootless.
|
|
3
|
+
*
|
|
4
|
+
* @module @pwfabric/pack/archive
|
|
5
|
+
*/
|
|
6
|
+
import { gunzipSync, gzipSync } from 'node:zlib';
|
|
7
|
+
import { PackError } from './types.js';
|
|
8
|
+
import { findPathConflict, unsafePathReason } from './paths.js';
|
|
9
|
+
import { readTar, writeTar } from './tar.js';
|
|
10
|
+
/** zlib level used for every archive. Part of the byte-identity contract. */
|
|
11
|
+
export const GZIP_LEVEL = 9;
|
|
12
|
+
/** RFC 1952 OS byte written into every archive (3 = Unix), whatever platform wrote it. */
|
|
13
|
+
export const GZIP_OS_UNIX = 3;
|
|
14
|
+
/**
|
|
15
|
+
* Defaults, chosen to match the most generous limits an existing extractor
|
|
16
|
+
* enforces (5 000 entries, 20 MiB per file, 50 MiB of files, 128 MiB of tar)
|
|
17
|
+
* so nothing that opens today stops opening here.
|
|
18
|
+
*/
|
|
19
|
+
export const DEFAULT_LIMITS = {
|
|
20
|
+
maxArchiveBytes: 64 * 1024 * 1024,
|
|
21
|
+
maxTarBytes: 128 * 1024 * 1024,
|
|
22
|
+
maxEntries: 5000,
|
|
23
|
+
maxEntryBytes: 20 * 1024 * 1024,
|
|
24
|
+
maxTotalBytes: 50 * 1024 * 1024,
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The deterministic `.pwpack` bytes of a folder.
|
|
28
|
+
*
|
|
29
|
+
* Tar: ustar, one entry per file plus one per implied directory, names in
|
|
30
|
+
* byte order, mode 0644 / 0755, uid/gid 0, empty uname/gname, mtime 0; a
|
|
31
|
+
* path that fits no ustar name/prefix split travels in a PAX `path` record.
|
|
32
|
+
* Gzip: level 9, header mtime 0, no file name, OS byte fixed to Unix (zlib
|
|
33
|
+
* otherwise stamps the platform it was built for, so the same folder hashed
|
|
34
|
+
* differently on Windows and Linux). File bytes are written exactly as given.
|
|
35
|
+
*
|
|
36
|
+
* Throws {@link PackError} `UNSAFE_PATH` if any path is unsafe and
|
|
37
|
+
* `PATH_CONFLICT` if a path is both a file and a directory — the writer
|
|
38
|
+
* never produces an archive a reader would have to refuse.
|
|
39
|
+
*/
|
|
40
|
+
export async function writePwpack(pack) {
|
|
41
|
+
for (const path of pack.files.keys()) {
|
|
42
|
+
const reason = unsafePathReason(path);
|
|
43
|
+
if (reason)
|
|
44
|
+
throw new PackError('UNSAFE_PATH', `${path}: ${reason}`, path);
|
|
45
|
+
}
|
|
46
|
+
assertNoPathConflict(pack.files.keys());
|
|
47
|
+
const gz = gzipSync(writeTar(pack.files), { level: GZIP_LEVEL });
|
|
48
|
+
gz[9] = GZIP_OS_UNIX;
|
|
49
|
+
return new Uint8Array(gz.buffer, gz.byteOffset, gz.byteLength);
|
|
50
|
+
}
|
|
51
|
+
/** Throws `PATH_CONFLICT` when a path is a file and also the directory of another path. */
|
|
52
|
+
function assertNoPathConflict(paths) {
|
|
53
|
+
const conflict = findPathConflict(paths);
|
|
54
|
+
if (conflict) {
|
|
55
|
+
throw new PackError('PATH_CONFLICT', `${conflict.file} is a file and also the directory of ${conflict.inside}`, conflict.file);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function isGzip(bytes) {
|
|
59
|
+
return bytes.length >= 2 && bytes[0] === 0x1f && bytes[1] === 0x8b;
|
|
60
|
+
}
|
|
61
|
+
function isBareTar(bytes) {
|
|
62
|
+
return bytes.length >= 512 && new TextDecoder().decode(bytes.subarray(257, 262)) === 'ustar';
|
|
63
|
+
}
|
|
64
|
+
/** The wrapper directory one existing producer puts in front of every member. */
|
|
65
|
+
const PACKAGE_WRAPPER = 'package/';
|
|
66
|
+
/**
|
|
67
|
+
* Open a `.pwpack` (or any pwpack tgz) into memory.
|
|
68
|
+
*
|
|
69
|
+
* Accepted: gzip-compressed tar (the normal case) and a bare uncompressed
|
|
70
|
+
* tar. Tolerated, from layouts existing producers emit: a `package/` wrapper
|
|
71
|
+
* on EVERY file (removed; a partial wrapper is kept as a plain directory),
|
|
72
|
+
* `./` prefixes, explicit directory entries (ignored), ustar `prefix` split
|
|
73
|
+
* names, PAX `path` records and GNU long names, header-less mtimes and any
|
|
74
|
+
* mode. Refused with {@link PackError}: unsafe paths (`UNSAFE_PATH`), link
|
|
75
|
+
* members (`LINK_ENTRY`), devices / FIFOs (`UNSUPPORTED_ENTRY`), a path
|
|
76
|
+
* present twice (`DUPLICATE_ENTRY`), a path that is both a file and a
|
|
77
|
+
* directory — of another file or of a directory member (`PATH_CONFLICT`),
|
|
78
|
+
* malformed streams (`GZIP_INVALID`,
|
|
79
|
+
* `TAR_INVALID`) and every crossed limit (`PACK_TOO_LARGE`,
|
|
80
|
+
* `ENTRY_TOO_LARGE`, `TOO_MANY_ENTRIES`).
|
|
81
|
+
*/
|
|
82
|
+
export async function readPwpack(bytes, limits = {}) {
|
|
83
|
+
const lim = { ...DEFAULT_LIMITS, ...limits };
|
|
84
|
+
if (bytes.length > lim.maxArchiveBytes) {
|
|
85
|
+
throw new PackError('PACK_TOO_LARGE', `archive is ${bytes.length} bytes (limit ${lim.maxArchiveBytes})`);
|
|
86
|
+
}
|
|
87
|
+
let tar;
|
|
88
|
+
if (isGzip(bytes)) {
|
|
89
|
+
try {
|
|
90
|
+
tar = gunzipSync(bytes, { maxOutputLength: lim.maxTarBytes });
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
if (e?.code === 'ERR_BUFFER_TOO_LARGE') {
|
|
94
|
+
throw new PackError('PACK_TOO_LARGE', `decompressed archive exceeds ${lim.maxTarBytes} bytes`);
|
|
95
|
+
}
|
|
96
|
+
throw new PackError('GZIP_INVALID', `archive is not a readable gzip stream: ${e.message}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else if (isBareTar(bytes)) {
|
|
100
|
+
if (bytes.length > lim.maxTarBytes) {
|
|
101
|
+
throw new PackError('PACK_TOO_LARGE', `tar is ${bytes.length} bytes (limit ${lim.maxTarBytes})`);
|
|
102
|
+
}
|
|
103
|
+
tar = bytes;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
throw new PackError('GZIP_INVALID', 'archive is neither gzip nor tar');
|
|
107
|
+
}
|
|
108
|
+
const files = [];
|
|
109
|
+
const dirs = [];
|
|
110
|
+
for (const entry of readTar(tar, lim)) {
|
|
111
|
+
let name = entry.name;
|
|
112
|
+
while (name.startsWith('./'))
|
|
113
|
+
name = name.slice(2);
|
|
114
|
+
if (entry.type === 'symlink' || entry.type === 'hardlink') {
|
|
115
|
+
throw new PackError('LINK_ENTRY', `${name} is a ${entry.type} member; a pwpack carries files only`, name);
|
|
116
|
+
}
|
|
117
|
+
if (entry.type === 'other') {
|
|
118
|
+
throw new PackError('UNSUPPORTED_ENTRY', `${name} is neither a file nor a directory`, name);
|
|
119
|
+
}
|
|
120
|
+
if (entry.type === 'dir') {
|
|
121
|
+
const dir = name.replace(/\/+$/, '');
|
|
122
|
+
const reason = dir === '' || dir === '.' ? null : unsafePathReason(dir);
|
|
123
|
+
if (reason)
|
|
124
|
+
throw new PackError('UNSAFE_PATH', `${name}: ${reason}`, name);
|
|
125
|
+
if (dir !== '' && dir !== '.')
|
|
126
|
+
dirs.push(dir);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const reason = unsafePathReason(name);
|
|
130
|
+
if (reason)
|
|
131
|
+
throw new PackError('UNSAFE_PATH', `${name}: ${reason}`, name);
|
|
132
|
+
files.push({ path: name, data: entry.data });
|
|
133
|
+
}
|
|
134
|
+
const wrapped = files.length > 0 && files.every((f) => f.path.startsWith(PACKAGE_WRAPPER));
|
|
135
|
+
const out = new Map();
|
|
136
|
+
for (const f of files) {
|
|
137
|
+
const path = wrapped ? f.path.slice(PACKAGE_WRAPPER.length) : f.path;
|
|
138
|
+
if (out.has(path))
|
|
139
|
+
throw new PackError('DUPLICATE_ENTRY', `${path} appears more than once in the archive`, path);
|
|
140
|
+
out.set(path, f.data.slice());
|
|
141
|
+
}
|
|
142
|
+
assertNoPathConflict(out.keys());
|
|
143
|
+
// An explicit directory member named like a file is the same collision.
|
|
144
|
+
for (const d of dirs) {
|
|
145
|
+
const dir = wrapped && d.startsWith(PACKAGE_WRAPPER) ? d.slice(PACKAGE_WRAPPER.length) : d;
|
|
146
|
+
if (out.has(dir))
|
|
147
|
+
throw new PackError('PATH_CONFLICT', `${dir} is a file and also a directory member`, dir);
|
|
148
|
+
}
|
|
149
|
+
return { files: out };
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=archive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
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C,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,CAAA;IAC1F,MAAM,GAAG,GAAG,IAAI,GAAG,EAAsB,CAAA;IACzC,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;QACtB,MAAM,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;QACpE,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,MAAM,IAAI,SAAS,CAAC,iBAAiB,EAAE,GAAG,IAAI,wCAAwC,EAAE,IAAI,CAAC,CAAA;QAChH,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;IAC/B,CAAC;IACD,oBAAoB,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAA;IAChC,wEAAwE;IACxE,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;QACrB,MAAM,GAAG,GAAG,OAAO,IAAI,CAAC,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAC1F,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,MAAM,IAAI,SAAS,CAAC,eAAe,EAAE,GAAG,GAAG,wCAAwC,EAAE,GAAG,CAAC,CAAA;IAC7G,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAA;AACvB,CAAC","sourcesContent":["/**\n * The `.pwpack` archive: a deterministic gzipped tar of the folder, rootless.\n *\n * @module @pwfabric/pack/archive\n */\nimport { gunzipSync, gzipSync } from 'node:zlib'\nimport { PackError, type PackFolder } from './types.js'\nimport { findPathConflict, unsafePathReason } from './paths.js'\nimport { readTar, writeTar } from './tar.js'\n\n/** zlib level used for every archive. Part of the byte-identity contract. */\nexport const GZIP_LEVEL = 9\n/** RFC 1952 OS byte written into every archive (3 = Unix), whatever platform wrote it. */\nexport const GZIP_OS_UNIX = 3\n\n/** Ceilings `readPwpack` enforces. Every field is optional when passed. */\nexport interface PwpackLimits {\n /** Size of the archive as given (compressed). */\n readonly maxArchiveBytes: number\n /** Size of the tar stream after gunzip — the decompression-bomb ceiling. */\n readonly maxTarBytes: number\n /** Number of tar members (files, directories, links). */\n readonly maxEntries: number\n /** Size of one file. */\n readonly maxEntryBytes: number\n /** Sum of all file sizes. */\n readonly maxTotalBytes: number\n}\n\n/**\n * Defaults, chosen to match the most generous limits an existing extractor\n * enforces (5 000 entries, 20 MiB per file, 50 MiB of files, 128 MiB of tar)\n * so nothing that opens today stops opening here.\n */\nexport const DEFAULT_LIMITS: PwpackLimits = {\n maxArchiveBytes: 64 * 1024 * 1024,\n maxTarBytes: 128 * 1024 * 1024,\n maxEntries: 5000,\n maxEntryBytes: 20 * 1024 * 1024,\n maxTotalBytes: 50 * 1024 * 1024,\n}\n\n/**\n * The deterministic `.pwpack` bytes of a folder.\n *\n * Tar: ustar, one entry per file plus one per implied directory, names in\n * byte order, mode 0644 / 0755, uid/gid 0, empty uname/gname, mtime 0; a\n * path that fits no ustar name/prefix split travels in a PAX `path` record.\n * Gzip: level 9, header mtime 0, no file name, OS byte fixed to Unix (zlib\n * otherwise stamps the platform it was built for, so the same folder hashed\n * differently on Windows and Linux). File bytes are written exactly as given.\n *\n * Throws {@link PackError} `UNSAFE_PATH` if any path is unsafe and\n * `PATH_CONFLICT` if a path is both a file and a directory — the writer\n * never produces an archive a reader would have to refuse.\n */\nexport async function writePwpack(pack: PackFolder): Promise<Uint8Array> {\n for (const path of pack.files.keys()) {\n const reason = unsafePathReason(path)\n if (reason) throw new PackError('UNSAFE_PATH', `${path}: ${reason}`, path)\n }\n assertNoPathConflict(pack.files.keys())\n const gz = gzipSync(writeTar(pack.files), { level: GZIP_LEVEL })\n gz[9] = GZIP_OS_UNIX\n return new Uint8Array(gz.buffer, gz.byteOffset, gz.byteLength)\n}\n\n/** Throws `PATH_CONFLICT` when a path is a file and also the directory of another path. */\nfunction assertNoPathConflict(paths: Iterable<string>): void {\n const conflict = findPathConflict(paths)\n if (conflict) {\n throw new PackError(\n 'PATH_CONFLICT',\n `${conflict.file} is a file and also the directory of ${conflict.inside}`,\n conflict.file\n )\n }\n}\n\nfunction isGzip(bytes: Uint8Array): boolean {\n return bytes.length >= 2 && bytes[0] === 0x1f && bytes[1] === 0x8b\n}\n\nfunction isBareTar(bytes: Uint8Array): boolean {\n return bytes.length >= 512 && new TextDecoder().decode(bytes.subarray(257, 262)) === 'ustar'\n}\n\n/** The wrapper directory one existing producer puts in front of every member. */\nconst PACKAGE_WRAPPER = 'package/'\n\n/**\n * Open a `.pwpack` (or any pwpack tgz) into memory.\n *\n * Accepted: gzip-compressed tar (the normal case) and a bare uncompressed\n * tar. Tolerated, from layouts existing producers emit: a `package/` wrapper\n * on EVERY file (removed; a partial wrapper is kept as a plain directory),\n * `./` prefixes, explicit directory entries (ignored), ustar `prefix` split\n * names, PAX `path` records and GNU long names, header-less mtimes and any\n * mode. Refused with {@link PackError}: unsafe paths (`UNSAFE_PATH`), link\n * members (`LINK_ENTRY`), devices / FIFOs (`UNSUPPORTED_ENTRY`), a path\n * present twice (`DUPLICATE_ENTRY`), a path that is both a file and a\n * directory — of another file or of a directory member (`PATH_CONFLICT`),\n * malformed streams (`GZIP_INVALID`,\n * `TAR_INVALID`) and every crossed limit (`PACK_TOO_LARGE`,\n * `ENTRY_TOO_LARGE`, `TOO_MANY_ENTRIES`).\n */\nexport async function readPwpack(bytes: Uint8Array, limits: Partial<PwpackLimits> = {}): Promise<PackFolder> {\n const lim: PwpackLimits = { ...DEFAULT_LIMITS, ...limits }\n if (bytes.length > lim.maxArchiveBytes) {\n throw new PackError('PACK_TOO_LARGE', `archive is ${bytes.length} bytes (limit ${lim.maxArchiveBytes})`)\n }\n\n let tar: Uint8Array\n if (isGzip(bytes)) {\n try {\n tar = gunzipSync(bytes, { maxOutputLength: lim.maxTarBytes })\n } catch (e) {\n if ((e as NodeJS.ErrnoException)?.code === 'ERR_BUFFER_TOO_LARGE') {\n throw new PackError('PACK_TOO_LARGE', `decompressed archive exceeds ${lim.maxTarBytes} bytes`)\n }\n throw new PackError('GZIP_INVALID', `archive is not a readable gzip stream: ${(e as Error).message}`)\n }\n } else if (isBareTar(bytes)) {\n if (bytes.length > lim.maxTarBytes) {\n throw new PackError('PACK_TOO_LARGE', `tar is ${bytes.length} bytes (limit ${lim.maxTarBytes})`)\n }\n tar = bytes\n } else {\n throw new PackError('GZIP_INVALID', 'archive is neither gzip nor tar')\n }\n\n const files: { path: string; data: Uint8Array }[] = []\n const dirs: string[] = []\n for (const entry of readTar(tar, lim)) {\n let name = entry.name\n while (name.startsWith('./')) name = name.slice(2)\n if (entry.type === 'symlink' || entry.type === 'hardlink') {\n throw new PackError('LINK_ENTRY', `${name} is a ${entry.type} member; a pwpack carries files only`, name)\n }\n if (entry.type === 'other') {\n throw new PackError('UNSUPPORTED_ENTRY', `${name} is neither a file nor a directory`, name)\n }\n if (entry.type === 'dir') {\n const dir = name.replace(/\\/+$/, '')\n const reason = dir === '' || dir === '.' ? null : unsafePathReason(dir)\n if (reason) throw new PackError('UNSAFE_PATH', `${name}: ${reason}`, name)\n if (dir !== '' && dir !== '.') dirs.push(dir)\n continue\n }\n const reason = unsafePathReason(name)\n if (reason) throw new PackError('UNSAFE_PATH', `${name}: ${reason}`, name)\n files.push({ path: name, data: entry.data })\n }\n\n const wrapped = files.length > 0 && files.every((f) => f.path.startsWith(PACKAGE_WRAPPER))\n const out = new Map<string, Uint8Array>()\n for (const f of files) {\n const path = wrapped ? f.path.slice(PACKAGE_WRAPPER.length) : f.path\n if (out.has(path)) throw new PackError('DUPLICATE_ENTRY', `${path} appears more than once in the archive`, path)\n out.set(path, f.data.slice())\n }\n assertNoPathConflict(out.keys())\n // An explicit directory member named like a file is the same collision.\n for (const d of dirs) {\n const dir = wrapped && d.startsWith(PACKAGE_WRAPPER) ? d.slice(PACKAGE_WRAPPER.length) : d\n if (out.has(dir)) throw new PackError('PATH_CONFLICT', `${dir} is a file and also a directory member`, dir)\n }\n return { files: out }\n}\n"]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pack content: the part of a folder that belongs to the pack. A source
|
|
3
|
+
* folder also holds the author's tooling (`package.json`, `tsconfig.json`,
|
|
4
|
+
* `scripts/`, `test/`, `vitest.config.ts` …); none of it is the pack, and
|
|
5
|
+
* none of it may change the pack's identity.
|
|
6
|
+
*
|
|
7
|
+
* @module @pwfabric/pack/content
|
|
8
|
+
*/
|
|
9
|
+
import type { PackFolder } from './types.js';
|
|
10
|
+
/** Root files that are pack content besides `pwpack.json`: the pack's own documentation and terms. */
|
|
11
|
+
export declare const PACK_DOC_FILES: ReadonlySet<string>;
|
|
12
|
+
/** Root directories of the canonical folder. */
|
|
13
|
+
export declare const PACK_ROOT_DIRS: ReadonlySet<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Root directories of the legacy rootless layout: the atom families of
|
|
16
|
+
* `classifyPackPath` (and its shared-code root `lib`). Root `test/` is not
|
|
17
|
+
* among them — in a source folder it is the author's tooling; a pack that
|
|
18
|
+
* ships its tests keeps them under `atoms/test/`.
|
|
19
|
+
*/
|
|
20
|
+
export declare const ROOTLESS_FAMILY_DIRS: ReadonlySet<string>;
|
|
21
|
+
/** True when `path` (a safe pack path) is pack content. */
|
|
22
|
+
export declare function isPackContentPath(path: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* The pack content of a folder: `pwpack.json`, `LICENSE`, `NOTICE` and
|
|
25
|
+
* `README.md` at the root; everything under `app/`, `atoms/` and `media/`;
|
|
26
|
+
* and, for the legacy rootless layout, everything under a root atom-family
|
|
27
|
+
* directory (`blocks/`, `capabilities/`, `connectors/`, `providers/`,
|
|
28
|
+
* `appearance/`, `patterns/`, `icons/`, `surfaces/`, `auth/`, `lib/`).
|
|
29
|
+
* Everything else is dropped. Bytes are shared, not copied.
|
|
30
|
+
*
|
|
31
|
+
* The content hash (`hashPack`) is computed over this, so a pack hashes the
|
|
32
|
+
* same whether it was read from its source folder or from its archive.
|
|
33
|
+
*/
|
|
34
|
+
export declare function packContent(folder: PackFolder): PackFolder;
|