lean-s3 0.1.6 → 0.2.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/src/url.js DELETED
@@ -1,87 +0,0 @@
1
- /**
2
- * @param {string} endpoint
3
- * @param {string} bucket
4
- * @param {string} region
5
- * @param {string} path
6
- * @returns {URL}
7
- */
8
- export function buildRequestUrl(endpoint, bucket, region, path) {
9
- const [endpointWithBucketAndRegion, replacedBucket] =
10
- replaceDomainPlaceholders(endpoint, bucket, region);
11
-
12
- const result = new URL(endpointWithBucketAndRegion);
13
-
14
- const pathPrefix = result.pathname.endsWith("/")
15
- ? result.pathname
16
- : `${result.pathname}/`;
17
-
18
- const pathSuffix = replacedBucket
19
- ? normalizePath(path)
20
- : `${normalizePath(bucket)}/${normalizePath(path)}`;
21
-
22
- result.pathname = pathPrefix + pathSuffix;
23
-
24
- return result;
25
- }
26
-
27
- /**
28
- * @param {string} endpoint
29
- * @param {string} bucket
30
- * @param {string} region
31
- * @returns {[endpoint: string, replacedBucket: boolean]}
32
- */
33
- function replaceDomainPlaceholders(endpoint, bucket, region) {
34
- const replacedBucket = endpoint.includes("{bucket}");
35
- return [
36
- endpoint.replaceAll("{bucket}", bucket).replaceAll("{region}", region),
37
- replacedBucket,
38
- ];
39
- }
40
-
41
- /**
42
- * Removes trailing and leading slashes.
43
- * @param {string} path
44
- * @returns {string}
45
- */
46
- function normalizePath(path) {
47
- const start = path[0] === "/" ? 1 : 0;
48
- const end = path[path.length - 1] === "/" ? path.length - 1 : path.length;
49
- return path.substring(start, end);
50
- }
51
-
52
- /**
53
- * Sorts headers alphabetically. Removes headers that are undefined/null.
54
- *
55
- * `http.request` doesn't allow passing `undefined` as header values (despite the types allowing it),
56
- * so we have to filter afterwards.
57
- *
58
- * @param {Record<string, string | undefined>} unfilteredHeadersUnsorted
59
- * @returns {Record<string, string>}
60
- */
61
- export function prepareHeadersForSigning(unfilteredHeadersUnsorted) {
62
- /** @type {Record<string, string>} */
63
- const result = {};
64
-
65
- // TODO: `Object.keys(headersUnsorted).sort()` is constant in our case,
66
- // maybe we want to move this somewhere else to avoid sorting every time
67
-
68
- for (const header of Object.keys(unfilteredHeadersUnsorted).sort()) {
69
- const v = unfilteredHeadersUnsorted[header];
70
- if (v !== undefined && v !== null) {
71
- result[header] = v;
72
- }
73
- }
74
- return result;
75
- }
76
-
77
- /**
78
- * @param {number | undefined} start
79
- * @param {number | undefined} endExclusive
80
- * @returns {string | undefined}
81
- */
82
- export function getRangeHeader(start, endExclusive) {
83
- return typeof start === "number" || typeof endExclusive === "number"
84
- ? // Http-ranges are end-inclusive, we are exclusiv ein our slice
85
- `bytes=${start ?? 0}-${typeof endExclusive === "number" ? endExclusive - 1 : ""}`
86
- : undefined;
87
- }