@adonisjs/lock 1.0.0-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ # The MIT License
2
+
3
+ Copyright (c) 2023
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # @adonisjs/lock
2
+
3
+ <br />
4
+
5
+ [![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
6
+
7
+ ## Introduction
8
+ Atomic locks (mutex) for AdonisJS applications. The package is built on top of [Verrou](https://github.com/Julien-R44/verrou) and provides a simple API to acquire and release locks with support for multiple drivers.
9
+
10
+ ## Official Documentation
11
+ The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/lock/introduction).
12
+
13
+ ## Contributing
14
+ One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.
15
+
16
+ We encourage you to read the [contribution guide](https://github.com/adonisjs/.github/blob/main/docs/CONTRIBUTING.md) before contributing to the framework.
17
+
18
+ ## Code of Conduct
19
+ In order to ensure that the AdonisJS community is welcoming to all, please review and abide by the [Code of Conduct](https://github.com/adonisjs/.github/blob/main/docs/CODE_OF_CONDUCT.md).
20
+
21
+ ## License
22
+ AdonisJS Lucid is open-sourced software licensed under the [MIT license](LICENSE.md).
23
+
24
+ [gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/lock/test.yml?style=for-the-badge
25
+ [gh-workflow-url]: https://github.com/adonisjs/lock/actions/workflows/test.yml "Github action"
26
+
27
+ [npm-image]: https://img.shields.io/npm/v/@adonisjs/lock/latest.svg?style=for-the-badge&logo=npm
28
+ [npm-url]: https://www.npmjs.com/package/@adonisjs/lock/v/latest "npm"
29
+
30
+ [typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
31
+
32
+ [license-url]: LICENSE.md
33
+ [license-image]: https://img.shields.io/github/license/adonisjs/lock?style=for-the-badge
@@ -0,0 +1,9 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ export {
8
+ __export
9
+ };
@@ -0,0 +1,42 @@
1
+ {{{
2
+ exports({ to: app.configPath('lock.ts') })
3
+ }}}
4
+ import env from '#start/env'
5
+ import { defineConfig, stores } from '@adonisjs/lock'
6
+
7
+ const lockConfig = defineConfig({
8
+ default: env.get('LOCK_STORE'),
9
+ stores: {
10
+ {{#if store === 'redis'}}
11
+ /**
12
+ * Redis store to save manage locks
13
+ */
14
+ redis: stores.redis({}),
15
+ {{/if}}
16
+ {{#if store === 'database'}}
17
+ /**
18
+ * Database store to manage locks
19
+ */
20
+ database: stores.database({
21
+ tableName: 'locks'
22
+ }),
23
+ {{/if}}
24
+ {{#if store === 'dynamodb'}}
25
+ /**
26
+ * DynamoDB store to manage locks
27
+ */
28
+ dynamodb: stores.dynamodb({}),
29
+ {{/if}}
30
+ /**
31
+ * Memory store could be used during
32
+ * testing
33
+ */
34
+ memory: stores.memory()
35
+ },
36
+ })
37
+
38
+ export default lockConfig
39
+
40
+ declare module '@adonisjs/lock/types' {
41
+ export interface LockStoresList extends InferLockStores<typeof lockConfig> {}
42
+ }
@@ -0,0 +1,47 @@
1
+ import { ConfigProvider } from '@adonisjs/core/types';
2
+ import { RedisConnections } from '@adonisjs/redis/types';
3
+ import { StoreFactory, DynamoDbOptions } from '@verrou/core/types';
4
+ import { E_LOCK_ALREADY_ACQUIRED, E_LOCK_NOT_OWNED, E_LOCK_STORAGE_ERROR, E_LOCK_TIMEOUT } from '@verrou/core';
5
+ import Configure from '@adonisjs/core/commands/configure';
6
+
7
+ /**
8
+ * Different stores supported by the lock module
9
+ */
10
+ declare const stores: {
11
+ memory: () => ConfigProvider<StoreFactory>;
12
+ redis: (config: {
13
+ connectionName?: keyof RedisConnections;
14
+ }) => ConfigProvider<StoreFactory>;
15
+ database: (config?: {
16
+ connectionName?: string;
17
+ tableName?: string;
18
+ }) => ConfigProvider<StoreFactory>;
19
+ dynamodb: (config: DynamoDbOptions) => ConfigProvider<StoreFactory>;
20
+ };
21
+
22
+ /**
23
+ * Re-exporting verrou errors
24
+ */
25
+
26
+ declare const errors_E_LOCK_ALREADY_ACQUIRED: typeof E_LOCK_ALREADY_ACQUIRED;
27
+ declare const errors_E_LOCK_NOT_OWNED: typeof E_LOCK_NOT_OWNED;
28
+ declare const errors_E_LOCK_STORAGE_ERROR: typeof E_LOCK_STORAGE_ERROR;
29
+ declare const errors_E_LOCK_TIMEOUT: typeof E_LOCK_TIMEOUT;
30
+ declare namespace errors {
31
+ export { errors_E_LOCK_ALREADY_ACQUIRED as E_LOCK_ALREADY_ACQUIRED, errors_E_LOCK_NOT_OWNED as E_LOCK_NOT_OWNED, errors_E_LOCK_STORAGE_ERROR as E_LOCK_STORAGE_ERROR, errors_E_LOCK_TIMEOUT as E_LOCK_TIMEOUT };
32
+ }
33
+
34
+ declare function configure(command: Configure): Promise<void>;
35
+
36
+ /**
37
+ * Define lock configuration
38
+ */
39
+ declare function defineConfig<KnownStores extends Record<string, ConfigProvider<StoreFactory>>>(config: {
40
+ default: keyof KnownStores;
41
+ stores: KnownStores;
42
+ }): {
43
+ default: keyof KnownStores;
44
+ stores: KnownStores;
45
+ };
46
+
47
+ export { configure, defineConfig, errors, stores };
package/build/index.js ADDED
@@ -0,0 +1,169 @@
1
+ import {
2
+ __export
3
+ } from "./chunk-6C3VEZWH.js";
4
+
5
+ // src/stores.ts
6
+ import { configProvider } from "@adonisjs/core";
7
+ import { RuntimeException } from "@adonisjs/core/exceptions";
8
+ var stores = {
9
+ /**
10
+ * Redis store
11
+ * You must install @adonisjs/redis to use this driver
12
+ */
13
+ redis(config) {
14
+ return configProvider.create(async (app) => {
15
+ const redis = await app.container.make("redis");
16
+ const { redisStore } = await import("@verrou/core/drivers/redis");
17
+ const redisConnection = redis.connection(config.connectionName);
18
+ const store = redisStore({ connection: redisConnection.ioConnection });
19
+ return { driver: store };
20
+ });
21
+ },
22
+ /**
23
+ * Memory store
24
+ */
25
+ memory() {
26
+ return configProvider.create(async () => {
27
+ const { memoryStore } = await import("@verrou/core/drivers/memory");
28
+ return { driver: memoryStore() };
29
+ });
30
+ },
31
+ /**
32
+ * Database store
33
+ * You must install @adonisjs/lucid to use this driver
34
+ */
35
+ database(config) {
36
+ return configProvider.create(async (app) => {
37
+ const db = await app.container.make("lucid.db");
38
+ const connectionName = config?.connectionName || db.primaryConnectionName;
39
+ const connection = db.manager.get(connectionName);
40
+ if (!connection) {
41
+ throw new RuntimeException(
42
+ `Invalid connection name "${connectionName}" referenced by "config/lock.ts" file. First register the connection inside "config/database.ts" file`
43
+ );
44
+ }
45
+ const dialect = db.connection(connectionName).dialect.name;
46
+ const supportedDialects = ["postgres", "mysql", "better-sqlite3", "sqlite3"];
47
+ if (!supportedDialects.includes(dialect)) {
48
+ throw new Error(`Unsupported dialect "${dialect}"`);
49
+ }
50
+ const rawConnection = db.getRawConnection(connectionName);
51
+ if (!rawConnection?.connection?.client) {
52
+ throw new Error(`Unable to get raw connection for "${connectionName}"`);
53
+ }
54
+ const { knexStore } = await import("@verrou/core/drivers/knex");
55
+ const store = knexStore({
56
+ connection: rawConnection.connection.client,
57
+ tableName: config?.tableName,
58
+ autoCreateTable: false
59
+ });
60
+ return { driver: store };
61
+ });
62
+ },
63
+ /**
64
+ * DynamoDB store
65
+ * You must install @aws-sdk/client-dynamodb to use this driver
66
+ */
67
+ dynamodb(config) {
68
+ return configProvider.create(async () => {
69
+ const { dynamodbStore } = await import("@verrou/core/drivers/dynamodb");
70
+ return { driver: dynamodbStore(config) };
71
+ });
72
+ }
73
+ };
74
+
75
+ // src/errors.ts
76
+ var errors_exports = {};
77
+ __export(errors_exports, {
78
+ E_LOCK_ALREADY_ACQUIRED: () => E_LOCK_ALREADY_ACQUIRED,
79
+ E_LOCK_NOT_OWNED: () => E_LOCK_NOT_OWNED,
80
+ E_LOCK_STORAGE_ERROR: () => E_LOCK_STORAGE_ERROR,
81
+ E_LOCK_TIMEOUT: () => E_LOCK_TIMEOUT
82
+ });
83
+ import {
84
+ E_LOCK_TIMEOUT,
85
+ E_LOCK_NOT_OWNED,
86
+ E_LOCK_STORAGE_ERROR,
87
+ E_LOCK_ALREADY_ACQUIRED
88
+ } from "@verrou/core";
89
+
90
+ // configure.ts
91
+ import string from "@adonisjs/core/helpers/string";
92
+
93
+ // stubs/main.ts
94
+ import { getDirname } from "@adonisjs/core/helpers";
95
+ var stubsRoot = getDirname(import.meta.url);
96
+
97
+ // configure.ts
98
+ var KNOWN_STORES = ["database", "redis", "dynamodb"];
99
+ async function configure(command) {
100
+ let selectedStore = command.parsedFlags.store;
101
+ if (!selectedStore) {
102
+ selectedStore = await command.prompt.choice(
103
+ "Select the storage layer you want to use",
104
+ KNOWN_STORES,
105
+ {
106
+ validate(value) {
107
+ return !value ? "Please select a store" : true;
108
+ }
109
+ }
110
+ );
111
+ }
112
+ if (!KNOWN_STORES.includes(selectedStore)) {
113
+ command.exitCode = 1;
114
+ command.logger.logError(
115
+ `Invalid lock store "${selectedStore}". Supported stores are: ${string.sentence(
116
+ KNOWN_STORES
117
+ )}`
118
+ );
119
+ return;
120
+ }
121
+ const codemods = await command.createCodemods();
122
+ await codemods.makeUsingStub(stubsRoot, "config/lock.stub", {
123
+ store: selectedStore
124
+ });
125
+ await codemods.updateRcFile((rcFile) => {
126
+ rcFile.addProvider("@adonisjs/lock/lock_provider");
127
+ });
128
+ if (selectedStore === "database") {
129
+ await codemods.makeUsingStub(stubsRoot, "make/migration/locks.stub", {
130
+ entity: command.app.generators.createEntity("locks"),
131
+ migration: {
132
+ folder: "database/migrations",
133
+ fileName: `${(/* @__PURE__ */ new Date()).getTime()}_create_locks_table.ts`
134
+ }
135
+ });
136
+ }
137
+ await codemods.defineEnvVariables({
138
+ LOCK_STORE: selectedStore
139
+ });
140
+ await codemods.defineEnvValidations({
141
+ leadingComment: "Variables for configuring the lock package",
142
+ variables: {
143
+ LOCK_STORE: `Env.schema.enum(['${selectedStore}', 'memory'] as const)`
144
+ }
145
+ });
146
+ }
147
+
148
+ // src/define_config.ts
149
+ import { InvalidArgumentsException } from "@adonisjs/core/exceptions";
150
+ function defineConfig(config) {
151
+ if (!config.stores) {
152
+ throw new InvalidArgumentsException('Missing "stores" property in lock config');
153
+ }
154
+ if (!config.default) {
155
+ throw new InvalidArgumentsException('Missing "default" store in lock config');
156
+ }
157
+ if (!config.stores[config.default]) {
158
+ throw new InvalidArgumentsException(
159
+ `Missing "stores.${config.default.toString()}" in lock config. It is referenced by the "default" property`
160
+ );
161
+ }
162
+ return config;
163
+ }
164
+ export {
165
+ configure,
166
+ defineConfig,
167
+ errors_exports as errors,
168
+ stores
169
+ };
@@ -0,0 +1,22 @@
1
+ {{{
2
+ exports({
3
+ to: app.makePath(migration.folder, entity.path, migration.fileName)
4
+ })
5
+ }}}
6
+ import { BaseSchema } from '@adonisjs/lucid/schema'
7
+
8
+ export default class extends BaseSchema {
9
+ protected tableName = 'locks'
10
+
11
+ async up() {
12
+ this.schema.createTable(this.tableName, (table) => {
13
+ table.string('key', 255).notNullable().primary()
14
+ table.string('owner').notNullable()
15
+ table.bigint('expiration').unsigned().nullable()
16
+ })
17
+ }
18
+
19
+ async down() {
20
+ this.schema.dropTable(this.tableName)
21
+ }
22
+ }
@@ -0,0 +1,23 @@
1
+ import { Verrou } from '@verrou/core';
2
+ import { ApplicationService } from '@adonisjs/core/types';
3
+ import { LockStoresList } from '../src/types.js';
4
+ import '@verrou/core/types';
5
+
6
+ /**
7
+ * Add lock manager type to the container bindings
8
+ */
9
+ declare module '@adonisjs/core/types' {
10
+ interface ContainerBindings {
11
+ 'lock.manager': Verrou<LockStoresList>;
12
+ }
13
+ }
14
+ declare class LockProvider {
15
+ protected app: ApplicationService;
16
+ constructor(app: ApplicationService);
17
+ /**
18
+ * Registers the lock manager in the application container
19
+ */
20
+ register(): void;
21
+ }
22
+
23
+ export { LockProvider as default };
@@ -0,0 +1,27 @@
1
+ import "../chunk-6C3VEZWH.js";
2
+
3
+ // providers/lock_provider.ts
4
+ var LockProvider = class {
5
+ constructor(app) {
6
+ this.app = app;
7
+ }
8
+ /**
9
+ * Registers the lock manager in the application container
10
+ */
11
+ register() {
12
+ this.app.container.singleton("lock.manager", async () => {
13
+ const { Verrou } = await import("@verrou/core");
14
+ const config = this.app.config.get("lock", {});
15
+ const stores = Object.entries(config.stores).map(async ([name, store]) => {
16
+ return [name, await store.resolver(this.app)];
17
+ });
18
+ return new Verrou({
19
+ default: config.default,
20
+ stores: Object.fromEntries(await Promise.all(stores))
21
+ });
22
+ });
23
+ }
24
+ };
25
+ export {
26
+ LockProvider as default
27
+ };
@@ -0,0 +1,8 @@
1
+ import { LockService } from '../src/types.js';
2
+ import '@verrou/core';
3
+ import '@verrou/core/types';
4
+ import '@adonisjs/core/types';
5
+
6
+ declare let lock: LockService;
7
+
8
+ export { lock as default };
@@ -0,0 +1,11 @@
1
+ import "../chunk-6C3VEZWH.js";
2
+
3
+ // services/main.ts
4
+ import app from "@adonisjs/core/services/app";
5
+ var lock;
6
+ await app.booted(async () => {
7
+ lock = await app.container.make("lock.manager");
8
+ });
9
+ export {
10
+ lock as default
11
+ };
@@ -0,0 +1,27 @@
1
+ import { Verrou } from '@verrou/core';
2
+ import { StoreFactory } from '@verrou/core/types';
3
+ export * from '@verrou/core/types';
4
+ import { ConfigProvider } from '@adonisjs/core/types';
5
+
6
+ /**
7
+ * A list of known lock stores inferred from the user config
8
+ */
9
+ interface LockStoresList extends Record<string, StoreFactory> {
10
+ }
11
+ /**
12
+ * Helper method to resolve configured lock stores
13
+ * inside user app
14
+ */
15
+ type InferLockStores<T extends {
16
+ stores: Record<string, ConfigProvider<StoreFactory>>;
17
+ }> = {
18
+ [K in keyof T['stores']]: Awaited<ReturnType<T['stores'][K]['resolver']>>;
19
+ };
20
+ /**
21
+ * Lock service is a singleton instance of Verrou
22
+ * configured using user app's config
23
+ */
24
+ interface LockService extends Verrou<LockStoresList> {
25
+ }
26
+
27
+ export type { InferLockStores, LockService, LockStoresList };
@@ -0,0 +1,2 @@
1
+ // src/types.ts
2
+ export * from "@verrou/core/types";
package/package.json ADDED
@@ -0,0 +1,131 @@
1
+ {
2
+ "name": "@adonisjs/lock",
3
+ "description": "Atomic locks (mutex) for AdonisJS applications",
4
+ "version": "1.0.0-0",
5
+ "engines": {
6
+ "node": ">=20.6.0"
7
+ },
8
+ "type": "module",
9
+ "files": [
10
+ "build"
11
+ ],
12
+ "exports": {
13
+ ".": "./build/index.js",
14
+ "./lock_provider": "./build/providers/lock_provider.js",
15
+ "./services/main": "./build/services/main.js",
16
+ "./types": "./build/src/types.js"
17
+ },
18
+ "scripts": {
19
+ "clean": "del-cli build",
20
+ "copy:templates": "copyfiles --up 1 \"stubs/**/*.stub\" build",
21
+ "typecheck": "tsc --noEmit",
22
+ "lint": "eslint . --ext=.ts",
23
+ "format": "prettier --write .",
24
+ "quick:test": "node --import=./tsnode.esm.js --enable-source-maps bin/test.ts",
25
+ "pretest": "npm run lint",
26
+ "test": "c8 npm run quick:test",
27
+ "prebuild": "npm run lint && npm run clean",
28
+ "build": "tsup-node && npm run copy:templates",
29
+ "release": "np",
30
+ "version": "npm run build",
31
+ "prepublishOnly": "npm run build"
32
+ },
33
+ "devDependencies": {
34
+ "@adonisjs/assembler": "^7.2.3",
35
+ "@adonisjs/core": "^6.3.1",
36
+ "@adonisjs/eslint-config": "^1.3.0",
37
+ "@adonisjs/lucid": "^20.3.1",
38
+ "@adonisjs/prettier-config": "^1.3.0",
39
+ "@adonisjs/redis": "^8.0.1",
40
+ "@adonisjs/tsconfig": "^1.3.0",
41
+ "@japa/assert": "^2.1.0",
42
+ "@japa/file-system": "^2.2.0",
43
+ "@japa/runner": "^3.1.1",
44
+ "@swc/core": "^1.4.6",
45
+ "@types/node": "^20.11.25",
46
+ "better-sqlite3": "^9.4.3",
47
+ "c8": "^9.1.0",
48
+ "copyfiles": "^2.4.1",
49
+ "del-cli": "^5.1.0",
50
+ "dotenv": "^16.4.5",
51
+ "eslint": "^8.57.0",
52
+ "mysql": "^2.18.1",
53
+ "np": "^10.0.0",
54
+ "pg": "^8.11.3",
55
+ "prettier": "^3.2.5",
56
+ "timekeeper": "^2.3.1",
57
+ "ts-node": "^10.9.2",
58
+ "tsup": "^8.0.2",
59
+ "typescript": "^5.4.2"
60
+ },
61
+ "dependencies": {
62
+ "@verrou/core": "^0.3.0"
63
+ },
64
+ "peerDependencies": {
65
+ "@adonisjs/assembler": "^7.0.0",
66
+ "@adonisjs/core": "^6.2.0",
67
+ "@adonisjs/lucid": "^20.0.0",
68
+ "@adonisjs/redis": "^8.0.0"
69
+ },
70
+ "peerDependenciesMeta": {
71
+ "@adonisjs/redis": {
72
+ "optional": true
73
+ },
74
+ "@adonisjs/lucid": {
75
+ "optional": true
76
+ }
77
+ },
78
+ "author": "Julien Ripouteau <julien@ripouteau.com>,adonisjs",
79
+ "license": "MIT",
80
+ "homepage": "https://github.com/adonisjs/lock#readme",
81
+ "repository": {
82
+ "type": "git",
83
+ "url": "git+https://github.com/adonisjs/lock.git"
84
+ },
85
+ "bugs": {
86
+ "url": "https://github.com/adonisjs/lock/issues"
87
+ },
88
+ "keywords": [
89
+ "locks",
90
+ "mutexes",
91
+ "atomic lock",
92
+ "adonisjs"
93
+ ],
94
+ "eslintConfig": {
95
+ "extends": "@adonisjs/eslint-config/package"
96
+ },
97
+ "prettier": "@adonisjs/prettier-config",
98
+ "publishConfig": {
99
+ "access": "public",
100
+ "tag": "latest"
101
+ },
102
+ "np": {
103
+ "message": "chore(release): %s",
104
+ "tag": "latest",
105
+ "branch": "main",
106
+ "anyBranch": false
107
+ },
108
+ "c8": {
109
+ "reporter": [
110
+ "text",
111
+ "html"
112
+ ],
113
+ "exclude": [
114
+ "tests/**",
115
+ "tests_helpers/**"
116
+ ]
117
+ },
118
+ "tsup": {
119
+ "entry": [
120
+ "./index.ts",
121
+ "./src/types.ts",
122
+ "./providers/lock_provider.ts",
123
+ "./services/main.ts"
124
+ ],
125
+ "outDir": "./build",
126
+ "clean": true,
127
+ "format": "esm",
128
+ "dts": true,
129
+ "target": "esnext"
130
+ }
131
+ }