Skip to content

Commit

Permalink
Merge pull request #19 from kemkriszt/dev
Browse files Browse the repository at this point in the history
Fix package in the browser
  • Loading branch information
kemkriszt authored Jan 1, 2024
2 parents f6291d9 + f457da2 commit 2f052a4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-balloons-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"label-printer": patch
---

Make package not include `usb` in the browser
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
"license": "ISC",
"repository": "https://github.com/kemkriszt/raw-thermal-print.git",
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@types/jest": "^29.5.11",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"@changesets/cli": "^2.27.1"
"typescript": "^5.3.3"
},
"dependencies": {
"@types/w3c-web-usb": "^1.0.10",
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/USBUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { WebUSB } from "usb";
import StringUtils from "./StringUtils";

const unsupportedUsbError = "usb-unsupported"
Expand All @@ -12,14 +11,15 @@ let usbAgent: USB
const getUSB = async (): Promise<USB> => {
if(usbAgent) return usbAgent

if(typeof navigator !== "undefined") {
if(typeof window !== "undefined") {
if(navigator.usb) {
usbAgent = navigator.usb
} else {
throw unsupportedUsbError
}
} else {
const { WebUSB } = await import("usb")
// TODO: Check how to avoid eval
const { WebUSB } = eval("require")("usb")
usbAgent = new WebUSB({allowAllDevices: true})
}

Expand All @@ -45,7 +45,7 @@ export const getDevices = async (): Promise<UsbDevice[]> => {
*/
export const requestDevice = async (): Promise<UsbDevice|undefined> => {
const agent = await getUSB()
const device = await agent.requestDevice()
const device = await agent.requestDevice({filters: []})
if(device) {
return new UsbDevice(device)
} else {
Expand Down

0 comments on commit 2f052a4

Please sign in to comment.