@openstax/ts-utils 1.21.1 → 1.21.3
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/dist/cjs/services/fileServer/index.d.ts +2 -0
- package/dist/cjs/services/fileServer/localFileServer.d.ts +4 -0
- package/dist/cjs/services/fileServer/localFileServer.js +54 -0
- package/dist/cjs/services/fileServer/s3FileServer.js +23 -0
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/fileServer/index.d.ts +2 -0
- package/dist/esm/services/fileServer/localFileServer.d.ts +4 -0
- package/dist/esm/services/fileServer/localFileServer.js +54 -0
- package/dist/esm/services/fileServer/s3FileServer.js +24 -1
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +17 -2
|
@@ -12,6 +12,8 @@ export declare type FolderValue = {
|
|
|
12
12
|
export declare const isFileValue: (thing: any) => thing is FileValue;
|
|
13
13
|
export declare const isFolderValue: (thing: any) => thing is FolderValue;
|
|
14
14
|
export interface FileServerAdapter {
|
|
15
|
+
putFileContent: (source: FileValue, content: string) => Promise<void>;
|
|
16
|
+
getSignedViewerUrl: (source: FileValue) => Promise<string>;
|
|
15
17
|
getFileContent: (source: FileValue) => Promise<Buffer>;
|
|
16
18
|
}
|
|
17
19
|
export declare const isFileOrFolder: (thing: any) => thing is FileValue | FolderValue;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { ConfigProviderForConfig } from '../../config';
|
|
2
2
|
import { FileServerAdapter } from '.';
|
|
3
3
|
export declare type Config = {
|
|
4
|
+
port: string;
|
|
5
|
+
host: string;
|
|
4
6
|
storagePrefix: string;
|
|
5
7
|
};
|
|
6
8
|
interface Initializer<C> {
|
|
@@ -8,6 +10,8 @@ interface Initializer<C> {
|
|
|
8
10
|
configSpace?: C;
|
|
9
11
|
}
|
|
10
12
|
export declare const localFileServer: <C extends string = "local">(initializer: Initializer<C>) => (configProvider: { [key in C]: {
|
|
13
|
+
port: import("../../config").ConfigValueProvider<string>;
|
|
14
|
+
host: import("../../config").ConfigValueProvider<string>;
|
|
11
15
|
storagePrefix: import("../../config").ConfigValueProvider<string>;
|
|
12
16
|
}; }) => FileServerAdapter;
|
|
13
17
|
export {};
|
|
@@ -2,15 +2,69 @@ import fs from 'fs';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { resolveConfigValue } from '../../config';
|
|
4
4
|
import { ifDefined } from '../../guards';
|
|
5
|
+
import cors from 'cors';
|
|
6
|
+
import express from 'express';
|
|
7
|
+
import multer from 'multer';
|
|
8
|
+
import https from 'https';
|
|
9
|
+
import { once } from "../../misc/helpers";
|
|
10
|
+
import { assertString } from "../../assertions";
|
|
11
|
+
/* istanbul ignore next */
|
|
12
|
+
const startServer = once((port, uploadDir) => {
|
|
13
|
+
// TODO - re-evaluate the `preservePath` behavior to match whatever s3 does
|
|
14
|
+
const upload = multer({ dest: uploadDir, preservePath: true });
|
|
15
|
+
const fileServerApp = express();
|
|
16
|
+
fileServerApp.use(cors());
|
|
17
|
+
fileServerApp.use(express.static(uploadDir));
|
|
18
|
+
fileServerApp.post('/', upload.single('file'), async (req, res) => {
|
|
19
|
+
const file = req.file;
|
|
20
|
+
if (!file) {
|
|
21
|
+
return res.status(400).send({ message: 'file is required' });
|
|
22
|
+
}
|
|
23
|
+
const destinationName = req.body.key.replace('${filename}', file.originalname);
|
|
24
|
+
const destinationPath = path.join(uploadDir, destinationName);
|
|
25
|
+
const destinationDirectory = path.dirname(destinationPath);
|
|
26
|
+
await fs.promises.mkdir(destinationDirectory, { recursive: true });
|
|
27
|
+
await fs.promises.rename(file.path, destinationPath);
|
|
28
|
+
res.status(201).send();
|
|
29
|
+
});
|
|
30
|
+
const server = https.createServer({
|
|
31
|
+
key: fs.readFileSync(assertString(process.env.SSL_KEY_FILE, new Error('ssl key is required for localFileServer')), 'utf8'),
|
|
32
|
+
cert: fs.readFileSync(assertString(process.env.SSL_CRT_FILE, new Error('ssl key is required for localFileServer')), 'utf8'),
|
|
33
|
+
}, fileServerApp);
|
|
34
|
+
server.once('error', function (err) {
|
|
35
|
+
if (err.code === 'EADDRINUSE') {
|
|
36
|
+
// when the local dev server reloads files on every request it doesn't
|
|
37
|
+
// actually tear down the old modules, so this server only starts on the
|
|
38
|
+
// first execution and changes in its code will not be reloaded
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
throw err;
|
|
42
|
+
});
|
|
43
|
+
server.listen(port);
|
|
44
|
+
return true;
|
|
45
|
+
});
|
|
5
46
|
export const localFileServer = (initializer) => (configProvider) => {
|
|
6
47
|
const config = configProvider[ifDefined(initializer.configSpace, 'local')];
|
|
48
|
+
const port = resolveConfigValue(config.port);
|
|
49
|
+
const host = resolveConfigValue(config.host);
|
|
7
50
|
const storagePrefix = resolveConfigValue(config.storagePrefix);
|
|
8
51
|
const fileDir = storagePrefix.then((prefix) => path.join(initializer.dataDir, prefix));
|
|
52
|
+
Promise.all([port, fileDir])
|
|
53
|
+
.then(([port, fileDir]) => startServer(port, fileDir));
|
|
54
|
+
const getSignedViewerUrl = async (source) => {
|
|
55
|
+
return `https://${await host}:${await port}/${source.path}`;
|
|
56
|
+
};
|
|
9
57
|
const getFileContent = async (source) => {
|
|
10
58
|
const filePath = path.join(await fileDir, source.path);
|
|
11
59
|
return fs.promises.readFile(filePath);
|
|
12
60
|
};
|
|
61
|
+
const putFileContent = async (source, content) => {
|
|
62
|
+
const filePath = path.join(await fileDir, source.path);
|
|
63
|
+
return fs.promises.writeFile(filePath, content);
|
|
64
|
+
};
|
|
13
65
|
return {
|
|
66
|
+
getSignedViewerUrl,
|
|
14
67
|
getFileContent,
|
|
68
|
+
putFileContent,
|
|
15
69
|
};
|
|
16
70
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
|
1
|
+
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
|
2
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
2
3
|
import { once } from '../..';
|
|
3
4
|
import { assertDefined } from '../../assertions';
|
|
4
5
|
import { resolveConfigValue } from '../../config';
|
|
@@ -9,13 +10,35 @@ export const s3FileServer = (initializer) => (configProvider) => {
|
|
|
9
10
|
const bucketRegion = once(() => resolveConfigValue(config.bucketRegion));
|
|
10
11
|
const client = ifDefined(initializer.s3Client, S3Client);
|
|
11
12
|
const s3Service = once(async () => new client({ apiVersion: '2012-08-10', region: await bucketRegion() }));
|
|
13
|
+
/*
|
|
14
|
+
* https://docs.aws.amazon.com/AmazonS3/latest/userguide/ShareObjectPreSignedURL.html
|
|
15
|
+
*/
|
|
16
|
+
const getSignedViewerUrl = async (source) => {
|
|
17
|
+
const bucket = (await bucketName());
|
|
18
|
+
const command = new GetObjectCommand({ Bucket: bucket, Key: source.path });
|
|
19
|
+
return getSignedUrl(await s3Service(), command, {
|
|
20
|
+
expiresIn: 3600, // 1 hour
|
|
21
|
+
});
|
|
22
|
+
};
|
|
12
23
|
const getFileContent = async (source) => {
|
|
13
24
|
const bucket = await bucketName();
|
|
14
25
|
const command = new GetObjectCommand({ Bucket: bucket, Key: source.path });
|
|
15
26
|
const response = await (await s3Service()).send(command);
|
|
16
27
|
return Buffer.from(await assertDefined(response.Body, new Error('Invalid Response from s3')).transformToByteArray());
|
|
17
28
|
};
|
|
29
|
+
const putFileContent = async (source, content) => {
|
|
30
|
+
const bucket = await bucketName();
|
|
31
|
+
const command = new PutObjectCommand({
|
|
32
|
+
Bucket: bucket,
|
|
33
|
+
Key: source.path,
|
|
34
|
+
Body: content,
|
|
35
|
+
ContentType: source.mimeType,
|
|
36
|
+
});
|
|
37
|
+
await (await s3Service()).send(command);
|
|
38
|
+
};
|
|
18
39
|
return {
|
|
19
40
|
getFileContent,
|
|
41
|
+
putFileContent,
|
|
42
|
+
getSignedViewerUrl,
|
|
20
43
|
};
|
|
21
44
|
};
|