-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(includers/unarchive): unarchive input
introduce new includer that allows to unpack tar archive before including content
- Loading branch information
Showing
6 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * as generic from './generic'; | ||
export * as sourcedocs from './sourcedocs'; | ||
export * as openapi from './openapi'; | ||
export * as unarchive from './unarchive'; |
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,94 @@ | ||
import {mkdirSync, createReadStream, createWriteStream} from 'fs'; | ||
import {join, dirname} from 'path'; | ||
import {extract, Headers} from 'tar-stream'; | ||
|
||
import type {PassThrough} from 'stream'; | ||
|
||
import {IncluderFunctionParams} from '../../../models'; | ||
|
||
const name = 'unarchive'; | ||
|
||
class UnarchiveIncluderError extends Error { | ||
path: string; | ||
|
||
constructor(message: string, path: string) { | ||
super(message); | ||
|
||
this.name = 'UnarchiveIncluderError'; | ||
this.path = path; | ||
} | ||
} | ||
|
||
function pipeline(readPath: string, writeBasePath: string): Promise<void> { | ||
return new Promise((res, rej) => { | ||
const reader = createReadStream(readPath); | ||
|
||
reader.on('error', (err: Error) => { | ||
rej(err); | ||
}); | ||
|
||
const extractor = extract(); | ||
|
||
extractor.on('error', (err: Error) => { | ||
rej(err); | ||
}); | ||
|
||
mkdirSync(writeBasePath, {recursive: true}); | ||
|
||
extractor.on('entry', (header: Headers, stream: PassThrough, next: Function) => { | ||
const {type, name} = header; | ||
|
||
const writePath = join(writeBasePath, name); | ||
|
||
const writeDirPath = type === 'directory' ? writePath : dirname(writePath); | ||
|
||
mkdirSync(writeDirPath, {recursive: true}); | ||
|
||
if (type !== 'directory') { | ||
const writer = createWriteStream(writePath, {flags: 'w'}); | ||
|
||
writer.on('error', (err) => { | ||
rej(err); | ||
}); | ||
|
||
stream.pipe(writer); | ||
} | ||
|
||
stream.on('end', () => { | ||
next(); | ||
}); | ||
|
||
stream.resume(); | ||
}); | ||
|
||
reader.pipe(extractor).on('finish', () => { | ||
res(); | ||
}); | ||
}); | ||
} | ||
|
||
async function includerFunction(params: IncluderFunctionParams) { | ||
const {readBasePath, writeBasePath, tocPath, passedParams: {input, output}, index} = params; | ||
|
||
if (!input?.length || !output?.length) { | ||
throw new UnarchiveIncluderError('provide includer with input parameter', tocPath); | ||
} | ||
|
||
const contentPath = index === 0 | ||
? join(writeBasePath, input) | ||
: join(readBasePath, input); | ||
|
||
const writePath = join(writeBasePath, output); | ||
|
||
try { | ||
await pipeline(contentPath, writePath); | ||
} catch (err) { | ||
throw new UnarchiveIncluderError(err.toString(), tocPath); | ||
} | ||
|
||
return {input: output}; | ||
} | ||
|
||
export {name, includerFunction}; | ||
|
||
export default {name, includerFunction}; |
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