-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,061 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,27 @@ | ||
import { HttpRequestType, HttpRequestReturnType, HttpRequestOptions } from './assetsTypes'; | ||
import { tryLocalAssetRequire } from './assetUtils'; | ||
|
||
export const httpRequest = (url: string, _type: string): any => tryLocalAssetRequire(url); | ||
export function httpRequest<T extends HttpRequestType>( | ||
url: string, | ||
type: T, | ||
options?: HttpRequestOptions, | ||
): Promise<HttpRequestReturnType<T>> { | ||
const asset = options?.skipLocalForceDownload ? null : tryLocalAssetRequire(url); | ||
|
||
if (!asset) { | ||
return fetch(url, { | ||
...options, | ||
}).then(response => { | ||
if (type === 'binary') { | ||
return response.arrayBuffer() as unknown as HttpRequestReturnType<T>; | ||
} | ||
if (type === 'json') { | ||
return response.json() as unknown as HttpRequestReturnType<T>; | ||
} | ||
|
||
return response.text() as unknown as HttpRequestReturnType<T>; | ||
}); | ||
} | ||
|
||
return asset as Promise<HttpRequestReturnType<T>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,25 @@ | ||
// https://github.com/trezor/connect/blob/develop/src/js/env/node/networkUtils.js | ||
|
||
import fetch from 'cross-fetch'; | ||
import { promises as fs } from 'fs'; | ||
import { httpRequest as browserHttpRequest } from './assets-browser'; | ||
import { HttpRequestOptions, HttpRequestReturnType, HttpRequestType } from './assetsTypes'; | ||
import { tryLocalAssetRequire } from './assetUtils'; | ||
|
||
if (global && typeof global.fetch !== 'function') { | ||
global.fetch = fetch; | ||
} | ||
|
||
export function httpRequest( | ||
url: string, | ||
type: 'text', | ||
options?: RequestInit, | ||
skipLocalForceDownload?: boolean, | ||
): Promise<string>; | ||
|
||
export function httpRequest( | ||
export function httpRequest<T extends HttpRequestType>( | ||
url: string, | ||
type: 'binary', | ||
options?: RequestInit, | ||
skipLocalForceDownload?: boolean, | ||
): Promise<ArrayBuffer>; | ||
|
||
export function httpRequest( | ||
url: string, | ||
type: 'json', | ||
options?: RequestInit, | ||
skipLocalForceDownload?: boolean, | ||
): Promise<Record<string, any>>; | ||
|
||
export function httpRequest( | ||
url: any, | ||
type: any, | ||
options?: RequestInit, | ||
skipLocalForceDownload?: boolean, | ||
) { | ||
const asset = skipLocalForceDownload ? null : tryLocalAssetRequire(url); | ||
type: T, | ||
options?: HttpRequestOptions, | ||
): Promise<HttpRequestReturnType<T>> { | ||
const asset = options?.skipLocalForceDownload ? null : tryLocalAssetRequire(url); | ||
|
||
if (!asset) { | ||
return /^https?/.test(url) ? browserHttpRequest(url, type, options) : fs.readFile(url); | ||
return /^https?/.test(url) | ||
? browserHttpRequest(url, type, options) | ||
: (fs.readFile(url) as Promise<HttpRequestReturnType<T>>); | ||
} | ||
|
||
return asset; | ||
return asset as Promise<HttpRequestReturnType<T>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export type HttpRequestType = 'text' | 'binary' | 'json'; | ||
|
||
export type HttpRequestReturnType<T extends HttpRequestType> = T extends 'text' | ||
? string | ||
: T extends 'binary' | ||
? ArrayBuffer | Buffer | ||
: T extends 'json' | ||
? Record<string, any> | ||
: never; | ||
|
||
export interface HttpRequestOptions extends RequestInit { | ||
skipLocalForceDownload?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@suite-native/firmware", | ||
"version": "1.0.0", | ||
"private": true, | ||
"license": "See LICENSE.md in repo root", | ||
"sideEffects": false, | ||
"main": "src/index", | ||
"scripts": { | ||
"depcheck": "yarn g:depcheck", | ||
"lint:js": "yarn g:eslint '**/*.{ts,tsx,js}'", | ||
"test:unit": "jest -c ../../jest.config.base.js", | ||
"type-check": "yarn g:tsc --build" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
|
||
interface FirmwareState { | ||
isFirmwareInstallationRunning: boolean; | ||
} | ||
|
||
const initialState: FirmwareState = { | ||
isFirmwareInstallationRunning: false, | ||
}; | ||
|
||
type FirmwareRootState = { | ||
firmware: FirmwareState; | ||
}; | ||
|
||
export const firmwareSlice = createSlice({ | ||
name: 'firmware', | ||
initialState, | ||
reducers: { | ||
setIsFirmwareInstallationRunning: (state, action: PayloadAction<boolean>) => { | ||
state.isFirmwareInstallationRunning = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export const firmwareActions = firmwareSlice.actions; | ||
export const firmwareReducer = firmwareSlice.reducer; | ||
|
||
export const selectIsFirmwareInstallationRunning = (state: FirmwareRootState) => | ||
state.firmware.isFirmwareInstallationRunning; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './firmwareSlice'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { "outDir": "libDev" }, | ||
"references": [] | ||
} |
Oops, something went wrong.