node-firebird-driver-native 3.2.0 → 3.3.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.
@@ -6,91 +6,104 @@ import { EventsImpl } from './events';
6
6
  import { createDpb } from './fb-util';
7
7
 
8
8
  import {
9
- Blob,
10
- ConnectOptions,
11
- CreateBlobOptions,
12
- CreateDatabaseOptions,
13
- PrepareOptions,
14
- TransactionOptions
9
+ Blob,
10
+ ConnectOptions,
11
+ CreateBlobOptions,
12
+ CreateDatabaseOptions,
13
+ PrepareOptions,
14
+ TransactionOptions,
15
15
  } from 'node-firebird-driver';
16
16
 
17
17
  import { AbstractAttachment, cancelType } from 'node-firebird-driver/dist/lib/impl';
18
18
 
19
19
  import * as fb from 'node-firebird-native-api';
20
20
 
21
-
22
21
  /** Attachment implementation. */
23
22
  export class AttachmentImpl extends AbstractAttachment {
24
- // Override declarations.
25
- override client: ClientImpl;
26
-
27
- attachmentHandle?: fb.Attachment;
28
-
29
- static async connect(client: ClientImpl, uri: string, options?: ConnectOptions): Promise<AttachmentImpl> {
30
- const attachment = new AttachmentImpl(client);
31
-
32
- return await client.statusAction(async status => {
33
- const dpb = createDpb(options);
34
- attachment.attachmentHandle = await client!.dispatcher!.attachDatabaseAsync(status, uri, dpb.length, dpb);
35
- return attachment;
36
- });
37
- }
38
-
39
- static async createDatabase(client: ClientImpl, uri: string, options?: CreateDatabaseOptions): Promise<AttachmentImpl> {
40
- const attachment = new AttachmentImpl(client);
41
-
42
- return await client.statusAction(async status => {
43
- const dpb = createDpb(options);
44
- attachment.attachmentHandle = await client!.dispatcher!.createDatabaseAsync(status, uri, dpb.length, dpb);
45
- return attachment;
46
- });
47
- }
48
-
49
- /** Disconnects this attachment. */
50
- protected async internalDisconnect(): Promise<void> {
51
- await this.client.statusAction(status => this.attachmentHandle!.detachAsync(status));
52
- this.attachmentHandle = undefined;
53
- }
54
-
55
- /** Drops the database and release this attachment. */
56
- protected async internalDropDatabase(): Promise<void> {
57
- await this.client.statusAction(status => this.attachmentHandle!.dropDatabaseAsync(status));
58
- this.attachmentHandle = undefined;
59
- }
60
-
61
- /** Enable/disable cancellation of operations in this attachment. */
62
- protected async internalEnableCancellation(enable: boolean): Promise<void> {
63
- await this.client.statusAction(status =>
64
- this.attachmentHandle!.cancelOperationAsync(status, (enable ? cancelType.enable : cancelType.disable)));
65
- }
66
-
67
- /** Cancel a running operation in this attachment. */
68
- protected async internalCancelOperation(forcibleAbort: boolean): Promise<void> {
69
- await this.client.statusAction(status =>
70
- this.attachmentHandle!.cancelOperationAsync(status, (forcibleAbort ? cancelType.abort : cancelType.raise)));
71
- }
72
-
73
- /** Starts a new transaction. */
74
- protected async internalStartTransaction(options?: TransactionOptions): Promise<TransactionImpl> {
75
- return await TransactionImpl.start(this, options);
76
- }
77
-
78
- protected async internalCreateBlob(transaction: TransactionImpl, options?: CreateBlobOptions): Promise<BlobStreamImpl> {
79
- return await BlobStreamImpl.create(this, transaction, options);
80
- }
81
-
82
- protected async internalOpenBlob(transaction: TransactionImpl, blob: Blob): Promise<BlobStreamImpl> {
83
- return await BlobStreamImpl.open(this, transaction, blob);
84
- }
85
-
86
- /** Prepares a query. */
87
- protected async internalPrepare(transaction: TransactionImpl, sqlStmt: string, options?: PrepareOptions): Promise<StatementImpl> {
88
- return await StatementImpl.prepare(this, transaction, sqlStmt, options);
89
- }
90
-
91
- protected async internalQueueEvents(
92
- names: string[], callBack: (counters: [string, number][]) => Promise<void>): Promise<EventsImpl>
93
- {
94
- return await EventsImpl.queue(this, names, callBack);
95
- }
23
+ // Override declarations.
24
+ override client: ClientImpl;
25
+
26
+ attachmentHandle?: fb.Attachment;
27
+
28
+ static async connect(client: ClientImpl, uri: string, options?: ConnectOptions): Promise<AttachmentImpl> {
29
+ const attachment = new AttachmentImpl(client);
30
+
31
+ return await client.statusAction(async (status) => {
32
+ const dpb = createDpb(options);
33
+ attachment.attachmentHandle = await client!.dispatcher!.attachDatabaseAsync(status, uri, dpb.length, dpb);
34
+ return attachment;
35
+ });
36
+ }
37
+
38
+ static async createDatabase(
39
+ client: ClientImpl,
40
+ uri: string,
41
+ options?: CreateDatabaseOptions,
42
+ ): Promise<AttachmentImpl> {
43
+ const attachment = new AttachmentImpl(client);
44
+
45
+ return await client.statusAction(async (status) => {
46
+ const dpb = createDpb(options);
47
+ attachment.attachmentHandle = await client!.dispatcher!.createDatabaseAsync(status, uri, dpb.length, dpb);
48
+ return attachment;
49
+ });
50
+ }
51
+
52
+ /** Disconnects this attachment. */
53
+ protected async internalDisconnect(): Promise<void> {
54
+ await this.client.statusAction((status) => this.attachmentHandle!.detachAsync(status));
55
+ this.attachmentHandle = undefined;
56
+ }
57
+
58
+ /** Drops the database and release this attachment. */
59
+ protected async internalDropDatabase(): Promise<void> {
60
+ await this.client.statusAction((status) => this.attachmentHandle!.dropDatabaseAsync(status));
61
+ this.attachmentHandle = undefined;
62
+ }
63
+
64
+ /** Enable/disable cancellation of operations in this attachment. */
65
+ protected async internalEnableCancellation(enable: boolean): Promise<void> {
66
+ await this.client.statusAction((status) =>
67
+ this.attachmentHandle!.cancelOperationAsync(status, enable ? cancelType.enable : cancelType.disable),
68
+ );
69
+ }
70
+
71
+ /** Cancel a running operation in this attachment. */
72
+ protected async internalCancelOperation(forcibleAbort: boolean): Promise<void> {
73
+ await this.client.statusAction((status) =>
74
+ this.attachmentHandle!.cancelOperationAsync(status, forcibleAbort ? cancelType.abort : cancelType.raise),
75
+ );
76
+ }
77
+
78
+ /** Starts a new transaction. */
79
+ protected async internalStartTransaction(options?: TransactionOptions): Promise<TransactionImpl> {
80
+ return await TransactionImpl.start(this, options);
81
+ }
82
+
83
+ protected async internalCreateBlob(
84
+ transaction: TransactionImpl,
85
+ options?: CreateBlobOptions,
86
+ ): Promise<BlobStreamImpl> {
87
+ return await BlobStreamImpl.create(this, transaction, options);
88
+ }
89
+
90
+ protected async internalOpenBlob(transaction: TransactionImpl, blob: Blob): Promise<BlobStreamImpl> {
91
+ return await BlobStreamImpl.open(this, transaction, blob);
92
+ }
93
+
94
+ /** Prepares a query. */
95
+ protected async internalPrepare(
96
+ transaction: TransactionImpl,
97
+ sqlStmt: string,
98
+ options?: PrepareOptions,
99
+ ): Promise<StatementImpl> {
100
+ return await StatementImpl.prepare(this, transaction, sqlStmt, options);
101
+ }
102
+
103
+ protected async internalQueueEvents(
104
+ names: string[],
105
+ callBack: (counters: [string, number][]) => Promise<void>,
106
+ ): Promise<EventsImpl> {
107
+ return await EventsImpl.queue(this, names, callBack);
108
+ }
96
109
  }
package/src/lib/blob.ts CHANGED
@@ -6,97 +6,111 @@ import { AbstractBlobStream, blobInfo, createBpb, getPortableInteger } from 'nod
6
6
  import * as fb from 'node-firebird-native-api';
7
7
  import { TransactionImpl } from './transaction';
8
8
 
9
-
10
9
  const MAX_SEGMENT_SIZE = 65535;
11
10
 
12
-
13
11
  /** BlobStream implementation. */
14
12
  export class BlobStreamImpl extends AbstractBlobStream {
15
- // Override declarations.
16
- override attachment: AttachmentImpl;
17
-
18
- blobHandle?: fb.Blob;
19
-
20
- static async create(attachment: AttachmentImpl, transaction: TransactionImpl, options?: CreateBlobOptions): Promise<BlobStreamImpl> {
21
- return await attachment.client.statusAction(async status => {
22
- const blobId = new Uint8Array(8);
23
- const bpb = createBpb(options);
24
-
25
- const blobHandle = await attachment.attachmentHandle!.createBlobAsync(
26
- status, transaction.transactionHandle, blobId, bpb.length, bpb);
27
-
28
- const blob = new Blob(attachment, blobId);
29
-
30
- const blobStream = new BlobStreamImpl(blob, attachment);
31
- blobStream.blobHandle = blobHandle;
32
- return blobStream;
33
- });
34
- }
35
-
36
- static async open(attachment: AttachmentImpl, transaction: TransactionImpl, blob: Blob): Promise<BlobStreamImpl> {
37
- return await attachment.client.statusAction(async status => {
38
- const blobStream = new BlobStreamImpl(blob, attachment);
39
- blobStream.blobHandle = await attachment.attachmentHandle!.openBlobAsync(
40
- status, transaction.transactionHandle, blob.id, 0, undefined);
41
- return blobStream;
42
- });
43
- }
44
-
45
- protected async internalGetLength(): Promise<number> {
46
- return await this.attachment.client.statusAction(async status => {
47
- const infoReq = new Uint8Array([blobInfo.totalLength]);
48
- const infoRet = new Uint8Array(20);
49
- await this.blobHandle!.getInfoAsync(status, infoReq.byteLength, infoReq, infoRet.byteLength, infoRet);
50
-
51
- if (infoRet[0] != blobInfo.totalLength)
52
- throw new Error('Unrecognized response from Blob::getInfo.');
53
-
54
- const size = getPortableInteger(infoRet.subarray(1), 2);
55
-
56
- return getPortableInteger(infoRet.subarray(3), size);
57
- });
58
- }
59
-
60
- protected async internalClose(): Promise<void> {
61
- await this.attachment.client.statusAction(status => this.blobHandle!.closeAsync(status));
62
- this.blobHandle = undefined;
63
- }
64
-
65
- protected async internalCancel(): Promise<void> {
66
- await this.attachment.client.statusAction(status => this.blobHandle!.cancelAsync(status));
67
- this.blobHandle = undefined;
68
- }
69
-
70
- protected async internalSeek(offset: number, whence?: BlobSeekWhence): Promise<number> {
71
- return await this.attachment.client.statusAction(async status => {
72
- return await this.blobHandle!.seekAsync(status, whence ?? BlobSeekWhence.START, offset);
73
- });
74
- }
75
-
76
- protected async internalRead(buffer: Buffer): Promise<number> {
77
- return await this.attachment.client.statusAction(async status => {
78
- const readingBytes = Math.min(buffer.length, MAX_SEGMENT_SIZE);
79
- const segLength = new Uint32Array(1);
80
- const result = await this.blobHandle!.getSegmentAsync(status, readingBytes, buffer, segLength);
81
-
82
- if (result == fb.Status.RESULT_NO_DATA)
83
- return -1;
84
-
85
- return segLength[0];
86
- });
87
- }
88
-
89
- protected async internalWrite(buffer: Buffer): Promise<void> {
90
- await this.attachment.client.statusAction(async status => {
91
- while (buffer.length > 0) {
92
- const writingBytes = Math.min(buffer.length, MAX_SEGMENT_SIZE);
93
- await this.blobHandle!.putSegmentAsync(status, writingBytes, buffer);
94
- buffer = buffer.slice(writingBytes);
95
- }
96
- });
97
- }
98
-
99
- get isValid(): boolean {
100
- return !!this.blobHandle && this.attachment.isValid;
101
- }
13
+ // Override declarations.
14
+ override attachment: AttachmentImpl;
15
+
16
+ blobHandle?: fb.Blob;
17
+
18
+ static async create(
19
+ attachment: AttachmentImpl,
20
+ transaction: TransactionImpl,
21
+ options?: CreateBlobOptions,
22
+ ): Promise<BlobStreamImpl> {
23
+ return await attachment.client.statusAction(async (status) => {
24
+ const blobId = new Uint8Array(8);
25
+ const bpb = createBpb(options);
26
+
27
+ const blobHandle = await attachment.attachmentHandle!.createBlobAsync(
28
+ status,
29
+ transaction.transactionHandle,
30
+ blobId,
31
+ bpb.length,
32
+ bpb,
33
+ );
34
+
35
+ const blob = new Blob(attachment, blobId);
36
+
37
+ const blobStream = new BlobStreamImpl(blob, attachment);
38
+ blobStream.blobHandle = blobHandle;
39
+ return blobStream;
40
+ });
41
+ }
42
+
43
+ static async open(attachment: AttachmentImpl, transaction: TransactionImpl, blob: Blob): Promise<BlobStreamImpl> {
44
+ return await attachment.client.statusAction(async (status) => {
45
+ const blobStream = new BlobStreamImpl(blob, attachment);
46
+ blobStream.blobHandle = await attachment.attachmentHandle!.openBlobAsync(
47
+ status,
48
+ transaction.transactionHandle,
49
+ blob.id,
50
+ 0,
51
+ undefined,
52
+ );
53
+ return blobStream;
54
+ });
55
+ }
56
+
57
+ protected async internalGetLength(): Promise<number> {
58
+ return await this.attachment.client.statusAction(async (status) => {
59
+ const infoReq = new Uint8Array([blobInfo.totalLength]);
60
+ const infoRet = new Uint8Array(20);
61
+ await this.blobHandle!.getInfoAsync(status, infoReq.byteLength, infoReq, infoRet.byteLength, infoRet);
62
+
63
+ if (infoRet[0] != blobInfo.totalLength) {
64
+ throw new Error('Unrecognized response from Blob::getInfo.');
65
+ }
66
+
67
+ const size = getPortableInteger(infoRet.subarray(1), 2);
68
+
69
+ return getPortableInteger(infoRet.subarray(3), size);
70
+ });
71
+ }
72
+
73
+ protected async internalClose(): Promise<void> {
74
+ await this.attachment.client.statusAction((status) => this.blobHandle!.closeAsync(status));
75
+ this.blobHandle = undefined;
76
+ }
77
+
78
+ protected async internalCancel(): Promise<void> {
79
+ await this.attachment.client.statusAction((status) => this.blobHandle!.cancelAsync(status));
80
+ this.blobHandle = undefined;
81
+ }
82
+
83
+ protected async internalSeek(offset: number, whence?: BlobSeekWhence): Promise<number> {
84
+ return await this.attachment.client.statusAction(async (status) => {
85
+ return await this.blobHandle!.seekAsync(status, whence ?? BlobSeekWhence.START, offset);
86
+ });
87
+ }
88
+
89
+ protected async internalRead(buffer: Buffer): Promise<number> {
90
+ return await this.attachment.client.statusAction(async (status) => {
91
+ const readingBytes = Math.min(buffer.length, MAX_SEGMENT_SIZE);
92
+ const segLength = new Uint32Array(1);
93
+ const result = await this.blobHandle!.getSegmentAsync(status, readingBytes, buffer, segLength);
94
+
95
+ if (result == fb.Status.RESULT_NO_DATA) {
96
+ return -1;
97
+ }
98
+
99
+ return segLength[0];
100
+ });
101
+ }
102
+
103
+ protected async internalWrite(buffer: Buffer): Promise<void> {
104
+ await this.attachment.client.statusAction(async (status) => {
105
+ while (buffer.length > 0) {
106
+ const writingBytes = Math.min(buffer.length, MAX_SEGMENT_SIZE);
107
+ await this.blobHandle!.putSegmentAsync(status, writingBytes, buffer);
108
+ buffer = buffer.slice(writingBytes);
109
+ }
110
+ });
111
+ }
112
+
113
+ get isValid(): boolean {
114
+ return !!this.blobHandle && this.attachment.isValid;
115
+ }
102
116
  }
package/src/lib/client.ts CHANGED
@@ -1,65 +1,58 @@
1
1
  import { AttachmentImpl } from './attachment';
2
2
 
3
- import {
4
- Client,
5
- ConnectOptions,
6
- CreateDatabaseOptions
7
- } from 'node-firebird-driver';
3
+ import { Client, ConnectOptions, CreateDatabaseOptions } from 'node-firebird-driver';
8
4
 
9
5
  import { AbstractClient } from 'node-firebird-driver/dist/lib/impl';
10
6
 
11
7
  import * as fb from 'node-firebird-native-api';
12
8
 
13
-
14
9
  /** Gets the default platform Firebird client library filename. */
15
10
  export { getDefaultLibraryFilename } from 'node-firebird-native-api';
16
11
 
17
12
  /** Creates a client for a given library filename. */
18
13
  export function createNativeClient(library: string): Client {
19
- return new ClientImpl(library);
14
+ return new ClientImpl(library);
20
15
  }
21
16
 
22
-
23
17
  /** Client implementation. */
24
18
  export class ClientImpl extends AbstractClient {
25
- master?: fb.Master;
26
- dispatcher?: fb.Provider;
27
- util?: fb.Util;
28
-
29
- constructor(library: string) {
30
- super();
31
- this.master = fb.getMaster(library);
32
- this.dispatcher = this.master.getDispatcherSync();
33
- this.util = this.master.getUtilInterfaceSync();
34
- }
35
-
36
- async statusAction<T>(action: (status: fb.Status) => Promise<T>): Promise<T> {
37
- const status = this.master!.getStatusSync()!;
38
- try {
39
- return await action(status);
40
- }
41
- finally {
42
- status.disposeSync();
43
- }
44
- }
45
-
46
- /** Connects to a database. */
47
- protected async internalConnect(uri: string, options?: ConnectOptions): Promise<AttachmentImpl> {
48
- return await AttachmentImpl.connect(this, uri, options);
49
- }
50
-
51
- /** Creates a database. */
52
- protected async internalCreateDatabase(uri: string, options?: CreateDatabaseOptions): Promise<AttachmentImpl> {
53
- return await AttachmentImpl.createDatabase(this, uri, options);
54
- }
55
-
56
- /** Disposes this client's resources. */
57
- protected async internalDispose(): Promise<void> {
58
- this.dispatcher!.releaseSync();
59
- fb.disposeMaster(this.master!);
60
-
61
- this.util = undefined;
62
- this.dispatcher = undefined;
63
- this.master = undefined;
64
- }
19
+ master?: fb.Master;
20
+ dispatcher?: fb.Provider;
21
+ util?: fb.Util;
22
+
23
+ constructor(library: string) {
24
+ super();
25
+ this.master = fb.getMaster(library);
26
+ this.dispatcher = this.master.getDispatcherSync();
27
+ this.util = this.master.getUtilInterfaceSync();
28
+ }
29
+
30
+ async statusAction<T>(action: (status: fb.Status) => Promise<T>): Promise<T> {
31
+ const status = this.master!.getStatusSync()!;
32
+ try {
33
+ return await action(status);
34
+ } finally {
35
+ status.disposeSync();
36
+ }
37
+ }
38
+
39
+ /** Connects to a database. */
40
+ protected async internalConnect(uri: string, options?: ConnectOptions): Promise<AttachmentImpl> {
41
+ return await AttachmentImpl.connect(this, uri, options);
42
+ }
43
+
44
+ /** Creates a database. */
45
+ protected async internalCreateDatabase(uri: string, options?: CreateDatabaseOptions): Promise<AttachmentImpl> {
46
+ return await AttachmentImpl.createDatabase(this, uri, options);
47
+ }
48
+
49
+ /** Disposes this client's resources. */
50
+ protected async internalDispose(): Promise<void> {
51
+ this.dispatcher!.releaseSync();
52
+ fb.disposeMaster(this.master!);
53
+
54
+ this.util = undefined;
55
+ this.dispatcher = undefined;
56
+ this.master = undefined;
57
+ }
65
58
  }
package/src/lib/events.ts CHANGED
@@ -4,30 +4,32 @@ import { AbstractEvents } from 'node-firebird-driver/dist/lib/impl';
4
4
 
5
5
  import * as fb from 'node-firebird-native-api';
6
6
 
7
-
8
7
  /** Events implementation. */
9
8
  export class EventsImpl extends AbstractEvents {
10
- // Override declarations.
11
- override attachment: AttachmentImpl;
12
-
13
- eventCallback: fb.EventCallback;
14
-
15
- static async queue(attachment: AttachmentImpl, names: string[], callBack: (counters: [string, number][]) => Promise<void>):
16
- Promise<EventsImpl> {
17
- const events = new EventsImpl(attachment);
18
- const { client } = attachment;
19
-
20
- events.eventCallback = await fb.queueEvent(client.master!, attachment.attachmentHandle!, names, callBack);
21
-
22
- return events;
23
- }
24
-
25
- /** Disposes this events' resources. */
26
- protected async internalCancel(): Promise<void> {
27
- if (this.eventCallback) {
28
- const { eventCallback } = this;
29
- this.eventCallback = undefined!;
30
- await fb.cancelEvent(eventCallback);
31
- }
32
- }
9
+ // Override declarations.
10
+ override attachment: AttachmentImpl;
11
+
12
+ eventCallback: fb.EventCallback;
13
+
14
+ static async queue(
15
+ attachment: AttachmentImpl,
16
+ names: string[],
17
+ callBack: (counters: [string, number][]) => Promise<void>,
18
+ ): Promise<EventsImpl> {
19
+ const events = new EventsImpl(attachment);
20
+ const { client } = attachment;
21
+
22
+ events.eventCallback = await fb.queueEvent(client.master!, attachment.attachmentHandle!, names, callBack);
23
+
24
+ return events;
25
+ }
26
+
27
+ /** Disposes this events' resources. */
28
+ protected async internalCancel(): Promise<void> {
29
+ if (this.eventCallback) {
30
+ const { eventCallback } = this;
31
+ this.eventCallback = undefined!;
32
+ await fb.cancelEvent(eventCallback);
33
+ }
34
+ }
33
35
  }