opencode-fs-plugin 0.0.0-stage → 1.0.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/INSTALL.md +186 -0
- package/LICENSE +225 -0
- package/README.md +265 -2
- package/SPEC.md +45 -0
- package/package.json +63 -4
- package/skills/filesystem/SKILL.md +153 -0
- package/src/core/errors.ts +81 -0
- package/src/core/format.ts +100 -0
- package/src/core/fsutil.ts +228 -0
- package/src/core/mode.ts +107 -0
- package/src/core/parallel.ts +76 -0
- package/src/core/permissions.ts +134 -0
- package/src/core/sandbox.ts +270 -0
- package/src/core/schema.ts +16 -0
- package/src/core/stream.ts +84 -0
- package/src/core/types.ts +51 -0
- package/src/index.ts +172 -0
- package/src/tools/cat.ts +90 -0
- package/src/tools/chmod.ts +93 -0
- package/src/tools/cp.ts +136 -0
- package/src/tools/cut.ts +93 -0
- package/src/tools/df.ts +78 -0
- package/src/tools/diff.ts +136 -0
- package/src/tools/du.ts +102 -0
- package/src/tools/find.ts +181 -0
- package/src/tools/grep.ts +205 -0
- package/src/tools/gzip.ts +164 -0
- package/src/tools/hash.ts +84 -0
- package/src/tools/head.ts +78 -0
- package/src/tools/ln.ts +98 -0
- package/src/tools/ls.ts +178 -0
- package/src/tools/mkdir.ts +70 -0
- package/src/tools/mktemp.ts +83 -0
- package/src/tools/mv.ts +135 -0
- package/src/tools/pipe.ts +107 -0
- package/src/tools/readlink.ts +90 -0
- package/src/tools/rm.ts +91 -0
- package/src/tools/rmdir.ts +117 -0
- package/src/tools/sed.ts +151 -0
- package/src/tools/sort.ts +73 -0
- package/src/tools/stat.ts +157 -0
- package/src/tools/tail.ts +74 -0
- package/src/tools/tar.ts +195 -0
- package/src/tools/tee.ts +51 -0
- package/src/tools/touch.ts +130 -0
- package/src/tools/tr.ts +123 -0
- package/src/tools/trash.ts +86 -0
- package/src/tools/truncate.ts +96 -0
- package/src/tools/uniq.ts +76 -0
- package/src/tools/wc.ts +97 -0
- package/src/tools/zip.ts +231 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: EPL-2.0
|
|
3
|
+
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Type, type Static } from "@sinclair/typebox";
|
|
7
|
+
import { inputStream, lines } from "../core/stream.js";
|
|
8
|
+
import { textResult, type ToolSpec } from "../core/types.js";
|
|
9
|
+
|
|
10
|
+
const Schema = Type.Object(
|
|
11
|
+
{
|
|
12
|
+
paths: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { description: "Files to read. Omit to read stdin." })),
|
|
13
|
+
count: Type.Optional(Type.Boolean({ description: "Prefix each line with its count (`-c`)." })),
|
|
14
|
+
repeated: Type.Optional(Type.Boolean({ description: "Only print repeated lines (`-d`)." })),
|
|
15
|
+
uniqueOnly: Type.Optional(Type.Boolean({ description: "Only print unique lines (`-u`)." })),
|
|
16
|
+
ignoreCase: Type.Optional(Type.Boolean({ description: "Fold case (`-i`)." })),
|
|
17
|
+
skipFields: Type.Optional(Type.Integer({ minimum: 0, description: "Skip fields when comparing (`-f`)." })),
|
|
18
|
+
skipChars: Type.Optional(Type.Integer({ minimum: 0, description: "Skip characters when comparing (`-s`)." })),
|
|
19
|
+
},
|
|
20
|
+
{ additionalProperties: false },
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
type Input = Static<typeof Schema>;
|
|
24
|
+
|
|
25
|
+
export const uniqTool: ToolSpec<Input> = {
|
|
26
|
+
name: "uniq",
|
|
27
|
+
description:
|
|
28
|
+
"Filter adjacent duplicate lines with Linux `uniq` semantics (`-c`, `-d`, `-u`, `-i`, `-f`, `-s`).",
|
|
29
|
+
schema: Schema,
|
|
30
|
+
pipeable: "filter",
|
|
31
|
+
mutates: false,
|
|
32
|
+
async run(ctx, input, io) {
|
|
33
|
+
const paths = input.paths ?? [];
|
|
34
|
+
const targets = [];
|
|
35
|
+
for (const requested of paths) {
|
|
36
|
+
const target = await ctx.sandbox.resolveFollow(requested);
|
|
37
|
+
targets.push({ requested, full: target.full, target });
|
|
38
|
+
}
|
|
39
|
+
if (targets.length > 0) {
|
|
40
|
+
await ctx.permission.assert(targets.map(({ target }) => ({ target, boundary: target.full })));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const output: string[] = [];
|
|
44
|
+
let previous: string | undefined;
|
|
45
|
+
let count = 0;
|
|
46
|
+
let first = "";
|
|
47
|
+
const flush = () => {
|
|
48
|
+
if (previous === undefined) return;
|
|
49
|
+
if (input.repeated === true && count < 2) return;
|
|
50
|
+
if (input.uniqueOnly === true && count > 1) return;
|
|
51
|
+
output.push(`${input.count === true ? `${String(count).padStart(7)} ` : ""}${first}`);
|
|
52
|
+
};
|
|
53
|
+
for await (const line of lines(inputStream(targets.map((target) => target.full), io.stdin))) {
|
|
54
|
+
const comparable = compareKey(line.text, input);
|
|
55
|
+
if (comparable === previous) {
|
|
56
|
+
count++;
|
|
57
|
+
} else {
|
|
58
|
+
flush();
|
|
59
|
+
previous = comparable;
|
|
60
|
+
count = 1;
|
|
61
|
+
first = line.text;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
flush();
|
|
65
|
+
return textResult(output.map((line) => `${line}\n`).join(""), { lines: output.length });
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
function compareKey(value: string, input: Input): string {
|
|
70
|
+
let text = value;
|
|
71
|
+
if (input.skipFields !== undefined && input.skipFields > 0) {
|
|
72
|
+
text = text.split(/\s+/).slice(input.skipFields).join(" ");
|
|
73
|
+
}
|
|
74
|
+
if (input.skipChars !== undefined && input.skipChars > 0) text = text.slice(input.skipChars);
|
|
75
|
+
return input.ignoreCase === true ? text.toLowerCase() : text;
|
|
76
|
+
}
|
package/src/tools/wc.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: EPL-2.0
|
|
3
|
+
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Type, type Static } from "@sinclair/typebox";
|
|
7
|
+
import { collect, inputStream, readFiles } from "../core/stream.js";
|
|
8
|
+
import { textResult, type ToolSpec } from "../core/types.js";
|
|
9
|
+
|
|
10
|
+
const Schema = Type.Object(
|
|
11
|
+
{
|
|
12
|
+
paths: Type.Optional(Type.Array(Type.String({ minLength: 1 }), { description: "Files to count. Omit to read stdin." })),
|
|
13
|
+
lines: Type.Optional(Type.Boolean({ description: "Count lines (`-l`)." })),
|
|
14
|
+
words: Type.Optional(Type.Boolean({ description: "Count words (`-w`)." })),
|
|
15
|
+
bytes: Type.Optional(Type.Boolean({ description: "Count bytes (`-c`)." })),
|
|
16
|
+
chars: Type.Optional(Type.Boolean({ description: "Count characters (`-m`)." })),
|
|
17
|
+
maxLineLength: Type.Optional(Type.Boolean({ description: "Print the longest line length (`-L`)." })),
|
|
18
|
+
},
|
|
19
|
+
{ additionalProperties: false },
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
type Input = Static<typeof Schema>;
|
|
23
|
+
|
|
24
|
+
interface Counts {
|
|
25
|
+
lines: number;
|
|
26
|
+
words: number;
|
|
27
|
+
bytes: number;
|
|
28
|
+
chars: number;
|
|
29
|
+
maxLine: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const wcTool: ToolSpec<Input> = {
|
|
33
|
+
name: "wc",
|
|
34
|
+
description:
|
|
35
|
+
"Count lines, words, bytes, characters or maximum line length with Linux `wc` semantics " +
|
|
36
|
+
"(`-l`, `-w`, `-c`, `-m`, `-L`).",
|
|
37
|
+
schema: Schema,
|
|
38
|
+
pipeable: "sink",
|
|
39
|
+
mutates: false,
|
|
40
|
+
async run(ctx, input, io) {
|
|
41
|
+
const paths = input.paths ?? [];
|
|
42
|
+
const targets = [];
|
|
43
|
+
for (const requested of paths) {
|
|
44
|
+
const target = await ctx.sandbox.resolveFollow(requested);
|
|
45
|
+
targets.push({ requested, full: target.full, target });
|
|
46
|
+
}
|
|
47
|
+
if (targets.length > 0) {
|
|
48
|
+
await ctx.permission.assert(targets.map(({ target }) => ({ target, boundary: target.full })));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const results: Array<{ name: string; counts: Counts }> = [];
|
|
52
|
+
if (targets.length === 0) {
|
|
53
|
+
results.push({ name: "", counts: count(await collect(await inputStream([], io.stdin))) });
|
|
54
|
+
} else {
|
|
55
|
+
for (const target of targets) {
|
|
56
|
+
results.push({ name: target.requested, counts: count(await collect(readFiles([target.full]))) });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const total = results.reduce(
|
|
61
|
+
(sum, result) => ({
|
|
62
|
+
lines: sum.lines + result.counts.lines,
|
|
63
|
+
words: sum.words + result.counts.words,
|
|
64
|
+
bytes: sum.bytes + result.counts.bytes,
|
|
65
|
+
chars: sum.chars + result.counts.chars,
|
|
66
|
+
maxLine: Math.max(sum.maxLine, result.counts.maxLine),
|
|
67
|
+
}),
|
|
68
|
+
{ lines: 0, words: 0, bytes: 0, chars: 0, maxLine: 0 },
|
|
69
|
+
);
|
|
70
|
+
if (results.length > 1) results.push({ name: "total", counts: total });
|
|
71
|
+
|
|
72
|
+
const any = input.lines || input.words || input.bytes || input.chars || input.maxLineLength;
|
|
73
|
+
const rows = results.map((result) => format(result.counts, result.name, input, !any));
|
|
74
|
+
/* v8 ignore next */
|
|
75
|
+
return textResult(rows.join("\n") + (rows.length > 0 ? "\n" : ""), { total });
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function count(data: Buffer): Counts {
|
|
80
|
+
const text = data.toString("utf8");
|
|
81
|
+
/* v8 ignore next 2 */
|
|
82
|
+
const lines = text.length === 0 ? 0 : text.split("\n").length - (text.endsWith("\n") ? 1 : 0);
|
|
83
|
+
/* v8 ignore next */
|
|
84
|
+
const words = text.trim().length === 0 ? 0 : text.trim().split(/\s+/).length;
|
|
85
|
+
const maxLine = text.split("\n").reduce((max, line) => Math.max(max, Array.from(line).length), 0);
|
|
86
|
+
return { lines, words, bytes: data.length, chars: Array.from(text).length, maxLine };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function format(counts: Counts, name: string, input: Input, defaults: boolean): string {
|
|
90
|
+
const values: number[] = [];
|
|
91
|
+
if (defaults || input.lines === true) values.push(counts.lines);
|
|
92
|
+
if (defaults || input.words === true) values.push(counts.words);
|
|
93
|
+
if (defaults || input.bytes === true) values.push(counts.bytes);
|
|
94
|
+
if (input.chars === true) values.push(counts.chars);
|
|
95
|
+
if (input.maxLineLength === true) values.push(counts.maxLine);
|
|
96
|
+
return `${values.map((value) => String(value).padStart(7)).join(" ")}${name === "" ? "" : ` ${name}`}`;
|
|
97
|
+
}
|
package/src/tools/zip.ts
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: EPL-2.0
|
|
3
|
+
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as fs from "node:fs/promises";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
import { ZipFile as ZipWriter } from "yazl";
|
|
9
|
+
import * as yauzl from "yauzl";
|
|
10
|
+
import { Type, type Static } from "@sinclair/typebox";
|
|
11
|
+
import { FsError, fromErrno } from "../core/errors.js";
|
|
12
|
+
import { collect } from "../core/stream.js";
|
|
13
|
+
import { textResult, type ToolSpec } from "../core/types.js";
|
|
14
|
+
|
|
15
|
+
const ZipSchema = Type.Object(
|
|
16
|
+
{
|
|
17
|
+
archive: Type.String({ minLength: 1, description: "ZIP archive to create." }),
|
|
18
|
+
paths: Type.Array(Type.String({ minLength: 1 }), { minItems: 1 }),
|
|
19
|
+
level: Type.Optional(Type.Integer({ minimum: 0, maximum: 9, description: "Compression level." })),
|
|
20
|
+
},
|
|
21
|
+
{ additionalProperties: false },
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const UnzipSchema = Type.Object(
|
|
25
|
+
{
|
|
26
|
+
archive: Type.String({ minLength: 1, description: "ZIP archive to read." }),
|
|
27
|
+
mode: Type.Optional(
|
|
28
|
+
Type.Union([Type.Literal("extract"), Type.Literal("list")], { description: "Operation. Default: extract." }),
|
|
29
|
+
),
|
|
30
|
+
directory: Type.Optional(Type.String({ description: "Destination directory (`-d`)." })),
|
|
31
|
+
overwrite: Type.Optional(Type.Boolean({ description: "Overwrite existing files (`-o`)." })),
|
|
32
|
+
noOverwrite: Type.Optional(Type.Boolean({ description: "Never overwrite (`-n`)." })),
|
|
33
|
+
},
|
|
34
|
+
{ additionalProperties: false },
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
type ZipInput = Static<typeof ZipSchema>;
|
|
38
|
+
type UnzipInput = Static<typeof UnzipSchema>;
|
|
39
|
+
|
|
40
|
+
export const zipTool: ToolSpec<ZipInput> = {
|
|
41
|
+
name: "zip",
|
|
42
|
+
description: "Create ZIP archives with common `zip` semantics (`-r` is implicit, compression level 0-9).",
|
|
43
|
+
schema: ZipSchema,
|
|
44
|
+
pipeable: false,
|
|
45
|
+
mutates: true,
|
|
46
|
+
async run(ctx, input) {
|
|
47
|
+
const archive = await ctx.sandbox.resolveForCreate(input.archive);
|
|
48
|
+
const members: Array<{ name: string; full: string; stats: Awaited<ReturnType<typeof fs.lstat>> }> = [];
|
|
49
|
+
for (const requested of input.paths) {
|
|
50
|
+
const target = await ctx.sandbox.resolveExisting(requested, { allowRoot: true });
|
|
51
|
+
const stats = await ctx.sandbox.lstat(target.full, requested);
|
|
52
|
+
const base = path.basename(path.resolve(requested));
|
|
53
|
+
members.push({ name: base, full: target.full, stats });
|
|
54
|
+
if (stats.isDirectory()) await collectMembers(target.full, base, members);
|
|
55
|
+
}
|
|
56
|
+
const entries = await Promise.all(
|
|
57
|
+
members.map(async (member) => ({
|
|
58
|
+
target: await ctx.sandbox.resolveExisting(member.full, { allowRoot: true }),
|
|
59
|
+
boundary: member.stats.isDirectory() ? member.full : path.dirname(member.full),
|
|
60
|
+
})),
|
|
61
|
+
);
|
|
62
|
+
await ctx.permission.assert([...entries, { target: archive, boundary: archive.full }]);
|
|
63
|
+
|
|
64
|
+
const writer = new ZipWriter();
|
|
65
|
+
for (const member of members) {
|
|
66
|
+
const name = member.name.split(path.sep).join("/");
|
|
67
|
+
if (member.stats.isDirectory()) {
|
|
68
|
+
/* v8 ignore next */
|
|
69
|
+
writer.addEmptyDirectory(name.endsWith("/") ? name : `${name}/`, {
|
|
70
|
+
mtime: member.stats.mtime,
|
|
71
|
+
mode: Number(member.stats.mode) & 0o7777,
|
|
72
|
+
});
|
|
73
|
+
} else if (member.stats.isSymbolicLink()) {
|
|
74
|
+
writer.addBuffer(Buffer.from(await fs.readlink(member.full)), name, {
|
|
75
|
+
mtime: member.stats.mtime,
|
|
76
|
+
mode: 0o120777,
|
|
77
|
+
compress: true,
|
|
78
|
+
});
|
|
79
|
+
} else {
|
|
80
|
+
writer.addFile(member.full, name, {
|
|
81
|
+
mtime: member.stats.mtime,
|
|
82
|
+
mode: Number(member.stats.mode) & 0o7777,
|
|
83
|
+
compress: (input.level ?? 6) !== 0,
|
|
84
|
+
compressionLevel: input.level ?? 6,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
writer.end();
|
|
89
|
+
const data = await collect(writer.outputStream as AsyncIterable<Buffer>);
|
|
90
|
+
/* v8 ignore next */
|
|
91
|
+
await fs.writeFile(archive.full, data).catch((error) => {
|
|
92
|
+
throw fromErrno(error);
|
|
93
|
+
});
|
|
94
|
+
return textResult("", { archive: input.archive, members: members.length });
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const unzipTool: ToolSpec<UnzipInput> = {
|
|
99
|
+
name: "unzip",
|
|
100
|
+
description: "List or extract ZIP archives with common `unzip` semantics (`-l`, `-d`, `-o`, `-n`).",
|
|
101
|
+
schema: UnzipSchema,
|
|
102
|
+
pipeable: false,
|
|
103
|
+
mutates: true,
|
|
104
|
+
async run(ctx, input) {
|
|
105
|
+
const archive = await ctx.sandbox.resolveExisting(input.archive, { allowRoot: true });
|
|
106
|
+
await ctx.permission.assert([{ target: archive, boundary: archive.full }]);
|
|
107
|
+
/* v8 ignore next */
|
|
108
|
+
const buffer = await fs.readFile(archive.full).catch((error) => {
|
|
109
|
+
throw fromErrno(error);
|
|
110
|
+
});
|
|
111
|
+
const zip = await openZip(buffer);
|
|
112
|
+
const names: string[] = [];
|
|
113
|
+
const rootTarget = input.directory === undefined
|
|
114
|
+
? await ctx.sandbox.resolveForCreate(".")
|
|
115
|
+
: await ctx.sandbox.resolveForCreate(input.directory);
|
|
116
|
+
const root = rootTarget.full;
|
|
117
|
+
if (input.mode === "list") {
|
|
118
|
+
for (const entry of zip.entries) names.push(entry.fileName);
|
|
119
|
+
return textResult(names.map((name) => `${name}\n`).join(""), { archive: input.archive, members: names });
|
|
120
|
+
}
|
|
121
|
+
await ctx.permission.assert([{ target: rootTarget, boundary: root }]);
|
|
122
|
+
await fs.mkdir(root, { recursive: true });
|
|
123
|
+
for (const entry of zip.entries) {
|
|
124
|
+
const name = safeName(entry.fileName);
|
|
125
|
+
/* v8 ignore next */
|
|
126
|
+
if (name === undefined) continue;
|
|
127
|
+
const destination = path.join(root, name);
|
|
128
|
+
/* v8 ignore next */
|
|
129
|
+
if (!isInside(root, destination)) throw new FsError(`Blocked archive path: ${entry.fileName}`, "archive");
|
|
130
|
+
if (entry.fileName.endsWith("/")) {
|
|
131
|
+
await fs.mkdir(destination, { recursive: true });
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (input.noOverwrite === true && (await exists(destination))) continue;
|
|
135
|
+
if (input.overwrite !== true && (await exists(destination))) {
|
|
136
|
+
throw new FsError(`File exists: "${destination}"`, "exists");
|
|
137
|
+
}
|
|
138
|
+
await fs.mkdir(path.dirname(destination), { recursive: true });
|
|
139
|
+
const mode = (entry.externalFileAttributes >>> 16) & 0o170777;
|
|
140
|
+
const data = await collect(await openEntry(zip.file, entry));
|
|
141
|
+
if ((mode & 0o170000) === 0o120000) {
|
|
142
|
+
const target = data.toString("utf8");
|
|
143
|
+
/* v8 ignore next */
|
|
144
|
+
if (path.isAbsolute(target) || target.split("/").includes("..")) {
|
|
145
|
+
throw new FsError(`Blocked symlink target: ${target}`, "archive");
|
|
146
|
+
}
|
|
147
|
+
await fs.symlink(target, destination);
|
|
148
|
+
} else {
|
|
149
|
+
await fs.writeFile(destination, data, { mode: mode & 0o7777 });
|
|
150
|
+
}
|
|
151
|
+
names.push(name);
|
|
152
|
+
}
|
|
153
|
+
zip.file.close();
|
|
154
|
+
return textResult("", { archive: input.archive, members: names });
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
async function collectMembers(
|
|
159
|
+
full: string,
|
|
160
|
+
name: string,
|
|
161
|
+
members: Array<{ name: string; full: string; stats: Awaited<ReturnType<typeof fs.lstat>> }>,
|
|
162
|
+
): Promise<void> {
|
|
163
|
+
const entries = await fs.readdir(full);
|
|
164
|
+
for (const entry of entries) {
|
|
165
|
+
const child = path.join(full, entry);
|
|
166
|
+
const member = `${name}/${entry}`;
|
|
167
|
+
const stats = await fs.lstat(child);
|
|
168
|
+
members.push({ name: member, full: child, stats });
|
|
169
|
+
if (stats.isDirectory()) await collectMembers(child, member, members);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
interface ZipContents {
|
|
174
|
+
file: yauzl.ZipFile;
|
|
175
|
+
entries: yauzl.Entry[];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function openZip(buffer: Buffer): Promise<ZipContents> {
|
|
179
|
+
return new Promise((resolve, reject) => {
|
|
180
|
+
yauzl.fromBuffer(buffer, { lazyEntries: true }, (error, file) => {
|
|
181
|
+
/* v8 ignore next */
|
|
182
|
+
if (error || !file) {
|
|
183
|
+
reject(fromErrno(error ?? new Error("Cannot open zip")));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const entries: yauzl.Entry[] = [];
|
|
187
|
+
file.on("entry", (entry: yauzl.Entry) => {
|
|
188
|
+
entries.push(entry);
|
|
189
|
+
file.readEntry();
|
|
190
|
+
});
|
|
191
|
+
file.on("end", () => resolve({ file, entries }));
|
|
192
|
+
/* v8 ignore next */
|
|
193
|
+
file.on("error", (zipError: Error) => reject(fromErrno(zipError)));
|
|
194
|
+
file.readEntry();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function openEntry(file: yauzl.ZipFile, entry: yauzl.Entry): Promise<AsyncIterable<Buffer>> {
|
|
200
|
+
return new Promise((resolve, reject) => {
|
|
201
|
+
file.openReadStream(entry, (error, stream) => {
|
|
202
|
+
/* v8 ignore next */
|
|
203
|
+
if (error || !stream) reject(fromErrno(error ?? new Error("Cannot open entry")));
|
|
204
|
+
else resolve(stream as AsyncIterable<Buffer>);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function safeName(name: string): string | undefined {
|
|
210
|
+
/* v8 ignore next */
|
|
211
|
+
if (name.includes("\0") || path.isAbsolute(name)) throw new FsError(`Blocked archive path: ${name}`, "archive");
|
|
212
|
+
const parts = name.split("/").filter((part) => part.length > 0);
|
|
213
|
+
/* v8 ignore next */
|
|
214
|
+
if (parts.some((part) => part === "..")) throw new FsError(`Blocked archive path: ${name}`, "archive");
|
|
215
|
+
/* v8 ignore next */
|
|
216
|
+
return parts.length === 0 ? undefined : parts.join(path.sep);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function isInside(root: string, target: string): boolean {
|
|
220
|
+
const rel = path.relative(root, target);
|
|
221
|
+
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function exists(target: string): Promise<boolean> {
|
|
225
|
+
try {
|
|
226
|
+
await fs.lstat(target);
|
|
227
|
+
return true;
|
|
228
|
+
} catch {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|