@live-change/image-service 0.9.40 → 0.9.42

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.
Files changed (2) hide show
  1. package/endpoint.js +28 -9
  2. package/package.json +4 -4
package/endpoint.js CHANGED
@@ -50,6 +50,23 @@ function sanitizeImageId(id) {
50
50
  return id.replace(/[^a-zA-Z0-9\[\]@\.-]/g,"_")
51
51
  }
52
52
 
53
+ const currentConversions = new Map()
54
+
55
+ async function convertIfNeeded(convertedFilePath, convert) {
56
+ const currentConversion = currentConversions.get(convertedFilePath)
57
+ if(currentConversion) {
58
+ await currentConversion
59
+ return
60
+ }
61
+ if(await fileExists(convertedFilePath)) {
62
+ return // already converted
63
+ }
64
+ const conversionPromise = convert()
65
+ currentConversions.set(convertedFilePath, conversionPromise)
66
+ await conversionPromise
67
+ currentConversions.delete(convertedFilePath)
68
+ }
69
+
53
70
  async function handleImageGet(req, res, params) {
54
71
  const { image } = params
55
72
  const metadata = await getImageMetadata(image)
@@ -69,6 +86,8 @@ async function handleImageGet(req, res, params) {
69
86
  return
70
87
  }
71
88
 
89
+ debug("SERVING IMAGE", params)
90
+
72
91
  const kernel = "lanczos3"
73
92
 
74
93
  switch(params.type) {
@@ -77,9 +96,9 @@ async function handleImageGet(req, res, params) {
77
96
  debug("CONVERTING IMAGE!", metadata.extension, params.format)
78
97
  const normalized = normalizeFormat(params.format)
79
98
  const convertedFilePath = path.resolve(imagePrefix + 'converted.' + normalized)
80
- if(!(await fileExists(convertedFilePath))) {
81
- await sharp(sourceFilePath).toFile(convertedFilePath)
82
- }
99
+ await convertIfNeeded(convertedFilePath, async () => {
100
+ await sharp(sourceFilePath).toFile(convertedFilePath)
101
+ })
83
102
  res.sendFile(convertedFilePath)
84
103
  } else res.sendFile(sourceFilePath)
85
104
  } break;
@@ -93,14 +112,14 @@ async function handleImageGet(req, res, params) {
93
112
  if(width >= metadata.width) return res.sendFile(sourceFilePath)
94
113
  const normalized = normalizeFormat(params.format || metadata.extension)
95
114
  const convertedFilePath = path.resolve(imagePrefix + `width-${width}.${normalized}`)
96
- if(!(await fileExists(convertedFilePath))) {
115
+ await convertIfNeeded(convertedFilePath, async () => {
97
116
  await sharp(sourceFilePath)
98
117
  .resize({
99
118
  width,
100
119
  kernel
101
120
  })
102
121
  .toFile(convertedFilePath)
103
- }
122
+ })
104
123
  res.sendFile(convertedFilePath)
105
124
  } break;
106
125
  case "height": {
@@ -113,14 +132,14 @@ async function handleImageGet(req, res, params) {
113
132
  if(height >= metadata.height) return res.sendFile(sourceFilePath)
114
133
  const normalized = normalizeFormat(params.format || metadata.extension)
115
134
  const convertedFilePath = path.resolve(imagePrefix + `height-${height}.${normalized}`)
116
- if(!(await fileExists(convertedFilePath))) {
135
+ await convertIfNeeded(convertedFilePath, async () => {
117
136
  await sharp(sourceFilePath)
118
137
  .resize({
119
138
  height,
120
139
  kernel
121
140
  })
122
141
  .toFile(convertedFilePath)
123
- }
142
+ })
124
143
  res.sendFile(convertedFilePath)
125
144
  } break;
126
145
  case "rect": {
@@ -142,7 +161,7 @@ async function handleImageGet(req, res, params) {
142
161
  if(width === metadata.width && height === metadata.height) return res.sendFile(sourceFilePath)
143
162
  const normalized = normalizeFormat(params.format || metadata.extension)
144
163
  const convertedFilePath = path.resolve(imagePrefix + `rect-${width}-${height}.${normalized}`)
145
- if(!(await fileExists(convertedFilePath))) {
164
+ await convertIfNeeded(convertedFilePath, async () => {
146
165
  await sharp(sourceFilePath)
147
166
  .resize({
148
167
  width, height,
@@ -151,7 +170,7 @@ async function handleImageGet(req, res, params) {
151
170
  kernel
152
171
  })
153
172
  .toFile(convertedFilePath)
154
- }
173
+ })
155
174
  res.sendFile(convertedFilePath)
156
175
  }
157
176
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/image-service",
3
- "version": "0.9.40",
3
+ "version": "0.9.42",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,12 +21,12 @@
21
21
  "url": "https://www.viamage.com/"
22
22
  },
23
23
  "dependencies": {
24
- "@live-change/framework": "^0.9.40",
25
- "@live-change/relations-plugin": "^0.9.40",
24
+ "@live-change/framework": "^0.9.42",
25
+ "@live-change/relations-plugin": "^0.9.42",
26
26
  "download": "^8.0.0",
27
27
  "pluralize": "^8.0.0",
28
28
  "sharp": "^0.32.5"
29
29
  },
30
- "gitHead": "d25366854a7a30557fd8f8e428a2e02cabb5b323",
30
+ "gitHead": "6571e1434eed33df905a7708aad783e9339968f1",
31
31
  "type": "module"
32
32
  }