@things-factory/lock-client 8.0.0-beta.0 → 8.0.0-beta.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/lock-client",
3
- "version": "8.0.0-beta.0",
3
+ "version": "8.0.0-beta.2",
4
4
  "main": "dist-server/index.js",
5
5
  "things-factory": true,
6
6
  "license": "MIT",
@@ -22,9 +22,9 @@
22
22
  "clean": "npm run clean:server"
23
23
  },
24
24
  "dependencies": {
25
- "@things-factory/auth-base": "^8.0.0-beta.0",
26
- "@things-factory/env": "^8.0.0-beta.0",
27
- "@things-factory/shell": "^8.0.0-beta.0"
25
+ "@things-factory/auth-base": "^8.0.0-beta.2",
26
+ "@things-factory/env": "^8.0.0-beta.2",
27
+ "@things-factory/shell": "^8.0.0-beta.2"
28
28
  },
29
- "gitHead": "add6fb8224b2cb19cbea47bed6a5ecb0424c9a28"
29
+ "gitHead": "f03431a09435511b2595515658f9cb8f78ba4ebb"
30
30
  }
@@ -1,20 +0,0 @@
1
- {
2
- "keep": {
3
- "days": true,
4
- "amount": 2
5
- },
6
- "auditLog": "logs/.08636eb59927f12972f6774f5947c8507b3564c2-audit.json",
7
- "files": [
8
- {
9
- "date": 1709125953037,
10
- "name": "logs/application-2024-02-28-22.log",
11
- "hash": "6073bd0aad5c94c65e8423169d232549019b393b7d8d59c095cf3efb97777fa8"
12
- },
13
- {
14
- "date": 1709129008763,
15
- "name": "logs/application-2024-02-28-23.log",
16
- "hash": "adc0056c45944c69923971071201490cfee929ea494af0746ebba5d1246408aa"
17
- }
18
- ],
19
- "hashType": "sha256"
20
- }
File without changes
File without changes
@@ -1,4 +0,0 @@
1
- import { config } from '@things-factory/env'
2
-
3
- export const LockServiceEndpoint: string = config.get('lock/endpoint', 'http://127.0.0.1:9910')
4
- export const Application: string = config.get('lock/application')
@@ -1,4 +0,0 @@
1
- export * from './request-lock'
2
- export * from './try-lock'
3
- export * from './release-lock'
4
- export * from './locks'
@@ -1,14 +0,0 @@
1
- import fetch from 'node-fetch'
2
- import { LockServiceEndpoint } from '../config'
3
-
4
- export async function getLocks() {
5
- const response = await fetch(LockServiceEndpoint + '/locks', {
6
- method: 'get',
7
- headers: {
8
- accept: 'application/json',
9
- 'Content-Type': 'application/json'
10
- }
11
- })
12
-
13
- return await response.json()
14
- }
@@ -1,26 +0,0 @@
1
- import fetch from 'node-fetch'
2
- import { LockServiceEndpoint } from '../config'
3
- import { LockRequest } from '../types'
4
-
5
- export async function releaseLock(request: LockRequest) {
6
- var body = JSON.stringify(request)
7
- const response = await fetch(LockServiceEndpoint + '/locking/release', {
8
- method: 'post',
9
- headers: {
10
- accept: 'application/json',
11
- 'Content-Type': 'application/json'
12
- },
13
- body
14
- })
15
-
16
- var result
17
- if (response.ok) {
18
- result = await response.json()
19
- } else if (response.status === 404) {
20
- result = { error: 'Item not found' }
21
- } else {
22
- result = { error: await response.text() }
23
- }
24
-
25
- return result
26
- }
@@ -1,17 +0,0 @@
1
- import fetch from 'node-fetch'
2
-
3
- import { LockServiceEndpoint } from '../config'
4
-
5
- export async function requestLock() {
6
- const response = await fetch(LockServiceEndpoint + '/locking', {
7
- method: 'POST',
8
- headers: {
9
- accept: 'application/json',
10
- 'Content-Type': 'application/json'
11
- },
12
- body: ''
13
- })
14
-
15
- const { id } = await response.json()
16
- return id
17
- }
@@ -1,37 +0,0 @@
1
- import fetch, { AbortError } from 'node-fetch'
2
-
3
- import { LockServiceEndpoint } from '../config'
4
- import { LockRequest } from '../types'
5
- import { releaseLock } from './release-lock'
6
-
7
- export async function tryLock(request: LockRequest, maxTimeout: number = 3000) {
8
- var body = JSON.stringify(request)
9
-
10
- const controller = new AbortController()
11
- const timeout = setTimeout(() => {
12
- controller.abort()
13
- }, maxTimeout)
14
-
15
- try {
16
- const response = await fetch(LockServiceEndpoint + '/locking/try', {
17
- method: 'POST',
18
- headers: {
19
- accept: 'application/json',
20
- 'Content-Type': 'application/json'
21
- },
22
- body,
23
- signal: controller.signal
24
- })
25
-
26
- return await response.json()
27
- } catch (e) {
28
- if (e instanceof AbortError) {
29
- await releaseLock(request)
30
- return {
31
- payload: { error: 'timeout' }
32
- }
33
- }
34
- } finally {
35
- clearTimeout(timeout)
36
- }
37
- }
@@ -1 +0,0 @@
1
- export * from './locks'
@@ -1,24 +0,0 @@
1
- export class Lock {
2
- private resolver: (value: unknown) => any
3
- private timeout: any
4
-
5
- acquire(timeout: number) {
6
- return new Promise((resolve, reject) => {
7
- if (!this.resolver) {
8
- this.resolver = resolve
9
- this.timeout = setTimeout(() => {
10
- reject('timeout')
11
- }, timeout)
12
- }
13
- })
14
- }
15
-
16
- release(result) {
17
- if (this.resolver) {
18
- clearTimeout(this.timeout)
19
- const resolve = this.resolver
20
- this.resolver = null
21
- resolve(result)
22
- }
23
- }
24
- }
@@ -1,53 +0,0 @@
1
- import { Lock } from './lock'
2
- import { LockServiceEndpoint } from '../config'
3
- import { LockRequest } from '../types'
4
-
5
- const locks: {
6
- [id: string]: {
7
- lock: Lock
8
- payload: any
9
- }
10
- } = {}
11
-
12
- var id = 1
13
-
14
- export async function getLocks() {
15
- return this.locks
16
- }
17
-
18
- export async function requestLock() {
19
- return String(id++)
20
- }
21
-
22
- export async function tryLock(request: LockRequest, maxTimeout: number = 30000) {
23
- const { id, payload } = request
24
-
25
- if (locks[id]) {
26
- throw `Lock [id:${id}] is already taken.`
27
- }
28
- const lock = new Lock()
29
-
30
- locks[id] = {
31
- lock,
32
- payload
33
- }
34
-
35
- try {
36
- return await lock.acquire(maxTimeout)
37
- } catch (e) {
38
- throw e
39
- }
40
- }
41
-
42
- export function releaseLock(request: LockRequest) {
43
- const { id, payload } = request
44
- const lock = locks[id]
45
-
46
- if (!lock) {
47
- throw `Lock [id:${id}] is not found.`
48
- }
49
-
50
- delete locks[id]
51
-
52
- lock.lock.release(payload)
53
- }
@@ -1,11 +0,0 @@
1
- export type LockRequest = {
2
- id?: string
3
- payload?: any
4
- }
5
-
6
- export interface LockClient {
7
- getLocks: (name: string, group: string, dlq: boolean) => Promise<any>
8
- requestLock: () => Promise<any>
9
- tryLock: (request: LockRequest, maxTimeout?: number) => Promise<any>
10
- releaseLock: (request: LockRequest) => void
11
- }
package/server/index.ts DELETED
@@ -1,11 +0,0 @@
1
- import { LockServiceEndpoint } from './controllers/config'
2
-
3
- import { getLocks as remoteGetLocks, requestLock as remoteRequestLock, tryLock as remoteTryLock, releaseLock as remoteReleaseLock } from './controllers/lock-api'
4
- import { getLocks as localGetLocks, requestLock as localRequestLock, tryLock as localTryLock, releaseLock as localReleaseLock } from './controllers/lock-service-local'
5
-
6
- const useLocalLock = LockServiceEndpoint == 'internal'
7
-
8
- export const getLocks = useLocalLock ? localGetLocks : remoteGetLocks
9
- export const requestLock = useLocalLock ? localRequestLock : remoteRequestLock
10
- export const tryLock = useLocalLock ? localTryLock : remoteTryLock
11
- export const releaseLock = useLocalLock ? localReleaseLock : remoteReleaseLock
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../../tsconfig-base.json",
3
- "compilerOptions": {
4
- "strict": false,
5
- "module": "commonjs",
6
- "outDir": "../dist-server",
7
- "baseUrl": "./"
8
- },
9
- "include": ["./**/*"]
10
- }