-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom adapter per engine possibility
- Loading branch information
Showing
24 changed files
with
1,032 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import fs, { promises as fsp } from 'node:fs'; | ||
import { basename, join } from 'node:path'; | ||
import { MemoryAdapter, STORAGE_TYPE_FILE, STORAGE_TYPE_DEPLOYMENT, HttpError } from '../../src/index.js'; | ||
import { LRUCache } from 'lru-cache'; | ||
|
||
export class CustomAdapter extends MemoryAdapter { | ||
/** | ||
* @param {string} rootFolder | ||
* @param {import('lru-cache').LRUCache} [storage] | ||
*/ | ||
constructor(rootFolder, storage) { | ||
super(storage ?? new LRUCache({ max: 1000, allowStale: false, fetchMethod: fetchMethod.bind(null, rootFolder) })); | ||
this.rootFolder = rootFolder; | ||
} | ||
} | ||
|
||
async function fetchMethod(rootFolder, fetchKey) { | ||
const [type, key, ...restKey] = fetchKey.split(':'); | ||
|
||
if (type === STORAGE_TYPE_DEPLOYMENT && key === 'fs' && restKey.length) { | ||
const fileName = `${basename(restKey.join(''))}.bpmn`; | ||
const filePath = join(rootFolder, fileName); | ||
|
||
await fsp.stat(filePath).catch((err) => { | ||
/* c8 ignore next */ | ||
if (err.code !== 'ENOENT') throw err; | ||
throw new HttpError(`deployment ${filePath} not found`, 404); | ||
}); | ||
|
||
return JSON.stringify([{ path: filePath }]); | ||
} | ||
if (type === STORAGE_TYPE_FILE) { | ||
return readFileContent(key).catch((err) => { | ||
/* c8 ignore next */ | ||
if (err.code !== 'ENOENT') throw err; | ||
throw new HttpError(`file ${key} not found`, 404); | ||
}); | ||
} | ||
} | ||
|
||
function readFileContent(file) { | ||
return new Promise((resolve, reject) => { | ||
let content = ''; | ||
let size = 0; | ||
fs.createReadStream(file) | ||
.on('data', (chunk) => { | ||
size += chunk.byteLength; | ||
content += chunk; | ||
}) | ||
.on('end', () => { | ||
return resolve(JSON.stringify({ mimetype: 'text/xml', size, content })); | ||
}) | ||
.on('error', reject); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.