-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(gatsby): Convert cache.js to TypeScript #20622
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
const babelPreset = require(`babel-preset-gatsby-package`)() | ||
module.exports = require(`babel-jest`).createTransformer(babelPreset) | ||
module.exports = require(`babel-jest`).createTransformer({ | ||
...babelPreset, | ||
overrides: [ | ||
...(babelPreset.overrides || []), | ||
{ | ||
test: `**/*.ts`, | ||
plugins: [[`@babel/plugin-transform-typescript`, { isTSX: true }]], | ||
}, | ||
], | ||
}) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import manager, { Store, StoreConfig, CachingConfig } from "cache-manager" | ||
import fs from "fs-extra" | ||
import fsStore from "cache-manager-fs-hash" | ||
import path from "path" | ||
|
||
const MAX_CACHE_SIZE = 250 | ||
const TTL = Number.MAX_SAFE_INTEGER | ||
|
||
interface ICacheProperties { | ||
name?: string | ||
store?: Store | ||
} | ||
|
||
export default class Cache { | ||
public name: string | ||
public store: Store | ||
public cache?: manager.Cache | ||
|
||
constructor({ name = `db`, store = fsStore }: ICacheProperties = {}) { | ||
this.name = name | ||
this.store = store | ||
} | ||
|
||
get directory(): string { | ||
return path.join(process.cwd(), `.cache/caches/${this.name}`) | ||
} | ||
|
||
init(): Cache { | ||
fs.ensureDirSync(this.directory) | ||
|
||
const configs: StoreConfig[] = [ | ||
{ | ||
store: `memory`, | ||
max: MAX_CACHE_SIZE, | ||
ttl: TTL, | ||
}, | ||
{ | ||
store: this.store, | ||
ttl: TTL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cache-manager says this is a required proeprty, even though the fs-hash store uses the one inside options |
||
options: { | ||
path: this.directory, | ||
ttl: TTL, | ||
}, | ||
}, | ||
] | ||
|
||
const caches = configs.map(cache => manager.caching(cache)) | ||
|
||
this.cache = manager.multiCaching(caches) | ||
|
||
return this | ||
} | ||
|
||
get<T = unknown>(key): Promise<T | undefined> { | ||
return new Promise(resolve => { | ||
// eslint-disable-next-line no-unused-expressions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ESLint is complaining here, which I think is because it's an older version that doesn't recognize optional chaining |
||
this.cache?.get<T>(key, (err, res) => { | ||
resolve(err ? undefined : res) | ||
}) | ||
}) | ||
} | ||
|
||
set<T>( | ||
key: string, | ||
value: T, | ||
args: CachingConfig = { ttl: TTL } | ||
): Promise<T | undefined> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had a goal to avoid using generics as much as possible. Do you think it's possible to do here? I'm not totally familiar with the cache module yet. Is the value supposed to be any specific expected types? I would look through the codebase and see when we use this if we align on any specific types. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generics are unavoidable if you're typing Promises. I'm also using them for the cache getters and setters, because the return type depends on the input type. |
||
return new Promise(resolve => { | ||
// eslint-disable-next-line no-unused-expressions | ||
this.cache?.set(key, value, args, err => { | ||
resolve(err ? undefined : value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit surpised it didn't pick up that this was a rename. That resolve(undefined) was in the original file. |
||
}) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the right target. I don't think it affects the actual compilation, as that's up to Bable. It's just for typechecking, so it knows which language features are available |
||
"lib": ["ES2015"], | ||
"moduleResolution": "node", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey there! Can you explain these config changes to me? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing.
|
||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍