@jd-data-limited/easy-fm 3.1.7 → 3.2.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jd-data-limited/easy-fm",
3
- "version": "3.1.7",
3
+ "version": "3.2.1",
4
4
  "dependencies": {
5
5
  "@types/btoa": "^1.2.3",
6
6
  "@types/events": "^3.0.0",
@@ -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(field: any, url: any): Promise<http.IncomingMessage>;
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(field, url) {
162
- return new Promise((resolve, reject) => {
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
- (url.startsWith("https") ? https : http).get(url, {
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(field, url): Promise<http.IncomingMessage> {
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
- // Automatically switch between the http and https modules, based on which is needed
205
- (url.startsWith("https") ? https : http).get(url, {
206
- headers
207
- }, (res) => {
208
- // Check for the 'set-cookie' header. If it exists, remember it and strip it for better security.
209
- if (res.headers['set-cookie']) {
210
- for (let cookie of res.headers['set-cookie']) {
211
- let cookie_split = cookie.split("=")
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
- resolve(res)
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
  }
@@ -1,8 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import { RecordBase } from "./recordBase.js";
4
- import * as http from "http";
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<http.IncomingMessage>;
23
- download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerBufferResult>;
21
+ download(mode: DOWNLOAD_MODES.Stream): Promise<ContainerDataStream<NodeJS.ReadableStream>>;
22
+ download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerDataStream<ArrayBuffer>>;
24
23
  }
@@ -149,32 +149,27 @@ export class Field {
149
149
  }));
150
150
  }
151
151
  download(mode = DOWNLOAD_MODES.Stream) {
152
- return new Promise((resolve, reject) => {
153
- // @ts-ignore
154
- this.record.layout.database.streamContainer(this, this._value)
155
- .then(stream => {
156
- if (mode === DOWNLOAD_MODES.Stream) {
157
- resolve(stream);
158
- return;
159
- }
160
- let body = [];
161
- stream.on("data", chunk => {
162
- body.push(chunk);
163
- });
164
- stream.on("error", (e) => {
165
- reject(e);
166
- });
167
- stream.on("end", () => {
168
- resolve({
169
- buffer: Buffer.concat(body),
170
- mime: stream.headers["content-type"],
171
- request: stream
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.get("content-type")
172
+ };
178
173
  });
179
174
  }
180
175
  }
@@ -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 {ContainerBufferResult, DOWNLOAD_MODES, FieldMetaData} from "../types.js";
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<http.IncomingMessage>
162
- download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerBufferResult>
163
- download(mode: DOWNLOAD_MODES = DOWNLOAD_MODES.Stream): Promise<http.IncomingMessage | ContainerBufferResult> {
164
- return new Promise((resolve, reject) => {
165
- // @ts-ignore
166
- this.record.layout.database.streamContainer(this, this._value)
167
- .then(stream => {
168
- if (mode === DOWNLOAD_MODES.Stream) {
169
- resolve(stream)
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
- let body = []
174
- stream.on("data", chunk => {
175
- body.push(chunk)
176
- })
177
- stream.on("error", (e) => {
178
- reject(e)
179
- })
180
- stream.on("end", () => {
181
- resolve({
182
- buffer: Buffer.concat(body),
183
- mime: stream.headers["content-type"],
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.get("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 ContainerBufferResult {
112
- buffer: Buffer;
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 ContainerBufferResult {
136
- buffer: Buffer,
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
  //