Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix package in the browser #19

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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