Skip to content

Commit

Permalink
Implement writing and reading values.
Browse files Browse the repository at this point in the history
  • Loading branch information
tec27 committed Jan 16, 2024
1 parent bc48d0b commit 40a58e0
Show file tree
Hide file tree
Showing 9 changed files with 603 additions and 18 deletions.
55 changes: 55 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export class WindowsRegistry {
/**
* Constructs a new `WindowsRegistry` instance. This will start a background thread to perform
* operations that will live as long as this instance does.
*/
constructor()
/**
* Closes the registry, ending its background thread immediately. This is not necessary to call,
* as the background thread will be automatically ended upon garbage collection, but can be used
* if you would like to exit the process immediately.
*/
close(): void

read(hive: Hkey, key: string, name: string): Promise<RegistryDataTypeJs[RegistryDataType]>
write<R extends RegistryDataType>(
hive: Hkey,
key: string,
name: string,
type: R,
value: RegistryDataTypeJs[R],
): Promise<void>
}

export const HKCU = 'HKEY_CURRENT_USER'
export const HKLM = 'HKEY_LOCAL_MACHINE'
export const HKCR = 'HKEY_CLASSES_ROOT'
export const HKU = 'HKEY_USERS'
export const HKCC = 'HKEY_CURRENT_CONFIG'
export type Hkey = typeof HKCU | typeof HKLM | typeof HKCR | typeof HKU | typeof HKCC

export const REG_NONE = 'REG_NONE'
export const REG_SZ = 'REG_SZ'
export const REG_MULTI_SZ = 'REG_MULTI_SZ'
export const REG_EXPAND_SZ = 'REG_EXPAND_SZ'
export const REG_DWORD = 'REG_DWORD'
export const REG_QWORD = 'REG_QWORD'
export const REG_BINARY = 'REG_BINARY'
export type RegistryDataType =
| typeof REG_NONE
| typeof REG_SZ
| typeof REG_MULTI_SZ
| typeof REG_EXPAND_SZ
| typeof REG_DWORD
| typeof REG_QWORD
| typeof REG_BINARY

interface RegistryDataTypeJs extends Record<RegistryDataType, any> {
[REG_NONE]: undefined | null
[REG_SZ]: string
[REG_MULTI_SZ]: string[]
[REG_EXPAND_SZ]: string
[REG_DWORD]: number
[REG_QWORD]: number
[REG_BINARY]: Uint8Array
}
48 changes: 48 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { registryNew, registryClose, registryRead, registryWrite } = require('./native.js')

class WindowsRegistry {
/**
* Constructs a new `WindowsRegistry` instance. This will start a background thread to perform
* operations that will live as long as this instance does.
*/
constructor() {
this.registry = registryNew()
}

/**
* Closes the registry, ending its background thread immediately. This is not necessary to call,
* as the background thread will be automatically ended upon garbage collection, but can be used
* if you would like to exit the process immediately.
*/
close() {
registryClose.call(this.registry)
}

read(hive, key, value) {
return registryRead.call(this.registry, hive, key, value)
}

write(hive, key, value, type, data) {
return registryWrite.call(this.registry, hive, key, value, type, data)
}
}

module.exports = {
WindowsRegistry,

HKCU: 'HKEY_CURRENT_USER',
HKLM: 'HKEY_LOCAL_MACHINE',
HKCR: 'HKEY_CLASSES_ROOT',
HKU: 'HKEY_USERS',
HKCC: 'HKEY_CURRENT_CONFIG',

REG_NONE: 'REG_NONE',
REG_SZ: 'REG_SZ',
REG_MULTI_SZ: 'REG_MULTI_SZ',
REG_EXPAND_SZ: 'REG_EXPAND_SZ',
REG_DWORD: 'REG_DWORD',
REG_QWORD: 'REG_QWORD',
REG_BINARY: 'REG_BINARY',

DEFAULT_VALUE: '',
}
9 changes: 9 additions & 0 deletions native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// TODO(tec27): Pick between 32 and 64-bit, as well as whether to use a locally built binary?
let native
try {
native = require('./prebuilds/x64.node')
} catch (e) {
native = require('./native/index.node')
}

module.exports = native
80 changes: 72 additions & 8 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "native"
name = "windows-registry"
version = "0.1.0"
description = "A Rust/neon-based node.js native module for accessing and modifying the Windows registry."
license = "MIT"
Expand All @@ -11,8 +11,10 @@ crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0"
registry = "1.2"
utfx = "0.1"

[dependencies.neon]
version = "0.10"
default-features = false
features = ["napi-6", "promise-api", "channel-api"]
features = ["napi-6", "channel-api", "promise-api", "try-catch-api"]
Loading

0 comments on commit 40a58e0

Please sign in to comment.