@aeriajs/core 0.0.214 → 0.0.215
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/functions/upload.js +24 -2
- package/dist/functions/upload.mjs +24 -2
- package/package.json +3 -3
package/dist/functions/upload.js
CHANGED
|
@@ -41,10 +41,17 @@ const fs_1 = require("fs");
|
|
|
41
41
|
const crypto_1 = require("crypto");
|
|
42
42
|
const [FileMetadata, validateFileMetadata] = (0, validation_1.validator)({
|
|
43
43
|
type: 'object',
|
|
44
|
+
required: ['name'],
|
|
44
45
|
properties: {
|
|
45
46
|
name: {
|
|
46
47
|
type: 'string',
|
|
47
48
|
},
|
|
49
|
+
format: {
|
|
50
|
+
enum: [
|
|
51
|
+
'raw',
|
|
52
|
+
'base64',
|
|
53
|
+
],
|
|
54
|
+
},
|
|
48
55
|
},
|
|
49
56
|
});
|
|
50
57
|
const streamToFs = (metadata, context) => {
|
|
@@ -61,17 +68,32 @@ const streamToFs = (metadata, context) => {
|
|
|
61
68
|
throw new Error();
|
|
62
69
|
}
|
|
63
70
|
const absolutePath = path.join(tmpPath, `${nameHash}.${extension}`);
|
|
64
|
-
return new Promise((resolve, reject) => {
|
|
71
|
+
return new Promise(async (resolve, reject) => {
|
|
65
72
|
const stream = (0, fs_1.createWriteStream)(absolutePath);
|
|
66
|
-
stream.on('open', () => context.request.pipe(stream));
|
|
67
73
|
stream.on('close', () => resolve(absolutePath));
|
|
68
74
|
stream.on('error', (error) => reject(error));
|
|
75
|
+
switch (metadata.format) {
|
|
76
|
+
case undefined:
|
|
77
|
+
case 'raw': {
|
|
78
|
+
stream.on('open', () => context.request.pipe(stream));
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case 'base64': {
|
|
82
|
+
stream.write(Buffer.from(Buffer.concat(await Array.fromAsync(context.request)).toString(), 'base64'));
|
|
83
|
+
stream.close();
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
69
87
|
});
|
|
70
88
|
};
|
|
71
89
|
const upload = async (_props, context) => {
|
|
72
90
|
const { error: headersError } = (0, validation_1.validate)(context.request.headers, {
|
|
73
91
|
type: 'object',
|
|
74
92
|
additionalProperties: true,
|
|
93
|
+
required: [
|
|
94
|
+
'x-stream-request',
|
|
95
|
+
'content-type',
|
|
96
|
+
],
|
|
75
97
|
properties: {
|
|
76
98
|
'x-stream-request': {
|
|
77
99
|
const: '1',
|
|
@@ -6,9 +6,16 @@ import { createWriteStream } from "fs";
|
|
|
6
6
|
import { createHash } from "crypto";
|
|
7
7
|
const [FileMetadata, validateFileMetadata] = validator({
|
|
8
8
|
type: "object",
|
|
9
|
+
required: ["name"],
|
|
9
10
|
properties: {
|
|
10
11
|
name: {
|
|
11
12
|
type: "string"
|
|
13
|
+
},
|
|
14
|
+
format: {
|
|
15
|
+
enum: [
|
|
16
|
+
"raw",
|
|
17
|
+
"base64"
|
|
18
|
+
]
|
|
12
19
|
}
|
|
13
20
|
}
|
|
14
21
|
});
|
|
@@ -20,17 +27,32 @@ const streamToFs = (metadata, context) => {
|
|
|
20
27
|
throw new Error();
|
|
21
28
|
}
|
|
22
29
|
const absolutePath = path.join(tmpPath, `${nameHash}.${extension}`);
|
|
23
|
-
return new Promise((resolve, reject) => {
|
|
30
|
+
return new Promise(async (resolve, reject) => {
|
|
24
31
|
const stream = createWriteStream(absolutePath);
|
|
25
|
-
stream.on("open", () => context.request.pipe(stream));
|
|
26
32
|
stream.on("close", () => resolve(absolutePath));
|
|
27
33
|
stream.on("error", (error) => reject(error));
|
|
34
|
+
switch (metadata.format) {
|
|
35
|
+
case void 0:
|
|
36
|
+
case "raw": {
|
|
37
|
+
stream.on("open", () => context.request.pipe(stream));
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case "base64": {
|
|
41
|
+
stream.write(Buffer.from(Buffer.concat(await Array.fromAsync(context.request)).toString(), "base64"));
|
|
42
|
+
stream.close();
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
28
46
|
});
|
|
29
47
|
};
|
|
30
48
|
export const upload = async (_props, context) => {
|
|
31
49
|
const { error: headersError } = validate(context.request.headers, {
|
|
32
50
|
type: "object",
|
|
33
51
|
additionalProperties: true,
|
|
52
|
+
required: [
|
|
53
|
+
"x-stream-request",
|
|
54
|
+
"content-type"
|
|
55
|
+
],
|
|
34
56
|
properties: {
|
|
35
57
|
"x-stream-request": {
|
|
36
58
|
const: "1"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.215",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"aeriaMain": "tests/fixtures/aeriaMain.js",
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"mongodb-memory-server": "^9.2.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@aeriajs/builtins": "^0.0.
|
|
45
|
+
"@aeriajs/builtins": "^0.0.215",
|
|
46
46
|
"@aeriajs/common": "^0.0.125",
|
|
47
47
|
"@aeriajs/entrypoint": "^0.0.128",
|
|
48
48
|
"@aeriajs/http": "^0.0.150",
|
|
49
|
-
"@aeriajs/security": "^0.0.
|
|
49
|
+
"@aeriajs/security": "^0.0.215",
|
|
50
50
|
"@aeriajs/types": "^0.0.107",
|
|
51
51
|
"@aeriajs/validation": "^0.0.139"
|
|
52
52
|
},
|