@motionscript/headless 0.0.0-stage → 0.1.0-alpha.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/CHANGELOG.md +5 -0
- package/LICENSE +201 -0
- package/README.md +107 -3
- package/dist/deadline.d.ts +13 -0
- package/dist/deadline.d.ts.map +1 -0
- package/dist/deadline.js +39 -0
- package/dist/deadline.js.map +1 -0
- package/dist/errors.d.ts +44 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/frame.d.ts +47 -0
- package/dist/frame.d.ts.map +1 -0
- package/dist/frame.js +60 -0
- package/dist/frame.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/page-bridge.d.ts +53 -0
- package/dist/page-bridge.d.ts.map +1 -0
- package/dist/page-bridge.js +9 -0
- package/dist/page-bridge.js.map +1 -0
- package/dist/page-bundle.d.ts +31 -0
- package/dist/page-bundle.d.ts.map +1 -0
- package/dist/page-bundle.js +142 -0
- package/dist/page-bundle.js.map +1 -0
- package/dist/semaphore.d.ts +29 -0
- package/dist/semaphore.d.ts.map +1 -0
- package/dist/semaphore.js +74 -0
- package/dist/semaphore.js.map +1 -0
- package/dist/server-engine.d.ts +117 -0
- package/dist/server-engine.d.ts.map +1 -0
- package/dist/server-engine.js +293 -0
- package/dist/server-engine.js.map +1 -0
- package/dist/validate.d.ts +38 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +103 -0
- package/dist/validate.js.map +1 -0
- package/package.json +56 -3
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,4 +1,108 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @motionscript/headless
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
Render [Motion Script](https://motionscript.dev) projects to stills and video
|
|
4
|
+
from a Node process, so a render can be a request rather than a person at a
|
|
5
|
+
browser.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { createServerEngine } from '@motionscript/headless';
|
|
9
|
+
|
|
10
|
+
const engine = createServerEngine({ projectRoot: '/srv/projects/promo' });
|
|
11
|
+
|
|
12
|
+
const still = await engine.renderImage({ document, frame: 'last', format: 'jpeg' });
|
|
13
|
+
const video = await engine.renderVideo({ document, scale: 2 });
|
|
14
|
+
|
|
15
|
+
await engine.close();
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
The engine bundles the project entry with esbuild, loads it into a headless
|
|
19
|
+
Chromium page driven by Playwright, and renders there with
|
|
20
|
+
[`@motionscript/browser`](../browser). A server export and a browser preview
|
|
21
|
+
are the same code, `Canvas3D` and audio included.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @motionscript/headless
|
|
27
|
+
npx playwright install --with-deps chromium
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## The project entry
|
|
31
|
+
|
|
32
|
+
The engine renders a project **entry**: a file, by default
|
|
33
|
+
`<projectRoot>/src/project.ts`, that exports what the documents need.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
// src/project.ts
|
|
37
|
+
export const nodes = [PulseRing]; // node classes the documents name
|
|
38
|
+
export const shaders = { ripple: Ripple }; // optional
|
|
39
|
+
export default projectDocument; // optional: rendered when a request brings no document
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Node types are classes, and a class does not survive a process boundary, so the
|
|
43
|
+
entry is bundled into the page once. Documents are JSON and travel with each
|
|
44
|
+
request. The built bundle is cached (by default in the project's
|
|
45
|
+
`node_modules/.cache/motionscript`), keyed on the files esbuild resolved.
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
|
|
49
|
+
- `createServerEngine(options)` returns a `MotionScriptServerEngine`. It
|
|
50
|
+
starts on the first call, or when you call `start()`.
|
|
51
|
+
- `engine.renderImage({ document?, frame?, scale?, format? })` returns
|
|
52
|
+
`{ bytes, frame, totalFrames }`. `frame` is a frame index, `'first'` or
|
|
53
|
+
`'last'` (the default). `format` is `'png'` (the default) or `'jpeg'`.
|
|
54
|
+
- `engine.renderVideo({ document?, scale? })` returns the encoded MP4 bytes.
|
|
55
|
+
- `engine.listTracks()`, `engine.projectName()` and `engine.fps()` describe the
|
|
56
|
+
entry's default project.
|
|
57
|
+
- `engine.close()` shuts the browser down. A closed engine throws `CLOSED`.
|
|
58
|
+
|
|
59
|
+
## Options
|
|
60
|
+
|
|
61
|
+
| Option | Default | |
|
|
62
|
+
| --- | --- | --- |
|
|
63
|
+
| `projectRoot` | `process.cwd()` | Where the entry and assets are looked up. |
|
|
64
|
+
| `entry` | `<projectRoot>/src/project.ts` | The file to bundle. |
|
|
65
|
+
| `assetsDir` | `<projectRoot>/public` | Served at the page's origin root, for the assets a document names. |
|
|
66
|
+
| `serve` | none | Extra files to serve, by URL path. Overrides `assetsDir`. |
|
|
67
|
+
| `manifest` | none | Asset manifest the page resolves `src` values against. Fonts need it: an undeclared family is never fetched. |
|
|
68
|
+
| `softwareRender` | `MS_SOFTWARE_RENDER=1` | Render through SwiftShader instead of a GPU. |
|
|
69
|
+
| `cacheDir` | `<entry dir>/../node_modules/.cache/motionscript` | Where the built bundle is cached. |
|
|
70
|
+
| `onPageMessage` | stderr | Receives every warning and error the page logs. |
|
|
71
|
+
|
|
72
|
+
## Errors
|
|
73
|
+
|
|
74
|
+
Start-up failures are an `EngineError` carrying a `code`, so a handler can map
|
|
75
|
+
them without matching on message text:
|
|
76
|
+
|
|
77
|
+
| Code | Raised when |
|
|
78
|
+
| --- | --- |
|
|
79
|
+
| `PROJECT_NOT_FOUND` | There is no file at the entry path. |
|
|
80
|
+
| `START_FAILED` | The entry failed to bundle or to load in the page. |
|
|
81
|
+
| `BRIDGE_TIMEOUT` | The page never published its bridge. |
|
|
82
|
+
| `BRIDGE_INCOMPATIBLE` | The page bridge is missing a method, which usually means a stale cached bundle. |
|
|
83
|
+
| `CLOSED` | The engine was used after `close()`. |
|
|
84
|
+
|
|
85
|
+
`isEngineError(err)` narrows an unknown error. An error thrown inside the page
|
|
86
|
+
during a render reaches the caller as Playwright reports it.
|
|
87
|
+
|
|
88
|
+
The option parsers are exported for validating a request body at the edge:
|
|
89
|
+
`parseFrameSelector`, `toFrameSpec`, `parseScale`, `parseImageFormat`,
|
|
90
|
+
`parseCodec`, `parseBitrate`, `parseSupersample` and `parseTimeout`. Each
|
|
91
|
+
throws `INVALID_OPTION` with a message naming the value it refused.
|
|
92
|
+
|
|
93
|
+
## Running it in production
|
|
94
|
+
|
|
95
|
+
**Containers need `softwareRender`.** On a host without a GPU, asking for
|
|
96
|
+
hardware acceleration fails to create a WebGL context rather than degrading.
|
|
97
|
+
Set `MS_SOFTWARE_RENDER=1` (or `softwareRender: true`) there, and start from
|
|
98
|
+
Playwright's own image so Chromium's system libraries are present. A GPU frame
|
|
99
|
+
and a SwiftShader frame do not match pixel for pixel, so anything comparing
|
|
100
|
+
renders should fix the mode and record which one made a baseline.
|
|
101
|
+
|
|
102
|
+
**Memory.** Bytes cross the page boundary base64-encoded, because Playwright
|
|
103
|
+
cannot round-trip a `Uint8Array`. A video exists in memory more than once
|
|
104
|
+
around delivery, so size the container against the longest render.
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
Apache-2.0
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Race `work` against a caller's abort signal and a time budget.
|
|
3
|
+
*
|
|
4
|
+
* `work` is deliberately *not* cancelled when it loses: nothing can interrupt
|
|
5
|
+
* a render already running inside a page, so the caller retires the page
|
|
6
|
+
* instead (see `MotionScriptEngine.run`). Its eventual result is still
|
|
7
|
+
* observed here, so a late failure cannot surface as an unhandled rejection
|
|
8
|
+
* and take a server process down with it.
|
|
9
|
+
*
|
|
10
|
+
* A `timeout` of `0` (or less) means no time limit.
|
|
11
|
+
*/
|
|
12
|
+
export declare function withDeadline<T>(work: Promise<T>, signal: AbortSignal | undefined, timeout: number): Promise<T>;
|
|
13
|
+
//# sourceMappingURL=deadline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deadline.d.ts","sourceRoot":"","sources":["../src/deadline.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC1B,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAChB,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,OAAO,EAAE,MAAM,GAChB,OAAO,CAAC,CAAC,CAAC,CAuCZ"}
|
package/dist/deadline.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { EngineError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Race `work` against a caller's abort signal and a time budget.
|
|
4
|
+
*
|
|
5
|
+
* `work` is deliberately *not* cancelled when it loses: nothing can interrupt
|
|
6
|
+
* a render already running inside a page, so the caller retires the page
|
|
7
|
+
* instead (see `MotionScriptEngine.run`). Its eventual result is still
|
|
8
|
+
* observed here, so a late failure cannot surface as an unhandled rejection
|
|
9
|
+
* and take a server process down with it.
|
|
10
|
+
*
|
|
11
|
+
* A `timeout` of `0` (or less) means no time limit.
|
|
12
|
+
*/
|
|
13
|
+
export function withDeadline(work, signal, timeout) {
|
|
14
|
+
if (!signal && timeout <= 0)
|
|
15
|
+
return work;
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
let settled = false;
|
|
18
|
+
const finish = (settleOnce) => {
|
|
19
|
+
if (settled)
|
|
20
|
+
return;
|
|
21
|
+
settled = true;
|
|
22
|
+
if (timer)
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
signal?.removeEventListener('abort', onAbort);
|
|
25
|
+
settleOnce();
|
|
26
|
+
};
|
|
27
|
+
const timer = timeout > 0
|
|
28
|
+
? setTimeout(() => finish(() => reject(new EngineError('TIMEOUT', `The render exceeded its ${timeout} ms budget.`))), timeout)
|
|
29
|
+
: null;
|
|
30
|
+
const onAbort = () => finish(() => reject(new EngineError('ABORTED', 'The job was aborted.', { cause: signal?.reason })));
|
|
31
|
+
if (signal?.aborted) {
|
|
32
|
+
onAbort();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
36
|
+
work.then(value => finish(() => resolve(value)), (err) => finish(() => reject(err)));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=deadline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deadline.js","sourceRoot":"","sources":["../src/deadline.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CACxB,IAAgB,EAChB,MAA+B,EAC/B,OAAe;IAEf,IAAI,CAAC,MAAM,IAAI,OAAO,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAEzC,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACtC,IAAI,OAAO,GAAG,KAAK,CAAC;QAEpB,MAAM,MAAM,GAAG,CAAC,UAAsB,EAAQ,EAAE;YAC5C,IAAI,OAAO;gBAAE,OAAO;YACpB,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,KAAK;gBAAE,YAAY,CAAC,KAAK,CAAC,CAAC;YAC/B,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC9C,UAAU,EAAE,CAAC;QACjB,CAAC,CAAC;QAEF,MAAM,KAAK,GAAG,OAAO,GAAG,CAAC;YACrB,CAAC,CAAC,UAAU,CACR,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,WAAW,CACrC,SAAS,EACT,2BAA2B,OAAO,aAAa,CAClD,CAAC,CAAC,EACH,OAAO,CACV;YACD,CAAC,CAAC,IAAI,CAAC;QAEX,MAAM,OAAO,GAAG,GAAS,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAC3C,IAAI,WAAW,CAAC,SAAS,EAAE,sBAAsB,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAChF,CAAC,CAAC;QAEH,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;YAClB,OAAO,EAAE,CAAC;YACV,OAAO;QACX,CAAC;QACD,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAE3D,IAAI,CAAC,IAAI,CACL,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EACrC,CAAC,GAAY,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAC9C,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC","sourcesContent":["import { EngineError } from './errors.js';\n\n/**\n * Race `work` against a caller's abort signal and a time budget.\n *\n * `work` is deliberately *not* cancelled when it loses: nothing can interrupt\n * a render already running inside a page, so the caller retires the page\n * instead (see `MotionScriptEngine.run`). Its eventual result is still\n * observed here, so a late failure cannot surface as an unhandled rejection\n * and take a server process down with it.\n *\n * A `timeout` of `0` (or less) means no time limit.\n */\nexport function withDeadline<T>(\n work: Promise<T>,\n signal: AbortSignal | undefined,\n timeout: number,\n): Promise<T> {\n if (!signal && timeout <= 0) return work;\n\n return new Promise<T>((resolve, reject) => {\n let settled = false;\n\n const finish = (settleOnce: () => void): void => {\n if (settled) return;\n settled = true;\n if (timer) clearTimeout(timer);\n signal?.removeEventListener('abort', onAbort);\n settleOnce();\n };\n\n const timer = timeout > 0\n ? setTimeout(\n () => finish(() => reject(new EngineError(\n 'TIMEOUT',\n `The render exceeded its ${timeout} ms budget.`,\n ))),\n timeout,\n )\n : null;\n\n const onAbort = (): void => finish(() => reject(\n new EngineError('ABORTED', 'The job was aborted.', { cause: signal?.reason }),\n ));\n\n if (signal?.aborted) {\n onAbort();\n return;\n }\n signal?.addEventListener('abort', onAbort, { once: true });\n\n work.then(\n value => finish(() => resolve(value)),\n (err: unknown) => finish(() => reject(err)),\n );\n });\n}\n"]}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every failure the engine surfaces carries one of these codes, so a service
|
|
3
|
+
* can map it onto a response without pattern-matching on message text.
|
|
4
|
+
*
|
|
5
|
+
* - `PROJECT_NOT_FOUND` — the path is not a Motion Script project (→ 400/404)
|
|
6
|
+
* - `INVALID_OPTION` — a caller-supplied option is out of range (→ 400)
|
|
7
|
+
* - `UNKNOWN_SCENE` — a requested scene name is not in the project (→ 400)
|
|
8
|
+
* - `START_FAILED` — the entry failed to bundle or load (→ 500)
|
|
9
|
+
* - `BRIDGE_TIMEOUT` — the page loaded but never installed the bridge (→ 500)
|
|
10
|
+
* - `BRIDGE_INCOMPATIBLE`— the page bridge lacks a method, e.g. a stale cached bundle (→ 500)
|
|
11
|
+
* - `RENDER_FAILED` — the render itself threw inside the page (→ 500)
|
|
12
|
+
* - `TIMEOUT` — the job exceeded its time budget (→ 504)
|
|
13
|
+
* - `ABORTED` — the caller's `AbortSignal` fired (→ 499)
|
|
14
|
+
* - `CLOSED` — the engine was closed while the job was pending (→ 503)
|
|
15
|
+
*/
|
|
16
|
+
export type EngineErrorCode = 'PROJECT_NOT_FOUND' | 'INVALID_OPTION' | 'UNKNOWN_SCENE' | 'START_FAILED' | 'BRIDGE_TIMEOUT' | 'BRIDGE_INCOMPATIBLE' | 'RENDER_FAILED' | 'TIMEOUT' | 'ABORTED' | 'CLOSED';
|
|
17
|
+
/** An error raised by the engine, tagged with a stable {@link EngineErrorCode}. */
|
|
18
|
+
export declare class EngineError extends Error {
|
|
19
|
+
readonly name = "EngineError";
|
|
20
|
+
readonly code: EngineErrorCode;
|
|
21
|
+
constructor(code: EngineErrorCode, message: string, options?: {
|
|
22
|
+
cause?: unknown;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export declare function isEngineError(err: unknown): err is EngineError;
|
|
26
|
+
/**
|
|
27
|
+
* Strip Playwright's wrapper from an in-page error.
|
|
28
|
+
*
|
|
29
|
+
* A throw inside the page surfaces as `page.evaluate: <real message>` followed
|
|
30
|
+
* by the browser stack. Callers want the actionable first line, not the
|
|
31
|
+
* plumbing, and a service that logs the raw string leaks a browser stack into
|
|
32
|
+
* its own logs for what is usually a bad scene name.
|
|
33
|
+
*/
|
|
34
|
+
export declare function cleanPageErrorMessage(err: unknown): string;
|
|
35
|
+
/**
|
|
36
|
+
* Translate an error thrown by a page call into an {@link EngineError}.
|
|
37
|
+
*
|
|
38
|
+
* The bridge validates scene names itself and throws `Unknown scene(s): …`
|
|
39
|
+
* (see the headless bridge). That is caller error, not engine
|
|
40
|
+
* failure, so it gets its own code — the distinction is exactly what lets a
|
|
41
|
+
* service answer 400 instead of 500.
|
|
42
|
+
*/
|
|
43
|
+
export declare function toRenderError(err: unknown): EngineError;
|
|
44
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,eAAe,GACrB,mBAAmB,GACnB,gBAAgB,GAChB,eAAe,GACf,cAAc,GACd,gBAAgB,GAChB,qBAAqB,GACrB,eAAe,GACf,SAAS,GACT,SAAS,GACT,QAAQ,CAAC;AAEf,mFAAmF;AACnF,qBAAa,WAAY,SAAQ,KAAK;IAClC,SAAkB,IAAI,iBAAiB;IACvC,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;gBAEnB,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAIpF;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,WAAW,CAE9D;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAS1D;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,WAAW,CAOvD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** An error raised by the engine, tagged with a stable {@link EngineErrorCode}. */
|
|
2
|
+
export class EngineError extends Error {
|
|
3
|
+
name = 'EngineError';
|
|
4
|
+
code;
|
|
5
|
+
constructor(code, message, options) {
|
|
6
|
+
super(message, options);
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function isEngineError(err) {
|
|
11
|
+
return err instanceof EngineError;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Strip Playwright's wrapper from an in-page error.
|
|
15
|
+
*
|
|
16
|
+
* A throw inside the page surfaces as `page.evaluate: <real message>` followed
|
|
17
|
+
* by the browser stack. Callers want the actionable first line, not the
|
|
18
|
+
* plumbing, and a service that logs the raw string leaks a browser stack into
|
|
19
|
+
* its own logs for what is usually a bad scene name.
|
|
20
|
+
*/
|
|
21
|
+
export function cleanPageErrorMessage(err) {
|
|
22
|
+
const raw = err instanceof Error ? err.message : String(err);
|
|
23
|
+
return raw
|
|
24
|
+
.split('\n')[0]
|
|
25
|
+
.replace(/^(page\.evaluate|page\.goto|page\.waitForSelector|locator\.\w+):\s*/, '')
|
|
26
|
+
// The page-side message arrives as "Error: <msg>"; drop the redundant
|
|
27
|
+
// prefix so callers can add their own label.
|
|
28
|
+
.replace(/^Error:\s*/, '')
|
|
29
|
+
.trim();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Translate an error thrown by a page call into an {@link EngineError}.
|
|
33
|
+
*
|
|
34
|
+
* The bridge validates scene names itself and throws `Unknown scene(s): …`
|
|
35
|
+
* (see the headless bridge). That is caller error, not engine
|
|
36
|
+
* failure, so it gets its own code — the distinction is exactly what lets a
|
|
37
|
+
* service answer 400 instead of 500.
|
|
38
|
+
*/
|
|
39
|
+
export function toRenderError(err) {
|
|
40
|
+
if (isEngineError(err))
|
|
41
|
+
return err;
|
|
42
|
+
const message = cleanPageErrorMessage(err);
|
|
43
|
+
if (/^Unknown scene\(s\):/i.test(message)) {
|
|
44
|
+
return new EngineError('UNKNOWN_SCENE', message, { cause: err });
|
|
45
|
+
}
|
|
46
|
+
return new EngineError('RENDER_FAILED', message || 'The render failed.', { cause: err });
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AA2BA,mFAAmF;AACnF,MAAM,OAAO,WAAY,SAAQ,KAAK;IAChB,IAAI,GAAG,aAAa,CAAC;IAC9B,IAAI,CAAkB;IAE/B,YAAY,IAAqB,EAAE,OAAe,EAAE,OAA6B;QAC7E,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;CACJ;AAED,MAAM,UAAU,aAAa,CAAC,GAAY;IACtC,OAAO,GAAG,YAAY,WAAW,CAAC;AACtC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,GAAY;IAC9C,MAAM,GAAG,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7D,OAAO,GAAG;SACL,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;SACd,OAAO,CAAC,qEAAqE,EAAE,EAAE,CAAC;QACnF,sEAAsE;QACtE,6CAA6C;SAC5C,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC;SACzB,IAAI,EAAE,CAAC;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,GAAY;IACtC,IAAI,aAAa,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC;IACnC,MAAM,OAAO,GAAG,qBAAqB,CAAC,GAAG,CAAC,CAAC;IAC3C,IAAI,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACxC,OAAO,IAAI,WAAW,CAAC,eAAe,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,IAAI,WAAW,CAAC,eAAe,EAAE,OAAO,IAAI,oBAAoB,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;AAC7F,CAAC","sourcesContent":["/**\n * Every failure the engine surfaces carries one of these codes, so a service\n * can map it onto a response without pattern-matching on message text.\n *\n * - `PROJECT_NOT_FOUND` — the path is not a Motion Script project (→ 400/404)\n * - `INVALID_OPTION` — a caller-supplied option is out of range (→ 400)\n * - `UNKNOWN_SCENE` — a requested scene name is not in the project (→ 400)\n * - `START_FAILED` — the entry failed to bundle or load (→ 500)\n * - `BRIDGE_TIMEOUT` — the page loaded but never installed the bridge (→ 500)\n * - `BRIDGE_INCOMPATIBLE`— the page bridge lacks a method, e.g. a stale cached bundle (→ 500)\n * - `RENDER_FAILED` — the render itself threw inside the page (→ 500)\n * - `TIMEOUT` — the job exceeded its time budget (→ 504)\n * - `ABORTED` — the caller's `AbortSignal` fired (→ 499)\n * - `CLOSED` — the engine was closed while the job was pending (→ 503)\n */\nexport type EngineErrorCode =\n | 'PROJECT_NOT_FOUND'\n | 'INVALID_OPTION'\n | 'UNKNOWN_SCENE'\n | 'START_FAILED'\n | 'BRIDGE_TIMEOUT'\n | 'BRIDGE_INCOMPATIBLE'\n | 'RENDER_FAILED'\n | 'TIMEOUT'\n | 'ABORTED'\n | 'CLOSED';\n\n/** An error raised by the engine, tagged with a stable {@link EngineErrorCode}. */\nexport class EngineError extends Error {\n override readonly name = 'EngineError';\n readonly code: EngineErrorCode;\n\n constructor(code: EngineErrorCode, message: string, options?: { cause?: unknown }) {\n super(message, options);\n this.code = code;\n }\n}\n\nexport function isEngineError(err: unknown): err is EngineError {\n return err instanceof EngineError;\n}\n\n/**\n * Strip Playwright's wrapper from an in-page error.\n *\n * A throw inside the page surfaces as `page.evaluate: <real message>` followed\n * by the browser stack. Callers want the actionable first line, not the\n * plumbing, and a service that logs the raw string leaks a browser stack into\n * its own logs for what is usually a bad scene name.\n */\nexport function cleanPageErrorMessage(err: unknown): string {\n const raw = err instanceof Error ? err.message : String(err);\n return raw\n .split('\\n')[0]\n .replace(/^(page\\.evaluate|page\\.goto|page\\.waitForSelector|locator\\.\\w+):\\s*/, '')\n // The page-side message arrives as \"Error: <msg>\"; drop the redundant\n // prefix so callers can add their own label.\n .replace(/^Error:\\s*/, '')\n .trim();\n}\n\n/**\n * Translate an error thrown by a page call into an {@link EngineError}.\n *\n * The bridge validates scene names itself and throws `Unknown scene(s): …`\n * (see the headless bridge). That is caller error, not engine\n * failure, so it gets its own code — the distinction is exactly what lets a\n * service answer 400 instead of 500.\n */\nexport function toRenderError(err: unknown): EngineError {\n if (isEngineError(err)) return err;\n const message = cleanPageErrorMessage(err);\n if (/^Unknown scene\\(s\\):/i.test(message)) {\n return new EngineError('UNKNOWN_SCENE', message, { cause: err });\n }\n return new EngineError('RENDER_FAILED', message || 'The render failed.', { cause: err });\n}\n"]}
|
package/dist/frame.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which frame a still addresses.
|
|
3
|
+
*
|
|
4
|
+
* Declared here rather than imported from `@motionscript/skia-render`, which
|
|
5
|
+
* this package no longer depends on: the renderer lives in the page now, and
|
|
6
|
+
* Node holds only the driver. It is the same three cases the bridge accepts.
|
|
7
|
+
*/
|
|
8
|
+
export type FrameSpec = {
|
|
9
|
+
kind: 'frame';
|
|
10
|
+
frame: number;
|
|
11
|
+
} | {
|
|
12
|
+
kind: 'first';
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'last';
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* How a caller names the frame to capture.
|
|
18
|
+
*
|
|
19
|
+
* - `'first'` / `'last'` — that end of the timeline.
|
|
20
|
+
* - a `number` — a **frame index**, always. Whole numbers only; a fractional
|
|
21
|
+
* number is rejected rather than silently reinterpreted as seconds, because
|
|
22
|
+
* a library caller computing `duration * 0.5` deserves an error rather than
|
|
23
|
+
* a different unit.
|
|
24
|
+
* - `{ frame }` / `{ seconds }` — the same two units, said explicitly.
|
|
25
|
+
* - a `string` — free text from a CLI flag, an HTTP query param or a job
|
|
26
|
+
* record, parsed by {@link parseFrameSelector}: `'42'` is a frame, `'2.5'`
|
|
27
|
+
* and `'2.5s'` are times, `'first'`/`'last'` are the ends.
|
|
28
|
+
*/
|
|
29
|
+
export type FrameSelector = number | 'first' | 'last' | string | {
|
|
30
|
+
frame: number;
|
|
31
|
+
} | {
|
|
32
|
+
seconds: number;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* A validated selector, before `fps` is known. Times stay in seconds here so a
|
|
36
|
+
* caller can reject a malformed selector *before* paying for a browser
|
|
37
|
+
* start-up, and convert once the project's frame rate has been read.
|
|
38
|
+
*/
|
|
39
|
+
export type ParsedFrame = FrameSpec | {
|
|
40
|
+
kind: 'time';
|
|
41
|
+
seconds: number;
|
|
42
|
+
};
|
|
43
|
+
/** Validate a {@link FrameSelector} into a {@link ParsedFrame}. Throws `INVALID_OPTION`. */
|
|
44
|
+
export declare function parseFrameSelector(selector: FrameSelector): ParsedFrame;
|
|
45
|
+
/** Resolve a {@link ParsedFrame} to a concrete {@link FrameSpec}, converting any time via `fps`. */
|
|
46
|
+
export declare function toFrameSpec(parsed: ParsedFrame, fps: number): FrameSpec;
|
|
47
|
+
//# sourceMappingURL=frame.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frame.d.ts","sourceRoot":"","sources":["../src/frame.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,GACf;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAChC;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GACjB;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvB;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,aAAa,GACnB,MAAM,GACN,OAAO,GACP,MAAM,GACN,MAAM,GACN;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,GACjB;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1B;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAUxE,4FAA4F;AAC5F,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,aAAa,GAAG,WAAW,CA8CvE;AAED,oGAAoG;AACpG,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAGvE"}
|
package/dist/frame.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { EngineError } from './errors.js';
|
|
2
|
+
function invalid(raw) {
|
|
3
|
+
return new EngineError('INVALID_OPTION', `Invalid frame selector: ${JSON.stringify(raw)} ` +
|
|
4
|
+
`(expected a frame number, a time like "2.5s", "first" or "last").`);
|
|
5
|
+
}
|
|
6
|
+
/** Validate a {@link FrameSelector} into a {@link ParsedFrame}. Throws `INVALID_OPTION`. */
|
|
7
|
+
export function parseFrameSelector(selector) {
|
|
8
|
+
if (typeof selector === 'number') {
|
|
9
|
+
if (!Number.isInteger(selector) || selector < 0) {
|
|
10
|
+
throw new EngineError('INVALID_OPTION', `Invalid frame index: ${selector}. A number selector is a whole frame index — ` +
|
|
11
|
+
`pass { seconds: ${selector} } (or "${selector}s") for a time.`);
|
|
12
|
+
}
|
|
13
|
+
return { kind: 'frame', frame: selector };
|
|
14
|
+
}
|
|
15
|
+
if (typeof selector === 'object' && selector !== null) {
|
|
16
|
+
if ('frame' in selector) {
|
|
17
|
+
const { frame } = selector;
|
|
18
|
+
if (!Number.isInteger(frame) || frame < 0)
|
|
19
|
+
throw invalid(selector);
|
|
20
|
+
return { kind: 'frame', frame };
|
|
21
|
+
}
|
|
22
|
+
if ('seconds' in selector) {
|
|
23
|
+
const { seconds } = selector;
|
|
24
|
+
if (!Number.isFinite(seconds) || seconds < 0)
|
|
25
|
+
throw invalid(selector);
|
|
26
|
+
return { kind: 'time', seconds };
|
|
27
|
+
}
|
|
28
|
+
throw invalid(selector);
|
|
29
|
+
}
|
|
30
|
+
if (typeof selector !== 'string')
|
|
31
|
+
throw invalid(selector);
|
|
32
|
+
const value = selector.trim().toLowerCase();
|
|
33
|
+
if (value === '')
|
|
34
|
+
throw invalid(selector);
|
|
35
|
+
if (value === 'first')
|
|
36
|
+
return { kind: 'first' };
|
|
37
|
+
if (value === 'last')
|
|
38
|
+
return { kind: 'last' };
|
|
39
|
+
// An explicit `s` suffix is always a time ("2.5s", "3s").
|
|
40
|
+
if (value.endsWith('s')) {
|
|
41
|
+
const seconds = Number(value.slice(0, -1));
|
|
42
|
+
if (!Number.isFinite(seconds) || seconds < 0)
|
|
43
|
+
throw invalid(selector);
|
|
44
|
+
return { kind: 'time', seconds };
|
|
45
|
+
}
|
|
46
|
+
const num = Number(value);
|
|
47
|
+
if (!Number.isFinite(num) || num < 0)
|
|
48
|
+
throw invalid(selector);
|
|
49
|
+
// In text, the integer-vs-decimal split is what disambiguates the unit:
|
|
50
|
+
// frame indices are whole numbers, times are written with a decimal or `s`.
|
|
51
|
+
// (A typed `number` selector has no such ambiguity and is always a frame.)
|
|
52
|
+
return Number.isInteger(num) ? { kind: 'frame', frame: num } : { kind: 'time', seconds: num };
|
|
53
|
+
}
|
|
54
|
+
/** Resolve a {@link ParsedFrame} to a concrete {@link FrameSpec}, converting any time via `fps`. */
|
|
55
|
+
export function toFrameSpec(parsed, fps) {
|
|
56
|
+
if (parsed.kind === 'time')
|
|
57
|
+
return { kind: 'frame', frame: Math.round(parsed.seconds * fps) };
|
|
58
|
+
return parsed;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=frame.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frame.js","sourceRoot":"","sources":["../src/frame.ts"],"names":[],"mappings":"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","sourcesContent":["import { EngineError } from './errors.js';\n\n/**\n * Which frame a still addresses.\n *\n * Declared here rather than imported from `@motionscript/skia-render`, which\n * this package no longer depends on: the renderer lives in the page now, and\n * Node holds only the driver. It is the same three cases the bridge accepts.\n */\nexport type FrameSpec =\n | { kind: 'frame'; frame: number }\n | { kind: 'first' }\n | { kind: 'last' };\n\n/**\n * How a caller names the frame to capture.\n *\n * - `'first'` / `'last'` — that end of the timeline.\n * - a `number` — a **frame index**, always. Whole numbers only; a fractional\n * number is rejected rather than silently reinterpreted as seconds, because\n * a library caller computing `duration * 0.5` deserves an error rather than\n * a different unit.\n * - `{ frame }` / `{ seconds }` — the same two units, said explicitly.\n * - a `string` — free text from a CLI flag, an HTTP query param or a job\n * record, parsed by {@link parseFrameSelector}: `'42'` is a frame, `'2.5'`\n * and `'2.5s'` are times, `'first'`/`'last'` are the ends.\n */\nexport type FrameSelector =\n | number\n | 'first'\n | 'last'\n | string\n | { frame: number }\n | { seconds: number };\n\n/**\n * A validated selector, before `fps` is known. Times stay in seconds here so a\n * caller can reject a malformed selector *before* paying for a browser\n * start-up, and convert once the project's frame rate has been read.\n */\nexport type ParsedFrame = FrameSpec | { kind: 'time'; seconds: number };\n\nfunction invalid(raw: unknown): EngineError {\n return new EngineError(\n 'INVALID_OPTION',\n `Invalid frame selector: ${JSON.stringify(raw)} ` +\n `(expected a frame number, a time like \"2.5s\", \"first\" or \"last\").`,\n );\n}\n\n/** Validate a {@link FrameSelector} into a {@link ParsedFrame}. Throws `INVALID_OPTION`. */\nexport function parseFrameSelector(selector: FrameSelector): ParsedFrame {\n if (typeof selector === 'number') {\n if (!Number.isInteger(selector) || selector < 0) {\n throw new EngineError(\n 'INVALID_OPTION',\n `Invalid frame index: ${selector}. A number selector is a whole frame index — ` +\n `pass { seconds: ${selector} } (or \"${selector}s\") for a time.`,\n );\n }\n return { kind: 'frame', frame: selector };\n }\n\n if (typeof selector === 'object' && selector !== null) {\n if ('frame' in selector) {\n const { frame } = selector;\n if (!Number.isInteger(frame) || frame < 0) throw invalid(selector);\n return { kind: 'frame', frame };\n }\n if ('seconds' in selector) {\n const { seconds } = selector;\n if (!Number.isFinite(seconds) || seconds < 0) throw invalid(selector);\n return { kind: 'time', seconds };\n }\n throw invalid(selector);\n }\n\n if (typeof selector !== 'string') throw invalid(selector);\n\n const value = selector.trim().toLowerCase();\n if (value === '') throw invalid(selector);\n if (value === 'first') return { kind: 'first' };\n if (value === 'last') return { kind: 'last' };\n\n // An explicit `s` suffix is always a time (\"2.5s\", \"3s\").\n if (value.endsWith('s')) {\n const seconds = Number(value.slice(0, -1));\n if (!Number.isFinite(seconds) || seconds < 0) throw invalid(selector);\n return { kind: 'time', seconds };\n }\n\n const num = Number(value);\n if (!Number.isFinite(num) || num < 0) throw invalid(selector);\n // In text, the integer-vs-decimal split is what disambiguates the unit:\n // frame indices are whole numbers, times are written with a decimal or `s`.\n // (A typed `number` selector has no such ambiguity and is always a frame.)\n return Number.isInteger(num) ? { kind: 'frame', frame: num } : { kind: 'time', seconds: num };\n}\n\n/** Resolve a {@link ParsedFrame} to a concrete {@link FrameSpec}, converting any time via `fps`. */\nexport function toFrameSpec(parsed: ParsedFrame, fps: number): FrameSpec {\n if (parsed.kind === 'time') return { kind: 'frame', frame: Math.round(parsed.seconds * fps) };\n return parsed;\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { MotionScriptServerEngine, createServerEngine } from './server-engine.js';
|
|
2
|
+
export type { ServerEngineOptions, ServerImageOptions, ServerImage } from './server-engine.js';
|
|
3
|
+
export { bundlePage } from './page-bundle.js';
|
|
4
|
+
export type { BundleOptions, BuiltBundle } from './page-bundle.js';
|
|
5
|
+
export type { Bridge, ScreenshotRequest, ScreenshotResult, ExportRequest } from './page-bridge.js';
|
|
6
|
+
export { EngineError, isEngineError, type EngineErrorCode, } from './errors.js';
|
|
7
|
+
export { parseFrameSelector, toFrameSpec, type FrameSelector, type FrameSpec, type ParsedFrame, } from './frame.js';
|
|
8
|
+
export { parseBitrate, parseCodec, parseImageFormat, parseScale, parseSupersample, parseTimeout, type ResolvedImageFormat, type VideoCodec, type ImageFormat, } from './validate.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAmBA,OAAO,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAClF,YAAY,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE/F,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEnE,YAAY,EAAE,MAAM,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEnG,OAAO,EACH,WAAW,EACX,aAAa,EACb,KAAK,eAAe,GACvB,MAAM,aAAa,CAAC;AAErB,OAAO,EACH,kBAAkB,EAClB,WAAW,EACX,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,WAAW,GACnB,MAAM,YAAY,CAAC;AAKpB,OAAO,EACH,YAAY,EACZ,UAAU,EACV,gBAAgB,EAChB,UAAU,EACV,gBAAgB,EAChB,YAAY,EACZ,KAAK,mBAAmB,EACxB,KAAK,UAAU,EACf,KAAK,WAAW,GACnB,MAAM,eAAe,CAAC"}
|