Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Switch from using mtime to ctime for cache validation. [closes #746]
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Mar 23, 2019
1 parent 86123b5 commit b8e2f7b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ if (cachePath !== "") {
circular: 0,
code: null,
codeWithTDZ: null,
ctime: -1,
filename: null,
firstAwaitOutsideFunction: null,
mtime: -1,
scriptData,
sourceType: 1,
transforms: 0,
Expand Down
12 changes: 6 additions & 6 deletions src/caching-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ function init() {
circular: 0,
code: null,
codeWithTDZ: null,
ctime: -1,
filename: null,
firstAwaitOutsideFunction: null,
mtime: -1,
scriptData: null,
sourceType: SOURCE_TYPE_SCRIPT,
transforms: 0,
Expand All @@ -90,7 +90,7 @@ function init() {
}
}

result.mtime = +meta[3]
result.ctime = +meta[3]
result.sourceType = +meta[4]
result.transforms = +meta[2]
}
Expand Down Expand Up @@ -123,8 +123,8 @@ function init() {
return result
}

result.ctime = options.ctime
result.filename = options.filename
result.mtime = options.mtime

return result
}
Expand Down Expand Up @@ -293,9 +293,9 @@ function init() {

if (compileData !== null) {
const {
ctime,
filename,
firstAwaitOutsideFunction,
mtime,
sourceType,
transforms
} = compileData
Expand All @@ -313,7 +313,7 @@ function init() {
if (transforms !== 0) {
meta.push(
transforms,
mtime,
ctime,
sourceType,
deflatedFirstAwaitOutsideFunction,
relative(cachePath, filename)
Expand All @@ -322,7 +322,7 @@ function init() {
} else {
meta.push(
transforms,
mtime,
ctime,
sourceType,
deflatedFirstAwaitOutsideFunction,
relative(cachePath, filename),
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ function init() {
circular: 0,
code,
codeWithTDZ: null,
ctime: -1,
filename: null,
firstAwaitOutsideFunction: null,
mtime: -1,
scriptData: null,
sourceType: SOURCE_TYPE_SCRIPT,
transforms: 0,
Expand Down
10 changes: 5 additions & 5 deletions src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import encodeId from "./util/encode-id.js"
import errors from "./errors.js"
import getCacheName from "./util/get-cache-name.js"
import getCacheStateHash from "./util/get-cache-state-hash.js"
import getCtime from "./fs/get-ctime.js"
import getModuleDirname from "./util/get-module-dirname.js"
import getModuleName from "./util/get-module-name.js"
import getMtime from "./fs/get-mtime.js"
import getPrototypeOf from "./util/get-prototype-of.js"
import has from "./util/has.js"
import isCalledFromStrictCode from "./util/is-called-from-strict-code.js"
Expand Down Expand Up @@ -155,7 +155,7 @@ class Entry {
setDeferred(this, "cacheName", () => {
const pkg = this.package

return getCacheName(this.mtime, {
return getCacheName(this.ctime, {
cachePath: pkg.cachePath,
filename: this.filename,
packageOptions: pkg.options
Expand Down Expand Up @@ -188,9 +188,9 @@ class Entry {
return createImmutableNamespaceProxy(this, this._completeNamespace)
})

// The mtime of the module.
setDeferred(this, "mtime", () => {
return getMtime(this.filename)
// The ctime of the module.
setDeferred(this, "ctime", () => {
return getCtime(this.filename)
})

// The mutable namespace object that non-ESM importers receive.
Expand Down
18 changes: 8 additions & 10 deletions src/fs/get-mtime.js → src/fs/get-ctime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@ import shared from "../shared.js"
import statSync from "../fs/stat-sync.js"

function init() {
const { round } = Math

function getMtime(filename) {
function getCtime(filename) {
if (typeof filename === "string") {
try {
const stat = statSync(filename)
const { mtimeMs } = stat
const { ctimeMs } = stat

// Add 0.5 to avoid rounding down.
// https://github.com/nodejs/node/pull/12607
return typeof mtimeMs === "number"
? round(mtimeMs + 0.5)
: GenericDate.getTime(stat.mtime)
return typeof ctimeMs === "number"
? Math.round(ctimeMs + 0.5)
: GenericDate.getTime(stat.ctime)
} catch {}
}

return -1
}

return getMtime
return getCtime
}

export default shared.inited
? shared.module.fsGetMtime
: shared.module.fsGetMtime = init()
? shared.module.fsGetCtime
: shared.module.fsGetCtime = init()
4 changes: 2 additions & 2 deletions src/module/internal/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ function compile(caller, entry, content, filename, fallback) {
circular: 0,
code: null,
codeWithTDZ: null,
ctime: -1,
filename: null,
firstAwaitOutsideFunction: null,
mtime: -1,
scriptData: null,
sourceType: hint,
transforms: 0,
Expand Down Expand Up @@ -165,9 +165,9 @@ function compile(caller, entry, content, filename, fallback) {
cachePath: pkg.cachePath,
cjsPaths,
cjsVars,
ctime: entry.ctime,
filename,
hint,
mtime: entry.mtime,
runtimeName: entry.runtimeName,
sourceType,
topLevelReturn
Expand Down

0 comments on commit b8e2f7b

Please sign in to comment.