chore(): Reorganize modules (#7210)

**What**
Move all modules to the modules directory
This commit is contained in:
Adrien de Peretti
2024-05-02 17:33:34 +02:00
committed by GitHub
parent 7a351eef09
commit 4eae25e1ef
870 changed files with 91 additions and 62 deletions

View File

@@ -0,0 +1,6 @@
/dist
node_modules
.DS_store
.env*
.env
*.sql

View File

@@ -0,0 +1,132 @@
# @medusajs/cache-inmemory
## 1.8.10
### Patch Changes
- [#5511](https://github.com/medusajs/medusa/pull/5511) [`80fe362f3`](https://github.com/medusajs/medusa/commit/80fe362f33cba69e52418b57e2e2d476923fc510) Thanks [@adrien2p](https://github.com/adrien2p)! - Integration tests fixes and ignore ttl 0 on cache modules
- Updated dependencies [[`c39bf69a5`](https://github.com/medusajs/medusa/commit/c39bf69a5e5cae75d7fa12aa6022b10903557a32), [`154c9b43b`](https://github.com/medusajs/medusa/commit/154c9b43bde1fdff562aba9da8a79af2660b29b3)]:
- @medusajs/modules-sdk@1.12.3
## 1.8.9
### Patch Changes
- [#5468](https://github.com/medusajs/medusa/pull/5468) [`a45da9215`](https://github.com/medusajs/medusa/commit/a45da9215d2a7834c368037726aaa3961caadaf9) Thanks [@adrien2p](https://github.com/adrien2p)! - fix(medusa, modules-sdk, modules): Module loading was missing the expected dependencies and remote query reference fix
- Updated dependencies [[`a45da9215`](https://github.com/medusajs/medusa/commit/a45da9215d2a7834c368037726aaa3961caadaf9)]:
- @medusajs/modules-sdk@1.12.2
## 1.8.8
### Patch Changes
- [#4276](https://github.com/medusajs/medusa/pull/4276) [`afd1b67f1`](https://github.com/medusajs/medusa/commit/afd1b67f1c7de8cf07fd9fcbdde599a37914e9b5) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Use caret range
- Updated dependencies [[`14c0f62f8`](https://github.com/medusajs/medusa/commit/14c0f62f84704a4c87beff3daaff60a52f5c88b8)]:
- @medusajs/modules-sdk@1.8.8
## 1.8.7
### Patch Changes
- Updated dependencies [[`e73c3e51c`](https://github.com/medusajs/medusa/commit/e73c3e51c9cd192eeae7a57b24b07bd466214145)]:
- @medusajs/modules-sdk@1.8.7
## 1.8.6
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.6
## 1.8.5
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.5
## 1.8.4
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.4
## 1.8.3
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.3
## 1.8.2
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.2
## 1.8.1
### Patch Changes
- Updated dependencies []:
- @medusajs/modules-sdk@1.8.1
## 1.8.0
### Minor Changes
- [#3187](https://github.com/medusajs/medusa/pull/3187) [`f97b3d7cc`](https://github.com/medusajs/medusa/commit/f97b3d7ccee381d3491337ab5144bb44520382a7) Thanks [@fPolic](https://github.com/fPolic)! - feat(medusa, cache-redis, cache-inmemory): Added cache modules
### Patch Changes
- [#3685](https://github.com/medusajs/medusa/pull/3685) [`8ddb3952c`](https://github.com/medusajs/medusa/commit/8ddb3952c045e6c05c8d0f6922f0d4ba30cf3bd4) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Fix RC package versions
- [#3649](https://github.com/medusajs/medusa/pull/3649) [`bd12a9508`](https://github.com/medusajs/medusa/commit/bd12a95083b69a70b83ad38578c5a68738c41b2b) Thanks [@carlos-r-l-rodrigues](https://github.com/carlos-r-l-rodrigues)! - Export initialize method for all modules
- [#3531](https://github.com/medusajs/medusa/pull/3531) [`4e9d257d3`](https://github.com/medusajs/medusa/commit/4e9d257d3bf76703ef5be8ca054cc9f0f7339def) Thanks [@carlos-r-l-rodrigues](https://github.com/carlos-r-l-rodrigues)! - Remove dependency on @medusajs/medusa from Inventory and Stock-Location Modules
- Updated dependencies [[`8ddb3952c`](https://github.com/medusajs/medusa/commit/8ddb3952c045e6c05c8d0f6922f0d4ba30cf3bd4), [`55e94d0b4`](https://github.com/medusajs/medusa/commit/55e94d0b45776776639d3970d4264b8f5c5385dd), [`bd12a9508`](https://github.com/medusajs/medusa/commit/bd12a95083b69a70b83ad38578c5a68738c41b2b), [`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73), [`bca1f80dd`](https://github.com/medusajs/medusa/commit/bca1f80dd501d878455e1ad4f5091cf20ef900ea), [`271844aed`](https://github.com/medusajs/medusa/commit/271844aedbe45c369e188b5d06458dbd6984cd39), [`4e9d257d3`](https://github.com/medusajs/medusa/commit/4e9d257d3bf76703ef5be8ca054cc9f0f7339def)]:
- @medusajs/modules-sdk@1.8.0
## 1.8.0-rc.3
### Patch Changes
- [#3649](https://github.com/medusajs/medusa/pull/3649) [`bd12a9508`](https://github.com/medusajs/medusa/commit/bd12a95083b69a70b83ad38578c5a68738c41b2b) Thanks [@carlos-r-l-rodrigues](https://github.com/carlos-r-l-rodrigues)! - Export initialize method for all modules
- Updated dependencies [[`bd12a9508`](https://github.com/medusajs/medusa/commit/bd12a95083b69a70b83ad38578c5a68738c41b2b)]:
- @medusajs/modules-sdk@0.1.0-rc.4
## 1.8.0-rc.2
### Patch Changes
- Updated dependencies [[`55e94d0b4`](https://github.com/medusajs/medusa/commit/55e94d0b45776776639d3970d4264b8f5c5385dd)]:
- @medusajs/modules-sdk@0.1.0-rc.3
## 1.8.0-rc.1
### Patch Changes
- chore: Fix RC package versions
- Updated dependencies []:
- @medusajs/modules-sdk@0.1.0-rc.2
## 1.8.0-rc.0
### Minor Changes
- [#3187](https://github.com/medusajs/medusa/pull/3187) [`f97b3d7cc`](https://github.com/medusajs/medusa/commit/f97b3d7ccee381d3491337ab5144bb44520382a7) Thanks [@fPolic](https://github.com/fPolic)! - feat(medusa, cache-redis, cache-inmemory): Added cache modules
### Patch Changes
- [#3531](https://github.com/medusajs/medusa/pull/3531) [`4e9d257d3`](https://github.com/medusajs/medusa/commit/4e9d257d3bf76703ef5be8ca054cc9f0f7339def) Thanks [@carlos-r-l-rodrigues](https://github.com/carlos-r-l-rodrigues)! - Remove dependency on @medusajs/medusa from Inventory and Stock-Location Modules
- Updated dependencies [[`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73), [`271844aed`](https://github.com/medusajs/medusa/commit/271844aedbe45c369e188b5d06458dbd6984cd39), [`4e9d257d3`](https://github.com/medusajs/medusa/commit/4e9d257d3bf76703ef5be8ca054cc9f0f7339def)]:
- @medusajs/modules-sdk@0.1.0-rc.0

View File

@@ -0,0 +1,23 @@
# Medusa Cache In-memory
Medusa in-memory cache module. Use plain JS Map as a cache store.
## Installation
```
yarn add @medusajs/cache-inmemory
```
## Options
```
{
ttl?: number // Time to keep data in cache (in seconds)
}
```
### Note
Recommended for testing and development. For production, use Redis cache module.
### Other caching modules
- [Medusa Cache Redis](../cache-redis/README.md)

View File

@@ -0,0 +1,13 @@
module.exports = {
transform: {
"^.+\\.[jt]s?$": [
"ts-jest",
{
tsconfig: "tsconfig.json",
isolatedModules: true,
},
],
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `ts`],
}

View File

@@ -0,0 +1,39 @@
{
"name": "@medusajs/cache-inmemory",
"version": "1.8.10",
"description": "In-memory Cache Module for Medusa",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/cache-inmemory"
},
"engines": {
"node": ">=16"
},
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"author": "Medusa",
"license": "MIT",
"devDependencies": {
"@medusajs/types": "^1.11.6",
"cross-env": "^5.2.1",
"jest": "^29.6.3",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
},
"scripts": {
"watch": "tsc --build --watch",
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"build": "rimraf dist && tsc --build",
"test": "jest --passWithNoTests"
},
"dependencies": {
"@medusajs/modules-sdk": "^1.12.3"
}
}

View File

@@ -0,0 +1,12 @@
import { ModuleExports } from "@medusajs/modules-sdk"
import InMemoryCacheService from "./services/inmemory-cache"
const service = InMemoryCacheService
const moduleDefinition: ModuleExports = {
service,
}
export default moduleDefinition
export * from "./initialize"
export * from "./types"

View File

@@ -0,0 +1,23 @@
import {
ExternalModuleDeclaration,
InternalModuleDeclaration,
MedusaModule,
Modules,
} from "@medusajs/modules-sdk"
import { ICacheService } from "@medusajs/types"
import { InMemoryCacheModuleOptions } from "../types"
export const initialize = async (
options?: InMemoryCacheModuleOptions | ExternalModuleDeclaration
): Promise<ICacheService> => {
const serviceKey = Modules.CACHE
const loaded = await MedusaModule.bootstrap<ICacheService>({
moduleKey: serviceKey,
defaultPath: "@medusajs/cache-inmemory",
declaration: options as
| InternalModuleDeclaration
| ExternalModuleDeclaration,
})
return loaded[serviceKey]
}

View File

@@ -0,0 +1,96 @@
import { InMemoryCacheService } from "../index"
jest.setTimeout(40000)
describe("InMemoryCacheService", () => {
let inMemoryCache
beforeEach(() => {
jest.clearAllMocks()
})
it("Stores and retrieves data", async () => {
inMemoryCache = new InMemoryCacheService({}, {})
await inMemoryCache.set("cache-key", { data: "value" })
expect(await inMemoryCache.get("cache-key")).toEqual({ data: "value" })
})
it("Invalidates single record", async () => {
inMemoryCache = new InMemoryCacheService({}, {})
await inMemoryCache.set("cache-key", { data: "value" })
await inMemoryCache.invalidate("cache-key")
expect(await inMemoryCache.get("cache-key")).toEqual(null)
})
it("Invalidates multiple keys with wildcard (end matching)", async () => {
inMemoryCache = new InMemoryCacheService({}, {})
await inMemoryCache.set("cache-key:id_1:x:y", { data: "value" })
await inMemoryCache.set("cache-key:id_2:x:y", { data: "value" })
await inMemoryCache.set("cache-key:id_3:x:y", { data: "value" })
await inMemoryCache.set("cache-key-old", { data: "value" })
await inMemoryCache.invalidate("cache-key:*")
expect(await inMemoryCache.get("cache-key:id1:x:y")).toEqual(null)
expect(await inMemoryCache.get("cache-key:id2:x:y")).toEqual(null)
expect(await inMemoryCache.get("cache-key:id3:x:y")).toEqual(null)
expect(await inMemoryCache.get("cache-key-old")).toEqual({ data: "value" })
})
it("Invalidates multiple keys with wildcard (middle matching)", async () => {
inMemoryCache = new InMemoryCacheService({}, {})
await inMemoryCache.set("cache-key:1:new", { data: "value" })
await inMemoryCache.set("cache-key:2:new", { data: "value" })
await inMemoryCache.set("cache-key:3:new", { data: "value" })
await inMemoryCache.set("cache-key:4:old", { data: "value" })
await inMemoryCache.invalidate("cache-key:*:new")
expect(await inMemoryCache.get("cache-key:1:new")).toEqual(null)
expect(await inMemoryCache.get("cache-key:2:new")).toEqual(null)
expect(await inMemoryCache.get("cache-key:3:new")).toEqual(null)
expect(await inMemoryCache.get("cache-key:4:old")).toEqual({
data: "value",
})
})
it("Removes data after TTL", async () => {
inMemoryCache = new InMemoryCacheService({}, {})
await inMemoryCache.set("cache-key", { data: "value" }, 2)
expect(await inMemoryCache.get("cache-key")).toEqual({ data: "value" })
await new Promise((res) => setTimeout(res, 3000))
expect(await inMemoryCache.get("cache-key")).toEqual(null)
})
it("Removes data after default TTL if TTL params isn't passed", async () => {
inMemoryCache = new InMemoryCacheService({})
await inMemoryCache.set("cache-key", { data: "value" })
expect(await inMemoryCache.get("cache-key")).toEqual({ data: "value" })
await new Promise((res) => setTimeout(res, 33000))
expect(await inMemoryCache.get("cache-key")).toEqual(null)
})
it("Removes data after TTL from the config if TTL params isn't passed", async () => {
inMemoryCache = new InMemoryCacheService({}, { ttl: 1 })
await inMemoryCache.set("cache-key", { data: "value" })
expect(await inMemoryCache.get("cache-key")).toEqual({ data: "value" })
await new Promise((res) => setTimeout(res, 2000))
expect(await inMemoryCache.get("cache-key")).toEqual(null)
})
})

View File

@@ -0,0 +1 @@
export { default as InMemoryCacheService } from "./inmemory-cache"

View File

@@ -0,0 +1,106 @@
import { ICacheService } from "@medusajs/types"
import { CacheRecord, InMemoryCacheModuleOptions } from "../types"
const DEFAULT_TTL = 30 // seconds
type InjectedDependencies = {}
/**
* Class represents basic, in-memory, cache store.
*/
class InMemoryCacheService implements ICacheService {
protected readonly TTL: number
protected readonly store = new Map<string, CacheRecord<any>>()
protected readonly timoutRefs = new Map<string, NodeJS.Timeout>()
constructor(
deps: InjectedDependencies,
options: InMemoryCacheModuleOptions = {}
) {
this.TTL = options.ttl ?? DEFAULT_TTL
}
/**
* Retrieve data from the cache.
* @param key - cache key
*/
async get<T>(key: string): Promise<T | null> {
const now = Date.now()
const record: CacheRecord<T> | undefined = this.store.get(key)
const recordExpire = record?.expire ?? Infinity
if (!record || recordExpire < now) {
return null
}
return record.data
}
/**
* Set data to the cache.
* @param key - cache key under which the data is stored
* @param data - data to be stored in the cache
* @param ttl - expiration time in seconds
*/
async set<T>(key: string, data: T, ttl: number = this.TTL): Promise<void> {
if (ttl === 0) {
return
}
const record: CacheRecord<T> = { data, expire: ttl * 1000 + Date.now() }
const oldRecord = this.store.get(key)
if (oldRecord) {
clearTimeout(this.timoutRefs.get(key))
this.timoutRefs.delete(key)
}
const ref = setTimeout(async () => {
await this.invalidate(key)
}, ttl * 1000)
ref.unref()
this.timoutRefs.set(key, ref)
this.store.set(key, record)
}
/**
* Delete data from the cache.
* Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:"
*
* @param key - cache key
*/
async invalidate(key: string): Promise<void> {
let keys = [key]
if (key.includes("*")) {
const regExp = new RegExp(key.replace("*", ".*"))
keys = Array.from(this.store.keys()).filter((k) => k.match(regExp))
}
keys.forEach((key) => {
const timeoutRef = this.timoutRefs.get(key)
if (timeoutRef) {
clearTimeout(timeoutRef)
this.timoutRefs.delete(key)
}
this.store.delete(key)
})
}
/**
* Delete the entire cache.
*/
async clear() {
this.timoutRefs.forEach((ref) => clearTimeout(ref))
this.timoutRefs.clear()
this.store.clear()
}
}
export default InMemoryCacheService

View File

@@ -0,0 +1,17 @@
/**
* Shape of a record saved in `in-memory` cache
*/
export type CacheRecord<T> = {
data: T
/**
* Timestamp in milliseconds
*/
expire: number
}
export type InMemoryCacheModuleOptions = {
/**
* Time to keep data in cache (in seconds)
*/
ttl?: number
}

View File

@@ -0,0 +1,33 @@
{
"compilerOptions": {
"lib": [
"es5",
"es6",
"es2019"
],
"target": "es5",
"outDir": "./dist",
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true // to use ES5 specific tooling
},
"include": ["src"],
"exclude": [
"dist",
"./src/**/__tests__",
"./src/**/__mocks__",
"./src/**/__fixtures__",
"node_modules"
]
}

View File

@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src", "integration-tests"],
"exclude": ["node_modules"]
}