Skip to content

Commit

Permalink
feat(win32-api): breaking change return type of genUcsBufferFrom()
Browse files Browse the repository at this point in the history
remove null value
  • Loading branch information
waitingsong committed Jul 20, 2022
1 parent f2332f5 commit fb18c7e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/win32-api/src/func/winspool/index.winspool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert'

import { genUcsBufferFrom, ucsBufferToString } from '../../index.js'
import { getMod } from '../func.helper.js'

import { Win32Fns, dllName, M, ref } from './helper.js'
Expand Down Expand Up @@ -28,7 +29,7 @@ export async function winspoolGetDefaultPrinter(maxNameLength = 256): Promise<st
const pcch = pcchBuf.readUInt32LE()
if (pcch > 0) {
const size = pcch - 1
const psz = pszBuf.toString('ucs2', 0, size * 2).replace(/\0+$/u, '')
const psz = ucsBufferToString(pszBuf, size)
return psz
}
return ''
Expand All @@ -45,7 +46,7 @@ export async function winspoolOpenPrinter(printerName: string): Promise<M.HANDLE

assert(printerName)

const nameBuf = Buffer.from(printerName + '\0', 'ucs2')
const nameBuf = genUcsBufferFrom(printerName)
const ptr = Buffer.alloc(8)
const ret = await mod.OpenPrinterW(nameBuf, ptr, ref.NULL)
if (ret) {
Expand Down
4 changes: 2 additions & 2 deletions packages/win32-api/src/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ export function retrieveStructFromPtrAddress<R extends StructInstanceBase>(
}


export function genUcsBufferFrom(str: string | undefined | null): Buffer | null {
export function genUcsBufferFrom(str: string | undefined | null): Buffer {
if (typeof str === 'string' && str.length) {
return Buffer.from(str + '\0', 'ucs2')
}
return null
return ref.NULL
}

export function ucsBufferToString(buffer: Buffer, charCount?: number | undefined): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/win32-api/test/winspool/502.OpenPrinter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
describe(fileShortPath(import.meta.url), () => {

describe('Should work', () => {
it('normal', async () => {
it.only('normal', async () => {
const name = await winspoolGetDefaultPrinter()
assert(name)
const hWnd = await winspoolOpenPrinter(name)
Expand Down

0 comments on commit fb18c7e

Please sign in to comment.