@tim-code/my-util 0.3.1 → 0.4.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/package.json +1 -1
- package/src/fs.js +13 -20
- package/src/fs.test.js +25 -17
- package/src/promise.js +1 -0
package/package.json
CHANGED
package/src/fs.js
CHANGED
|
@@ -1,47 +1,40 @@
|
|
|
1
1
|
import { readFile, stat, writeFile } from "node:fs/promises"
|
|
2
2
|
import { tmpdir } from "node:os"
|
|
3
3
|
import { promisify } from "node:util"
|
|
4
|
-
import { gunzip as _gunzip } from "node:zlib"
|
|
4
|
+
import { gunzip as _gunzip, gzip as _gzip } from "node:zlib"
|
|
5
5
|
|
|
6
6
|
const gunzip = promisify(_gunzip)
|
|
7
|
+
const gzip = promisify(_gzip)
|
|
7
8
|
|
|
8
9
|
// the integration tests for this file are the s3-fs integration tests in lambda-integrations
|
|
9
10
|
// changes to this file should be tested against lambda-integrations' integration tests
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
|
-
* Get JSON from a path.
|
|
13
|
+
* Get JSON from a path. If path ends with .gz, will automatically decompress data.
|
|
13
14
|
* @param {string} path
|
|
14
15
|
* @returns {Object|Array}
|
|
15
16
|
*/
|
|
16
|
-
export async function
|
|
17
|
-
|
|
17
|
+
export async function readJSON(path) {
|
|
18
|
+
let buffer = await readFile(path)
|
|
19
|
+
if (path.endsWith(".gz")) {
|
|
20
|
+
buffer = await gunzip(buffer)
|
|
21
|
+
}
|
|
18
22
|
return JSON.parse(buffer.toString())
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
/**
|
|
22
|
-
* Write JSON to a path.
|
|
26
|
+
* Write JSON to a path. If path ends with .gz, will automatically compress data.
|
|
23
27
|
* @param {string} path
|
|
24
28
|
* @param {Object|Array} object
|
|
25
29
|
* @param {Object} $1
|
|
26
30
|
* @param {number=} $1.indent Indent used to format JSON object. Default 2. If 0, does not indent object.
|
|
27
31
|
*/
|
|
28
32
|
export async function writeJSON(path, object, { indent = 2 } = {}) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Get gzipped JSON from a path.
|
|
35
|
-
* @param {string} path
|
|
36
|
-
* @returns {Object|Array}
|
|
37
|
-
*/
|
|
38
|
-
export async function getCompressedJSON(path) {
|
|
39
|
-
if (!path.endsWith(".gz")) {
|
|
40
|
-
throw new Error("path does not suggest a compressed file")
|
|
33
|
+
let data = JSON.stringify(object, undefined, indent)
|
|
34
|
+
if (path.endsWith(".gz")) {
|
|
35
|
+
data = await gzip(data)
|
|
41
36
|
}
|
|
42
|
-
|
|
43
|
-
const uncompressed = await gunzip(buffer)
|
|
44
|
-
return JSON.parse(uncompressed.toString())
|
|
37
|
+
await writeFile(path, data)
|
|
45
38
|
}
|
|
46
39
|
|
|
47
40
|
/**
|
package/src/fs.test.js
CHANGED
|
@@ -4,6 +4,7 @@ const readFileMock = jest.fn()
|
|
|
4
4
|
const statMock = jest.fn()
|
|
5
5
|
const tmpdirMock = jest.fn()
|
|
6
6
|
const gunzipMock = jest.fn()
|
|
7
|
+
const gzipMock = jest.fn()
|
|
7
8
|
const writeFileMock = jest.fn()
|
|
8
9
|
jest.unstable_mockModule("node:fs/promises", () => ({
|
|
9
10
|
readFile: readFileMock,
|
|
@@ -15,6 +16,7 @@ jest.unstable_mockModule("node:os", () => ({
|
|
|
15
16
|
}))
|
|
16
17
|
jest.unstable_mockModule("node:zlib", () => ({
|
|
17
18
|
gunzip: gunzipMock,
|
|
19
|
+
gzip: gzipMock,
|
|
18
20
|
}))
|
|
19
21
|
jest.unstable_mockModule("node:util", () => ({
|
|
20
22
|
promisify: (mock) => mock,
|
|
@@ -22,16 +24,24 @@ jest.unstable_mockModule("node:util", () => ({
|
|
|
22
24
|
|
|
23
25
|
// Now import the module under test
|
|
24
26
|
const mod = await import("./fs.js")
|
|
25
|
-
const {
|
|
27
|
+
const { readJSON, writeJSON, pathExists, makeTempDirectory } = mod
|
|
26
28
|
|
|
27
|
-
describe("
|
|
29
|
+
describe("readJSON", () => {
|
|
28
30
|
beforeEach(() => jest.clearAllMocks())
|
|
29
31
|
it("parses JSON from file", async () => {
|
|
30
32
|
readFileMock.mockResolvedValue(Buffer.from('{"a":1}'))
|
|
31
|
-
const result = await
|
|
33
|
+
const result = await readJSON("foo.json")
|
|
32
34
|
expect(result).toEqual({ a: 1 })
|
|
33
35
|
expect(readFileMock).toHaveBeenCalledWith("foo.json")
|
|
34
36
|
})
|
|
37
|
+
it("decompresses and parses JSON from .gz file", async () => {
|
|
38
|
+
readFileMock.mockResolvedValue(Buffer.from("gzipped"))
|
|
39
|
+
gunzipMock.mockResolvedValue(Buffer.from('{"b":2}'))
|
|
40
|
+
const result = await readJSON("bar.gz")
|
|
41
|
+
expect(result).toEqual({ b: 2 })
|
|
42
|
+
expect(readFileMock).toHaveBeenCalledWith("bar.gz")
|
|
43
|
+
expect(gunzipMock).toHaveBeenCalledWith(Buffer.from("gzipped"))
|
|
44
|
+
})
|
|
35
45
|
})
|
|
36
46
|
|
|
37
47
|
describe("writeJSON", () => {
|
|
@@ -55,20 +65,18 @@ describe("writeJSON", () => {
|
|
|
55
65
|
await writeJSON("foo.json", { x: 2 }, { indent: 0 })
|
|
56
66
|
expect(writeFileMock).toHaveBeenCalledWith("foo.json", '{"x":2}')
|
|
57
67
|
})
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
expect(
|
|
69
|
-
|
|
70
|
-
it("throws for bad filename", async () => {
|
|
71
|
-
await expect(getCompressedJSON("bar")).rejects.toThrow(/a compressed file/u)
|
|
68
|
+
it("compresses and writes JSON to .gz file", async () => {
|
|
69
|
+
gzipMock.mockResolvedValue(Buffer.from("compressed"))
|
|
70
|
+
await writeJSON("foo.gz", { y: 3 })
|
|
71
|
+
// Should gzip the JSON string and write the compressed buffer
|
|
72
|
+
expect(gzipMock).toHaveBeenCalledWith(JSON.stringify({ y: 3 }, undefined, 2))
|
|
73
|
+
expect(writeFileMock).toHaveBeenCalledWith("foo.gz", Buffer.from("compressed"))
|
|
74
|
+
})
|
|
75
|
+
it("compresses and writes JSON with custom indent to .gz file", async () => {
|
|
76
|
+
gzipMock.mockResolvedValue(Buffer.from("compressed"))
|
|
77
|
+
await writeJSON("foo.gz", { y: 3 }, { indent: 0 })
|
|
78
|
+
expect(gzipMock).toHaveBeenCalledWith(JSON.stringify({ y: 3 }, undefined, 0))
|
|
79
|
+
expect(writeFileMock).toHaveBeenCalledWith("foo.gz", Buffer.from("compressed"))
|
|
72
80
|
})
|
|
73
81
|
})
|
|
74
82
|
|
package/src/promise.js
CHANGED
|
@@ -139,6 +139,7 @@ export function alert(result) {
|
|
|
139
139
|
return result
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
+
// unused but included for reference
|
|
142
143
|
/**
|
|
143
144
|
* Parallelize executions of a function using `Promise.all()`.
|
|
144
145
|
* This is useful because usually you want to set a limit to the number of parallel requests possible at once.
|