• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taichunmin / chameleon-ultra.js / 15847482271

24 Jun 2025 10:04AM UTC coverage: 69.194% (+5.8%) from 63.351%
15847482271

push

github

web-flow
v0.3.30: change to vitest and lodash-es (#196)

502 of 657 branches covered (76.41%)

Branch coverage included in aggregate %.

12 of 18 new or added lines in 14 files covered. (66.67%)

218 existing lines in 5 files now uncovered.

2683 of 3946 relevant lines covered (67.99%)

3159783.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/plugin/DfuZip.ts
1
import { Buffer } from '@taichunmin/buffer'
×
2
import JSZip from 'jszip'
×
NEW
3
import * as _ from 'lodash-es'
×
4
import { setObject } from '../iifeExportHelper'
×
5

6
export default class DfuZip {
×
7
  readonly #buf: Buffer
×
8
  #zip: JSZip | null = null
×
9
  #manifest: DfuManifest | null = null
×
10

11
  constructor (buf: Buffer) {
×
12
    this.#buf = buf
×
13
  }
×
14

15
  async getManifest (): Promise<DfuManifest> {
×
16
    if (_.isNil(this.#zip)) this.#zip = await JSZip.loadAsync(this.#buf)
×
17
    if (_.isNil(this.#manifest)) {
×
18
      const manifestJson = await this.#zip.file('manifest.json')?.async('string')
×
19
      if (_.isNil(manifestJson)) throw new Error('Unable to find manifest, is this a proper DFU package?')
×
20
      this.#manifest = JSON.parse(manifestJson).manifest
×
21
    }
×
22
    return this.#manifest as DfuManifest
×
23
  }
×
24

25
  async getImage (types: DfuImageType[]): Promise<DfuImage | null> {
×
26
    const manifest = await this.getManifest()
×
27
    for (const type of types) {
×
28
      const image = manifest[type]
×
29
      if (_.isNil(image)) continue
×
30
      const [header, body] = await Promise.all(_.map([image.dat_file, image.bin_file], async file => {
×
31
        const u8 = await this.#zip?.file(file)?.async('uint8array')
×
32
        if (_.isNil(u8)) throw new Error(`Failed to read ${file} from DFU package`)
×
33
        return Buffer.fromView(u8)
×
34
      }))
×
35
      return { type, header, body }
×
36
    }
×
37
    return null
×
38
  }
×
39

40
  async getBaseImage (): Promise<DfuImage | null> {
×
41
    return await this.getImage(['softdevice', 'bootloader', 'softdevice_bootloader'])
×
42
  }
×
43

44
  async getAppImage (): Promise<DfuImage | null> {
×
45
    return await this.getImage(['application'])
×
46
  }
×
47

48
  async getGitVersion (): Promise<string | null> {
×
49
    const image = await this.getAppImage()
×
50
    if (_.isNil(image)) return null
×
51
    // eslint-disable-next-line no-control-regex
52
    return image.body.toString('utf8').match(/\x00(v\d+(?:\.\d+)*[\w-]*)\x00/)?.[1] ?? null
×
53
  }
×
54
}
×
55

56
setObject(globalThis, ['ChameleonUltraJS', 'DfuZip'], DfuZip)
×
57

58
/** @inline */
59
export interface DfuImage {
60
  type: DfuImageType
61
  header: Buffer
62
  body: Buffer
63
}
64

65
/** @inline */
66
type DfuManifest = Record<DfuImageType, { bin_file: string, dat_file: string }>
67

68
type DfuImageType = 'application' | 'softdevice' | 'bootloader' | 'softdevice_bootloader'
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc