@aeriajs/builtins 0.0.179 → 0.0.181
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.
|
@@ -2,6 +2,9 @@ import type { Context } from '@aeriajs/types';
|
|
|
2
2
|
import type { description } from './description.js';
|
|
3
3
|
import { HTTPStatus, ACError } from '@aeriajs/types';
|
|
4
4
|
import * as fs from 'fs';
|
|
5
|
+
export declare enum DownloadError {
|
|
6
|
+
RangeNotSatisfiable = "RANGE_NOT_SATISFIABLE"
|
|
7
|
+
}
|
|
5
8
|
export declare const download: (payload: {
|
|
6
9
|
fileId: string;
|
|
7
10
|
options: readonly ("picture" | "download")[];
|
|
@@ -10,4 +13,8 @@ export declare const download: (payload: {
|
|
|
10
13
|
readonly code: ACError.ResourceNotFound;
|
|
11
14
|
} & {
|
|
12
15
|
httpStatus: HTTPStatus.NotFound;
|
|
16
|
+
}> | import("@aeriajs/types").Result.Error<{
|
|
17
|
+
readonly code: DownloadError;
|
|
18
|
+
} & {
|
|
19
|
+
httpStatus: HTTPStatus.RangeNotSatisfiable;
|
|
13
20
|
}> | fs.ReadStream>;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.download = void 0;
|
|
3
|
+
exports.download = exports.DownloadError = void 0;
|
|
4
4
|
const types_1 = require("@aeriajs/types");
|
|
5
5
|
const core_1 = require("@aeriajs/core");
|
|
6
6
|
const fs = require("fs");
|
|
7
|
+
var DownloadError;
|
|
8
|
+
(function (DownloadError) {
|
|
9
|
+
DownloadError["RangeNotSatisfiable"] = "RANGE_NOT_SATISFIABLE";
|
|
10
|
+
})(DownloadError || (exports.DownloadError = DownloadError = {}));
|
|
7
11
|
const download = async (payload, context) => {
|
|
8
12
|
const { fileId, options = [] } = payload;
|
|
9
13
|
const file = await context.collection.model.findOne({
|
|
@@ -17,7 +21,7 @@ const download = async (payload, context) => {
|
|
|
17
21
|
});
|
|
18
22
|
if (!file) {
|
|
19
23
|
if (!payload.noHeaders) {
|
|
20
|
-
context.response.writeHead(
|
|
24
|
+
context.response.writeHead(types_1.HTTPStatus.NotFound, {
|
|
21
25
|
'content-type': 'application/json',
|
|
22
26
|
});
|
|
23
27
|
}
|
|
@@ -44,6 +48,15 @@ const download = async (payload, context) => {
|
|
|
44
48
|
const end = parts[1]
|
|
45
49
|
? parseInt(parts[1])
|
|
46
50
|
: stat.size - 1;
|
|
51
|
+
if (start >= stat.size || end >= stat.size) {
|
|
52
|
+
context.response.writeHead(types_1.HTTPStatus.RangeNotSatisfiable, {
|
|
53
|
+
'content-type': 'application/json',
|
|
54
|
+
'content-range': `bytes */${stat.size}`,
|
|
55
|
+
});
|
|
56
|
+
return context.error(types_1.HTTPStatus.RangeNotSatisfiable, {
|
|
57
|
+
code: DownloadError.RangeNotSatisfiable,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
47
60
|
const chunkSize = (end - start) + 1;
|
|
48
61
|
if (!payload.noHeaders) {
|
|
49
62
|
context.response.writeHead(206, {
|
|
@@ -62,7 +75,7 @@ const download = async (payload, context) => {
|
|
|
62
75
|
});
|
|
63
76
|
}
|
|
64
77
|
if (!payload.noHeaders) {
|
|
65
|
-
context.response.writeHead(
|
|
78
|
+
context.response.writeHead(types_1.HTTPStatus.Ok, {
|
|
66
79
|
'content-type': file.type,
|
|
67
80
|
'content-disposition': `${options.includes('download')
|
|
68
81
|
? 'attachment; '
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
import { HTTPStatus, ACError } from "@aeriajs/types";
|
|
3
3
|
import { ObjectId } from "@aeriajs/core";
|
|
4
4
|
import * as fs from "fs";
|
|
5
|
+
export var DownloadError = /* @__PURE__ */ ((DownloadError2) => {
|
|
6
|
+
DownloadError2["RangeNotSatisfiable"] = "RANGE_NOT_SATISFIABLE";
|
|
7
|
+
return DownloadError2;
|
|
8
|
+
})(DownloadError || {});
|
|
5
9
|
export const download = async (payload, context) => {
|
|
6
10
|
const { fileId, options = [] } = payload;
|
|
7
11
|
const file = await context.collection.model.findOne({
|
|
@@ -15,7 +19,7 @@ export const download = async (payload, context) => {
|
|
|
15
19
|
});
|
|
16
20
|
if (!file) {
|
|
17
21
|
if (!payload.noHeaders) {
|
|
18
|
-
context.response.writeHead(
|
|
22
|
+
context.response.writeHead(HTTPStatus.NotFound, {
|
|
19
23
|
"content-type": "application/json"
|
|
20
24
|
});
|
|
21
25
|
}
|
|
@@ -39,6 +43,15 @@ export const download = async (payload, context) => {
|
|
|
39
43
|
const parts = range.replace(/bytes=/, "").split("-");
|
|
40
44
|
const start = parseInt(parts[0]);
|
|
41
45
|
const end = parts[1] ? parseInt(parts[1]) : stat.size - 1;
|
|
46
|
+
if (start >= stat.size || end >= stat.size) {
|
|
47
|
+
context.response.writeHead(HTTPStatus.RangeNotSatisfiable, {
|
|
48
|
+
"content-type": "application/json",
|
|
49
|
+
"content-range": `bytes */${stat.size}`
|
|
50
|
+
});
|
|
51
|
+
return context.error(HTTPStatus.RangeNotSatisfiable, {
|
|
52
|
+
code: "RANGE_NOT_SATISFIABLE" /* RangeNotSatisfiable */
|
|
53
|
+
});
|
|
54
|
+
}
|
|
42
55
|
const chunkSize = end - start + 1;
|
|
43
56
|
if (!payload.noHeaders) {
|
|
44
57
|
context.response.writeHead(206, {
|
|
@@ -55,7 +68,7 @@ export const download = async (payload, context) => {
|
|
|
55
68
|
});
|
|
56
69
|
}
|
|
57
70
|
if (!payload.noHeaders) {
|
|
58
|
-
context.response.writeHead(
|
|
71
|
+
context.response.writeHead(HTTPStatus.Ok, {
|
|
59
72
|
"content-type": file.type,
|
|
60
73
|
"content-disposition": `${options.includes("download") ? "attachment; " : ""}name=${encodeURI(file.name)}`
|
|
61
74
|
});
|
|
@@ -233,6 +233,10 @@ export declare const file: Omit<import("@aeriajs/types").Collection<never>, "fun
|
|
|
233
233
|
readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
|
|
234
234
|
} & {
|
|
235
235
|
httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
|
|
236
|
+
}> | import("@aeriajs/types").Result.Error<{
|
|
237
|
+
readonly code: import("./download.js").DownloadError;
|
|
238
|
+
} & {
|
|
239
|
+
httpStatus: import("@aeriajs/types").HTTPStatus.RangeNotSatisfiable;
|
|
236
240
|
}> | import("fs").ReadStream>;
|
|
237
241
|
remove: (payload: import("@aeriajs/types").RemovePayload<import("@aeriajs/types").SchemaWithId<typeof description>>, context: import("@aeriajs/types").Context<typeof description>) => Promise<{
|
|
238
242
|
readonly _tag: "Result";
|
package/dist/index.d.ts
CHANGED
|
@@ -177,6 +177,10 @@ export declare const collections: {
|
|
|
177
177
|
readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
|
|
178
178
|
} & {
|
|
179
179
|
httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
|
|
180
|
+
}> | import("@aeriajs/types").Result.Error<{
|
|
181
|
+
readonly code: import("./collections/file/download.js").DownloadError;
|
|
182
|
+
} & {
|
|
183
|
+
httpStatus: import("@aeriajs/types").HTTPStatus.RangeNotSatisfiable;
|
|
180
184
|
}> | import("fs").ReadStream>;
|
|
181
185
|
remove: (payload: import("@aeriajs/types").RemovePayload<import("@aeriajs/types").SchemaWithId<typeof import("./collections/file/description.js").description>>, context: import("@aeriajs/types").Context<typeof import("./collections/file/description.js").description>) => Promise<{
|
|
182
186
|
readonly _tag: "Result";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/builtins",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.181",
|
|
4
4
|
"description": "## Installation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -45,11 +45,11 @@
|
|
|
45
45
|
"mongodb": "^6.5.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@aeriajs/core": "^0.0.
|
|
49
|
-
"@aeriajs/common": "^0.0.
|
|
50
|
-
"@aeriajs/entrypoint": "^0.0.
|
|
51
|
-
"@aeriajs/types": "^0.0.
|
|
52
|
-
"@aeriajs/validation": "^0.0.
|
|
48
|
+
"@aeriajs/core": "^0.0.181",
|
|
49
|
+
"@aeriajs/common": "^0.0.111",
|
|
50
|
+
"@aeriajs/entrypoint": "^0.0.114",
|
|
51
|
+
"@aeriajs/types": "^0.0.94",
|
|
52
|
+
"@aeriajs/validation": "^0.0.114"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"test": "echo skipping",
|