@prisma/compute 0.0.0-dev.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/LICENSE +158 -0
- package/README.md +50 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/scale-to-zero-control.js +33 -0
- package/dist/scale-to-zero.d.ts +93 -0
- package/dist/scale-to-zero.js +116 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
|
34
|
+
available under the License, as indicated by a copyright notice that is included
|
|
35
|
+
in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work by
|
|
47
|
+
the copyright owner or by an individual or Legal Entity authorized to submit on
|
|
48
|
+
behalf of the copyright owner. For the purposes of this definition, "submitted"
|
|
49
|
+
means any form of electronic, verbal, or written communication sent to the
|
|
50
|
+
Licensor or its representatives, including but not limited to communication on
|
|
51
|
+
electronic mailing lists, source code control systems, and issue tracking
|
|
52
|
+
systems that are managed by, or on behalf of, the Licensor for the purpose of
|
|
53
|
+
discussing and improving the Work, but excluding communication that is
|
|
54
|
+
conspicuously marked or otherwise designated in writing by the copyright owner
|
|
55
|
+
as "Not a Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or Object
|
|
66
|
+
form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
71
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
72
|
+
transfer the Work, where such license applies only to those patent claims
|
|
73
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
74
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work to
|
|
75
|
+
which such Contribution(s) was submitted. If You institute patent litigation
|
|
76
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
77
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
78
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
79
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
80
|
+
such litigation is filed.
|
|
81
|
+
|
|
82
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
83
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
84
|
+
Source or Object form, provided that You meet the following conditions:
|
|
85
|
+
|
|
86
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
87
|
+
this License; and
|
|
88
|
+
|
|
89
|
+
(b) You must cause any modified files to carry prominent notices stating that You
|
|
90
|
+
changed the files; and
|
|
91
|
+
|
|
92
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
93
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
94
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
95
|
+
of the Derivative Works; and
|
|
96
|
+
|
|
97
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
98
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
99
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
100
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
101
|
+
following places: within a NOTICE text file distributed as part of the Derivative
|
|
102
|
+
Works; within the Source form or documentation, if provided along with the
|
|
103
|
+
Derivative Works; or, within a display generated by the Derivative Works, if and
|
|
104
|
+
wherever such third-party notices normally appear. The contents of the NOTICE
|
|
105
|
+
file are for informational purposes only and do not modify the License. You may
|
|
106
|
+
add Your own attribution notices within Derivative Works that You distribute,
|
|
107
|
+
alongside or as an addendum to the NOTICE text from the Work, provided that such
|
|
108
|
+
additional attribution notices cannot be construed as modifying the License.
|
|
109
|
+
|
|
110
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
111
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
112
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
113
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
114
|
+
with the conditions stated in this License.
|
|
115
|
+
|
|
116
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
117
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
118
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
119
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
120
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
121
|
+
executed with Licensor regarding such Contributions.
|
|
122
|
+
|
|
123
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
124
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
125
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
126
|
+
reproducing the content of the NOTICE file.
|
|
127
|
+
|
|
128
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
129
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
130
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
131
|
+
either express or implied, including, without limitation, any warranties or
|
|
132
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
133
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
134
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
135
|
+
associated with Your exercise of permissions under this License.
|
|
136
|
+
|
|
137
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
138
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
139
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
140
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
141
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
142
|
+
arising as a result of this License or out of the use or inability to use the
|
|
143
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
144
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
145
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
146
|
+
damages.
|
|
147
|
+
|
|
148
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
149
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
150
|
+
acceptance of support, warranty, indemnity, or other liability obligations and/or
|
|
151
|
+
rights consistent with this License. However, in accepting such obligations, You
|
|
152
|
+
may act only on Your own behalf and on Your sole responsibility, not on behalf of
|
|
153
|
+
any other Contributor, and only if You agree to indemnify, defend, and hold each
|
|
154
|
+
Contributor harmless for any liability incurred by, or claims asserted against,
|
|
155
|
+
such Contributor by reason of your accepting any such warranty or additional
|
|
156
|
+
liability.
|
|
157
|
+
|
|
158
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Prisma Compute
|
|
2
|
+
|
|
3
|
+
`@prisma/compute` provides runtime utilities for applications deployed to Prisma
|
|
4
|
+
Compute.
|
|
5
|
+
|
|
6
|
+
## Preventing Application Sleep
|
|
7
|
+
|
|
8
|
+
Applications deployed to Prisma Compute can sleep after short periods of inactivity. When an application sleeps, Prisma Compute snapshots its memory and resumes from that snapshot when the next request arrives.
|
|
9
|
+
|
|
10
|
+
This works well for request-driven code, but background work outside the request lifecycle can be interrupted when the application sleeps. Examples include `setTimeout`, `setInterval`, and background `Promise` work.
|
|
11
|
+
|
|
12
|
+
`@prisma/compute` provides two utilities that signal to Prisma Compute that work is still active and the application should stay awake.
|
|
13
|
+
|
|
14
|
+
`waitUntil` keeps the application awake until a `Promise` settles. It returns `void`, so callers should keep using the original promise for result and error handling. Pass an `AbortSignal`, usually from `AbortSignal.timeout(ms)`, as a safety bound if the promise does not settle. `waitUntil` can be called multiple times during a single request.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { waitUntil } from "@prisma/compute";
|
|
18
|
+
|
|
19
|
+
waitUntil(doCriticalWork(), { signal: AbortSignal.timeout(30_000) });
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`ScaleToZeroGuard` is a disposable object that keeps the application awake until the guard is released. Use it for a scoped function or block of background work. `ScaleToZeroGuard` can be created multiple times during a single request and is safe to nest. Pass an `AbortSignal`, usually from `AbortSignal.timeout(ms)`, as a safety bound if release is not reached.
|
|
23
|
+
|
|
24
|
+
Read more about disposables and the `using` keyword in the [MDN resource management guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Resource_management).
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { ScaleToZeroGuard } from "@prisma/compute";
|
|
28
|
+
|
|
29
|
+
async function runsInBackground() {
|
|
30
|
+
// guard is acquired here
|
|
31
|
+
using guard = new ScaleToZeroGuard({ signal: AbortSignal.timeout(30_000) });
|
|
32
|
+
await doCriticalWork();
|
|
33
|
+
} // guard is released here
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
If `using` is not available, call `.release()` manually. Always release the guard in a `finally` block so it is released even if the guarded code throws.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { ScaleToZeroGuard } from "@prisma/compute";
|
|
40
|
+
|
|
41
|
+
async function runsInBackground() {
|
|
42
|
+
const guard = new ScaleToZeroGuard({ signal: AbortSignal.timeout(30_000) });
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
await doCriticalWork();
|
|
46
|
+
} finally {
|
|
47
|
+
guard.release();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
let controlFileState = {
|
|
3
|
+
kind: "uninitialized",
|
|
4
|
+
path: "/uk/libukp/scale_to_zero_disable"
|
|
5
|
+
};
|
|
6
|
+
function writeScaleToZeroSignal(signal) {
|
|
7
|
+
const state = getControlFileState();
|
|
8
|
+
if (state.kind !== "open") return false;
|
|
9
|
+
try {
|
|
10
|
+
fs.writeSync(state.fd, signal === "acquire" ? "+" : "-");
|
|
11
|
+
return true;
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function getControlFileState() {
|
|
17
|
+
if (controlFileState.kind !== "uninitialized") return controlFileState;
|
|
18
|
+
try {
|
|
19
|
+
controlFileState = {
|
|
20
|
+
kind: "open",
|
|
21
|
+
fd: fs.openSync(controlFileState.path, fs.constants.O_WRONLY),
|
|
22
|
+
path: controlFileState.path
|
|
23
|
+
};
|
|
24
|
+
} catch {
|
|
25
|
+
controlFileState = {
|
|
26
|
+
kind: "unavailable",
|
|
27
|
+
path: controlFileState.path
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return controlFileState;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { writeScaleToZeroSignal };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
//#region src/scale-to-zero.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Options for holding a Prisma Compute sleep guard.
|
|
4
|
+
*/
|
|
5
|
+
interface ScaleToZeroGuardOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Signal that releases the guard when aborted.
|
|
8
|
+
*
|
|
9
|
+
* Use `AbortSignal.timeout(ms)` for a time bound, or pass a request or
|
|
10
|
+
* operation signal to tie the guard to caller-owned cancellation. Passing a
|
|
11
|
+
* signal is strongly recommended as a safety bound for dangling guards.
|
|
12
|
+
*/
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Keeps a Prisma Compute application awake for scoped async work.
|
|
17
|
+
*
|
|
18
|
+
* Creating a guard signals the compute runtime to stay awake. Calling
|
|
19
|
+
* {@link ScaleToZeroGuard.release}, leaving a `using` scope, or reaching
|
|
20
|
+
* the configured abort signal releases that signal. Release is idempotent, so
|
|
21
|
+
* manual release and disposal can be combined safely.
|
|
22
|
+
*
|
|
23
|
+
* Pass `signal` whenever possible, usually from `AbortSignal.timeout(ms)`, to
|
|
24
|
+
* bound how long the guard can keep the instance awake if release is not reached.
|
|
25
|
+
*
|
|
26
|
+
* Outside the Prisma Compute runtime, where the sleep control endpoint is
|
|
27
|
+
* unavailable, the guard is a no-op.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { ScaleToZeroGuard } from "@prisma/compute";
|
|
32
|
+
*
|
|
33
|
+
* using guard = new ScaleToZeroGuard({ signal: AbortSignal.timeout(30_000) });
|
|
34
|
+
* await doCriticalWork();
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const guard = new ScaleToZeroGuard();
|
|
40
|
+
* try {
|
|
41
|
+
* await doCriticalWork();
|
|
42
|
+
* } finally {
|
|
43
|
+
* guard.release();
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare class ScaleToZeroGuard implements Disposable {
|
|
48
|
+
#private;
|
|
49
|
+
/**
|
|
50
|
+
* Creates a guard and immediately signals the compute runtime to stay awake.
|
|
51
|
+
*
|
|
52
|
+
* If `signal` is already aborted, no signal is written. If `signal` aborts
|
|
53
|
+
* while the guard is active, the guard releases itself. Passing a signal is
|
|
54
|
+
* recommended as a safety bound if release is not reached.
|
|
55
|
+
*/
|
|
56
|
+
constructor(options?: ScaleToZeroGuardOptions);
|
|
57
|
+
/**
|
|
58
|
+
* Releases the guard's keep-awake signal.
|
|
59
|
+
*
|
|
60
|
+
* This method is idempotent. Calling it multiple times, or calling it before
|
|
61
|
+
* a `using` scope exits, writes at most one release signal.
|
|
62
|
+
*/
|
|
63
|
+
release(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Releases the guard when used with TypeScript's `using` syntax.
|
|
66
|
+
*
|
|
67
|
+
* Most callers should prefer `using` for scoped work and call
|
|
68
|
+
* {@link ScaleToZeroGuard.release} only when release needs to happen before
|
|
69
|
+
* the scope exits.
|
|
70
|
+
*/
|
|
71
|
+
[Symbol.dispose](): void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Keeps a Prisma Compute application awake until a promise settles.
|
|
75
|
+
*
|
|
76
|
+
* The guard is acquired immediately, then released from a `finally` handler on
|
|
77
|
+
* the passed promise. This function returns `void`; callers should keep using
|
|
78
|
+
* the original promise for result and error handling. If `signal` aborts first,
|
|
79
|
+
* only the guard is released; the passed promise continues independently.
|
|
80
|
+
*
|
|
81
|
+
* Pass `signal`, usually from `AbortSignal.timeout(ms)`, to bound guard lifetime
|
|
82
|
+
* even when the promise does not settle.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { waitUntil } from "@prisma/compute";
|
|
87
|
+
*
|
|
88
|
+
* waitUntil(sendWebhook(), { signal: AbortSignal.timeout(10_000) });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
declare function waitUntil(promise: PromiseLike<unknown>, options?: ScaleToZeroGuardOptions): void;
|
|
92
|
+
//#endregion
|
|
93
|
+
export { ScaleToZeroGuard, ScaleToZeroGuardOptions, waitUntil };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { writeScaleToZeroSignal } from "./scale-to-zero-control.js";
|
|
2
|
+
//#region src/scale-to-zero.ts
|
|
3
|
+
/**
|
|
4
|
+
* Keeps a Prisma Compute application awake for scoped async work.
|
|
5
|
+
*
|
|
6
|
+
* Creating a guard signals the compute runtime to stay awake. Calling
|
|
7
|
+
* {@link ScaleToZeroGuard.release}, leaving a `using` scope, or reaching
|
|
8
|
+
* the configured abort signal releases that signal. Release is idempotent, so
|
|
9
|
+
* manual release and disposal can be combined safely.
|
|
10
|
+
*
|
|
11
|
+
* Pass `signal` whenever possible, usually from `AbortSignal.timeout(ms)`, to
|
|
12
|
+
* bound how long the guard can keep the instance awake if release is not reached.
|
|
13
|
+
*
|
|
14
|
+
* Outside the Prisma Compute runtime, where the sleep control endpoint is
|
|
15
|
+
* unavailable, the guard is a no-op.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { ScaleToZeroGuard } from "@prisma/compute";
|
|
20
|
+
*
|
|
21
|
+
* using guard = new ScaleToZeroGuard({ signal: AbortSignal.timeout(30_000) });
|
|
22
|
+
* await doCriticalWork();
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const guard = new ScaleToZeroGuard();
|
|
28
|
+
* try {
|
|
29
|
+
* await doCriticalWork();
|
|
30
|
+
* } finally {
|
|
31
|
+
* guard.release();
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
var ScaleToZeroGuard = class {
|
|
36
|
+
#active;
|
|
37
|
+
#abortSignal;
|
|
38
|
+
#abortListener;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a guard and immediately signals the compute runtime to stay awake.
|
|
41
|
+
*
|
|
42
|
+
* If `signal` is already aborted, no signal is written. If `signal` aborts
|
|
43
|
+
* while the guard is active, the guard releases itself. Passing a signal is
|
|
44
|
+
* recommended as a safety bound if release is not reached.
|
|
45
|
+
*/
|
|
46
|
+
constructor(options = {}) {
|
|
47
|
+
if (options.signal?.aborted) {
|
|
48
|
+
this.#active = false;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.#active = writeScaleToZeroSignal("acquire");
|
|
52
|
+
if (this.#active && options.signal !== void 0) {
|
|
53
|
+
this.#abortSignal = options.signal;
|
|
54
|
+
this.#abortListener = () => {
|
|
55
|
+
this.release();
|
|
56
|
+
};
|
|
57
|
+
options.signal.addEventListener("abort", this.#abortListener, { once: true });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Releases the guard's keep-awake signal.
|
|
62
|
+
*
|
|
63
|
+
* This method is idempotent. Calling it multiple times, or calling it before
|
|
64
|
+
* a `using` scope exits, writes at most one release signal.
|
|
65
|
+
*/
|
|
66
|
+
release() {
|
|
67
|
+
if (!this.#active) return;
|
|
68
|
+
this.#active = false;
|
|
69
|
+
this.#removeAbortListener();
|
|
70
|
+
writeScaleToZeroSignal("release");
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Releases the guard when used with TypeScript's `using` syntax.
|
|
74
|
+
*
|
|
75
|
+
* Most callers should prefer `using` for scoped work and call
|
|
76
|
+
* {@link ScaleToZeroGuard.release} only when release needs to happen before
|
|
77
|
+
* the scope exits.
|
|
78
|
+
*/
|
|
79
|
+
[Symbol.dispose]() {
|
|
80
|
+
this.release();
|
|
81
|
+
}
|
|
82
|
+
#removeAbortListener() {
|
|
83
|
+
if (this.#abortSignal === void 0 || this.#abortListener === void 0) return;
|
|
84
|
+
this.#abortSignal.removeEventListener("abort", this.#abortListener);
|
|
85
|
+
this.#abortSignal = void 0;
|
|
86
|
+
this.#abortListener = void 0;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Keeps a Prisma Compute application awake until a promise settles.
|
|
91
|
+
*
|
|
92
|
+
* The guard is acquired immediately, then released from a `finally` handler on
|
|
93
|
+
* the passed promise. This function returns `void`; callers should keep using
|
|
94
|
+
* the original promise for result and error handling. If `signal` aborts first,
|
|
95
|
+
* only the guard is released; the passed promise continues independently.
|
|
96
|
+
*
|
|
97
|
+
* Pass `signal`, usually from `AbortSignal.timeout(ms)`, to bound guard lifetime
|
|
98
|
+
* even when the promise does not settle.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { waitUntil } from "@prisma/compute";
|
|
103
|
+
*
|
|
104
|
+
* waitUntil(sendWebhook(), { signal: AbortSignal.timeout(10_000) });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
function waitUntil(promise, options) {
|
|
108
|
+
const guard = new ScaleToZeroGuard(options);
|
|
109
|
+
Promise.resolve(promise).finally(() => {
|
|
110
|
+
try {
|
|
111
|
+
guard.release();
|
|
112
|
+
} catch {}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
export { ScaleToZeroGuard, waitUntil };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prisma/compute",
|
|
3
|
+
"version": "0.0.0-dev.0",
|
|
4
|
+
"description": "Utilities for applications running on the Prisma compute runtime.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=22.12.0"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"prisma",
|
|
25
|
+
"compute",
|
|
26
|
+
"runtime"
|
|
27
|
+
],
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/prisma/prisma-cli.git",
|
|
31
|
+
"directory": "packages/compute"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/prisma/prisma-cli#readme",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/prisma/prisma-cli/issues"
|
|
36
|
+
},
|
|
37
|
+
"license": "Apache-2.0",
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsdown",
|
|
40
|
+
"prepack": "pnpm run build",
|
|
41
|
+
"test": "vitest run"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^22.19.19",
|
|
45
|
+
"tsdown": "^0.21.10",
|
|
46
|
+
"typescript": "^6.0.3",
|
|
47
|
+
"vitest": "^4.1.8"
|
|
48
|
+
}
|
|
49
|
+
}
|