@neko-makura/railid 0.0.0-stage → 1.1.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 +21 -0
- package/README.md +143 -2
- package/index.d.ts +46 -0
- package/index.js +73 -0
- package/package.json +38 -4
- package/railid-tpm.linux-x64-gnu.node +0 -0
- package/railid.darwin-arm64.node +0 -0
- package/railid.darwin-x64.node +0 -0
- package/railid.linux-x64-gnu.node +0 -0
- package/railid.win32-x64-msvc.node +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nekomakura
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,144 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @neko-makura/railid (Node.js)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Native N-API bindings for [RailID](https://crates.io/crates/railid).
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @neko-makura/railid
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
The package includes prebuilt N-API binaries. Node.js 18 or newer is required.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const railid = require('@neko-makura/railid');
|
|
15
|
+
|
|
16
|
+
console.log(railid.version());
|
|
17
|
+
console.log(railid.backend()); // "tpm" or "os-only"
|
|
18
|
+
|
|
19
|
+
// Standard TPM hybrid profile. Throws if the backend is OS-only or no TPM
|
|
20
|
+
// device is available.
|
|
21
|
+
const id = railid.generate();
|
|
22
|
+
console.log(id);
|
|
23
|
+
|
|
24
|
+
// Explicitly degraded OS-only profile.
|
|
25
|
+
const osId = railid.generateOsOnly();
|
|
26
|
+
|
|
27
|
+
// Synchronous batch.
|
|
28
|
+
const ids = railid.generateMany(1000, true);
|
|
29
|
+
const batch = railid.generateManyEncoded(1000, true);
|
|
30
|
+
// batch is a Buffer of 1000 * 32 ASCII bytes.
|
|
31
|
+
|
|
32
|
+
// Asynchronous batch: runs on the libuv thread pool.
|
|
33
|
+
const asyncIds = await railid.generateManyAsync(1000, true);
|
|
34
|
+
const asyncBatch = await railid.generateManyEncodedAsync(1000, true);
|
|
35
|
+
|
|
36
|
+
const decoded = railid.decode(id);
|
|
37
|
+
console.log(decoded.timestampMs, decoded.randomHex, decoded.encoded);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
| Function | Result | Notes |
|
|
43
|
+
|---|---|---|
|
|
44
|
+
| `generate()` | `string` | TPM hybrid profile; throws `TpmUnavailable` if unavailable |
|
|
45
|
+
| `generateOsOnly()` | `string` | Explicit degraded profile |
|
|
46
|
+
| `generateBuffer()` | `Buffer` | TPM profile as 32 ASCII bytes, no JS string |
|
|
47
|
+
| `generateOsOnlyBuffer()` | `Buffer` | OS-only as 32 ASCII bytes, no JS string |
|
|
48
|
+
| `generateMany(count, osOnly?)` | `string[]` | Sync; `osOnly` defaults to `false` |
|
|
49
|
+
| `generateManyEncoded(count, osOnly?)` | `Buffer` | Sync; `count * 32` bytes |
|
|
50
|
+
| `generateManyAsync(count, osOnly?)` | `Promise<string[]>` | Bulk work off the JS thread |
|
|
51
|
+
| `generateManyEncodedAsync(count, osOnly?)` | `Promise<Buffer>` | Bulk work off the JS thread |
|
|
52
|
+
| `decode(input)` | `{ timestampMs, randomHex, encoded }` | Validates canonical Base64URL |
|
|
53
|
+
| `version()` | `string` | RailID crate version |
|
|
54
|
+
| `backend()` | `"tpm" \| "os-only"` | Backend compiled into the loaded binary |
|
|
55
|
+
|
|
56
|
+
Limits:
|
|
57
|
+
|
|
58
|
+
- OS-only strings: 100,000 IDs
|
|
59
|
+
- OS-only encoded: 1,000,000 IDs
|
|
60
|
+
- TPM: 1,024 IDs
|
|
61
|
+
|
|
62
|
+
Counts above the limit are rejected before allocation.
|
|
63
|
+
|
|
64
|
+
`generateBuffer()` avoids the JS string but still allocates a Node Buffer per
|
|
65
|
+
call; for throughput use `generateManyEncoded()` or
|
|
66
|
+
`generateManyEncodedAsync()`, which allocate one contiguous buffer.
|
|
67
|
+
|
|
68
|
+
## Caller-provided buffer API (design, not shipped)
|
|
69
|
+
|
|
70
|
+
A zero-allocation single-ID API would look like:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
generateInto(target: Buffer, offset?: number): number;
|
|
74
|
+
generateOsOnlyInto(target: Buffer, offset?: number): number;
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
- `offset` defaults to 0 and must satisfy `offset + 32 <= target.length`.
|
|
78
|
+
- The function writes 32 ASCII bytes and returns the end offset.
|
|
79
|
+
- It performs no allocation and no JS string creation.
|
|
80
|
+
- It is synchronous only.
|
|
81
|
+
|
|
82
|
+
This is **not shipped** because N-API buffers can be shared with other JS
|
|
83
|
+
execution contexts, and napi-rs documents mutable `Buffer` access as an aliasing
|
|
84
|
+
hazard. Shipping it safely needs an exclusive-ownership guarantee, a rejection
|
|
85
|
+
of `SharedArrayBuffer`-backed buffers, or a custom writer object. Until then,
|
|
86
|
+
use the batch APIs.
|
|
87
|
+
|
|
88
|
+
## Linux and TPM
|
|
89
|
+
|
|
90
|
+
The Linux TPM binary links against `libtss2` (`tss-esapi`). The package ships
|
|
91
|
+
two Linux x64 binaries:
|
|
92
|
+
|
|
93
|
+
- `railid-tpm.linux-x64-gnu.node` — TPM backend; needs `libtss2`
|
|
94
|
+
- `railid.linux-x64-gnu.node` — OS-only; no TSS dependency
|
|
95
|
+
|
|
96
|
+
`index.js` tries the TPM binary first and falls back to the OS-only binary when
|
|
97
|
+
the TPM binary cannot be loaded (for example, when `libtss2` is missing). In
|
|
98
|
+
that case `generate()` throws `TpmUnavailable`; `generateOsOnly()` still works.
|
|
99
|
+
This preserves fail-closed behavior.
|
|
100
|
+
|
|
101
|
+
Debian/Ubuntu TPM users need:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
sudo apt install libtss2
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Windows uses the system TBS service (`Tbs.dll`) and needs no extra DLL. macOS
|
|
108
|
+
ships the OS-only backend.
|
|
109
|
+
|
|
110
|
+
## Build from source
|
|
111
|
+
|
|
112
|
+
Requirements:
|
|
113
|
+
|
|
114
|
+
- Rust 1.88+
|
|
115
|
+
- Linux TPM build: `pkg-config`, `libtss2-dev`
|
|
116
|
+
- Node.js 18+
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
npm run build # platform default
|
|
120
|
+
npm run build -- --os-only
|
|
121
|
+
npm test
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
On Linux the default build produces both TPM and OS-only binaries. On Windows
|
|
125
|
+
it produces the TPM binary. On macOS it produces the OS-only binary.
|
|
126
|
+
|
|
127
|
+
## Benchmarks
|
|
128
|
+
|
|
129
|
+
The Node addon benchmark against UUIDv4 and NanoID is in
|
|
130
|
+
[`bench/`](bench/README.md). It includes the latest Windows result.
|
|
131
|
+
|
|
132
|
+
## Security notes
|
|
133
|
+
|
|
134
|
+
RailID is an identifier generator. This package does not provide single-use
|
|
135
|
+
tokens, expiry, or revocation; enforce those in your application. A RailID
|
|
136
|
+
discloses its creation time at 100 ms precision. TPM presence does not prove
|
|
137
|
+
the firmware or boot chain is trustworthy.
|
|
138
|
+
|
|
139
|
+
TPM access is serialized inside the process. Use the async batch APIs for
|
|
140
|
+
server workloads so the event loop is not blocked by large batches.
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT. Copyright (c) 2026 Nekomakura. See [LICENSE](LICENSE).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export type Backend = "tpm" | "os-only";
|
|
2
|
+
|
|
3
|
+
export interface DecodedRailId {
|
|
4
|
+
/** Unix timestamp in milliseconds. */
|
|
5
|
+
timestampMs: number;
|
|
6
|
+
/** 150-bit random field as 38 lowercase hex characters. */
|
|
7
|
+
randomHex: string;
|
|
8
|
+
/** Canonical 32-character Base64URL encoding. */
|
|
9
|
+
encoded: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Generate one RailID with the standard TPM hybrid profile. */
|
|
13
|
+
export declare function generate(): string;
|
|
14
|
+
|
|
15
|
+
/** Generate one RailID with the explicitly degraded OS-only profile. */
|
|
16
|
+
export declare function generateOsOnly(): string;
|
|
17
|
+
|
|
18
|
+
/** Generate one TPM-profile RailID as 32 ASCII bytes, without a JS string. */
|
|
19
|
+
export declare function generateBuffer(): Buffer;
|
|
20
|
+
|
|
21
|
+
/** Generate one OS-only RailID as 32 ASCII bytes, without a JS string. */
|
|
22
|
+
export declare function generateOsOnlyBuffer(): Buffer;
|
|
23
|
+
|
|
24
|
+
/** Generate `count` IDs synchronously as strings. */
|
|
25
|
+
export declare function generateMany(count: number, osOnly?: boolean): string[];
|
|
26
|
+
|
|
27
|
+
/** Generate `count` IDs synchronously as one contiguous `count * 32` Buffer. */
|
|
28
|
+
export declare function generateManyEncoded(count: number, osOnly?: boolean): Buffer;
|
|
29
|
+
|
|
30
|
+
/** Generate `count` IDs asynchronously as strings. */
|
|
31
|
+
export declare function generateManyAsync(count: number, osOnly?: boolean): Promise<string[]>;
|
|
32
|
+
|
|
33
|
+
/** Generate `count` IDs asynchronously as one contiguous `count * 32` Buffer. */
|
|
34
|
+
export declare function generateManyEncodedAsync(
|
|
35
|
+
count: number,
|
|
36
|
+
osOnly?: boolean,
|
|
37
|
+
): Promise<Buffer>;
|
|
38
|
+
|
|
39
|
+
/** Decode and validate a RailID. */
|
|
40
|
+
export declare function decode(input: string): DecodedRailId;
|
|
41
|
+
|
|
42
|
+
/** RailID crate version. */
|
|
43
|
+
export declare function version(): string;
|
|
44
|
+
|
|
45
|
+
/** Native backend compiled into the loaded addon. */
|
|
46
|
+
export declare function backend(): Backend;
|
package/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
|
|
5
|
+
function linuxLibc() {
|
|
6
|
+
if (process.platform !== 'linux') {
|
|
7
|
+
return '';
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const report = process.report && process.report.getReport();
|
|
11
|
+
const version = report && report.header && report.header.glibcVersionRuntime;
|
|
12
|
+
return version ? 'gnu' : 'musl';
|
|
13
|
+
} catch {
|
|
14
|
+
return 'gnu';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function suffix() {
|
|
19
|
+
const platform = process.platform;
|
|
20
|
+
const arch = process.arch;
|
|
21
|
+
if (platform === 'win32') {
|
|
22
|
+
return `win32-${arch}-msvc`;
|
|
23
|
+
}
|
|
24
|
+
if (platform === 'linux') {
|
|
25
|
+
return `linux-${arch}-${linuxLibc()}`;
|
|
26
|
+
}
|
|
27
|
+
if (platform === 'darwin') {
|
|
28
|
+
return `darwin-${arch}`;
|
|
29
|
+
}
|
|
30
|
+
return `${platform}-${arch}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function candidates() {
|
|
34
|
+
const tag = suffix();
|
|
35
|
+
if (process.platform === 'win32') {
|
|
36
|
+
return [`railid.${tag}.node`];
|
|
37
|
+
}
|
|
38
|
+
if (process.platform === 'linux') {
|
|
39
|
+
return [`railid-tpm.${tag}.node`, `railid.${tag}.node`];
|
|
40
|
+
}
|
|
41
|
+
return [`railid.${tag}.node`];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let binding = null;
|
|
45
|
+
let backend = null;
|
|
46
|
+
const loadErrors = [];
|
|
47
|
+
|
|
48
|
+
for (const file of candidates()) {
|
|
49
|
+
try {
|
|
50
|
+
binding = require(path.join(__dirname, file));
|
|
51
|
+
backend = typeof binding.backend === 'function' ? binding.backend() : 'unknown';
|
|
52
|
+
break;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
loadErrors.push(`${file}: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!binding) {
|
|
59
|
+
const error = new Error(
|
|
60
|
+
`No usable RailID native addon for ${process.platform}/${process.arch}. ` +
|
|
61
|
+
`Tried: ${candidates().join(', ')}. ` +
|
|
62
|
+
'Install a prebuilt package or run `npm run build` with Rust and the platform TPM libraries.'
|
|
63
|
+
);
|
|
64
|
+
error.cause = loadErrors.join('\n');
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = {
|
|
69
|
+
...binding,
|
|
70
|
+
// `backend` is the backend compiled into the loaded binary. A TPM build can
|
|
71
|
+
// still return TpmUnavailable at runtime when no TPM device is present.
|
|
72
|
+
backend: () => backend,
|
|
73
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neko-makura/railid",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Native N-API bindings for RailID: 192-bit timestamp-first identifiers with TPM 2.0 + OS CSPRNG randomness",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/nekomakura-nyan/rail-id.git",
|
|
11
|
+
"directory": "node"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/nekomakura-nyan/rail-id",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/nekomakura-nyan/rail-id/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"railid",
|
|
19
|
+
"identifier",
|
|
20
|
+
"uuid",
|
|
21
|
+
"tpm",
|
|
22
|
+
"napi",
|
|
23
|
+
"native"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"index.js",
|
|
30
|
+
"index.d.ts",
|
|
31
|
+
"*.node",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "node scripts/build.js",
|
|
37
|
+
"test": "node test.js",
|
|
38
|
+
"pack:check": "npm pack --dry-run"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|