@ohos-ports/parcel-optimizer-image 2.9.3-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-present Devon Govett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function _path() {
8
+ const data = _interopRequireDefault(require("path"));
9
+ _path = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _process() {
15
+ const data = _interopRequireDefault(require("process"));
16
+ _process = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _plugin() {
22
+ const data = require("@parcel/plugin");
23
+ _plugin = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _utils() {
29
+ const data = require("@parcel/utils");
30
+ _utils = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _diagnostic() {
36
+ const data = require("@parcel/diagnostic");
37
+ _diagnostic = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ var _native = require("../native");
43
+ function _workers() {
44
+ const data = _interopRequireDefault(require("@parcel/workers"));
45
+ _workers = function () {
46
+ return data;
47
+ };
48
+ return data;
49
+ }
50
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
51
+ var _default = new (_plugin().Optimizer)({
52
+ async optimize({
53
+ bundle,
54
+ contents,
55
+ logger
56
+ }) {
57
+ if (!bundle.env.shouldOptimize) {
58
+ return {
59
+ contents
60
+ };
61
+ }
62
+ await loadOnMainThreadIfNeeded();
63
+ let buffer = await (0, _utils().blobToBuffer)(contents);
64
+
65
+ // Attempt to optimize it, if the optimize fails we log a warning...
66
+ try {
67
+ let optimized = (0, _native.optimize)(bundle.type, buffer);
68
+ return {
69
+ contents: optimized.length < buffer.length ? optimized : buffer
70
+ };
71
+ } catch (err) {
72
+ var _bundle$getMainEntry;
73
+ const filepath = (_bundle$getMainEntry = bundle.getMainEntry()) === null || _bundle$getMainEntry === void 0 ? void 0 : _bundle$getMainEntry.filePath;
74
+ const filename = filepath ? _path().default.relative(_process().default.cwd(), filepath) : 'unknown';
75
+ logger.warn({
76
+ message: (0, _diagnostic().md)`Could not optimize image ${filename}: ${err.message}`,
77
+ stack: err.stack
78
+ });
79
+ }
80
+ return {
81
+ contents: buffer
82
+ };
83
+ }
84
+ }); // On linux with older versions of glibc (e.g. CentOS 7), we encounter a segmentation fault
85
+ // when worker threads exit due to thread local variables in Rust. A workaround is to
86
+ // also load the native module on the main thread, so that it is not unloaded until process exit.
87
+ // See https://github.com/rust-lang/rust/issues/91979.
88
+ exports.default = _default;
89
+ let isLoadedOnMainThread = false;
90
+ async function loadOnMainThreadIfNeeded() {
91
+ if (!isLoadedOnMainThread && _process().default.platform === 'linux' && _workers().default.isWorker()) {
92
+ // $FlowFixMe
93
+ let {
94
+ glibcVersionRuntime
95
+ } = _process().default.report.getReport().header;
96
+ if (glibcVersionRuntime && parseFloat(glibcVersionRuntime) <= 2.17) {
97
+ let api = _workers().default.getWorkerApi();
98
+ await api.callMaster({
99
+ location: __dirname + '/loadNative.js',
100
+ args: []
101
+ });
102
+ isLoadedOnMainThread = true;
103
+ }
104
+ }
105
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ // This function is called from the main thread to prevent unloading the module
4
+ // until the main thread exits. This avoids a segfault in older glibc versions.
5
+ // See https://github.com/rust-lang/rust/issues/91979
6
+ module.exports = () => {
7
+ require('../native');
8
+ };
package/native.js ADDED
@@ -0,0 +1,277 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /* prettier-ignore */
4
+
5
+ /* auto-generated by NAPI-RS */
6
+
7
+ const { existsSync, readFileSync } = require('fs')
8
+ const { join } = require('path')
9
+
10
+ const { platform, arch } = process
11
+
12
+ let nativeBinding = null
13
+ let localFileExisted = false
14
+ let loadError = null
15
+
16
+ function isMusl() {
17
+ // For Node 10
18
+ if (!process.report || typeof process.report.getReport !== 'function') {
19
+ try {
20
+ const lddPath = require('child_process').execSync('which ldd').toString().trim();
21
+ return readFileSync(lddPath, 'utf8').includes('musl')
22
+ } catch (e) {
23
+ return true
24
+ }
25
+ } else {
26
+ const { glibcVersionRuntime } = process.report.getReport().header
27
+ return !glibcVersionRuntime
28
+ }
29
+ }
30
+
31
+ switch (platform) {
32
+ case 'android':
33
+ switch (arch) {
34
+ case 'arm64':
35
+ localFileExisted = existsSync(join(__dirname, 'parcel-image.android-arm64.node'))
36
+ try {
37
+ if (localFileExisted) {
38
+ nativeBinding = require('./parcel-image.android-arm64.node')
39
+ } else {
40
+ nativeBinding = require('@parcel/optimizer-image-android-arm64')
41
+ }
42
+ } catch (e) {
43
+ loadError = e
44
+ }
45
+ break
46
+ case 'arm':
47
+ localFileExisted = existsSync(join(__dirname, 'parcel-image.android-arm-eabi.node'))
48
+ try {
49
+ if (localFileExisted) {
50
+ nativeBinding = require('./parcel-image.android-arm-eabi.node')
51
+ } else {
52
+ nativeBinding = require('@parcel/optimizer-image-android-arm-eabi')
53
+ }
54
+ } catch (e) {
55
+ loadError = e
56
+ }
57
+ break
58
+ default:
59
+ throw new Error(`Unsupported architecture on Android ${arch}`)
60
+ }
61
+ break
62
+ case 'win32':
63
+ switch (arch) {
64
+ case 'x64':
65
+ localFileExisted = existsSync(
66
+ join(__dirname, 'parcel-image.win32-x64-msvc.node')
67
+ )
68
+ try {
69
+ if (localFileExisted) {
70
+ nativeBinding = require('./parcel-image.win32-x64-msvc.node')
71
+ } else {
72
+ nativeBinding = require('@parcel/optimizer-image-win32-x64-msvc')
73
+ }
74
+ } catch (e) {
75
+ loadError = e
76
+ }
77
+ break
78
+ case 'ia32':
79
+ localFileExisted = existsSync(
80
+ join(__dirname, 'parcel-image.win32-ia32-msvc.node')
81
+ )
82
+ try {
83
+ if (localFileExisted) {
84
+ nativeBinding = require('./parcel-image.win32-ia32-msvc.node')
85
+ } else {
86
+ nativeBinding = require('@parcel/optimizer-image-win32-ia32-msvc')
87
+ }
88
+ } catch (e) {
89
+ loadError = e
90
+ }
91
+ break
92
+ case 'arm64':
93
+ localFileExisted = existsSync(
94
+ join(__dirname, 'parcel-image.win32-arm64-msvc.node')
95
+ )
96
+ try {
97
+ if (localFileExisted) {
98
+ nativeBinding = require('./parcel-image.win32-arm64-msvc.node')
99
+ } else {
100
+ nativeBinding = require('@parcel/optimizer-image-win32-arm64-msvc')
101
+ }
102
+ } catch (e) {
103
+ loadError = e
104
+ }
105
+ break
106
+ default:
107
+ throw new Error(`Unsupported architecture on Windows: ${arch}`)
108
+ }
109
+ break
110
+ case 'darwin':
111
+ localFileExisted = existsSync(join(__dirname, 'parcel-image.darwin-universal.node'))
112
+ try {
113
+ if (localFileExisted) {
114
+ nativeBinding = require('./parcel-image.darwin-universal.node')
115
+ } else {
116
+ nativeBinding = require('@parcel/optimizer-image-darwin-universal')
117
+ }
118
+ break
119
+ } catch {}
120
+ switch (arch) {
121
+ case 'x64':
122
+ localFileExisted = existsSync(join(__dirname, 'parcel-image.darwin-x64.node'))
123
+ try {
124
+ if (localFileExisted) {
125
+ nativeBinding = require('./parcel-image.darwin-x64.node')
126
+ } else {
127
+ nativeBinding = require('@parcel/optimizer-image-darwin-x64')
128
+ }
129
+ } catch (e) {
130
+ loadError = e
131
+ }
132
+ break
133
+ case 'arm64':
134
+ localFileExisted = existsSync(
135
+ join(__dirname, 'parcel-image.darwin-arm64.node')
136
+ )
137
+ try {
138
+ if (localFileExisted) {
139
+ nativeBinding = require('./parcel-image.darwin-arm64.node')
140
+ } else {
141
+ nativeBinding = require('@parcel/optimizer-image-darwin-arm64')
142
+ }
143
+ } catch (e) {
144
+ loadError = e
145
+ }
146
+ break
147
+ default:
148
+ throw new Error(`Unsupported architecture on macOS: ${arch}`)
149
+ }
150
+ break
151
+ case 'openharmony':
152
+ switch (arch) {
153
+ case 'arm64':
154
+ localFileExisted = existsSync(
155
+ join(__dirname, 'parcel-image.openharmony-arm64.node')
156
+ )
157
+ try {
158
+ if (localFileExisted) {
159
+ nativeBinding = require('./parcel-image.openharmony-arm64.node')
160
+ } else {
161
+ nativeBinding = require('@parcel/optimizer-image-openharmony-arm64')
162
+ }
163
+ } catch (e) {
164
+ loadError = e
165
+ }
166
+ break
167
+ default:
168
+ throw new Error(`Unsupported architecture on openharmony: ${arch}`)
169
+ }
170
+ break
171
+ case 'freebsd':
172
+ if (arch !== 'x64') {
173
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
174
+ }
175
+ localFileExisted = existsSync(join(__dirname, 'parcel-image.freebsd-x64.node'))
176
+ try {
177
+ if (localFileExisted) {
178
+ nativeBinding = require('./parcel-image.freebsd-x64.node')
179
+ } else {
180
+ nativeBinding = require('@parcel/optimizer-image-freebsd-x64')
181
+ }
182
+ } catch (e) {
183
+ loadError = e
184
+ }
185
+ break
186
+ case 'linux':
187
+ switch (arch) {
188
+ case 'x64':
189
+ if (isMusl()) {
190
+ localFileExisted = existsSync(
191
+ join(__dirname, 'parcel-image.linux-x64-musl.node')
192
+ )
193
+ try {
194
+ if (localFileExisted) {
195
+ nativeBinding = require('./parcel-image.linux-x64-musl.node')
196
+ } else {
197
+ nativeBinding = require('@parcel/optimizer-image-linux-x64-musl')
198
+ }
199
+ } catch (e) {
200
+ loadError = e
201
+ }
202
+ } else {
203
+ localFileExisted = existsSync(
204
+ join(__dirname, 'parcel-image.linux-x64-gnu.node')
205
+ )
206
+ try {
207
+ if (localFileExisted) {
208
+ nativeBinding = require('./parcel-image.linux-x64-gnu.node')
209
+ } else {
210
+ nativeBinding = require('@parcel/optimizer-image-linux-x64-gnu')
211
+ }
212
+ } catch (e) {
213
+ loadError = e
214
+ }
215
+ }
216
+ break
217
+ case 'arm64':
218
+ if (isMusl()) {
219
+ localFileExisted = existsSync(
220
+ join(__dirname, 'parcel-image.linux-arm64-musl.node')
221
+ )
222
+ try {
223
+ if (localFileExisted) {
224
+ nativeBinding = require('./parcel-image.linux-arm64-musl.node')
225
+ } else {
226
+ nativeBinding = require('@parcel/optimizer-image-linux-arm64-musl')
227
+ }
228
+ } catch (e) {
229
+ loadError = e
230
+ }
231
+ } else {
232
+ localFileExisted = existsSync(
233
+ join(__dirname, 'parcel-image.linux-arm64-gnu.node')
234
+ )
235
+ try {
236
+ if (localFileExisted) {
237
+ nativeBinding = require('./parcel-image.linux-arm64-gnu.node')
238
+ } else {
239
+ nativeBinding = require('@parcel/optimizer-image-linux-arm64-gnu')
240
+ }
241
+ } catch (e) {
242
+ loadError = e
243
+ }
244
+ }
245
+ break
246
+ case 'arm':
247
+ localFileExisted = existsSync(
248
+ join(__dirname, 'parcel-image.linux-arm-gnueabihf.node')
249
+ )
250
+ try {
251
+ if (localFileExisted) {
252
+ nativeBinding = require('./parcel-image.linux-arm-gnueabihf.node')
253
+ } else {
254
+ nativeBinding = require('@parcel/optimizer-image-linux-arm-gnueabihf')
255
+ }
256
+ } catch (e) {
257
+ loadError = e
258
+ }
259
+ break
260
+ default:
261
+ throw new Error(`Unsupported architecture on Linux: ${arch}`)
262
+ }
263
+ break
264
+ default:
265
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
266
+ }
267
+
268
+ if (!nativeBinding) {
269
+ if (loadError) {
270
+ throw loadError
271
+ }
272
+ throw new Error(`Failed to load native binding`)
273
+ }
274
+
275
+ const { optimize } = nativeBinding
276
+
277
+ module.exports.optimize = optimize
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@ohos-ports/parcel-optimizer-image",
3
+ "version": "2.9.3-beta.0",
4
+ "license": "MIT",
5
+ "main": "lib/ImageOptimizer.js",
6
+ "source": "src/ImageOptimizer.js",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "funding": {
11
+ "type": "opencollective",
12
+ "url": "https://opencollective.com/parcel"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
17
+ "directory": "ports/parcel-optimizer-image/2.9.3"
18
+ },
19
+ "engines": {
20
+ "node": ">= 12.0.0",
21
+ "parcel": "^2.9.3"
22
+ },
23
+ "files": [
24
+ "lib",
25
+ "native.js",
26
+ "*.node"
27
+ ],
28
+ "napi": {
29
+ "name": "parcel-image"
30
+ },
31
+ "scripts": {
32
+ "build": "napi build --platform --js native.js --dts native.d.ts",
33
+ "build-release": "napi build --platform --release --js native.js --dts native.d.ts"
34
+ },
35
+ "dependencies": {
36
+ "@parcel/diagnostic": "2.9.3",
37
+ "@parcel/plugin": "2.9.3",
38
+ "@parcel/utils": "2.9.3",
39
+ "@parcel/workers": "2.9.3"
40
+ },
41
+ "peerDependencies": {
42
+ "@parcel/core": "^2.9.3"
43
+ },
44
+ "devDependencies": {
45
+ "@napi-rs/cli": "^2.15.2",
46
+ "tiny-benchy": "^1.0.2"
47
+ },
48
+ "gitHead": "db3bcae10497fa6a712fd9a135f93f26c5745454",
49
+ "bugs": {
50
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
51
+ }
52
+ }
Binary file
Binary file
Binary file
Binary file
Binary file