@joripspace/cli 0.5.4 → 0.5.7

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.
@@ -1,42 +1,42 @@
1
- async function cacheTagForKey(key) {
2
- const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(key));
3
- const hex = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
4
- return `r2-${hex}`;
5
- }
6
-
7
- export default {
8
- async fetch(request, env, ctx) {
9
- const url = new URL(request.url);
10
- const match = url.pathname.match(/^\/files\/(.+)$/);
11
- if (!match) {
12
- return Response.json({ ok: true, routes: ['/files/{key}'] });
13
- }
14
-
15
- const key = decodeURIComponent(match[1]);
16
- if (request.method === 'GET') {
17
- const object = await env.STORAGE.get(key);
18
- if (!object) {
19
- return new Response('Not found', { status: 404 });
20
- }
21
- return new Response(object.body, {
22
- headers: {
23
- 'Content-Type': object.httpMetadata?.contentType ?? 'application/octet-stream',
24
- 'Cache-Control': 'public, max-age=3600',
25
- 'Cache-Tag': await cacheTagForKey(key),
26
- },
27
- });
28
- }
29
-
30
- if (request.method === 'PUT') {
31
- await env.STORAGE.put(key, request.body, {
32
- httpMetadata: {
33
- contentType: request.headers.get('content-type') ?? 'application/octet-stream',
34
- },
35
- });
36
- await ctx.cache.purge({ tags: [await cacheTagForKey(key)] });
37
- return Response.json({ ok: true, key }, { headers: { 'Cache-Control': 'private, no-store' } });
38
- }
39
-
40
- return new Response('Method not allowed', { status: 405 });
41
- },
42
- };
1
+ async function cacheTagForKey(key) {
2
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(key));
3
+ const hex = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
4
+ return `r2-${hex}`;
5
+ }
6
+
7
+ export default {
8
+ async fetch(request, env, ctx) {
9
+ const url = new URL(request.url);
10
+ const match = url.pathname.match(/^\/files\/(.+)$/);
11
+ if (!match) {
12
+ return Response.json({ ok: true, routes: ['/files/{key}'] });
13
+ }
14
+
15
+ const key = decodeURIComponent(match[1]);
16
+ if (request.method === 'GET') {
17
+ const object = await env.STORAGE.get(key);
18
+ if (!object) {
19
+ return new Response('Not found', { status: 404 });
20
+ }
21
+ return new Response(object.body, {
22
+ headers: {
23
+ 'Content-Type': object.httpMetadata?.contentType ?? 'application/octet-stream',
24
+ 'Cache-Control': 'public, max-age=3600',
25
+ 'Cache-Tag': await cacheTagForKey(key),
26
+ },
27
+ });
28
+ }
29
+
30
+ if (request.method === 'PUT') {
31
+ await env.STORAGE.put(key, request.body, {
32
+ httpMetadata: {
33
+ contentType: request.headers.get('content-type') ?? 'application/octet-stream',
34
+ },
35
+ });
36
+ await ctx.cache.purge({ tags: [await cacheTagForKey(key)] });
37
+ return Response.json({ ok: true, key }, { headers: { 'Cache-Control': 'private, no-store' } });
38
+ }
39
+
40
+ return new Response('Method not allowed', { status: 405 });
41
+ },
42
+ };
@@ -1,7 +1,7 @@
1
- {
2
- "id": "r2-file-api",
3
- "name": "R2 File API",
4
- "description": "PUT and GET files through the project R2 bucket.",
5
- "entrypoint": "worker.js",
6
- "tags": ["starter", "r2", "api"]
7
- }
1
+ {
2
+ "id": "r2-file-api",
3
+ "name": "R2 File API",
4
+ "description": "PUT and GET files through the project R2 bucket.",
5
+ "entrypoint": "worker.js",
6
+ "tags": ["starter", "r2", "api"]
7
+ }
@@ -1,28 +0,0 @@
1
- const path = require('node:path');
2
-
3
- function installerFlags(args) {
4
- const values = Array.isArray(args) ? args : [];
5
- return {
6
- wantsHelp: values.includes('--help') || values.includes('-h'),
7
- wantsJson: values.includes('--json'),
8
- wantsQuiet: values.includes('--quiet') || values.includes('-quiet'),
9
- wantsVersion: values.includes('--version') || values.includes('-v'),
10
- };
11
- }
12
-
13
- function normalizeWindowsPathEntry(value, env = process.env) {
14
- const variables = new Map(
15
- Object.entries(env || {}).map(([name, nested]) => [name.toLowerCase(), String(nested || '')])
16
- );
17
- const expanded = String(value || '')
18
- .trim()
19
- .replace(/^"|"$/g, '')
20
- .replace(/%([^%]+)%/g, (match, name) => variables.get(String(name).toLowerCase()) || match);
21
- if (!expanded) return '';
22
- return path.win32
23
- .normalize(expanded)
24
- .replace(/[\\/]+$/u, '')
25
- .toLowerCase();
26
- }
27
-
28
- module.exports = { installerFlags, normalizeWindowsPathEntry };
@@ -1,134 +0,0 @@
1
- const crypto = require('node:crypto');
2
- const fs = require('node:fs');
3
- const path = require('node:path');
4
-
5
- function installExecutableAtomically(sourcePath, targetPath, options = {}) {
6
- const source = path.resolve(sourcePath);
7
- const target = path.resolve(targetPath);
8
- const platform = options.platform || process.platform;
9
- const sourceStat = fs.lstatSync(source);
10
- if (!sourceStat.isFile() || sourceStat.isSymbolicLink()) {
11
- throw new Error('JoripSpace installer source must be a regular file.');
12
- }
13
-
14
- const directory = path.dirname(target);
15
- fs.mkdirSync(directory, { recursive: true });
16
- const before = executableSnapshot(target);
17
- const suffix = `${process.pid}-${Date.now()}-${crypto.randomBytes(8).toString('hex')}`;
18
- const temporary = path.join(directory, `.${path.basename(target)}.tmp-${suffix}`);
19
- let backup = '';
20
- let installed = false;
21
-
22
- try {
23
- fs.copyFileSync(source, temporary, fs.constants.COPYFILE_EXCL);
24
- fs.chmodSync(temporary, options.mode ?? 0o755);
25
- fsyncFile(temporary);
26
- const copied = fs.lstatSync(temporary);
27
- if (!copied.isFile() || copied.isSymbolicLink() || copied.size !== sourceStat.size) {
28
- throw new Error('JoripSpace installer temporary executable failed integrity checks.');
29
- }
30
- if (!sameSnapshot(before, executableSnapshot(target))) {
31
- throw new Error('Installed JoripSpace CLI changed during upgrade.');
32
- }
33
-
34
- if (platform === 'win32' && before) {
35
- backup = path.join(directory, `.${path.basename(target)}.previous-${suffix}`);
36
- fs.renameSync(target, backup);
37
- try {
38
- if (typeof options.beforePromote === 'function') options.beforePromote();
39
- fs.renameSync(temporary, target);
40
- } catch (error) {
41
- try {
42
- if (!fs.existsSync(target)) fs.renameSync(backup, target);
43
- } catch (rollbackError) {
44
- throw new Error(
45
- `JoripSpace CLI upgrade failed and the previous executable remains at ${backup}: ${messageOf(
46
- rollbackError
47
- )}`,
48
- { cause: error }
49
- );
50
- }
51
- backup = '';
52
- throw error;
53
- }
54
- } else {
55
- if (typeof options.beforePromote === 'function') options.beforePromote();
56
- fs.renameSync(temporary, target);
57
- }
58
- installed = true;
59
- fsyncDirectory(directory);
60
- if (backup) {
61
- try {
62
- fs.rmSync(backup, { force: true });
63
- backup = '';
64
- fsyncDirectory(directory);
65
- } catch {}
66
- }
67
- return { target, replaced: Boolean(before) };
68
- } finally {
69
- fs.rmSync(temporary, { force: true });
70
- if (installed && backup) {
71
- try {
72
- fs.rmSync(backup, { force: true });
73
- } catch {}
74
- }
75
- }
76
- }
77
-
78
- function executableSnapshot(filePath) {
79
- let stat;
80
- try {
81
- stat = fs.lstatSync(filePath);
82
- } catch (error) {
83
- if (error?.code === 'ENOENT') return null;
84
- throw error;
85
- }
86
- if (!stat.isFile() || stat.isSymbolicLink()) {
87
- throw new Error('Installed JoripSpace CLI target must be a regular file.');
88
- }
89
- return {
90
- dev: stat.dev,
91
- ino: stat.ino,
92
- size: stat.size,
93
- mtimeMs: stat.mtimeMs,
94
- sha256: crypto.createHash('sha256').update(fs.readFileSync(filePath)).digest('hex'),
95
- };
96
- }
97
-
98
- function sameSnapshot(left, right) {
99
- if (!left || !right) return left === right;
100
- return (
101
- left.dev === right.dev &&
102
- left.ino === right.ino &&
103
- left.size === right.size &&
104
- left.mtimeMs === right.mtimeMs &&
105
- left.sha256 === right.sha256
106
- );
107
- }
108
-
109
- function fsyncFile(filePath) {
110
- const descriptor = fs.openSync(filePath, 'r+');
111
- try {
112
- fs.fsyncSync(descriptor);
113
- } finally {
114
- fs.closeSync(descriptor);
115
- }
116
- }
117
-
118
- function fsyncDirectory(directory) {
119
- let descriptor;
120
- try {
121
- descriptor = fs.openSync(directory, 'r');
122
- fs.fsyncSync(descriptor);
123
- } catch (error) {
124
- if (!['EINVAL', 'ENOTSUP', 'EISDIR', 'EPERM'].includes(error?.code)) throw error;
125
- } finally {
126
- if (descriptor !== undefined) fs.closeSync(descriptor);
127
- }
128
- }
129
-
130
- function messageOf(error) {
131
- return error instanceof Error ? error.message : String(error);
132
- }
133
-
134
- module.exports = { installExecutableAtomically };