-
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.
Merge pull request #414 from Inist-CNRS/add-breaker
feat: 🎸 add [breaker]
- Loading branch information
Showing
13 changed files
with
300 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { access, constants, writeFile, unlink } from 'fs'; | ||
import { resolve, normalize } from 'path'; | ||
import { tmpdir } from 'os'; | ||
import generate from 'nanoid/async/generate'; | ||
import nolookalikes from 'nanoid-dictionary/nolookalikes'; | ||
|
||
import { checksum } from './statements/identify'; | ||
|
||
const location = tmpdir(); | ||
const extension = '.sid'; | ||
|
||
export const createFusible = async () => { | ||
const fusible = await generate(nolookalikes, 16); | ||
return fusible; | ||
}; | ||
|
||
export const checkFusible = (fusible) => new Promise((next) => { | ||
if (!fusible) { | ||
return next(false); | ||
} | ||
const fusibleFile = resolve(normalize(location), fusible + extension); | ||
return access(fusibleFile, constants.R_OK, (err) => { | ||
if (err) { | ||
return next(false); | ||
} | ||
return next(true); | ||
}); | ||
}); | ||
|
||
|
||
export const enableFusible = (fusible) => new Promise((next, cancel) => { | ||
const fusibleFile = resolve(normalize(location), fusible + extension); | ||
checkFusible(fusible).then((check) => { | ||
if (!check) { | ||
const fileContent = checksum(fusible); | ||
writeFile(fusibleFile, fileContent, (err) => { | ||
if (err) { | ||
return cancel(err); | ||
} | ||
return next(true); | ||
}); | ||
} | ||
return next(true); | ||
}); | ||
}); | ||
|
||
export const disableFusible = (fusible) => new Promise((next, cancel) => { | ||
const fusibleFile = resolve(normalize(location), fusible + extension); | ||
checkFusible(fusible).then((check) => { | ||
if (check) { | ||
unlink(fusibleFile, (err) => { | ||
if (err) { | ||
return cancel(err); | ||
} | ||
return next(true); | ||
}); | ||
} | ||
}); | ||
return true; | ||
}); | ||
|
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,31 @@ | ||
import debug from 'debug'; | ||
import { disableFusible } from '../fusible'; | ||
|
||
const serverInformation = () => (request, response, next) => { | ||
if (!request.methodMatch(['DELETE']) || request.pathName !== '/') { | ||
return next(); | ||
} | ||
request.catched = true; | ||
debug('ezs')(`Create middleware 'serverControl' for ${request.method} ${request.pathName}`); | ||
const input = []; | ||
return request | ||
.on('error', err => next(err)) | ||
.on('data', chunk => { | ||
input.push(chunk); | ||
}) | ||
.on('end', async () => { | ||
try { | ||
const body = Buffer.concat(input).toString(); | ||
const bodyParsed = JSON.parse(body); | ||
await disableFusible(bodyParsed['x-request-id'] || bodyParsed['X-Request-ID']); | ||
response.writeHead(202); | ||
response.end(); | ||
next(); | ||
} | ||
catch (e) { | ||
next(e); | ||
} | ||
}); | ||
}; | ||
|
||
export default serverInformation; |
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,24 @@ | ||
import debug from 'debug'; | ||
import { checkFusible } from '../fusible'; | ||
/** | ||
* Break the stream if the control file cannot be checked | ||
* | ||
* | ||
* @name delegate | ||
* @param {String} [fusible] file to check | ||
* @returns {Object} | ||
*/ | ||
export default async function breaker(data, feed) { | ||
if (this.isFirst()) { | ||
this.fusible = this.getParam('fusible'); | ||
} | ||
if (this.isLast()) { | ||
return feed.close(); | ||
} | ||
const check = await checkFusible(this.fusible); | ||
if (!check) { | ||
debug('ezs')(`Stream break, ${this.fusible} no longer active.`); | ||
return feed.close(data); | ||
} | ||
return feed.send(data); | ||
} |
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,20 @@ | ||
import { | ||
createFusible, | ||
enableFusible, | ||
checkFusible, | ||
disableFusible | ||
} from '../src/fusible'; | ||
|
||
test('fusible', async () => { | ||
const fusible = await createFusible(); | ||
expect(fusible).toMatch(/.+/); | ||
const isEnable = await enableFusible(fusible); | ||
expect(isEnable).toBeTruthy(); | ||
const isCheckOK = await checkFusible(fusible); | ||
expect(isCheckOK).toBeTruthy(); | ||
const isDisable = await disableFusible(fusible); | ||
expect(isDisable).toBeTruthy(); | ||
const isCheckKO = await checkFusible(fusible); | ||
expect(isCheckKO).not.toBeTruthy(); | ||
}); | ||
|
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.