@owlmeans/file-streams 0.0.0-stage → 0.2.0-alpha.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/LICENSE +21 -0
- package/README.md +71 -2
- package/agent-meta/manifest.json +64 -0
- package/agent-meta/skills/file-streams/SKILL.md +183 -0
- package/agent-meta/skills/file-streams/parts/network.md +47 -0
- package/agent-meta/skills/file-streams/parts/workspaces.md +129 -0
- package/build/archive/consts.d.ts +16 -0
- package/build/archive/consts.d.ts.map +1 -0
- package/build/archive/consts.js +24 -0
- package/build/archive/consts.js.map +1 -0
- package/build/archive/formats.d.ts +4 -0
- package/build/archive/formats.d.ts.map +1 -0
- package/build/archive/formats.js +53 -0
- package/build/archive/formats.js.map +1 -0
- package/build/archive/guard.d.ts +5 -0
- package/build/archive/guard.d.ts.map +1 -0
- package/build/archive/guard.js +62 -0
- package/build/archive/guard.js.map +1 -0
- package/build/archive/ratio.d.ts +5 -0
- package/build/archive/ratio.d.ts.map +1 -0
- package/build/archive/ratio.js +30 -0
- package/build/archive/ratio.js.map +1 -0
- package/build/archive/tar.d.ts +4 -0
- package/build/archive/tar.d.ts.map +1 -0
- package/build/archive/tar.js +142 -0
- package/build/archive/tar.js.map +1 -0
- package/build/archive/types.d.ts +95 -0
- package/build/archive/types.d.ts.map +1 -0
- package/build/archive/types.js +2 -0
- package/build/archive/types.js.map +1 -0
- package/build/archive/types.local.d.ts +64 -0
- package/build/archive/types.local.d.ts.map +1 -0
- package/build/archive/types.local.js +2 -0
- package/build/archive/types.local.js.map +1 -0
- package/build/archive/zip.d.ts +4 -0
- package/build/archive/zip.d.ts.map +1 -0
- package/build/archive/zip.js +135 -0
- package/build/archive/zip.js.map +1 -0
- package/build/archive.d.ts +4 -0
- package/build/archive.d.ts.map +1 -0
- package/build/archive.js +265 -0
- package/build/archive.js.map +1 -0
- package/build/backed-workspace/types.d.ts +67 -0
- package/build/backed-workspace/types.d.ts.map +1 -0
- package/build/backed-workspace/types.js +2 -0
- package/build/backed-workspace/types.js.map +1 -0
- package/build/backed-workspace.d.ts +4 -0
- package/build/backed-workspace.d.ts.map +1 -0
- package/build/backed-workspace.js +63 -0
- package/build/backed-workspace.js.map +1 -0
- package/build/consts.d.ts +3 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +3 -0
- package/build/consts.js.map +1 -0
- package/build/consts.local.d.ts +3 -0
- package/build/consts.local.d.ts.map +1 -0
- package/build/consts.local.js +3 -0
- package/build/consts.local.js.map +1 -0
- package/build/errors.d.ts +59 -0
- package/build/errors.d.ts.map +1 -0
- package/build/errors.js +105 -0
- package/build/errors.js.map +1 -0
- package/build/http/connector.d.ts +11 -0
- package/build/http/connector.d.ts.map +1 -0
- package/build/http/connector.js +124 -0
- package/build/http/connector.js.map +1 -0
- package/build/http/consts.d.ts +9 -0
- package/build/http/consts.d.ts.map +1 -0
- package/build/http/consts.js +8 -0
- package/build/http/consts.js.map +1 -0
- package/build/http/source.d.ts +5 -0
- package/build/http/source.d.ts.map +1 -0
- package/build/http/source.js +76 -0
- package/build/http/source.js.map +1 -0
- package/build/http/types.d.ts +67 -0
- package/build/http/types.d.ts.map +1 -0
- package/build/http/types.js +2 -0
- package/build/http/types.js.map +1 -0
- package/build/http/types.local.d.ts +21 -0
- package/build/http/types.local.d.ts.map +1 -0
- package/build/http/types.local.js +2 -0
- package/build/http/types.local.js.map +1 -0
- package/build/http.d.ts +4 -0
- package/build/http.d.ts.map +1 -0
- package/build/http.js +248 -0
- package/build/http.js.map +1 -0
- package/build/index.d.ts +53 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +33 -0
- package/build/index.js.map +1 -0
- package/build/network/consts.d.ts +12 -0
- package/build/network/consts.d.ts.map +1 -0
- package/build/network/consts.js +20 -0
- package/build/network/consts.js.map +1 -0
- package/build/network/types.d.ts +43 -0
- package/build/network/types.d.ts.map +1 -0
- package/build/network/types.js +2 -0
- package/build/network/types.js.map +1 -0
- package/build/network.d.ts +4 -0
- package/build/network.d.ts.map +1 -0
- package/build/network.js +165 -0
- package/build/network.js.map +1 -0
- package/build/proxy/consts.d.ts +7 -0
- package/build/proxy/consts.d.ts.map +1 -0
- package/build/proxy/consts.js +9 -0
- package/build/proxy/consts.js.map +1 -0
- package/build/proxy/endpoint.d.ts +4 -0
- package/build/proxy/endpoint.d.ts.map +1 -0
- package/build/proxy/endpoint.js +27 -0
- package/build/proxy/endpoint.js.map +1 -0
- package/build/proxy/http.d.ts +5 -0
- package/build/proxy/http.d.ts.map +1 -0
- package/build/proxy/http.js +95 -0
- package/build/proxy/http.js.map +1 -0
- package/build/proxy/router.d.ts +4 -0
- package/build/proxy/router.d.ts.map +1 -0
- package/build/proxy/router.js +85 -0
- package/build/proxy/router.js.map +1 -0
- package/build/proxy/socks.d.ts +5 -0
- package/build/proxy/socks.d.ts.map +1 -0
- package/build/proxy/socks.js +44 -0
- package/build/proxy/socks.js.map +1 -0
- package/build/proxy/types.d.ts +99 -0
- package/build/proxy/types.d.ts.map +1 -0
- package/build/proxy/types.js +2 -0
- package/build/proxy/types.js.map +1 -0
- package/build/resolvers/google-drive/consts.d.ts +10 -0
- package/build/resolvers/google-drive/consts.d.ts.map +1 -0
- package/build/resolvers/google-drive/consts.js +16 -0
- package/build/resolvers/google-drive/consts.js.map +1 -0
- package/build/resolvers/google-drive/types.d.ts +21 -0
- package/build/resolvers/google-drive/types.d.ts.map +1 -0
- package/build/resolvers/google-drive/types.js +2 -0
- package/build/resolvers/google-drive/types.js.map +1 -0
- package/build/resolvers/google-drive.d.ts +10 -0
- package/build/resolvers/google-drive.d.ts.map +1 -0
- package/build/resolvers/google-drive.js +130 -0
- package/build/resolvers/google-drive.js.map +1 -0
- package/build/resolvers/types.d.ts +39 -0
- package/build/resolvers/types.d.ts.map +1 -0
- package/build/resolvers/types.js +2 -0
- package/build/resolvers/types.js.map +1 -0
- package/build/s3/consts.d.ts +9 -0
- package/build/s3/consts.d.ts.map +1 -0
- package/build/s3/consts.js +9 -0
- package/build/s3/consts.js.map +1 -0
- package/build/s3/prefix.d.ts +4 -0
- package/build/s3/prefix.d.ts.map +1 -0
- package/build/s3/prefix.js +87 -0
- package/build/s3/prefix.js.map +1 -0
- package/build/s3/sink.d.ts +6 -0
- package/build/s3/sink.d.ts.map +1 -0
- package/build/s3/sink.js +30 -0
- package/build/s3/sink.js.map +1 -0
- package/build/s3/source.d.ts +5 -0
- package/build/s3/source.d.ts.map +1 -0
- package/build/s3/source.js +81 -0
- package/build/s3/source.js.map +1 -0
- package/build/s3/types.d.ts +66 -0
- package/build/s3/types.d.ts.map +1 -0
- package/build/s3/types.js +2 -0
- package/build/s3/types.js.map +1 -0
- package/build/s3/types.local.d.ts +43 -0
- package/build/s3/types.local.d.ts.map +1 -0
- package/build/s3/types.local.js +2 -0
- package/build/s3/types.local.js.map +1 -0
- package/build/s3/upload.d.ts +4 -0
- package/build/s3/upload.d.ts.map +1 -0
- package/build/s3/upload.js +118 -0
- package/build/s3/upload.js.map +1 -0
- package/build/s3.d.ts +5 -0
- package/build/s3.d.ts.map +1 -0
- package/build/s3.js +68 -0
- package/build/s3.js.map +1 -0
- package/build/service/types.d.ts +66 -0
- package/build/service/types.d.ts.map +1 -0
- package/build/service/types.js +2 -0
- package/build/service/types.js.map +1 -0
- package/build/service/types.local.d.ts +11 -0
- package/build/service/types.local.d.ts.map +1 -0
- package/build/service/types.local.js +2 -0
- package/build/service/types.local.js.map +1 -0
- package/build/service.d.ts +6 -0
- package/build/service.d.ts.map +1 -0
- package/build/service.js +73 -0
- package/build/service.js.map +1 -0
- package/build/sinks/buffer/consts.d.ts +2 -0
- package/build/sinks/buffer/consts.d.ts.map +1 -0
- package/build/sinks/buffer/consts.js +2 -0
- package/build/sinks/buffer/consts.js.map +1 -0
- package/build/sinks/buffer/types.d.ts +11 -0
- package/build/sinks/buffer/types.d.ts.map +1 -0
- package/build/sinks/buffer/types.js +2 -0
- package/build/sinks/buffer/types.js.map +1 -0
- package/build/sinks/buffer.d.ts +4 -0
- package/build/sinks/buffer.d.ts.map +1 -0
- package/build/sinks/buffer.js +13 -0
- package/build/sinks/buffer.js.map +1 -0
- package/build/sinks/directory/types.d.ts +7 -0
- package/build/sinks/directory/types.d.ts.map +1 -0
- package/build/sinks/directory/types.js +2 -0
- package/build/sinks/directory/types.js.map +1 -0
- package/build/sinks/directory.d.ts +8 -0
- package/build/sinks/directory.d.ts.map +1 -0
- package/build/sinks/directory.js +43 -0
- package/build/sinks/directory.js.map +1 -0
- package/build/sinks/file.d.ts +5 -0
- package/build/sinks/file.d.ts.map +1 -0
- package/build/sinks/file.js +46 -0
- package/build/sinks/file.js.map +1 -0
- package/build/sinks/types.d.ts +11 -0
- package/build/sinks/types.d.ts.map +1 -0
- package/build/sinks/types.js +2 -0
- package/build/sinks/types.js.map +1 -0
- package/build/sinks/workload/consts.d.ts +5 -0
- package/build/sinks/workload/consts.d.ts.map +1 -0
- package/build/sinks/workload/consts.js +5 -0
- package/build/sinks/workload/consts.js.map +1 -0
- package/build/sinks/workload/types.d.ts +17 -0
- package/build/sinks/workload/types.d.ts.map +1 -0
- package/build/sinks/workload/types.js +2 -0
- package/build/sinks/workload/types.js.map +1 -0
- package/build/sinks/workload.d.ts +8 -0
- package/build/sinks/workload.d.ts.map +1 -0
- package/build/sinks/workload.js +51 -0
- package/build/sinks/workload.js.map +1 -0
- package/build/sources/buffer.d.ts +5 -0
- package/build/sources/buffer.d.ts.map +1 -0
- package/build/sources/buffer.js +20 -0
- package/build/sources/buffer.js.map +1 -0
- package/build/sources/file.d.ts +5 -0
- package/build/sources/file.d.ts.map +1 -0
- package/build/sources/file.js +50 -0
- package/build/sources/file.js.map +1 -0
- package/build/sources/stream.d.ts +5 -0
- package/build/sources/stream.d.ts.map +1 -0
- package/build/sources/stream.js +19 -0
- package/build/sources/stream.js.map +1 -0
- package/build/sources/types.d.ts +10 -0
- package/build/sources/types.d.ts.map +1 -0
- package/build/sources/types.js +2 -0
- package/build/sources/types.js.map +1 -0
- package/build/sources/workload/types.d.ts +29 -0
- package/build/sources/workload/types.d.ts.map +1 -0
- package/build/sources/workload/types.js +2 -0
- package/build/sources/workload/types.js.map +1 -0
- package/build/sources/workload.d.ts +7 -0
- package/build/sources/workload.d.ts.map +1 -0
- package/build/sources/workload.js +67 -0
- package/build/sources/workload.js.map +1 -0
- package/build/transfer/consts.d.ts +5 -0
- package/build/transfer/consts.d.ts.map +1 -0
- package/build/transfer/consts.js +8 -0
- package/build/transfer/consts.js.map +1 -0
- package/build/transfer/types.d.ts +49 -0
- package/build/transfer/types.d.ts.map +1 -0
- package/build/transfer/types.js +2 -0
- package/build/transfer/types.js.map +1 -0
- package/build/transfer.d.ts +4 -0
- package/build/transfer.d.ts.map +1 -0
- package/build/transfer.js +75 -0
- package/build/transfer.js.map +1 -0
- package/build/types.d.ts +99 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/build/utils/names/consts.d.ts +5 -0
- package/build/utils/names/consts.d.ts.map +1 -0
- package/build/utils/names/consts.js +5 -0
- package/build/utils/names/consts.js.map +1 -0
- package/build/utils/names/types.d.ts +13 -0
- package/build/utils/names/types.d.ts.map +1 -0
- package/build/utils/names/types.js +2 -0
- package/build/utils/names/types.js.map +1 -0
- package/build/utils/names.d.ts +4 -0
- package/build/utils/names.d.ts.map +1 -0
- package/build/utils/names.js +60 -0
- package/build/utils/names.js.map +1 -0
- package/build/utils/paths/consts.d.ts +3 -0
- package/build/utils/paths/consts.d.ts.map +1 -0
- package/build/utils/paths/consts.js +2 -0
- package/build/utils/paths/consts.js.map +1 -0
- package/build/utils/paths/types.d.ts +20 -0
- package/build/utils/paths/types.d.ts.map +1 -0
- package/build/utils/paths/types.js +2 -0
- package/build/utils/paths/types.js.map +1 -0
- package/build/utils/paths.d.ts +4 -0
- package/build/utils/paths.d.ts.map +1 -0
- package/build/utils/paths.js +67 -0
- package/build/utils/paths.js.map +1 -0
- package/build/utils/range/types.d.ts +11 -0
- package/build/utils/range/types.d.ts.map +1 -0
- package/build/utils/range/types.js +2 -0
- package/build/utils/range/types.js.map +1 -0
- package/build/utils/range.d.ts +4 -0
- package/build/utils/range.d.ts.map +1 -0
- package/build/utils/range.js +64 -0
- package/build/utils/range.js.map +1 -0
- package/build/utils/redact/types.d.ts +16 -0
- package/build/utils/redact/types.d.ts.map +1 -0
- package/build/utils/redact/types.js +2 -0
- package/build/utils/redact/types.js.map +1 -0
- package/build/utils/redact.d.ts +4 -0
- package/build/utils/redact.d.ts.map +1 -0
- package/build/utils/redact.js +18 -0
- package/build/utils/redact.js.map +1 -0
- package/build/utils/signals/types.d.ts +17 -0
- package/build/utils/signals/types.d.ts.map +1 -0
- package/build/utils/signals/types.js +2 -0
- package/build/utils/signals/types.js.map +1 -0
- package/build/utils/signals.d.ts +4 -0
- package/build/utils/signals.d.ts.map +1 -0
- package/build/utils/signals.js +52 -0
- package/build/utils/signals.js.map +1 -0
- package/build/utils/streams/consts.d.ts +3 -0
- package/build/utils/streams/consts.d.ts.map +1 -0
- package/build/utils/streams/consts.js +3 -0
- package/build/utils/streams/consts.js.map +1 -0
- package/build/utils/streams/types.d.ts +59 -0
- package/build/utils/streams/types.d.ts.map +1 -0
- package/build/utils/streams/types.js +2 -0
- package/build/utils/streams/types.js.map +1 -0
- package/build/utils/streams.d.ts +4 -0
- package/build/utils/streams.d.ts.map +1 -0
- package/build/utils/streams.js +167 -0
- package/build/utils/streams.js.map +1 -0
- package/build/utils/tree/types.d.ts +6 -0
- package/build/utils/tree/types.d.ts.map +1 -0
- package/build/utils/tree/types.js +2 -0
- package/build/utils/tree/types.js.map +1 -0
- package/build/utils/tree.d.ts +4 -0
- package/build/utils/tree.d.ts.map +1 -0
- package/build/utils/tree.js +14 -0
- package/build/utils/tree.js.map +1 -0
- package/build/workspace/consts.d.ts +12 -0
- package/build/workspace/consts.d.ts.map +1 -0
- package/build/workspace/consts.js +12 -0
- package/build/workspace/consts.js.map +1 -0
- package/build/workspace/consts.local.d.ts +3 -0
- package/build/workspace/consts.local.d.ts.map +1 -0
- package/build/workspace/consts.local.js +3 -0
- package/build/workspace/consts.local.js.map +1 -0
- package/build/workspace/fs.d.ts +4 -0
- package/build/workspace/fs.d.ts.map +1 -0
- package/build/workspace/fs.js +45 -0
- package/build/workspace/fs.js.map +1 -0
- package/build/workspace/scope.d.ts +6 -0
- package/build/workspace/scope.d.ts.map +1 -0
- package/build/workspace/scope.js +180 -0
- package/build/workspace/scope.js.map +1 -0
- package/build/workspace/types.d.ts +66 -0
- package/build/workspace/types.d.ts.map +1 -0
- package/build/workspace/types.js +2 -0
- package/build/workspace/types.js.map +1 -0
- package/build/workspace/types.local.d.ts +20 -0
- package/build/workspace/types.local.d.ts.map +1 -0
- package/build/workspace/types.local.js +2 -0
- package/build/workspace/types.local.js.map +1 -0
- package/build/workspace.d.ts +7 -0
- package/build/workspace.d.ts.map +1 -0
- package/build/workspace.js +87 -0
- package/build/workspace.js.map +1 -0
- package/package.json +61 -4
- package/src/archive/consts.ts +28 -0
- package/src/archive/formats.ts +60 -0
- package/src/archive/guard.ts +68 -0
- package/src/archive/ratio.ts +35 -0
- package/src/archive/tar.ts +147 -0
- package/src/archive/types.local.ts +72 -0
- package/src/archive/types.ts +107 -0
- package/src/archive/zip.ts +142 -0
- package/src/archive.ts +298 -0
- package/src/backed-workspace/types.ts +72 -0
- package/src/backed-workspace.ts +85 -0
- package/src/consts.local.ts +2 -0
- package/src/consts.ts +2 -0
- package/src/errors.ts +128 -0
- package/src/http/connector.ts +136 -0
- package/src/http/consts.ts +13 -0
- package/src/http/source.ts +89 -0
- package/src/http/types.local.ts +23 -0
- package/src/http/types.ts +74 -0
- package/src/http.ts +276 -0
- package/src/index.ts +65 -0
- package/src/network/consts.ts +22 -0
- package/src/network/types.ts +45 -0
- package/src/network.ts +179 -0
- package/src/proxy/consts.ts +11 -0
- package/src/proxy/endpoint.ts +28 -0
- package/src/proxy/http.ts +108 -0
- package/src/proxy/router.ts +93 -0
- package/src/proxy/socks.ts +48 -0
- package/src/proxy/types.ts +114 -0
- package/src/resolvers/google-drive/consts.ts +22 -0
- package/src/resolvers/google-drive/types.ts +22 -0
- package/src/resolvers/google-drive.ts +153 -0
- package/src/resolvers/types.ts +45 -0
- package/src/s3/consts.ts +12 -0
- package/src/s3/prefix.ts +100 -0
- package/src/s3/sink.ts +36 -0
- package/src/s3/source.ts +90 -0
- package/src/s3/types.local.ts +49 -0
- package/src/s3/types.ts +70 -0
- package/src/s3/upload.ts +130 -0
- package/src/s3.ts +83 -0
- package/src/service/types.local.ts +9 -0
- package/src/service/types.ts +71 -0
- package/src/service.ts +97 -0
- package/src/sinks/buffer/consts.ts +1 -0
- package/src/sinks/buffer/types.ts +12 -0
- package/src/sinks/buffer.ts +18 -0
- package/src/sinks/directory/types.ts +7 -0
- package/src/sinks/directory.ts +49 -0
- package/src/sinks/file.ts +52 -0
- package/src/sinks/types.ts +12 -0
- package/src/sinks/workload/consts.ts +5 -0
- package/src/sinks/workload/types.ts +17 -0
- package/src/sinks/workload.ts +59 -0
- package/src/sources/buffer.ts +27 -0
- package/src/sources/file.ts +57 -0
- package/src/sources/stream.ts +26 -0
- package/src/sources/types.ts +12 -0
- package/src/sources/workload/types.ts +31 -0
- package/src/sources/workload.ts +79 -0
- package/src/transfer/consts.ts +10 -0
- package/src/transfer/types.ts +55 -0
- package/src/transfer.ts +89 -0
- package/src/types.ts +106 -0
- package/src/utils/names/consts.ts +5 -0
- package/src/utils/names/types.ts +12 -0
- package/src/utils/names.ts +70 -0
- package/src/utils/paths/consts.ts +3 -0
- package/src/utils/paths/types.ts +20 -0
- package/src/utils/paths.ts +75 -0
- package/src/utils/range/types.ts +11 -0
- package/src/utils/range.ts +80 -0
- package/src/utils/redact/types.ts +16 -0
- package/src/utils/redact.ts +23 -0
- package/src/utils/signals/types.ts +17 -0
- package/src/utils/signals.ts +63 -0
- package/src/utils/streams/consts.ts +2 -0
- package/src/utils/streams/types.ts +65 -0
- package/src/utils/streams.ts +188 -0
- package/src/utils/tree/types.ts +6 -0
- package/src/utils/tree.ts +18 -0
- package/src/workspace/consts.local.ts +2 -0
- package/src/workspace/consts.ts +14 -0
- package/src/workspace/fs.ts +48 -0
- package/src/workspace/scope.ts +196 -0
- package/src/workspace/types.local.ts +21 -0
- package/src/workspace/types.ts +71 -0
- package/src/workspace.ts +102 -0
- package/tests/archive.spec.ts +134 -0
- package/tests/backed.spec.ts +147 -0
- package/tests/context.ts +114 -0
- package/tests/fixtures.ts +190 -0
- package/tests/google-drive.spec.ts +76 -0
- package/tests/harness/drive.ts +92 -0
- package/tests/harness/http-proxy.ts +96 -0
- package/tests/harness/origin.ts +134 -0
- package/tests/harness/socks5.ts +135 -0
- package/tests/harness/workload.ts +118 -0
- package/tests/http.spec.ts +101 -0
- package/tests/inspector.spec.ts +40 -0
- package/tests/live.spec.ts +27 -0
- package/tests/network.spec.ts +79 -0
- package/tests/proxy.spec.ts +149 -0
- package/tests/s3.spec.ts +133 -0
- package/tests/service.spec.ts +77 -0
- package/tests/sources-sinks.spec.ts +96 -0
- package/tests/transfer.spec.ts +99 -0
- package/tests/tsconfig.json +12 -0
- package/tests/workload.spec.ts +140 -0
- package/tests/workspace.spec.ts +158 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Transform } from 'node:stream'
|
|
2
|
+
import { ArchiveError, LimitError, PathError } from '../errors.js'
|
|
3
|
+
import { pathUtils } from '../utils/paths.js'
|
|
4
|
+
import type { ArchiveEntryInfo, ArchiveLimits } from './types.js'
|
|
5
|
+
import type { EntryAdmission, EntryGuard } from './types.local.js'
|
|
6
|
+
|
|
7
|
+
/** The rules every entry of one extraction passes: counts, names, links, duplicates, sizes. */
|
|
8
|
+
export const makeEntryGuard = (limits: ArchiveLimits, links: 'skip' | 'refuse', strip: number): EntryGuard => {
|
|
9
|
+
const seen = new Set<string>()
|
|
10
|
+
let entries = 0
|
|
11
|
+
let total = 0
|
|
12
|
+
|
|
13
|
+
const admit = (info: ArchiveEntryInfo): EntryAdmission => {
|
|
14
|
+
if (++entries > limits.maxEntries) {
|
|
15
|
+
throw new LimitError('entries')
|
|
16
|
+
}
|
|
17
|
+
if (info.type === 'symlink' || info.type === 'hardlink') {
|
|
18
|
+
if (links === 'refuse') {
|
|
19
|
+
throw new ArchiveError('link')
|
|
20
|
+
}
|
|
21
|
+
return { skip: 'link' }
|
|
22
|
+
}
|
|
23
|
+
if (info.type === 'other') {
|
|
24
|
+
return { skip: 'unsupported-type' }
|
|
25
|
+
}
|
|
26
|
+
if (info.encrypted === true) {
|
|
27
|
+
throw new ArchiveError('encrypted')
|
|
28
|
+
}
|
|
29
|
+
const segments = info.path.replace(/\\/g, '/').split('/').filter(segment => segment !== '')
|
|
30
|
+
if (segments.length <= strip) {
|
|
31
|
+
return { skip: 'stripped' }
|
|
32
|
+
}
|
|
33
|
+
const path = pathUtils.entry(segments.slice(strip).join('/'), limits)
|
|
34
|
+
if (info.type === 'file') {
|
|
35
|
+
if (info.size != null && info.size > limits.maxEntryBytes) {
|
|
36
|
+
throw new LimitError('entry-bytes')
|
|
37
|
+
}
|
|
38
|
+
const folded = path.toLowerCase()
|
|
39
|
+
if (seen.has(folded)) {
|
|
40
|
+
throw new PathError('duplicate')
|
|
41
|
+
}
|
|
42
|
+
seen.add(folded)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return { path }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const meter = (): Transform => {
|
|
49
|
+
let bytes = 0
|
|
50
|
+
return new Transform({
|
|
51
|
+
transform: (chunk: Buffer, _encoding, callback) => {
|
|
52
|
+
bytes += chunk.length
|
|
53
|
+
total += chunk.length
|
|
54
|
+
if (bytes > limits.maxEntryBytes) {
|
|
55
|
+
callback(new LimitError('entry-bytes'))
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
if (total > limits.maxTotalBytes) {
|
|
59
|
+
callback(new LimitError('total-bytes'))
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
callback(null, chunk)
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { limits, admit, meter }
|
|
68
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Transform } from 'node:stream'
|
|
2
|
+
import { LimitError } from '../errors.js'
|
|
3
|
+
import { RATIO_FLOOR_BYTES } from './consts.js'
|
|
4
|
+
import type { ArchiveLimits } from './types.js'
|
|
5
|
+
import type { RatioGuard } from './types.local.js'
|
|
6
|
+
|
|
7
|
+
/** Counts compressed input and decompressed output of one stream and refuses a bomb. */
|
|
8
|
+
export const makeRatioGuard = (limits: ArchiveLimits): RatioGuard => {
|
|
9
|
+
let consumed = 0
|
|
10
|
+
let produced = 0
|
|
11
|
+
|
|
12
|
+
const input = new Transform({
|
|
13
|
+
transform: (chunk: Buffer, _encoding, callback) => {
|
|
14
|
+
consumed += chunk.length
|
|
15
|
+
callback(null, chunk)
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const output = new Transform({
|
|
20
|
+
transform: (chunk: Buffer, _encoding, callback) => {
|
|
21
|
+
produced += chunk.length
|
|
22
|
+
if (produced > limits.maxTotalBytes) {
|
|
23
|
+
callback(new LimitError('total-bytes'))
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
if (produced > RATIO_FLOOR_BYTES && produced > consumed * limits.maxRatio) {
|
|
27
|
+
callback(new LimitError('ratio'))
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
callback(null, chunk)
|
|
31
|
+
},
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
return { input, output }
|
|
35
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs'
|
|
2
|
+
import { once } from 'node:events'
|
|
3
|
+
import { PassThrough, Readable } from 'node:stream'
|
|
4
|
+
import tarStream from 'tar-stream'
|
|
5
|
+
import type { Header } from 'tar-stream'
|
|
6
|
+
import { ArchiveError } from '../errors.js'
|
|
7
|
+
import { pathUtils } from '../utils/paths.js'
|
|
8
|
+
import { streamUtils } from '../utils/streams.js'
|
|
9
|
+
import { DEFAULT_ARCHIVE_LIMITS } from './consts.js'
|
|
10
|
+
import type { ArchiveEntryInfo, ArchiveEntryType, ArchivePackOptions, PackEntry } from './types.js'
|
|
11
|
+
import type { ArchiveEntryHandle, TarCodec, TarEntryWriter } from './types.local.js'
|
|
12
|
+
|
|
13
|
+
/** Tar read and written as streams (tar-stream); an entry is never destroyed, only drained. */
|
|
14
|
+
export const createTarCodec = (): TarCodec => {
|
|
15
|
+
const typeOf = (type: Header['type'] | undefined): ArchiveEntryType => {
|
|
16
|
+
switch (type) {
|
|
17
|
+
case 'file':
|
|
18
|
+
case 'contiguous-file':
|
|
19
|
+
case undefined:
|
|
20
|
+
return 'file'
|
|
21
|
+
case 'directory':
|
|
22
|
+
return 'directory'
|
|
23
|
+
case 'symlink':
|
|
24
|
+
return 'symlink'
|
|
25
|
+
case 'link':
|
|
26
|
+
return 'hardlink'
|
|
27
|
+
default:
|
|
28
|
+
return 'other'
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const infoOf = (header: Header): ArchiveEntryInfo => ({
|
|
33
|
+
path: header.name,
|
|
34
|
+
type: typeOf(header.type),
|
|
35
|
+
size: header.size,
|
|
36
|
+
mode: header.mode != null ? header.mode & 0o7777 : undefined,
|
|
37
|
+
modifiedAt: header.mtime,
|
|
38
|
+
linkTarget: header.linkname !== '' ? header.linkname : undefined,
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const entries = async function * (stream: Readable): AsyncGenerator<ArchiveEntryHandle> {
|
|
42
|
+
const extract = tarStream.extract()
|
|
43
|
+
stream.once('error', error => extract.destroy(error))
|
|
44
|
+
stream.pipe(extract)
|
|
45
|
+
try {
|
|
46
|
+
for await (const entry of extract) {
|
|
47
|
+
let ended = false
|
|
48
|
+
let opened = false
|
|
49
|
+
const finished = new Promise<void>(resolve => {
|
|
50
|
+
entry.once('end', () => { ended = true; resolve() })
|
|
51
|
+
entry.once('close', () => resolve())
|
|
52
|
+
})
|
|
53
|
+
const drain = async (): Promise<void> => {
|
|
54
|
+
if (!ended) {
|
|
55
|
+
if (!opened) {
|
|
56
|
+
entry.resume()
|
|
57
|
+
}
|
|
58
|
+
await finished
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
yield {
|
|
62
|
+
info: infoOf(entry.header),
|
|
63
|
+
open: async () => {
|
|
64
|
+
opened = true
|
|
65
|
+
const relay = new PassThrough()
|
|
66
|
+
let detached = false
|
|
67
|
+
relay.once('close', () => { detached = true })
|
|
68
|
+
const pump = async (): Promise<void> => {
|
|
69
|
+
// The entry is read to its end even when the consumer gave up: destroying a tar
|
|
70
|
+
// entry would destroy the whole archive stream.
|
|
71
|
+
for await (const chunk of entry) {
|
|
72
|
+
if (!detached && !relay.write(chunk)) {
|
|
73
|
+
await Promise.race([once(relay, 'drain'), once(relay, 'close')])
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!detached) {
|
|
77
|
+
relay.end()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
pump().catch(error => { if (!detached) relay.destroy(error as Error) })
|
|
81
|
+
return relay
|
|
82
|
+
},
|
|
83
|
+
drain,
|
|
84
|
+
}
|
|
85
|
+
await drain()
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
throw error instanceof Error && /unexpected end|invalid tar|header/i.test(error.message) ? new ArchiveError('corrupt') : error
|
|
89
|
+
} finally {
|
|
90
|
+
if (!extract.destroyed) {
|
|
91
|
+
extract.destroy()
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const write = async (writer: TarEntryWriter, stream: Readable): Promise<void> => {
|
|
97
|
+
for await (const chunk of stream) {
|
|
98
|
+
if (!writer.write(chunk as Buffer)) {
|
|
99
|
+
await new Promise(resolve => writer.once('drain', resolve))
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const done = new Promise((resolve, reject) => {
|
|
103
|
+
writer.once('finish', resolve)
|
|
104
|
+
writer.once('error', reject)
|
|
105
|
+
})
|
|
106
|
+
writer.end()
|
|
107
|
+
await done
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const pack = (list: Iterable<PackEntry> | AsyncIterable<PackEntry>, options: ArchivePackOptions): Readable => {
|
|
111
|
+
const packer = tarStream.pack()
|
|
112
|
+
const output = Readable.from(packer as unknown as AsyncIterable<Buffer>, { objectMode: false })
|
|
113
|
+
const add = async (): Promise<void> => {
|
|
114
|
+
for await (const entry of list) {
|
|
115
|
+
const header = { name: pathUtils.entry(entry.path), mode: (entry.mode ?? 0o644) & 0o777, mtime: entry.modifiedAt ?? new Date() }
|
|
116
|
+
const source = entry.source
|
|
117
|
+
if (source instanceof Uint8Array) {
|
|
118
|
+
await new Promise<void>((resolve, reject) => {
|
|
119
|
+
packer.entry({ ...header, size: source.length }, Buffer.from(source), error => error != null ? reject(error) : resolve())
|
|
120
|
+
})
|
|
121
|
+
continue
|
|
122
|
+
}
|
|
123
|
+
let size = (await source.info()).size
|
|
124
|
+
if (size != null) {
|
|
125
|
+
await write(packer.entry({ ...header, size }), (await source.open()).stream)
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
const spooled = await streamUtils.spool((await source.open()).stream, { maxBytes: options.maxSpoolBytes ?? DEFAULT_ARCHIVE_LIMITS.maxEntryBytes })
|
|
129
|
+
try {
|
|
130
|
+
size = spooled.size
|
|
131
|
+
await write(packer.entry({ ...header, size }), createReadStream(spooled.path))
|
|
132
|
+
} finally {
|
|
133
|
+
await spooled.dispose()
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
packer.finalize()
|
|
137
|
+
}
|
|
138
|
+
add().catch(error => {
|
|
139
|
+
packer.destroy(error as Error)
|
|
140
|
+
output.destroy(error as Error)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
return output
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { entries, pack }
|
|
147
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Readable, Transform } from 'node:stream'
|
|
2
|
+
import type { ArchiveFormat } from './consts.js'
|
|
3
|
+
import type { RangeReader } from '../types.js'
|
|
4
|
+
import type { ArchiveEntryInfo, ArchiveLimits, ArchivePackOptions, PackEntry } from './types.js'
|
|
5
|
+
|
|
6
|
+
/** One entry an archive codec reached. */
|
|
7
|
+
export interface ArchiveEntryHandle {
|
|
8
|
+
info: ArchiveEntryInfo
|
|
9
|
+
/** The entry's bytes; for a stream archive valid only until the next entry. */
|
|
10
|
+
open: () => Promise<Readable>
|
|
11
|
+
/** Consumes what is left of the entry (a stream archive cannot move on before). */
|
|
12
|
+
drain: () => Promise<void>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface EntryAdmission {
|
|
16
|
+
path?: string
|
|
17
|
+
skip?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The safety rules every codec's entries pass. */
|
|
21
|
+
export interface EntryGuard {
|
|
22
|
+
readonly limits: ArchiveLimits
|
|
23
|
+
/** The normalized path to write, or why the entry is skipped; throws on what must stop extraction. */
|
|
24
|
+
admit: (info: ArchiveEntryInfo) => EntryAdmission
|
|
25
|
+
/** Counts an entry's bytes against the per-entry and total ceilings. */
|
|
26
|
+
meter: () => Transform
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Expansion guard for a decompressed stream against the compressed bytes fed in. */
|
|
30
|
+
export interface RatioGuard {
|
|
31
|
+
/** Counts compressed bytes; pipe the input through it. */
|
|
32
|
+
readonly input: Transform
|
|
33
|
+
/** Counts produced bytes and checks the ratio and the total; pipe the output through it. */
|
|
34
|
+
readonly output: Transform
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FormatSniffer {
|
|
38
|
+
/** The archive format leading bytes show, or `null`. */
|
|
39
|
+
formatOf: (head: Uint8Array) => ArchiveFormat | null
|
|
40
|
+
/** The original name a gzip header carries (FNAME). */
|
|
41
|
+
gzipName: (head: Uint8Array) => string | undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ZipCodec {
|
|
45
|
+
/** Entries from the central directory, after the count, size, ratio and overlap checks. */
|
|
46
|
+
entries: (reader: RangeReader, limits: ArchiveLimits) => AsyncGenerator<ArchiveEntryHandle>
|
|
47
|
+
/** A zip of the entries, as a stream. */
|
|
48
|
+
pack: (entries: Iterable<PackEntry> | AsyncIterable<PackEntry>, options: ArchivePackOptions) => Readable
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface TarCodec {
|
|
52
|
+
/** Entries in stream order; each must be consumed (or drained) before the next. */
|
|
53
|
+
entries: (stream: Readable) => AsyncGenerator<ArchiveEntryHandle>
|
|
54
|
+
/** A tar of the entries, as a stream (sizes are learned first, spooling when unknown). */
|
|
55
|
+
pack: (entries: Iterable<PackEntry> | AsyncIterable<PackEntry>, options: ArchivePackOptions) => Readable
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A source opened for reading an archive: random access when possible, otherwise a stream. */
|
|
59
|
+
export interface AcquiredArchive {
|
|
60
|
+
format: ArchiveFormat | null
|
|
61
|
+
head: Uint8Array
|
|
62
|
+
reader?: RangeReader
|
|
63
|
+
stream?: Readable
|
|
64
|
+
dispose: () => Promise<void>
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The part of a tar entry writer the packer uses. */
|
|
68
|
+
export interface TarEntryWriter {
|
|
69
|
+
write: (chunk: Buffer) => boolean
|
|
70
|
+
end: (data?: unknown) => unknown
|
|
71
|
+
once: (event: string, listener: (...args: unknown[]) => void) => unknown
|
|
72
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { FileInspector, VerifyPolicy } from '@owlmeans/file-inspector'
|
|
2
|
+
import type { FileSource, SignalOptions, SinkResult, TreeSink } from '../types.js'
|
|
3
|
+
import type { ArchiveFormat } from './consts.js'
|
|
4
|
+
|
|
5
|
+
export interface ArchiveLimits {
|
|
6
|
+
maxEntries: number
|
|
7
|
+
maxEntryBytes: number
|
|
8
|
+
maxTotalBytes: number
|
|
9
|
+
/** Largest expansion ratio of a zip entry or a gzip stream. */
|
|
10
|
+
maxRatio: number
|
|
11
|
+
/** Archive nesting followed: 1 keeps nested archives as files. */
|
|
12
|
+
maxDepth: number
|
|
13
|
+
maxPathBytes: number
|
|
14
|
+
maxPathSegments: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** What an archive entry is. */
|
|
18
|
+
// A closed set of entry kinds — a union.
|
|
19
|
+
export type ArchiveEntryType = 'file' | 'directory' | 'symlink' | 'hardlink' | 'other'
|
|
20
|
+
|
|
21
|
+
export interface ArchiveEntryInfo {
|
|
22
|
+
/** The path as the archive spells it. */
|
|
23
|
+
path: string
|
|
24
|
+
type: ArchiveEntryType
|
|
25
|
+
size?: number
|
|
26
|
+
compressedSize?: number
|
|
27
|
+
mode?: number
|
|
28
|
+
modifiedAt?: Date
|
|
29
|
+
/** Reported, never created. */
|
|
30
|
+
linkTarget?: string
|
|
31
|
+
encrypted?: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ArchiveListOptions extends SignalOptions {
|
|
35
|
+
limits?: Partial<ArchiveLimits>
|
|
36
|
+
/** Skip detection. */
|
|
37
|
+
format?: ArchiveFormat
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ArchiveExtractOptions extends ArchiveListOptions {
|
|
41
|
+
/** Links in the archive: `skip` (reported, default) or `refuse` (`ArchiveError('link')`). */
|
|
42
|
+
links?: 'skip' | 'refuse'
|
|
43
|
+
/** An entry whose write or check fails: `fail` (default) or `skip`. Limit and safety errors always fail. */
|
|
44
|
+
onEntryError?: 'fail' | 'skip'
|
|
45
|
+
/** Leading path segments dropped. */
|
|
46
|
+
strip?: number
|
|
47
|
+
filter?: (entry: ArchiveEntryInfo) => boolean
|
|
48
|
+
/** Each entry's head checked before it is written. */
|
|
49
|
+
verify?: VerifyPolicy
|
|
50
|
+
onEntry?: (entry: ArchiveEntryInfo, result: SinkResult) => void
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ExtractedEntry {
|
|
54
|
+
entry: ArchiveEntryInfo
|
|
55
|
+
/** The normalized path written. */
|
|
56
|
+
path: string
|
|
57
|
+
result: SinkResult
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface SkippedEntry {
|
|
61
|
+
entry: ArchiveEntryInfo
|
|
62
|
+
reason: string
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ExtractResult {
|
|
66
|
+
format: ArchiveFormat
|
|
67
|
+
extracted: ExtractedEntry[]
|
|
68
|
+
skipped: SkippedEntry[]
|
|
69
|
+
bytes: number
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface PackEntry {
|
|
73
|
+
path: string
|
|
74
|
+
source: FileSource | Uint8Array
|
|
75
|
+
mode?: number
|
|
76
|
+
modifiedAt?: Date
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ArchivePackOptions {
|
|
80
|
+
/** The packed file's name. */
|
|
81
|
+
name?: string
|
|
82
|
+
/** zlib level. */
|
|
83
|
+
level?: number
|
|
84
|
+
/** A tar needs sizes up front: a source without one is spooled up to this many bytes. Default 512 MiB. */
|
|
85
|
+
maxSpoolBytes?: number
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface ArchiveHelperOptions {
|
|
89
|
+
limits?: Partial<ArchiveLimits>
|
|
90
|
+
inspector?: FileInspector
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Reads and writes zip, tar, tar.gz and gz — safely, as streams, without trusting names. */
|
|
94
|
+
export interface ArchiveHelper {
|
|
95
|
+
/** The format, from the bytes. */
|
|
96
|
+
detect: (source: FileSource, options?: SignalOptions) => Promise<ArchiveFormat | null>
|
|
97
|
+
/** Entries without extracting: a zip from its central directory (tail reads when ranged), a tar by its headers. */
|
|
98
|
+
list: (source: FileSource, options?: ArchiveListOptions) => Promise<ArchiveEntryInfo[]>
|
|
99
|
+
/** Every admitted entry written into the tree. */
|
|
100
|
+
extract: (source: FileSource, tree: TreeSink, options?: ArchiveExtractOptions) => Promise<ExtractResult>
|
|
101
|
+
/** A one-shot source producing the archive (gz takes exactly one entry). */
|
|
102
|
+
pack: (format: ArchiveFormat, entries: Iterable<PackEntry> | AsyncIterable<PackEntry>, options?: ArchivePackOptions) => FileSource
|
|
103
|
+
/** A source of the gzip-compressed bytes. */
|
|
104
|
+
gzip: (source: FileSource, options?: ArchivePackOptions) => FileSource
|
|
105
|
+
/** A source of the decompressed bytes, under the ratio and size limits. */
|
|
106
|
+
gunzip: (source: FileSource, options?: ArchiveListOptions) => FileSource
|
|
107
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { Readable } from 'node:stream'
|
|
2
|
+
import yauzl from 'yauzl'
|
|
3
|
+
import type { Entry, ZipFile } from 'yauzl'
|
|
4
|
+
import yazl from 'yazl'
|
|
5
|
+
import { ArchiveError, LimitError, PathError } from '../errors.js'
|
|
6
|
+
import type { RangeReader } from '../types.js'
|
|
7
|
+
import { pathUtils } from '../utils/paths.js'
|
|
8
|
+
import { rangeUtils } from '../utils/range.js'
|
|
9
|
+
import { RATIO_FLOOR_BYTES, ZIP_LOCAL_HEADER_BYTES } from './consts.js'
|
|
10
|
+
import type { ArchiveEntryInfo, ArchiveLimits, ArchivePackOptions, PackEntry } from './types.js'
|
|
11
|
+
import type { ArchiveEntryHandle, ZipCodec } from './types.local.js'
|
|
12
|
+
|
|
13
|
+
/** Zip read from its central directory (yauzl, random access) and written as a stream (yazl). */
|
|
14
|
+
export const createZipCodec = (): ZipCodec => {
|
|
15
|
+
const mapError = (error: unknown): Error => {
|
|
16
|
+
const message = (error as Error).message ?? ''
|
|
17
|
+
if (/absolute path/i.test(message)) {
|
|
18
|
+
return new PathError('absolute')
|
|
19
|
+
}
|
|
20
|
+
if (/invalid relative path|\.\./i.test(message)) {
|
|
21
|
+
return new PathError('traversal')
|
|
22
|
+
}
|
|
23
|
+
if (/invalid characters/i.test(message)) {
|
|
24
|
+
return new PathError('invalid-char')
|
|
25
|
+
}
|
|
26
|
+
return error instanceof Error && error.name !== 'Error' ? error : new ArchiveError('corrupt')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const readAll = (zipfile: ZipFile, limits: ArchiveLimits): Promise<Entry[]> => new Promise((resolve, reject) => {
|
|
30
|
+
const all: Entry[] = []
|
|
31
|
+
zipfile.on('entry', (entry: Entry) => {
|
|
32
|
+
all.push(entry)
|
|
33
|
+
if (all.length > limits.maxEntries) {
|
|
34
|
+
zipfile.removeAllListeners()
|
|
35
|
+
reject(new LimitError('entries'))
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
zipfile.readEntry()
|
|
39
|
+
})
|
|
40
|
+
zipfile.once('end', () => resolve(all))
|
|
41
|
+
zipfile.once('error', error => reject(mapError(error)))
|
|
42
|
+
zipfile.readEntry()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
const infoOf = (entry: Entry): ArchiveEntryInfo => {
|
|
46
|
+
const mode = (entry.externalFileAttributes >>> 16) & 0xffff
|
|
47
|
+
const directory = entry.fileName.endsWith('/')
|
|
48
|
+
const symlink = (mode & 0o170000) === 0o120000
|
|
49
|
+
return {
|
|
50
|
+
path: entry.fileName,
|
|
51
|
+
type: directory ? 'directory' : symlink ? 'symlink' : 'file',
|
|
52
|
+
size: entry.uncompressedSize,
|
|
53
|
+
compressedSize: entry.compressedSize,
|
|
54
|
+
mode: mode !== 0 ? mode & 0o7777 : undefined,
|
|
55
|
+
modifiedAt: entry.getLastModDate(),
|
|
56
|
+
encrypted: entry.isEncrypted(),
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const audit = (all: Entry[], limits: ArchiveLimits): void => {
|
|
61
|
+
let declared = 0
|
|
62
|
+
for (const entry of all) {
|
|
63
|
+
declared += entry.uncompressedSize
|
|
64
|
+
if (entry.uncompressedSize > RATIO_FLOOR_BYTES && entry.uncompressedSize > entry.compressedSize * limits.maxRatio) {
|
|
65
|
+
throw new LimitError('ratio')
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (declared > limits.maxTotalBytes) {
|
|
69
|
+
throw new LimitError('total-bytes')
|
|
70
|
+
}
|
|
71
|
+
const ordered = [...all].sort((a, b) => a.relativeOffsetOfLocalHeader - b.relativeOffsetOfLocalHeader)
|
|
72
|
+
for (let index = 1; index < ordered.length; index++) {
|
|
73
|
+
const previous = ordered[index - 1]
|
|
74
|
+
const end = previous.relativeOffsetOfLocalHeader + ZIP_LOCAL_HEADER_BYTES + previous.fileNameLength + previous.compressedSize
|
|
75
|
+
if (ordered[index].relativeOffsetOfLocalHeader < end) {
|
|
76
|
+
throw new ArchiveError('overlap')
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const entries = async function * (reader: RangeReader, limits: ArchiveLimits): AsyncGenerator<ArchiveEntryHandle> {
|
|
82
|
+
let zipfile: ZipFile
|
|
83
|
+
try {
|
|
84
|
+
zipfile = await yauzl.fromRandomAccessReaderPromise(rangeUtils.zipReader(reader), reader.size, {
|
|
85
|
+
lazyEntries: true, validateEntrySizes: true, strictFileNames: false, decodeStrings: true, autoClose: false,
|
|
86
|
+
})
|
|
87
|
+
} catch (error) {
|
|
88
|
+
throw mapError(error)
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const all = await readAll(zipfile, limits)
|
|
92
|
+
audit(all, limits)
|
|
93
|
+
for (const entry of all) {
|
|
94
|
+
yield {
|
|
95
|
+
info: infoOf(entry),
|
|
96
|
+
open: async () => {
|
|
97
|
+
if (entry.compressionMethod !== 0 && entry.compressionMethod !== 8) {
|
|
98
|
+
throw new ArchiveError(`method:${entry.compressionMethod}`)
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
return await zipfile.openReadStreamPromise(entry)
|
|
102
|
+
} catch (error) {
|
|
103
|
+
throw mapError(error)
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
drain: async () => undefined,
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
} finally {
|
|
110
|
+
zipfile.close()
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const pack = (list: Iterable<PackEntry> | AsyncIterable<PackEntry>, options: ArchivePackOptions): Readable => {
|
|
115
|
+
const zip = new yazl.ZipFile()
|
|
116
|
+
const output = zip.outputStream as Readable
|
|
117
|
+
const add = async (): Promise<void> => {
|
|
118
|
+
for await (const entry of list) {
|
|
119
|
+
const path = pathUtils.entry(entry.path)
|
|
120
|
+
const metadata = {
|
|
121
|
+
mtime: entry.modifiedAt ?? new Date(),
|
|
122
|
+
mode: 0o100000 | ((entry.mode ?? 0o644) & 0o777),
|
|
123
|
+
...(options.level != null ? { compressionLevel: options.level } : {}),
|
|
124
|
+
}
|
|
125
|
+
const source = entry.source
|
|
126
|
+
if (source instanceof Uint8Array) {
|
|
127
|
+
zip.addBuffer(Buffer.from(source), path, metadata)
|
|
128
|
+
} else {
|
|
129
|
+
zip.addReadStreamLazy(path, metadata, callback => {
|
|
130
|
+
source.open().then(opened => callback(null, opened.stream), error => callback(error, Readable.from([])))
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
zip.end()
|
|
135
|
+
}
|
|
136
|
+
add().catch(error => output.destroy(error as Error))
|
|
137
|
+
|
|
138
|
+
return output
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return { entries, pack }
|
|
142
|
+
}
|