@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,11 @@
|
|
|
1
|
+
import type { RandomAccessReader as ZipRandomAccessReader } from 'yauzl'
|
|
2
|
+
import type { RangeReader } from '../../types.js'
|
|
3
|
+
|
|
4
|
+
export interface RangeUtils {
|
|
5
|
+
/** A yauzl random-access reader (central-directory zip reading) over a reader. */
|
|
6
|
+
zipReader: (reader: RangeReader) => ZipRandomAccessReader
|
|
7
|
+
/** A reader over bytes in memory. */
|
|
8
|
+
ofBuffer: (bytes: Uint8Array) => RangeReader
|
|
9
|
+
/** A reader over a regular file of known size. */
|
|
10
|
+
ofFile: (path: string, size: number) => RangeReader
|
|
11
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs'
|
|
2
|
+
import { open } from 'node:fs/promises'
|
|
3
|
+
import type { FileHandle } from 'node:fs/promises'
|
|
4
|
+
import { PassThrough, Readable } from 'node:stream'
|
|
5
|
+
import yauzl from 'yauzl'
|
|
6
|
+
import type { RandomAccessReader as ZipRandomAccessReader } from 'yauzl'
|
|
7
|
+
import type { RangeReader } from '../types.js'
|
|
8
|
+
import type { RangeUtils } from './range/types.js'
|
|
9
|
+
|
|
10
|
+
export const createRangeUtils = (): RangeUtils => {
|
|
11
|
+
const zipReader = (reader: RangeReader): ZipRandomAccessReader => {
|
|
12
|
+
const ZipReader = yauzl.RandomAccessReader as unknown as new () => ZipRandomAccessReader
|
|
13
|
+
const adapter = new ZipReader()
|
|
14
|
+
|
|
15
|
+
adapter._readStreamForRange = (start: number, end: number): Readable => {
|
|
16
|
+
const out = new PassThrough()
|
|
17
|
+
reader.stream(start, end).then(
|
|
18
|
+
stream => {
|
|
19
|
+
stream.once('error', error => out.destroy(error))
|
|
20
|
+
stream.pipe(out)
|
|
21
|
+
},
|
|
22
|
+
error => out.destroy(error as Error)
|
|
23
|
+
)
|
|
24
|
+
return out
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
adapter.read = (buffer, offset, length, position, callback) => {
|
|
28
|
+
reader.read(position, length).then(bytes => {
|
|
29
|
+
if (bytes.length < length) {
|
|
30
|
+
callback(new Error('unexpected EOF'))
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
buffer.set(bytes.subarray(0, length), offset)
|
|
34
|
+
callback(null)
|
|
35
|
+
}, error => callback(error as Error))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
adapter.close = callback => {
|
|
39
|
+
reader.close().then(() => callback(null), error => callback(error as Error))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return adapter
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const ofBuffer = (bytes: Uint8Array): RangeReader => ({
|
|
46
|
+
size: bytes.length,
|
|
47
|
+
read: async (offset, length) => bytes.subarray(offset, Math.min(offset + length, bytes.length)),
|
|
48
|
+
stream: async (start, end) => Readable.from([Buffer.from(bytes.subarray(start, end))], { objectMode: false }),
|
|
49
|
+
close: async () => undefined,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const ofFile = (path: string, size: number): RangeReader => {
|
|
53
|
+
let handle: Promise<FileHandle> | undefined
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
size,
|
|
57
|
+
read: async (offset, length) => {
|
|
58
|
+
handle ??= open(path, 'r')
|
|
59
|
+
const count = Math.max(0, Math.min(length, size - offset))
|
|
60
|
+
const buffer = Buffer.alloc(count)
|
|
61
|
+
const { bytesRead } = await (await handle).read(buffer, 0, count, offset)
|
|
62
|
+
return buffer.subarray(0, bytesRead)
|
|
63
|
+
},
|
|
64
|
+
stream: async (start, end) => end <= start
|
|
65
|
+
? Readable.from([], { objectMode: false })
|
|
66
|
+
: createReadStream(path, { start, end: end - 1 }),
|
|
67
|
+
close: async () => {
|
|
68
|
+
const opened = handle
|
|
69
|
+
handle = undefined
|
|
70
|
+
if (opened != null) {
|
|
71
|
+
await (await opened).close()
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return { zipReader, ofBuffer, ofFile }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const rangeUtils = createRangeUtils()
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface RedactedProxy {
|
|
2
|
+
type: string
|
|
3
|
+
host: string
|
|
4
|
+
port: number
|
|
5
|
+
id?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface RedactUtils {
|
|
9
|
+
/**
|
|
10
|
+
* `scheme://host[:port]/path` with no userinfo, query or fragment, and opaque path segments
|
|
11
|
+
* (20+ token characters: Drive ids, capability paths) masked.
|
|
12
|
+
*/
|
|
13
|
+
url: (url: string | URL) => string
|
|
14
|
+
/** A proxy by its credential-free id. */
|
|
15
|
+
proxy: (endpoint: RedactedProxy) => string
|
|
16
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RedactedProxy, RedactUtils } from './redact/types.js'
|
|
2
|
+
|
|
3
|
+
export const createRedactUtils = (): RedactUtils => {
|
|
4
|
+
const opaque = /^[A-Za-z0-9_-]{20,}$/
|
|
5
|
+
|
|
6
|
+
const url = (input: string | URL): string => {
|
|
7
|
+
let parsed: URL
|
|
8
|
+
try {
|
|
9
|
+
parsed = typeof input === 'string' ? new URL(input) : input
|
|
10
|
+
} catch {
|
|
11
|
+
return '<invalid-url>'
|
|
12
|
+
}
|
|
13
|
+
const path = parsed.pathname.split('/').map(segment => opaque.test(segment) ? '***' : segment).join('/')
|
|
14
|
+
|
|
15
|
+
return `${parsed.protocol}//${parsed.host}${path}`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const proxy = (endpoint: RedactedProxy): string => endpoint.id ?? `${endpoint.type}://${endpoint.host}:${endpoint.port}`
|
|
19
|
+
|
|
20
|
+
return { url, proxy }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const redactUtils = createRedactUtils()
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface ComposedSignal {
|
|
2
|
+
signal: AbortSignal
|
|
3
|
+
/** Releases the timer and the listeners on the parent signals. */
|
|
4
|
+
dispose: () => void
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface SignalUtils {
|
|
8
|
+
/**
|
|
9
|
+
* One signal that aborts when any parent aborts (with its reason) or, given `timeoutMs`, when
|
|
10
|
+
* the time runs out (with `reason()`).
|
|
11
|
+
*/
|
|
12
|
+
compose: (signals: Array<AbortSignal | undefined>, timeoutMs?: number, reason?: () => Error) => ComposedSignal
|
|
13
|
+
/** Throws the signal's reason when it already aborted. */
|
|
14
|
+
throwIfAborted: (signal?: AbortSignal) => void
|
|
15
|
+
/** Calls `listener` once when the signal aborts (in a microtask when it already did); answers the unsubscribe. */
|
|
16
|
+
onAbort: (signal: AbortSignal | undefined, listener: (reason: unknown) => void) => () => void
|
|
17
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { LimitError } from '../errors.js'
|
|
2
|
+
import type { ComposedSignal, SignalUtils } from './signals/types.js'
|
|
3
|
+
|
|
4
|
+
export const createSignalUtils = (): SignalUtils => {
|
|
5
|
+
const onAbort = (signal: AbortSignal | undefined, listener: (reason: unknown) => void): () => void => {
|
|
6
|
+
if (signal == null) {
|
|
7
|
+
return () => undefined
|
|
8
|
+
}
|
|
9
|
+
if (signal.aborted) {
|
|
10
|
+
// Deferred, so a caller can finish wiring (its own release, its handlers) before it runs.
|
|
11
|
+
let active = true
|
|
12
|
+
queueMicrotask(() => { if (active) listener(signal.reason) })
|
|
13
|
+
return () => { active = false }
|
|
14
|
+
}
|
|
15
|
+
const handler = (): void => listener(signal.reason)
|
|
16
|
+
signal.addEventListener('abort', handler, { once: true })
|
|
17
|
+
|
|
18
|
+
return () => signal.removeEventListener('abort', handler)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const compose = (
|
|
22
|
+
signals: Array<AbortSignal | undefined>, timeoutMs?: number, reason: () => Error = () => new LimitError('timeout')
|
|
23
|
+
): ComposedSignal => {
|
|
24
|
+
const controller = new AbortController()
|
|
25
|
+
const releases: Array<() => void> = []
|
|
26
|
+
let timer: NodeJS.Timeout | undefined
|
|
27
|
+
|
|
28
|
+
const dispose = (): void => {
|
|
29
|
+
if (timer != null) {
|
|
30
|
+
clearTimeout(timer)
|
|
31
|
+
timer = undefined
|
|
32
|
+
}
|
|
33
|
+
releases.splice(0).forEach(release => release())
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for (const parent of signals) {
|
|
37
|
+
releases.push(onAbort(parent, cause => {
|
|
38
|
+
controller.abort(cause)
|
|
39
|
+
dispose()
|
|
40
|
+
}))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (timeoutMs != null && !controller.signal.aborted) {
|
|
44
|
+
timer = setTimeout(() => {
|
|
45
|
+
controller.abort(reason())
|
|
46
|
+
dispose()
|
|
47
|
+
}, timeoutMs)
|
|
48
|
+
timer.unref?.()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return { signal: controller.signal, dispose }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const throwIfAborted = (signal?: AbortSignal): void => {
|
|
55
|
+
if (signal?.aborted === true) {
|
|
56
|
+
throw signal.reason
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return { compose, throwIfAborted, onAbort }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const signalUtils = createSignalUtils()
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Readable, Transform } from 'node:stream'
|
|
2
|
+
import type { StreamInput } from '../../sources/types.js'
|
|
3
|
+
|
|
4
|
+
export interface MeterProgress {
|
|
5
|
+
bytes: number
|
|
6
|
+
total?: number
|
|
7
|
+
elapsedMs: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface MeterOptions {
|
|
11
|
+
/** Refuse with `LimitError('max-bytes')` past this many bytes. */
|
|
12
|
+
maxBytes?: number
|
|
13
|
+
/** Refuse with `LimitError('idle-timeout')` after this long without a chunk. */
|
|
14
|
+
idleTimeoutMs?: number
|
|
15
|
+
/** The expected total, reported with progress. */
|
|
16
|
+
total?: number
|
|
17
|
+
onProgress?: (progress: MeterProgress) => void
|
|
18
|
+
progressIntervalMs?: number
|
|
19
|
+
hash?: 'sha256'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface StreamMeter {
|
|
23
|
+
/** The pass-through to pipe the bytes through. */
|
|
24
|
+
readonly stream: Transform
|
|
25
|
+
/** Bytes that passed so far. */
|
|
26
|
+
bytes: () => number
|
|
27
|
+
/** Hex sha256 of what passed, once the stream ended and a hash was asked for. */
|
|
28
|
+
sha256: () => string | undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface PeekedStream {
|
|
32
|
+
/** The leading bytes read (at least the asked size unless the stream ended first). */
|
|
33
|
+
head: Uint8Array
|
|
34
|
+
/** The whole stream again, head included. */
|
|
35
|
+
stream: Readable
|
|
36
|
+
/** Whether the head is the whole stream. */
|
|
37
|
+
ended: boolean
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface SpoolOptions {
|
|
41
|
+
maxBytes?: number
|
|
42
|
+
signal?: AbortSignal
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface SpooledFile {
|
|
46
|
+
path: string
|
|
47
|
+
size: number
|
|
48
|
+
/** Removes the file and its private directory. */
|
|
49
|
+
dispose: () => Promise<void>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface StreamUtils {
|
|
53
|
+
/** A Node stream over any accepted input. */
|
|
54
|
+
toReadable: (input: StreamInput | Uint8Array) => Readable
|
|
55
|
+
/** Counting, limiting, idle-guarding, hashing pass-through. */
|
|
56
|
+
meter: (options: MeterOptions) => StreamMeter
|
|
57
|
+
/** Reads the head of a stream and hands back a stream that replays it. */
|
|
58
|
+
peek: (stream: Readable, size: number) => Promise<PeekedStream>
|
|
59
|
+
/** The whole stream in memory, refused past `maxBytes`. */
|
|
60
|
+
collect: (stream: Readable, maxBytes: number) => Promise<Buffer>
|
|
61
|
+
/** The stream written to a private temporary file. */
|
|
62
|
+
spool: (stream: Readable, options?: SpoolOptions) => Promise<SpooledFile>
|
|
63
|
+
/** Destroys a stream nobody will read, swallowing its error. */
|
|
64
|
+
discard: (stream: Readable) => void
|
|
65
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { createWriteStream } from 'node:fs'
|
|
3
|
+
import { mkdtemp, rm } from 'node:fs/promises'
|
|
4
|
+
import { tmpdir } from 'node:os'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { Readable, Transform } from 'node:stream'
|
|
7
|
+
import { pipeline } from 'node:stream/promises'
|
|
8
|
+
import { LimitError } from '../errors.js'
|
|
9
|
+
import type { StreamInput } from '../sources/types.js'
|
|
10
|
+
import { DEFAULT_PROGRESS_INTERVAL_MS } from './streams/consts.js'
|
|
11
|
+
import type { MeterOptions, PeekedStream, SpooledFile, SpoolOptions, StreamMeter, StreamUtils } from './streams/types.js'
|
|
12
|
+
|
|
13
|
+
export const createStreamUtils = (): StreamUtils => {
|
|
14
|
+
const toReadable = (input: StreamInput | Uint8Array): Readable => {
|
|
15
|
+
if (input instanceof Readable) {
|
|
16
|
+
return input
|
|
17
|
+
}
|
|
18
|
+
if (input instanceof Uint8Array) {
|
|
19
|
+
return Readable.from([Buffer.from(input.buffer, input.byteOffset, input.byteLength)], { objectMode: false })
|
|
20
|
+
}
|
|
21
|
+
if (typeof (input as ReadableStream<Uint8Array>).getReader === 'function') {
|
|
22
|
+
return Readable.fromWeb(input as Parameters<typeof Readable.fromWeb>[0])
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return Readable.from(input as AsyncIterable<Uint8Array>, { objectMode: false })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const meter = (options: MeterOptions): StreamMeter => {
|
|
29
|
+
const started = Date.now()
|
|
30
|
+
const hash = options.hash === 'sha256' ? createHash('sha256') : undefined
|
|
31
|
+
const interval = options.progressIntervalMs ?? DEFAULT_PROGRESS_INTERVAL_MS
|
|
32
|
+
let bytes = 0
|
|
33
|
+
let lastReport = 0
|
|
34
|
+
let digest: string | undefined
|
|
35
|
+
let idle: NodeJS.Timeout | undefined
|
|
36
|
+
|
|
37
|
+
const report = (force: boolean): void => {
|
|
38
|
+
if (options.onProgress == null) {
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
const now = Date.now()
|
|
42
|
+
if (force || now - lastReport >= interval) {
|
|
43
|
+
lastReport = now
|
|
44
|
+
options.onProgress({ bytes, total: options.total, elapsedMs: now - started })
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const stopIdle = (): void => {
|
|
49
|
+
if (idle != null) {
|
|
50
|
+
clearTimeout(idle)
|
|
51
|
+
idle = undefined
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const transform = new Transform({
|
|
56
|
+
transform: (chunk: Buffer, _encoding, callback) => {
|
|
57
|
+
bytes += chunk.length
|
|
58
|
+
if (options.maxBytes != null && bytes > options.maxBytes) {
|
|
59
|
+
stopIdle()
|
|
60
|
+
callback(new LimitError('max-bytes'))
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
hash?.update(chunk)
|
|
64
|
+
armIdle()
|
|
65
|
+
report(false)
|
|
66
|
+
callback(null, chunk)
|
|
67
|
+
},
|
|
68
|
+
flush: callback => {
|
|
69
|
+
stopIdle()
|
|
70
|
+
digest = hash?.digest('hex')
|
|
71
|
+
report(true)
|
|
72
|
+
callback()
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const armIdle = (): void => {
|
|
77
|
+
if (options.idleTimeoutMs == null) {
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
stopIdle()
|
|
81
|
+
idle = setTimeout(() => transform.destroy(new LimitError('idle-timeout')), options.idleTimeoutMs)
|
|
82
|
+
idle.unref?.()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
transform.once('close', stopIdle)
|
|
86
|
+
armIdle()
|
|
87
|
+
|
|
88
|
+
return { stream: transform, bytes: () => bytes, sha256: () => digest }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const peek = async (stream: Readable, size: number): Promise<PeekedStream> => {
|
|
92
|
+
const iterator = stream[Symbol.asyncIterator]() as AsyncIterator<Buffer>
|
|
93
|
+
const chunks: Buffer[] = []
|
|
94
|
+
let length = 0
|
|
95
|
+
let ended = false
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
while (length < size) {
|
|
99
|
+
const next = await iterator.next()
|
|
100
|
+
if (next.done === true) {
|
|
101
|
+
ended = true
|
|
102
|
+
break
|
|
103
|
+
}
|
|
104
|
+
const chunk = typeof next.value === 'string' ? Buffer.from(next.value) : next.value
|
|
105
|
+
chunks.push(chunk)
|
|
106
|
+
length += chunk.length
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
await iterator.return?.()
|
|
110
|
+
throw error
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const head = Buffer.concat(chunks, length)
|
|
114
|
+
|
|
115
|
+
let finished = ended
|
|
116
|
+
const replay = async function * (): AsyncGenerator<Buffer> {
|
|
117
|
+
try {
|
|
118
|
+
if (head.length > 0) {
|
|
119
|
+
yield head
|
|
120
|
+
}
|
|
121
|
+
while (!finished) {
|
|
122
|
+
const next = await iterator.next()
|
|
123
|
+
if (next.done === true) {
|
|
124
|
+
finished = true
|
|
125
|
+
break
|
|
126
|
+
}
|
|
127
|
+
yield next.value
|
|
128
|
+
}
|
|
129
|
+
} finally {
|
|
130
|
+
if (!finished) {
|
|
131
|
+
await iterator.return?.()
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const replayed = Readable.from(replay(), { objectMode: false })
|
|
137
|
+
// A replay destroyed before it was read never enters the generator: release the source here.
|
|
138
|
+
replayed.once('close', () => {
|
|
139
|
+
if (!finished) {
|
|
140
|
+
finished = true
|
|
141
|
+
stream.destroy()
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
return { head, stream: replayed, ended }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const collect = async (stream: Readable, maxBytes: number): Promise<Buffer> => {
|
|
149
|
+
const chunks: Buffer[] = []
|
|
150
|
+
let length = 0
|
|
151
|
+
for await (const chunk of stream) {
|
|
152
|
+
const buffer = typeof chunk === 'string' ? Buffer.from(chunk) : chunk as Buffer
|
|
153
|
+
length += buffer.length
|
|
154
|
+
if (length > maxBytes) {
|
|
155
|
+
stream.destroy()
|
|
156
|
+
throw new LimitError('max-bytes')
|
|
157
|
+
}
|
|
158
|
+
chunks.push(buffer)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return Buffer.concat(chunks, length)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const spool = async (stream: Readable, options: SpoolOptions = {}): Promise<SpooledFile> => {
|
|
165
|
+
const dir = await mkdtemp(join(tmpdir(), 'owlmeans-file-streams-'))
|
|
166
|
+
const path = join(dir, 'spool')
|
|
167
|
+
const dispose = async (): Promise<void> => { await rm(dir, { recursive: true, force: true }) }
|
|
168
|
+
const counter = meter({ maxBytes: options.maxBytes })
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
await pipeline(stream, counter.stream, createWriteStream(path, { mode: 0o600, flags: 'wx' }), { signal: options.signal })
|
|
172
|
+
} catch (error) {
|
|
173
|
+
await dispose()
|
|
174
|
+
throw error
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return { path, size: counter.bytes(), dispose }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const discard = (stream: Readable): void => {
|
|
181
|
+
stream.once('error', () => undefined)
|
|
182
|
+
stream.destroy()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return { toReadable, meter, peek, collect, spool, discard }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export const streamUtils = createStreamUtils()
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { TreeSink } from '../types.js'
|
|
2
|
+
import { pathUtils } from './paths.js'
|
|
3
|
+
import type { TreeUtils } from './tree/types.js'
|
|
4
|
+
|
|
5
|
+
export const createTreeUtils = (): TreeUtils => {
|
|
6
|
+
const prefixed = (tree: TreeSink, prefix: string): TreeSink => {
|
|
7
|
+
const base = pathUtils.entry(prefix)
|
|
8
|
+
return {
|
|
9
|
+
kind: tree.kind,
|
|
10
|
+
entry: path => tree.entry(`${base}/${pathUtils.entry(path)}`),
|
|
11
|
+
directory: async (path, options) => await tree.directory(`${base}/${pathUtils.entry(path)}`, options),
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return { prefixed }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const treeUtils = createTreeUtils()
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The folder under `os.tmpdir()` a workspace lives in when no root is named. */
|
|
2
|
+
export const DEFAULT_WORKSPACE_DIR = 'owlmeans-workspace'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The file that makes a folder a scope. It is reserved: no entry path may contain a segment of this
|
|
6
|
+
* name, listings never show it.
|
|
7
|
+
*/
|
|
8
|
+
export const WORKSPACE_SCOPE_MARKER = '.owlmeans-scope'
|
|
9
|
+
|
|
10
|
+
/** Mode of the folders a workspace creates: private to the service's user. */
|
|
11
|
+
export const DEFAULT_WORKSPACE_DIR_MODE = 0o700
|
|
12
|
+
|
|
13
|
+
/** Mode of the files a workspace writes. */
|
|
14
|
+
export const DEFAULT_WORKSPACE_FILE_MODE = 0o600
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { lstat, readdir, rmdir } from 'node:fs/promises'
|
|
2
|
+
import { dirname, relative, resolve } from 'node:path'
|
|
3
|
+
import type { WorkspaceFs } from './types.local.js'
|
|
4
|
+
|
|
5
|
+
export const createWorkspaceFs = (): WorkspaceFs => {
|
|
6
|
+
const absent = (error: unknown): boolean => ['ENOENT', 'ENOTDIR'].includes((error as NodeJS.ErrnoException).code ?? '')
|
|
7
|
+
|
|
8
|
+
const isFile = async (path: string): Promise<boolean> => {
|
|
9
|
+
try {
|
|
10
|
+
return (await lstat(path)).isFile()
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (absent(error)) {
|
|
13
|
+
return false
|
|
14
|
+
}
|
|
15
|
+
throw error
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const dirs = async (path: string): Promise<string[]> => {
|
|
20
|
+
try {
|
|
21
|
+
return (await readdir(path, { withFileTypes: true })).filter(item => item.isDirectory()).map(item => item.name)
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (absent(error)) {
|
|
24
|
+
return []
|
|
25
|
+
}
|
|
26
|
+
throw error
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const removeEmptyParents = async (from: string, root: string): Promise<void> => {
|
|
31
|
+
const base = resolve(root)
|
|
32
|
+
let current = resolve(from)
|
|
33
|
+
while (current !== base && !relative(base, current).startsWith('..')) {
|
|
34
|
+
try {
|
|
35
|
+
await rmdir(current)
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (!absent(error)) {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
current = dirname(current)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return { isFile, dirs, removeEmptyParents }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const workspaceFs = createWorkspaceFs()
|