@aeriajs/builtins 0.0.46 → 0.0.48

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.
@@ -1,258 +1,66 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
- try {
3
- var info = gen[key](arg);
4
- var value = info.value;
5
- } catch (error) {
6
- reject(error);
7
- return;
8
- }
9
- if (info.done) {
10
- resolve(value);
11
- } else {
12
- Promise.resolve(value).then(_next, _throw);
13
- }
14
- }
15
- function _async_to_generator(fn) {
16
- return function() {
17
- var self = this, args = arguments;
18
- return new Promise(function(resolve, reject) {
19
- var gen = fn.apply(self, args);
20
- function _next(value) {
21
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
- }
23
- function _throw(err) {
24
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
- }
26
- _next(undefined);
27
- });
28
- };
29
- }
30
- function _ts_generator(thisArg, body) {
31
- var f, y, t, g, _ = {
32
- label: 0,
33
- sent: function() {
34
- if (t[0] & 1) throw t[1];
35
- return t[1];
36
- },
37
- trys: [],
38
- ops: []
39
- };
40
- return g = {
41
- next: verb(0),
42
- "throw": verb(1),
43
- "return": verb(2)
44
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
45
- return this;
46
- }), g;
47
- function verb(n) {
48
- return function(v) {
49
- return step([
50
- n,
51
- v
52
- ]);
53
- };
54
- }
55
- function step(op) {
56
- if (f) throw new TypeError("Generator is already executing.");
57
- while(_)try {
58
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
- if (y = 0, t) op = [
60
- op[0] & 2,
61
- t.value
62
- ];
63
- switch(op[0]){
64
- case 0:
65
- case 1:
66
- t = op;
67
- break;
68
- case 4:
69
- _.label++;
70
- return {
71
- value: op[1],
72
- done: false
73
- };
74
- case 5:
75
- _.label++;
76
- y = op[1];
77
- op = [
78
- 0
79
- ];
80
- continue;
81
- case 7:
82
- op = _.ops.pop();
83
- _.trys.pop();
84
- continue;
85
- default:
86
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
87
- _ = 0;
88
- continue;
89
- }
90
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
- _.label = op[1];
92
- break;
93
- }
94
- if (op[0] === 6 && _.label < t[1]) {
95
- _.label = t[1];
96
- t = op;
97
- break;
98
- }
99
- if (t && _.label < t[2]) {
100
- _.label = t[2];
101
- _.ops.push(op);
102
- break;
103
- }
104
- if (t[2]) _.ops.pop();
105
- _.trys.pop();
106
- continue;
107
- }
108
- op = body.call(thisArg, _);
109
- } catch (e) {
110
- op = [
111
- 6,
112
- e
113
- ];
114
- y = 0;
115
- } finally{
116
- f = t = 0;
117
- }
118
- if (op[0] & 5) throw op[1];
119
- return {
120
- value: op[0] ? op[1] : void 0,
121
- done: true
122
- };
123
- }
124
- }
1
+ "use strict";
125
2
  import { defineDescription } from "@aeriajs/api";
126
3
  import { getConfig } from "@aeriajs/entrypoint";
127
- var link = function() {
128
- var _ref = _async_to_generator(function(_id) {
129
- var config;
130
- return _ts_generator(this, function(_state) {
131
- switch(_state.label){
132
- case 0:
133
- return [
134
- 4,
135
- getConfig()
136
- ];
137
- case 1:
138
- config = _state.sent();
139
- return [
140
- 2,
141
- "".concat(config.apiUrl || "", "/file/").concat(_id)
142
- ];
143
- }
144
- });
145
- });
146
- return function link(_id) {
147
- return _ref.apply(this, arguments);
148
- };
149
- }();
150
- var timestamp = function(lastModified) {
151
- return lastModified ? new Date(lastModified).getTime() : "fresh";
4
+ const link = async (_id) => {
5
+ const config = await getConfig();
6
+ return `${config.apiUrl || ""}/file/${_id}`;
152
7
  };
153
- export var description = defineDescription({
154
- $id: "file",
155
- owned: "always",
156
- presets: [
157
- "owned"
158
- ],
159
- indexes: [
160
- "filename",
161
- "link",
162
- "mime"
163
- ],
164
- properties: {
165
- mime: {
166
- type: "string"
167
- },
168
- size: {
169
- type: "number"
170
- },
171
- last_modified: {
172
- type: "string",
173
- format: "date-time"
174
- },
175
- filename: {
176
- type: "string"
177
- },
178
- absolute_path: {
179
- type: "string"
180
- },
181
- relative_path: {
182
- type: "string"
183
- },
184
- immutable: {
185
- type: "boolean"
186
- },
187
- link: {
188
- getter: function() {
189
- var _ref = _async_to_generator(function(value) {
190
- var _, _1;
191
- return _ts_generator(this, function(_state) {
192
- switch(_state.label){
193
- case 0:
194
- _1 = (_ = "").concat;
195
- return [
196
- 4,
197
- link(value._id)
198
- ];
199
- case 1:
200
- return [
201
- 2,
202
- _1.apply(_, [
203
- _state.sent(),
204
- "/"
205
- ]).concat(timestamp(value.last_modified))
206
- ];
207
- }
208
- });
209
- });
210
- return function(value) {
211
- return _ref.apply(this, arguments);
212
- };
213
- }()
214
- },
215
- download_link: {
216
- getter: function() {
217
- var _ref = _async_to_generator(function(value) {
218
- var _, _1;
219
- return _ts_generator(this, function(_state) {
220
- switch(_state.label){
221
- case 0:
222
- _1 = (_ = "").concat;
223
- return [
224
- 4,
225
- link(value._id)
226
- ];
227
- case 1:
228
- return [
229
- 2,
230
- _1.apply(_, [
231
- _state.sent(),
232
- "/download/"
233
- ]).concat(timestamp(value.last_modified))
234
- ];
235
- }
236
- });
237
- });
238
- return function(value) {
239
- return _ref.apply(this, arguments);
240
- };
241
- }()
242
- }
8
+ const timestamp = (lastModified) => lastModified ? new Date(lastModified).getTime() : "fresh";
9
+ export const description = defineDescription({
10
+ $id: "file",
11
+ owned: "always",
12
+ presets: ["owned"],
13
+ indexes: [
14
+ "filename",
15
+ "link",
16
+ "mime"
17
+ ],
18
+ properties: {
19
+ mime: {
20
+ type: "string"
21
+ },
22
+ size: {
23
+ type: "number"
24
+ },
25
+ last_modified: {
26
+ type: "string",
27
+ format: "date-time"
243
28
  },
244
- actions: {
245
- deleteAll: {
246
- name: "Remover",
247
- ask: true,
248
- selection: true
249
- }
29
+ filename: {
30
+ type: "string"
250
31
  },
251
- individualActions: {
252
- remove: {
253
- name: "Remover",
254
- icon: "trash",
255
- ask: true
256
- }
32
+ absolute_path: {
33
+ type: "string"
34
+ },
35
+ relative_path: {
36
+ type: "string"
37
+ },
38
+ immutable: {
39
+ type: "boolean"
40
+ },
41
+ link: {
42
+ getter: async (value) => {
43
+ return `${await link(value._id)}/${timestamp(value.last_modified)}`;
44
+ }
45
+ },
46
+ download_link: {
47
+ getter: async (value) => {
48
+ return `${await link(value._id)}/download/${timestamp(value.last_modified)}`;
49
+ }
50
+ }
51
+ },
52
+ actions: {
53
+ deleteAll: {
54
+ name: "Remover",
55
+ ask: true,
56
+ selection: true
57
+ }
58
+ },
59
+ individualActions: {
60
+ remove: {
61
+ name: "Remover",
62
+ icon: "trash",
63
+ ask: true
257
64
  }
65
+ }
258
66
  });
@@ -1,232 +1,65 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
- try {
3
- var info = gen[key](arg);
4
- var value = info.value;
5
- } catch (error) {
6
- reject(error);
7
- return;
8
- }
9
- if (info.done) {
10
- resolve(value);
11
- } else {
12
- Promise.resolve(value).then(_next, _throw);
13
- }
14
- }
15
- function _async_to_generator(fn) {
16
- return function() {
17
- var self = this, args = arguments;
18
- return new Promise(function(resolve, reject) {
19
- var gen = fn.apply(self, args);
20
- function _next(value) {
21
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
- }
23
- function _throw(err) {
24
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
- }
26
- _next(undefined);
27
- });
28
- };
29
- }
30
- function _ts_generator(thisArg, body) {
31
- var f, y, t, g, _ = {
32
- label: 0,
33
- sent: function() {
34
- if (t[0] & 1) throw t[1];
35
- return t[1];
36
- },
37
- trys: [],
38
- ops: []
39
- };
40
- return g = {
41
- next: verb(0),
42
- "throw": verb(1),
43
- "return": verb(2)
44
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
45
- return this;
46
- }), g;
47
- function verb(n) {
48
- return function(v) {
49
- return step([
50
- n,
51
- v
52
- ]);
53
- };
54
- }
55
- function step(op) {
56
- if (f) throw new TypeError("Generator is already executing.");
57
- while(_)try {
58
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
- if (y = 0, t) op = [
60
- op[0] & 2,
61
- t.value
62
- ];
63
- switch(op[0]){
64
- case 0:
65
- case 1:
66
- t = op;
67
- break;
68
- case 4:
69
- _.label++;
70
- return {
71
- value: op[1],
72
- done: false
73
- };
74
- case 5:
75
- _.label++;
76
- y = op[1];
77
- op = [
78
- 0
79
- ];
80
- continue;
81
- case 7:
82
- op = _.ops.pop();
83
- _.trys.pop();
84
- continue;
85
- default:
86
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
87
- _ = 0;
88
- continue;
89
- }
90
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
- _.label = op[1];
92
- break;
93
- }
94
- if (op[0] === 6 && _.label < t[1]) {
95
- _.label = t[1];
96
- t = op;
97
- break;
98
- }
99
- if (t && _.label < t[2]) {
100
- _.label = t[2];
101
- _.ops.push(op);
102
- break;
103
- }
104
- if (t[2]) _.ops.pop();
105
- _.trys.pop();
106
- continue;
107
- }
108
- op = body.call(thisArg, _);
109
- } catch (e) {
110
- op = [
111
- 6,
112
- e
113
- ];
114
- y = 0;
115
- } finally{
116
- f = t = 0;
117
- }
118
- if (op[0] & 5) throw op[1];
119
- return {
120
- value: op[0] ? op[1] : void 0,
121
- done: true
122
- };
123
- }
124
- }
1
+ "use strict";
125
2
  import { ObjectId } from "@aeriajs/api";
126
3
  import { left } from "@aeriajs/common";
127
4
  import fs from "fs";
128
- export var FileReadError;
129
- (function(FileReadError) {
130
- FileReadError["DocumentNotFound"] = "DOCUMENT_NOT_FOUND";
131
- FileReadError["FileNotFound"] = "FILE_NOT_FOUND";
132
- })(FileReadError || (FileReadError = {}));
133
- export var download = function() {
134
- var _ref = _async_to_generator(function(payload, context) {
135
- var fileId, _payload_options, options, file, stat, e, range, parts, start, end, chunkSize;
136
- return _ts_generator(this, function(_state) {
137
- switch(_state.label){
138
- case 0:
139
- fileId = payload.fileId, _payload_options = payload.options, options = _payload_options === void 0 ? [] : _payload_options;
140
- return [
141
- 4,
142
- context.collection.model.findOne({
143
- _id: new ObjectId(fileId)
144
- }, {
145
- projection: {
146
- absolute_path: 1,
147
- filename: 1,
148
- mime: 1
149
- }
150
- })
151
- ];
152
- case 1:
153
- file = _state.sent();
154
- if (!file) {
155
- if (!payload.noHeaders) {
156
- context.response.writeHead(404, {
157
- "content-type": "application/json"
158
- });
159
- }
160
- return [
161
- 2,
162
- left("DOCUMENT_NOT_FOUND")
163
- ];
164
- }
165
- _state.label = 2;
166
- case 2:
167
- _state.trys.push([
168
- 2,
169
- 4,
170
- ,
171
- 5
172
- ]);
173
- return [
174
- 4,
175
- fs.promises.stat(file.absolute_path)
176
- ];
177
- case 3:
178
- stat = _state.sent();
179
- return [
180
- 3,
181
- 5
182
- ];
183
- case 4:
184
- e = _state.sent();
185
- context.response.writeHead(404, {
186
- "content-type": "application/json"
187
- });
188
- return [
189
- 2,
190
- left("FILE_NOT_FOUND")
191
- ];
192
- case 5:
193
- range = context.request.headers.range;
194
- if (typeof range === "string") {
195
- parts = range.replace(/bytes=/, "").split("-");
196
- start = parseInt(parts[0]);
197
- end = parts[1] ? parseInt(parts[1]) : stat.size - 1;
198
- chunkSize = end - start + 1;
199
- if (!payload.noHeaders) {
200
- context.response.writeHead(206, {
201
- "accept-ranges": "bytes",
202
- "content-range": "bytes ".concat(start, "-").concat(end, "/").concat(stat.size),
203
- "content-length": chunkSize,
204
- "content-type": file.mime,
205
- "content-disposition": "".concat(options.includes("download") ? "attachment; " : "", "filename=").concat(encodeURI(file.filename))
206
- });
207
- }
208
- return [
209
- 2,
210
- fs.createReadStream(file.absolute_path, {
211
- start: start,
212
- end: end
213
- })
214
- ];
215
- }
216
- if (!payload.noHeaders) {
217
- context.response.writeHead(200, {
218
- "content-type": file.mime,
219
- "content-disposition": "".concat(options.includes("download") ? "attachment; " : "", "filename=").concat(encodeURI(file.filename))
220
- });
221
- }
222
- return [
223
- 2,
224
- fs.createReadStream(file.absolute_path)
225
- ];
226
- }
227
- });
5
+ export var FileReadError = /* @__PURE__ */ ((FileReadError2) => {
6
+ FileReadError2["DocumentNotFound"] = "DOCUMENT_NOT_FOUND";
7
+ FileReadError2["FileNotFound"] = "FILE_NOT_FOUND";
8
+ return FileReadError2;
9
+ })(FileReadError || {});
10
+ export const download = async (payload, context) => {
11
+ const { fileId, options = [] } = payload;
12
+ const file = await context.collection.model.findOne({
13
+ _id: new ObjectId(fileId)
14
+ }, {
15
+ projection: {
16
+ absolute_path: 1,
17
+ filename: 1,
18
+ mime: 1
19
+ }
20
+ });
21
+ if (!file) {
22
+ if (!payload.noHeaders) {
23
+ context.response.writeHead(404, {
24
+ "content-type": "application/json"
25
+ });
26
+ }
27
+ return left("DOCUMENT_NOT_FOUND" /* DocumentNotFound */);
28
+ }
29
+ let stat;
30
+ try {
31
+ stat = await fs.promises.stat(file.absolute_path);
32
+ } catch (e) {
33
+ context.response.writeHead(404, {
34
+ "content-type": "application/json"
35
+ });
36
+ return left("FILE_NOT_FOUND" /* FileNotFound */);
37
+ }
38
+ const range = context.request.headers.range;
39
+ if (typeof range === "string") {
40
+ const parts = range.replace(/bytes=/, "").split("-");
41
+ const start = parseInt(parts[0]);
42
+ const end = parts[1] ? parseInt(parts[1]) : stat.size - 1;
43
+ const chunkSize = end - start + 1;
44
+ if (!payload.noHeaders) {
45
+ context.response.writeHead(206, {
46
+ "accept-ranges": "bytes",
47
+ "content-range": `bytes ${start}-${end}/${stat.size}`,
48
+ "content-length": chunkSize,
49
+ "content-type": file.mime,
50
+ "content-disposition": `${options.includes("download") ? "attachment; " : ""}filename=${encodeURI(file.filename)}`
51
+ });
52
+ }
53
+ return fs.createReadStream(file.absolute_path, {
54
+ start,
55
+ end
56
+ });
57
+ }
58
+ if (!payload.noHeaders) {
59
+ context.response.writeHead(200, {
60
+ "content-type": file.mime,
61
+ "content-disposition": `${options.includes("download") ? "attachment; " : ""}filename=${encodeURI(file.filename)}`
228
62
  });
229
- return function download(payload, context) {
230
- return _ref.apply(this, arguments);
231
- };
232
- }();
63
+ }
64
+ return fs.createReadStream(file.absolute_path);
65
+ };