@cc-livekit/ttsignal 1.0.8 → 1.0.9

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.
Files changed (2) hide show
  1. package/index.js +66 -8
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,20 +1,78 @@
1
+ "use strict";
2
+
1
3
  /*******************************************************************************
2
4
  * javascript glue wrapper of ttsignal module
3
5
  * @module index.js
4
6
  * @author anto.
5
- *******************************************************************************/
7
+ ******************************************************************************/
8
+
9
+ const EventEmitter = require("events").EventEmitter;
10
+ const fs = require("fs");
11
+ const path = require("path");
12
+ const Module = require("module");
13
+
14
+ const PLATFORM = process.platform;
15
+ const ARCH = process.arch;
16
+ const ADDON_BASENAME = `ttsignal.${PLATFORM}.${ARCH}.node`;
17
+
18
+ function expandAsar(candidate) {
19
+ if (!candidate) return candidate;
20
+ const idx = candidate.indexOf(".asar/");
21
+ if (idx === -1) return candidate;
22
+ return candidate.replace(".asar/", ".asar.unpacked/");
23
+ }
24
+
25
+ function resolveAddonPath() {
26
+ const candidates = [];
27
+ if (process.env.TTSIGNAL_NATIVE_PATH) {
28
+ candidates.push(process.env.TTSIGNAL_NATIVE_PATH);
29
+ }
6
30
 
7
- var util = require('util');
8
- var EventEmitter = require('events').EventEmitter;
31
+ const baseDir = __dirname;
32
+ const kind = process.ttsBuildType === "debug" ? "Debug" : "Release";
33
+ const searchRoots = [
34
+ path.join(baseDir, "build", kind),
35
+ path.join(baseDir, "dist", "build", kind),
36
+ path.join(baseDir, "..", "build", kind),
37
+ path.join(baseDir, "..", "dist", "build", kind),
38
+ ];
9
39
 
10
- var ttsignal = null;
40
+ for (const root of searchRoots) {
41
+ candidates.push(path.join(root, ADDON_BASENAME));
42
+ }
11
43
 
12
- if (process.ttsBuildType == 'debug') {
13
- ttsignal = require('./Debug/ttsignal.'+process.platform+'.'+process.arch+'.node');
14
- } else {
15
- ttsignal = require('./Release/ttsignal.'+process.platform+'.'+process.arch+'.node');
44
+ const unique = [...new Set(candidates.filter(Boolean))];
45
+
46
+ for (const candidate of unique) {
47
+ if (fs.existsSync(candidate)) {
48
+ return candidate;
49
+ }
50
+ const unpacked = expandAsar(candidate);
51
+ if (unpacked !== candidate && fs.existsSync(unpacked)) {
52
+ return unpacked;
53
+ }
54
+ }
55
+
56
+ const tried = unique.concat(unique.map(expandAsar)).filter(Boolean);
57
+ throw new Error(
58
+ `Unable to locate ttsignal native addon (${PLATFORM}/${ARCH}). Tried: ${tried.join(
59
+ ", "
60
+ )}`
61
+ );
16
62
  }
17
63
 
64
+ function loadNativeAddon() {
65
+ const createRequire =
66
+ Module.createRequire ||
67
+ Module.createRequireFromPath ||
68
+ require("module").createRequire;
69
+ const req = createRequire(__filename);
70
+ const addonPath = resolveAddonPath();
71
+ return req(addonPath);
72
+ }
73
+
74
+ const ttsignal = loadNativeAddon();
75
+
18
76
  const CONST = {
19
77
  TTS_TYPE_COMMAND : 0x01,
20
78
  TTS_TYPE_MESSAGE : 0x02,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cc-livekit/ttsignal",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"