win-guid 0.1.3
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.txt +9 -0
- package/README.md +85 -0
- package/lib/guid.d.ts +20 -0
- package/lib/guid.js +107 -0
- package/package.json +48 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2026 Borewit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
[](https://npmjs.org/package/win-guid)
|
|
2
|
+
[](https://github.com/Borewit/win-guid/actions/workflows/nodejs-ci.yml)
|
|
3
|
+
[](https://npmcharts.com/compare/win-guid?start=365)
|
|
4
|
+
|
|
5
|
+
# win-guid
|
|
6
|
+
|
|
7
|
+
Small, dependency-free utility for working with **Windows / CFBF GUIDs** in JavaScript and TypeScript.
|
|
8
|
+
|
|
9
|
+
It parses canonical GUID strings into the **Windows byte layout** used by COM, OLE, and Compound File Binary Format (CFBF), and converts them back to the standard string form when needed.
|
|
10
|
+
|
|
11
|
+
This is useful when dealing with Microsoft file formats such as `.asf`, `.doc,`, `.xls`, `.ppt`, structured storage,
|
|
12
|
+
or other binary formats that store GUIDs in little-endian Windows order.
|
|
13
|
+
|
|
14
|
+
For RFC9562 compliant UUIDs (network byte order), use [uuid](https://github.com/uuidjs/uuid) instead.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install win-guid
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Parse a GUID string
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import { parseWindowsGuid } from "win-guid";
|
|
28
|
+
|
|
29
|
+
const bytes = parseWindowsGuid("00020906-0000-0000-C000-000000000046");
|
|
30
|
+
|
|
31
|
+
// Uint8Array(16) in Windows / CFBF byte order
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Use the Guid helper class
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { Guid } from "win-guid";
|
|
38
|
+
|
|
39
|
+
const guid = Guid.fromString("00020906-0000-0000-C000-000000000046");
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
`parseWindowsGuid(guid: string): Uint8Array`
|
|
45
|
+
|
|
46
|
+
Parses a canonical GUID string:
|
|
47
|
+
```js
|
|
48
|
+
const bytes = parseWindowsGuid("00020906-0000-0000-C000-000000000046");
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
into a 16-byte Uint8Array using Windows / CFBF byte order.
|
|
52
|
+
|
|
53
|
+
- Input is validated strictly
|
|
54
|
+
- Case-insensitive
|
|
55
|
+
- Throws Error on invalid input
|
|
56
|
+
|
|
57
|
+
`class Guid`
|
|
58
|
+
|
|
59
|
+
Creates a GUID from a canonical GUID string.
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const guid = Guid.fromString("00020906-0000-0000-C000-000000000046");
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`guid.toString(): string`
|
|
66
|
+
|
|
67
|
+
Converts the GUID back into the canonical string form.
|
|
68
|
+
|
|
69
|
+
- Always uppercase
|
|
70
|
+
- Round-trips cleanly with fromString
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
guid.toString();
|
|
74
|
+
````
|
|
75
|
+
Outputs something like:
|
|
76
|
+
```
|
|
77
|
+
00020906-0000-0000-C000-000000000046`
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
## Licence
|
|
84
|
+
|
|
85
|
+
This project is licensed under the [MIT License](LICENSE.txt). Feel free to use, modify, and distribute as needed.
|
package/lib/guid.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse canonical GUID string (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
|
|
3
|
+
* into Windows / CFBF byte order.
|
|
4
|
+
*/
|
|
5
|
+
export declare function parseWindowsGuid(guid: string): Uint8Array;
|
|
6
|
+
export declare class Guid {
|
|
7
|
+
private readonly bytes;
|
|
8
|
+
constructor(bytes: Uint8Array);
|
|
9
|
+
static fromString(guid: string): Guid;
|
|
10
|
+
/**
|
|
11
|
+
* Convert Windows / CFBF byte order into canonical GUID string:
|
|
12
|
+
* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
13
|
+
*/
|
|
14
|
+
toString(): string;
|
|
15
|
+
/**
|
|
16
|
+
* Compare against a Uint8Array containing GUID bytes
|
|
17
|
+
* in Windows / CFBF layout.
|
|
18
|
+
*/
|
|
19
|
+
equals(buf: Uint8Array, offset?: number): boolean;
|
|
20
|
+
}
|
package/lib/guid.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse canonical GUID string (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
|
|
3
|
+
* into Windows / CFBF byte order.
|
|
4
|
+
*/
|
|
5
|
+
export function parseWindowsGuid(guid) {
|
|
6
|
+
let s = guid.trim();
|
|
7
|
+
// Keep validation readable and strict, avoid lowercasing allocations
|
|
8
|
+
if (s.length !== 36 ||
|
|
9
|
+
s[8] !== "-" ||
|
|
10
|
+
s[13] !== "-" ||
|
|
11
|
+
s[18] !== "-" ||
|
|
12
|
+
s[23] !== "-") {
|
|
13
|
+
throw new Error(`Invalid GUID format: ${guid}`);
|
|
14
|
+
}
|
|
15
|
+
let v;
|
|
16
|
+
const out = new Uint8Array(16);
|
|
17
|
+
// Data1: 8 hex, uint32 little-endian
|
|
18
|
+
v = parseInt(s.slice(0, 8), 16);
|
|
19
|
+
out[0] = v & 0xff;
|
|
20
|
+
out[1] = (v >>> 8) & 0xff;
|
|
21
|
+
out[2] = (v >>> 16) & 0xff;
|
|
22
|
+
out[3] = (v >>> 24) & 0xff;
|
|
23
|
+
// Data2: 4 hex, uint16 little-endian
|
|
24
|
+
v = parseInt(s.slice(9, 13), 16);
|
|
25
|
+
out[4] = v & 0xff;
|
|
26
|
+
out[5] = (v >>> 8) & 0xff;
|
|
27
|
+
// Data3: 4 hex, uint16 little-endian
|
|
28
|
+
v = parseInt(s.slice(14, 18), 16);
|
|
29
|
+
out[6] = v & 0xff;
|
|
30
|
+
out[7] = (v >>> 8) & 0xff;
|
|
31
|
+
// Data4: 4 hex, as-is (string order)
|
|
32
|
+
v = parseInt(s.slice(19, 23), 16);
|
|
33
|
+
out[8] = (v >>> 8) & 0xff;
|
|
34
|
+
out[9] = v & 0xff;
|
|
35
|
+
// Data5: 12 hex, 6 bytes, as-is (string order)
|
|
36
|
+
// Parse as two chunks to avoid any precision worries, keep it simple.
|
|
37
|
+
v = parseInt(s.slice(24, 32), 16); // 8 hex -> 4 bytes
|
|
38
|
+
out[10] = (v >>> 24) & 0xff;
|
|
39
|
+
out[11] = (v >>> 16) & 0xff;
|
|
40
|
+
out[12] = (v >>> 8) & 0xff;
|
|
41
|
+
out[13] = v & 0xff;
|
|
42
|
+
v = parseInt(s.slice(32, 36), 16); // 4 hex -> 2 bytes
|
|
43
|
+
out[14] = (v >>> 8) & 0xff;
|
|
44
|
+
out[15] = v & 0xff;
|
|
45
|
+
// Ensure all parsed parts were valid hex (parseInt can yield NaN)
|
|
46
|
+
for (let i = 0; i < 16; i++) {
|
|
47
|
+
if (!Number.isFinite(out[i])) {
|
|
48
|
+
throw new Error(`Invalid GUID format: ${guid}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Also catch NaN early (more useful error locality)
|
|
52
|
+
// If any parseInt produced NaN, assignments above would have become 0,
|
|
53
|
+
// so instead validate hex characters directly with a lightweight check.
|
|
54
|
+
// (Keeps code small while staying strict.)
|
|
55
|
+
if (!/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(s)) {
|
|
56
|
+
throw new Error(`Invalid GUID format: ${guid}`);
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
export class Guid {
|
|
61
|
+
constructor(bytes) {
|
|
62
|
+
if (bytes.length !== 16)
|
|
63
|
+
throw new Error("GUID must be exactly 16 bytes");
|
|
64
|
+
this.bytes = bytes;
|
|
65
|
+
}
|
|
66
|
+
static fromString(guid) {
|
|
67
|
+
return new Guid(parseWindowsGuid(guid));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Convert Windows / CFBF byte order into canonical GUID string:
|
|
71
|
+
* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
72
|
+
*/
|
|
73
|
+
toString() {
|
|
74
|
+
const b = this.bytes;
|
|
75
|
+
const hx = (n) => n.toString(16).padStart(2, "0");
|
|
76
|
+
// Data1 (uint32 LE) -> big-endian text
|
|
77
|
+
const g1 = hx(b[3]) + hx(b[2]) + hx(b[1]) + hx(b[0]);
|
|
78
|
+
// Data2 (uint16 LE)
|
|
79
|
+
const g2 = hx(b[5]) + hx(b[4]);
|
|
80
|
+
// Data3 (uint16 LE)
|
|
81
|
+
const g3 = hx(b[7]) + hx(b[6]);
|
|
82
|
+
// Data4 (as-is)
|
|
83
|
+
const g4 = hx(b[8]) + hx(b[9]);
|
|
84
|
+
// Data5 (as-is)
|
|
85
|
+
const g5 = hx(b[10]) +
|
|
86
|
+
hx(b[11]) +
|
|
87
|
+
hx(b[12]) +
|
|
88
|
+
hx(b[13]) +
|
|
89
|
+
hx(b[14]) +
|
|
90
|
+
hx(b[15]);
|
|
91
|
+
return `${g1}-${g2}-${g3}-${g4}-${g5}`.toUpperCase();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Compare against a Uint8Array containing GUID bytes
|
|
95
|
+
* in Windows / CFBF layout.
|
|
96
|
+
*/
|
|
97
|
+
equals(buf, offset = 0) {
|
|
98
|
+
if (offset < 0 || buf.length - offset < 16)
|
|
99
|
+
return false;
|
|
100
|
+
const a = this.bytes;
|
|
101
|
+
for (let i = 0; i < 16; i++) {
|
|
102
|
+
if (buf[offset + i] !== a[i])
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "win-guid",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Windows GUID",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./lib/guid.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'src/**/*.d.ts'",
|
|
9
|
+
"compile-src": "tsc -p lib",
|
|
10
|
+
"compile": "yarn run compile-src",
|
|
11
|
+
"format": "biome format",
|
|
12
|
+
"lint:ts": "biome check",
|
|
13
|
+
"build": "yarn run clean && yarn run compile",
|
|
14
|
+
"test": "mocha",
|
|
15
|
+
"prepublishOnly": "yarn run build",
|
|
16
|
+
"update-biome": "yarn add -D --exact @biomejs/biome && npx @biomejs/biome migrate --write"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"lib/**/*.js",
|
|
20
|
+
"lib/**/*.d.ts",
|
|
21
|
+
"lib/*.cjs"
|
|
22
|
+
],
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Borewit",
|
|
25
|
+
"url": "https://github.com/Borewit"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/Borewit/win-guid.git"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/chai": "^5.2.3",
|
|
34
|
+
"@types/mocha": "^10.0.10",
|
|
35
|
+
"biome": "^0.3.3",
|
|
36
|
+
"chai": "^6.2.2",
|
|
37
|
+
"del-cli": "^7.0.0",
|
|
38
|
+
"mocha": "^11.7.5",
|
|
39
|
+
"typescript": "^5.9.3"
|
|
40
|
+
},
|
|
41
|
+
"main": "index.js",
|
|
42
|
+
"packageManager": "yarn@4.9.1",
|
|
43
|
+
"keywords": [
|
|
44
|
+
"GUID",
|
|
45
|
+
"Windows",
|
|
46
|
+
"COM"
|
|
47
|
+
]
|
|
48
|
+
}
|