bare-media 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client.js CHANGED
@@ -42,6 +42,17 @@ export class WorkerClient extends ReadyResource {
42
42
  this.worker?.IPC.end()
43
43
  }
44
44
 
45
+ #reset() {
46
+ this.opening = null
47
+ this.closing = null
48
+
49
+ this.opened = false
50
+ this.closed = false
51
+
52
+ this.worker = null
53
+ this.rpc = null
54
+ }
55
+
45
56
  async #run() {
46
57
  const { filename, requireSource, args } = this.opts
47
58
  const source = requireSource?.()
@@ -50,7 +61,13 @@ export class WorkerClient extends ReadyResource {
50
61
  const ipc = this.worker.IPC
51
62
 
52
63
  ipc.on('end', () => ipc.end())
53
- ipc.on('close', () => this.onClose?.())
64
+ ipc.on('close', () => {
65
+ this.#reset()
66
+ this.onClose?.()
67
+ console.error(
68
+ '[bare-media] Worker has exited. IPC channel closed unexpectedly.'
69
+ )
70
+ })
54
71
 
55
72
  this.rpc = new HRPC(ipc)
56
73
  }
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "bare-media",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build:rpc": "cd shared/spec && bare ./build.js",
8
8
  "format": "prettier --write .",
9
9
  "format:check": "prettier --check .",
10
- "test": "npm run format:check && brittle-bare test/index.js"
10
+ "lint": "lunte",
11
+ "test": "npm run lint && npm run format:check && brittle-bare test/index.js"
11
12
  },
12
13
  "keywords": [],
13
14
  "author": "Holepunch Inc",
@@ -17,6 +18,7 @@
17
18
  "b4a": "^1.6.7",
18
19
  "bare-fetch": "^2.4.1",
19
20
  "bare-fs": "^4.1.5",
21
+ "bare-gif": "^1.1.2",
20
22
  "bare-heif": "^1.0.5",
21
23
  "bare-image-resample": "^1.0.1",
22
24
  "bare-jpeg": "^1.0.1",
@@ -24,6 +26,7 @@
24
26
  "bare-tiff": "^1.0.1",
25
27
  "bare-webp": "^1.0.3",
26
28
  "cross-worker": "^1.1.0",
29
+ "get-file-format": "^1.0.1",
27
30
  "get-mime-type": "^2.0.1",
28
31
  "hrpc": "^4.0.0",
29
32
  "hyperschema": "^1.12.3",
@@ -35,6 +38,7 @@
35
38
  "corestore": "^7.4.5",
36
39
  "hyperblobs": "^2.8.0",
37
40
  "hypercore-blob-server": "^1.11.0",
41
+ "lunte": "^1.3.0",
38
42
  "prettier": "^3.6.2",
39
43
  "prettier-config-holepunch": "^1.0.0",
40
44
  "test-tmp": "^1.4.0"
package/shared/codecs.js CHANGED
@@ -7,7 +7,8 @@ export const codecs = {
7
7
  'image/webp': () => import('bare-webp'),
8
8
  'image/png': () => import('bare-png'),
9
9
  'image/tif': () => import('bare-tiff'),
10
- 'image/tiff': () => import('bare-tiff')
10
+ 'image/tiff': () => import('bare-tiff'),
11
+ 'image/gif': () => import('bare-gif')
11
12
  }
12
13
 
13
14
  export function isCodecSupported(mimetype) {
package/worker/media.js CHANGED
@@ -1,10 +1,13 @@
1
1
  import b4a from 'b4a'
2
2
  import fs from 'bare-fs'
3
3
  import fetch from 'bare-fetch'
4
- import getMimeType from 'get-mime-type'
5
4
 
6
- import { importCodec, supportsQuality } from '../shared/codecs.js'
7
- import { calculateFitDimensions } from './util'
5
+ import {
6
+ importCodec,
7
+ isCodecSupported,
8
+ supportsQuality
9
+ } from '../shared/codecs.js'
10
+ import { detectMimeType, calculateFitDimensions } from './util'
8
11
 
9
12
  const DEFAULT_PREVIEW_FORMAT = 'image/webp'
10
13
 
@@ -22,10 +25,15 @@ export async function createPreview({
22
25
  format,
23
26
  encoding
24
27
  }) {
25
- mimetype = mimetype || getMimeType(path)
26
28
  format = format || DEFAULT_PREVIEW_FORMAT
27
29
 
28
30
  const buff = await getBuffer({ path, httpLink, buffer })
31
+ mimetype = mimetype || detectMimeType(buff, path)
32
+
33
+ if (!isCodecSupported(mimetype)) {
34
+ throw new Error(`Unsupported file type: No codec available for ${mimetype}`)
35
+ }
36
+
29
37
  const rgba = await decodeImageToRGBA(buff, mimetype, maxFrames)
30
38
  const { width, height } = rgba
31
39
 
@@ -118,6 +126,11 @@ export async function createPreview({
118
126
 
119
127
  export async function decodeImage({ path, httpLink, buffer, mimetype }) {
120
128
  const buff = await getBuffer({ path, httpLink, buffer })
129
+ mimetype = mimetype || detectMimeType(buff, path)
130
+
131
+ if (!isCodecSupported(mimetype)) {
132
+ throw new Error(`Unsupported file type: No codec available for ${mimetype}`)
133
+ }
121
134
 
122
135
  const rgba = await decodeImageToRGBA(buff, mimetype)
123
136
  const { width, height, data } = rgba
package/worker/util.js CHANGED
@@ -1,5 +1,12 @@
1
+ import getMimeType from 'get-mime-type'
2
+ import getFileFormat from 'get-file-format'
3
+
1
4
  export const log = (...args) => console.log('[bare-media]', ...args)
2
5
 
6
+ export function detectMimeType(buffer, path) {
7
+ return getMimeType(getFileFormat(buffer)) || getMimeType(path)
8
+ }
9
+
3
10
  export function calculateFitDimensions(width, height, maxWidth, maxHeight) {
4
11
  if (width <= maxWidth && height <= maxHeight) {
5
12
  return { width, height }