This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (166 loc) · 4.46 KB
/
index.js
File metadata and controls
182 lines (166 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import test from 'blue-tape'
import initMemoize from '../src'
import initBlobStore from 'fs-blob-store'
import rimraf from 'rimraf'
import fs from 'fs'
import { join } from 'path'
import isStream from 'is-stream'
import * as streamUtils from '../src/stream-utils'
import request from 'request'
const tmpPath = join(__dirname, '.tmp')
// Clean tmp dir
rimraf.sync(tmpPath)
const blobStore = initBlobStore(tmpPath)
const memoize = initMemoize(blobStore)
test('simple cache miss, cache hit', (t) => {
let callCount = 0
const square = memoize((i) => {
callCount++
if (callCount === 1) {
return Promise.resolve(i * i)
}
return Promise.resolve(i * i * i)
}, 'test-1')
square(2)
.then((val) => {
t.equal(val, 4, 'val = 4')
})
.then(() => square(2))
.then((val) => {
t.equal(val, 4, 'returns cached result')
})
.then(() => {
t.end()
})
.catch((err) => {
t.fail(err)
})
})
// promisified read file
// returns buffer
const readFile = (filePath) => new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
const poemPath = `${__dirname}/poem.txt`
const poemData = fs.readFileSync(poemPath).toString()
test('simple cache miss, cache hit with promise', (t) => {
let callCount = 0
const readPoem = memoize(() => {
callCount++
if (callCount === 1) {
return readFile(poemPath).then((data) => data.toString())
}
return Promise.resolve('oopsie')
}, 'test-2')
readPoem()
.then((val) => {
t.equal(val, poemData, 'poem data')
})
.then(() => readPoem())
.then((val) => {
t.equal(val, poemData, 'returns cached result')
})
.then(() => t.end())
.catch((err) => t.fail(err))
})
test('simple cache miss, cache hit with promise that returns buffer', (t) => {
let callCount = 0
const readPoem = memoize(() => {
callCount++
if (callCount === 1) {
return readFile(poemPath)
}
return Promise.resolve('oopsie')
}, 'test-3')
readPoem()
.then((val) => {
t.equal(val.toString(), poemData, 'poem data')
})
.then(() => readPoem())
.then((val) => {
t.ok(Buffer.isBuffer(val), 'is a buffer')
t.equal(val.toString(), poemData, 'returns cached result')
})
.then(() => t.end())
.catch((err) => t.fail(err))
})
test('promise returning a stream', (t) => {
let callCount = 0
const readPoemStream = memoize(() => {
callCount++
if (callCount === 1) {
return fs.createReadStream(poemPath)
}
return Promise.resolve('oopsie')
}, 'test-4')
readPoemStream()
.then((readStream) => {
t.ok(isStream(readStream, 'returns a stream'))
return streamUtils.streamToString(readStream)
})
.then((val) => {
t.equal(val.toString(), poemData, 'poem data')
})
.then(() => readPoemStream())
.then((readStream) => {
t.ok(isStream(readStream, 'returns a stream'))
return streamUtils.streamToString(readStream)
})
.then((val) => {
t.equal(val.toString(), poemData, 'returns cached result')
})
.then(() => t.end())
.catch((err) => t.fail(err))
})
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
test('maxAge', (t) => {
let callCount = 0
const someFn = memoize(() => {
callCount++
return `callCount: ${callCount}`
}, 'test-5', { maxAge: 300 })
return someFn()
.then((str) => t.equal(str, 'callCount: 1'))
.then(() => someFn())
.then((str) => t.equal(str, 'callCount: 1'))
.then(() => timeout(400))
.then(() => someFn())
.then((str) => t.equal(str, 'callCount: 2'))
.then(() => someFn())
.then((str) => t.equal(str, 'callCount: 2'))
})
test('issue #1', (t) => {
const getBody = (url) => new Promise((resolve, reject) => {
request({
url,
encoding: null,
}, (err, res, body) => {
if (err) {
return reject(err)
}
resolve(body)
})
})
const memoGetBody = memoize(getBody, 'getBody')
const url = 'https://raw.githubusercontent.com/maxogden/abstract-blob-store/master/badge.png'
let firstBody
let secondBody
return memoGetBody(url)
.then((body) => {
firstBody = body
// console.log(body)
})
.then(() => memoGetBody(url))
.then((body) => {
secondBody = body
// console.log(body)
})
.then(() => {
t.ok(Buffer.isBuffer(firstBody))
t.ok(Buffer.isBuffer(secondBody))
t.deepEqual(firstBody, secondBody)
})
})