Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Maybe enough for a CJS build?
Still seem to be issues for the integration I'm testing.
One to pick up with more motivation in the New Year!
  • Loading branch information
microbit-matt-hillsdon committed Dec 23, 2024
commit e068597a29b805575f4c4f4d61d0619cf1eb48ae
35 changes: 14 additions & 21 deletions lib/usb-partial-flashing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ import {
pageAlignBlocks,
read32FromUInt8Array,
} from "./usb-partial-flashing-utils.js";
import MemoryMap from "nrf-intel-hex";
import { BoardVersion } from "./device.js";

// Workaround for packaging issues
import * as Hex from "nrf-intel-hex";
const MemoryMapClass = (Hex.default as any).default ?? Hex.default;
type MemoryMap = Hex.default;

type ProgressCallback = (n: number, partial: boolean) => void;

// Source code for binaries in can be found at https://github.com/microsoft/pxt-microbit/blob/dec5b8ce72d5c2b4b0b20aafefce7474a6f0c7b2/external/sha/source/main.c
Expand Down Expand Up @@ -205,7 +209,7 @@ export class PartialFlashing {
// Falls back to a full flash if partial flashing fails.
// Drawn from https://github.com/microsoft/pxt-microbit/blob/dec5b8ce72d5c2b4b0b20aafefce7474a6f0c7b2/editor/extension.tsx#L335
private async partialFlashAsync(
data: string | Uint8Array | MemoryMap.default,
data: string | Uint8Array | MemoryMap,
updateProgress: ProgressCallback,
): Promise<boolean> {
const flashBytes = this.convertDataToPaddedBytes(data);
Expand Down Expand Up @@ -251,7 +255,7 @@ export class PartialFlashing {

// Perform full flash of micro:bit's ROM using daplink.
async fullFlashAsync(
data: string | Uint8Array | MemoryMap.default,
data: string | Uint8Array | MemoryMap,
updateProgress: ProgressCallback,
) {
this.log("Full flash");
Expand Down Expand Up @@ -279,7 +283,7 @@ export class PartialFlashing {
// Flash the micro:bit's ROM with the provided image, resetting the micro:bit first.
// Drawn from https://github.com/microsoft/pxt-microbit/blob/dec5b8ce72d5c2b4b0b20aafefce7474a6f0c7b2/editor/extension.tsx#L439
async flashAsync(
data: string | Uint8Array | MemoryMap.default,
data: string | Uint8Array | MemoryMap,
updateProgress: ProgressCallback,
): Promise<boolean> {
let resetPromise = (async () => {
Expand Down Expand Up @@ -321,7 +325,7 @@ export class PartialFlashing {
}

private convertDataToHexString(
data: string | Uint8Array | MemoryMap.default,
data: string | Uint8Array | MemoryMap,
): string {
if (typeof data === "string") {
return data;
Expand All @@ -333,7 +337,7 @@ export class PartialFlashing {
}

private convertDataToPaddedBytes(
data: string | Uint8Array | MemoryMap.default,
data: string | Uint8Array | MemoryMap,
): Uint8Array {
if (data instanceof Uint8Array) {
return data;
Expand All @@ -345,26 +349,15 @@ export class PartialFlashing {
}

private hexStringToPaddedBytes(hex: string): Uint8Array {
// Cludge for a packaging issue
const fromHex: (
hexText: string,
maxBlockSize?: number,
) => MemoryMap.default =
(MemoryMap as any).fromHex ?? MemoryMap.default.fromHex;

return this.memoryMapToPaddedBytes(fromHex(hex));
const m = MemoryMapClass.fromHex(hex);
return this.memoryMapToPaddedBytes(m);
}

private paddedBytesToHexString(data: Uint8Array): string {
// Cludge for a packaging issue
const fromPaddedUint8Array: (data: Uint8Array) => MemoryMap.default =
(MemoryMap as any).fromPaddedUint8Array ??
MemoryMap.default.fromPaddedUint8Array;

return fromPaddedUint8Array(data).asHexString();
return MemoryMapClass.fromPaddedUint8Array(data).asHexString();
}

private memoryMapToPaddedBytes(memoryMap: MemoryMap.default): Uint8Array {
private memoryMapToPaddedBytes(memoryMap: MemoryMap): Uint8Array {
const flashSize = {
V1: 256 * 1024,
V2: 512 * 1024,
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
"name": "@microbit/microbit-connection",
"version": "0.0.0",
"type": "module",
"module": "./build/index.js",
"main": "./build/cjs/index.js",
"module": "./build/esm/index.js",
"types": "./build/esm/index.d.ts",
"exports": {
".": {
"import": "./build/index.js"
"import": "./build/esm/index.js",
"require": "./build/cjs/index.js",
"types": "./build/esm/index.d.ts"
}
},
"scripts": {
"dev": "vite",
"build:lib": "tsc",
"build:esm": "tsc -p tsconfig.json",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:lib": "npm run build:esm && npm run build:cjs",
"build:demo": "vite build --mode=demo",
"ci": "npm run build:lib && npm run test && npx prettier --check lib src",
"test": "vitest",
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "Node",
"outDir": "./build/cjs"
}
}
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

/* Bundler mode */
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"isolatedModules": true,
"moduleDetection": "force",
"outDir": "build",
"outDir": "build/esm",
"sourceMap": true,
"declaration": true,

Expand Down
Loading