node-datalith 0.0.2 → 0.0.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.
Files changed (3) hide show
  1. package/lib/lib.d.ts +20 -0
  2. package/lib/lib.js +37 -35
  3. package/package.json +2 -2
package/lib/lib.d.ts CHANGED
@@ -42,6 +42,16 @@ export interface ResourcePutOptions extends WithBodyTimeoutOptions {
42
42
  * @default undefined
43
43
  */
44
44
  fileType?: string;
45
+ /**
46
+ * The file size.
47
+ *
48
+ * If not provided (`undefined`), Datalith will not be aware of the correct file size, which may result in the file being incomplete.
49
+ *
50
+ * If you're unsure about the file size, you can set it to the maximum file size you want it to be.
51
+ *
52
+ * @default undefined
53
+ */
54
+ fileSize?: number;
45
55
  /**
46
56
  * Indicates if the file is temporary. If `true`, the file may be deleted after a short period and can use the `getRecource` method to retrieve it only once.
47
57
  *
@@ -66,6 +76,16 @@ export interface ImagePutOptions extends WithBodyTimeoutOptions {
66
76
  * @default undefined
67
77
  */
68
78
  fileName?: string;
79
+ /**
80
+ * The file size.
81
+ *
82
+ * If not provided (`undefined`), Datalith will not be aware of the correct file size, which may result in the file being incomplete.
83
+ *
84
+ * If you're unsure about the file size, you can set it to the maximum file size you want it to be.
85
+ *
86
+ * @default undefined
87
+ */
88
+ fileSize?: number;
69
89
  /**
70
90
  * The maximum width of the image in pixels.
71
91
  *
package/lib/lib.js CHANGED
@@ -54,35 +54,36 @@ export class Datalith {
54
54
  if (typeof options.temporary !== "undefined") {
55
55
  searchParams.append("temporary", options.temporary ? "1" : "0");
56
56
  }
57
+ const headers = {};
58
+ if (typeof options.fileSize !== "undefined") {
59
+ headers["x-file-length"] = options.fileSize.toString();
60
+ }
57
61
  const response = await timeoutFetch(this._apiOperate.toString(), {
58
62
  method: "PUT",
63
+ headers,
59
64
  body: fileStream,
60
65
  requestTimeout: typeof options.reqeustTimeout !== "undefined" ? options.reqeustTimeout : DEFAULT_REQUEST_TIMEOUT,
61
66
  idleTimeout: typeof options.idleTimeout !== "undefined" ? options.idleTimeout : DEFAULT_IDLE_TIMEOUT,
62
67
  duplex: "half",
63
68
  });
64
- switch (response.status) {
65
- case 200:
66
- break;
67
- case 400:
68
- await response.cancelBody();
69
- throw new BadRequestError();
70
- case 413:
71
- await response.cancelBody();
72
- throw new PayloadTooLargeError();
73
- default:
74
- await response.cancelBody();
75
- throw new Error("unknown error");
76
- }
77
- let json;
78
69
  try {
79
- json = await response.json();
70
+ switch (response.status) {
71
+ case 200:
72
+ break;
73
+ case 400:
74
+ throw new BadRequestError();
75
+ case 413:
76
+ throw new PayloadTooLargeError();
77
+ default:
78
+ throw new Error("unknown error");
79
+ }
80
+ const json = await response.json();
81
+ return new Resource(json.id, new Date(json.created_at), json.file_type, json.file_size, json.file_name, json.is_temporary);
80
82
  }
81
83
  catch (error) {
82
84
  await response.cancelBody();
83
85
  throw error;
84
86
  }
85
- return new Resource(json.id, new Date(json.created_at), json.file_type, json.file_size, json.file_name, json.is_temporary);
86
87
  }
87
88
  /**
88
89
  * Input a image into Datalith.
@@ -116,38 +117,39 @@ export class Datalith {
116
117
  if (typeof options.saveOriginalFile !== "undefined") {
117
118
  searchParams.append("save_original_file", options.saveOriginalFile ? "1" : "0");
118
119
  }
120
+ const headers = {};
121
+ if (typeof options.fileSize !== "undefined") {
122
+ headers["x-file-length"] = options.fileSize.toString();
123
+ }
119
124
  const response = await timeoutFetch(this._apiOperateImage.toString(), {
120
125
  method: "PUT",
126
+ headers,
121
127
  body: fileStream,
122
128
  requestTimeout: typeof options.reqeustTimeout !== "undefined" ? options.reqeustTimeout : DEFAULT_REQUEST_TIMEOUT,
123
129
  idleTimeout: typeof options.idleTimeout !== "undefined" ? options.idleTimeout : DEFAULT_IDLE_TIMEOUT,
124
130
  duplex: "half",
125
131
  });
126
- switch (response.status) {
127
- case 200:
128
- break;
129
- case 400:
130
- await response.cancelBody();
131
- throw new BadRequestError();
132
- case 413:
133
- await response.cancelBody();
134
- throw new PayloadTooLargeError();
135
- default:
136
- await response.cancelBody();
137
- throw new Error("unknown error");
138
- }
139
- let json;
140
132
  try {
141
- json = await response.json();
133
+ switch (response.status) {
134
+ case 200:
135
+ break;
136
+ case 400:
137
+ throw new BadRequestError();
138
+ case 413:
139
+ throw new PayloadTooLargeError();
140
+ default:
141
+ throw new Error("unknown error");
142
+ }
143
+ const json = await response.json();
144
+ return new Image(json.id, new Date(json.created_at), json.image_stem, {
145
+ width: json.image_width,
146
+ height: json.image_height,
147
+ });
142
148
  }
143
149
  catch (error) {
144
150
  await response.cancelBody();
145
151
  throw error;
146
152
  }
147
- return new Image(json.id, new Date(json.created_at), json.image_stem, {
148
- width: json.image_width,
149
- height: json.image_height,
150
- });
151
153
  }
152
154
  /**
153
155
  * Get a resource from Datalith.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-datalith",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Datalith is a file management system powered by SQLite for metadata storage and the file system for file storage. This library can help you conmunicate with Datalith in Node.js.",
5
5
  "type": "module",
6
6
  "exports": "./lib/lib.js",
@@ -43,7 +43,7 @@
43
43
  "homepage": "https://magiclen.org/datalith/",
44
44
  "dependencies": {
45
45
  "@types/node": "^22.5.4",
46
- "fetch-helper-x": "^0.1.4"
46
+ "fetch-helper-x": "^0.1.5"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/jest": "^29.5.12",