workbite 0.1.19 → 0.1.21

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.
@@ -1,54 +1,54 @@
1
- 'use strict';
2
- const fs = require('node:fs/promises');
3
- const path = require('node:path');
4
- const { execFile } = require('node:child_process');
5
- // WMI LastBootUpTime is the OS boot time, not wall-clock minus uptime:
6
- // https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem
7
- // Use the framework WMI API directly, with no profile, PATH or module lookup.
8
- const SCRIPT = String.raw`$ErrorActionPreference='Stop'
9
- Add-Type -AssemblyName System.Management
10
- $q = [System.Management.ManagementObjectSearcher]::new('SELECT LastBootUpTime FROM Win32_OperatingSystem')
11
- try {
12
- $rows = @($q.Get())
13
- if ($rows.Count -ne 1) { throw 'ambiguous operating system' }
14
- $r = $rows[0]
15
- $t = [System.Management.ManagementDateTimeConverter]::ToDateTime([string]$r.LastBootUpTime).ToUniversalTime().Ticks
16
- $key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SOFTWARE\Microsoft\Cryptography')
17
- try { $hostId = [string]$key.GetValue('MachineGuid') } finally { $key.Dispose() }
18
- [Console]::Out.Write($hostId.ToLowerInvariant() + ':' + $t.ToString([Globalization.CultureInfo]::InvariantCulture))
19
- } finally { $q.Dispose() }`;
20
- function validBoot(value) {
21
- return value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === 3
22
- && value.host !== '0'.repeat(32) && value.id !== '00000000-0000-0000-0000-000000000000'
23
- && typeof value.host === 'string' && /^[0-9a-f]{32}$/.test(value.host)
24
- && typeof value.id === 'string'
25
- && ((value.source === 'linux-boot-id' && /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(value.id))
26
- || (value.source === 'windows-wmi-boot-time' && /^[1-9][0-9]{17}$/.test(value.id)));
27
- }
28
- async function bootIdentity({ platform = process.platform, environment = process.env, execImpl = execFile,
29
- readImpl = fs.readFile } = {}) {
30
- let result;
31
- try {
32
- if (platform === 'linux') result = { source: 'linux-boot-id',
33
- host: (await readImpl('/etc/machine-id', 'utf8')).trim(),
34
- id: (await readImpl('/proc/sys/kernel/random/boot_id', 'utf8')).trim() };
35
- else if (platform === 'win32') {
36
- const root = environment.SystemRoot || environment.SYSTEMROOT || 'C:\\Windows';
37
- if (!/^[A-Za-z]:\\[^\x00-\x1f"*?<>|]*$/.test(root)) return null;
38
- const id = await new Promise(resolve => execImpl(path.win32.join(root, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
39
- ['-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(SCRIPT, 'utf16le').toString('base64')],
40
- { env: { SystemRoot: root }, shell: false, windowsHide: true, timeout: 5000, maxBuffer: 4096, encoding: 'utf8' },
41
- (error, stdout) => resolve(error ? '' : String(stdout).trim())));
42
- const [host, ticks, extra] = id.split(':');
43
- if (extra !== undefined || !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(host || '')) return null;
44
- result = { source: 'windows-wmi-boot-time', host: host.replace(/-/g, ''), id: ticks };
45
- }
46
- } catch { return null; }
47
- return validBoot(result) ? result : null;
48
- }
49
- function previousBoot(saved, current) {
50
- if (!validBoot(saved) || !validBoot(current) || saved.source !== current.source || saved.host !== current.host) return false;
51
- if (saved.source === 'linux-boot-id') return saved.id !== current.id;
52
- return BigInt(saved.id) < BigInt(current.id);
53
- }
54
- module.exports = { bootIdentity, validBoot, previousBoot };
1
+ 'use strict';
2
+ const fs = require('node:fs/promises');
3
+ const path = require('node:path');
4
+ const { execFile } = require('node:child_process');
5
+ // WMI LastBootUpTime is the OS boot time, not wall-clock minus uptime:
6
+ // https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem
7
+ // Use the framework WMI API directly, with no profile, PATH or module lookup.
8
+ const SCRIPT = String.raw`$ErrorActionPreference='Stop'
9
+ Add-Type -AssemblyName System.Management
10
+ $q = [System.Management.ManagementObjectSearcher]::new('SELECT LastBootUpTime FROM Win32_OperatingSystem')
11
+ try {
12
+ $rows = @($q.Get())
13
+ if ($rows.Count -ne 1) { throw 'ambiguous operating system' }
14
+ $r = $rows[0]
15
+ $t = [System.Management.ManagementDateTimeConverter]::ToDateTime([string]$r.LastBootUpTime).ToUniversalTime().Ticks
16
+ $key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SOFTWARE\Microsoft\Cryptography')
17
+ try { $hostId = [string]$key.GetValue('MachineGuid') } finally { $key.Dispose() }
18
+ [Console]::Out.Write($hostId.ToLowerInvariant() + ':' + $t.ToString([Globalization.CultureInfo]::InvariantCulture))
19
+ } finally { $q.Dispose() }`;
20
+ function validBoot(value) {
21
+ return value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === 3
22
+ && value.host !== '0'.repeat(32) && value.id !== '00000000-0000-0000-0000-000000000000'
23
+ && typeof value.host === 'string' && /^[0-9a-f]{32}$/.test(value.host)
24
+ && typeof value.id === 'string'
25
+ && ((value.source === 'linux-boot-id' && /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(value.id))
26
+ || (value.source === 'windows-wmi-boot-time' && /^[1-9][0-9]{17}$/.test(value.id)));
27
+ }
28
+ async function bootIdentity({ platform = process.platform, environment = process.env, execImpl = execFile,
29
+ readImpl = fs.readFile } = {}) {
30
+ let result;
31
+ try {
32
+ if (platform === 'linux') result = { source: 'linux-boot-id',
33
+ host: (await readImpl('/etc/machine-id', 'utf8')).trim(),
34
+ id: (await readImpl('/proc/sys/kernel/random/boot_id', 'utf8')).trim() };
35
+ else if (platform === 'win32') {
36
+ const root = environment.SystemRoot || environment.SYSTEMROOT || 'C:\\Windows';
37
+ if (!/^[A-Za-z]:\\[^\x00-\x1f"*?<>|]*$/.test(root)) return null;
38
+ const id = await new Promise(resolve => execImpl(path.win32.join(root, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
39
+ ['-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(SCRIPT, 'utf16le').toString('base64')],
40
+ { env: { SystemRoot: root }, shell: false, windowsHide: true, timeout: 5000, maxBuffer: 4096, encoding: 'utf8' },
41
+ (error, stdout) => resolve(error ? '' : String(stdout).trim())));
42
+ const [host, ticks, extra] = id.split(':');
43
+ if (extra !== undefined || !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(host || '')) return null;
44
+ result = { source: 'windows-wmi-boot-time', host: host.replace(/-/g, ''), id: ticks };
45
+ }
46
+ } catch { return null; }
47
+ return validBoot(result) ? result : null;
48
+ }
49
+ function previousBoot(saved, current) {
50
+ if (!validBoot(saved) || !validBoot(current) || saved.source !== current.source || saved.host !== current.host) return false;
51
+ if (saved.source === 'linux-boot-id') return saved.id !== current.id;
52
+ return BigInt(saved.id) < BigInt(current.id);
53
+ }
54
+ module.exports = { bootIdentity, validBoot, previousBoot };