Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion jest-transformer.js
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 }]],
},
],
})
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ module.exports = {
`__tests__/fixtures`,
],
transform: {
"^.+\\.js$": `<rootDir>/jest-transformer.js`,
"^.+\\.tsx?$": `<rootDir>/node_modules/ts-jest/preprocessor.js`,
"^.+\\.[jt]sx?$": `<rootDir>/jest-transformer.js`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

},
moduleNameMapper: {
"^highlight.js$": `<rootDir>/node_modules/highlight.js/lib/index.js`,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"@babel/plugin-transform-typescript": "^7.7.4",
"@babel/runtime": "^7.7.7",
"@lerna/prompt": "3.18.5",
"@types/cache-manager": "^2.10.1",
"@types/express": "^4.17.2",
"@types/fs-extra": "^8.0.1",
"@types/got": "^9.6.9",
"@types/jest": "^24.0.23",
"@types/node": "^12.12.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/__tests__/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jest.mock(`fs-extra`, () => {
ensureDirSync: jest.fn(),
}
})
const Cache = require(`../cache`)
const Cache = require(`../cache`).default
const fs = require(`fs-extra`)
const manager = require(`cache-manager`)

Expand Down
58 changes: 0 additions & 58 deletions packages/gatsby/src/utils/cache.js

This file was deleted.

75 changes: 75 additions & 0 deletions packages/gatsby/src/utils/cache.ts
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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> {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use git mv to preserve git history? It's hard to review this file because I can't tell what's different. E.g., this resolve(undefined) on error feels like a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

})
})
}
}
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/get-cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Cache = require(`./cache`)
const Cache = require(`./cache`).default

let caches = new Map()
const caches = new Map()

module.exports = function getCache(name) {
let cache = caches.get(name)
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"compilerOptions": {
"target": "ESNext",
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! Can you explain these config changes to me?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing.

  • target Even though Babel chooses the actual compilation target, the type checker uses this to see if the target supports certain language features. I added this target because it defaults to ES3, which doesn't support getters and setters, so TS complains if you use them.
  • lib. The default lib includes DOM, which is incorrect and was causing a conflic with Cache
  • moduelResolution. Just telling it that we're using node. It was complaining about the json resolution option without it

"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3789,6 +3789,11 @@
"@types/connect" "*"
"@types/node" "*"

"@types/cache-manager@^2.10.1":
version "2.10.1"
resolved "https://registry.yarnpkg.com/@types/cache-manager/-/cache-manager-2.10.1.tgz#c7bc354be7988659e139e10fc7bde4b221f7f128"
integrity sha512-oJhVIOeC8dX9RZ7OtEZvZ/6cHF5aQWoRcuJ7KwK3Xb69hIIdElpWzfJ35fOXvYOgoyRXA34jFrD8lqEjDWz37w==

"@types/color-name@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
Expand Down Expand Up @@ -3839,6 +3844,13 @@
"@types/express-serve-static-core" "*"
"@types/serve-static" "*"

"@types/fs-extra@^8.0.1":
version "8.0.1"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-8.0.1.tgz#a2378d6e7e8afea1564e44aafa2e207dadf77686"
integrity sha512-J00cVDALmi/hJOYsunyT52Hva5TnJeKP5yd1r+mH/ZU0mbYZflR0Z5kw5kITtKTRYMhm1JMClOFYdHnQszEvqw==
dependencies:
"@types/node" "*"

"@types/get-port@^0.0.4":
version "0.0.4"
resolved "https://registry.yarnpkg.com/@types/get-port/-/get-port-0.0.4.tgz#eb6bb7423d9f888b632660dc7d2fd3e69a35643e"
Expand Down