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

Move to TypeScript #683

Merged
merged 36 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2d64821
Convert first file to TypeScript
Acconut Apr 16, 2024
f16b675
Transpile using TypeScript
Acconut Apr 16, 2024
e7ce111
Remove Babel
Acconut Apr 16, 2024
677e063
Add back missing dependencies for ESLint
Acconut Apr 16, 2024
f04835f
Move eslint exceptions into .eslintrc
Acconut Apr 16, 2024
12effd8
Move logger.js to TypeScript
Acconut Apr 16, 2024
9a65843
Rename tsconfig-base.json to tsconfig.json
Acconut Apr 16, 2024
b887d7e
Move upload.js to TypeScript
Acconut Apr 16, 2024
b85b7e1
Move all node-related source files to TypeScript
Acconut Apr 16, 2024
de3473f
Move browser source code to TypeScript
Acconut May 13, 2024
d8e4f16
Lint
Acconut May 17, 2024
044dad0
Add linting rules for TypeScript
Acconut May 17, 2024
3cd76fd
Remove obsolete type definitions
Acconut May 17, 2024
4620b9c
Properly transpile to ESM and CJS
Acconut May 17, 2024
3407b9b
Fix build command
Acconut May 17, 2024
4e349f5
Restructure exports in `package.json`
Acconut May 17, 2024
5a9f228
Keep main pointing to Node index
Acconut May 27, 2024
ca9081e
Merge branch 'main' into typescript2
Acconut May 28, 2024
3bfdacc
Remove ESLint comments
Acconut May 28, 2024
f0bad11
Remove typescript-eslint
Acconut May 28, 2024
43372eb
Remove Babel (again)
Acconut May 28, 2024
7c9425d
Remove empty line
Acconut May 28, 2024
dbafd50
Revert to original structure in `package.json`
Acconut May 28, 2024
57cbb49
Remove `tsd`
Acconut May 28, 2024
8538ead
Rename `lib.es5` to more appropriate `lib.cjs`
Acconut May 28, 2024
e5a38ea
Explicitly install `types/node`
Acconut May 29, 2024
f68c22c
Move types and values for options in own file
Acconut May 29, 2024
d3670cc
Remove `unknown` type for timeout value
Acconut May 29, 2024
695a763
Remove generics to unify the types for Browser and Node.js
Acconut May 29, 2024
2e79a9c
Better detect input types for fingerprinting
Acconut May 29, 2024
f97916d
Move to TypeScript: Upload, StreamSource (#734)
holloway Nov 13, 2024
cdc6838
Merge branch 'main' into typescript2
Acconut Jan 14, 2025
2011a04
Error message for React Native environments
Acconut Jan 14, 2025
5d82646
Comment for TODO
Acconut Jan 14, 2025
08f498d
Rename classes and files for file sources
Acconut Jan 14, 2025
d693361
Remove comment about exports syntax
Acconut Apr 19, 2024
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
12 changes: 0 additions & 12 deletions .babelrc

This file was deleted.

2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ jobs:
suite: 'browserstack'
- desc: 'Puppeteer'
suite: 'puppeteer'
- desc: 'Types'
suite: 'types'
- desc: 'Node.js 18'
suite: 'node'
node: 18
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.vscode/settings.json
node_modules
demos/reactnative/.expo
lib.es5
lib.cjs
lib.esm
dist
.DS_Store
Expand Down
4 changes: 4 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
{
"include": ["demos/browser/**", "demos/cordova/**"],
"javascript": { "globals": ["tus", "Camera"] }
},
{
"include": ["tsconfig*.json"],
"json": { "parser": { "allowComments": true } }
}
]
}
51 changes: 0 additions & 51 deletions lib/browser/fileReader.js

This file was deleted.

58 changes: 58 additions & 0 deletions lib/browser/fileReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { isReactNativeFile, isReactNativePlatform } from './isReactNative.js'
import uriToBlob from './uriToBlob.js'

import type { FileReader, FileSource, UploadInput } from '../options.js'
import BlobFileSource from './sources/BlobFileSource.js'
import StreamFileSource from './sources/StreamFileSource.js'

function isWebStream(input: UploadInput): input is Pick<ReadableStreamDefaultReader, 'read'> {
return 'read' in input && typeof input.read === 'function'
}

// TODO: Make sure that we support ArrayBuffers, TypedArrays, DataViews and Blobs
export default class BrowserFileReader implements FileReader {
async openFile(input: UploadInput, chunkSize: number): Promise<FileSource> {
// In React Native, when user selects a file, instead of a File or Blob,
// you usually get a file object {} with a uri property that contains
// a local path to the file. We use XMLHttpRequest to fetch
// the file blob, before uploading with tus.
if (isReactNativeFile(input)) {
if (!isReactNativePlatform()) {
throw new Error('tus: file objects with `uri` property is only supported in React Native')
}

try {
const blob = await uriToBlob(input.uri)
return new BlobFileSource(blob)
} catch (err) {
throw new Error(
`tus: cannot fetch \`file.uri\` as Blob, make sure the uri is correct and accessible. ${err}`,
)
}
}

// File is a subtype of Blob, so we can check for Blob here.
if (input instanceof Blob) {
return Promise.resolve(new BlobFileSource(input))
}

if (isWebStream(input)) {
chunkSize = Number(chunkSize)
if (!Number.isFinite(chunkSize)) {
return Promise.reject(
new Error(
'cannot create source for stream without a finite value for the `chunkSize` option',
),
)
}

return Promise.resolve(new StreamFileSource(input))
}

return Promise.reject(
new Error(
'source object may only be an instance of File, Blob, or Reader in this environment',
),
)
}
}
41 changes: 0 additions & 41 deletions lib/browser/fileSignature.js

This file was deleted.

42 changes: 42 additions & 0 deletions lib/browser/fileSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { ReactNativeFile, UploadInput, UploadOptions } from '../options.js'
import { isReactNativeFile, isReactNativePlatform } from './isReactNative.js'

/**
* Generate a fingerprint for a file which will be used the store the endpoint
*/
export default function fingerprint(file: UploadInput, options: UploadOptions) {
if (isReactNativePlatform() && isReactNativeFile(file)) {
return Promise.resolve(reactNativeFingerprint(file, options))
}

if (file instanceof Blob) {
return Promise.resolve(
//@ts-expect-error TODO: We have to check the input type here
// This can be fixed by moving the fingerprint function to the FileReader class
['tus-br', file.name, file.type, file.size, file.lastModified, options.endpoint].join('-'),
)
}

return Promise.resolve(null)
}

function reactNativeFingerprint(file: ReactNativeFile, options: UploadOptions): string {
const exifHash = file.exif ? hashCode(JSON.stringify(file.exif)) : 'noexif'
return ['tus-rn', file.name || 'noname', file.size || 'nosize', exifHash, options.endpoint].join(
'/',
)
}

function hashCode(str: string): number {
// from https://stackoverflow.com/a/8831937/151666
let hash = 0
if (str.length === 0) {
return hash
}
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = (hash << 5) - hash + char
hash &= hash // Convert to 32bit integer
}
return hash
}
97 changes: 0 additions & 97 deletions lib/browser/httpStack.js

This file was deleted.

Loading
Loading