@jd-data-limited/easy-fm 3.1.7 → 3.2.0
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/package.json +1 -1
- package/src/connection/database.d.ts +1 -3
- package/src/connection/database.js +11 -23
- package/src/connection/database.ts +22 -32
- package/src/connection/databaseBase.d.ts +2 -0
- package/src/connection/databaseBase.ts +2 -0
- package/src/records/field.d.ts +3 -4
- package/src/records/field.js +21 -26
- package/src/records/field.ts +21 -31
- package/src/types.d.ts +2 -5
- package/src/types.ts +3 -4
package/package.json
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
2
|
import { EventEmitter } from "events";
|
|
4
3
|
import { Request } from "node-fetch";
|
|
5
4
|
import { LayoutInterface } from "../layouts/layoutInterface.js";
|
|
6
5
|
import { Layout } from "../layouts/layout.js";
|
|
7
|
-
import * as http from "http";
|
|
8
6
|
import { databaseOptionsWithExternalSources, DatabaseStructure, Script } from "../types.js";
|
|
9
7
|
import { HostBase } from "./HostBase.js";
|
|
10
8
|
import { DatabaseBase } from "./databaseBase";
|
|
@@ -26,5 +24,5 @@ export declare class Database<T extends DatabaseStructure> extends EventEmitter
|
|
|
26
24
|
setGlobals(globalFields: any): Promise<void>;
|
|
27
25
|
script(name: any, parameter?: string): Script;
|
|
28
26
|
_tokenExpired(): void;
|
|
29
|
-
streamContainer(
|
|
27
|
+
streamContainer(url: any): Promise<import("node-fetch").Response>;
|
|
30
28
|
}
|
|
@@ -15,8 +15,6 @@ import fetch from "node-fetch";
|
|
|
15
15
|
import { generateAuthorizationHeaders } from "./generateAuthorizationHeaders.js";
|
|
16
16
|
import { FMError } from "../FMError.js";
|
|
17
17
|
import { Layout } from "../layouts/layout.js";
|
|
18
|
-
import * as http from "http";
|
|
19
|
-
import * as https from "https";
|
|
20
18
|
export class Database extends EventEmitter {
|
|
21
19
|
constructor(host, conn) {
|
|
22
20
|
super();
|
|
@@ -158,16 +156,8 @@ export class Database extends EventEmitter {
|
|
|
158
156
|
_tokenExpired() {
|
|
159
157
|
this.emit("token_expired");
|
|
160
158
|
}
|
|
161
|
-
streamContainer(
|
|
162
|
-
return
|
|
163
|
-
if (field.metadata.result !== "container") {
|
|
164
|
-
reject("Cannot stream the field " + field.id + " as it is not a container");
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
if (!url || typeof url !== "string") {
|
|
168
|
-
reject("Container is empty, or has invalid value");
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
159
|
+
streamContainer(url) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
171
161
|
let headers = {};
|
|
172
162
|
if (Object.keys(this.cookies).length !== 0) {
|
|
173
163
|
headers["Cookie"] = Object.keys(this.cookies)
|
|
@@ -177,19 +167,17 @@ export class Database extends EventEmitter {
|
|
|
177
167
|
.join("; ");
|
|
178
168
|
}
|
|
179
169
|
// Automatically switch between the http and https modules, based on which is needed
|
|
180
|
-
|
|
170
|
+
let res = yield fetch(url, {
|
|
181
171
|
headers
|
|
182
|
-
}, (res) => {
|
|
183
|
-
// Check for the 'set-cookie' header. If it exists, remember it and strip it for better security.
|
|
184
|
-
if (res.headers['set-cookie']) {
|
|
185
|
-
for (let cookie of res.headers['set-cookie']) {
|
|
186
|
-
let cookie_split = cookie.split("=");
|
|
187
|
-
this.cookies[cookie_split[0]] = cookie_split[1];
|
|
188
|
-
}
|
|
189
|
-
res.headers['set-cookie'] = null;
|
|
190
|
-
}
|
|
191
|
-
resolve(res);
|
|
192
172
|
});
|
|
173
|
+
if (res.headers['set-cookie']) {
|
|
174
|
+
for (let cookie of res.headers['set-cookie']) {
|
|
175
|
+
let cookie_split = cookie.split("=");
|
|
176
|
+
this.cookies[cookie_split[0]] = cookie_split[1];
|
|
177
|
+
}
|
|
178
|
+
res.headers['set-cookie'] = null;
|
|
179
|
+
}
|
|
180
|
+
return res;
|
|
193
181
|
});
|
|
194
182
|
}
|
|
195
183
|
}
|
|
@@ -181,41 +181,31 @@ export class Database<T extends DatabaseStructure> extends EventEmitter implemen
|
|
|
181
181
|
this.emit("token_expired")
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
streamContainer(
|
|
185
|
-
return new Promise((resolve, reject) => {
|
|
186
|
-
if (field.metadata.result !== "container") {
|
|
187
|
-
reject("Cannot stream the field " + field.id + " as it is not a container")
|
|
188
|
-
return
|
|
189
|
-
}
|
|
190
|
-
if (!url || typeof url !== "string") {
|
|
191
|
-
reject("Container is empty, or has invalid value")
|
|
192
|
-
return
|
|
193
|
-
}
|
|
184
|
+
async streamContainer(url) {
|
|
194
185
|
|
|
195
|
-
let headers = {}
|
|
196
|
-
if (Object.keys(this.cookies).length !== 0) {
|
|
197
|
-
headers["Cookie"] = Object.keys(this.cookies)
|
|
198
|
-
.map(key => {
|
|
199
|
-
return key + "=" + this.cookies[key]
|
|
200
|
-
})
|
|
201
|
-
.join("; ")
|
|
202
|
-
}
|
|
203
186
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
this.cookies[cookie_split[0]] = cookie_split[1]
|
|
213
|
-
}
|
|
214
|
-
res.headers['set-cookie'] = null
|
|
215
|
-
}
|
|
187
|
+
let headers = {}
|
|
188
|
+
if (Object.keys(this.cookies).length !== 0) {
|
|
189
|
+
headers["Cookie"] = Object.keys(this.cookies)
|
|
190
|
+
.map(key => {
|
|
191
|
+
return key + "=" + this.cookies[key]
|
|
192
|
+
})
|
|
193
|
+
.join("; ")
|
|
194
|
+
}
|
|
216
195
|
|
|
217
|
-
|
|
218
|
-
|
|
196
|
+
// Automatically switch between the http and https modules, based on which is needed
|
|
197
|
+
let res = await fetch(url, {
|
|
198
|
+
headers
|
|
219
199
|
})
|
|
200
|
+
|
|
201
|
+
if (res.headers['set-cookie']) {
|
|
202
|
+
for (let cookie of res.headers['set-cookie']) {
|
|
203
|
+
let cookie_split = cookie.split("=")
|
|
204
|
+
this.cookies[cookie_split[0]] = cookie_split[1]
|
|
205
|
+
}
|
|
206
|
+
res.headers['set-cookie'] = null
|
|
207
|
+
}
|
|
208
|
+
return res
|
|
220
209
|
}
|
|
210
|
+
|
|
221
211
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { HostBase } from "./HostBase.js";
|
|
2
2
|
import { Request } from "node-fetch";
|
|
3
|
+
import { Response } from "node-fetch";
|
|
3
4
|
export interface DatabaseBase {
|
|
4
5
|
host: HostBase;
|
|
5
6
|
readonly name: string;
|
|
6
7
|
endpoint: string;
|
|
7
8
|
apiRequest(url: string | Request, options?: any, autoRelogin?: boolean): Promise<any>;
|
|
9
|
+
streamContainer(url: string): Promise<Response>;
|
|
8
10
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import {HostBase} from "./HostBase.js"
|
|
6
6
|
import {Request} from "node-fetch";
|
|
7
7
|
import {DatabaseStructure} from "../types";
|
|
8
|
+
import {Response} from "node-fetch";
|
|
8
9
|
|
|
9
10
|
export interface DatabaseBase {
|
|
10
11
|
host: HostBase,
|
|
@@ -14,4 +15,5 @@ export interface DatabaseBase {
|
|
|
14
15
|
// layouts: DatabaseStructure["layouts"]
|
|
15
16
|
|
|
16
17
|
apiRequest(url: string | Request, options?: any, autoRelogin?: boolean): Promise<any>
|
|
18
|
+
streamContainer(url: string): Promise<Response>
|
|
17
19
|
}
|
package/src/records/field.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import { RecordBase } from "./recordBase.js";
|
|
4
|
-
import
|
|
5
|
-
import { ContainerBufferResult, DOWNLOAD_MODES, FieldMetaData } from "../types.js";
|
|
4
|
+
import { ContainerDataStream, DOWNLOAD_MODES, FieldMetaData } from "../types.js";
|
|
6
5
|
export type FieldValue = string | number | Date | Container;
|
|
7
6
|
export type Container = null;
|
|
8
7
|
export declare class Field<T extends FieldValue> {
|
|
@@ -19,6 +18,6 @@ export declare class Field<T extends FieldValue> {
|
|
|
19
18
|
get date(): Date;
|
|
20
19
|
get number(): number;
|
|
21
20
|
upload(buffer: Buffer, filename: string, mime: string): Promise<void>;
|
|
22
|
-
download(mode: DOWNLOAD_MODES.Stream): Promise<
|
|
23
|
-
download(mode: DOWNLOAD_MODES.Buffer): Promise<
|
|
21
|
+
download(mode: DOWNLOAD_MODES.Stream): Promise<ContainerDataStream<NodeJS.ReadableStream>>;
|
|
22
|
+
download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerDataStream<ArrayBuffer>>;
|
|
24
23
|
}
|
package/src/records/field.js
CHANGED
|
@@ -149,32 +149,27 @@ export class Field {
|
|
|
149
149
|
}));
|
|
150
150
|
}
|
|
151
151
|
download(mode = DOWNLOAD_MODES.Stream) {
|
|
152
|
-
return
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
});
|
|
174
|
-
})
|
|
175
|
-
.catch(e => {
|
|
176
|
-
reject(e);
|
|
177
|
-
});
|
|
152
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
if (this.metadata.result !== "container") {
|
|
154
|
+
throw new Error("Cannot stream the field " + this.id + " as it is not a container");
|
|
155
|
+
}
|
|
156
|
+
if (!this.value || typeof this.value !== "string") {
|
|
157
|
+
throw new Error("Container is empty, or has invalid value");
|
|
158
|
+
}
|
|
159
|
+
let res = yield this.record.layout.database.streamContainer(this.string);
|
|
160
|
+
let data;
|
|
161
|
+
switch (mode) {
|
|
162
|
+
case DOWNLOAD_MODES.Buffer:
|
|
163
|
+
data = yield res.arrayBuffer();
|
|
164
|
+
break;
|
|
165
|
+
case DOWNLOAD_MODES.Stream:
|
|
166
|
+
data = res.body;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
data,
|
|
171
|
+
mime: res.headers["content-type"]
|
|
172
|
+
};
|
|
178
173
|
});
|
|
179
174
|
}
|
|
180
175
|
}
|
package/src/records/field.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import {RecordBase, RecordTypes} from "./recordBase.js";
|
|
6
6
|
import fetch, {File, FormData} from "node-fetch";
|
|
7
7
|
import * as http from "http";
|
|
8
|
-
import {
|
|
8
|
+
import {ContainerDataStream, DOWNLOAD_MODES, FieldMetaData} from "../types.js";
|
|
9
9
|
import {FMError} from "../FMError.js";
|
|
10
10
|
|
|
11
11
|
export type FieldValue = string | number | Date | Container
|
|
@@ -158,36 +158,26 @@ export class Field<T extends FieldValue> {
|
|
|
158
158
|
})
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
download(mode: DOWNLOAD_MODES.Stream): Promise<
|
|
162
|
-
download(mode: DOWNLOAD_MODES.Buffer): Promise<
|
|
163
|
-
download(mode: DOWNLOAD_MODES = DOWNLOAD_MODES.Stream): Promise<
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return
|
|
171
|
-
}
|
|
161
|
+
download(mode: DOWNLOAD_MODES.Stream): Promise<ContainerDataStream<NodeJS.ReadableStream>>
|
|
162
|
+
download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerDataStream<ArrayBuffer>>
|
|
163
|
+
async download(mode: DOWNLOAD_MODES = DOWNLOAD_MODES.Stream): Promise<ContainerDataStream> {
|
|
164
|
+
if (this.metadata.result !== "container") {
|
|
165
|
+
throw new Error("Cannot stream the field " + this.id + " as it is not a container")
|
|
166
|
+
}
|
|
167
|
+
if (!this.value || typeof this.value !== "string") {
|
|
168
|
+
throw new Error("Container is empty, or has invalid value")
|
|
169
|
+
}
|
|
172
170
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
request: stream
|
|
185
|
-
} as ContainerBufferResult)
|
|
186
|
-
})
|
|
187
|
-
})
|
|
188
|
-
.catch(e => {
|
|
189
|
-
reject(e)
|
|
190
|
-
})
|
|
191
|
-
})
|
|
171
|
+
let res = await this.record.layout.database.streamContainer(this.string)
|
|
172
|
+
let data: NodeJS.ReadableStream | ArrayBuffer
|
|
173
|
+
switch (mode) {
|
|
174
|
+
case DOWNLOAD_MODES.Buffer: data = await res.arrayBuffer(); break
|
|
175
|
+
case DOWNLOAD_MODES.Stream: data = res.body; break
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
data,
|
|
180
|
+
mime: res.headers["content-type"]
|
|
181
|
+
}
|
|
192
182
|
}
|
|
193
183
|
}
|
package/src/types.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import * as http from "http";
|
|
4
2
|
import { FMError } from "./FMError.js";
|
|
5
3
|
import { LayoutInterface } from "./layouts/layoutInterface";
|
|
6
4
|
export interface DatabaseStructure {
|
|
@@ -108,10 +106,9 @@ export interface ScriptResult {
|
|
|
108
106
|
scriptError?: FMError;
|
|
109
107
|
scriptResult?: string;
|
|
110
108
|
}
|
|
111
|
-
export interface
|
|
112
|
-
|
|
109
|
+
export interface ContainerDataStream<T = NodeJS.ReadableStream | ArrayBuffer> {
|
|
110
|
+
data: T;
|
|
113
111
|
mime: string;
|
|
114
|
-
request: http.IncomingMessage;
|
|
115
112
|
}
|
|
116
113
|
export interface AuthorizationHeaders {
|
|
117
114
|
"Content-Type": "application/json";
|
package/src/types.ts
CHANGED
|
@@ -132,10 +132,9 @@ export interface ScriptResult {
|
|
|
132
132
|
scriptResult?: string
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
export interface
|
|
136
|
-
|
|
137
|
-
mime: string
|
|
138
|
-
request: http.IncomingMessage
|
|
135
|
+
export interface ContainerDataStream<T = NodeJS.ReadableStream | ArrayBuffer> {
|
|
136
|
+
data: T,
|
|
137
|
+
mime: string
|
|
139
138
|
}
|
|
140
139
|
|
|
141
140
|
//
|