bare-kit 0.1.0__tar.gz
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.
- bare_kit-0.1.0/.gitattributes +1 -0
- bare_kit-0.1.0/.github/workflows/integrate.yml +54 -0
- bare_kit-0.1.0/.github/workflows/publish.yml +66 -0
- bare_kit-0.1.0/.gitignore +10 -0
- bare_kit-0.1.0/.prettierrc +1 -0
- bare_kit-0.1.0/CMakeLists.txt +18 -0
- bare_kit-0.1.0/LICENSE +201 -0
- bare_kit-0.1.0/NOTICE +13 -0
- bare_kit-0.1.0/PKG-INFO +56 -0
- bare_kit-0.1.0/README.md +39 -0
- bare_kit-0.1.0/hatch_build.py +49 -0
- bare_kit-0.1.0/package-lock.json +70 -0
- bare_kit-0.1.0/package.json +10 -0
- bare_kit-0.1.0/pyproject.toml +46 -0
- bare_kit-0.1.0/src/bare_kit/__init__.py +11 -0
- bare_kit-0.1.0/src/bare_kit/_host.py +25 -0
- bare_kit-0.1.0/src/bare_kit/_native.py +104 -0
- bare_kit-0.1.0/src/bare_kit/py.typed +0 -0
- bare_kit-0.1.0/src/bare_kit/worklet.py +403 -0
- bare_kit-0.1.0/tests/fixtures/argv.js +1 -0
- bare_kit-0.1.0/tests/fixtures/collect.js +5 -0
- bare_kit-0.1.0/tests/fixtures/echo.js +1 -0
- bare_kit-0.1.0/tests/fixtures/exit.js +1 -0
- bare_kit-0.1.0/tests/fixtures/lifecycle.js +3 -0
- bare_kit-0.1.0/tests/fixtures/sleep.js +1 -0
- bare_kit-0.1.0/tests/test_worklet.py +109 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* text=auto eol=lf
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
name: Integrate
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
pull_request:
|
|
7
|
+
branches:
|
|
8
|
+
- main
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
jobs:
|
|
12
|
+
security:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
name: Security
|
|
15
|
+
steps:
|
|
16
|
+
- uses: holepunchto/actions/security-base@v1
|
|
17
|
+
lint:
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
name: Lint
|
|
20
|
+
steps:
|
|
21
|
+
- uses: holepunchto/actions/python-base@v1
|
|
22
|
+
- run: ruff format --check .
|
|
23
|
+
- run: ruff check .
|
|
24
|
+
test:
|
|
25
|
+
strategy:
|
|
26
|
+
matrix:
|
|
27
|
+
include:
|
|
28
|
+
- os: ubuntu-22.04
|
|
29
|
+
platform: linux
|
|
30
|
+
arch: x64
|
|
31
|
+
- os: ubuntu-22.04-arm
|
|
32
|
+
platform: linux
|
|
33
|
+
arch: arm64
|
|
34
|
+
- os: macos-15
|
|
35
|
+
platform: darwin
|
|
36
|
+
arch: arm64
|
|
37
|
+
- os: windows-2022
|
|
38
|
+
platform: win32
|
|
39
|
+
arch: x64
|
|
40
|
+
- os: windows-11-arm
|
|
41
|
+
platform: win32
|
|
42
|
+
arch: arm64
|
|
43
|
+
runs-on: ${{ matrix.os }}
|
|
44
|
+
name: Test / ${{ matrix.platform }}-${{ matrix.arch }}
|
|
45
|
+
steps:
|
|
46
|
+
- uses: holepunchto/actions/python-base@v1
|
|
47
|
+
- uses: holepunchto/actions/setup-node@v1
|
|
48
|
+
- uses: holepunchto/actions/install-npm-dependencies@v1
|
|
49
|
+
- uses: holepunchto/actions/compile-prebuilds@v1
|
|
50
|
+
with:
|
|
51
|
+
platform: ${{ matrix.platform }}
|
|
52
|
+
arch: ${{ matrix.arch }}
|
|
53
|
+
prefix: src/bare_kit/prebuilds
|
|
54
|
+
- run: pytest
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
tags:
|
|
5
|
+
- v*
|
|
6
|
+
permissions:
|
|
7
|
+
contents: read
|
|
8
|
+
jobs:
|
|
9
|
+
wheel:
|
|
10
|
+
strategy:
|
|
11
|
+
matrix:
|
|
12
|
+
include:
|
|
13
|
+
- os: ubuntu-22.04
|
|
14
|
+
platform: linux
|
|
15
|
+
arch: x64
|
|
16
|
+
- os: ubuntu-22.04-arm
|
|
17
|
+
platform: linux
|
|
18
|
+
arch: arm64
|
|
19
|
+
- os: macos-15
|
|
20
|
+
platform: darwin
|
|
21
|
+
arch: x64
|
|
22
|
+
- os: macos-15
|
|
23
|
+
platform: darwin
|
|
24
|
+
arch: arm64
|
|
25
|
+
- os: windows-2022
|
|
26
|
+
platform: win32
|
|
27
|
+
arch: x64
|
|
28
|
+
- os: windows-11-arm
|
|
29
|
+
platform: win32
|
|
30
|
+
arch: arm64
|
|
31
|
+
runs-on: ${{ matrix.os }}
|
|
32
|
+
name: Wheel / ${{ matrix.platform }}-${{ matrix.arch }}
|
|
33
|
+
steps:
|
|
34
|
+
- uses: holepunchto/actions/python-base@v1
|
|
35
|
+
- uses: holepunchto/actions/install-pip-dependencies@v1
|
|
36
|
+
with:
|
|
37
|
+
packages: build==1.5.0
|
|
38
|
+
- uses: holepunchto/actions/setup-node@v1
|
|
39
|
+
- uses: holepunchto/actions/install-npm-dependencies@v1
|
|
40
|
+
- uses: holepunchto/actions/compile-prebuilds@v1
|
|
41
|
+
with:
|
|
42
|
+
platform: ${{ matrix.platform }}
|
|
43
|
+
arch: ${{ matrix.arch }}
|
|
44
|
+
prefix: src/bare_kit/prebuilds
|
|
45
|
+
- run: python -m build --wheel
|
|
46
|
+
env:
|
|
47
|
+
BARE_KIT_HOST: ${{ matrix.platform }}-${{ matrix.arch }}
|
|
48
|
+
- uses: holepunchto/actions/upload-wheels@v1
|
|
49
|
+
with:
|
|
50
|
+
platform: ${{ matrix.platform }}
|
|
51
|
+
arch: ${{ matrix.arch }}
|
|
52
|
+
publish:
|
|
53
|
+
runs-on: ubuntu-latest
|
|
54
|
+
permissions:
|
|
55
|
+
id-token: write
|
|
56
|
+
contents: write
|
|
57
|
+
environment:
|
|
58
|
+
name: pypi
|
|
59
|
+
needs: wheel
|
|
60
|
+
name: Publish
|
|
61
|
+
steps:
|
|
62
|
+
- uses: holepunchto/actions/python-base@v1
|
|
63
|
+
- uses: holepunchto/actions/download-wheels@v1
|
|
64
|
+
- uses: holepunchto/actions/publish-pypi@v1
|
|
65
|
+
with:
|
|
66
|
+
build: sdist
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"prettier-config-holepunch"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 4.0)
|
|
2
|
+
|
|
3
|
+
find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
|
|
4
|
+
find_package(cmake-fetch REQUIRED PATHS node_modules/cmake-fetch)
|
|
5
|
+
|
|
6
|
+
project(bare_kit_python C)
|
|
7
|
+
|
|
8
|
+
fetch_package("github:holepunchto/bare-kit@3.0.1")
|
|
9
|
+
|
|
10
|
+
add_custom_target(bare_kit_python ALL DEPENDS bare::kit::shared)
|
|
11
|
+
|
|
12
|
+
bare_target(target)
|
|
13
|
+
|
|
14
|
+
install(
|
|
15
|
+
TARGETS bare_kit_shared
|
|
16
|
+
LIBRARY DESTINATION ${target}
|
|
17
|
+
RUNTIME DESTINATION ${target}
|
|
18
|
+
)
|
bare_kit-0.1.0/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.
|
bare_kit-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2026 Holepunch Inc
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
bare_kit-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: bare-kit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python bindings for Bare Kit
|
|
5
|
+
Project-URL: Homepage, https://github.com/holepunchto/bare-kit-python
|
|
6
|
+
Project-URL: Repository, https://github.com/holepunchto/bare-kit-python
|
|
7
|
+
Project-URL: Issues, https://github.com/holepunchto/bare-kit-python/issues
|
|
8
|
+
Author: Holepunch
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Provides-Extra: dev
|
|
14
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
15
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# bare-kit-python
|
|
19
|
+
|
|
20
|
+
Python bindings for [Bare Kit](https://github.com/holepunchto/bare-kit), which runs Bare in a thread of its own within the application, known as a worklet, with an IPC to it exposed as `Bare.IPC` inside the worklet.
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
pip install bare-kit
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import asyncio
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
|
|
32
|
+
from bare_kit import start
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
async def main():
|
|
36
|
+
async with await start(Path(__file__).parent / "app.bundle") as worklet:
|
|
37
|
+
await worklet.write(b"Hello world")
|
|
38
|
+
|
|
39
|
+
async for data in worklet:
|
|
40
|
+
print(data.decode())
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
asyncio.run(main())
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Where `app.bundle` is a Bare bundle using `Bare.IPC`:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
Bare.IPC.on('data', (data) => Bare.IPC.end(data))
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Closing a worklet terminates it, so keep it open for as long as it should run.
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
Apache-2.0
|
bare_kit-0.1.0/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# bare-kit-python
|
|
2
|
+
|
|
3
|
+
Python bindings for [Bare Kit](https://github.com/holepunchto/bare-kit), which runs Bare in a thread of its own within the application, known as a worklet, with an IPC to it exposed as `Bare.IPC` inside the worklet.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
pip install bare-kit
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
import asyncio
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from bare_kit import start
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def main():
|
|
19
|
+
async with await start(Path(__file__).parent / "app.bundle") as worklet:
|
|
20
|
+
await worklet.write(b"Hello world")
|
|
21
|
+
|
|
22
|
+
async for data in worklet:
|
|
23
|
+
print(data.decode())
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
asyncio.run(main())
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Where `app.bundle` is a Bare bundle using `Bare.IPC`:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
Bare.IPC.on('data', (data) => Bare.IPC.end(data))
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Closing a worklet terminates it, so keep it open for as long as it should run.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
Apache-2.0
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import runpy
|
|
3
|
+
|
|
4
|
+
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
|
|
5
|
+
|
|
6
|
+
# The oldest platforms the prebuilds run on: they are linked against glibc 2.34
|
|
7
|
+
# and target macOS 13.
|
|
8
|
+
TAGS = {
|
|
9
|
+
"darwin-arm64": "macosx_13_0_arm64",
|
|
10
|
+
"darwin-x64": "macosx_13_0_x86_64",
|
|
11
|
+
"linux-arm64": "manylinux_2_34_aarch64",
|
|
12
|
+
"linux-x64": "manylinux_2_34_x86_64",
|
|
13
|
+
"win32-arm64": "win_arm64",
|
|
14
|
+
"win32-x64": "win_amd64",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class CustomBuildHook(BuildHookInterface):
|
|
19
|
+
"""Include the prebuild for one host in the wheel and tag it for that host.
|
|
20
|
+
|
|
21
|
+
The host is taken from ``BARE_KIT_HOST`` and defaults to the platform the
|
|
22
|
+
wheel is built on.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def initialize(self, version, build_data):
|
|
26
|
+
if self.target_name != "wheel" or version == "editable":
|
|
27
|
+
return
|
|
28
|
+
|
|
29
|
+
helpers = runpy.run_path(os.path.join(self.root, "src/bare_kit/_host.py"))
|
|
30
|
+
|
|
31
|
+
requested = os.environ.get("BARE_KIT_HOST")
|
|
32
|
+
host = requested or helpers["host"]()
|
|
33
|
+
|
|
34
|
+
if host not in TAGS:
|
|
35
|
+
raise ValueError(f"Unknown host '{host}'")
|
|
36
|
+
|
|
37
|
+
prebuild = f"src/bare_kit/prebuilds/{host}/{helpers['library_name'](host)}"
|
|
38
|
+
|
|
39
|
+
if not os.path.isfile(os.path.join(self.root, prebuild)):
|
|
40
|
+
# Built without prebuilds, such as from the sdist, the wheel is left
|
|
41
|
+
# pure and fails to load the library at runtime instead.
|
|
42
|
+
if requested is None:
|
|
43
|
+
return
|
|
44
|
+
|
|
45
|
+
raise FileNotFoundError(f"No prebuild at '{prebuild}'")
|
|
46
|
+
|
|
47
|
+
build_data["pure_python"] = False
|
|
48
|
+
build_data["tag"] = f"py3-none-{TAGS[host]}"
|
|
49
|
+
build_data["force_include"][prebuild] = prebuild.removeprefix("src/")
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bare-kit-python",
|
|
3
|
+
"lockfileVersion": 3,
|
|
4
|
+
"requires": true,
|
|
5
|
+
"packages": {
|
|
6
|
+
"": {
|
|
7
|
+
"name": "bare-kit-python",
|
|
8
|
+
"devDependencies": {
|
|
9
|
+
"cmake-bare": "^1.12.0",
|
|
10
|
+
"cmake-fetch": "^1.5.3",
|
|
11
|
+
"prettier": "^3.9.10",
|
|
12
|
+
"prettier-config-holepunch": "^2.0.0"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"node_modules/cmake-bare": {
|
|
16
|
+
"version": "1.12.0",
|
|
17
|
+
"resolved": "https://registry.npmjs.org/cmake-bare/-/cmake-bare-1.12.0.tgz",
|
|
18
|
+
"integrity": "sha512-AkRij7IpB4N4V2XBomA+OUbwe8/NyzDpGIsX+CElgXU3CqPzsAwOHgM70+BgGXrcbgadUQZNyY9noNs7OZ4IxA==",
|
|
19
|
+
"dev": true,
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"workspaces": [
|
|
22
|
+
"test/fixtures/*",
|
|
23
|
+
"test/fixtures/*/*"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"cmake-npm": "^1.1.0"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"node_modules/cmake-fetch": {
|
|
30
|
+
"version": "1.5.3",
|
|
31
|
+
"resolved": "https://registry.npmjs.org/cmake-fetch/-/cmake-fetch-1.5.3.tgz",
|
|
32
|
+
"integrity": "sha512-VQFzj45gv+tDIbHdYsFaasFQKZbOOJf4+/WIF3EUDm0SzaeKBZ1C0t8DfvZ+AeMwxdYG2prUD/4k0I8DO4PG8Q==",
|
|
33
|
+
"dev": true,
|
|
34
|
+
"license": "Apache-2.0"
|
|
35
|
+
},
|
|
36
|
+
"node_modules/cmake-npm": {
|
|
37
|
+
"version": "1.1.2",
|
|
38
|
+
"resolved": "https://registry.npmjs.org/cmake-npm/-/cmake-npm-1.1.2.tgz",
|
|
39
|
+
"integrity": "sha512-CfN53Ov9usNBeaIsr8TtEQwwXeRZkJWZSoGe0qybr9bojxGlwTdxHk8LQwJFNypsSgFoYyOWUMjKGuahsiGifA==",
|
|
40
|
+
"dev": true,
|
|
41
|
+
"license": "Apache-2.0"
|
|
42
|
+
},
|
|
43
|
+
"node_modules/prettier": {
|
|
44
|
+
"version": "3.9.10",
|
|
45
|
+
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.9.10.tgz",
|
|
46
|
+
"integrity": "sha512-skGHTH1Yb9QvaTC4hWnRTzEYtCpApF0UMBPz+t/0ZPzP8ihefo/btKd+QA7pUE0fEwA1SbS/NmhyEcstdXOIdA==",
|
|
47
|
+
"dev": true,
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"bin": {
|
|
50
|
+
"prettier": "bin/prettier.cjs"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=14"
|
|
54
|
+
},
|
|
55
|
+
"funding": {
|
|
56
|
+
"url": "https://github.com/prettier/prettier?sponsor=1"
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"node_modules/prettier-config-holepunch": {
|
|
60
|
+
"version": "2.0.0",
|
|
61
|
+
"resolved": "https://registry.npmjs.org/prettier-config-holepunch/-/prettier-config-holepunch-2.0.0.tgz",
|
|
62
|
+
"integrity": "sha512-yuskcdPRfQYLFPkOGvxS4TFmCb7QvAowjO+YaNLPLBWRev+qu6HXoKkPWH9lfNsL9gUThV7IeZ6t/B1QSu/jng==",
|
|
63
|
+
"dev": true,
|
|
64
|
+
"license": "Apache-2.0",
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"prettier": "^3.6.2"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "bare-kit"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python bindings for Bare Kit"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
authors = [{ name = "Holepunch" }]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
dependencies = []
|
|
14
|
+
|
|
15
|
+
[project.optional-dependencies]
|
|
16
|
+
dev = [
|
|
17
|
+
"pytest>=8",
|
|
18
|
+
"ruff>=0.6",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/holepunchto/bare-kit-python"
|
|
23
|
+
Repository = "https://github.com/holepunchto/bare-kit-python"
|
|
24
|
+
Issues = "https://github.com/holepunchto/bare-kit-python/issues"
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.sdist]
|
|
27
|
+
exclude = ["src/bare_kit/prebuilds", "node_modules", "build"]
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.wheel]
|
|
30
|
+
packages = ["src/bare_kit"]
|
|
31
|
+
exclude = ["src/bare_kit/prebuilds"]
|
|
32
|
+
|
|
33
|
+
[tool.hatch.build.targets.wheel.hooks.custom]
|
|
34
|
+
|
|
35
|
+
[tool.ruff]
|
|
36
|
+
src = ["src", "tests"]
|
|
37
|
+
|
|
38
|
+
[tool.ruff.lint]
|
|
39
|
+
select = ["E", "F", "I", "UP", "B"]
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
testpaths = ["tests"]
|
|
43
|
+
filterwarnings = [
|
|
44
|
+
"error::ResourceWarning",
|
|
45
|
+
"error::pytest.PytestUnraisableExceptionWarning",
|
|
46
|
+
]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import platform
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
SYSTEMS = {"darwin", "linux", "win32"}
|
|
5
|
+
ARCHS = {"arm64": "arm64", "aarch64": "arm64", "x86_64": "x64", "amd64": "x64"}
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def host():
|
|
9
|
+
"""The host triple of the running platform, named as by Bare."""
|
|
10
|
+
arch = ARCHS.get(platform.machine().lower())
|
|
11
|
+
|
|
12
|
+
if sys.platform not in SYSTEMS or arch is None:
|
|
13
|
+
raise OSError(f"Unsupported platform {sys.platform}-{platform.machine()}")
|
|
14
|
+
|
|
15
|
+
return f"{sys.platform}-{arch}"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def library_name(host):
|
|
19
|
+
if host.startswith("win32"):
|
|
20
|
+
return "bare-kit.dll"
|
|
21
|
+
|
|
22
|
+
if host.startswith("darwin"):
|
|
23
|
+
return "libbare-kit.dylib"
|
|
24
|
+
|
|
25
|
+
return "libbare-kit.so"
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
from functools import cache
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from ._host import host, library_name
|
|
6
|
+
|
|
7
|
+
READABLE = 0x1
|
|
8
|
+
WRITABLE = 0x2
|
|
9
|
+
|
|
10
|
+
WOULD_BLOCK = -1
|
|
11
|
+
ERROR = -2
|
|
12
|
+
|
|
13
|
+
READ_BUFFER_SIZE = 64 * 1024
|
|
14
|
+
|
|
15
|
+
ExitCallback = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_void_p)
|
|
16
|
+
PollCallback = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_int)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class WorkletOptions(ctypes.Structure):
|
|
20
|
+
_fields_ = [
|
|
21
|
+
("version", ctypes.c_int),
|
|
22
|
+
("memory_limit", ctypes.c_size_t),
|
|
23
|
+
("platform", ctypes.c_void_p * 8),
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
SIGNATURES = {
|
|
28
|
+
"bare_worklet_optimize_for_memory": (ctypes.c_int, [ctypes.c_bool]),
|
|
29
|
+
"bare_worklet_teardown": (ctypes.c_int, []),
|
|
30
|
+
"bare_worklet_create": (
|
|
31
|
+
ctypes.c_int,
|
|
32
|
+
[ctypes.POINTER(WorkletOptions), ctypes.POINTER(ctypes.c_void_p)],
|
|
33
|
+
),
|
|
34
|
+
"bare_worklet_destroy": (None, [ctypes.c_void_p]),
|
|
35
|
+
"bare_worklet_on_exit": (
|
|
36
|
+
ctypes.c_int,
|
|
37
|
+
[ctypes.c_void_p, ExitCallback, ctypes.c_void_p],
|
|
38
|
+
),
|
|
39
|
+
"bare_worklet_start": (
|
|
40
|
+
ctypes.c_int,
|
|
41
|
+
[
|
|
42
|
+
ctypes.c_void_p,
|
|
43
|
+
ctypes.c_char_p,
|
|
44
|
+
ctypes.c_void_p,
|
|
45
|
+
ctypes.c_size_t,
|
|
46
|
+
ctypes.c_int,
|
|
47
|
+
ctypes.POINTER(ctypes.c_char_p),
|
|
48
|
+
],
|
|
49
|
+
),
|
|
50
|
+
"bare_worklet_suspend": (ctypes.c_int, [ctypes.c_void_p, ctypes.c_int]),
|
|
51
|
+
"bare_worklet_resume": (ctypes.c_int, [ctypes.c_void_p]),
|
|
52
|
+
"bare_worklet_wakeup": (ctypes.c_int, [ctypes.c_void_p, ctypes.c_int]),
|
|
53
|
+
"bare_worklet_terminate": (ctypes.c_int, [ctypes.c_void_p]),
|
|
54
|
+
"bare_ipc_create": (
|
|
55
|
+
ctypes.c_int,
|
|
56
|
+
[ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)],
|
|
57
|
+
),
|
|
58
|
+
"bare_ipc_close": (None, [ctypes.c_void_p]),
|
|
59
|
+
"bare_ipc_end": (None, [ctypes.c_void_p]),
|
|
60
|
+
"bare_ipc_destroy": (None, [ctypes.c_void_p]),
|
|
61
|
+
"bare_ipc_read": (
|
|
62
|
+
ctypes.c_int,
|
|
63
|
+
[ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t],
|
|
64
|
+
),
|
|
65
|
+
"bare_ipc_write": (
|
|
66
|
+
ctypes.c_int,
|
|
67
|
+
[ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t],
|
|
68
|
+
),
|
|
69
|
+
"bare_ipc_poll_create": (
|
|
70
|
+
ctypes.c_int,
|
|
71
|
+
[ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)],
|
|
72
|
+
),
|
|
73
|
+
"bare_ipc_poll_destroy": (None, [ctypes.c_void_p]),
|
|
74
|
+
"bare_ipc_poll_get_data": (ctypes.c_void_p, [ctypes.c_void_p]),
|
|
75
|
+
"bare_ipc_poll_set_data": (None, [ctypes.c_void_p, ctypes.c_void_p]),
|
|
76
|
+
"bare_ipc_poll_start": (
|
|
77
|
+
ctypes.c_int,
|
|
78
|
+
[ctypes.c_void_p, ctypes.c_int, PollCallback],
|
|
79
|
+
),
|
|
80
|
+
"bare_ipc_poll_stop": (ctypes.c_int, [ctypes.c_void_p]),
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def path():
|
|
85
|
+
"""The path of the Bare Kit library shipped for the running platform."""
|
|
86
|
+
current = host()
|
|
87
|
+
path = Path(__file__).parent / "prebuilds" / current / library_name(current)
|
|
88
|
+
|
|
89
|
+
if not path.is_file():
|
|
90
|
+
raise FileNotFoundError(f"No Bare Kit prebuild for {current}")
|
|
91
|
+
|
|
92
|
+
return path
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@cache
|
|
96
|
+
def library():
|
|
97
|
+
lib = ctypes.CDLL(str(path()))
|
|
98
|
+
|
|
99
|
+
for name, (restype, argtypes) in SIGNATURES.items():
|
|
100
|
+
fn = getattr(lib, name)
|
|
101
|
+
fn.restype = restype
|
|
102
|
+
fn.argtypes = argtypes
|
|
103
|
+
|
|
104
|
+
return lib
|
|
File without changes
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import ctypes
|
|
3
|
+
import itertools
|
|
4
|
+
import os
|
|
5
|
+
from collections import deque
|
|
6
|
+
from contextlib import suppress
|
|
7
|
+
|
|
8
|
+
from ._native import (
|
|
9
|
+
ERROR,
|
|
10
|
+
READ_BUFFER_SIZE,
|
|
11
|
+
READABLE,
|
|
12
|
+
WOULD_BLOCK,
|
|
13
|
+
WRITABLE,
|
|
14
|
+
ExitCallback,
|
|
15
|
+
PollCallback,
|
|
16
|
+
WorkletOptions,
|
|
17
|
+
library,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class BareKitError(Exception):
|
|
22
|
+
"""A call into Bare Kit failed with ``status``."""
|
|
23
|
+
|
|
24
|
+
def __init__(self, status):
|
|
25
|
+
super().__init__(f"Bare Kit failed with status {status}")
|
|
26
|
+
self.status = status
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _check(status):
|
|
30
|
+
if status != 0:
|
|
31
|
+
raise BareKitError(status)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def optimize_for_memory(enabled):
|
|
35
|
+
"""Trade performance for memory. Must be configured before starting the
|
|
36
|
+
first worklet."""
|
|
37
|
+
library().bare_worklet_optimize_for_memory(enabled)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def teardown():
|
|
41
|
+
"""Tear down the state shared by the worklets of the process.
|
|
42
|
+
|
|
43
|
+
Every worklet must have been closed first. Must be called at most once,
|
|
44
|
+
after which starting a worklet fails. A process that runs worklets for as
|
|
45
|
+
long as it lives need not call it.
|
|
46
|
+
"""
|
|
47
|
+
library().bare_worklet_teardown()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Bare Kit calls back from threads of its own, possibly after the worklet that a
|
|
51
|
+
# callback is for has been released, so the callbacks are shared and look the
|
|
52
|
+
# worklet up by key.
|
|
53
|
+
_worklets = {}
|
|
54
|
+
_keys = itertools.count(1)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@ExitCallback
|
|
58
|
+
def _on_exit(code, data):
|
|
59
|
+
worklet = _worklets.get(data)
|
|
60
|
+
|
|
61
|
+
if worklet is not None:
|
|
62
|
+
worklet._call_soon(worklet._on_exit, code)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@PollCallback
|
|
66
|
+
def _on_poll(poll, events):
|
|
67
|
+
# Polls are level-triggered, so the poll is stopped until the loop has
|
|
68
|
+
# handled the events and starts it again.
|
|
69
|
+
library().bare_ipc_poll_stop(poll)
|
|
70
|
+
|
|
71
|
+
worklet = _worklets.get(library().bare_ipc_poll_get_data(poll))
|
|
72
|
+
|
|
73
|
+
if worklet is not None:
|
|
74
|
+
worklet._call_soon(worklet._on_poll)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
async def start(filename, *args, source=None, memory_limit=0):
|
|
78
|
+
"""Start ``filename`` in a worklet with an IPC connection to it and return
|
|
79
|
+
its ``Worklet``.
|
|
80
|
+
|
|
81
|
+
``args`` are passed to the worklet as ``Bare.argv``. ``source`` is the
|
|
82
|
+
source of the module, read from ``filename`` if ``None``. ``memory_limit``
|
|
83
|
+
is the memory limit of each JavaScript heap in bytes, or 0 to infer it from
|
|
84
|
+
the amount of physical memory of the device.
|
|
85
|
+
"""
|
|
86
|
+
worklet = Worklet(asyncio.get_running_loop(), memory_limit)
|
|
87
|
+
|
|
88
|
+
try:
|
|
89
|
+
worklet._start(filename, args, source)
|
|
90
|
+
except BaseException:
|
|
91
|
+
worklet._destroy()
|
|
92
|
+
raise
|
|
93
|
+
|
|
94
|
+
return worklet
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class Worklet:
|
|
98
|
+
"""A Bare runtime running on a thread of its own, connected to its
|
|
99
|
+
``Bare.IPC``.
|
|
100
|
+
|
|
101
|
+
Iterating it with ``async for`` yields the data the worklet writes to
|
|
102
|
+
``Bare.IPC`` until the connection ends, and leaving ``async with`` closes
|
|
103
|
+
it. Use ``start()`` to start one.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
def __init__(self, loop, memory_limit):
|
|
107
|
+
lib = library()
|
|
108
|
+
|
|
109
|
+
self._loop = loop
|
|
110
|
+
self._key = next(_keys)
|
|
111
|
+
self._handle = ctypes.c_void_p()
|
|
112
|
+
self._ipc = ctypes.c_void_p()
|
|
113
|
+
self._poll = ctypes.c_void_p()
|
|
114
|
+
self._buffer = ctypes.create_string_buffer(READ_BUFFER_SIZE)
|
|
115
|
+
self._exited = loop.create_future()
|
|
116
|
+
self._started = False
|
|
117
|
+
self._closed = False
|
|
118
|
+
|
|
119
|
+
self._events = 0
|
|
120
|
+
self._chunks = deque()
|
|
121
|
+
self._buffered = 0
|
|
122
|
+
self._eof = False
|
|
123
|
+
self._reader = None
|
|
124
|
+
self._writes = deque()
|
|
125
|
+
self._ending = False
|
|
126
|
+
self._ended = False
|
|
127
|
+
self._disconnected = False
|
|
128
|
+
|
|
129
|
+
options = WorkletOptions(version=0, memory_limit=memory_limit)
|
|
130
|
+
|
|
131
|
+
_check(
|
|
132
|
+
lib.bare_worklet_create(ctypes.byref(options), ctypes.byref(self._handle))
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
try:
|
|
136
|
+
_check(lib.bare_ipc_create(self._handle, ctypes.byref(self._ipc)))
|
|
137
|
+
_check(lib.bare_ipc_poll_create(self._ipc, ctypes.byref(self._poll)))
|
|
138
|
+
except BaseException:
|
|
139
|
+
self._destroy()
|
|
140
|
+
raise
|
|
141
|
+
|
|
142
|
+
lib.bare_ipc_poll_set_data(self._poll, self._key)
|
|
143
|
+
lib.bare_worklet_on_exit(self._handle, _on_exit, self._key)
|
|
144
|
+
|
|
145
|
+
_worklets[self._key] = self
|
|
146
|
+
|
|
147
|
+
def _start(self, filename, args, source):
|
|
148
|
+
argv = [os.fsencode(arg) for arg in args]
|
|
149
|
+
|
|
150
|
+
_check(
|
|
151
|
+
library().bare_worklet_start(
|
|
152
|
+
self._handle,
|
|
153
|
+
os.fsencode(filename),
|
|
154
|
+
source,
|
|
155
|
+
0 if source is None else len(source),
|
|
156
|
+
len(argv),
|
|
157
|
+
(ctypes.c_char_p * (len(argv) + 1))(*argv, None),
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
self._started = True
|
|
162
|
+
self._update()
|
|
163
|
+
|
|
164
|
+
@property
|
|
165
|
+
def exit_code(self):
|
|
166
|
+
"""The exit code of the worklet, or ``None`` while it is running."""
|
|
167
|
+
return self._exited.result() if self._exited.done() else None
|
|
168
|
+
|
|
169
|
+
async def read(self, n=65536):
|
|
170
|
+
"""Read up to ``n`` bytes from the worklet, or ``b""`` once it has
|
|
171
|
+
ended its end of the connection."""
|
|
172
|
+
while not self._chunks and not self._eof:
|
|
173
|
+
self._reader = self._loop.create_future()
|
|
174
|
+
|
|
175
|
+
try:
|
|
176
|
+
await self._reader
|
|
177
|
+
finally:
|
|
178
|
+
self._reader = None
|
|
179
|
+
|
|
180
|
+
if not self._chunks:
|
|
181
|
+
self._close_ipc()
|
|
182
|
+
return b""
|
|
183
|
+
|
|
184
|
+
chunk = self._chunks.popleft()
|
|
185
|
+
|
|
186
|
+
if len(chunk) > n:
|
|
187
|
+
self._chunks.appendleft(chunk[n:])
|
|
188
|
+
chunk = chunk[:n]
|
|
189
|
+
|
|
190
|
+
self._buffered -= len(chunk)
|
|
191
|
+
self._update()
|
|
192
|
+
|
|
193
|
+
return chunk
|
|
194
|
+
|
|
195
|
+
def __aiter__(self):
|
|
196
|
+
return self._chunks_until_eof()
|
|
197
|
+
|
|
198
|
+
async def _chunks_until_eof(self):
|
|
199
|
+
while chunk := await self.read():
|
|
200
|
+
yield chunk
|
|
201
|
+
|
|
202
|
+
async def write(self, data):
|
|
203
|
+
"""Write ``data`` to the worklet and wait for it to drain.
|
|
204
|
+
|
|
205
|
+
Data written once the connection has ended or closed, such as while the
|
|
206
|
+
worklet exits, is dropped, as its exit already accounts for it.
|
|
207
|
+
"""
|
|
208
|
+
if self._ending or not data:
|
|
209
|
+
return
|
|
210
|
+
|
|
211
|
+
written = self._loop.create_future()
|
|
212
|
+
|
|
213
|
+
self._writes.append([bytes(data), 0, written])
|
|
214
|
+
self._flush()
|
|
215
|
+
self._update()
|
|
216
|
+
|
|
217
|
+
await written
|
|
218
|
+
|
|
219
|
+
def end(self):
|
|
220
|
+
"""End the data written to the worklet once the pending writes drain,
|
|
221
|
+
after which ``Bare.IPC`` reads end-of-stream while reading continues."""
|
|
222
|
+
if self._ending:
|
|
223
|
+
return
|
|
224
|
+
|
|
225
|
+
self._ending = True
|
|
226
|
+
self._flush()
|
|
227
|
+
self._update()
|
|
228
|
+
|
|
229
|
+
def suspend(self, linger=-1):
|
|
230
|
+
"""Suspend the worklet, emitting ``suspend`` on ``Bare``.
|
|
231
|
+
|
|
232
|
+
``linger`` is how long in milliseconds the worklet may keep running
|
|
233
|
+
before it goes idle, or -1 for as long as the platform allows.
|
|
234
|
+
"""
|
|
235
|
+
library().bare_worklet_suspend(self._handle, linger)
|
|
236
|
+
|
|
237
|
+
def resume(self):
|
|
238
|
+
"""Resume a suspended worklet, emitting ``resume`` on ``Bare``."""
|
|
239
|
+
library().bare_worklet_resume(self._handle)
|
|
240
|
+
|
|
241
|
+
def wakeup(self, deadline=-1):
|
|
242
|
+
"""Wake up a suspended worklet, emitting ``wakeup`` on ``Bare``.
|
|
243
|
+
|
|
244
|
+
``deadline`` is how long in milliseconds the worklet may run before it
|
|
245
|
+
goes idle again, or -1 for as long as the platform allows.
|
|
246
|
+
"""
|
|
247
|
+
library().bare_worklet_wakeup(self._handle, deadline)
|
|
248
|
+
|
|
249
|
+
def terminate(self):
|
|
250
|
+
"""Ask the worklet to exit unless it has already exited."""
|
|
251
|
+
library().bare_worklet_terminate(self._handle)
|
|
252
|
+
|
|
253
|
+
async def wait(self):
|
|
254
|
+
"""Wait for the worklet to exit and return its exit code."""
|
|
255
|
+
return await asyncio.shield(self._exited)
|
|
256
|
+
|
|
257
|
+
async def close(self):
|
|
258
|
+
"""Close the connection, terminating the worklet if it is still
|
|
259
|
+
running, and wait for it to exit."""
|
|
260
|
+
if self._closed:
|
|
261
|
+
return
|
|
262
|
+
|
|
263
|
+
self._close_ipc()
|
|
264
|
+
self.terminate()
|
|
265
|
+
|
|
266
|
+
await self.wait()
|
|
267
|
+
|
|
268
|
+
self._destroy()
|
|
269
|
+
|
|
270
|
+
async def __aenter__(self):
|
|
271
|
+
return self
|
|
272
|
+
|
|
273
|
+
async def __aexit__(self, *exc):
|
|
274
|
+
await self.close()
|
|
275
|
+
|
|
276
|
+
def _call_soon(self, callback, *args):
|
|
277
|
+
# The loop may have been closed while a worklet was left running.
|
|
278
|
+
with suppress(RuntimeError):
|
|
279
|
+
self._loop.call_soon_threadsafe(callback, *args)
|
|
280
|
+
|
|
281
|
+
def _on_exit(self, code):
|
|
282
|
+
if not self._exited.done():
|
|
283
|
+
self._exited.set_result(code)
|
|
284
|
+
|
|
285
|
+
def _on_poll(self):
|
|
286
|
+
if self._closed:
|
|
287
|
+
return
|
|
288
|
+
|
|
289
|
+
self._events = 0
|
|
290
|
+
self._drain()
|
|
291
|
+
self._flush()
|
|
292
|
+
self._update()
|
|
293
|
+
|
|
294
|
+
def _drain(self):
|
|
295
|
+
lib = library()
|
|
296
|
+
|
|
297
|
+
while not self._eof and self._buffered < READ_BUFFER_SIZE:
|
|
298
|
+
read = lib.bare_ipc_read(self._ipc, self._buffer, READ_BUFFER_SIZE)
|
|
299
|
+
|
|
300
|
+
if read > 0:
|
|
301
|
+
self._chunks.append(ctypes.string_at(self._buffer, read))
|
|
302
|
+
self._buffered += read
|
|
303
|
+
else:
|
|
304
|
+
if read == 0:
|
|
305
|
+
self._eof = True
|
|
306
|
+
|
|
307
|
+
break
|
|
308
|
+
|
|
309
|
+
if self._reader is not None and not self._reader.done():
|
|
310
|
+
if self._chunks or self._eof:
|
|
311
|
+
self._reader.set_result(None)
|
|
312
|
+
|
|
313
|
+
def _flush(self):
|
|
314
|
+
lib = library()
|
|
315
|
+
|
|
316
|
+
while self._writes:
|
|
317
|
+
write = self._writes[0]
|
|
318
|
+
data, offset, written = write
|
|
319
|
+
|
|
320
|
+
result = lib.bare_ipc_write(
|
|
321
|
+
self._ipc,
|
|
322
|
+
ctypes.cast(data, ctypes.c_void_p).value + offset,
|
|
323
|
+
len(data) - offset,
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
if result == WOULD_BLOCK:
|
|
327
|
+
break
|
|
328
|
+
|
|
329
|
+
if result == ERROR:
|
|
330
|
+
self._drop_writes()
|
|
331
|
+
break
|
|
332
|
+
|
|
333
|
+
write[1] = offset = offset + result
|
|
334
|
+
|
|
335
|
+
if offset == len(data):
|
|
336
|
+
self._writes.popleft()
|
|
337
|
+
|
|
338
|
+
if not written.done():
|
|
339
|
+
written.set_result(None)
|
|
340
|
+
|
|
341
|
+
if self._ending and not self._writes and not self._ended:
|
|
342
|
+
self._ended = True
|
|
343
|
+
lib.bare_ipc_end(self._ipc)
|
|
344
|
+
|
|
345
|
+
def _drop_writes(self):
|
|
346
|
+
while self._writes:
|
|
347
|
+
_, _, written = self._writes.popleft()
|
|
348
|
+
|
|
349
|
+
if not written.done():
|
|
350
|
+
written.set_result(None)
|
|
351
|
+
|
|
352
|
+
def _update(self):
|
|
353
|
+
if self._closed or not self._started:
|
|
354
|
+
return
|
|
355
|
+
|
|
356
|
+
events = 0
|
|
357
|
+
|
|
358
|
+
if not self._eof and self._buffered < READ_BUFFER_SIZE:
|
|
359
|
+
events |= READABLE
|
|
360
|
+
|
|
361
|
+
if self._writes:
|
|
362
|
+
events |= WRITABLE
|
|
363
|
+
|
|
364
|
+
if events == self._events:
|
|
365
|
+
return
|
|
366
|
+
|
|
367
|
+
self._events = events
|
|
368
|
+
|
|
369
|
+
if events:
|
|
370
|
+
library().bare_ipc_poll_start(self._poll, events, _on_poll)
|
|
371
|
+
else:
|
|
372
|
+
library().bare_ipc_poll_stop(self._poll)
|
|
373
|
+
|
|
374
|
+
def _close_ipc(self):
|
|
375
|
+
"""Close the connection, which the worklet waits for before it exits
|
|
376
|
+
once it has ended its side."""
|
|
377
|
+
if self._ipc and not self._disconnected:
|
|
378
|
+
self._disconnected = True
|
|
379
|
+
library().bare_ipc_close(self._ipc)
|
|
380
|
+
|
|
381
|
+
self._ending = self._ended = True
|
|
382
|
+
self._eof = True
|
|
383
|
+
self._drop_writes()
|
|
384
|
+
|
|
385
|
+
if self._reader is not None and not self._reader.done():
|
|
386
|
+
self._reader.set_result(None)
|
|
387
|
+
|
|
388
|
+
def _destroy(self):
|
|
389
|
+
lib = library()
|
|
390
|
+
|
|
391
|
+
self._closed = True
|
|
392
|
+
self._close_ipc()
|
|
393
|
+
|
|
394
|
+
_worklets.pop(self._key, None)
|
|
395
|
+
|
|
396
|
+
if self._poll:
|
|
397
|
+
lib.bare_ipc_poll_destroy(self._poll)
|
|
398
|
+
|
|
399
|
+
if self._ipc:
|
|
400
|
+
lib.bare_ipc_destroy(self._ipc)
|
|
401
|
+
|
|
402
|
+
if self._handle:
|
|
403
|
+
lib.bare_worklet_destroy(self._handle)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Bare.IPC.end(JSON.stringify(Bare.argv))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Bare.IPC.on('data', (data) => Bare.IPC.write(data))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Bare.exit(3)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
setInterval(() => {}, 1000)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from bare_kit import start
|
|
5
|
+
|
|
6
|
+
FIXTURES = Path(__file__).parent / "fixtures"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def run(coro):
|
|
10
|
+
return asyncio.run(asyncio.wait_for(coro, 10))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_echoes_ipc_data():
|
|
14
|
+
async def body():
|
|
15
|
+
async with await start(FIXTURES / "echo.js") as worklet:
|
|
16
|
+
await worklet.write(b"Hello world")
|
|
17
|
+
return await worklet.read()
|
|
18
|
+
|
|
19
|
+
assert run(body()) == b"Hello world"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_ends_ipc_data():
|
|
23
|
+
async def body():
|
|
24
|
+
async with await start(FIXTURES / "collect.js") as worklet:
|
|
25
|
+
await worklet.write(b"Hello ")
|
|
26
|
+
await worklet.write(b"world")
|
|
27
|
+
worklet.end()
|
|
28
|
+
await worklet.write(b"!")
|
|
29
|
+
return b"".join([chunk async for chunk in worklet]), await worklet.wait()
|
|
30
|
+
|
|
31
|
+
assert run(body()) == (b"Hello world", 0)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_passes_arguments():
|
|
35
|
+
async def body():
|
|
36
|
+
async with await start(FIXTURES / "argv.js", "foo", "bar") as worklet:
|
|
37
|
+
return b"".join([chunk async for chunk in worklet])
|
|
38
|
+
|
|
39
|
+
assert run(body()) == b'["foo","bar"]'
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_runs_source():
|
|
43
|
+
async def body():
|
|
44
|
+
source = b"Bare.IPC.end('Hello world')"
|
|
45
|
+
|
|
46
|
+
async with await start("/app.js", source=source) as worklet:
|
|
47
|
+
return b"".join([chunk async for chunk in worklet])
|
|
48
|
+
|
|
49
|
+
assert run(body()) == b"Hello world"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_reports_the_exit_code():
|
|
53
|
+
async def body():
|
|
54
|
+
async with await start(FIXTURES / "exit.js") as worklet:
|
|
55
|
+
return await worklet.wait(), await worklet.read(), worklet.exit_code
|
|
56
|
+
|
|
57
|
+
assert run(body()) == (3, b"", 3)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_terminates():
|
|
61
|
+
async def body():
|
|
62
|
+
async with await start(FIXTURES / "sleep.js") as worklet:
|
|
63
|
+
worklet.terminate()
|
|
64
|
+
await worklet.wait()
|
|
65
|
+
return await worklet.read()
|
|
66
|
+
|
|
67
|
+
assert run(body()) == b""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_suspends_and_resumes():
|
|
71
|
+
async def body():
|
|
72
|
+
async with await start(FIXTURES / "lifecycle.js") as worklet:
|
|
73
|
+
events = []
|
|
74
|
+
|
|
75
|
+
async for chunk in worklet:
|
|
76
|
+
events.append(chunk)
|
|
77
|
+
|
|
78
|
+
if chunk == b"ready":
|
|
79
|
+
worklet.suspend(0)
|
|
80
|
+
elif chunk == b"suspend":
|
|
81
|
+
worklet.resume()
|
|
82
|
+
else:
|
|
83
|
+
break
|
|
84
|
+
|
|
85
|
+
return events
|
|
86
|
+
|
|
87
|
+
assert run(body()) == [b"ready", b"suspend", b"resume"]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_drops_writes_once_closed():
|
|
91
|
+
async def body():
|
|
92
|
+
worklet = await start(FIXTURES / "echo.js")
|
|
93
|
+
await worklet.close()
|
|
94
|
+
await worklet.write(b"Hello world")
|
|
95
|
+
return await worklet.read()
|
|
96
|
+
|
|
97
|
+
assert run(body()) == b""
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_streams_large_data():
|
|
101
|
+
data = bytes(range(256)) * 4096
|
|
102
|
+
|
|
103
|
+
async def body():
|
|
104
|
+
async with await start(FIXTURES / "collect.js") as worklet:
|
|
105
|
+
await worklet.write(data)
|
|
106
|
+
worklet.end()
|
|
107
|
+
return b"".join([chunk async for chunk in worklet])
|
|
108
|
+
|
|
109
|
+
assert run(body()) == data
|