Skip to content

Commit

Permalink
Merge pull request #15 from yjl9903/main
Browse files Browse the repository at this point in the history
fix: remove circular deps
  • Loading branch information
hydrati authored Jan 22, 2024
2 parents 764c038 + 4947c12 commit 17b28f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
25 changes: 1 addition & 24 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,4 @@ export const useTimeout = <T>(
}),
])

export const decodeMessageData = (data: any) => {
if (typeof data == 'string') {
return data
} else if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
return new TextDecoder().decode(data)
} else if (data.buffer instanceof ArrayBuffer) {
return new TextDecoder().decode(data.buffer)
} else if (Array.isArray(data)) {
const out: string[] = []
const decoder = new TextDecoder()
for (const chunk of data) {
if (typeof chunk == 'string') {
out.push(chunk)
} else if (chunk instanceof Uint8Array || chunk instanceof ArrayBuffer) {
out.push(decoder.decode(chunk))
} else if (chunk.buffer instanceof ArrayBuffer) {
out.push(decoder.decode(chunk.buffer))
}
}
return out.join('')
} else {
throw new Error('[maria2 error] Message data cannot be decoded')
}
}
export { decodeMessageData } from './shims/decode.ts'
24 changes: 24 additions & 0 deletions src/shims/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const decodeMessageData = (data: any) => {
if (typeof data == 'string') {
return data
} else if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
return new TextDecoder().decode(data)
} else if (data.buffer instanceof ArrayBuffer) {
return new TextDecoder().decode(data.buffer)
} else if (Array.isArray(data)) {
const out: string[] = []
const decoder = new TextDecoder()
for (const chunk of data) {
if (typeof chunk == 'string') {
out.push(chunk)
} else if (chunk instanceof Uint8Array || chunk instanceof ArrayBuffer) {
out.push(decoder.decode(chunk))
} else if (chunk.buffer instanceof ArrayBuffer) {
out.push(decoder.decode(chunk.buffer))
}
}
return out.join('')
} else {
throw new Error('[maria2 error] Message data cannot be decoded')
}
}
2 changes: 1 addition & 1 deletion src/shims/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decodeMessageData } from '../shared.ts'
import { decodeMessageData } from './decode.ts'

export const randomUUID = await (async () => {
const nodeCrypto = await import('node:crypto')
Expand Down

0 comments on commit 17b28f4

Please sign in to comment.