8000 refactor: remove `setFile` and `setFiles` · netlify/blobs@9cbbab8 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9cbbab8

Browse files
committed
refactor: remove setFile and setFiles
1 parent 043a339 commit 9cbbab8

File tree

4 files changed

+0
-222
lines changed

4 files changed

+0
-222
lines changed

package-lock.json

Lines changed: 0 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,10 @@
5656
"husky": "^8.0.0",
5757
"node-fetch": "^3.3.1",
5858
"semver": "^7.5.3",
59-
"tmp-promise": "^3.0.3",
6059
&qu 10000 ot;typescript": "^5.0.0",
6160
"vitest": "^0.34.0"
6261
},
6362
"engines": {
6463
"node": "^14.16.0 || >=16.0.0"
65-
},
66-
"dependencies": {
67-
"p-map": "^6.0.0"
6864
}
6965
}

src/main.test.ts

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Buffer } from 'node:buffer'
2-
import { writeFile } from 'node:fs/promises'
32
import { env, version as nodeVersion } from 'node:process'
43

54
import semver from 'semver'
6-
import tmp from 'tmp-promise'
75
import { describe, test, expect, beforeAll, afterEach } from 'vitest'
86

97
import { MockFetch } from '../test/mock_fetch.js'
@@ -415,127 +413,6 @@ describe('set', () => {
415413
expect(mockStore.fulfilled).toBeTruthy()
416414
})
417415

418-
// We need `Readable.toWeb` to be available, which needs Node 16+.
419-
if (semver.gte(nodeVersion, '16.0.0')) {
420-
test('Accepts a file', async () => {
421-
const fileContents = 'Hello from a file'
422-
const mockStore = new MockFetch()
423-
.put({
424-
headers: { authorization: `Bearer ${apiToken}` },
425-
response: new Response(JSON.stringify({ url: signedURL })),
426-
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`,
427-
})
428-
.put({
429-
body: async (body) => {
430-
expect(await streamToString(body as unknown as NodeJS.ReadableStream)).toBe(fileContents)
431-
},
432-
headers: {
433-
'cache-control': 'max-age=0, stale-while-revalidate=60',
434-
},
435-
response: new Response(null),
436-
url: signedURL,
437-
})
438-
439-
globalThis.fetch = mockStore.fetcher
440-
441-
const { cleanup, path } = await tmp.file()
442-
443-
await writeFile(path, fileContents)
444-
445-
const blobs = getStore({
446-
name: 'production',
447-
token: apiToken,
448-
siteID,
449-
})
450-
451-
await blobs.setFile(key, path)
452-
453-
expect(mockStore.fulfilled).toBeTruthy()
454-
455-
await cleanup()
456-
})
457-
458-
test('Accepts multiple files concurrently', async () => {
459-
const contents = ['Hello from key-0', 'Hello from key-1', 'Hello from key-2']
460-
const signedURLs = ['https://signed-url.aws/0', 'https://signed-url.aws/1', 'https://signed-url.aws/2']
461-
462-
const mockStore = new MockFetch()
463-
.put({
464-
headers: { authorization: `Bearer ${apiToken}` },
465-
response: new Response(JSON.stringify({ url: signedURLs[0] })),
466-
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/key-0?context=production`,
467-
})
468-
.put({
469-
body: async (body) => {
470-
expect(await streamToString(body as unknown as NodeJS.ReadableStream)).toBe(contents[0])
471-
},
472-
headers: {
473-
'cache-control': 'max-age=0, stale-while-revalidate=60',
474-
},
475-
response: new Response(null),
476-
url: signedURLs[0],
477-
})
478-
.put({
479-
headers: { authorization: `Bearer ${apiToken}` },
480-
response: new Response(JSON.stringify({ url: signedURLs[1] })),
481-
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/key-1?context=production`,
482-
})
483-
.put({
484-
body: async (body) => {
485-
expect(await streamToString(body as unknown as NodeJS.ReadableStream)).toBe(contents[1])
486-
},
487-
headers: {
488-
'cache-control': 'max-age=0, stale-while-revalidate=60',
489-
},
490-
response: new Response(null),
491-
url: signedURLs[1],
492-
})
493-
.put({
494-
headers: { authorization: `Bearer ${apiToken}` },
495-
response: new Response(JSON.stringify({ url: signedURLs[2] })),
496-
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/key-2?context=production`,
497-
})
498-
.put({
499-
body: async (body) => {
500-
expect(await streamToString(body as unknown as NodeJS.ReadableStream)).toBe(contents[2])
501-
},
502-
headers: {
503-
'cache-control': 'max-age=0, stale-while-revalidate=60',
504-
},
505-
response: new Response(null),
506-
url: signedURLs[2],
507-
})
508-
509-
globalThis.fetch = mockStore.fetcher
510-
511-
const writes = await Promise.all(
512-
contents.map(async (content) => {
513-
const { cleanup, path } = await tmp.file()
514-
515-
await writeFile(path, content)
516-
517-
return { cleanup, path }
518-
}),
519-
)
520-
const files = writes.map(({ path }, idx) => ({
521-
key: `key-${idx}`,
522-
path,
523-
}))
524-
525-
const blobs = getStore({
526-
name: 'production',
527-
token: apiToken,
528-
siteID,
529-
})
530-
531-
await blobs.setFiles(files)
532-
533-
expect(mockStore.fulfilled).toBeTruthy()
534-
535-
await Promise.all(writes.map(({ cleanup }) => cleanup()))
536-
})
537-
}
538-
539416
test('Throws when the API returns a non-200 status code', async () => {
540417
const mockStore = new MockFetch().put({
541418
headers: { authorization: `Bearer ${apiToken}` },

src/store.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import { createReadStream } from 'node:fs'
2-
import { stat } from 'node:fs/promises'
3-
import { Readable } from 'node:stream'
4-
5-
import pMap from 'p-map'
6-
71
import { Client, Context } from './client.js'
82
import { BlobInput, Fetcher, HTTPMethod } from './types.js'
93

@@ -25,15 +19,6 @@ interface SetOptions {
2519
expiration?: Date | number
2620
}
2721

28-
interface SetFilesItem extends SetOptions {
29-
key: string
30-
path: string
31-
}
32-
33-
interface SetFilesOptions {
34-
concurrency?: number
35-
}
36-
3722
const EXPIRY_HEADER = 'x-nf-expires-at'
3823

3924
class Store {
@@ -131,27 +116,6 @@ class Store {
131116
})
132117
}
133118

134-
async setFile(key: string, path: string, { expiration }: SetOptions = {}) {
135-
const { size } = await stat(path)
136-
const file = Readable.toWeb(createReadStream(path))
137-
const headers = {
138-
...Store.getExpirationHeaders(expiration),
139-
'content-length': size.toString(),
140-
}
141-
142-
await this.client.makeRequest({
143-
body: file as ReadableStream,
144-
headers,
145-
key,
146-
method: HTTPMethod.Put,
147-
storeName: this.name,
148-
})
149-
}
150-
151-
setFiles(files: SetFilesItem[], { concurrency = 5 }: SetFilesOptions = {}) {
152-
return pMap(files, ({ key, path, ...options }) => this.setFile(key, path, options), { concurrency })
153-
}
154-
155119
async setJSON(key: string, data: unknown, { expiration }: SetOptions = {}) {
156120
const payload = JSON.stringify(data)
157121
const headers = {

0 commit comments

Comments
 (0)
0