-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup: separate utils into separate files (#109)
- Loading branch information
Showing
12 changed files
with
82 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Env } from '../env'; | ||
|
||
/** | ||
* @param env Worker env | ||
* @returns True if we want to either cache files or | ||
* directory listings | ||
*/ | ||
export function isCacheEnabled(env: Env): boolean { | ||
return env.ENVIRONMENT !== 'e2e-tests'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const byteUnits = ['B', 'KB', 'MB', 'GB', 'TB']; | ||
|
||
/** | ||
* Checks whether or not an R2 object or R2ObjectBody has a body | ||
*/ | ||
export function objectHasBody( | ||
object: R2Object | R2ObjectBody | ||
): object is R2ObjectBody { | ||
return (<R2ObjectBody>object).body !== undefined; | ||
} | ||
|
||
/** | ||
* Converts raw size into human readable bytes | ||
* @returns Something like `4.5 KB` or `8.7 MB` | ||
*/ | ||
export function toReadableBytes(bytes: number): string { | ||
let l = 0; | ||
let n = parseInt(bytes.toString(), 10) || 0; | ||
|
||
while (n >= 1000 && ++l) { | ||
n = n / 1000; | ||
} | ||
|
||
return n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + byteUnits[l]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @param request Request object | ||
* @returns {@link URL} instance if url is valid, a 400 | ||
* response otherwise | ||
*/ | ||
export function parseUrl(request: Request): URL | undefined { | ||
let url: URL | undefined; | ||
|
||
try { | ||
url = new URL(request.url); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
return url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import './util.test'; | ||
import './utils/object.test'; | ||
import './utils/path.test'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
import { toReadableBytes } from '../../../src/utils/object'; | ||
|
||
describe('toReadableBytes', () => { | ||
it('converts 10 to `10 B`', () => { | ||
const result = toReadableBytes(10); | ||
assert.strictEqual(result, '10 B'); | ||
}); | ||
|
||
it('converts 1024 to `1.0 KB`', () => { | ||
const result = toReadableBytes(1024); | ||
assert.strictEqual(result, '1.0 KB'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters