RedisManager.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. let RedisManager
  2. const Settings = require('@overleaf/settings')
  3. const rclient = require('@overleaf/redis-wrapper').createClient(
  4. Settings.redis.documentupdater
  5. )
  6. const logger = require('@overleaf/logger')
  7. const metrics = require('./Metrics')
  8. const Errors = require('./Errors')
  9. const crypto = require('crypto')
  10. const async = require('async')
  11. const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
  12. const { docIsTooLarge } = require('./Limits')
  13. // Sometimes Redis calls take an unexpectedly long time. We have to be
  14. // quick with Redis calls because we're holding a lock that expires
  15. // after 30 seconds. We can't let any errors in the rest of the stack
  16. // hold us up, and need to bail out quickly if there is a problem.
  17. const MAX_REDIS_REQUEST_LENGTH = 5000 // 5 seconds
  18. // Make times easy to read
  19. const minutes = 60 // seconds for Redis expire
  20. const logHashReadErrors = Settings.documentupdater?.logHashErrors?.read
  21. const MEGABYTES = 1024 * 1024
  22. const MAX_RANGES_SIZE = 3 * MEGABYTES
  23. const keys = Settings.redis.documentupdater.key_schema
  24. const historyKeys = Settings.redis.history.key_schema // note: this is track changes, not project-history
  25. module.exports = RedisManager = {
  26. rclient,
  27. putDocInMemory(
  28. projectId,
  29. docId,
  30. docLines,
  31. version,
  32. ranges,
  33. pathname,
  34. projectHistoryId,
  35. _callback
  36. ) {
  37. const timer = new metrics.Timer('redis.put-doc')
  38. const callback = error => {
  39. timer.done()
  40. _callback(error)
  41. }
  42. const docLinesArray = docLines
  43. docLines = JSON.stringify(docLines)
  44. if (docLines.indexOf('\u0000') !== -1) {
  45. const error = new Error('null bytes found in doc lines')
  46. // this check was added to catch memory corruption in JSON.stringify.
  47. // It sometimes returned null bytes at the end of the string.
  48. logger.error({ err: error, docId, docLines }, error.message)
  49. return callback(error)
  50. }
  51. // Do an optimised size check on the docLines using the serialised
  52. // length as an upper bound
  53. const sizeBound = docLines.length
  54. if (docIsTooLarge(sizeBound, docLinesArray, Settings.max_doc_length)) {
  55. const docSize = docLines.length
  56. const err = new Error('blocking doc insert into redis: doc is too large')
  57. logger.error({ projectId, docId, err, docSize }, err.message)
  58. return callback(err)
  59. }
  60. const docHash = RedisManager._computeHash(docLines)
  61. // record bytes sent to redis
  62. metrics.summary('redis.docLines', docLines.length, { status: 'set' })
  63. logger.debug(
  64. { projectId, docId, version, docHash, pathname, projectHistoryId },
  65. 'putting doc in redis'
  66. )
  67. RedisManager._serializeRanges(ranges, (error, ranges) => {
  68. if (error) {
  69. logger.error({ err: error, docId, projectId }, error.message)
  70. return callback(error)
  71. }
  72. // update docsInProject set before writing doc contents
  73. rclient.sadd(
  74. keys.docsInProject({ project_id: projectId }),
  75. docId,
  76. error => {
  77. if (error) return callback(error)
  78. if (!pathname) {
  79. metrics.inc('pathname', 1, {
  80. path: 'RedisManager.setDoc',
  81. status: pathname === '' ? 'zero-length' : 'undefined',
  82. })
  83. }
  84. rclient.mset(
  85. {
  86. [keys.docLines({ doc_id: docId })]: docLines,
  87. [keys.projectKey({ doc_id: docId })]: projectId,
  88. [keys.docVersion({ doc_id: docId })]: version,
  89. [keys.docHash({ doc_id: docId })]: docHash,
  90. [keys.ranges({ doc_id: docId })]: ranges,
  91. [keys.pathname({ doc_id: docId })]: pathname,
  92. [keys.projectHistoryId({ doc_id: docId })]: projectHistoryId,
  93. },
  94. callback
  95. )
  96. }
  97. )
  98. })
  99. },
  100. removeDocFromMemory(projectId, docId, _callback) {
  101. logger.debug({ projectId, docId }, 'removing doc from redis')
  102. const callback = err => {
  103. if (err) {
  104. logger.err({ projectId, docId, err }, 'error removing doc from redis')
  105. _callback(err)
  106. } else {
  107. logger.debug({ projectId, docId }, 'removed doc from redis')
  108. _callback()
  109. }
  110. }
  111. let multi = rclient.multi()
  112. multi.strlen(keys.docLines({ doc_id: docId }))
  113. multi.del(
  114. keys.docLines({ doc_id: docId }),
  115. keys.projectKey({ doc_id: docId }),
  116. keys.docVersion({ doc_id: docId }),
  117. keys.docHash({ doc_id: docId }),
  118. keys.ranges({ doc_id: docId }),
  119. keys.pathname({ doc_id: docId }),
  120. keys.projectHistoryId({ doc_id: docId }),
  121. keys.projectHistoryType({ doc_id: docId }),
  122. keys.unflushedTime({ doc_id: docId }),
  123. keys.lastUpdatedAt({ doc_id: docId }),
  124. keys.lastUpdatedBy({ doc_id: docId })
  125. )
  126. multi.exec((error, response) => {
  127. if (error) {
  128. return callback(error)
  129. }
  130. const length = response?.[0]
  131. if (length > 0) {
  132. // record bytes freed in redis
  133. metrics.summary('redis.docLines', length, { status: 'del' })
  134. }
  135. multi = rclient.multi()
  136. multi.srem(keys.docsInProject({ project_id: projectId }), docId)
  137. multi.del(keys.projectState({ project_id: projectId }))
  138. multi.exec(callback)
  139. })
  140. },
  141. checkOrSetProjectState(projectId, newState, callback) {
  142. const multi = rclient.multi()
  143. multi.getset(keys.projectState({ project_id: projectId }), newState)
  144. multi.expire(keys.projectState({ project_id: projectId }), 30 * minutes)
  145. multi.exec((error, response) => {
  146. if (error) {
  147. return callback(error)
  148. }
  149. logger.debug(
  150. { projectId, newState, oldState: response[0] },
  151. 'checking project state'
  152. )
  153. callback(null, response[0] !== newState)
  154. })
  155. },
  156. clearProjectState(projectId, callback) {
  157. rclient.del(keys.projectState({ project_id: projectId }), callback)
  158. },
  159. getDoc(projectId, docId, callback) {
  160. const timer = new metrics.Timer('redis.get-doc')
  161. const collectKeys = [
  162. keys.docLines({ doc_id: docId }),
  163. keys.docVersion({ doc_id: docId }),
  164. keys.docHash({ doc_id: docId }),
  165. keys.projectKey({ doc_id: docId }),
  166. keys.ranges({ doc_id: docId }),
  167. keys.pathname({ doc_id: docId }),
  168. keys.projectHistoryId({ doc_id: docId }),
  169. keys.unflushedTime({ doc_id: docId }),
  170. keys.lastUpdatedAt({ doc_id: docId }),
  171. keys.lastUpdatedBy({ doc_id: docId }),
  172. ]
  173. rclient.mget(...collectKeys, (error, result) => {
  174. if (error) {
  175. return callback(error)
  176. }
  177. let [
  178. docLines,
  179. version,
  180. storedHash,
  181. docProjectId,
  182. ranges,
  183. pathname,
  184. projectHistoryId,
  185. unflushedTime,
  186. lastUpdatedAt,
  187. lastUpdatedBy,
  188. ] = result
  189. const timeSpan = timer.done()
  190. // check if request took too long and bail out. only do this for
  191. // get, because it is the first call in each update, so if this
  192. // passes we'll assume others have a reasonable chance to succeed.
  193. if (timeSpan > MAX_REDIS_REQUEST_LENGTH) {
  194. error = new Error('redis getDoc exceeded timeout')
  195. return callback(error)
  196. }
  197. // record bytes loaded from redis
  198. if (docLines != null) {
  199. metrics.summary('redis.docLines', docLines.length, { status: 'get' })
  200. }
  201. // check sha1 hash value if present
  202. if (docLines != null && storedHash != null) {
  203. const computedHash = RedisManager._computeHash(docLines)
  204. if (logHashReadErrors && computedHash !== storedHash) {
  205. logger.error(
  206. {
  207. projectId,
  208. docId,
  209. docProjectId,
  210. computedHash,
  211. storedHash,
  212. docLines,
  213. },
  214. 'hash mismatch on retrieved document'
  215. )
  216. }
  217. }
  218. try {
  219. docLines = JSON.parse(docLines)
  220. ranges = RedisManager._deserializeRanges(ranges)
  221. } catch (e) {
  222. return callback(e)
  223. }
  224. version = parseInt(version || 0, 10)
  225. // check doc is in requested project
  226. if (docProjectId != null && docProjectId !== projectId) {
  227. logger.error({ projectId, docId, docProjectId }, 'doc not in project')
  228. return callback(new Errors.NotFoundError('document not found'))
  229. }
  230. if (projectHistoryId != null) {
  231. projectHistoryId = parseInt(projectHistoryId)
  232. }
  233. if (docLines && version && !pathname) {
  234. metrics.inc('pathname', 1, {
  235. path: 'RedisManager.getDoc',
  236. status: pathname === '' ? 'zero-length' : 'undefined',
  237. })
  238. }
  239. callback(
  240. null,
  241. docLines,
  242. version,
  243. ranges,
  244. pathname,
  245. projectHistoryId,
  246. unflushedTime,
  247. lastUpdatedAt,
  248. lastUpdatedBy
  249. )
  250. })
  251. },
  252. getDocVersion(docId, callback) {
  253. rclient.mget(
  254. keys.docVersion({ doc_id: docId }),
  255. keys.projectHistoryType({ doc_id: docId }),
  256. (error, result) => {
  257. if (error) {
  258. return callback(error)
  259. }
  260. let [version, projectHistoryType] = result || []
  261. version = parseInt(version, 10)
  262. callback(null, version, projectHistoryType)
  263. }
  264. )
  265. },
  266. getDocLines(docId, callback) {
  267. rclient.get(keys.docLines({ doc_id: docId }), (error, docLines) => {
  268. if (error) {
  269. return callback(error)
  270. }
  271. callback(null, docLines)
  272. })
  273. },
  274. getPreviousDocOps(docId, start, end, callback) {
  275. const timer = new metrics.Timer('redis.get-prev-docops')
  276. rclient.llen(keys.docOps({ doc_id: docId }), (error, length) => {
  277. if (error) {
  278. return callback(error)
  279. }
  280. rclient.get(keys.docVersion({ doc_id: docId }), (error, version) => {
  281. if (error) {
  282. return callback(error)
  283. }
  284. version = parseInt(version, 10)
  285. const firstVersionInRedis = version - length
  286. if (start < firstVersionInRedis || end > version) {
  287. error = new Errors.OpRangeNotAvailableError(
  288. 'doc ops range is not loaded in redis'
  289. )
  290. logger.debug(
  291. { err: error, docId, length, version, start, end },
  292. 'doc ops range is not loaded in redis'
  293. )
  294. return callback(error)
  295. }
  296. start = start - firstVersionInRedis
  297. if (end > -1) {
  298. end = end - firstVersionInRedis
  299. }
  300. if (isNaN(start) || isNaN(end)) {
  301. error = new Error('inconsistent version or lengths')
  302. logger.error(
  303. { err: error, docId, length, version, start, end },
  304. 'inconsistent version or length'
  305. )
  306. return callback(error)
  307. }
  308. rclient.lrange(
  309. keys.docOps({ doc_id: docId }),
  310. start,
  311. end,
  312. (error, jsonOps) => {
  313. let ops
  314. if (error) {
  315. return callback(error)
  316. }
  317. try {
  318. ops = jsonOps.map(jsonOp => JSON.parse(jsonOp))
  319. } catch (e) {
  320. return callback(e)
  321. }
  322. const timeSpan = timer.done()
  323. if (timeSpan > MAX_REDIS_REQUEST_LENGTH) {
  324. error = new Error('redis getPreviousDocOps exceeded timeout')
  325. return callback(error)
  326. }
  327. callback(null, ops)
  328. }
  329. )
  330. })
  331. })
  332. },
  333. getHistoryType(docId, callback) {
  334. rclient.get(
  335. keys.projectHistoryType({ doc_id: docId }),
  336. (error, projectHistoryType) => {
  337. if (error) {
  338. return callback(error)
  339. }
  340. callback(null, projectHistoryType)
  341. }
  342. )
  343. },
  344. setHistoryType(docId, projectHistoryType, callback) {
  345. rclient.set(
  346. keys.projectHistoryType({ doc_id: docId }),
  347. projectHistoryType,
  348. callback
  349. )
  350. },
  351. DOC_OPS_TTL: 60 * minutes,
  352. DOC_OPS_MAX_LENGTH: 100,
  353. updateDocument(
  354. projectId,
  355. docId,
  356. docLines,
  357. newVersion,
  358. appliedOps,
  359. ranges,
  360. updateMeta,
  361. callback
  362. ) {
  363. if (appliedOps == null) {
  364. appliedOps = []
  365. }
  366. RedisManager.getDocVersion(
  367. docId,
  368. (error, currentVersion, projectHistoryType) => {
  369. if (error) {
  370. return callback(error)
  371. }
  372. if (currentVersion + appliedOps.length !== newVersion) {
  373. error = new Error(`Version mismatch. '${docId}' is corrupted.`)
  374. logger.error(
  375. {
  376. err: error,
  377. docId,
  378. currentVersion,
  379. newVersion,
  380. opsLength: appliedOps.length,
  381. },
  382. 'version mismatch'
  383. )
  384. return callback(error)
  385. }
  386. const jsonOps = appliedOps.map(op => JSON.stringify(op))
  387. for (const op of jsonOps) {
  388. if (op.indexOf('\u0000') !== -1) {
  389. error = new Error('null bytes found in jsonOps')
  390. // this check was added to catch memory corruption in JSON.stringify
  391. logger.error({ err: error, docId, jsonOps }, error.message)
  392. return callback(error)
  393. }
  394. }
  395. const newDocLines = JSON.stringify(docLines)
  396. if (newDocLines.indexOf('\u0000') !== -1) {
  397. error = new Error('null bytes found in doc lines')
  398. // this check was added to catch memory corruption in JSON.stringify
  399. logger.error({ err: error, docId, newDocLines }, error.message)
  400. return callback(error)
  401. }
  402. // Do an optimised size check on the docLines using the serialised
  403. // length as an upper bound
  404. const sizeBound = newDocLines.length
  405. if (docIsTooLarge(sizeBound, docLines, Settings.max_doc_length)) {
  406. const err = new Error('blocking doc update: doc is too large')
  407. const docSize = newDocLines.length
  408. logger.error({ projectId, docId, err, docSize }, err.message)
  409. return callback(err)
  410. }
  411. const newHash = RedisManager._computeHash(newDocLines)
  412. const opVersions = appliedOps.map(op => op?.v)
  413. logger.debug(
  414. {
  415. docId,
  416. version: newVersion,
  417. hash: newHash,
  418. op_versions: opVersions,
  419. },
  420. 'updating doc in redis'
  421. )
  422. // record bytes sent to redis in update
  423. metrics.summary('redis.docLines', newDocLines.length, {
  424. status: 'update',
  425. })
  426. RedisManager._serializeRanges(ranges, (error, ranges) => {
  427. if (error) {
  428. logger.error({ err: error, docId }, error.message)
  429. return callback(error)
  430. }
  431. if (ranges && ranges.indexOf('\u0000') !== -1) {
  432. error = new Error('null bytes found in ranges')
  433. // this check was added to catch memory corruption in JSON.stringify
  434. logger.error({ err: error, docId, ranges }, error.message)
  435. return callback(error)
  436. }
  437. const multi = rclient.multi()
  438. multi.mset({
  439. [keys.docLines({ doc_id: docId })]: newDocLines,
  440. [keys.docVersion({ doc_id: docId })]: newVersion,
  441. [keys.docHash({ doc_id: docId })]: newHash,
  442. [keys.ranges({ doc_id: docId })]: ranges,
  443. [keys.lastUpdatedAt({ doc_id: docId })]: Date.now(),
  444. [keys.lastUpdatedBy({ doc_id: docId })]:
  445. updateMeta && updateMeta.user_id,
  446. })
  447. multi.ltrim(
  448. keys.docOps({ doc_id: docId }),
  449. -RedisManager.DOC_OPS_MAX_LENGTH,
  450. -1
  451. ) // index 3
  452. // push the ops last so we can get the lengths at fixed index position 7
  453. if (jsonOps.length > 0) {
  454. multi.rpush(keys.docOps({ doc_id: docId }), ...jsonOps) // index 5
  455. // expire must come after rpush since before it will be a no-op if the list is empty
  456. multi.expire(
  457. keys.docOps({ doc_id: docId }),
  458. RedisManager.DOC_OPS_TTL
  459. ) // index 6
  460. if (
  461. Settings.disableTrackChanges ||
  462. projectHistoryType === 'project-history'
  463. ) {
  464. metrics.inc('history-queue', 1, { status: 'skip-track-changes' })
  465. logger.debug(
  466. { docId },
  467. 'skipping push of uncompressed ops for project using project-history'
  468. )
  469. } else {
  470. // project is using old track-changes history service
  471. metrics.inc('history-queue', 1, { status: 'track-changes' })
  472. multi.rpush(
  473. historyKeys.uncompressedHistoryOps({ doc_id: docId }),
  474. ...jsonOps
  475. ) // index 7
  476. }
  477. // Set the unflushed timestamp to the current time if the doc
  478. // hasn't been modified before (the content in mongo has been
  479. // valid up to this point). Otherwise leave it alone ("NX" flag).
  480. multi.set(keys.unflushedTime({ doc_id: docId }), Date.now(), 'NX')
  481. }
  482. multi.exec((error, result) => {
  483. if (error) {
  484. return callback(error)
  485. }
  486. let docUpdateCount
  487. if (
  488. Settings.disableTrackChanges ||
  489. projectHistoryType === 'project-history'
  490. ) {
  491. docUpdateCount = undefined // only using project history, don't bother with track-changes
  492. } else {
  493. // project is using old track-changes history service
  494. docUpdateCount = result[4]
  495. }
  496. if (jsonOps.length > 0 && Settings.apis?.project_history?.enabled) {
  497. metrics.inc('history-queue', 1, { status: 'project-history' })
  498. ProjectHistoryRedisManager.queueOps(
  499. projectId,
  500. ...jsonOps,
  501. (error, projectUpdateCount) => {
  502. if (error) {
  503. // The full project history can re-sync a project in case
  504. // updates went missing.
  505. // Just record the error here and acknowledge the write-op.
  506. metrics.inc('history-queue-error')
  507. }
  508. callback(null, docUpdateCount, projectUpdateCount)
  509. }
  510. )
  511. } else {
  512. callback(null, docUpdateCount)
  513. }
  514. })
  515. })
  516. }
  517. )
  518. },
  519. renameDoc(projectId, docId, userId, update, projectHistoryId, callback) {
  520. RedisManager.getDoc(projectId, docId, (error, lines, version) => {
  521. if (error) {
  522. return callback(error)
  523. }
  524. if (lines != null && version != null) {
  525. if (!update.newPathname) {
  526. logger.warn(
  527. { projectId, docId, update },
  528. 'missing pathname in RedisManager.renameDoc'
  529. )
  530. metrics.inc('pathname', 1, {
  531. path: 'RedisManager.renameDoc',
  532. status: update.newPathname === '' ? 'zero-length' : 'undefined',
  533. })
  534. }
  535. rclient.set(
  536. keys.pathname({ doc_id: docId }),
  537. update.newPathname,
  538. callback
  539. )
  540. } else {
  541. callback()
  542. }
  543. })
  544. },
  545. clearUnflushedTime(docId, callback) {
  546. rclient.del(keys.unflushedTime({ doc_id: docId }), callback)
  547. },
  548. getDocIdsInProject(projectId, callback) {
  549. rclient.smembers(keys.docsInProject({ project_id: projectId }), callback)
  550. },
  551. /**
  552. * Get lastupdatedat timestamps for an array of docIds
  553. */
  554. getDocTimestamps(docIds, callback) {
  555. async.mapSeries(
  556. docIds,
  557. (docId, cb) => rclient.get(keys.lastUpdatedAt({ doc_id: docId }), cb),
  558. callback
  559. )
  560. },
  561. /**
  562. * Store the project id in a sorted set ordered by time with a random offset
  563. * to smooth out spikes
  564. */
  565. queueFlushAndDeleteProject(projectId, callback) {
  566. const SMOOTHING_OFFSET =
  567. Settings.smoothingOffset > 0
  568. ? Math.round(Settings.smoothingOffset * Math.random())
  569. : 0
  570. rclient.zadd(
  571. keys.flushAndDeleteQueue(),
  572. Date.now() + SMOOTHING_OFFSET,
  573. projectId,
  574. callback
  575. )
  576. },
  577. /**
  578. * Find the oldest queued flush that is before the cutoff time
  579. */
  580. getNextProjectToFlushAndDelete(cutoffTime, callback) {
  581. rclient.zrangebyscore(
  582. keys.flushAndDeleteQueue(),
  583. 0,
  584. cutoffTime,
  585. 'WITHSCORES',
  586. 'LIMIT',
  587. 0,
  588. 1,
  589. (err, reply) => {
  590. if (err) {
  591. return callback(err)
  592. }
  593. // return if no projects ready to be processed
  594. if (!reply || reply.length === 0) {
  595. return callback()
  596. }
  597. // pop the oldest entry (get and remove in a multi)
  598. const multi = rclient.multi()
  599. // Poor man's version of ZPOPMIN, which is only available in Redis 5.
  600. multi.zrange(keys.flushAndDeleteQueue(), 0, 0, 'WITHSCORES')
  601. multi.zremrangebyrank(keys.flushAndDeleteQueue(), 0, 0)
  602. multi.zcard(keys.flushAndDeleteQueue()) // the total length of the queue (for metrics)
  603. multi.exec((err, reply) => {
  604. if (err) {
  605. return callback(err)
  606. }
  607. if (!reply || reply.length === 0) {
  608. return callback()
  609. }
  610. const [key, timestamp] = reply[0]
  611. const queueLength = reply[2]
  612. callback(null, key, timestamp, queueLength)
  613. })
  614. }
  615. )
  616. },
  617. _serializeRanges(ranges, callback) {
  618. let jsonRanges = JSON.stringify(ranges)
  619. if (jsonRanges && jsonRanges.length > MAX_RANGES_SIZE) {
  620. return callback(new Error('ranges are too large'))
  621. }
  622. if (jsonRanges === '{}') {
  623. // Most doc will have empty ranges so don't fill redis with lots of '{}' keys
  624. jsonRanges = null
  625. }
  626. callback(null, jsonRanges)
  627. },
  628. _deserializeRanges(ranges) {
  629. if (ranges == null || ranges === '') {
  630. return {}
  631. } else {
  632. return JSON.parse(ranges)
  633. }
  634. },
  635. _computeHash(docLines) {
  636. // use sha1 checksum of doclines to detect data corruption.
  637. //
  638. // note: must specify 'utf8' encoding explicitly, as the default is
  639. // binary in node < v5
  640. return crypto.createHash('sha1').update(docLines, 'utf8').digest('hex')
  641. },
  642. }