-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor ResourceTokenManager to TS
- Loading branch information
1 parent
8b13242
commit 5fec4f6
Showing
9 changed files
with
66 additions
and
70 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
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,50 @@ | ||
import { v4 as uuid } from "uuid"; | ||
import logger from "./logwrapper"; | ||
|
||
interface ResourceToken { | ||
path: string; | ||
length: number; | ||
} | ||
|
||
class ResourceTokenManager { | ||
tokens: Record<string, ResourceToken> = {}; | ||
|
||
private deleteToken(token: string) { | ||
logger.debug(`Deleting token: ${token}`); | ||
if (this.tokens[token] !== undefined) { | ||
delete this.tokens[token]; | ||
} | ||
} | ||
|
||
getResourcePath(token: string) { | ||
const resource = this.tokens[token]; | ||
|
||
// delete the token if we actually had something saved. | ||
// delay for the given length before deletion to allow multiple requests at once and loading. | ||
if (resource != null) { | ||
setTimeout((t) => { | ||
this.deleteToken(t); | ||
}, resource.length, token); | ||
return resource.path; | ||
} | ||
return null; | ||
} | ||
|
||
storeResourcePath(path: string, length: string | number) { | ||
let tokenLength = 5; | ||
|
||
if (typeof length === "string" && length != null && length !== "") { | ||
tokenLength = parseFloat(length); | ||
} else if (typeof length === "number" && length != null && !isNaN(length)) { | ||
tokenLength = length; | ||
} | ||
|
||
const token = uuid(); | ||
this.tokens[token] = { path: path, length: tokenLength * 1000 }; | ||
return token; | ||
} | ||
} | ||
|
||
const resourceTokenManager = new ResourceTokenManager(); | ||
|
||
export { resourceTokenManager as ResourceTokenManager }; |
This file was deleted.
Oops, something went wrong.
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